Systemd/udev.c

237 lines
5.6 KiB
C
Raw Normal View History

2005-04-27 05:59:47 +02:00
/*
* udev.c
*
* Userspace devfs
*
* Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
* Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
2005-04-27 05:59:47 +02:00
*
* 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 version 2 of the License.
*
* 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, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <stddef.h>
2005-04-27 05:59:47 +02:00
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
2004-10-19 08:14:20 +02:00
#include <unistd.h>
2003-04-10 19:53:46 +02:00
#include "libsysfs/sysfs/libsysfs.h"
2005-04-27 05:59:47 +02:00
#include "udev.h"
#include "udev_utils.h"
#include "udev_sysfs.h"
2005-04-27 05:59:47 +02:00
#include "udev_version.h"
#include "namedev.h"
2004-11-12 06:32:19 +01:00
#include "logging.h"
2005-04-27 05:59:47 +02:00
#ifdef LOG
unsigned char logname[LOGNAME_SIZE];
void log_message(int level, const char *format, ...)
{
va_list args;
if (!udev_log)
return;
va_start(args, format);
vsyslog(level, format, args);
va_end(args);
}
#endif
/* (for now) true if udevsend is the helper */
static int manage_hotplug_event(void) {
char helper[256];
int fd;
int len;
fd = open("/proc/sys/kernel/hotplug", O_RDONLY);
if (fd < 0)
goto exit;
len = read(fd, helper, 256);
close(fd);
if (len < 0)
goto exit;
helper[len] = '\0';
if (strstr(helper, "udevsend"))
return 1;
exit:
return 0;
}
static void asmlinkage sig_handler(int signum)
{
switch (signum) {
case SIGALRM:
exit(1);
case SIGINT:
case SIGTERM:
exit(20 + signum);
}
}
int main(int argc, char *argv[], char *envp[])
{
struct sigaction act;
struct sysfs_class_device *class_dev;
struct sysfs_device *devices_dev;
struct udevice udev;
char path[SYSFS_PATH_MAX];
int retval = -EINVAL;
const char *error;
const char *action = getenv("ACTION");
const char *devpath = getenv("DEVPATH");
const char *subsystem = argv[1];
dbg("version %s", UDEV_VERSION);
logging_init("udev");
udev_init_config();
/* set signal handlers */
act.sa_handler = (void (*) (int))sig_handler;
sigemptyset (&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGALRM, &act, NULL);
sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL);
/* trigger timeout to prevent hanging processes */
alarm(ALARM_TIMEOUT);
action = getenv("ACTION");
devpath = getenv("DEVPATH");
subsystem = getenv("SUBSYSTEM");
/* older kernels passed the SUBSYSTEM only as argument */
if (!subsystem && argc == 2)
subsystem = argv[1];
udev_init_device(&udev, devpath, subsystem);
if (strstr(argv[0], "udevstart") || (argc == 2 && strstr(argv[1], "udevstart"))) {
dbg("udevstart");
/* disable all logging, as it's much too slow on some facilities */
udev_log = 0;
namedev_init();
retval = udev_start();
goto exit;
}
if (!action) {
dbg("no action");
goto hotplug;
}
if (!subsystem) {
dbg("no subsystem");
goto hotplug;
}
if (!devpath) {
dbg("no devpath");
goto hotplug;
}
/* export logging flag, as called scripts may want to do the same as udev */
if (udev_log)
setenv("UDEV_LOG", "1", 1);
if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
if (strcmp(action, "add") == 0) {
/* wait for sysfs and possibly add node */
dbg("udev add");
/* skip subsystems without "dev", but handle net devices */
if (udev.type != 'n' && subsystem_expect_no_dev(udev.subsystem)) {
dbg("don't care about '%s' devices", udev.subsystem);
goto hotplug;
}
snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
class_dev = wait_class_device_open(path);
if (class_dev == NULL) {
dbg ("open class device failed");
goto hotplug;
}
dbg("opened class_dev->name='%s'", class_dev->name);
wait_for_class_device(class_dev, &error);
/* init rules, permissions */
namedev_init();
/* name, create node, store in db */
retval = udev_add_device(&udev, class_dev);
sysfs_close_class_device(class_dev);
} else if (strcmp(action, "remove") == 0) {
/* possibly remove a node */
dbg("udev remove");
/* skip subsystems without "dev" */
if (subsystem_expect_no_dev(udev.subsystem)) {
dbg("don't care about '%s' devices", udev.subsystem);
goto hotplug;
}
/* get node from db, remove db-entry, delete created node */
retval = udev_remove_device(&udev);
}
/* run dev.d/ scripts if we created/deleted a node or changed a netif name */
if (udev_dev_d && udev.devname[0] != '\0') {
setenv("DEVNAME", udev.devname, 1);
udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
}
} else if ((strncmp(devpath, "/devices/", 9) == 0)) {
if (strcmp(action, "add") == 0) {
/* wait for sysfs */
dbg("devices add");
snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath);
devices_dev = wait_devices_device_open(path);
if (!devices_dev) {
dbg("devices device unavailable (probably remove has beaten us)");
goto hotplug;
}
dbg("devices device opened '%s'", path);
wait_for_devices_device(devices_dev, &error);
sysfs_close_device(devices_dev);
} else if (strcmp(action, "remove") == 0) {
dbg("devices remove");
}
} else {
dbg("unhandled");
}
hotplug:
if (udev_hotplug_d && manage_hotplug_event())
udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX);
exit:
logging_close();
[PATCH] hmm, handle net devices with udev? Hmm, Arndt Bergmann sent a patch like this one a few weeks ago and I want to bring the question back, if we want to handle net device naming with udev. With this patch it is actually possible to specify something like this in udev.rules: KERNEL="dummy*", SYSFS{address}="00:00:00:00:00:00", SYSFS{features}="0x0", NAME="blind%n" KERNEL="eth*", SYSFS{address}="00:0d:60:77:30:91", NAME="private" and you will get: [root@pim udev.kay]# cat /proc/net/dev Inter-| Receive | Transmit face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed lo: 1500 30 0 0 0 0 0 0 1500 30 0 0 0 0 0 0 private: 278393 1114 0 0 0 0 0 0 153204 1468 0 0 0 0 0 0 sit0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 blind0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The udevinfo program is also working: [root@pim udev.kay]# ./udevinfo -a -p /sys/class/net/private looking at class device '/sys/class/net/private': SYSFS{addr_len}="6" SYSFS{address}="00:0d:60:77:30:91" SYSFS{broadcast}="ff:ff:ff:ff:ff:ff" SYSFS{features}="0x3a9" SYSFS{flags}="0x1003" SYSFS{ifindex}="2" SYSFS{iflink}="2" SYSFS{mtu}="1500" SYSFS{tx_queue_len}="1000" SYSFS{type}="1" follow the class device's "device" looking at the device chain at '/sys/devices/pci0000:00/0000:00:1e.0/0000:02:01.0': BUS="pci" ID="0000:02:01.0" SYSFS{class}="0x020000" SYSFS{detach_state}="0" SYSFS{device}="0x101e" SYSFS{irq}="11" SYSFS{subsystem_device}="0x0549" SYSFS{subsystem_vendor}="0x1014" SYSFS{vendor}="0x8086" The matching device will be renamed to the given name. The device name will not be put into the udev database, cause the kernel renames the device and the sysfs name disappears. I like it, cause it plugs in nicely. We have all the naming features and sysfs queries and walks inside of udev. The sysfs timing races are already solved and the management tools are working for net devices too. nameif can only match the MAC address now. udev can match any sysfs value of the device tree the net device is connected to. But right, net devices do not have device nodes :)
2004-03-25 08:19:39 +01:00
return retval;
2005-04-27 05:59:47 +02:00
}