diff --git a/src/tools/vfs/vfs_main.c b/src/tools/vfs/vfs_main.c index 27d6b604999..e879dfdd697 100644 --- a/src/tools/vfs/vfs_main.c +++ b/src/tools/vfs/vfs_main.c @@ -10,7 +10,9 @@ #include "vfs_daemon.h" +#include #include +#include #include #include #include @@ -151,10 +153,17 @@ static const char *vfs_get_process_name(int pid, char *buf, size_t max_length) int vfs_mount(int pid) { - char mountpoint[PATH_MAX]; char mountopts[1024]; char name[NAME_MAX]; + char *mountpoint; int fuse_fd, ret; + ucs_status_t status; + + status = ucs_string_alloc_path_buffer(&mountpoint, "mountpoint"); + if (status != UCS_OK) { + ret = -ENOMEM; + goto out; + } /* Add common mount options: * - File system name (source) : process name and pid @@ -168,39 +177,53 @@ int vfs_mount(int pid) vfs_get_process_name(pid, name, sizeof(name)), (strlen(g_opts.mount_opts) > 0) ? "," : "", g_opts.mount_opts); if (ret >= sizeof(mountopts)) { - return -ENOMEM; + ret = -ENOMEM; + goto out_free_mountpoint; } /* Create the mount point directory, and ignore "already exists" error */ - vfs_get_mountpoint(pid, mountpoint, sizeof(mountpoint)); + vfs_get_mountpoint(pid, mountpoint, PATH_MAX); ret = mkdir(mountpoint, S_IRWXU); if ((ret < 0) && (errno != EEXIST)) { ret = -errno; vfs_error("failed to create directory '%s': %m", mountpoint); - return ret; + goto out_free_mountpoint; } /* Mount a new FUSE filesystem in the mount point directory */ vfs_log("mounting directory '%s' with options '%s'", mountpoint, mountopts); fuse_fd = fuse_open_channel(mountpoint, mountopts); if (fuse_fd < 0) { + ret = fuse_fd; vfs_error("fuse_open_channel(%s,opts=%s) failed: %m", mountpoint, mountopts); - return fuse_fd; + goto out_free_mountpoint; } vfs_log("mounted directory '%s' with fd %d", mountpoint, fuse_fd); - return fuse_fd; + ret = fuse_fd; + +out_free_mountpoint: + ucs_free(mountpoint); +out: + return ret; } int vfs_unmount(int pid) { - char mountpoint[PATH_MAX]; + char *mountpoint; char *argv[5]; int ret; + ucs_status_t status; + + status = ucs_string_alloc_path_buffer(&mountpoint, "mountpoint"); + if (status != UCS_OK) { + ret = -ENOMEM; + goto out; + } /* Unmount FUSE file system */ - vfs_get_mountpoint(pid, mountpoint, sizeof(mountpoint)); + vfs_get_mountpoint(pid, mountpoint, PATH_MAX); argv[0] = "-u"; argv[1] = "-z"; argv[2] = "--"; @@ -208,7 +231,7 @@ int vfs_unmount(int pid) argv[4] = NULL; ret = vfs_run_fusermount(argv); if (ret < 0) { - return ret; + goto out_free_mountpoint; } /* Remove mount point directory */ @@ -216,10 +239,13 @@ int vfs_unmount(int pid) ret = rmdir(mountpoint); if (ret < 0) { vfs_error("failed to remove directory '%s': %m", mountpoint); - return ret; + goto out_free_mountpoint; } - return 0; +out_free_mountpoint: + ucs_free(mountpoint); +out: + return ret; } static int vfs_unlink_socket(int silent_notexist) diff --git a/src/ucp/proto/proto_debug.c b/src/ucp/proto/proto_debug.c index 75b09319d05..68e249d90e6 100644 --- a/src/ucp/proto/proto_debug.c +++ b/src/ucp/proto/proto_debug.c @@ -984,24 +984,30 @@ ucp_proto_select_write_info(ucp_worker_h worker, ucp_proto_query_attr_t proto_attr; size_t range_start, range_end; ucs_string_buffer_t dot_strb; - char dir_path[PATH_MAX]; + ucs_status_t status; + char *dir_path; FILE *fp; int ret; + status = ucs_string_alloc_path_buffer(&dir_path, "dir_path"); + if (status != UCS_OK) { + goto out; + } + ucp_proto_select_param_dump(worker, ep_cfg_index, rkey_cfg_index, select_param, ucp_operation_names, &ep_cfg_strb, &sel_param_strb); if (!ucp_proto_debug_is_info_enabled( worker->context, ucs_string_buffer_cstr(&sel_param_strb))) { - return; + goto out_free_dir_path; } ucs_fill_filename_template(worker->context->config.ext.proto_info_dir, - dir_path, sizeof(dir_path)); + dir_path, PATH_MAX); ret = mkdir(dir_path, S_IRWXU | S_IRGRP | S_IXGRP); if ((ret != 0) && (errno != EEXIST)) { ucs_debug("failed to create directory %s: %m", dir_path); - return; + goto out_free_dir_path; } ucs_string_buffer_translate(&ep_cfg_strb, ucp_proto_debug_fix_filename); @@ -1040,6 +1046,11 @@ ucp_proto_select_write_info(ucp_worker_h worker, fclose(fp); } while (range_end != SIZE_MAX); + +out_free_dir_path: + ucs_free(dir_path); +out: + return; } void ucp_proto_select_elem_trace(ucp_worker_h worker, diff --git a/src/ucs/config/parser.c b/src/ucs/config/parser.c index f3d452e6809..5d4c20edce5 100644 --- a/src/ucs/config/parser.c +++ b/src/ucs/config/parser.c @@ -1614,15 +1614,21 @@ void ucs_config_parse_config_file(const char *dir_path, const char *file_name, .section_info = {.name = "", .skip = 0} }; - char file_path[MAXPATHLEN]; + char *file_path; int parse_result; FILE* file; + ucs_status_t status; + + status = ucs_string_alloc_path_buffer(&file_path, "file_path"); + if (status != UCS_OK) { + goto out; + } ucs_snprintf_safe(file_path, MAXPATHLEN, "%s/%s", dir_path, file_name); file = fopen(file_path, "r"); if (file == NULL) { ucs_debug("failed to open config file %s: %m", file_path); - return; + goto out_free_file_path; } parse_result = ini_parse_file(file, ucs_config_parse_config_file_line, @@ -1633,6 +1639,11 @@ void ucs_config_parse_config_file(const char *dir_path, const char *file_name, ucs_debug("parsed config file %s", file_path); fclose(file); + +out_free_file_path: + ucs_free(file_path); +out: + return; } static ucs_status_t @@ -1750,8 +1761,14 @@ static ucs_status_t ucs_config_parser_get_sub_prefix(const char *env_prefix, void ucs_config_parse_config_files() { + char *path; const char *path_p; - char path[PATH_MAX]; + ucs_status_t status; + + status = ucs_string_alloc_path_buffer(&path, "path"); + if (status != UCS_OK) { + goto out; + } /* System-wide configuration file */ ucs_config_parse_config_file(UCX_CONFIG_DIR, UCX_CONFIG_FILE_NAME, 1); @@ -1778,6 +1795,11 @@ void ucs_config_parse_config_files() /* Current working dir */ ucs_config_parse_config_file(".", UCX_CONFIG_FILE_NAME, 1); + +out_free_path: + ucs_free(path); +out: + return; } ucs_status_t diff --git a/src/ucs/debug/log.c b/src/ucs/debug/log.c index 110c1576115..8da585db7f1 100644 --- a/src/ucs/debug/log.c +++ b/src/ucs/debug/log.c @@ -145,15 +145,27 @@ static void ucs_log_get_file_name(char *log_file_name, size_t max, int idx) static void ucs_log_file_rotate() { - char old_log_file_name[PATH_MAX]; - char new_log_file_name[PATH_MAX]; + char *old_log_file_name, *new_log_file_name; int idx, ret; + ucs_status_t status; + + status = ucs_string_alloc_path_buffer(&old_log_file_name, + "old_log_file_name"); + if (status != UCS_OK) { + goto out; + } + + status = ucs_string_alloc_path_buffer(&new_log_file_name, + "new_log_file_name"); + if (status != UCS_OK) { + goto out_free_old_log_file_name; + } if (ucs_log_file_last_idx == ucs_global_opts.log_file_rotate) { /* remove the last file and log rotation from the * `log_file_rotate - 1` file */ ucs_log_get_file_name(old_log_file_name, - sizeof(old_log_file_name), + PATH_MAX, ucs_log_file_last_idx); unlink(old_log_file_name); } else { @@ -164,9 +176,9 @@ static void ucs_log_file_rotate() for (idx = ucs_log_file_last_idx - 1; idx >= 0; --idx) { ucs_log_get_file_name(old_log_file_name, - sizeof(old_log_file_name), idx); + PATH_MAX, idx); ucs_log_get_file_name(new_log_file_name, - sizeof(new_log_file_name), idx + 1); + PATH_MAX, idx + 1); if (access(old_log_file_name, W_OK) != 0) { ucs_fatal("unable to write to %s", old_log_file_name); @@ -188,6 +200,13 @@ static void ucs_log_file_rotate() ucs_fatal("unable to write to %s", new_log_file_name); } } + +out_free_new_log_file_name: + ucs_free(new_log_file_name); +out_free_old_log_file_name: + ucs_free(old_log_file_name); +out: + return; } static void ucs_log_handle_file_max_size(int log_entry_len) diff --git a/src/ucs/debug/memtrack.c b/src/ucs/debug/memtrack.c index 973a3e44e1b..d39f562a7d5 100644 --- a/src/ucs/debug/memtrack.c +++ b/src/ucs/debug/memtrack.c @@ -433,10 +433,10 @@ void ucs_memtrack_init() return; } + ucs_memtrack_vfs_init(); + ucs_debug("memtrack enabled"); ucs_memtrack_context.enabled = 1; - - ucs_memtrack_vfs_init(); } void ucs_memtrack_cleanup() @@ -447,12 +447,11 @@ void ucs_memtrack_cleanup() return; } - ucs_vfs_obj_remove(&ucs_memtrack_context); - ucs_memtrack_generate_report(); - /* disable before releasing the stats node */ + /* disable before releasing the vfs object and the stats node */ ucs_memtrack_context.enabled = 0; + ucs_vfs_obj_remove(&ucs_memtrack_context); UCS_STATS_NODE_FREE(ucs_memtrack_context.stats); /* cleanup entries */ diff --git a/src/ucs/memory/numa.c b/src/ucs/memory/numa.c index 6ce4f3aa4b0..41c6d376f4f 100644 --- a/src/ucs/memory/numa.c +++ b/src/ucs/memory/numa.c @@ -125,16 +125,25 @@ ucs_numa_node_t ucs_numa_node_of_cpu(int cpu) { /* Used for caching to improve performance */ static ucs_numa_node_t cpu_numa_node[__CPU_SETSIZE] = {0}; + char *core_dir_path; ucs_numa_node_t node; - char core_dir_path[PATH_MAX]; + ucs_status_t status; ucs_assert(cpu < __CPU_SETSIZE); if (cpu_numa_node[cpu] == 0) { + status = ucs_string_alloc_path_buffer(&core_dir_path, "core_dir_path"); + if (status != UCS_OK) { + return UCS_NUMA_NODE_UNDEFINED; + } + ucs_snprintf_safe(core_dir_path, PATH_MAX, UCS_NUMA_CORE_DIR_PATH, cpu); - node = ucs_numa_get_max_dirent(core_dir_path, "node", - ucs_numa_num_configured_nodes(), - UCS_NUMA_NODE_DEFAULT); + node = ucs_numa_get_max_dirent(core_dir_path, "node", + ucs_numa_num_configured_nodes(), + UCS_NUMA_NODE_DEFAULT); + + ucs_free(core_dir_path); + cpu_numa_node[cpu] = node + 1; } diff --git a/src/ucs/sys/module.c b/src/ucs/sys/module.c index a4e6e742983..a52d36c91ec 100644 --- a/src/ucs/sys/module.c +++ b/src/ucs/sys/module.c @@ -175,17 +175,26 @@ static void ucs_module_init(const char *module_path, void *dl) const char *module_init_name = UCS_PP_MAKE_STRING(UCS_MODULE_CONSTRUCTOR_NAME); - char *fullpath, buffer[PATH_MAX]; + char *fullpath, *buffer; init_func_t init_func; ucs_status_t status; + status = ucs_string_alloc_path_buffer(&buffer, "buffer"); + if (status != UCS_OK) { + goto out; + } + fullpath = realpath(module_path, buffer); + if (fullpath == NULL) { + goto out_free_buffer; + } + ucs_module_trace("loaded %s [%p]", fullpath, dl); init_func = (init_func_t)ucs_module_dlsym_shallow(module_path, dl, module_init_name); if (init_func == NULL) { - return; + goto out_free_buffer; } ucs_module_trace("calling '%s' in '%s': [%p]", module_init_name, fullpath, @@ -196,6 +205,11 @@ static void ucs_module_init(const char *module_path, void *dl) ucs_status_string(status)); dlclose(dl); } + +out_free_buffer: + ucs_free(buffer); +out: + return; } @@ -217,16 +231,22 @@ static int ucs_module_is_enabled(const char *module_name) static void ucs_module_load_one(const char *framework, const char *module_name, unsigned flags) { - char module_path[PATH_MAX] = {0}; + char *module_path; const char *error; unsigned i; void *dl; int mode; + ucs_status_t status; + + status = ucs_string_alloc_path_buffer(&module_path, "module_path"); + if (status != UCS_OK) { + goto out; + } if (!ucs_module_is_enabled(module_name)) { ucs_module_trace("module '%s' is disabled by configuration", module_name); - return; + goto out_free_module_path; } mode = RTLD_LAZY; @@ -242,7 +262,7 @@ static void ucs_module_load_one(const char *framework, const char *module_name, ucs_module_trace("loading module '%s' with mode 0x%x", module_name, mode); for (i = 0; i < ucs_module_loader_state.srchpath_cnt; ++i) { - snprintf(module_path, sizeof(module_path) - 1, "%s/lib%s_%s%s", + snprintf(module_path, PATH_MAX, "%s/lib%s_%s%s", ucs_module_loader_state.srch_path[i], framework, module_name, ucs_module_loader_state.module_ext); @@ -251,7 +271,7 @@ static void ucs_module_load_one(const char *framework, const char *module_name, dl = dlopen(module_path, mode); if (dl != NULL) { ucs_module_init(module_path, dl); - break; + goto out_free_module_path; } else { /* If a module fails to load, silently give up */ error = dlerror(); @@ -260,6 +280,10 @@ static void ucs_module_load_one(const char *framework, const char *module_name, } } +out_free_module_path: + ucs_free(module_path); +out: + return; /* coverity[leaked_storage] : a loaded module is never unloaded */ } #endif /* UCX_SHARED_LIB */ diff --git a/src/ucs/sys/sock.c b/src/ucs/sys/sock.c index a2a65b74987..56b4df76797 100644 --- a/src/ucs/sys/sock.c +++ b/src/ucs/sys/sock.c @@ -170,15 +170,23 @@ int ucs_netif_is_active(const char *if_name, sa_family_t af) unsigned ucs_netif_bond_ad_num_ports(const char *bond_name) { + char *lowest_path_buf; ucs_status_t status; long ad_num_ports; const char *bond_path; - char lowest_path_buf[PATH_MAX]; + unsigned ret; + + status = ucs_string_alloc_path_buffer(&lowest_path_buf, "lowest_path_buf"); + if (status != UCS_OK) { + ret = 1; + goto out_free_lowest_path_buf; + } status = ucs_netif_get_lowest_device_path(bond_name, lowest_path_buf, PATH_MAX); if (status != UCS_OK) { - return 1; + ret = 1; + goto out_free_lowest_path_buf; } bond_path = ucs_dirname(lowest_path_buf, 1); @@ -189,10 +197,16 @@ unsigned ucs_netif_bond_ad_num_ports(const char *bond_name) ucs_trace("failed to read from %s/%s: %m, " "assuming 802.3ad bonding is disabled", bond_path, UCS_BOND_NUM_PORTS_FILE); - return 1; + ret = 1; + goto out_free_lowest_path_buf; } - return (unsigned)ad_num_ports; + ret = (unsigned)ad_num_ports; + +out_free_lowest_path_buf: + ucs_free(lowest_path_buf); +out: + return ret; } ucs_status_t ucs_socket_create(int domain, int type, int *fd_p) diff --git a/src/ucs/vfs/base/vfs_obj.c b/src/ucs/vfs/base/vfs_obj.c index 2a089411930..ed526ffd343 100644 --- a/src/ucs/vfs/base/vfs_obj.c +++ b/src/ucs/vfs/base/vfs_obj.c @@ -223,17 +223,28 @@ static void ucs_vfs_node_build_path(ucs_vfs_node_t *parent_node, static ucs_vfs_node_t * ucs_vfs_node_add_subdir(ucs_vfs_node_t *parent_node, const char *name) { - char path_buf[PATH_MAX]; - ucs_vfs_node_t *node; + ucs_vfs_node_t *node = NULL; + char *path_buf; + ucs_status_t status; + + status = ucs_string_alloc_path_buffer(&path_buf, "path_buf"); + if (status != UCS_OK) { + goto out; + } - ucs_vfs_node_build_path(parent_node, name, path_buf, sizeof(path_buf)); + ucs_vfs_node_build_path(parent_node, name, path_buf, PATH_MAX); node = ucs_vfs_node_find_by_path(path_buf); if (node != NULL) { - return node; + goto out_free_path_buf; } - return ucs_vfs_node_create(parent_node, path_buf, UCS_VFS_NODE_TYPE_SUBDIR, + node = ucs_vfs_node_create(parent_node, path_buf, UCS_VFS_NODE_TYPE_SUBDIR, NULL); + +out_free_path_buf: + ucs_free(path_buf); +out: + return node; } static int ucs_vfs_node_need_update_path(ucs_vfs_node_type_t type, @@ -258,18 +269,28 @@ static ucs_status_t ucs_vfs_node_add(void *parent_obj, ucs_vfs_node_type_t type, void *obj, const char *rel_path, va_list ap, ucs_vfs_node_t **new_node) { + char *rel_path_buf, *abs_path_buf, *token, *next_token; ucs_vfs_node_t *current_node; - char rel_path_buf[PATH_MAX]; - char abs_path_buf[PATH_MAX]; - char *token, *next_token; + ucs_status_t status; + + status = ucs_string_alloc_path_buffer(&rel_path_buf, "rel_path_buf"); + if (status != UCS_OK) { + goto out; + } + + status = ucs_string_alloc_path_buffer(&abs_path_buf, "abs_path_buf"); + if (status != UCS_OK) { + goto out_free_rel_path_buf; + } current_node = ucs_vfs_node_get_by_obj(parent_obj); if (current_node == NULL) { - return UCS_ERR_INVALID_PARAM; + status = UCS_ERR_INVALID_PARAM; + goto out_free_abs_path_buf; } /* generate the relative path */ - ucs_vsnprintf_safe(rel_path_buf, sizeof(rel_path_buf), rel_path, ap); + ucs_vsnprintf_safe(rel_path_buf, PATH_MAX, rel_path, ap); /* Build parent nodes along the rel_path, without associated object */ next_token = rel_path_buf; @@ -277,31 +298,39 @@ static ucs_status_t ucs_vfs_node_add(void *parent_obj, ucs_vfs_node_type_t type, while (next_token != NULL) { current_node = ucs_vfs_node_add_subdir(current_node, token); if (current_node == NULL) { - return UCS_ERR_NO_MEMORY; + status = UCS_ERR_NO_MEMORY; + goto out_free_abs_path_buf; } token = strsep(&next_token, "/"); } ucs_vfs_node_build_path(current_node, token, abs_path_buf, - sizeof(abs_path_buf)); + PATH_MAX); if (ucs_vfs_node_need_update_path(type, abs_path_buf, obj)) { - ucs_vfs_node_update_path(obj, abs_path_buf, sizeof(abs_path_buf)); + ucs_vfs_node_update_path(obj, abs_path_buf, PATH_MAX); } if (ucs_vfs_node_find_by_path(abs_path_buf) != NULL) { - return UCS_ERR_ALREADY_EXISTS; + status = UCS_ERR_ALREADY_EXISTS; + goto out_free_abs_path_buf; } current_node = ucs_vfs_node_create(current_node, abs_path_buf, type, obj); if (current_node == NULL) { - return UCS_ERR_NO_MEMORY; + status = UCS_ERR_NO_MEMORY; + goto out_free_abs_path_buf; } *new_node = current_node; - return UCS_OK; +out_free_abs_path_buf: + ucs_free(abs_path_buf); +out_free_rel_path_buf: + ucs_free(rel_path_buf); +out: + return status; } /* must be called with lock held */ diff --git a/src/ucs/vfs/fuse/vfs_fuse.c b/src/ucs/vfs/fuse/vfs_fuse.c index a2c684d919f..8d60ee11d7f 100644 --- a/src/ucs/vfs/fuse/vfs_fuse.c +++ b/src/ucs/vfs/fuse/vfs_fuse.c @@ -242,16 +242,21 @@ static void ucs_vfs_fuse_main() static ucs_status_t ucs_vfs_fuse_wait_for_path(const char *path) { #ifdef HAVE_INOTIFY + char *dir_buf; char event_buf[sizeof(struct inotify_event) + NAME_MAX]; const struct inotify_event *event; char watch_filename[NAME_MAX]; const char *watch_dirname; - char dir_buf[PATH_MAX]; ucs_status_t status; ssize_t nread; size_t offset; int ret; + status = ucs_string_alloc_path_buffer(&dir_buf, "dir_buf"); + if (status != UCS_OK) { + goto out; + } + pthread_mutex_lock(&ucs_vfs_fuse_context.mutex); /* Check 'stop' flag before entering the loop. If the main thread sets @@ -261,14 +266,14 @@ static ucs_status_t ucs_vfs_fuse_wait_for_path(const char *path) */ if (ucs_vfs_fuse_context.stop) { status = UCS_ERR_CANCELED; - goto out; + goto out_unlock; } /* Create directory path */ ret = ucs_vfs_sock_mkdir(path, UCS_LOG_LEVEL_DIAG); if (ret != 0) { status = UCS_ERR_IO_ERROR; - goto out; + goto out_unlock; } /* Create inotify channel */ @@ -283,11 +288,11 @@ static ucs_status_t ucs_vfs_fuse_wait_for_path(const char *path) ucs_error("inotify_init() failed: %m"); } status = UCS_ERR_IO_ERROR; - goto out; + goto out_unlock; } /* copy path components to 'dir_buf' and 'watch_filename' */ - ucs_strncpy_safe(dir_buf, path, sizeof(dir_buf)); + ucs_strncpy_safe(dir_buf, path, PATH_MAX); ucs_strncpy_safe(watch_filename, ucs_basename(path), sizeof(watch_filename)); watch_dirname = dirname(dir_buf); @@ -366,8 +371,11 @@ static ucs_status_t ucs_vfs_fuse_wait_for_path(const char *path) out_close_inotify_fd: close(ucs_vfs_fuse_context.inotify_fd); ucs_vfs_fuse_context.inotify_fd = -1; -out: +out_unlock: pthread_mutex_unlock(&ucs_vfs_fuse_context.mutex); +out_free_dir_buf: + ucs_free(dir_buf); +out: return status; #else return UCS_ERR_UNSUPPORTED; diff --git a/src/ucs/vfs/sock/vfs_sock.c b/src/ucs/vfs/sock/vfs_sock.c index 189029ccfd9..614e6dbd9fa 100644 --- a/src/ucs/vfs/sock/vfs_sock.c +++ b/src/ucs/vfs/sock/vfs_sock.c @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -40,18 +41,29 @@ void ucs_vfs_sock_get_address(struct sockaddr_un *un_addr) int ucs_vfs_sock_mkdir(const char *sock_path, ucs_log_level_t log_level) { - char sock_path_dir[PATH_MAX]; + char *sock_path_dir; int ret; + ucs_status_t status; - ucs_strncpy_safe(sock_path_dir, sock_path, sizeof(sock_path_dir)); + status = ucs_string_alloc_path_buffer(&sock_path_dir, "sock_path_dir"); + if (status != UCS_OK) { + ret = -ENOMEM; + goto out; + } + + ucs_strncpy_safe(sock_path_dir, sock_path, PATH_MAX); ret = mkdir(dirname(sock_path_dir), S_IRWXU); if ((ret < 0) && (errno != EEXIST)) { ucs_log(log_level, "failed to create directory '%s': %m", sock_path_dir); - return -errno; + ret = -errno; + goto out_free_sock_path_dir; } - return 0; +out_free_sock_path_dir: + ucs_free(sock_path_dir); +out: + return ret; } int ucs_vfs_sock_setopt_passcred(int sockfd) diff --git a/src/uct/ib/base/ib_device.c b/src/uct/ib/base/ib_device.c index d88918e4fe6..8e68025117b 100644 --- a/src/uct/ib/base/ib_device.c +++ b/src/uct/ib/base/ib_device.c @@ -504,18 +504,23 @@ ucs_status_t uct_ib_device_query(uct_ib_device_t *dev, const char *dev_name = uct_ib_device_name(dev); const char *dev_path = dev->ibv_context->device->ibdev_path; const unsigned sys_device_priority = 20; - char path_buffer[PATH_MAX]; - const char *sysfs_path; ucs_status_t status; + char *path_buffer; + const char *sysfs_path; uint8_t i; int ret; + status = ucs_string_alloc_path_buffer(&path_buffer, "path_buffer"); + if (status != UCS_OK) { + goto out; + } + status = uct_ib_query_device(dev->ibv_context, &dev->dev_attr); if (status != UCS_OK) { - return status; + goto out_free_path_buffer; } - /* Check device type*/ + /* Check device type */ switch (ibv_device->node_type) { case IBV_NODE_SWITCH: dev->first_port = 0; @@ -540,7 +545,8 @@ ucs_status_t uct_ib_device_query(uct_ib_device_t *dev, &dev->port_attr[i]); if (ret != 0) { ucs_error("ibv_query_port() returned %d: %m", ret); - return UCS_ERR_IO_ERROR; + status = UCS_ERR_IO_ERROR; + goto out_free_path_buffer; } } @@ -550,7 +556,10 @@ ucs_status_t uct_ib_device_query(uct_ib_device_t *dev, uct_ib_device_set_pci_id(dev, sysfs_path); dev->pci_bw = ucs_topo_get_pci_bw(dev_name, sysfs_path); - return UCS_OK; +out_free_path_buffer: + ucs_free(path_buffer); +out: + return status; } ucs_status_t uct_ib_device_init(uct_ib_device_t *dev, diff --git a/src/uct/sm/mm/posix/mm_posix.c b/src/uct/sm/mm/posix/mm_posix.c index ba6de3c4bd6..d5614c8d11b 100644 --- a/src/uct/sm/mm/posix/mm_posix.c +++ b/src/uct/sm/mm/posix/mm_posix.c @@ -255,24 +255,44 @@ static ucs_status_t uct_posix_shm_open(uint64_t mmid, int open_flags, int *fd_p) static ucs_status_t uct_posix_file_open(const char *dir, uint64_t mmid, int open_flags, int* fd_p) { - char file_path[PATH_MAX]; + char *file_path; int ret; + ucs_status_t status; - ucs_snprintf_safe(file_path, sizeof(file_path), "%s" UCT_POSIX_FILE_FMT, - dir, mmid); + status = ucs_string_alloc_path_buffer(&file_path, "file_path"); + if (status != UCS_OK) { + goto out; + } + + ucs_snprintf_safe(file_path, PATH_MAX, "%s" UCT_POSIX_FILE_FMT, dir, mmid); ret = open(file_path, open_flags | O_RDWR, UCT_POSIX_SHM_OPEN_MODE); - return uct_posix_open_check_result("open", file_path, open_flags, ret, fd_p); + status = uct_posix_open_check_result("open", file_path, open_flags, ret, fd_p); + +out_free_file_path: + ucs_free(file_path); +out: + return status; } static ucs_status_t uct_posix_procfs_open(int pid, int peer_fd, int* fd_p) { - char file_path[PATH_MAX]; + char *file_path; int ret; + ucs_status_t status; + + status = ucs_string_alloc_path_buffer(&file_path, "file_path"); + if (status != UCS_OK) { + goto out; + } - ucs_snprintf_safe(file_path, sizeof(file_path), UCT_POSIX_PROCFS_FILE_FMT, - pid, peer_fd); + ucs_snprintf_safe(file_path, PATH_MAX, UCT_POSIX_PROCFS_FILE_FMT, pid, peer_fd); ret = open(file_path, O_RDWR, UCT_POSIX_SHM_OPEN_MODE); - return uct_posix_open_check_result("open", file_path, 0, ret, fd_p); + status = uct_posix_open_check_result("open", file_path, 0, ret, fd_p); + +out_free_file_path: + ucs_free(file_path); +out: + return status; } static ucs_status_t @@ -280,28 +300,40 @@ uct_posix_unlink(uct_mm_md_t *md, uint64_t seg_id, ucs_log_level_t err_level) { uct_posix_md_config_t *posix_config = ucs_derived_of(md->config, uct_posix_md_config_t); - char file_path[PATH_MAX]; + char *file_path; int ret; + ucs_status_t status; + + + status = ucs_string_alloc_path_buffer(&file_path, "file_path"); + if (status != UCS_OK) { + goto out; + } if (seg_id & UCT_POSIX_SEG_FLAG_SHM_OPEN) { - ucs_snprintf_safe(file_path, sizeof(file_path), UCT_POSIX_FILE_FMT, + ucs_snprintf_safe(file_path, PATH_MAX, UCT_POSIX_FILE_FMT, seg_id & UCT_POSIX_SEG_MMID_MASK); ret = shm_unlink(file_path); if (ret < 0) { ucs_log(err_level, "shm_unlink(%s) failed: %m", file_path); - return UCS_ERR_SHMEM_SEGMENT; + status = UCS_ERR_SHMEM_SEGMENT; + goto out_free_file_path; } } else { - ucs_snprintf_safe(file_path, sizeof(file_path), "%s" UCT_POSIX_FILE_FMT, + ucs_snprintf_safe(file_path, PATH_MAX, "%s" UCT_POSIX_FILE_FMT, posix_config->dir, seg_id & UCT_POSIX_SEG_MMID_MASK); ret = unlink(file_path); if (ret < 0) { ucs_error("unlink(%s) failed: %m", file_path); - return UCS_ERR_SHMEM_SEGMENT; + status = UCS_ERR_SHMEM_SEGMENT; + goto out_free_file_path; } } - return UCS_OK; +out_free_file_path: + ucs_free(file_path); +out: + return status; } static ucs_status_t diff --git a/src/uct/tcp/tcp_iface.c b/src/uct/tcp/tcp_iface.c index 38c31bfac81..5c1488ad640 100644 --- a/src/uct/tcp/tcp_iface.c +++ b/src/uct/tcp/tcp_iface.c @@ -244,9 +244,14 @@ uct_tcp_iface_is_reachable_v2(const uct_iface_h tl_iface, static const char * uct_tcp_iface_get_sysfs_path(const char *dev_name, char *path_buffer) { + const char *sysfs_path = NULL; ucs_status_t status; - const char *sysfs_path; - char lowest_path_buf[PATH_MAX]; + char *lowest_path_buf; + + status = ucs_string_alloc_path_buffer(&lowest_path_buf, "lowest_path_buf"); + if (status != UCS_OK) { + goto out; + } /* Deep search to find the lowest device sysfs path: * 1) For regular device, use regular sysfs form. @@ -255,11 +260,15 @@ uct_tcp_iface_get_sysfs_path(const char *dev_name, char *path_buffer) status = ucs_netif_get_lowest_device_path(dev_name, lowest_path_buf, PATH_MAX); if (status != UCS_OK) { - return NULL; + goto out_free_lowest_path_buf; } /* 'path_buffer' size is PATH_MAX */ sysfs_path = ucs_topo_resolve_sysfs_path(lowest_path_buf, path_buffer); + +out_free_lowest_path_buf: + ucs_free(lowest_path_buf); +out: return sysfs_path; } @@ -272,14 +281,19 @@ static ucs_status_t uct_tcp_iface_query(uct_iface_h tl_iface, ucs_status_t status; int is_default; double pci_bw, network_bw, calculated_bw; - char path_buffer[PATH_MAX]; + char *path_buffer; const char *sysfs_path; + status = ucs_string_alloc_path_buffer(&path_buffer, "path_buffer"); + if (status != UCS_OK) { + goto out; + } + uct_base_iface_query(&iface->super, attr); status = uct_tcp_netif_caps(iface->if_name, &attr->latency.c, &network_bw); if (status != UCS_OK) { - return status; + goto out_free_path_buffer; } sysfs_path = uct_tcp_iface_get_sysfs_path(iface->if_name, path_buffer); @@ -339,7 +353,7 @@ static ucs_status_t uct_tcp_iface_query(uct_iface_h tl_iface, if (iface->config.prefer_default) { status = uct_tcp_netif_is_default(iface->if_name, &is_default); if (status != UCS_OK) { - return status; + goto out_free_path_buffer; } attr->priority = is_default ? 0 : 1; @@ -347,7 +361,10 @@ static ucs_status_t uct_tcp_iface_query(uct_iface_h tl_iface, attr->priority = 0; } - return UCS_OK; +out_free_path_buffer: + ucs_free(path_buffer); +out: + return status; } static ucs_status_t uct_tcp_iface_event_fd_get(uct_iface_h tl_iface, int *fd_p) @@ -882,13 +899,26 @@ static UCS_CLASS_DEFINE_NEW_FUNC(uct_tcp_iface_t, uct_iface_t, uct_md_h, static int uct_tcp_is_bridge(const char *if_name) { - char path[PATH_MAX]; + char *path; + int ret; struct stat st; + ucs_status_t status; + + status = ucs_string_alloc_path_buffer(&path, "path"); + if (status != UCS_OK) { + ret = 0; + goto out; + } ucs_snprintf_safe(path, PATH_MAX, UCT_TCP_IFACE_NETDEV_DIR "/%s/bridge", if_name); - return (stat(path, &st) == 0) && S_ISDIR(st.st_mode); + ret = (stat(path, &st) == 0) && S_ISDIR(st.st_mode); + +out_free_path: + ucs_free(path); +out: + return ret; } ucs_status_t uct_tcp_query_devices(uct_md_h md, @@ -903,14 +933,20 @@ ucs_status_t uct_tcp_query_devices(uct_md_h md, int is_active, i, n; ucs_status_t status; const char *sysfs_path; - char path_buffer[PATH_MAX]; + char *path_buffer = ucs_malloc(PATH_MAX, "path_buffer"); ucs_sys_device_t sys_dev; + if (path_buffer == NULL) { + ucs_error("Failed to allocate memory for path buffer"); + status = UCS_ERR_NO_MEMORY; + goto out; + } + n = scandir(UCT_TCP_IFACE_NETDEV_DIR, &entries, NULL, alphasort); if (n == -1) { ucs_error("scandir(%s) failed: %m", UCT_TCP_IFACE_NETDEV_DIR); status = UCS_ERR_IO_ERROR; - goto out; + goto out_free_buffer; } devices = NULL; @@ -977,6 +1013,8 @@ ucs_status_t uct_tcp_query_devices(uct_md_h md, } free(entries); +out_free_buffer: + ucs_free(path_buffer); out: return status; } diff --git a/test/gtest/common/test_helpers.cc b/test/gtest/common/test_helpers.cc index c484be0b20b..4917533df45 100644 --- a/test/gtest/common/test_helpers.cc +++ b/test/gtest/common/test_helpers.cc @@ -456,12 +456,13 @@ bool is_inet_addr(const struct sockaddr* ifa_addr) { static bool netif_has_sysfs_file(const char *ifa_name, const char *file_name) { - char path[PATH_MAX]; - ucs_snprintf_safe(path, sizeof(path), "/sys/class/net/%s/%s", ifa_name, - file_name); + std::string path(PATH_MAX, '\0'); + + ucs_snprintf_safe(&path[0], path.size(), "/sys/class/net/%s/%s", + ifa_name, file_name); struct stat st; - return stat(path, &st) >= 0; + return stat(path.c_str(), &st) >= 0; } bool is_interface_usable(struct ifaddrs *ifa) diff --git a/test/gtest/ucp/test_ucp_worker.cc b/test/gtest/ucp/test_ucp_worker.cc index 6cf01e6d229..10a43143e08 100644 --- a/test/gtest/ucp/test_ucp_worker.cc +++ b/test/gtest/ucp/test_ucp_worker.cc @@ -903,17 +903,18 @@ UCS_TEST_P(test_pci_bw, get_pci_bw) { ucp_worker_h worker = sender().worker(); ucp_context_h ctx = worker->context; - char path_buffer[PATH_MAX]; + std::string path_buffer(PATH_MAX, '\0'); const ucp_worker_iface_t *wiface; for (auto i = 0; i < worker->num_ifaces; ++i) { - wiface = worker->ifaces[i]; - const auto dev_name = ctx->tl_rscs[wiface->rsc_index].tl_rsc.dev_name; - const auto tcp_device = get_tcp_device(dev_name); - const auto dev_path = !tcp_device.empty() ? tcp_device : - get_ib_device(dev_name); + wiface = worker->ifaces[i]; + const auto dev_name = ctx->tl_rscs[wiface->rsc_index].tl_rsc.dev_name; + const auto tcp_device = get_tcp_device(dev_name); + const auto dev_path = !tcp_device.empty() ? tcp_device : + get_ib_device(dev_name); + const char *sysfs_path = ucs_topo_resolve_sysfs_path(dev_path.c_str(), - path_buffer); + &path_buffer[0]); const double pci_bw = ucs_topo_get_pci_bw(dev_name, sysfs_path); uct_iface_attr_t attr; diff --git a/test/gtest/ucs/test_string.cc b/test/gtest/ucs/test_string.cc index b87a44fcc30..c9d8c01f0fe 100644 --- a/test/gtest/ucs/test_string.cc +++ b/test/gtest/ucs/test_string.cc @@ -64,9 +64,10 @@ UCS_TEST_F(test_string, common_prefix_len) { } UCS_TEST_F(test_string, path) { - char path[PATH_MAX]; - ucs_path_get_common_parent("/sys/dev/one", "/sys/dev/two", path); - EXPECT_STREQ("/sys/dev", path); + std::string path(PATH_MAX, '\0'); + + ucs_path_get_common_parent("/sys/dev/one", "/sys/dev/two", &path[0]); + EXPECT_STREQ("/sys/dev", path.c_str()); EXPECT_EQ(4, ucs_path_calc_distance("/root/foo/bar", "/root/charlie/fox")); EXPECT_EQ(2, ucs_path_calc_distance("/a/b/c/d", "/a/b/c/e"));