]> mj.ucw.cz Git - nwho.git/commitdiff
Implemented non-daemonizing mode
authorMartin Mares <mj@ucw.cz>
Thu, 15 Feb 2018 17:03:33 +0000 (18:03 +0100)
committerMartin Mares <mj@ucw.cz>
Thu, 15 Feb 2018 17:03:33 +0000 (18:03 +0100)
nwhod.c

diff --git a/nwhod.c b/nwhod.c
index 4e32ea67ee12adb4384e30b5732852974bc83775..443f78a900c1ddb4aa55a3f9f7fd14cfc01bb1ef 100644 (file)
--- a/nwhod.c
+++ b/nwhod.c
@@ -1,13 +1,14 @@
 /*
  *     The Remote User Information Daemon
  *
- *     (c) 1997--2010 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2017 Martin Mares <mj@ucw.cz>
  */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
+#include <getopt.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include <netdb.h>
@@ -30,6 +31,7 @@ struct hostrec {
   u32 addr;
 };
 
+static int do_not_daemonize;
 static int sock, port;
 static struct hostrec *first_host;
 static time_t now, last_local_scan;
@@ -291,6 +293,9 @@ do_tick(void)
 static void
 daemonize(void)
 {
+  if (do_not_daemonize)
+    return;
+
   pid_t pid = fork();
   if (pid < 0)
     die("Fork failed: %m");
@@ -350,6 +355,13 @@ client(char *serv)
     }
 }
 
+static void
+usage(void)
+{
+  fprintf(stderr, "Usage: nwhod [-d] [<server>]\n");
+  exit(1);
+}
+
 int
 main(int argc, char **argv)
 {
@@ -359,14 +371,22 @@ main(int argc, char **argv)
       return 0;
     }
 
-  if (argc == 2)
-    client(argv[1]);
-  else if (argc == 1)
+  int opt;
+  while ((opt = getopt(argc, argv, "d")) >= 0)
+    switch (opt)
+      {
+      case 'd':
+       do_not_daemonize = 1;
+       break;
+      default:
+       usage();
+      }
+
+  if (optind == argc-1)
+    client(argv[optind]);
+  else if (optind == argc)
     server();
   else
-    {
-      fprintf(stderr, "Usage: nwhod [<server>]\n");
-      return 1;
-    }
+    usage();
   return 0;
 }