umount.c revision 643a14a5271ed19ec30a9882d4b9e9ae1c357fb1
/*-*- 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 <unistd.h>
#include <linux/dm-ioctl.h>
#include <libudev.h>
#include "list.h"
#include "mount-setup.h"
#include "umount.h"
#include "util.h"
typedef struct MountPoint {
char *path;
bool skip_ro;
} MountPoint;
assert(m);
free(m);
}
while (*head)
}
char *path, *p;
unsigned int i;
int r;
return -errno;
for (i = 1;; i++) {
int k;
MountPoint *m;
char *root;
bool skip_ro;
if ((k = fscanf(proc_self_mountinfo,
"%*s " /* (1) mount id */
"%*s " /* (2) parent id */
"%*s " /* (3) major:minor */
"%ms " /* (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 */
&root,
&path)) != 2) {
if (k == EOF)
break;
log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
continue;
}
/* If we encounter a bind mount, don't try to remount
* the source dir too early */
if (!p) {
r = -ENOMEM;
goto finish;
}
if (mount_point_is_api(p) || mount_point_ignore(p)) {
free(p);
continue;
}
free(p);
r = -ENOMEM;
goto finish;
}
m->path = p;
}
r = 0;
return r;
}
unsigned int i;
int r;
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) {
r = -ENOMEM;
goto finish;
}
free(d);
r = -ENOMEM;
goto finish;
}
}
r = 0;
return r;
}
int r;
struct udev_enumerate *e = NULL;
r = -ENOMEM;
goto finish;
}
if (!(e = udev_enumerate_new(udev))) {
r = -ENOMEM;
goto finish;
}
if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
udev_enumerate_add_match_sysname(e, "loop*") < 0) {
r = -EIO;
goto finish;
}
if (udev_enumerate_scan_devices(e) < 0) {
r = -EIO;
goto finish;
}
MountPoint *lb;
struct udev_device *d;
char *loop;
const char *dn;
r = -ENOMEM;
goto finish;
}
if (!(dn = udev_device_get_devnode(d))) {
continue;
}
if (!loop) {
r = -ENOMEM;
goto finish;
}
r = -ENOMEM;
goto finish;
}
}
r = 0;
if (e)
if (udev)
return r;
}
int r;
struct udev_enumerate *e = NULL;
r = -ENOMEM;
goto finish;
}
if (!(e = udev_enumerate_new(udev))) {
r = -ENOMEM;
goto finish;
}
if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
udev_enumerate_add_match_sysname(e, "dm-*") < 0) {
r = -EIO;
goto finish;
}
if (udev_enumerate_scan_devices(e) < 0) {
r = -EIO;
goto finish;
}
MountPoint *m;
struct udev_device *d;
char *node;
const char *dn;
r = -ENOMEM;
goto finish;
}
devnum = udev_device_get_devnum(d);
dn = udev_device_get_devnode(d);
continue;
}
if (!node) {
r = -ENOMEM;
goto finish;
}
r = -ENOMEM;
goto finish;
}
}
r = 0;
if (e)
if (udev)
return r;
}
static int delete_loopback(const char *device) {
int fd, r;
if (r >= 0)
return 1;
/* ENXIO: not bound, so no error */
return 0;
return -errno;
}
int fd, r;
return -errno;
return r >= 0 ? 0 : -errno;
}
MountPoint *m, *n;
int n_failed = 0;
#ifndef HAVE_SPLIT_USR
#endif
) {
n_failed++;
continue;
}
/* Trying to umount. Forcing to umount if busy (only for NFS mounts) */
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 (m->skip_ro) {
n_failed++;
continue;
}
/* Trying to remount read-only */
if (changed)
*changed = true;
mount_point_free(head, m);
} else {
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;
}
if ((r = delete_loopback(m->path)) >= 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 && 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;
}