Compute sun_path from PID

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2019-04-02 13:51:15 +00:00
parent 568eeee70b
commit d703a66988

View File

@@ -2,15 +2,13 @@
#include "ctl.h" #include "ctl.h"
#include "str.h" #include "str.h"
#include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/un.h> #include <sys/un.h>
#define CTL_BIND_MAX 64
int int
ctl_reply(int fd, struct ctl_msg *res, struct ctl_msg *req) 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 static int
ctl_bind(int fd, const char *dir, const char *file) ctl_bind(int fd, const char *dir, const char *file)
{ {
char tmp[32];
struct sockaddr_un sun; struct sockaddr_un sun;
if (str_empty(file)) { if (str_empty(file)) {
for (int i = 0; i < CTL_BIND_MAX; i++) { char name[10] = { [0] = '.' };
if (snprintf(tmp, sizeof(tmp), ".%i", i) >= sizeof(tmp)) unsigned pid = (unsigned)getpid();
return -1;
if (ctl_setsun(&sun, dir, tmp)) for (size_t i = 1; i < sizeof(name) - 1; i++, pid >>= 4)
return -1; name[i] = "uncopyrightables"[pid & 15];
if (!bind(fd, (struct sockaddr *)&sun, sizeof(sun))) if (ctl_setsun(&sun, dir, name))
return 0; return -1;
}
if (!bind(fd, (struct sockaddr *)&sun, sizeof(sun)))
return 0;
} else { } else {
if (ctl_setsun(&sun, dir, file)) if (ctl_setsun(&sun, dir, file))
return -1; return -1;