X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=2417a7d255ffb1445e12ec9e24ba2b5a29900e1d;hp=3c9e70ddad6439ee362d9187c751151eca60d32a;hb=d7c23421dddd4dcfbdfd329299816fa7d020eeb8;hpb=494a8e99d905eb46572f011225bef6054aaeaf5f diff --git a/src/common/utils.c b/src/common/utils.c index 3c9e70dda..2417a7d25 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -17,14 +17,12 @@ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#define _GNU_SOURCE #define _LGPL_SOURCE #include #include #include #include #include -#include #include #include #include @@ -32,11 +30,13 @@ #include #include #include -#include #include #include #include +#include +#include +#include #include "utils.h" #include "defaults.h" @@ -82,6 +82,8 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) /* Resolve the canonical path of the first part of the path */ while (try_path != NULL && next != end) { + char *try_path_buf = NULL; + /* * If there is not any '/' left, we want to try with * the full path @@ -92,15 +94,22 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) } /* Cut the part we will be trying to resolve */ - cut_path = strndup(path, next - path); + cut_path = lttng_strndup(path, next - path); if (cut_path == NULL) { - PERROR("strndup"); + PERROR("lttng_strndup"); + goto error; + } + + try_path_buf = zmalloc(LTTNG_PATH_MAX); + if (!try_path_buf) { + PERROR("zmalloc"); goto error; } /* Try to resolve this part */ - try_path = realpath((char *)cut_path, NULL); + try_path = realpath((char *)cut_path, try_path_buf); if (try_path == NULL) { + free(try_path_buf); /* * There was an error, we just want to be assured it * is linked to an unexistent directory, if it's another @@ -117,6 +126,7 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) } } else { /* Save the place we are before trying the next step */ + try_path_buf = NULL; free(try_path_prev); try_path_prev = try_path; prev = next; @@ -230,9 +240,9 @@ char *utils_expand_path(const char *path) while ((next = strstr(absolute_path, "/./"))) { /* We prepare the start_path not containing it */ - start_path = strndup(absolute_path, next - absolute_path); + start_path = lttng_strndup(absolute_path, next - absolute_path); if (!start_path) { - PERROR("strndup"); + PERROR("lttng_strndup"); goto error; } /* And we concatenate it with the part after this string */ @@ -250,9 +260,9 @@ char *utils_expand_path(const char *path) } /* Then we prepare the start_path not containing it */ - start_path = strndup(absolute_path, previous - absolute_path); + start_path = lttng_strndup(absolute_path, previous - absolute_path); if (!start_path) { - PERROR("strndup"); + PERROR("lttng_strndup"); goto error; } @@ -480,7 +490,7 @@ int utils_create_pid_file(pid_t pid, const char *filepath) goto error; } - ret = fprintf(fp, "%d\n", pid); + ret = fprintf(fp, "%d\n", (int) pid); if (ret < 0) { PERROR("fprintf pid file"); goto error; @@ -489,7 +499,7 @@ int utils_create_pid_file(pid_t pid, const char *filepath) if (fclose(fp)) { PERROR("fclose"); } - DBG("Pid %d written in file %s", pid, filepath); + DBG("Pid %d written in file %s", (int) pid, filepath); ret = 0; error: return ret; @@ -504,11 +514,13 @@ int utils_create_lock_file(const char *filepath) { int ret; int fd; + struct flock lock; assert(filepath); - fd = open(filepath, O_CREAT, - O_WRONLY | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + memset(&lock, 0, sizeof(lock)); + fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | + S_IRGRP | S_IWGRP); if (fd < 0) { PERROR("open lock file %s", filepath); ret = -1; @@ -520,8 +532,12 @@ int utils_create_lock_file(const char *filepath) * already a process using the same lock file running * and we should exit. */ - ret = flock(fd, LOCK_EX | LOCK_NB); - if (ret) { + lock.l_whence = SEEK_SET; + lock.l_type = F_WRLCK; + + ret = fcntl(fd, F_SETLK, &lock); + if (ret == -1) { + PERROR("fcntl lock file"); ERR("Could not get lock file %s, another instance is running.", filepath); if (close(fd)) { @@ -1197,6 +1213,7 @@ LTTNG_HIDDEN int utils_recursive_rmdir(const char *path) { DIR *dir; + size_t path_len; int dir_fd, ret = 0, closeret, is_empty = 1; struct dirent *entry; @@ -1206,19 +1223,40 @@ int utils_recursive_rmdir(const char *path) PERROR("Cannot open '%s' path", path); return -1; } - dir_fd = dirfd(dir); + dir_fd = lttng_dirfd(dir); if (dir_fd < 0) { - PERROR("dirfd"); + PERROR("lttng_dirfd"); return -1; } + path_len = strlen(path); while ((entry = readdir(dir))) { if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue; - switch (entry->d_type) { - case DT_DIR: - { + + struct stat st; + size_t name_len; + char filename[PATH_MAX]; + + name_len = strlen(entry->d_name); + if (path_len + name_len + 2 > sizeof(filename)) { + ERR("Failed to remove file: path name too long (%s/%s)", + path, entry->d_name); + continue; + } + if (snprintf(filename, sizeof(filename), "%s/%s", + path, entry->d_name) < 0) { + ERR("Failed to format path."); + continue; + } + + if (stat(filename, &st)) { + PERROR("stat"); + continue; + } + + if (S_ISDIR(st.st_mode)) { char subpath[PATH_MAX]; strncpy(subpath, path, PATH_MAX); @@ -1230,12 +1268,9 @@ int utils_recursive_rmdir(const char *path) if (utils_recursive_rmdir(subpath)) { is_empty = 0; } - break; - } - case DT_REG: + } else if (S_ISREG(st.st_mode)) { is_empty = 0; - break; - default: + } else { ret = -EINVAL; goto end; }