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 /* Takes over possession of path */
42 static MountPoint *mount_point_alloc(char *path) {
45 if (!(mp = new(MountPoint, 1)))
52 static void mount_point_remove_and_free(MountPoint *mount_point, MountPoint **mount_point_list_head) {
53 LIST_REMOVE(MountPoint, mount_point, *mount_point_list_head, mount_point);
55 free(mount_point->path);
59 static void mount_points_list_free(MountPoint **mount_point_list_head) {
60 while (*mount_point_list_head)
61 mount_point_remove_and_free(*mount_point_list_head, mount_point_list_head);
64 static int mount_points_list_get(MountPoint **mount_point_list_head) {
65 FILE *proc_self_mountinfo;
70 if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
79 if ((k = fscanf(proc_self_mountinfo,
80 "%*s " /* (1) mount id */
81 "%*s " /* (2) parent id */
82 "%*s " /* (3) major:minor */
84 "%ms " /* (5) mount point */
85 "%*s" /* (6) mount options */
86 "%*[^-]" /* (7) optional fields */
87 "- " /* (8) separator */
88 "%*s " /* (9) file system type */
89 "%*s" /* (10) mount source */
90 "%*s" /* (11) mount options 2 */
91 "%*[^\n]", /* some rubbish at the end */
96 log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
110 if (mount_point_is_api(p)) {
115 if (!(mp = mount_point_alloc(p))) {
121 LIST_PREPEND(MountPoint, mount_point, *mount_point_list_head, mp);
127 fclose(proc_self_mountinfo);
132 static int swap_list_get(MountPoint **swap_list_head) {
137 if (!(proc_swaps = fopen("/proc/swaps", "re")))
140 (void) fscanf(proc_swaps, "%*s %*s %*s %*s %*s\n");
144 char *dev = NULL, *d;
147 if ((k = fscanf(proc_swaps,
148 "%ms " /* device/file */
149 "%*s " /* type of swap */
150 "%*s " /* swap size */
152 "%*s\n", /* priority */
158 log_warning("Failed to parse /proc/swaps:%u.", i);
164 if (endswith(dev, "(deleted)")) {
177 swap = mount_point_alloc(d);
184 LIST_PREPEND(MountPoint, mount_point, *swap_list_head, swap);
195 static int loopback_list_get(MountPoint **loopback_list_head) {
198 struct udev_enumerate *e = NULL;
199 struct udev_list_entry *item = NULL, *first = NULL;
201 if (!(udev = udev_new())) {
206 if (!(e = udev_enumerate_new(udev))) {
211 if (udev_enumerate_add_match_subsystem(e, "block") < 0) {
216 if (udev_enumerate_add_match_sysname(e, "loop*") < 0) {
221 if (udev_enumerate_scan_devices(e) < 0) {
226 first = udev_enumerate_get_list_entry(e);
228 udev_list_entry_foreach(item, first) {
230 struct udev_device *d;
234 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
239 if ((dn = udev_device_get_devnode(d))) {
241 udev_device_unref(d);
248 udev_device_unref(d);
250 if (!(lb = mount_point_alloc(loop))) {
256 LIST_PREPEND(MountPoint, mount_point, *loopback_list_head, lb);
263 udev_enumerate_unref(e);
271 static int delete_loopback(const char *device) {
274 if ((fd = open(device, O_RDONLY|O_CLOEXEC)) < 0)
277 r = ioctl(fd, LOOP_CLR_FD, 0);
278 close_nointr_nofail(fd);
280 /* ENXIO: not bound, so no error */
281 return (r >= 0 || errno == ENXIO) ? 0 : -errno;
284 static int mount_points_list_umount(MountPoint **mount_point_list_head) {
285 MountPoint *mp, *mp_next;
288 LIST_FOREACH_SAFE(mount_point, mp, mp_next, *mount_point_list_head) {
289 if (streq(mp->path, "/"))
292 /* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
293 if (umount2(mp->path, MNT_FORCE) == 0)
294 mount_point_remove_and_free(mp, mount_point_list_head);
296 log_debug("Could not unmount %s: %m", mp->path);
304 static int mount_points_list_remount_read_only(MountPoint **mount_point_list_head) {
305 MountPoint *mp, *mp_next;
308 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 mount_point_remove_and_free(mp, mount_point_list_head);
313 log_debug("Could not remount as read-only %s: %m", mp->path);
321 static int swap_points_list_off(MountPoint **swap_list_head) {
322 MountPoint *swap, *swap_next;
325 LIST_FOREACH_SAFE(mount_point, swap, swap_next, *swap_list_head) {
326 if (swapoff(swap->path) == 0)
327 mount_point_remove_and_free(swap, swap_list_head);
329 log_debug("Could not swapoff %s: %m", swap->path);
337 static int loopback_points_list_detach(MountPoint **loopback_list_head) {
338 MountPoint *loopback, *loopback_next;
341 LIST_FOREACH_SAFE(mount_point, loopback, loopback_next, *loopback_list_head) {
342 if (delete_loopback(loopback->path) == 0)
343 mount_point_remove_and_free(loopback, loopback_list_head);
345 log_debug("Could not delete loopback %s: %m", loopback->path);
353 int umount_all(void) {
355 LIST_HEAD(MountPoint, mp_list_head);
357 LIST_HEAD_INIT(MountPoint, mp_list_head);
359 r = mount_points_list_get(&mp_list_head);
363 r = mount_points_list_umount(&mp_list_head);
367 r = mount_points_list_remount_read_only(&mp_list_head);
370 mount_points_list_free(&mp_list_head);
375 int swapoff_all(void) {
377 LIST_HEAD(MountPoint, swap_list_head);
379 LIST_HEAD_INIT(MountPoint, swap_list_head);
381 r = swap_list_get(&swap_list_head);
385 r = swap_points_list_off(&swap_list_head);
388 mount_points_list_free(&swap_list_head);
393 int loopback_detach_all(void) {
395 LIST_HEAD(MountPoint, loopback_list_head);
397 LIST_HEAD_INIT(MountPoint, loopback_list_head);
399 r = loopback_list_get(&loopback_list_head);
403 r = loopback_points_list_detach(&loopback_list_head);
406 mount_points_list_free(&loopback_list_head);