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