umount.c revision 2eec67acbb00593e414549a7e5b35eb7dd776b1b
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 ProFUSION embedded systems
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <linux/dm-ioctl.h>
#include "list.h"
#include "mount-setup.h"
#include "umount.h"
#include "path-util.h"
#include "util.h"
#include "virt.h"
#include "libudev.h"
#include "udev-util.h"
typedef struct MountPoint {
char *path;
} MountPoint;
assert(m);
free(m);
}
while (*head)
}
unsigned int i;
if (!proc_self_mountinfo)
return -errno;
for (i = 1;; i++) {
char *p = NULL;
MountPoint *m;
int k;
"%*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) separator */
"%*s " /* (9) file system type */
"%*s" /* (10) mount source */
"%*s" /* (11) mount options 2 */
"%*[^\n]", /* some rubbish at the end */
&path);
if (k != 1) {
if (k == EOF)
break;
log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
continue;
}
if (!p)
return -ENOMEM;
/* Ignore mount points we can't unmount because they
* are API or because we are keeping them open (like
* file systems, since they are likely virtual too,
* and hence not worth spending time on. Also, in
* unprivileged containers we might lack the rights to
* unmount these things, hence don't bother. */
if (mount_point_is_api(p) ||
mount_point_ignore(p) ||
path_startswith(p, "/dev") ||
path_startswith(p, "/sys") ||
path_startswith(p, "/proc")) {
free(p);
continue;
}
if (!m) {
free(p);
return -ENOMEM;
}
m->path = p;
}
return 0;
}
unsigned int i;
for (i = 2;; i++) {
int k;
if ((k = fscanf(proc_swaps,
"%*s " /* type of swap */
"%*s " /* swap size */
"%*s " /* used */
"%*s\n", /* priority */
&dev)) != 1) {
if (k == EOF)
break;
log_warning("Failed to parse /proc/swaps:%u.", i);
continue;
}
continue;
}
if (!d) {
return -ENOMEM;
}
free(d);
return -ENOMEM;
}
}
return 0;
}
int r;
if (!udev)
return -ENOMEM;
e = udev_enumerate_new(udev);
if (!e)
return -ENOMEM;
r = udev_enumerate_add_match_subsystem(e, "block");
if (r < 0)
return r;
r = udev_enumerate_add_match_sysname(e, "loop*");
if (r < 0)
return r;
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
if (r < 0)
return r;
MountPoint *lb;
_cleanup_udev_device_unref_ struct udev_device *d;
char *loop;
const char *dn;
if (!d)
return -ENOMEM;
dn = udev_device_get_devnode(d);
if (!dn)
continue;
if (!loop)
return -ENOMEM;
if (!lb) {
return -ENOMEM;
}
}
return 0;
}
int r;
if (!udev)
return -ENOMEM;
e = udev_enumerate_new(udev);
if (!e)
return -ENOMEM;
r = udev_enumerate_add_match_subsystem(e, "block");
if (r < 0)
return r;
r = udev_enumerate_add_match_sysname(e, "dm-*");
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
if (r < 0)
return r;
MountPoint *m;
_cleanup_udev_device_unref_ struct udev_device *d;
char *node;
const char *dn;
if (!d)
return -ENOMEM;
devnum = udev_device_get_devnum(d);
dn = udev_device_get_devnode(d);
continue;
if (!node)
return -ENOMEM;
if (!m) {
return -ENOMEM;
}
}
return 0;
}
static int delete_loopback(const char *device) {
int r;
if (fd < 0)
if (r >= 0)
return 1;
/* ENXIO: not bound, so no error */
return 0;
return -errno;
}
int r;
.version = {DM_VERSION_MAJOR,
};
if (fd < 0)
return -errno;
return r >= 0 ? 0 : -errno;
}
MountPoint *m, *n;
int n_failed = 0;
/* If we are in a container, don't attempt to
read-only mount anything as that brings no real
benefits, but might confuse the host, as we remount
the superblock here, not the bind mound. */
if (detect_container(NULL) <= 0) {
/* We always try to remount directories
* read-only first, before we go on and umount
* them.
*
* Mount points can be stacked. If a mount
* point is stacked below / or /usr, we
* cannot umount or remount it directly,
* since there is no way to refer to the
* underlying mount. There's nothing we can do
* about it for the general case, but we can
* do something about it if it is aliased
* somehwere else via a bind mount. If we
* explicitly remount the super block of that
* alias read-only we hence should be
* relatively safe regarding keeping the fs we
* can otherwise not see dirty. */
}
/* Skip / and /usr since we cannot unmount that
* anyway, since we are running from it. They have
* already been remounted ro. */
#ifndef HAVE_SPLIT_USR
#endif
)
continue;
/* Trying to umount. We don't force here since we rely
* on busy NFS and FUSE file systems to return EBUSY
* until we closed everything on top of them. */
if (changed)
*changed = true;
mount_point_free(head, m);
} else if (log_error) {
n_failed++;
}
}
return n_failed;
}
MountPoint *m, *n;
int n_failed = 0;
if (changed)
*changed = true;
mount_point_free(head, m);
} else {
n_failed++;
}
}
return n_failed;
}
MountPoint *m, *n;
int n_failed = 0, k;
int r;
struct stat loopback_st;
if (k >= 0 &&
n_failed ++;
continue;
}
r = delete_loopback(m->path);
if (r >= 0) {
if (r > 0 && changed)
*changed = true;
mount_point_free(head, m);
} else {
n_failed++;
}
}
return n_failed;
}
MountPoint *m, *n;
int n_failed = 0, k;
int r;
if (k >= 0 &&
n_failed ++;
continue;
}
if (r >= 0) {
if (changed)
*changed = true;
mount_point_free(head, m);
} else {
n_failed++;
}
}
return n_failed;
}
int umount_all(bool *changed) {
int r;
bool umount_changed;
if (r < 0)
goto end;
/* retry umount, until nothing can be umounted anymore */
do {
umount_changed = false;
if (umount_changed)
*changed = true;
} while (umount_changed);
/* umount one more time with logging enabled */
if (r <= 0)
goto end;
end:
return r;
}
int swapoff_all(bool *changed) {
int r;
r = swap_list_get(&swap_list_head);
if (r < 0)
goto end;
end:
return r;
}
int loopback_detach_all(bool *changed) {
int r;
if (r < 0)
goto end;
end:
return r;
}
int dm_detach_all(bool *changed) {
int r;
r = dm_list_get(&dm_list_head);
if (r < 0)
goto end;
end:
return r;
}