devices.c revision 5f7b017f41b5bd0b86b5078b17c41ef7bc201c8d
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 <stddef.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <sys/stat.h>
22#include <sys/types.h>
23
24#include <fcntl.h>
25#include <dirent.h>
26#include <unistd.h>
27#include <string.h>
28
29#include <sys/socket.h>
30#include <sys/un.h>
31#include <linux/netlink.h>
32
33#include <selinux/selinux.h>
34#include <selinux/label.h>
35#include <selinux/android.h>
36
37#include <private/android_filesystem_config.h>
38#include <sys/time.h>
39#include <asm/page.h>
40#include <sys/wait.h>
41
42#include <cutils/list.h>
43#include <cutils/uevent.h>
44
45#include "devices.h"
46#include "util.h"
47#include "log.h"
48
49#define SYSFS_PREFIX    "/sys"
50#define FIRMWARE_DIR1   "/etc/firmware"
51#define FIRMWARE_DIR2   "/vendor/firmware"
52#define FIRMWARE_DIR3   "/firmware/image"
53
54extern struct selabel_handle *sehandle;
55
56static int device_fd = -1;
57
58struct uevent {
59    const char *action;
60    const char *path;
61    const char *subsystem;
62    const char *firmware;
63    const char *partition_name;
64    const char *device_name;
65    int partition_num;
66    int major;
67    int minor;
68};
69
70struct perms_ {
71    char *name;
72    char *attr;
73    mode_t perm;
74    unsigned int uid;
75    unsigned int gid;
76    unsigned short prefix;
77};
78
79struct perm_node {
80    struct perms_ dp;
81    struct listnode plist;
82};
83
84struct platform_node {
85    char *name;
86    char *path;
87    int path_len;
88    struct listnode list;
89};
90
91static list_declare(sys_perms);
92static list_declare(dev_perms);
93static list_declare(platform_names);
94
95int add_dev_perms(const char *name, const char *attr,
96                  mode_t perm, unsigned int uid, unsigned int gid,
97                  unsigned short prefix) {
98    struct perm_node *node = calloc(1, sizeof(*node));
99    if (!node)
100        return -ENOMEM;
101
102    node->dp.name = strdup(name);
103    if (!node->dp.name)
104        return -ENOMEM;
105
106    if (attr) {
107        node->dp.attr = strdup(attr);
108        if (!node->dp.attr)
109            return -ENOMEM;
110    }
111
112    node->dp.perm = perm;
113    node->dp.uid = uid;
114    node->dp.gid = gid;
115    node->dp.prefix = prefix;
116
117    if (attr)
118        list_add_tail(&sys_perms, &node->plist);
119    else
120        list_add_tail(&dev_perms, &node->plist);
121
122    return 0;
123}
124
125void fixup_sys_perms(const char *upath)
126{
127    char buf[512];
128    struct listnode *node;
129    struct perms_ *dp;
130    char *secontext;
131
132        /* upaths omit the "/sys" that paths in this list
133         * contain, so we add 4 when comparing...
134         */
135    list_for_each(node, &sys_perms) {
136        dp = &(node_to_item(node, struct perm_node, plist))->dp;
137        if (dp->prefix) {
138            if (strncmp(upath, dp->name + 4, strlen(dp->name + 4)))
139                continue;
140        } else {
141            if (strcmp(upath, dp->name + 4))
142                continue;
143        }
144
145        if ((strlen(upath) + strlen(dp->attr) + 6) > sizeof(buf))
146            return;
147
148        sprintf(buf,"/sys%s/%s", upath, dp->attr);
149        INFO("fixup %s %d %d 0%o\n", buf, dp->uid, dp->gid, dp->perm);
150        chown(buf, dp->uid, dp->gid);
151        chmod(buf, dp->perm);
152        if (sehandle) {
153            secontext = NULL;
154            selabel_lookup(sehandle, &secontext, buf, 0);
155            if (secontext) {
156                setfilecon(buf, secontext);
157                freecon(secontext);
158           }
159        }
160    }
161}
162
163static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
164{
165    mode_t perm;
166    struct listnode *node;
167    struct perm_node *perm_node;
168    struct perms_ *dp;
169
170    /* search the perms list in reverse so that ueventd.$hardware can
171     * override ueventd.rc
172     */
173    list_for_each_reverse(node, &dev_perms) {
174        perm_node = node_to_item(node, struct perm_node, plist);
175        dp = &perm_node->dp;
176
177        if (dp->prefix) {
178            if (strncmp(path, dp->name, strlen(dp->name)))
179                continue;
180        } else {
181            if (strcmp(path, dp->name))
182                continue;
183        }
184        *uid = dp->uid;
185        *gid = dp->gid;
186        return dp->perm;
187    }
188    /* Default if nothing found. */
189    *uid = 0;
190    *gid = 0;
191    return 0600;
192}
193
194static void make_device(const char *path,
195                        const char *upath,
196                        int block, int major, int minor)
197{
198    unsigned uid;
199    unsigned gid;
200    mode_t mode;
201    dev_t dev;
202    char *secontext = NULL;
203
204    mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
205
206    if (sehandle) {
207        selabel_lookup(sehandle, &secontext, path, mode);
208        setfscreatecon(secontext);
209    }
210
211    dev = makedev(major, minor);
212    /* Temporarily change egid to avoid race condition setting the gid of the
213     * device node. Unforunately changing the euid would prevent creation of
214     * some device nodes, so the uid has to be set with chown() and is still
215     * racy. Fixing the gid race at least fixed the issue with system_server
216     * opening dynamic input devices under the AID_INPUT gid. */
217    setegid(gid);
218    mknod(path, mode, dev);
219    chown(path, uid, -1);
220    setegid(AID_ROOT);
221
222    if (secontext) {
223        freecon(secontext);
224        setfscreatecon(NULL);
225    }
226}
227
228static void add_platform_device(const char *path)
229{
230    int path_len = strlen(path);
231    struct listnode *node;
232    struct platform_node *bus;
233    const char *name = path;
234
235    if (!strncmp(path, "/devices/", 9)) {
236        name += 9;
237        if (!strncmp(name, "platform/", 9))
238            name += 9;
239    }
240
241    list_for_each_reverse(node, &platform_names) {
242        bus = node_to_item(node, struct platform_node, list);
243        if ((bus->path_len < path_len) &&
244                (path[bus->path_len] == '/') &&
245                !strncmp(path, bus->path, bus->path_len))
246            /* subdevice of an existing platform, ignore it */
247            return;
248    }
249
250    INFO("adding platform device %s (%s)\n", name, path);
251
252    bus = calloc(1, sizeof(struct platform_node));
253    bus->path = strdup(path);
254    bus->path_len = path_len;
255    bus->name = bus->path + (name - path);
256    list_add_tail(&platform_names, &bus->list);
257}
258
259/*
260 * given a path that may start with a platform device, find the length of the
261 * platform device prefix.  If it doesn't start with a platform device, return
262 * 0.
263 */
264static struct platform_node *find_platform_device(const char *path)
265{
266    int path_len = strlen(path);
267    struct listnode *node;
268    struct platform_node *bus;
269
270    list_for_each_reverse(node, &platform_names) {
271        bus = node_to_item(node, struct platform_node, list);
272        if ((bus->path_len < path_len) &&
273                (path[bus->path_len] == '/') &&
274                !strncmp(path, bus->path, bus->path_len))
275            return bus;
276    }
277
278    return NULL;
279}
280
281static void remove_platform_device(const char *path)
282{
283    struct listnode *node;
284    struct platform_node *bus;
285
286    list_for_each_reverse(node, &platform_names) {
287        bus = node_to_item(node, struct platform_node, list);
288        if (!strcmp(path, bus->path)) {
289            INFO("removing platform device %s\n", bus->name);
290            free(bus->path);
291            list_remove(node);
292            free(bus);
293            return;
294        }
295    }
296}
297
298#if LOG_UEVENTS
299
300static inline suseconds_t get_usecs(void)
301{
302    struct timeval tv;
303    gettimeofday(&tv, 0);
304    return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
305}
306
307#define log_event_print(x...) INFO(x)
308
309#else
310
311#define log_event_print(fmt, args...)   do { } while (0)
312#define get_usecs()                     0
313
314#endif
315
316static void parse_event(const char *msg, struct uevent *uevent)
317{
318    uevent->action = "";
319    uevent->path = "";
320    uevent->subsystem = "";
321    uevent->firmware = "";
322    uevent->major = -1;
323    uevent->minor = -1;
324    uevent->partition_name = NULL;
325    uevent->partition_num = -1;
326    uevent->device_name = NULL;
327
328        /* currently ignoring SEQNUM */
329    while(*msg) {
330        if(!strncmp(msg, "ACTION=", 7)) {
331            msg += 7;
332            uevent->action = msg;
333        } else if(!strncmp(msg, "DEVPATH=", 8)) {
334            msg += 8;
335            uevent->path = msg;
336        } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
337            msg += 10;
338            uevent->subsystem = msg;
339        } else if(!strncmp(msg, "FIRMWARE=", 9)) {
340            msg += 9;
341            uevent->firmware = msg;
342        } else if(!strncmp(msg, "MAJOR=", 6)) {
343            msg += 6;
344            uevent->major = atoi(msg);
345        } else if(!strncmp(msg, "MINOR=", 6)) {
346            msg += 6;
347            uevent->minor = atoi(msg);
348        } else if(!strncmp(msg, "PARTN=", 6)) {
349            msg += 6;
350            uevent->partition_num = atoi(msg);
351        } else if(!strncmp(msg, "PARTNAME=", 9)) {
352            msg += 9;
353            uevent->partition_name = msg;
354        } else if(!strncmp(msg, "DEVNAME=", 8)) {
355            msg += 8;
356            uevent->device_name = msg;
357        }
358
359        /* advance to after the next \0 */
360        while(*msg++)
361            ;
362    }
363
364    log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
365                    uevent->action, uevent->path, uevent->subsystem,
366                    uevent->firmware, uevent->major, uevent->minor);
367}
368
369static char **get_character_device_symlinks(struct uevent *uevent)
370{
371    const char *parent;
372    char *slash;
373    char **links;
374    int link_num = 0;
375    int width;
376    struct platform_node *pdev;
377
378    pdev = find_platform_device(uevent->path);
379    if (!pdev)
380        return NULL;
381
382    links = malloc(sizeof(char *) * 2);
383    if (!links)
384        return NULL;
385    memset(links, 0, sizeof(char *) * 2);
386
387    /* skip "/devices/platform/<driver>" */
388    parent = strchr(uevent->path + pdev->path_len, '/');
389    if (!*parent)
390        goto err;
391
392    if (!strncmp(parent, "/usb", 4)) {
393        /* skip root hub name and device. use device interface */
394        while (*++parent && *parent != '/');
395        if (*parent)
396            while (*++parent && *parent != '/');
397        if (!*parent)
398            goto err;
399        slash = strchr(++parent, '/');
400        if (!slash)
401            goto err;
402        width = slash - parent;
403        if (width <= 0)
404            goto err;
405
406        if (asprintf(&links[link_num], "/dev/usb/%s%.*s", uevent->subsystem, width, parent) > 0)
407            link_num++;
408        else
409            links[link_num] = NULL;
410        mkdir("/dev/usb", 0755);
411    }
412    else {
413        goto err;
414    }
415
416    return links;
417err:
418    free(links);
419    return NULL;
420}
421
422static char **parse_platform_block_device(struct uevent *uevent)
423{
424    const char *device;
425    struct platform_node *pdev;
426    char *slash;
427    int width;
428    char buf[256];
429    char link_path[256];
430    int fd;
431    int link_num = 0;
432    int ret;
433    char *p;
434    unsigned int size;
435    struct stat info;
436
437    pdev = find_platform_device(uevent->path);
438    if (!pdev)
439        return NULL;
440    device = pdev->name;
441
442    char **links = malloc(sizeof(char *) * 4);
443    if (!links)
444        return NULL;
445    memset(links, 0, sizeof(char *) * 4);
446
447    INFO("found platform device %s\n", device);
448
449    snprintf(link_path, sizeof(link_path), "/dev/block/platform/%s", device);
450
451    if (uevent->partition_name) {
452        p = strdup(uevent->partition_name);
453        sanitize(p);
454        if (asprintf(&links[link_num], "%s/by-name/%s", link_path, p) > 0)
455            link_num++;
456        else
457            links[link_num] = NULL;
458        free(p);
459    }
460
461    if (uevent->partition_num >= 0) {
462        if (asprintf(&links[link_num], "%s/by-num/p%d", link_path, uevent->partition_num) > 0)
463            link_num++;
464        else
465            links[link_num] = NULL;
466    }
467
468    slash = strrchr(uevent->path, '/');
469    if (asprintf(&links[link_num], "%s/%s", link_path, slash + 1) > 0)
470        link_num++;
471    else
472        links[link_num] = NULL;
473
474    return links;
475}
476
477static void handle_device(const char *action, const char *devpath,
478        const char *path, int block, int major, int minor, char **links)
479{
480    int i;
481
482    if(!strcmp(action, "add")) {
483        make_device(devpath, path, block, major, minor);
484        if (links) {
485            for (i = 0; links[i]; i++)
486                make_link(devpath, links[i]);
487        }
488    }
489
490    if(!strcmp(action, "remove")) {
491        if (links) {
492            for (i = 0; links[i]; i++)
493                remove_link(devpath, links[i]);
494        }
495        unlink(devpath);
496    }
497
498    if (links) {
499        for (i = 0; links[i]; i++)
500            free(links[i]);
501        free(links);
502    }
503}
504
505static void handle_platform_device_event(struct uevent *uevent)
506{
507    const char *path = uevent->path;
508
509    if (!strcmp(uevent->action, "add"))
510        add_platform_device(path);
511    else if (!strcmp(uevent->action, "remove"))
512        remove_platform_device(path);
513}
514
515static const char *parse_device_name(struct uevent *uevent, unsigned int len)
516{
517    const char *name;
518
519    /* if it's not a /dev device, nothing else to do */
520    if((uevent->major < 0) || (uevent->minor < 0))
521        return NULL;
522
523    /* do we have a name? */
524    name = strrchr(uevent->path, '/');
525    if(!name)
526        return NULL;
527    name++;
528
529    /* too-long names would overrun our buffer */
530    if(strlen(name) > len)
531        return NULL;
532
533    return name;
534}
535
536static void handle_block_device_event(struct uevent *uevent)
537{
538    const char *base = "/dev/block/";
539    const char *name;
540    char devpath[96];
541    char **links = NULL;
542
543    name = parse_device_name(uevent, 64);
544    if (!name)
545        return;
546
547    snprintf(devpath, sizeof(devpath), "%s%s", base, name);
548    make_dir(base, 0755);
549
550    if (!strncmp(uevent->path, "/devices/", 9))
551        links = parse_platform_block_device(uevent);
552
553    handle_device(uevent->action, devpath, uevent->path, 1,
554            uevent->major, uevent->minor, links);
555}
556
557static void handle_generic_device_event(struct uevent *uevent)
558{
559    char *base;
560    const char *name;
561    char devpath[96] = {0};
562    char **links = NULL;
563
564    name = parse_device_name(uevent, 64);
565    if (!name)
566        return;
567
568    if (!strncmp(uevent->subsystem, "usb", 3)) {
569         if (!strcmp(uevent->subsystem, "usb")) {
570            if (uevent->device_name) {
571                /*
572                 * create device node provided by kernel if present
573                 * see drivers/base/core.c
574                 */
575                char *p = devpath;
576                snprintf(devpath, sizeof(devpath), "/dev/%s", uevent->device_name);
577                /* skip leading /dev/ */
578                p += 5;
579                /* build directories */
580                while (*p) {
581                    if (*p == '/') {
582                        *p = 0;
583                        make_dir(devpath, 0755);
584                        *p = '/';
585                    }
586                    p++;
587                }
588             }
589             else {
590                 /* This imitates the file system that would be created
591                  * if we were using devfs instead.
592                  * Minors are broken up into groups of 128, starting at "001"
593                  */
594                 int bus_id = uevent->minor / 128 + 1;
595                 int device_id = uevent->minor % 128 + 1;
596                 /* build directories */
597                 make_dir("/dev/bus", 0755);
598                 make_dir("/dev/bus/usb", 0755);
599                 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
600                 make_dir(devpath, 0755);
601                 snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
602             }
603         } else {
604             /* ignore other USB events */
605             return;
606         }
607     } else if (!strncmp(uevent->subsystem, "graphics", 8)) {
608         base = "/dev/graphics/";
609         make_dir(base, 0755);
610     } else if (!strncmp(uevent->subsystem, "drm", 3)) {
611         base = "/dev/dri/";
612         make_dir(base, 0755);
613     } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
614         base = "/dev/oncrpc/";
615         make_dir(base, 0755);
616     } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
617         base = "/dev/adsp/";
618         make_dir(base, 0755);
619     } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
620         base = "/dev/msm_camera/";
621         make_dir(base, 0755);
622     } else if(!strncmp(uevent->subsystem, "input", 5)) {
623         base = "/dev/input/";
624         make_dir(base, 0755);
625     } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
626         base = "/dev/mtd/";
627         make_dir(base, 0755);
628     } else if(!strncmp(uevent->subsystem, "sound", 5)) {
629         base = "/dev/snd/";
630         make_dir(base, 0755);
631     } else if(!strncmp(uevent->subsystem, "misc", 4) &&
632                 !strncmp(name, "log_", 4)) {
633         base = "/dev/log/";
634         make_dir(base, 0755);
635         name += 4;
636     } else
637         base = "/dev/";
638     links = get_character_device_symlinks(uevent);
639
640     if (!devpath[0])
641         snprintf(devpath, sizeof(devpath), "%s%s", base, name);
642
643     handle_device(uevent->action, devpath, uevent->path, 0,
644             uevent->major, uevent->minor, links);
645}
646
647static void handle_device_event(struct uevent *uevent)
648{
649    if (!strcmp(uevent->action,"add") || !strcmp(uevent->action, "change"))
650        fixup_sys_perms(uevent->path);
651
652    if (!strncmp(uevent->subsystem, "block", 5)) {
653        handle_block_device_event(uevent);
654    } else if (!strncmp(uevent->subsystem, "platform", 8)) {
655        handle_platform_device_event(uevent);
656    } else {
657        handle_generic_device_event(uevent);
658    }
659}
660
661static int load_firmware(int fw_fd, int loading_fd, int data_fd)
662{
663    struct stat st;
664    long len_to_copy;
665    int ret = 0;
666
667    if(fstat(fw_fd, &st) < 0)
668        return -1;
669    len_to_copy = st.st_size;
670
671    write(loading_fd, "1", 1);  /* start transfer */
672
673    while (len_to_copy > 0) {
674        char buf[PAGE_SIZE];
675        ssize_t nr;
676
677        nr = read(fw_fd, buf, sizeof(buf));
678        if(!nr)
679            break;
680        if(nr < 0) {
681            ret = -1;
682            break;
683        }
684
685        len_to_copy -= nr;
686        while (nr > 0) {
687            ssize_t nw = 0;
688
689            nw = write(data_fd, buf + nw, nr);
690            if(nw <= 0) {
691                ret = -1;
692                goto out;
693            }
694            nr -= nw;
695        }
696    }
697
698out:
699    if(!ret)
700        write(loading_fd, "0", 1);  /* successful end of transfer */
701    else
702        write(loading_fd, "-1", 2); /* abort transfer */
703
704    return ret;
705}
706
707static int is_booting(void)
708{
709    return access("/dev/.booting", F_OK) == 0;
710}
711
712static void process_firmware_event(struct uevent *uevent)
713{
714    char *root, *loading, *data, *file1 = NULL, *file2 = NULL, *file3 = NULL;
715    int l, loading_fd, data_fd, fw_fd;
716    int booting = is_booting();
717
718    INFO("firmware: loading '%s' for '%s'\n",
719         uevent->firmware, uevent->path);
720
721    l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
722    if (l == -1)
723        return;
724
725    l = asprintf(&loading, "%sloading", root);
726    if (l == -1)
727        goto root_free_out;
728
729    l = asprintf(&data, "%sdata", root);
730    if (l == -1)
731        goto loading_free_out;
732
733    l = asprintf(&file1, FIRMWARE_DIR1"/%s", uevent->firmware);
734    if (l == -1)
735        goto data_free_out;
736
737    l = asprintf(&file2, FIRMWARE_DIR2"/%s", uevent->firmware);
738    if (l == -1)
739        goto data_free_out;
740
741    l = asprintf(&file3, FIRMWARE_DIR3"/%s", uevent->firmware);
742    if (l == -1)
743        goto data_free_out;
744
745    loading_fd = open(loading, O_WRONLY);
746    if(loading_fd < 0)
747        goto file_free_out;
748
749    data_fd = open(data, O_WRONLY);
750    if(data_fd < 0)
751        goto loading_close_out;
752
753try_loading_again:
754    fw_fd = open(file1, O_RDONLY);
755    if(fw_fd < 0) {
756        fw_fd = open(file2, O_RDONLY);
757        if (fw_fd < 0) {
758            fw_fd = open(file3, O_RDONLY);
759            if (fw_fd < 0) {
760                if (booting) {
761                        /* If we're not fully booted, we may be missing
762                         * filesystems needed for firmware, wait and retry.
763                         */
764                    usleep(100000);
765                    booting = is_booting();
766                    goto try_loading_again;
767                }
768                INFO("firmware: could not open '%s' %d\n", uevent->firmware, errno);
769                write(loading_fd, "-1", 2);
770                goto data_close_out;
771            }
772        }
773    }
774
775    if(!load_firmware(fw_fd, loading_fd, data_fd))
776        INFO("firmware: copy success { '%s', '%s' }\n", root, uevent->firmware);
777    else
778        INFO("firmware: copy failure { '%s', '%s' }\n", root, uevent->firmware);
779
780    close(fw_fd);
781data_close_out:
782    close(data_fd);
783loading_close_out:
784    close(loading_fd);
785file_free_out:
786    free(file1);
787    free(file2);
788data_free_out:
789    free(data);
790loading_free_out:
791    free(loading);
792root_free_out:
793    free(root);
794}
795
796static void handle_firmware_event(struct uevent *uevent)
797{
798    pid_t pid;
799    int ret;
800
801    if(strcmp(uevent->subsystem, "firmware"))
802        return;
803
804    if(strcmp(uevent->action, "add"))
805        return;
806
807    /* we fork, to avoid making large memory allocations in init proper */
808    pid = fork();
809    if (!pid) {
810        process_firmware_event(uevent);
811        exit(EXIT_SUCCESS);
812    }
813}
814
815#define UEVENT_MSG_LEN  1024
816void handle_device_fd()
817{
818    char msg[UEVENT_MSG_LEN+2];
819    int n;
820    while ((n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN)) > 0) {
821        if(n >= UEVENT_MSG_LEN)   /* overflow -- discard */
822            continue;
823
824        msg[n] = '\0';
825        msg[n+1] = '\0';
826
827        struct uevent uevent;
828        parse_event(msg, &uevent);
829
830        handle_device_event(&uevent);
831        handle_firmware_event(&uevent);
832    }
833}
834
835/* Coldboot walks parts of the /sys tree and pokes the uevent files
836** to cause the kernel to regenerate device add events that happened
837** before init's device manager was started
838**
839** We drain any pending events from the netlink socket every time
840** we poke another uevent file to make sure we don't overrun the
841** socket's buffer.
842*/
843
844static void do_coldboot(DIR *d)
845{
846    struct dirent *de;
847    int dfd, fd;
848
849    dfd = dirfd(d);
850
851    fd = openat(dfd, "uevent", O_WRONLY);
852    if(fd >= 0) {
853        write(fd, "add\n", 4);
854        close(fd);
855        handle_device_fd();
856    }
857
858    while((de = readdir(d))) {
859        DIR *d2;
860
861        if(de->d_type != DT_DIR || de->d_name[0] == '.')
862            continue;
863
864        fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
865        if(fd < 0)
866            continue;
867
868        d2 = fdopendir(fd);
869        if(d2 == 0)
870            close(fd);
871        else {
872            do_coldboot(d2);
873            closedir(d2);
874        }
875    }
876}
877
878static void coldboot(const char *path)
879{
880    DIR *d = opendir(path);
881    if(d) {
882        do_coldboot(d);
883        closedir(d);
884    }
885}
886
887void device_init(void)
888{
889    suseconds_t t0, t1;
890    struct stat info;
891    int fd;
892
893    sehandle = NULL;
894    if (is_selinux_enabled() > 0) {
895        sehandle = selinux_android_file_context_handle();
896    }
897
898    /* is 256K enough? udev uses 16MB! */
899    device_fd = uevent_open_socket(256*1024, true);
900    if(device_fd < 0)
901        return;
902
903    fcntl(device_fd, F_SETFD, FD_CLOEXEC);
904    fcntl(device_fd, F_SETFL, O_NONBLOCK);
905
906    if (stat(coldboot_done, &info) < 0) {
907        t0 = get_usecs();
908        coldboot("/sys/class");
909        coldboot("/sys/block");
910        coldboot("/sys/devices");
911        t1 = get_usecs();
912        fd = open(coldboot_done, O_WRONLY|O_CREAT, 0000);
913        close(fd);
914        log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
915    } else {
916        log_event_print("skipping coldboot, already done\n");
917    }
918}
919
920int get_device_fd()
921{
922    return device_fd;
923}
924