libudev-monitor.c revision e6ac88ddf213c431c678cfdac689577abbc14b28
/***
This file is part of systemd.
Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
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.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <poll.h>
#include "libudev.h"
#include "libudev-private.h"
#include "socket-util.h"
#include "missing.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;
}
/* we consider udev running when /dev is on devtmpfs */
union file_handle_union h = FILE_HANDLE_INIT;
int mount_id;
int r;
if (r < 0) {
if (errno != EOPNOTSUPP)
return false;
}
if (!f)
return false;
FOREACH_LINE(line, f, return false) {
int mid;
continue;
continue;
if (!e)
continue;
/* accept any name that starts with the currently expected type */
return true;
}
return false;
}
{
struct udev_monitor *udev_monitor;
unsigned int group;
return NULL;
/*
* We do not support subscribing to uevents if no instance of
* udev is running. Uevents would otherwise broadcast the
* processing data of the host into containers, which is not
* desired.
*
* Containers will currently not get any udev uevents, until
* a supporting infrastructure is available.
*
* We do not set a netlink multicast group here, so the socket
* will not receive any messages.
*/
log_debug("the udev service seems not to be active, disable the monitor");
} else
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 -E2BIG;
}
/* nothing matched, drop packet */
}
/* matched, pass packet */
/* install filter */
}
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;
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 -errno;
}
/* enable receiving of sender credentials */
if (err < 0)
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 -EINVAL;
}
{
int 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.
*
* Returns: #NULL
**/
{
if (udev_monitor == NULL)
return NULL;
udev_monitor->refcount--;
if (udev_monitor->refcount > 0)
return NULL;
if (udev_monitor->sock >= 0)
return NULL;
}
/**
* 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 -EINVAL;
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 monitor socket is by default set to NONBLOCK. A variant of poll() on
* the file descriptor returned by udev_monitor_get_fd() should to be used to
* wake up when new devices arrive, or alternatively the file descriptor
* switched into blocking mode.
*
* 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;
union {
struct udev_monitor_netlink_header nlh;
char raw[8192];
} buf;
bool is_initialized = false;
if (udev_monitor == NULL)
return NULL;
if (buflen < 0) {
log_debug("unable to receive message");
return NULL;
}
log_debug("invalid message length");
return NULL;
}
/* unicast message, check if we trust the sender */
log_debug("unicast netlink message ignored");
return NULL;
}
return NULL;
}
}
log_debug("no sender credentials received, message ignored");
return NULL;
}
return NULL;
}
/* udev message needs proper version magic */
log_debug("unrecognized message signature (%x != %x)",
return NULL;
}
log_debug("message smaller than expected (%u > %zd)",
return NULL;
}
/* devices received from udev are always initialized */
is_initialized = true;
} else {
/* kernel message with header */
log_debug("invalid message length");
return NULL;
}
/* check message header */
log_debug("unrecognized message header");
return NULL;
}
}
if (!udev_device) {
log_debug("could not create device: %m");
return NULL;
}
if (is_initialized)
/* 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;
}
{
struct udev_monitor_netlink_header nlh = {
.prefix = "libudev",
.header_size = sizeof nlh,
};
};
.msg_iovlen = 2,
};
struct udev_list_entry *list_entry;
if (blen < 32) {
log_debug("device buffer is too small to contain a valid device");
return -EINVAL;
}
/* fill in 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)
else
if (count < 0) {
return 0;
} else
return -errno;
}
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.
*/
{
}