From: Lennart Poettering Date: Fri, 17 Nov 2017 14:22:11 +0000 (+0100) Subject: core: be more defensive if we can't determine per-connection socket peer (#7329) X-Git-Tag: v236~187 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f56e7bfe2b330798f8421b5e081ad8ea79af8216;p=platform%2Fupstream%2Fsystemd.git core: be more defensive if we can't determine per-connection socket peer (#7329) Let's handle gracefully if a client disconnects very early on. This builds on #4120, but relaxes the condition checks further, since we getpeername() might already fail during ExecStartPre= and friends. Fixes: #7172 --- diff --git a/src/core/service.c b/src/core/service.c index 4b912fc..95b5290 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -1299,17 +1299,14 @@ static int service_spawn( union sockaddr_union sa; socklen_t salen = sizeof(sa); - r = getpeername(s->socket_fd, &sa.sa, &salen); - if (r < 0) { - r = -errno; + /* If this is a per-connection service instance, let's set $REMOTE_ADDR and $REMOTE_PORT to something + * useful. Note that we do this only when we are still connected at this point in time, which we might + * very well not be. Hence we ignore all errors when retrieving peer information (as that might result + * in ENOTCONN), and just use whate we can use. */ - /* ENOTCONN is legitimate if the endpoint disappeared on shutdown. - * This connection is over, but the socket unit lives on. */ - if (r != -ENOTCONN || !IN_SET(s->control_command_id, SERVICE_EXEC_STOP, SERVICE_EXEC_STOP_POST)) - return r; - } + if (getpeername(s->socket_fd, &sa.sa, &salen) >= 0 && + IN_SET(sa.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK)) { - if (r == 0 && IN_SET(sa.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK)) { _cleanup_free_ char *addr = NULL; char *t; unsigned port;