Skip to content

Commit f0f76aa

Browse files
committed
eject: use libmount context API for umount
libmount provides a context API that can be used to replace the fork+exec code pattern needed to leverage umount(8). This does not only simplify the code base but also removes the concern for signal handling due to a wait(2) call, avoids forking and allocating additional system resources, reduces security management and makes the code more consistent as other functions were already using some libmount functionalities. Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
1 parent 174f184 commit f0f76aa

1 file changed

Lines changed: 16 additions & 26 deletions

File tree

sys-utils/eject.c

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -654,41 +654,34 @@ static int eject_tape(int fd)
654654
return ioctl(fd, MTIOCTOP, &op) >= 0;
655655
}
656656

657-
658657
/* umount a device. */
659658
static void umount_one(const struct eject_control *ctl, const char *name)
660659
{
661-
int status;
660+
int rc;
661+
struct libmnt_context *mnt_ctx;
662662

663663
if (!name)
664-
return;
664+
errx(EXIT_FAILURE, _("missing device/partition path"));
665665

666666
verbose(ctl, _("%s: unmounting"), name);
667667

668-
switch (fork()) {
669-
case 0: /* child */
670-
if (drop_permissions() != 0)
671-
err(EXIT_FAILURE, _("drop permissions failed"));
672-
if (ctl->p_option)
673-
execl("/bin/umount", "/bin/umount", name, "-n", (char *)NULL);
674-
else
675-
execl("/bin/umount", "/bin/umount", name, (char *)NULL);
668+
mnt_ctx = mnt_new_context();
669+
if (!mnt_ctx)
670+
err(EXIT_FAILURE, _("libmount context allocation failed"));
676671

677-
errexec("/bin/umount");
672+
rc = mnt_context_set_target(mnt_ctx, name);
673+
if (rc)
674+
err(EXIT_FAILURE, _("failed to set libmount context target"));
678675

679-
case -1:
680-
warn( _("unable to fork"));
681-
break;
676+
if (ctl->p_option)
677+
mnt_context_disable_mtab(mnt_ctx, true);
682678

683-
default: /* parent */
684-
if (wait(&status) == -1 || WIFEXITED(status) == 0)
685-
errx(EXIT_FAILURE,
686-
_("unmount of `%s' did not exit normally"), name);
679+
rc = mnt_context_umount(mnt_ctx);
680+
rc = mnt_context_get_excode(mnt_ctx, rc, NULL, 0);
681+
if (rc != MNT_EX_SUCCESS)
682+
errx(EXIT_FAILURE, _("unmount of `%s' failed"), name);
687683

688-
if (WEXITSTATUS(status) != 0)
689-
errx(EXIT_FAILURE, _("unmount of `%s' failed"), name);
690-
break;
691-
}
684+
mnt_free_context(mnt_ctx);
692685
}
693686

694687
/* Open a device file. */
@@ -872,9 +865,6 @@ int main(int argc, char **argv)
872865
return EXIT_SUCCESS;
873866
}
874867

875-
/* clear any inherited settings */
876-
signal(SIGCHLD, SIG_DFL);
877-
878868
if (!ctl.device) {
879869
ctl.device = mnt_resolve_path(EJECT_DEFAULT_DEVICE, NULL);
880870
verbose(&ctl, _("using default device `%s'"), ctl.device);

0 commit comments

Comments
 (0)