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