From 6789835ba4df321d684cc489f872ef8b045a7a19 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 29 Dec 2015 16:18:54 +0100 Subject: fix possible race condition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/satd.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/satd.c b/src/satd.c index 5530b6b..be50082 100644 --- a/src/satd.c +++ b/src/satd.c @@ -72,19 +72,17 @@ create_socket(struct sockaddr_un *address) if (fd == -1) { t (errno != ENOENT); goto does_not_exist; - } else { - if (flock(fd, LOCK_EX | LOCK_NB) == -1) { - t (errno != EWOULDBLOCK); - fprintf(stderr, "%s: the daemon's socket file is already in use.\n", argv0); - errno = 0; - goto fail; - } - flock(fd, LOCK_UN); - close(fd), fd = -1; + } else if (flock(fd, LOCK_EX | LOCK_NB) == -1) { + t (errno != EWOULDBLOCK); + fprintf(stderr, "%s: the daemon's socket file is already in use.\n", argv0); + errno = 0; + goto fail; } /* Create socket. */ unlink(address->sun_path); + flock(fd, LOCK_UN); + close(fd), fd = -1; does_not_exist: t ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1); t (fchmod(fd, S_IRWXU) == -1); -- cgit v1.2.3-70-g09d2