From 287c2039af668ec13fb7b509475acbff338dd4fe Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sun, 14 Oct 2018 19:50:44 -0400 Subject: Add server daemonization --- server.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'server.c') diff --git a/server.c b/server.c index 52082e1..a62b286 100644 --- a/server.c +++ b/server.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -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"); -- cgit 1.4.1