blob: 44faf1716d0734d836425aaba1b7a45b314b0b74 [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>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070033
34#include "init.h"
35#include "keywords.h"
36#include "property_service.h"
37#include "devices.h"
38
39#include <private/android_filesystem_config.h>
40
41void add_environment(const char *name, const char *value);
42
43extern int init_module(void *, unsigned long, const char *);
44
45static int write_file(const char *path, const char *value)
46{
47 int fd, ret, len;
48
49 fd = open(path, O_WRONLY|O_CREAT, 0622);
50
51 if (fd < 0)
Mike Chan008abac2009-06-29 20:30:55 -070052 return -errno;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053
54 len = strlen(value);
55
56 do {
57 ret = write(fd, value, len);
58 } while (ret < 0 && errno == EINTR);
59
60 close(fd);
61 if (ret < 0) {
Mike Chan008abac2009-06-29 20:30:55 -070062 return -errno;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063 } else {
64 return 0;
65 }
66}
67
The Android Open Source Project35237d12008-12-17 18:08:08 -080068static int insmod(const char *filename, char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069{
70 void *module;
71 unsigned size;
72 int ret;
73
74 module = read_file(filename, &size);
75 if (!module)
76 return -1;
77
The Android Open Source Project35237d12008-12-17 18:08:08 -080078 ret = init_module(module, size, options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079
80 free(module);
81
82 return ret;
83}
84
85static int setkey(struct kbentry *kbe)
86{
87 int fd, ret;
88
89 fd = open("/dev/tty0", O_RDWR | O_SYNC);
90 if (fd < 0)
91 return -1;
92
93 ret = ioctl(fd, KDSKBENT, kbe);
94
95 close(fd);
96 return ret;
97}
98
99static int __ifupdown(const char *interface, int up)
100{
101 struct ifreq ifr;
102 int s, ret;
103
104 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
105
106 s = socket(AF_INET, SOCK_DGRAM, 0);
107 if (s < 0)
108 return -1;
109
110 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
111 if (ret < 0) {
112 goto done;
113 }
114
115 if (up)
116 ifr.ifr_flags |= IFF_UP;
117 else
118 ifr.ifr_flags &= ~IFF_UP;
119
120 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
121
122done:
123 close(s);
124 return ret;
125}
126
127static void service_start_if_not_disabled(struct service *svc)
128{
129 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700130 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131 }
132}
133
Jay Freeman (saurik)e7cb1372008-11-17 06:41:10 +0000134int do_chdir(int nargs, char **args)
135{
136 chdir(args[1]);
137 return 0;
138}
139
140int do_chroot(int nargs, char **args)
141{
142 chroot(args[1]);
143 return 0;
144}
145
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700146int do_class_start(int nargs, char **args)
147{
148 /* Starting a class does not start services
149 * which are explicitly disabled. They must
150 * be started individually.
151 */
152 service_for_each_class(args[1], service_start_if_not_disabled);
153 return 0;
154}
155
156int do_class_stop(int nargs, char **args)
157{
158 service_for_each_class(args[1], service_stop);
159 return 0;
160}
161
162int do_domainname(int nargs, char **args)
163{
164 return write_file("/proc/sys/kernel/domainname", args[1]);
165}
166
167int do_exec(int nargs, char **args)
168{
169 return -1;
170}
171
172int do_export(int nargs, char **args)
173{
174 add_environment(args[1], args[2]);
175 return 0;
176}
177
178int do_hostname(int nargs, char **args)
179{
180 return write_file("/proc/sys/kernel/hostname", args[1]);
181}
182
183int do_ifup(int nargs, char **args)
184{
185 return __ifupdown(args[1], 1);
186}
187
The Android Open Source Project35237d12008-12-17 18:08:08 -0800188
189static int do_insmod_inner(int nargs, char **args, int opt_len)
190{
191 char options[opt_len + 1];
192 int i;
193
194 options[0] = '\0';
195 if (nargs > 2) {
196 strcpy(options, args[2]);
197 for (i = 3; i < nargs; ++i) {
198 strcat(options, " ");
199 strcat(options, args[i]);
200 }
201 }
202
203 return insmod(args[1], options);
204}
205
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206int do_insmod(int nargs, char **args)
207{
The Android Open Source Project35237d12008-12-17 18:08:08 -0800208 int i;
209 int size = 0;
210
211 if (nargs > 2) {
212 for (i = 2; i < nargs; ++i)
213 size += strlen(args[i]) + 1;
214 }
215
216 return do_insmod_inner(nargs, args, size);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700217}
218
219int do_import(int nargs, char **args)
220{
Jay Freeman (saurik)9f28bde2008-12-01 07:24:12 +0000221 return parse_config_file(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700222}
223
224int do_mkdir(int nargs, char **args)
225{
226 mode_t mode = 0755;
227
228 /* mkdir <path> [mode] [owner] [group] */
229
230 if (nargs >= 3) {
231 mode = strtoul(args[2], 0, 8);
232 }
233
234 if (mkdir(args[1], mode)) {
235 return -errno;
236 }
237
238 if (nargs >= 4) {
239 uid_t uid = decode_uid(args[3]);
240 gid_t gid = -1;
241
242 if (nargs == 5) {
243 gid = decode_uid(args[4]);
244 }
245
246 if (chown(args[1], uid, gid)) {
247 return -errno;
248 }
249 }
250
251 return 0;
252}
253
254static struct {
255 const char *name;
256 unsigned flag;
257} mount_flags[] = {
Jay Freeman (saurik)ed336252009-01-13 21:55:35 +0000258 { "move", MS_MOVE },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700259 { "noatime", MS_NOATIME },
260 { "nosuid", MS_NOSUID },
261 { "nodev", MS_NODEV },
262 { "nodiratime", MS_NODIRATIME },
263 { "ro", MS_RDONLY },
264 { "rw", 0 },
265 { "remount", MS_REMOUNT },
266 { "defaults", 0 },
267 { 0, 0 },
268};
269
270/* mount <type> <device> <path> <flags ...> <options> */
271int do_mount(int nargs, char **args)
272{
273 char tmp[64];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000274 char *source, *target, *system;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700275 char *options = NULL;
276 unsigned flags = 0;
277 int n, i;
278
279 for (n = 4; n < nargs; n++) {
280 for (i = 0; mount_flags[i].name; i++) {
281 if (!strcmp(args[n], mount_flags[i].name)) {
282 flags |= mount_flags[i].flag;
283 break;
284 }
285 }
286
287 /* if our last argument isn't a flag, wolf it up as an option string */
288 if (n + 1 == nargs && !mount_flags[i].name)
289 options = args[n];
290 }
291
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000292 system = args[1];
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700293 source = args[2];
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000294 target = args[3];
295
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700296 if (!strncmp(source, "mtd@", 4)) {
297 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000298 if (n < 0) {
299 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000301
302 sprintf(tmp, "/dev/block/mtdblock%d", n);
303
304 if (mount(tmp, target, system, flags, options) < 0) {
305 return -1;
306 }
307
308 return 0;
309 } else if (!strncmp(source, "loop@", 5)) {
310 int mode, loop, fd;
311 struct loop_info info;
312
313 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
314 fd = open(source + 5, mode);
315 if (fd < 0) {
316 return -1;
317 }
318
319 for (n = 0; ; n++) {
320 sprintf(tmp, "/dev/block/loop%d", n);
321 loop = open(tmp, mode);
322 if (loop < 0) {
323 return -1;
324 }
325
326 /* if it is a blank loop device */
327 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
328 /* if it becomes our loop device */
329 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
330 close(fd);
331
332 if (mount(tmp, target, system, flags, options) < 0) {
333 ioctl(loop, LOOP_CLR_FD, 0);
334 close(loop);
335 return -1;
336 }
337
338 close(loop);
339 return 0;
340 }
341 }
342
343 close(loop);
344 }
345
346 close(fd);
347 ERROR("out of loopback devices");
348 return -1;
349 } else {
350 if (mount(source, target, system, flags, options) < 0) {
351 return -1;
352 }
353
354 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700355 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356}
357
358int do_setkey(int nargs, char **args)
359{
360 struct kbentry kbe;
361 kbe.kb_table = strtoul(args[1], 0, 0);
362 kbe.kb_index = strtoul(args[2], 0, 0);
363 kbe.kb_value = strtoul(args[3], 0, 0);
364 return setkey(&kbe);
365}
366
367int do_setprop(int nargs, char **args)
368{
369 property_set(args[1], args[2]);
370 return 0;
371}
372
373int do_setrlimit(int nargs, char **args)
374{
375 struct rlimit limit;
376 int resource;
377 resource = atoi(args[1]);
378 limit.rlim_cur = atoi(args[2]);
379 limit.rlim_max = atoi(args[3]);
380 return setrlimit(resource, &limit);
381}
382
383int do_start(int nargs, char **args)
384{
385 struct service *svc;
386 svc = service_find_by_name(args[1]);
387 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700388 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700389 }
390 return 0;
391}
392
393int do_stop(int nargs, char **args)
394{
395 struct service *svc;
396 svc = service_find_by_name(args[1]);
397 if (svc) {
398 service_stop(svc);
399 }
400 return 0;
401}
402
403int do_restart(int nargs, char **args)
404{
405 struct service *svc;
406 svc = service_find_by_name(args[1]);
407 if (svc) {
408 service_stop(svc);
San Mehatf24e2522009-05-19 13:30:46 -0700409 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700410 }
411 return 0;
412}
413
414int do_trigger(int nargs, char **args)
415{
Jay Freeman (saurik)11e1c422008-11-17 06:35:08 +0000416 action_for_each_trigger(args[1], action_add_queue_tail);
417 drain_action_queue();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700418 return 0;
419}
420
421int do_symlink(int nargs, char **args)
422{
423 return symlink(args[1], args[2]);
424}
425
The Android Open Source Project35237d12008-12-17 18:08:08 -0800426int do_sysclktz(int nargs, char **args)
427{
428 struct timezone tz;
429
430 if (nargs != 2)
431 return -1;
432
433 memset(&tz, 0, sizeof(tz));
434 tz.tz_minuteswest = atoi(args[1]);
435 if (settimeofday(NULL, &tz))
436 return -1;
437 return 0;
438}
439
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700440int do_write(int nargs, char **args)
441{
442 return write_file(args[1], args[2]);
443}
444
San Mehat7c44fe52009-08-26 16:39:25 -0700445int do_copy(int nargs, char **args)
446{
447 char *buffer = NULL;
448 int rc = 0;
449 int fd1 = -1, fd2 = -1;
450 struct stat info;
451 int brtw, brtr;
452 char *p;
453
454 if (nargs != 3)
455 return -1;
456
457 if (stat(args[1], &info) < 0)
458 return -1;
459
460 if ((fd1 = open(args[1], O_RDONLY)) < 0)
461 goto out_err;
462
Tom Zhu4833d9f2009-09-28 19:53:12 -0500463 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700464 goto out_err;
465
466 if (!(buffer = malloc(info.st_size)))
467 goto out_err;
468
469 p = buffer;
470 brtr = info.st_size;
471 while(brtr) {
472 rc = read(fd1, p, brtr);
473 if (rc < 0)
474 goto out_err;
475 if (rc == 0)
476 break;
477 p += rc;
478 brtr -= rc;
479 }
480
481 p = buffer;
482 brtw = info.st_size;
483 while(brtw) {
484 rc = write(fd2, p, brtw);
485 if (rc < 0)
486 goto out_err;
487 if (rc == 0)
488 break;
489 p += rc;
490 brtw -= rc;
491 }
492
493 rc = 0;
494 goto out;
495out_err:
496 rc = -1;
497out:
498 if (buffer)
499 free(buffer);
500 if (fd1 >= 0)
501 close(fd1);
502 if (fd2 >= 0)
503 close(fd2);
504 return rc;
505}
506
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700507int do_chown(int nargs, char **args) {
508 /* GID is optional. */
509 if (nargs == 3) {
510 if (chown(args[2], decode_uid(args[1]), -1) < 0)
511 return -errno;
512 } else if (nargs == 4) {
513 if (chown(args[3], decode_uid(args[1]), decode_uid(args[2])))
514 return -errno;
515 } else {
516 return -1;
517 }
518 return 0;
519}
520
521static mode_t get_mode(const char *s) {
522 mode_t mode = 0;
523 while (*s) {
524 if (*s >= '0' && *s <= '7') {
525 mode = (mode<<3) | (*s-'0');
526 } else {
527 return -1;
528 }
529 s++;
530 }
531 return mode;
532}
533
534int do_chmod(int nargs, char **args) {
535 mode_t mode = get_mode(args[1]);
536 if (chmod(args[2], mode) < 0) {
537 return -errno;
538 }
539 return 0;
540}
541
542int do_loglevel(int nargs, char **args) {
543 if (nargs == 2) {
544 log_set_level(atoi(args[1]));
545 return 0;
546 }
547 return -1;
548}
549
550int do_device(int nargs, char **args) {
551 int len;
552 char tmp[64];
553 char *source = args[1];
554 int prefix = 0;
555
556 if (nargs != 5)
557 return -1;
558 /* Check for wildcard '*' at the end which indicates a prefix. */
559 len = strlen(args[1]) - 1;
560 if (args[1][len] == '*') {
561 args[1][len] = '\0';
562 prefix = 1;
563 }
564 /* If path starts with mtd@ lookup the mount number. */
565 if (!strncmp(source, "mtd@", 4)) {
566 int n = mtd_name_to_number(source + 4);
567 if (n >= 0) {
568 snprintf(tmp, sizeof(tmp), "/dev/mtd/mtd%d", n);
569 source = tmp;
570 }
571 }
572 add_devperms_partners(source, get_mode(args[2]), decode_uid(args[3]),
573 decode_uid(args[4]), prefix);
574 return 0;
575}