summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-01-07 00:44:55 +0100
committerLeah Neukirchen <leah@vuxu.org>2022-01-07 00:44:55 +0100
commit6607f16375b26b588c148e0b5bfd61bc37696a7e (patch)
tree7fbabf3eafc75554850ec715c8ec80f7094d7cf7
parent060ae0b92db81f4cfe507d3d0be82d5baac2e14f (diff)
downloadrvnit-6607f16375b26b588c148e0b5bfd61bc37696a7e.tar.gz
rvnit-6607f16375b26b588c148e0b5bfd61bc37696a7e.tar.xz
rvnit-6607f16375b26b588c148e0b5bfd61bc37696a7e.zip
add rvnitctl
-rw-r--r--Makefile2
-rw-r--r--rvnitctl.c46
2 files changed, 47 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index aeae237..a45e672 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 CFLAGS=-g -O2 -Wall -Wno-switch -Wextra -Wwrite-strings -Wno-format-truncation -pthread
 
-ALL=rvnit
+ALL=rvnit rvnitctl
 
 all: $(ALL)
 
diff --git a/rvnitctl.c b/rvnitctl.c
new file mode 100644
index 0000000..1f3977c
--- /dev/null
+++ b/rvnitctl.c
@@ -0,0 +1,46 @@
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+	if (argc < 2) {
+		dprintf(2, "usage: rvnitctl COMMAND [SERVICE]\n");
+		exit(2);
+	}
+
+	const char *path = "/tmp/rvnit.sock";
+
+	struct sockaddr_un addr = { 0 };
+	addr.sun_family = AF_UNIX;
+	strncpy(addr.sun_path, path, sizeof addr.sun_path - 1);
+	int connfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+	if (connfd < 0) {
+		perror("socket");
+		exit(111);
+	}
+
+	if (connect(connfd, (struct sockaddr *)&addr, sizeof addr) < 0) {
+		perror("connect");
+		exit(111);
+	}
+
+	dprintf(connfd, "%c%s", *argv[1], argv[2] ? argv[2] : "");
+
+	ssize_t rd;
+	do {
+		char buf[4096];
+		rd = read(connfd, buf, sizeof buf);
+		if (rd < 0) {
+			perror("read");
+			exit(111);
+		}
+		write(1, buf, rd);
+	} while (rd > 0);
+
+	return 0;
+}