]> mj.ucw.cz Git - nwho.git/blobdiff - nwhod.c
Make the systemd service be ordered after autofs.service, so that it is ordered befor...
[nwho.git] / nwhod.c
diff --git a/nwhod.c b/nwhod.c
index 4e32ea67ee12adb4384e30b5732852974bc83775..9407f6fa0334af796649741e361cc4b0e30969a8 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,27 @@ main(int argc, char **argv)
       return 0;
     }
 
-  if (argc == 2)
-    client(argv[1]);
-  else if (argc == 1)
-    server();
-  else
+  int opt;
+  while ((opt = getopt(argc, argv, "d")) >= 0)
+    switch (opt)
+      {
+      case 'd':
+       do_not_daemonize = 1;
+       break;
+      default:
+       usage();
+      }
+
+  if (optind == argc-1)
     {
-      fprintf(stderr, "Usage: nwhod [<server>]\n");
-      return 1;
+      if (argv[optind][0])
+       client(argv[optind]);
+      else
+       server();
     }
+  else if (optind == argc)
+    server();
+  else
+    usage();
   return 0;
 }