diff options
author | Lennart Poettering <lennart@poettering.net> | 2015-10-03 16:41:36 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2015-10-06 09:41:02 +0200 |
commit | 79c7626d1f239e02152ad698298a1b5d0e9fbacf (patch) | |
tree | 8c44dacdd28f0ec358f5751c5a7733a9d72ebb6e | |
parent | 6ec4ed645e37ba7dd5628747ea78ec1663599ab7 (diff) |
core: simplify fd collection code, return number of fds as return value
Let's simplify the fd collection code a bit, and return the number of
collected fds as positive integer, the way it's customary in our usual
code.
-rw-r--r-- | src/core/service.c | 40 | ||||
-rw-r--r-- | src/core/socket.c | 26 | ||||
-rw-r--r-- | src/core/socket.h | 2 |
3 files changed, 28 insertions, 40 deletions
diff --git a/src/core/service.c b/src/core/service.c index cb0394f930..2c78cb96c2 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -957,57 +957,51 @@ static int service_coldplug(Unit *u) { return 0; } -static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { +static int service_collect_fds(Service *s, int **fds) { _cleanup_free_ int *rfds = NULL; - unsigned rn_fds = 0; + int rn_fds = 0; Iterator i; - int r; Unit *u; assert(s); assert(fds); - assert(n_fds); if (s->socket_fd >= 0) - return 0; + return -EINVAL; SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) { - int *cfds; - unsigned cn_fds; + _cleanup_free_ int *cfds = NULL; Socket *sock; + int cn_fds; if (u->type != UNIT_SOCKET) continue; sock = SOCKET(u); - r = socket_collect_fds(sock, &cfds, &cn_fds); - if (r < 0) - return r; + cn_fds = socket_collect_fds(sock, &cfds); + if (cn_fds < 0) + return cn_fds; - if (cn_fds <= 0) { - free(cfds); + if (cn_fds <= 0) continue; - } if (!rfds) { rfds = cfds; rn_fds = cn_fds; + + cfds = NULL; } else { int *t; t = realloc(rfds, (rn_fds + cn_fds) * sizeof(int)); - if (!t) { - free(cfds); + if (!t) return -ENOMEM; - } memcpy(t + rn_fds, cfds, cn_fds * sizeof(int)); + rfds = t; rn_fds += cn_fds; - - free(cfds); - } } @@ -1025,10 +1019,9 @@ static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { } *fds = rfds; - *n_fds = rn_fds; - rfds = NULL; - return 0; + + return rn_fds; } static int service_spawn( @@ -1082,11 +1075,12 @@ static int service_spawn( fds = &s->socket_fd; n_fds = 1; } else { - r = service_collect_fds(s, &fdsbuf, &n_fds); + r = service_collect_fds(s, &fdsbuf); if (r < 0) goto fail; fds = fdsbuf; + n_fds = r; } } diff --git a/src/core/socket.c b/src/core/socket.c index f547d1ba7c..4462fbd72d 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -2628,49 +2628,43 @@ static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *use return 0; } -int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) { - int *rfds; - unsigned rn_fds, k; - int i; +int socket_collect_fds(Socket *s, int **fds) { + int *rfds, k = 0, n = 0; SocketPort *p; assert(s); assert(fds); - assert(n_fds); /* Called from the service code for requesting our fds */ - rn_fds = 0; LIST_FOREACH(port, p, s->ports) { if (p->fd >= 0) - rn_fds++; - rn_fds += p->n_auxiliary_fds; + n++; + n += p->n_auxiliary_fds; } - if (rn_fds <= 0) { + if (n <= 0) { *fds = NULL; - *n_fds = 0; return 0; } - rfds = new(int, rn_fds); + rfds = new(int, n); if (!rfds) return -ENOMEM; - k = 0; LIST_FOREACH(port, p, s->ports) { + int i; + if (p->fd >= 0) rfds[k++] = p->fd; for (i = 0; i < p->n_auxiliary_fds; ++i) rfds[k++] = p->auxiliary_fds[i]; } - assert(k == rn_fds); + assert(k == n); *fds = rfds; - *n_fds = rn_fds; - - return 0; + return n; } static void socket_reset_failed(Unit *u) { diff --git a/src/core/socket.h b/src/core/socket.h index d20dc8d81a..96091b0609 100644 --- a/src/core/socket.h +++ b/src/core/socket.h @@ -157,7 +157,7 @@ struct Socket { }; /* Called from the service code when collecting fds */ -int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds); +int socket_collect_fds(Socket *s, int **fds); /* Called from the service code when a per-connection service ended */ void socket_connection_unref(Socket *s); |