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 {
39 LIST_FIELDS (struct MountPoint, mount_point);
42 /* Takes over possession of path */
43 static MountPoint *mount_point_alloc(char *path) {
46 if (!(mp = new(MountPoint, 1)))
53 static void mount_point_remove_and_free(MountPoint *mount_point, MountPoint **mount_point_list_head) {
54 LIST_REMOVE(MountPoint, mount_point, *mount_point_list_head, mount_point);
56 free(mount_point->path);
60 static void mount_points_list_free(MountPoint **mount_point_list_head) {
61 while (*mount_point_list_head)
62 mount_point_remove_and_free(*mount_point_list_head, mount_point_list_head);
65 static int mount_points_list_get(MountPoint **mount_point_list_head) {
66 FILE *proc_self_mountinfo;
71 if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
80 if ((k = fscanf(proc_self_mountinfo,
81 "%*s " /* (1) mount id */
82 "%*s " /* (2) parent id */
83 "%*s " /* (3) major:minor */
85 "%ms " /* (5) mount point */
86 "%*s" /* (6) mount options */
87 "%*[^-]" /* (7) optional fields */
88 "- " /* (8) separator */
89 "%*s " /* (9) file system type */
90 "%*s" /* (10) mount source */
91 "%*s" /* (11) mount options 2 */
92 "%*[^\n]", /* some rubbish at the end */
97 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
111 if (mount_point_is_api(p)) {
116 if (!(mp = mount_point_alloc(p))) {
122 LIST_PREPEND(MountPoint, mount_point, *mount_point_list_head, mp);
128 fclose(proc_self_mountinfo);
133 static int swap_list_get(MountPoint **swap_list_head) {
138 if (!(proc_swaps = fopen("/proc/swaps", "re")))
141 (void) fscanf(proc_swaps, "%*s %*s %*s %*s %*s\n");
145 char *dev = NULL, *d;
148 if ((k = fscanf(proc_swaps,
149 "%ms " /* device/file */
150 "%*s " /* type of swap */
151 "%*s " /* swap size */
153 "%*s\n", /* priority */
159 log_warning("Failed to parse /proc/swaps:%u.", i);
165 if (endswith(dev, "(deleted)")) {
178 swap = mount_point_alloc(d);
185 LIST_PREPEND(MountPoint, mount_point, *swap_list_head, swap);
196 static int loopback_list_get(MountPoint **loopback_list_head) {
199 struct udev_enumerate *e = NULL;
200 struct udev_list_entry *item = NULL, *first = NULL;
202 if (!(udev = udev_new())) {
207 if (!(e = udev_enumerate_new(udev))) {
212 if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
213 udev_enumerate_add_match_sysname(e, "loop*") < 0) {
218 if (udev_enumerate_scan_devices(e) < 0) {
223 first = udev_enumerate_get_list_entry(e);
225 udev_list_entry_foreach(item, first) {
227 struct udev_device *d;
231 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
236 if ((dn = udev_device_get_devnode(d))) {
238 udev_device_unref(d);
245 udev_device_unref(d);
247 if (!(lb = mount_point_alloc(loop))) {
253 LIST_PREPEND(MountPoint, mount_point, *loopback_list_head, lb);
260 udev_enumerate_unref(e);
268 static int dm_list_get(MountPoint **dm_list_head) {
271 struct udev_enumerate *e = NULL;
272 struct udev_list_entry *item = NULL, *first = NULL;
274 if (!(udev = udev_new())) {
279 if (!(e = udev_enumerate_new(udev))) {
284 if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
285 udev_enumerate_add_match_sysname(e, "dm-*") < 0) {
290 if (udev_enumerate_scan_devices(e) < 0) {
295 first = udev_enumerate_get_list_entry(e);
297 udev_list_entry_foreach(item, first) {
299 struct udev_device *d;
301 struct udev_list_entry *dlink = NULL, *first_dlink = NULL;
303 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
308 first_dlink = udev_device_get_devlinks_list_entry(d);
309 udev_list_entry_foreach(dlink, first_dlink) {
311 if (startswith(udev_list_entry_get_name(dlink), "/dev/mapper/")) {
313 if (!(dm = strdup(udev_list_entry_get_name(dlink)))) {
314 udev_device_unref(d);
323 udev_device_unref(d);
328 if (!(lb = mount_point_alloc(dm))) {
334 LIST_PREPEND(MountPoint, mount_point, *dm_list_head, lb);
341 udev_enumerate_unref(e);
349 static int delete_loopback(const char *device) {
352 if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0)
355 r = ioctl(fd, LOOP_CLR_FD, 0);
356 close_nointr_nofail(fd);
358 /* ENXIO: not bound, so no error */
359 return (r >= 0 || errno == ENXIO) ? 0 : -errno;
362 static int delete_dm(const char *device) {
368 if (!startswith(device, "/dev/mapper/"))
371 if ((fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC)) < 0)
375 dm.version[0] = DM_VERSION_MAJOR;
376 dm.version[1] = DM_VERSION_MINOR;
377 dm.version[2] = DM_VERSION_PATCHLEVEL;
379 dm.data_size = sizeof(dm);
381 strncpy(dm.name, device + 12, sizeof(dm.name));
383 r = ioctl(fd, DM_DEV_REMOVE, &dm);
384 close_nointr_nofail(fd);
386 return r >= 0 ? 0 : -errno;
389 static int mount_points_list_umount(MountPoint **mount_point_list_head) {
390 MountPoint *mp, *mp_next;
393 LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
394 if (streq(mp->path, "/"))
397 /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
398 if (umount2(mp->path, MNT_FORCE) == 0)
399 mount_point_remove_and_free(mp, mount_point_list_head);
401 log_warning("Could not unmount %s: %m", mp->path);
409 static int mount_points_list_remount_read_only(MountPoint **mount_point_list_head) {
410 MountPoint *mp, *mp_next;
413 LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
414 /* Trying to remount read-only */
415 if (mount(NULL, mp->path, NULL, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0)
416 mount_point_remove_and_free(mp, mount_point_list_head);
418 log_warning("Could not remount as read-only %s: %m", mp->path);
426 static int swap_points_list_off(MountPoint **swap_list_head) {
427 MountPoint *swap, *swap_next;
430 LIST_FOREACH_SAFE(mount_point, swap, swap_next, *swap_list_head) {
431 if (swapoff(swap->path) == 0)
432 mount_point_remove_and_free(swap, swap_list_head);
434 log_warning("Could not deactivate swap %s: %m", swap->path);
442 static int loopback_points_list_detach(MountPoint **loopback_list_head) {
443 MountPoint *loopback, *loopback_next;
446 LIST_FOREACH_SAFE(mount_point, loopback, loopback_next, *loopback_list_head) {
447 if (delete_loopback(loopback->path) == 0)
448 mount_point_remove_and_free(loopback, loopback_list_head);
450 log_warning("Could not delete loopback %s: %m", loopback->path);
458 static int dm_points_list_detach(MountPoint **dm_list_head) {
459 MountPoint *dm, *dm_next;
462 LIST_FOREACH_SAFE(mount_point, dm, dm_next, *dm_list_head) {
463 if (delete_dm(dm->path) == 0)
464 mount_point_remove_and_free(dm, dm_list_head);
466 log_warning("Could not delete dm %s: %m", dm->path);
474 int umount_all(void) {
476 LIST_HEAD(MountPoint, mp_list_head);
478 LIST_HEAD_INIT(MountPoint, mp_list_head);
480 r = mount_points_list_get(&mp_list_head);
484 r = mount_points_list_umount(&mp_list_head);
488 r = mount_points_list_remount_read_only(&mp_list_head);
491 mount_points_list_free(&mp_list_head);
496 int swapoff_all(void) {
498 LIST_HEAD(MountPoint, swap_list_head);
500 LIST_HEAD_INIT(MountPoint, swap_list_head);
502 r = swap_list_get(&swap_list_head);
506 r = swap_points_list_off(&swap_list_head);
509 mount_points_list_free(&swap_list_head);
514 int loopback_detach_all(void) {
516 LIST_HEAD(MountPoint, loopback_list_head);
518 LIST_HEAD_INIT(MountPoint, loopback_list_head);
520 r = loopback_list_get(&loopback_list_head);
524 r = loopback_points_list_detach(&loopback_list_head);
527 mount_points_list_free(&loopback_list_head);
532 int dm_detach_all(void) {
534 LIST_HEAD(MountPoint, dm_list_head);
536 LIST_HEAD_INIT(MountPoint, dm_list_head);
538 r = dm_list_get(&dm_list_head);
542 r = dm_points_list_detach(&dm_list_head);
545 mount_points_list_free(&dm_list_head);