45
src/ctl.c
45
src/ctl.c
@@ -18,27 +18,23 @@ ctl_init(const char *dir, const char *file)
|
||||
if (mkdir(dir, 0700) == -1 && errno != EEXIST)
|
||||
return -1;
|
||||
|
||||
const char *strs[] = {dir, "/", file};
|
||||
char *path = str_cat(strs, 3);
|
||||
|
||||
if (!path)
|
||||
return -1;
|
||||
|
||||
int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||
|
||||
if (fd == -1) {
|
||||
int err = errno;
|
||||
free(path);
|
||||
errno = err;
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct sockaddr_un sun = {
|
||||
.sun_family = AF_UNIX,
|
||||
};
|
||||
|
||||
str_cpy(sun.sun_path, path, sizeof(sun.sun_path) - 1);
|
||||
free(path);
|
||||
const char *path[] = {dir, "/", file};
|
||||
const size_t len = sizeof(sun.sun_path) - 1;
|
||||
|
||||
if (str_cat(sun.sun_path, path, COUNT(path), len) > len) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||
|
||||
if (fd == -1)
|
||||
return -1;
|
||||
|
||||
unlink(sun.sun_path);
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
|
||||
@@ -59,18 +55,17 @@ ctl_connect(int fd, const char *dir, const char *file)
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *strs[] = {dir, "/", file};
|
||||
char *path = str_cat(strs, 3);
|
||||
|
||||
if (!path)
|
||||
return -1;
|
||||
|
||||
struct sockaddr_un sun = {
|
||||
.sun_family = AF_UNIX,
|
||||
};
|
||||
|
||||
str_cpy(sun.sun_path, path, sizeof(sun.sun_path) - 1);
|
||||
free(path);
|
||||
const char *path[] = {dir, "/", file};
|
||||
const size_t len = sizeof(sun.sun_path) - 1;
|
||||
|
||||
if (str_cat(sun.sun_path, path, COUNT(path), len) > len) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return connect(fd, (struct sockaddr *)&sun, sizeof(sun));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user