chiark / gitweb /
[PATCH] update the tests to handle block devices too.
[elogind.git] / udev.c
diff --git a/udev.c b/udev.c
index 971ac10809bedb81be6584cc64a16da5e6b91719..dc5f5b8d8944006125b6d941d3c0e8da467df3aa 100644 (file)
--- a/udev.c
+++ b/udev.c
@@ -30,6 +30,7 @@
 
 #include "udev.h"
 #include "udev_version.h"
+#include "namedev.h"
 
 
 static char *get_action(void)
@@ -49,7 +50,8 @@ static char *get_device(void)
        temp = getenv("DEVPATH");
        if (temp == NULL)
                return NULL;
-       strcpy(device, SYSFS_ROOT);
+       strcpy(device, "");
+//     strcpy(device, SYSFS_ROOT);
        strcat(device, temp);
 
        return device;
@@ -183,33 +185,79 @@ static int create_node(char *name, char type, int major, int minor, int mode)
        return retval;
 }
 
-static int remove_node(char *name)
+/*
+ * We also want to clean up any symlinks that were created in create_node()
+ */
+static int delete_node(char *name)
 {
-       return 0;
+       char filename[255];
+
+       strncpy(filename, UDEV_ROOT, sizeof(filename));
+       strncat(filename, name, sizeof(filename));
+
+       dbg("unlinking %s", filename);
+       return unlink(filename);
 }
 
-static int do_it(char *action, char *name, char type, int major, int minor, int mode)
+static int add_device(char *device, char type, struct device_attr *attr)
 {
-       if (strcmp(action, "add") == 0)
-               return create_node(name, type, major, minor, mode);
+       char *name;
+       int major;
+       int minor;
+       int mode;
+       int retval = -EINVAL;
+#if 0
+       retval = get_major_minor(device, &major, &minor);
+       if (retval) {
+               dbg ("get_major_minor failed");
+               goto exit;
+       }
 
-       if (strcmp(action, "remove") == 0)
-               return remove_node(name);
+       name = get_name(device, major, minor);
+       if (name == NULL) {
+               dbg ("get_name failed");
+               retval = -ENODEV;
+               goto exit;
+       }
 
-       dbg("Unknown action: %s", action);
-       return -EINVAL;
+       mode = get_mode(name, device, major, minor);
+       if (mode < 0) {
+               dbg ("get_mode failed");
+               retval = -EINVAL;
+               goto exit;
+       }
+#endif
+       return create_node(attr->name, type, attr->major, attr->minor, attr->mode);
+
+exit:
+       return retval;
+}
+
+static int remove_device(char *device)
+{
+       char *name;
+       int retval = 0;
+
+       name = get_name(device, 0, 0);
+       if (name == NULL) {
+               dbg ("get_name failed");
+               retval = -ENODEV;
+               goto exit;
+       }
+
+       return delete_node(name);
+
+exit:
+       return retval;
 }
 
 int main(int argc, char *argv[])
 {
+       struct device_attr attr;
        char *subsystem;
        char *action;
        char *device;
-       char *name;
        char type;
-       int major;
-       int minor;
-       int mode;
        int retval = -EINVAL;
        
        if (argc != 2) {
@@ -217,6 +265,13 @@ int main(int argc, char *argv[])
                goto exit;
        }
 
+       namedev_init();
+
+       /* sleep for a second or two to give the kernel a chance to
+        * create the dev file
+        */
+       sleep(2);
+
        /* for now, the block layer is the only place where block devices are */
        subsystem = argv[1];
        if (strcmp(subsystem, "block") == 0)
@@ -237,31 +292,18 @@ int main(int argc, char *argv[])
        }
        dbg("looking at %s", device);
 
-       retval = get_major_minor(device, &major, &minor);
-       if (retval) {
-               dbg ("get_major_minor failed");
-               goto exit;
-       }
+       retval = namedev_name_device(device, &attr);
+       if (retval)
+               return retval;
 
-       name = get_name(device, major, minor);
-       if (name == NULL) {
-               dbg ("get_name failed");
-               retval = -ENODEV;
-               goto exit;
-       }
+       if (strcmp(action, "add") == 0)
+               return add_device(device, type, &attr);
 
-       mode = get_mode(name, device, major, minor);
-       if (mode < 0) {
-               dbg ("get_mode failed");
-               retval = -EINVAL;
-               goto exit;
-       }
+       if (strcmp(action, "remove") == 0)
+               return remove_device(device);
 
-       retval = do_it(action, name, type, major, minor, mode);
-       if (retval) {
-               dbg ("do_it failed");
-               goto exit;
-       }
+       dbg("Unknown action: %s", action);
+       return -EINVAL;
 
        retval = 0;
 exit: