[systemd-devel] [PATCH 2/2] Adding unmount functions to be used in shutdown

Fabiano Fidencio fidencio at profusion.mobi
Sun Sep 26 12:55:28 PDT 2010


This functions will unmount all filesystem that aren't api
(and those which fail will be remounted read-only).
Filesystems are being read from /proc/self/mountinfo.
---
 Makefile.am  |    1 +
 src/umount.c |  192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/umount.h |   27 ++++++++
 3 files changed, 220 insertions(+), 0 deletions(-)
 create mode 100644 src/umount.c
 create mode 100644 src/umount.h

diff --git a/Makefile.am b/Makefile.am
index 70a6c19..b1c0438 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -337,6 +337,7 @@ libsystemd_core_la_SOURCES = \
 	src/service.c \
 	src/automount.c \
 	src/mount.c \
+	src/umount.c \
 	src/swap.c \
 	src/device.c \
 	src/target.c \
diff --git a/src/umount.c b/src/umount.c
new file mode 100644
index 0000000..db36111
--- /dev/null
+++ b/src/umount.c
@@ -0,0 +1,192 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2010 ProFUSION embedded systems
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <sys/mount.h>
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "log.h"
+#include "macro.h"
+#include "mount-setup.h"
+#include "umount.h"
+#include "util.h"
+
+typedef struct MountPoint {
+        char *path;
+        char *device;
+        char *fstype;
+        struct MountPoint *next;
+} MountPoint;
+
+typedef MountPoint * MountPointsList;
+
+static MountPoint *mountpoint_alloc(char *path, char *device, char *fstype) {
+        MountPoint *mp;
+
+        mp = new(MountPoint, 1);
+        mp->path = path;
+        mp->device = device;
+        mp->fstype = fstype;
+        mp->next = NULL;
+
+        return mp;
+}
+
+static void mountpoint_free(MountPoint *mp) {
+        free(mp->path);
+        free(mp->device);
+        free(mp->fstype);
+        free(mp);
+}
+
+static void mountpoints_list_prepend(MountPointsList *mp_list, MountPoint *mp) {
+        if (*mp_list)
+                mp->next = *mp_list;
+        *mp_list = mp;
+}
+
+static void mountpoints_list_init(MountPointsList *mp_list) {
+        *mp_list = NULL;
+}
+
+static int mountpoints_list_get(MountPointsList *mp_list) {
+        FILE *proc_self_mountinfo;
+        char *device, *path, *fstype, *d, *p;
+        int r;
+
+        if (!(proc_self_mountinfo = fopen("/proc/self/mountinfo", "re")))
+                return -errno;
+
+        for (;;) {
+                int k;
+                MountPoint *mp;
+
+                device = path = fstype = d = p = NULL;
+
+                if ((k = fscanf(proc_self_mountinfo,
+                                "%*s "       /* (1) mount id */
+                                "%*s "       /* (2) parent id */
+                                "%*s "       /* (3) major:minor */
+                                "%*s "       /* (4) root */
+                                "%ms "       /* (5) mount point */
+                                "%*s"        /* (6) mount options */
+                                "%*[^-]"     /* (7) optional fields */
+                                "- "         /* (8) seperator */
+                                "%ms "       /* (9) file system type */
+                                "%ms"        /* (10) mount source */
+                                "%*s"        /* (11) mount options 2 */
+                                "%*[^\n]",   /* some rubbish at the end */
+                                &path,
+                                &fstype,
+                                &device)) != 3) {
+                        if (k == EOF)
+                                break;
+
+                        r = -EBADMSG;
+                        goto finish;
+                }
+
+                if (mount_point_is_api(path))
+                        goto free;
+
+                if (!(d = cunescape(device)) ||
+                    !(p = cunescape(path))) {
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                mp = mountpoint_alloc(p, d, fstype);
+                mountpoints_list_prepend(mp_list, mp);
+free:
+                free(path);
+                free(device);
+                free(fstype);
+        }
+
+        r = 0;
+
+finish:
+        fclose(proc_self_mountinfo);
+
+        free(path);
+        free(device);
+        free(fstype);
+        free(p);
+        free(d);
+
+        return r;
+}
+
+static int mountpoints_list_umount(MountPointsList *mp_list) {
+        MountPoint *mp, *prev = NULL;
+        mp = *mp_list;
+
+        do {
+                /* Trying to umount */
+                if (umount(mp->path) == 0) {
+                        log_info("umount: %s was umounted succesfully\n", mp->path);
+                        goto success;
+                }
+
+                /* Forcing to umount if busy (only for NFS mounts) */
+                if (umount2(mp->path, MNT_FORCE) == 0) {
+                        log_info("umount: %s was umounted succesfully\n", mp->path);
+                        goto success;
+                }
+
+                /* Trying to remount read-only */
+                log_warning("umount: forced umount of %s failed! Trying to remount read-only.\n", mp->path);
+                if (mount(mp->device, mp->path, mp->fstype, MS_MGC_VAL|MS_REMOUNT|MS_RDONLY, NULL) == 0)
+                        log_warning("umount: %s busy - remounted read-only\n", mp->device);
+                else {
+                        prev = mp;
+                        mp = mp->next;
+                        log_error("umount: Cannot remount %s read-only\n", mp->device);
+                        continue;
+                }
+success:
+                if (prev)
+                        prev->next = mp->next;
+                else
+                        *mp_list = mp->next;
+                mountpoint_free(mp);
+                mp = prev ? prev->next : *mp_list;
+
+        } while (mp);
+
+        return 0;
+}
+
+void umount_init(void) {
+        MountPointsList mp_list;
+        int r;
+
+        mountpoints_list_init(&mp_list);
+        if ((r = mountpoints_list_get(&mp_list)) < 0)
+                log_warning("An error [%d] ocurred getting mount point's list - "
+                            "umounting the mount points that are possible", r);
+
+        while (mp_list)
+                mountpoints_list_umount(&mp_list);
+}
diff --git a/src/umount.h b/src/umount.h
new file mode 100644
index 0000000..6a69e67
--- /dev/null
+++ b/src/umount.h
@@ -0,0 +1,27 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#ifndef fooumounthfoo
+#define fooumounthfoo
+
+/***
+  This file is part of systemd.
+
+  Copyright 2010 ProFUSION embedded systems
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+void umount_init(void);
+
+#endif
-- 
1.7.3



More information about the systemd-devel mailing list