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