libudev-monitor.c revision 1e03b754aef576a5cb75f01b1805cdc1f9cc292f
/*
* libudev - interface to udev device information
*
* Copyright (C) 2008-2009 Kay Sievers <kay.sievers@vrfy.org>
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include "libudev.h"
#include "libudev-private.h"
struct udev_monitor {
int refcount;
int sock;
struct sockaddr_nl snl;
struct sockaddr_nl snl_trusted_sender;
struct sockaddr_nl snl_destination;
struct sockaddr_un sun;
struct udev_list_node filter_subsystem_list;
};
enum udev_monitor_netlink_group {
};
#define UDEV_MONITOR_MAGIC 0xcafe1dea
struct udev_monitor_netlink_header {
/* udev version text */
char version[16];
/*
* magic to protect against daemon <-> library message format mismatch
* used in the kernel from socket filter rules; needs to be stored in network order
*/
unsigned int magic;
/* properties buffer */
unsigned short properties_off;
unsigned short properties_len;
/*
* hashes of some common device properties strings to filter with socket filters in
* the client used in the kernel from socket filter rules; needs to be stored in
* network order
*/
unsigned int filter_subsystem;
unsigned int filter_devtype;
};
{
struct udev_monitor *udev_monitor;
if (udev_monitor == NULL)
return NULL;
return udev_monitor;
}
/**
* udev_monitor_new_from_socket:
* @udev: udev library context
* @socket_path: unix socket path
*
* Create new udev monitor and connect to a specified socket. The
* path to a socket either points to an existing socket file, or if
* the socket path starts with a '@' character, an abstract namespace
* socket will be used.
*
* A socket file will not be created. If it does not already exist,
* it will fall-back and connect to an abstract namespace socket with
* the given path. The permissions adjustment of a socket file, as
* well as the later cleanup, needs to be done by the caller.
*
* The initial refcount is 1, and needs to be decremented to
* release the resources of the udev monitor.
*
* Returns: a new udev monitor, or #NULL, in case of an error
**/
{
struct udev_monitor *udev_monitor;
return NULL;
if (socket_path == NULL)
return NULL;
if (udev_monitor == NULL)
return NULL;
if (socket_path[0] == '@') {
/* translate leading '@' to abstract namespace */
/* existing socket file */
} else {
/* no socket file, assume abstract namespace socket */
}
return NULL;
}
return udev_monitor;
}
/**
* udev_monitor_new_from_netlink:
* @udev: udev library context
* @name: name of event source
*
* Create new udev monitor and connect to a specified event
* source. Valid sources identifiers are "udev" and "kernel".
*
* Applications should usually not connect directly to the
* "kernel" events, because the devices might not be useable
* at that time, before udev has configured them, and created
* device nodes.
*
* Accessing devices at the same time as udev, might result
* in unpredictable behavior.
*
* The "udev" events are sent out after udev has finished its
* event processing, all rules have been processed, and needed
* device nodes are created.
*
* The initial refcount is 1, and needs to be decremented to
* release the resources of the udev monitor.
*
* Returns: a new udev monitor, or #NULL, in case of an error
**/
{
struct udev_monitor *udev_monitor;
unsigned int group;
return NULL;
else
return NULL;
if (udev_monitor == NULL)
return NULL;
return NULL;
}
/* default destination for sending */
return udev_monitor;
}
{
(*i)++;
}
{
(*i)++;
}
{
static struct sock_fprog filter;
unsigned int i;
struct udev_list_entry *list_entry;
int err;
return 0;
i = 0;
/* load magic in A */
/* jump if magic matches */
/* wrong magic, pass packet */
/* add all subsystem match values */
unsigned int hash;
/* load filter_subsystem value in A */
bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem));
/* jump if subsystem does not match */
} else {
/* jump if subsystem does not match */
/* load filter_devtype value in A */
bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype));
/* jump if value does not match */
}
/* matched, pass packet */
return -1;
}
/* nothing matched, drop packet */
/* install filter */
return err;
}
int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
{
return 0;
}
{
int err;
const int on = 1;
if (err == 0) {
struct sockaddr_nl snl;
/*
* get the address the kernel has assigned us
* it is usually, but not neccessarily the pid
*/
addrlen = sizeof(struct sockaddr_nl);
if (err == 0)
}
} else {
return -EINVAL;
}
if (err < 0) {
return err;
}
/* enable receiving of sender credentials */
return 0;
}
{
if (udev_monitor == NULL)
return -1;
}
{
int err;
return err;
}
/**
* udev_monitor_ref:
* @udev_monitor: udev monitor
*
* Take a reference of a udev monitor.
*
* Returns: the passed udev monitor
**/
{
if (udev_monitor == NULL)
return NULL;
udev_monitor->refcount++;
return udev_monitor;
}
/**
* udev_monitor_unref:
* @udev_monitor: udev monitor
*
* Drop a reference of a udev monitor. If the refcount reaches zero,
* the bound socket will be closed, and the resources of the monitor
* will be released.
*
**/
{
if (udev_monitor == NULL)
return;
udev_monitor->refcount--;
if (udev_monitor->refcount > 0)
return;
if (udev_monitor->sock >= 0)
}
/**
* udev_monitor_get_udev:
* @udev_monitor: udev monitor
*
* Retrieve the udev library context the monitor was created with.
*
* Returns: the udev library context
**/
{
if (udev_monitor == NULL)
return NULL;
return udev_monitor->udev;
}
/**
* udev_monitor_get_fd:
* @udev_monitor: udev monitor
*
* Retrieve the socket file descriptor associated with the monitor.
*
* Returns: the socket file descriptor
**/
{
if (udev_monitor == NULL)
return -1;
return udev_monitor->sock;
}
{
struct udev_list_entry *list_entry;
return 1;
const char *devtype;
const char *ddevtype;
continue;
return 1;
continue;
return 1;
}
return 0;
}
/**
* udev_monitor_receive_device:
* @udev_monitor: udev monitor
*
* Receive data from the udev monitor socket, allocate a new udev
* device, fill in the received data, and return the device.
*
* Only socket connections with uid=0 are accepted. The caller
* needs to make sure that there is data to read from the socket.
* The call will block until the socket becomes readable.
*
* The initial refcount is 1, and needs to be decremented to
* release the resources of the udev device.
*
* Returns: a new udev device, or #NULL, in case of an error
**/
{
struct udev_device *udev_device;
struct sockaddr_nl snl;
char buf[8192];
struct udev_monitor_netlink_header *nlh;
int devpath_set = 0;
int subsystem_set = 0;
int action_set = 0;
int maj = 0;
int min = 0;
int is_kernel = 0;
if (udev_monitor == NULL)
return NULL;
}
if (buflen < 0) {
return NULL;
}
return NULL;
}
/* unicast message, check if we trust the sender */
return NULL;
}
return NULL;
}
is_kernel = 1;
}
}
return NULL;
}
return NULL;
}
/* udev message needs proper version magic */
return NULL;
return NULL;
return NULL;
} else {
/* kernel message with header */
return NULL;
}
/* check message header */
return NULL;
}
}
if (udev_device == NULL) {
return NULL;
}
char *key;
if (keylen == 0)
break;
char path[UTIL_PATH_SIZE];
devpath_set = 1;
subsystem_set = 1;
if (is_kernel)
else
char devlinks[UTIL_PATH_SIZE];
char *slink;
char *next;
next[0] = '\0';
}
if (slink[0] != '\0')
action_set = 1;
} else {
}
}
return NULL;
}
/* skip device, if it does not pass the current filter */
int rc;
/* if something is queued, get next device */
if (rc > 0)
goto retry;
return NULL;
}
if (maj > 0)
return udev_device;
}
{
const char *buf;
if (blen < 32)
return -1;
const char *action;
char header[2048];
char *s;
/* header <action>@<devpath> */
return -EINVAL;
s = header;
if (util_strpcpyl(&s, sizeof(header), action, "@", udev_device_get_devpath(udev_device), NULL) == 0)
return -EINVAL;
/* add properties list */
const char *val;
struct udev_monitor_netlink_header nlh;
/* add versioned header */
/* add properties list */
/*
* Use custom address for target, or the default one.
*
* If we send to a muticast group, we will get
* ECONNREFUSED, which is expected.
*/
if (destination != NULL)
else
} else {
return -1;
}
return count;
}
int udev_monitor_filter_add_match_subsystem_devtype(struct udev_monitor *udev_monitor, const char *subsystem, const char *devtype)
{
if (udev_monitor == NULL)
return -EINVAL;
return 0;
return -ENOMEM;
return 0;
}
{
}