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