• 内核发送uevent的API,用户空间解析uevent(转)


    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/socket.h>
    #include <linux/netlink.h>

    #define UEVENT_MSG_LEN 4096
    struct luther_gliethttp {
    const char *action;
    const char *path;
    const char *subsystem;
    const char *firmware;
    int major;
    int minor;
    };
    static int open_luther_gliethttp_socket(void);
    static void parse_event(const char *msg, struct luther_gliethttp *luther_gliethttp);

    int main(int argc, char* argv[])
    {
    int device_fd = -1;
    char msg[UEVENT_MSG_LEN+2];
    int n;

    device_fd = open_luther_gliethttp_socket();
    printf("device_fd = %d ", device_fd);

    do {
    while((n = recv(device_fd, msg, UEVENT_MSG_LEN, 0)) > 0) {
    struct luther_gliethttp luther_gliethttp;

    if(n == UEVENT_MSG_LEN)
    continue;

    msg[n] = '';
    msg[n+1] = '';

    parse_event(msg, &luther_gliethttp);
    }
    } while(1);
    }

    static int open_luther_gliethttp_socket(void)
    {
    struct sockaddr_nl addr;
    int sz = 64*1024;
    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));

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

    return s;
    }

    static void parse_event(const char *msg, struct luther_gliethttp *luther_gliethttp)
    {
    luther_gliethttp->action = "";
    luther_gliethttp->path = "";
    luther_gliethttp->subsystem = "";
    luther_gliethttp->firmware = "";
    luther_gliethttp->major = -1;
    luther_gliethttp->minor = -1;


    printf("======================================================== ");
    while (*msg) {

    printf("%s ", msg);

    if (!strncmp(msg, "ACTION=", 7)) {
    msg += 7;
    luther_gliethttp->action = msg;
    } else if (!strncmp(msg, "DEVPATH=", 8)) {
    msg += 8;
    luther_gliethttp->path = msg;
    } else if (!strncmp(msg, "SUBSYSTEM=", 10)) {
    msg += 10;
    luther_gliethttp->subsystem = msg;
    } else if (!strncmp(msg, "FIRMWARE=", 9)) {
    msg += 9;
    luther_gliethttp->firmware = msg;
    } else if (!strncmp(msg, "MAJOR=", 6)) {
    msg += 6;
    luther_gliethttp->major = atoi(msg);
    } else if (!strncmp(msg, "MINOR=", 6)) {
    msg += 6;
    luther_gliethttp->minor = atoi(msg);
    }


    while(*msg++)
    ;
    }

    printf("event { '%s', '%s', '%s', '%s', %d, %d } ",
    luther_gliethttp->action, luther_gliethttp->path, luther_gliethttp->subsystem,
    luther_gliethttp->firmware, luther_gliethttp->major, luther_gliethttp->minor);
    }
    ————————————————
    版权声明:本文为CSDN博主「luckywang1103」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/luckywang1103/article/details/50529534

  • 相关阅读:
    jquery Table基础操作
    window.opener
    CSS基础
    CSS样式
    CSS框架
    常用正则表达式
    HTML字体对应word字体
    SQL获取所有数据库名、表名、储存过程以及参数列表
    SQL集合运算:差集、交集、并集
    sql数据分页
  • 原文地址:https://www.cnblogs.com/erhu-67786482/p/11792642.html
Copyright © 2020-2023  润新知