1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

udevd: use libudev

This commit is contained in:
Kay Sievers 2008-10-16 17:16:58 +02:00
parent d56f94d168
commit aa8734ffcb
16 changed files with 1277 additions and 2218 deletions

View File

@ -17,12 +17,10 @@ common_files = \
udev_rules.h \
udev_sysdeps.h \
udev_db.c \
udev_device.c \
udev_device_event.c \
udev_node.c \
udev_rules.c \
udev_rules_parse.c \
udev_sysfs.c \
udev_utils.c \
udev_utils_file.c \
list.h \

View File

@ -75,7 +75,7 @@ static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *f
return util_path_encode(&filename[start], len - start);
}
static int device_read_db(struct udev_device *udev_device)
int udev_device_read_db(struct udev_device *udev_device)
{
struct stat stats;
char filename[UTIL_PATH_SIZE];
@ -220,7 +220,7 @@ static void device_load_info(struct udev_device *device)
{
device->info_loaded = 1;
udev_device_read_uevent_file(device);
device_read_db(device);
udev_device_read_db(device);
}
void udev_device_set_info_loaded(struct udev_device *device)

View File

@ -62,6 +62,7 @@ extern void udev_device_cleanup_devlinks_list(struct udev_device *udev_device);
extern struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value);
extern struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property);
extern char **udev_device_get_properties_envp(struct udev_device *udev_device);
extern int udev_device_read_db(struct udev_device *udev_device);
extern int udev_device_read_uevent_file(struct udev_device *udev_device);
extern int udev_device_set_action(struct udev_device *udev_device, const char *action);
extern int udev_device_set_driver(struct udev_device *udev_device, const char *driver);

View File

@ -45,20 +45,20 @@ static void asmlinkage sig_handler(int signum)
int main(int argc, char *argv[])
{
struct udev *udev;
struct sysfs_device *dev;
struct udevice *udevice;
const char *maj, *min;
struct udev_event *event;
struct udev_device *dev;
struct udev_rules rules;
const char *action;
char syspath[UTIL_PATH_SIZE];
const char *devpath;
const char *action;
const char *subsystem;
struct sigaction act;
int retval = -EINVAL;
int err = -EINVAL;
udev = udev_new();
if (udev == NULL)
exit(1);
dbg(udev, "version %s\n", VERSION);
info(udev, "version %s\n", VERSION);
selinux_init(udev);
/* set signal handlers */
@ -76,65 +76,45 @@ int main(int argc, char *argv[])
action = getenv("ACTION");
devpath = getenv("DEVPATH");
subsystem = getenv("SUBSYSTEM");
/* older kernels passed the SUBSYSTEM only as argument */
if (subsystem == NULL && argc == 2)
subsystem = argv[1];
if (action == NULL || subsystem == NULL || devpath == NULL) {
err(udev, "action, subsystem or devpath missing\n");
goto exit;
}
/* export log_priority , as called programs may want to do the same as udev */
if (udev_get_log_priority(udev) > 0) {
char priority[32];
sprintf(priority, "%i", udev_get_log_priority(udev));
setenv("UDEV_LOG", priority, 1);
}
sysfs_init();
udev_rules_init(udev, &rules, 0);
dev = sysfs_device_get(udev, devpath);
util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
util_strlcat(syspath, devpath, sizeof(syspath));
dev = udev_device_new_from_syspath(udev, syspath);
if (dev == NULL) {
info(udev, "unable to open '%s'\n", devpath);
info(udev, "unknown device '%s'\n", devpath);
goto fail;
}
udevice = udev_device_init(udev);
if (udevice == NULL)
goto fail;
/* skip reading of db, but read kernel parameters */
udev_device_set_info_loaded(dev);
udev_device_read_uevent_file(dev);
/* override built-in sysfs device */
udevice->dev = dev;
util_strlcpy(udevice->action, action, sizeof(udevice->action));
/* get dev_t from environment, which is needed for "remove" to work, "add" works also from sysfs */
maj = getenv("MAJOR");
min = getenv("MINOR");
if (maj != NULL && min != NULL)
udevice->devt = makedev(atoi(maj), atoi(min));
else
udevice->devt = udev_device_get_devt(udevice);
retval = udev_device_event(&rules, udevice);
udev_device_set_action(dev, action);
event = udev_event_new(dev);
err = udev_event_run(event, &rules);
/* rules may change/disable the timeout */
if (udevice->event_timeout >= 0)
alarm(udevice->event_timeout);
if (udev_device_get_event_timeout(dev) >= 0)
alarm(udev_device_get_event_timeout(dev));
if (retval == 0 && !udevice->ignore_device && udev_get_run(udev))
udev_rules_run(udevice);
if (err == 0 && !event->ignore_device && udev_get_run(udev))
udev_rules_run(event);
udev_device_cleanup(udevice);
udev_event_unref(event);
udev_device_unref(dev);
fail:
udev_rules_cleanup(&rules);
sysfs_cleanup();
exit:
selinux_exit(udev);
udev_unref(udev);
if (retval != 0)
if (err != 0)
return 1;
return 0;
}

View File

@ -23,7 +23,6 @@
#include <sys/param.h>
#include "udev_sysdeps.h"
#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE 1
#include "lib/libudev.h"
#include "lib/libudev-private.h"
#include "list.h"
@ -49,52 +48,6 @@
struct udev_rules;
struct sysfs_device {
struct list_head node; /* for device cache */
struct sysfs_device *parent; /* already cached parent*/
char devpath[UTIL_PATH_SIZE];
char subsystem[UTIL_NAME_SIZE];
char kernel[UTIL_NAME_SIZE]; /* device instance name */
char kernel_number[UTIL_NAME_SIZE];
char driver[UTIL_NAME_SIZE];
};
struct udevice {
struct udev *udev;
/* device event */
struct sysfs_device *dev; /* points to dev_local by default */
struct sysfs_device dev_local;
struct sysfs_device *dev_parent; /* current parent device used for matching */
char action[UTIL_NAME_SIZE];
char *devpath_old;
/* node */
char name[UTIL_PATH_SIZE];
struct list_head symlink_list;
int symlink_final;
char owner[UTIL_NAME_SIZE];
int owner_final;
char group[UTIL_NAME_SIZE];
int group_final;
mode_t mode;
int mode_final;
dev_t devt;
/* event processing */
struct list_head run_list;
int run_final;
struct list_head env_list;
char tmp_node[UTIL_PATH_SIZE];
int partitions;
int ignore_device;
int ignore_remove;
char program_result[UTIL_PATH_SIZE];
int link_priority;
int event_timeout;
int test_run;
};
static inline void logging_init(const char *program_name)
{
openlog(program_name, LOG_PID | LOG_CONS, LOG_DAEMON);
@ -112,40 +65,48 @@ static inline void logging_close(void)
closelog();
}
/* udev_device.c */
extern struct udevice *udev_device_init(struct udev *udev);
extern void udev_device_cleanup(struct udevice *udevice);
extern dev_t udev_device_get_devt(struct udevice *udevice);
/* udev-event.c */
struct udev_event {
struct udev *udev;
struct udev_device *dev;
struct udev_device *dev_parent;
int devlink_final;
int owner_final;
int group_final;
int mode_final;
char tmp_node[UTIL_PATH_SIZE];
char program_result[UTIL_PATH_SIZE];
int run_final;
/* udev_device_event.c */
extern int udev_device_event(struct udev_rules *rules, struct udevice *udevice);
char name[UTIL_PATH_SIZE];
mode_t mode;
char owner[UTIL_NAME_SIZE];
char group[UTIL_NAME_SIZE];
struct udev_list_node run_list;
int ignore_device;
int test;
/* udev_sysfs.c */
extern int sysfs_init(void);
extern void sysfs_cleanup(void);
extern void sysfs_device_set_values(struct udev *udev,
struct sysfs_device *dev, const char *devpath,
const char *subsystem, const char *driver);
extern struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath);
extern struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev);
extern struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, struct sysfs_device *dev, const char *subsystem);
extern char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name);
extern int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath, size_t len, const char *subsystem, const char *id);
struct list_head node;
pid_t pid;
int exitstatus;
time_t queue_time;
};
extern struct udev_event *udev_event_new(struct udev_device *dev);
extern void udev_event_unref(struct udev_event *event);
extern int udev_event_run(struct udev_event *event, struct udev_rules *rules);
/* udev_node.c */
extern int udev_node_mknod(struct udevice *udevice, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid);
extern void udev_node_update_symlinks(struct udevice *udevice, struct udevice *udev_old);
extern int udev_node_add(struct udevice *udevice);
extern int udev_node_remove(struct udevice *udevice);
/* udev-node.c */
extern int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid);
extern int udev_node_add(struct udev_device *dev, mode_t mode, const char *owner, const char *group, int test);
extern int udev_node_remove(struct udev_device *dev, int test);
extern void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old, int test);
/* udev_db.c */
extern int udev_db_add_device(struct udevice *udevice);
extern int udev_db_delete_device(struct udevice *udevice);
extern int udev_db_rename(struct udev *udev, const char *devpath_old, const char *devpath);
extern int udev_db_get_device(struct udevice *udevice, const char *devpath);
extern int udev_db_get_devices_by_name(struct udev *udev, const char *name, struct list_head *name_list);
/* udev-device-db.c */
extern int udev_device_update_db(struct udev_device *udev_device);
extern int udev_device_delete_db(struct udev_device *udev_device);
extern int udev_device_rename_db(struct udev_device *udev_device, const char *devpath);
/* udev_utils.c */
/* udev-util.c */
struct name_entry {
struct list_head node;
char name[UTIL_PATH_SIZE];
@ -167,7 +128,7 @@ extern int file_map(const char *filename, char **buf, size_t *bufsize);
extern void file_unmap(void *buf, size_t bufsize);
extern size_t buf_get_line(const char *buf, size_t buflen, size_t cur);
/* udev_selinux */
/* udev-selinux.c */
#ifndef USE_SELINUX
static inline void selinux_init(struct udev *udev) {}
static inline void selinux_exit(struct udev *udev) {}
@ -189,5 +150,4 @@ extern int udevadm_control(struct udev *udev, int argc, char *argv[]);
extern int udevadm_trigger(struct udev *udev, int argc, char *argv[]);
extern int udevadm_settle(struct udev *udev, int argc, char *argv[]);
extern int udevadm_test(struct udev *udev, int argc, char *argv[]);
#endif

View File

@ -1,6 +1,5 @@
/*
* Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
* Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
* Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -23,10 +22,7 @@
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "udev.h"
@ -41,282 +37,99 @@ static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *f
return util_path_encode(&filename[start], len - start);
}
/* reverse mapping from the device file name to the devpath */
static int name_index(struct udev *udev, const char *devpath, const char *name, int add)
{
char device[UTIL_PATH_SIZE];
char filename[UTIL_PATH_SIZE * 2];
size_t start;
int fd;
/* directory with device name */
util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
start = util_strlcat(filename, "/.udev/names/", sizeof(filename));
util_strlcat(filename, name, sizeof(filename));
util_path_encode(&filename[start], sizeof(filename) - start);
/* entry with the devpath */
util_strlcpy(device, devpath, sizeof(device));
util_path_encode(device, sizeof(device));
util_strlcat(filename, "/", sizeof(filename));
util_strlcat(filename, device, sizeof(filename));
if (add) {
info(udev, "creating index: '%s'\n", filename);
create_path(udev, filename);
fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (fd > 0)
close(fd);
} else {
info(udev, "removing index: '%s'\n", filename);
unlink(filename);
delete_path(udev, filename);
}
return 0;
}
int udev_db_get_devices_by_name(struct udev *udev, const char *name, struct list_head *name_list)
{
char dirname[PATH_MAX];
size_t start;
DIR *dir;
int rc = 0;
util_strlcpy(dirname, udev_get_dev_path(udev), sizeof(dirname));
start = util_strlcat(dirname, "/.udev/names/", sizeof(dirname));
util_strlcat(dirname, name, sizeof(dirname));
util_path_encode(&dirname[start], sizeof(dirname) - start);
dir = opendir(dirname);
if (dir == NULL) {
info(udev, "no index directory '%s': %m\n", dirname);
rc = -1;
goto out;
}
info(udev, "found index directory '%s'\n", dirname);
while (1) {
struct dirent *ent;
char device[UTIL_PATH_SIZE];
ent = readdir(dir);
if (ent == NULL || ent->d_name[0] == '\0')
break;
if (ent->d_name[0] == '.')
continue;
util_strlcpy(device, ent->d_name, sizeof(device));
util_path_decode(device);
name_list_add(udev, name_list, device, 0);
rc++;
}
closedir(dir);
out:
return rc;
}
int udev_db_rename(struct udev *udev, const char *devpath_old, const char *devpath)
{
char filename[UTIL_PATH_SIZE];
char filename_old[UTIL_PATH_SIZE];
devpath_to_db_path(udev, devpath_old, filename_old, sizeof(filename_old));
devpath_to_db_path(udev, devpath, filename, sizeof(filename));
return rename(filename_old, filename);
}
int udev_db_add_device(struct udevice *udevice)
int udev_device_update_db(struct udev_device *udev_device)
{
struct udev *udev = udev_device_get_udev(udev_device);
char filename[UTIL_PATH_SIZE];
FILE *f;
struct name_entry *name_loop;
char target[232]; /* on 64bit, tmpfs inlines up to 239 bytes */
size_t devlen = strlen(udev_get_dev_path(udev))+1;
struct udev_list_entry *list_entry;
int ret;
if (udevice->test_run)
return 0;
devpath_to_db_path(udevice->udev, udevice->dev->devpath, filename, sizeof(filename));
create_path(udevice->udev, filename);
devpath_to_db_path(udev,
udev_device_get_devpath(udev_device),
filename, sizeof(filename));
create_path(udev, filename);
unlink(filename);
if (!list_empty(&udevice->env_list))
udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device))
if (udev_list_entry_get_flag(list_entry))
goto file;
if (udev_device_get_num_fake_partitions(udev_device))
goto file;
if (udevice->partitions || udevice->ignore_remove)
if (udev_device_get_ignore_remove(udev_device))
goto file;
/* try not to waste tmpfs memory, store values, if they fit, in a symlink target */
util_strlcpy(target, udevice->name, sizeof(target));
list_for_each_entry(name_loop, &udevice->symlink_list, node) {
/* try not to waste tmpfs memory; store values, if they fit, in a symlink target */
util_strlcpy(target, &udev_device_get_devnode(udev_device)[devlen], sizeof(target));
udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device)) {
size_t len;
util_strlcat(target, " ", sizeof(target));
len = util_strlcat(target, name_loop->name, sizeof(target));
len = util_strlcat(target, &udev_list_entry_get_name(list_entry)[devlen], sizeof(target));
if (len >= sizeof(target)) {
info(udevice->udev, "size of links too large, create file\n");
info(udev, "size of links too large, create file\n");
goto file;
}
}
/* add symlink names to index */
list_for_each_entry(name_loop, &udevice->symlink_list, node) {
name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 1);
}
info(udevice->udev, "create db link (%s)\n", target);
udev_selinux_setfscreatecon(udevice->udev, filename, S_IFLNK);
info(udev, "create db link (%s)\n", target);
udev_selinux_setfscreatecon(udev, filename, S_IFLNK);
ret = symlink(target, filename);
udev_selinux_resetfscreatecon(udevice->udev);
udev_selinux_resetfscreatecon(udev);
if (ret == 0)
goto out;
file:
f = fopen(filename, "w");
if (f == NULL) {
err(udevice->udev, "unable to create db file '%s': %m\n", filename);
err(udev, "unable to create db file '%s': %m\n", filename);
return -1;
}
info(udevice->udev, "created db file for '%s' in '%s'\n", udevice->dev->devpath, filename);
info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
fprintf(f, "N:%s\n", udevice->name);
list_for_each_entry(name_loop, &udevice->symlink_list, node) {
fprintf(f, "S:%s\n", name_loop->name);
/* add symlink names to index */
name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 1);
fprintf(f, "N:%s\n", &udev_device_get_devnode(udev_device)[devlen]);
udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(udev_device))
fprintf(f, "S:%s\n", &udev_list_entry_get_name(list_entry)[devlen]);
if (udev_device_get_devlink_priority(udev_device) != 0)
fprintf(f, "L:%u\n", udev_device_get_devlink_priority(udev_device));
if (udev_device_get_event_timeout(udev_device) >= 0)
fprintf(f, "T:%u\n", udev_device_get_event_timeout(udev_device));
if (udev_device_get_num_fake_partitions(udev_device) != 0)
fprintf(f, "A:%u\n", udev_device_get_num_fake_partitions(udev_device));
if (udev_device_get_ignore_remove(udev_device))
fprintf(f, "R:%u\n", udev_device_get_ignore_remove(udev_device));
udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
if (!udev_list_entry_get_flag(list_entry))
continue;
fprintf(f, "E:%s=%s\n",
udev_list_entry_get_name(list_entry),
udev_list_entry_get_value(list_entry));
}
fprintf(f, "M:%u:%u\n", major(udevice->devt), minor(udevice->devt));
if (udevice->link_priority != 0)
fprintf(f, "L:%u\n", udevice->link_priority);
if (udevice->event_timeout >= 0)
fprintf(f, "T:%u\n", udevice->event_timeout);
if (udevice->partitions != 0)
fprintf(f, "A:%u\n", udevice->partitions);
if (udevice->ignore_remove)
fprintf(f, "R:%u\n", udevice->ignore_remove);
list_for_each_entry(name_loop, &udevice->env_list, node)
fprintf(f, "E:%s\n", name_loop->name);
fclose(f);
out:
/* add name to index */
name_index(udevice->udev, udevice->dev->devpath, udevice->name, 1);
return 0;
}
int udev_db_get_device(struct udevice *udevice, const char *devpath)
{
struct stat stats;
char filename[UTIL_PATH_SIZE];
char line[UTIL_PATH_SIZE];
unsigned int maj, min;
char *bufline;
char *buf;
size_t bufsize;
size_t cur;
size_t count;
sysfs_device_set_values(udevice->udev, udevice->dev, devpath, NULL, NULL);
devpath_to_db_path(udevice->udev, devpath, filename, sizeof(filename));
if (lstat(filename, &stats) != 0) {
info(udevice->udev, "no db file to read %s: %m\n", filename);
return -1;
}
if ((stats.st_mode & S_IFMT) == S_IFLNK) {
char target[UTIL_NAME_SIZE];
int target_len;
char *next;
info(udevice->udev, "found db symlink\n");
target_len = readlink(filename, target, sizeof(target));
if (target_len > 0)
target[target_len] = '\0';
else {
info(udevice->udev, "error reading db link %s: %m\n", filename);
return -1;
}
next = strchr(target, ' ');
if (next != NULL) {
next[0] = '\0';
next = &next[1];
}
info(udevice->udev, "got db link node: '%s'\n", target);
util_strlcpy(udevice->name, target, sizeof(udevice->name));
while (next != NULL) {
char *lnk;
lnk = next;
next = strchr(next, ' ');
if (next != NULL) {
next[0] = '\0';
next = &next[1];
}
info(udevice->udev, "got db link link: '%s'\n", lnk);
name_list_add(udevice->udev, &udevice->symlink_list, lnk, 0);
}
return 0;
}
if (file_map(filename, &buf, &bufsize) != 0) {
info(udevice->udev, "error reading db file %s: %m\n", filename);
return -1;
}
cur = 0;
while (cur < bufsize) {
count = buf_get_line(buf, bufsize, cur);
bufline = &buf[cur];
cur += count+1;
if (count > sizeof(line))
count = sizeof(line);
memcpy(line, &bufline[2], count-2);
line[count-2] = '\0';
switch(bufline[0]) {
case 'N':
util_strlcpy(udevice->name, line, sizeof(udevice->name));
break;
case 'M':
sscanf(line, "%u:%u", &maj, &min);
udevice->devt = makedev(maj, min);
break;
case 'S':
name_list_add(udevice->udev, &udevice->symlink_list, line, 0);
break;
case 'L':
udevice->link_priority = atoi(line);
break;
case 'T':
udevice->event_timeout = atoi(line);
break;
case 'A':
udevice->partitions = atoi(line);
break;
case 'R':
udevice->ignore_remove = atoi(line);
break;
case 'E':
name_list_add(udevice->udev, &udevice->env_list, line, 0);
break;
}
}
file_unmap(buf, bufsize);
if (udevice->name[0] == '\0')
return -1;
return 0;
}
int udev_db_delete_device(struct udevice *udevice)
int udev_device_delete_db(struct udev_device *udev_device)
{
char filename[UTIL_PATH_SIZE];
struct name_entry *name_loop;
if (udevice->test_run)
return 0;
devpath_to_db_path(udevice->udev, udevice->dev->devpath, filename, sizeof(filename));
devpath_to_db_path(udev_device_get_udev(udev_device),
udev_device_get_devpath(udev_device),
filename, sizeof(filename));
unlink(filename);
name_index(udevice->udev, udevice->dev->devpath, udevice->name, 0);
list_for_each_entry(name_loop, &udevice->symlink_list, node)
name_index(udevice->udev, udevice->dev->devpath, name_loop->name, 0);
return 0;
}
int udev_device_rename_db(struct udev_device *udev_device, const char *devpath_old)
{
char filename_old[UTIL_PATH_SIZE];
char filename[UTIL_PATH_SIZE];
devpath_to_db_path(udev_device_get_udev(udev_device),
devpath_old,
filename_old, sizeof(filename_old));
devpath_to_db_path(udev_device_get_udev(udev_device),
udev_device_get_devpath(udev_device),
filename, sizeof(filename));
return rename(filename_old, filename);
}

View File

@ -1,84 +0,0 @@
/*
* Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <linux/sockios.h>
#include "udev.h"
#include "udev_rules.h"
struct udevice *udev_device_init(struct udev *udev)
{
struct udevice *udevice;
udevice = malloc(sizeof(struct udevice));
if (udevice == NULL)
return NULL;
memset(udevice, 0x00, sizeof(struct udevice));
udevice->udev = udev;
INIT_LIST_HEAD(&udevice->symlink_list);
INIT_LIST_HEAD(&udevice->run_list);
INIT_LIST_HEAD(&udevice->env_list);
/* set sysfs device to local storage, can be overridden if needed */
udevice->dev = &udevice->dev_local;
/* default node permissions */
udevice->mode = 0660;
strcpy(udevice->owner, "root");
strcpy(udevice->group, "root");
udevice->event_timeout = -1;
return udevice;
}
void udev_device_cleanup(struct udevice *udevice)
{
if (udevice == NULL)
return;
name_list_cleanup(udevice->udev, &udevice->symlink_list);
name_list_cleanup(udevice->udev, &udevice->run_list);
name_list_cleanup(udevice->udev, &udevice->env_list);
free(udevice);
}
dev_t udev_device_get_devt(struct udevice *udevice)
{
const char *attr;
unsigned int maj, min;
/* read it from sysfs */
attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "dev");
if (attr != NULL) {
if (sscanf(attr, "%u:%u", &maj, &min) == 2)
return makedev(maj, min);
}
return makedev(0, 0);
}

View File

@ -31,6 +31,33 @@
#include "udev.h"
#include "udev_rules.h"
struct udev_event *udev_event_new(struct udev_device *dev)
{
struct udev_event *event;
event = malloc(sizeof(struct udev_event));
if (event == NULL)
return NULL;
memset(event, 0x00, sizeof(struct udev_event));
event->dev = dev;
event->udev = udev_device_get_udev(dev);
udev_list_init(&event->run_list);
event->mode = 0660;
util_strlcpy(event->owner, "0", sizeof(event->owner));
util_strlcpy(event->group, "0", sizeof(event->group));
dbg(event->udev, "allocated event %p\n", event);
return event;
}
void udev_event_unref(struct udev_event *event)
{
udev_list_cleanup(event->udev, &event->run_list);
dbg(event->udev, "free event %p\n", event);
free(event);
}
static void kernel_log(struct ifreq ifr)
{
int klog;
@ -51,209 +78,209 @@ static void kernel_log(struct ifreq ifr)
fclose(f);
}
static int rename_netif(struct udevice *udevice)
static int rename_netif(struct udev_event *event)
{
struct udev_device *dev = event->dev;
int sk;
struct ifreq ifr;
int retval;
int err;
info(udevice->udev, "changing net interface name from '%s' to '%s'\n", udevice->dev->kernel, udevice->name);
if (udevice->test_run)
info(event->udev, "changing net interface name from '%s' to '%s'\n",
udev_device_get_sysname(dev), event->name);
if (event->test)
return 0;
sk = socket(PF_INET, SOCK_DGRAM, 0);
if (sk < 0) {
err(udevice->udev, "error opening socket: %m\n");
err(event->udev, "error opening socket: %m\n");
return -1;
}
memset(&ifr, 0x00, sizeof(struct ifreq));
util_strlcpy(ifr.ifr_name, udevice->dev->kernel, IFNAMSIZ);
util_strlcpy(ifr.ifr_newname, udevice->name, IFNAMSIZ);
retval = ioctl(sk, SIOCSIFNAME, &ifr);
if (retval == 0)
util_strlcpy(ifr.ifr_name, udev_device_get_sysname(dev), IFNAMSIZ);
util_strlcpy(ifr.ifr_newname, event->name, IFNAMSIZ);
err = ioctl(sk, SIOCSIFNAME, &ifr);
if (err == 0)
kernel_log(ifr);
else {
int loop;
/* see if the destination interface name already exists */
if (errno != EEXIST) {
err(udevice->udev, "error changing netif name %s to %s: %m\n",
err(event->udev, "error changing netif name %s to %s: %m\n",
ifr.ifr_name, ifr.ifr_newname);
goto exit;
}
/* free our own name, another process may wait for us */
util_strlcpy(ifr.ifr_newname, udevice->dev->kernel, IFNAMSIZ);
util_strlcpy(ifr.ifr_newname, udev_device_get_sysname(dev), IFNAMSIZ);
util_strlcat(ifr.ifr_newname, "_rename", IFNAMSIZ);
retval = ioctl(sk, SIOCSIFNAME, &ifr);
if (retval != 0) {
err(udevice->udev, "error changing netif name %s to %s: %m\n",
err = ioctl(sk, SIOCSIFNAME, &ifr);
if (err != 0) {
err(event->udev, "error changing netif name %s to %s: %m\n",
ifr.ifr_name, ifr.ifr_newname);
goto exit;
}
/* wait 30 seconds for our target to become available */
util_strlcpy(ifr.ifr_name, ifr.ifr_newname, IFNAMSIZ);
util_strlcpy(ifr.ifr_newname, udevice->name, IFNAMSIZ);
util_strlcpy(ifr.ifr_newname, udev_device_get_devnode(dev), IFNAMSIZ);
loop = 30 * 20;
while (loop--) {
retval = ioctl(sk, SIOCSIFNAME, &ifr);
if (retval == 0) {
err = ioctl(sk, SIOCSIFNAME, &ifr);
if (err == 0) {
kernel_log(ifr);
break;
}
if (errno != EEXIST) {
err(udevice->udev, "error changing net interface name %s to %s: %m\n",
err(event->udev, "error changing net interface name %s to %s: %m\n",
ifr.ifr_name, ifr.ifr_newname);
break;
}
dbg(udevice->udev, "wait for netif '%s' to become free, loop=%i\n",
udevice->name, (30 * 20) - loop);
dbg(event->udev, "wait for netif '%s' to become free, loop=%i\n",
udev_device_get_devnode(dev), (30 * 20) - loop);
usleep(1000 * 1000 / 20);
}
}
exit:
close(sk);
return retval;
return err;
}
int udev_device_event(struct udev_rules *rules, struct udevice *udevice)
int udev_event_run(struct udev_event *event, struct udev_rules *rules)
{
int retval = 0;
struct udev_device *dev = event->dev;
int err = 0;
if (udevice->devpath_old != NULL)
if (udev_db_rename(udevice->udev, udevice->devpath_old, udevice->dev->devpath) == 0)
info(udevice->udev, "moved database from '%s' to '%s'\n", udevice->devpath_old, udevice->dev->devpath);
if (udev_device_get_devpath_old(dev) != NULL) {
if (udev_device_rename_db(dev, udev_device_get_devpath(dev)) == 0)
info(event->udev, "moved database from '%s' to '%s'\n",
udev_device_get_devpath_old(dev), udev_device_get_devpath(dev));
}
/* add device node */
if (major(udevice->devt) != 0 &&
(strcmp(udevice->action, "add") == 0 || strcmp(udevice->action, "change") == 0)) {
struct udevice *udevice_old;
if (major(udev_device_get_devnum(dev)) != 0 &&
(strcmp(udev_device_get_action(dev), "add") == 0 || strcmp(udev_device_get_action(dev), "change") == 0)) {
char filename[UTIL_PATH_SIZE];
struct udev_device *dev_old;
dbg(udevice->udev, "device node add '%s'\n", udevice->dev->devpath);
dbg(event->udev, "device node add '%s'\n", udev_device_get_devpath(dev));
udev_rules_get_name(rules, udevice);
if (udevice->ignore_device) {
info(udevice->udev, "device event will be ignored\n");
udev_rules_get_name(rules, event);
if (event->ignore_device) {
info(event->udev, "device event will be ignored\n");
goto exit;
}
if (udevice->name[0] == '\0') {
info(udevice->udev, "device node creation supressed\n");
if (event->name[0] == '\0') {
info(event->udev, "device node creation supressed\n");
goto exit;
}
/* set device node name */
util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
util_strlcat(filename, "/", sizeof(filename));
util_strlcat(filename, event->name, sizeof(filename));
udev_device_set_devnode(dev, filename);
/* read current database entry; cleanup, if it is known device */
udevice_old = udev_device_init(udevice->udev);
if (udevice_old != NULL) {
udevice_old->test_run = udevice->test_run;
if (udev_db_get_device(udevice_old, udevice->dev->devpath) == 0) {
info(udevice->udev, "device '%s' already in database, cleanup\n", udevice->dev->devpath);
udev_db_delete_device(udevice_old);
} else {
udev_device_cleanup(udevice_old);
udevice_old = NULL;
}
dev_old = udev_device_new_from_syspath(event->udev, udev_device_get_syspath(dev));
if (dev_old != NULL) {
info(event->udev, "device '%s' already in database, updating\n",
udev_device_get_devpath(dev));
udev_node_update_old_links(dev, dev_old, event->test);
udev_device_unref(dev_old);
}
/* create node */
retval = udev_node_add(udevice);
if (retval != 0)
udev_device_update_db(dev);
err = udev_node_add(dev, event->mode, event->owner, event->group, event->test);
if (err != 0)
goto exit;
/* store in database */
udev_db_add_device(udevice);
/* create, replace, delete symlinks according to priority */
udev_node_update_symlinks(udevice, udevice_old);
if (udevice_old != NULL)
udev_device_cleanup(udevice_old);
goto exit;
}
/* add netif */
if (strcmp(udevice->dev->subsystem, "net") == 0 && strcmp(udevice->action, "add") == 0) {
dbg(udevice->udev, "netif add '%s'\n", udevice->dev->devpath);
udev_rules_get_name(rules, udevice);
if (udevice->ignore_device) {
info(udevice->udev, "device event will be ignored\n");
if (strcmp(udev_device_get_subsystem(dev), "net") == 0 && strcmp(udev_device_get_action(dev), "add") == 0) {
dbg(event->udev, "netif add '%s'\n", udev_device_get_devpath(dev));
udev_rules_get_name(rules, event);
if (event->ignore_device) {
info(event->udev, "device event will be ignored\n");
goto exit;
}
if (udevice->name[0] == '\0') {
info(udevice->udev, "device renaming supressed\n");
if (event->name[0] == '\0') {
info(event->udev, "device renaming supressed\n");
goto exit;
}
/* look if we want to change the name of the netif */
if (strcmp(udevice->name, udevice->dev->kernel) != 0) {
char devpath[PATH_MAX];
if (strcmp(event->name, udev_device_get_sysname(dev)) != 0) {
char syspath[UTIL_PATH_SIZE];
char *pos;
retval = rename_netif(udevice);
if (retval != 0)
err = rename_netif(event);
if (err != 0)
goto exit;
info(udevice->udev, "renamed netif to '%s'\n", udevice->name);
info(event->udev, "renamed netif to '%s'\n", event->name);
/* export old name */
setenv("INTERFACE_OLD", udevice->dev->kernel, 1);
/* remember old name */
udev_device_add_property(dev, "INTERFACE_OLD", udev_device_get_sysname(dev));
/* now change the devpath, because the kernel device name has changed */
util_strlcpy(devpath, udevice->dev->devpath, sizeof(devpath));
pos = strrchr(devpath, '/');
util_strlcpy(syspath, udev_device_get_syspath(dev), sizeof(syspath));
pos = strrchr(syspath, '/');
if (pos != NULL) {
pos[1] = '\0';
util_strlcat(devpath, udevice->name, sizeof(devpath));
sysfs_device_set_values(udevice->udev, udevice->dev, devpath, NULL, NULL);
setenv("DEVPATH", udevice->dev->devpath, 1);
setenv("INTERFACE", udevice->name, 1);
info(udevice->udev, "changed devpath to '%s'\n", udevice->dev->devpath);
util_strlcat(syspath, event->name, sizeof(syspath));
udev_device_set_syspath(event->dev, syspath);
udev_device_add_property(dev, "INTERFACE", udev_device_get_sysname(dev));
info(event->udev, "changed devpath to '%s'\n", udev_device_get_devpath(dev));
}
}
goto exit;
}
/* remove device node */
if (major(udevice->devt) != 0 && strcmp(udevice->action, "remove") == 0) {
struct name_entry *name_loop;
if (major(udev_device_get_devnum(dev)) != 0 && strcmp(udev_device_get_action(dev), "remove") == 0) {
/* import database entry and delete it */
udev_device_read_db(dev);
if (!event->test)
udev_device_delete_db(dev);
/* import database entry, and delete it */
if (udev_db_get_device(udevice, udevice->dev->devpath) == 0) {
udev_db_delete_device(udevice);
/* restore stored persistent data */
list_for_each_entry(name_loop, &udevice->env_list, node)
putenv(name_loop->name);
} else {
dbg(udevice->udev, "'%s' not found in database, using kernel name '%s'\n",
udevice->dev->devpath, udevice->dev->kernel);
util_strlcpy(udevice->name, udevice->dev->kernel, sizeof(udevice->name));
if (udev_device_get_devnode(dev) == NULL) {
char devnode[UTIL_PATH_SIZE];
info(event->udev, "'%s' not found in database, using kernel name '%s'\n",
udev_device_get_syspath(dev), udev_device_get_sysname(dev));
util_strlcpy(devnode, udev_get_dev_path(event->udev), sizeof(devnode));
util_strlcat(devnode, "/", sizeof(devnode));
util_strlcat(devnode, udev_device_get_sysname(dev), sizeof(devnode));
udev_device_set_devnode(dev, devnode);
}
udev_rules_get_run(rules, udevice);
if (udevice->ignore_device) {
info(udevice->udev, "device event will be ignored\n");
udev_rules_get_run(rules, event);
if (event->ignore_device) {
info(event->udev, "device event will be ignored\n");
goto exit;
}
if (udevice->ignore_remove) {
info(udevice->udev, "ignore_remove for '%s'\n", udevice->name);
if (udev_device_get_ignore_remove(dev)) {
info(event->udev, "ignore_remove for '%s'\n", udev_device_get_devnode(dev));
goto exit;
}
/* remove the node */
retval = udev_node_remove(udevice);
/* delete or restore symlinks according to priority */
udev_node_update_symlinks(udevice, NULL);
err = udev_node_remove(dev, event->test);
goto exit;
}
/* default devices */
udev_rules_get_run(rules, udevice);
if (udevice->ignore_device)
info(udevice->udev, "device event will be ignored\n");
udev_rules_get_run(rules, event);
if (event->ignore_device)
info(event->udev, "device event will be ignored\n");
exit:
return retval;
return err;
}

View File

@ -32,68 +32,108 @@
#define TMP_FILE_EXT ".udev-tmp"
int udev_node_mknod(struct udevice *udevice, const char *file, dev_t devt, mode_t mode, uid_t uid, gid_t gid)
/* reverse mapping from the device file name to the devpath */
static int name_index(struct udev *udev, const char *devpath, const char *name, int add, int test)
{
char device[UTIL_PATH_SIZE];
char filename[UTIL_PATH_SIZE * 2];
size_t devlen = strlen(udev_get_dev_path(udev))+1;
size_t start;
int fd;
/* directory with device name */
util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
start = util_strlcat(filename, "/.udev/names/", sizeof(filename));
util_strlcat(filename, &name[devlen], sizeof(filename));
util_path_encode(&filename[start], sizeof(filename) - start);
/* entry with the devpath */
util_strlcpy(device, devpath, sizeof(device));
util_path_encode(device, sizeof(device));
util_strlcat(filename, "/", sizeof(filename));
util_strlcat(filename, device, sizeof(filename));
if (add) {
info(udev, "creating index: '%s'\n", filename);
create_path(udev, filename);
fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (fd > 0)
close(fd);
} else {
info(udev, "removing index: '%s'\n", filename);
unlink(filename);
delete_path(udev, filename);
}
return 0;
}
int udev_node_mknod(struct udev_device *dev, const char *file, dev_t devnum, mode_t mode, uid_t uid, gid_t gid)
{
struct udev *udev = udev_device_get_udev(dev);
char file_tmp[UTIL_PATH_SIZE + sizeof(TMP_FILE_EXT)];
struct stat stats;
int preserve = 0;
int err = 0;
if (major(devt) != 0 && strcmp(udevice->dev->subsystem, "block") == 0)
if (major(devnum) == 0)
devnum = udev_device_get_devnum(dev);
if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
mode |= S_IFBLK;
else
mode |= S_IFCHR;
if (file == NULL)
file = udev_device_get_devnode(dev);
if (lstat(file, &stats) == 0) {
if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devt)) {
info(udevice->udev, "preserve file '%s', because it has correct dev_t\n", file);
if (((stats.st_mode & S_IFMT) == (mode & S_IFMT)) && (stats.st_rdev == devnum)) {
info(udev, "preserve file '%s', because it has correct dev_t\n", file);
preserve = 1;
udev_selinux_lsetfilecon(udevice->udev, file, mode);
udev_selinux_lsetfilecon(udev, file, mode);
} else {
info(udevice->udev, "atomically replace existing file '%s'\n", file);
info(udev, "atomically replace existing file '%s'\n", file);
util_strlcpy(file_tmp, file, sizeof(file_tmp));
util_strlcat(file_tmp, TMP_FILE_EXT, sizeof(file_tmp));
unlink(file_tmp);
udev_selinux_setfscreatecon(udevice->udev, file_tmp, mode);
err = mknod(file_tmp, mode, devt);
udev_selinux_resetfscreatecon(udevice->udev);
udev_selinux_setfscreatecon(udev, file_tmp, mode);
err = mknod(file_tmp, mode, devnum);
udev_selinux_resetfscreatecon(udev);
if (err != 0) {
err(udevice->udev, "mknod(%s, %#o, %u, %u) failed: %m\n",
file_tmp, mode, major(devt), minor(devt));
err(udev, "mknod(%s, %#o, %u, %u) failed: %m\n",
file_tmp, mode, major(devnum), minor(devnum));
goto exit;
}
err = rename(file_tmp, file);
if (err != 0) {
err(udevice->udev, "rename(%s, %s) failed: %m\n", file_tmp, file);
err(udev, "rename(%s, %s) failed: %m\n", file_tmp, file);
unlink(file_tmp);
}
}
} else {
info(udevice->udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devt), minor(devt));
udev_selinux_setfscreatecon(udevice->udev, file, mode);
err = mknod(file, mode, devt);
udev_selinux_resetfscreatecon(udevice->udev);
info(udev, "mknod(%s, %#o, (%u,%u))\n", file, mode, major(devnum), minor(devnum));
udev_selinux_setfscreatecon(udev, file, mode);
err = mknod(file, mode, devnum);
udev_selinux_resetfscreatecon(udev);
if (err != 0) {
err(udevice->udev, "mknod(%s, %#o, (%u,%u) failed: %m\n",
file, mode, major(devt), minor(devt));
err(udev, "mknod(%s, %#o, (%u,%u) failed: %m\n", file, mode, major(devnum), minor(devnum));
goto exit;
}
}
if (!preserve || stats.st_mode != mode) {
info(udevice->udev, "chmod(%s, %#o)\n", file, mode);
info(udev, "chmod(%s, %#o)\n", file, mode);
err = chmod(file, mode);
if (err != 0) {
err(udevice->udev, "chmod(%s, %#o) failed: %m\n", file, mode);
err(udev, "chmod(%s, %#o) failed: %m\n", file, mode);
goto exit;
}
}
if (!preserve || stats.st_uid != uid || stats.st_gid != gid) {
info(udevice->udev, "chown(%s, %u, %u)\n", file, uid, gid);
info(udev, "chown(%s, %u, %u)\n", file, uid, gid);
err = chown(file, uid, gid);
if (err != 0) {
err(udevice->udev, "chown(%s, %u, %u) failed: %m\n", file, uid, gid);
err(udev, "chown(%s, %u, %u) failed: %m\n", file, uid, gid);
goto exit;
}
}
@ -101,7 +141,7 @@ exit:
return err;
}
static int node_symlink(struct udevice *udevice, const char *node, const char *slink)
static int node_symlink(struct udev *udev, const char *node, const char *slink)
{
struct stat stats;
char target[UTIL_PATH_SIZE] = "";
@ -109,7 +149,7 @@ static int node_symlink(struct udevice *udevice, const char *node, const char *s
int i = 0;
int tail = 0;
int len;
int retval = 0;
int err = 0;
/* use relative link */
while (node[i] && (node[i] == slink[i])) {
@ -129,316 +169,357 @@ static int node_symlink(struct udevice *udevice, const char *node, const char *s
if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
struct stat stats2;
info(udevice->udev, "found existing node instead of symlink '%s'\n", slink);
info(udev, "found existing node instead of symlink '%s'\n", slink);
if (lstat(node, &stats2) == 0) {
if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
stats.st_rdev == stats2.st_rdev) {
info(udevice->udev, "replace device node '%s' with symlink to our node '%s'\n", slink, node);
info(udev, "replace device node '%s' with symlink to our node '%s'\n",
slink, node);
} else {
err(udevice->udev, "device node '%s' already exists, link to '%s' will not overwrite it\n", slink, node);
err(udev, "device node '%s' already exists, "
"link to '%s' will not overwrite it\n",
slink, node);
goto exit;
}
}
} else if (S_ISLNK(stats.st_mode)) {
char buf[UTIL_PATH_SIZE];
info(udevice->udev, "found existing symlink '%s'\n", slink);
info(udev, "found existing symlink '%s'\n", slink);
len = readlink(slink, buf, sizeof(buf));
if (len > 0) {
buf[len] = '\0';
if (strcmp(target, buf) == 0) {
info(udevice->udev, "preserve already existing symlink '%s' to '%s'\n", slink, target);
udev_selinux_lsetfilecon(udevice->udev, slink, S_IFLNK);
info(udev, "preserve already existing symlink '%s' to '%s'\n",
slink, target);
udev_selinux_lsetfilecon(udev, slink, S_IFLNK);
goto exit;
}
}
}
} else {
info(udevice->udev, "creating symlink '%s' to '%s'\n", slink, target);
udev_selinux_setfscreatecon(udevice->udev, slink, S_IFLNK);
retval = symlink(target, slink);
udev_selinux_resetfscreatecon(udevice->udev);
if (retval == 0)
info(udev, "creating symlink '%s' to '%s'\n", slink, target);
udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
err = symlink(target, slink);
udev_selinux_resetfscreatecon(udev);
if (err == 0)
goto exit;
}
info(udevice->udev, "atomically replace '%s'\n", slink);
info(udev, "atomically replace '%s'\n", slink);
util_strlcpy(slink_tmp, slink, sizeof(slink_tmp));
util_strlcat(slink_tmp, TMP_FILE_EXT, sizeof(slink_tmp));
unlink(slink_tmp);
udev_selinux_setfscreatecon(udevice->udev, slink, S_IFLNK);
retval = symlink(target, slink_tmp);
udev_selinux_resetfscreatecon(udevice->udev);
if (retval != 0) {
err(udevice->udev, "symlink(%s, %s) failed: %m\n", target, slink_tmp);
udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
err = symlink(target, slink_tmp);
udev_selinux_resetfscreatecon(udev);
if (err != 0) {
err(udev, "symlink(%s, %s) failed: %m\n", target, slink_tmp);
goto exit;
}
retval = rename(slink_tmp, slink);
if (retval != 0) {
err(udevice->udev, "rename(%s, %s) failed: %m\n", slink_tmp, slink);
err = rename(slink_tmp, slink);
if (err != 0) {
err(udev, "rename(%s, %s) failed: %m\n", slink_tmp, slink);
unlink(slink_tmp);
goto exit;
}
exit:
return retval;
return err;
}
static int update_link(struct udevice *udevice, const char *name)
static int get_devices_by_name(struct udev *udev, const char *name, struct list_head *name_list)
{
char dirname[PATH_MAX];
size_t devlen = strlen(udev_get_dev_path(udev))+1;
size_t start;
DIR *dir;
int count = 0;
util_strlcpy(dirname, udev_get_dev_path(udev), sizeof(dirname));
start = util_strlcat(dirname, "/.udev/names/", sizeof(dirname));
util_strlcat(dirname, &name[devlen], sizeof(dirname));
util_path_encode(&dirname[start], sizeof(dirname) - start);
dir = opendir(dirname);
if (dir == NULL) {
info(udev, "no index directory '%s': %m\n", dirname);
count = -1;
goto out;
}
info(udev, "found index directory '%s'\n", dirname);
while (1) {
struct dirent *ent;
char device[UTIL_PATH_SIZE];
ent = readdir(dir);
if (ent == NULL || ent->d_name[0] == '\0')
break;
if (ent->d_name[0] == '.')
continue;
util_strlcpy(device, udev_get_sys_path(udev), sizeof(device));
util_strlcat(device, ent->d_name, sizeof(device));
util_path_decode(device);
name_list_add(udev, name_list, device, 0);
count++;
}
closedir(dir);
out:
return count;
}
static int update_link(struct udev_device *dev, const char *slink, int test)
{
struct udev *udev = udev_device_get_udev(dev);
LIST_HEAD(name_list);
char slink[UTIL_PATH_SIZE];
char node[UTIL_PATH_SIZE];
struct udevice *udevice_db;
struct name_entry *device;
char target[UTIL_PATH_SIZE] = "";
int count;
int priority = 0;
int rc = 0;
util_strlcpy(slink, udev_get_dev_path(udevice->udev), sizeof(slink));
util_strlcat(slink, "/", sizeof(slink));
util_strlcat(slink, name, sizeof(slink));
info(udev, "update symlink '%s' of '%s'\n", slink, udev_device_get_syspath(dev));
count = udev_db_get_devices_by_name(udevice->udev, name, &name_list);
info(udevice->udev, "found %i devices with name '%s'\n", count, name);
count = get_devices_by_name(udev, slink, &name_list);
info(udev, "found %i devices with name '%s'\n", count, slink);
/* if we don't have a reference, delete it */
if (count <= 0) {
info(udevice->udev, "no reference left, remove '%s'\n", name);
if (!udevice->test_run) {
info(udev, "no reference left, remove '%s'\n", slink);
if (!test) {
unlink(slink);
delete_path(udevice->udev, slink);
delete_path(udev, slink);
}
goto out;
}
/* find the device with the highest priority */
list_for_each_entry(device, &name_list, node) {
info(udevice->udev, "found '%s' for '%s'\n", device->name, name);
struct udev_device *dev_db;
const char *devnode;
info(udev, "found '%s' for '%s'\n", device->name, slink);
/* did we find ourself? we win, if we have the same priority */
if (strcmp(udevice->dev->devpath, device->name) == 0) {
info(udevice->udev, "compare (our own) priority of '%s' %i >= %i\n",
udevice->dev->devpath, udevice->link_priority, priority);
if (strcmp(udevice->name, name) == 0) {
info(udevice->udev, "'%s' is our device node, database inconsistent, skip link update\n", udevice->name);
} else if (target[0] == '\0' || udevice->link_priority >= priority) {
priority = udevice->link_priority;
util_strlcpy(target, udevice->name, sizeof(target));
if (strcmp(udev_device_get_syspath(dev), device->name) == 0) {
info(udev, "compare (our own) priority of '%s' %i >= %i\n",
udev_device_get_devpath(dev), udev_device_get_devlink_priority(dev), priority);
if (strcmp(udev_device_get_devnode(dev), slink) == 0) {
info(udev, "'%s' is our device node, database inconsistent, skip link update\n",
udev_device_get_devnode(dev));
} else if (target[0] == '\0' || udev_device_get_devlink_priority(dev) >= priority) {
priority = udev_device_get_devlink_priority(dev);
util_strlcpy(target, udev_device_get_devnode(dev), sizeof(target));
}
continue;
}
/* another device, read priority from database */
udevice_db = udev_device_init(udevice->udev);
if (udevice_db == NULL)
dev_db = udev_device_new_from_syspath(udev, device->name);
if (dev_db == NULL)
continue;
if (udev_db_get_device(udevice_db, device->name) == 0) {
if (strcmp(udevice_db->name, name) == 0) {
info(udevice->udev, "'%s' is a device node of '%s', skip link update\n", udevice_db->name, device->name);
devnode = udev_device_get_devnode(dev_db);
if (devnode != NULL) {
if (strcmp(devnode, slink) == 0) {
info(udev, "'%s' is a device node of '%s', skip link update\n",
devnode, device->name);
} else {
info(udevice->udev, "compare priority of '%s' %i > %i\n",
udevice_db->dev->devpath, udevice_db->link_priority, priority);
if (target[0] == '\0' || udevice_db->link_priority > priority) {
priority = udevice_db->link_priority;
util_strlcpy(target, udevice_db->name, sizeof(target));
info(udev, "compare priority of '%s' %i > %i\n",
udev_device_get_devpath(dev_db),
udev_device_get_devlink_priority(dev_db),
priority);
if (target[0] == '\0' || udev_device_get_devlink_priority(dev_db) > priority) {
priority = udev_device_get_devlink_priority(dev_db);
util_strlcpy(target, devnode, sizeof(target));
}
}
}
udev_device_cleanup(udevice_db);
udev_device_unref(dev_db);
}
name_list_cleanup(udevice->udev, &name_list);
name_list_cleanup(udev, &name_list);
if (target[0] == '\0') {
info(udevice->udev, "no current target for '%s' found\n", name);
info(udev, "no current target for '%s' found\n", slink);
rc = 1;
goto out;
}
/* create symlink to the target with the highest priority */
util_strlcpy(node, udev_get_dev_path(udevice->udev), sizeof(node));
util_strlcat(node, "/", sizeof(node));
util_strlcat(node, target, sizeof(node));
info(udevice->udev, "'%s' with target '%s' has the highest priority %i, create it\n", name, target, priority);
if (!udevice->test_run) {
create_path(udevice->udev, slink);
node_symlink(udevice, node, slink);
info(udev, "'%s' with target '%s' has the highest priority %i, create it\n", slink, target, priority);
if (!test) {
create_path(udev, slink);
node_symlink(udev, target, slink);
}
out:
return rc;
}
void udev_node_update_symlinks(struct udevice *udevice, struct udevice *udevice_old)
void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old, int test)
{
struct name_entry *name_loop;
char symlinks[UTIL_PATH_SIZE] = "";
struct udev *udev = udev_device_get_udev(dev);
struct udev_list_entry *list_entry;
const char *devnode_old;
list_for_each_entry(name_loop, &udevice->symlink_list, node) {
info(udevice->udev, "update symlink '%s' of '%s'\n", name_loop->name, udevice->dev->devpath);
update_link(udevice, name_loop->name);
util_strlcat(symlinks, udev_get_dev_path(udevice->udev), sizeof(symlinks));
util_strlcat(symlinks, "/", sizeof(symlinks));
util_strlcat(symlinks, name_loop->name, sizeof(symlinks));
util_strlcat(symlinks, " ", sizeof(symlinks));
/* update possible left-over symlinks */
udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev_old)) {
struct udev_list_entry *list_entry_current;
udev_list_entry_foreach(list_entry_current, udev_device_get_devlinks_list_entry(dev)) {
if (strcmp(udev_list_entry_get_name(list_entry_current),
udev_list_entry_get_name(list_entry)) == 0)
continue;
}
/* link does no longer belong to this device */
info(udev, "update old symlink '%s' no longer belonging to '%s'\n",
udev_list_entry_get_name(list_entry), udev_device_get_devpath(dev));
update_link(dev, udev_list_entry_get_name(list_entry), test);
}
/* export symlinks to environment */
util_remove_trailing_chars(symlinks, ' ');
if (symlinks[0] != '\0')
setenv("DEVLINKS", symlinks, 1);
/*
* if the node name has changed, delete the node,
* and possibly restore a symlink of another device
*/
devnode_old = udev_device_get_devnode(dev_old);
if (devnode_old != NULL) {
const char *devnode = udev_device_get_devnode(dev);
/* update possible left-over symlinks (device metadata changed) */
if (udevice_old != NULL) {
struct name_entry *link_loop;
struct name_entry *link_old_loop;
int found;
/* remove current symlinks from old list */
list_for_each_entry(link_old_loop, &udevice_old->symlink_list, node) {
found = 0;
list_for_each_entry(link_loop, &udevice->symlink_list, node) {
if (strcmp(link_old_loop->name, link_loop->name) == 0) {
found = 1;
break;
}
}
if (!found) {
/* link does no longer belong to this device */
info(udevice->udev, "update old symlink '%s' no longer belonging to '%s'\n",
link_old_loop->name, udevice->dev->devpath);
update_link(udevice, link_old_loop->name);
}
}
/*
* if the node name has changed, delete the node,
* or possibly restore a symlink of another device
*/
if (strcmp(udevice->name, udevice_old->name) != 0)
update_link(udevice, udevice_old->name);
if (devnode != NULL && strcmp(devnode_old, devnode) != 0)
update_link(dev, devnode_old, test);
}
}
int udev_node_add(struct udevice *udevice)
int udev_node_add(struct udev_device *dev, mode_t mode, const char *owner, const char *group, int test)
{
char filename[UTIL_PATH_SIZE];
struct udev *udev = udev_device_get_udev(dev);
uid_t uid;
gid_t gid;
int i;
int retval = 0;
int num;
struct udev_list_entry *list_entry;
int err = 0;
util_strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
util_strlcat(filename, "/", sizeof(filename));
util_strlcat(filename, udevice->name, sizeof(filename));
create_path(udevice->udev, filename);
create_path(udev, udev_device_get_devnode(dev));
if (strcmp(udevice->owner, "root") == 0)
if (strcmp(owner, "root") == 0)
uid = 0;
else {
char *endptr;
unsigned long id;
id = strtoul(udevice->owner, &endptr, 10);
id = strtoul(owner, &endptr, 10);
if (endptr[0] == '\0')
uid = (uid_t) id;
else
uid = lookup_user(udevice->udev, udevice->owner);
uid = lookup_user(udev, owner);
}
if (strcmp(udevice->group, "root") == 0)
if (strcmp(group, "root") == 0)
gid = 0;
else {
char *endptr;
unsigned long id;
id = strtoul(udevice->group, &endptr, 10);
id = strtoul(group, &endptr, 10);
if (endptr[0] == '\0')
gid = (gid_t) id;
else
gid = lookup_group(udevice->udev, udevice->group);
gid = lookup_group(udev, group);
}
info(udevice->udev, "creating device node '%s', major=%d, minor=%d, mode=%#o, uid=%d, gid=%d\n",
filename, major(udevice->devt), minor(udevice->devt), udevice->mode, uid, gid);
info(udev, "creating device node '%s', devnum=%d:%d, mode=%#o, uid=%d, gid=%d\n",
udev_device_get_devnode(dev),
major(udev_device_get_devnum(dev)), minor(udev_device_get_devnum(dev)),
mode, uid, gid);
if (!udevice->test_run)
if (udev_node_mknod(udevice, filename, udevice->devt, udevice->mode, uid, gid) != 0) {
retval = -1;
if (!test)
if (udev_node_mknod(dev, NULL, makedev(0,0), mode, uid, gid) != 0) {
err = -1;
goto exit;
}
setenv("DEVNAME", filename, 1);
/* create all_partitions if requested */
if (udevice->partitions) {
char partitionname[UTIL_PATH_SIZE];
char *attr;
int range;
num = udev_device_get_num_fake_partitions(dev);
if (num > 0) {
info(udev, "creating device partition nodes '%s[1-%i]'\n", udev_device_get_devnode(dev), num);
if (!test) {
for (i = 1; i <= num; i++) {
char partitionname[UTIL_PATH_SIZE];
dev_t part_devnum;
/* take the maximum registered minor range */
attr = sysfs_attr_get_value(udevice->udev, udevice->dev->devpath, "range");
if (attr != NULL) {
range = atoi(attr);
if (range > 1)
udevice->partitions = range-1;
}
info(udevice->udev, "creating device partition nodes '%s[1-%i]'\n", filename, udevice->partitions);
if (!udevice->test_run) {
for (i = 1; i <= udevice->partitions; i++) {
dev_t part_devt;
snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
snprintf(partitionname, sizeof(partitionname), "%s%d",
udev_device_get_devnode(dev), i);
partitionname[sizeof(partitionname)-1] = '\0';
part_devt = makedev(major(udevice->devt), minor(udevice->devt) + i);
udev_node_mknod(udevice, partitionname, part_devt, udevice->mode, uid, gid);
part_devnum = makedev(major(udev_device_get_devnum(dev)),
minor(udev_device_get_devnum(dev)) + i);
udev_node_mknod(dev, partitionname, part_devnum, mode, uid, gid);
}
}
}
/* add node and to name index */
name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 1, test);
/* create/update symlinks, add symlinks to name index */
udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 1, test);
update_link(dev, udev_list_entry_get_name(list_entry), test);
}
exit:
return retval;
return err;
}
int udev_node_remove(struct udevice *udevice)
extern int udev_node_remove(struct udev_device *dev, int test)
{
char filename[UTIL_PATH_SIZE];
struct udev *udev = udev_device_get_udev(dev);
struct udev_list_entry *list_entry;
const char *devnode;
char partitionname[UTIL_PATH_SIZE];
struct stat stats;
int retval = 0;
int err = 0;
int num;
util_strlcpy(filename, udev_get_dev_path(udevice->udev), sizeof(filename));
util_strlcat(filename, "/", sizeof(filename));
util_strlcat(filename, udevice->name, sizeof(filename));
if (stat(filename, &stats) != 0) {
info(udevice->udev, "device node '%s' not found\n", filename);
/* remove node from name index */
name_index(udev, udev_device_get_devpath(dev), udev_device_get_devnode(dev), 0, test);
/* remove,update symlinks, remove symlinks from name index */
udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(dev)) {
name_index(udev, udev_device_get_devpath(dev), udev_list_entry_get_name(list_entry), 0, test);
update_link(dev, udev_list_entry_get_name(list_entry), test);
}
devnode = udev_device_get_devnode(dev);
if (devnode == NULL)
return 0;
if (stat(devnode, &stats) != 0) {
info(udev, "device node '%s' not found\n", devnode);
return 0;
}
if (udevice->devt && stats.st_rdev != udevice->devt) {
info(udevice->udev, "device node '%s' points to a different device, skip removal\n", filename);
if (stats.st_rdev != udev_device_get_devnum(dev)) {
info(udev, "device node '%s' points to a different device, skip removal\n", devnode);
return -1;
}
info(udevice->udev, "removing device node '%s'\n", filename);
if (!udevice->test_run)
retval = unlink_secure(udevice->udev, filename);
if (retval)
return retval;
info(udev, "removing device node '%s'\n", devnode);
if (!test)
err = unlink_secure(udev, devnode);
if (err)
return err;
setenv("DEVNAME", filename, 1);
num = udevice->partitions;
num = udev_device_get_num_fake_partitions(dev);
if (num > 0) {
int i;
info(udevice->udev, "removing all_partitions '%s[1-%i]'\n", filename, num);
info(udev, "removing all_partitions '%s[1-%i]'\n", devnode, num);
if (num > 255)
return -1;
for (i = 1; i <= num; i++) {
snprintf(partitionname, sizeof(partitionname), "%s%d", filename, i);
snprintf(partitionname, sizeof(partitionname), "%s%d", devnode, i);
partitionname[sizeof(partitionname)-1] = '\0';
if (!udevice->test_run)
unlink_secure(udevice->udev, partitionname);
if (!test)
unlink_secure(udev, partitionname);
}
}
delete_path(udevice->udev, filename);
return retval;
delete_path(udev, devnode);
return err;
}

File diff suppressed because it is too large Load Diff

View File

@ -125,10 +125,10 @@ extern void udev_rules_iter_init(struct udev_rules_iter *iter, struct udev_rules
extern struct udev_rule *udev_rules_iter_next(struct udev_rules_iter *iter);
extern struct udev_rule *udev_rules_iter_goto(struct udev_rules_iter *iter, size_t rule_off);
extern int udev_rules_get_name(struct udev_rules *rules, struct udevice *udev);
extern int udev_rules_get_run(struct udev_rules *rules, struct udevice *udev);
extern int udev_rules_run(struct udevice *udev);
extern int udev_rules_get_name(struct udev_rules *rules, struct udev_event *event);
extern int udev_rules_get_run(struct udev_rules *rules, struct udev_event *event);
extern int udev_rules_run(struct udev_event *event);
extern void udev_rules_apply_format(struct udevice *udev, char *string, size_t maxsize);
extern void udev_rules_apply_format(struct udev_event *event, char *string, size_t maxsize);
#endif

View File

@ -1,499 +0,0 @@
/*
* Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include "udev.h"
/* device cache */
static LIST_HEAD(dev_list);
/* attribute value cache */
static LIST_HEAD(attr_list);
struct sysfs_attr {
struct list_head node;
char path[UTIL_PATH_SIZE];
char *value; /* points to value_local if value is cached */
char value_local[UTIL_NAME_SIZE];
};
static int resolve_sys_link(struct udev *udev, char *path, size_t size)
{
char link_path[UTIL_PATH_SIZE];
char link_target[UTIL_PATH_SIZE];
int len;
int i;
int back;
util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
util_strlcat(link_path, path, sizeof(link_path));
len = readlink(link_path, link_target, sizeof(link_target));
if (len <= 0)
return -1;
link_target[len] = '\0';
dbg(udev, "path link '%s' points to '%s'\n", path, link_target);
for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
;
dbg(udev, "base '%s', tail '%s', back %i\n", path, &link_target[back * 3], back);
for (i = 0; i <= back; i++) {
char *pos = strrchr(path, '/');
if (pos == NULL)
return -1;
pos[0] = '\0';
}
dbg(udev, "after moving back '%s'\n", path);
util_strlcat(path, "/", size);
util_strlcat(path, &link_target[back * 3], size);
return 0;
}
int sysfs_init(void)
{
INIT_LIST_HEAD(&dev_list);
INIT_LIST_HEAD(&attr_list);
return 0;
}
void sysfs_cleanup(void)
{
struct sysfs_attr *attr_loop;
struct sysfs_attr *attr_temp;
struct sysfs_device *dev_loop;
struct sysfs_device *dev_temp;
list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
list_del(&attr_loop->node);
free(attr_loop);
}
list_for_each_entry_safe(dev_loop, dev_temp, &dev_list, node) {
list_del(&dev_loop->node);
free(dev_loop);
}
}
void sysfs_device_set_values(struct udev *udev,
struct sysfs_device *dev, const char *devpath,
const char *subsystem, const char *driver)
{
char *pos;
util_strlcpy(dev->devpath, devpath, sizeof(dev->devpath));
if (subsystem != NULL)
util_strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
if (driver != NULL)
util_strlcpy(dev->driver, driver, sizeof(dev->driver));
/* set kernel name */
pos = strrchr(dev->devpath, '/');
if (pos == NULL)
return;
util_strlcpy(dev->kernel, &pos[1], sizeof(dev->kernel));
dbg(udev, "kernel='%s'\n", dev->kernel);
/* some devices have '!' in their name, change that to '/' */
pos = dev->kernel;
while (pos[0] != '\0') {
if (pos[0] == '!')
pos[0] = '/';
pos++;
}
/* get kernel number */
pos = &dev->kernel[strlen(dev->kernel)];
while (isdigit(pos[-1]))
pos--;
util_strlcpy(dev->kernel_number, pos, sizeof(dev->kernel_number));
dbg(udev, "kernel_number='%s'\n", dev->kernel_number);
}
struct sysfs_device *sysfs_device_get(struct udev *udev, const char *devpath)
{
char path[UTIL_PATH_SIZE];
char devpath_real[UTIL_PATH_SIZE];
struct sysfs_device *dev;
struct sysfs_device *dev_loop;
struct stat statbuf;
char link_path[UTIL_PATH_SIZE];
char link_target[UTIL_PATH_SIZE];
int len;
char *pos;
/* we handle only these devpathes */
if (devpath != NULL &&
strncmp(devpath, "/devices/", 9) != 0 &&
strncmp(devpath, "/subsystem/", 11) != 0 &&
strncmp(devpath, "/module/", 8) != 0 &&
strncmp(devpath, "/bus/", 5) != 0 &&
strncmp(devpath, "/class/", 7) != 0 &&
strncmp(devpath, "/block/", 7) != 0)
return NULL;
dbg(udev, "open '%s'\n", devpath);
util_strlcpy(devpath_real, devpath, sizeof(devpath_real));
util_remove_trailing_chars(devpath_real, '/');
if (devpath[0] == '\0' )
return NULL;
/* look for device already in cache (we never put an untranslated path in the cache) */
list_for_each_entry(dev_loop, &dev_list, node) {
if (strcmp(dev_loop->devpath, devpath_real) == 0) {
dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
return dev_loop;
}
}
/* if we got a link, resolve it to the real device */
util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
util_strlcat(path, devpath_real, sizeof(path));
if (lstat(path, &statbuf) != 0) {
dbg(udev, "stat '%s' failed: %m\n", path);
return NULL;
}
if (S_ISLNK(statbuf.st_mode)) {
if (resolve_sys_link(udev, devpath_real, sizeof(devpath_real)) != 0)
return NULL;
/* now look for device in cache after path translation */
list_for_each_entry(dev_loop, &dev_list, node) {
if (strcmp(dev_loop->devpath, devpath_real) == 0) {
dbg(udev, "found in cache '%s'\n", dev_loop->devpath);
return dev_loop;
}
}
}
/* it is a new device */
dbg(udev, "new uncached device '%s'\n", devpath_real);
dev = malloc(sizeof(struct sysfs_device));
if (dev == NULL)
return NULL;
memset(dev, 0x00, sizeof(struct sysfs_device));
sysfs_device_set_values(udev, dev, devpath_real, NULL, NULL);
/* get subsystem name */
util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
util_strlcat(link_path, dev->devpath, sizeof(link_path));
util_strlcat(link_path, "/subsystem", sizeof(link_path));
len = readlink(link_path, link_target, sizeof(link_target));
if (len > 0) {
/* get subsystem from "subsystem" link */
link_target[len] = '\0';
dbg(udev, "subsystem link '%s' points to '%s'\n", link_path, link_target);
pos = strrchr(link_target, '/');
if (pos != NULL)
util_strlcpy(dev->subsystem, &pos[1], sizeof(dev->subsystem));
} else if (strstr(dev->devpath, "/drivers/") != NULL) {
util_strlcpy(dev->subsystem, "drivers", sizeof(dev->subsystem));
} else if (strncmp(dev->devpath, "/module/", 8) == 0) {
util_strlcpy(dev->subsystem, "module", sizeof(dev->subsystem));
} else if (strncmp(dev->devpath, "/subsystem/", 11) == 0) {
pos = strrchr(dev->devpath, '/');
if (pos == &dev->devpath[10])
util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
} else if (strncmp(dev->devpath, "/class/", 7) == 0) {
pos = strrchr(dev->devpath, '/');
if (pos == &dev->devpath[6])
util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
} else if (strncmp(dev->devpath, "/bus/", 5) == 0) {
pos = strrchr(dev->devpath, '/');
if (pos == &dev->devpath[4])
util_strlcpy(dev->subsystem, "subsystem", sizeof(dev->subsystem));
}
/* get driver name */
util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
util_strlcat(link_path, dev->devpath, sizeof(link_path));
util_strlcat(link_path, "/driver", sizeof(link_path));
len = readlink(link_path, link_target, sizeof(link_target));
if (len > 0) {
link_target[len] = '\0';
dbg(udev, "driver link '%s' points to '%s'\n", link_path, link_target);
pos = strrchr(link_target, '/');
if (pos != NULL)
util_strlcpy(dev->driver, &pos[1], sizeof(dev->driver));
}
dbg(udev, "add to cache 'devpath=%s', subsystem='%s', driver='%s'\n", dev->devpath, dev->subsystem, dev->driver);
list_add(&dev->node, &dev_list);
return dev;
}
struct sysfs_device *sysfs_device_get_parent(struct udev *udev, struct sysfs_device *dev)
{
char parent_devpath[UTIL_PATH_SIZE];
char *pos;
dbg(udev, "open '%s'\n", dev->devpath);
/* look if we already know the parent */
if (dev->parent != NULL)
return dev->parent;
util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
dbg(udev, "'%s'\n", parent_devpath);
/* strip last element */
pos = strrchr(parent_devpath, '/');
if (pos == NULL || pos == parent_devpath)
return NULL;
pos[0] = '\0';
if (strncmp(parent_devpath, "/class", 6) == 0) {
pos = strrchr(parent_devpath, '/');
if (pos == &parent_devpath[6] || pos == parent_devpath) {
dbg(udev, "/class top level, look for device link\n");
goto device_link;
}
}
if (strcmp(parent_devpath, "/block") == 0) {
dbg(udev, "/block top level, look for device link\n");
goto device_link;
}
/* are we at the top level? */
pos = strrchr(parent_devpath, '/');
if (pos == NULL || pos == parent_devpath)
return NULL;
/* get parent and remember it */
dev->parent = sysfs_device_get(udev, parent_devpath);
return dev->parent;
device_link:
util_strlcpy(parent_devpath, dev->devpath, sizeof(parent_devpath));
util_strlcat(parent_devpath, "/device", sizeof(parent_devpath));
if (resolve_sys_link(udev, parent_devpath, sizeof(parent_devpath)) != 0)
return NULL;
/* get parent and remember it */
dev->parent = sysfs_device_get(udev, parent_devpath);
return dev->parent;
}
struct sysfs_device *sysfs_device_get_parent_with_subsystem(struct udev *udev, struct sysfs_device *dev, const char *subsystem)
{
struct sysfs_device *dev_parent;
dev_parent = sysfs_device_get_parent(udev, dev);
while (dev_parent != NULL) {
if (strcmp(dev_parent->subsystem, subsystem) == 0)
return dev_parent;
dev_parent = sysfs_device_get_parent(udev, dev_parent);
}
return NULL;
}
char *sysfs_attr_get_value(struct udev *udev, const char *devpath, const char *attr_name)
{
char path_full[UTIL_PATH_SIZE];
const char *path;
char value[UTIL_NAME_SIZE];
struct sysfs_attr *attr_loop;
struct sysfs_attr *attr;
struct stat statbuf;
int fd;
ssize_t size;
size_t sysfs_len;
dbg(udev, "open '%s'/'%s'\n", devpath, attr_name);
sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
if(sysfs_len >= sizeof(path_full))
sysfs_len = sizeof(path_full) - 1;
path = &path_full[sysfs_len];
util_strlcat(path_full, devpath, sizeof(path_full));
util_strlcat(path_full, "/", sizeof(path_full));
util_strlcat(path_full, attr_name, sizeof(path_full));
/* look for attribute in cache */
list_for_each_entry(attr_loop, &attr_list, node) {
if (strcmp(attr_loop->path, path) == 0) {
dbg(udev, "found in cache '%s'\n", attr_loop->path);
return attr_loop->value;
}
}
/* store attribute in cache (also negatives are kept in cache) */
dbg(udev, "new uncached attribute '%s'\n", path_full);
attr = malloc(sizeof(struct sysfs_attr));
if (attr == NULL)
return NULL;
memset(attr, 0x00, sizeof(struct sysfs_attr));
util_strlcpy(attr->path, path, sizeof(attr->path));
dbg(udev, "add to cache '%s'\n", path_full);
list_add(&attr->node, &attr_list);
if (lstat(path_full, &statbuf) != 0) {
dbg(udev, "stat '%s' failed: %m\n", path_full);
goto out;
}
if (S_ISLNK(statbuf.st_mode)) {
/* links return the last element of the target path */
char link_target[UTIL_PATH_SIZE];
int len;
const char *pos;
len = readlink(path_full, link_target, sizeof(link_target));
if (len > 0) {
link_target[len] = '\0';
pos = strrchr(link_target, '/');
if (pos != NULL) {
dbg(udev, "cache '%s' with link value '%s'\n", path_full, value);
util_strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
attr->value = attr->value_local;
}
}
goto out;
}
/* skip directories */
if (S_ISDIR(statbuf.st_mode))
goto out;
/* skip non-readable files */
if ((statbuf.st_mode & S_IRUSR) == 0)
goto out;
/* read attribute value */
fd = open(path_full, O_RDONLY);
if (fd < 0) {
dbg(udev, "attribute '%s' can not be opened\n", path_full);
goto out;
}
size = read(fd, value, sizeof(value));
close(fd);
if (size < 0)
goto out;
if (size == sizeof(value))
goto out;
/* got a valid value, store and return it */
value[size] = '\0';
util_remove_trailing_chars(value, '\n');
dbg(udev, "cache '%s' with attribute value '%s'\n", path_full, value);
util_strlcpy(attr->value_local, value, sizeof(attr->value_local));
attr->value = attr->value_local;
out:
return attr->value;
}
int sysfs_lookup_devpath_by_subsys_id(struct udev *udev, char *devpath_full, size_t len, const char *subsystem, const char *id)
{
size_t sysfs_len;
char path_full[UTIL_PATH_SIZE];
char *path;
struct stat statbuf;
sysfs_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
path = &path_full[sysfs_len];
if (strcmp(subsystem, "subsystem") == 0) {
util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
util_strlcat(path, id, sizeof(path_full) - sysfs_len);
if (stat(path_full, &statbuf) == 0)
goto found;
util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
util_strlcat(path, id, sizeof(path_full) - sysfs_len);
if (stat(path_full, &statbuf) == 0)
goto found;
goto out;
util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
util_strlcat(path, id, sizeof(path_full) - sysfs_len);
if (stat(path_full, &statbuf) == 0)
goto found;
}
if (strcmp(subsystem, "module") == 0) {
util_strlcpy(path, "/module/", sizeof(path_full) - sysfs_len);
util_strlcat(path, id, sizeof(path_full) - sysfs_len);
if (stat(path_full, &statbuf) == 0)
goto found;
goto out;
}
if (strcmp(subsystem, "drivers") == 0) {
char subsys[UTIL_NAME_SIZE];
char *driver;
util_strlcpy(subsys, id, sizeof(subsys));
driver = strchr(subsys, ':');
if (driver != NULL) {
driver[0] = '\0';
driver = &driver[1];
util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
util_strlcat(path, subsys, sizeof(path_full) - sysfs_len);
util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
util_strlcat(path, driver, sizeof(path_full) - sysfs_len);
if (stat(path_full, &statbuf) == 0)
goto found;
util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
util_strlcat(path, subsys, sizeof(path_full) - sysfs_len);
util_strlcat(path, "/drivers/", sizeof(path_full) - sysfs_len);
util_strlcat(path, driver, sizeof(path_full) - sysfs_len);
if (stat(path_full, &statbuf) == 0)
goto found;
}
goto out;
}
util_strlcpy(path, "/subsystem/", sizeof(path_full) - sysfs_len);
util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
util_strlcat(path, id, sizeof(path_full) - sysfs_len);
if (stat(path_full, &statbuf) == 0)
goto found;
util_strlcpy(path, "/bus/", sizeof(path_full) - sysfs_len);
util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
util_strlcat(path, "/devices/", sizeof(path_full) - sysfs_len);
util_strlcat(path, id, sizeof(path_full) - sysfs_len);
if (stat(path_full, &statbuf) == 0)
goto found;
util_strlcpy(path, "/class/", sizeof(path_full) - sysfs_len);
util_strlcat(path, subsystem, sizeof(path_full) - sysfs_len);
util_strlcat(path, "/", sizeof(path_full) - sysfs_len);
util_strlcat(path, id, sizeof(path_full) - sysfs_len);
if (stat(path_full, &statbuf) == 0)
goto found;
out:
return 0;
found:
if (S_ISLNK(statbuf.st_mode))
resolve_sys_link(udev, path, sizeof(path_full) - sysfs_len);
util_strlcpy(devpath_full, path, len);
return 1;
}

View File

@ -141,15 +141,15 @@ static void print_record(struct udev_device *device)
if (str != NULL)
printf("N: %s\n", &str[len+1]);
i = device_get_devlink_priority(device);
i = udev_device_get_devlink_priority(device);
if (i != 0)
printf("L: %i\n", i);
i = device_get_num_fake_partitions(device);
i = udev_device_get_num_fake_partitions(device);
if (i != 0)
printf("A:%u\n", i);
i = device_get_ignore_remove(device);
i = udev_device_get_ignore_remove(device);
if (i != 0)
printf("R:%u\n", i);

View File

@ -31,60 +31,20 @@
#include "udev.h"
#include "udev_rules.h"
static int import_uevent_var(struct udev *udev, const char *devpath)
{
char path[UTIL_PATH_SIZE];
static char value[4096]; /* must stay, used with putenv */
ssize_t size;
int fd;
char *key;
char *next;
int rc = -1;
/* read uevent file */
util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
util_strlcat(path, devpath, sizeof(path));
util_strlcat(path, "/uevent", sizeof(path));
fd = open(path, O_RDONLY);
if (fd < 0)
goto out;
size = read(fd, value, sizeof(value));
close(fd);
if (size < 0)
goto out;
value[size] = '\0';
/* import keys into environment */
key = value;
while (key[0] != '\0') {
next = strchr(key, '\n');
if (next == NULL)
goto out;
next[0] = '\0';
info(udev, "import into environment: '%s'\n", key);
putenv(key);
key = &next[1];
}
rc = 0;
out:
return rc;
}
int udevadm_test(struct udev *udev, int argc, char *argv[])
{
char filename[UTIL_PATH_SIZE];
int force = 0;
const char *action = "add";
const char *subsystem = NULL;
const char *devpath = NULL;
struct udevice *udevice;
struct sysfs_device *dev;
const char *syspath = NULL;
struct udev_event *event;
struct udev_device *dev;
struct udev_rules rules = {};
int retval;
int err;
int rc = 0;
static const struct option options[] = {
{ "action", required_argument, NULL, 'a' },
{ "subsystem", required_argument, NULL, 's' },
{ "force", no_argument, NULL, 'f' },
{ "help", no_argument, NULL, 'h' },
{}
@ -92,14 +52,6 @@ int udevadm_test(struct udev *udev, int argc, char *argv[])
info(udev, "version %s\n", VERSION);
/* export log priority to executed programs */
if (udev_get_log_priority(udev) > 0) {
char priority[32];
sprintf(priority, "%i", udev_get_log_priority(udev));
setenv("UDEV_LOG", priority, 1);
}
while (1) {
int option;
@ -112,16 +64,12 @@ int udevadm_test(struct udev *udev, int argc, char *argv[])
case 'a':
action = optarg;
break;
case 's':
subsystem = optarg;
break;
case 'f':
force = 1;
break;
case 'h':
printf("Usage: udevadm test OPTIONS <devpath>\n"
printf("Usage: udevadm test OPTIONS <syspath>\n"
" --action=<string> set action string\n"
" --subsystem=<string> set subsystem string\n"
" --force don't skip node/link creation\n"
" --help print this help text\n\n");
exit(0);
@ -129,10 +77,10 @@ int udevadm_test(struct udev *udev, int argc, char *argv[])
exit(1);
}
}
devpath = argv[optind];
syspath = argv[optind];
if (devpath == NULL) {
fprintf(stderr, "devpath parameter missing\n");
if (syspath == NULL) {
fprintf(stderr, "syspath parameter missing\n");
rc = 1;
goto exit;
}
@ -144,60 +92,49 @@ int udevadm_test(struct udev *udev, int argc, char *argv[])
udev_rules_init(udev, &rules, 0);
/* remove /sys if given */
if (strncmp(devpath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) == 0)
devpath = &devpath[strlen(udev_get_sys_path(udev))];
/* add /sys if needed */
if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) {
util_strlcpy(filename, udev_get_sys_path(udev), sizeof(filename));
util_strlcat(filename, syspath, sizeof(filename));
syspath = filename;
}
dev = sysfs_device_get(udev, devpath);
dev = udev_device_new_from_syspath(udev, syspath);
if (dev == NULL) {
fprintf(stderr, "unable to open device '%s'\n", devpath);
fprintf(stderr, "unable to open device '%s'\n", syspath);
rc = 2;
goto exit;
}
udevice = udev_device_init(udev);
if (udevice == NULL) {
fprintf(stderr, "error initializing device\n");
rc = 3;
goto exit;
}
/* skip reading of db, but read kernel parameters */
udev_device_set_info_loaded(dev);
udev_device_read_uevent_file(dev);
if (subsystem != NULL)
util_strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
/* override built-in sysfs device */
udevice->dev = dev;
util_strlcpy(udevice->action, action, sizeof(udevice->action));
udevice->devt = udev_device_get_devt(udevice);
udev_device_set_action(dev, action);
event = udev_event_new(dev);
/* simulate node creation with test flag */
if (!force)
udevice->test_run = 1;
event->test = 1;
setenv("DEVPATH", udevice->dev->devpath, 1);
setenv("SUBSYSTEM", udevice->dev->subsystem, 1);
setenv("ACTION", udevice->action, 1);
import_uevent_var(udev, udevice->dev->devpath);
err = udev_event_run(event, &rules);
info(udev, "looking at device '%s' from subsystem '%s'\n", udevice->dev->devpath, udevice->dev->subsystem);
retval = udev_device_event(&rules, udevice);
if (udev_device_get_event_timeout(dev) >= 0)
info(udev, "custom event timeout: %i\n", udev_device_get_event_timeout(dev));
if (udevice->event_timeout >= 0)
info(udev, "custom event timeout: %i\n", udevice->event_timeout);
if (err == 0 && !event->ignore_device && udev_get_run(udev)) {
struct udev_list_entry *entry;
if (retval == 0 && !udevice->ignore_device && udev_get_run(udev)) {
struct name_entry *name_loop;
list_for_each_entry(name_loop, &udevice->run_list, node) {
udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) {
char program[UTIL_PATH_SIZE];
util_strlcpy(program, name_loop->name, sizeof(program));
udev_rules_apply_format(udevice, program, sizeof(program));
util_strlcpy(program, udev_list_entry_get_name(entry), sizeof(program));
udev_rules_apply_format(event, program, sizeof(program));
info(udev, "run: '%s'\n", program);
}
}
udev_device_cleanup(udevice);
udev_event_unref(event);
udev_device_unref(dev);
exit:
udev_rules_cleanup(&rules);
return rc;

View File

@ -141,7 +141,6 @@ int main(int argc, char *argv[])
logging_init("udevadm");
udev_set_log_fn(udev, log_fn);
selinux_init(udev);
sysfs_init();
/* see if we are a compat link, this will be removed in a future release */
command = argv[0];
@ -211,7 +210,6 @@ int main(int argc, char *argv[])
help(udev, argc, argv);
rc = 2;
out:
sysfs_cleanup();
selinux_exit(udev);
udev_unref(udev);
logging_close();

View File

@ -33,11 +33,8 @@
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/netlink.h>
#ifdef HAVE_INOTIFY
#include <sys/inotify.h>
#endif
@ -65,29 +62,10 @@ static void log_fn(struct udev *udev, int priority,
}
}
struct udevd_uevent_msg {
struct udev *udev;
struct list_head node;
pid_t pid;
int exitstatus;
time_t queue_time;
char *action;
char *devpath;
char *subsystem;
char *driver;
dev_t devt;
unsigned long long seqnum;
char *devpath_old;
char *physdevpath;
unsigned int timeout;
char *envp[UEVENT_NUM_ENVP+1];
char envbuf[];
};
static int debug_trace;
static struct udev_rules rules;
static struct udev_ctrl *udev_ctrl;
static int uevent_netlink_sock = -1;
static struct udev_monitor *kernel_monitor;
static int inotify_fd = -1;
static int signal_pipe[2] = {-1, -1};
@ -97,156 +75,108 @@ static volatile int reload_config;
static int run_exec_q;
static int stop_exec_q;
static int max_childs;
static char udev_log_env[32];
static LIST_HEAD(exec_list);
static LIST_HEAD(running_list);
static void asmlinkage udev_event_sig_handler(int signum)
{
if (signum == SIGALRM)
exit(1);
}
static int udev_event_process(struct udevd_uevent_msg *msg)
{
struct sigaction act;
struct udevice *udevice;
int i;
int retval;
/* set signal handlers */
memset(&act, 0x00, sizeof(act));
act.sa_handler = (void (*)(int)) udev_event_sig_handler;
sigemptyset (&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGALRM, &act, NULL);
/* reset to default */
act.sa_handler = SIG_DFL;
sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
sigaction(SIGCHLD, &act, NULL);
sigaction(SIGHUP, &act, NULL);
/* trigger timeout to prevent hanging processes */
alarm(UDEV_EVENT_TIMEOUT);
/* reconstruct event environment from message */
for (i = 0; msg->envp[i]; i++)
putenv(msg->envp[i]);
udevice = udev_device_init(msg->udev);
if (udevice == NULL)
return -1;
util_strlcpy(udevice->action, msg->action, sizeof(udevice->action));
sysfs_device_set_values(udevice->udev, udevice->dev, msg->devpath, msg->subsystem, msg->driver);
udevice->devpath_old = msg->devpath_old;
udevice->devt = msg->devt;
retval = udev_device_event(&rules, udevice);
/* rules may change/disable the timeout */
if (udevice->event_timeout >= 0)
alarm(udevice->event_timeout);
/* run programs collected by RUN-key*/
if (retval == 0 && !udevice->ignore_device && udev_get_run(msg->udev))
retval = udev_rules_run(udevice);
udev_device_cleanup(udevice);
return retval;
}
enum event_state {
EVENT_QUEUED,
EVENT_FINISHED,
EVENT_FAILED,
};
static void export_event_state(struct udevd_uevent_msg *msg, enum event_state state)
static void export_event_state(struct udev_event *event, enum event_state state)
{
char filename[UTIL_PATH_SIZE];
char filename_failed[UTIL_PATH_SIZE];
size_t start;
/* location of queue file */
snprintf(filename, sizeof(filename), "%s/.udev/queue/%llu", udev_get_dev_path(msg->udev), msg->seqnum);
snprintf(filename, sizeof(filename), "%s/.udev/queue/%llu",
udev_get_dev_path(event->udev), udev_device_get_seqnum(event->dev));
/* location of failed file */
util_strlcpy(filename_failed, udev_get_dev_path(msg->udev), sizeof(filename_failed));
util_strlcpy(filename_failed, udev_get_dev_path(event->udev), sizeof(filename_failed));
util_strlcat(filename_failed, "/", sizeof(filename_failed));
start = util_strlcat(filename_failed, ".udev/failed/", sizeof(filename_failed));
util_strlcat(filename_failed, msg->devpath, sizeof(filename_failed));
util_strlcat(filename_failed, udev_device_get_devpath(event->dev), sizeof(filename_failed));
util_path_encode(&filename_failed[start], sizeof(filename_failed) - start);
switch (state) {
case EVENT_QUEUED:
unlink(filename_failed);
delete_path(msg->udev, filename_failed);
create_path(msg->udev, filename);
udev_selinux_setfscreatecon(msg->udev, filename, S_IFLNK);
symlink(msg->devpath, filename);
udev_selinux_resetfscreatecon(msg->udev);
delete_path(event->udev, filename_failed);
create_path(event->udev, filename);
udev_selinux_setfscreatecon(event->udev, filename, S_IFLNK);
symlink(udev_device_get_devpath(event->dev), filename);
udev_selinux_resetfscreatecon(event->udev);
break;
case EVENT_FINISHED:
if (msg->devpath_old != NULL) {
if (udev_device_get_devpath_old(event->dev) != NULL) {
/* "move" event - rename failed file to current name, do not delete failed */
char filename_failed_old[UTIL_PATH_SIZE];
util_strlcpy(filename_failed_old, udev_get_dev_path(msg->udev), sizeof(filename_failed_old));
util_strlcpy(filename_failed_old, udev_get_dev_path(event->udev), sizeof(filename_failed_old));
util_strlcat(filename_failed_old, "/", sizeof(filename_failed_old));
start = util_strlcat(filename_failed_old, ".udev/failed/", sizeof(filename_failed_old));
util_strlcat(filename_failed_old, msg->devpath_old, sizeof(filename_failed_old));
util_strlcat(filename_failed_old, udev_device_get_devpath_old(event->dev), sizeof(filename_failed_old));
util_path_encode(&filename_failed_old[start], sizeof(filename) - start);
if (rename(filename_failed_old, filename_failed) == 0)
info(msg->udev, "renamed devpath, moved failed state of '%s' to %s'\n",
msg->devpath_old, msg->devpath);
info(event->udev, "renamed devpath, moved failed state of '%s' to %s'\n",
udev_device_get_devpath_old(event->dev), udev_device_get_devpath(event->dev));
} else {
unlink(filename_failed);
delete_path(msg->udev, filename_failed);
delete_path(event->udev, filename_failed);
}
unlink(filename);
delete_path(msg->udev, filename);
delete_path(event->udev, filename);
break;
case EVENT_FAILED:
/* move failed event to the failed directory */
create_path(msg->udev, filename_failed);
create_path(event->udev, filename_failed);
rename(filename, filename_failed);
/* clean up possibly empty queue directory */
delete_path(msg->udev, filename);
delete_path(event->udev, filename);
break;
}
return;
}
static void msg_queue_delete(struct udevd_uevent_msg *msg)
static void event_queue_delete(struct udev_event *event)
{
list_del(&msg->node);
list_del(&event->node);
/* mark as failed, if "add" event returns non-zero */
if (msg->exitstatus && strcmp(msg->action, "add") == 0)
export_event_state(msg, EVENT_FAILED);
if (event->exitstatus && strcmp(udev_device_get_action(event->dev), "add") == 0)
export_event_state(event, EVENT_FAILED);
else
export_event_state(msg, EVENT_FINISHED);
export_event_state(event, EVENT_FINISHED);
free(msg);
udev_device_unref(event->dev);
udev_event_unref(event);
}
static void udev_event_run(struct udevd_uevent_msg *msg)
static void asmlinkage event_sig_handler(int signum)
{
if (signum == SIGALRM)
exit(1);
}
static void event_fork(struct udev_event *event)
{
pid_t pid;
int retval;
struct sigaction act;
int err;
pid = fork();
switch (pid) {
case 0:
/* child */
close(uevent_netlink_sock);
udev_monitor_unref(kernel_monitor);
udev_ctrl_unref(udev_ctrl);
if (inotify_fd >= 0)
close(inotify_fd);
@ -256,64 +186,93 @@ static void udev_event_run(struct udevd_uevent_msg *msg)
logging_init("udevd-event");
setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
retval = udev_event_process(msg);
info(msg->udev, "seq %llu finished with %i\n", msg->seqnum, retval);
/* set signal handlers */
memset(&act, 0x00, sizeof(act));
act.sa_handler = (void (*)(int)) event_sig_handler;
sigemptyset (&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGALRM, &act, NULL);
/* reset to default */
act.sa_handler = SIG_DFL;
sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
sigaction(SIGCHLD, &act, NULL);
sigaction(SIGHUP, &act, NULL);
/* set timeout to prevent hanging processes */
alarm(UDEV_EVENT_TIMEOUT);
/* apply rules, create node, symlinks */
err = udev_event_run(event, &rules);
/* rules may change/disable the timeout */
if (udev_device_get_event_timeout(event->dev) >= 0)
alarm(udev_device_get_event_timeout(event->dev));
/* execute RUN= */
if (err == 0 && !event->ignore_device && udev_get_run(event->udev))
udev_rules_run(event);
info(event->udev, "seq %llu finished with %i\n", udev_device_get_seqnum(event->dev), err);
logging_close();
if (retval)
if (err != 0)
exit(1);
exit(0);
case -1:
err(msg->udev, "fork of child failed: %m\n");
msg_queue_delete(msg);
err(event->udev, "fork of child failed: %m\n");
event_queue_delete(event);
break;
default:
/* get SIGCHLD in main loop */
info(msg->udev, "seq %llu forked, pid [%d], '%s' '%s', %ld seconds old\n",
msg->seqnum, pid, msg->action, msg->subsystem, time(NULL) - msg->queue_time);
msg->pid = pid;
info(event->udev, "seq %llu forked, pid [%d], '%s' '%s', %ld seconds old\n",
udev_device_get_seqnum(event->dev),
pid,
udev_device_get_action(event->dev),
udev_device_get_subsystem(event->dev),
time(NULL) - event->queue_time);
event->pid = pid;
}
}
static void msg_queue_insert(struct udevd_uevent_msg *msg)
static void event_queue_insert(struct udev_event *event)
{
char filename[UTIL_PATH_SIZE];
int fd;
msg->queue_time = time(NULL);
event->queue_time = time(NULL);
export_event_state(msg, EVENT_QUEUED);
info(msg->udev, "seq %llu queued, '%s' '%s'\n", msg->seqnum, msg->action, msg->subsystem);
export_event_state(event, EVENT_QUEUED);
info(event->udev, "seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(event->dev), udev_device_get_action(event->dev), udev_device_get_subsystem(event->dev));
util_strlcpy(filename, udev_get_dev_path(msg->udev), sizeof(filename));
util_strlcpy(filename, udev_get_dev_path(event->udev), sizeof(filename));
util_strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename));
fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (fd >= 0) {
char str[32];
int len;
len = sprintf(str, "%llu\n", msg->seqnum);
len = sprintf(str, "%llu\n", udev_device_get_seqnum(event->dev));
write(fd, str, len);
close(fd);
}
/* run one event after the other in debug mode */
if (debug_trace) {
list_add_tail(&msg->node, &running_list);
udev_event_run(msg);
waitpid(msg->pid, NULL, 0);
msg_queue_delete(msg);
list_add_tail(&event->node, &running_list);
event_fork(event);
waitpid(event->pid, NULL, 0);
event_queue_delete(event);
return;
}
/* run all events with a timeout set immediately */
if (msg->timeout != 0) {
list_add_tail(&msg->node, &running_list);
udev_event_run(msg);
if (udev_device_get_timeout(event->dev) > 0) {
list_add_tail(&event->node, &running_list);
event_fork(event);
return;
}
list_add_tail(&msg->node, &exec_list);
list_add_tail(&event->node, &exec_list);
run_exec_q = 1;
}
@ -366,80 +325,99 @@ static int compare_devpath(const char *running, const char *waiting)
}
/* lookup event for identical, parent, child, or physical device */
static int devpath_busy(struct udevd_uevent_msg *msg, int limit)
static int devpath_busy(struct udev_event *event, int limit)
{
struct udevd_uevent_msg *loop_msg;
struct udev_event *loop_event;
int childs_count = 0;
/* check exec-queue which may still contain delayed events we depend on */
list_for_each_entry(loop_msg, &exec_list, node) {
list_for_each_entry(loop_event, &exec_list, node) {
/* skip ourself and all later events */
if (loop_msg->seqnum >= msg->seqnum)
if (udev_device_get_seqnum(loop_event->dev) >= udev_device_get_seqnum(event->dev))
break;
/* check our old name */
if (msg->devpath_old != NULL)
if (strcmp(loop_msg->devpath , msg->devpath_old) == 0)
if (udev_device_get_devpath_old(event->dev) != NULL)
if (strcmp(udev_device_get_devpath(loop_event->dev), udev_device_get_devpath_old(event->dev)) == 0)
return 2;
/* check identical, parent, or child device event */
if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
dbg(msg->udev, "%llu, device event still pending %llu (%s)\n",
msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
if (compare_devpath(udev_device_get_devpath(loop_event->dev), udev_device_get_devpath(event->dev)) != 0) {
dbg(event->udev, "%llu, device event still pending %llu (%s)\n",
udev_device_get_seqnum(event->dev),
udev_device_get_seqnum(loop_event->dev),
udev_device_get_devpath(loop_event->dev));
return 3;
}
/* check for our major:minor number */
if (msg->devt && loop_msg->devt == msg->devt &&
strcmp(msg->subsystem, loop_msg->subsystem) == 0) {
dbg(msg->udev, "%llu, device event still pending %llu (%d:%d)\n", msg->seqnum,
loop_msg->seqnum, major(loop_msg->devt), minor(loop_msg->devt));
if (major(udev_device_get_devnum(event->dev)) > 0 &&
udev_device_get_devnum(loop_event->dev) == udev_device_get_devnum(event->dev) &&
strcmp(udev_device_get_subsystem(event->dev), udev_device_get_subsystem(loop_event->dev)) == 0) {
dbg(event->udev, "%llu, device event still pending %llu (%d:%d)\n",
udev_device_get_seqnum(event->dev),
udev_device_get_seqnum(loop_event->dev),
major(udev_device_get_devnum(loop_event->dev)), minor(udev_device_get_devnum(loop_event->dev)));
return 4;
}
/* check physical device event (special case of parent) */
if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
dbg(msg->udev, "%llu, physical device event still pending %llu (%s)\n",
msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
if (udev_device_get_physdevpath(event->dev) != NULL &&
strcmp(udev_device_get_action(event->dev), "add") == 0)
if (compare_devpath(udev_device_get_devpath(loop_event->dev),
udev_device_get_physdevpath(event->dev)) != 0) {
dbg(event->udev, "%llu, physical device event still pending %llu (%s)\n",
udev_device_get_seqnum(event->dev),
udev_device_get_seqnum(loop_event->dev),
udev_device_get_devpath(loop_event->dev));
return 5;
}
}
/* check run queue for still running events */
list_for_each_entry(loop_msg, &running_list, node) {
list_for_each_entry(loop_event, &running_list, node) {
childs_count++;
if (childs_count++ >= limit) {
info(msg->udev, "%llu, maximum number (%i) of childs reached\n", msg->seqnum, childs_count);
info(event->udev, "%llu, maximum number (%i) of childs reached\n",
udev_device_get_seqnum(event->dev), childs_count);
return 1;
}
/* check our old name */
if (msg->devpath_old != NULL)
if (strcmp(loop_msg->devpath , msg->devpath_old) == 0)
if (udev_device_get_devpath_old(event->dev) != NULL)
if (strcmp(udev_device_get_devpath(loop_event->dev), udev_device_get_devpath_old(event->dev)) == 0)
return 2;
/* check identical, parent, or child device event */
if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
dbg(msg->udev, "%llu, device event still running %llu (%s)\n",
msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
if (compare_devpath(udev_device_get_devpath(loop_event->dev), udev_device_get_devpath(event->dev)) != 0) {
dbg(event->udev, "%llu, device event still running %llu (%s)\n",
udev_device_get_seqnum(event->dev),
udev_device_get_seqnum(loop_event->dev),
udev_device_get_devpath(loop_event->dev));
return 3;
}
/* check for our major:minor number */
if (msg->devt && loop_msg->devt == msg->devt &&
strcmp(msg->subsystem, loop_msg->subsystem) == 0) {
dbg(msg->udev, "%llu, device event still running %llu (%d:%d)\n", msg->seqnum,
loop_msg->seqnum, major(loop_msg->devt), minor(loop_msg->devt));
if (major(udev_device_get_devnum(event->dev)) > 0 &&
udev_device_get_devnum(loop_event->dev) == udev_device_get_devnum(event->dev) &&
strcmp(udev_device_get_subsystem(event->dev), udev_device_get_subsystem(loop_event->dev)) == 0) {
dbg(event->udev, "%llu, device event still pending %llu (%d:%d)\n",
udev_device_get_seqnum(event->dev),
udev_device_get_seqnum(loop_event->dev),
major(udev_device_get_devnum(loop_event->dev)), minor(udev_device_get_devnum(loop_event->dev)));
return 4;
}
/* check physical device event (special case of parent) */
if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
dbg(msg->udev, "%llu, physical device event still running %llu (%s)\n",
msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
if (udev_device_get_physdevpath(event->dev) != NULL &&
strcmp(udev_device_get_action(event->dev), "add") == 0)
if (compare_devpath(udev_device_get_devpath(loop_event->dev),
udev_device_get_physdevpath(event->dev)) != 0) {
dbg(event->udev, "%llu, physical device event still pending %llu (%s)\n",
udev_device_get_seqnum(event->dev),
udev_device_get_seqnum(loop_event->dev),
udev_device_get_devpath(loop_event->dev));
return 5;
}
}
@ -447,99 +425,30 @@ static int devpath_busy(struct udevd_uevent_msg *msg, int limit)
}
/* serializes events for the identical and parent and child devices */
static void msg_queue_manager(struct udev *udev)
static void event_queue_manager(struct udev *udev)
{
struct udevd_uevent_msg *loop_msg;
struct udevd_uevent_msg *tmp_msg;
struct udev_event *loop_event;
struct udev_event *tmp_event;
if (list_empty(&exec_list))
return;
list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
list_for_each_entry_safe(loop_event, tmp_event, &exec_list, node) {
/* serialize and wait for parent or child events */
if (devpath_busy(loop_msg, max_childs) != 0) {
dbg(udev, "delay seq %llu (%s)\n", loop_msg->seqnum, loop_msg->devpath);
if (devpath_busy(loop_event, max_childs) != 0) {
dbg(udev, "delay seq %llu (%s)\n",
udev_device_get_seqnum(loop_event->dev),
udev_device_get_devpath(loop_event->dev));
continue;
}
/* move event to run list */
list_move_tail(&loop_msg->node, &running_list);
udev_event_run(loop_msg);
dbg(udev, "moved seq %llu to running list\n", loop_msg->seqnum);
list_move_tail(&loop_event->node, &running_list);
event_fork(loop_event);
dbg(udev, "moved seq %llu to running list\n", udev_device_get_seqnum(loop_event->dev));
}
}
static struct udevd_uevent_msg *get_msg_from_envbuf(struct udev *udev, const char *buf, int buf_size)
{
int bufpos;
int i;
struct udevd_uevent_msg *msg;
char *physdevdriver_key = NULL;
int maj = 0;
int min = 0;
msg = malloc(sizeof(struct udevd_uevent_msg) + buf_size);
if (msg == NULL)
return NULL;
memset(msg, 0x00, sizeof(struct udevd_uevent_msg) + buf_size);
msg->udev = udev;
/* copy environment buffer and reconstruct envp */
memcpy(msg->envbuf, buf, buf_size);
bufpos = 0;
for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
int keylen;
char *key;
key = &msg->envbuf[bufpos];
keylen = strlen(key);
msg->envp[i] = key;
bufpos += keylen + 1;
dbg(udev, "add '%s' to msg.envp[%i]\n", msg->envp[i], i);
/* remember some keys for further processing */
if (strncmp(key, "ACTION=", 7) == 0)
msg->action = &key[7];
else if (strncmp(key, "DEVPATH=", 8) == 0)
msg->devpath = &key[8];
else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
msg->subsystem = &key[10];
else if (strncmp(key, "DRIVER=", 7) == 0)
msg->driver = &key[7];
else if (strncmp(key, "SEQNUM=", 7) == 0)
msg->seqnum = strtoull(&key[7], NULL, 10);
else if (strncmp(key, "DEVPATH_OLD=", 12) == 0)
msg->devpath_old = &key[12];
else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
msg->physdevpath = &key[12];
else if (strncmp(key, "PHYSDEVDRIVER=", 14) == 0)
physdevdriver_key = key;
else if (strncmp(key, "MAJOR=", 6) == 0)
maj = strtoull(&key[6], NULL, 10);
else if (strncmp(key, "MINOR=", 6) == 0)
min = strtoull(&key[6], NULL, 10);
else if (strncmp(key, "TIMEOUT=", 8) == 0)
msg->timeout = strtoull(&key[8], NULL, 10);
}
msg->devt = makedev(maj, min);
msg->envp[i++] = "UDEVD_EVENT=1";
if (msg->driver == NULL && msg->physdevpath == NULL && physdevdriver_key != NULL) {
/* for older kernels DRIVER is empty for a bus device, export PHYSDEVDRIVER as DRIVER */
msg->envp[i++] = &physdevdriver_key[7];
msg->driver = &physdevdriver_key[14];
}
msg->envp[i] = NULL;
if (msg->devpath == NULL || msg->action == NULL) {
info(udev, "DEVPATH or ACTION missing, ignore message\n");
free(msg);
return NULL;
}
return msg;
}
/* receive the udevd message from userspace */
static void handle_ctrl_msg(struct udev_ctrl *uctrl)
{
@ -556,8 +465,6 @@ static void handle_ctrl_msg(struct udev_ctrl *uctrl)
if (i >= 0) {
info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
udev_set_log_priority(udev, i);
sprintf(udev_log_env, "UDEV_LOG=%i", i);
putenv(udev_log_env);
}
if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
@ -568,7 +475,7 @@ static void handle_ctrl_msg(struct udev_ctrl *uctrl)
if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
info(udev, "udevd message (START_EXEC_QUEUE) received\n");
stop_exec_q = 0;
msg_queue_manager(udev);
event_queue_manager(udev);
}
if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
@ -578,24 +485,28 @@ static void handle_ctrl_msg(struct udev_ctrl *uctrl)
str = udev_ctrl_get_set_env(ctrl_msg);
if (str != NULL) {
char *key = strdup(str);
char *val;
char *key;
val = strchr(str, '=');
if (val != NULL) {
val[0] = '\0';
val = &val[1];
if (val[0] == '\0') {
info(udev, "udevd message (ENV) received, unset '%s'\n", key);
unsetenv(str);
key = strdup(str);
if (key != NULL) {
char *val;
val = strchr(key, '=');
if (val != NULL) {
val[0] = '\0';
val = &val[1];
if (val[0] == '\0') {
info(udev, "udevd message (ENV) received, unset '%s'\n", key);
udev_add_property(udev, key, NULL);
} else {
info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
udev_add_property(udev, key, val);
}
} else {
info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
setenv(key, val, 1);
err(udev, "wrong key format '%s'\n", key);
}
} else {
err(udev, "wrong key format '%s'\n", key);
free(key);
}
free(key);
}
i = udev_ctrl_get_set_max_childs(ctrl_msg);
@ -607,57 +518,6 @@ static void handle_ctrl_msg(struct udev_ctrl *uctrl)
udev_ctrl_msg_unref(ctrl_msg);
}
/* receive the kernel user event message and do some sanity checks */
static struct udevd_uevent_msg *get_netlink_msg(struct udev *udev)
{
struct udevd_uevent_msg *msg;
int bufpos;
ssize_t size;
static char buffer[UEVENT_BUFFER_SIZE+512];
char *pos;
size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
if (size < 0) {
if (errno != EINTR)
err(udev, "unable to receive kernel netlink message: %m\n");
return NULL;
}
if ((size_t)size > sizeof(buffer)-1)
size = sizeof(buffer)-1;
buffer[size] = '\0';
dbg(udev, "uevent_size=%zi\n", size);
/* start of event payload */
bufpos = strlen(buffer)+1;
msg = get_msg_from_envbuf(udev, &buffer[bufpos], size-bufpos);
if (msg == NULL)
return NULL;
/* validate message */
pos = strchr(buffer, '@');
if (pos == NULL) {
err(udev, "invalid uevent '%s'\n", buffer);
free(msg);
return NULL;
}
pos[0] = '\0';
if (msg->action == NULL) {
info(udev, "no ACTION in payload found, skip event '%s'\n", buffer);
free(msg);
return NULL;
}
if (strcmp(msg->action, buffer) != 0) {
err(udev, "ACTION in payload does not match uevent, skip event '%s'\n", buffer);
free(msg);
return NULL;
}
return msg;
}
static void asmlinkage sig_handler(int signum)
{
switch (signum) {
@ -680,15 +540,16 @@ static void asmlinkage sig_handler(int signum)
static void udev_done(int pid, int exitstatus)
{
/* find msg associated with pid and delete it */
struct udevd_uevent_msg *msg;
/* find event associated with pid and delete it */
struct udev_event *event;
list_for_each_entry(msg, &running_list, node) {
if (msg->pid == pid) {
info(msg->udev, "seq %llu, pid [%d] exit with %i, %ld seconds old\n", msg->seqnum, msg->pid,
exitstatus, time(NULL) - msg->queue_time);
msg->exitstatus = exitstatus;
msg_queue_delete(msg);
list_for_each_entry(event, &running_list, node) {
if (event->pid == pid) {
info(event->udev, "seq %llu, pid [%d] exit with %i, %ld seconds old\n",
udev_device_get_seqnum(event->dev), event->pid,
exitstatus, time(NULL) - event->queue_time);
event->exitstatus = exitstatus;
event_queue_delete(event);
/* there may be events waiting with the same devpath */
run_exec_q = 1;
@ -716,36 +577,6 @@ static void reap_sigchilds(void)
}
}
static int init_uevent_netlink_sock(struct udev *udev)
{
struct sockaddr_nl snl;
const int buffersize = 16 * 1024 * 1024;
int retval;
memset(&snl, 0x00, sizeof(struct sockaddr_nl));
snl.nl_family = AF_NETLINK;
snl.nl_pid = getpid();
snl.nl_groups = 1;
uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
if (uevent_netlink_sock == -1) {
err(udev, "error getting socket: %m\n");
return -1;
}
/* set receive buffersize */
setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
if (retval < 0) {
err(udev, "bind failed: %m\n");
close(uevent_netlink_sock);
uevent_netlink_sock = -1;
return -1;
}
return 0;
}
static void export_initial_seqnum(struct udev *udev)
{
char filename[UTIL_PATH_SIZE];
@ -777,7 +608,7 @@ static void export_initial_seqnum(struct udev *udev)
int main(int argc, char *argv[])
{
struct udev *udev;
int retval;
int err;
int fd;
struct sigaction act;
fd_set readfds;
@ -800,7 +631,7 @@ int main(int argc, char *argv[])
logging_init("udevd");
udev_set_log_fn(udev, log_fn);
dbg(udev, "version %s\n", VERSION);
info(udev, "version %s\n", VERSION);
selinux_init(udev);
while (1) {
@ -868,45 +699,49 @@ int main(int argc, char *argv[])
goto exit;
}
if (init_uevent_netlink_sock(udev) < 0) {
kernel_monitor = udev_monitor_new_from_netlink(udev);
if (kernel_monitor == NULL || udev_monitor_enable_receiving(kernel_monitor) < 0) {
fprintf(stderr, "error initializing netlink socket\n");
err(udev, "error initializing netlink socket\n");
rc = 3;
goto exit;
} else {
/* set receive buffersize */
const int buffersize = 32 * 1024 * 1024;
setsockopt(udev_monitor_get_fd(kernel_monitor),
SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
}
retval = pipe(signal_pipe);
if (retval < 0) {
err = pipe(signal_pipe);
if (err < 0) {
err(udev, "error getting pipes: %m\n");
goto exit;
}
retval = fcntl(signal_pipe[READ_END], F_GETFL, 0);
if (retval < 0) {
err = fcntl(signal_pipe[READ_END], F_GETFL, 0);
if (err < 0) {
err(udev, "error fcntl on read pipe: %m\n");
goto exit;
}
retval = fcntl(signal_pipe[READ_END], F_SETFL, retval | O_NONBLOCK);
if (retval < 0) {
err = fcntl(signal_pipe[READ_END], F_SETFL, err | O_NONBLOCK);
if (err < 0) {
err(udev, "error fcntl on read pipe: %m\n");
goto exit;
}
retval = fcntl(signal_pipe[WRITE_END], F_GETFL, 0);
if (retval < 0) {
err = fcntl(signal_pipe[WRITE_END], F_GETFL, 0);
if (err < 0) {
err(udev, "error fcntl on write pipe: %m\n");
goto exit;
}
retval = fcntl(signal_pipe[WRITE_END], F_SETFL, retval | O_NONBLOCK);
if (retval < 0) {
err = fcntl(signal_pipe[WRITE_END], F_SETFL, err | O_NONBLOCK);
if (err < 0) {
err(udev, "error fcntl on write pipe: %m\n");
goto exit;
}
/* parse the rules and keep them in memory */
sysfs_init();
udev_rules_init(udev, &rules, 1);
export_initial_seqnum(udev);
if (daemonize) {
@ -1021,28 +856,18 @@ int main(int argc, char *argv[])
}
info(udev, "initialize max_childs to %u\n", max_childs);
/* clear environment for forked event processes */
clearenv();
/* export log_priority , as called programs may want to follow that setting */
sprintf(udev_log_env, "UDEV_LOG=%i", udev_get_log_priority(udev));
putenv(udev_log_env);
if (debug_trace)
putenv("DEBUG=1");
maxfd = udev_ctrl_get_fd(udev_ctrl);
maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
maxfd = UDEV_MAX(maxfd, udev_monitor_get_fd(kernel_monitor));
maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
maxfd = UDEV_MAX(maxfd, inotify_fd);
while (!udev_exit) {
struct udevd_uevent_msg *msg;
int fdcount;
FD_ZERO(&readfds);
FD_SET(signal_pipe[READ_END], &readfds);
FD_SET(udev_ctrl_get_fd(udev_ctrl), &readfds);
FD_SET(uevent_netlink_sock, &readfds);
FD_SET(udev_monitor_get_fd(kernel_monitor), &readfds);
if (inotify_fd >= 0)
FD_SET(inotify_fd, &readfds);
@ -1057,11 +882,20 @@ int main(int argc, char *argv[])
if (FD_ISSET(udev_ctrl_get_fd(udev_ctrl), &readfds))
handle_ctrl_msg(udev_ctrl);
/* get netlink message */
if (FD_ISSET(uevent_netlink_sock, &readfds)) {
msg = get_netlink_msg(udev);
if (msg)
msg_queue_insert(msg);
/* get kernel uevent */
if (FD_ISSET(udev_monitor_get_fd(kernel_monitor), &readfds)) {
struct udev_device *dev;
dev = udev_monitor_receive_device(kernel_monitor);
if (dev != NULL) {
struct udev_event *event;
event = udev_event_new(dev);
if (event != NULL)
event_queue_insert(event);
else
udev_device_unref(dev);
}
}
/* received a signal, clear our notification pipe */
@ -1098,7 +932,6 @@ int main(int argc, char *argv[])
udev_rules_init(udev, &rules, 1);
}
/* forked child has returned */
if (sigchilds_waiting) {
sigchilds_waiting = 0;
reap_sigchilds();
@ -1107,14 +940,13 @@ int main(int argc, char *argv[])
if (run_exec_q) {
run_exec_q = 0;
if (!stop_exec_q)
msg_queue_manager(udev);
event_queue_manager(udev);
}
}
rc = 0;
exit:
udev_rules_cleanup(&rules);
sysfs_cleanup();
if (signal_pipe[READ_END] >= 0)
close(signal_pipe[READ_END]);
@ -1124,8 +956,7 @@ exit:
udev_ctrl_unref(udev_ctrl);
if (inotify_fd >= 0)
close(inotify_fd);
if (uevent_netlink_sock >= 0)
close(uevent_netlink_sock);
udev_monitor_unref(kernel_monitor);
selinux_exit(udev);
udev_unref(udev);