Probe run/tmp directory at runtime

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2019-11-14 19:19:23 +00:00
parent a4f63ecf40
commit b9aaab661f
8 changed files with 60 additions and 26 deletions

View File

@@ -6,11 +6,10 @@ DESTDIR ?=
CC ?= gcc CC ?= gcc
INSTALL ?= install INSTALL ?= install
prefix ?= /usr prefix ?= /usr
rundir ?= /run
CFLAGS ?= -std=c11 -O2 -Wall -fstack-protector-strong CFLAGS ?= -std=c11 -O2 -Wall -fstack-protector-strong
FLAGS := $(CFLAGS) $(LDFLAGS) $(CPPFLAGS) FLAGS := $(CFLAGS) $(LDFLAGS) $(CPPFLAGS)
FLAGS += -DPACKAGE_NAME=\"$(NAME)\" -DPACKAGE_VERSION=\"$(VERSION)\" -DGT_RUNDIR=\"$(DESTDIR)$(rundir)/$(NAME)\" FLAGS += -DPACKAGE_NAME=\"$(NAME)\" -DPACKAGE_VERSION=\"$(VERSION)\"
FLAGS += -I.static/$(CROSS)/libsodium-stable/src/libsodium/include FLAGS += -I.static/$(CROSS)/libsodium-stable/src/libsodium/include
FLAGS += -L.static/$(CROSS)/libsodium-stable/src/libsodium/.libs FLAGS += -L.static/$(CROSS)/libsodium-stable/src/libsodium/.libs

View File

@@ -185,11 +185,16 @@ gt_bind(int argc, char **argv)
} }
} }
const int ctl_fd = ctl_create(GT_RUNDIR, tun_name); const int ctl_fd = ctl_create(tun_name);
if (ctl_fd == -1) { if (ctl_fd == -1) {
gt_log("couldn't create "GT_RUNDIR"/%s: %s\n", char dir[64];
tun_name, strerror(errno)); if (ctl_rundir(dir, sizeof(dir))) {
gt_log("couldn't create %s/%s: %s\n",
dir, tun_name, strerror(errno));
} else {
gt_log("couldn't find a writable run/tmp directory\n");
}
return 1; return 1;
} }

View File

@@ -21,10 +21,6 @@
#define PACKAGE_VERSION "0.0.0" #define PACKAGE_VERSION "0.0.0"
#endif #endif
#ifndef GT_RUNDIR
#define GT_RUNDIR "/run/" PACKAGE_NAME
#endif
#define COUNT(x) (sizeof(x)/sizeof(x[0])) #define COUNT(x) (sizeof(x)/sizeof(x[0]))
#define ALIGN_SIZE (1<<4) #define ALIGN_SIZE (1<<4)

View File

@@ -5,11 +5,44 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <libgen.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/un.h> #include <sys/un.h>
char *
ctl_rundir(char *dst, size_t size)
{
if (dst && size)
dst[0] = 0;
const char *fmt[] = {
"/run/user/%u/" PACKAGE_NAME,
"/run/" PACKAGE_NAME ".%u",
"/var/run/" PACKAGE_NAME ".%u",
"/tmp/" PACKAGE_NAME ".%u",
};
for (int i = 0; i < COUNT(fmt); i++) {
char path[128];
int ret = snprintf(dst, size, fmt[i], geteuid());
if ((ret <= 0) ||
((size_t)ret >= size) ||
((size_t)ret >= sizeof(path)))
continue;
memcpy(path, dst, ret + 1);
if (!access(dirname(path), W_OK))
return dst;
}
errno = EINTR;
return NULL;
}
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)
{ {
@@ -88,12 +121,12 @@ ctl_delete(int fd)
} }
int int
ctl_create(const char *dir, const char *file) ctl_create(const char *file)
{ {
if (str_empty(dir)) { char dir[64];
errno = EINVAL;
if (!ctl_rundir(dir, sizeof(dir)))
return -1; return -1;
}
if (mkdir(dir, 0700) == -1 && errno != EEXIST) if (mkdir(dir, 0700) == -1 && errno != EEXIST)
return -1; return -1;
@@ -111,14 +144,13 @@ ctl_create(const char *dir, const char *file)
} }
int int
ctl_connect(const char *dir, const char *file) ctl_connect(const char *file)
{ {
char dir[64];
DIR *dp = NULL; DIR *dp = NULL;
if (str_empty(dir)) { if (!ctl_rundir(dir, sizeof(dir)))
errno = EINVAL;
return -1; return -1;
}
if (!file) { if (!file) {
if (dp = opendir(dir), !dp) if (dp = opendir(dir), !dp)
@@ -156,9 +188,10 @@ ctl_connect(const char *dir, const char *file)
if (ret) if (ret)
return -1; return -1;
int fd = ctl_create(dir, NULL); int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (connect(fd, (struct sockaddr *)&sun, sizeof(sun))) { if (ctl_bind(fd, dir, NULL) ||
connect(fd, (struct sockaddr *)&sun, sizeof(sun))) {
int err = errno; int err = errno;
ctl_delete(fd); ctl_delete(fd);
errno = err; errno = err;

View File

@@ -47,7 +47,8 @@ struct ctl_msg {
}; };
}; };
int ctl_create (const char *, const char *); char *ctl_rundir (char *, size_t);
int ctl_connect (const char *, const char *); int ctl_create (const char *);
int ctl_reply (int, struct ctl_msg *, struct ctl_msg *); int ctl_connect (const char *);
void ctl_delete (int); int ctl_reply (int, struct ctl_msg *, struct ctl_msg *);
void ctl_delete (int);

View File

@@ -164,7 +164,7 @@ gt_path(int argc, char **argv)
if (argz(pathz, argc, argv)) if (argz(pathz, argc, argv))
return 1; return 1;
int fd = ctl_connect(GT_RUNDIR, dev); int fd = ctl_connect(dev);
if (fd < 0) { if (fd < 0) {
switch (fd) { switch (fd) {

View File

@@ -147,7 +147,7 @@ gt_set(int argc, char **argv)
if (argz(pathz, argc, argv)) if (argz(pathz, argc, argv))
return 1; return 1;
int fd = ctl_connect(GT_RUNDIR, dev); int fd = ctl_connect(dev);
if (fd < 0) { if (fd < 0) {
switch (fd) { switch (fd) {

View File

@@ -129,7 +129,7 @@ gt_show(int argc, char **argv)
if (argz(showz, argc, argv)) if (argz(showz, argc, argv))
return 1; return 1;
int fd = ctl_connect(GT_RUNDIR, dev); int fd = ctl_connect(dev);
if (fd < 0) { if (fd < 0) {
switch (fd) { switch (fd) {