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