sd-rtnl.c revision 6203e07a83214a55bb1f88508fcda2005c601dea
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Tom Gundersen <teg@jklm.no>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <poll.h>
#include "macro.h"
#include "util.h"
#include "hashmap.h"
#include "sd-rtnl.h"
#include "rtnl-internal.h"
#include "rtnl-util.h"
if (!rtnl)
return -ENOMEM;
/* We guarantee that wqueue always has space for at least
* one entry */
return -ENOMEM;
}
return 0;
}
/* We don't support people creating an rtnl connection and
* keeping it around over a fork(). Let's complain. */
}
int r;
r = sd_rtnl_new(&rtnl);
if (r < 0)
return r;
return -errno;
if (r < 0)
return -errno;
if (r < 0)
return r;
return 0;
}
if (rtnl)
return rtnl;
}
struct match_callback *f;
unsigned i;
for (i = 0; i < rtnl->rqueue_size; i++)
for (i = 0; i < rtnl->wqueue_size; i++)
while ((f = rtnl->match_callbacks)) {
free(f);
}
}
return NULL;
}
int r;
if (r < 0)
return r;
if (nl->wqueue_size <= 0) {
/* send directly */
if (r < 0)
return r;
else if (r == 0) {
/* nothing was sent, so let's put it on
* the queue */
}
} else {
sd_rtnl_message **q;
/* append to queue */
return -ENOBUFS;
if (!q)
return -ENOMEM;
}
if (serial)
return 1;
}
sd_rtnl_message *z = NULL;
int r;
if (rtnl->rqueue_size > 0) {
/* Dispatch a queued message */
rtnl->rqueue_size --;
return 1;
}
/* Try to read a new message */
r = socket_read_message(rtnl, &z);
if (r < 0)
return r;
if (r == 0)
return 0;
*message = z;
return 1;
}
int r, ret = 0;
while (rtnl->wqueue_size > 0) {
if (r < 0)
return r;
else if (r == 0)
/* Didn't do anything this time */
return ret;
else {
/* see equivalent in sd-bus.c */
rtnl->wqueue_size --;
ret = 1;
}
}
return ret;
}
struct reply_callback *c;
usec_t n;
int r;
if (!c)
return 0;
n = now(CLOCK_MONOTONIC);
if (c->timeout > n)
return 0;
if (r < 0)
return r;
free(c);
return r < 0 ? r : 1;
}
struct reply_callback *c;
int r;
assert(m);
serial = message_get_serial(m);
if (!c)
return 0;
if (c->timeout != 0)
free(c);
return r;
}
struct match_callback *c;
int r;
assert(m);
r = sd_rtnl_message_get_type(m, &type);
if (r < 0)
return r;
if (r != 0)
return r;
}
}
return 0;
}
int r;
r = process_timeout(rtnl);
if (r != 0)
goto null_message;
r = dispatch_wqueue(rtnl);
if (r != 0)
goto null_message;
r = dispatch_rqueue(rtnl, &m);
if (r < 0)
return r;
if (!m)
goto null_message;
r = process_reply(rtnl, m);
if (r != 0)
goto null_message;
r = process_match(rtnl, m);
if (r != 0)
goto null_message;
if (ret) {
*ret = m;
m = NULL;
return 1;
}
return 1;
if (r >= 0 && ret)
return r;
}
int r;
rtnl->processing = true;
rtnl->processing = false;
return r;
}
return 0;
if (usec == 0)
}
struct pollfd p[1] = {};
int r, e;
e = sd_rtnl_get_events(rtnl);
if (e < 0)
return e;
if (need_more)
/* Caller wants more data, and doesn't care about
* what's been read or any other timeouts. */
return e |= POLLIN;
else {
/* Caller wants to process if there is something to
* process, but doesn't care otherwise */
if (r < 0)
return r;
if (r > 0) {
}
}
m = timeout_usec;
p[0].events = e;
if (r < 0)
return -errno;
return r > 0 ? 1 : 0;
}
if (nl->rqueue_size > 0)
return 0;
}
static int timeout_compare(const void *a, const void *b) {
const struct reply_callback *x = a, *y = b;
return -1;
return 1;
return -1;
return 1;
return 0;
}
sd_rtnl_message *m,
void *userdata,
struct reply_callback *c;
uint32_t s;
int r, k;
assert_return(m, -EINVAL);
if (r < 0)
return r;
if (r < 0)
return r;
}
if (!c)
return -ENOMEM;
k = sd_rtnl_send(nl, m, &s);
if (k < 0) {
free(c);
return k;
}
c->serial = s;
if (r < 0) {
free(c);
return r;
}
if (c->timeout != 0) {
if (r > 0) {
c->timeout = 0;
return r;
}
}
if (serial)
*serial = s;
return k;
}
struct reply_callback *c;
if (!c)
return 0;
if (c->timeout != 0)
free(c);
return 1;
}
sd_rtnl_message **ret) {
bool room = false;
int r;
if (r < 0)
return r;
for (;;) {
if (!room) {
sd_rtnl_message **q;
return -ENOBUFS;
/* Make sure there's room for queueing this
* locally, before we read the message */
if (!q)
return -ENOMEM;
room = true;
}
if (r < 0)
return r;
if (incoming) {
if (received_serial == serial) {
if (r < 0)
return r;
if (ret) {
}
return 1;
}
/* Room was allocated on the queue above */
room = false;
/* Try to read more, right away */
continue;
}
if (r != 0)
continue;
if (timeout > 0) {
usec_t n;
n = now(CLOCK_MONOTONIC);
if (n >= timeout)
return -ETIMEDOUT;
} else
if (r < 0)
return r;
r = dispatch_wqueue(nl);
if (r < 0)
return r;
}
}
int r;
if (rtnl->wqueue_size <= 0)
return 0;
for (;;) {
r = dispatch_wqueue(rtnl);
if (r < 0)
return r;
if (rtnl->wqueue_size <= 0)
return 0;
if (r < 0)
return r;
}
}
int flags = 0;
if (rtnl->rqueue_size <= 0)
if (rtnl->wqueue_size > 0)
return flags;
}
struct reply_callback *c;
if (rtnl->rqueue_size > 0) {
*timeout_usec = 0;
return 1;
}
if (!c) {
return 0;
}
*timeout_usec = c->timeout;
return 1;
}
int r;
if (r < 0)
return r;
return 1;
}
int r;
if (r < 0)
return r;
return 1;
}
int r, e;
assert(s);
e = sd_rtnl_get_events(rtnl);
if (e < 0)
return e;
if (r < 0)
return r;
if (r < 0)
return r;
if (r > 0) {
int j;
if (j < 0)
return j;
}
if (r < 0)
return r;
return 1;
}
return 1;
}
int r;
if (event)
else {
if (r < 0)
return r;
}
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (r < 0)
goto fail;
return 0;
fail:
return r;
}
if (rtnl->io_event_source)
if (rtnl->time_event_source)
if (rtnl->exit_event_source)
return 0;
}
void *userdata) {
struct match_callback *c;
if (!c)
return -ENOMEM;
return 0;
}
void *userdata) {
struct match_callback *c;
free(c);
return 1;
}
return 0;
}