builtins.c revision 4e84d3bcf9af3452128fe2e8dc7607dff9016fac
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        return 0;
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                    return 0;
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        } else {
397            if (!strcmp(target, DATA_MNT_POINT)) {
398                /* We succeeded in mounting /data, so it's not encrypted */
399                property_set("ro.crypto.state", "unencrypted");
400                action_for_each_trigger("nonencrypted", action_add_queue_tail);
401            }
402        }
403
404        if (!strcmp(target, DATA_MNT_POINT)) {
405            char fs_flags[32];
406
407            /* Save the original mount options */
408            property_set("ro.crypto.fs_type", system);
409            property_set("ro.crypto.fs_real_blkdev", source);
410            property_set("ro.crypto.fs_mnt_point", target);
411            if (options) {
412                property_set("ro.crypto.fs_options", options);
413            }
414            snprintf(fs_flags, sizeof(fs_flags), "0x%8.8x", flags);
415            property_set("ro.crypto.fs_flags", fs_flags);
416        }
417        return 0;
418    }
419}
420
421int do_setkey(int nargs, char **args)
422{
423    struct kbentry kbe;
424    kbe.kb_table = strtoul(args[1], 0, 0);
425    kbe.kb_index = strtoul(args[2], 0, 0);
426    kbe.kb_value = strtoul(args[3], 0, 0);
427    return setkey(&kbe);
428}
429
430int do_setprop(int nargs, char **args)
431{
432    property_set(args[1], args[2]);
433    return 0;
434}
435
436int do_setrlimit(int nargs, char **args)
437{
438    struct rlimit limit;
439    int resource;
440    resource = atoi(args[1]);
441    limit.rlim_cur = atoi(args[2]);
442    limit.rlim_max = atoi(args[3]);
443    return setrlimit(resource, &limit);
444}
445
446int do_start(int nargs, char **args)
447{
448    struct service *svc;
449    svc = service_find_by_name(args[1]);
450    if (svc) {
451        service_start(svc, NULL);
452    }
453    return 0;
454}
455
456int do_stop(int nargs, char **args)
457{
458    struct service *svc;
459    svc = service_find_by_name(args[1]);
460    if (svc) {
461        service_stop(svc);
462    }
463    return 0;
464}
465
466int do_restart(int nargs, char **args)
467{
468    struct service *svc;
469    svc = service_find_by_name(args[1]);
470    if (svc) {
471        service_stop(svc);
472        service_start(svc, NULL);
473    }
474    return 0;
475}
476
477int do_trigger(int nargs, char **args)
478{
479    action_for_each_trigger(args[1], action_add_queue_tail);
480    return 0;
481}
482
483int do_symlink(int nargs, char **args)
484{
485    return symlink(args[1], args[2]);
486}
487
488int do_sysclktz(int nargs, char **args)
489{
490    struct timezone tz;
491
492    if (nargs != 2)
493        return -1;
494
495    memset(&tz, 0, sizeof(tz));
496    tz.tz_minuteswest = atoi(args[1]);
497    if (settimeofday(NULL, &tz))
498        return -1;
499    return 0;
500}
501
502int do_write(int nargs, char **args)
503{
504    return write_file(args[1], args[2]);
505}
506
507int do_copy(int nargs, char **args)
508{
509    char *buffer = NULL;
510    int rc = 0;
511    int fd1 = -1, fd2 = -1;
512    struct stat info;
513    int brtw, brtr;
514    char *p;
515
516    if (nargs != 3)
517        return -1;
518
519    if (stat(args[1], &info) < 0)
520        return -1;
521
522    if ((fd1 = open(args[1], O_RDONLY)) < 0)
523        goto out_err;
524
525    if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0)
526        goto out_err;
527
528    if (!(buffer = malloc(info.st_size)))
529        goto out_err;
530
531    p = buffer;
532    brtr = info.st_size;
533    while(brtr) {
534        rc = read(fd1, p, brtr);
535        if (rc < 0)
536            goto out_err;
537        if (rc == 0)
538            break;
539        p += rc;
540        brtr -= rc;
541    }
542
543    p = buffer;
544    brtw = info.st_size;
545    while(brtw) {
546        rc = write(fd2, p, brtw);
547        if (rc < 0)
548            goto out_err;
549        if (rc == 0)
550            break;
551        p += rc;
552        brtw -= rc;
553    }
554
555    rc = 0;
556    goto out;
557out_err:
558    rc = -1;
559out:
560    if (buffer)
561        free(buffer);
562    if (fd1 >= 0)
563        close(fd1);
564    if (fd2 >= 0)
565        close(fd2);
566    return rc;
567}
568
569int do_chown(int nargs, char **args) {
570    /* GID is optional. */
571    if (nargs == 3) {
572        if (chown(args[2], decode_uid(args[1]), -1) < 0)
573            return -errno;
574    } else if (nargs == 4) {
575        if (chown(args[3], decode_uid(args[1]), decode_uid(args[2])))
576            return -errno;
577    } else {
578        return -1;
579    }
580    return 0;
581}
582
583static mode_t get_mode(const char *s) {
584    mode_t mode = 0;
585    while (*s) {
586        if (*s >= '0' && *s <= '7') {
587            mode = (mode<<3) | (*s-'0');
588        } else {
589            return -1;
590        }
591        s++;
592    }
593    return mode;
594}
595
596int do_chmod(int nargs, char **args) {
597    mode_t mode = get_mode(args[1]);
598    if (chmod(args[2], mode) < 0) {
599        return -errno;
600    }
601    return 0;
602}
603
604int do_loglevel(int nargs, char **args) {
605    if (nargs == 2) {
606        log_set_level(atoi(args[1]));
607        return 0;
608    }
609    return -1;
610}
611
612int do_wait(int nargs, char **args)
613{
614    if (nargs == 2) {
615        return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT);
616    }
617    return -1;
618}
619