]> mj.ucw.cz Git - nwho.git/blobdiff - nwhod.c
gethostbyname() does not set errno
[nwho.git] / nwhod.c
diff --git a/nwhod.c b/nwhod.c
index f432d00d67f418f21ee83a0d0d88285fe4e1a33c..05e73a91a23eece788753f5cd1d5696bfbcb2bef 100644 (file)
--- a/nwhod.c
+++ b/nwhod.c
@@ -20,7 +20,6 @@
 #include <utmp.h>
 #include <signal.h>
 #include <dirent.h>
 #include <utmp.h>
 #include <signal.h>
 #include <dirent.h>
-#include <asm/types.h>
 #include <errno.h>
 
 #include "net.h"
 #include <errno.h>
 
 #include "net.h"
@@ -59,7 +58,7 @@ net_init(char *name)
   if (name)
     {
       if (! (h = gethostbyname(name)))
   if (name)
     {
       if (! (h = gethostbyname(name)))
-       die("%s: %m", name);
+       die("Failed to resolve %s", name);
     }
   else
     h = NULL;
     }
   else
     h = NULL;
@@ -199,16 +198,16 @@ is_valid(u32 ipa)
 static void
 receive(void)
 {
 static void
 receive(void)
 {
-  int r, al;
+  int r;
   struct nwho_pkt pkt;
   struct sockaddr_in sa;
   struct nwho_pkt pkt;
   struct sockaddr_in sa;
+  socklen_t al = sizeof(sa);
   int n = sizeof(struct nwho_pkt) - MAX_USERS * sizeof(struct userinfo);
   struct hostrec *e;
   struct hostent *h;
   char *c;
 
   alarm(DEFAULT_PRUNE_TIME);
   int n = sizeof(struct nwho_pkt) - MAX_USERS * sizeof(struct userinfo);
   struct hostrec *e;
   struct hostent *h;
   char *c;
 
   alarm(DEFAULT_PRUNE_TIME);
-  al = sizeof(sa);
   r = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa, &al);
   alarm(0);
   now = time(NULL);
   r = recvfrom(sock, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa, &al);
   alarm(0);
   now = time(NULL);
@@ -311,6 +310,12 @@ do_tick(void)
 static void
 daemonize(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);
   close(0);
   close(1);
   close(2);
@@ -367,24 +372,14 @@ client(char *serv)
 int
 main(int argc, char **argv)
 {
 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();
   if (argc == 2)
     client(argv[1]);
   else if (argc == 1)
     server();
-  return 1;
+  else
+    {
+      fprintf(stderr, "Usage: nwhod [<server>]\n");
+      return 1;
+    }
+  return 0;
 }
 }