From d703a66988f71d4d0b7709c6e2671678706cf387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Tue, 2 Apr 2019 13:51:15 +0000 Subject: [PATCH] Compute sun_path from PID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adrien Gallouët --- src/ctl.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/ctl.c b/src/ctl.c index 30df164..f28529f 100644 --- a/src/ctl.c +++ b/src/ctl.c @@ -2,15 +2,13 @@ #include "ctl.h" #include "str.h" -#include #include #include #include +#include #include #include -#define CTL_BIND_MAX 64 - int ctl_reply(int fd, struct ctl_msg *res, struct ctl_msg *req) { @@ -61,20 +59,20 @@ 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 tmp[32]; struct sockaddr_un sun; if (str_empty(file)) { - for (int i = 0; i < CTL_BIND_MAX; i++) { - if (snprintf(tmp, sizeof(tmp), ".%i", i) >= sizeof(tmp)) - return -1; + char name[10] = { [0] = '.' }; + unsigned pid = (unsigned)getpid(); - if (ctl_setsun(&sun, dir, tmp)) - return -1; + for (size_t i = 1; i < sizeof(name) - 1; i++, pid >>= 4) + name[i] = "uncopyrightables"[pid & 15]; - if (!bind(fd, (struct sockaddr *)&sun, sizeof(sun))) - return 0; - } + if (ctl_setsun(&sun, dir, name)) + return -1; + + if (!bind(fd, (struct sockaddr *)&sun, sizeof(sun))) + return 0; } else { if (ctl_setsun(&sun, dir, file)) return -1;