38f78a7e8e7d40cd6fcebefd297e5bb240c52aaf
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.
25 #include <sys/types.h>
31 #include <common/common.h>
32 #include <common/runas.h>
37 * Return the realpath(3) of the path even if the last directory token does not
38 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
39 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
40 * fails if the end point directory does not exist.
43 char *utils_expand_path(const char *path
)
45 const char *end_path
= path
;
46 char *next
, *cut_path
= NULL
, *expanded_path
= NULL
;
53 /* Find last token delimited by '/' */
54 while ((next
= strpbrk(end_path
+ 1, "/"))) {
58 /* Cut last token from original path */
59 cut_path
= strndup(path
, end_path
- path
);
61 expanded_path
= zmalloc(PATH_MAX
);
62 if (expanded_path
== NULL
) {
63 PERROR("zmalloc expand path");
67 expanded_path
= realpath((char *)cut_path
, expanded_path
);
68 if (expanded_path
== NULL
) {
71 ERR("%s: No such file or directory", cut_path
);
74 PERROR("realpath utils expand path");
80 /* Add end part to expanded path */
81 strncat(expanded_path
, end_path
, PATH_MAX
- strlen(expanded_path
) - 1);
93 * Create a pipe in dst.
96 int utils_create_pipe(int *dst
)
106 PERROR("create pipe");
113 * Create pipe and set CLOEXEC flag to both fd.
115 * Make sure the pipe opened by this function are closed at some point. Use
116 * utils_close_pipe().
119 int utils_create_pipe_cloexec(int *dst
)
127 ret
= utils_create_pipe(dst
);
132 for (i
= 0; i
< 2; i
++) {
133 ret
= fcntl(dst
[i
], F_SETFD
, FD_CLOEXEC
);
135 PERROR("fcntl pipe cloexec");
145 * Close both read and write side of the pipe.
148 void utils_close_pipe(int *src
)
156 for (i
= 0; i
< 2; i
++) {
164 PERROR("close pipe");
170 * Create a new string using two strings range.
173 char *utils_strdupdelim(const char *begin
, const char *end
)
177 str
= zmalloc(end
- begin
+ 1);
179 PERROR("zmalloc strdupdelim");
183 memcpy(str
, begin
, end
- begin
);
184 str
[end
- begin
] = '\0';
191 * Set CLOEXEC flag to the give file descriptor.
194 int utils_set_fd_cloexec(int fd
)
203 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
205 PERROR("fcntl cloexec");
214 * Create pid file to the given path and filename.
217 int utils_create_pid_file(pid_t pid
, const char *filepath
)
224 fp
= fopen(filepath
, "w");
226 PERROR("open pid file %s", filepath
);
231 ret
= fprintf(fp
, "%d\n", pid
);
233 PERROR("fprintf pid file");
237 DBG("Pid %d written in file %s", pid
, filepath
);
243 * Recursively create directory using the given path and mode.
245 * On success, return 0 else a negative error code.
248 int utils_mkdir_recursive(const char *path
, mode_t mode
)
250 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
= stat(tmp
, &statbuf
);
281 ret
= mkdir(tmp
, mode
);
283 if (errno
!= EEXIST
) {
284 PERROR("mkdir recursive");
294 ret
= mkdir(tmp
, mode
);
296 if (errno
!= EEXIST
) {
297 PERROR("mkdir recursive last piece");
309 * Create the stream tracefile on disk.
311 * Return 0 on success or else a negative value.
314 int utils_create_stream_file(char *path_name
, char *file_name
, uint64_t size
,
315 uint64_t count
, int uid
, int gid
)
317 int ret
, out_fd
, flags
, mode
;
318 char full_path
[PATH_MAX
], *path_name_id
= NULL
, *path
;
323 ret
= snprintf(full_path
, sizeof(full_path
), "%s/%s",
324 path_name
, file_name
);
326 PERROR("snprintf create output file");
331 * If we split the trace in multiple files, we have to add the count at the
332 * end of the tracefile name
335 ret
= asprintf(&path_name_id
, "%s_%" PRIu64
, full_path
, count
);
337 PERROR("Allocating path name ID");
345 flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
346 /* Open with 660 mode */
347 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
349 if (uid
< 0 || gid
< 0) {
350 out_fd
= open(path
, flags
, mode
);
352 out_fd
= run_as_open(path
, flags
, mode
, uid
, gid
);
355 PERROR("open stream path %s", path
);
367 * Change the output tracefile according to the given size and count The
368 * new_count pointer is set during this operation.
370 * From the consumer, the stream lock MUST be held before calling this function
371 * because we are modifying the stream status.
373 * Return 0 on success or else a negative value.
376 int utils_rotate_stream_file(char *path_name
, char *file_name
, uint64_t size
,
377 uint64_t count
, int uid
, int gid
, int out_fd
, uint64_t *new_count
)
383 PERROR("Closing tracefile");
388 *new_count
= (*new_count
+ 1) % count
;
393 return utils_create_stream_file(path_name
, file_name
, size
, *new_count
,
400 * Prints the error message corresponding to a regex error code.
402 * @param errcode The error code.
403 * @param regex The regex object that produced the error code.
405 static void regex_print_error(int errcode
, regex_t
*regex
)
407 /* Get length of error message and allocate accordingly */
411 assert(regex
!= NULL
);
413 length
= regerror(errcode
, regex
, NULL
, 0);
415 ERR("regerror returned a length of 0");
419 buffer
= zmalloc(length
);
421 ERR("regex_print_error: zmalloc failed");
425 /* Get and print error message */
426 regerror(errcode
, regex
, buffer
, length
);
427 ERR("regex error: %s\n", buffer
);
433 * Parse a string that represents a size in human readable format. It
434 * supports decimal integers suffixed by 'k', 'M' or 'G'.
436 * The suffix multiply the integer by:
441 * @param str The string to parse.
442 * @param size Pointer to a size_t that will be filled with the
445 * @return 0 on success, -1 on failure.
447 int utils_parse_size_suffix(char *str
, uint64_t *size
)
451 const int nmatch
= 3;
452 regmatch_t suffix_match
, matches
[nmatch
];
453 unsigned long long base_size
;
461 ret
= regcomp(®ex
, "^\\(0x\\)\\{0,1\\}[0-9][0-9]*\\([kKMG]\\{0,1\\}\\)$", 0);
463 regex_print_error(ret
, ®ex
);
469 ret
= regexec(®ex
, str
, nmatch
, matches
, 0);
475 /* There is a match ! */
477 base_size
= strtoull(str
, NULL
, 0);
484 /* Check if there is a suffix */
485 suffix_match
= matches
[2];
486 if (suffix_match
.rm_eo
- suffix_match
.rm_so
== 1) {
487 switch (*(str
+ suffix_match
.rm_so
)) {
499 ERR("parse_human_size: invalid suffix");
505 *size
= base_size
<< shift
;
507 /* Check for overflow */
508 if ((*size
>> shift
) != base_size
) {
509 ERR("parse_size_suffix: oops, overflow detected.");
523 * fls: returns the position of the most significant bit.
524 * Returns 0 if no bit is set, else returns the position of the most
525 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
527 #if defined(__i386) || defined(__x86_64)
528 static inline unsigned int fls_u32(uint32_t x
)
536 : "=r" (r
) : "rm" (x
));
543 static __attribute__((unused
)) unsigned int fls_u32(uint32_t x
)
550 if (!(x
& 0xFFFF0000U
)) {
554 if (!(x
& 0xFF000000U
)) {
558 if (!(x
& 0xF0000000U
)) {
562 if (!(x
& 0xC0000000U
)) {
566 if (!(x
& 0x80000000U
)) {
575 * Return the minimum order for which x <= (1UL << order).
576 * Return -1 if x is 0.
579 int utils_get_count_order_u32(uint32_t x
)
585 return fls_u32(x
- 1);
This page took 0.043608 seconds and 5 git commands to generate.