libudev-monitor.c revision c8f8394a9309d4390daac70b736b34d0b6734f95
/*
* libudev - interface to udev device information
*
* Copyright (C) 2008-2010 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"
#include "socket-util.h"
/**
* SECTION:libudev-monitor
* @short_description: device event source
*
* Connects to a device event source.
*/
/**
* udev_monitor:
*
* Opaque object handling an event source.
*/
struct udev_monitor {
int refcount;
int sock;
union sockaddr_union snl;
union sockaddr_union snl_trusted_sender;
union sockaddr_union snl_destination;
struct udev_list filter_subsystem_list;
struct udev_list filter_tag_list;
bool bound;
};
enum udev_monitor_netlink_group {
};
#define UDEV_MONITOR_MAGIC 0xfeedcafe
struct udev_monitor_netlink_header {
/* "libudev" prefix to distinguish libudev and kernel messages */
char prefix[8];
/*
* 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;
/* total length of header structure known to the sender */
unsigned int header_size;
/* properties string buffer */
unsigned int properties_off;
unsigned int properties_len;
/*
* hashes of primary device properties strings, to let libudev subscribers
* use in-kernel socket filters; values need to be stored in network order
*/
unsigned int filter_subsystem_hash;
unsigned int filter_devtype_hash;
unsigned int filter_tag_bloom_hi;
unsigned int filter_tag_bloom_lo;
};
{
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
*
* This function is removed from libudev and will not do anything.
*
* Returns: #NULL
**/
_public_ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
{
return NULL;
}
{
struct udev_monitor *udev_monitor;
unsigned int group;
return NULL;
else
return NULL;
if (udev_monitor == NULL)
return NULL;
if (fd < 0) {
udev_monitor->sock = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
return NULL;
}
} else {
udev_monitor->bound = true;
}
/* default destination for sending */
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
**/
{
}
{
(*i)++;
}
{
(*i)++;
}
/**
* udev_monitor_filter_update:
* @udev_monitor: monitor
*
* Update the installed socket filter. This is only needed,
* if the filter was removed or changed.
*
* Returns: 0 on success, otherwise a negative error value.
*/
{
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 */
int tag_matches;
/* count tag matches, to calculate end of tag match block */
tag_matches = 0;
tag_matches++;
/* add all tags matches */
/* load device bloom bits in A */
bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_hi));
/* clear bits (tag bits & bloom bits) */
/* jump to next tag if it does not match */
/* load device bloom bits in A */
bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_tag_bloom_lo));
/* clear bits (tag bits & bloom bits) */
/* jump behind end of tag match block if tag matches */
tag_matches--;
}
/* nothing matched, drop packet */
}
/* add all subsystem matches */
/* load device subsystem value in A */
bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_subsystem_hash));
/* jump if subsystem does not match */
} else {
/* jump if subsystem does not match */
/* load device devtype value in A */
bpf_stmt(ins, &i, BPF_LD|BPF_W|BPF_ABS, offsetof(struct udev_monitor_netlink_header, filter_devtype_hash));
/* jump if value does not match */
}
/* matched, pass packet */
return -1;
}
/* nothing matched, drop packet */
}
/* matched, pass packet */
/* install filter */
return err;
}
int udev_monitor_allow_unicast_sender(struct udev_monitor *udev_monitor, struct udev_monitor *sender)
{
return 0;
}
/**
* udev_monitor_enable_receiving:
* @udev_monitor: the monitor which should receive events
*
* Binds the @udev_monitor socket to the event source.
*
* Returns: 0 on success, otherwise a negative error value.
*/
{
int err = 0;
const int on = 1;
return -EINVAL;
if (!udev_monitor->bound) {
if (err == 0)
udev_monitor->bound = true;
}
if (err >= 0) {
union sockaddr_union snl;
/*
* get the address the kernel has assigned us
* it is usually, but not necessarily the pid
*/
addrlen = sizeof(struct sockaddr_nl);
if (err == 0)
} else {
return err;
}
/* enable receiving of sender credentials */
return 0;
}
/**
* udev_monitor_set_receive_buffer_size:
* @udev_monitor: the monitor which should receive events
* @size: the size in bytes
*
* Set the size of the kernel socket buffer. This call needs the
* appropriate privileges to succeed.
*
* Returns: 0 on success, otherwise -1 on error.
*/
{
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;
goto tag;
const char *devtype;
const char *ddevtype;
continue;
goto tag;
continue;
goto tag;
}
return 0;
tag:
return 1;
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 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;
union sockaddr_union snl;
char buf[8192];
struct udev_monitor_netlink_header *nlh;
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;
}
}
}
return NULL;
}
return NULL;
}
/* udev message needs proper version magic */
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;
}
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;
}
return udev_device;
}
{
const char *buf;
const char *val;
struct udev_monitor_netlink_header nlh;
struct udev_list_entry *list_entry;
return -EINVAL;
if (blen < 32)
return -EINVAL;
/* add versioned header */
/* add tag bloom filter */
tag_bloom_bits = 0;
if (tag_bloom_bits > 0) {
}
/* add properties list */
/*
* Use custom address for target, or the default one.
*
* If we send to a multicast group, we will get
* ECONNREFUSED, which is expected.
*/
if (destination != NULL)
else
return count;
}
/**
* udev_monitor_filter_add_match_subsystem_devtype:
* @udev_monitor: the monitor
* @subsystem: the subsystem value to match the incoming devices against
* @devtype: the devtype value to match the incoming devices against
*
* This filter is efficiently executed inside the kernel, and libudev subscribers
* will usually not be woken up for devices which do not match.
*
* The filter must be installed before the monitor is switched to listening mode.
*
* Returns: 0 on success, otherwise a negative error value.
*/
_public_ 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 -EINVAL;
return -ENOMEM;
return 0;
}
/**
* udev_monitor_filter_add_match_tag:
* @udev_monitor: the monitor
* @tag: the name of a tag
*
* This filter is efficiently executed inside the kernel, and libudev subscribers
* will usually not be woken up for devices which do not match.
*
* The filter must be installed before the monitor is switched to listening mode.
*
* Returns: 0 on success, otherwise a negative error value.
*/
{
if (udev_monitor == NULL)
return -EINVAL;
return -EINVAL;
return -ENOMEM;
return 0;
}
/**
* udev_monitor_filter_remove:
* @udev_monitor: monitor
*
* Remove all filters from monitor.
*
* Returns: 0 on success, otherwise a negative error value.
*/
{
}