chiark / gitweb /
[PATCH] trivial cleanups and change some comments
[elogind.git] / udev.c
diff --git a/udev.c b/udev.c
index 971ac10809bedb81be6584cc64a16da5e6b91719..ca79bb4ec0e37812770ceaea2b76acf901d9e438 100644 (file)
--- a/udev.c
+++ b/udev.c
@@ -3,8 +3,8 @@
  *
  * Userspace devfs
  *
- * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
- *
+ * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
+ * Copyright (C) 2004 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
  *
  */
 
+#include <stdio.h>
+#include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
 #include <fcntl.h>
-#include <unistd.h>
+#include <ctype.h>
 #include <errno.h>
+#include <signal.h>
+#include <unistd.h>
 
+#include "libsysfs/sysfs/libsysfs.h"
 #include "udev.h"
+#include "udev_utils.h"
+#include "udev_sysfs.h"
 #include "udev_version.h"
+#include "namedev.h"
+#include "logging.h"
 
 
-static char *get_action(void)
-{
-       char *action;
-
-       action = getenv("ACTION");
-       return action;
-}
-
-
-static char *get_device(void)
+#ifdef LOG
+unsigned char logname[LOGNAME_SIZE];
+void log_message(int level, const char *format, ...)
 {
-       static char device[255];
-       char *temp;
+       va_list args;
 
-       temp = getenv("DEVPATH");
-       if (temp == NULL)
-               return NULL;
-       strcpy(device, SYSFS_ROOT);
-       strcat(device, temp);
+       if (!udev_log)
+               return;
 
-       return device;
+       va_start(args, format);
+       vsyslog(level, format, args);
+       va_end(args);
 }
+#endif
 
-/* 
- * Right now the major/minor of a device is stored in a file called
- * "dev" in sysfs.
- * The number is stored as:
- *     MMmm
- *             MM is the major
- *             mm is the minor
- *             The value is in hex.
- * Yes, this will probably change when we go to a bigger major/minor
- * range, and will have to be changed at that time.
- */
-static int get_major_minor (char *dev, int *major, int *minor)
-{
-       char filename[255];
-       char line[20];
-       char temp[3];
+/* (for now) true if udevsend is the helper */
+static int manage_hotplug_event(void) {
+       char helper[256];
        int fd;
-       int retval = 0;
-
-       /* add the dev file to the directory and see if it's present */
-       strncpy(filename, dev, sizeof(filename));
-       strncat(filename, DEV_FILE, sizeof(filename));
-       fd = open(filename, O_RDONLY);
-       if (fd < 0) {
-               dbg("Can't open %s", filename);
-               return -ENODEV;
-       }
+       int len;
 
-       /* get the major/minor */
-       retval = read(fd, line, sizeof(line));
-       if (retval < 0) {
-               dbg("read error on %s", dev);
+       fd = open("/proc/sys/kernel/hotplug", O_RDONLY);
+       if (fd < 0)
                goto exit;
-       }
-
-       temp[0] = line[0];
-       temp[1] = line[1];
-       temp[2] = 0x00;
-       *major = (int)strtol(&temp[0], NULL, 16);
-
-       temp[0] = line[2];
-       temp[1] = line[3];
-       temp[2] = 0x00;
-       *minor = (int)strtol(&temp[0], NULL, 16);
-
-       dbg("found major = %d, minor = %d", *major, *minor);
 
-       retval = 0;
-exit:
+       len = read(fd, helper, 256);
        close(fd);
-       return retval;
-}
 
-/*
- * Here would go a call to the naming deamon, to get the name we want to have
- * for this device.  But for now, let's just default to whatever the kernel is
- * calling the device as that will keep the "old-style" naming policy
- */
-static char *get_name(char *dev, int major, int minor)
-{
-       static char name[100];
-       char *temp;
-
-       temp = strrchr(dev, '/');
-       if (temp == NULL)
-               return NULL;
-       strncpy(name, &temp[1], sizeof(name));
-
-       dbg("name is %s", name);
+       if (len < 0)
+               goto exit;
+       helper[len] = '\0';
 
-       return &name[0];
-}
+       if (strstr(helper, "udevsend"))
+               return 1;
 
-/*
- * Again, this will live in the naming deamon
- */
-static int get_mode(char *name, char *dev, int major, int minor)
-{
-       /* just default everyone to rw for the world! */
-       return 0666;
+exit:
+       return 0;
 }
 
-/*
- * We also want to add some permissions here, and possibly some symlinks
- */
-static int create_node(char *name, char type, int major, int minor, int mode)
+static void asmlinkage sig_handler(int signum)
 {
-       char *argv[7];
-       char mode_string[100];
-       char type_string[3];
-       char major_string[20];
-       char minor_string[20];
-       char filename[255];
-       int retval = 0;
-
-       strncpy(filename, UDEV_ROOT, sizeof(filename));
-       strncat(filename, name, sizeof(filename));
-
-       snprintf(mode_string, sizeof(mode_string), "--mode=%#o", mode);
-       snprintf(type_string, sizeof(type_string), "%c", type);
-       snprintf(major_string, sizeof(major_string), "%d", major);
-       snprintf(minor_string, sizeof(minor_string), "%d", minor);
-       
-       argv[0] = MKNOD;
-       argv[1] = mode_string;
-       argv[2] = filename;
-       argv[3] = type_string;
-       argv[4] = major_string;
-       argv[5] = minor_string;
-       argv[6] = NULL;
-       dbg ("executing %s %s %s %s %s %s",
-               argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
-       switch (fork()) {
-               case 0:
-                       /* we are the child, so lets run the program */
-                       execv (MKNOD, argv);
-                       exit(0);
-                       break;
-               case (-1):
-                       dbg ("fork failed.");
-                       retval = -EFAULT;
-                       break;
-               default:
-                       break;
+       switch (signum) {
+               case SIGALRM:
+                       exit(1);
+               case SIGINT:
+               case SIGTERM:
+                       exit(20 + signum);
        }
-       return retval;
 }
 
-static int remove_node(char *name)
+int main(int argc, char *argv[], char *envp[])
 {
-       return 0;
-}
+       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);
 
-static int do_it(char *action, char *name, char type, int major, int minor, int mode)
-{
-       if (strcmp(action, "add") == 0)
-               return create_node(name, type, major, minor, mode);
+       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 (strcmp(action, "remove") == 0)
-               return remove_node(name);
+       if (strstr(argv[0], "udevstart") || (argc == 2 && strstr(argv[1], "udevstart"))) {
+               dbg("udevstart");
 
-       dbg("Unknown action: %s", action);
-       return -EINVAL;
-}
+               /* disable all logging, as it's much too slow on some facilities */
+               udev_log = 0;
 
-int main(int argc, char *argv[])
-{
-       char *subsystem;
-       char *action;
-       char *device;
-       char *name;
-       char type;
-       int major;
-       int minor;
-       int mode;
-       int retval = -EINVAL;
-       
-       if (argc != 2) {
-               dbg ("unknown number of arguments");
+               namedev_init();
+               retval = udev_start();
                goto exit;
        }
 
-       /* for now, the block layer is the only place where block devices are */
-       subsystem = argv[1];
-       if (strcmp(subsystem, "block") == 0)
-               type = 'b';
-       else
-               type = 'c';
-
-       action = get_action();
        if (!action) {
-               dbg ("no action?");
-               goto exit;
-       }
-
-       device = get_device();
-       if (!device) {
-               dbg ("no device?");
-               goto exit;
+               dbg("no action");
+               goto hotplug;
        }
-       dbg("looking at %s", device);
 
-       retval = get_major_minor(device, &major, &minor);
-       if (retval) {
-               dbg ("get_major_minor failed");
-               goto exit;
+       if (!subsystem) {
+               dbg("no subsystem");
+               goto hotplug;
        }
 
-       name = get_name(device, major, minor);
-       if (name == NULL) {
-               dbg ("get_name failed");
-               retval = -ENODEV;
-               goto exit;
+       if (!devpath) {
+               dbg("no devpath");
+               goto hotplug;
        }
 
-       mode = get_mode(name, device, major, minor);
-       if (mode < 0) {
-               dbg ("get_mode failed");
-               retval = -EINVAL;
-               goto exit;
+       /* 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");
        }
 
-       retval = do_it(action, name, type, major, minor, mode);
-       if (retval) {
-               dbg ("do_it failed");
-               goto exit;
-       }
+hotplug:
+       if (udev_hotplug_d && manage_hotplug_event())
+               udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX);
 
-       retval = 0;
-exit:  
+exit:
+       logging_close();
        return retval;
 }
-