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