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) {
- sd_rtnl *rtnl;
+ _cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
+ socklen_t addrlen;
int r;
r = sd_rtnl_new(&rtnl);
return r;
rtnl->fd = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_ROUTE);
- if (rtnl->fd < 0) {
- r = -errno;
- sd_rtnl_unref(rtnl);
- return r;
- }
+ if (rtnl->fd < 0)
+ return -errno;
rtnl->sockaddr.nl.nl_groups = groups;
- r = bind(rtnl->fd, &rtnl->sockaddr.sa, sizeof(rtnl->sockaddr));
- if (r < 0) {
- r = -errno;
- sd_rtnl_unref(rtnl);
+ 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;
return 0;
}
uint64_t usec,
sd_rtnl_message **ret) {
struct pollfd p[1] = {};
- sd_rtnl_message *reply;
struct timespec left;
usec_t timeout;
int r, serial;
assert_return(nl, -EINVAL);
+ assert_return(!rtnl_pid_changed(nl), -ECHILD);
assert_return(message, -EINVAL);
r = message_seal(nl, message);
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);
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;
p[0].events = POLLIN;
for (;;) {
- if (usec != (uint64_t) -1) {
+ _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *reply = NULL;
+
+ if (timeout) {
usec_t n;
n = now(CLOCK_MONOTONIC);
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;
if (received_serial == serial) {
r = message_get_errno(reply);
- if (r < 0) {
- sd_rtnl_message_unref(reply);
+ if (r < 0)
return r;
- }
- if (ret)
+ if (ret) {
*ret = reply;
- else
- reply = sd_rtnl_message_unref(reply);
+ reply = NULL;
+ }
break;;
}
-
- reply = sd_rtnl_message_unref(reply);
}
}