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