|
|
@@ -43,6 +43,8 @@ |
|
|
|
#include <errno.h> |
|
|
|
#include <fcntl.h> |
|
|
|
#include <libgen.h> |
|
|
|
#include <libutil.h> |
|
|
|
#include <paths.h> |
|
|
|
#include <pthread.h> |
|
|
|
#include <signal.h> |
|
|
|
#include <stdarg.h> |
|
|
@@ -946,15 +948,18 @@ huphandler(int sig __unused) |
|
|
|
int |
|
|
|
main(int argc, char *argv[]) |
|
|
|
{ |
|
|
|
const char *ggated_pidfile = _PATH_VARRUN "/ggated.pid"; |
|
|
|
struct pidfh *pfh; |
|
|
|
struct sockaddr_in serv; |
|
|
|
struct sockaddr from; |
|
|
|
socklen_t fromlen; |
|
|
|
pid_t otherpid; |
|
|
|
int ch, sfd, tmpsfd; |
|
|
|
unsigned port; |
|
|
|
|
|
|
|
bindaddr = htonl(INADDR_ANY); |
|
|
|
port = G_GATE_PORT; |
|
|
|
while ((ch = getopt(argc, argv, "a:hnp:R:S:v")) != -1) { |
|
|
|
while ((ch = getopt(argc, argv, "a:hnp:F:R:S:v")) != -1) { |
|
|
|
switch (ch) { |
|
|
|
case 'a': |
|
|
|
bindaddr = g_gate_str2ip(optarg); |
|
|
@@ -963,6 +968,9 @@ main(int argc, char *argv[]) |
|
|
|
"Invalid IP/host name to bind to."); |
|
|
|
} |
|
|
|
break; |
|
|
|
case 'F': |
|
|
|
ggated_pidfile = optarg; |
|
|
|
break; |
|
|
|
case 'n': |
|
|
|
nagle = 0; |
|
|
|
break; |
|
|
@@ -999,12 +1007,23 @@ main(int argc, char *argv[]) |
|
|
|
exports_file = argv[0]; |
|
|
|
exports_get(); |
|
|
|
|
|
|
|
pfh = pidfile_open(ggated_pidfile, 0600, &otherpid); |
|
|
|
if (pfh == NULL) { |
|
|
|
if (errno == EEXIST) { |
|
|
|
errx(EXIT_FAILURE, "Daemon already running, pid: %jd.", |
|
|
|
(intmax_t)otherpid); |
|
|
|
} |
|
|
|
err(EXIT_FAILURE, "Cannot open/create pidfile"); |
|
|
|
} |
|
|
|
|
|
|
|
if (!g_gate_verbose) { |
|
|
|
/* Run in daemon mode. */ |
|
|
|
if (daemon(0, 0) == -1) |
|
|
|
g_gate_xlog("Cannot daemonize: %s", strerror(errno)); |
|
|
|
} |
|
|
|
|
|
|
|
pidfile_write(pfh); |
|
|
|
|
|
|
|
signal(SIGCHLD, SIG_IGN); |
|
|
|
|
|
|
|
sfd = socket(AF_INET, SOCK_STREAM, 0); |
|
|
@@ -1041,5 +1060,6 @@ main(int argc, char *argv[]) |
|
|
|
close(tmpsfd); |
|
|
|
} |
|
|
|
close(sfd); |
|
|
|
pidfile_remove(pfh); |
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
} |