[systemd-commits] 3 commits - Makefile.am src/libsystemd-rtnl src/systemd src/udev

Tom Gundersen tomegun at kemper.freedesktop.org
Tue Oct 29 21:41:10 CET 2013


 Makefile.am                         |    5 +++
 src/libsystemd-rtnl/rtnl-internal.h |    3 +-
 src/libsystemd-rtnl/rtnl-message.c  |   26 ++++++++++++++-----
 src/libsystemd-rtnl/sd-rtnl.c       |    9 ++++++
 src/libsystemd-rtnl/test-rtnl.c     |   16 ++++++++++--
 src/systemd/sd-rtnl.h               |    2 +
 src/udev/net/link-config.c          |   48 +-----------------------------------
 src/udev/udev-event.c               |   46 +++++++++++++++-------------------
 8 files changed, 72 insertions(+), 83 deletions(-)

New commits:
commit 16d26d559df69ad394d406fce679e45960eb15ef
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue Oct 29 20:03:26 2013 +0100

    udev-event: use rtnl to set ifname

diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c
index c4603a0c..bdf3a38 100644
--- a/src/udev/udev-event.c
+++ b/src/udev/udev-event.c
@@ -25,16 +25,14 @@
 #include <string.h>
 #include <time.h>
 #include <net/if.h>
-#include <sys/ioctl.h>
 #include <sys/prctl.h>
 #include <sys/poll.h>
 #include <sys/epoll.h>
 #include <sys/wait.h>
-#include <sys/socket.h>
 #include <sys/signalfd.h>
-#include <linux/sockios.h>
 
 #include "udev.h"
+#include "rtnl-util.h"
 
 struct udev_event *udev_event_new(struct udev_device *dev)
 {
@@ -750,32 +748,30 @@ out:
 static int rename_netif(struct udev_event *event)
 {
         struct udev_device *dev = event->dev;
-        int sk;
-        struct ifreq ifr;
-        int err;
+        _cleanup_sd_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'\n",
-                  udev_device_get_sysname(dev), event->name);
+                  oldname, event->name);
 
-        sk = socket(PF_INET, SOCK_DGRAM, 0);
-        if (sk < 0) {
-                err = -errno;
-                log_error("error opening socket: %m\n");
-                return err;
-        }
+        strscpy(name, IFNAMSIZ, event->name);
 
-        memset(&ifr, 0x00, sizeof(struct ifreq));
-        strscpy(ifr.ifr_name, IFNAMSIZ, udev_device_get_sysname(dev));
-        strscpy(ifr.ifr_newname, IFNAMSIZ, event->name);
-        err = ioctl(sk, SIOCSIFNAME, &ifr);
-        if (err >= 0) {
-                print_kmsg("renamed network interface %s to %s\n", ifr.ifr_name, ifr.ifr_newname);
-        } else {
-                err = -errno;
-                log_error("error changing net interface name %s to %s: %m\n", ifr.ifr_name, ifr.ifr_newname);
-        }
-        close(sk);
-        return err;
+        r = sd_rtnl_open(0, &rtnl);
+        if (r < 0)
+                return r;
+
+        r = rtnl_set_link_properties(rtnl, udev_device_get_ifindex(dev), name, NULL, 0);
+        if (r < 0)
+                log_error("error changing net interface name %s to %s: %s",
+                          oldname, name, strerror(-r));
+        else
+                log_info("renamed network interface %s to %s", oldname, name);
+
+        return r;
 }
 
 int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules, const sigset_t *sigmask)

commit 3aeb37bc4f32b5edc334f2ac7c5d3c7b0a121328
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue Oct 29 19:55:54 2013 +0100

    rtnl: move set_link_properties to rtnl-utils

diff --git a/Makefile.am b/Makefile.am
index a06a79b..85b2325 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -183,6 +183,7 @@ AM_CPPFLAGS = \
 	-I $(top_srcdir)/src/udev/net \
 	-I $(top_builddir)/src/udev \
 	-I $(top_srcdir)/src/libsystemd-bus \
+	-I $(top_srcdir)/src/libsystemd-rtnl \
 	$(OUR_CPPFLAGS)
 
 AM_CFLAGS = $(OUR_CFLAGS)
@@ -627,7 +628,9 @@ libsystemd_rtnl_la_SOURCES = \
 	src/systemd/sd-rtnl.h \
 	src/libsystemd-rtnl/sd-rtnl.c \
 	src/libsystemd-rtnl/rtnl-internal.h \
-	src/libsystemd-rtnl/rtnl-message.c
+	src/libsystemd-rtnl/rtnl-message.c \
+	src/libsystemd-rtnl/rtnl-util.h \
+	src/libsystemd-rtnl/rtnl-util.c
 
 test_rtnl_SOURCES = \
 	src/libsystemd-rtnl/test-rtnl.c
diff --git a/src/systemd/sd-rtnl.h b/src/systemd/sd-rtnl.h
index a2400d8..ae0dcd8 100644
--- a/src/systemd/sd-rtnl.h
+++ b/src/systemd/sd-rtnl.h
@@ -19,6 +19,8 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#pragma once
+
 #include <linux/rtnetlink.h>
 #include <linux/netlink.h>
 #include <stdint.h>
diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c
index f93a4d8..cf966bf 100644
--- a/src/udev/net/link-config.c
+++ b/src/udev/net/link-config.c
@@ -37,6 +37,7 @@
 #include "conf-files.h"
 #include "fileio.h"
 #include "hashmap.h"
+#include "rtnl-util.h"
 
 struct link_config_ctx {
         LIST_HEAD(link_config, links);
@@ -266,51 +267,6 @@ int link_config_get(link_config_ctx *ctx, struct udev_device *device, link_confi
         return -ENOENT;
 }
 
-static int rtnl_set_properties(sd_rtnl *rtnl, int ifindex, const char *name, const struct ether_addr *mac, unsigned int mtu) {
-        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
-        bool need_update = false;
-        int r;
-
-        assert(rtnl);
-        assert(ifindex > 0);
-
-        r = sd_rtnl_message_link_new(RTM_NEWLINK, ifindex, 0, 0, &message);
-        if (r < 0)
-                return r;
-
-        if (name) {
-                r = sd_rtnl_message_append(message, IFLA_IFNAME, name);
-                if (r < 0)
-                        return r;
-
-                need_update = true;
-        }
-
-        if (mac) {
-                r = sd_rtnl_message_append(message, IFLA_ADDRESS, mac);
-                if (r < 0)
-                        return r;
-
-                need_update = true;
-        }
-
-        if (mtu > 0) {
-                r = sd_rtnl_message_append(message, IFLA_MTU, &mtu);
-                if (r < 0)
-                        return r;
-
-                need_update = true;
-        }
-
-        if  (need_update) {
-                r = sd_rtnl_send_with_reply_and_block(rtnl, message, 0, NULL);
-                if (r < 0)
-                        return r;
-        }
-
-        return 0;
-}
-
 static bool enable_name_policy(void) {
         _cleanup_free_ char *line;
         char *w, *state;
@@ -498,7 +454,7 @@ int link_config_apply(link_config_ctx *ctx, link_config *config, struct udev_dev
                         mac = config->mac;
         }
 
-        r = rtnl_set_properties(ctx->rtnl, ifindex, new_name, mac, config->mtu);
+        r = rtnl_set_link_properties(ctx->rtnl, ifindex, new_name, mac, config->mtu);
         if (r < 0) {
                 log_warning("Could not set Name, MACAddress or MTU on %s: %s", name, strerror(-r));
                 return r;

commit d4bbdb77aff9abb1aaf13f1f92fb5f9513688ce1
Author: Tom Gundersen <teg at jklm.no>
Date:   Tue Oct 29 21:20:25 2013 +0100

    rtnl: fix sockaddr confusion

diff --git a/src/libsystemd-rtnl/rtnl-internal.h b/src/libsystemd-rtnl/rtnl-internal.h
index c24d12b..70bb6d0 100644
--- a/src/libsystemd-rtnl/rtnl-internal.h
+++ b/src/libsystemd-rtnl/rtnl-internal.h
@@ -31,7 +31,8 @@ struct sd_rtnl {
                 struct sockaddr_nl nl;
         } sockaddr;
 
-        unsigned serial;
+        uint32_t serial;
+
         pid_t original_pid;
 };
 
diff --git a/src/libsystemd-rtnl/rtnl-message.c b/src/libsystemd-rtnl/rtnl-message.c
index 557e690..eae73a6 100644
--- a/src/libsystemd-rtnl/rtnl-message.c
+++ b/src/libsystemd-rtnl/rtnl-message.c
@@ -344,13 +344,19 @@ static int message_receive_need(sd_rtnl *rtnl, size_t *need) {
 
 /* returns the number of bytes sent, or a negative error code */
 int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) {
+        union {
+                struct sockaddr sa;
+                struct sockaddr_nl nl;
+        } addr = {
+                .nl.nl_family = AF_NETLINK,
+        };
         ssize_t k;
 
         assert_return(nl, -EINVAL);
         assert_return(m, -EINVAL);
 
         k = sendto(nl->fd, m->hdr, m->hdr->nlmsg_len,
-                        0, &nl->sockaddr.sa, sizeof(nl->sockaddr));
+                        0, &addr.sa, sizeof(addr));
         if (k < 0)
                 return (errno == EAGAIN) ? 0 : -errno;
 
@@ -364,7 +370,11 @@ int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m) {
  */
 int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
         sd_rtnl_message *m;
-        socklen_t addr_len = sizeof(nl->sockaddr);
+        union {
+                struct sockaddr sa;
+                struct sockaddr_nl nl;
+        } addr;
+        socklen_t addr_len;
         int r;
         ssize_t k;
         size_t need;
@@ -380,21 +390,23 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
         if (r < 0)
                 return r;
 
+        addr_len = sizeof(addr);
+
         k = recvfrom(nl->fd, m->hdr, need,
-                        0, &nl->sockaddr.sa, &addr_len);
+                        0, &addr.sa, &addr_len);
         if (k < 0)
                 k = (errno == EAGAIN) ? 0 : -errno; /* no data */
         else if (k == 0)
                 k = -ECONNRESET; /* connection was closed by the kernel */
-        else if (addr_len != sizeof(nl->sockaddr.nl) ||
-                        nl->sockaddr.nl.nl_family != AF_NETLINK)
+        else if (addr_len != sizeof(addr.nl) ||
+                        addr.nl.nl_family != AF_NETLINK)
                 k = -EIO; /* not a netlink message */
-        else if (nl->sockaddr.nl.nl_pid != 0)
+        else if (addr.nl.nl_pid != 0)
                 k = 0; /* not from the kernel */
         else if ((size_t) k < sizeof(struct nlmsghdr) ||
                         (size_t) k < m->hdr->nlmsg_len)
                 k = -EIO; /* too small (we do accept too big though) */
-        else if ((pid_t) m->hdr->nlmsg_pid != getpid())
+        else if (m->hdr->nlmsg_pid != nl->sockaddr.nl.nl_pid)
                 k = 0; /* not for us */
 
         if (k > 0)
diff --git a/src/libsystemd-rtnl/sd-rtnl.c b/src/libsystemd-rtnl/sd-rtnl.c
index 9c1f40e..ed145b9 100644
--- a/src/libsystemd-rtnl/sd-rtnl.c
+++ b/src/libsystemd-rtnl/sd-rtnl.c
@@ -60,6 +60,7 @@ static bool rtnl_pid_changed(sd_rtnl *rtnl) {
 
 int sd_rtnl_open(uint32_t groups, sd_rtnl **ret) {
         _cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
+        socklen_t addrlen;
         int r;
 
         r = sd_rtnl_new(&rtnl);
@@ -72,10 +73,16 @@ int sd_rtnl_open(uint32_t groups, sd_rtnl **ret) {
 
         rtnl->sockaddr.nl.nl_groups = groups;
 
-        r = bind(rtnl->fd, &rtnl->sockaddr.sa, sizeof(rtnl->sockaddr));
+        addrlen = sizeof(rtnl->sockaddr);
+
+        r = bind(rtnl->fd, &rtnl->sockaddr.sa, addrlen);
         if (r < 0)
                 return -errno;
 
+        r = getsockname(rtnl->fd, &rtnl->sockaddr.sa, &addrlen);
+        if (r < 0)
+                return r;
+
         *ret = rtnl;
         rtnl = NULL;
 
diff --git a/src/libsystemd-rtnl/test-rtnl.c b/src/libsystemd-rtnl/test-rtnl.c
index 1cdd6e1..601548b 100644
--- a/src/libsystemd-rtnl/test-rtnl.c
+++ b/src/libsystemd-rtnl/test-rtnl.c
@@ -51,7 +51,17 @@ static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
         assert(type == IFLA_MTU);
         assert(mtu == *(unsigned int *) data);
 
-        assert(sd_rtnl_send_with_reply_and_block(rtnl, message, 2 * USEC_PER_SEC, NULL) == 0);
+        assert(sd_rtnl_send_with_reply_and_block(rtnl, message, 0, NULL) == 0);
+}
+
+static void test_multiple(void) {
+        sd_rtnl *rtnl1, *rtnl2;
+
+        assert(sd_rtnl_open(0, &rtnl1) >= 0);
+        assert(sd_rtnl_open(0, &rtnl2) >= 0);
+
+        rtnl1 = sd_rtnl_unref(rtnl1);
+        rtnl2 = sd_rtnl_unref(rtnl2);
 }
 
 int main(void) {
@@ -64,6 +74,8 @@ int main(void) {
         unsigned int mtu = 0;
         unsigned int *mtu_reply;
 
+        test_multiple();
+
         assert(sd_rtnl_open(0, &rtnl) >= 0);
         assert(rtnl);
 
@@ -80,7 +92,7 @@ int main(void) {
 
         assert(sd_rtnl_message_read(m, &type, &data) == 0);
 
-        assert(sd_rtnl_send_with_reply_and_block(rtnl, m, 100000000, &r) >= 0);
+        assert(sd_rtnl_send_with_reply_and_block(rtnl, m, 0, &r) >= 0);
         assert(sd_rtnl_message_get_type(r, &type) >= 0);
         assert(type == RTM_NEWLINK);
 



More information about the systemd-commits mailing list