chiark / gitweb /
468eb715d65bb142bc81cbb3fae07dfb8247fa0e
[elogind.git] / src / umount.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 ProFUSION embedded systems
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <sys/mount.h>
26 #include <sys/swap.h>
27 #include <unistd.h>
28 #include <linux/loop.h>
29 #include <libudev.h>
30
31 #include "list.h"
32 #include "mount-setup.h"
33 #include "umount.h"
34 #include "util.h"
35
36 typedef struct MountPoint {
37         char *path;
38         LIST_FIELDS (struct MountPoint, mount_point);
39 } MountPoint;
40
41 static MountPoint *mount_point_alloc(char *path) {
42         MountPoint *mp;
43
44         if (!(mp = new(MountPoint, 1)))
45                 return NULL;
46
47         mp->path = path;
48         return mp;
49 }
50
51 static void mount_point_remove_and_free(MountPoint *mount_point, MountPoint **mount_point_list_head) {
52         LIST_REMOVE(MountPoint, mount_point, *mount_point_list_head, mount_point);
53
54         free(mount_point->path);
55         free(mount_point);
56 }
57
58 static void mount_points_list_free(MountPoint **mount_point_list_head) {
59         while (*mount_point_list_head)
60                 mount_point_remove_and_free(*mount_point_list_head, mount_point_list_head);
61 }
62
63 static int mount_points_list_get(MountPoint **mount_point_list_head) {
64         FILE *proc_self_mountinfo;
65         char *path, *p;
66         unsigned int i;
67         int r;
68
69         if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
70                 return -errno;
71
72         for (i = 1;; i++) {
73                 int k;
74                 MountPoint *mp;
75
76                 path = p = NULL;
77
78                 if ((k = fscanf(proc_self_mountinfo,
79                                 "%*s "       /* (1) mount id */
80                                 "%*s "       /* (2) parent id */
81                                 "%*s "       /* (3) major:minor */
82                                 "%*s "       /* (4) root */
83                                 "%ms "       /* (5) mount point */
84                                 "%*s"        /* (6) mount options */
85                                 "%*[^-]"     /* (7) optional fields */
86                                 "- "         /* (8) separator */
87                                 "%*s "       /* (9) file system type */
88                                 "%*s"        /* (10) mount source */
89                                 "%*s"        /* (11) mount options 2 */
90                                 "%*[^\n]",   /* some rubbish at the end */
91                                 &path)) != 1) {
92                         if (k == EOF)
93                                 break;
94
95                         log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
96
97                         free(path);
98                         continue;
99                 }
100
101                 if (mount_point_is_api(path)) {
102                         free(path);
103                         continue;
104                 }
105
106                 if (!(p = cunescape(path))) {
107                         r = -ENOMEM;
108                         goto finish;
109                 }
110
111                 if (!(mp = mount_point_alloc(p))) {
112                         r = -ENOMEM;
113                         goto finish;
114                 }
115                 LIST_PREPEND(MountPoint, mount_point, *mount_point_list_head, mp);
116
117                 free(path);
118         }
119
120         r = 0;
121
122 finish:
123         fclose(proc_self_mountinfo);
124
125         free(path);
126
127         return r;
128 }
129
130 static int swap_list_get(MountPoint **swap_list_head) {
131         FILE *proc_swaps;
132         unsigned int i;
133         int r;
134
135         if (!(proc_swaps = fopen("/proc/swaps", "re")))
136                 return -errno;
137
138         (void) fscanf(proc_swaps, "%*s %*s %*s %*s %*s\n");
139
140         for (i = 2;; i++) {
141                 MountPoint *swap;
142                 char *dev = NULL, *d;
143                 int k;
144
145                 if ((k = fscanf(proc_swaps,
146                                 "%ms " /* device/file */
147                                 "%*s " /* type of swap */
148                                 "%*s " /* swap size */
149                                 "%*s " /* used */
150                                 "%*s\n", /* priority */
151                                 &dev)) != 1) {
152
153                         if (k == EOF)
154                                 break;
155
156                         log_warning("Failed to parse /proc/swaps:%u.", i);
157
158                         free(dev);
159                         continue;
160                 }
161
162                 if (endswith(dev, "(deleted)")) {
163                         free(dev);
164                         continue;
165                 }
166
167                 d = cunescape(dev);
168                 free(dev);
169
170                 if (!d) {
171                         r = -ENOMEM;
172                         goto finish;
173                 }
174
175                 swap = mount_point_alloc(d);
176                 if (!swap) {
177                         free(d);
178                         r = -ENOMEM;
179                         goto finish;
180                 }
181
182                 LIST_PREPEND(MountPoint, mount_point, *swap_list_head, swap);
183         }
184
185         r = 0;
186
187 finish:
188         fclose(proc_swaps);
189
190         return r;
191 }
192
193 static int loopback_list_get(MountPoint **loopback_list_head) {
194         int r;
195         struct udev *udev;
196         struct udev_enumerate *e = NULL;
197         struct udev_list_entry *item = NULL, *first = NULL;
198
199         if (!(udev = udev_new())) {
200                 r = -ENOMEM;
201                 goto finish;
202         }
203
204         if (!(e = udev_enumerate_new(udev))) {
205                 r = -ENOMEM;
206                 goto finish;
207         }
208
209         if (udev_enumerate_add_match_subsystem(e, "block") < 0) {
210                 r = -EIO;
211                 goto finish;
212         }
213
214         if (udev_enumerate_add_match_sysname(e, "loop*") < 0) {
215                 r = -EIO;
216                 goto finish;
217         }
218
219         if (udev_enumerate_scan_devices(e) < 0) {
220                 r = -EIO;
221                 goto finish;
222         }
223
224         first = udev_enumerate_get_list_entry(e);
225
226         udev_list_entry_foreach(item, first) {
227                 MountPoint *lb;
228                 char *loop;
229
230                 loop = cunescape(udev_list_entry_get_name(item));
231                 if (!loop) {
232                         r = -ENOMEM;
233                         goto finish;
234                 }
235
236                 lb = mount_point_alloc(loop);
237                 if (!lb) {
238                         free(loop);
239                         r = -ENOMEM;
240                         goto finish;
241                 }
242
243                 LIST_PREPEND(MountPoint, mount_point, *loopback_list_head, lb);
244         }
245
246         r = 0;
247
248 finish:
249         if (e)
250                 udev_enumerate_unref(e);
251
252         free(udev);
253         return r;
254 }
255
256 static int delete_loopback(const char *device) {
257         int fd, r;
258
259         if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0) {
260                 if (errno == ENOENT) {
261                         log_debug("Loop device %s does not exist.", device);
262                         errno = 0;
263                         return 0;
264                 }
265                 return -errno;
266         }
267
268         ioctl(fd, LOOP_CLR_FD, 0);
269         r = errno;
270         close_nointr_nofail(fd);
271
272         if (r == ENXIO) /* not bound, so no error */
273                 r = 0;
274         errno = r;
275         return -errno;
276 }
277
278 static int mount_points_list_umount(MountPoint **mount_point_list_head) {
279         MountPoint *mp, *mp_next;
280         int failed = 0;
281
282         LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
283                 if (streq(mp->path, "/"))
284                         continue;
285
286                 /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
287                 if (umount2(mp->path, MNT_FORCE) == 0)
288                         mount_point_remove_and_free(mp, mount_point_list_head);
289                 else {
290                         log_debug("Could not unmount %s: %m", mp->path);
291                         failed++;
292                 }
293         }
294
295         return failed;
296 }
297
298 static int mount_points_list_remount_read_only(MountPoint **mount_point_list_head) {
299         MountPoint *mp, *mp_next;
300         int failed = 0;
301
302         LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
303                 /* Trying to remount read-only */
304                 if (mount(NULL, mp->path, NULL, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0)
305                         mount_point_remove_and_free(mp, mount_point_list_head);
306                 else {
307                         log_debug("Could not remount as read-only %s: %m", mp->path);
308                         failed++;
309                 }
310         }
311
312         return failed;
313 }
314
315 static int swap_points_list_off(MountPoint **swap_list_head) {
316         MountPoint *swap, *swap_next;
317         int failed = 0;
318
319         LIST_FOREACH_SAFE(mount_point, swap, swap_next, *swap_list_head) {
320                 if (swapoff(swap->path) == 0)
321                         mount_point_remove_and_free(swap, swap_list_head);
322                 else {
323                         log_debug("Could not swapoff %s: %m", swap->path);
324                         failed++;
325                 }
326         }
327
328         return failed;
329 }
330
331 static int loopback_points_list_detach(MountPoint **loopback_list_head) {
332         MountPoint *loopback, *loopback_next;
333         int failed = 0;
334
335         LIST_FOREACH_SAFE(mount_point, loopback, loopback_next, *loopback_list_head) {
336                 if (delete_loopback(loopback->path) == 0)
337                         mount_point_remove_and_free(loopback, loopback_list_head);
338                 else {
339                         log_debug("Could not delete loopback %s: %m", loopback->path);
340                         failed++;
341                 }
342         }
343
344         return failed;
345 }
346
347 int umount_all(void) {
348         int r;
349         LIST_HEAD(MountPoint, mp_list_head);
350
351         LIST_HEAD_INIT(MountPoint, mp_list_head);
352
353         r = mount_points_list_get(&mp_list_head);
354         if (r < 0)
355                 goto end;
356
357         r = mount_points_list_umount(&mp_list_head);
358         if (r <= 0)
359                 goto end;
360
361         r = mount_points_list_remount_read_only(&mp_list_head);
362
363   end:
364         mount_points_list_free(&mp_list_head);
365
366         return r;
367 }
368
369 int swapoff_all(void) {
370         int r;
371         LIST_HEAD(MountPoint, swap_list_head);
372
373         LIST_HEAD_INIT(MountPoint, swap_list_head);
374
375         r = swap_list_get(&swap_list_head);
376         if (r < 0)
377                 goto end;
378
379         r = swap_points_list_off(&swap_list_head);
380
381   end:
382         mount_points_list_free(&swap_list_head);
383
384         return r;
385 }
386
387 int loopback_detach_all(void) {
388         int r;
389         LIST_HEAD(MountPoint, loopback_list_head);
390
391         LIST_HEAD_INIT(MountPoint, loopback_list_head);
392
393         r = loopback_list_get(&loopback_list_head);
394         if (r < 0)
395                 goto end;
396
397         r = loopback_points_list_detach(&loopback_list_head);
398
399   end:
400         mount_points_list_free(&loopback_list_head);
401
402         return r;
403 }