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