Implement device name translation. Remove device name hacks in tests.
[libguestfs.git] / daemon / guestfsd.c
index eeb84bd..dc839ce 100644 (file)
@@ -32,6 +32,7 @@
 #include <sys/select.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <sys/stat.h>
 #include <ctype.h>
 #include <signal.h>
 
@@ -150,6 +151,14 @@ main (int argc, char *argv[])
   if (sigaction (SIGPIPE, &sa, NULL) == -1)
     perror ("sigaction SIGPIPE"); /* but try to continue anyway ... */
 
+  /* Set up a basic environment.  After we are called by /init the
+   * environment is essentially empty.
+   * https://bugzilla.redhat.com/show_bug.cgi?id=502074#c5
+   */
+  setenv ("PATH", "/usr/bin:/bin", 1);
+  setenv ("SHELL", "/bin/sh", 1);
+  setenv ("LANG", "C", 1);
+
   /* Resolve the hostname. */
   memset (&hints, 0, sizeof hints);
   hints.ai_socktype = SOCK_STREAM;
@@ -587,6 +596,60 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv)
     return -1;
 }
 
+/* Split an output string into a NULL-terminated list of lines.
+ * Typically this is used where we have run an external command
+ * which has printed out a list of things, and we want to return
+ * an actual list.
+ *
+ * The corner cases here are quite tricky.  Note in particular:
+ *
+ *   "" -> []
+ *   "\n" -> [""]
+ *   "a\nb" -> ["a"; "b"]
+ *   "a\nb\n" -> ["a"; "b"]
+ *   "a\nb\n\n" -> ["a"; "b"; ""]
+ *
+ * The original string is written over and destroyed by this
+ * function (which is usually OK because it's the 'out' string
+ * from command()).  You can free the original string, because
+ * add_string() strdups the strings.
+ */
+char **
+split_lines (char *str)
+{
+  char **lines = NULL;
+  int size = 0, alloc = 0;
+  char *p, *pend;
+
+  if (strcmp (str, "") == 0)
+    goto empty_list;
+
+  p = str;
+  while (p) {
+    /* Empty last line? */
+    if (p[0] == '\0')
+      break;
+
+    pend = strchr (p, '\n');
+    if (pend) {
+      *pend = '\0';
+      pend++;
+    }
+
+    if (add_string (&lines, &size, &alloc, p) == -1) {
+      return NULL;
+    }
+
+    p = pend;
+  }
+
+ empty_list:
+  if (add_string (&lines, &size, &alloc, NULL) == -1)
+    return NULL;
+
+  return lines;
+}
+
 /* Quote 'in' for the shell, and write max len-1 bytes to out.  The
  * result will be NUL-terminated, even if it is truncated.
  *
@@ -623,3 +686,35 @@ 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.
+ */
+int
+device_name_translation (char *device, const char *func)
+{
+  struct stat statbuf;
+
+  if (stat (device, &statbuf) == -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) */
+    if (stat (device, &statbuf) == 0)
+      return 0;
+
+    device[5] = 'v';           /* /dev/vd (for virtio devices) */
+    if (stat (device, &statbuf) == 0)
+      return 0;
+
+    device[5] = 's';           /* Restore original device name. */
+
+   error:
+    reply_with_perror ("%s: %s", func, device);
+    return -1;
+  }
+  return 0;
+}