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> |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 32 | #include <sys/wait.h> |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 33 | #include <linux/loop.h> |
Ken Sumrall | 7bc6e9e | 2011-05-26 20:01:39 -0700 | [diff] [blame] | 34 | #include <cutils/partition_utils.h> |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 35 | #include <sys/system_properties.h> |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 36 | #include <fs_mgr.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 38 | #include <selinux/selinux.h> |
| 39 | #include <selinux/label.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 40 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | #include "init.h" |
| 42 | #include "keywords.h" |
| 43 | #include "property_service.h" |
| 44 | #include "devices.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 45 | #include "init_parser.h" |
Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame] | 46 | #include "util.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 47 | #include "log.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | |
| 49 | #include <private/android_filesystem_config.h> |
| 50 | |
| 51 | void add_environment(const char *name, const char *value); |
| 52 | |
| 53 | extern int init_module(void *, unsigned long, const char *); |
| 54 | |
| 55 | static int write_file(const char *path, const char *value) |
| 56 | { |
| 57 | int fd, ret, len; |
| 58 | |
| 59 | fd = open(path, O_WRONLY|O_CREAT, 0622); |
| 60 | |
| 61 | if (fd < 0) |
Mike Chan | 008abac | 2009-06-29 20:30:55 -0700 | [diff] [blame] | 62 | return -errno; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 | |
| 64 | len = strlen(value); |
| 65 | |
| 66 | do { |
| 67 | ret = write(fd, value, len); |
| 68 | } while (ret < 0 && errno == EINTR); |
| 69 | |
| 70 | close(fd); |
| 71 | if (ret < 0) { |
Mike Chan | 008abac | 2009-06-29 20:30:55 -0700 | [diff] [blame] | 72 | return -errno; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 | } else { |
| 74 | return 0; |
| 75 | } |
| 76 | } |
| 77 | |
Benoit Goby | 5890301 | 2012-03-28 18:15:56 -0700 | [diff] [blame] | 78 | static int _open(const char *path) |
| 79 | { |
| 80 | int fd; |
| 81 | |
| 82 | fd = open(path, O_RDONLY | O_NOFOLLOW); |
| 83 | if (fd < 0) |
| 84 | fd = open(path, O_WRONLY | O_NOFOLLOW); |
| 85 | |
| 86 | return fd; |
| 87 | } |
| 88 | |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 89 | static int _chown(const char *path, unsigned int uid, unsigned int gid) |
| 90 | { |
| 91 | int fd; |
| 92 | int ret; |
| 93 | |
Benoit Goby | 5890301 | 2012-03-28 18:15:56 -0700 | [diff] [blame] | 94 | fd = _open(path); |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 95 | if (fd < 0) { |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | ret = fchown(fd, uid, gid); |
| 100 | if (ret < 0) { |
| 101 | int errno_copy = errno; |
| 102 | close(fd); |
| 103 | errno = errno_copy; |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | close(fd); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int _chmod(const char *path, mode_t mode) |
| 113 | { |
| 114 | int fd; |
| 115 | int ret; |
| 116 | |
Benoit Goby | 5890301 | 2012-03-28 18:15:56 -0700 | [diff] [blame] | 117 | fd = _open(path); |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 118 | if (fd < 0) { |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | ret = fchmod(fd, mode); |
| 123 | if (ret < 0) { |
| 124 | int errno_copy = errno; |
| 125 | close(fd); |
| 126 | errno = errno_copy; |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | close(fd); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 135 | static int insmod(const char *filename, char *options) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 136 | { |
| 137 | void *module; |
| 138 | unsigned size; |
| 139 | int ret; |
| 140 | |
| 141 | module = read_file(filename, &size); |
| 142 | if (!module) |
| 143 | return -1; |
| 144 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 145 | ret = init_module(module, size, options); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 146 | |
| 147 | free(module); |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | static int setkey(struct kbentry *kbe) |
| 153 | { |
| 154 | int fd, ret; |
| 155 | |
| 156 | fd = open("/dev/tty0", O_RDWR | O_SYNC); |
| 157 | if (fd < 0) |
| 158 | return -1; |
| 159 | |
| 160 | ret = ioctl(fd, KDSKBENT, kbe); |
| 161 | |
| 162 | close(fd); |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | static int __ifupdown(const char *interface, int up) |
| 167 | { |
| 168 | struct ifreq ifr; |
| 169 | int s, ret; |
| 170 | |
| 171 | strlcpy(ifr.ifr_name, interface, IFNAMSIZ); |
| 172 | |
| 173 | s = socket(AF_INET, SOCK_DGRAM, 0); |
| 174 | if (s < 0) |
| 175 | return -1; |
| 176 | |
| 177 | ret = ioctl(s, SIOCGIFFLAGS, &ifr); |
| 178 | if (ret < 0) { |
| 179 | goto done; |
| 180 | } |
| 181 | |
| 182 | if (up) |
| 183 | ifr.ifr_flags |= IFF_UP; |
| 184 | else |
| 185 | ifr.ifr_flags &= ~IFF_UP; |
| 186 | |
| 187 | ret = ioctl(s, SIOCSIFFLAGS, &ifr); |
| 188 | |
| 189 | done: |
| 190 | close(s); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | static void service_start_if_not_disabled(struct service *svc) |
| 195 | { |
| 196 | if (!(svc->flags & SVC_DISABLED)) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 197 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
Jay Freeman (saurik) | e7cb137 | 2008-11-17 06:41:10 +0000 | [diff] [blame] | 201 | int do_chdir(int nargs, char **args) |
| 202 | { |
| 203 | chdir(args[1]); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | int do_chroot(int nargs, char **args) |
| 208 | { |
| 209 | chroot(args[1]); |
| 210 | return 0; |
| 211 | } |
| 212 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 213 | int do_class_start(int nargs, char **args) |
| 214 | { |
| 215 | /* Starting a class does not start services |
| 216 | * which are explicitly disabled. They must |
| 217 | * be started individually. |
| 218 | */ |
| 219 | service_for_each_class(args[1], service_start_if_not_disabled); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | int do_class_stop(int nargs, char **args) |
| 224 | { |
| 225 | service_for_each_class(args[1], service_stop); |
| 226 | return 0; |
| 227 | } |
| 228 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 229 | int do_class_reset(int nargs, char **args) |
| 230 | { |
| 231 | service_for_each_class(args[1], service_reset); |
| 232 | return 0; |
| 233 | } |
| 234 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 235 | int do_domainname(int nargs, char **args) |
| 236 | { |
| 237 | return write_file("/proc/sys/kernel/domainname", args[1]); |
| 238 | } |
| 239 | |
| 240 | int do_exec(int nargs, char **args) |
| 241 | { |
| 242 | return -1; |
| 243 | } |
| 244 | |
| 245 | int do_export(int nargs, char **args) |
| 246 | { |
| 247 | add_environment(args[1], args[2]); |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | int do_hostname(int nargs, char **args) |
| 252 | { |
| 253 | return write_file("/proc/sys/kernel/hostname", args[1]); |
| 254 | } |
| 255 | |
| 256 | int do_ifup(int nargs, char **args) |
| 257 | { |
| 258 | return __ifupdown(args[1], 1); |
| 259 | } |
| 260 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 261 | |
| 262 | static int do_insmod_inner(int nargs, char **args, int opt_len) |
| 263 | { |
| 264 | char options[opt_len + 1]; |
| 265 | int i; |
| 266 | |
| 267 | options[0] = '\0'; |
| 268 | if (nargs > 2) { |
| 269 | strcpy(options, args[2]); |
| 270 | for (i = 3; i < nargs; ++i) { |
| 271 | strcat(options, " "); |
| 272 | strcat(options, args[i]); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return insmod(args[1], options); |
| 277 | } |
| 278 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 279 | int do_insmod(int nargs, char **args) |
| 280 | { |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 281 | int i; |
| 282 | int size = 0; |
| 283 | |
| 284 | if (nargs > 2) { |
| 285 | for (i = 2; i < nargs; ++i) |
| 286 | size += strlen(args[i]) + 1; |
| 287 | } |
| 288 | |
| 289 | return do_insmod_inner(nargs, args, size); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 290 | } |
| 291 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 292 | int do_mkdir(int nargs, char **args) |
| 293 | { |
| 294 | mode_t mode = 0755; |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 295 | int ret; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 296 | |
| 297 | /* mkdir <path> [mode] [owner] [group] */ |
| 298 | |
| 299 | if (nargs >= 3) { |
| 300 | mode = strtoul(args[2], 0, 8); |
| 301 | } |
| 302 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 303 | ret = make_dir(args[1], mode); |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 304 | /* chmod in case the directory already exists */ |
| 305 | if (ret == -1 && errno == EEXIST) { |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 306 | ret = _chmod(args[1], mode); |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 307 | } |
| 308 | if (ret == -1) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 309 | return -errno; |
| 310 | } |
| 311 | |
| 312 | if (nargs >= 4) { |
| 313 | uid_t uid = decode_uid(args[3]); |
| 314 | gid_t gid = -1; |
| 315 | |
| 316 | if (nargs == 5) { |
| 317 | gid = decode_uid(args[4]); |
| 318 | } |
| 319 | |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 320 | if (_chown(args[1], uid, gid) < 0) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 321 | return -errno; |
| 322 | } |
Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 323 | |
| 324 | /* chown may have cleared S_ISUID and S_ISGID, chmod again */ |
| 325 | if (mode & (S_ISUID | S_ISGID)) { |
| 326 | ret = _chmod(args[1], mode); |
| 327 | if (ret == -1) { |
| 328 | return -errno; |
| 329 | } |
| 330 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static struct { |
| 337 | const char *name; |
| 338 | unsigned flag; |
| 339 | } mount_flags[] = { |
| 340 | { "noatime", MS_NOATIME }, |
Lars Svensson | b6ee25e | 2011-07-14 13:39:09 +0200 | [diff] [blame] | 341 | { "noexec", MS_NOEXEC }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 342 | { "nosuid", MS_NOSUID }, |
| 343 | { "nodev", MS_NODEV }, |
| 344 | { "nodiratime", MS_NODIRATIME }, |
| 345 | { "ro", MS_RDONLY }, |
| 346 | { "rw", 0 }, |
| 347 | { "remount", MS_REMOUNT }, |
Jeff Sharkey | e50ac5f | 2012-08-14 11:34:34 -0700 | [diff] [blame] | 348 | { "bind", MS_BIND }, |
| 349 | { "rec", MS_REC }, |
| 350 | { "unbindable", MS_UNBINDABLE }, |
| 351 | { "private", MS_PRIVATE }, |
| 352 | { "slave", MS_SLAVE }, |
| 353 | { "shared", MS_SHARED }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 354 | { "defaults", 0 }, |
| 355 | { 0, 0 }, |
| 356 | }; |
| 357 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 358 | #define DATA_MNT_POINT "/data" |
| 359 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 360 | /* mount <type> <device> <path> <flags ...> <options> */ |
| 361 | int do_mount(int nargs, char **args) |
| 362 | { |
| 363 | char tmp[64]; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 364 | char *source, *target, *system; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 365 | char *options = NULL; |
| 366 | unsigned flags = 0; |
| 367 | int n, i; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 368 | int wait = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 369 | |
| 370 | for (n = 4; n < nargs; n++) { |
| 371 | for (i = 0; mount_flags[i].name; i++) { |
| 372 | if (!strcmp(args[n], mount_flags[i].name)) { |
| 373 | flags |= mount_flags[i].flag; |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 378 | if (!mount_flags[i].name) { |
| 379 | if (!strcmp(args[n], "wait")) |
| 380 | wait = 1; |
| 381 | /* if our last argument isn't a flag, wolf it up as an option string */ |
| 382 | else if (n + 1 == nargs) |
| 383 | options = args[n]; |
| 384 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 387 | system = args[1]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 388 | source = args[2]; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 389 | target = args[3]; |
| 390 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 391 | if (!strncmp(source, "mtd@", 4)) { |
| 392 | n = mtd_name_to_number(source + 4); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 393 | if (n < 0) { |
| 394 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 395 | } |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 396 | |
| 397 | sprintf(tmp, "/dev/block/mtdblock%d", n); |
| 398 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 399 | if (wait) |
| 400 | wait_for_file(tmp, COMMAND_RETRY_TIMEOUT); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 401 | if (mount(tmp, target, system, flags, options) < 0) { |
| 402 | return -1; |
| 403 | } |
| 404 | |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 405 | goto exit_success; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 406 | } else if (!strncmp(source, "loop@", 5)) { |
| 407 | int mode, loop, fd; |
| 408 | struct loop_info info; |
| 409 | |
| 410 | mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; |
| 411 | fd = open(source + 5, mode); |
| 412 | if (fd < 0) { |
| 413 | return -1; |
| 414 | } |
| 415 | |
| 416 | for (n = 0; ; n++) { |
| 417 | sprintf(tmp, "/dev/block/loop%d", n); |
| 418 | loop = open(tmp, mode); |
| 419 | if (loop < 0) { |
| 420 | return -1; |
| 421 | } |
| 422 | |
| 423 | /* if it is a blank loop device */ |
| 424 | if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { |
| 425 | /* if it becomes our loop device */ |
| 426 | if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { |
| 427 | close(fd); |
| 428 | |
| 429 | if (mount(tmp, target, system, flags, options) < 0) { |
| 430 | ioctl(loop, LOOP_CLR_FD, 0); |
| 431 | close(loop); |
| 432 | return -1; |
| 433 | } |
| 434 | |
| 435 | close(loop); |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 436 | goto exit_success; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | close(loop); |
| 441 | } |
| 442 | |
| 443 | close(fd); |
| 444 | ERROR("out of loopback devices"); |
| 445 | return -1; |
| 446 | } else { |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 447 | if (wait) |
| 448 | wait_for_file(source, COMMAND_RETRY_TIMEOUT); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 449 | if (mount(source, target, system, flags, options) < 0) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 450 | return -1; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 451 | } |
| 452 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 453 | } |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 454 | |
| 455 | exit_success: |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 456 | return 0; |
| 457 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 460 | int do_mount_all(int nargs, char **args) |
| 461 | { |
| 462 | pid_t pid; |
| 463 | int ret = -1; |
| 464 | int child_ret = -1; |
| 465 | int status; |
| 466 | const char *prop; |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 467 | struct fstab *fstab; |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 468 | |
| 469 | if (nargs != 2) { |
| 470 | return -1; |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and |
| 475 | * do the call in the child to provide protection to the main init |
| 476 | * process if anything goes wrong (crash or memory leak), and wait for |
| 477 | * the child to finish in the parent. |
| 478 | */ |
| 479 | pid = fork(); |
| 480 | if (pid > 0) { |
| 481 | /* Parent. Wait for the child to return */ |
| 482 | waitpid(pid, &status, 0); |
| 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 | } |
| 497 | exit(child_ret); |
| 498 | } else { |
| 499 | /* fork failed, return an error */ |
| 500 | return -1; |
| 501 | } |
| 502 | |
| 503 | /* ret is 1 if the device is encrypted, 0 if not, and -1 on error */ |
| 504 | if (ret == 1) { |
| 505 | property_set("ro.crypto.state", "encrypted"); |
| 506 | property_set("vold.decrypt", "1"); |
| 507 | } else if (ret == 0) { |
| 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); |
| 513 | } |
| 514 | |
| 515 | return ret; |
| 516 | } |
| 517 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 518 | int do_setcon(int nargs, char **args) { |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 519 | if (is_selinux_enabled() <= 0) |
| 520 | return 0; |
| 521 | if (setcon(args[1]) < 0) { |
| 522 | return -errno; |
| 523 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | int do_setenforce(int nargs, char **args) { |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 528 | if (is_selinux_enabled() <= 0) |
| 529 | return 0; |
| 530 | if (security_setenforce(atoi(args[1])) < 0) { |
| 531 | return -errno; |
| 532 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 533 | return 0; |
| 534 | } |
| 535 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 536 | int do_setkey(int nargs, char **args) |
| 537 | { |
| 538 | struct kbentry kbe; |
| 539 | kbe.kb_table = strtoul(args[1], 0, 0); |
| 540 | kbe.kb_index = strtoul(args[2], 0, 0); |
| 541 | kbe.kb_value = strtoul(args[3], 0, 0); |
| 542 | return setkey(&kbe); |
| 543 | } |
| 544 | |
| 545 | int do_setprop(int nargs, char **args) |
| 546 | { |
Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 547 | const char *name = args[1]; |
| 548 | const char *value = args[2]; |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 549 | char prop_val[PROP_VALUE_MAX]; |
| 550 | int ret; |
Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 551 | |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 552 | ret = expand_props(prop_val, value, sizeof(prop_val)); |
| 553 | if (ret) { |
| 554 | ERROR("cannot expand '%s' while assigning to '%s'\n", value, name); |
| 555 | return -EINVAL; |
Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 556 | } |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 557 | property_set(name, prop_val); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | int do_setrlimit(int nargs, char **args) |
| 562 | { |
| 563 | struct rlimit limit; |
| 564 | int resource; |
| 565 | resource = atoi(args[1]); |
| 566 | limit.rlim_cur = atoi(args[2]); |
| 567 | limit.rlim_max = atoi(args[3]); |
| 568 | return setrlimit(resource, &limit); |
| 569 | } |
| 570 | |
| 571 | int do_start(int nargs, char **args) |
| 572 | { |
| 573 | struct service *svc; |
| 574 | svc = service_find_by_name(args[1]); |
| 575 | if (svc) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 576 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 577 | } |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | int do_stop(int nargs, char **args) |
| 582 | { |
| 583 | struct service *svc; |
| 584 | svc = service_find_by_name(args[1]); |
| 585 | if (svc) { |
| 586 | service_stop(svc); |
| 587 | } |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | int do_restart(int nargs, char **args) |
| 592 | { |
| 593 | struct service *svc; |
| 594 | svc = service_find_by_name(args[1]); |
| 595 | if (svc) { |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 596 | service_restart(svc); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 597 | } |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | int do_trigger(int nargs, char **args) |
| 602 | { |
Jay Freeman (saurik) | 11e1c42 | 2008-11-17 06:35:08 +0000 | [diff] [blame] | 603 | 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] | 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | int do_symlink(int nargs, char **args) |
| 608 | { |
| 609 | return symlink(args[1], args[2]); |
| 610 | } |
| 611 | |
Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 612 | int do_rm(int nargs, char **args) |
| 613 | { |
| 614 | return unlink(args[1]); |
| 615 | } |
| 616 | |
| 617 | int do_rmdir(int nargs, char **args) |
| 618 | { |
| 619 | return rmdir(args[1]); |
| 620 | } |
| 621 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 622 | int do_sysclktz(int nargs, char **args) |
| 623 | { |
| 624 | struct timezone tz; |
| 625 | |
| 626 | if (nargs != 2) |
| 627 | return -1; |
| 628 | |
| 629 | memset(&tz, 0, sizeof(tz)); |
| 630 | tz.tz_minuteswest = atoi(args[1]); |
| 631 | if (settimeofday(NULL, &tz)) |
| 632 | return -1; |
| 633 | return 0; |
| 634 | } |
| 635 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 636 | int do_write(int nargs, char **args) |
| 637 | { |
Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 638 | const char *path = args[1]; |
| 639 | const char *value = args[2]; |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 640 | char prop_val[PROP_VALUE_MAX]; |
| 641 | int ret; |
Mike Lockwood | 2c4d5dc | 2011-06-07 17:30:11 -0700 | [diff] [blame] | 642 | |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 643 | ret = expand_props(prop_val, value, sizeof(prop_val)); |
| 644 | if (ret) { |
| 645 | ERROR("cannot expand '%s' while writing to '%s'\n", value, path); |
| 646 | return -EINVAL; |
| 647 | } |
| 648 | return write_file(path, prop_val); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 649 | } |
| 650 | |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 651 | int do_copy(int nargs, char **args) |
| 652 | { |
| 653 | char *buffer = NULL; |
| 654 | int rc = 0; |
| 655 | int fd1 = -1, fd2 = -1; |
| 656 | struct stat info; |
| 657 | int brtw, brtr; |
| 658 | char *p; |
| 659 | |
| 660 | if (nargs != 3) |
| 661 | return -1; |
| 662 | |
| 663 | if (stat(args[1], &info) < 0) |
| 664 | return -1; |
| 665 | |
| 666 | if ((fd1 = open(args[1], O_RDONLY)) < 0) |
| 667 | goto out_err; |
| 668 | |
Tom Zhu | 4833d9f | 2009-09-28 19:53:12 -0500 | [diff] [blame] | 669 | if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0) |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 670 | goto out_err; |
| 671 | |
| 672 | if (!(buffer = malloc(info.st_size))) |
| 673 | goto out_err; |
| 674 | |
| 675 | p = buffer; |
| 676 | brtr = info.st_size; |
| 677 | while(brtr) { |
| 678 | rc = read(fd1, p, brtr); |
| 679 | if (rc < 0) |
| 680 | goto out_err; |
| 681 | if (rc == 0) |
| 682 | break; |
| 683 | p += rc; |
| 684 | brtr -= rc; |
| 685 | } |
| 686 | |
| 687 | p = buffer; |
| 688 | brtw = info.st_size; |
| 689 | while(brtw) { |
| 690 | rc = write(fd2, p, brtw); |
| 691 | if (rc < 0) |
| 692 | goto out_err; |
| 693 | if (rc == 0) |
| 694 | break; |
| 695 | p += rc; |
| 696 | brtw -= rc; |
| 697 | } |
| 698 | |
| 699 | rc = 0; |
| 700 | goto out; |
| 701 | out_err: |
| 702 | rc = -1; |
| 703 | out: |
| 704 | if (buffer) |
| 705 | free(buffer); |
| 706 | if (fd1 >= 0) |
| 707 | close(fd1); |
| 708 | if (fd2 >= 0) |
| 709 | close(fd2); |
| 710 | return rc; |
| 711 | } |
| 712 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 713 | int do_chown(int nargs, char **args) { |
| 714 | /* GID is optional. */ |
| 715 | if (nargs == 3) { |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 716 | if (_chown(args[2], decode_uid(args[1]), -1) < 0) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 717 | return -errno; |
| 718 | } else if (nargs == 4) { |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 719 | if (_chown(args[3], decode_uid(args[1]), decode_uid(args[2])) < 0) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 720 | return -errno; |
| 721 | } else { |
| 722 | return -1; |
| 723 | } |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | static mode_t get_mode(const char *s) { |
| 728 | mode_t mode = 0; |
| 729 | while (*s) { |
| 730 | if (*s >= '0' && *s <= '7') { |
| 731 | mode = (mode<<3) | (*s-'0'); |
| 732 | } else { |
| 733 | return -1; |
| 734 | } |
| 735 | s++; |
| 736 | } |
| 737 | return mode; |
| 738 | } |
| 739 | |
| 740 | int do_chmod(int nargs, char **args) { |
| 741 | mode_t mode = get_mode(args[1]); |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 742 | if (_chmod(args[2], mode) < 0) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 743 | return -errno; |
| 744 | } |
| 745 | return 0; |
| 746 | } |
| 747 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 748 | int do_restorecon(int nargs, char **args) { |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 749 | int i; |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame^] | 750 | int ret = 0; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 751 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 752 | for (i = 1; i < nargs; i++) { |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 753 | if (restorecon(args[i]) < 0) |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame^] | 754 | ret = -errno; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 755 | } |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame^] | 756 | return ret; |
| 757 | } |
| 758 | |
| 759 | int do_restorecon_recursive(int nargs, char **args) { |
| 760 | int i; |
| 761 | int ret = 0; |
| 762 | |
| 763 | for (i = 1; i < nargs; i++) { |
| 764 | if (restorecon_recursive(args[i]) < 0) |
| 765 | ret = -errno; |
| 766 | } |
| 767 | return ret; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | int do_setsebool(int nargs, char **args) { |
Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 771 | const char *name = args[1]; |
| 772 | const char *value = args[2]; |
| 773 | SELboolean b; |
| 774 | int ret; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 775 | |
| 776 | if (is_selinux_enabled() <= 0) |
| 777 | return 0; |
| 778 | |
Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 779 | b.name = name; |
| 780 | if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on")) |
| 781 | b.value = 1; |
| 782 | else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off")) |
| 783 | b.value = 0; |
| 784 | else { |
| 785 | ERROR("setsebool: invalid value %s\n", value); |
| 786 | return -EINVAL; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 787 | } |
| 788 | |
Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 789 | if (security_set_boolean_list(1, &b, 0) < 0) { |
| 790 | ret = -errno; |
| 791 | ERROR("setsebool: could not set %s to %s\n", name, value); |
| 792 | return ret; |
| 793 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 794 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 795 | return 0; |
| 796 | } |
| 797 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 798 | int do_loglevel(int nargs, char **args) { |
| 799 | if (nargs == 2) { |
Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 800 | klog_set_level(atoi(args[1])); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 801 | return 0; |
| 802 | } |
| 803 | return -1; |
| 804 | } |
| 805 | |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 806 | int do_load_persist_props(int nargs, char **args) { |
| 807 | if (nargs == 1) { |
| 808 | load_persist_props(); |
| 809 | return 0; |
| 810 | } |
| 811 | return -1; |
| 812 | } |
| 813 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 814 | int do_wait(int nargs, char **args) |
| 815 | { |
| 816 | if (nargs == 2) { |
| 817 | return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT); |
Patrick McCormick | 96d0a4d | 2011-02-04 10:51:39 -0800 | [diff] [blame] | 818 | } else if (nargs == 3) { |
| 819 | return wait_for_file(args[1], atoi(args[2])); |
| 820 | } else |
| 821 | return -1; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 822 | } |