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