From ed82531fc0b616570aa2bda23c27ed0cddc7f813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Fri, 19 Jul 2019 21:44:52 +0000 Subject: [PATCH] Always unlink ctl socket MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adrien Gallouët --- src/ctl.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/ctl.c b/src/ctl.c index 257a30d..98ad6ab 100644 --- a/src/ctl.c +++ b/src/ctl.c @@ -58,24 +58,24 @@ ctl_setsun(struct sockaddr_un *dst, const char *dir, const char *file) static int ctl_bind(int fd, const char *dir, const char *file) { + char name[10] = { [0] = '.' }; struct sockaddr_un sun; if (str_empty(file)) { - char name[10] = { [0] = '.' }; unsigned pid = (unsigned)getpid(); for (size_t i = 1; i < sizeof(name) - 1; i++, pid >>= 4) name[i] = "uncopyrightables"[pid & 15]; - if (ctl_setsun(&sun, dir, name)) - return -1; - } else { - if (ctl_setsun(&sun, dir, file)) - return -1; - - unlink(sun.sun_path); + file = name; } + if (ctl_setsun(&sun, dir, file)) + return -1; + + if (unlink(sun.sun_path) && errno != ENOENT) + return -1; + return bind(fd, (struct sockaddr *)&sun, sizeof(sun)); } @@ -132,9 +132,7 @@ ctl_connect(const char *dir, const char *file) } if (!file) { - dp = opendir(dir); - - if (!dp) + if (dp = opendir(dir), !dp) return -1; struct dirent *d = NULL;