about summary refs log tree commit diff homepage
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
commit287c2039af668ec13fb7b509475acbff338dd4fe (patch)
treeec009885735b1053f15950af7be9c2b7cf9b1e88
parentReplace torus.7 with torus.1 (diff)
downloadtorus-287c2039af668ec13fb7b509475acbff338dd4fe.tar.gz
torus-287c2039af668ec13fb7b509475acbff338dd4fe.zip
Add server daemonization
-rw-r--r--Makefile2
-rw-r--r--README5
-rw-r--r--server.c17
-rw-r--r--torus.15
4 files changed, 26 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index 776a032..3b3a08a 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ CHROOT_GROUP = $(CHROOT_USER)
 
 CFLAGS += -Wall -Wextra -Wpedantic
 LDFLAGS += -static
-LDLIBS = -lcursesw -lz
+LDLIBS = -lcursesw -lutil -lz
 BINS = server client image meta merge
 OBJS = $(BINS:%=%.o)
 
diff --git a/README b/README
index c4f2494..743a4e6 100644
--- a/README
+++ b/README
@@ -4,7 +4,7 @@ NAME
      server, client, image, meta, merge – collaborative ASCII art
 
 SYNOPSIS
-     server [-d data] [-s sock]
+     server [-d data] [-p pidfile] [-s sock]
      client [-h] [-s sock]
      image [-d data] [-f font] [-x x] [-y y]
      meta
@@ -38,6 +38,9 @@ DESCRIPTION
 
      -h      Write help page data to standard output and exit.
 
+     -p pidfile
+             Daemonize and write PID to pidfile.
+
      -s sock
              Set path to UNIX-domain socket.  The default path is torus.sock.
 
diff --git a/server.c b/server.c
index 52082e1..a62b286 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");
 
diff --git a/torus.1 b/torus.1
index 52f12db..6bad6be 100644
--- a/torus.1
+++ b/torus.1
@@ -13,6 +13,7 @@
 .Sh SYNOPSIS
 .Nm server
 .Op Fl d Ar data
+.Op Fl p Ar pidfile
 .Op Fl s Ar sock
 .
 .Nm client
@@ -97,6 +98,10 @@ The default path is
 .It Fl h
 Write help page data to standard output and exit.
 .
+.It Fl p Ar pidfile
+Daemonize and write PID to
+.Ar pidfile .
+.
 .It Fl s Ar sock
 Set path to UNIX-domain socket.
 The default path is