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>
32 #include "mount-setup.h"
36 typedef struct MountPoint {
39 LIST_FIELDS (struct MountPoint, mount_point);
42 static MountPoint *mount_point_alloc(char *path) {
45 if (!(mp = new(MountPoint, 1)))
49 mp->read_only = false;
54 static void mount_point_remove_and_free(MountPoint *mount_point, MountPoint **mount_point_list_head) {
55 LIST_REMOVE(MountPoint, mount_point, *mount_point_list_head, mount_point);
57 free(mount_point->path);
61 static void mount_points_list_free(MountPoint **mount_point_list_head) {
62 while (*mount_point_list_head)
63 mount_point_remove_and_free(*mount_point_list_head, mount_point_list_head);
66 static int mount_points_list_get(MountPoint **mount_point_list_head) {
67 FILE *proc_self_mountinfo;
72 if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
81 if ((k = fscanf(proc_self_mountinfo,
82 "%*s " /* (1) mount id */
83 "%*s " /* (2) parent id */
84 "%*s " /* (3) major:minor */
86 "%ms " /* (5) mount point */
87 "%*s" /* (6) mount options */
88 "%*[^-]" /* (7) optional fields */
89 "- " /* (8) separator */
90 "%*s " /* (9) file system type */
91 "%*s" /* (10) mount source */
92 "%*s" /* (11) mount options 2 */
93 "%*[^\n]", /* some rubbish at the end */
98 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
104 if (mount_point_is_api(path)) {
109 if (!(p = cunescape(path))) {
114 if (!(mp = mount_point_alloc(p))) {
118 LIST_PREPEND(MountPoint, mount_point, *mount_point_list_head, mp);
126 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) {
217 if (udev_enumerate_add_match_sysname(e, "loop*") < 0) {
222 if (udev_enumerate_scan_devices(e) < 0) {
227 first = udev_enumerate_get_list_entry(e);
229 udev_list_entry_foreach(item, first) {
233 loop = cunescape(udev_list_entry_get_name(item));
239 lb = mount_point_alloc(loop);
246 LIST_PREPEND(MountPoint, mount_point, *loopback_list_head, lb);
253 udev_enumerate_unref(e);
259 static int delete_loopback(const char *device) {
262 if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0) {
263 if (errno == ENOENT) {
264 log_debug("Loop device %s does not exist.", device);
271 ioctl(fd, LOOP_CLR_FD, 0);
275 if (r == ENXIO) /* not bound, so no error */
281 static int mount_points_list_umount(MountPoint **mount_point_list_head) {
282 MountPoint *mp, *mp_next;
285 LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
286 if (streq(mp->path, "/"))
289 /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
290 if (umount2(mp->path, MNT_FORCE) == 0)
291 mount_point_remove_and_free(mp, mount_point_list_head);
293 log_debug("Could not unmount %s: %m", mp->path);
301 static int mount_points_list_remount_read_only(MountPoint **mount_point_list_head) {
302 MountPoint *mp, *mp_next;
305 LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
309 /* Trying to remount read-only */
310 if (mount(NULL, mp->path, NULL, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0) {
311 mp->read_only = true;
312 mount_point_remove_and_free(mp, mount_point_list_head);
314 log_debug("Could not remount as read-only %s: %m", mp->path);
322 static int swap_points_list_off(MountPoint **swap_list_head) {
323 MountPoint *swap, *swap_next;
326 LIST_FOREACH_SAFE(mount_point, swap, swap_next, *swap_list_head) {
327 if (swapoff(swap->path) == 0)
328 mount_point_remove_and_free(swap, swap_list_head);
330 log_debug("Could not swapoff %s: %m", swap->path);
338 static int loopback_points_list_detach(MountPoint **loopback_list_head) {
339 MountPoint *loopback, *loopback_next;
342 LIST_FOREACH_SAFE(mount_point, loopback, loopback_next, *loopback_list_head) {
343 if (delete_loopback(loopback->path) == 0)
344 mount_point_remove_and_free(loopback, loopback_list_head);
346 log_debug("Could not delete loopback %s: %m", loopback->path);
354 int umount_all(void) {
356 LIST_HEAD(MountPoint, mp_list_head);
358 LIST_HEAD_INIT(MountPoint, mp_list_head);
360 r = mount_points_list_get(&mp_list_head);
364 r = mount_points_list_umount(&mp_list_head);
368 r = mount_points_list_remount_read_only(&mp_list_head);
371 mount_points_list_free(&mp_list_head);
376 int swapoff_all(void) {
378 LIST_HEAD(MountPoint, swap_list_head);
380 LIST_HEAD_INIT(MountPoint, swap_list_head);
382 r = swap_list_get(&swap_list_head);
386 r = swap_points_list_off(&swap_list_head);
389 mount_points_list_free(&swap_list_head);
394 int loopback_detach_all(void) {
396 LIST_HEAD(MountPoint, loopback_list_head);
398 LIST_HEAD_INIT(MountPoint, loopback_list_head);
400 r = loopback_list_get(&loopback_list_head);
404 r = loopback_points_list_detach(&loopback_list_head);
407 mount_points_list_free(&loopback_list_head);