]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: use cleanup helper to close file descriptor
authorDaniel Wagner <dwagner@suse.de>
Thu, 4 Jul 2024 16:43:54 +0000 (18:43 +0200)
committerDaniel Wagner <wagi@monom.org>
Tue, 9 Jul 2024 09:29:47 +0000 (11:29 +0200)
io_mgmt_send uses a file descriptor. Let's use the cleanup helper to
close it.

Signed-off-by: Daniel Wagner <dwagner@suse.de>
nvme.c

diff --git a/nvme.c b/nvme.c
index 0c18f4e1956b98dcfaf50e7e39aa731f95cb1c44..17d3fac678861ca862962360eccda47d308a6243 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -2096,7 +2096,7 @@ static int io_mgmt_send(int argc, char **argv, struct command *cmd, struct plugi
        _cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
        _cleanup_free_ void *buf = NULL;
        int err = -1;
-       int dfd = STDIN_FILENO;
+       _cleanup_fd_ int dfd = -1;
 
        struct config {
                __u16 mos;
@@ -2141,12 +2141,13 @@ static int io_mgmt_send(int argc, char **argv, struct command *cmd, struct plugi
                        nvme_show_perror(cfg.file);
                        return -errno;
                }
-       }
+       } else
+               dfd = dup(STDIN_FILENO);
 
        err = read(dfd, buf, cfg.data_len);
        if (err < 0) {
                nvme_show_perror("read");
-               goto close_fd;
+               return err;
        }
 
        struct nvme_io_mgmt_send_args args = {
@@ -2169,9 +2170,6 @@ static int io_mgmt_send(int argc, char **argv, struct command *cmd, struct plugi
        else
                nvme_show_perror("io-mgmt-send");
 
-close_fd:
-       if (cfg.file)
-               close(dfd);
        return err;
 }