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