Generated code for mknod, mkfifo, mknod_b, mknod_c, umask.
[libguestfs.git] / daemon / guestfsd.c
index 1d74054..7eabbd4 100644 (file)
@@ -32,6 +32,8 @@
 #include <sys/select.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <ctype.h>
 #include <signal.h>
 
@@ -158,6 +160,9 @@ main (int argc, char *argv[])
   setenv ("SHELL", "/bin/sh", 1);
   setenv ("LANG", "C", 1);
 
+  /* We document that umask defaults to 022 (it should be this anyway). */
+  umask (022);
+
   /* Resolve the hostname. */
   memset (&hints, 0, sizeof hints);
   hints.ai_socktype = SOCK_STREAM;
@@ -451,7 +456,8 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv)
 {
   int so_size = 0, se_size = 0;
   int so_fd[2], se_fd[2];
-  int pid, r, quit, i;
+  pid_t pid;
+  int r, quit, i;
   fd_set rset, rset2;
   char buf[256];
   char *p;
@@ -587,7 +593,10 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv)
   }
 
   /* Get the exit status of the command. */
-  waitpid (pid, &r, 0);
+  if (waitpid (pid, &r, 0) != pid) {
+    perror ("waitpid");
+    return -1;
+  }
 
   if (WIFEXITED (r)) {
     return WEXITSTATUS (r);
@@ -685,3 +694,50 @@ shell_quote (char *out, int len, const char *in)
 
   return outlen;
 }
+
+/* Perform device name translation.  Don't call this directly -
+ * use the IS_DEVICE macro.
+ *
+ * See guestfs(3) for the algorithm.
+ *
+ * We have to open the device and test for ENXIO, because
+ * the device nodes themselves will exist in the appliance.
+ */
+int
+device_name_translation (char *device, const char *func)
+{
+  int fd;
+
+  fd = open (device, O_RDONLY);
+  if (fd >= 0) {
+    close (fd);
+    return 0;
+  }
+
+  if (errno != ENXIO) {
+  error:
+    reply_with_perror ("%s: %s", func, device);
+    return -1;
+  }
+
+  /* If the name begins with "/dev/sd" then try the alternatives. */
+  if (strncmp (device, "/dev/sd", 7) != 0)
+    goto error;
+
+  device[5] = 'h';             /* /dev/hd (old IDE driver) */
+  fd = open (device, O_RDONLY);
+  if (fd >= 0) {
+    close (fd);
+    return 0;
+  }
+
+  device[5] = 'v';             /* /dev/vd (for virtio devices) */
+  fd = open (device, O_RDONLY);
+  if (fd >= 0) {
+    close (fd);
+    return 0;
+  }
+
+  device[5] = 's';             /* Restore original device name. */
+  goto error;
+}