diff --git a/source/tools/combine/iosdiag/hangdetect/main.c b/source/tools/combine/iosdiag/hangdetect/main.c index 08fd5e4eb3367e37e24bfa7a264400a199aa8d22..c157960c9be590c6b4fe609115c31c51f3339319 100644 --- a/source/tools/combine/iosdiag/hangdetect/main.c +++ b/source/tools/combine/iosdiag/hangdetect/main.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -144,11 +145,34 @@ static void put_rq_hang_info_buffer(struct rq_hang_info *buf) close(g_fd_rq_hang_detect); } +int create_directory(const char* path, mode_t mode) +{ + struct stat st; + if (stat(path, &st) == 0) { + if (S_ISDIR(st.st_mode) == 1) { + return 0; + } + else { + return -1; + } + } else { + char* path_copy = strdup(path); + + char* parent_path = dirname(path_copy); + + create_directory(parent_path, mode); + free(path_copy); + + if (mkdir(path, mode) == -1) { + return -1; + } + return 0; + } +} static int open_result_file(char *filename) { int fd; char buf[256]; - char cmd[272]; if (strlen(filename) > (sizeof(buf) - 1)) { printf("error: output file name(%s) too large(max %d bytes)\n", @@ -156,11 +180,15 @@ static int open_result_file(char *filename) return -1; } strcpy(buf, filename); - sprintf(cmd, "mkdir %s -p", dirname(buf)); - exec_shell_cmd(cmd); + char *dir = dirname(buf); + if (create_directory(dir, 0755) != 0 && errno != EEXIST) { + printf("error: create directory \"%s\" fail\n", dir); + return -1; + } + fd = open(filename, O_RDWR | O_CREAT, 0755); if (fd < 0) { - printf("error: create output file \"%s\" fail\n", filename); + printf("error: open output file \"%s\" fail\n", filename); return -1; } return fd; diff --git a/source/tools/combine/iosdiag/latency/collect.c b/source/tools/combine/iosdiag/latency/collect.c index 2d752eb24aff8c01b40778790d27d79115ce9344..6890f8c1bb76c4ccf3dfa22ea2ff753ecfc460db 100644 --- a/source/tools/combine/iosdiag/latency/collect.c +++ b/source/tools/combine/iosdiag/latency/collect.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -55,6 +56,31 @@ static int exec_shell_cmd(char *cmd) return 0; } +int create_directory(const char* path, mode_t mode) +{ + struct stat st; + if (stat(path, &st) == 0) { + if (S_ISDIR(st.st_mode) == 1) { + return 0; + } + else { + return -1; + } + } else { + char* path_copy = strdup(path); + + char* parent_path = dirname(path_copy); + + create_directory(parent_path, mode); + free(path_copy); + + if (mkdir(path, mode) == -1) { + return -1; + } + return 0; + } +} + static int over_threshold(struct iosdiag_req *iop) { unsigned long threshold_ns = get_threshold_us() * 1000; @@ -249,7 +275,6 @@ int iosdiag_init(char *devname) int iosdiag_run(int timeout, char *output_file) { char filepath[256]; - char cmd[272]; if (strlen(output_file) > (sizeof(filepath) - 1)) { printf("error: output file name(%s) too large(max %lu bytes)\n", @@ -257,11 +282,15 @@ int iosdiag_run(int timeout, char *output_file) return -1; } strcpy(filepath, output_file); - sprintf(cmd, "mkdir %s -p", dirname(filepath)); - exec_shell_cmd(cmd); + char *dir = dirname(filepath); + if (create_directory(dir, 0755) != 0 && errno != EEXIST) { + printf("error: create directory \"%s\" fail\n", dir); + return -1; + } + g_log_fd = open(output_file, O_RDWR | O_CREAT, 0755); if (g_log_fd < 0) { - printf("error: create output file \"%s\" fail\n", output_file); + printf("error: open output file \"%s\" fail\n", output_file); return -1; } signal(SIGINT, iosdiag_stop);