daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / daemon / mount.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <mntent.h>
28
29 #include "daemon.h"
30 #include "actions.h"
31
32 /* You must mount something on "/" first before many operations.
33  * Hence we have an internal function which can test if something is
34  * mounted on *or under* the sysroot directory.  (It has to be *or
35  * under* because of mkmountpoint and friends).
36  */
37 int
38 is_root_mounted (void)
39 {
40   FILE *fp;
41   struct mntent *m;
42
43   /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
44    * that requires custom parsing code.
45    */
46   fp = setmntent ("/proc/mounts", "r");
47   if (fp == NULL) {
48     perror ("/proc/mounts");
49     exit (EXIT_FAILURE);
50   }
51
52   while ((m = getmntent (fp)) != NULL) {
53     /* Allow a mount directory like "/sysroot". */
54     if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
55     gotit:
56       endmntent (fp);
57       return 1;
58     }
59     /* Or allow a mount directory like "/sysroot/...". */
60     if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/')
61       goto gotit;
62   }
63
64   endmntent (fp);
65   return 0;
66 }
67
68 /* The "simple mount" call offers no complex options, you can just
69  * mount a device on a mountpoint.  The variations like mount_ro,
70  * mount_options and mount_vfs let you set progressively more things.
71  *
72  * It's tempting to try a direct mount(2) syscall, but that doesn't
73  * do any autodetection, so we are better off calling out to
74  * /bin/mount.
75  */
76
77 int
78 do_mount_vfs (const char *options, const char *vfstype,
79               const char *device, const char *mountpoint)
80 {
81   int r;
82   char *mp;
83   char *error;
84   struct stat statbuf;
85
86   ABS_PATH (mountpoint, , return -1);
87
88   mp = sysroot_path (mountpoint);
89   if (!mp) {
90     reply_with_perror ("malloc");
91     return -1;
92   }
93
94   /* Check the mountpoint exists and is a directory. */
95   if (stat (mp, &statbuf) == -1) {
96     reply_with_perror ("mount: %s", mountpoint);
97     free (mp);
98     return -1;
99   }
100   if (!S_ISDIR (statbuf.st_mode)) {
101     reply_with_perror ("mount: %s: mount point is not a directory", mountpoint);
102     free (mp);
103     return -1;
104   }
105
106   if (vfstype)
107     r = command (NULL, &error,
108                  "mount", "-o", options, "-t", vfstype, device, mp, NULL);
109   else
110     r = command (NULL, &error,
111                  "mount", "-o", options, device, mp, NULL);
112   free (mp);
113   if (r == -1) {
114     reply_with_error ("%s on %s: %s", device, mountpoint, error);
115     free (error);
116     return -1;
117   }
118
119   free (error);
120   return 0;
121 }
122
123 int
124 do_mount (const char *device, const char *mountpoint)
125 {
126   return do_mount_vfs ("", NULL, device, mountpoint);
127 }
128
129 int
130 do_mount_ro (const char *device, const char *mountpoint)
131 {
132   return do_mount_vfs ("ro", NULL, device, mountpoint);
133 }
134
135 int
136 do_mount_options (const char *options, const char *device,
137                   const char *mountpoint)
138 {
139   return do_mount_vfs (options, NULL, device, mountpoint);
140 }
141
142 /* Again, use the external /bin/umount program, so that /etc/mtab
143  * is kept updated.
144  */
145 int
146 do_umount (const char *pathordevice)
147 {
148   int r;
149   char *err;
150   char *buf;
151   int is_dev;
152
153   is_dev = STREQLEN (pathordevice, "/dev/", 5);
154   buf = is_dev ? strdup (pathordevice)
155                : sysroot_path (pathordevice);
156   if (buf == NULL) {
157     reply_with_perror ("malloc");
158     return -1;
159   }
160
161   if (is_dev)
162     RESOLVE_DEVICE (buf, , { free (buf); return -1; });
163
164   r = command (NULL, &err, "umount", buf, NULL);
165   free (buf);
166
167   if (r == -1) {
168     reply_with_error ("%s: %s", pathordevice, err);
169     free (err);
170     return -1;
171   }
172
173   free (err);
174
175   return 0;
176 }
177
178 /* Implement 'mounts' (mp==0) and 'mountpoints' (mp==1) calls. */
179 static char **
180 mounts_or_mountpoints (int mp)
181 {
182   FILE *fp;
183   struct mntent *m;
184   char **ret = NULL;
185   int size = 0, alloc = 0;
186   size_t i;
187   int r;
188
189   /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
190    * that requires custom parsing code.
191    */
192   fp = setmntent ("/proc/mounts", "r");
193   if (fp == NULL) {
194     perror ("/proc/mounts");
195     exit (EXIT_FAILURE);
196   }
197
198   while ((m = getmntent (fp)) != NULL) {
199     /* Allow a mount directory like "/sysroot". */
200     if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
201       if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1) {
202       error:
203         endmntent (fp);
204         return NULL;
205       }
206       if (mp &&
207           add_string (&ret, &size, &alloc, "/") == -1)
208         goto error;
209     }
210     /* Or allow a mount directory like "/sysroot/...". */
211     if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') {
212       if (add_string (&ret, &size, &alloc, m->mnt_fsname) == -1)
213         goto error;
214       if (mp &&
215           add_string (&ret, &size, &alloc, &m->mnt_dir[sysroot_len]) == -1)
216         goto error;
217     }
218   }
219
220   endmntent (fp);
221
222   if (add_string (&ret, &size, &alloc, NULL) == -1)
223     return NULL;
224
225   /* Convert /dev/mapper LV paths into canonical paths (RHBZ#646432). */
226   for (i = 0; ret[i] != NULL; i += mp ? 2 : 1) {
227     if (STRPREFIX (ret[i], "/dev/mapper/") || STRPREFIX (ret[i], "/dev/dm-")) {
228       char *canonical;
229       r = lv_canonical (ret[i], &canonical);
230       if (r == -1) {
231         free_strings (ret);
232         return NULL;
233       }
234       if (r == 1) {
235         free (ret[i]);
236         ret[i] = canonical;
237       }
238       /* Ignore the case where r == 0.  This might happen where
239        * eg. a LUKS /dev/mapper device is mounted, but that won't
240        * correspond to any LV.
241        */
242     }
243   }
244
245   return ret;
246 }
247
248 char **
249 do_mounts (void)
250 {
251   return mounts_or_mountpoints (0);
252 }
253
254 char **
255 do_mountpoints (void)
256 {
257   return mounts_or_mountpoints (1);
258 }
259
260 /* Unmount everything mounted under /sysroot.
261  *
262  * We have to unmount in the correct order, so we sort the paths by
263  * longest first to ensure that child paths are unmounted by parent
264  * paths.
265  *
266  * This call is more important than it appears at first, because it
267  * is widely used by both test and production code in order to
268  * get back to a known state (nothing mounted, everything synchronized).
269  */
270 static int
271 compare_longest_first (const void *vp1, const void *vp2)
272 {
273   char * const *p1 = (char * const *) vp1;
274   char * const *p2 = (char * const *) vp2;
275   int n1 = strlen (*p1);
276   int n2 = strlen (*p2);
277   return n2 - n1;
278 }
279
280 int
281 do_umount_all (void)
282 {
283   FILE *fp;
284   struct mntent *m;
285   char **mounts = NULL;
286   int size = 0, alloc = 0;
287   char *err;
288   int i, r;
289
290   /* NB: Eventually we should aim to parse /proc/self/mountinfo, but
291    * that requires custom parsing code.
292    */
293   fp = setmntent ("/proc/mounts", "r");
294   if (fp == NULL) {
295     perror ("/proc/mounts");
296     exit (EXIT_FAILURE);
297   }
298
299   while ((m = getmntent (fp)) != NULL) {
300     /* Allow a mount directory like "/sysroot". */
301     if (sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) {
302       if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
303         endmntent (fp);
304         return -1;
305       }
306     }
307     /* Or allow a mount directory like "/sysroot/...". */
308     if (STRPREFIX (m->mnt_dir, sysroot) && m->mnt_dir[sysroot_len] == '/') {
309       if (add_string (&mounts, &size, &alloc, m->mnt_dir) == -1) {
310         endmntent (fp);
311         return -1;
312       }
313     }
314   }
315
316   endmntent (fp);
317
318   qsort (mounts, size, sizeof (char *), compare_longest_first);
319
320   /* Unmount them. */
321   for (i = 0; i < size; ++i) {
322     r = command (NULL, &err, "umount", mounts[i], NULL);
323     if (r == -1) {
324       reply_with_error ("umount: %s: %s", mounts[i], err);
325       free (err);
326       free_stringslen (mounts, size);
327       return -1;
328     }
329     free (err);
330   }
331
332   free_stringslen (mounts, size);
333
334   return 0;
335 }
336
337 /* Mount using the loopback device.  You can't use the generic
338  * do_mount call for this because the first parameter isn't a
339  * device.
340  */
341 int
342 do_mount_loop (const char *file, const char *mountpoint)
343 {
344   int r;
345   char *buf, *mp;
346   char *error;
347
348   /* We have to prefix /sysroot on both the filename and the mountpoint. */
349   mp = sysroot_path (mountpoint);
350   if (!mp) {
351     reply_with_perror ("malloc");
352     return -1;
353   }
354
355   buf = sysroot_path (file);
356   if (!buf) {
357     reply_with_perror ("malloc");
358     free (mp);
359     return -1;
360   }
361
362   r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
363   free (mp);
364   free (buf);
365   if (r == -1) {
366     reply_with_error ("%s on %s: %s", file, mountpoint, error);
367     free (error);
368     return -1;
369   }
370
371   free (error);
372   return 0;
373 }
374
375 /* Specialized calls mkmountpoint and rmmountpoint are really
376  * variations on mkdir and rmdir which do no checking of the
377  * is_root_mounted() flag.
378  */
379 int
380 do_mkmountpoint (const char *path)
381 {
382   int r;
383
384   /* NEED_ROOT (return -1); - we don't want this test for this call. */
385   ABS_PATH (path, , return -1);
386
387   CHROOT_IN;
388   r = mkdir (path, 0777);
389   CHROOT_OUT;
390
391   if (r == -1) {
392     reply_with_perror ("%s", path);
393     return -1;
394   }
395
396   return 0;
397 }
398
399 int
400 do_rmmountpoint (const char *path)
401 {
402   int r;
403
404   /* NEED_ROOT (return -1); - we don't want this test for this call. */
405   ABS_PATH (path, , return -1);
406
407   CHROOT_IN;
408   r = rmdir (path);
409   CHROOT_OUT;
410
411   if (r == -1) {
412     reply_with_perror ("%s", path);
413     return -1;
414   }
415
416   return 0;
417 }