builtins.c revision c5c51033c86353d1a448c72e0e9388906eed7114
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>
32#include <linux/loop.h>
33
34#include "init.h"
35#include "keywords.h"
36#include "property_service.h"
37#include "devices.h"
38#include "init_parser.h"
39#include "util.h"
40#include "log.h"
41
42#include <private/android_filesystem_config.h>
43
44void add_environment(const char *name, const char *value);
45
46extern int init_module(void *, unsigned long, const char *);
47
48static int write_file(const char *path, const char *value)
49{
50    int fd, ret, len;
51
52    fd = open(path, O_WRONLY|O_CREAT, 0622);
53
54    if (fd < 0)
55        return -errno;
56
57    len = strlen(value);
58
59    do {
60        ret = write(fd, value, len);
61    } while (ret < 0 && errno == EINTR);
62
63    close(fd);
64    if (ret < 0) {
65        return -errno;
66    } else {
67        return 0;
68    }
69}
70
71static int insmod(const char *filename, char *options)
72{
73    void *module;
74    unsigned size;
75    int ret;
76
77    module = read_file(filename, &size);
78    if (!module)
79        return -1;
80
81    ret = init_module(module, size, options);
82
83    free(module);
84
85    return ret;
86}
87
88static int setkey(struct kbentry *kbe)
89{
90    int fd, ret;
91
92    fd = open("/dev/tty0", O_RDWR | O_SYNC);
93    if (fd < 0)
94        return -1;
95
96    ret = ioctl(fd, KDSKBENT, kbe);
97
98    close(fd);
99    return ret;
100}
101
102static int __ifupdown(const char *interface, int up)
103{
104    struct ifreq ifr;
105    int s, ret;
106
107    strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
108
109    s = socket(AF_INET, SOCK_DGRAM, 0);
110    if (s < 0)
111        return -1;
112
113    ret = ioctl(s, SIOCGIFFLAGS, &ifr);
114    if (ret < 0) {
115        goto done;
116    }
117
118    if (up)
119        ifr.ifr_flags |= IFF_UP;
120    else
121        ifr.ifr_flags &= ~IFF_UP;
122
123    ret = ioctl(s, SIOCSIFFLAGS, &ifr);
124
125done:
126    close(s);
127    return ret;
128}
129
130static void service_start_if_not_disabled(struct service *svc)
131{
132    if (!(svc->flags & SVC_DISABLED)) {
133        service_start(svc, NULL);
134    }
135}
136
137int do_chdir(int nargs, char **args)
138{
139    chdir(args[1]);
140    return 0;
141}
142
143int do_chroot(int nargs, char **args)
144{
145    chroot(args[1]);
146    return 0;
147}
148
149int do_class_start(int nargs, char **args)
150{
151        /* Starting a class does not start services
152         * which are explicitly disabled.  They must
153         * be started individually.
154         */
155    service_for_each_class(args[1], service_start_if_not_disabled);
156    return 0;
157}
158
159int do_class_stop(int nargs, char **args)
160{
161    service_for_each_class(args[1], service_stop);
162    return 0;
163}
164
165int do_class_reset(int nargs, char **args)
166{
167    service_for_each_class(args[1], service_reset);
168    return 0;
169}
170
171int do_domainname(int nargs, char **args)
172{
173    return write_file("/proc/sys/kernel/domainname", args[1]);
174}
175
176int do_exec(int nargs, char **args)
177{
178    return -1;
179}
180
181int do_export(int nargs, char **args)
182{
183    add_environment(args[1], args[2]);
184    return 0;
185}
186
187int do_hostname(int nargs, char **args)
188{
189    return write_file("/proc/sys/kernel/hostname", args[1]);
190}
191
192int do_ifup(int nargs, char **args)
193{
194    return __ifupdown(args[1], 1);
195}
196
197
198static int do_insmod_inner(int nargs, char **args, int opt_len)
199{
200    char options[opt_len + 1];
201    int i;
202
203    options[0] = '\0';
204    if (nargs > 2) {
205        strcpy(options, args[2]);
206        for (i = 3; i < nargs; ++i) {
207            strcat(options, " ");
208            strcat(options, args[i]);
209        }
210    }
211
212    return insmod(args[1], options);
213}
214
215int do_insmod(int nargs, char **args)
216{
217    int i;
218    int size = 0;
219
220    if (nargs > 2) {
221        for (i = 2; i < nargs; ++i)
222            size += strlen(args[i]) + 1;
223    }
224
225    return do_insmod_inner(nargs, args, size);
226}
227
228int do_import(int nargs, char **args)
229{
230    return init_parse_config_file(args[1]);
231}
232
233int do_mkdir(int nargs, char **args)
234{
235    mode_t mode = 0755;
236
237    /* mkdir <path> [mode] [owner] [group] */
238
239    if (nargs >= 3) {
240        mode = strtoul(args[2], 0, 8);
241    }
242
243    if (mkdir(args[1], mode)) {
244        return -errno;
245    }
246
247    if (nargs >= 4) {
248        uid_t uid = decode_uid(args[3]);
249        gid_t gid = -1;
250
251        if (nargs == 5) {
252            gid = decode_uid(args[4]);
253        }
254
255        if (chown(args[1], uid, gid)) {
256            return -errno;
257        }
258    }
259
260    return 0;
261}
262
263static struct {
264    const char *name;
265    unsigned flag;
266} mount_flags[] = {
267    { "noatime",    MS_NOATIME },
268    { "nosuid",     MS_NOSUID },
269    { "nodev",      MS_NODEV },
270    { "nodiratime", MS_NODIRATIME },
271    { "ro",         MS_RDONLY },
272    { "rw",         0 },
273    { "remount",    MS_REMOUNT },
274    { "defaults",   0 },
275    { 0,            0 },
276};
277
278#define DATA_MNT_POINT "/data"
279
280/* mount <type> <device> <path> <flags ...> <options> */
281int do_mount(int nargs, char **args)
282{
283    char tmp[64];
284    char *source, *target, *system;
285    char *options = NULL;
286    unsigned flags = 0;
287    int n, i;
288    int wait = 0;
289
290    for (n = 4; n < nargs; n++) {
291        for (i = 0; mount_flags[i].name; i++) {
292            if (!strcmp(args[n], mount_flags[i].name)) {
293                flags |= mount_flags[i].flag;
294                break;
295            }
296        }
297
298        if (!mount_flags[i].name) {
299            if (!strcmp(args[n], "wait"))
300                wait = 1;
301            /* if our last argument isn't a flag, wolf it up as an option string */
302            else if (n + 1 == nargs)
303                options = args[n];
304        }
305    }
306
307    system = args[1];
308    source = args[2];
309    target = args[3];
310
311    if (!strncmp(source, "mtd@", 4)) {
312        n = mtd_name_to_number(source + 4);
313        if (n < 0) {
314            return -1;
315        }
316
317        sprintf(tmp, "/dev/block/mtdblock%d", n);
318
319        if (wait)
320            wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
321        if (mount(tmp, target, system, flags, options) < 0) {
322            return -1;
323        }
324
325        goto exit_success;
326    } else if (!strncmp(source, "loop@", 5)) {
327        int mode, loop, fd;
328        struct loop_info info;
329
330        mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
331        fd = open(source + 5, mode);
332        if (fd < 0) {
333            return -1;
334        }
335
336        for (n = 0; ; n++) {
337            sprintf(tmp, "/dev/block/loop%d", n);
338            loop = open(tmp, mode);
339            if (loop < 0) {
340                return -1;
341            }
342
343            /* if it is a blank loop device */
344            if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
345                /* if it becomes our loop device */
346                if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
347                    close(fd);
348
349                    if (mount(tmp, target, system, flags, options) < 0) {
350                        ioctl(loop, LOOP_CLR_FD, 0);
351                        close(loop);
352                        return -1;
353                    }
354
355                    close(loop);
356                    goto exit_success;
357                }
358            }
359
360            close(loop);
361        }
362
363        close(fd);
364        ERROR("out of loopback devices");
365        return -1;
366    } else {
367        if (wait)
368            wait_for_file(source, COMMAND_RETRY_TIMEOUT);
369        if (mount(source, target, system, flags, options) < 0) {
370            /* If this fails, it may be an encrypted filesystem.
371             * We only support encrypting /data.  Check
372             * if we're trying to mount it, and if so,
373             * assume it's encrypted, mount a tmpfs instead.
374             * Then save the orig mount parms in properties
375             * for vold to query when it mounts the real
376             * encrypted /data.
377             */
378            if (!strcmp(target, DATA_MNT_POINT)) {
379                const char *tmpfs_options;
380
381                tmpfs_options = property_get("ro.crypto.tmpfs_options");
382
383                if (mount("tmpfs", target, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV,
384                    tmpfs_options) < 0) {
385                    return -1;
386                }
387
388                /* Set the property that triggers the framework to do a minimal
389                 * startup and ask the user for a password
390                 */
391                property_set("ro.crypto.state", "encrypted");
392                property_set("vold.decrypt", "1");
393            } else {
394                return -1;
395            }
396        }
397
398        if (!strcmp(target, DATA_MNT_POINT)) {
399            char fs_flags[32];
400
401            /* Save the original mount options */
402            property_set("ro.crypto.fs_type", system);
403            property_set("ro.crypto.fs_real_blkdev", source);
404            property_set("ro.crypto.fs_mnt_point", target);
405            if (options) {
406                property_set("ro.crypto.fs_options", options);
407            }
408            snprintf(fs_flags, sizeof(fs_flags), "0x%8.8x", flags);
409            property_set("ro.crypto.fs_flags", fs_flags);
410        }
411    }
412
413exit_success:
414    /* If not running encrypted, then set the property saying we are
415     * unencrypted, and also trigger the action for a nonencrypted system.
416     */
417    if (!strcmp(target, DATA_MNT_POINT)) {
418        const char *prop;
419
420        prop = property_get("ro.crypto.state");
421        if (! prop) {
422            prop = "notset";
423        }
424        if (strcmp(prop, "encrypted")) {
425            property_set("ro.crypto.state", "unencrypted");
426            action_for_each_trigger("nonencrypted", action_add_queue_tail);
427        }
428    }
429
430    return 0;
431
432}
433
434int do_setkey(int nargs, char **args)
435{
436    struct kbentry kbe;
437    kbe.kb_table = strtoul(args[1], 0, 0);
438    kbe.kb_index = strtoul(args[2], 0, 0);
439    kbe.kb_value = strtoul(args[3], 0, 0);
440    return setkey(&kbe);
441}
442
443int do_setprop(int nargs, char **args)
444{
445    property_set(args[1], args[2]);
446    return 0;
447}
448
449int do_setrlimit(int nargs, char **args)
450{
451    struct rlimit limit;
452    int resource;
453    resource = atoi(args[1]);
454    limit.rlim_cur = atoi(args[2]);
455    limit.rlim_max = atoi(args[3]);
456    return setrlimit(resource, &limit);
457}
458
459int do_start(int nargs, char **args)
460{
461    struct service *svc;
462    svc = service_find_by_name(args[1]);
463    if (svc) {
464        service_start(svc, NULL);
465    }
466    return 0;
467}
468
469int do_stop(int nargs, char **args)
470{
471    struct service *svc;
472    svc = service_find_by_name(args[1]);
473    if (svc) {
474        service_stop(svc);
475    }
476    return 0;
477}
478
479int do_restart(int nargs, char **args)
480{
481    struct service *svc;
482    svc = service_find_by_name(args[1]);
483    if (svc) {
484        service_stop(svc);
485        service_start(svc, NULL);
486    }
487    return 0;
488}
489
490int do_trigger(int nargs, char **args)
491{
492    action_for_each_trigger(args[1], action_add_queue_tail);
493    return 0;
494}
495
496int do_symlink(int nargs, char **args)
497{
498    return symlink(args[1], args[2]);
499}
500
501int do_rm(int nargs, char **args)
502{
503    return unlink(args[1]);
504}
505
506int do_rmdir(int nargs, char **args)
507{
508    return rmdir(args[1]);
509}
510
511int do_sysclktz(int nargs, char **args)
512{
513    struct timezone tz;
514
515    if (nargs != 2)
516        return -1;
517
518    memset(&tz, 0, sizeof(tz));
519    tz.tz_minuteswest = atoi(args[1]);
520    if (settimeofday(NULL, &tz))
521        return -1;
522    return 0;
523}
524
525int do_write(int nargs, char **args)
526{
527    return write_file(args[1], args[2]);
528}
529
530int do_copy(int nargs, char **args)
531{
532    char *buffer = NULL;
533    int rc = 0;
534    int fd1 = -1, fd2 = -1;
535    struct stat info;
536    int brtw, brtr;
537    char *p;
538
539    if (nargs != 3)
540        return -1;
541
542    if (stat(args[1], &info) < 0)
543        return -1;
544
545    if ((fd1 = open(args[1], O_RDONLY)) < 0)
546        goto out_err;
547
548    if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
549        goto out_err;
550
551    if (!(buffer = malloc(info.st_size)))
552        goto out_err;
553
554    p = buffer;
555    brtr = info.st_size;
556    while(brtr) {
557        rc = read(fd1, p, brtr);
558        if (rc < 0)
559            goto out_err;
560        if (rc == 0)
561            break;
562        p += rc;
563        brtr -= rc;
564    }
565
566    p = buffer;
567    brtw = info.st_size;
568    while(brtw) {
569        rc = write(fd2, p, brtw);
570        if (rc < 0)
571            goto out_err;
572        if (rc == 0)
573            break;
574        p += rc;
575        brtw -= rc;
576    }
577
578    rc = 0;
579    goto out;
580out_err:
581    rc = -1;
582out:
583    if (buffer)
584        free(buffer);
585    if (fd1 >= 0)
586        close(fd1);
587    if (fd2 >= 0)
588        close(fd2);
589    return rc;
590}
591
592int do_chown(int nargs, char **args) {
593    /* GID is optional. */
594    if (nargs == 3) {
595        if (chown(args[2], decode_uid(args[1]), -1) < 0)
596            return -errno;
597    } else if (nargs == 4) {
598        if (chown(args[3], decode_uid(args[1]), decode_uid(args[2])))
599            return -errno;
600    } else {
601        return -1;
602    }
603    return 0;
604}
605
606static mode_t get_mode(const char *s) {
607    mode_t mode = 0;
608    while (*s) {
609        if (*s >= '0' && *s <= '7') {
610            mode = (mode<<3) | (*s-'0');
611        } else {
612            return -1;
613        }
614        s++;
615    }
616    return mode;
617}
618
619int do_chmod(int nargs, char **args) {
620    mode_t mode = get_mode(args[1]);
621    if (chmod(args[2], mode) < 0) {
622        return -errno;
623    }
624    return 0;
625}
626
627int do_loglevel(int nargs, char **args) {
628    if (nargs == 2) {
629        log_set_level(atoi(args[1]));
630        return 0;
631    }
632    return -1;
633}
634
635int do_load_persist_props(int nargs, char **args) {
636    if (nargs == 1) {
637        load_persist_props();
638        return 0;
639    }
640    return -1;
641}
642
643int do_wait(int nargs, char **args)
644{
645    if (nargs == 2) {
646        return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
647    }
648    return -1;
649}
650