chiark / gitweb /
[PATCH] cleanup netif handling and netif-dev.d/ events
authorkay.sievers@vrfy.org <kay.sievers@vrfy.org>
Tue, 19 Oct 2004 02:28:39 +0000 (19:28 -0700)
committerGreg KH <gregkh@suse.de>
Wed, 27 Apr 2005 05:02:46 +0000 (22:02 -0700)
Here we supress the dev.d/ execution if we didn't change a network
interface's name with a rule. This should solve the issue of two
running dhclients for the same interface, cause the
/etc/dev.d/net/hotplug.dev script that fakes the hotplug event runs
with every udevstart for every interface and fakes a second identical
hotplug event on bootup.

With this patch netif interfaces are no longer stored in the udevdb.
It is not needed, cause we don't have permissions or symlinks :) and
all information is available in sysfs.

This patch also moves the dev_d execution calls out of the
udev_add/udev_remove. As with the former api-cleanup-patch we have
all processed data in one udev struct and can place the execution
calls where needed.

dev_d.c
udev.c
udev.h
udev_add.c
udev_remove.c
udevdb.c
udevstart.c
udevtest.c

diff --git a/dev_d.c b/dev_d.c
index 5580b5a3627ea09ee59ed9acd47000cc0190d750..be85f527759d1d838040165fdfd13d4c9cf4f64e 100644 (file)
--- a/dev_d.c
+++ b/dev_d.c
@@ -62,7 +62,7 @@ static int run_program(char *name)
 
                execv(name, argv);
                dbg("exec of child failed");
-               exit(1);
+               _exit(1);
        case -1:
                dbg("fork of child failed");
                break;
@@ -80,42 +80,35 @@ static int run_program(char *name)
  *     subsystem/
  *     default/
  */
-void dev_d_send(struct udevice *udev)
+void dev_d_execute(struct udevice *udev)
 {
-       char dirname[256];
-       char env_devname[NAME_SIZE];
-       char *devname;
+       char dirname[PATH_MAX];
+       char devname[NAME_SIZE];
        char *temp;
 
+       /* skip if UDEV_NO_DEVD is set */
        if (udev_dev_d == 0)
                return;
 
-       memset(env_devname, 0x00, sizeof(env_devname));
-       if (udev->type == 'b' || udev->type == 'c') {
-               strfieldcpy(env_devname, udev_root);
-               strfieldcat(env_devname, udev->name);
-       } else if (udev->type == 'n') {
-               strfieldcpy(env_devname, udev->name);
-               setenv("DEVPATH", udev->devpath, 1);
-       }
-       setenv("DEVNAME", env_devname, 1);
-       dbg("DEVNAME='%s'", env_devname);
-
-       devname = strdup(udev->name);
-       if (!devname) {
-               dbg("out of memory");
+       /* 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 '/' */
        temp = strchr(devname, '/');
        while (temp != NULL) {
-               *temp = 0x00;
+               temp[0] = '\0';
                strcpy(dirname, DEVD_DIR);
                strfieldcat(dirname, devname);
                call_foreach_file(run_program, dirname, DEVD_SUFFIX);
 
-               *temp = '/';
+               temp[0] = '/';
                ++temp;
                temp = strchr(temp, '/');
        }
diff --git a/udev.c b/udev.c
index 8d5be0542e8d3edff7fabc74f388eaeecb01ade2..dfb2baddbc34dfdabbf5264bd9abf587d68304a2 100644 (file)
--- a/udev.c
+++ b/udev.c
@@ -210,10 +210,20 @@ 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);
+
+               sysfs_close_class_device(class_dev);
                break;
        case REMOVE:
                dbg("udev remove");
+
+               /* get node from db, delete it*/
                retval = udev_remove_device(&udev);
+
+               /* run scripts */
+               dev_d_execute(&udev);
        }
 
        udevdb_exit();
diff --git a/udev.h b/udev.h
index 70de729f30033c7a92f94013e343011703f96f64..f7a13a634b77e75fff756248e0ca98df7579b0a2 100644 (file)
--- a/udev.h
+++ b/udev.h
@@ -66,6 +66,7 @@ struct udevice {
        char program_result[NAME_SIZE];
        char kernel_number[NAME_SIZE];
        char kernel_name[NAME_SIZE];
+       char devname[NAME_SIZE];
        int test_run;
 };
 
@@ -74,7 +75,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_send(struct udevice *udev);
+extern void dev_d_execute(struct udevice *udev);
 
 extern char **main_argv;
 extern char **main_envp;
index d07120de62a14103b443c36a11076a669f6fab07..809a33cedb664f7f4dafab1dde562982e85ccdef 100644 (file)
@@ -355,7 +355,7 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
                retval = get_major_minor(class_dev, udev);
                if (retval != 0) {
                        dbg("no dev-file found, do nothing");
-                       goto close;
+                       return 0;
                }
        }
 
@@ -365,45 +365,44 @@ int udev_add_device(struct udevice *udev, struct sysfs_class_device *class_dev)
        dbg("adding name='%s'", udev->name);
 
        selinux_init();
-       switch (udev->type) {
-       case 'b':
-       case 'c':
+
+       if (udev->type == 'b' || udev->type == 'c') {
                retval = create_node(udev);
                if (retval != 0)
                        goto exit;
-               if ((!udev->test_run) && (udevdb_add_dev(udev) != 0))
-                       dbg("udevdb_add_dev failed, but we are going to try "
-                           "to create the node anyway. But remove might not "
-                           "work properly for this device.");
 
-               dev_d_send(udev);
-               break;
+               if (udevdb_add_dev(udev) != 0)
+                       dbg("udevdb_add_dev failed, but we create the node anyway, "
+                           "remove might not work for custom names");
 
-       case 'n':
+               /* use full path to the environment */
+               snprintf(udev->devname, NAME_SIZE-1, "%s%s", udev_root, udev->name);
+
+       } else if (udev->type == 'n') {
+               /* look if we want to change the name of the netif */
                if (strcmp(udev->name, udev->kernel_name) != 0) {
                        retval = rename_net_if(udev);
                        if (retval != 0)
                                goto exit;
-                       /* netif's are keyed with the configured name, cause
-                        * the original kernel name sleeps with the fishes
+
+                       /* we've changed the name, now fake the devpath,
+                        * cause original kernel name sleeps with the fishes
+                        * and we don't get any event from the kernel now
                         */
                        pos = strrchr(udev->devpath, '/');
                        if (pos != NULL) {
                                pos[1] = '\0';
                                strfieldcat(udev->devpath, udev->name);
+                               setenv("DEVPATH", udev->devpath, 1);
                        }
-               }
-               if ((!udev->test_run) && (udevdb_add_dev(udev) != 0))
-                       dbg("udevdb_add_dev failed");
 
-               dev_d_send(udev);
-               break;
+                       /* use netif name for the environment */
+                       strfieldcpy(udev->devname, udev->name);
+               }
        }
 
 exit:
        selinux_restore();
-close:
-       sysfs_close_class_device(class_dev);
 
        return retval;
 }
index d97a2411f4d7fe587d8a330e4b5a8e656be9f93e..0dcec731ead0457745c49771b4a061448f5d64f4 100644 (file)
@@ -36,7 +36,7 @@
 #include "namedev.h"
 #include "udevdb.h"
 
-static int delete_path(char *path)
+static int delete_path(const char *path)
 {
        char *pos;
        int retval;
@@ -168,14 +168,15 @@ static int delete_node(struct udevice *dev)
 int udev_remove_device(struct udevice *udev)
 {
        struct udevice db_dev;
-       char *temp;
+       const char *temp;
        int retval;
 
-       memset(&db_dev, 0x00, sizeof(struct udevice));
+       if (udev->type != 'b' && udev->type != 'c')
+               return 0;
 
        retval = udevdb_get_dev(udev->devpath, &db_dev);
        if (retval == 0) {
-               /* get stored values in our device */
+               /* copy over the stored values to our device */
                memcpy(udev, &db_dev, UDEVICE_DB_LEN);
        } else {
                /* fall back to kernel name */
@@ -185,15 +186,12 @@ int udev_remove_device(struct udevice *udev)
                strfieldcpy(udev->name, &temp[1]);
                dbg("'%s' not found in database, falling back on default name", udev->name);
        }
-       dbg("remove name='%s'", udev->name);
 
-       dev_d_send(udev);
+       dbg("remove name='%s'", udev->name);
        udevdb_delete_dev(udev->devpath);
 
-       if (udev->type == 'b' || udev->type == 'c')
-               retval = delete_node(udev);
-       else
-               retval = 0;
+       /* use full path to the environment */
+       snprintf(udev->devname, NAME_SIZE-1, "%s%s", udev_root, udev->name);
 
-       return retval;
+       return delete_node(udev);
 }
index 3d0a9ea3c2b6cc2a646ebe6b3bf7f5fc88ffd7bc..23de22751d47802714d479df05ac5d997675580b 100644 (file)
--- a/udevdb.c
+++ b/udevdb.c
@@ -49,6 +49,9 @@ int udevdb_add_dev(struct udevice *udev)
        TDB_DATA key, data;
        char keystr[SYSFS_PATH_MAX];
 
+       if (udev->test_run)
+               return 0;
+
        if (udevdb == NULL)
                return -1;
 
index c4ec0f7b7abd132399d7fc77f73e2f46d5db7c20..fd490f0791688ea8b0d4bd5d045f1431546e62c1 100644 (file)
@@ -103,7 +103,7 @@ static int add_device(char *devpath, char *subsystem)
        setenv("DEVPATH", devpath, 1);
        setenv("ACTION", "add", 1);
 
-       snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath);
+       snprintf(path, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath);
        class_dev = sysfs_open_class_device_path(path);
        if (class_dev == NULL) {
                dbg ("sysfs_open_class_device_path failed");
@@ -111,8 +111,14 @@ static int add_device(char *devpath, char *subsystem)
        }
 
        udev_set_values(&udev, devpath, subsystem);
+       udev_add_device(&udev, class_dev);
 
-       return udev_add_device(&udev, class_dev);
+       /* run scripts */
+       dev_d_execute(&udev);
+
+       sysfs_close_class_device(class_dev);
+
+       return 0;
 }
 
 static void exec_list(struct list_head *device_list)
index fa1629af2552197023f35b5facb51613082b4a37..f2b0c98edb2dd35a720c2960bbdd775c1a468098 100644 (file)
@@ -69,12 +69,9 @@ int main(int argc, char *argv[], char *envp[])
 
        if (argv[1] == NULL) {
                info("udevinfo expects the DEVPATH of the sysfs device as a argument");
-               goto exit;
+               return 1;
        }
 
-       /* initialize our configuration */
-       udev_init_config();
-
        /* remove sysfs_path if given */
        if (strncmp(argv[1], sysfs_path, strlen(sysfs_path)) == 0)
                devpath = argv[1] + strlen(sysfs_path);
@@ -93,9 +90,12 @@ int main(int argc, char *argv[], char *envp[])
        /* we only care about class devices and block stuff */
        if (!strstr(devpath, "class") && !strstr(devpath, "block")) {
                dbg("not a block or class device");
-               goto exit;
+               return 2;
        }
 
+       /* initialize our configuration */
+       udev_init_config();
+
        /* initialize the naming deamon */
        namedev_init();
 
@@ -104,7 +104,6 @@ int main(int argc, char *argv[], char *envp[])
 
        /* fill in values and test_run flag*/
        udev_set_values(&udev, devpath, subsystem);
-       udev.test_run = 1;
 
        /* open the device */
        snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
@@ -114,9 +113,11 @@ int main(int argc, char *argv[], char *envp[])
        else
                dbg("opened class_dev->name='%s'", class_dev->name);
 
-       /* simulate node creation with fake flag */
+       /* simulate node creation with test flag */
+       udev.test_run = 1;
        udev_add_device(&udev, class_dev);
 
-exit:
+       sysfs_close_class_device(class_dev);
+
        return 0;
 }