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>
32 #include <common/common.h>
33 #include <common/runas.h>
39 * Return the realpath(3) of the path even if the last directory token does not
40 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
41 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
42 * fails if the end point directory does not exist.
45 char *utils_expand_path(const char *path
)
47 const char *end_path
= path
;
48 char *next
, *cut_path
= NULL
, *expanded_path
= NULL
;
55 /* Find last token delimited by '/' */
56 while ((next
= strpbrk(end_path
+ 1, "/"))) {
60 /* Cut last token from original path */
61 cut_path
= strndup(path
, end_path
- path
);
63 expanded_path
= zmalloc(PATH_MAX
);
64 if (expanded_path
== NULL
) {
65 PERROR("zmalloc expand path");
69 expanded_path
= realpath((char *)cut_path
, expanded_path
);
70 if (expanded_path
== NULL
) {
73 ERR("%s: No such file or directory", cut_path
);
76 PERROR("realpath utils expand path");
82 /* Add end part to expanded path */
83 strncat(expanded_path
, end_path
, PATH_MAX
- strlen(expanded_path
) - 1);
95 * Create a pipe in dst.
98 int utils_create_pipe(int *dst
)
108 PERROR("create pipe");
115 * Create pipe and set CLOEXEC flag to both fd.
117 * Make sure the pipe opened by this function are closed at some point. Use
118 * utils_close_pipe().
121 int utils_create_pipe_cloexec(int *dst
)
129 ret
= utils_create_pipe(dst
);
134 for (i
= 0; i
< 2; i
++) {
135 ret
= fcntl(dst
[i
], F_SETFD
, FD_CLOEXEC
);
137 PERROR("fcntl pipe cloexec");
147 * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK.
149 * Make sure the pipe opened by this function are closed at some point. Use
150 * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to
151 * support OSes other than Linux 2.6.23+.
154 int utils_create_pipe_cloexec_nonblock(int *dst
)
162 ret
= utils_create_pipe(dst
);
167 for (i
= 0; i
< 2; i
++) {
168 ret
= fcntl(dst
[i
], F_SETFD
, FD_CLOEXEC
);
170 PERROR("fcntl pipe cloexec");
174 * Note: we override any flag that could have been
175 * previously set on the fd.
177 ret
= fcntl(dst
[i
], F_SETFL
, O_NONBLOCK
);
179 PERROR("fcntl pipe nonblock");
189 * Close both read and write side of the pipe.
192 void utils_close_pipe(int *src
)
200 for (i
= 0; i
< 2; i
++) {
208 PERROR("close pipe");
214 * Create a new string using two strings range.
217 char *utils_strdupdelim(const char *begin
, const char *end
)
221 str
= zmalloc(end
- begin
+ 1);
223 PERROR("zmalloc strdupdelim");
227 memcpy(str
, begin
, end
- begin
);
228 str
[end
- begin
] = '\0';
235 * Set CLOEXEC flag to the give file descriptor.
238 int utils_set_fd_cloexec(int fd
)
247 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
249 PERROR("fcntl cloexec");
258 * Create pid file to the given path and filename.
261 int utils_create_pid_file(pid_t pid
, const char *filepath
)
268 fp
= fopen(filepath
, "w");
270 PERROR("open pid file %s", filepath
);
275 ret
= fprintf(fp
, "%d\n", pid
);
277 PERROR("fprintf pid file");
281 DBG("Pid %d written in file %s", pid
, filepath
);
287 * Recursively create directory using the given path and mode.
289 * On success, return 0 else a negative error code.
292 int utils_mkdir_recursive(const char *path
, mode_t mode
)
294 char *p
, tmp
[PATH_MAX
];
300 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
302 PERROR("snprintf mkdir");
307 if (tmp
[len
- 1] == '/') {
311 for (p
= tmp
+ 1; *p
; p
++) {
314 if (tmp
[strlen(tmp
) - 1] == '.' &&
315 tmp
[strlen(tmp
) - 2] == '.' &&
316 tmp
[strlen(tmp
) - 3] == '/') {
317 ERR("Using '/../' is not permitted in the trace path (%s)",
322 ret
= mkdir(tmp
, mode
);
324 if (errno
!= EEXIST
) {
325 PERROR("mkdir recursive");
334 ret
= mkdir(tmp
, mode
);
336 if (errno
!= EEXIST
) {
337 PERROR("mkdir recursive last piece");
349 * Create the stream tracefile on disk.
351 * Return 0 on success or else a negative value.
354 int utils_create_stream_file(const char *path_name
, char *file_name
, uint64_t size
,
355 uint64_t count
, int uid
, int gid
, char *suffix
)
357 int ret
, out_fd
, flags
, mode
;
358 char full_path
[PATH_MAX
], *path_name_suffix
= NULL
, *path
;
364 ret
= snprintf(full_path
, sizeof(full_path
), "%s/%s",
365 path_name
, file_name
);
367 PERROR("snprintf create output file");
371 /* Setup extra string if suffix or/and a count is needed. */
372 if (size
> 0 && suffix
) {
373 ret
= asprintf(&extra
, "_%" PRIu64
"%s", count
, suffix
);
374 } else if (size
> 0) {
375 ret
= asprintf(&extra
, "_%" PRIu64
, count
);
377 ret
= asprintf(&extra
, "%s", suffix
);
380 PERROR("Allocating extra string to name");
385 * If we split the trace in multiple files, we have to add the count at the
386 * end of the tracefile name
389 ret
= asprintf(&path_name_suffix
, "%s%s", full_path
, extra
);
391 PERROR("Allocating path name with extra string");
392 goto error_free_suffix
;
394 path
= path_name_suffix
;
399 flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
400 /* Open with 660 mode */
401 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
403 if (uid
< 0 || gid
< 0) {
404 out_fd
= open(path
, flags
, mode
);
406 out_fd
= run_as_open(path
, flags
, mode
, uid
, gid
);
409 PERROR("open stream path %s", path
);
415 free(path_name_suffix
);
423 * Change the output tracefile according to the given size and count The
424 * new_count pointer is set during this operation.
426 * From the consumer, the stream lock MUST be held before calling this function
427 * because we are modifying the stream status.
429 * Return 0 on success or else a negative value.
432 int utils_rotate_stream_file(char *path_name
, char *file_name
, uint64_t size
,
433 uint64_t count
, int uid
, int gid
, int out_fd
, uint64_t *new_count
,
443 PERROR("Closing tracefile");
448 *new_count
= (*new_count
+ 1) % count
;
453 ret
= utils_create_stream_file(path_name
, file_name
, size
, *new_count
,
468 * Prints the error message corresponding to a regex error code.
470 * @param errcode The error code.
471 * @param regex The regex object that produced the error code.
473 static void regex_print_error(int errcode
, regex_t
*regex
)
475 /* Get length of error message and allocate accordingly */
479 assert(regex
!= NULL
);
481 length
= regerror(errcode
, regex
, NULL
, 0);
483 ERR("regerror returned a length of 0");
487 buffer
= zmalloc(length
);
489 ERR("regex_print_error: zmalloc failed");
493 /* Get and print error message */
494 regerror(errcode
, regex
, buffer
, length
);
495 ERR("regex error: %s\n", buffer
);
501 * Parse a string that represents a size in human readable format. It
502 * supports decimal integers suffixed by 'k', 'M' or 'G'.
504 * The suffix multiply the integer by:
509 * @param str The string to parse.
510 * @param size Pointer to a size_t that will be filled with the
513 * @return 0 on success, -1 on failure.
516 int utils_parse_size_suffix(char *str
, uint64_t *size
)
520 const int nmatch
= 3;
521 regmatch_t suffix_match
, matches
[nmatch
];
522 unsigned long long base_size
;
530 ret
= regcomp(®ex
, "^\\(0x\\)\\{0,1\\}[0-9][0-9]*\\([kKMG]\\{0,1\\}\\)$", 0);
532 regex_print_error(ret
, ®ex
);
538 ret
= regexec(®ex
, str
, nmatch
, matches
, 0);
544 /* There is a match ! */
546 base_size
= strtoull(str
, NULL
, 0);
553 /* Check if there is a suffix */
554 suffix_match
= matches
[2];
555 if (suffix_match
.rm_eo
- suffix_match
.rm_so
== 1) {
556 switch (*(str
+ suffix_match
.rm_so
)) {
568 ERR("parse_human_size: invalid suffix");
574 *size
= base_size
<< shift
;
576 /* Check for overflow */
577 if ((*size
>> shift
) != base_size
) {
578 ERR("parse_size_suffix: oops, overflow detected.");
592 * fls: returns the position of the most significant bit.
593 * Returns 0 if no bit is set, else returns the position of the most
594 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
596 #if defined(__i386) || defined(__x86_64)
597 static inline unsigned int fls_u32(uint32_t x
)
605 : "=r" (r
) : "rm" (x
));
612 static __attribute__((unused
)) unsigned int fls_u32(uint32_t x
)
619 if (!(x
& 0xFFFF0000U
)) {
623 if (!(x
& 0xFF000000U
)) {
627 if (!(x
& 0xF0000000U
)) {
631 if (!(x
& 0xC0000000U
)) {
635 if (!(x
& 0x80000000U
)) {
644 * Return the minimum order for which x <= (1UL << order).
645 * Return -1 if x is 0.
648 int utils_get_count_order_u32(uint32_t x
)
654 return fls_u32(x
- 1);
658 * Obtain the value of LTTNG_HOME environment variable, if exists.
659 * Otherwise returns the value of HOME.
662 char *utils_get_home_dir(void)
665 val
= getenv(DEFAULT_LTTNG_HOME_ENV_VAR
);
669 return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR
);
673 * With the given format, fill dst with the time of len maximum siz.
675 * Return amount of bytes set in the buffer or else 0 on error.
678 size_t utils_get_current_time_str(const char *format
, char *dst
, size_t len
)
687 /* Get date and time for session path */
689 timeinfo
= localtime(&rawtime
);
690 ret
= strftime(dst
, len
, format
, timeinfo
);
692 ERR("Unable to strftime with format %s at dst %p of len %lu", format
,
700 * Return the group ID matching name, else 0 if it cannot be found.
703 gid_t
utils_get_group_id(const char *name
)
707 grp
= getgrnam(name
);
709 static volatile int warn_once
;
712 WARN("No tracing group detected");