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