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