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