mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-05-31 22:40:48 +00:00
-d switch for daemonizing on Unix
This commit is contained in:
parent
cef750d1d2
commit
bae9fa1480
29
main.cpp
29
main.cpp
@ -95,6 +95,9 @@ static void printHelp(const char *cn,FILE *out)
|
|||||||
fprintf(out," -v - Show version"ZT_EOL_S);
|
fprintf(out," -v - Show version"ZT_EOL_S);
|
||||||
fprintf(out," -p<port> - Bind to this port for network I/O"ZT_EOL_S);
|
fprintf(out," -p<port> - Bind to this port for network I/O"ZT_EOL_S);
|
||||||
fprintf(out," -c<port> - Bind to this port for local control packets"ZT_EOL_S);
|
fprintf(out," -c<port> - 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," -q - Send a query to a running service (zerotier-cli)"ZT_EOL_S);
|
||||||
fprintf(out," -i - Run idtool command (zerotier-idtool)"ZT_EOL_S);
|
fprintf(out," -i - Run idtool command (zerotier-idtool)"ZT_EOL_S);
|
||||||
#ifdef __WINDOWS__
|
#ifdef __WINDOWS__
|
||||||
@ -520,6 +523,9 @@ int main(int argc,char **argv)
|
|||||||
const char *homeDir = (const char *)0;
|
const char *homeDir = (const char *)0;
|
||||||
unsigned int port = 0;
|
unsigned int port = 0;
|
||||||
unsigned int controlPort = 0;
|
unsigned int controlPort = 0;
|
||||||
|
#ifdef __UNIX_LIKE__
|
||||||
|
bool runAsDaemon = false;
|
||||||
|
#endif
|
||||||
#ifdef __WINDOWS__
|
#ifdef __WINDOWS__
|
||||||
bool winRunFromCommandLine = false;
|
bool winRunFromCommandLine = false;
|
||||||
#endif
|
#endif
|
||||||
@ -533,6 +539,11 @@ int main(int argc,char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
#ifdef __UNIX_LIKE__
|
||||||
|
case 'd':
|
||||||
|
runAsDaemon = true;
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
case 'v':
|
case 'v':
|
||||||
printf("%s"ZT_EOL_S,Node::versionString());
|
printf("%s"ZT_EOL_S,Node::versionString());
|
||||||
return 0;
|
return 0;
|
||||||
@ -617,11 +628,23 @@ int main(int argc,char **argv)
|
|||||||
homeDir = ZT_DEFAULTS.defaultHomePath.c_str();
|
homeDir = ZT_DEFAULTS.defaultHomePath.c_str();
|
||||||
|
|
||||||
#ifdef __UNIX_LIKE__
|
#ifdef __UNIX_LIKE__
|
||||||
if (getuid()) {
|
if (getuid() != 0) {
|
||||||
fprintf(stderr,"%s: must be run as root (uid==0)\n",argv[0]);
|
fprintf(stderr,"%s: must be run as root (uid 0)\n",argv[0]);
|
||||||
return 1;
|
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
|
// Write .pid file to home folder
|
||||||
char pidpath[4096];
|
char pidpath[4096];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user