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 * Close both read and write side of the pipe.
149 void utils_close_pipe(int *src
)
157 for (i
= 0; i
< 2; i
++) {
165 PERROR("close pipe");
171 * Create a new string using two strings range.
174 char *utils_strdupdelim(const char *begin
, const char *end
)
178 str
= zmalloc(end
- begin
+ 1);
180 PERROR("zmalloc strdupdelim");
184 memcpy(str
, begin
, end
- begin
);
185 str
[end
- begin
] = '\0';
192 * Set CLOEXEC flag to the give file descriptor.
195 int utils_set_fd_cloexec(int fd
)
204 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
206 PERROR("fcntl cloexec");
215 * Create pid file to the given path and filename.
218 int utils_create_pid_file(pid_t pid
, const char *filepath
)
225 fp
= fopen(filepath
, "w");
227 PERROR("open pid file %s", filepath
);
232 ret
= fprintf(fp
, "%d\n", pid
);
234 PERROR("fprintf pid file");
238 DBG("Pid %d written in file %s", pid
, filepath
);
244 * Recursively create directory using the given path and mode.
246 * On success, return 0 else a negative error code.
249 int utils_mkdir_recursive(const char *path
, mode_t mode
)
251 char *p
, tmp
[PATH_MAX
];
257 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
259 PERROR("snprintf mkdir");
264 if (tmp
[len
- 1] == '/') {
268 for (p
= tmp
+ 1; *p
; p
++) {
271 if (tmp
[strlen(tmp
) - 1] == '.' &&
272 tmp
[strlen(tmp
) - 2] == '.' &&
273 tmp
[strlen(tmp
) - 3] == '/') {
274 ERR("Using '/../' is not permitted in the trace path (%s)",
279 ret
= mkdir(tmp
, mode
);
281 if (errno
!= EEXIST
) {
282 PERROR("mkdir recursive");
291 ret
= mkdir(tmp
, mode
);
293 if (errno
!= EEXIST
) {
294 PERROR("mkdir recursive last piece");
306 * Create the stream tracefile on disk.
308 * Return 0 on success or else a negative value.
311 int utils_create_stream_file(const char *path_name
, char *file_name
, uint64_t size
,
312 uint64_t count
, int uid
, int gid
)
314 int ret
, out_fd
, flags
, mode
;
315 char full_path
[PATH_MAX
], *path_name_id
= NULL
, *path
;
320 ret
= snprintf(full_path
, sizeof(full_path
), "%s/%s",
321 path_name
, file_name
);
323 PERROR("snprintf create output file");
328 * If we split the trace in multiple files, we have to add the count at the
329 * end of the tracefile name
332 ret
= asprintf(&path_name_id
, "%s_%" PRIu64
, full_path
, count
);
334 PERROR("Allocating path name ID");
342 flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
343 /* Open with 660 mode */
344 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
346 if (uid
< 0 || gid
< 0) {
347 out_fd
= open(path
, flags
, mode
);
349 out_fd
= run_as_open(path
, flags
, mode
, uid
, gid
);
352 PERROR("open stream path %s", path
);
364 * Change the output tracefile according to the given size and count The
365 * new_count pointer is set during this operation.
367 * From the consumer, the stream lock MUST be held before calling this function
368 * because we are modifying the stream status.
370 * Return 0 on success or else a negative value.
373 int utils_rotate_stream_file(char *path_name
, char *file_name
, uint64_t size
,
374 uint64_t count
, int uid
, int gid
, int out_fd
, uint64_t *new_count
)
380 PERROR("Closing tracefile");
385 *new_count
= (*new_count
+ 1) % count
;
390 return utils_create_stream_file(path_name
, file_name
, size
, *new_count
,
397 * Prints the error message corresponding to a regex error code.
399 * @param errcode The error code.
400 * @param regex The regex object that produced the error code.
402 static void regex_print_error(int errcode
, regex_t
*regex
)
404 /* Get length of error message and allocate accordingly */
408 assert(regex
!= NULL
);
410 length
= regerror(errcode
, regex
, NULL
, 0);
412 ERR("regerror returned a length of 0");
416 buffer
= zmalloc(length
);
418 ERR("regex_print_error: zmalloc failed");
422 /* Get and print error message */
423 regerror(errcode
, regex
, buffer
, length
);
424 ERR("regex error: %s\n", buffer
);
430 * Parse a string that represents a size in human readable format. It
431 * supports decimal integers suffixed by 'k', 'M' or 'G'.
433 * The suffix multiply the integer by:
438 * @param str The string to parse.
439 * @param size Pointer to a size_t that will be filled with the
442 * @return 0 on success, -1 on failure.
445 int utils_parse_size_suffix(char *str
, uint64_t *size
)
449 const int nmatch
= 3;
450 regmatch_t suffix_match
, matches
[nmatch
];
451 unsigned long long base_size
;
459 ret
= regcomp(®ex
, "^\\(0x\\)\\{0,1\\}[0-9][0-9]*\\([kKMG]\\{0,1\\}\\)$", 0);
461 regex_print_error(ret
, ®ex
);
467 ret
= regexec(®ex
, str
, nmatch
, matches
, 0);
473 /* There is a match ! */
475 base_size
= strtoull(str
, NULL
, 0);
482 /* Check if there is a suffix */
483 suffix_match
= matches
[2];
484 if (suffix_match
.rm_eo
- suffix_match
.rm_so
== 1) {
485 switch (*(str
+ suffix_match
.rm_so
)) {
497 ERR("parse_human_size: invalid suffix");
503 *size
= base_size
<< shift
;
505 /* Check for overflow */
506 if ((*size
>> shift
) != base_size
) {
507 ERR("parse_size_suffix: oops, overflow detected.");
521 * fls: returns the position of the most significant bit.
522 * Returns 0 if no bit is set, else returns the position of the most
523 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
525 #if defined(__i386) || defined(__x86_64)
526 static inline unsigned int fls_u32(uint32_t x
)
534 : "=r" (r
) : "rm" (x
));
541 static __attribute__((unused
)) unsigned int fls_u32(uint32_t x
)
548 if (!(x
& 0xFFFF0000U
)) {
552 if (!(x
& 0xFF000000U
)) {
556 if (!(x
& 0xF0000000U
)) {
560 if (!(x
& 0xC0000000U
)) {
564 if (!(x
& 0x80000000U
)) {
573 * Return the minimum order for which x <= (1UL << order).
574 * Return -1 if x is 0.
577 int utils_get_count_order_u32(uint32_t x
)
583 return fls_u32(x
- 1);
587 * Obtain the value of LTTNG_HOME environment variable, if exists.
588 * Otherwise returns the value of HOME.
591 char *utils_get_home_dir(void)
594 val
= getenv(DEFAULT_LTTNG_HOME_ENV_VAR
);
598 return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR
);
602 * With the given format, fill dst with the time of len maximum siz.
604 * Return amount of bytes set in the buffer or else 0 on error.
607 size_t utils_get_current_time_str(const char *format
, char *dst
, size_t len
)
616 /* Get date and time for session path */
618 timeinfo
= localtime(&rawtime
);
619 ret
= strftime(dst
, len
, format
, timeinfo
);
621 ERR("Unable to strftime with format %s at dst %p of len %lu", format
,