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