]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev: event - keep one rtnl per worker, rather than per event
authorTom Gundersen <teg@jklm.no>
Tue, 9 Sep 2014 09:15:37 +0000 (11:15 +0200)
committerTom Gundersen <teg@jklm.no>
Tue, 9 Sep 2014 13:30:10 +0000 (15:30 +0200)
Creating the rtnl context is cheap, but freeing it may not be, due to
synchronous close().

Also drop some excessive logging. We now log about the changing ifname
exactly once.

src/libsystemd/sd-rtnl/rtnl-util.c
src/libsystemd/sd-rtnl/rtnl-util.h
src/udev/udev-event.c
src/udev/udev.h
src/udev/udevd.c

index 0bc2c9b1f5720f68772d4264c61bbafb19a49720..fe0f34e125de53c7c3b55641df489c46b46cb15b 100644 (file)
@@ -26,7 +26,7 @@
 #include "rtnl-util.h"
 #include "rtnl-internal.h"
 
-int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
+int rtnl_set_link_name(sd_rtnl **rtnl, int ifindex, const char *name) {
         _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
         int r;
 
@@ -34,7 +34,13 @@ int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
         assert(ifindex > 0);
         assert(name);
 
-        r = sd_rtnl_message_new_link(rtnl, &message, RTM_SETLINK, ifindex);
+        if (!*rtnl) {
+                r = sd_rtnl_open(rtnl, 0);
+                if (r < 0)
+                        return r;
+        }
+
+        r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
         if (r < 0)
                 return r;
 
@@ -42,7 +48,7 @@ int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
         if (r < 0)
                 return r;
 
-        r = sd_rtnl_call(rtnl, message, 0, NULL);
+        r = sd_rtnl_call(*rtnl, message, 0, NULL);
         if (r < 0)
                 return r;
 
index 2963f02d3ea81a81e831072adb3dc8fff129a7de..94af3b17200fd620f5c68b8f2e4ec913674a3a07 100644 (file)
@@ -34,7 +34,7 @@ bool rtnl_message_type_is_link(uint16_t type);
 bool rtnl_message_type_is_addr(uint16_t type);
 bool rtnl_message_type_is_route(uint16_t type);
 
-int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name);
+int rtnl_set_link_name(sd_rtnl **rtnl, int ifindex, const char *name);
 int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias, const struct ether_addr *mac, unsigned mtu);
 
 int rtnl_log_parse_error(int r);
index 18b92ca428769b08e79b7891ba37ef0ae4f72445..1bbf41e757744c38c15ec08fa9b3314b16f87892 100644 (file)
@@ -53,6 +53,7 @@ struct udev_event *udev_event_new(struct udev_device *dev) {
 void udev_event_unref(struct udev_event *event) {
         if (event == NULL)
                 return;
+        sd_rtnl_unref(event->rtnl);
         udev_list_cleanup(&event->run_list);
         udev_list_cleanup(&event->seclabel_list);
         free(event->program_result);
@@ -746,30 +747,24 @@ out:
 
 static int rename_netif(struct udev_event *event) {
         struct udev_device *dev = event->dev;
-        _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
         char name[IFNAMSIZ];
         const char *oldname;
         int r;
 
         oldname = udev_device_get_sysname(dev);
 
-        log_debug("changing net interface name from '%s' to '%s'",
-                  oldname, event->name);
-
         strscpy(name, IFNAMSIZ, event->name);
 
-        r = sd_rtnl_open(&rtnl, 0);
-        if (r < 0)
+        r = rtnl_set_link_name(&event->rtnl, udev_device_get_ifindex(dev), name);
+        if (r < 0) {
+                log_error("error changing net interface name '%s' to '%s': %s",
+                          oldname, name, strerror(-r));
                 return r;
+        }
 
-        r = rtnl_set_link_name(rtnl, udev_device_get_ifindex(dev), name);
-        if (r < 0)
-                log_error("error changing net interface name %s to %s: %s",
-                          oldname, name, strerror(-r));
-        else
-                print_kmsg("renamed network interface %s to %s\n", oldname, name);
+        print_kmsg("renamed network interface '%s' to '%s'\n", oldname, name);
 
-        return r;
+        return 0;
 }
 
 void udev_event_execute_rules(struct udev_event *event,
@@ -832,8 +827,6 @@ void udev_event_execute_rules(struct udev_event *event,
 
                         r = rename_netif(event);
                         if (r >= 0) {
-                                log_debug("renamed netif to '%s'", event->name);
-
                                 /* remember old name */
                                 udev_device_add_property(dev, "INTERFACE_OLD", udev_device_get_sysname(dev));
 
index faa8f566c267c63b1307e4beb034f3d4e3aca827..ed01da30e4e0514108ba9bd6f5562412a5f68cc3 100644 (file)
@@ -23,6 +23,7 @@
 #include <signal.h>
 
 #include "macro.h"
+#include "sd-rtnl.h"
 #include "libudev.h"
 #include "libudev-private.h"
 #include "util.h"
@@ -44,6 +45,7 @@ struct udev_event {
         int exec_delay;
         usec_t birth_usec;
         int fd_signal;
+        sd_rtnl *rtnl;
         unsigned int builtin_run;
         unsigned int builtin_ret;
         bool sigterm;
index e72c5b231e004aec76e9b16461acb8af6dfea094..be0acc3177138e95dac40f79c539b0ed3569ca56 100644 (file)
@@ -48,6 +48,7 @@
 
 #include "udev.h"
 #include "udev-util.h"
+#include "rtnl-util.h"
 #include "sd-daemon.h"
 #include "cgroup-util.h"
 #include "dev-setup.h"
@@ -200,6 +201,7 @@ static void worker_new(struct event *event) {
         case 0: {
                 struct udev_device *dev = NULL;
                 int fd_monitor;
+                _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
                 struct epoll_event ep_signal, ep_monitor;
                 sigset_t mask;
                 int rc = EXIT_SUCCESS;
@@ -301,11 +303,17 @@ static void worker_new(struct event *event) {
                                 }
                         }
 
+                        /* needed for renaming netifs */
+                        udev_event->rtnl = rtnl;
+
                         /* apply rules, create node, symlinks */
                         udev_event_execute_rules(udev_event, event_timeout_usec, rules, &sigmask_orig);
 
                         udev_event_execute_run(udev_event, event_timeout_usec, &sigmask_orig);
 
+                        /* in case rtnl was initialized */
+                        rtnl = sd_rtnl_ref(udev_event->rtnl);
+
                         /* apply/restore inotify watch */
                         if (udev_event->inotify_watch) {
                                 udev_watch_begin(udev, dev);