Always unlink ctl socket

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2019-07-19 21:44:52 +00:00
parent e077554304
commit ed82531fc0

View File

@@ -58,24 +58,24 @@ ctl_setsun(struct sockaddr_un *dst, const char *dir, const char *file)
static int static int
ctl_bind(int fd, const char *dir, const char *file) ctl_bind(int fd, const char *dir, const char *file)
{ {
char name[10] = { [0] = '.' };
struct sockaddr_un sun; struct sockaddr_un sun;
if (str_empty(file)) { if (str_empty(file)) {
char name[10] = { [0] = '.' };
unsigned pid = (unsigned)getpid(); unsigned pid = (unsigned)getpid();
for (size_t i = 1; i < sizeof(name) - 1; i++, pid >>= 4) for (size_t i = 1; i < sizeof(name) - 1; i++, pid >>= 4)
name[i] = "uncopyrightables"[pid & 15]; name[i] = "uncopyrightables"[pid & 15];
if (ctl_setsun(&sun, dir, name)) file = name;
return -1;
} else {
if (ctl_setsun(&sun, dir, file))
return -1;
unlink(sun.sun_path);
} }
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)); return bind(fd, (struct sockaddr *)&sun, sizeof(sun));
} }
@@ -132,9 +132,7 @@ ctl_connect(const char *dir, const char *file)
} }
if (!file) { if (!file) {
dp = opendir(dir); if (dp = opendir(dir), !dp)
if (!dp)
return -1; return -1;
struct dirent *d = NULL; struct dirent *d = NULL;