summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-01-07 16:42:46 +0100
committerLeah Neukirchen <leah@vuxu.org>2022-01-07 16:42:46 +0100
commit460803f3c3740e076f6db8d56f900b6a53f5ef8f (patch)
treec562d58dc4ca376d7be07049386149803ef89a06
parent8fb64f181ea12a12d9f9e9d58b54298f3e31b84b (diff)
downloadrvnit-460803f3c3740e076f6db8d56f900b6a53f5ef8f.tar.gz
rvnit-460803f3c3740e076f6db8d56f900b6a53f5ef8f.tar.xz
rvnit-460803f3c3740e076f6db8d56f900b6a53f5ef8f.zip
move socket by default to /run/rvnit/rvnit.sock and create with perm 0700
-rw-r--r--rvnit.c17
-rw-r--r--rvnitctl.c5
2 files changed, 20 insertions, 2 deletions
diff --git a/rvnit.c b/rvnit.c
index b35850b..f427fe1 100644
--- a/rvnit.c
+++ b/rvnit.c
@@ -3,6 +3,7 @@
 
 #include <sys/wait.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <sys/un.h>
 
 #include <ctype.h>
@@ -172,7 +173,19 @@ socket_loop(void* ignored)
 {
 	(void)ignored;
 
-	const char *path = "/tmp/rvnit.sock";
+	static const char default_sock[] = "/run/rvnit/rvnit.sock";
+	const char *path = getenv("RVNIT_SOCK");
+	if (!path || !*path)
+		path = default_sock;
+
+	char *last_slash = strrchr(path, '/');
+	if (last_slash) {
+		char dir[PATH_MAX];
+		memcpy(dir, path, last_slash - path);
+		dir[last_slash - path] = 0;
+		mkdir(dir, 0700);
+		// ignore errors
+	}
 
 	struct sockaddr_un addr = { 0 };
 	addr.sun_family = AF_UNIX;
@@ -183,7 +196,9 @@ socket_loop(void* ignored)
 		exit(111);
 	}
 	unlink(path);
+	mode_t mask = umask(0077);
 	int r = bind(listenfd, (struct sockaddr *)&addr, sizeof addr);
+	umask(mask);
 	if (r < 0) {
 		perror("bind");
 		exit(111);
diff --git a/rvnitctl.c b/rvnitctl.c
index c6bec1a..7d48e01 100644
--- a/rvnitctl.c
+++ b/rvnitctl.c
@@ -13,7 +13,10 @@ main(int argc, char *argv[])
 		exit(2);
 	}
 
-	const char *path = "/tmp/rvnit.sock";
+	static const char default_sock[] = "/run/rvnit/rvnit.sock";
+	const char *path = getenv("RVNIT_SOCK");
+	if (!path || !*path)
+		path = default_sock;
 
 	struct sockaddr_un addr = { 0 };
 	addr.sun_family = AF_UNIX;