1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 ProFUSION embedded systems
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.
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.
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/>.
25 #include <sys/mount.h>
28 #include <linux/loop.h>
29 #include <linux/dm-ioctl.h>
33 #include "mount-setup.h"
37 typedef struct MountPoint {
40 LIST_FIELDS (struct MountPoint, mount_point);
43 static void mount_point_remove_and_free(MountPoint *mount_point, MountPoint **mount_point_list_head) {
44 LIST_REMOVE(MountPoint, mount_point, *mount_point_list_head, mount_point);
46 free(mount_point->path);
50 static void mount_points_list_free(MountPoint **mount_point_list_head) {
51 while (*mount_point_list_head)
52 mount_point_remove_and_free(*mount_point_list_head, mount_point_list_head);
55 static int mount_points_list_get(MountPoint **mount_point_list_head) {
56 FILE *proc_self_mountinfo;
61 if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
70 if ((k = fscanf(proc_self_mountinfo,
71 "%*s " /* (1) mount id */
72 "%*s " /* (2) parent id */
73 "%*s " /* (3) major:minor */
75 "%ms " /* (5) mount point */
76 "%*s" /* (6) mount options */
77 "%*[^-]" /* (7) optional fields */
78 "- " /* (8) separator */
79 "%*s " /* (9) file system type */
80 "%*s" /* (10) mount source */
81 "%*s" /* (11) mount options 2 */
82 "%*[^\n]", /* some rubbish at the end */
87 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
101 if (mount_point_is_api(p)) {
106 if (!(mp = new0(MountPoint, 1))) {
113 LIST_PREPEND(MountPoint, mount_point, *mount_point_list_head, mp);
119 fclose(proc_self_mountinfo);
124 static int swap_list_get(MountPoint **swap_list_head) {
129 if (!(proc_swaps = fopen("/proc/swaps", "re")))
132 (void) fscanf(proc_swaps, "%*s %*s %*s %*s %*s\n");
136 char *dev = NULL, *d;
139 if ((k = fscanf(proc_swaps,
140 "%ms " /* device/file */
141 "%*s " /* type of swap */
142 "%*s " /* swap size */
144 "%*s\n", /* priority */
150 log_warning("Failed to parse /proc/swaps:%u.", i);
156 if (endswith(dev, "(deleted)")) {
169 if (!(swap = new0(MountPoint, 1))) {
176 LIST_PREPEND(MountPoint, mount_point, *swap_list_head, swap);
187 static int loopback_list_get(MountPoint **loopback_list_head) {
190 struct udev_enumerate *e = NULL;
191 struct udev_list_entry *item = NULL, *first = NULL;
193 if (!(udev = udev_new())) {
198 if (!(e = udev_enumerate_new(udev))) {
203 if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
204 udev_enumerate_add_match_sysname(e, "loop*") < 0) {
209 if (udev_enumerate_scan_devices(e) < 0) {
214 first = udev_enumerate_get_list_entry(e);
216 udev_list_entry_foreach(item, first) {
218 struct udev_device *d;
222 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
227 if (!(dn = udev_device_get_devnode(d))) {
228 udev_device_unref(d);
233 udev_device_unref(d);
240 if (!(lb = new0(MountPoint, 1))) {
247 LIST_PREPEND(MountPoint, mount_point, *loopback_list_head, lb);
254 udev_enumerate_unref(e);
262 static int dm_list_get(MountPoint **dm_list_head) {
265 struct udev_enumerate *e = NULL;
266 struct udev_list_entry *item = NULL, *first = NULL;
268 if (!(udev = udev_new())) {
273 if (!(e = udev_enumerate_new(udev))) {
278 if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
279 udev_enumerate_add_match_sysname(e, "dm-*") < 0) {
284 if (udev_enumerate_scan_devices(e) < 0) {
289 first = udev_enumerate_get_list_entry(e);
291 udev_list_entry_foreach(item, first) {
293 struct udev_device *d;
298 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
303 devnum = udev_device_get_devnum(d);
304 dn = udev_device_get_devnode(d);
306 if (major(devnum) == 0 || !dn) {
307 udev_device_unref(d);
312 udev_device_unref(d);
319 if (!(m = new(MountPoint, 1))) {
327 LIST_PREPEND(MountPoint, mount_point, *dm_list_head, m);
334 udev_enumerate_unref(e);
342 static int delete_loopback(const char *device) {
345 if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0)
348 r = ioctl(fd, LOOP_CLR_FD, 0);
349 close_nointr_nofail(fd);
351 /* ENXIO: not bound, so no error */
352 return (r >= 0 || errno == ENXIO) ? 0 : -errno;
355 static int delete_dm(dev_t devnum) {
359 assert(major(devnum) != 0);
361 if ((fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC)) < 0)
365 dm.version[0] = DM_VERSION_MAJOR;
366 dm.version[1] = DM_VERSION_MINOR;
367 dm.version[2] = DM_VERSION_PATCHLEVEL;
369 dm.data_size = sizeof(dm);
372 r = ioctl(fd, DM_DEV_REMOVE, &dm);
373 close_nointr_nofail(fd);
375 return r >= 0 ? 0 : -errno;
378 static int mount_points_list_umount(MountPoint **mount_point_list_head) {
379 MountPoint *mp, *mp_next;
382 LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
383 if (streq(mp->path, "/"))
386 /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
387 if (umount2(mp->path, MNT_FORCE) == 0)
388 mount_point_remove_and_free(mp, mount_point_list_head);
390 log_warning("Could not unmount %s: %m", mp->path);
398 static int mount_points_list_remount_read_only(MountPoint **mount_point_list_head) {
399 MountPoint *mp, *mp_next;
402 LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
403 /* Trying to remount read-only */
404 if (mount(NULL, mp->path, NULL, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0)
405 mount_point_remove_and_free(mp, mount_point_list_head);
407 log_warning("Could not remount as read-only %s: %m", mp->path);
415 static int swap_points_list_off(MountPoint **swap_list_head) {
416 MountPoint *swap, *swap_next;
419 LIST_FOREACH_SAFE(mount_point, swap, swap_next, *swap_list_head) {
420 if (swapoff(swap->path) == 0)
421 mount_point_remove_and_free(swap, swap_list_head);
423 log_warning("Could not deactivate swap %s: %m", swap->path);
431 static int loopback_points_list_detach(MountPoint **loopback_list_head) {
432 MountPoint *loopback, *loopback_next;
435 LIST_FOREACH_SAFE(mount_point, loopback, loopback_next, *loopback_list_head) {
436 if (delete_loopback(loopback->path) == 0)
437 mount_point_remove_and_free(loopback, loopback_list_head);
439 log_warning("Could not delete loopback %s: %m", loopback->path);
447 static int dm_points_list_detach(MountPoint **dm_list_head) {
448 MountPoint *dm, *dm_next;
451 LIST_FOREACH_SAFE(mount_point, dm, dm_next, *dm_list_head) {
452 if (delete_dm(dm->devnum) == 0)
453 mount_point_remove_and_free(dm, dm_list_head);
455 log_warning("Could not delete dm %s: %m", dm->path);
463 int umount_all(void) {
465 LIST_HEAD(MountPoint, mp_list_head);
467 LIST_HEAD_INIT(MountPoint, mp_list_head);
469 r = mount_points_list_get(&mp_list_head);
473 r = mount_points_list_umount(&mp_list_head);
477 r = mount_points_list_remount_read_only(&mp_list_head);
480 mount_points_list_free(&mp_list_head);
485 int swapoff_all(void) {
487 LIST_HEAD(MountPoint, swap_list_head);
489 LIST_HEAD_INIT(MountPoint, swap_list_head);
491 r = swap_list_get(&swap_list_head);
495 r = swap_points_list_off(&swap_list_head);
498 mount_points_list_free(&swap_list_head);
503 int loopback_detach_all(void) {
505 LIST_HEAD(MountPoint, loopback_list_head);
507 LIST_HEAD_INIT(MountPoint, loopback_list_head);
509 r = loopback_list_get(&loopback_list_head);
513 r = loopback_points_list_detach(&loopback_list_head);
516 mount_points_list_free(&loopback_list_head);
521 int dm_detach_all(void) {
523 LIST_HEAD(MountPoint, dm_list_head);
525 LIST_HEAD_INIT(MountPoint, dm_list_head);
527 r = dm_list_get(&dm_list_head);
531 r = dm_points_list_detach(&dm_list_head);
534 mount_points_list_free(&dm_list_head);