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