about summary refs log tree commit diff homepage
path: root/server.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-10-14 19:50:44 -0400
committerJune McEnroe <june@causal.agency>2018-10-14 19:50:44 -0400
commit54f2d53263221ce96bb8073122511adeb87df68d (patch)
tree687a9c53224d891a02e88ca8370a2aaaa2b09a94 /server.c
parentReplace torus.7 with torus.1 (diff)
downloadtorus-54f2d53263221ce96bb8073122511adeb87df68d.tar.gz
torus-54f2d53263221ce96bb8073122511adeb87df68d.zip
Add server daemonization
Diffstat (limited to '')
-rw-r--r--server.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/server.c b/server.c
index 787b258..7edf3f2 100644
--- a/server.c
+++ b/server.c
@@ -19,6 +19,7 @@
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <libutil.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <stdint.h>
@@ -359,15 +360,23 @@ int main(int argc, char *argv[]) {
 
 	const char *dataPath = "torus.dat";
 	const char *sockPath = "torus.sock";
+	const char *pidPath = NULL;
 	int opt;
-	while (0 < (opt = getopt(argc, argv, "d:s:"))) {
+	while (0 < (opt = getopt(argc, argv, "d:p:s:"))) {
 		switch (opt) {
 			break; case 'd': dataPath = optarg;
+			break; case 'p': pidPath = optarg;
 			break; case 's': sockPath = optarg;
 			break; default:  return EX_USAGE;
 		}
 	}
 
+	struct pidfh *pid = NULL;
+	if (pidPath) {
+		pid = pidfile_open(pidPath, 0600, NULL);
+		if (!pid) err(EX_CANTCREAT, "%s", pidPath);
+	}
+
 	tilesMap(dataPath);
 
 	int server = socket(PF_LOCAL, SOCK_STREAM, 0);
@@ -381,6 +390,12 @@ int main(int argc, char *argv[]) {
 	error = bind(server, (struct sockaddr *)&addr, SUN_LEN(&addr));
 	if (error) err(EX_CANTCREAT, "%s", sockPath);
 
+	if (pid) {
+		error = daemon(0, 0);
+		if (error) err(EX_OSERR, "daemon");
+		pidfile_write(pid);
+	}
+
 	error = listen(server, 0);
 	if (error) err(EX_OSERR, "listen");