[systemd-commits] 2 commits - src/libudev src/udev

Kay Sievers kay at kemper.freedesktop.org
Thu Jul 5 08:43:04 PDT 2012


 src/libudev/libudev-queue-private.c |    2 -
 src/udev/cdrom_id/cdrom_id.c        |    2 -
 src/udev/keymap/keymap.c            |    6 +--
 src/udev/scsi_id/scsi_id.c          |    2 -
 src/udev/udev-event.c               |   69 ++++--------------------------------
 src/udev/udev-rules.c               |    8 ++--
 src/udev/udevd.c                    |    8 ++--
 7 files changed, 23 insertions(+), 74 deletions(-)

New commits:
commit 97595710b77aa162ca5e20da57d0a1ed7355eaad
Author: Kay Sievers <kay at vrfy.org>
Date:   Thu Jul 5 17:40:50 2012 +0200

    udev: network device renaming - immediately give up if the target name isn't available

diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c
index aff1487..46e579d 100644
--- a/src/udev/udev-event.c
+++ b/src/udev/udev-event.c
@@ -745,32 +745,11 @@ out:
         return err;
 }
 
-static void rename_netif_kernel_log(struct ifreq ifr)
-{
-        int klog;
-        FILE *f;
-
-        klog = open("/dev/kmsg", O_WRONLY|O_CLOEXEC);
-        if (klog < 0)
-                return;
-
-        f = fdopen(klog, "w");
-        if (f == NULL) {
-                close(klog);
-                return;
-        }
-
-        fprintf(f, "<30>udevd[%u]: renamed network interface %s to %s\n",
-                getpid(), ifr.ifr_name, ifr.ifr_newname);
-        fclose(f);
-}
-
 static int rename_netif(struct udev_event *event)
 {
         struct udev_device *dev = event->dev;
         int sk;
         struct ifreq ifr;
-        int loop;
         int err;
 
         log_debug("changing net interface name from '%s' to '%s'\n",
@@ -787,49 +766,19 @@ static int rename_netif(struct udev_event *event)
         util_strscpy(ifr.ifr_name, IFNAMSIZ, udev_device_get_sysname(dev));
         util_strscpy(ifr.ifr_newname, IFNAMSIZ, event->name);
         err = ioctl(sk, SIOCSIFNAME, &ifr);
-        if (err == 0) {
-                rename_netif_kernel_log(ifr);
-                goto out;
-        }
-
-        /* keep trying if the destination interface name already exists */
-        err = -errno;
-        if (err != -EEXIST)
-                goto out;
-
-        /* free our own name, another process may wait for us */
-        snprintf(ifr.ifr_newname, IFNAMSIZ, "rename%u", udev_device_get_ifindex(dev));
-        err = ioctl(sk, SIOCSIFNAME, &ifr);
-        if (err < 0) {
-                err = -errno;
-                goto out;
-        }
-
-        /* log temporary name */
-        rename_netif_kernel_log(ifr);
-
-        /* wait a maximum of 90 seconds for our target to become available */
-        util_strscpy(ifr.ifr_name, IFNAMSIZ, ifr.ifr_newname);
-        util_strscpy(ifr.ifr_newname, IFNAMSIZ, event->name);
-        loop = 90 * 20;
-        while (loop--) {
-                const struct timespec duration = { 0, 1000 * 1000 * 1000 / 20 };
-
-                nanosleep(&duration, NULL);
+        if (err >= 0) {
+                FILE *f;
 
-                err = ioctl(sk, SIOCSIFNAME, &ifr);
-                if (err == 0) {
-                        rename_netif_kernel_log(ifr);
-                        break;
+                f = fopen("/dev/kmsg", "we");
+                if (f != NULL) {
+                        fprintf(f, "<30>systemd-udevd[%u]: renamed network interface %s to %s\n",
+                                getpid(), ifr.ifr_name, ifr.ifr_newname);
+                        fclose(f);
                 }
+        } else {
                 err = -errno;
-                if (err != -EEXIST)
-                        break;
-        }
-
-out:
-        if (err < 0)
                 log_error("error changing net interface name %s to %s: %m\n", ifr.ifr_name, ifr.ifr_newname);
+        }
         close(sk);
         return err;
 }

commit 47ef94ac5f39db6c5c18be10afe32397a0a8d5cc
Author: Kay Sievers <kay at vrfy.org>
Date:   Thu Jul 5 17:33:24 2012 +0200

    udev: add some O_CLOEXEC

diff --git a/src/libudev/libudev-queue-private.c b/src/libudev/libudev-queue-private.c
index 3df99be..367395d 100644
--- a/src/libudev/libudev-queue-private.c
+++ b/src/libudev/libudev-queue-private.c
@@ -212,7 +212,7 @@ static int rebuild_queue_file(struct udev_queue_export *udev_queue_export)
         }
 
         /* create new queue file */
-        new_queue_file = fopen("/run/udev/queue.tmp", "w+");
+        new_queue_file = fopen("/run/udev/queue.tmp", "w+e");
         if (new_queue_file == NULL)
                 goto error;
         seqnum = udev_queue_export->seqnum_max;
diff --git a/src/udev/cdrom_id/cdrom_id.c b/src/udev/cdrom_id/cdrom_id.c
index a199dd7..74c5165 100644
--- a/src/udev/cdrom_id/cdrom_id.c
+++ b/src/udev/cdrom_id/cdrom_id.c
@@ -125,7 +125,7 @@ static bool is_mounted(const char *device)
         if (stat(device, &statbuf) < 0)
                 return -ENODEV;
 
-        fp = fopen("/proc/self/mountinfo", "r");
+        fp = fopen("/proc/self/mountinfo", "re");
         if (fp == NULL)
                 return -ENOSYS;
         while (fscanf(fp, "%*s %*s %i:%i %*[^\n]", &maj, &min) == 2) {
diff --git a/src/udev/keymap/keymap.c b/src/udev/keymap/keymap.c
index a2e43f9..051aa42 100644
--- a/src/udev/keymap/keymap.c
+++ b/src/udev/keymap/keymap.c
@@ -410,7 +410,7 @@ int main(int argc, char **argv)
                 const char *filearg = argv[optind+1];
                 if (strchr(filearg, '/')) {
                         /* Keymap file argument is a path */
-                        FILE *f = fopen(filearg, "r");
+                        FILE *f = fopen(filearg, "re");
                         if (f)
                                 merge_table(fd, f);
                         else
@@ -421,12 +421,12 @@ int main(int argc, char **argv)
                         char keymap_path[PATH_MAX];
                         FILE *f;
                         snprintf(keymap_path, sizeof(keymap_path), "%s%s", SYSCONFDIR "/udev/keymaps/", filearg);
-                        f = fopen(keymap_path, "r");
+                        f = fopen(keymap_path, "re");
                         if (f) {
                                 merge_table(fd, f);
                         } else {
                                 snprintf(keymap_path, sizeof(keymap_path), "%s%s", UDEVLIBEXECDIR "/keymaps/", filearg);
-                                f = fopen(keymap_path, "r");
+                                f = fopen(keymap_path, "re");
                                 if (f)
                                         merge_table(fd, f);
                                 else
diff --git a/src/udev/scsi_id/scsi_id.c b/src/udev/scsi_id/scsi_id.c
index dd8517b..6fc41e9 100644
--- a/src/udev/scsi_id/scsi_id.c
+++ b/src/udev/scsi_id/scsi_id.c
@@ -180,7 +180,7 @@ static int get_file_options(struct udev *udev,
         int c;
         int retval = 0;
 
-        fd = fopen(config_file, "r");
+        fd = fopen(config_file, "re");
         if (fd == NULL) {
                 if (errno == ENOENT) {
                         return 1;
diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c
index e6f405b..aff1487 100644
--- a/src/udev/udev-event.c
+++ b/src/udev/udev-event.c
@@ -750,7 +750,7 @@ static void rename_netif_kernel_log(struct ifreq ifr)
         int klog;
         FILE *f;
 
-        klog = open("/dev/kmsg", O_WRONLY);
+        klog = open("/dev/kmsg", O_WRONLY|O_CLOEXEC);
         if (klog < 0)
                 return;
 
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
index b5b54dd..c571c82 100644
--- a/src/udev/udev-rules.c
+++ b/src/udev/udev-rules.c
@@ -745,7 +745,7 @@ static int import_file_into_properties(struct udev_device *dev, const char *file
         FILE *f;
         char line[UTIL_LINE_SIZE];
 
-        f = fopen(filename, "r");
+        f = fopen(filename, "re");
         if (f == NULL)
                 return -1;
         while (fgets(line, sizeof(line), f) != NULL)
@@ -1645,7 +1645,7 @@ static int parse_file(struct udev_rules *rules, const char *filename)
         }
         log_debug("read rules file: %s\n", filename);
 
-        f = fopen(filename, "r");
+        f = fopen(filename, "re");
         if (f == NULL)
                 return -1;
 
@@ -2322,7 +2322,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
                         FILE *f;
                         bool imported = false;
 
-                        f = fopen("/proc/cmdline", "r");
+                        f = fopen("/proc/cmdline", "re");
                         if (f != NULL) {
                                 char cmdline[4096];
 
@@ -2607,7 +2607,7 @@ int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event
                         log_debug("ATTR '%s' writing '%s' %s:%u\n", attr, value,
                                   &rules->buf[rule->rule.filename_off],
                                   rule->rule.filename_line);
-                        f = fopen(attr, "w");
+                        f = fopen(attr, "we");
                         if (f != NULL) {
                                 if (fprintf(f, "%s", value) <= 0)
                                         log_error("error writing ATTR{%s}: %m\n", attr);
diff --git a/src/udev/udevd.c b/src/udev/udevd.c
index 393e2a9..176e4e2 100644
--- a/src/udev/udevd.c
+++ b/src/udev/udevd.c
@@ -807,7 +807,7 @@ static void static_dev_create_from_modules(struct udev *udev)
 
         uname(&kernel);
         util_strscpyl(modules, sizeof(modules), ROOTPREFIX "/lib/modules/", kernel.release, "/modules.devname", NULL);
-        f = fopen(modules, "r");
+        f = fopen(modules, "re");
         if (f == NULL)
                 return;
 
@@ -871,7 +871,7 @@ static int mem_size_mb(void)
         char buf[4096];
         long int memsize = -1;
 
-        f = fopen("/proc/meminfo", "r");
+        f = fopen("/proc/meminfo", "re");
         if (f == NULL)
                 return -1;
 
@@ -907,7 +907,7 @@ static int convert_db(struct udev *udev)
         if (access(filename, F_OK) < 0)
                 return 0;
 
-        f = fopen("/dev/kmsg", "w");
+        f = fopen("/dev/kmsg", "we");
         if (f != NULL) {
                 fprintf(f, "<30>systemd-udevd[%u]: converting old udev database\n", getpid());
                 fclose(f);
@@ -1264,7 +1264,7 @@ int main(int argc, char *argv[])
                 sd_notify(1, "READY=1");
         }
 
-        f = fopen("/dev/kmsg", "w");
+        f = fopen("/dev/kmsg", "we");
         if (f != NULL) {
                 fprintf(f, "<30>systemd-udevd[%u]: starting version " VERSION "\n", getpid());
                 fclose(f);



More information about the systemd-commits mailing list