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