Brief analysis on the user space interception method and implementation of the Uevent event of U-disk and other hot-swappable

Source: Internet
Author: User

#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/n", device_fd);

do {
while ((n = recv (DEVICE_FD, MSG, Uevent_msg_len, 0)) > 0) {
struct Luther_gliethttp luther_gliethttp;

if (n = = uevent_msg_len)/* Overflow--Discard */
Continue

Msg[n] = '/0 ';
MSG[N+1] = '/0 ';

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;

/* Currently ignoring SEQNUM */
printf ("========================================================/n");
while (*msg) {

printf ("%s/n", 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);
}

/* advance to after the next/0 */
while (*msg++)
;
}

printf ("Event {'%s ', '%s ', '%s ', '%s ',%d,%d}/n",
Luther_gliethttp->action, Luther_gliethttp->path,luther_gliethttp->subsystem,
Luther_gliethttp->firmware, Luther_gliethttp->major,luther_gliethttp->minor);
}

command, you can use Udevmonitor.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.