From bae9fa1480c4247295756e0a2312c58210f360e8 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 3 Mar 2014 11:53:43 -0800 Subject: [PATCH] -d switch for daemonizing on Unix --- main.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 5ca12f0fc..7ccf4e4aa 100644 --- a/main.cpp +++ b/main.cpp @@ -95,6 +95,9 @@ static void printHelp(const char *cn,FILE *out) fprintf(out," -v - Show version"ZT_EOL_S); fprintf(out," -p - Bind to this port for network I/O"ZT_EOL_S); fprintf(out," -c - Bind to this port for local control packets"ZT_EOL_S); +#ifdef __UNIX_LIKE__ + fprintf(out," -d - Fork and run as daemon (Unix-ish OSes)"ZT_EOL_S); +#endif fprintf(out," -q - Send a query to a running service (zerotier-cli)"ZT_EOL_S); fprintf(out," -i - Run idtool command (zerotier-idtool)"ZT_EOL_S); #ifdef __WINDOWS__ @@ -520,6 +523,9 @@ int main(int argc,char **argv) const char *homeDir = (const char *)0; unsigned int port = 0; unsigned int controlPort = 0; +#ifdef __UNIX_LIKE__ + bool runAsDaemon = false; +#endif #ifdef __WINDOWS__ bool winRunFromCommandLine = false; #endif @@ -533,6 +539,11 @@ int main(int argc,char **argv) return 1; } break; +#ifdef __UNIX_LIKE__ + case 'd': + runAsDaemon = true; + break; +#endif case 'v': printf("%s"ZT_EOL_S,Node::versionString()); return 0; @@ -617,11 +628,23 @@ int main(int argc,char **argv) homeDir = ZT_DEFAULTS.defaultHomePath.c_str(); #ifdef __UNIX_LIKE__ - if (getuid()) { - fprintf(stderr,"%s: must be run as root (uid==0)\n",argv[0]); + if (getuid() != 0) { + fprintf(stderr,"%s: must be run as root (uid 0)\n",argv[0]); return 1; } - mkdir(homeDir,0755); // will fail if it already exists + + if (runAsDaemon) { + long p = (long)fork(); + if (p < 0) { + fprintf(stderr,"%s: could not fork"ZT_EOL_S,argv[0]); + return 1; + } else if (p > 0) + return 0; // forked + // else p == 0, so we are daemonized + } + + mkdir(homeDir,0755); // will fail if it already exists, but that's fine + { // Write .pid file to home folder char pidpath[4096];