Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 320cbfa7 authored by Colin Cross's avatar Colin Cross Committed by Android (Google) Code Review
Browse files

Merge "ueventd: refactor uevent handling"

parents 7b43eb12 eb5ba830
Loading
Loading
Loading
Loading
+126 −89
Original line number Diff line number Diff line
@@ -393,42 +393,88 @@ err:
    return NULL;
}

static void handle_device_event(struct uevent *uevent)
static void handle_device(const char *action, const char *devpath,
        const char *path, int block, int major, int minor, char **links)
{
    char devpath[96];
    int devpath_ready = 0;
    char *base, *name;
    char **links = NULL;
    int block;
    int i;

    if (!strcmp(uevent->action,"add"))
        fixup_sys_perms(uevent->path);
    if(!strcmp(action, "add")) {
        make_device(devpath, path, block, major, minor);
        if (links) {
            for (i = 0; links[i]; i++)
                make_link(devpath, links[i]);
        }
    }

    if(!strcmp(action, "remove")) {
        if (links) {
            for (i = 0; links[i]; i++)
                remove_link(devpath, links[i]);
        }
        unlink(devpath);
    }

    if (links) {
        for (i = 0; links[i]; i++)
            free(links[i]);
        free(links);
    }
}

static const char *parse_device_name(struct uevent *uevent, unsigned int len)
{
    const char *name;

    /* if it's not a /dev device, nothing else to do */
    if((uevent->major < 0) || (uevent->minor < 0))
        return;
        return NULL;

    /* do we have a name? */
    name = strrchr(uevent->path, '/');
    if(!name)
        return;
        return NULL;
    name++;

    /* too-long names would overrun our buffer */
    if(strlen(name) > 64)
    if(strlen(name) > len)
        return NULL;

    return name;
}

static void handle_block_device_event(struct uevent *uevent)
{
    const char *base = "/dev/block/";
    const char *name;
    char devpath[96];
    char **links = NULL;

    name = parse_device_name(uevent, 64);
    if (!name)
        return;

        /* are we block or char? where should we live? */
    if(!strncmp(uevent->subsystem, "block", 5)) {
        block = 1;
        base = "/dev/block/";
    snprintf(devpath, sizeof(devpath), "%s%s", base, name);
    mkdir(base, 0755);

    if (!strncmp(uevent->path, "/devices/platform/", 18))
        links = parse_platform_block_device(uevent);
    } else {
        block = 0;
            /* this should probably be configurable somehow */

    handle_device(uevent->action, devpath, uevent->path, 1,
            uevent->major, uevent->minor, links);

}

static void handle_generic_device_event(struct uevent *uevent)
{
    char *base;
    const char *name;
    char devpath[96] = {0};
    char **links = NULL;

    name = parse_device_name(uevent, 64);
    if (!name)
        return;

    if (!strncmp(uevent->subsystem, "usb", 3)) {
         if (!strcmp(uevent->subsystem, "usb")) {
             /* This imitates the file system that would be created
@@ -443,7 +489,6 @@ static void handle_device_event(struct uevent *uevent)
             snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d", bus_id);
             mkdir(devpath, 0755);
             snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", bus_id, device_id);
                devpath_ready = 1;
         } else {
             /* ignore other USB events */
             return;
@@ -477,31 +522,23 @@ static void handle_device_event(struct uevent *uevent)
     } else
         base = "/dev/";
     links = get_character_device_symlinks(uevent);
    }

    if (!devpath_ready)
     if (!devpath[0])
         snprintf(devpath, sizeof(devpath), "%s%s", base, name);

    if(!strcmp(uevent->action, "add")) {
        make_device(devpath, uevent->path, block, uevent->major, uevent->minor);
        if (links) {
            for (i = 0; links[i]; i++)
                make_link(devpath, links[i]);
        }
     handle_device(uevent->action, devpath, uevent->path, 0,
             uevent->major, uevent->minor, links);
}

    if(!strcmp(uevent->action, "remove")) {
        if (links) {
            for (i = 0; links[i]; i++)
                remove_link(devpath, links[i]);
        }
        unlink(devpath);
    }
static void handle_device_event(struct uevent *uevent)
{
    if (!strcmp(uevent->action,"add"))
        fixup_sys_perms(uevent->path);

    if (links) {
        for (i = 0; links[i]; i++)
            free(links[i]);
        free(links);
    if (!strncmp(uevent->subsystem, "block", 5)) {
        handle_block_device_event(uevent);
    } else {
        handle_generic_device_event(uevent);
    }
}