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

Commit 2d55e02d authored by Dima Zavin's avatar Dima Zavin
Browse files

libcutils/init: move uevent socket opening code to libcutils



Change-Id: I90adf78c0eb6185505f2bf7b62e96e25ab918345
Signed-off-by: default avatarDima Zavin <dima@android.com>
parent 09e32886
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -17,12 +17,14 @@
#ifndef __CUTILS_UEVENT_H
#define __CUTILS_UEVENT_H

#include <stdbool.h>
#include <sys/socket.h>

#ifdef __cplusplus
extern "C" {
#endif

int uevent_open_socket(int buf_sz, bool passcred);
ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length);

#ifdef __cplusplus
+2 −28
Original line number Diff line number Diff line
@@ -58,33 +58,6 @@ struct uevent {
    int minor;
};

static int open_uevent_socket(void)
{
    struct sockaddr_nl addr;
    int sz = 64*1024; // XXX larger? udev uses 16MB!
    int on = 1;
    int s;

    memset(&addr, 0, sizeof(addr));
    addr.nl_family = AF_NETLINK;
    addr.nl_pid = getpid();
    addr.nl_groups = 0xffffffff;

    s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
    if(s < 0)
        return -1;

    setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
    setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));

    if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        close(s);
        return -1;
    }

    return s;
}

struct perms_ {
    char *name;
    char *attr;
@@ -847,7 +820,8 @@ void device_init(void)
    struct stat info;
    int fd;

    device_fd = open_uevent_socket();
    /* is 64K enough? udev uses 16MB! */
    device_fd = uevent_open_socket(64*1024, true);
    if(device_fd < 0)
        return;

+31 −0
Original line number Diff line number Diff line
@@ -17,7 +17,12 @@
#include <cutils/uevent.h>

#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

#include <linux/netlink.h>

@@ -68,3 +73,29 @@ out:
    errno = EIO;
    return -1;
}

int uevent_open_socket(int buf_sz, bool passcred)
{
    struct sockaddr_nl addr;
    int on = passcred;
    int s;

    memset(&addr, 0, sizeof(addr));
    addr.nl_family = AF_NETLINK;
    addr.nl_pid = getpid();
    addr.nl_groups = 0xffffffff;

    s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
    if(s < 0)
        return -1;

    setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &buf_sz, sizeof(buf_sz));
    setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));

    if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
        close(s);
        return -1;
    }

    return s;
}