]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
ocp: fix to return c9 log page data error to open file
authorTokunori Ikegami <ikegami.t@gmail.com>
Sun, 1 Sep 2024 14:02:58 +0000 (23:02 +0900)
committerDaniel Wagner <wagi@monom.org>
Mon, 9 Sep 2024 07:12:56 +0000 (09:12 +0200)
Use _cleanup_fd_ function so delete close() call and goto statement.
Also fix some minor coding style errors around the code.

Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
plugins/ocp/ocp-nvme.c

index d6cac6f5a36270ee8bf9f8006a09dcb53b0f0ac3..51c9f67ba49e8cd54c036c76ede000d00c17ec03 100644 (file)
@@ -1588,13 +1588,15 @@ exit_status:
 
 static int get_c9_log_page_data(struct nvme_dev *dev, int print_data, int save_bin)
 {
-       int ret = 0, fd;
+       int ret = 0;
        __le64 stat_id_str_table_ofst = 0;
        __le64 event_str_table_ofst = 0;
        __le64 vu_event_str_table_ofst = 0;
        __le64 ascii_table_ofst = 0;
        char file_path[PATH_MAX];
 
+       _cleanup_fd_ int fd = STDIN_FILENO;
+
        header_data = (__u8 *)malloc(sizeof(__u8) * C9_TELEMETRY_STR_LOG_LEN);
        if (!header_data) {
                fprintf(stderr, "ERROR : OCP : malloc : %s\n", strerror(errno));
@@ -1642,26 +1644,24 @@ static int get_c9_log_page_data(struct nvme_dev *dev, int print_data, int save_b
 
                ret = nvme_get_log_simple(dev_fd(dev), C9_TELEMETRY_STRING_LOG_ENABLE_OPCODE,
                                          total_log_page_sz, pC9_string_buffer);
-       } else
+       } else {
                fprintf(stderr, "ERROR : OCP : Unable to read C9 data.\n");
+       }
 
        if (save_bin) {
                sprintf(file_path, DEFAULT_STRING_BIN);
                fd = open(file_path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
                if (fd < 0) {
-                       fprintf(stderr, "Failed to open output file %s: %s!\n",
-                               file_path, strerror(errno));
-                       goto exit_status;
+                       fprintf(stderr, "Failed to open output file %s: %s!\n", file_path,
+                               strerror(errno));
+                       return fd;
                }
 
                ret = write(fd, (void *)pC9_string_buffer, total_log_page_sz);
                if (ret != total_log_page_sz)
                        fprintf(stderr, "Failed to flush all data to file!\n");
-
-               close(fd);
        }
 
-exit_status:
        return 0;
 }