builtins.c revision f5cb5b24356fae2dfa3477589ee0f3c094479e63
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#include <cutils/partition_utils.h>
34
35#include "init.h"
36#include "keywords.h"
37#include "property_service.h"
38#include "devices.h"
39#include "init_parser.h"
40#include "util.h"
41#include "log.h"
42
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)
56        return -errno;
57
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) {
66        return -errno;
67    } else {
68        return 0;
69    }
70}
71
72static int insmod(const char *filename, char *options)
73{
74    void *module;
75    unsigned size;
76    int ret;
77
78    module = read_file(filename, &size);
79    if (!module)
80        return -1;
81
82    ret = init_module(module, size, options);
83
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)) {
134        service_start(svc, NULL);
135    }
136}
137
138int 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
150int 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
166int do_class_reset(int nargs, char **args)
167{
168    service_for_each_class(args[1], service_reset);
169    return 0;
170}
171
172int 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
198
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
216int do_insmod(int nargs, char **args)
217{
218    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);
227}
228
229int 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
274#define DATA_MNT_POINT "/data"
275
276/* mount <type> <device> <path> <flags ...> <options> */
277int do_mount(int nargs, char **args)
278{
279    char tmp[64];
280    char *source, *target, *system;
281    char *options = NULL;
282    unsigned flags = 0;
283    int n, i;
284    int wait = 0;
285
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
294        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        }
301    }
302
303    system = args[1];
304    source = args[2];
305    target = args[3];
306
307    if (!strncmp(source, "mtd@", 4)) {
308        n = mtd_name_to_number(source + 4);
309        if (n < 0) {
310            return -1;
311        }
312
313        sprintf(tmp, "/dev/block/mtdblock%d", n);
314
315        if (wait)
316            wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
317        if (mount(tmp, target, system, flags, options) < 0) {
318            return -1;
319        }
320
321        goto exit_success;
322    } 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);
352                    goto exit_success;
353                }
354            }
355
356            close(loop);
357        }
358
359        close(fd);
360        ERROR("out of loopback devices");
361        return -1;
362    } else {
363        if (wait)
364            wait_for_file(source, COMMAND_RETRY_TIMEOUT);
365        if (mount(source, target, system, flags, options) < 0) {
366            /* 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.
369             * 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             */
376            if (!strcmp(target, DATA_MNT_POINT) && !partition_wiped(source)) {
377                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                 */
389                property_set("ro.crypto.state", "encrypted");
390                property_set("vold.decrypt", "1");
391            } else {
392                return -1;
393            }
394        }
395
396        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        }
409    }
410
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)) {
416        const char *prop;
417
418        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
430}
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{
443    property_set(args[1], args[2]);
444    return 0;
445}
446
447int do_setrlimit(int nargs, char **args)
448{
449    struct rlimit limit;
450    int resource;
451    resource = atoi(args[1]);
452    limit.rlim_cur = atoi(args[2]);
453    limit.rlim_max = atoi(args[3]);
454    return setrlimit(resource, &limit);
455}
456
457int do_start(int nargs, char **args)
458{
459    struct service *svc;
460    svc = service_find_by_name(args[1]);
461    if (svc) {
462        service_start(svc, NULL);
463    }
464    return 0;
465}
466
467int do_stop(int nargs, char **args)
468{
469    struct service *svc;
470    svc = service_find_by_name(args[1]);
471    if (svc) {
472        service_stop(svc);
473    }
474    return 0;
475}
476
477int do_restart(int nargs, char **args)
478{
479    struct service *svc;
480    svc = service_find_by_name(args[1]);
481    if (svc) {
482        service_stop(svc);
483        service_start(svc, NULL);
484    }
485    return 0;
486}
487
488int do_trigger(int nargs, char **args)
489{
490    action_for_each_trigger(args[1], action_add_queue_tail);
491    return 0;
492}
493
494int do_symlink(int nargs, char **args)
495{
496    return symlink(args[1], args[2]);
497}
498
499int do_rm(int nargs, char **args)
500{
501    return unlink(args[1]);
502}
503
504int do_rmdir(int nargs, char **args)
505{
506    return rmdir(args[1]);
507}
508
509int do_sysclktz(int nargs, char **args)
510{
511    struct timezone tz;
512
513    if (nargs != 2)
514        return -1;
515
516    memset(&tz, 0, sizeof(tz));
517    tz.tz_minuteswest = atoi(args[1]);
518    if (settimeofday(NULL, &tz))
519        return -1;
520    return 0;
521}
522
523int do_write(int nargs, char **args)
524{
525    char *path = args[1];
526    char *value = args[2];
527    if (value[0] == '$') {
528        /* Write the value of a system property if value starts with '$' */
529        value++;
530        if (value[0] != '$') {
531            value = property_get(value);
532            if (!value) {
533                ERROR("property %s has no value for writing to %s\n", value, path);
534                return -EINVAL;
535            }
536        } /* else fall through to support double '$' prefix for writing
537           * string literals that start with '$'
538           */
539    }
540
541    return write_file(path, value);
542}
543
544int do_copy(int nargs, char **args)
545{
546    char *buffer = NULL;
547    int rc = 0;
548    int fd1 = -1, fd2 = -1;
549    struct stat info;
550    int brtw, brtr;
551    char *p;
552
553    if (nargs != 3)
554        return -1;
555
556    if (stat(args[1], &info) < 0)
557        return -1;
558
559    if ((fd1 = open(args[1], O_RDONLY)) < 0)
560        goto out_err;
561
562    if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
563        goto out_err;
564
565    if (!(buffer = malloc(info.st_size)))
566        goto out_err;
567
568    p = buffer;
569    brtr = info.st_size;
570    while(brtr) {
571        rc = read(fd1, p, brtr);
572        if (rc < 0)
573            goto out_err;
574        if (rc == 0)
575            break;
576        p += rc;
577        brtr -= rc;
578    }
579
580    p = buffer;
581    brtw = info.st_size;
582    while(brtw) {
583        rc = write(fd2, p, brtw);
584        if (rc < 0)
585            goto out_err;
586        if (rc == 0)
587            break;
588        p += rc;
589        brtw -= rc;
590    }
591
592    rc = 0;
593    goto out;
594out_err:
595    rc = -1;
596out:
597    if (buffer)
598        free(buffer);
599    if (fd1 >= 0)
600        close(fd1);
601    if (fd2 >= 0)
602        close(fd2);
603    return rc;
604}
605
606int do_chown(int nargs, char **args) {
607    /* GID is optional. */
608    if (nargs == 3) {
609        if (chown(args[2], decode_uid(args[1]), -1) < 0)
610            return -errno;
611    } else if (nargs == 4) {
612        if (chown(args[3], decode_uid(args[1]), decode_uid(args[2])))
613            return -errno;
614    } else {
615        return -1;
616    }
617    return 0;
618}
619
620static mode_t get_mode(const char *s) {
621    mode_t mode = 0;
622    while (*s) {
623        if (*s >= '0' && *s <= '7') {
624            mode = (mode<<3) | (*s-'0');
625        } else {
626            return -1;
627        }
628        s++;
629    }
630    return mode;
631}
632
633int do_chmod(int nargs, char **args) {
634    mode_t mode = get_mode(args[1]);
635    if (chmod(args[2], mode) < 0) {
636        return -errno;
637    }
638    return 0;
639}
640
641int do_loglevel(int nargs, char **args) {
642    if (nargs == 2) {
643        log_set_level(atoi(args[1]));
644        return 0;
645    }
646    return -1;
647}
648
649int do_load_persist_props(int nargs, char **args) {
650    if (nargs == 1) {
651        load_persist_props();
652        return 0;
653    }
654    return -1;
655}
656
657int do_wait(int nargs, char **args)
658{
659    if (nargs == 2) {
660        return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
661    }
662    return -1;
663}
664