chiark / gitweb /
bd4f01ff64ae553d5cbdef1c77ff0cbf411e3ad6
[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 /* Takes over possession of path */
42 static MountPoint *mount_point_alloc(char *path) {
43         MountPoint *mp;
44
45         if (!(mp = new(MountPoint, 1)))
46                 return NULL;
47
48         mp->path = path;
49         return mp;
50 }
51
52 static void mount_point_remove_and_free(MountPoint *mount_point, MountPoint **mount_point_list_head) {
53         LIST_REMOVE(MountPoint, mount_point, *mount_point_list_head, mount_point);
54
55         free(mount_point->path);
56         free(mount_point);
57 }
58
59 static void mount_points_list_free(MountPoint **mount_point_list_head) {
60         while (*mount_point_list_head)
61                 mount_point_remove_and_free(*mount_point_list_head, mount_point_list_head);
62 }
63
64 static int mount_points_list_get(MountPoint **mount_point_list_head) {
65         FILE *proc_self_mountinfo;
66         char *path, *p;
67         unsigned int i;
68         int r;
69
70         if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
71                 return -errno;
72
73         for (i = 1;; i++) {
74                 int k;
75                 MountPoint *mp;
76
77                 path = p = NULL;
78
79                 if ((k = fscanf(proc_self_mountinfo,
80                                 "%*s "       /* (1) mount id */
81                                 "%*s "       /* (2) parent id */
82                                 "%*s "       /* (3) major:minor */
83                                 "%*s "       /* (4) root */
84                                 "%ms "       /* (5) mount point */
85                                 "%*s"        /* (6) mount options */
86                                 "%*[^-]"     /* (7) optional fields */
87                                 "- "         /* (8) separator */
88                                 "%*s "       /* (9) file system type */
89                                 "%*s"        /* (10) mount source */
90                                 "%*s"        /* (11) mount options 2 */
91                                 "%*[^\n]",   /* some rubbish at the end */
92                                 &path)) != 1) {
93                         if (k == EOF)
94                                 break;
95
96                         log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
97
98                         free(path);
99                         continue;
100                 }
101
102                 p = cunescape(path);
103                 free(path);
104
105                 if (!p) {
106                         r = -ENOMEM;
107                         goto finish;
108                 }
109
110                 if (mount_point_is_api(p)) {
111                         free(p);
112                         continue;
113                 }
114
115                 if (!(mp = mount_point_alloc(p))) {
116                         free(p);
117                         r = -ENOMEM;
118                         goto finish;
119                 }
120
121                 LIST_PREPEND(MountPoint, mount_point, *mount_point_list_head, mp);
122         }
123
124         r = 0;
125
126 finish:
127         fclose(proc_self_mountinfo);
128
129         return r;
130 }
131
132 static int swap_list_get(MountPoint **swap_list_head) {
133         FILE *proc_swaps;
134         unsigned int i;
135         int r;
136
137         if (!(proc_swaps = fopen("/proc/swaps", "re")))
138                 return -errno;
139
140         (void) fscanf(proc_swaps, "%*s %*s %*s %*s %*s\n");
141
142         for (i = 2;; i++) {
143                 MountPoint *swap;
144                 char *dev = NULL, *d;
145                 int k;
146
147                 if ((k = fscanf(proc_swaps,
148                                 "%ms " /* device/file */
149                                 "%*s " /* type of swap */
150                                 "%*s " /* swap size */
151                                 "%*s " /* used */
152                                 "%*s\n", /* priority */
153                                 &dev)) != 1) {
154
155                         if (k == EOF)
156                                 break;
157
158                         log_warning("Failed to parse /proc/swaps:%u.", i);
159
160                         free(dev);
161                         continue;
162                 }
163
164                 if (endswith(dev, "(deleted)")) {
165                         free(dev);
166                         continue;
167                 }
168
169                 d = cunescape(dev);
170                 free(dev);
171
172                 if (!d) {
173                         r = -ENOMEM;
174                         goto finish;
175                 }
176
177                 swap = mount_point_alloc(d);
178                 if (!swap) {
179                         free(d);
180                         r = -ENOMEM;
181                         goto finish;
182                 }
183
184                 LIST_PREPEND(MountPoint, mount_point, *swap_list_head, swap);
185         }
186
187         r = 0;
188
189 finish:
190         fclose(proc_swaps);
191
192         return r;
193 }
194
195 static int loopback_list_get(MountPoint **loopback_list_head) {
196         int r;
197         struct udev *udev;
198         struct udev_enumerate *e = NULL;
199         struct udev_list_entry *item = NULL, *first = NULL;
200
201         if (!(udev = udev_new())) {
202                 r = -ENOMEM;
203                 goto finish;
204         }
205
206         if (!(e = udev_enumerate_new(udev))) {
207                 r = -ENOMEM;
208                 goto finish;
209         }
210
211         if (udev_enumerate_add_match_subsystem(e, "block") < 0) {
212                 r = -EIO;
213                 goto finish;
214         }
215
216         if (udev_enumerate_add_match_sysname(e, "loop*") < 0) {
217                 r = -EIO;
218                 goto finish;
219         }
220
221         if (udev_enumerate_scan_devices(e) < 0) {
222                 r = -EIO;
223                 goto finish;
224         }
225
226         first = udev_enumerate_get_list_entry(e);
227
228         udev_list_entry_foreach(item, first) {
229                 MountPoint *lb;
230                 struct udev_device *d;
231                 char *loop;
232                 const char *dn;
233
234                 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
235                         r = -ENOMEM;
236                         goto finish;
237                 }
238
239                 if ((dn = udev_device_get_devnode(d))) {
240                         loop = strdup(dn);
241                         udev_device_unref(d);
242
243                         if (!loop) {
244                                 r = -ENOMEM;
245                                 goto finish;
246                         }
247                 } else
248                         udev_device_unref(d);
249
250                 if (!(lb = mount_point_alloc(loop))) {
251                         free(loop);
252                         r = -ENOMEM;
253                         goto finish;
254                 }
255
256                 LIST_PREPEND(MountPoint, mount_point, *loopback_list_head, lb);
257         }
258
259         r = 0;
260
261 finish:
262         if (e)
263                 udev_enumerate_unref(e);
264
265         if (udev)
266                 udev_unref(udev);
267
268         return r;
269 }
270
271 static int delete_loopback(const char *device) {
272         int fd, r;
273
274         if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0) {
275                 if (errno == ENOENT) {
276                         log_debug("Loop device %s does not exist.", device);
277                         errno = 0;
278                         return 0;
279                 }
280                 return -errno;
281         }
282
283         ioctl(fd, LOOP_CLR_FD, 0);
284         r = errno;
285         close_nointr_nofail(fd);
286
287         if (r == ENXIO) /* not bound, so no error */
288                 r = 0;
289         errno = r;
290         return -errno;
291 }
292
293 static int mount_points_list_umount(MountPoint **mount_point_list_head) {
294         MountPoint *mp, *mp_next;
295         int failed = 0;
296
297         LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
298                 if (streq(mp->path, "/"))
299                         continue;
300
301                 /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
302                 if (umount2(mp->path, MNT_FORCE) == 0)
303                         mount_point_remove_and_free(mp, mount_point_list_head);
304                 else {
305                         log_debug("Could not unmount %s: %m", mp->path);
306                         failed++;
307                 }
308         }
309
310         return failed;
311 }
312
313 static int mount_points_list_remount_read_only(MountPoint **mount_point_list_head) {
314         MountPoint *mp, *mp_next;
315         int failed = 0;
316
317         LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
318                 /* Trying to remount read-only */
319                 if (mount(NULL, mp->path, NULL, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0)
320                         mount_point_remove_and_free(mp, mount_point_list_head);
321                 else {
322                         log_debug("Could not remount as read-only %s: %m", mp->path);
323                         failed++;
324                 }
325         }
326
327         return failed;
328 }
329
330 static int swap_points_list_off(MountPoint **swap_list_head) {
331         MountPoint *swap, *swap_next;
332         int failed = 0;
333
334         LIST_FOREACH_SAFE(mount_point, swap, swap_next, *swap_list_head) {
335                 if (swapoff(swap->path) == 0)
336                         mount_point_remove_and_free(swap, swap_list_head);
337                 else {
338                         log_debug("Could not swapoff %s: %m", swap->path);
339                         failed++;
340                 }
341         }
342
343         return failed;
344 }
345
346 static int loopback_points_list_detach(MountPoint **loopback_list_head) {
347         MountPoint *loopback, *loopback_next;
348         int failed = 0;
349
350         LIST_FOREACH_SAFE(mount_point, loopback, loopback_next, *loopback_list_head) {
351                 if (delete_loopback(loopback->path) == 0)
352                         mount_point_remove_and_free(loopback, loopback_list_head);
353                 else {
354                         log_debug("Could not delete loopback %s: %m", loopback->path);
355                         failed++;
356                 }
357         }
358
359         return failed;
360 }
361
362 int umount_all(void) {
363         int r;
364         LIST_HEAD(MountPoint, mp_list_head);
365
366         LIST_HEAD_INIT(MountPoint, mp_list_head);
367
368         r = mount_points_list_get(&mp_list_head);
369         if (r < 0)
370                 goto end;
371
372         r = mount_points_list_umount(&mp_list_head);
373         if (r <= 0)
374                 goto end;
375
376         r = mount_points_list_remount_read_only(&mp_list_head);
377
378   end:
379         mount_points_list_free(&mp_list_head);
380
381         return r;
382 }
383
384 int swapoff_all(void) {
385         int r;
386         LIST_HEAD(MountPoint, swap_list_head);
387
388         LIST_HEAD_INIT(MountPoint, swap_list_head);
389
390         r = swap_list_get(&swap_list_head);
391         if (r < 0)
392                 goto end;
393
394         r = swap_points_list_off(&swap_list_head);
395
396   end:
397         mount_points_list_free(&swap_list_head);
398
399         return r;
400 }
401
402 int loopback_detach_all(void) {
403         int r;
404         LIST_HEAD(MountPoint, loopback_list_head);
405
406         LIST_HEAD_INIT(MountPoint, loopback_list_head);
407
408         r = loopback_list_get(&loopback_list_head);
409         if (r < 0)
410                 goto end;
411
412         r = loopback_points_list_detach(&loopback_list_head);
413
414   end:
415         mount_points_list_free(&loopback_list_head);
416
417         return r;
418 }