blob: 3781dcdc8d4743f763bbe89da5b84c461f07d1dc [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>
Dima Zavin84bf9af2011-12-20 13:44:41 -080034#include <sys/system_properties.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035
36#include "init.h"
37#include "keywords.h"
38#include "property_service.h"
39#include "devices.h"
Colin Cross6310a822010-04-20 14:29:05 -070040#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070041#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070042#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070043
44#include <private/android_filesystem_config.h>
45
46void add_environment(const char *name, const char *value);
47
48extern int init_module(void *, unsigned long, const char *);
49
50static int write_file(const char *path, const char *value)
51{
52 int fd, ret, len;
53
54 fd = open(path, O_WRONLY|O_CREAT, 0622);
55
56 if (fd < 0)
Mike Chan008abac2009-06-29 20:30:55 -070057 return -errno;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058
59 len = strlen(value);
60
61 do {
62 ret = write(fd, value, len);
63 } while (ret < 0 && errno == EINTR);
64
65 close(fd);
66 if (ret < 0) {
Mike Chan008abac2009-06-29 20:30:55 -070067 return -errno;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068 } else {
69 return 0;
70 }
71}
72
The Android Open Source Project35237d12008-12-17 18:08:08 -080073static int insmod(const char *filename, char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074{
75 void *module;
76 unsigned size;
77 int ret;
78
79 module = read_file(filename, &size);
80 if (!module)
81 return -1;
82
The Android Open Source Project35237d12008-12-17 18:08:08 -080083 ret = init_module(module, size, options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084
85 free(module);
86
87 return ret;
88}
89
90static int setkey(struct kbentry *kbe)
91{
92 int fd, ret;
93
94 fd = open("/dev/tty0", O_RDWR | O_SYNC);
95 if (fd < 0)
96 return -1;
97
98 ret = ioctl(fd, KDSKBENT, kbe);
99
100 close(fd);
101 return ret;
102}
103
104static int __ifupdown(const char *interface, int up)
105{
106 struct ifreq ifr;
107 int s, ret;
108
109 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
110
111 s = socket(AF_INET, SOCK_DGRAM, 0);
112 if (s < 0)
113 return -1;
114
115 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
116 if (ret < 0) {
117 goto done;
118 }
119
120 if (up)
121 ifr.ifr_flags |= IFF_UP;
122 else
123 ifr.ifr_flags &= ~IFF_UP;
124
125 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
126
127done:
128 close(s);
129 return ret;
130}
131
132static void service_start_if_not_disabled(struct service *svc)
133{
134 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700135 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700136 }
137}
138
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000139int do_chdir(int nargs, char **args)
140{
141 chdir(args[1]);
142 return 0;
143}
144
145int do_chroot(int nargs, char **args)
146{
147 chroot(args[1]);
148 return 0;
149}
150
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700151int do_class_start(int nargs, char **args)
152{
153 /* Starting a class does not start services
154 * which are explicitly disabled. They must
155 * be started individually.
156 */
157 service_for_each_class(args[1], service_start_if_not_disabled);
158 return 0;
159}
160
161int do_class_stop(int nargs, char **args)
162{
163 service_for_each_class(args[1], service_stop);
164 return 0;
165}
166
Ken Sumrall752923c2010-12-03 16:33:31 -0800167int do_class_reset(int nargs, char **args)
168{
169 service_for_each_class(args[1], service_reset);
170 return 0;
171}
172
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173int do_domainname(int nargs, char **args)
174{
175 return write_file("/proc/sys/kernel/domainname", args[1]);
176}
177
178int do_exec(int nargs, char **args)
179{
180 return -1;
181}
182
183int do_export(int nargs, char **args)
184{
185 add_environment(args[1], args[2]);
186 return 0;
187}
188
189int do_hostname(int nargs, char **args)
190{
191 return write_file("/proc/sys/kernel/hostname", args[1]);
192}
193
194int do_ifup(int nargs, char **args)
195{
196 return __ifupdown(args[1], 1);
197}
198
The Android Open Source Project35237d12008-12-17 18:08:08 -0800199
200static int do_insmod_inner(int nargs, char **args, int opt_len)
201{
202 char options[opt_len + 1];
203 int i;
204
205 options[0] = '\0';
206 if (nargs > 2) {
207 strcpy(options, args[2]);
208 for (i = 3; i < nargs; ++i) {
209 strcat(options, " ");
210 strcat(options, args[i]);
211 }
212 }
213
214 return insmod(args[1], options);
215}
216
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700217int do_insmod(int nargs, char **args)
218{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800219 int i;
220 int size = 0;
221
222 if (nargs > 2) {
223 for (i = 2; i < nargs; ++i)
224 size += strlen(args[i]) + 1;
225 }
226
227 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700228}
229
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230int do_mkdir(int nargs, char **args)
231{
232 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700233 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700234
235 /* mkdir <path> [mode] [owner] [group] */
236
237 if (nargs >= 3) {
238 mode = strtoul(args[2], 0, 8);
239 }
240
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700241 ret = mkdir(args[1], mode);
242 /* chmod in case the directory already exists */
243 if (ret == -1 && errno == EEXIST) {
244 ret = chmod(args[1], mode);
245 }
246 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700247 return -errno;
248 }
249
250 if (nargs >= 4) {
251 uid_t uid = decode_uid(args[3]);
252 gid_t gid = -1;
253
254 if (nargs == 5) {
255 gid = decode_uid(args[4]);
256 }
257
258 if (chown(args[1], uid, gid)) {
259 return -errno;
260 }
261 }
262
263 return 0;
264}
265
266static struct {
267 const char *name;
268 unsigned flag;
269} mount_flags[] = {
270 { "noatime", MS_NOATIME },
271 { "nosuid", MS_NOSUID },
272 { "nodev", MS_NODEV },
273 { "nodiratime", MS_NODIRATIME },
274 { "ro", MS_RDONLY },
275 { "rw", 0 },
276 { "remount", MS_REMOUNT },
277 { "defaults", 0 },
278 { 0, 0 },
279};
280
Ken Sumrall752923c2010-12-03 16:33:31 -0800281#define DATA_MNT_POINT "/data"
282
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283/* mount <type> <device> <path> <flags ...> <options> */
284int do_mount(int nargs, char **args)
285{
286 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000287 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288 char *options = NULL;
289 unsigned flags = 0;
290 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700291 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700292
293 for (n = 4; n < nargs; n++) {
294 for (i = 0; mount_flags[i].name; i++) {
295 if (!strcmp(args[n], mount_flags[i].name)) {
296 flags |= mount_flags[i].flag;
297 break;
298 }
299 }
300
Colin Crosscd0f1732010-04-19 17:10:24 -0700301 if (!mount_flags[i].name) {
302 if (!strcmp(args[n], "wait"))
303 wait = 1;
304 /* if our last argument isn't a flag, wolf it up as an option string */
305 else if (n + 1 == nargs)
306 options = args[n];
307 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 }
309
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000310 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700311 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000312 target = args[3];
313
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314 if (!strncmp(source, "mtd@", 4)) {
315 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000316 if (n < 0) {
317 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700318 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000319
320 sprintf(tmp, "/dev/block/mtdblock%d", n);
321
Colin Crosscd0f1732010-04-19 17:10:24 -0700322 if (wait)
323 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000324 if (mount(tmp, target, system, flags, options) < 0) {
325 return -1;
326 }
327
Ken Sumralldd4d7862011-02-17 18:09:47 -0800328 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000329 } else if (!strncmp(source, "loop@", 5)) {
330 int mode, loop, fd;
331 struct loop_info info;
332
333 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
334 fd = open(source + 5, mode);
335 if (fd < 0) {
336 return -1;
337 }
338
339 for (n = 0; ; n++) {
340 sprintf(tmp, "/dev/block/loop%d", n);
341 loop = open(tmp, mode);
342 if (loop < 0) {
343 return -1;
344 }
345
346 /* if it is a blank loop device */
347 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
348 /* if it becomes our loop device */
349 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
350 close(fd);
351
352 if (mount(tmp, target, system, flags, options) < 0) {
353 ioctl(loop, LOOP_CLR_FD, 0);
354 close(loop);
355 return -1;
356 }
357
358 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800359 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000360 }
361 }
362
363 close(loop);
364 }
365
366 close(fd);
367 ERROR("out of loopback devices");
368 return -1;
369 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700370 if (wait)
371 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000372 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall7bc6e9e2011-05-26 20:01:39 -0700373 /* If this fails, it may be an encrypted filesystem
374 * or it could just be wiped. If wiped, that will be
375 * handled later in the boot process.
Ken Sumrall752923c2010-12-03 16:33:31 -0800376 * We only support encrypting /data. Check
377 * if we're trying to mount it, and if so,
378 * assume it's encrypted, mount a tmpfs instead.
379 * Then save the orig mount parms in properties
380 * for vold to query when it mounts the real
381 * encrypted /data.
382 */
Ken Sumrall7bc6e9e2011-05-26 20:01:39 -0700383 if (!strcmp(target, DATA_MNT_POINT) && !partition_wiped(source)) {
Ken Sumrall752923c2010-12-03 16:33:31 -0800384 const char *tmpfs_options;
385
386 tmpfs_options = property_get("ro.crypto.tmpfs_options");
387
388 if (mount("tmpfs", target, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV,
389 tmpfs_options) < 0) {
390 return -1;
391 }
392
393 /* Set the property that triggers the framework to do a minimal
394 * startup and ask the user for a password
395 */
Ken Sumrall4e84d3b2011-01-14 12:44:09 -0800396 property_set("ro.crypto.state", "encrypted");
Ken Sumrall752923c2010-12-03 16:33:31 -0800397 property_set("vold.decrypt", "1");
398 } else {
399 return -1;
400 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000401 }
402
Ken Sumrall752923c2010-12-03 16:33:31 -0800403 if (!strcmp(target, DATA_MNT_POINT)) {
404 char fs_flags[32];
405
406 /* Save the original mount options */
407 property_set("ro.crypto.fs_type", system);
408 property_set("ro.crypto.fs_real_blkdev", source);
409 property_set("ro.crypto.fs_mnt_point", target);
410 if (options) {
411 property_set("ro.crypto.fs_options", options);
412 }
413 snprintf(fs_flags, sizeof(fs_flags), "0x%8.8x", flags);
414 property_set("ro.crypto.fs_flags", fs_flags);
415 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700416 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800417
418exit_success:
419 /* If not running encrypted, then set the property saying we are
420 * unencrypted, and also trigger the action for a nonencrypted system.
421 */
422 if (!strcmp(target, DATA_MNT_POINT)) {
Ken Sumrallc5c51032011-03-08 17:01:29 -0800423 const char *prop;
424
Ken Sumralldd4d7862011-02-17 18:09:47 -0800425 prop = property_get("ro.crypto.state");
426 if (! prop) {
427 prop = "notset";
428 }
429 if (strcmp(prop, "encrypted")) {
430 property_set("ro.crypto.state", "unencrypted");
431 action_for_each_trigger("nonencrypted", action_add_queue_tail);
432 }
433 }
434
435 return 0;
436
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700437}
438
439int do_setkey(int nargs, char **args)
440{
441 struct kbentry kbe;
442 kbe.kb_table = strtoul(args[1], 0, 0);
443 kbe.kb_index = strtoul(args[2], 0, 0);
444 kbe.kb_value = strtoul(args[3], 0, 0);
445 return setkey(&kbe);
446}
447
448int do_setprop(int nargs, char **args)
449{
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700450 const char *name = args[1];
451 const char *value = args[2];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800452 char prop_val[PROP_VALUE_MAX];
453 int ret;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700454
Dima Zavin84bf9af2011-12-20 13:44:41 -0800455 ret = expand_props(prop_val, value, sizeof(prop_val));
456 if (ret) {
457 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
458 return -EINVAL;
Mike Lockwood1f0bd322011-06-08 17:31:27 -0700459 }
Dima Zavin84bf9af2011-12-20 13:44:41 -0800460 property_set(name, prop_val);
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];
Dima Zavin84bf9af2011-12-20 13:44:41 -0800544 char prop_val[PROP_VALUE_MAX];
545 int ret;
Mike Lockwood2c4d5dc2011-06-07 17:30:11 -0700546
Dima Zavin84bf9af2011-12-20 13:44:41 -0800547 ret = expand_props(prop_val, value, sizeof(prop_val));
548 if (ret) {
549 ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
550 return -EINVAL;
551 }
552 return write_file(path, prop_val);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700553}
554
San Mehat7c44fe52009-08-26 16:39:25 -0700555int do_copy(int nargs, char **args)
556{
557 char *buffer = NULL;
558 int rc = 0;
559 int fd1 = -1, fd2 = -1;
560 struct stat info;
561 int brtw, brtr;
562 char *p;
563
564 if (nargs != 3)
565 return -1;
566
567 if (stat(args[1], &info) < 0)
568 return -1;
569
570 if ((fd1 = open(args[1], O_RDONLY)) < 0)
571 goto out_err;
572
Tom Zhu4833d9f2009-09-28 19:53:12 -0500573 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700574 goto out_err;
575
576 if (!(buffer = malloc(info.st_size)))
577 goto out_err;
578
579 p = buffer;
580 brtr = info.st_size;
581 while(brtr) {
582 rc = read(fd1, p, brtr);
583 if (rc < 0)
584 goto out_err;
585 if (rc == 0)
586 break;
587 p += rc;
588 brtr -= rc;
589 }
590
591 p = buffer;
592 brtw = info.st_size;
593 while(brtw) {
594 rc = write(fd2, p, brtw);
595 if (rc < 0)
596 goto out_err;
597 if (rc == 0)
598 break;
599 p += rc;
600 brtw -= rc;
601 }
602
603 rc = 0;
604 goto out;
605out_err:
606 rc = -1;
607out:
608 if (buffer)
609 free(buffer);
610 if (fd1 >= 0)
611 close(fd1);
612 if (fd2 >= 0)
613 close(fd2);
614 return rc;
615}
616
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700617int do_chown(int nargs, char **args) {
618 /* GID is optional. */
619 if (nargs == 3) {
620 if (chown(args[2], decode_uid(args[1]), -1) < 0)
621 return -errno;
622 } else if (nargs == 4) {
623 if (chown(args[3], decode_uid(args[1]), decode_uid(args[2])))
624 return -errno;
625 } else {
626 return -1;
627 }
628 return 0;
629}
630
631static mode_t get_mode(const char *s) {
632 mode_t mode = 0;
633 while (*s) {
634 if (*s >= '0' && *s <= '7') {
635 mode = (mode<<3) | (*s-'0');
636 } else {
637 return -1;
638 }
639 s++;
640 }
641 return mode;
642}
643
644int do_chmod(int nargs, char **args) {
645 mode_t mode = get_mode(args[1]);
646 if (chmod(args[2], mode) < 0) {
647 return -errno;
648 }
649 return 0;
650}
651
652int do_loglevel(int nargs, char **args) {
653 if (nargs == 2) {
Dima Zavin8f912822011-08-31 18:26:17 -0700654 klog_set_level(atoi(args[1]));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700655 return 0;
656 }
657 return -1;
658}
659
Ken Sumrallc5c51032011-03-08 17:01:29 -0800660int do_load_persist_props(int nargs, char **args) {
661 if (nargs == 1) {
662 load_persist_props();
663 return 0;
664 }
665 return -1;
666}
667
Colin Crosscd0f1732010-04-19 17:10:24 -0700668int do_wait(int nargs, char **args)
669{
670 if (nargs == 2) {
671 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
672 }
673 return -1;
674}