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