Probe run/tmp directory at runtime
Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
3
Makefile
3
Makefile
@@ -6,11 +6,10 @@ DESTDIR ?=
|
||||
CC ?= gcc
|
||||
INSTALL ?= install
|
||||
prefix ?= /usr
|
||||
rundir ?= /run
|
||||
CFLAGS ?= -std=c11 -O2 -Wall -fstack-protector-strong
|
||||
|
||||
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 += -L.static/$(CROSS)/libsodium-stable/src/libsodium/.libs
|
||||
|
||||
11
src/bind.c
11
src/bind.c
@@ -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) {
|
||||
gt_log("couldn't create "GT_RUNDIR"/%s: %s\n",
|
||||
tun_name, strerror(errno));
|
||||
char dir[64];
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,10 +21,6 @@
|
||||
#define PACKAGE_VERSION "0.0.0"
|
||||
#endif
|
||||
|
||||
#ifndef GT_RUNDIR
|
||||
#define GT_RUNDIR "/run/" PACKAGE_NAME
|
||||
#endif
|
||||
|
||||
#define COUNT(x) (sizeof(x)/sizeof(x[0]))
|
||||
|
||||
#define ALIGN_SIZE (1<<4)
|
||||
|
||||
53
src/ctl.c
53
src/ctl.c
@@ -5,11 +5,44 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <libgen.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.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
|
||||
ctl_reply(int fd, struct ctl_msg *res, struct ctl_msg *req)
|
||||
{
|
||||
@@ -88,12 +121,12 @@ ctl_delete(int fd)
|
||||
}
|
||||
|
||||
int
|
||||
ctl_create(const char *dir, const char *file)
|
||||
ctl_create(const char *file)
|
||||
{
|
||||
if (str_empty(dir)) {
|
||||
errno = EINVAL;
|
||||
char dir[64];
|
||||
|
||||
if (!ctl_rundir(dir, sizeof(dir)))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (mkdir(dir, 0700) == -1 && errno != EEXIST)
|
||||
return -1;
|
||||
@@ -111,14 +144,13 @@ ctl_create(const char *dir, const char *file)
|
||||
}
|
||||
|
||||
int
|
||||
ctl_connect(const char *dir, const char *file)
|
||||
ctl_connect(const char *file)
|
||||
{
|
||||
char dir[64];
|
||||
DIR *dp = NULL;
|
||||
|
||||
if (str_empty(dir)) {
|
||||
errno = EINVAL;
|
||||
if (!ctl_rundir(dir, sizeof(dir)))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!file) {
|
||||
if (dp = opendir(dir), !dp)
|
||||
@@ -156,9 +188,10 @@ ctl_connect(const char *dir, const char *file)
|
||||
if (ret)
|
||||
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;
|
||||
ctl_delete(fd);
|
||||
errno = err;
|
||||
|
||||
@@ -47,7 +47,8 @@ struct ctl_msg {
|
||||
};
|
||||
};
|
||||
|
||||
int ctl_create (const char *, const char *);
|
||||
int ctl_connect (const char *, const char *);
|
||||
int ctl_reply (int, struct ctl_msg *, struct ctl_msg *);
|
||||
void ctl_delete (int);
|
||||
char *ctl_rundir (char *, size_t);
|
||||
int ctl_create (const char *);
|
||||
int ctl_connect (const char *);
|
||||
int ctl_reply (int, struct ctl_msg *, struct ctl_msg *);
|
||||
void ctl_delete (int);
|
||||
|
||||
@@ -164,7 +164,7 @@ gt_path(int argc, char **argv)
|
||||
if (argz(pathz, argc, argv))
|
||||
return 1;
|
||||
|
||||
int fd = ctl_connect(GT_RUNDIR, dev);
|
||||
int fd = ctl_connect(dev);
|
||||
|
||||
if (fd < 0) {
|
||||
switch (fd) {
|
||||
|
||||
@@ -147,7 +147,7 @@ gt_set(int argc, char **argv)
|
||||
if (argz(pathz, argc, argv))
|
||||
return 1;
|
||||
|
||||
int fd = ctl_connect(GT_RUNDIR, dev);
|
||||
int fd = ctl_connect(dev);
|
||||
|
||||
if (fd < 0) {
|
||||
switch (fd) {
|
||||
|
||||
@@ -129,7 +129,7 @@ gt_show(int argc, char **argv)
|
||||
if (argz(showz, argc, argv))
|
||||
return 1;
|
||||
|
||||
int fd = ctl_connect(GT_RUNDIR, dev);
|
||||
int fd = ctl_connect(dev);
|
||||
|
||||
if (fd < 0) {
|
||||
switch (fd) {
|
||||
|
||||
Reference in New Issue
Block a user