builtins.c revision dd4d786cbf993a024fcd0f926fd40b90c8a08d51
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    char *prop;
290
291    for (n = 4; n < nargs; n++) {
292        for (i = 0; mount_flags[i].name; i++) {
293            if (!strcmp(args[n], mount_flags[i].name)) {
294                flags |= mount_flags[i].flag;
295                break;
296            }
297        }
298
299        if (!mount_flags[i].name) {
300            if (!strcmp(args[n], "wait"))
301                wait = 1;
302            /* if our last argument isn't a flag, wolf it up as an option string */
303            else if (n + 1 == nargs)
304                options = args[n];
305        }
306    }
307
308    system = args[1];
309    source = args[2];
310    target = args[3];
311
312    if (!strncmp(source, "mtd@", 4)) {
313        n = mtd_name_to_number(source + 4);
314        if (n < 0) {
315            return -1;
316        }
317
318        sprintf(tmp, "/dev/block/mtdblock%d", n);
319
320        if (wait)
321            wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
322        if (mount(tmp, target, system, flags, options) < 0) {
323            return -1;
324        }
325
326        goto exit_success;
327    } else if (!strncmp(source, "loop@", 5)) {
328        int mode, loop, fd;
329        struct loop_info info;
330
331        mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
332        fd = open(source + 5, mode);
333        if (fd < 0) {
334            return -1;
335        }
336
337        for (n = 0; ; n++) {
338            sprintf(tmp, "/dev/block/loop%d", n);
339            loop = open(tmp, mode);
340            if (loop < 0) {
341                return -1;
342            }
343
344            /* if it is a blank loop device */
345            if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
346                /* if it becomes our loop device */
347                if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
348                    close(fd);
349
350                    if (mount(tmp, target, system, flags, options) < 0) {
351                        ioctl(loop, LOOP_CLR_FD, 0);
352                        close(loop);
353                        return -1;
354                    }
355
356                    close(loop);
357                    goto exit_success;
358                }
359            }
360
361            close(loop);
362        }
363
364        close(fd);
365        ERROR("out of loopback devices");
366        return -1;
367    } else {
368        if (wait)
369            wait_for_file(source, COMMAND_RETRY_TIMEOUT);
370        if (mount(source, target, system, flags, options) < 0) {
371            /* If this fails, it may be an encrypted filesystem.
372             * We only support encrypting /data.  Check
373             * if we're trying to mount it, and if so,
374             * assume it's encrypted, mount a tmpfs instead.
375             * Then save the orig mount parms in properties
376             * for vold to query when it mounts the real
377             * encrypted /data.
378             */
379            if (!strcmp(target, DATA_MNT_POINT)) {
380                const char *tmpfs_options;
381
382                tmpfs_options = property_get("ro.crypto.tmpfs_options");
383
384                if (mount("tmpfs", target, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV,
385                    tmpfs_options) < 0) {
386                    return -1;
387                }
388
389                /* Set the property that triggers the framework to do a minimal
390                 * startup and ask the user for a password
391                 */
392                property_set("ro.crypto.state", "encrypted");
393                property_set("vold.decrypt", "1");
394            } else {
395                return -1;
396            }
397        }
398
399        if (!strcmp(target, DATA_MNT_POINT)) {
400            char fs_flags[32];
401
402            /* Save the original mount options */
403            property_set("ro.crypto.fs_type", system);
404            property_set("ro.crypto.fs_real_blkdev", source);
405            property_set("ro.crypto.fs_mnt_point", target);
406            if (options) {
407                property_set("ro.crypto.fs_options", options);
408            }
409            snprintf(fs_flags, sizeof(fs_flags), "0x%8.8x", flags);
410            property_set("ro.crypto.fs_flags", fs_flags);
411        }
412    }
413
414exit_success:
415    /* If not running encrypted, then set the property saying we are
416     * unencrypted, and also trigger the action for a nonencrypted system.
417     */
418    if (!strcmp(target, DATA_MNT_POINT)) {
419        prop = property_get("ro.crypto.state");
420        if (! prop) {
421            prop = "notset";
422        }
423        if (strcmp(prop, "encrypted")) {
424            property_set("ro.crypto.state", "unencrypted");
425            action_for_each_trigger("nonencrypted", action_add_queue_tail);
426        }
427    }
428
429    return 0;
430
431}
432
433int do_setkey(int nargs, char **args)
434{
435    struct kbentry kbe;
436    kbe.kb_table = strtoul(args[1], 0, 0);
437    kbe.kb_index = strtoul(args[2], 0, 0);
438    kbe.kb_value = strtoul(args[3], 0, 0);
439    return setkey(&kbe);
440}
441
442int do_setprop(int nargs, char **args)
443{
444    property_set(args[1], args[2]);
445    return 0;
446}
447
448int do_setrlimit(int nargs, char **args)
449{
450    struct rlimit limit;
451    int resource;
452    resource = atoi(args[1]);
453    limit.rlim_cur = atoi(args[2]);
454    limit.rlim_max = atoi(args[3]);
455    return setrlimit(resource, &limit);
456}
457
458int do_start(int nargs, char **args)
459{
460    struct service *svc;
461    svc = service_find_by_name(args[1]);
462    if (svc) {
463        service_start(svc, NULL);
464    }
465    return 0;
466}
467
468int do_stop(int nargs, char **args)
469{
470    struct service *svc;
471    svc = service_find_by_name(args[1]);
472    if (svc) {
473        service_stop(svc);
474    }
475    return 0;
476}
477
478int do_restart(int nargs, char **args)
479{
480    struct service *svc;
481    svc = service_find_by_name(args[1]);
482    if (svc) {
483        service_stop(svc);
484        service_start(svc, NULL);
485    }
486    return 0;
487}
488
489int do_trigger(int nargs, char **args)
490{
491    action_for_each_trigger(args[1], action_add_queue_tail);
492    return 0;
493}
494
495int do_symlink(int nargs, char **args)
496{
497    return symlink(args[1], args[2]);
498}
499
500int do_rm(int nargs, char **args)
501{
502    return unlink(args[1]);
503}
504
505int do_rmdir(int nargs, char **args)
506{
507    return rmdir(args[1]);
508}
509
510int do_sysclktz(int nargs, char **args)
511{
512    struct timezone tz;
513
514    if (nargs != 2)
515        return -1;
516
517    memset(&tz, 0, sizeof(tz));
518    tz.tz_minuteswest = atoi(args[1]);
519    if (settimeofday(NULL, &tz))
520        return -1;
521    return 0;
522}
523
524int do_write(int nargs, char **args)
525{
526    return write_file(args[1], args[2]);
527}
528
529int do_copy(int nargs, char **args)
530{
531    char *buffer = NULL;
532    int rc = 0;
533    int fd1 = -1, fd2 = -1;
534    struct stat info;
535    int brtw, brtr;
536    char *p;
537
538    if (nargs != 3)
539        return -1;
540
541    if (stat(args[1], &info) < 0)
542        return -1;
543
544    if ((fd1 = open(args[1], O_RDONLY)) < 0)
545        goto out_err;
546
547    if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
548        goto out_err;
549
550    if (!(buffer = malloc(info.st_size)))
551        goto out_err;
552
553    p = buffer;
554    brtr = info.st_size;
555    while(brtr) {
556        rc = read(fd1, p, brtr);
557        if (rc < 0)
558            goto out_err;
559        if (rc == 0)
560            break;
561        p += rc;
562        brtr -= rc;
563    }
564
565    p = buffer;
566    brtw = info.st_size;
567    while(brtw) {
568        rc = write(fd2, p, brtw);
569        if (rc < 0)
570            goto out_err;
571        if (rc == 0)
572            break;
573        p += rc;
574        brtw -= rc;
575    }
576
577    rc = 0;
578    goto out;
579out_err:
580    rc = -1;
581out:
582    if (buffer)
583        free(buffer);
584    if (fd1 >= 0)
585        close(fd1);
586    if (fd2 >= 0)
587        close(fd2);
588    return rc;
589}
590
591int do_chown(int nargs, char **args) {
592    /* GID is optional. */
593    if (nargs == 3) {
594        if (chown(args[2], decode_uid(args[1]), -1) < 0)
595            return -errno;
596    } else if (nargs == 4) {
597        if (chown(args[3], decode_uid(args[1]), decode_uid(args[2])))
598            return -errno;
599    } else {
600        return -1;
601    }
602    return 0;
603}
604
605static mode_t get_mode(const char *s) {
606    mode_t mode = 0;
607    while (*s) {
608        if (*s >= '0' && *s <= '7') {
609            mode = (mode<<3) | (*s-'0');
610        } else {
611            return -1;
612        }
613        s++;
614    }
615    return mode;
616}
617
618int do_chmod(int nargs, char **args) {
619    mode_t mode = get_mode(args[1]);
620    if (chmod(args[2], mode) < 0) {
621        return -errno;
622    }
623    return 0;
624}
625
626int do_loglevel(int nargs, char **args) {
627    if (nargs == 2) {
628        log_set_level(atoi(args[1]));
629        return 0;
630    }
631    return -1;
632}
633
634int do_wait(int nargs, char **args)
635{
636    if (nargs == 2) {
637        return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
638    }
639    return -1;
640}
641