2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
31 #include <common/common.h>
32 #include <common/runas.h>
38 * Return the realpath(3) of the path even if the last directory token does not
39 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
40 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
41 * fails if the end point directory does not exist.
44 char *utils_expand_path(const char *path
)
46 const char *end_path
= path
;
47 char *next
, *cut_path
= NULL
, *expanded_path
= NULL
;
54 /* Find last token delimited by '/' */
55 while ((next
= strpbrk(end_path
+ 1, "/"))) {
59 /* Cut last token from original path */
60 cut_path
= strndup(path
, end_path
- path
);
62 expanded_path
= zmalloc(PATH_MAX
);
63 if (expanded_path
== NULL
) {
64 PERROR("zmalloc expand path");
68 expanded_path
= realpath((char *)cut_path
, expanded_path
);
69 if (expanded_path
== NULL
) {
72 ERR("%s: No such file or directory", cut_path
);
75 PERROR("realpath utils expand path");
81 /* Add end part to expanded path */
82 strncat(expanded_path
, end_path
, PATH_MAX
- strlen(expanded_path
) - 1);
94 * Create a pipe in dst.
97 int utils_create_pipe(int *dst
)
107 PERROR("create pipe");
114 * Create pipe and set CLOEXEC flag to both fd.
116 * Make sure the pipe opened by this function are closed at some point. Use
117 * utils_close_pipe().
120 int utils_create_pipe_cloexec(int *dst
)
128 ret
= utils_create_pipe(dst
);
133 for (i
= 0; i
< 2; i
++) {
134 ret
= fcntl(dst
[i
], F_SETFD
, FD_CLOEXEC
);
136 PERROR("fcntl pipe cloexec");
146 * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK.
148 * Make sure the pipe opened by this function are closed at some point. Use
149 * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to
150 * support OSes other than Linux 2.6.23+.
153 int utils_create_pipe_cloexec_nonblock(int *dst
)
161 ret
= utils_create_pipe(dst
);
166 for (i
= 0; i
< 2; i
++) {
167 ret
= fcntl(dst
[i
], F_SETFD
, FD_CLOEXEC
);
169 PERROR("fcntl pipe cloexec");
173 * Note: we override any flag that could have been
174 * previously set on the fd.
176 ret
= fcntl(dst
[i
], F_SETFL
, O_NONBLOCK
);
178 PERROR("fcntl pipe nonblock");
188 * Close both read and write side of the pipe.
191 void utils_close_pipe(int *src
)
199 for (i
= 0; i
< 2; i
++) {
207 PERROR("close pipe");
213 * Create a new string using two strings range.
216 char *utils_strdupdelim(const char *begin
, const char *end
)
220 str
= zmalloc(end
- begin
+ 1);
222 PERROR("zmalloc strdupdelim");
226 memcpy(str
, begin
, end
- begin
);
227 str
[end
- begin
] = '\0';
234 * Set CLOEXEC flag to the give file descriptor.
237 int utils_set_fd_cloexec(int fd
)
246 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
248 PERROR("fcntl cloexec");
257 * Create pid file to the given path and filename.
260 int utils_create_pid_file(pid_t pid
, const char *filepath
)
267 fp
= fopen(filepath
, "w");
269 PERROR("open pid file %s", filepath
);
274 ret
= fprintf(fp
, "%d\n", pid
);
276 PERROR("fprintf pid file");
280 DBG("Pid %d written in file %s", pid
, filepath
);
286 * Recursively create directory using the given path and mode.
288 * On success, return 0 else a negative error code.
291 int utils_mkdir_recursive(const char *path
, mode_t mode
)
293 char *p
, tmp
[PATH_MAX
];
299 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
301 PERROR("snprintf mkdir");
306 if (tmp
[len
- 1] == '/') {
310 for (p
= tmp
+ 1; *p
; p
++) {
313 if (tmp
[strlen(tmp
) - 1] == '.' &&
314 tmp
[strlen(tmp
) - 2] == '.' &&
315 tmp
[strlen(tmp
) - 3] == '/') {
316 ERR("Using '/../' is not permitted in the trace path (%s)",
321 ret
= mkdir(tmp
, mode
);
323 if (errno
!= EEXIST
) {
324 PERROR("mkdir recursive");
333 ret
= mkdir(tmp
, mode
);
335 if (errno
!= EEXIST
) {
336 PERROR("mkdir recursive last piece");
348 * Create the stream tracefile on disk.
350 * Return 0 on success or else a negative value.
353 int utils_create_stream_file(const char *path_name
, char *file_name
, uint64_t size
,
354 uint64_t count
, int uid
, int gid
)
356 int ret
, out_fd
, flags
, mode
;
357 char full_path
[PATH_MAX
], *path_name_id
= NULL
, *path
;
362 ret
= snprintf(full_path
, sizeof(full_path
), "%s/%s",
363 path_name
, file_name
);
365 PERROR("snprintf create output file");
370 * If we split the trace in multiple files, we have to add the count at the
371 * end of the tracefile name
374 ret
= asprintf(&path_name_id
, "%s_%" PRIu64
, full_path
, count
);
376 PERROR("Allocating path name ID");
384 flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
385 /* Open with 660 mode */
386 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
388 if (uid
< 0 || gid
< 0) {
389 out_fd
= open(path
, flags
, mode
);
391 out_fd
= run_as_open(path
, flags
, mode
, uid
, gid
);
394 PERROR("open stream path %s", path
);
406 * Change the output tracefile according to the given size and count The
407 * new_count pointer is set during this operation.
409 * From the consumer, the stream lock MUST be held before calling this function
410 * because we are modifying the stream status.
412 * Return 0 on success or else a negative value.
415 int utils_rotate_stream_file(char *path_name
, char *file_name
, uint64_t size
,
416 uint64_t count
, int uid
, int gid
, int out_fd
, uint64_t *new_count
)
422 PERROR("Closing tracefile");
427 *new_count
= (*new_count
+ 1) % count
;
432 return utils_create_stream_file(path_name
, file_name
, size
, *new_count
,
439 * Prints the error message corresponding to a regex error code.
441 * @param errcode The error code.
442 * @param regex The regex object that produced the error code.
444 static void regex_print_error(int errcode
, regex_t
*regex
)
446 /* Get length of error message and allocate accordingly */
450 assert(regex
!= NULL
);
452 length
= regerror(errcode
, regex
, NULL
, 0);
454 ERR("regerror returned a length of 0");
458 buffer
= zmalloc(length
);
460 ERR("regex_print_error: zmalloc failed");
464 /* Get and print error message */
465 regerror(errcode
, regex
, buffer
, length
);
466 ERR("regex error: %s\n", buffer
);
472 * Parse a string that represents a size in human readable format. It
473 * supports decimal integers suffixed by 'k', 'M' or 'G'.
475 * The suffix multiply the integer by:
480 * @param str The string to parse.
481 * @param size Pointer to a size_t that will be filled with the
484 * @return 0 on success, -1 on failure.
487 int utils_parse_size_suffix(char *str
, uint64_t *size
)
491 const int nmatch
= 3;
492 regmatch_t suffix_match
, matches
[nmatch
];
493 unsigned long long base_size
;
501 ret
= regcomp(®ex
, "^\\(0x\\)\\{0,1\\}[0-9][0-9]*\\([kKMG]\\{0,1\\}\\)$", 0);
503 regex_print_error(ret
, ®ex
);
509 ret
= regexec(®ex
, str
, nmatch
, matches
, 0);
515 /* There is a match ! */
517 base_size
= strtoull(str
, NULL
, 0);
524 /* Check if there is a suffix */
525 suffix_match
= matches
[2];
526 if (suffix_match
.rm_eo
- suffix_match
.rm_so
== 1) {
527 switch (*(str
+ suffix_match
.rm_so
)) {
539 ERR("parse_human_size: invalid suffix");
545 *size
= base_size
<< shift
;
547 /* Check for overflow */
548 if ((*size
>> shift
) != base_size
) {
549 ERR("parse_size_suffix: oops, overflow detected.");
563 * fls: returns the position of the most significant bit.
564 * Returns 0 if no bit is set, else returns the position of the most
565 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
567 #if defined(__i386) || defined(__x86_64)
568 static inline unsigned int fls_u32(uint32_t x
)
576 : "=r" (r
) : "rm" (x
));
583 static __attribute__((unused
)) unsigned int fls_u32(uint32_t x
)
590 if (!(x
& 0xFFFF0000U
)) {
594 if (!(x
& 0xFF000000U
)) {
598 if (!(x
& 0xF0000000U
)) {
602 if (!(x
& 0xC0000000U
)) {
606 if (!(x
& 0x80000000U
)) {
615 * Return the minimum order for which x <= (1UL << order).
616 * Return -1 if x is 0.
619 int utils_get_count_order_u32(uint32_t x
)
625 return fls_u32(x
- 1);
629 * Obtain the value of LTTNG_HOME environment variable, if exists.
630 * Otherwise returns the value of HOME.
633 char *utils_get_home_dir(void)
636 val
= getenv(DEFAULT_LTTNG_HOME_ENV_VAR
);
640 return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR
);
644 * With the given format, fill dst with the time of len maximum siz.
646 * Return amount of bytes set in the buffer or else 0 on error.
649 size_t utils_get_current_time_str(const char *format
, char *dst
, size_t len
)
658 /* Get date and time for session path */
660 timeinfo
= localtime(&rawtime
);
661 ret
= strftime(dst
, len
, format
, timeinfo
);
663 ERR("Unable to strftime with format %s at dst %p of len %lu", format
,