chiark / gitweb /
rtnl: rename rtnl_bus_send_with_reply_and_block() to rtnl_bus_call()
[elogind.git] / src / libsystemd-rtnl / sd-rtnl.c
index b11a813dfe3cea502657b3b194dded9999eb563e..eb3b01b72572bdbed0fa12e0ae9c50e4f43f986f 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "sd-rtnl.h"
 #include "rtnl-internal.h"
+#include "rtnl-util.h"
 
 static int sd_rtnl_new(sd_rtnl **ret) {
         sd_rtnl *rtnl;
@@ -43,12 +44,24 @@ static int sd_rtnl_new(sd_rtnl **ret) {
 
         rtnl->sockaddr.nl.nl_family = AF_NETLINK;
 
+        rtnl->original_pid = getpid();
+
         *ret = rtnl;
         return 0;
 }
 
+static bool rtnl_pid_changed(sd_rtnl *rtnl) {
+        assert(rtnl);
+
+        /* We don't support people creating an rtnl connection and
+         * keeping it around over a fork(). Let's complain. */
+
+        return rtnl->original_pid != getpid();
+}
+
 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);
@@ -61,10 +74,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;
 
@@ -88,7 +107,7 @@ sd_rtnl *sd_rtnl_unref(sd_rtnl *rtnl) {
         return NULL;
 }
 
-int sd_rtnl_send_with_reply_and_block(sd_rtnl *nl,
+int sd_rtnl_call(sd_rtnl *nl,
                 sd_rtnl_message *message,
                 uint64_t usec,
                 sd_rtnl_message **ret) {
@@ -98,6 +117,7 @@ int sd_rtnl_send_with_reply_and_block(sd_rtnl *nl,
         int r, serial;
 
         assert_return(nl, -EINVAL);
+        assert_return(!rtnl_pid_changed(nl), -ECHILD);
         assert_return(message, -EINVAL);
 
         r = message_seal(nl, message);