libudev-enumerate.c revision c195956988799837b763ab1b9f078e5f0b7f26e6
/*
* 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 <fnmatch.h>
#include <stdbool.h>
#include "libudev.h"
#include "libudev-private.h"
/**
* SECTION:libudev-enumerate
* @short_description: lookup and sort sys devices
*
* Lookup devices in the sys filesystem, filter devices by properties,
* and return a sorted list of devices.
*/
struct syspath {
char *syspath;
};
/**
* udev_enumerate:
*
*/
struct udev_enumerate {
int refcount;
struct udev_list sysattr_match_list;
struct udev_list sysattr_nomatch_list;
struct udev_list subsystem_match_list;
struct udev_list subsystem_nomatch_list;
struct udev_list sysname_match_list;
struct udev_list properties_match_list;
struct udev_list tags_match_list;
struct udev_device *parent_match;
struct udev_list devices_list;
unsigned int devices_cur;
unsigned int devices_max;
bool devices_uptodate:1;
bool match_is_initialized;
};
/**
* udev_enumerate_new:
* @udev: udev library context
*
* Create an enumeration context to scan /sys.
*
* Returns: an enumeration context.
**/
{
struct udev_enumerate *udev_enumerate;
if (udev_enumerate == NULL)
return NULL;
return udev_enumerate;
}
/**
* udev_enumerate_ref:
* @udev_enumerate: context
*
* Take a reference of a enumeration context.
*
* Returns: the passed enumeration context
**/
{
if (udev_enumerate == NULL)
return NULL;
return udev_enumerate;
}
/**
* udev_enumerate_unref:
* @udev_enumerate: context
*
* Drop a reference of an enumeration context. If the refcount reaches zero,
* all resources of the enumeration context will be released.
*
* Returns: the passed enumeration context if it has still an active reference, or #NULL otherwise.
**/
{
unsigned int i;
if (udev_enumerate == NULL)
return NULL;
if (udev_enumerate->refcount > 0)
return udev_enumerate;
for (i = 0; i < udev_enumerate->devices_cur; i++)
return NULL;
}
/**
* udev_enumerate_get_udev:
* @udev_enumerate: context
*
* Get the udev library context.
*
* Returns: a pointer to the context.
*/
{
if (udev_enumerate == NULL)
return NULL;
return udev_enumerate->udev;
}
{
char *path;
/* double array size if needed */
unsigned int add;
if (add < 1024)
add = 1024;
buf = realloc(udev_enumerate->devices, (udev_enumerate->devices_max + add) * sizeof(struct syspath));
return -ENOMEM;
}
return -ENOMEM;
udev_enumerate->devices_uptodate = false;
return 0;
}
{
int ret;
if (ret == 0) {
ret = -1;
ret = 1;
}
return ret;
}
/* For devices that should be moved to the absolute end of the list */
{
static const char *delay_device_list[] = {
};
int i;
for (i = 0; delay_device_list[i] != NULL; i++) {
return true;
}
return false;
}
/* For devices that should just be moved a little bit later, just
* before the point where some common path prefix changes. Returns the
* number of characters that make up that common prefix */
{
const char *c;
/* For sound cards the control device must be enumerated last
* to make sure it's the final device node that gets ACLs
* applied. Applications rely on this fact and use ACL changes
* on the control node as an indicator that the ACL change of
* the entire sound card completed. The kernel makes this
* guarantee when creating those devices, and hence we should
* too when enumerating them. */
c += 11;
c += strcspn(c, "/");
if (startswith(c, "/controlC"))
return c - syspath + 1;
}
return 0;
}
/**
* udev_enumerate_get_list_entry:
* @udev_enumerate: context
*
* Get the first entry of the sorted list of device paths.
*
* Returns: a udev_list_entry.
*/
_public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate)
{
if (udev_enumerate == NULL)
return NULL;
if (!udev_enumerate->devices_uptodate) {
unsigned int i;
unsigned int max;
size_t move_later_prefix = 0;
for (i = 0; i < max; i++) {
/* skip duplicated entries */
continue;
/* skip to be delayed devices, and add them to the end of the list */
/* need to update prev here for the case realloc() gives a different address */
continue;
}
/* skip to be delayed devices, and move the to
* the point where the prefix changes. We can
* only move one item at a time. */
if (!move_later) {
if (move_later_prefix > 0) {
move_later = entry;
continue;
}
}
if (move_later &&
move_later = NULL;
}
}
if (move_later)
/* add and cleanup delayed devices from end of list */
}
udev_enumerate->devices_uptodate = true;
}
}
/**
* udev_enumerate_add_match_subsystem:
* @udev_enumerate: context
* @subsystem: filter for a subsystem of the device to include in the list
*
* Match only devices belonging to a certain kernel subsystem.
*
* Returns: 0 on success, otherwise a negative error value.
*/
_public_ int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
{
if (udev_enumerate == NULL)
return -EINVAL;
return 0;
return -ENOMEM;
return 0;
}
/**
* udev_enumerate_add_nomatch_subsystem:
* @udev_enumerate: context
* @subsystem: filter for a subsystem of the device to exclude from the list
*
* Match only devices not belonging to a certain kernel subsystem.
*
* Returns: 0 on success, otherwise a negative error value.
*/
_public_ int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
{
if (udev_enumerate == NULL)
return -EINVAL;
return 0;
return -ENOMEM;
return 0;
}
/**
* udev_enumerate_add_match_sysattr:
* @udev_enumerate: context
* @sysattr: filter for a sys attribute at the device to include in the list
* @value: optional value of the sys attribute
*
* Match only devices with a certain /sys device attribute.
*
* Returns: 0 on success, otherwise a negative error value.
*/
_public_ int udev_enumerate_add_match_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
{
if (udev_enumerate == NULL)
return -EINVAL;
return 0;
return -ENOMEM;
return 0;
}
/**
* udev_enumerate_add_nomatch_sysattr:
* @udev_enumerate: context
* @sysattr: filter for a sys attribute at the device to exclude from the list
* @value: optional value of the sys attribute
*
* Match only devices not having a certain /sys device attribute.
*
* Returns: 0 on success, otherwise a negative error value.
*/
_public_ int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
{
if (udev_enumerate == NULL)
return -EINVAL;
return 0;
return -ENOMEM;
return 0;
}
{
bool match = false;
goto exit;
match = true;
goto exit;
}
match = true;
goto exit;
}
exit:
return match;
}
/**
* udev_enumerate_add_match_property:
* @udev_enumerate: context
* @property: filter for a property of the device to include in the list
* @value: value of the property
*
* Match only devices with a certain property.
*
* Returns: 0 on success, otherwise a negative error value.
*/
_public_ int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value)
{
if (udev_enumerate == NULL)
return -EINVAL;
return 0;
return -ENOMEM;
return 0;
}
/**
* udev_enumerate_add_match_tag:
* @udev_enumerate: context
* @tag: filter for a tag of the device to include in the list
*
* Match only devices with a certain tag.
*
* Returns: 0 on success, otherwise a negative error value.
*/
{
if (udev_enumerate == NULL)
return -EINVAL;
return 0;
return -ENOMEM;
return 0;
}
/**
* udev_enumerate_add_match_parent:
* @udev_enumerate: context
* @parent: parent device where to start searching
*
* Return the devices on the subtree of one given device. The parent
* itself is included in the list.
*
* A reference for the device is held until the udev_enumerate context
* is cleaned up.
*
* Returns: 0 on success, otherwise a negative error value.
*/
_public_ int udev_enumerate_add_match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *parent)
{
if (udev_enumerate == NULL)
return -EINVAL;
return 0;
return 0;
}
/**
* udev_enumerate_add_match_is_initialized:
* @udev_enumerate: context
*
* Match only devices which udev has set up already. This makes
* sure, that the device node permissions and context are properly set
* and that network devices are fully renamed.
*
* Usually, devices which are found in the kernel but not already
* handled by udev, have still pending events. Services should subscribe
* to monitor events and wait for these devices to become ready, instead
* of using uninitialized devices.
*
* For now, this will not affect devices which do not have a device node
* and are not network interfaces.
*
* Returns: 0 on success, otherwise a negative error value.
*/
{
if (udev_enumerate == NULL)
return -EINVAL;
udev_enumerate->match_is_initialized = true;
return 0;
}
/**
* udev_enumerate_add_match_sysname:
* @udev_enumerate: context
* @sysname: filter for the name of the device to include in the list
*
* Match only devices with a given /sys device name.
*
* Returns: 0 on success, otherwise a negative error value.
*/
_public_ int udev_enumerate_add_match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
{
if (udev_enumerate == NULL)
return -EINVAL;
return 0;
return -ENOMEM;
return 0;
}
{
struct udev_list_entry *list_entry;
/* skip list */
return false;
}
/* include list */
/* anything that does not match, will make it FALSE */
return false;
}
return true;
}
return true;
}
{
struct udev_list_entry *list_entry;
bool match = false;
/* no match always matches */
return true;
/* loop over matches */
struct udev_list_entry *property_entry;
/* loop over device properties */
continue;
match = true;
goto out;
}
continue;
match = true;
goto out;
}
}
}
out:
return match;
}
{
struct udev_list_entry *list_entry;
/* no match always matches */
return true;
/* loop over matches */
return false;
return true;
}
{
return true;
return startswith(udev_device_get_devpath(dev), udev_device_get_devpath(udev_enumerate->parent_match));
}
{
struct udev_list_entry *list_entry;
return true;
continue;
return true;
}
return false;
}
{
char path[UTIL_PATH_SIZE];
size_t l;
char *s;
s = path;
return -ENOENT;
char syspath[UTIL_PATH_SIZE];
struct udev_device *dev;
continue;
continue;
continue;
if (udev_enumerate->match_is_initialized) {
/*
* All devices with a device node or network interfaces
* possibly need udev to adjust the device node permission
* or context, or rename the interface before it can be
* reliably used from other processes.
*
* For now, we can only check these types of devices, we
* might not store a database, and have no way to find out
* for all other types of devices.
*/
if (!udev_device_get_is_initialized(dev) &&
goto nomatch;
}
goto nomatch;
goto nomatch;
goto nomatch;
goto nomatch;
}
return 0;
}
{
struct udev_list_entry *list_entry;
return false;
}
return true;
}
return false;
}
return true;
}
static int scan_dir(struct udev_enumerate *udev_enumerate, const char *basedir, const char *subdir, const char *subsystem)
{
char path[UTIL_PATH_SIZE];
return -1;
continue;
continue;
}
return 0;
}
/**
* udev_enumerate_add_syspath:
* @udev_enumerate: context
* @syspath: path of a device
*
* Add a device to the list of devices, to retrieve it back sorted in dependency order.
*
* Returns: 0 on success, otherwise a negative error value.
*/
{
struct udev_device *udev_device;
if (udev_enumerate == NULL)
return -EINVAL;
return 0;
/* resolve to real syspath */
if (udev_device == NULL)
return -EINVAL;
return 0;
}
{
struct udev_list_entry *list_entry;
/* scan only tagged devices, use tags reverse-index, instead of searching all devices in /sys */
char path[UTIL_PATH_SIZE];
continue;
struct udev_device *dev;
continue;
continue;
goto nomatch;
goto nomatch;
goto nomatch;
goto nomatch;
goto nomatch;
}
}
return 0;
}
{
struct udev_device *dev;
return -ENODEV;
return 0;
return 0;
return 0;
return 0;
return 1;
}
{
DIR *d;
if (d == NULL)
return -errno;
char *child;
continue;
continue;
continue;
if (maxdepth > 0)
}
closedir(d);
return 0;
}
{
const char *path;
}
{
/* we have /subsystem/, forget all the old stuff */
} else {
}
return 0;
}
/**
* udev_enumerate_scan_devices:
* @udev_enumerate: udev enumeration context
*
* Scan /sys for all devices which match the given filters. No matches
* will return all currently available devices.
*
* Returns: 0 on success, otherwise a negative error value.
**/
{
if (udev_enumerate == NULL)
return -EINVAL;
/* efficiently lookup tags only, we maintain a reverse-index */
return scan_devices_tags(udev_enumerate);
/* walk the subtree of one parent device only */
return scan_devices_children(udev_enumerate);
/* scan devices of all subsystems */
return scan_devices_all(udev_enumerate);
}
/**
* udev_enumerate_scan_subsystems:
* @udev_enumerate: udev enumeration context
*
* Scan /sys for all kernel subsystems, including buses, classes, drivers.
*
* Returns: 0 on success, otherwise a negative error value.
**/
{
const char *subsysdir;
if (udev_enumerate == NULL)
return -EINVAL;
/* all kernel modules */
subsysdir = "subsystem";
else
subsysdir = "bus";
/* all subsystems (only buses support coldplug) */
/* all subsystem drivers */
return 0;
}