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