diff options
author | Lennart Poettering <lennart@poettering.net> | 2017-11-17 15:22:11 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2017-11-17 15:22:11 +0100 |
commit | f56e7bfe2b330798f8421b5e081ad8ea79af8216 (patch) | |
tree | 495f3a8001b91744d6b816db4297f8ae1606e876 /src/core/service.c | |
parent | man: rescue.target could be seen as single-user.target (#7323) (diff) | |
download | systemd-f56e7bfe2b330798f8421b5e081ad8ea79af8216.tar.xz systemd-f56e7bfe2b330798f8421b5e081ad8ea79af8216.zip |
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
Diffstat (limited to '')
-rw-r--r-- | src/core/service.c | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/src/core/service.c b/src/core/service.c index 4b912fc3b9..95b52905de 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; |