Docs: Add "API Overview" section to guestfs(3) manpage.
[libguestfs.git] / daemon / guestfsd.c
index 581c82e..5c250f0 100644 (file)
@@ -47,6 +47,10 @@ static void usage (void);
 
 int verbose = 0;
 
+/* Location to mount root device. */
+const char *sysroot = "/sysroot"; /* No trailing slash. */
+int sysroot_len = 8;
+
 int
 main (int argc, char *argv[])
 {
@@ -221,6 +225,25 @@ main (int argc, char *argv[])
   exit (0);
 }
 
+/* Turn "/path" into "/sysroot/path".
+ *
+ * Caller must check for NULL and call reply_with_perror ("malloc")
+ * if it is.  Caller must also free the string.
+ */
+char *
+sysroot_path (const char *path)
+{
+  char *r;
+  int len = strlen (path) + sysroot_len + 1;
+
+  r = malloc (len);
+  if (r == NULL)
+    return NULL;
+
+  snprintf (r, len, "%s%s", sysroot, path);
+  return r;
+}
+
 int
 xwrite (int sock, const void *buf, size_t len)
 {
@@ -572,19 +595,23 @@ commandrv (char **stdoutput, char **stderror, char * const* const argv)
    * trailing \n characters from the error buffer (not from stdout).
    */
   if (stdoutput) {
-    *stdoutput = realloc (*stdoutput, so_size+1);
-    if (*stdoutput == NULL) {
+    void *q = realloc (*stdoutput, so_size+1);
+    if (q == NULL) {
       perror ("realloc");
-      *stdoutput = NULL;
-    } else
+      free (*stdoutput);
+    }
+    *stdoutput = q;
+    if (*stdoutput)
       (*stdoutput)[so_size] = '\0';
   }
   if (stderror) {
-    *stderror = realloc (*stderror, se_size+1);
-    if (*stderror == NULL) {
+    void *q = realloc (*stderror, se_size+1);
+    if (q == NULL) {
       perror ("realloc");
-      *stderror = NULL;
-    } else {
+      free (*stderror);
+    }
+    *stderror = q;
+    if (*stderror) {
       (*stderror)[se_size] = '\0';
       se_size--;
       while (se_size >= 0 && (*stderror)[se_size] == '\n')