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.
This commit is contained in:
Lennart Poettering
2015-10-06 09:41:02 +02:00
parent 6ec4ed645e
commit 79c7626d1f
3 changed files with 28 additions and 40 deletions
+17 -23
View File
@@ -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;
}
}
+10 -16
View File
@@ -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) {
+1 -1
View File
@@ -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);