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