| 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 | 6095afc | 2015-02-25 15:09:39 -0800 | [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 | 6095afc | 2015-02-25 15:09:39 -0800 | [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 | |
| 450 | |
| 451 | /* |
| 452 | * This function might request a reboot, in which case it will |
| 453 | * not return. |
| 454 | */ |
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 455 | int do_mount_all(int nargs, char **args) |
| 456 | { |
| 457 | pid_t pid; |
| 458 | int ret = -1; |
| 459 | int child_ret = -1; |
| 460 | int status; |
| Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 461 | struct fstab *fstab; |
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 462 | |
| 463 | if (nargs != 2) { |
| 464 | return -1; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and |
| 469 | * do the call in the child to provide protection to the main init |
| 470 | * process if anything goes wrong (crash or memory leak), and wait for |
| 471 | * the child to finish in the parent. |
| 472 | */ |
| 473 | pid = fork(); |
| 474 | if (pid > 0) { |
| 475 | /* Parent. Wait for the child to return */ |
| Paul Lawrence | 40af092 | 2014-09-16 14:31:23 -0700 | [diff] [blame] | 476 | int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 477 | if (wp_ret < 0) { |
| 478 | /* Unexpected error code. We will continue anyway. */ |
| 479 | NOTICE("waitpid failed rc=%d, errno=%d\n", wp_ret, errno); |
| 480 | } |
| 481 | |
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 482 | if (WIFEXITED(status)) { |
| 483 | ret = WEXITSTATUS(status); |
| 484 | } else { |
| 485 | ret = -1; |
| 486 | } |
| 487 | } else if (pid == 0) { |
| 488 | /* child, call fs_mgr_mount_all() */ |
| 489 | 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] | 490 | fstab = fs_mgr_read_fstab(args[1]); |
| 491 | child_ret = fs_mgr_mount_all(fstab); |
| 492 | fs_mgr_free_fstab(fstab); |
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 493 | if (child_ret == -1) { |
| 494 | ERROR("fs_mgr_mount_all returned an error\n"); |
| 495 | } |
| JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 496 | _exit(child_ret); |
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 497 | } else { |
| 498 | /* fork failed, return an error */ |
| 499 | return -1; |
| 500 | } |
| 501 | |
| JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 502 | if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) { |
| Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 503 | property_set("vold.decrypt", "trigger_encryption"); |
| JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 504 | } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) { |
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 505 | property_set("ro.crypto.state", "encrypted"); |
| Paul Lawrence | 13d5bb4 | 2014-01-30 10:43:52 -0800 | [diff] [blame] | 506 | property_set("vold.decrypt", "trigger_default_encryption"); |
| JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 507 | } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) { |
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 508 | property_set("ro.crypto.state", "unencrypted"); |
| 509 | /* If fs_mgr determined this is an unencrypted device, then trigger |
| 510 | * that action. |
| 511 | */ |
| 512 | action_for_each_trigger("nonencrypted", action_add_queue_tail); |
| JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 513 | } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) { |
| 514 | /* Setup a wipe via recovery, and reboot into recovery */ |
| 515 | ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n"); |
| 516 | ret = wipe_data_via_recovery(); |
| 517 | /* If reboot worked, there is no return. */ |
| Paul Lawrence | 6095afc | 2015-02-25 15:09:39 -0800 | [diff] [blame^] | 518 | } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) { |
| 519 | // We have to create the key files here. Only init can call make_dir, |
| 520 | // and we can't do it from fs_mgr as then fs_mgr would depend on |
| 521 | // make_dir creating a circular dependency. |
| 522 | fstab = fs_mgr_read_fstab(args[1]); |
| 523 | for (int i = 0; i < fstab->num_entries; ++i) { |
| 524 | if (fs_mgr_is_file_encrypted(&fstab->recs[i])) { |
| 525 | if (e4crypt_create_device_key(fstab->recs[i].mount_point)) { |
| 526 | ERROR("Could not create device key on %s" |
| 527 | " - continue unencrypted\n", |
| 528 | fstab->recs[i].mount_point); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | fs_mgr_free_fstab(fstab); |
| 533 | |
| 534 | if (e4crypt_install_keyring()) { |
| 535 | return -1; |
| 536 | } |
| 537 | property_set("ro.crypto.state", "encrypted"); |
| 538 | |
| 539 | // Although encrypted, we have device key, so we do not need to |
| 540 | // do anything different from the nonencrypted case. |
| 541 | action_for_each_trigger("nonencrypted", action_add_queue_tail); |
| 542 | } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) { |
| 543 | if (e4crypt_install_keyring()) { |
| 544 | return -1; |
| 545 | } |
| 546 | property_set("ro.crypto.state", "encrypted"); |
| 547 | property_set("vold.decrypt", "trigger_restart_min_framework"); |
| JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 548 | } else if (ret > 0) { |
| 549 | ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret); |
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 550 | } |
| JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 551 | /* else ... < 0: error */ |
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 552 | |
| 553 | return ret; |
| 554 | } |
| 555 | |
| Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 556 | int do_swapon_all(int nargs, char **args) |
| 557 | { |
| 558 | struct fstab *fstab; |
| 559 | int ret; |
| 560 | |
| 561 | fstab = fs_mgr_read_fstab(args[1]); |
| 562 | ret = fs_mgr_swapon_all(fstab); |
| 563 | fs_mgr_free_fstab(fstab); |
| 564 | |
| 565 | return ret; |
| 566 | } |
| 567 | |
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 568 | int do_setcon(int nargs, char **args) { |
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 569 | if (is_selinux_enabled() <= 0) |
| 570 | return 0; |
| 571 | if (setcon(args[1]) < 0) { |
| 572 | return -errno; |
| 573 | } |
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 577 | int do_setprop(int nargs, char **args) |
| 578 | { |
| Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 579 | const char *name = args[1]; |
| 580 | const char *value = args[2]; |
| Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 581 | char prop_val[PROP_VALUE_MAX]; |
| 582 | int ret; |
| Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 583 | |
| Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 584 | ret = expand_props(prop_val, value, sizeof(prop_val)); |
| 585 | if (ret) { |
| 586 | ERROR("cannot expand '%s' while assigning to '%s'\n", value, name); |
| 587 | return -EINVAL; |
| Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 588 | } |
| Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 589 | property_set(name, prop_val); |
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | int do_setrlimit(int nargs, char **args) |
| 594 | { |
| 595 | struct rlimit limit; |
| 596 | int resource; |
| 597 | resource = atoi(args[1]); |
| 598 | limit.rlim_cur = atoi(args[2]); |
| 599 | limit.rlim_max = atoi(args[3]); |
| 600 | return setrlimit(resource, &limit); |
| 601 | } |
| 602 | |
| 603 | int do_start(int nargs, char **args) |
| 604 | { |
| 605 | struct service *svc; |
| 606 | svc = service_find_by_name(args[1]); |
| 607 | if (svc) { |
| San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 608 | service_start(svc, NULL); |
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 609 | } |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | int do_stop(int nargs, char **args) |
| 614 | { |
| 615 | struct service *svc; |
| 616 | svc = service_find_by_name(args[1]); |
| 617 | if (svc) { |
| 618 | service_stop(svc); |
| 619 | } |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | int do_restart(int nargs, char **args) |
| 624 | { |
| 625 | struct service *svc; |
| 626 | svc = service_find_by_name(args[1]); |
| 627 | if (svc) { |
| Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 628 | service_restart(svc); |
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 629 | } |
| 630 | return 0; |
| 631 | } |
| 632 | |
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 633 | int do_powerctl(int nargs, char **args) |
| 634 | { |
| 635 | char command[PROP_VALUE_MAX]; |
| 636 | int res; |
| 637 | int len = 0; |
| 638 | int cmd = 0; |
| Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 639 | const char *reboot_target; |
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 640 | |
| 641 | res = expand_props(command, args[1], sizeof(command)); |
| 642 | if (res) { |
| 643 | ERROR("powerctl: cannot expand '%s'\n", args[1]); |
| 644 | return -EINVAL; |
| 645 | } |
| 646 | |
| 647 | if (strncmp(command, "shutdown", 8) == 0) { |
| 648 | cmd = ANDROID_RB_POWEROFF; |
| 649 | len = 8; |
| 650 | } else if (strncmp(command, "reboot", 6) == 0) { |
| 651 | cmd = ANDROID_RB_RESTART2; |
| 652 | len = 6; |
| 653 | } else { |
| 654 | ERROR("powerctl: unrecognized command '%s'\n", command); |
| 655 | return -EINVAL; |
| 656 | } |
| 657 | |
| 658 | if (command[len] == ',') { |
| 659 | reboot_target = &command[len + 1]; |
| 660 | } else if (command[len] == '\0') { |
| 661 | reboot_target = ""; |
| 662 | } else { |
| 663 | ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]); |
| 664 | return -EINVAL; |
| 665 | } |
| 666 | |
| 667 | return android_reboot(cmd, 0, reboot_target); |
| 668 | } |
| 669 | |
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 670 | int do_trigger(int nargs, char **args) |
| 671 | { |
| Jay Freeman (saurik) | 11e1c42 | 2008-11-17 06:35:08 +0000 | [diff] [blame] | 672 | 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] | 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | int do_symlink(int nargs, char **args) |
| 677 | { |
| 678 | return symlink(args[1], args[2]); |
| 679 | } |
| 680 | |
| Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 681 | int do_rm(int nargs, char **args) |
| 682 | { |
| 683 | return unlink(args[1]); |
| 684 | } |
| 685 | |
| 686 | int do_rmdir(int nargs, char **args) |
| 687 | { |
| 688 | return rmdir(args[1]); |
| 689 | } |
| 690 | |
| The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 691 | int do_sysclktz(int nargs, char **args) |
| 692 | { |
| 693 | struct timezone tz; |
| 694 | |
| 695 | if (nargs != 2) |
| 696 | return -1; |
| 697 | |
| 698 | memset(&tz, 0, sizeof(tz)); |
| Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 699 | tz.tz_minuteswest = atoi(args[1]); |
| The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 700 | if (settimeofday(NULL, &tz)) |
| 701 | return -1; |
| 702 | return 0; |
| 703 | } |
| 704 | |
| Sami Tolvanen | 8ff0190 | 2015-02-16 11:03:34 +0000 | [diff] [blame] | 705 | int do_verity_load_state(int nargs, char **args) { |
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 706 | int mode = -1; |
| 707 | int rc = fs_mgr_load_verity_state(&mode); |
| 708 | if (rc == 0 && mode == VERITY_MODE_LOGGING) { |
| 709 | action_for_each_trigger("verity-logging", action_add_queue_tail); |
| Sami Tolvanen | 8ff0190 | 2015-02-16 11:03:34 +0000 | [diff] [blame] | 710 | } |
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 711 | return rc; |
| Sami Tolvanen | 8ff0190 | 2015-02-16 11:03:34 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 714 | static void verity_update_property(fstab_rec *fstab, const char *mount_point, int status) { |
| 715 | 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] | 716 | } |
| 717 | |
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 718 | int do_verity_update_state(int nargs, char** args) { |
| 719 | return fs_mgr_update_verity_state(verity_update_property); |
| Sami Tolvanen | acbf9be | 2015-03-19 10:00:34 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 722 | int do_write(int nargs, char **args) |
| 723 | { |
| Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 724 | const char *path = args[1]; |
| 725 | const char *value = args[2]; |
| Mike Lockwood | 2c4d5dc | 2011-06-07 17:30:11 -0700 | [diff] [blame] | 726 | |
| Johan Redestig | 7e952f4 | 2014-12-05 00:36:41 +0100 | [diff] [blame] | 727 | char expanded_value[256]; |
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 728 | if (expand_props(expanded_value, value, sizeof(expanded_value))) { |
| Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 729 | ERROR("cannot expand '%s' while writing to '%s'\n", value, path); |
| 730 | return -EINVAL; |
| 731 | } |
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 732 | return write_file(path, expanded_value); |
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 735 | int do_copy(int nargs, char **args) |
| 736 | { |
| 737 | char *buffer = NULL; |
| 738 | int rc = 0; |
| 739 | int fd1 = -1, fd2 = -1; |
| 740 | struct stat info; |
| 741 | int brtw, brtr; |
| 742 | char *p; |
| 743 | |
| 744 | if (nargs != 3) |
| 745 | return -1; |
| 746 | |
| Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 747 | if (stat(args[1], &info) < 0) |
| San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 748 | return -1; |
| 749 | |
| Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 750 | if ((fd1 = open(args[1], O_RDONLY|O_CLOEXEC)) < 0) |
| San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 751 | goto out_err; |
| 752 | |
| Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 753 | 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] | 754 | goto out_err; |
| 755 | |
| Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 756 | if (!(buffer = (char*) malloc(info.st_size))) |
| San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 757 | goto out_err; |
| 758 | |
| 759 | p = buffer; |
| 760 | brtr = info.st_size; |
| 761 | while(brtr) { |
| 762 | rc = read(fd1, p, brtr); |
| 763 | if (rc < 0) |
| 764 | goto out_err; |
| 765 | if (rc == 0) |
| 766 | break; |
| 767 | p += rc; |
| 768 | brtr -= rc; |
| 769 | } |
| 770 | |
| 771 | p = buffer; |
| 772 | brtw = info.st_size; |
| 773 | while(brtw) { |
| 774 | rc = write(fd2, p, brtw); |
| 775 | if (rc < 0) |
| 776 | goto out_err; |
| 777 | if (rc == 0) |
| 778 | break; |
| 779 | p += rc; |
| 780 | brtw -= rc; |
| 781 | } |
| 782 | |
| 783 | rc = 0; |
| 784 | goto out; |
| 785 | out_err: |
| 786 | rc = -1; |
| 787 | out: |
| 788 | if (buffer) |
| 789 | free(buffer); |
| 790 | if (fd1 >= 0) |
| 791 | close(fd1); |
| 792 | if (fd2 >= 0) |
| 793 | close(fd2); |
| 794 | return rc; |
| 795 | } |
| 796 | |
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 797 | int do_chown(int nargs, char **args) { |
| 798 | /* GID is optional. */ |
| 799 | if (nargs == 3) { |
| Nick Kralevich | bc60954 | 2015-01-31 21:39:46 -0800 | [diff] [blame] | 800 | 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] | 801 | return -errno; |
| 802 | } else if (nargs == 4) { |
| Nick Kralevich | bc60954 | 2015-01-31 21:39:46 -0800 | [diff] [blame] | 803 | 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] | 804 | return -errno; |
| 805 | } else { |
| 806 | return -1; |
| 807 | } |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | static mode_t get_mode(const char *s) { |
| 812 | mode_t mode = 0; |
| 813 | while (*s) { |
| 814 | if (*s >= '0' && *s <= '7') { |
| 815 | mode = (mode<<3) | (*s-'0'); |
| 816 | } else { |
| 817 | return -1; |
| 818 | } |
| 819 | s++; |
| 820 | } |
| 821 | return mode; |
| 822 | } |
| 823 | |
| 824 | int do_chmod(int nargs, char **args) { |
| 825 | mode_t mode = get_mode(args[1]); |
| Nick Kralevich | bc60954 | 2015-01-31 21:39:46 -0800 | [diff] [blame] | 826 | 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] | 827 | return -errno; |
| 828 | } |
| 829 | return 0; |
| 830 | } |
| 831 | |
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 832 | int do_restorecon(int nargs, char **args) { |
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 833 | int i; |
| Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 834 | int ret = 0; |
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 835 | |
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 836 | for (i = 1; i < nargs; i++) { |
| Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 837 | if (restorecon(args[i]) < 0) |
| Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 838 | ret = -errno; |
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 839 | } |
| Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 840 | return ret; |
| 841 | } |
| 842 | |
| 843 | int do_restorecon_recursive(int nargs, char **args) { |
| 844 | int i; |
| 845 | int ret = 0; |
| 846 | |
| 847 | for (i = 1; i < nargs; i++) { |
| 848 | if (restorecon_recursive(args[i]) < 0) |
| 849 | ret = -errno; |
| 850 | } |
| 851 | return ret; |
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 852 | } |
| 853 | |
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 854 | int do_loglevel(int nargs, char **args) { |
| Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 855 | int log_level; |
| 856 | char log_level_str[PROP_VALUE_MAX] = ""; |
| 857 | if (nargs != 2) { |
| 858 | ERROR("loglevel: missing argument\n"); |
| 859 | return -EINVAL; |
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 860 | } |
| Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 861 | |
| 862 | if (expand_props(log_level_str, args[1], sizeof(log_level_str))) { |
| 863 | ERROR("loglevel: cannot expand '%s'\n", args[1]); |
| 864 | return -EINVAL; |
| 865 | } |
| 866 | log_level = atoi(log_level_str); |
| 867 | if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) { |
| 868 | ERROR("loglevel: invalid log level'%d'\n", log_level); |
| 869 | return -EINVAL; |
| 870 | } |
| 871 | klog_set_level(log_level); |
| 872 | return 0; |
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 873 | } |
| 874 | |
| Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 875 | int do_load_persist_props(int nargs, char **args) { |
| 876 | if (nargs == 1) { |
| 877 | load_persist_props(); |
| 878 | return 0; |
| 879 | } |
| 880 | return -1; |
| 881 | } |
| 882 | |
| Riley Andrews | e4b7b29 | 2014-06-16 15:06:21 -0700 | [diff] [blame] | 883 | int do_load_all_props(int nargs, char **args) { |
| 884 | if (nargs == 1) { |
| 885 | load_all_props(); |
| 886 | return 0; |
| 887 | } |
| 888 | return -1; |
| 889 | } |
| 890 | |
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 891 | int do_wait(int nargs, char **args) |
| 892 | { |
| 893 | if (nargs == 2) { |
| 894 | return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT); |
| Patrick McCormick | 96d0a4d | 2011-02-04 10:51:39 -0800 | [diff] [blame] | 895 | } else if (nargs == 3) { |
| 896 | return wait_for_file(args[1], atoi(args[2])); |
| 897 | } else |
| 898 | return -1; |
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 899 | } |
| Paul Lawrence | 6095afc | 2015-02-25 15:09:39 -0800 | [diff] [blame^] | 900 | |
| 901 | int do_installkey(int nargs, char **args) |
| 902 | { |
| 903 | if (nargs == 2) { |
| 904 | return e4crypt_install_key(args[1]); |
| 905 | } |
| 906 | |
| 907 | return -1; |
| 908 | } |