daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / daemon / mount.c
index c584f81..98b9488 100644 (file)
@@ -13,7 +13,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <config.h>
@@ -40,9 +40,12 @@ is_root_mounted (void)
   FILE *fp;
   struct mntent *m;
 
-  fp = setmntent ("/etc/mtab", "r");
+  /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
+   * that requires custom parsing code.
+   */
+  fp = setmntent ("/proc/mounts", "r");
   if (fp == NULL) {
-    perror ("/etc/mtab");
+    perror ("/proc/mounts");
     exit (EXIT_FAILURE);
   }
 
@@ -80,7 +83,7 @@ do_mount_vfs (const char *options, const char *vfstype,
   char *error;
   struct stat statbuf;
 
-  ABS_PATH (mountpoint, 0, return -1);
+  ABS_PATH (mountpoint, , return -1);
 
   mp = sysroot_path (mountpoint);
   if (!mp) {
@@ -113,13 +116,14 @@ do_mount_vfs (const char *options, const char *vfstype,
     return -1;
   }
 
+  free (error);
   return 0;
 }
 
 int
 do_mount (const char *device, const char *mountpoint)
 {
-  return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
+  return do_mount_vfs ("", NULL, device, mountpoint);
 }
 
 int
@@ -155,7 +159,7 @@ do_umount (const char *pathordevice)
   }
 
   if (is_dev)
-    RESOLVE_DEVICE (buf, 0, { free (buf); return -1; });
+    RESOLVE_DEVICE (buf, , { free (buf); return -1; });
 
   r = command (NULL, &err, "umount", buf, NULL);
   free (buf);
@@ -171,68 +175,49 @@ do_umount (const char *pathordevice)
   return 0;
 }
 
+/* Implement 'mounts' (mp==0) and 'mountpoints' (mp==1) calls. */
 static char **
 mounts_or_mountpoints (int mp)
 {
-  char *out, *err;
-  int r;
+  FILE *fp;
+  struct mntent *m;
   char **ret = NULL;
   int size = 0, alloc = 0;
-  char *p, *pend, *p2;
-  int len;
-  char matching[5 + sysroot_len];
   size_t i;
+  int r;
 
-  r = command (&out, &err, "mount", NULL);
-  if (r == -1) {
-    reply_with_error ("mount: %s", err);
-    free (out);
-    free (err);
-    return NULL;
-  }
-
-  free (err);
-
-  /* Lines have the format:
-   *   /dev/foo on /mountpoint type ...
+  /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
+   * that requires custom parsing code.
    */
-  snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
-
-  p = out;
-  while (p) {
-    pend = strchr (p, '\n');
-    if (pend) {
-      *pend = '\0';
-      pend++;
-    }
+  fp = setmntent ("/proc/mounts", "r");
+  if (fp == NULL) {
+    perror ("/proc/mounts");
+    exit (EXIT_FAILURE);
+  }
 
-    p2 = strstr (p, matching);
-    if (p2 != NULL) {
-      *p2 = '\0';
-      if (add_string (&ret, &size, &alloc, p) == -1) {
-        free (out);
+  while ((m = getmntent (fp)) != NULL) {
+    /* Allow a mount directory like "/sysroot". */
+    if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
+      if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1) {
+      error:
+        endmntent (fp);
         return NULL;
       }
-      if (mp) {
-        p2 += 4 + sysroot_len; /* skip " on /sysroot" */
-        len = strcspn (p2, " ");
-
-        if (len == 0)          /* .. just /sysroot, so we turn it into "/" */
-          p2 = (char *) "/";
-        else
-          p2[len] = '\0';
-
-        if (add_string (&ret, &size, &alloc, p2) == -1) {
-          free (out);
-          return NULL;
-        }
-      }
+      if (mp &&
+          add_string (&ret, &size, &alloc, "/") == -1)
+        goto error;
+    }
+    /* Or allow a mount directory like "/sysroot/...". */
+    if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') {
+      if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1)
+        goto error;
+      if (mp &&
+          add_string (&ret, &size, &alloc, &m->mnt_dir[sysroot_len]) == -1)
+        goto error;
     }
-
-    p = pend;
   }
 
-  free (out);
+  endmntent (fp);
 
   if (add_string (&ret, &size, &alloc, NULL) == -1)
     return NULL;
@@ -295,50 +280,40 @@ compare_longest_first (const void *vp1, const void *vp2)
 int
 do_umount_all (void)
 {
-  char *out, *err;
-  int i, r;
+  FILE *fp;
+  struct mntent *m;
   char **mounts = NULL;
   int size = 0, alloc = 0;
-  char *p, *p2, *p3, *pend;
-  char matching[5 + sysroot_len];
+  char *err;
+  int i, r;
 
-  r = command (&out, &err, "mount", NULL);
-  if (r == -1) {
-    reply_with_error ("mount: %s", err);
-    free (out);
-    free (err);
-    return -1;
+  /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
+   * that requires custom parsing code.
+   */
+  fp = setmntent ("/proc/mounts", "r");
+  if (fp == NULL) {
+    perror ("/proc/mounts");
+    exit (EXIT_FAILURE);
   }
 
-  free (err);
-
-  /* Lines have the format:
-   *   /dev/foo on /mountpoint type ...
-   */
-  snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
-
-  p = out;
-  while (p) {
-    pend = strchr (p, '\n');
-    if (pend) {
-      *pend = '\0';
-      pend++;
+  while ((m = getmntent (fp)) != NULL) {
+    /* Allow a mount directory like "/sysroot". */
+    if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
+      if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
+        endmntent (fp);
+        return -1;
+      }
     }
-
-    p2 = strstr (p, matching);
-    if (p2 != NULL) {
-      p2 += 4;
-      p3 = p2 + strcspn (p2, " ");
-      *p3 = '\0';
-      if (add_string (&mounts, &size, &alloc, p2) == -1) {
-        free (out);
+    /* Or allow a mount directory like "/sysroot/...". */
+    if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') {
+      if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
+        endmntent (fp);
         return -1;
       }
     }
-
-    p = pend;
   }
-  free (out);
+
+  endmntent (fp);
 
   qsort (mounts, size, sizeof (char *), compare_longest_first);
 
@@ -378,7 +353,7 @@ do_mount_loop (const char *file, const char *mountpoint)
   }
 
   buf = sysroot_path (file);
-  if (!file) {
+  if (!buf) {
     reply_with_perror ("malloc");
     free (mp);
     return -1;
@@ -393,6 +368,7 @@ do_mount_loop (const char *file, const char *mountpoint)
     return -1;
   }
 
+  free (error);
   return 0;
 }
 
@@ -406,7 +382,7 @@ do_mkmountpoint (const char *path)
   int r;
 
   /* NEED_ROOT (return -1); - we don't want this test for this call. */
-  ABS_PATH (path, 0, return -1);
+  ABS_PATH (path, , return -1);
 
   CHROOT_IN;
   r = mkdir (path, 0777);
@@ -426,7 +402,7 @@ do_rmmountpoint (const char *path)
   int r;
 
   /* NEED_ROOT (return -1); - we don't want this test for this call. */
-  ABS_PATH (path, 0, return -1);
+  ABS_PATH (path, , return -1);
 
   CHROOT_IN;
   r = rmdir (path);