]> mj.ucw.cz Git - nwho.git/blobdiff - nwhod.c
Fixed calculation of idle time (only atime should be used)
[nwho.git] / nwhod.c
diff --git a/nwhod.c b/nwhod.c
index c6ebade19f03c52ce9646716c6b8d9e090e3a01c..5d0cee02fae186192658bf61d73da082bef5f040 100644 (file)
--- a/nwhod.c
+++ b/nwhod.c
@@ -20,7 +20,6 @@
 #include <utmp.h>
 #include <signal.h>
 #include <dirent.h>
-#include <asm/types.h>
 #include <errno.h>
 
 #include "net.h"
@@ -59,26 +58,26 @@ net_init(char *name)
   if (name)
     {
       if (! (h = gethostbyname(name)))
-       die("%s: %m", name);
+       die("Failed to resolve %s", name);
     }
   else
     h = NULL;
 
   sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
   if (sock < 0)
-    die("socket: %m");
+    die("Cannot create socket: %m");
 
   sa.sin_family = AF_INET;
   sa.sin_port = port = htons(NWHO_PORT);
   sa.sin_addr.s_addr = INADDR_ANY;
   if (bind(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
-    die("bind: %m");
+    die("Cannot bind to UDP port %d: %m", NWHO_PORT);
 
   if (h)
     {
       memcpy(&sa.sin_addr.s_addr, h->h_addr, sizeof(sa.sin_addr.s_addr));
       if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
-       die("connect: %m");
+       die("Cannot connect socket: %m");
     }
 }
 
@@ -108,10 +107,6 @@ scan_utmp(struct nwho_pkt *p, time_t now)
          continue;
        h->mesg_y = !!(S_IWGRP & st.st_mode);
        last = st.st_atime;
-       if (st.st_mtime > last)
-         last = st.st_mtime;
-       if (st.st_ctime > last)
-         last = st.st_ctime;
        if (now < last)
          last = now;
        h->idle_time = htonl(now - last);
@@ -311,6 +306,12 @@ do_tick(void)
 static void
 daemonize(void)
 {
+  pid_t pid = fork();
+  if (pid < 0)
+    die("Fork failed: %m");
+  if (pid)
+    exit(0);
+
   close(0);
   close(1);
   close(2);
@@ -367,24 +368,14 @@ client(char *serv)
 int
 main(int argc, char **argv)
 {
-  int pid;
-
-  if (argc != 1 && argc != 2)
-    {
-      fprintf(stderr, "Usage: nwhod [<server>]\n");
-      return 1;
-    }
-  pid = fork();
-  if (pid < 0)
-    {
-      perror("fork");
-      return 1;
-    }
-  if (pid)
-    return 0;
   if (argc == 2)
     client(argv[1]);
   else if (argc == 1)
     server();
-  return 1;
+  else
+    {
+      fprintf(stderr, "Usage: nwhod [<server>]\n");
+      return 1;
+    }
+  return 0;
 }