X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=b345100ea0fa5ad6517d28c72cbc53a5bd87e391;hp=f253e797fae641e31ef98abcf4e829655feb8dfc;hb=efa116c6f19ecdc344a82709e7d919703ec96c45;hpb=feb0f3e5900ff58202ea0c3f227de3c1b8291952 diff --git a/src/common/utils.c b/src/common/utils.c index f253e797f..b345100ea 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -142,6 +142,48 @@ error: return ret; } +/* + * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK. + * + * Make sure the pipe opened by this function are closed at some point. Use + * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to + * support OSes other than Linux 2.6.23+. + */ +LTTNG_HIDDEN +int utils_create_pipe_cloexec_nonblock(int *dst) +{ + int ret, i; + + if (dst == NULL) { + return -1; + } + + ret = utils_create_pipe(dst); + if (ret < 0) { + goto error; + } + + for (i = 0; i < 2; i++) { + ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); + if (ret < 0) { + PERROR("fcntl pipe cloexec"); + goto error; + } + /* + * Note: we override any flag that could have been + * previously set on the fd. + */ + ret = fcntl(dst[i], F_SETFL, O_NONBLOCK); + if (ret < 0) { + PERROR("fcntl pipe nonblock"); + goto error; + } + } + +error: + return ret; +} + /* * Close both read and write side of the pipe. */ @@ -308,7 +350,7 @@ error: * Return 0 on success or else a negative value. */ LTTNG_HIDDEN -int utils_create_stream_file(char *path_name, char *file_name, uint64_t size, +int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size, uint64_t count, int uid, int gid) { int ret, out_fd, flags, mode; @@ -441,6 +483,7 @@ static void regex_print_error(int errcode, regex_t *regex) * * @return 0 on success, -1 on failure. */ +LTTNG_HIDDEN int utils_parse_size_suffix(char *str, uint64_t *size) { regex_t regex; @@ -586,6 +629,7 @@ int utils_get_count_order_u32(uint32_t x) * Obtain the value of LTTNG_HOME environment variable, if exists. * Otherwise returns the value of HOME. */ +LTTNG_HIDDEN char *utils_get_home_dir(void) { char *val = NULL; @@ -595,3 +639,30 @@ char *utils_get_home_dir(void) } return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); } + +/* + * With the given format, fill dst with the time of len maximum siz. + * + * Return amount of bytes set in the buffer or else 0 on error. + */ +LTTNG_HIDDEN +size_t utils_get_current_time_str(const char *format, char *dst, size_t len) +{ + size_t ret; + time_t rawtime; + struct tm *timeinfo; + + assert(format); + assert(dst); + + /* Get date and time for session path */ + time(&rawtime); + timeinfo = localtime(&rawtime); + ret = strftime(dst, len, format, timeinfo); + if (ret == 0) { + ERR("Unable to strftime with format %s at dst %p of len %lu", format, + dst, len); + } + + return ret; +}