The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 17 | #include "builtins.h" |
| 18 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | #include <errno.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 21 | #include <mntent.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 22 | #include <net/if.h> |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 23 | #include <signal.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | #include <stdlib.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <sys/socket.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | #include <sys/mount.h> |
| 29 | #include <sys/resource.h> |
Elliott Hughes | 3d74d7a | 2015-01-29 21:31:23 -0800 | [diff] [blame] | 30 | #include <sys/time.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 33 | #include <sys/wait.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 34 | #include <unistd.h> |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 35 | #include <linux/loop.h> |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 36 | #include <ext4_crypt_init_extensions.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 38 | #include <selinux/selinux.h> |
| 39 | #include <selinux/label.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 40 | |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 41 | #include <fs_mgr.h> |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 42 | #include <android-base/parseint.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 43 | #include <android-base/stringprintf.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 44 | #include <cutils/partition_utils.h> |
| 45 | #include <cutils/android_reboot.h> |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 46 | #include <logwrap/logwrap.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 47 | #include <private/android_filesystem_config.h> |
| 48 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 49 | #include "action.h" |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 50 | #include "bootchart.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | #include "devices.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 52 | #include "init.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 53 | #include "init_parser.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 54 | #include "log.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 55 | #include "property_service.h" |
| 56 | #include "service.h" |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 57 | #include "signal_handler.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 58 | #include "util.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 59 | |
Nick Kralevich | bc60954 | 2015-01-31 21:39:46 -0800 | [diff] [blame] | 60 | #define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 61 | #define UNMOUNT_CHECK_MS 5000 |
| 62 | #define UNMOUNT_CHECK_TIMES 10 |
Nick Kralevich | bc60954 | 2015-01-31 21:39:46 -0800 | [diff] [blame] | 63 | |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 64 | // System call provided by bionic but not in any header file. |
| 65 | extern "C" int init_module(void *, unsigned long, const char *); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 67 | static const int kTerminateServiceDelayMicroSeconds = 50000; |
| 68 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 69 | static int insmod(const char *filename, const char *options) { |
Tom Cherry | eaa3b4e | 2015-05-12 13:54:41 -0700 | [diff] [blame] | 70 | std::string module; |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 71 | if (!read_file(filename, &module)) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | return -1; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 73 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 75 | // TODO: use finit_module for >= 3.8 kernels. |
| 76 | return init_module(&module[0], module.size(), options); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 79 | static int __ifupdown(const char *interface, int up) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 80 | struct ifreq ifr; |
| 81 | int s, ret; |
| 82 | |
| 83 | strlcpy(ifr.ifr_name, interface, IFNAMSIZ); |
| 84 | |
| 85 | s = socket(AF_INET, SOCK_DGRAM, 0); |
| 86 | if (s < 0) |
| 87 | return -1; |
| 88 | |
| 89 | ret = ioctl(s, SIOCGIFFLAGS, &ifr); |
| 90 | if (ret < 0) { |
| 91 | goto done; |
| 92 | } |
| 93 | |
| 94 | if (up) |
| 95 | ifr.ifr_flags |= IFF_UP; |
| 96 | else |
| 97 | ifr.ifr_flags &= ~IFF_UP; |
| 98 | |
| 99 | ret = ioctl(s, SIOCSIFFLAGS, &ifr); |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 100 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | done: |
| 102 | close(s); |
| 103 | return ret; |
| 104 | } |
| 105 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 106 | static void unmount_and_fsck(const struct mntent *entry) { |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 107 | if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4")) |
| 108 | return; |
| 109 | |
| 110 | /* First, lazily unmount the directory. This unmount request finishes when |
| 111 | * all processes that open a file or directory in |entry->mnt_dir| exit. |
| 112 | */ |
| 113 | TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH)); |
| 114 | |
| 115 | /* Next, kill all processes except init, kthreadd, and kthreadd's |
| 116 | * children to finish the lazy unmount. Killing all processes here is okay |
| 117 | * because this callback function is only called right before reboot(). |
| 118 | * It might be cleaner to selectively kill processes that actually use |
| 119 | * |entry->mnt_dir| rather than killing all, probably by reusing a function |
| 120 | * like killProcessesWithOpenFiles() in vold/, but the selinux policy does |
| 121 | * not allow init to scan /proc/<pid> files which the utility function |
| 122 | * heavily relies on. The policy does not allow the process to execute |
| 123 | * killall/pkill binaries either. Note that some processes might |
| 124 | * automatically restart after kill(), but that is not really a problem |
| 125 | * because |entry->mnt_dir| is no longer visible to such new processes. |
| 126 | */ |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 127 | ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); }); |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 128 | TEMP_FAILURE_RETRY(kill(-1, SIGKILL)); |
| 129 | |
| 130 | int count = 0; |
| 131 | while (count++ < UNMOUNT_CHECK_TIMES) { |
| 132 | int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL)); |
| 133 | if (fd >= 0) { |
| 134 | /* |entry->mnt_dir| has sucessfully been unmounted. */ |
| 135 | close(fd); |
| 136 | break; |
| 137 | } else if (errno == EBUSY) { |
| 138 | /* Some processes using |entry->mnt_dir| are still alive. Wait for a |
| 139 | * while then retry. |
| 140 | */ |
| 141 | TEMP_FAILURE_RETRY( |
| 142 | usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES)); |
| 143 | continue; |
| 144 | } else { |
| 145 | /* Cannot open the device. Give up. */ |
| 146 | return; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | int st; |
| 151 | if (!strcmp(entry->mnt_type, "f2fs")) { |
| 152 | const char *f2fs_argv[] = { |
| 153 | "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname, |
| 154 | }; |
| 155 | android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv, |
Yusuke Sato | d81c3c6 | 2015-08-14 01:22:53 -0700 | [diff] [blame] | 156 | &st, true, LOG_KLOG, true, NULL, NULL, 0); |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 157 | } else if (!strcmp(entry->mnt_type, "ext4")) { |
| 158 | const char *ext4_argv[] = { |
| 159 | "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname, |
| 160 | }; |
| 161 | android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv, |
Yusuke Sato | d81c3c6 | 2015-08-14 01:22:53 -0700 | [diff] [blame] | 162 | &st, true, LOG_KLOG, true, NULL, NULL, 0); |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 166 | static int do_class_start(const std::vector<std::string>& args) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 167 | /* Starting a class does not start services |
| 168 | * which are explicitly disabled. They must |
| 169 | * be started individually. |
| 170 | */ |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 171 | ServiceManager::GetInstance(). |
| 172 | ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); }); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 176 | static int do_class_stop(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 177 | ServiceManager::GetInstance(). |
| 178 | ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); }); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 182 | static int do_class_reset(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 183 | ServiceManager::GetInstance(). |
| 184 | ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); }); |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 188 | static int do_domainname(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 189 | return write_file("/proc/sys/kernel/domainname", args[1].c_str()); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 192 | static int do_enable(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 193 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); |
| 194 | if (!svc) { |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 195 | return -1; |
| 196 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 197 | return svc->Enable(); |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 200 | static int do_exec(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 201 | Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args); |
| 202 | if (!svc) { |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 203 | return -1; |
| 204 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 205 | if (!svc->Start()) { |
| 206 | return -1; |
| 207 | } |
| 208 | waiting_for_exec = true; |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 209 | return 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 212 | static int do_export(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 213 | return add_environment(args[1].c_str(), args[2].c_str()); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 216 | static int do_hostname(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 217 | return write_file("/proc/sys/kernel/hostname", args[1].c_str()); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 220 | static int do_ifup(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 221 | return __ifupdown(args[1].c_str(), 1); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 224 | static int do_insmod(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 225 | std::string options; |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 226 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 227 | if (args.size() > 2) { |
| 228 | options += args[2]; |
| 229 | for (std::size_t i = 3; i < args.size(); ++i) { |
| 230 | options += ' '; |
| 231 | options += args[i]; |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 235 | return insmod(args[1].c_str(), options.c_str()); |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 238 | static int do_mkdir(const std::vector<std::string>& args) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 239 | mode_t mode = 0755; |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 240 | int ret; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 241 | |
| 242 | /* mkdir <path> [mode] [owner] [group] */ |
| 243 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 244 | if (args.size() >= 3) { |
| 245 | mode = std::stoul(args[2], 0, 8); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 248 | ret = make_dir(args[1].c_str(), mode); |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 249 | /* chmod in case the directory already exists */ |
| 250 | if (ret == -1 && errno == EEXIST) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 251 | ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW); |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 252 | } |
| 253 | if (ret == -1) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 254 | return -errno; |
| 255 | } |
| 256 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 257 | if (args.size() >= 4) { |
| 258 | uid_t uid = decode_uid(args[3].c_str()); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 259 | gid_t gid = -1; |
| 260 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 261 | if (args.size() == 5) { |
| 262 | gid = decode_uid(args[4].c_str()); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 265 | if (lchown(args[1].c_str(), uid, gid) == -1) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 266 | return -errno; |
| 267 | } |
Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 268 | |
| 269 | /* chown may have cleared S_ISUID and S_ISGID, chmod again */ |
| 270 | if (mode & (S_ISUID | S_ISGID)) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 271 | ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW); |
Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 272 | if (ret == -1) { |
| 273 | return -errno; |
| 274 | } |
| 275 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 278 | return e4crypt_set_directory_policy(args[1].c_str()); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static struct { |
| 282 | const char *name; |
| 283 | unsigned flag; |
| 284 | } mount_flags[] = { |
| 285 | { "noatime", MS_NOATIME }, |
Lars Svensson | b6ee25e | 2011-07-14 13:39:09 +0200 | [diff] [blame] | 286 | { "noexec", MS_NOEXEC }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 287 | { "nosuid", MS_NOSUID }, |
| 288 | { "nodev", MS_NODEV }, |
| 289 | { "nodiratime", MS_NODIRATIME }, |
| 290 | { "ro", MS_RDONLY }, |
| 291 | { "rw", 0 }, |
| 292 | { "remount", MS_REMOUNT }, |
Jeff Sharkey | e50ac5f | 2012-08-14 11:34:34 -0700 | [diff] [blame] | 293 | { "bind", MS_BIND }, |
| 294 | { "rec", MS_REC }, |
| 295 | { "unbindable", MS_UNBINDABLE }, |
| 296 | { "private", MS_PRIVATE }, |
| 297 | { "slave", MS_SLAVE }, |
| 298 | { "shared", MS_SHARED }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 299 | { "defaults", 0 }, |
| 300 | { 0, 0 }, |
| 301 | }; |
| 302 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 303 | #define DATA_MNT_POINT "/data" |
| 304 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 305 | /* mount <type> <device> <path> <flags ...> <options> */ |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 306 | static int do_mount(const std::vector<std::string>& args) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 307 | char tmp[64]; |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 308 | const char *source, *target, *system; |
| 309 | const char *options = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 310 | unsigned flags = 0; |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 311 | std::size_t na = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 312 | int n, i; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 313 | int wait = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 314 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 315 | for (na = 4; na < args.size(); na++) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 316 | for (i = 0; mount_flags[i].name; i++) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 317 | if (!args[na].compare(mount_flags[i].name)) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 318 | flags |= mount_flags[i].flag; |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 323 | if (!mount_flags[i].name) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 324 | if (!args[na].compare("wait")) |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 325 | wait = 1; |
| 326 | /* if our last argument isn't a flag, wolf it up as an option string */ |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 327 | else if (na + 1 == args.size()) |
| 328 | options = args[na].c_str(); |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 329 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 332 | system = args[1].c_str(); |
| 333 | source = args[2].c_str(); |
| 334 | target = args[3].c_str(); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 335 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 336 | if (!strncmp(source, "mtd@", 4)) { |
| 337 | n = mtd_name_to_number(source + 4); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 338 | if (n < 0) { |
| 339 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 340 | } |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 341 | |
Yabin Cui | e2d63af | 2015-02-17 19:27:51 -0800 | [diff] [blame] | 342 | snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 343 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 344 | if (wait) |
| 345 | wait_for_file(tmp, COMMAND_RETRY_TIMEOUT); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 346 | if (mount(tmp, target, system, flags, options) < 0) { |
| 347 | return -1; |
| 348 | } |
| 349 | |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 350 | goto exit_success; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 351 | } else if (!strncmp(source, "loop@", 5)) { |
| 352 | int mode, loop, fd; |
| 353 | struct loop_info info; |
| 354 | |
| 355 | mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 356 | fd = open(source + 5, mode | O_CLOEXEC); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 357 | if (fd < 0) { |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | for (n = 0; ; n++) { |
Yabin Cui | e2d63af | 2015-02-17 19:27:51 -0800 | [diff] [blame] | 362 | snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n); |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 363 | loop = open(tmp, mode | O_CLOEXEC); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 364 | if (loop < 0) { |
Tomasz Kondel | bfdcc40 | 2014-02-06 08:57:27 +0100 | [diff] [blame] | 365 | close(fd); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 366 | return -1; |
| 367 | } |
| 368 | |
| 369 | /* if it is a blank loop device */ |
| 370 | if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { |
| 371 | /* if it becomes our loop device */ |
| 372 | if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { |
| 373 | close(fd); |
| 374 | |
| 375 | if (mount(tmp, target, system, flags, options) < 0) { |
| 376 | ioctl(loop, LOOP_CLR_FD, 0); |
| 377 | close(loop); |
| 378 | return -1; |
| 379 | } |
| 380 | |
| 381 | close(loop); |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 382 | goto exit_success; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | close(loop); |
| 387 | } |
| 388 | |
| 389 | close(fd); |
| 390 | ERROR("out of loopback devices"); |
| 391 | return -1; |
| 392 | } else { |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 393 | if (wait) |
| 394 | wait_for_file(source, COMMAND_RETRY_TIMEOUT); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 395 | if (mount(source, target, system, flags, options) < 0) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 396 | return -1; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 397 | } |
| 398 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 399 | } |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 400 | |
| 401 | exit_success: |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 402 | return 0; |
| 403 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 406 | static int wipe_data_via_recovery() { |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 407 | mkdir("/cache/recovery", 0700); |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 408 | int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600); |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 409 | if (fd >= 0) { |
Jeff Sharkey | d26135b | 2014-09-24 11:46:36 -0700 | [diff] [blame] | 410 | write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1); |
| 411 | write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1); |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 412 | close(fd); |
| 413 | } else { |
| 414 | ERROR("could not open /cache/recovery/command\n"); |
| 415 | return -1; |
| 416 | } |
| 417 | android_reboot(ANDROID_RB_RESTART2, 0, "recovery"); |
| 418 | while (1) { pause(); } // never reached |
| 419 | } |
| 420 | |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 421 | /* Imports .rc files from the specified paths. Default ones are applied if none is given. |
| 422 | * |
| 423 | * start_index: index of the first path in the args list |
| 424 | */ |
| 425 | static void import_late(const std::vector<std::string>& args, size_t start_index) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 426 | Parser& parser = Parser::GetInstance(); |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 427 | if (args.size() <= start_index) { |
| 428 | // Use the default set if no path is given |
| 429 | static const std::vector<std::string> init_directories = { |
| 430 | "/system/etc/init", |
| 431 | "/vendor/etc/init", |
| 432 | "/odm/etc/init" |
| 433 | }; |
| 434 | |
| 435 | for (const auto& dir : init_directories) { |
| 436 | parser.ParseConfig(dir); |
| 437 | } |
| 438 | } else { |
| 439 | for (size_t i = start_index; i < args.size(); ++i) { |
| 440 | parser.ParseConfig(args[i]); |
| 441 | } |
Tom Cherry | b8dd027 | 2015-07-22 14:23:33 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 445 | /* mount_all <fstab> [ <path> ]* |
| 446 | * |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 447 | * This function might request a reboot, in which case it will |
| 448 | * not return. |
| 449 | */ |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 450 | static int do_mount_all(const std::vector<std::string>& args) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 451 | pid_t pid; |
| 452 | int ret = -1; |
| 453 | int child_ret = -1; |
| 454 | int status; |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 455 | struct fstab *fstab; |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 456 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 457 | const char* fstabfile = args[1].c_str(); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 458 | /* |
| 459 | * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and |
| 460 | * do the call in the child to provide protection to the main init |
| 461 | * process if anything goes wrong (crash or memory leak), and wait for |
| 462 | * the child to finish in the parent. |
| 463 | */ |
| 464 | pid = fork(); |
| 465 | if (pid > 0) { |
| 466 | /* Parent. Wait for the child to return */ |
Paul Lawrence | 40af092 | 2014-09-16 14:31:23 -0700 | [diff] [blame] | 467 | int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 468 | if (wp_ret < 0) { |
| 469 | /* Unexpected error code. We will continue anyway. */ |
Elliott Hughes | cd67f00 | 2015-03-20 17:05:56 -0700 | [diff] [blame] | 470 | NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno)); |
Paul Lawrence | 40af092 | 2014-09-16 14:31:23 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 473 | if (WIFEXITED(status)) { |
| 474 | ret = WEXITSTATUS(status); |
| 475 | } else { |
| 476 | ret = -1; |
| 477 | } |
| 478 | } else if (pid == 0) { |
| 479 | /* child, call fs_mgr_mount_all() */ |
| 480 | klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */ |
Nan Liu | 12df1e1 | 2015-07-21 19:44:07 +0800 | [diff] [blame] | 481 | fstab = fs_mgr_read_fstab(fstabfile); |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 482 | child_ret = fs_mgr_mount_all(fstab); |
| 483 | fs_mgr_free_fstab(fstab); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 484 | if (child_ret == -1) { |
| 485 | ERROR("fs_mgr_mount_all returned an error\n"); |
| 486 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 487 | _exit(child_ret); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 488 | } else { |
| 489 | /* fork failed, return an error */ |
| 490 | return -1; |
| 491 | } |
| 492 | |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 493 | /* Paths of .rc files are specified at the 2nd argument and beyond */ |
| 494 | import_late(args, 2); |
Tom Cherry | b8dd027 | 2015-07-22 14:23:33 -0700 | [diff] [blame] | 495 | |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 496 | if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) { |
Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 497 | property_set("vold.decrypt", "trigger_encryption"); |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 498 | } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 499 | property_set("ro.crypto.state", "encrypted"); |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 500 | property_set("ro.crypto.type", "block"); |
Paul Lawrence | 13d5bb4 | 2014-01-30 10:43:52 -0800 | [diff] [blame] | 501 | property_set("vold.decrypt", "trigger_default_encryption"); |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 502 | } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 503 | property_set("ro.crypto.state", "unencrypted"); |
| 504 | /* If fs_mgr determined this is an unencrypted device, then trigger |
| 505 | * that action. |
| 506 | */ |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 507 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 508 | } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) { |
| 509 | /* Setup a wipe via recovery, and reboot into recovery */ |
| 510 | ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n"); |
| 511 | ret = wipe_data_via_recovery(); |
| 512 | /* If reboot worked, there is no return. */ |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 513 | } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) { |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 514 | if (e4crypt_install_keyring()) { |
| 515 | return -1; |
| 516 | } |
| 517 | property_set("ro.crypto.state", "encrypted"); |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 518 | property_set("ro.crypto.type", "file"); |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 519 | |
| 520 | // Although encrypted, we have device key, so we do not need to |
| 521 | // do anything different from the nonencrypted case. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 522 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 523 | } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) { |
| 524 | if (e4crypt_install_keyring()) { |
| 525 | return -1; |
| 526 | } |
| 527 | property_set("ro.crypto.state", "encrypted"); |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 528 | property_set("ro.crypto.type", "file"); |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 529 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 530 | } else if (ret > 0) { |
| 531 | ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 532 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 533 | /* else ... < 0: error */ |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 534 | |
| 535 | return ret; |
| 536 | } |
| 537 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 538 | static int do_swapon_all(const std::vector<std::string>& args) { |
Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 539 | struct fstab *fstab; |
| 540 | int ret; |
| 541 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 542 | fstab = fs_mgr_read_fstab(args[1].c_str()); |
Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 543 | ret = fs_mgr_swapon_all(fstab); |
| 544 | fs_mgr_free_fstab(fstab); |
| 545 | |
| 546 | return ret; |
| 547 | } |
| 548 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 549 | static int do_setprop(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 550 | const char* name = args[1].c_str(); |
| 551 | const char* value = args[2].c_str(); |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 552 | property_set(name, value); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 556 | static int do_setrlimit(const std::vector<std::string>& args) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 557 | struct rlimit limit; |
| 558 | int resource; |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 559 | resource = std::stoi(args[1]); |
| 560 | limit.rlim_cur = std::stoi(args[2]); |
| 561 | limit.rlim_max = std::stoi(args[3]); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 562 | return setrlimit(resource, &limit); |
| 563 | } |
| 564 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 565 | static int do_start(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 566 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); |
| 567 | if (!svc) { |
| 568 | ERROR("do_start: Service %s not found\n", args[1].c_str()); |
| 569 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 570 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 571 | if (!svc->Start()) |
| 572 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 576 | static int do_stop(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 577 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); |
| 578 | if (!svc) { |
| 579 | ERROR("do_stop: Service %s not found\n", args[1].c_str()); |
| 580 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 581 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 582 | svc->Stop(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 583 | return 0; |
| 584 | } |
| 585 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 586 | static int do_restart(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 587 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); |
| 588 | if (!svc) { |
| 589 | ERROR("do_restart: Service %s not found\n", args[1].c_str()); |
| 590 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 591 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 592 | svc->Restart(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 596 | static int do_powerctl(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 597 | const char* command = args[1].c_str(); |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 598 | int len = 0; |
Yusuke Sato | f93d429 | 2015-07-21 15:50:59 -0700 | [diff] [blame] | 599 | unsigned int cmd = 0; |
| 600 | const char *reboot_target = ""; |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 601 | void (*callback_on_ro_remount)(const struct mntent*) = NULL; |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 602 | |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 603 | if (strncmp(command, "shutdown", 8) == 0) { |
| 604 | cmd = ANDROID_RB_POWEROFF; |
| 605 | len = 8; |
| 606 | } else if (strncmp(command, "reboot", 6) == 0) { |
| 607 | cmd = ANDROID_RB_RESTART2; |
| 608 | len = 6; |
| 609 | } else { |
| 610 | ERROR("powerctl: unrecognized command '%s'\n", command); |
| 611 | return -EINVAL; |
| 612 | } |
| 613 | |
| 614 | if (command[len] == ',') { |
Yusuke Sato | f93d429 | 2015-07-21 15:50:59 -0700 | [diff] [blame] | 615 | if (cmd == ANDROID_RB_POWEROFF && |
| 616 | !strcmp(&command[len + 1], "userrequested")) { |
| 617 | // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED. |
| 618 | // Run fsck once the file system is remounted in read-only mode. |
| 619 | callback_on_ro_remount = unmount_and_fsck; |
| 620 | } else if (cmd == ANDROID_RB_RESTART2) { |
| 621 | reboot_target = &command[len + 1]; |
| 622 | } |
| 623 | } else if (command[len] != '\0') { |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 624 | ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]); |
| 625 | return -EINVAL; |
| 626 | } |
| 627 | |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 628 | std::string timeout = property_get("ro.build.shutdown_timeout"); |
| 629 | unsigned int delay = 0; |
| 630 | |
| 631 | if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) { |
| 632 | Timer t; |
| 633 | // Ask all services to terminate. |
| 634 | ServiceManager::GetInstance().ForEachService( |
| 635 | [] (Service* s) { s->Terminate(); }); |
| 636 | |
| 637 | while (t.duration() < delay) { |
| 638 | ServiceManager::GetInstance().ReapAnyOutstandingChildren(); |
| 639 | |
| 640 | int service_count = 0; |
| 641 | ServiceManager::GetInstance().ForEachService( |
| 642 | [&service_count] (Service* s) { |
| 643 | // Count the number of services running. |
| 644 | // Exclude the console as it will ignore the SIGTERM signal |
| 645 | // and not exit. |
| 646 | // Note: SVC_CONSOLE actually means "requires console" but |
| 647 | // it is only used by the shell. |
| 648 | if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) { |
| 649 | service_count++; |
| 650 | } |
| 651 | }); |
| 652 | |
| 653 | if (service_count == 0) { |
| 654 | // All terminable services terminated. We can exit early. |
| 655 | break; |
| 656 | } |
| 657 | |
| 658 | // Wait a bit before recounting the number or running services. |
| 659 | usleep(kTerminateServiceDelayMicroSeconds); |
| 660 | } |
| 661 | NOTICE("Terminating running services took %.02f seconds", t.duration()); |
| 662 | } |
| 663 | |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 664 | return android_reboot_with_callback(cmd, 0, reboot_target, |
| 665 | callback_on_ro_remount); |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 668 | static int do_trigger(const std::vector<std::string>& args) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 669 | ActionManager::GetInstance().QueueEventTrigger(args[1]); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 670 | return 0; |
| 671 | } |
| 672 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 673 | static int do_symlink(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 674 | return symlink(args[1].c_str(), args[2].c_str()); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 677 | static int do_rm(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 678 | return unlink(args[1].c_str()); |
Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 679 | } |
| 680 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 681 | static int do_rmdir(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 682 | return rmdir(args[1].c_str()); |
Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 683 | } |
| 684 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 685 | static int do_sysclktz(const std::vector<std::string>& args) { |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 686 | struct timezone tz; |
| 687 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 688 | memset(&tz, 0, sizeof(tz)); |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 689 | tz.tz_minuteswest = std::stoi(args[1]); |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 690 | if (settimeofday(NULL, &tz)) |
| 691 | return -1; |
| 692 | return 0; |
| 693 | } |
| 694 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 695 | static int do_verity_load_state(const std::vector<std::string>& args) { |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 696 | int mode = -1; |
| 697 | int rc = fs_mgr_load_verity_state(&mode); |
Sami Tolvanen | 90f52df | 2015-12-02 14:23:09 +0000 | [diff] [blame] | 698 | if (rc == 0 && mode != VERITY_MODE_DEFAULT) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 699 | ActionManager::GetInstance().QueueEventTrigger("verity-logging"); |
Sami Tolvanen | 8ff0190 | 2015-02-16 11:03:34 +0000 | [diff] [blame] | 700 | } |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 701 | return rc; |
Sami Tolvanen | 8ff0190 | 2015-02-16 11:03:34 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 704 | static void verity_update_property(fstab_rec *fstab, const char *mount_point, |
| 705 | int mode, int status) { |
Sami Tolvanen | 4547423 | 2015-03-30 11:38:38 +0100 | [diff] [blame] | 706 | property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(), |
| 707 | android::base::StringPrintf("%d", mode).c_str()); |
Sami Tolvanen | acbf9be | 2015-03-19 10:00:34 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 710 | static int do_verity_update_state(const std::vector<std::string>& args) { |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 711 | return fs_mgr_update_verity_state(verity_update_property); |
Sami Tolvanen | acbf9be | 2015-03-19 10:00:34 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 714 | static int do_write(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 715 | const char* path = args[1].c_str(); |
| 716 | const char* value = args[2].c_str(); |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 717 | return write_file(path, value); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 720 | static int do_copy(const std::vector<std::string>& args) { |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 721 | char *buffer = NULL; |
| 722 | int rc = 0; |
| 723 | int fd1 = -1, fd2 = -1; |
| 724 | struct stat info; |
| 725 | int brtw, brtr; |
| 726 | char *p; |
| 727 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 728 | if (stat(args[1].c_str(), &info) < 0) |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 729 | return -1; |
| 730 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 731 | if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0) |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 732 | goto out_err; |
| 733 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 734 | if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0) |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 735 | goto out_err; |
| 736 | |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 737 | if (!(buffer = (char*) malloc(info.st_size))) |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 738 | goto out_err; |
| 739 | |
| 740 | p = buffer; |
| 741 | brtr = info.st_size; |
| 742 | while(brtr) { |
| 743 | rc = read(fd1, p, brtr); |
| 744 | if (rc < 0) |
| 745 | goto out_err; |
| 746 | if (rc == 0) |
| 747 | break; |
| 748 | p += rc; |
| 749 | brtr -= rc; |
| 750 | } |
| 751 | |
| 752 | p = buffer; |
| 753 | brtw = info.st_size; |
| 754 | while(brtw) { |
| 755 | rc = write(fd2, p, brtw); |
| 756 | if (rc < 0) |
| 757 | goto out_err; |
| 758 | if (rc == 0) |
| 759 | break; |
| 760 | p += rc; |
| 761 | brtw -= rc; |
| 762 | } |
| 763 | |
| 764 | rc = 0; |
| 765 | goto out; |
| 766 | out_err: |
| 767 | rc = -1; |
| 768 | out: |
| 769 | if (buffer) |
| 770 | free(buffer); |
| 771 | if (fd1 >= 0) |
| 772 | close(fd1); |
| 773 | if (fd2 >= 0) |
| 774 | close(fd2); |
| 775 | return rc; |
| 776 | } |
| 777 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 778 | static int do_chown(const std::vector<std::string>& args) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 779 | /* GID is optional. */ |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 780 | if (args.size() == 3) { |
| 781 | if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 782 | return -errno; |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 783 | } else if (args.size() == 4) { |
| 784 | if (lchown(args[3].c_str(), decode_uid(args[1].c_str()), |
| 785 | decode_uid(args[2].c_str())) == -1) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 786 | return -errno; |
| 787 | } else { |
| 788 | return -1; |
| 789 | } |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | static mode_t get_mode(const char *s) { |
| 794 | mode_t mode = 0; |
| 795 | while (*s) { |
| 796 | if (*s >= '0' && *s <= '7') { |
| 797 | mode = (mode<<3) | (*s-'0'); |
| 798 | } else { |
| 799 | return -1; |
| 800 | } |
| 801 | s++; |
| 802 | } |
| 803 | return mode; |
| 804 | } |
| 805 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 806 | static int do_chmod(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 807 | mode_t mode = get_mode(args[1].c_str()); |
| 808 | if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 809 | return -errno; |
| 810 | } |
| 811 | return 0; |
| 812 | } |
| 813 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 814 | static int do_restorecon(const std::vector<std::string>& args) { |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 815 | int ret = 0; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 816 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 817 | for (auto it = std::next(args.begin()); it != args.end(); ++it) { |
| 818 | if (restorecon(it->c_str()) < 0) |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 819 | ret = -errno; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 820 | } |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 821 | return ret; |
| 822 | } |
| 823 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 824 | static int do_restorecon_recursive(const std::vector<std::string>& args) { |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 825 | int ret = 0; |
| 826 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 827 | for (auto it = std::next(args.begin()); it != args.end(); ++it) { |
| 828 | if (restorecon_recursive(it->c_str()) < 0) |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 829 | ret = -errno; |
| 830 | } |
| 831 | return ret; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 832 | } |
| 833 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 834 | static int do_loglevel(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 835 | int log_level = std::stoi(args[1]); |
Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 836 | if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) { |
| 837 | ERROR("loglevel: invalid log level'%d'\n", log_level); |
| 838 | return -EINVAL; |
| 839 | } |
| 840 | klog_set_level(log_level); |
| 841 | return 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 844 | static int do_load_persist_props(const std::vector<std::string>& args) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 845 | load_persist_props(); |
| 846 | return 0; |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 847 | } |
| 848 | |
Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 849 | static int do_load_system_props(const std::vector<std::string>& args) { |
| 850 | load_system_props(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 851 | return 0; |
Riley Andrews | e4b7b29 | 2014-06-16 15:06:21 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 854 | static int do_wait(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 855 | if (args.size() == 2) { |
| 856 | return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT); |
| 857 | } else if (args.size() == 3) { |
| 858 | return wait_for_file(args[1].c_str(), std::stoi(args[2])); |
Patrick McCormick | 96d0a4d | 2011-02-04 10:51:39 -0800 | [diff] [blame] | 859 | } else |
| 860 | return -1; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 861 | } |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 862 | |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 863 | /* |
| 864 | * Callback to make a directory from the ext4 code |
| 865 | */ |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 866 | static int do_installkeys_ensure_dir_exists(const char* dir) { |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 867 | if (make_dir(dir, 0700) && errno != EEXIST) { |
| 868 | return -1; |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 874 | static bool is_file_crypto() { |
Yabin Cui | 0ff8590 | 2015-07-24 13:58:03 -0700 | [diff] [blame] | 875 | std::string value = property_get("ro.crypto.type"); |
| 876 | return value == "file"; |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 877 | } |
| 878 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 879 | static int do_installkey(const std::vector<std::string>& args) { |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 880 | if (!is_file_crypto()) { |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 881 | return 0; |
| 882 | } |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 883 | return e4crypt_create_device_key(args[1].c_str(), |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 884 | do_installkeys_ensure_dir_exists); |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 885 | } |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 886 | |
Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 887 | static int do_setusercryptopolicies(const std::vector<std::string>& args) { |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 888 | if (!is_file_crypto()) { |
| 889 | return 0; |
| 890 | } |
Tom Cherry | 087cd35 | 2015-08-03 14:19:35 -0700 | [diff] [blame] | 891 | return e4crypt_set_user_crypto_policies(args[1].c_str()); |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 892 | } |
Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 893 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 894 | BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const { |
| 895 | constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max(); |
| 896 | static const Map builtin_functions = { |
| 897 | {"bootchart_init", {0, 0, do_bootchart_init}}, |
| 898 | {"chmod", {2, 2, do_chmod}}, |
| 899 | {"chown", {2, 3, do_chown}}, |
| 900 | {"class_reset", {1, 1, do_class_reset}}, |
| 901 | {"class_start", {1, 1, do_class_start}}, |
| 902 | {"class_stop", {1, 1, do_class_stop}}, |
| 903 | {"copy", {2, 2, do_copy}}, |
| 904 | {"domainname", {1, 1, do_domainname}}, |
| 905 | {"enable", {1, 1, do_enable}}, |
| 906 | {"exec", {1, kMax, do_exec}}, |
| 907 | {"export", {2, 2, do_export}}, |
| 908 | {"hostname", {1, 1, do_hostname}}, |
| 909 | {"ifup", {1, 1, do_ifup}}, |
| 910 | {"insmod", {1, kMax, do_insmod}}, |
| 911 | {"installkey", {1, 1, do_installkey}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 912 | {"load_persist_props", {0, 0, do_load_persist_props}}, |
Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 913 | {"load_system_props", {0, 0, do_load_system_props}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 914 | {"loglevel", {1, 1, do_loglevel}}, |
| 915 | {"mkdir", {1, 4, do_mkdir}}, |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 916 | {"mount_all", {1, kMax, do_mount_all}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 917 | {"mount", {3, kMax, do_mount}}, |
| 918 | {"powerctl", {1, 1, do_powerctl}}, |
| 919 | {"restart", {1, 1, do_restart}}, |
| 920 | {"restorecon", {1, kMax, do_restorecon}}, |
| 921 | {"restorecon_recursive", {1, kMax, do_restorecon_recursive}}, |
| 922 | {"rm", {1, 1, do_rm}}, |
| 923 | {"rmdir", {1, 1, do_rmdir}}, |
| 924 | {"setprop", {2, 2, do_setprop}}, |
| 925 | {"setrlimit", {3, 3, do_setrlimit}}, |
Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 926 | {"setusercryptopolicies", {1, 1, do_setusercryptopolicies}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 927 | {"start", {1, 1, do_start}}, |
| 928 | {"stop", {1, 1, do_stop}}, |
| 929 | {"swapon_all", {1, 1, do_swapon_all}}, |
| 930 | {"symlink", {2, 2, do_symlink}}, |
| 931 | {"sysclktz", {1, 1, do_sysclktz}}, |
| 932 | {"trigger", {1, 1, do_trigger}}, |
| 933 | {"verity_load_state", {0, 0, do_verity_load_state}}, |
| 934 | {"verity_update_state", {0, 0, do_verity_update_state}}, |
| 935 | {"wait", {1, 2, do_wait}}, |
| 936 | {"write", {2, 2, do_write}}, |
| 937 | }; |
| 938 | return builtin_functions; |
| 939 | } |