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