X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibsystemd-rtnl%2Fsd-rtnl.c;h=8ea11df808d2981dda7c59cedd141e3ed542b035;hb=8e1519bddb5eefedac2d9c4bf6f6e6b4d5e8ce81;hp=9938d463b05675136d01014ddaebaca5af7640c1;hpb=fe5c4e493e19f5b7979235783170c231dd7d008c;p=elogind.git diff --git a/src/libsystemd-rtnl/sd-rtnl.c b/src/libsystemd-rtnl/sd-rtnl.c index 9938d463b..8ea11df80 100644 --- a/src/libsystemd-rtnl/sd-rtnl.c +++ b/src/libsystemd-rtnl/sd-rtnl.c @@ -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; @@ -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); @@ -109,10 +129,15 @@ int sd_rtnl_send_with_reply_and_block(sd_rtnl *nl, p[0].fd = nl->fd; p[0].events = POLLOUT; - timeout = now(CLOCK_MONOTONIC) + usec; + if (usec == (uint64_t) -1) + timeout = 0; + else if (usec == 0) + timeout = now(CLOCK_MONOTONIC) + RTNL_DEFAULT_TIMEOUT; + else + timeout = now(CLOCK_MONOTONIC) + usec; for (;;) { - if (usec != (uint64_t) -1) { + if (timeout) { usec_t n; n = now(CLOCK_MONOTONIC); @@ -122,7 +147,7 @@ int sd_rtnl_send_with_reply_and_block(sd_rtnl *nl, timespec_store(&left, timeout - n); } - r = ppoll(p, 1, usec == (uint64_t) -1 ? NULL : &left, NULL); + r = ppoll(p, 1, timeout ? &left : NULL, NULL); if (r < 0) return 0; @@ -140,7 +165,7 @@ int sd_rtnl_send_with_reply_and_block(sd_rtnl *nl, for (;;) { _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *reply = NULL; - if (usec != (uint64_t) -1) { + if (timeout) { usec_t n; n = now(CLOCK_MONOTONIC); @@ -150,7 +175,7 @@ int sd_rtnl_send_with_reply_and_block(sd_rtnl *nl, timespec_store(&left, timeout - n); } - r = ppoll(p, 1, usec == (uint64_t) -1 ? NULL : &left, NULL); + r = ppoll(p, 1, timeout ? &left : NULL, NULL); if (r < 0) return r;