maint: use EXIT_SUCCESS and EXIT_FAILURE, not 0 and 1 to exit
[libguestfs.git] / daemon / guestfsd.c
index 61a6236..067c916 100644 (file)
 #include <rpc/types.h>
 #include <rpc/xdr.h>
 #include <getopt.h>
-#include <netdb.h>
 #include <sys/param.h>
-#include <sys/select.h>
 #include <sys/types.h>
-#include <sys/wait.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <signal.h>
+
+#ifdef HAVE_NETDB_H
+#include <netdb.h>
+#endif
+
+#ifdef HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif
+
+#ifdef HAVE_SYS_WAIT_H
+#include <sys/wait.h>
+#endif
+
+#ifdef HAVE_PRINTF_H
 #include <printf.h>
+#endif
 
 #include "c-ctype.h"
 #include "ignore-value.h"
@@ -121,17 +133,17 @@ main (int argc, char *argv[])
 
     case '?':
       usage ();
-      exit (0);
+      exit (EXIT_SUCCESS);
 
     default:
       fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
   }
 
   if (optind < argc) {
     usage ();
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   cmdline = read_cmdline ();
@@ -190,7 +202,7 @@ main (int argc, char *argv[])
       vmchannel = strndup (p + 18, len);
       if (!vmchannel) {
         perror ("strndup");
-        exit (1);
+        exit (EXIT_FAILURE);
       }
     }
 
@@ -204,7 +216,7 @@ main (int argc, char *argv[])
         vmchannel = strndup (p + 4, len);
         if (!vmchannel) {
           perror ("strndup");
-          exit (1);
+          exit (EXIT_FAILURE);
         }
         memcpy (vmchannel, "tcp:", 4);
       }
@@ -216,7 +228,7 @@ main (int argc, char *argv[])
     vmchannel = strdup ("tcp:" GUESTFWD_ADDR ":" GUESTFWD_PORT);
     if (!vmchannel) {
       perror ("strdup");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
   }
 
@@ -226,7 +238,7 @@ main (int argc, char *argv[])
   /* Connect to vmchannel. */
   int sock = -1;
 
-  if (strncmp (vmchannel, "tcp:", 4) == 0) {
+  if (STREQLEN (vmchannel, "tcp:", 4)) {
     /* Resolve the hostname. */
     struct addrinfo *res, *rr;
     struct addrinfo hints;
@@ -241,7 +253,7 @@ main (int argc, char *argv[])
     } else {
       fprintf (stderr, "vmchannel: expecting \"tcp:<ip>:<port>\": %s\n",
                vmchannel);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 
     memset (&hints, 0, sizeof hints);
@@ -251,7 +263,7 @@ main (int argc, char *argv[])
     if (r != 0) {
       fprintf (stderr, "%s:%s: %s\n",
                host, port, gai_strerror (r));
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 
     /* Connect to the given TCP socket. */
@@ -272,7 +284,7 @@ main (int argc, char *argv[])
              "unknown vmchannel connection type: %s\n"
              "expecting \"tcp:<ip>:<port>\"\n",
              vmchannel);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   if (sock == -1) {
@@ -291,7 +303,7 @@ main (int argc, char *argv[])
              "or on the libguestfs redhat com mailing list.\n"
              "\n",
              vmchannel);
-    exit (1);
+    exit (EXIT_FAILURE);
   }
 
   /* Send the magic length message which indicates that
@@ -304,7 +316,7 @@ main (int argc, char *argv[])
   xdr_uint32_t (&xdr, &len);
 
   if (xwrite (sock, lenbuf, sizeof lenbuf) == -1)
-    exit (1);
+    exit (EXIT_FAILURE);
 
   xdr_destroy (&xdr);
 
@@ -312,14 +324,14 @@ main (int argc, char *argv[])
   if (!dont_fork) {
     if (daemon (0, 1) == -1) {
       perror ("daemon");
-      exit (1);
+      exit (EXIT_FAILURE);
     }
   }
 
   /* Enter the main loop, reading and performing actions. */
   main_loop (sock);
 
-  exit (0);
+  exit (EXIT_SUCCESS);
 }
 
 /* Read /proc/cmdline. */
@@ -831,7 +843,7 @@ split_lines (char *str)
   int size = 0, alloc = 0;
   char *p, *pend;
 
-  if (strcmp (str, "") == 0)
+  if (STREQ (str, ""))
     goto empty_list;
 
   p = str;
@@ -976,5 +988,25 @@ device_name_translation (char *device, const char *func)
 void
 udev_settle (void)
 {
-  command (NULL, NULL, "/sbin/udevadm", "settle", NULL);
+  static int which_prog = 0;
+
+  if (which_prog == 0) {
+    if (access ("/sbin/udevsettle", X_OK) == 0)
+      which_prog = 2;
+    else if (access ("/sbin/udevadm", X_OK) == 0)
+      which_prog = 1;
+    else
+      which_prog = 3;
+  }
+
+  switch (which_prog) {
+  case 1:
+    command (NULL, NULL, "/sbin/udevadm", "settle", NULL);
+    break;
+  case 2:
+    command (NULL, NULL, "/sbin/udevsettle", NULL);
+    break;
+  default:
+    ;
+  }
 }