devices.c revision 3306e0678c5e68c1b7e8d5c99499b7645e4952f3
1/*
2 * Copyright (C) 2007 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 <errno.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <fcntl.h>
24#include <dirent.h>
25#include <unistd.h>
26#include <string.h>
27
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <linux/netlink.h>
31#include <private/android_filesystem_config.h>
32#include <sys/time.h>
33#include <asm/page.h>
34
35#include "init.h"
36#include "devices.h"
37
38#define CMDLINE_PREFIX  "/dev"
39#define SYSFS_PREFIX    "/sys"
40#define FIRMWARE_DIR    "/etc/firmware"
41#define MAX_QEMU_PERM 6
42
43struct uevent {
44    const char *action;
45    const char *path;
46    const char *subsystem;
47    const char *firmware;
48    int major;
49    int minor;
50};
51
52static int open_uevent_socket(void)
53{
54    struct sockaddr_nl addr;
55    int sz = 64*1024; // XXX larger? udev uses 16MB!
56    int s;
57
58    memset(&addr, 0, sizeof(addr));
59    addr.nl_family = AF_NETLINK;
60    addr.nl_pid = getpid();
61    addr.nl_groups = 0xffffffff;
62
63    s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
64    if(s < 0)
65        return -1;
66
67    setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
68
69    if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
70        close(s);
71        return -1;
72    }
73
74    return s;
75}
76
77struct perms_ {
78    char *name;
79    mode_t perm;
80    unsigned int uid;
81    unsigned int gid;
82    unsigned short prefix;
83};
84static struct perms_ devperms[] = {
85    { "/dev/null",          0666,   AID_ROOT,       AID_ROOT,       0 },
86    { "/dev/zero",          0666,   AID_ROOT,       AID_ROOT,       0 },
87    { "/dev/full",          0666,   AID_ROOT,       AID_ROOT,       0 },
88    { "/dev/ptmx",          0666,   AID_ROOT,       AID_ROOT,       0 },
89    { "/dev/tty",           0666,   AID_ROOT,       AID_ROOT,       0 },
90    { "/dev/random",        0666,   AID_ROOT,       AID_ROOT,       0 },
91    { "/dev/urandom",       0666,   AID_ROOT,       AID_ROOT,       0 },
92    { "/dev/ashmem",        0666,   AID_ROOT,       AID_ROOT,       0 },
93    { "/dev/binder",        0666,   AID_ROOT,       AID_ROOT,       0 },
94
95	    /* logger should be world writable (for logging) but not readable */
96    { "/dev/log/",          0662,   AID_ROOT,       AID_LOG,        1 },
97
98        /* these should not be world writable */
99    { "/dev/android_adb",   0660,   AID_ADB,        AID_ADB,        0 },
100    { "/dev/android_adb_enable",   0660,   AID_ADB,        AID_ADB,        0 },
101    { "/dev/ttyMSM0",       0600,   AID_BLUETOOTH,  AID_BLUETOOTH,  0 },
102    { "/dev/ttyHS0",        0600,   AID_BLUETOOTH,  AID_BLUETOOTH,  0 },
103    { "/dev/uinput",        0600,   AID_BLUETOOTH,  AID_BLUETOOTH,  0 },
104    { "/dev/alarm",         0664,   AID_SYSTEM,     AID_RADIO,      0 },
105    { "/dev/tty0",          0660,   AID_ROOT,       AID_SYSTEM,     0 },
106    { "/dev/graphics/",     0660,   AID_ROOT,       AID_GRAPHICS,   1 },
107    { "/dev/hw3d",          0660,   AID_SYSTEM,     AID_GRAPHICS,   0 },
108    { "/dev/input/",        0660,   AID_ROOT,       AID_INPUT,      1 },
109    { "/dev/eac",           0660,   AID_ROOT,       AID_AUDIO,      0 },
110    { "/dev/cam",           0660,   AID_ROOT,       AID_CAMERA,     0 },
111    { "/dev/pmem",          0660,   AID_SYSTEM,     AID_GRAPHICS,   0 },
112    { "/dev/pmem_gpu",      0660,   AID_SYSTEM,     AID_GRAPHICS,   1 },
113    { "/dev/pmem_adsp",     0660,   AID_SYSTEM,     AID_AUDIO,      1 },
114    { "/dev/pmem_camera",   0660,   AID_SYSTEM,     AID_CAMERA,     1 },
115    { "/dev/oncrpc/",       0660,   AID_ROOT,       AID_SYSTEM,     1 },
116    { "/dev/adsp/",         0660,   AID_SYSTEM,     AID_AUDIO,      1 },
117    { "/dev/mt9t013",       0660,   AID_SYSTEM,     AID_SYSTEM,     0 },
118    { "/dev/msm_camera/",   0660,   AID_SYSTEM,     AID_SYSTEM,     1 },
119    { "/dev/akm8976_daemon",0640,   AID_COMPASS,    AID_SYSTEM,     0 },
120    { "/dev/akm8976_aot",   0640,   AID_COMPASS,    AID_SYSTEM,     0 },
121    { "/dev/akm8973_daemon",0640,   AID_COMPASS,    AID_SYSTEM,     0 },
122    { "/dev/akm8973_aot",   0640,   AID_COMPASS,    AID_SYSTEM,     0 },
123    { "/dev/bma150",        0640,   AID_COMPASS,    AID_SYSTEM,     0 },
124    { "/dev/cm3602",        0640,   AID_COMPASS,    AID_SYSTEM,     0 },
125    { "/dev/akm8976_pffd",  0640,   AID_COMPASS,    AID_SYSTEM,     0 },
126    { "/dev/msm_pcm_out",   0660,   AID_SYSTEM,     AID_AUDIO,      1 },
127    { "/dev/msm_pcm_in",    0660,   AID_SYSTEM,     AID_AUDIO,      1 },
128    { "/dev/msm_pcm_ctl",   0660,   AID_SYSTEM,     AID_AUDIO,      1 },
129    { "/dev/msm_snd",       0660,   AID_SYSTEM,     AID_AUDIO,      1 },
130    { "/dev/msm_mp3",       0660,   AID_SYSTEM,     AID_AUDIO,      1 },
131    { "/dev/msm_audpre",    0660,   AID_SYSTEM,     AID_AUDIO,      0 },
132    { "/dev/htc-acoustic",  0660,   AID_SYSTEM,     AID_AUDIO,      0 },
133    { "/dev/snd/dsp",       0660,   AID_SYSTEM,     AID_AUDIO,      0 },
134    { "/dev/snd/dsp1",      0660,   AID_SYSTEM,     AID_AUDIO,      0 },
135    { "/dev/snd/mixer",     0660,   AID_SYSTEM,     AID_AUDIO,      0 },
136    { "/dev/smd0",          0640,   AID_RADIO,      AID_RADIO,      0 },
137    { "/dev/qemu_trace",    0666,   AID_SYSTEM,     AID_SYSTEM,     0 },
138    { "/dev/qmi",           0640,   AID_RADIO,      AID_RADIO,      0 },
139    { "/dev/qmi0",          0640,   AID_RADIO,      AID_RADIO,      0 },
140    { "/dev/qmi1",          0640,   AID_RADIO,      AID_RADIO,      0 },
141    { "/dev/qmi2",          0640,   AID_RADIO,      AID_RADIO,      0 },
142        /* CDMA radio interface MUX */
143    { "/dev/ts0710mux",     0640,   AID_RADIO,      AID_RADIO,      1 },
144    { "/dev/ppp",           0660,   AID_RADIO,      AID_VPN,        0 },
145    { "/dev/tun",           0640,   AID_VPN,        AID_VPN,        0 },
146    { NULL, 0, 0, 0, 0 },
147};
148
149/* devperms_partners list and perm_node are for hardware specific /dev entries */
150struct perm_node {
151    struct perms_ dp;
152    struct listnode plist;
153};
154list_declare(devperms_partners);
155
156/*
157 * Permission override when in emulator mode, must be parsed before
158 * system properties is initalized.
159 */
160static int qemu_perm_count;
161static struct perms_ qemu_perms[MAX_QEMU_PERM + 1];
162
163int add_devperms_partners(const char *name, mode_t perm, unsigned int uid,
164                        unsigned int gid, unsigned short prefix) {
165    int size;
166    struct perm_node *node = malloc(sizeof (struct perm_node));
167    if (!node)
168        return -ENOMEM;
169
170    size = strlen(name) + 1;
171    if ((node->dp.name = malloc(size)) == NULL)
172        return -ENOMEM;
173
174    memcpy(node->dp.name, name, size);
175    node->dp.perm = perm;
176    node->dp.uid = uid;
177    node->dp.gid = gid;
178    node->dp.prefix = prefix;
179
180    list_add_tail(&devperms_partners, &node->plist);
181    return 0;
182}
183
184void qemu_init(void) {
185    qemu_perm_count = 0;
186    memset(&qemu_perms, 0, sizeof(qemu_perms));
187}
188
189static int qemu_perm(const char* name, mode_t perm, unsigned int uid,
190                         unsigned int gid, unsigned short prefix)
191{
192    char *buf;
193    if (qemu_perm_count == MAX_QEMU_PERM)
194        return -ENOSPC;
195
196    buf = malloc(strlen(name) + 1);
197    if (!buf)
198        return -errno;
199
200    strlcpy(buf, name, strlen(name) + 1);
201    qemu_perms[qemu_perm_count].name = buf;
202    qemu_perms[qemu_perm_count].perm = perm;
203    qemu_perms[qemu_perm_count].uid = uid;
204    qemu_perms[qemu_perm_count].gid = gid;
205    qemu_perms[qemu_perm_count].prefix = prefix;
206
207    qemu_perm_count++;
208    return 0;
209}
210
211/* Permission overrides for emulator that are parsed from /proc/cmdline. */
212void qemu_cmdline(const char* name, const char *value)
213{
214    char *buf;
215    if (!strcmp(name, "android.ril")) {
216        /* cmd line params currently assume /dev/ prefix */
217        if (asprintf(&buf, CMDLINE_PREFIX"/%s", value) == -1) {
218            return;
219        }
220        INFO("nani- buf:: %s\n", buf);
221        qemu_perm(buf, 0660, AID_RADIO, AID_ROOT, 0);
222    }
223}
224
225static int get_device_perm_inner(struct perms_ *perms, const char *path,
226                                    unsigned *uid, unsigned *gid, mode_t *perm)
227{
228    int i;
229    for(i = 0; perms[i].name; i++) {
230
231        if(perms[i].prefix) {
232            if(strncmp(path, perms[i].name, strlen(perms[i].name)))
233                continue;
234        } else {
235            if(strcmp(path, perms[i].name))
236                continue;
237        }
238        *uid = perms[i].uid;
239        *gid = perms[i].gid;
240        *perm = perms[i].perm;
241        return 0;
242    }
243    return -1;
244}
245
246/* First checks for emulator specific permissions specified in /proc/cmdline. */
247static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
248{
249    mode_t perm;
250
251    if (get_device_perm_inner(qemu_perms, path, uid, gid, &perm) == 0) {
252        return perm;
253    } else if (get_device_perm_inner(devperms, path, uid, gid, &perm) == 0) {
254        return perm;
255    } else {
256        struct listnode *node;
257        struct perm_node *perm_node;
258        struct perms_ *dp;
259
260        /* Check partners list. */
261        list_for_each(node, &devperms_partners) {
262            perm_node = node_to_item(node, struct perm_node, plist);
263            dp = &perm_node->dp;
264
265            if (dp->prefix) {
266                if (strncmp(path, dp->name, strlen(dp->name)))
267                    continue;
268            } else {
269                if (strcmp(path, dp->name))
270                    continue;
271            }
272            /* Found perm in partner list. */
273            *uid = dp->uid;
274            *gid = dp->gid;
275            return dp->perm;
276        }
277        /* Default if nothing found. */
278        *uid = 0;
279        *gid = 0;
280        return 0600;
281    }
282}
283
284static void make_device(const char *path, int block, int major, int minor)
285{
286    unsigned uid;
287    unsigned gid;
288    mode_t mode;
289    dev_t dev;
290
291    if(major > 255 || minor > 255)
292        return;
293
294    mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
295    dev = (major << 8) | minor;
296    mknod(path, mode, dev);
297    chown(path, uid, gid);
298}
299
300#ifdef LOG_UEVENTS
301
302static inline suseconds_t get_usecs(void)
303{
304    struct timeval tv;
305    gettimeofday(&tv, 0);
306    return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
307}
308
309#define log_event_print(x...) INFO(x)
310
311#else
312
313#define log_event_print(fmt, args...)   do { } while (0)
314#define get_usecs()                     0
315
316#endif
317
318static void parse_event(const char *msg, struct uevent *uevent)
319{
320    uevent->action = "";
321    uevent->path = "";
322    uevent->subsystem = "";
323    uevent->firmware = "";
324    uevent->major = -1;
325    uevent->minor = -1;
326
327        /* currently ignoring SEQNUM */
328    while(*msg) {
329        if(!strncmp(msg, "ACTION=", 7)) {
330            msg += 7;
331            uevent->action = msg;
332        } else if(!strncmp(msg, "DEVPATH=", 8)) {
333            msg += 8;
334            uevent->path = msg;
335        } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
336            msg += 10;
337            uevent->subsystem = msg;
338        } else if(!strncmp(msg, "FIRMWARE=", 9)) {
339            msg += 9;
340            uevent->firmware = msg;
341        } else if(!strncmp(msg, "MAJOR=", 6)) {
342            msg += 6;
343            uevent->major = atoi(msg);
344        } else if(!strncmp(msg, "MINOR=", 6)) {
345            msg += 6;
346            uevent->minor = atoi(msg);
347        }
348
349            /* advance to after the next \0 */
350        while(*msg++)
351            ;
352    }
353
354    log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
355                    uevent->action, uevent->path, uevent->subsystem,
356                    uevent->firmware, uevent->major, uevent->minor);
357}
358
359static void handle_device_event(struct uevent *uevent)
360{
361    char devpath[96];
362    char *base, *name;
363    int block;
364
365        /* if it's not a /dev device, nothing to do */
366    if((uevent->major < 0) || (uevent->minor < 0))
367        return;
368
369        /* do we have a name? */
370    name = strrchr(uevent->path, '/');
371    if(!name)
372        return;
373    name++;
374
375        /* too-long names would overrun our buffer */
376    if(strlen(name) > 64)
377        return;
378
379        /* are we block or char? where should we live? */
380    if(!strncmp(uevent->subsystem, "block", 5)) {
381        block = 1;
382        base = "/dev/block/";
383        mkdir(base, 0755);
384    } else {
385        block = 0;
386            /* this should probably be configurable somehow */
387        if(!strncmp(uevent->subsystem, "graphics", 8)) {
388            base = "/dev/graphics/";
389            mkdir(base, 0755);
390        } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
391            base = "/dev/oncrpc/";
392            mkdir(base, 0755);
393        } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
394            base = "/dev/adsp/";
395            mkdir(base, 0755);
396        } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
397            base = "/dev/msm_camera/";
398            mkdir(base, 0755);
399        } else if(!strncmp(uevent->subsystem, "input", 5)) {
400            base = "/dev/input/";
401            mkdir(base, 0755);
402        } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
403            base = "/dev/mtd/";
404            mkdir(base, 0755);
405        } else if(!strncmp(uevent->subsystem, "sound", 5)) {
406            base = "/dev/snd/";
407            mkdir(base, 0755);
408        } else if(!strncmp(uevent->subsystem, "misc", 4) &&
409                    !strncmp(name, "log_", 4)) {
410            base = "/dev/log/";
411            mkdir(base, 0755);
412            name += 4;
413        } else
414            base = "/dev/";
415    }
416
417    snprintf(devpath, sizeof(devpath), "%s%s", base, name);
418
419    if(!strcmp(uevent->action, "add")) {
420        make_device(devpath, block, uevent->major, uevent->minor);
421        return;
422    }
423
424    if(!strcmp(uevent->action, "remove")) {
425        unlink(devpath);
426        return;
427    }
428}
429
430static int load_firmware(int fw_fd, int loading_fd, int data_fd)
431{
432    struct stat st;
433    long len_to_copy;
434    int ret = 0;
435
436    if(fstat(fw_fd, &st) < 0)
437        return -1;
438    len_to_copy = st.st_size;
439
440    write(loading_fd, "1", 1);  /* start transfer */
441
442    while (len_to_copy > 0) {
443        char buf[PAGE_SIZE];
444        ssize_t nr;
445
446        nr = read(fw_fd, buf, sizeof(buf));
447        if(!nr)
448            break;
449        if(nr < 0) {
450            ret = -1;
451            break;
452        }
453
454        len_to_copy -= nr;
455        while (nr > 0) {
456            ssize_t nw = 0;
457
458            nw = write(data_fd, buf + nw, nr);
459            if(nw <= 0) {
460                ret = -1;
461                goto out;
462            }
463            nr -= nw;
464        }
465    }
466
467out:
468    if(!ret)
469        write(loading_fd, "0", 1);  /* successful end of transfer */
470    else
471        write(loading_fd, "-1", 2); /* abort transfer */
472
473    return ret;
474}
475
476static void process_firmware_event(struct uevent *uevent)
477{
478    char *root, *loading, *data, *file;
479    int l, loading_fd, data_fd, fw_fd;
480
481    log_event_print("firmware event { '%s', '%s' }\n",
482                    uevent->path, uevent->firmware);
483
484    l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
485    if (l == -1)
486        return;
487
488    l = asprintf(&loading, "%sloading", root);
489    if (l == -1)
490        goto root_free_out;
491
492    l = asprintf(&data, "%sdata", root);
493    if (l == -1)
494        goto loading_free_out;
495
496    l = asprintf(&file, FIRMWARE_DIR"/%s", uevent->firmware);
497    if (l == -1)
498        goto data_free_out;
499
500    loading_fd = open(loading, O_WRONLY);
501    if(loading_fd < 0)
502        goto file_free_out;
503
504    data_fd = open(data, O_WRONLY);
505    if(data_fd < 0)
506        goto loading_close_out;
507
508    fw_fd = open(file, O_RDONLY);
509    if(fw_fd < 0)
510        goto data_close_out;
511
512    if(!load_firmware(fw_fd, loading_fd, data_fd))
513        log_event_print("firmware copy success { '%s', '%s' }\n", root, file);
514    else
515        log_event_print("firmware copy failure { '%s', '%s' }\n", root, file);
516
517    close(fw_fd);
518data_close_out:
519    close(data_fd);
520loading_close_out:
521    close(loading_fd);
522file_free_out:
523    free(file);
524data_free_out:
525    free(data);
526loading_free_out:
527    free(loading);
528root_free_out:
529    free(root);
530}
531
532static void handle_firmware_event(struct uevent *uevent)
533{
534    pid_t pid;
535
536    if(strcmp(uevent->subsystem, "firmware"))
537        return;
538
539    if(strcmp(uevent->action, "add"))
540        return;
541
542    /* we fork, to avoid making large memory allocations in init proper */
543    pid = fork();
544    if (!pid) {
545        process_firmware_event(uevent);
546        exit(EXIT_SUCCESS);
547    }
548}
549
550#define UEVENT_MSG_LEN  1024
551void handle_device_fd(int fd)
552{
553    char msg[UEVENT_MSG_LEN+2];
554    int n;
555
556    while((n = recv(fd, msg, UEVENT_MSG_LEN, 0)) > 0) {
557        struct uevent uevent;
558
559        if(n == UEVENT_MSG_LEN)   /* overflow -- discard */
560            continue;
561
562        msg[n] = '\0';
563        msg[n+1] = '\0';
564
565        parse_event(msg, &uevent);
566
567        handle_device_event(&uevent);
568        handle_firmware_event(&uevent);
569    }
570}
571
572/* Coldboot walks parts of the /sys tree and pokes the uevent files
573** to cause the kernel to regenerate device add events that happened
574** before init's device manager was started
575**
576** We drain any pending events from the netlink socket every time
577** we poke another uevent file to make sure we don't overrun the
578** socket's buffer.
579*/
580
581static void do_coldboot(int event_fd, DIR *d)
582{
583    struct dirent *de;
584    int dfd, fd;
585
586    dfd = dirfd(d);
587
588    fd = openat(dfd, "uevent", O_WRONLY);
589    if(fd >= 0) {
590        write(fd, "add\n", 4);
591        close(fd);
592        handle_device_fd(event_fd);
593    }
594
595    while((de = readdir(d))) {
596        DIR *d2;
597
598        if(de->d_type != DT_DIR || de->d_name[0] == '.')
599            continue;
600
601        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
602        if(fd < 0)
603            continue;
604
605        d2 = fdopendir(fd);
606        if(d2 == 0)
607            close(fd);
608        else {
609            do_coldboot(event_fd, d2);
610            closedir(d2);
611        }
612    }
613}
614
615static void coldboot(int event_fd, const char *path)
616{
617    DIR *d = opendir(path);
618    if(d) {
619        do_coldboot(event_fd, d);
620        closedir(d);
621    }
622}
623
624int device_init(void)
625{
626    suseconds_t t0, t1;
627    int fd;
628
629    fd = open_uevent_socket();
630    if(fd < 0)
631        return -1;
632
633    fcntl(fd, F_SETFD, FD_CLOEXEC);
634    fcntl(fd, F_SETFL, O_NONBLOCK);
635
636    t0 = get_usecs();
637    coldboot(fd, "/sys/class");
638    coldboot(fd, "/sys/block");
639    coldboot(fd, "/sys/devices");
640    t1 = get_usecs();
641
642    log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
643
644    return fd;
645}
646