]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
eject: cleanup before successful exit
authorKarel Zak <kzak@redhat.com>
Mon, 1 Feb 2021 11:21:18 +0000 (12:21 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 1 Feb 2021 11:21:18 +0000 (12:21 +0100)
Fixes: https://github.com/karelzak/util-linux/issues/1239
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/eject.c

index e0f540e9d8751638500dc76ac6a1211560c090c1..e3039496336f0650e54b3ddcbae4c329f5627eb7 100644 (file)
@@ -850,7 +850,7 @@ int main(int argc, char **argv)
        char *disk = NULL;
        char *mountpoint = NULL;
        int worked = 0;    /* set to 1 when successfully ejected */
-       struct eject_control ctl = { NULL };
+       struct eject_control ctl = { .fd = -1 };
 
        setlocale(LC_ALL,"");
        bindtextdomain(PACKAGE, LOCALEDIR);
@@ -920,14 +920,14 @@ int main(int argc, char **argv)
        if (ctl.n_option) {
                info(_("device is `%s'"), ctl.device);
                verbose(&ctl, _("exiting due to -n/--noop option"));
-               return EXIT_SUCCESS;
+               goto done;
        }
 
        /* handle -i option */
        if (ctl.i_option) {
                open_device(&ctl);
                manual_eject(&ctl);
-               return EXIT_SUCCESS;
+               goto done;
        }
 
        /* handle -a option */
@@ -938,7 +938,7 @@ int main(int argc, char **argv)
                        verbose(&ctl, _("%s: disabling auto-eject mode"), ctl.device);
                open_device(&ctl);
                auto_eject(&ctl);
-               return EXIT_SUCCESS;
+               goto done;
        }
 
        /* handle -t option */
@@ -947,7 +947,7 @@ int main(int argc, char **argv)
                open_device(&ctl);
                close_tray(ctl.fd);
                set_device_speed(&ctl);
-               return EXIT_SUCCESS;
+               goto done;
        }
 
        /* handle -T option */
@@ -956,7 +956,7 @@ int main(int argc, char **argv)
                open_device(&ctl);
                toggle_tray(ctl.fd);
                set_device_speed(&ctl);
-               return EXIT_SUCCESS;
+               goto done;
        }
 
        /* handle -X option */
@@ -964,7 +964,7 @@ int main(int argc, char **argv)
                verbose(&ctl, _("%s: listing CD-ROM speed"), ctl.device);
                open_device(&ctl);
                list_speeds(&ctl);
-               return EXIT_SUCCESS;
+               goto done;
        }
 
        /* handle -x option only */
@@ -1002,7 +1002,7 @@ int main(int argc, char **argv)
                open_device(&ctl);
                changer_select(&ctl);
                set_device_speed(&ctl);
-               return EXIT_SUCCESS;
+               goto done;
        }
 
        /* if user did not specify type of eject, try all four methods */
@@ -1044,8 +1044,11 @@ int main(int argc, char **argv)
        if (!worked)
                errx(EXIT_FAILURE, _("unable to eject"));
 
+done:
        /* cleanup */
-       close(ctl.fd);
+       if (ctl.fd >= 0)
+               close(ctl.fd);
+
        free(ctl.device);
        free(mountpoint);