builtins.c revision b5982bf7c65b30be981ecfe1847437b05e32b2bb
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 <sys/wait.h>
33#include <linux/loop.h>
34#include <cutils/partition_utils.h>
35#include <sys/system_properties.h>
36#include <fs_mgr.h>
37
38#include <selinux/selinux.h>
39#include <selinux/label.h>
40
41#include "init.h"
42#include "keywords.h"
43#include "property_service.h"
44#include "devices.h"
45#include "init_parser.h"
46#include "util.h"
47#include "log.h"
48
49#include <private/android_filesystem_config.h>
50
51void add_environment(const char *name, const char *value);
52
53extern int init_module(void *, unsigned long, const char *);
54
55static int write_file(const char *path, const char *value)
56{
57    int fd, ret, len;
58
59    fd = open(path, O_WRONLY|O_CREAT, 0622);
60
61    if (fd < 0)
62        return -errno;
63
64    len = strlen(value);
65
66    do {
67        ret = write(fd, value, len);
68    } while (ret < 0 && errno == EINTR);
69
70    close(fd);
71    if (ret < 0) {
72        return -errno;
73    } else {
74        return 0;
75    }
76}
77
78static int _open(const char *path)
79{
80    int fd;
81
82    fd = open(path, O_RDONLY | O_NOFOLLOW);
83    if (fd < 0)
84        fd = open(path, O_WRONLY | O_NOFOLLOW);
85
86    return fd;
87}
88
89static int _chown(const char *path, unsigned int uid, unsigned int gid)
90{
91    int fd;
92    int ret;
93
94    fd = _open(path);
95    if (fd < 0) {
96        return -1;
97    }
98
99    ret = fchown(fd, uid, gid);
100    if (ret < 0) {
101        int errno_copy = errno;
102        close(fd);
103        errno = errno_copy;
104        return -1;
105    }
106
107    close(fd);
108
109    return 0;
110}
111
112static int _chmod(const char *path, mode_t mode)
113{
114    int fd;
115    int ret;
116
117    fd = _open(path);
118    if (fd < 0) {
119        return -1;
120    }
121
122    ret = fchmod(fd, mode);
123    if (ret < 0) {
124        int errno_copy = errno;
125        close(fd);
126        errno = errno_copy;
127        return -1;
128    }
129
130    close(fd);
131
132    return 0;
133}
134
135static int insmod(const char *filename, char *options)
136{
137    void *module;
138    unsigned size;
139    int ret;
140
141    module = read_file(filename, &size);
142    if (!module)
143        return -1;
144
145    ret = init_module(module, size, options);
146
147    free(module);
148
149    return ret;
150}
151
152static int setkey(struct kbentry *kbe)
153{
154    int fd, ret;
155
156    fd = open("/dev/tty0", O_RDWR | O_SYNC);
157    if (fd < 0)
158        return -1;
159
160    ret = ioctl(fd, KDSKBENT, kbe);
161
162    close(fd);
163    return ret;
164}
165
166static int __ifupdown(const char *interface, int up)
167{
168    struct ifreq ifr;
169    int s, ret;
170
171    strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
172
173    s = socket(AF_INET, SOCK_DGRAM, 0);
174    if (s < 0)
175        return -1;
176
177    ret = ioctl(s, SIOCGIFFLAGS, &ifr);
178    if (ret < 0) {
179        goto done;
180    }
181
182    if (up)
183        ifr.ifr_flags |= IFF_UP;
184    else
185        ifr.ifr_flags &= ~IFF_UP;
186
187    ret = ioctl(s, SIOCSIFFLAGS, &ifr);
188
189done:
190    close(s);
191    return ret;
192}
193
194static void service_start_if_not_disabled(struct service *svc)
195{
196    if (!(svc->flags & SVC_DISABLED)) {
197        service_start(svc, NULL);
198    }
199}
200
201int do_chdir(int nargs, char **args)
202{
203    chdir(args[1]);
204    return 0;
205}
206
207int do_chroot(int nargs, char **args)
208{
209    chroot(args[1]);
210    return 0;
211}
212
213int do_class_start(int nargs, char **args)
214{
215        /* Starting a class does not start services
216         * which are explicitly disabled.  They must
217         * be started individually.
218         */
219    service_for_each_class(args[1], service_start_if_not_disabled);
220    return 0;
221}
222
223int do_class_stop(int nargs, char **args)
224{
225    service_for_each_class(args[1], service_stop);
226    return 0;
227}
228
229int do_class_reset(int nargs, char **args)
230{
231    service_for_each_class(args[1], service_reset);
232    return 0;
233}
234
235int do_domainname(int nargs, char **args)
236{
237    return write_file("/proc/sys/kernel/domainname", args[1]);
238}
239
240int do_exec(int nargs, char **args)
241{
242    return -1;
243}
244
245int do_export(int nargs, char **args)
246{
247    add_environment(args[1], args[2]);
248    return 0;
249}
250
251int do_hostname(int nargs, char **args)
252{
253    return write_file("/proc/sys/kernel/hostname", args[1]);
254}
255
256int do_ifup(int nargs, char **args)
257{
258    return __ifupdown(args[1], 1);
259}
260
261
262static int do_insmod_inner(int nargs, char **args, int opt_len)
263{
264    char options[opt_len + 1];
265    int i;
266
267    options[0] = '\0';
268    if (nargs > 2) {
269        strcpy(options, args[2]);
270        for (i = 3; i < nargs; ++i) {
271            strcat(options, " ");
272            strcat(options, args[i]);
273        }
274    }
275
276    return insmod(args[1], options);
277}
278
279int do_insmod(int nargs, char **args)
280{
281    int i;
282    int size = 0;
283
284    if (nargs > 2) {
285        for (i = 2; i < nargs; ++i)
286            size += strlen(args[i]) + 1;
287    }
288
289    return do_insmod_inner(nargs, args, size);
290}
291
292int do_mkdir(int nargs, char **args)
293{
294    mode_t mode = 0755;
295    int ret;
296
297    /* mkdir <path> [mode] [owner] [group] */
298
299    if (nargs >= 3) {
300        mode = strtoul(args[2], 0, 8);
301    }
302
303    ret = make_dir(args[1], mode);
304    /* chmod in case the directory already exists */
305    if (ret == -1 && errno == EEXIST) {
306        ret = _chmod(args[1], mode);
307    }
308    if (ret == -1) {
309        return -errno;
310    }
311
312    if (nargs >= 4) {
313        uid_t uid = decode_uid(args[3]);
314        gid_t gid = -1;
315
316        if (nargs == 5) {
317            gid = decode_uid(args[4]);
318        }
319
320        if (_chown(args[1], uid, gid) < 0) {
321            return -errno;
322        }
323    }
324
325    return 0;
326}
327
328static struct {
329    const char *name;
330    unsigned flag;
331} mount_flags[] = {
332    { "noatime",    MS_NOATIME },
333    { "noexec",     MS_NOEXEC },
334    { "nosuid",     MS_NOSUID },
335    { "nodev",      MS_NODEV },
336    { "nodiratime", MS_NODIRATIME },
337    { "ro",         MS_RDONLY },
338    { "rw",         0 },
339    { "remount",    MS_REMOUNT },
340    { "defaults",   0 },
341    { 0,            0 },
342};
343
344#define DATA_MNT_POINT "/data"
345
346/* mount <type> <device> <path> <flags ...> <options> */
347int do_mount(int nargs, char **args)
348{
349    char tmp[64];
350    char *source, *target, *system;
351    char *options = NULL;
352    unsigned flags = 0;
353    int n, i;
354    int wait = 0;
355
356    for (n = 4; n < nargs; n++) {
357        for (i = 0; mount_flags[i].name; i++) {
358            if (!strcmp(args[n], mount_flags[i].name)) {
359                flags |= mount_flags[i].flag;
360                break;
361            }
362        }
363
364        if (!mount_flags[i].name) {
365            if (!strcmp(args[n], "wait"))
366                wait = 1;
367            /* if our last argument isn't a flag, wolf it up as an option string */
368            else if (n + 1 == nargs)
369                options = args[n];
370        }
371    }
372
373    system = args[1];
374    source = args[2];
375    target = args[3];
376
377    if (!strncmp(source, "mtd@", 4)) {
378        n = mtd_name_to_number(source + 4);
379        if (n < 0) {
380            return -1;
381        }
382
383        sprintf(tmp, "/dev/block/mtdblock%d", n);
384
385        if (wait)
386            wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
387        if (mount(tmp, target, system, flags, options) < 0) {
388            return -1;
389        }
390
391        goto exit_success;
392    } else if (!strncmp(source, "loop@", 5)) {
393        int mode, loop, fd;
394        struct loop_info info;
395
396        mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
397        fd = open(source + 5, mode);
398        if (fd < 0) {
399            return -1;
400        }
401
402        for (n = 0; ; n++) {
403            sprintf(tmp, "/dev/block/loop%d", n);
404            loop = open(tmp, mode);
405            if (loop < 0) {
406                return -1;
407            }
408
409            /* if it is a blank loop device */
410            if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
411                /* if it becomes our loop device */
412                if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
413                    close(fd);
414
415                    if (mount(tmp, target, system, flags, options) < 0) {
416                        ioctl(loop, LOOP_CLR_FD, 0);
417                        close(loop);
418                        return -1;
419                    }
420
421                    close(loop);
422                    goto exit_success;
423                }
424            }
425
426            close(loop);
427        }
428
429        close(fd);
430        ERROR("out of loopback devices");
431        return -1;
432    } else {
433        if (wait)
434            wait_for_file(source, COMMAND_RETRY_TIMEOUT);
435        if (mount(source, target, system, flags, options) < 0) {
436            return -1;
437        }
438
439    }
440
441exit_success:
442    return 0;
443
444}
445
446int do_mount_all(int nargs, char **args)
447{
448    pid_t pid;
449    int ret = -1;
450    int child_ret = -1;
451    int status;
452    const char *prop;
453
454    if (nargs != 2) {
455        return -1;
456    }
457
458    /*
459     * Call fs_mgr_mount_all() to mount all filesystems.  We fork(2) and
460     * do the call in the child to provide protection to the main init
461     * process if anything goes wrong (crash or memory leak), and wait for
462     * the child to finish in the parent.
463     */
464    pid = fork();
465    if (pid > 0) {
466        /* Parent.  Wait for the child to return */
467        waitpid(pid, &status, 0);
468        if (WIFEXITED(status)) {
469            ret = WEXITSTATUS(status);
470        } else {
471            ret = -1;
472        }
473    } else if (pid == 0) {
474        /* child, call fs_mgr_mount_all() */
475        klog_set_level(6);  /* So we can see what fs_mgr_mount_all() does */
476        child_ret = fs_mgr_mount_all(args[1]);
477        if (child_ret == -1) {
478            ERROR("fs_mgr_mount_all returned an error\n");
479        }
480        exit(child_ret);
481    } else {
482        /* fork failed, return an error */
483        return -1;
484    }
485
486    /* ret is 1 if the device is encrypted, 0 if not, and -1 on error */
487    if (ret == 1) {
488        property_set("ro.crypto.state", "encrypted");
489        property_set("vold.decrypt", "1");
490    } else if (ret == 0) {
491        property_set("ro.crypto.state", "unencrypted");
492        /* If fs_mgr determined this is an unencrypted device, then trigger
493         * that action.
494         */
495        action_for_each_trigger("nonencrypted", action_add_queue_tail);
496    }
497
498    return ret;
499}
500
501int do_setcon(int nargs, char **args) {
502    if (is_selinux_enabled() <= 0)
503        return 0;
504    if (setcon(args[1]) < 0) {
505        return -errno;
506    }
507    return 0;
508}
509
510int do_setenforce(int nargs, char **args) {
511    if (is_selinux_enabled() <= 0)
512        return 0;
513    if (security_setenforce(atoi(args[1])) < 0) {
514        return -errno;
515    }
516    return 0;
517}
518
519int do_setkey(int nargs, char **args)
520{
521    struct kbentry kbe;
522    kbe.kb_table = strtoul(args[1], 0, 0);
523    kbe.kb_index = strtoul(args[2], 0, 0);
524    kbe.kb_value = strtoul(args[3], 0, 0);
525    return setkey(&kbe);
526}
527
528int do_setprop(int nargs, char **args)
529{
530    const char *name = args[1];
531    const char *value = args[2];
532    char prop_val[PROP_VALUE_MAX];
533    int ret;
534
535    ret = expand_props(prop_val, value, sizeof(prop_val));
536    if (ret) {
537        ERROR("cannot expand '%s' while assigning to '%s'\n", value, name);
538        return -EINVAL;
539    }
540    property_set(name, prop_val);
541    return 0;
542}
543
544int do_setrlimit(int nargs, char **args)
545{
546    struct rlimit limit;
547    int resource;
548    resource = atoi(args[1]);
549    limit.rlim_cur = atoi(args[2]);
550    limit.rlim_max = atoi(args[3]);
551    return setrlimit(resource, &limit);
552}
553
554int do_start(int nargs, char **args)
555{
556    struct service *svc;
557    svc = service_find_by_name(args[1]);
558    if (svc) {
559        service_start(svc, NULL);
560    }
561    return 0;
562}
563
564int do_stop(int nargs, char **args)
565{
566    struct service *svc;
567    svc = service_find_by_name(args[1]);
568    if (svc) {
569        service_stop(svc);
570    }
571    return 0;
572}
573
574int do_restart(int nargs, char **args)
575{
576    struct service *svc;
577    svc = service_find_by_name(args[1]);
578    if (svc) {
579        service_stop(svc);
580        service_start(svc, NULL);
581    }
582    return 0;
583}
584
585int do_trigger(int nargs, char **args)
586{
587    action_for_each_trigger(args[1], action_add_queue_tail);
588    return 0;
589}
590
591int do_symlink(int nargs, char **args)
592{
593    return symlink(args[1], args[2]);
594}
595
596int do_rm(int nargs, char **args)
597{
598    return unlink(args[1]);
599}
600
601int do_rmdir(int nargs, char **args)
602{
603    return rmdir(args[1]);
604}
605
606int do_sysclktz(int nargs, char **args)
607{
608    struct timezone tz;
609
610    if (nargs != 2)
611        return -1;
612
613    memset(&tz, 0, sizeof(tz));
614    tz.tz_minuteswest = atoi(args[1]);
615    if (settimeofday(NULL, &tz))
616        return -1;
617    return 0;
618}
619
620int do_write(int nargs, char **args)
621{
622    const char *path = args[1];
623    const char *value = args[2];
624    char prop_val[PROP_VALUE_MAX];
625    int ret;
626
627    ret = expand_props(prop_val, value, sizeof(prop_val));
628    if (ret) {
629        ERROR("cannot expand '%s' while writing to '%s'\n", value, path);
630        return -EINVAL;
631    }
632    return write_file(path, prop_val);
633}
634
635int do_copy(int nargs, char **args)
636{
637    char *buffer = NULL;
638    int rc = 0;
639    int fd1 = -1, fd2 = -1;
640    struct stat info;
641    int brtw, brtr;
642    char *p;
643
644    if (nargs != 3)
645        return -1;
646
647    if (stat(args[1], &info) < 0)
648        return -1;
649
650    if ((fd1 = open(args[1], O_RDONLY)) < 0)
651        goto out_err;
652
653    if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
654        goto out_err;
655
656    if (!(buffer = malloc(info.st_size)))
657        goto out_err;
658
659    p = buffer;
660    brtr = info.st_size;
661    while(brtr) {
662        rc = read(fd1, p, brtr);
663        if (rc < 0)
664            goto out_err;
665        if (rc == 0)
666            break;
667        p += rc;
668        brtr -= rc;
669    }
670
671    p = buffer;
672    brtw = info.st_size;
673    while(brtw) {
674        rc = write(fd2, p, brtw);
675        if (rc < 0)
676            goto out_err;
677        if (rc == 0)
678            break;
679        p += rc;
680        brtw -= rc;
681    }
682
683    rc = 0;
684    goto out;
685out_err:
686    rc = -1;
687out:
688    if (buffer)
689        free(buffer);
690    if (fd1 >= 0)
691        close(fd1);
692    if (fd2 >= 0)
693        close(fd2);
694    return rc;
695}
696
697int do_chown(int nargs, char **args) {
698    /* GID is optional. */
699    if (nargs == 3) {
700        if (_chown(args[2], decode_uid(args[1]), -1) < 0)
701            return -errno;
702    } else if (nargs == 4) {
703        if (_chown(args[3], decode_uid(args[1]), decode_uid(args[2])) < 0)
704            return -errno;
705    } else {
706        return -1;
707    }
708    return 0;
709}
710
711static mode_t get_mode(const char *s) {
712    mode_t mode = 0;
713    while (*s) {
714        if (*s >= '0' && *s <= '7') {
715            mode = (mode<<3) | (*s-'0');
716        } else {
717            return -1;
718        }
719        s++;
720    }
721    return mode;
722}
723
724int do_chmod(int nargs, char **args) {
725    mode_t mode = get_mode(args[1]);
726    if (_chmod(args[2], mode) < 0) {
727        return -errno;
728    }
729    return 0;
730}
731
732int do_restorecon(int nargs, char **args) {
733    int i;
734
735    for (i = 1; i < nargs; i++) {
736        if (restorecon(args[i]) < 0)
737            return -errno;
738    }
739    return 0;
740}
741
742int do_setsebool(int nargs, char **args) {
743    SELboolean *b = alloca(nargs * sizeof(SELboolean));
744    char *v;
745    int i;
746
747    if (is_selinux_enabled() <= 0)
748        return 0;
749
750    for (i = 1; i < nargs; i++) {
751        char *name = args[i];
752        v = strchr(name, '=');
753        if (!v) {
754            ERROR("setsebool: argument %s had no =\n", name);
755            return -EINVAL;
756        }
757        *v++ = 0;
758        b[i-1].name = name;
759        if (!strcmp(v, "1") || !strcasecmp(v, "true") || !strcasecmp(v, "on"))
760            b[i-1].value = 1;
761        else if (!strcmp(v, "0") || !strcasecmp(v, "false") || !strcasecmp(v, "off"))
762            b[i-1].value = 0;
763        else {
764            ERROR("setsebool: invalid value %s\n", v);
765            return -EINVAL;
766        }
767    }
768
769    if (security_set_boolean_list(nargs - 1, b, 0) < 0)
770        return -errno;
771
772    return 0;
773}
774
775int do_loglevel(int nargs, char **args) {
776    if (nargs == 2) {
777        klog_set_level(atoi(args[1]));
778        return 0;
779    }
780    return -1;
781}
782
783int do_load_persist_props(int nargs, char **args) {
784    if (nargs == 1) {
785        load_persist_props();
786        return 0;
787    }
788    return -1;
789}
790
791int do_wait(int nargs, char **args)
792{
793    if (nargs == 2) {
794        return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
795    } else if (nargs == 3) {
796        return wait_for_file(args[1], atoi(args[2]));
797    } else
798        return -1;
799}
800