From 0494cae03d762eaf2fb7217ee7d70f615dcb5183 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Thu, 8 Mar 2018 17:22:58 +0100 Subject: [PATCH] umount: Add more asserts and remove some unused arguments --- src/core/umount.c | 54 +++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/core/umount.c b/src/core/umount.c index 8ba4179a0fb..fd478d91238 100644 --- a/src/core/umount.c +++ b/src/core/umount.c @@ -333,6 +333,8 @@ static int delete_loopback(const char *device) { _cleanup_close_ int fd = -1; int r; + assert(device); + fd = open(device, O_RDONLY|O_CLOEXEC); if (fd < 0) return errno == ENOENT ? 0 : -errno; @@ -382,12 +384,15 @@ static bool nonunmountable_path(const char *path) { || path_startswith(path, "/run/initramfs"); } -static int remount_with_timeout(MountPoint *m, char *options, int *n_failed) { +static int remount_with_timeout(MountPoint *m, char *options) { pid_t pid; int r; BLOCK_SIGNALS(SIGCHLD); + assert(m); + assert(options); + /* Due to the possiblity of a remount operation hanging, we * fork a child process and set a timeout. If the timeout * lapses, the assumption is that that particular remount @@ -418,12 +423,14 @@ static int remount_with_timeout(MountPoint *m, char *options, int *n_failed) { return r; } -static int umount_with_timeout(MountPoint *m, bool *changed) { +static int umount_with_timeout(MountPoint *m) { pid_t pid; int r; BLOCK_SIGNALS(SIGCHLD); + assert(m); + /* Due to the possiblity of a umount operation hanging, we * fork a child process and set a timeout. If the timeout * lapses, the assumption is that that particular umount @@ -468,6 +475,7 @@ static int mount_points_list_umount(MountPoint **head, bool *changed) { int n_failed = 0; assert(head); + assert(changed); LIST_FOREACH(mount_point, m, *head) { bool mount_is_readonly; @@ -514,7 +522,7 @@ static int mount_points_list_umount(MountPoint **head, bool *changed) { * Since the remount can hang in the instance of * remote filesystems, we remount asynchronously * and skip the subsequent umount if it fails */ - if (remount_with_timeout(m, options, &n_failed) < 0) { + if (remount_with_timeout(m, options) < 0) { if (nonunmountable_path(m->path)) n_failed++; continue; @@ -528,12 +536,10 @@ static int mount_points_list_umount(MountPoint **head, bool *changed) { continue; /* Trying to umount */ - if (umount_with_timeout(m, changed) < 0) + if (umount_with_timeout(m) < 0) n_failed++; - else { - if (changed) - *changed = true; - } + else + *changed = true; } return n_failed; @@ -544,13 +550,12 @@ static int swap_points_list_off(MountPoint **head, bool *changed) { int n_failed = 0; assert(head); + assert(changed); LIST_FOREACH_SAFE(mount_point, m, n, *head) { log_info("Deactivating swap %s.", m->path); if (swapoff(m->path) == 0) { - if (changed) - *changed = true; - + *changed = true; mount_point_free(head, m); } else { log_warning_errno(errno, "Could not deactivate swap %s: %m", m->path); @@ -567,6 +572,7 @@ static int loopback_points_list_detach(MountPoint **head, bool *changed) { struct stat root_st; assert(head); + assert(changed); k = lstat("/", &root_st); @@ -585,7 +591,7 @@ static int loopback_points_list_detach(MountPoint **head, bool *changed) { log_info("Detaching loopback %s.", m->path); r = delete_loopback(m->path); if (r >= 0) { - if (r > 0 && changed) + if (r > 0) *changed = true; mount_point_free(head, m); @@ -604,6 +610,7 @@ static int dm_points_list_detach(MountPoint **head, bool *changed) { dev_t rootdev; assert(head); + assert(changed); r = get_block_device("/", &rootdev); if (r <= 0) @@ -611,18 +618,15 @@ static int dm_points_list_detach(MountPoint **head, bool *changed) { LIST_FOREACH_SAFE(mount_point, m, n, *head) { - if (major(rootdev) != 0) - if (rootdev == m->devnum) { - n_failed ++; - continue; - } + if (major(rootdev) != 0 && rootdev == m->devnum) { + n_failed ++; + continue; + } log_info("Detaching DM %u:%u.", major(m->devnum), minor(m->devnum)); r = delete_dm(m->devnum); if (r >= 0) { - if (changed) - *changed = true; - + *changed = true; mount_point_free(head, m); } else { log_warning_errno(errno, "Could not detach DM %s: %m", m->path); @@ -637,6 +641,8 @@ static int umount_all_once(bool *changed) { int r; LIST_HEAD(MountPoint, mp_list_head); + assert(changed); + LIST_HEAD_INIT(mp_list_head); r = mount_points_list_get(&mp_list_head); if (r < 0) @@ -654,6 +660,8 @@ int umount_all(bool *changed) { bool umount_changed; int r; + assert(changed); + /* Retry umount, until nothing can be umounted anymore. Mounts are * processed in order, newest first. The retries are needed when * an old mount has been moved, to a path inside a newer mount. */ @@ -672,6 +680,8 @@ int swapoff_all(bool *changed) { int r; LIST_HEAD(MountPoint, swap_list_head); + assert(changed); + LIST_HEAD_INIT(swap_list_head); r = swap_list_get(&swap_list_head); @@ -690,6 +700,8 @@ int loopback_detach_all(bool *changed) { int r; LIST_HEAD(MountPoint, loopback_list_head); + assert(changed); + LIST_HEAD_INIT(loopback_list_head); r = loopback_list_get(&loopback_list_head); @@ -708,6 +720,8 @@ int dm_detach_all(bool *changed) { int r; LIST_HEAD(MountPoint, dm_list_head); + assert(changed); + LIST_HEAD_INIT(dm_list_head); r = dm_list_get(&dm_list_head);