chiark / gitweb /
[PATCH] Make dev.d/ handling a separate processing stage
authorkay.sievers@vrfy.org <kay.sievers@vrfy.org>
Fri, 12 Nov 2004 05:21:16 +0000 (06:21 +0100)
committerGreg KH <gregkh@suse.de>
Wed, 27 Apr 2005 05:25:09 +0000 (22:25 -0700)
Move the logic when and how to call the dev.d/ scripts into the
main processing path.

dev_d.c
namedev.c
udev.c
udev.h
udev_add.c
udev_lib.c
udev_remove.c
udevdb.c
udevstart.c
wait_for_sysfs.c

diff --git a/dev_d.c b/dev_d.c
index 1108c8c..b3d05d9 100644 (file)
--- a/dev_d.c
+++ b/dev_d.c
@@ -32,9 +32,6 @@
 #include "udevdb.h"
 #include "logging.h"
 
-#define DEVD_DIR                       "/etc/dev.d/"
-#define DEVD_SUFFIX                    ".dev"
-
 static int run_program(const char *filename, void *data)
 {
        pid_t pid;
@@ -75,7 +72,7 @@ static int run_program(const char *filename, void *data)
  *     subsystem/
  *     default/
  */
-void dev_d_execute(struct udevice *udev)
+void dev_d_execute(struct udevice *udev, const char *basedir, const char *suffix)
 {
        char dirname[PATH_MAX];
        char devname[NAME_SIZE];
@@ -85,37 +82,30 @@ void dev_d_execute(struct udevice *udev)
        if (udev_dev_d == 0)
                return;
 
-       /* skip if udev did nothing, like unchanged netif or no "dev" file */
-       if (udev->devname[0] == '\0')
-               return;
-
-       /* add the node name or the netif name to the environment */
-       setenv("DEVNAME", udev->devname, 1);
-       dbg("DEVNAME='%s'", udev->devname);
-
        strfieldcpy(devname, udev->name);
 
-       /* Chop the device name up into pieces based on '/' */
+       /* chop the device name up into pieces based on '/' */
        temp = strchr(devname, '/');
        while (temp != NULL) {
                temp[0] = '\0';
-               strcpy(dirname, DEVD_DIR);
-               strfieldcat(dirname, devname);
-               call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev);
+               snprintf(dirname, PATH_MAX, "%s/%s", basedir, devname);
+               dirname[PATH_MAX-1] = '\0';
+               call_foreach_file(run_program, dirname, suffix, udev);
 
                temp[0] = '/';
                ++temp;
                temp = strchr(temp, '/');
        }
 
-       strcpy(dirname, DEVD_DIR);
-       strfieldcat(dirname, udev->name);
-       call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev);
+       snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->name);
+       dirname[PATH_MAX-1] = '\0';
+       call_foreach_file(run_program, dirname, suffix, udev);
 
-       strcpy(dirname, DEVD_DIR);
-       strfieldcat(dirname, udev->subsystem);
-       call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev);
+       snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->subsystem);
+       dirname[PATH_MAX-1] = '\0';
+       call_foreach_file(run_program, dirname, suffix, udev);
 
-       strcpy(dirname, DEVD_DIR "default");
-       call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev);
+       snprintf(dirname, PATH_MAX, "%s/default", basedir);
+       dirname[PATH_MAX-1] = '\0';
+       call_foreach_file(run_program, dirname, suffix, udev);
 }
index 268a3d5..9c94e69 100644 (file)
--- a/namedev.c
+++ b/namedev.c
@@ -204,7 +204,7 @@ static int find_free_number(struct udevice *udev, const char *name)
                        info("find_free_number gone crazy (num=%d), aborted", num);
                        return -1;
                }
-               snprintf(filename, NAME_SIZE-1, "%s%d", name, num);
+               snprintf(filename, NAME_SIZE, "%s%d", name, num);
                filename[NAME_SIZE-1] = '\0';
        }
 }
diff --git a/udev.c b/udev.c
index 300e85c..bb3385e 100644 (file)
--- a/udev.c
+++ b/udev.c
@@ -165,8 +165,11 @@ int main(int argc, char *argv[], char *envp[])
                /* name, create node, store in db */
                retval = udev_add_device(&udev, class_dev);
 
-               /* run scripts */
-               dev_d_execute(&udev);
+               /* run dev.d/ scripts if we created a node or changed a netif name */
+               if (udev.devname[0] != '\0') {
+                       setenv("DEVNAME", udev.devname, 1);
+                       dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX);
+               }
 
                sysfs_close_class_device(class_dev);
                break;
@@ -177,7 +180,7 @@ int main(int argc, char *argv[], char *envp[])
                retval = udev_remove_device(&udev);
 
                /* run scripts */
-               dev_d_execute(&udev);
+               dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX);
        }
 
 exit:
diff --git a/udev.h b/udev.h
index d031b96..73733ad 100644 (file)
--- a/udev.h
+++ b/udev.h
@@ -41,6 +41,9 @@
 
 #define LINE_SIZE                      256
 
+#define DEVD_DIR                       "/etc/dev.d"
+#define DEVD_SUFFIX                    ".dev"
+
 struct udevice {
        char devpath[DEVPATH_SIZE];
        char subsystem[SUBSYSTEM_SIZE];
@@ -71,7 +74,7 @@ extern int udev_remove_device(struct udevice *udev);
 extern void udev_init_config(void);
 extern int udev_start(void);
 extern int parse_get_pair(char **orig_string, char **left, char **right);
-extern void dev_d_execute(struct udevice *udev);
+extern void dev_d_execute(struct udevice *udev, const char *basedir, const char *suffix);
 
 extern char sysfs_path[SYSFS_PATH_MAX];
 extern char udev_root[PATH_MAX];
index 60506b4..c139bd3 100644 (file)
@@ -129,7 +129,7 @@ static int create_node(struct udevice *udev)
        char *pos;
        int len;
 
-       snprintf(filename, NAME_SIZE-1, "%s/%s", udev_root, udev->name);
+       snprintf(filename, NAME_SIZE, "%s/%s", udev_root, udev->name);
        filename[NAME_SIZE-1] = '\0';
 
        switch (udev->type) {
@@ -210,7 +210,7 @@ static int create_node(struct udevice *udev)
                char linktarget[NAME_SIZE];
 
                strfieldcpymax(linkname, pos, len+1);
-               snprintf(filename, NAME_SIZE-1, "%s/%s", udev_root, linkname);
+               snprintf(filename, NAME_SIZE, "%s/%s", udev_root, linkname);
                filename[NAME_SIZE-1] = '\0';
 
                dbg("symlink '%s' to node '%s' requested", filename, udev->name);
@@ -308,7 +308,7 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
                            "remove might not work for custom names");
 
                /* use full path to the environment */
-               snprintf(udev->devname, NAME_SIZE-1, "%s/%s", udev_root, udev->name);
+               snprintf(udev->devname, NAME_SIZE, "%s/%s", udev_root, udev->name);
                udev->devname[NAME_SIZE-1] = '\0';
 
        } else if (udev->type == 'n') {
index 012d60b..6807f81 100644 (file)
@@ -219,7 +219,7 @@ int call_foreach_file(file_fnct_t fnct, const char *dirname,
        list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) {
                char filename[NAME_SIZE];
 
-               snprintf(filename, NAME_SIZE-1, "%s/%s", dirname, loop_file->name);
+               snprintf(filename, NAME_SIZE, "%s/%s", dirname, loop_file->name);
                filename[NAME_SIZE-1] = '\0';
 
                fnct(filename, data);
index d855fce..56834a4 100644 (file)
@@ -112,7 +112,7 @@ static int delete_node(struct udevice *udev)
        int len;
        int num;
 
-       snprintf(filename, NAME_SIZE-1, "%s/%s", udev_root, udev->name);
+       snprintf(filename, NAME_SIZE, "%s/%s", udev_root, udev->name);
        filename[NAME_SIZE-1] = '\0';
 
        info("removing device node '%s'", filename);
@@ -143,7 +143,7 @@ static int delete_node(struct udevice *udev)
                char linkname[NAME_SIZE];
 
                strfieldcpymax(linkname, pos, len+1);
-               snprintf(filename, NAME_SIZE-1, "%s/%s", udev_root, linkname);
+               snprintf(filename, NAME_SIZE, "%s/%s", udev_root, linkname);
                filename[NAME_SIZE-1] = '\0';
 
                dbg("unlinking symlink '%s'", filename);
@@ -189,7 +189,7 @@ int udev_remove_device(struct udevice *udev)
        udevdb_delete_dev(udev);
 
        /* use full path to the environment */
-       snprintf(udev->devname, NAME_SIZE-1, "%s/%s", udev_root, udev->name);
+       snprintf(udev->devname, NAME_SIZE, "%s/%s", udev_root, udev->name);
 
        return delete_node(udev);
 }
index fc579a9..38830b0 100644 (file)
--- a/udevdb.c
+++ b/udevdb.c
@@ -51,7 +51,7 @@ static int get_db_filename(struct udevice *udev, char *filename, int len)
                pos[0] = PATH_TO_NAME_CHAR;
                pos = strchr(&pos[1], '/');
        }
-       snprintf(filename, len-1, "%s%s", udev_db_path, devpath);
+       snprintf(filename, len, "%s%s", udev_db_path, devpath);
        filename[len-1] = '\0';
 
        return 0;
@@ -175,7 +175,7 @@ int udevdb_get_dev_byname(struct udevice *udev, const char *name)
                if (ent->d_name[0] == '.')
                        continue;
 
-               snprintf(filename, NAME_SIZE-1, "%s/%s", udev_db_path, ent->d_name);
+               snprintf(filename, NAME_SIZE, "%s/%s", udev_db_path, ent->d_name);
                filename[NAME_SIZE-1] = '\0';
 
                memset(&db_udev, 0x00, sizeof(struct udevice));
index 0433e56..c1d877f 100644 (file)
@@ -97,7 +97,8 @@ static int add_device(char *devpath, char *subsystem)
        setenv("DEVPATH", devpath, 1);
        setenv("SUBSYSTEM", subsystem, 1);
 
-       snprintf(path, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
+       snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath);
+       path[SYSFS_PATH_MAX-1] = '\0';
        class_dev = sysfs_open_class_device_path(path);
        if (class_dev == NULL) {
                dbg ("sysfs_open_class_device_path failed");
@@ -107,8 +108,11 @@ static int add_device(char *devpath, char *subsystem)
        udev_set_values(&udev, devpath, subsystem, "add");
        udev_add_device(&udev, class_dev);
 
-       /* run scripts */
-       dev_d_execute(&udev);
+       /* run dev.d/ scripts if we created a node or changed a netif name */
+       if (udev.devname[0] != '\0') {
+               setenv("DEVNAME", udev.devname, 1);
+               dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX);
+       }
 
        sysfs_close_class_device(class_dev);
 
index 3b1b0cd..42b9d38 100644 (file)
@@ -96,7 +96,7 @@ int main(int argc, char *argv[], char *envp[])
        }
 
        if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
-               snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_mnt_path, devpath);
+               snprintf(filename, SYSFS_PATH_MAX, "%s%s", sysfs_mnt_path, devpath);
                filename[SYSFS_PATH_MAX-1] = '\0';
 
                /* skip bad events where we get no device for the class */
@@ -128,7 +128,7 @@ int main(int argc, char *argv[], char *envp[])
                sysfs_close_class_device(class_dev);
 
        } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
-               snprintf(filename, SYSFS_PATH_MAX-1, "%s%s", sysfs_mnt_path, devpath);
+               snprintf(filename, SYSFS_PATH_MAX, "%s%s", sysfs_mnt_path, devpath);
                filename[SYSFS_PATH_MAX-1] = '\0';
 
                /* open the path we are called for */