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