blob: bfdd65402738d4c09a3c108885b4772dc56d62a4 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
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)e520d032008-11-20 03:37:30 +000032#include <linux/loop.h>
Ken Sumrall7bc6e9e2011-05-26 20:01:39 -070033#include <cutils/partition_utils.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070034
35#include "init.h"
36#include "keywords.h"
37#include "property_service.h"
38#include "devices.h"
Colin Cross6310a822010-04-20 14:29:05 -070039#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070040#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070041#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042
43#include <private/android_filesystem_config.h>
44
45void add_environment(const char *name, const char *value);
46
47extern int init_module(void *, unsigned long, const char *);
48
49static int write_file(const char *path, const char *value)
50{
51 int fd, ret, len;
52
53 fd = open(path, O_WRONLY|O_CREAT, 0622);
54
55 if (fd < 0)
Mike Chan008abac2009-06-29 20:30:55 -070056 return -errno;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057
58 len = strlen(value);
59
60 do {
61 ret = write(fd, value, len);
62 } while (ret < 0 && errno == EINTR);
63
64 close(fd);
65 if (ret < 0) {
Mike Chan008abac2009-06-29 20:30:55 -070066 return -errno;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067 } else {
68 return 0;
69 }
70}
71
The Android Open Source Project35237d12008-12-17 18:08:08 -080072static int insmod(const char *filename, char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073{
74 void *module;
75 unsigned size;
76 int ret;
77
78 module = read_file(filename, &size);
79 if (!module)
80 return -1;
81
The Android Open Source Project35237d12008-12-17 18:08:08 -080082 ret = init_module(module, size, options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083
84 free(module);
85
86 return ret;
87}
88
89static int setkey(struct kbentry *kbe)
90{
91 int fd, ret;
92
93 fd = open("/dev/tty0", O_RDWR | O_SYNC);
94 if (fd < 0)
95 return -1;
96
97 ret = ioctl(fd, KDSKBENT, kbe);
98
99 close(fd);
100 return ret;
101}
102
103static int __ifupdown(const char *interface, int up)
104{
105 struct ifreq ifr;
106 int s, ret;
107
108 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
109
110 s = socket(AF_INET, SOCK_DGRAM, 0);
111 if (s < 0)
112 return -1;
113
114 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
115 if (ret < 0) {
116 goto done;
117 }
118
119 if (up)
120 ifr.ifr_flags |= IFF_UP;
121 else
122 ifr.ifr_flags &= ~IFF_UP;
123
124 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
125
126done:
127 close(s);
128 return ret;
129}
130
131static void service_start_if_not_disabled(struct service *svc)
132{
133 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700134 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700135 }
136}
137
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000138int do_chdir(int nargs, char **args)
139{
140 chdir(args[1]);
141 return 0;
142}
143
144int do_chroot(int nargs, char **args)
145{
146 chroot(args[1]);
147 return 0;
148}
149
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700150int do_class_start(int nargs, char **args)
151{
152 /* Starting a class does not start services
153 * which are explicitly disabled. They must
154 * be started individually.
155 */
156 service_for_each_class(args[1], service_start_if_not_disabled);
157 return 0;
158}
159
160int do_class_stop(int nargs, char **args)
161{
162 service_for_each_class(args[1], service_stop);
163 return 0;
164}
165
Ken Sumrall752923c2010-12-03 16:33:31 -0800166int do_class_reset(int nargs, char **args)
167{
168 service_for_each_class(args[1], service_reset);
169 return 0;
170}
171
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700172int do_domainname(int nargs, char **args)
173{
174 return write_file("/proc/sys/kernel/domainname", args[1]);
175}
176
177int do_exec(int nargs, char **args)
178{
179 return -1;
180}
181
182int do_export(int nargs, char **args)
183{
184 add_environment(args[1], args[2]);
185 return 0;
186}
187
188int do_hostname(int nargs, char **args)
189{
190 return write_file("/proc/sys/kernel/hostname", args[1]);
191}
192
193int do_ifup(int nargs, char **args)
194{
195 return __ifupdown(args[1], 1);
196}
197
The Android Open Source Project35237d12008-12-17 18:08:08 -0800198
199static int do_insmod_inner(int nargs, char **args, int opt_len)
200{
201 char options[opt_len + 1];
202 int i;
203
204 options[0] = '\0';
205 if (nargs > 2) {
206 strcpy(options, args[2]);
207 for (i = 3; i < nargs; ++i) {
208 strcat(options, " ");
209 strcat(options, args[i]);
210 }
211 }
212
213 return insmod(args[1], options);
214}
215
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700216int do_insmod(int nargs, char **args)
217{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800218 int i;
219 int size = 0;
220
221 if (nargs > 2) {
222 for (i = 2; i < nargs; ++i)
223 size += strlen(args[i]) + 1;
224 }
225
226 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227}
228
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229int do_mkdir(int nargs, char **args)
230{
231 mode_t mode = 0755;
232
233 /* mkdir <path> [mode] [owner] [group] */
234
235 if (nargs >= 3) {
236 mode = strtoul(args[2], 0, 8);
237 }
238
239 if (mkdir(args[1], mode)) {
240 return -errno;
241 }
242
243 if (nargs >= 4) {
244 uid_t uid = decode_uid(args[3]);
245 gid_t gid = -1;
246
247 if (nargs == 5) {
248 gid = decode_uid(args[4]);
249 }
250
251 if (chown(args[1], uid, gid)) {
252 return -errno;
253 }
254 }
255
256 return 0;
257}
258
259static struct {
260 const char *name;
261 unsigned flag;
262} mount_flags[] = {
263 { "noatime", MS_NOATIME },
264 { "nosuid", MS_NOSUID },
265 { "nodev", MS_NODEV },
266 { "nodiratime", MS_NODIRATIME },
267 { "ro", MS_RDONLY },
268 { "rw", 0 },
269 { "remount", MS_REMOUNT },
270 { "defaults", 0 },
271 { 0, 0 },
272};
273
Ken Sumrall752923c2010-12-03 16:33:31 -0800274#define DATA_MNT_POINT "/data"
275
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276/* mount <type> <device> <path> <flags ...> <options> */
277int do_mount(int nargs, char **args)
278{
279 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000280 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281 char *options = NULL;
282 unsigned flags = 0;
283 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700284 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285
286 for (n = 4; n < nargs; n++) {
287 for (i = 0; mount_flags[i].name; i++) {
288 if (!strcmp(args[n], mount_flags[i].name)) {
289 flags |= mount_flags[i].flag;
290 break;
291 }
292 }
293
Colin Crosscd0f1732010-04-19 17:10:24 -0700294 if (!mount_flags[i].name) {
295 if (!strcmp(args[n], "wait"))
296 wait = 1;
297 /* if our last argument isn't a flag, wolf it up as an option string */
298 else if (n + 1 == nargs)
299 options = args[n];
300 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 }
302
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000303 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700304 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000305 target = args[3];
306
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 if (!strncmp(source, "mtd@", 4)) {
308 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000309 if (n < 0) {
310 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700311 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000312
313 sprintf(tmp, "/dev/block/mtdblock%d", n);
314
Colin Crosscd0f1732010-04-19 17:10:24 -0700315 if (wait)
316 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000317 if (mount(tmp, target, system, flags, options) < 0) {
318 return -1;
319 }
320
Ken Sumralldd4d7862011-02-17 18:09:47 -0800321 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000322 } else if (!strncmp(source, "loop@", 5)) {
323 int mode, loop, fd;
324 struct loop_info info;
325
326 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
327 fd = open(source + 5, mode);
328 if (fd < 0) {
329 return -1;
330 }
331
332 for (n = 0; ; n++) {
333 sprintf(tmp, "/dev/block/loop%d", n);
334 loop = open(tmp, mode);
335 if (loop < 0) {
336 return -1;
337 }
338
339 /* if it is a blank loop device */
340 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
341 /* if it becomes our loop device */
342 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
343 close(fd);
344
345 if (mount(tmp, target, system, flags, options) < 0) {
346 ioctl(loop, LOOP_CLR_FD, 0);
347 close(loop);
348 return -1;
349 }
350
351 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800352 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000353 }
354 }
355
356 close(loop);
357 }
358
359 close(fd);
360 ERROR("out of loopback devices");
361 return -1;
362 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700363 if (wait)
364 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000365 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall7bc6e9e2011-05-26 20:01:39 -0700366 /* If this fails, it may be an encrypted filesystem
367 * or it could just be wiped. If wiped, that will be
368 * handled later in the boot process.
Ken Sumrall752923c2010-12-03 16:33:31 -0800369 * We only support encrypting /data. Check
370 * if we're trying to mount it, and if so,
371 * assume it's encrypted, mount a tmpfs instead.
372 * Then save the orig mount parms in properties
373 * for vold to query when it mounts the real
374 * encrypted /data.
375 */
Ken Sumrall7bc6e9e2011-05-26 20:01:39 -0700376 if (!strcmp(target, DATA_MNT_POINT) && !partition_wiped(source)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800377 const char *tmpfs_options;
378
379 tmpfs_options = property_get("ro.crypto.tmpfs_options");
380
381 if (mount("tmpfs", target, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV,
382 tmpfs_options) < 0) {
383 return -1;
384 }
385
386 /* Set the property that triggers the framework to do a minimal
387 * startup and ask the user for a password
388 */
Ken Sumrall4e84d3b2011-01-14 12:44:09 -0800389 property_set("ro.crypto.state", "encrypted");
Ken Sumrall752923c2010-12-03 16:33:31 -0800390 property_set("vold.decrypt", "1");
391 } else {
392 return -1;
393 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000394 }
395
Ken Sumrall752923c2010-12-03 16:33:31 -0800396 if (!strcmp(target, DATA_MNT_POINT)) {
397 char fs_flags[32];
398
399 /* Save the original mount options */
400 property_set("ro.crypto.fs_type", system);
401 property_set("ro.crypto.fs_real_blkdev", source);
402 property_set("ro.crypto.fs_mnt_point", target);
403 if (options) {
404 property_set("ro.crypto.fs_options", options);
405 }
406 snprintf(fs_flags, sizeof(fs_flags), "0x%8.8x", flags);
407 property_set("ro.crypto.fs_flags", fs_flags);
408 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700409 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800410
411exit_success:
412 /* If not running encrypted, then set the property saying we are
413 * unencrypted, and also trigger the action for a nonencrypted system.
414 */
415 if (!strcmp(target, DATA_MNT_POINT)) {
Ken Sumrallc5c51032011-03-08 17:01:29 -0800416 const char *prop;
417
Ken Sumralldd4d7862011-02-17 18:09:47 -0800418 prop = property_get("ro.crypto.state");
419 if (! prop) {
420 prop = "notset";
421 }
422 if (strcmp(prop, "encrypted")) {
423 property_set("ro.crypto.state", "unencrypted");
424 action_for_each_trigger("nonencrypted", action_add_queue_tail);
425 }
426 }
427
428 return 0;
429
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430}
431
432int do_setkey(int nargs, char **args)
433{
434 struct kbentry kbe;
435 kbe.kb_table = strtoul(args[1], 0, 0);
436 kbe.kb_index = strtoul(args[2], 0, 0);
437 kbe.kb_value = strtoul(args[3], 0, 0);
438 return setkey(&kbe);
439}
440
441int do_setprop(int nargs, char **args)
442{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700443 const char *name = args[1];
444 const char *value = args[2];
445
446 if (value[0] == '$') {
447 /* Use the value of a system property if value starts with '$' */
448 value++;
449 if (value[0] != '$') {
450 value = property_get(value);
451 if (!value) {
452 ERROR("property %s has no value for assigning to %s\n", value, name);
453 return -EINVAL;
454 }
455 } /* else fall through to support double '$' prefix for setting properties
456 * to string literals that start with '$'
457 */
458 }
459
460 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700461 return 0;
462}
463
464int do_setrlimit(int nargs, char **args)
465{
466 struct rlimit limit;
467 int resource;
468 resource = atoi(args[1]);
469 limit.rlim_cur = atoi(args[2]);
470 limit.rlim_max = atoi(args[3]);
471 return setrlimit(resource, &limit);
472}
473
474int do_start(int nargs, char **args)
475{
476 struct service *svc;
477 svc = service_find_by_name(args[1]);
478 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700479 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700480 }
481 return 0;
482}
483
484int do_stop(int nargs, char **args)
485{
486 struct service *svc;
487 svc = service_find_by_name(args[1]);
488 if (svc) {
489 service_stop(svc);
490 }
491 return 0;
492}
493
494int do_restart(int nargs, char **args)
495{
496 struct service *svc;
497 svc = service_find_by_name(args[1]);
498 if (svc) {
499 service_stop(svc);
San Mehatf24e2522009-05-19 13:30:46 -0700500 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700501 }
502 return 0;
503}
504
505int do_trigger(int nargs, char **args)
506{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000507 action_for_each_trigger(args[1], action_add_queue_tail);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700508 return 0;
509}
510
511int do_symlink(int nargs, char **args)
512{
513 return symlink(args[1], args[2]);
514}
515
Ken Sumrall203bad52011-01-18 17:37:41 -0800516int do_rm(int nargs, char **args)
517{
518 return unlink(args[1]);
519}
520
521int do_rmdir(int nargs, char **args)
522{
523 return rmdir(args[1]);
524}
525
The Android Open Source Project35237d12008-12-17 18:08:08 -0800526int do_sysclktz(int nargs, char **args)
527{
528 struct timezone tz;
529
530 if (nargs != 2)
531 return -1;
532
533 memset(&tz, 0, sizeof(tz));
534 tz.tz_minuteswest = atoi(args[1]);
535 if (settimeofday(NULL, &tz))
536 return -1;
537 return 0;
538}
539
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700540int do_write(int nargs, char **args)
541{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700542 const char *path = args[1];
543 const char *value = args[2];
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700544 if (value[0] == '$') {
545 /* Write the value of a system property if value starts with '$' */
546 value++;
547 if (value[0] != '$') {
548 value = property_get(value);
549 if (!value) {
550 ERROR("property %s has no value for writing to %s\n", value, path);
551 return -EINVAL;
552 }
553 } /* else fall through to support double '$' prefix for writing
554 * string literals that start with '$'
555 */
556 }
557
558 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559}
560
San Mehat7c44fe52009-08-26 16:39:25 -0700561int do_copy(int nargs, char **args)
562{
563 char *buffer = NULL;
564 int rc = 0;
565 int fd1 = -1, fd2 = -1;
566 struct stat info;
567 int brtw, brtr;
568 char *p;
569
570 if (nargs != 3)
571 return -1;
572
573 if (stat(args[1], &info) < 0)
574 return -1;
575
576 if ((fd1 = open(args[1], O_RDONLY)) < 0)
577 goto out_err;
578
Tom Zhu4833d9f2009-09-28 19:53:12 -0500579 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700580 goto out_err;
581
582 if (!(buffer = malloc(info.st_size)))
583 goto out_err;
584
585 p = buffer;
586 brtr = info.st_size;
587 while(brtr) {
588 rc = read(fd1, p, brtr);
589 if (rc < 0)
590 goto out_err;
591 if (rc == 0)
592 break;
593 p += rc;
594 brtr -= rc;
595 }
596
597 p = buffer;
598 brtw = info.st_size;
599 while(brtw) {
600 rc = write(fd2, p, brtw);
601 if (rc < 0)
602 goto out_err;
603 if (rc == 0)
604 break;
605 p += rc;
606 brtw -= rc;
607 }
608
609 rc = 0;
610 goto out;
611out_err:
612 rc = -1;
613out:
614 if (buffer)
615 free(buffer);
616 if (fd1 >= 0)
617 close(fd1);
618 if (fd2 >= 0)
619 close(fd2);
620 return rc;
621}
622
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700623int do_chown(int nargs, char **args) {
624 /* GID is optional. */
625 if (nargs == 3) {
626 if (chown(args[2], decode_uid(args[1]), -1) < 0)
627 return -errno;
628 } else if (nargs == 4) {
629 if (chown(args[3], decode_uid(args[1]), decode_uid(args[2])))
630 return -errno;
631 } else {
632 return -1;
633 }
634 return 0;
635}
636
637static mode_t get_mode(const char *s) {
638 mode_t mode = 0;
639 while (*s) {
640 if (*s >= '0' && *s <= '7') {
641 mode = (mode<<3) | (*s-'0');
642 } else {
643 return -1;
644 }
645 s++;
646 }
647 return mode;
648}
649
650int do_chmod(int nargs, char **args) {
651 mode_t mode = get_mode(args[1]);
652 if (chmod(args[2], mode) < 0) {
653 return -errno;
654 }
655 return 0;
656}
657
658int do_loglevel(int nargs, char **args) {
659 if (nargs == 2) {
660 log_set_level(atoi(args[1]));
661 return 0;
662 }
663 return -1;
664}
665
Ken Sumrallc5c51032011-03-08 17:01:29 -0800666int do_load_persist_props(int nargs, char **args) {
667 if (nargs == 1) {
668 load_persist_props();
669 return 0;
670 }
671 return -1;
672}
673
Colin Crosscd0f1732010-04-19 17:10:24 -0700674int do_wait(int nargs, char **args)
675{
676 if (nargs == 2) {
677 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
678 }
679 return -1;
680}