]> 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 872a68d5e72b8d5cfb10623a2cd02f13b2e5e5da..9407f6fa0334af796649741e361cc4b0e30969a8 100644 (file)
--- a/nwhod.c
+++ b/nwhod.c
@@ -1,14 +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 <stdarg.h>
 #include <fcntl.h>
+#include <getopt.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include <netdb.h>
@@ -22,7 +22,7 @@
 #include <dirent.h>
 #include <errno.h>
 
-#include "net.h"
+#include "nwho.h"
 
 struct hostrec {
   struct hostrec *next;
@@ -31,25 +31,12 @@ 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;
 static char hostname[64];
 
-static void die(char *msg, ...) __attribute__((noreturn));
-
-static void
-die(char *msg, ...)
-{
-  va_list args;
-
-  va_start(args, msg);
-  fprintf(stderr, "nwhod: ");
-  vfprintf(stderr, msg, args);
-  fputc('\n', stderr);
-  exit(1);
-}
-
 static void
 net_init(char *name)
 {
@@ -151,7 +138,7 @@ scan_load(struct nwho_pkt *p)
 static void
 make_pkt(struct nwho_pkt *pkt)
 {
-  bzero(pkt, sizeof(pkt));
+  bzero(pkt, sizeof(*pkt));
   pkt->magic = htonl(NWHO_MAGIC);
   pkt->local_time = htonl(now);
   scan_utmp(pkt, now);
@@ -176,7 +163,7 @@ cleanup(void)
 static void
 save_pkt(char *name, struct nwho_pkt *pkt)
 {
-  int len = sizeof(pkt) - (MAX_USERS - ntohl(pkt->num_users))*sizeof(struct userinfo);
+  int len = nwho_pkt_size(pkt);
   int fd = open(name, O_WRONLY | O_CREAT, 0666);
   if (fd < 0)
     {
@@ -230,7 +217,7 @@ receive(void)
   if (r < n ||
       pkt.magic != htonl(NWHO_MAGIC) ||
       ntohl(pkt.num_users) > MAX_USERS ||
-      r != n + ntohl(pkt.num_users)*sizeof(struct userinfo))
+      r < nwho_pkt_size(&pkt))
     {
       syslog(LOG_WARNING, "Malformed packet from %s", inet_ntoa(sa.sin_addr));
       return;
@@ -306,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");
@@ -359,23 +349,49 @@ client(char *serv)
     {
       now = time(NULL);
       make_pkt(&pkt);
-      if (send(sock, &pkt, sizeof(pkt) - (MAX_USERS - ntohl(pkt.num_users))*sizeof(struct userinfo), 0) < 0)
+      if (send(sock, &pkt, nwho_pkt_size(&pkt), 0) < 0)
        syslog(LOG_ERR, "sendmsg: %m");
       sleep(DEFAULT_SEND_TIME);
     }
 }
 
+static void
+usage(void)
+{
+  fprintf(stderr, "Usage: nwhod [-d] [<server>]\n");
+  exit(1);
+}
+
 int
 main(int argc, char **argv)
 {
-  if (argc == 2)
-    client(argv[1]);
-  else if (argc == 1)
-    server();
-  else
+  if (argc == 2 && !strcmp(argv[1], "--version"))
+    {
+      printf("nwho " STRINGIFY(VERSION) "\n");
+      return 0;
+    }
+
+  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;
 }