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