chiark / gitweb /
shutdown: loop only as long as we manage to unmount/detach devices, give up immediate...
[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 <linux/dm-ioctl.h>
30 #include <libudev.h>
31
32 #include "list.h"
33 #include "mount-setup.h"
34 #include "umount.h"
35 #include "util.h"
36
37 typedef struct MountPoint {
38         char *path;
39         dev_t devnum;
40         LIST_FIELDS (struct MountPoint, mount_point);
41 } MountPoint;
42
43 static void mount_point_free(MountPoint **head, MountPoint *m) {
44         assert(head);
45         assert(m);
46
47         LIST_REMOVE(MountPoint, mount_point, *head, m);
48
49         free(m->path);
50         free(m);
51 }
52
53 static void mount_points_list_free(MountPoint **head) {
54         assert(head);
55
56         while (*head)
57                 mount_point_free(head, *head);
58 }
59
60 static int mount_points_list_get(MountPoint **head) {
61         FILE *proc_self_mountinfo;
62         char *path, *p;
63         unsigned int i;
64         int r;
65
66         assert(head);
67
68         if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
69                 return -errno;
70
71         for (i = 1;; i++) {
72                 int k;
73                 MountPoint *m;
74
75                 path = p = NULL;
76
77                 if ((k = fscanf(proc_self_mountinfo,
78                                 "%*s "       /* (1) mount id */
79                                 "%*s "       /* (2) parent id */
80                                 "%*s "       /* (3) major:minor */
81                                 "%*s "       /* (4) root */
82                                 "%ms "       /* (5) mount point */
83                                 "%*s"        /* (6) mount options */
84                                 "%*[^-]"     /* (7) optional fields */
85                                 "- "         /* (8) separator */
86                                 "%*s "       /* (9) file system type */
87                                 "%*s"        /* (10) mount source */
88                                 "%*s"        /* (11) mount options 2 */
89                                 "%*[^\n]",   /* some rubbish at the end */
90                                 &path)) != 1) {
91                         if (k == EOF)
92                                 break;
93
94                         log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
95
96                         free(path);
97                         continue;
98                 }
99
100                 p = cunescape(path);
101                 free(path);
102
103                 if (!p) {
104                         r = -ENOMEM;
105                         goto finish;
106                 }
107
108                 if (mount_point_is_api(p)) {
109                         free(p);
110                         continue;
111                 }
112
113                 if (!(m = new0(MountPoint, 1))) {
114                         free(p);
115                         r = -ENOMEM;
116                         goto finish;
117                 }
118
119                 m->path = p;
120                 LIST_PREPEND(MountPoint, mount_point, *head, m);
121         }
122
123         r = 0;
124
125 finish:
126         fclose(proc_self_mountinfo);
127
128         return r;
129 }
130
131 static int swap_list_get(MountPoint **head) {
132         FILE *proc_swaps;
133         unsigned int i;
134         int r;
135
136         assert(head);
137
138         if (!(proc_swaps = fopen("/proc/swaps", "re")))
139                 return -errno;
140
141         (void) fscanf(proc_swaps, "%*s %*s %*s %*s %*s\n");
142
143         for (i = 2;; i++) {
144                 MountPoint *swap;
145                 char *dev = NULL, *d;
146                 int k;
147
148                 if ((k = fscanf(proc_swaps,
149                                 "%ms " /* device/file */
150                                 "%*s " /* type of swap */
151                                 "%*s " /* swap size */
152                                 "%*s " /* used */
153                                 "%*s\n", /* priority */
154                                 &dev)) != 1) {
155
156                         if (k == EOF)
157                                 break;
158
159                         log_warning("Failed to parse /proc/swaps:%u.", i);
160
161                         free(dev);
162                         continue;
163                 }
164
165                 if (endswith(dev, "(deleted)")) {
166                         free(dev);
167                         continue;
168                 }
169
170                 d = cunescape(dev);
171                 free(dev);
172
173                 if (!d) {
174                         r = -ENOMEM;
175                         goto finish;
176                 }
177
178                 if (!(swap = new0(MountPoint, 1))) {
179                         free(d);
180                         r = -ENOMEM;
181                         goto finish;
182                 }
183
184                 swap->path = d;
185                 LIST_PREPEND(MountPoint, mount_point, *head, swap);
186         }
187
188         r = 0;
189
190 finish:
191         fclose(proc_swaps);
192
193         return r;
194 }
195
196 static int loopback_list_get(MountPoint **head) {
197         int r;
198         struct udev *udev;
199         struct udev_enumerate *e = NULL;
200         struct udev_list_entry *item = NULL, *first = NULL;
201
202         assert(head);
203
204         if (!(udev = udev_new())) {
205                 r = -ENOMEM;
206                 goto finish;
207         }
208
209         if (!(e = udev_enumerate_new(udev))) {
210                 r = -ENOMEM;
211                 goto finish;
212         }
213
214         if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
215             udev_enumerate_add_match_sysname(e, "loop*") < 0) {
216                 r = -EIO;
217                 goto finish;
218         }
219
220         if (udev_enumerate_scan_devices(e) < 0) {
221                 r = -EIO;
222                 goto finish;
223         }
224
225         first = udev_enumerate_get_list_entry(e);
226
227         udev_list_entry_foreach(item, first) {
228                 MountPoint *lb;
229                 struct udev_device *d;
230                 char *loop;
231                 const char *dn;
232
233                 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
234                         r = -ENOMEM;
235                         goto finish;
236                 }
237
238                 if (!(dn = udev_device_get_devnode(d))) {
239                         udev_device_unref(d);
240                         continue;
241                 }
242
243                 loop = strdup(dn);
244                 udev_device_unref(d);
245
246                 if (!loop) {
247                         r = -ENOMEM;
248                         goto finish;
249                 }
250
251                 if (!(lb = new0(MountPoint, 1))) {
252                         free(loop);
253                         r = -ENOMEM;
254                         goto finish;
255                 }
256
257                 lb->path = loop;
258                 LIST_PREPEND(MountPoint, mount_point, *head, lb);
259         }
260
261         r = 0;
262
263 finish:
264         if (e)
265                 udev_enumerate_unref(e);
266
267         if (udev)
268                 udev_unref(udev);
269
270         return r;
271 }
272
273 static int dm_list_get(MountPoint **head) {
274         int r;
275         struct udev *udev;
276         struct udev_enumerate *e = NULL;
277         struct udev_list_entry *item = NULL, *first = NULL;
278
279         assert(head);
280
281         if (!(udev = udev_new())) {
282                 r = -ENOMEM;
283                 goto finish;
284         }
285
286         if (!(e = udev_enumerate_new(udev))) {
287                 r = -ENOMEM;
288                 goto finish;
289         }
290
291         if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
292             udev_enumerate_add_match_sysname(e, "dm-*") < 0) {
293                 r = -EIO;
294                 goto finish;
295         }
296
297         if (udev_enumerate_scan_devices(e) < 0) {
298                 r = -EIO;
299                 goto finish;
300         }
301
302         first = udev_enumerate_get_list_entry(e);
303
304         udev_list_entry_foreach(item, first) {
305                 MountPoint *m;
306                 struct udev_device *d;
307                 dev_t devnum;
308                 char *node;
309                 const char *dn;
310
311                 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
312                         r = -ENOMEM;
313                         goto finish;
314                 }
315
316                 devnum = udev_device_get_devnum(d);
317                 dn = udev_device_get_devnode(d);
318
319                 if (major(devnum) == 0 || !dn) {
320                         udev_device_unref(d);
321                         continue;
322                 }
323
324                 node = strdup(dn);
325                 udev_device_unref(d);
326
327                 if (!node) {
328                         r = -ENOMEM;
329                         goto finish;
330                 }
331
332                 if (!(m = new(MountPoint, 1))) {
333                         free(node);
334                         r = -ENOMEM;
335                         goto finish;
336                 }
337
338                 m->path = node;
339                 m->devnum = devnum;
340                 LIST_PREPEND(MountPoint, mount_point, *head, m);
341         }
342
343         r = 0;
344
345 finish:
346         if (e)
347                 udev_enumerate_unref(e);
348
349         if (udev)
350                 udev_unref(udev);
351
352         return r;
353 }
354
355 static int delete_loopback(const char *device) {
356         int fd, r;
357
358         if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0)
359                 return -errno;
360
361         r = ioctl(fd, LOOP_CLR_FD, 0);
362         close_nointr_nofail(fd);
363
364         if (r >= 0)
365                 return 1;
366
367         /* ENXIO: not bound, so no error */
368         if (errno == ENXIO)
369                 return 0;
370
371         return -errno;
372 }
373
374 static int delete_dm(dev_t devnum) {
375         int fd, r;
376         struct dm_ioctl dm;
377
378         assert(major(devnum) != 0);
379
380         if ((fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC)) < 0)
381                 return -errno;
382
383         zero(dm);
384         dm.version[0] = DM_VERSION_MAJOR;
385         dm.version[1] = DM_VERSION_MINOR;
386         dm.version[2] = DM_VERSION_PATCHLEVEL;
387
388         dm.data_size = sizeof(dm);
389         dm.dev = devnum;
390
391         r = ioctl(fd, DM_DEV_REMOVE, &dm);
392         close_nointr_nofail(fd);
393
394         return r >= 0 ? 0 : -errno;
395 }
396
397 static int mount_points_list_umount(MountPoint **head, bool *changed) {
398         MountPoint *m, *n;
399         int n_failed = 0;
400
401         assert(head);
402
403         LIST_FOREACH_SAFE(mount_point, m, n, *head) {
404                 if (streq(m->path, "/"))
405                         continue;
406
407                 /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
408                 if (umount2(m->path, MNT_FORCE) == 0) {
409
410                         if (changed)
411                                 *changed = true;
412
413                         mount_point_free(head, m);
414                 } else {
415                         log_warning("Could not unmount %s: %m", m->path);
416                         n_failed++;
417                 }
418         }
419
420         return n_failed;
421 }
422
423 static int mount_points_list_remount_read_only(MountPoint **head, bool *changed) {
424         MountPoint *m, *n;
425         int n_failed = 0;
426
427         assert(head);
428
429         LIST_FOREACH_SAFE(mount_point, m, n, *head) {
430
431                 /* Trying to remount read-only */
432                 if (mount(NULL, m->path, NULL, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0) {
433                         if (changed)
434                                 *changed = true;
435
436                         mount_point_free(head, m);
437                 } else {
438                         log_warning("Could not remount as read-only %s: %m", m->path);
439                         n_failed++;
440                 }
441         }
442
443         return n_failed;
444 }
445
446 static int swap_points_list_off(MountPoint **head, bool *changed) {
447         MountPoint *m, *n;
448         int n_failed = 0;
449
450         assert(head);
451
452         LIST_FOREACH_SAFE(mount_point, m, n, *head) {
453                 if (swapoff(m->path) == 0) {
454                         if (changed)
455                                 *changed = true;
456
457                         mount_point_free(head, m);
458                 } else {
459                         log_warning("Could not deactivate swap %s: %m", m->path);
460                         n_failed++;
461                 }
462         }
463
464         return n_failed;
465 }
466
467 static int loopback_points_list_detach(MountPoint **head, bool *changed) {
468         MountPoint *m, *n;
469         int n_failed = 0;
470
471         assert(head);
472
473         LIST_FOREACH_SAFE(mount_point, m, n, *head) {
474                 int r;
475
476                 if ((r = delete_loopback(m->path)) >= 0) {
477
478                         if (r > 0 && changed)
479                                 *changed = true;
480
481                         mount_point_free(head, m);
482                 } else {
483                         log_warning("Could not delete loopback %s: %m", m->path);
484                         n_failed++;
485                 }
486         }
487
488         return n_failed;
489 }
490
491 static int dm_points_list_detach(MountPoint **head, bool *changed) {
492         MountPoint *m, *n;
493         int n_failed = 0;
494
495         assert(head);
496
497         LIST_FOREACH_SAFE(mount_point, m, n, *head) {
498                 int r;
499
500                 if ((r = delete_dm(m->devnum)) >= 0) {
501
502                         if (r > 0 && changed)
503                                 *changed = true;
504
505                         mount_point_free(head, m);
506                 } else {
507                         log_warning("Could not delete dm %s: %m", m->path);
508                         n_failed++;
509                 }
510         }
511
512         return n_failed;
513 }
514
515 int umount_all(bool *changed) {
516         int r;
517         LIST_HEAD(MountPoint, mp_list_head);
518
519         LIST_HEAD_INIT(MountPoint, mp_list_head);
520
521         r = mount_points_list_get(&mp_list_head);
522         if (r < 0)
523                 goto end;
524
525         r = mount_points_list_umount(&mp_list_head, changed);
526         if (r <= 0)
527                 goto end;
528
529         r = mount_points_list_remount_read_only(&mp_list_head, changed);
530
531   end:
532         mount_points_list_free(&mp_list_head);
533
534         return r;
535 }
536
537 int swapoff_all(bool *changed) {
538         int r;
539         LIST_HEAD(MountPoint, swap_list_head);
540
541         LIST_HEAD_INIT(MountPoint, swap_list_head);
542
543         r = swap_list_get(&swap_list_head);
544         if (r < 0)
545                 goto end;
546
547         r = swap_points_list_off(&swap_list_head, changed);
548
549   end:
550         mount_points_list_free(&swap_list_head);
551
552         return r;
553 }
554
555 int loopback_detach_all(bool *changed) {
556         int r;
557         LIST_HEAD(MountPoint, loopback_list_head);
558
559         LIST_HEAD_INIT(MountPoint, loopback_list_head);
560
561         r = loopback_list_get(&loopback_list_head);
562         if (r < 0)
563                 goto end;
564
565         r = loopback_points_list_detach(&loopback_list_head, changed);
566
567   end:
568         mount_points_list_free(&loopback_list_head);
569
570         return r;
571 }
572
573 int dm_detach_all(bool *changed) {
574         int r;
575         LIST_HEAD(MountPoint, dm_list_head);
576
577         LIST_HEAD_INIT(MountPoint, dm_list_head);
578
579         r = dm_list_get(&dm_list_head);
580         if (r < 0)
581                 goto end;
582
583         r = dm_points_list_detach(&dm_list_head, changed);
584
585   end:
586         mount_points_list_free(&dm_list_head);
587
588         return r;
589 }