mount: Check mountpoints are absolute 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 = strcmp (mountpoint, "/") == 0;
54
55   if (!root_mounted && !is_root) {
56     reply_with_error ("mount: 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 ("mount: %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   return 0;
83 }
84
85 int
86 do_mount (const char *device, const char *mountpoint)
87 {
88   return do_mount_vfs ("sync,noatime", NULL, device, mountpoint);
89 }
90
91 int
92 do_mount_ro (const char *device, const char *mountpoint)
93 {
94   return do_mount_vfs ("ro", NULL, device, mountpoint);
95 }
96
97 int
98 do_mount_options (const char *options, const char *device,
99                   const char *mountpoint)
100 {
101   return do_mount_vfs (options, NULL, device, mountpoint);
102 }
103
104 /* Again, use the external /bin/umount program, so that /etc/mtab
105  * is kept updated.
106  */
107 int
108 do_umount (const char *pathordevice)
109 {
110   int r;
111   char *err;
112
113   char *buf = (strncmp (pathordevice, "/dev/", 5) == 0
114                ? strdup (pathordevice)
115                : sysroot_path (pathordevice));
116   if (buf == NULL) {
117     reply_with_perror ("malloc");
118     return -1;
119   }
120
121   r = command (NULL, &err, "umount", buf, NULL);
122   free (buf);
123
124   if (r == -1) {
125     reply_with_error ("umount: %s: %s", pathordevice, err);
126     free (err);
127     return -1;
128   }
129
130   free (err);
131
132   /* update root_mounted? */
133
134   return 0;
135 }
136
137 static char **
138 mounts_or_mountpoints (int mp)
139 {
140   char *out, *err;
141   int r;
142   char **ret = NULL;
143   int size = 0, alloc = 0;
144   char *p, *pend, *p2;
145   int len;
146   char matching[5 + sysroot_len];
147
148   r = command (&out, &err, "mount", NULL);
149   if (r == -1) {
150     reply_with_error ("mount: %s", err);
151     free (out);
152     free (err);
153     return NULL;
154   }
155
156   free (err);
157
158   /* Lines have the format:
159    *   /dev/foo on /mountpoint type ...
160    */
161   snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
162
163   p = out;
164   while (p) {
165     pend = strchr (p, '\n');
166     if (pend) {
167       *pend = '\0';
168       pend++;
169     }
170
171     p2 = strstr (p, matching);
172     if (p2 != NULL) {
173       *p2 = '\0';
174       if (add_string (&ret, &size, &alloc, p) == -1) {
175         free (out);
176         return NULL;
177       }
178       if (mp) {
179         p2 += 4 + sysroot_len;  /* skip " on /sysroot" */
180         len = strcspn (p2, " ");
181
182         if (len == 0)           /* .. just /sysroot, so we turn it into "/" */
183           p2 = (char *) "/";
184         else
185           p2[len] = '\0';
186
187         if (add_string (&ret, &size, &alloc, p2) == -1) {
188           free (out);
189           return NULL;
190         }
191       }
192     }
193
194     p = pend;
195   }
196
197   free (out);
198
199   if (add_string (&ret, &size, &alloc, NULL) == -1)
200     return NULL;
201
202   return ret;
203 }
204
205 char **
206 do_mounts (void)
207 {
208   return mounts_or_mountpoints (0);
209 }
210
211 char **
212 do_mountpoints (void)
213 {
214   return mounts_or_mountpoints (1);
215 }
216
217 /* Unmount everything mounted under /sysroot.
218  *
219  * We have to unmount in the correct order, so we sort the paths by
220  * longest first to ensure that child paths are unmounted by parent
221  * paths.
222  *
223  * This call is more important than it appears at first, because it
224  * is widely used by both test and production code in order to
225  * get back to a known state (nothing mounted, everything synchronized).
226  */
227 static int
228 compare_longest_first (const void *vp1, const void *vp2)
229 {
230   char * const *p1 = (char * const *) vp1;
231   char * const *p2 = (char * const *) vp2;
232   int n1 = strlen (*p1);
233   int n2 = strlen (*p2);
234   return n2 - n1;
235 }
236
237 int
238 do_umount_all (void)
239 {
240   char *out, *err;
241   int i, r;
242   char **mounts = NULL;
243   int size = 0, alloc = 0;
244   char *p, *p2, *p3, *pend;
245   char matching[5 + sysroot_len];
246
247   r = command (&out, &err, "mount", NULL);
248   if (r == -1) {
249     reply_with_error ("mount: %s", err);
250     free (out);
251     free (err);
252     return -1;
253   }
254
255   free (err);
256
257   /* Lines have the format:
258    *   /dev/foo on /mountpoint type ...
259    */
260   snprintf (matching, 5 + sysroot_len, " on %s", sysroot);
261
262   p = out;
263   while (p) {
264     pend = strchr (p, '\n');
265     if (pend) {
266       *pend = '\0';
267       pend++;
268     }
269
270     p2 = strstr (p, matching);
271     if (p2 != NULL) {
272       p2 += 4;
273       p3 = p2 + strcspn (p2, " ");
274       *p3 = '\0';
275       if (add_string (&mounts, &size, &alloc, p2) == -1) {
276         free (out);
277         return -1;
278       }
279     }
280
281     p = pend;
282   }
283   free (out);
284
285   qsort (mounts, size, sizeof (char *), compare_longest_first);
286
287   /* Unmount them. */
288   for (i = 0; i < size; ++i) {
289     r = command (NULL, &err, "umount", mounts[i], NULL);
290     if (r == -1) {
291       reply_with_error ("umount: %s: %s", mounts[i], err);
292       free (err);
293       free_stringslen (mounts, size);
294       return -1;
295     }
296     free (err);
297   }
298
299   free_stringslen (mounts, size);
300
301   /* We've unmounted root now, so ... */
302   root_mounted = 0;
303
304   return 0;
305 }
306
307 /* Mount using the loopback device.  You can't use the generic
308  * do_mount call for this because the first parameter isn't a
309  * device.
310  */
311 int
312 do_mount_loop (const char *file, const char *mountpoint)
313 {
314   int r;
315   char *buf, *mp;
316   char *error;
317
318   /* We have to prefix /sysroot on both the filename and the mountpoint. */
319   mp = sysroot_path (mountpoint);
320   if (!mp) {
321     reply_with_perror ("malloc");
322     return -1;
323   }
324
325   buf = sysroot_path (file);
326   if (!file) {
327     reply_with_perror ("malloc");
328     free (mp);
329     return -1;
330   }
331
332   r = command (NULL, &error, "mount", "-o", "loop", buf, mp, NULL);
333   free (mp);
334   free (buf);
335   if (r == -1) {
336     reply_with_error ("mount: %s on %s: %s", file, mountpoint, error);
337     free (error);
338     return -1;
339   }
340
341   return 0;
342 }
343
344 /* Specialized calls mkmountpoint and rmmountpoint are really
345  * variations on mkdir and rmdir which do no checking and (in the
346  * mkmountpoint case) set the root_mounted flag.
347  */
348 int
349 do_mkmountpoint (const char *path)
350 {
351   int r;
352
353   /* NEED_ROOT (return -1); - we don't want this test for this call. */
354   ABS_PATH (path, return -1);
355
356   CHROOT_IN;
357   r = mkdir (path, 0777);
358   CHROOT_OUT;
359
360   if (r == -1) {
361     reply_with_perror ("mkmountpoint: %s", path);
362     return -1;
363   }
364
365   /* Set the flag so that filesystems can be mounted here,
366    * not just on /sysroot.
367    */
368   root_mounted = 1;
369
370   return 0;
371 }
372
373 int
374 do_rmmountpoint (const char *path)
375 {
376   int r;
377
378   /* NEED_ROOT (return -1); - we don't want this test for this call. */
379   ABS_PATH (path, return -1);
380
381   CHROOT_IN;
382   r = rmdir (path);
383   CHROOT_OUT;
384
385   if (r == -1) {
386     reply_with_perror ("rmmountpoint: %s", path);
387     return -1;
388   }
389
390   return 0;
391 }