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 | |
| 70 | static int setkey(struct kbentry *kbe) |
| 71 | { |
| 72 | int fd, ret; |
| 73 | |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 74 | fd = open("/dev/tty0", O_RDWR | O_SYNC | O_CLOEXEC); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 75 | if (fd < 0) |
| 76 | return -1; |
| 77 | |
| 78 | ret = ioctl(fd, KDSKBENT, kbe); |
| 79 | |
| 80 | close(fd); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | static int __ifupdown(const char *interface, int up) |
| 85 | { |
| 86 | struct ifreq ifr; |
| 87 | int s, ret; |
| 88 | |
| 89 | strlcpy(ifr.ifr_name, interface, IFNAMSIZ); |
| 90 | |
| 91 | s = socket(AF_INET, SOCK_DGRAM, 0); |
| 92 | if (s < 0) |
| 93 | return -1; |
| 94 | |
| 95 | ret = ioctl(s, SIOCGIFFLAGS, &ifr); |
| 96 | if (ret < 0) { |
| 97 | goto done; |
| 98 | } |
| 99 | |
| 100 | if (up) |
| 101 | ifr.ifr_flags |= IFF_UP; |
| 102 | else |
| 103 | ifr.ifr_flags &= ~IFF_UP; |
| 104 | |
| 105 | ret = ioctl(s, SIOCSIFFLAGS, &ifr); |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 106 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 107 | done: |
| 108 | close(s); |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static void service_start_if_not_disabled(struct service *svc) |
| 113 | { |
| 114 | if (!(svc->flags & SVC_DISABLED)) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 115 | service_start(svc, NULL); |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 116 | } else { |
| 117 | svc->flags |= SVC_DISABLED_START; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Jay Freeman (saurik) | e7cb137 | 2008-11-17 06:41:10 +0000 | [diff] [blame] | 121 | int do_chdir(int nargs, char **args) |
| 122 | { |
| 123 | chdir(args[1]); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | int do_chroot(int nargs, char **args) |
| 128 | { |
| 129 | chroot(args[1]); |
| 130 | return 0; |
| 131 | } |
| 132 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | int do_class_start(int nargs, char **args) |
| 134 | { |
| 135 | /* Starting a class does not start services |
| 136 | * which are explicitly disabled. They must |
| 137 | * be started individually. |
| 138 | */ |
| 139 | service_for_each_class(args[1], service_start_if_not_disabled); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | int do_class_stop(int nargs, char **args) |
| 144 | { |
| 145 | service_for_each_class(args[1], service_stop); |
| 146 | return 0; |
| 147 | } |
| 148 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 149 | int do_class_reset(int nargs, char **args) |
| 150 | { |
| 151 | service_for_each_class(args[1], service_reset); |
| 152 | return 0; |
| 153 | } |
| 154 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | int do_domainname(int nargs, char **args) |
| 156 | { |
| 157 | return write_file("/proc/sys/kernel/domainname", args[1]); |
| 158 | } |
| 159 | |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 160 | int do_enable(int nargs, char **args) |
| 161 | { |
| 162 | struct service *svc; |
| 163 | svc = service_find_by_name(args[1]); |
| 164 | if (svc) { |
| 165 | svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED); |
| 166 | if (svc->flags & SVC_DISABLED_START) { |
| 167 | service_start(svc, NULL); |
| 168 | } |
| 169 | } else { |
| 170 | return -1; |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame^] | 175 | int do_exec(int nargs, char** args) { |
| 176 | service* svc = make_exec_oneshot_service(nargs, args); |
| 177 | if (svc == NULL) { |
| 178 | return -1; |
| 179 | } |
| 180 | service_start(svc, NULL); |
| 181 | return 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame^] | 184 | // TODO: remove execonce when exec is available. |
San Mehat | 429721c | 2014-09-23 07:48:47 -0700 | [diff] [blame] | 185 | int do_execonce(int nargs, char **args) |
| 186 | { |
| 187 | pid_t child; |
| 188 | int child_status = 0; |
| 189 | static int already_done; |
| 190 | |
| 191 | if (already_done) { |
| 192 | return -1; |
| 193 | } |
| 194 | already_done = 1; |
| 195 | if (!(child = fork())) { |
| 196 | /* |
| 197 | * Child process. |
| 198 | */ |
| 199 | zap_stdio(); |
| 200 | char *exec_args[100]; |
Elliott Hughes | d3e37d1 | 2015-02-02 16:43:32 -0800 | [diff] [blame] | 201 | size_t num_process_args = nargs; |
San Mehat | 429721c | 2014-09-23 07:48:47 -0700 | [diff] [blame] | 202 | |
| 203 | memset(exec_args, 0, sizeof(exec_args)); |
| 204 | if (num_process_args > ARRAY_SIZE(exec_args) - 1) { |
Elliott Hughes | d3e37d1 | 2015-02-02 16:43:32 -0800 | [diff] [blame] | 205 | ERROR("exec called with %zu args, limit is %zu", num_process_args, |
San Mehat | 429721c | 2014-09-23 07:48:47 -0700 | [diff] [blame] | 206 | ARRAY_SIZE(exec_args) - 1); |
| 207 | _exit(1); |
| 208 | } |
Elliott Hughes | d3e37d1 | 2015-02-02 16:43:32 -0800 | [diff] [blame] | 209 | for (size_t i = 1; i < num_process_args; i++) |
San Mehat | 429721c | 2014-09-23 07:48:47 -0700 | [diff] [blame] | 210 | exec_args[i - 1] = args[i]; |
| 211 | |
| 212 | if (execv(exec_args[0], exec_args) == -1) { |
| 213 | ERROR("Failed to execv '%s' (%s)", exec_args[0], strerror(errno)); |
| 214 | _exit(1); |
| 215 | } |
| 216 | ERROR("Returned from execv()!"); |
| 217 | _exit(1); |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Parent process. |
| 222 | */ |
| 223 | if (child == -1) { |
| 224 | ERROR("Fork failed\n"); |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | if (TEMP_FAILURE_RETRY(waitpid(child, &child_status, 0)) == -1) { |
| 229 | ERROR("waitpid(): failed (%s)\n", strerror(errno)); |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | if (WIFSIGNALED(child_status)) { |
| 234 | INFO("Child exited due to signal %d\n", WTERMSIG(child_status)); |
| 235 | return -1; |
| 236 | } else if (WIFEXITED(child_status)) { |
| 237 | INFO("Child exited normally (exit code %d)\n", WEXITSTATUS(child_status)); |
| 238 | return WEXITSTATUS(child_status); |
| 239 | } |
| 240 | |
| 241 | ERROR("Abnormal child process exit\n"); |
| 242 | |
| 243 | return -1; |
| 244 | } |
| 245 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 246 | int do_export(int nargs, char **args) |
| 247 | { |
James Morrissey | 381341f | 2014-05-16 11:36:36 +0100 | [diff] [blame] | 248 | return add_environment(args[1], args[2]); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | int do_hostname(int nargs, char **args) |
| 252 | { |
| 253 | return write_file("/proc/sys/kernel/hostname", args[1]); |
| 254 | } |
| 255 | |
| 256 | int do_ifup(int nargs, char **args) |
| 257 | { |
| 258 | return __ifupdown(args[1], 1); |
| 259 | } |
| 260 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 261 | |
| 262 | static int do_insmod_inner(int nargs, char **args, int opt_len) |
| 263 | { |
| 264 | char options[opt_len + 1]; |
| 265 | int i; |
| 266 | |
| 267 | options[0] = '\0'; |
| 268 | if (nargs > 2) { |
| 269 | strcpy(options, args[2]); |
| 270 | for (i = 3; i < nargs; ++i) { |
| 271 | strcat(options, " "); |
| 272 | strcat(options, args[i]); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return insmod(args[1], options); |
| 277 | } |
| 278 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 279 | int do_insmod(int nargs, char **args) |
| 280 | { |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 281 | int i; |
| 282 | int size = 0; |
| 283 | |
| 284 | if (nargs > 2) { |
| 285 | for (i = 2; i < nargs; ++i) |
| 286 | size += strlen(args[i]) + 1; |
| 287 | } |
| 288 | |
| 289 | return do_insmod_inner(nargs, args, size); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 290 | } |
| 291 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 292 | int do_mkdir(int nargs, char **args) |
| 293 | { |
| 294 | mode_t mode = 0755; |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 295 | int ret; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 296 | |
| 297 | /* mkdir <path> [mode] [owner] [group] */ |
| 298 | |
| 299 | if (nargs >= 3) { |
| 300 | mode = strtoul(args[2], 0, 8); |
| 301 | } |
| 302 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 303 | ret = make_dir(args[1], mode); |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 304 | /* chmod in case the directory already exists */ |
| 305 | if (ret == -1 && errno == EEXIST) { |
Nick Kralevich | bc60954 | 2015-01-31 21:39:46 -0800 | [diff] [blame] | 306 | ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW); |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 307 | } |
| 308 | if (ret == -1) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 309 | return -errno; |
| 310 | } |
| 311 | |
| 312 | if (nargs >= 4) { |
| 313 | uid_t uid = decode_uid(args[3]); |
| 314 | gid_t gid = -1; |
| 315 | |
| 316 | if (nargs == 5) { |
| 317 | gid = decode_uid(args[4]); |
| 318 | } |
| 319 | |
Nick Kralevich | bc60954 | 2015-01-31 21:39:46 -0800 | [diff] [blame] | 320 | if (lchown(args[1], uid, gid) == -1) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 321 | return -errno; |
| 322 | } |
Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 323 | |
| 324 | /* chown may have cleared S_ISUID and S_ISGID, chmod again */ |
| 325 | if (mode & (S_ISUID | S_ISGID)) { |
Nick Kralevich | bc60954 | 2015-01-31 21:39:46 -0800 | [diff] [blame] | 326 | ret = fchmodat(AT_FDCWD, args[1], mode, AT_SYMLINK_NOFOLLOW); |
Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 327 | if (ret == -1) { |
| 328 | return -errno; |
| 329 | } |
| 330 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static struct { |
| 337 | const char *name; |
| 338 | unsigned flag; |
| 339 | } mount_flags[] = { |
| 340 | { "noatime", MS_NOATIME }, |
Lars Svensson | b6ee25e | 2011-07-14 13:39:09 +0200 | [diff] [blame] | 341 | { "noexec", MS_NOEXEC }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 342 | { "nosuid", MS_NOSUID }, |
| 343 | { "nodev", MS_NODEV }, |
| 344 | { "nodiratime", MS_NODIRATIME }, |
| 345 | { "ro", MS_RDONLY }, |
| 346 | { "rw", 0 }, |
| 347 | { "remount", MS_REMOUNT }, |
Jeff Sharkey | e50ac5f | 2012-08-14 11:34:34 -0700 | [diff] [blame] | 348 | { "bind", MS_BIND }, |
| 349 | { "rec", MS_REC }, |
| 350 | { "unbindable", MS_UNBINDABLE }, |
| 351 | { "private", MS_PRIVATE }, |
| 352 | { "slave", MS_SLAVE }, |
| 353 | { "shared", MS_SHARED }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 354 | { "defaults", 0 }, |
| 355 | { 0, 0 }, |
| 356 | }; |
| 357 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 358 | #define DATA_MNT_POINT "/data" |
| 359 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 360 | /* mount <type> <device> <path> <flags ...> <options> */ |
| 361 | int do_mount(int nargs, char **args) |
| 362 | { |
| 363 | char tmp[64]; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 364 | char *source, *target, *system; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 365 | char *options = NULL; |
| 366 | unsigned flags = 0; |
| 367 | int n, i; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 368 | int wait = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 369 | |
| 370 | for (n = 4; n < nargs; n++) { |
| 371 | for (i = 0; mount_flags[i].name; i++) { |
| 372 | if (!strcmp(args[n], mount_flags[i].name)) { |
| 373 | flags |= mount_flags[i].flag; |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 378 | if (!mount_flags[i].name) { |
| 379 | if (!strcmp(args[n], "wait")) |
| 380 | wait = 1; |
| 381 | /* if our last argument isn't a flag, wolf it up as an option string */ |
| 382 | else if (n + 1 == nargs) |
| 383 | options = args[n]; |
| 384 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 387 | system = args[1]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 388 | source = args[2]; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 389 | target = args[3]; |
| 390 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 391 | if (!strncmp(source, "mtd@", 4)) { |
| 392 | n = mtd_name_to_number(source + 4); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 393 | if (n < 0) { |
| 394 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 395 | } |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 396 | |
Yabin Cui | e2d63af | 2015-02-17 19:27:51 -0800 | [diff] [blame] | 397 | snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 398 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 399 | if (wait) |
| 400 | wait_for_file(tmp, COMMAND_RETRY_TIMEOUT); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 401 | if (mount(tmp, target, system, flags, options) < 0) { |
| 402 | return -1; |
| 403 | } |
| 404 | |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 405 | goto exit_success; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 406 | } else if (!strncmp(source, "loop@", 5)) { |
| 407 | int mode, loop, fd; |
| 408 | struct loop_info info; |
| 409 | |
| 410 | mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 411 | fd = open(source + 5, mode | O_CLOEXEC); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 412 | if (fd < 0) { |
| 413 | return -1; |
| 414 | } |
| 415 | |
| 416 | for (n = 0; ; n++) { |
Yabin Cui | e2d63af | 2015-02-17 19:27:51 -0800 | [diff] [blame] | 417 | snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n); |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 418 | loop = open(tmp, mode | O_CLOEXEC); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 419 | if (loop < 0) { |
Tomasz Kondel | bfdcc40 | 2014-02-06 08:57:27 +0100 | [diff] [blame] | 420 | close(fd); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 421 | return -1; |
| 422 | } |
| 423 | |
| 424 | /* if it is a blank loop device */ |
| 425 | if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { |
| 426 | /* if it becomes our loop device */ |
| 427 | if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { |
| 428 | close(fd); |
| 429 | |
| 430 | if (mount(tmp, target, system, flags, options) < 0) { |
| 431 | ioctl(loop, LOOP_CLR_FD, 0); |
| 432 | close(loop); |
| 433 | return -1; |
| 434 | } |
| 435 | |
| 436 | close(loop); |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 437 | goto exit_success; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | close(loop); |
| 442 | } |
| 443 | |
| 444 | close(fd); |
| 445 | ERROR("out of loopback devices"); |
| 446 | return -1; |
| 447 | } else { |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 448 | if (wait) |
| 449 | wait_for_file(source, COMMAND_RETRY_TIMEOUT); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 450 | if (mount(source, target, system, flags, options) < 0) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 451 | return -1; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 452 | } |
| 453 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 454 | } |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 455 | |
| 456 | exit_success: |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 457 | return 0; |
| 458 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 459 | } |
| 460 | |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 461 | static int wipe_data_via_recovery() |
| 462 | { |
| 463 | mkdir("/cache/recovery", 0700); |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 464 | 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] | 465 | if (fd >= 0) { |
Jeff Sharkey | d26135b | 2014-09-24 11:46:36 -0700 | [diff] [blame] | 466 | write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1); |
| 467 | 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] | 468 | close(fd); |
| 469 | } else { |
| 470 | ERROR("could not open /cache/recovery/command\n"); |
| 471 | return -1; |
| 472 | } |
| 473 | android_reboot(ANDROID_RB_RESTART2, 0, "recovery"); |
| 474 | while (1) { pause(); } // never reached |
| 475 | } |
| 476 | |
| 477 | |
| 478 | /* |
| 479 | * This function might request a reboot, in which case it will |
| 480 | * not return. |
| 481 | */ |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 482 | int do_mount_all(int nargs, char **args) |
| 483 | { |
| 484 | pid_t pid; |
| 485 | int ret = -1; |
| 486 | int child_ret = -1; |
| 487 | int status; |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 488 | struct fstab *fstab; |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 489 | |
| 490 | if (nargs != 2) { |
| 491 | return -1; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and |
| 496 | * do the call in the child to provide protection to the main init |
| 497 | * process if anything goes wrong (crash or memory leak), and wait for |
| 498 | * the child to finish in the parent. |
| 499 | */ |
| 500 | pid = fork(); |
| 501 | if (pid > 0) { |
| 502 | /* Parent. Wait for the child to return */ |
Paul Lawrence | 40af092 | 2014-09-16 14:31:23 -0700 | [diff] [blame] | 503 | int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 504 | if (wp_ret < 0) { |
| 505 | /* Unexpected error code. We will continue anyway. */ |
| 506 | NOTICE("waitpid failed rc=%d, errno=%d\n", wp_ret, errno); |
| 507 | } |
| 508 | |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 509 | if (WIFEXITED(status)) { |
| 510 | ret = WEXITSTATUS(status); |
| 511 | } else { |
| 512 | ret = -1; |
| 513 | } |
| 514 | } else if (pid == 0) { |
| 515 | /* child, call fs_mgr_mount_all() */ |
| 516 | 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] | 517 | fstab = fs_mgr_read_fstab(args[1]); |
| 518 | child_ret = fs_mgr_mount_all(fstab); |
| 519 | fs_mgr_free_fstab(fstab); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 520 | if (child_ret == -1) { |
| 521 | ERROR("fs_mgr_mount_all returned an error\n"); |
| 522 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 523 | _exit(child_ret); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 524 | } else { |
| 525 | /* fork failed, return an error */ |
| 526 | return -1; |
| 527 | } |
| 528 | |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 529 | if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) { |
Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 530 | property_set("vold.decrypt", "trigger_encryption"); |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 531 | } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 532 | property_set("ro.crypto.state", "encrypted"); |
Paul Lawrence | 13d5bb4 | 2014-01-30 10:43:52 -0800 | [diff] [blame] | 533 | property_set("vold.decrypt", "trigger_default_encryption"); |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 534 | } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 535 | property_set("ro.crypto.state", "unencrypted"); |
| 536 | /* If fs_mgr determined this is an unencrypted device, then trigger |
| 537 | * that action. |
| 538 | */ |
| 539 | action_for_each_trigger("nonencrypted", action_add_queue_tail); |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 540 | } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) { |
| 541 | /* Setup a wipe via recovery, and reboot into recovery */ |
| 542 | ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n"); |
| 543 | ret = wipe_data_via_recovery(); |
| 544 | /* If reboot worked, there is no return. */ |
| 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 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 565 | int do_setcon(int nargs, char **args) { |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 566 | if (is_selinux_enabled() <= 0) |
| 567 | return 0; |
| 568 | if (setcon(args[1]) < 0) { |
| 569 | return -errno; |
| 570 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | int do_setenforce(int nargs, char **args) { |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 575 | if (is_selinux_enabled() <= 0) |
| 576 | return 0; |
| 577 | if (security_setenforce(atoi(args[1])) < 0) { |
| 578 | return -errno; |
| 579 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 580 | return 0; |
| 581 | } |
| 582 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 583 | int do_setkey(int nargs, char **args) |
| 584 | { |
| 585 | struct kbentry kbe; |
| 586 | kbe.kb_table = strtoul(args[1], 0, 0); |
| 587 | kbe.kb_index = strtoul(args[2], 0, 0); |
| 588 | kbe.kb_value = strtoul(args[3], 0, 0); |
| 589 | return setkey(&kbe); |
| 590 | } |
| 591 | |
| 592 | int do_setprop(int nargs, char **args) |
| 593 | { |
Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 594 | const char *name = args[1]; |
| 595 | const char *value = args[2]; |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 596 | char prop_val[PROP_VALUE_MAX]; |
| 597 | int ret; |
Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 598 | |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 599 | ret = expand_props(prop_val, value, sizeof(prop_val)); |
| 600 | if (ret) { |
| 601 | ERROR("cannot expand '%s' while assigning to '%s'\n", value, name); |
| 602 | return -EINVAL; |
Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 603 | } |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 604 | property_set(name, prop_val); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | int do_setrlimit(int nargs, char **args) |
| 609 | { |
| 610 | struct rlimit limit; |
| 611 | int resource; |
| 612 | resource = atoi(args[1]); |
| 613 | limit.rlim_cur = atoi(args[2]); |
| 614 | limit.rlim_max = atoi(args[3]); |
| 615 | return setrlimit(resource, &limit); |
| 616 | } |
| 617 | |
| 618 | int do_start(int nargs, char **args) |
| 619 | { |
| 620 | struct service *svc; |
| 621 | svc = service_find_by_name(args[1]); |
| 622 | if (svc) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 623 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 624 | } |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | int do_stop(int nargs, char **args) |
| 629 | { |
| 630 | struct service *svc; |
| 631 | svc = service_find_by_name(args[1]); |
| 632 | if (svc) { |
| 633 | service_stop(svc); |
| 634 | } |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | int do_restart(int nargs, char **args) |
| 639 | { |
| 640 | struct service *svc; |
| 641 | svc = service_find_by_name(args[1]); |
| 642 | if (svc) { |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 643 | service_restart(svc); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 644 | } |
| 645 | return 0; |
| 646 | } |
| 647 | |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 648 | int do_powerctl(int nargs, char **args) |
| 649 | { |
| 650 | char command[PROP_VALUE_MAX]; |
| 651 | int res; |
| 652 | int len = 0; |
| 653 | int cmd = 0; |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 654 | const char *reboot_target; |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 655 | |
| 656 | res = expand_props(command, args[1], sizeof(command)); |
| 657 | if (res) { |
| 658 | ERROR("powerctl: cannot expand '%s'\n", args[1]); |
| 659 | return -EINVAL; |
| 660 | } |
| 661 | |
| 662 | if (strncmp(command, "shutdown", 8) == 0) { |
| 663 | cmd = ANDROID_RB_POWEROFF; |
| 664 | len = 8; |
| 665 | } else if (strncmp(command, "reboot", 6) == 0) { |
| 666 | cmd = ANDROID_RB_RESTART2; |
| 667 | len = 6; |
| 668 | } else { |
| 669 | ERROR("powerctl: unrecognized command '%s'\n", command); |
| 670 | return -EINVAL; |
| 671 | } |
| 672 | |
| 673 | if (command[len] == ',') { |
| 674 | reboot_target = &command[len + 1]; |
| 675 | } else if (command[len] == '\0') { |
| 676 | reboot_target = ""; |
| 677 | } else { |
| 678 | ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]); |
| 679 | return -EINVAL; |
| 680 | } |
| 681 | |
| 682 | return android_reboot(cmd, 0, reboot_target); |
| 683 | } |
| 684 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 685 | int do_trigger(int nargs, char **args) |
| 686 | { |
Jay Freeman (saurik) | 11e1c42 | 2008-11-17 06:35:08 +0000 | [diff] [blame] | 687 | 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] | 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | int do_symlink(int nargs, char **args) |
| 692 | { |
| 693 | return symlink(args[1], args[2]); |
| 694 | } |
| 695 | |
Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 696 | int do_rm(int nargs, char **args) |
| 697 | { |
| 698 | return unlink(args[1]); |
| 699 | } |
| 700 | |
| 701 | int do_rmdir(int nargs, char **args) |
| 702 | { |
| 703 | return rmdir(args[1]); |
| 704 | } |
| 705 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 706 | int do_sysclktz(int nargs, char **args) |
| 707 | { |
| 708 | struct timezone tz; |
| 709 | |
| 710 | if (nargs != 2) |
| 711 | return -1; |
| 712 | |
| 713 | memset(&tz, 0, sizeof(tz)); |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 714 | tz.tz_minuteswest = atoi(args[1]); |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 715 | if (settimeofday(NULL, &tz)) |
| 716 | return -1; |
| 717 | return 0; |
| 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 | |
| 852 | int do_setsebool(int nargs, char **args) { |
Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 853 | const char *name = args[1]; |
| 854 | const char *value = args[2]; |
| 855 | SELboolean b; |
| 856 | int ret; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 857 | |
| 858 | if (is_selinux_enabled() <= 0) |
| 859 | return 0; |
| 860 | |
Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 861 | b.name = name; |
| 862 | if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on")) |
| 863 | b.value = 1; |
| 864 | else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off")) |
| 865 | b.value = 0; |
| 866 | else { |
| 867 | ERROR("setsebool: invalid value %s\n", value); |
| 868 | return -EINVAL; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 869 | } |
| 870 | |
Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 871 | if (security_set_boolean_list(1, &b, 0) < 0) { |
| 872 | ret = -errno; |
| 873 | ERROR("setsebool: could not set %s to %s\n", name, value); |
| 874 | return ret; |
| 875 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 876 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 877 | return 0; |
| 878 | } |
| 879 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 880 | int do_loglevel(int nargs, char **args) { |
Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 881 | int log_level; |
| 882 | char log_level_str[PROP_VALUE_MAX] = ""; |
| 883 | if (nargs != 2) { |
| 884 | ERROR("loglevel: missing argument\n"); |
| 885 | return -EINVAL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 886 | } |
Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 887 | |
| 888 | if (expand_props(log_level_str, args[1], sizeof(log_level_str))) { |
| 889 | ERROR("loglevel: cannot expand '%s'\n", args[1]); |
| 890 | return -EINVAL; |
| 891 | } |
| 892 | log_level = atoi(log_level_str); |
| 893 | if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) { |
| 894 | ERROR("loglevel: invalid log level'%d'\n", log_level); |
| 895 | return -EINVAL; |
| 896 | } |
| 897 | klog_set_level(log_level); |
| 898 | return 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 901 | int do_load_persist_props(int nargs, char **args) { |
| 902 | if (nargs == 1) { |
| 903 | load_persist_props(); |
| 904 | return 0; |
| 905 | } |
| 906 | return -1; |
| 907 | } |
| 908 | |
Riley Andrews | e4b7b29 | 2014-06-16 15:06:21 -0700 | [diff] [blame] | 909 | int do_load_all_props(int nargs, char **args) { |
| 910 | if (nargs == 1) { |
| 911 | load_all_props(); |
| 912 | return 0; |
| 913 | } |
| 914 | return -1; |
| 915 | } |
| 916 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 917 | int do_wait(int nargs, char **args) |
| 918 | { |
| 919 | if (nargs == 2) { |
| 920 | return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT); |
Patrick McCormick | 96d0a4d | 2011-02-04 10:51:39 -0800 | [diff] [blame] | 921 | } else if (nargs == 3) { |
| 922 | return wait_for_file(args[1], atoi(args[2])); |
| 923 | } else |
| 924 | return -1; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 925 | } |