libudev-device.c revision 40fe8b11be9c1a1b38b91db097a5d6ebfa99304c
/*
* 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 <stdbool.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <ctype.h>
#include "libudev.h"
#include "libudev-private.h"
/**
* SECTION:libudev-device
* @short_description: kernel sys devices
*
* Representation of kernel sys devices. Devices are uniquely identified
* by their syspath, every device has exactly one path in the kernel sys
* filesystem. Devices usually belong to a kernel subsystem, and and have
* a unique name inside that subsystem.
*/
/**
* udev_device:
*
* Opaque object representing one kernel sys device.
*/
struct udev_device {
struct udev_device *parent_device;
char *syspath;
const char *devpath;
char *sysname;
const char *sysnum;
char *devnode;
char *subsystem;
char *devtype;
char *driver;
char *action;
char *devpath_old;
char *id_filename;
char **envp;
char *monitor_buf;
struct udev_list devlinks_list;
struct udev_list properties_list;
struct udev_list sysattr_value_list;
struct udev_list sysattr_list;
unsigned long long int seqnum;
int devlink_priority;
int refcount;
int ifindex;
int watch_handle;
bool parent_set;
bool subsystem_set;
bool devtype_set;
bool devlinks_uptodate;
bool envp_uptodate;
bool tags_uptodate;
bool driver_set;
bool info_loaded;
bool db_loaded;
bool uevent_loaded;
bool is_initialized;
bool sysattr_list_read;
bool db_persist;
};
/**
* udev_device_get_seqnum:
* @udev_device: udev device
*
* This is only valid if the device was received through a monitor. Devices read from
* sys do not have a sequence number.
*
* Returns: the kernel event sequence number, or 0 if there is no sequence number available.
**/
{
if (udev_device == NULL)
return 0;
return udev_device->seqnum;
}
{
char num[32];
return 0;
}
{
if (!udev_device->info_loaded)
return udev_device->ifindex;
}
{
char num[32];
return 0;
}
/**
* udev_device_get_devnum:
* @udev_device: udev device
*
*
* Returns: the dev_t number.
**/
{
if (udev_device == NULL)
return makedev(0, 0);
if (!udev_device->info_loaded)
return udev_device->devnum;
}
{
char num[32];
return 0;
}
{
return udev_device->devpath_old;
}
{
const char *pos;
return -ENOMEM;
return -EINVAL;
return 0;
}
/**
* udev_device_get_driver:
* @udev_device: udev device
*
* Get the kernel driver name.
*
* Returns: the driver name string, or #NULL if there is no driver attached.
**/
{
char driver[UTIL_NAME_SIZE];
if (udev_device == NULL)
return NULL;
if (!udev_device->driver_set) {
udev_device->driver_set = true;
if (util_get_sys_core_link_value(udev_device->udev, "driver", udev_device->syspath, driver, sizeof(driver)) > 0)
}
return udev_device->driver;
}
{
return -ENOMEM;
udev_device->driver_set = true;
return 0;
}
/**
* udev_device_get_devtype:
* @udev_device: udev device
*
* Retrieve the devtype string of the udev device.
*
* Returns: the devtype name of the udev device, or #NULL if it can not be determined
**/
{
if (udev_device == NULL)
return NULL;
if (!udev_device->devtype_set) {
udev_device->devtype_set = true;
}
return udev_device->devtype;
}
{
return -ENOMEM;
udev_device->devtype_set = true;
return 0;
}
{
return -ENOMEM;
udev_device->subsystem_set = true;
return 0;
}
/**
* udev_device_get_subsystem:
* @udev_device: udev device
*
* Retrieve the subsystem string of the udev device. The string does not
* contain any "/".
*
* Returns: the subsystem name of the udev device, or #NULL if it can not be determined
**/
{
char subsystem[UTIL_NAME_SIZE];
if (udev_device == NULL)
return NULL;
if (!udev_device->subsystem_set) {
udev_device->subsystem_set = true;
/* read "subsystem" link */
if (util_get_sys_core_link_value(udev_device->udev, "subsystem", udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
return udev_device->subsystem;
}
/* implicit names */
return udev_device->subsystem;
}
return udev_device->subsystem;
}
return udev_device->subsystem;
}
}
return udev_device->subsystem;
}
{
if (!udev_device->info_loaded)
return udev_device->devnode_mode;
}
{
char num[32];
return 0;
}
struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
{
udev_device->envp_uptodate = false;
struct udev_list_entry *list_entry;
if (list_entry != NULL)
return NULL;
}
}
static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
{
char name[UTIL_LINE_SIZE];
char *val;
return NULL;
val[0] = '\0';
if (val[0] == '\0')
}
/*
* parse property string, and if needed, update internal values accordingly
*
* udev_device_add_property_from_string_parse_finish() needs to be
* called after adding properties, and its return value checked
*
* udev_device_set_info_loaded() needs to be set, to avoid trying
* to use a device without a DEVPATH set
*/
void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
{
char path[UTIL_PATH_SIZE];
char devlinks[UTIL_PATH_SIZE];
char *slink;
char *next;
next[0] = '\0';
}
if (slink[0] != '\0')
char tags[UTIL_PATH_SIZE];
char *next;
next++;
while (next[0] != '\0') {
char *tag;
break;
next[0] = '\0';
next++;
}
}
} else {
}
}
{
if (udev_device->maj > 0)
udev_device->maj = 0;
udev_device->min = 0;
return -EINVAL;
return 0;
}
/**
* udev_device_get_property_value:
* @udev_device: udev device
* @key: property name
*
* Get the value of a given property.
*
* Returns: the property string, or #NULL if there is no such property.
**/
_public_ const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
{
struct udev_list_entry *list_entry;
if (udev_device == NULL)
return NULL;
return NULL;
return udev_list_entry_get_value(list_entry);
}
{
char filename[UTIL_PATH_SIZE];
char line[UTIL_LINE_SIZE];
FILE *f;
/* providing a database file will always force-load it */
const char *id;
if (udev_device->db_loaded)
return 0;
udev_device->db_loaded = true;
return -1;
}
if (f == NULL) {
return -1;
}
udev_device->is_initialized = true;
const char *val;
struct udev_list_entry *entry;
if (len < 4)
break;
switch(line[0]) {
case 'S':
break;
case 'L':
break;
case 'E':
udev_list_entry_set_num(entry, true);
break;
case 'G':
break;
case 'W':
break;
case 'I':
break;
}
}
fclose(f);
return 0;
}
{
char filename[UTIL_PATH_SIZE];
FILE *f;
char line[UTIL_LINE_SIZE];
int maj = 0;
int min = 0;
if (udev_device->uevent_loaded)
return 0;
if (f == NULL)
return -1;
udev_device->uevent_loaded = true;
char *pos;
continue;
pos[0] = '\0';
continue;
}
continue;
}
continue;
}
}
fclose(f);
return 0;
}
{
device->info_loaded = true;
}
{
struct udev_device *udev_device;
struct udev_list_entry *list_entry;
return NULL;
if (udev_device == NULL)
return NULL;
/* copy global properties */
return udev_device;
}
/**
* udev_device_new_from_syspath:
* @udev: udev library context
* @syspath: sys device path including sys directory
*
* Create new udev device, and fill in information from the sys
* device and the udev database entry. The syspath is the absolute
* path to the device, including the sys mount point.
*
* 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, if it does not exist
**/
{
const char *subdir;
char path[UTIL_PATH_SIZE];
char *pos;
struct udev_device *udev_device;
return NULL;
return NULL;
/* path starts in sys */
return NULL;
}
/* path is not a root directory */
return NULL;
/* resolve possible symlink to real path */
char file[UTIL_PATH_SIZE];
/* all "devices" require a "uevent" file */
return NULL;
} else {
/* everything else just needs to be a directory */
return NULL;
}
if (udev_device == NULL)
return NULL;
return udev_device;
}
/**
* udev_device_new_from_devnum:
* @udev: udev library context
* @type: char or block device
*
* Create new udev device, and fill in information from the sys
* device and the udev database entry. The device is looked-up
* numbers are not unique across the two types.
*
* 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, if it does not exist
**/
_public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
{
char path[UTIL_PATH_SIZE];
const char *type_str;
if (type == 'b')
type_str = "block";
else if (type == 'c')
type_str = "char";
else
return NULL;
}
/**
* udev_device_new_from_device_id:
* @udev: udev library context
* @id: text string identifying a kernel device
*
* Create new udev device, and fill in information from the sys
* device and the udev database entry. The device is looked-up
* by a special string:
* b8:2 - block device major:minor
* c128:1 - char device major:minor
* n3 - network device ifindex
* +sound:card29 - kernel driver core subsystem:device name
*
* 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, if it does not exist
**/
{
char type;
char subsys[UTIL_PATH_SIZE];
char *sysname;
switch(id[0]) {
case 'b':
case 'c':
return NULL;
case 'n': {
int sk;
struct udev_device *dev;
int ifindex;
if (ifindex <= 0)
return NULL;
if (sk < 0)
return NULL;
return NULL;
}
return NULL;
return dev;
return NULL;
}
case '+':
return NULL;
sysname[0] = '\0';
default:
return NULL;
}
}
/**
* udev_device_new_from_subsystem_sysname:
* @udev: udev library context
* @subsystem: the subsystem of the device
* @sysname: the name of the device
*
* Create new udev device, and fill in information from the sys device
* and the udev database entry. The device is looked up by the subsystem
* and name string of the device, like "mem" / "zero", or "block" / "sda".
*
* 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, if it does not exist
**/
_public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
{
char path[UTIL_PATH_SIZE];
goto found;
goto found;
goto found;
goto out;
}
goto found;
goto out;
}
char subsys[UTIL_NAME_SIZE];
char *driver;
driver[0] = '\0';
goto found;
goto found;
}
goto out;
}
goto found;
goto found;
goto found;
out:
return NULL;
}
/**
* udev_device_new_from_environment
* @udev: udev library context
*
* Create new udev device, and fill in information from the
* current process environment. This only works reliable if
* the process is called from a udev rule. It is usually used
* for tools executed from IMPORT= rules.
*
* 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, if it does not exist
**/
{
int i;
struct udev_device *udev_device;
if (udev_device == NULL)
return NULL;
udev_device = NULL;
}
return udev_device;
}
{
char path[UTIL_PATH_SIZE];
const char *subdir;
for (;;) {
char *pos;
break;
pos[0] = '\0';
if (udev_device_parent != NULL)
return udev_device_parent;
}
return NULL;
}
/**
* udev_device_get_parent:
* @udev_device: the device to start searching from
*
* Find the next parent device, and fill in information from the sys
* device and the udev database entry.
*
* The returned the device is not referenced. It is attached to the
* child device, and will be cleaned up when the child device
* is cleaned up.
*
* It is not necessarily just the upper level directory, empty or not
* recognized sys directories are ignored.
*
* It can be called as many times as needed, without caring about
* references.
*
* Returns: a new udev device, or #NULL, if it no parent exist.
**/
{
if (udev_device == NULL)
return NULL;
if (!udev_device->parent_set) {
udev_device->parent_set = true;
}
return udev_device->parent_device;
}
/**
* udev_device_get_parent_with_subsystem_devtype:
* @udev_device: udev device to start searching from
* @subsystem: the subsystem of the device
* @devtype: the type (DEVTYPE) of the device
*
* Find the next parent device, with a matching subsystem and devtype
* value, and fill in information from the sys device and the udev
* database entry.
*
* If devtype is #NULL, only subsystem is checked, and any devtype will
* match.
*
* The returned the device is not referenced. It is attached to the
* child device, and will be cleaned up when the child device
* is cleaned up.
*
* It can be called as many times as needed, without caring about
* references.
*
* Returns: a new udev device, or #NULL if no matching parent exists.
**/
_public_ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
{
struct udev_device *parent;
return NULL;
const char *parent_subsystem;
const char *parent_devtype;
break;
break;
}
}
return parent;
}
/**
* udev_device_get_udev:
* @udev_device: udev device
*
* Retrieve the udev library context the device was created with.
*
* Returns: the udev library context
**/
{
if (udev_device == NULL)
return NULL;
return udev_device->udev;
}
/**
* udev_device_ref:
* @udev_device: udev device
*
* Take a reference of a udev device.
*
* Returns: the passed udev device
**/
{
if (udev_device == NULL)
return NULL;
udev_device->refcount++;
return udev_device;
}
/**
* udev_device_unref:
* @udev_device: udev device
*
* Drop a reference of a udev device. If the refcount reaches zero,
* the resources of the device will be released.
*
* Returns: the passed udev device if it has still an active reference, or #NULL otherwise.
**/
{
if (udev_device == NULL)
return NULL;
udev_device->refcount--;
if (udev_device->refcount > 0)
return udev_device;
return NULL;
}
/**
* udev_device_get_devpath:
* @udev_device: udev device
*
* Retrieve the kernel devpath value of the udev device. The path
* does not contain the sys mount point, and starts with a '/'.
*
* Returns: the devpath of the udev device
**/
{
if (udev_device == NULL)
return NULL;
return udev_device->devpath;
}
/**
* udev_device_get_syspath:
* @udev_device: udev device
*
* Retrieve the sys path of the udev device. The path is an
* absolute path and starts with the sys mount point.
*
* Returns: the sys path of the udev device
**/
{
if (udev_device == NULL)
return NULL;
return udev_device->syspath;
}
/**
* udev_device_get_sysname:
* @udev_device: udev device
*
* Get the kernel device name in /sys.
*
* Returns: the name string of the device device
**/
{
if (udev_device == NULL)
return NULL;
return udev_device->sysname;
}
/**
* udev_device_get_sysnum:
* @udev_device: udev device
*
* Get the instance number of the device.
*
* Returns: the trailing number string of the device name
**/
{
if (udev_device == NULL)
return NULL;
return udev_device->sysnum;
}
/**
* udev_device_get_devnode:
* @udev_device: udev device
*
* Retrieve the device node file name belonging to the udev device.
* The path is an absolute path, and starts with the device directory.
*
* Returns: the device node file name of the udev device, or #NULL if no device node exists
**/
{
if (udev_device == NULL)
return NULL;
return udev_device->devnode;
if (!udev_device->info_loaded)
return udev_device->devnode;
}
/**
* udev_device_get_devlinks_list_entry:
* @udev_device: udev device
*
* Retrieve the list of device links pointing to the device file of
* the udev device. The next list entry can be retrieved with
* udev_list_entry_get_next(), which returns #NULL if no more entries exist.
* The devlink path can be retrieved from the list entry by
* udev_list_entry_get_name(). The path is an absolute path, and starts with
* the device directory.
*
* Returns: the first entry of the device node link list
**/
_public_ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
{
if (udev_device == NULL)
return NULL;
if (!udev_device->info_loaded)
}
{
udev_device->devlinks_uptodate = false;
}
/**
* udev_device_get_properties_list_entry:
* @udev_device: udev device
*
* device. The next list entry can be retrieved with udev_list_entry_get_next(),
* which returns #NULL if no more entries exist. The property name
* can be retrieved from the list entry by udev_list_entry_get_name(),
* the property value by udev_list_entry_get_value().
*
* Returns: the first entry of the property list
**/
_public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
{
if (udev_device == NULL)
return NULL;
if (!udev_device->info_loaded) {
}
if (!udev_device->devlinks_uptodate) {
char symlinks[UTIL_PATH_SIZE];
struct udev_list_entry *list_entry;
udev_device->devlinks_uptodate = true;
if (list_entry != NULL) {
char *s;
size_t l;
s = symlinks;
}
}
if (!udev_device->tags_uptodate) {
udev_device->tags_uptodate = true;
char tags[UTIL_PATH_SIZE];
struct udev_list_entry *list_entry;
char *s;
size_t l;
s = tags;
}
}
}
/**
* udev_device_get_action:
* @udev_device: udev device
*
* This is only valid if the device was received through a monitor. Devices read from
* sys do not have an action string. Usual actions are: add, remove, change, online,
* offline.
*
* Returns: the kernel action value, or #NULL if there is no action value available.
**/
{
if (udev_device == NULL)
return NULL;
return udev_device->action;
}
/**
* udev_device_get_usec_since_initialized:
* @udev_device: udev device
*
* Return the number of microseconds passed since udev set up the
* device for the first time.
*
* This is only implemented for devices with need to store properties
* in the udev database. All other devices return 0 here.
*
* Returns: the number of microseconds since the device was first seen.
**/
_public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
{
if (udev_device == NULL)
return 0;
if (!udev_device->info_loaded)
if (udev_device->usec_initialized == 0)
return 0;
if (now_ts == 0)
return 0;
}
{
return udev_device->usec_initialized;
}
{
char num[32];
}
/**
* udev_device_get_sysattr_value:
* @udev_device: udev device
* @sysattr: attribute name
*
* The retrieved value is cached in the device. Repeated calls will return the same
* value and not open the attribute again.
*
* Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
**/
_public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
{
struct udev_list_entry *list_entry;
char path[UTIL_PATH_SIZE];
char value[4096];
int fd;
if (udev_device == NULL)
return NULL;
return NULL;
/* look for possibly already cached result */
if (list_entry != NULL)
return udev_list_entry_get_value(list_entry);
goto out;
}
struct udev_device *dev;
/*
* Some core links return only the last element of the target path,
* these are just values, the paths should not be exposed.
*/
return NULL;
goto out;
}
/* resolve link to a device and return its syspath */
}
goto out;
}
/* skip directories */
goto out;
/* skip non-readable files */
goto out;
/* read attribute value */
if (fd < 0)
goto out;
if (size < 0)
goto out;
goto out;
/* got a valid value, store it in cache and return it */
out:
return val;
}
{
int num = 0;
if (udev_device == NULL)
return -1;
if (udev_device->sysattr_list_read)
return 0;
if (!dir)
return -1;
char path[UTIL_PATH_SIZE];
/* only handle symlinks and regular files */
continue;
continue;
continue;
num++;
}
udev_device->sysattr_list_read = true;
return num;
}
/**
* udev_device_get_sysattr_list_entry:
* @udev_device: udev device
*
* Retrieve the list of available sysattrs, with value being empty;
* This just return all available sysfs attributes for a particular
* device without reading their values.
*
* Returns: the first entry of the property list
**/
_public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
{
if (!udev_device->sysattr_list_read) {
int ret;
if (0 > ret)
return NULL;
}
}
{
const char *pos;
return -ENOMEM;
return -EINVAL;
return -ENOMEM;
/* some devices have '!' in their name, change that to '/' */
len = 0;
len++;
}
/* trailing number */
/* sysname is completely numeric */
if (len == 0)
return 0;
}
{
if (devnode[0] != '/') {
} else {
}
return -ENOMEM;
return 0;
}
{
struct udev_list_entry *list_entry;
udev_device->devlinks_uptodate = false;
if (list_entry == NULL)
return -ENOMEM;
return 0;
}
{
return NULL;
/* use dev_t -- b259:131072, c254:0 */
} else if (udev_device_get_ifindex(udev_device) > 0) {
/* use netdev ifindex -- n3 */
} else {
/*
* use $subsys:$syname -- pci:0000:00:1f.2
* sysname() has '!' translated, get it from devpath
*/
const char *sysname;
return NULL;
if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
}
}
return udev_device->id_filename;
}
/**
* udev_device_get_is_initialized:
* @udev_device: udev device
*
* Check if udev has already handled the device and has set up
* device node permissions and context, or has renamed a network
* device.
*
* This is only implemented for devices with a device node
* or network interfaces. All other devices return 1 here.
*
* Returns: 1 if the device is set up. 0 otherwise.
**/
{
if (!udev_device->info_loaded)
return udev_device->is_initialized;
}
{
udev_device->is_initialized = true;
}
{
return -EINVAL;
udev_device->tags_uptodate = false;
return 0;
return -ENOMEM;
}
{
udev_device->tags_uptodate = false;
}
/**
* udev_device_get_tags_list_entry:
* @udev_device: udev device
*
* Retrieve the list of tags attached to the udev device. The next
* list entry can be retrieved with udev_list_entry_get_next(),
* which returns #NULL if no more entries exist. The tag string
* can be retrieved from the list entry by udev_list_entry_get_name().
*
* Returns: the first entry of the tag list
**/
{
if (udev_device == NULL)
return NULL;
if (!udev_device->info_loaded)
}
/**
* udev_device_has_tag:
* @udev_device: udev device
* @tag: tag name
*
* Check if a given device has a certain tag associated.
*
* Returns: 1 if the tag is found. 0 otherwise.
**/
{
struct udev_list_entry *list_entry;
if (udev_device == NULL)
return false;
if (!udev_device->info_loaded)
return true;
return false;
}
#define ENVP_SIZE 128
#define MONITOR_BUF_SIZE 4096
{
struct udev_list_entry *list_entry;
char *s;
size_t l;
unsigned int i;
/* monitor buffer of property strings */
udev_device->monitor_buf_len = 0;
return -ENOMEM;
/* envp array, strings will point into monitor buffer */
return -ENOMEM;
i = 0;
s = udev_device->monitor_buf;
l = MONITOR_BUF_SIZE;
const char *key;
/* skip private variables */
if (key[0] == '.')
continue;
/* add string to envp array */
udev_device->envp[i++] = s;
if (i+1 >= ENVP_SIZE)
return -EINVAL;
/* add property string to monitor buffer */
if (l == 0)
return -EINVAL;
/* advance past the trailing '\0' that util_strpcpyl() guarantees */
s++;
l--;
}
udev_device->envp_uptodate = true;
return 0;
}
{
if (!udev_device->envp_uptodate)
if (update_envp_monitor_buf(udev_device) != 0)
return NULL;
return udev_device->envp;
}
{
if (!udev_device->envp_uptodate)
if (update_envp_monitor_buf(udev_device) != 0)
return -EINVAL;
return udev_device->monitor_buf_len;
}
{
return -ENOMEM;
return 0;
}
{
if (!udev_device->info_loaded)
return udev_device->devlink_priority;
}
{
return 0;
}
{
if (!udev_device->info_loaded)
return udev_device->watch_handle;
}
{
return 0;
}
{
return udev_device->db_persist;
}
{
udev_device->db_persist = true;
}