X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fconfig%2Fsession-config.c;h=518fde70b5e63d1d244369ca634d0a647bb75f21;hp=a600a5b47fe72b1dcec9f807a73f2ef899ace7df;hb=ff86d8d0bd96d3660ad79aa6158fbeb9055652b2;hpb=d2e67842b4b31399cd581edcbf21fbf8bd2ca1f0 diff --git a/src/common/config/session-config.c b/src/common/config/session-config.c index a600a5b47..518fde70b 100644 --- a/src/common/config/session-config.c +++ b/src/common/config/session-config.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -2952,23 +2953,6 @@ end: return ret; } -/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */ -static -struct dirent *alloc_dirent(const char *path) -{ - size_t len; - long name_max; - struct dirent *entry; - - name_max = pathconf(path, _PC_NAME_MAX); - if (name_max == -1) { - name_max = PATH_MAX; - } - len = offsetof(struct dirent, d_name) + name_max + 1; - entry = zmalloc(len); - return entry; -} - static int load_session_from_path(const char *path, const char *session_name, struct session_config_validation_ctx *validation_ctx, int overwrite, @@ -2976,9 +2960,19 @@ int load_session_from_path(const char *path, const char *session_name, { int ret, session_found = !session_name; DIR *directory = NULL; + struct lttng_dynamic_buffer file_path; + size_t path_len; assert(path); assert(validation_ctx); + path_len = strlen(path); + lttng_dynamic_buffer_init(&file_path); + if (path_len >= LTTNG_PATH_MAX) { + ERR("Session configuration load path \"%s\" length (%zu) exceeds the maximal length allowed (%d)", + path, path_len, LTTNG_PATH_MAX); + ret = -LTTNG_ERR_INVALID; + goto end; + } directory = opendir(path); if (!directory) { @@ -2995,67 +2989,107 @@ int load_session_from_path(const char *path, const char *session_name, } } if (directory) { - struct dirent *entry; - struct dirent *result; - char *file_path = NULL; - size_t path_len = strlen(path); - - if (path_len >= PATH_MAX) { - ret = -LTTNG_ERR_INVALID; - goto end; - } + size_t file_path_root_len; - entry = alloc_dirent(path); - if (!entry) { + ret = lttng_dynamic_buffer_set_capacity(&file_path, + LTTNG_PATH_MAX); + if (ret) { ret = -LTTNG_ERR_NOMEM; goto end; } - file_path = zmalloc(PATH_MAX); - if (!file_path) { + ret = lttng_dynamic_buffer_append(&file_path, path, path_len); + if (ret) { ret = -LTTNG_ERR_NOMEM; - free(entry); goto end; } - strncpy(file_path, path, path_len); - if (file_path[path_len - 1] != '/') { - file_path[path_len++] = '/'; + if (file_path.data[file_path.size - 1] != '/') { + ret = lttng_dynamic_buffer_append(&file_path, "/", 1); + if (ret) { + ret = -LTTNG_ERR_NOMEM; + goto end; + } } + file_path_root_len = file_path.size; - ret = 0; /* Search for *.lttng files */ - while (!readdir_r(directory, entry, &result) && result) { - size_t file_name_len = strlen(result->d_name); + for (;;) { + size_t file_name_len; + struct dirent *result; + + /* + * When the end of the directory stream is reached, NULL + * is returned and errno is kept unchanged. When an + * error occurs, NULL is returned and errno is set + * accordingly. To distinguish between the two, set + * errno to zero before calling readdir(). + * + * On success, readdir() returns a pointer to a dirent + * structure. This structure may be statically + * allocated, do not attempt to free(3) it. + */ + errno = 0; + result = readdir(directory); + + /* Reached end of dir stream or error out. */ + if (!result) { + if (errno) { + PERROR("Failed to enumerate the contents of path \"%s\" while loading session, readdir returned", path); + ret = -LTTNG_ERR_LOAD_IO_FAIL; + goto end; + } + break; + } + + file_name_len = strlen(result->d_name); if (file_name_len <= sizeof(DEFAULT_SESSION_CONFIG_FILE_EXTENSION)) { continue; } - if (path_len + file_name_len >= PATH_MAX) { + if (file_path.size + file_name_len >= LTTNG_PATH_MAX) { + WARN("Ignoring file \"%s\" since the path's length (%zu) would exceed the maximal permitted size (%d)", + result->d_name, + /* +1 to account for NULL terminator. */ + file_path.size + file_name_len + 1, + LTTNG_PATH_MAX); continue; } + /* Does the file end with .lttng? */ if (strcmp(DEFAULT_SESSION_CONFIG_FILE_EXTENSION, - result->d_name + file_name_len - sizeof( - DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) { + result->d_name + file_name_len - sizeof( + DEFAULT_SESSION_CONFIG_FILE_EXTENSION) + 1)) { continue; } - strncpy(file_path + path_len, result->d_name, file_name_len); - file_path[path_len + file_name_len] = '\0'; + ret = lttng_dynamic_buffer_append(&file_path, result->d_name, + file_name_len + 1); + if (ret) { + ret = -LTTNG_ERR_NOMEM; + goto end; + } - ret = load_session_from_file(file_path, session_name, + ret = load_session_from_file(file_path.data, session_name, validation_ctx, overwrite, overrides); if (session_name && !ret) { session_found = 1; break; } + /* + * Reset the buffer's size to the location of the + * path's trailing '/'. + */ + ret = lttng_dynamic_buffer_set_size(&file_path, + file_path_root_len); + if (ret) { + ret = -LTTNG_ERR_UNK; + goto end; + } } - free(entry); - free(file_path); } else { ret = load_session_from_file(path, session_name, validation_ctx, overwrite, overrides); @@ -3072,11 +3106,10 @@ end: PERROR("closedir"); } } - if (session_found && !ret) { ret = 0; } - + lttng_dynamic_buffer_reset(&file_path); return ret; }