Coverity: Close directory handle along error paths.
[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., 675 Mass Ave, Cambridge, MA 02139, 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
28 #include "daemon.h"
29 #include "actions.h"
30
31 /* You must mount something on "/" first, hence: */
32 int root_mounted = 0;
33
34 /* The "simple mount" call offers no complex options, you can just
35  * mount a device on a mountpoint.  The variations like mount_ro,
36  * mount_options and mount_vfs let you set progressively more things.
37  *
38  * It's tempting to try a direct mount(2) syscall, but that doesn't
39  * do any autodetection, so we are better off calling out to
40  * /bin/mount.
41  */
42
43 int
44 do_mount_vfs (const char *options, const char *vfstype,
45               const char *device, const char *mountpoint)
46 {
47   int r, is_root;
48   char *mp;
49   char *error;
50
51   ABS_PATH (mountpoint, , return -1);
52
53   is_root = STREQ (mountpoint, "/");
54
55   if (!root_mounted && !is_root) {
56     reply_with_error ("you must mount something on / first");
57     return -1;
58   }
59
60   mp = sysroot_path (mountpoint);
61   if (!mp) {
62     reply_with_perror ("malloc");
63     return -1;
64   }
65
66   if (vfstype)
67     r = command (NULL, &error,
68                  "mount", "-o", options, "-t", vfstype, device, mp, NULL);
69   else
70     r = command (NULL, &error,
71                  "mount", "-o", options, device, mp, NULL);
72   free (mp);
73   if (r == -1) {
74     reply_with_error ("%s on %s: %s", device, mountpoint, error);
75     free (error);
76     return -1;
77   }
78
79   if (is_root)
80     root_mounted = 1;
81
82   free (error);
83   return 0;
84 }
85
86 int
87 do_mount (const char *device, const char *mountpoint)
88 {
89   return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
90 }
91
92 int
93 do_mount_ro (const char *device, const char *mountpoint)
94 {
95   return do_mount_vfs ("ro", NULL, device, mountpoint);
96 }
97
98 int
99 do_mount_options (const char *options, const char *device,
100                   const char *mountpoint)
101 {
102   return do_mount_vfs (options, NULL, device, mountpoint);
103 }
104
105 /* Again, use the external /bin/umount program, so that /etc/mtab
106  * is kept updated.
107  */
108 int
109 do_umount (const char *pathordevice)
110 {
111   int r;
112   char *err;
113   char *buf;
114   int is_dev;
115
116   is_dev = STREQLEN (pathordevice, "/dev/", 5);
117   buf = is_dev ? strdup (pathordevice)
118                : sysroot_path (pathordevice);
119   if (buf == NULL) {
120     reply_with_perror ("malloc");
121     return -1;
122   }
123
124   if (is_dev)
125     RESOLVE_DEVICE (buf, , { free (buf); return -1; });
126
127   r = command (NULL, &err, "umount", buf, NULL);
128   free (buf);
129
130   if (r == -1) {
131     reply_with_error ("%s: %s", pathordevice, err);
132     free (err);
133     return -1;
134   }
135
136   free (err);
137
138   /* update root_mounted? */
139
140   return 0;
141 }
142
143 static char **
144 mounts_or_mountpoints (int mp)
145 {
146   char *out, *err;
147   int r;
148   char **ret = NULL;
149   int size = 0, alloc = 0;
150   char *p, *pend, *p2;
151   int len;
152   char matching[5 + sysroot_len];
153   size_t i;
154
155   r = command (&out, &err, "mount", NULL);
156   if (r == -1) {
157     reply_with_error ("mount: %s", err);
158     free (out);
159     free (err);
160     return NULL;
161   }
162
163   free (err);
164
165   /* Lines have the format:
166    *   /dev/foo on /mountpoint type ...
167    */
168   snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
169
170   p = out;
171   while (p) {
172     pend = strchr (p, '\n');
173     if (pend) {
174       *pend = '\0';
175       pend++;
176     }
177
178     p2 = strstr (p, matching);
179     if (p2 != NULL) {
180       *p2 = '\0';
181       if (add_string (&ret, &size, &alloc, p) == -1) {
182         free (out);
183         return NULL;
184       }
185       if (mp) {
186         p2 += 4 + sysroot_len;  /* skip " on /sysroot" */
187         len = strcspn (p2, " ");
188
189         if (len == 0)           /* .. just /sysroot, so we turn it into "/" */
190           p2 = (char *) "/";
191         else
192           p2[len] = '\0';
193
194         if (add_string (&ret, &size, &alloc, p2) == -1) {
195           free (out);
196           return NULL;
197         }
198       }
199     }
200
201     p = pend;
202   }
203
204   free (out);
205
206   if (add_string (&ret, &size, &alloc, NULL) == -1)
207     return NULL;
208
209   /* Convert /dev/mapper LV paths into canonical paths (RHBZ#646432). */
210   for (i = 0; ret[i] != NULL; i += mp ? 2 : 1) {
211     if (STRPREFIX (ret[i], "/dev/mapper/") || STRPREFIX (ret[i], "/dev/dm-")) {
212       char *canonical;
213       r = lv_canonical (ret[i], &canonical);
214       if (r == -1) {
215         free_strings (ret);
216         return NULL;
217       }
218       if (r == 1) {
219         free (ret[i]);
220         ret[i] = canonical;
221       }
222       /* Ignore the case where r == 0.  This might happen where
223        * eg. a LUKS /dev/mapper device is mounted, but that won't
224        * correspond to any LV.
225        */
226     }
227   }
228
229   return ret;
230 }
231
232 char **
233 do_mounts (void)
234 {
235   return mounts_or_mountpoints (0);
236 }
237
238 char **
239 do_mountpoints (void)
240 {
241   return mounts_or_mountpoints (1);
242 }
243
244 /* Unmount everything mounted under /sysroot.
245  *
246  * We have to unmount in the correct order, so we sort the paths by
247  * longest first to ensure that child paths are unmounted by parent
248  * paths.
249  *
250  * This call is more important than it appears at first, because it
251  * is widely used by both test and production code in order to
252  * get back to a known state (nothing mounted, everything synchronized).
253  */
254 static int
255 compare_longest_first (const void *vp1, const void *vp2)
256 {
257   char * const *p1 = (char * const *) vp1;
258   char * const *p2 = (char * const *) vp2;
259   int n1 = strlen (*p1);
260   int n2 = strlen (*p2);
261   return n2 - n1;
262 }
263
264 int
265 do_umount_all (void)
266 {
267   char *out, *err;
268   int i, r;
269   char **mounts = NULL;
270   int size = 0, alloc = 0;
271   char *p, *p2, *p3, *pend;
272   char matching[5 + sysroot_len];
273
274   r = command (&out, &err, "mount", NULL);
275   if (r == -1) {
276     reply_with_error ("mount: %s", err);
277     free (out);
278     free (err);
279     return -1;
280   }
281
282   free (err);
283
284   /* Lines have the format:
285    *   /dev/foo on /mountpoint type ...
286    */
287   snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
288
289   p = out;
290   while (p) {
291     pend = strchr (p, '\n');
292     if (pend) {
293       *pend = '\0';
294       pend++;
295     }
296
297     p2 = strstr (p, matching);
298     if (p2 != NULL) {
299       p2 += 4;
300       p3 = p2 + strcspn (p2, " ");
301       *p3 = '\0';
302       if (add_string (&mounts, &size, &alloc, p2) == -1) {
303         free (out);
304         return -1;
305       }
306     }
307
308     p = pend;
309   }
310   free (out);
311
312   qsort (mounts, size, sizeof (char *), compare_longest_first);
313
314   /* Unmount them. */
315   for (i = 0; i < size; ++i) {
316     r = command (NULL, &err, "umount", mounts[i], NULL);
317     if (r == -1) {
318       reply_with_error ("umount: %s: %s", mounts[i], err);
319       free (err);
320       free_stringslen (mounts, size);
321       return -1;
322     }
323     free (err);
324   }
325
326   free_stringslen (mounts, size);
327
328   /* We've unmounted root now, so ... */
329   root_mounted = 0;
330
331   return 0;
332 }
333
334 /* Mount using the loopback device.  You can't use the generic
335  * do_mount call for this because the first parameter isn't a
336  * device.
337  */
338 int
339 do_mount_loop (const char *file, const char *mountpoint)
340 {
341   int r;
342   char *buf, *mp;
343   char *error;
344
345   /* We have to prefix /sysroot on both the filename and the mountpoint. */
346   mp = sysroot_path (mountpoint);
347   if (!mp) {
348     reply_with_perror ("malloc");
349     return -1;
350   }
351
352   buf = sysroot_path (file);
353   if (!buf) {
354     reply_with_perror ("malloc");
355     free (mp);
356     return -1;
357   }
358
359   r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
360   free (mp);
361   free (buf);
362   if (r == -1) {
363     reply_with_error ("%s on %s: %s", file, mountpoint, error);
364     free (error);
365     return -1;
366   }
367
368   free (error);
369   return 0;
370 }
371
372 /* Specialized calls mkmountpoint and rmmountpoint are really
373  * variations on mkdir and rmdir which do no checking and (in the
374  * mkmountpoint case) set the root_mounted flag.
375  */
376 int
377 do_mkmountpoint (const char *path)
378 {
379   int r;
380
381   /* NEED_ROOT (return -1); - we don't want this test for this call. */
382   ABS_PATH (path, , return -1);
383
384   CHROOT_IN;
385   r = mkdir (path, 0777);
386   CHROOT_OUT;
387
388   if (r == -1) {
389     reply_with_perror ("%s", path);
390     return -1;
391   }
392
393   /* Set the flag so that filesystems can be mounted here,
394    * not just on /sysroot.
395    */
396   root_mounted = 1;
397
398   return 0;
399 }
400
401 int
402 do_rmmountpoint (const char *path)
403 {
404   int r;
405
406   /* NEED_ROOT (return -1); - we don't want this test for this call. */
407   ABS_PATH (path, , return -1);
408
409   CHROOT_IN;
410   r = rmdir (path);
411   CHROOT_OUT;
412
413   if (r == -1) {
414     reply_with_perror ("%s", path);
415     return -1;
416   }
417
418   return 0;
419 }