X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=766f224c7e167118d30dbd99c6b32595b4fe99e4;hp=3428d312d6ee8a2c562936284bf86690229be56f;hb=6addfa379ee608b20cfe5e15d135bcb6a9724e90;hpb=e205d79b4dc22178644fa6f3d0a4eae86df36e73 diff --git a/src/common/utils.c b/src/common/utils.c index 3428d312d..766f224c7 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -32,9 +32,11 @@ #include #include #include +#include #include #include +#include #include "utils.h" #include "defaults.h" @@ -516,7 +518,7 @@ int utils_create_lock_file(const char *filepath) */ ret = flock(fd, LOCK_EX | LOCK_NB); if (ret) { - WARN("Could not get lock file %s, another instance is running.", + ERR("Could not get lock file %s, another instance is running.", filepath); if (close(fd)) { PERROR("close lock file"); @@ -885,11 +887,11 @@ char *utils_get_home_dir(void) char *val = NULL; struct passwd *pwd; - val = getenv(DEFAULT_LTTNG_HOME_ENV_VAR); + val = lttng_secure_getenv(DEFAULT_LTTNG_HOME_ENV_VAR); if (val != NULL) { goto end; } - val = getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); + val = lttng_secure_getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); if (val != NULL) { goto end; } @@ -954,7 +956,7 @@ end: LTTNG_HIDDEN char *utils_get_kmod_probes_list(void) { - return getenv(DEFAULT_LTTNG_KMOD_PROBES); + return lttng_secure_getenv(DEFAULT_LTTNG_KMOD_PROBES); } /* @@ -964,7 +966,7 @@ char *utils_get_kmod_probes_list(void) LTTNG_HIDDEN char *utils_get_extra_kmod_probes_list(void) { - return getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES); + return lttng_secure_getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES); } /* @@ -1048,12 +1050,77 @@ char *utils_generate_optstring(const struct option *long_options, break; } - optstring[str_pos++] = (char)long_options[i].val; - if (long_options[i].has_arg) { - optstring[str_pos++] = ':'; + if (long_options[i].val != '\0') { + optstring[str_pos++] = (char) long_options[i].val; + if (long_options[i].has_arg) { + optstring[str_pos++] = ':'; + } } } end: return optstring; } + +/* + * Try to remove a hierarchy of empty directories, recursively. Don't unlink + * any file. Try to rmdir any empty directory within the hierarchy. + */ +LTTNG_HIDDEN +int utils_recursive_rmdir(const char *path) +{ + DIR *dir; + int dir_fd, ret = 0, closeret, is_empty = 1; + struct dirent *entry; + + /* Open directory */ + dir = opendir(path); + if (!dir) { + PERROR("Cannot open '%s' path", path); + return -1; + } + dir_fd = dirfd(dir); + if (dir_fd < 0) { + PERROR("dirfd"); + return -1; + } + + while ((entry = readdir(dir))) { + if (!strcmp(entry->d_name, ".") + || !strcmp(entry->d_name, "..")) + continue; + switch (entry->d_type) { + case DT_DIR: + { + char subpath[PATH_MAX]; + + strncpy(subpath, path, PATH_MAX); + subpath[PATH_MAX - 1] = '\0'; + strncat(subpath, "/", + PATH_MAX - strlen(subpath) - 1); + strncat(subpath, entry->d_name, + PATH_MAX - strlen(subpath) - 1); + if (utils_recursive_rmdir(subpath)) { + is_empty = 0; + } + break; + } + case DT_REG: + is_empty = 0; + break; + default: + ret = -EINVAL; + goto end; + } + } +end: + closeret = closedir(dir); + if (closeret) { + PERROR("closedir"); + } + if (is_empty) { + DBG3("Attempting rmdir %s", path); + ret = rmdir(path); + } + return ret; +}