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 {
38 LIST_FIELDS (struct MountPoint, mount_point);
41 static MountPoint *mount_point_alloc(char *path) {
44 if (!(mp = new(MountPoint, 1)))
51 static void mount_point_remove_and_free(MountPoint *mount_point, MountPoint **mount_point_list_head) {
52 LIST_REMOVE(MountPoint, mount_point, *mount_point_list_head, mount_point);
54 free(mount_point->path);
58 static void mount_points_list_free(MountPoint **mount_point_list_head) {
59 while (*mount_point_list_head)
60 mount_point_remove_and_free(*mount_point_list_head, mount_point_list_head);
63 static int mount_points_list_get(MountPoint **mount_point_list_head) {
64 FILE *proc_self_mountinfo;
69 if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
78 if ((k = fscanf(proc_self_mountinfo,
79 "%*s " /* (1) mount id */
80 "%*s " /* (2) parent id */
81 "%*s " /* (3) major:minor */
83 "%ms " /* (5) mount point */
84 "%*s" /* (6) mount options */
85 "%*[^-]" /* (7) optional fields */
86 "- " /* (8) separator */
87 "%*s " /* (9) file system type */
88 "%*s" /* (10) mount source */
89 "%*s" /* (11) mount options 2 */
90 "%*[^\n]", /* some rubbish at the end */
95 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
101 if (mount_point_is_api(path)) {
106 if (!(p = cunescape(path))) {
111 if (!(mp = mount_point_alloc(p))) {
115 LIST_PREPEND(MountPoint, mount_point, *mount_point_list_head, mp);
123 fclose(proc_self_mountinfo);
130 static int swap_list_get(MountPoint **swap_list_head) {
135 if (!(proc_swaps = fopen("/proc/swaps", "re")))
138 (void) fscanf(proc_swaps, "%*s %*s %*s %*s %*s\n");
142 char *dev = NULL, *d;
145 if ((k = fscanf(proc_swaps,
146 "%ms " /* device/file */
147 "%*s " /* type of swap */
148 "%*s " /* swap size */
150 "%*s\n", /* priority */
156 log_warning("Failed to parse /proc/swaps:%u.", i);
162 if (endswith(dev, "(deleted)")) {
175 swap = mount_point_alloc(d);
182 LIST_PREPEND(MountPoint, mount_point, *swap_list_head, swap);
193 static int loopback_list_get(MountPoint **loopback_list_head) {
196 struct udev_enumerate *e = NULL;
197 struct udev_list_entry *item = NULL, *first = NULL;
199 if (!(udev = udev_new())) {
204 if (!(e = udev_enumerate_new(udev))) {
209 if (udev_enumerate_add_match_subsystem(e, "block") < 0) {
214 if (udev_enumerate_add_match_sysname(e, "loop*") < 0) {
219 if (udev_enumerate_scan_devices(e) < 0) {
224 first = udev_enumerate_get_list_entry(e);
226 udev_list_entry_foreach(item, first) {
230 loop = cunescape(udev_list_entry_get_name(item));
236 lb = mount_point_alloc(loop);
243 LIST_PREPEND(MountPoint, mount_point, *loopback_list_head, lb);
250 udev_enumerate_unref(e);
256 static int delete_loopback(const char *device) {
259 if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0) {
260 if (errno == ENOENT) {
261 log_debug("Loop device %s does not exist.", device);
268 ioctl(fd, LOOP_CLR_FD, 0);
270 close_nointr_nofail(fd);
272 if (r == ENXIO) /* not bound, so no error */
278 static int mount_points_list_umount(MountPoint **mount_point_list_head) {
279 MountPoint *mp, *mp_next;
282 LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
283 if (streq(mp->path, "/"))
286 /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
287 if (umount2(mp->path, MNT_FORCE) == 0)
288 mount_point_remove_and_free(mp, mount_point_list_head);
290 log_debug("Could not unmount %s: %m", mp->path);
298 static int mount_points_list_remount_read_only(MountPoint **mount_point_list_head) {
299 MountPoint *mp, *mp_next;
302 LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
303 /* Trying to remount read-only */
304 if (mount(NULL, mp->path, NULL, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0)
305 mount_point_remove_and_free(mp, mount_point_list_head);
307 log_debug("Could not remount as read-only %s: %m", mp->path);
315 static int swap_points_list_off(MountPoint **swap_list_head) {
316 MountPoint *swap, *swap_next;
319 LIST_FOREACH_SAFE(mount_point, swap, swap_next, *swap_list_head) {
320 if (swapoff(swap->path) == 0)
321 mount_point_remove_and_free(swap, swap_list_head);
323 log_debug("Could not swapoff %s: %m", swap->path);
331 static int loopback_points_list_detach(MountPoint **loopback_list_head) {
332 MountPoint *loopback, *loopback_next;
335 LIST_FOREACH_SAFE(mount_point, loopback, loopback_next, *loopback_list_head) {
336 if (delete_loopback(loopback->path) == 0)
337 mount_point_remove_and_free(loopback, loopback_list_head);
339 log_debug("Could not delete loopback %s: %m", loopback->path);
347 int umount_all(void) {
349 LIST_HEAD(MountPoint, mp_list_head);
351 LIST_HEAD_INIT(MountPoint, mp_list_head);
353 r = mount_points_list_get(&mp_list_head);
357 r = mount_points_list_umount(&mp_list_head);
361 r = mount_points_list_remount_read_only(&mp_list_head);
364 mount_points_list_free(&mp_list_head);
369 int swapoff_all(void) {
371 LIST_HEAD(MountPoint, swap_list_head);
373 LIST_HEAD_INIT(MountPoint, swap_list_head);
375 r = swap_list_get(&swap_list_head);
379 r = swap_points_list_off(&swap_list_head);
382 mount_points_list_free(&swap_list_head);
387 int loopback_detach_all(void) {
389 LIST_HEAD(MountPoint, loopback_list_head);
391 LIST_HEAD_INIT(MountPoint, loopback_list_head);
393 r = loopback_list_get(&loopback_list_head);
397 r = loopback_points_list_detach(&loopback_list_head);
400 mount_points_list_free(&loopback_list_head);