2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2013 - Raphaël Beamonte <raphael.beamonte@gmail.com>
4 * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
35 #include <common/common.h>
36 #include <common/runas.h>
37 #include <common/compat/getenv.h>
38 #include <common/compat/string.h>
39 #include <common/compat/dirent.h>
40 #include <lttng/constant.h>
46 * Return a partial realpath(3) of the path even if the full path does not
47 * exist. For instance, with /tmp/test1/test2/test3, if test2/ does not exist
48 * but the /tmp/test1 does, the real path for /tmp/test1 is concatened with
49 * /test2/test3 then returned. In normal time, realpath(3) fails if the end
50 * point directory does not exist.
51 * In case resolved_path is NULL, the string returned was allocated in the
52 * function and thus need to be freed by the caller. The size argument allows
53 * to specify the size of the resolved_path argument if given, or the size to
57 char *utils_partial_realpath(const char *path
, char *resolved_path
, size_t size
)
59 char *cut_path
= NULL
, *try_path
= NULL
, *try_path_prev
= NULL
;
60 const char *next
, *prev
, *end
;
68 * Identify the end of the path, we don't want to treat the
69 * last char if it is a '/', we will just keep it on the side
70 * to be added at the end, and return a value coherent with
71 * the path given as argument
73 end
= path
+ strlen(path
);
74 if (*(end
-1) == '/') {
78 /* Initiate the values of the pointers before looping */
81 /* Only to ensure try_path is not NULL to enter the while */
82 try_path
= (char *)next
;
84 /* Resolve the canonical path of the first part of the path */
85 while (try_path
!= NULL
&& next
!= end
) {
86 char *try_path_buf
= NULL
;
89 * If there is not any '/' left, we want to try with
92 next
= strpbrk(next
+ 1, "/");
97 /* Cut the part we will be trying to resolve */
98 cut_path
= lttng_strndup(path
, next
- path
);
99 if (cut_path
== NULL
) {
100 PERROR("lttng_strndup");
104 try_path_buf
= zmalloc(LTTNG_PATH_MAX
);
110 /* Try to resolve this part */
111 try_path
= realpath((char *) cut_path
, try_path_buf
);
112 if (try_path
== NULL
) {
115 * There was an error, we just want to be assured it
116 * is linked to an unexistent directory, if it's another
117 * reason, we spawn an error
121 /* Ignore the error */
124 PERROR("realpath (partial_realpath)");
129 /* Save the place we are before trying the next step */
132 try_path_prev
= try_path
;
136 /* Free the allocated memory */
141 /* Allocate memory for the resolved path if necessary */
142 if (resolved_path
== NULL
) {
143 resolved_path
= zmalloc(size
);
144 if (resolved_path
== NULL
) {
145 PERROR("zmalloc resolved path");
151 * If we were able to solve at least partially the path, we can concatenate
152 * what worked and what didn't work
154 if (try_path_prev
!= NULL
) {
155 /* If we risk to concatenate two '/', we remove one of them */
156 if (try_path_prev
[strlen(try_path_prev
) - 1] == '/' && prev
[0] == '/') {
157 try_path_prev
[strlen(try_path_prev
) - 1] = '\0';
161 * Duplicate the memory used by prev in case resolved_path and
162 * path are pointers for the same memory space
164 cut_path
= strdup(prev
);
165 if (cut_path
== NULL
) {
170 /* Concatenate the strings */
171 snprintf(resolved_path
, size
, "%s%s", try_path_prev
, cut_path
);
173 /* Free the allocated memory */
177 try_path_prev
= NULL
;
179 * Else, we just copy the path in our resolved_path to
183 strncpy(resolved_path
, path
, size
);
186 /* Then we return the 'partially' resolved path */
187 return resolved_path
;
198 * Make a full resolution of the given path even if it doesn't exist.
199 * This function uses the utils_partial_realpath function to resolve
200 * symlinks and relatives paths at the start of the string, and
201 * implements functionnalities to resolve the './' and '../' strings
202 * in the middle of a path. This function is only necessary because
203 * realpath(3) does not accept to resolve unexistent paths.
204 * The returned string was allocated in the function, it is thus of
205 * the responsibility of the caller to free this memory.
208 char *utils_expand_path(const char *path
)
210 char *next
, *previous
, *slash
, *start_path
, *absolute_path
= NULL
;
212 int is_dot
, is_dotdot
;
219 /* Allocate memory for the absolute_path */
220 absolute_path
= zmalloc(PATH_MAX
);
221 if (absolute_path
== NULL
) {
222 PERROR("zmalloc expand path");
227 * If the path is not already absolute nor explicitly relative,
228 * consider we're in the current directory
230 if (*path
!= '/' && strncmp(path
, "./", 2) != 0 &&
231 strncmp(path
, "../", 3) != 0) {
232 snprintf(absolute_path
, PATH_MAX
, "./%s", path
);
233 /* Else, we just copy the path */
235 strncpy(absolute_path
, path
, PATH_MAX
);
238 /* Resolve partially our path */
239 absolute_path
= utils_partial_realpath(absolute_path
,
240 absolute_path
, PATH_MAX
);
242 /* As long as we find '/./' in the working_path string */
243 while ((next
= strstr(absolute_path
, "/./"))) {
245 /* We prepare the start_path not containing it */
246 start_path
= lttng_strndup(absolute_path
, next
- absolute_path
);
248 PERROR("lttng_strndup");
251 /* And we concatenate it with the part after this string */
252 snprintf(absolute_path
, PATH_MAX
, "%s%s", start_path
, next
+ 2);
257 /* As long as we find '/../' in the working_path string */
258 while ((next
= strstr(absolute_path
, "/../"))) {
259 /* We find the last level of directory */
260 previous
= absolute_path
;
261 while ((slash
= strpbrk(previous
, "/")) && slash
!= next
) {
262 previous
= slash
+ 1;
265 /* Then we prepare the start_path not containing it */
266 start_path
= lttng_strndup(absolute_path
, previous
- absolute_path
);
268 PERROR("lttng_strndup");
272 /* And we concatenate it with the part after the '/../' */
273 snprintf(absolute_path
, PATH_MAX
, "%s%s", start_path
, next
+ 4);
275 /* We can free the memory used for the start path*/
278 /* Then we verify for symlinks using partial_realpath */
279 absolute_path
= utils_partial_realpath(absolute_path
,
280 absolute_path
, PATH_MAX
);
283 /* Identify the last token */
284 last_token
= strrchr(absolute_path
, '/');
286 /* Verify that this token is not a relative path */
287 is_dotdot
= (strcmp(last_token
, "/..") == 0);
288 is_dot
= (strcmp(last_token
, "/.") == 0);
290 /* If it is, take action */
291 if (is_dot
|| is_dotdot
) {
292 /* For both, remove this token */
295 /* If it was a reference to parent directory, go back one more time */
297 last_token
= strrchr(absolute_path
, '/');
299 /* If there was only one level left, we keep the first '/' */
300 if (last_token
== absolute_path
) {
308 return absolute_path
;
316 * Create a pipe in dst.
319 int utils_create_pipe(int *dst
)
329 PERROR("create pipe");
336 * Create pipe and set CLOEXEC flag to both fd.
338 * Make sure the pipe opened by this function are closed at some point. Use
339 * utils_close_pipe().
342 int utils_create_pipe_cloexec(int *dst
)
350 ret
= utils_create_pipe(dst
);
355 for (i
= 0; i
< 2; i
++) {
356 ret
= fcntl(dst
[i
], F_SETFD
, FD_CLOEXEC
);
358 PERROR("fcntl pipe cloexec");
368 * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK.
370 * Make sure the pipe opened by this function are closed at some point. Use
371 * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to
372 * support OSes other than Linux 2.6.23+.
375 int utils_create_pipe_cloexec_nonblock(int *dst
)
383 ret
= utils_create_pipe(dst
);
388 for (i
= 0; i
< 2; i
++) {
389 ret
= fcntl(dst
[i
], F_SETFD
, FD_CLOEXEC
);
391 PERROR("fcntl pipe cloexec");
395 * Note: we override any flag that could have been
396 * previously set on the fd.
398 ret
= fcntl(dst
[i
], F_SETFL
, O_NONBLOCK
);
400 PERROR("fcntl pipe nonblock");
410 * Close both read and write side of the pipe.
413 void utils_close_pipe(int *src
)
421 for (i
= 0; i
< 2; i
++) {
429 PERROR("close pipe");
435 * Create a new string using two strings range.
438 char *utils_strdupdelim(const char *begin
, const char *end
)
442 str
= zmalloc(end
- begin
+ 1);
444 PERROR("zmalloc strdupdelim");
448 memcpy(str
, begin
, end
- begin
);
449 str
[end
- begin
] = '\0';
456 * Set CLOEXEC flag to the give file descriptor.
459 int utils_set_fd_cloexec(int fd
)
468 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
470 PERROR("fcntl cloexec");
479 * Create pid file to the given path and filename.
482 int utils_create_pid_file(pid_t pid
, const char *filepath
)
489 fp
= fopen(filepath
, "w");
491 PERROR("open pid file %s", filepath
);
496 ret
= fprintf(fp
, "%d\n", (int) pid
);
498 PERROR("fprintf pid file");
505 DBG("Pid %d written in file %s", (int) pid
, filepath
);
512 * Create lock file to the given path and filename.
513 * Returns the associated file descriptor, -1 on error.
516 int utils_create_lock_file(const char *filepath
)
524 memset(&lock
, 0, sizeof(lock
));
525 fd
= open(filepath
, O_CREAT
| O_WRONLY
, S_IRUSR
| S_IWUSR
|
528 PERROR("open lock file %s", filepath
);
534 * Attempt to lock the file. If this fails, there is
535 * already a process using the same lock file running
536 * and we should exit.
538 lock
.l_whence
= SEEK_SET
;
539 lock
.l_type
= F_WRLCK
;
541 ret
= fcntl(fd
, F_SETLK
, &lock
);
543 PERROR("fcntl lock file");
544 ERR("Could not get lock file %s, another instance is running.",
547 PERROR("close lock file");
558 * On some filesystems (e.g. nfs), mkdir will validate access rights before
559 * checking for the existence of the path element. This means that on a setup
560 * where "/home/" is a mounted NFS share, and running as an unpriviledged user,
561 * recursively creating a path of the form "/home/my_user/trace/" will fail with
562 * EACCES on mkdir("/home", ...).
564 * Performing a stat(...) on the path to check for existence allows us to
565 * work around this behaviour.
568 int mkdir_check_exists(const char *path
, mode_t mode
)
573 ret
= stat(path
, &st
);
575 if (S_ISDIR(st
.st_mode
)) {
576 /* Directory exists, skip. */
579 /* Exists, but is not a directory. */
587 * Let mkdir handle other errors as the caller expects mkdir
590 ret
= mkdir(path
, mode
);
596 * Create directory using the given path and mode.
598 * On success, return 0 else a negative error code.
601 int utils_mkdir(const char *path
, mode_t mode
, int uid
, int gid
)
605 if (uid
< 0 || gid
< 0) {
606 ret
= mkdir_check_exists(path
, mode
);
608 ret
= run_as_mkdir(path
, mode
, uid
, gid
);
611 if (errno
!= EEXIST
) {
612 PERROR("mkdir %s, uid %d, gid %d", path
? path
: "NULL",
623 * Internal version of mkdir_recursive. Runs as the current user.
624 * Don't call directly; use utils_mkdir_recursive().
626 * This function is ominously marked as "unsafe" since it should only
627 * be called by a caller that has transitioned to the uid and gid under which
628 * the directory creation should occur.
631 int _utils_mkdir_recursive_unsafe(const char *path
, mode_t mode
)
633 char *p
, tmp
[PATH_MAX
];
639 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
641 PERROR("snprintf mkdir");
646 if (tmp
[len
- 1] == '/') {
650 for (p
= tmp
+ 1; *p
; p
++) {
653 if (tmp
[strlen(tmp
) - 1] == '.' &&
654 tmp
[strlen(tmp
) - 2] == '.' &&
655 tmp
[strlen(tmp
) - 3] == '/') {
656 ERR("Using '/../' is not permitted in the trace path (%s)",
661 ret
= mkdir_check_exists(tmp
, mode
);
663 if (errno
!= EACCES
) {
664 PERROR("mkdir recursive");
673 ret
= mkdir_check_exists(tmp
, mode
);
675 PERROR("mkdir recursive last element");
684 * Recursively create directory using the given path and mode, under the
685 * provided uid and gid.
687 * On success, return 0 else a negative error code.
690 int utils_mkdir_recursive(const char *path
, mode_t mode
, int uid
, int gid
)
694 if (uid
< 0 || gid
< 0) {
695 /* Run as current user. */
696 ret
= _utils_mkdir_recursive_unsafe(path
, mode
);
698 ret
= run_as_mkdir_recursive(path
, mode
, uid
, gid
);
701 PERROR("mkdir %s, uid %d, gid %d", path
? path
: "NULL",
709 * path is the output parameter. It needs to be PATH_MAX len.
711 * Return 0 on success or else a negative value.
713 static int utils_stream_file_name(char *path
,
714 const char *path_name
, const char *file_name
,
715 uint64_t size
, uint64_t count
,
719 char full_path
[PATH_MAX
];
720 char *path_name_suffix
= NULL
;
723 ret
= snprintf(full_path
, sizeof(full_path
), "%s/%s",
724 path_name
, file_name
);
726 PERROR("snprintf create output file");
730 /* Setup extra string if suffix or/and a count is needed. */
731 if (size
> 0 && suffix
) {
732 ret
= asprintf(&extra
, "_%" PRIu64
"%s", count
, suffix
);
733 } else if (size
> 0) {
734 ret
= asprintf(&extra
, "_%" PRIu64
, count
);
736 ret
= asprintf(&extra
, "%s", suffix
);
739 PERROR("Allocating extra string to name");
744 * If we split the trace in multiple files, we have to add the count at
745 * the end of the tracefile name.
748 ret
= asprintf(&path_name_suffix
, "%s%s", full_path
, extra
);
750 PERROR("Allocating path name with extra string");
751 goto error_free_suffix
;
753 strncpy(path
, path_name_suffix
, PATH_MAX
- 1);
754 path
[PATH_MAX
- 1] = '\0';
756 strncpy(path
, full_path
, PATH_MAX
- 1);
758 path
[PATH_MAX
- 1] = '\0';
761 free(path_name_suffix
);
769 * Create the stream file on disk.
771 * Return 0 on success or else a negative value.
774 int utils_create_stream_file(const char *path_name
, char *file_name
, uint64_t size
,
775 uint64_t count
, int uid
, int gid
, char *suffix
)
777 int ret
, flags
, mode
;
780 ret
= utils_stream_file_name(path
, path_name
, file_name
,
781 size
, count
, suffix
);
786 flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
787 /* Open with 660 mode */
788 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
790 if (uid
< 0 || gid
< 0) {
791 ret
= open(path
, flags
, mode
);
793 ret
= run_as_open(path
, flags
, mode
, uid
, gid
);
796 PERROR("open stream path %s", path
);
803 * Unlink the stream tracefile from disk.
805 * Return 0 on success or else a negative value.
808 int utils_unlink_stream_file(const char *path_name
, char *file_name
, uint64_t size
,
809 uint64_t count
, int uid
, int gid
, char *suffix
)
814 ret
= utils_stream_file_name(path
, path_name
, file_name
,
815 size
, count
, suffix
);
819 if (uid
< 0 || gid
< 0) {
822 ret
= run_as_unlink(path
, uid
, gid
);
828 DBG("utils_unlink_stream_file %s returns %d", path
, ret
);
833 * Change the output tracefile according to the given size and count The
834 * new_count pointer is set during this operation.
836 * From the consumer, the stream lock MUST be held before calling this function
837 * because we are modifying the stream status.
839 * Return 0 on success or else a negative value.
842 int utils_rotate_stream_file(char *path_name
, char *file_name
, uint64_t size
,
843 uint64_t count
, int uid
, int gid
, int out_fd
, uint64_t *new_count
,
852 PERROR("Closing tracefile");
858 * In tracefile rotation, for the relay daemon we need
859 * to unlink the old file if present, because it may
860 * still be open in reading by the live thread, and we
861 * need to ensure that we do not overwrite the content
862 * between get_index and get_packet. Since we have no
863 * way to verify integrity of the data content compared
864 * to the associated index, we need to ensure the reader
865 * has exclusive access to the file content, and that
866 * the open of the data file is performed in get_index.
867 * Unlinking the old file rather than overwriting it
871 *new_count
= (*new_count
+ 1) % count
;
873 ret
= utils_unlink_stream_file(path_name
, file_name
, size
,
874 new_count
? *new_count
: 0, uid
, gid
, 0);
875 if (ret
< 0 && errno
!= ENOENT
) {
884 ret
= utils_create_stream_file(path_name
, file_name
, size
,
885 new_count
? *new_count
: 0, uid
, gid
, 0);
900 * Parse a string that represents a size in human readable format. It
901 * supports decimal integers suffixed by 'k', 'K', 'M' or 'G'.
903 * The suffix multiply the integer by:
908 * @param str The string to parse.
909 * @param size Pointer to a uint64_t that will be filled with the
912 * @return 0 on success, -1 on failure.
915 int utils_parse_size_suffix(const char * const str
, uint64_t * const size
)
924 DBG("utils_parse_size_suffix: received a NULL string.");
929 /* strtoull will accept a negative number, but we don't want to. */
930 if (strchr(str
, '-') != NULL
) {
931 DBG("utils_parse_size_suffix: invalid size string, should not contain '-'.");
936 /* str_end will point to the \0 */
937 str_end
= str
+ strlen(str
);
939 base_size
= strtoull(str
, &num_end
, 0);
941 PERROR("utils_parse_size_suffix strtoull");
946 if (num_end
== str
) {
947 /* strtoull parsed nothing, not good. */
948 DBG("utils_parse_size_suffix: strtoull had nothing good to parse.");
953 /* Check if a prefix is present. */
971 DBG("utils_parse_size_suffix: invalid suffix.");
976 /* Check for garbage after the valid input. */
977 if (num_end
!= str_end
) {
978 DBG("utils_parse_size_suffix: Garbage after size string.");
983 *size
= base_size
<< shift
;
985 /* Check for overflow */
986 if ((*size
>> shift
) != base_size
) {
987 DBG("utils_parse_size_suffix: oops, overflow detected.");
998 * fls: returns the position of the most significant bit.
999 * Returns 0 if no bit is set, else returns the position of the most
1000 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
1002 #if defined(__i386) || defined(__x86_64)
1003 static inline unsigned int fls_u32(uint32_t x
)
1007 asm("bsrl %1,%0\n\t"
1011 : "=r" (r
) : "rm" (x
));
1018 static __attribute__((unused
)) unsigned int fls_u32(uint32_t x
)
1020 unsigned int r
= 32;
1025 if (!(x
& 0xFFFF0000U
)) {
1029 if (!(x
& 0xFF000000U
)) {
1033 if (!(x
& 0xF0000000U
)) {
1037 if (!(x
& 0xC0000000U
)) {
1041 if (!(x
& 0x80000000U
)) {
1050 * Return the minimum order for which x <= (1UL << order).
1051 * Return -1 if x is 0.
1054 int utils_get_count_order_u32(uint32_t x
)
1060 return fls_u32(x
- 1);
1064 * Obtain the value of LTTNG_HOME environment variable, if exists.
1065 * Otherwise returns the value of HOME.
1068 char *utils_get_home_dir(void)
1073 val
= lttng_secure_getenv(DEFAULT_LTTNG_HOME_ENV_VAR
);
1077 val
= lttng_secure_getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR
);
1082 /* Fallback on the password file entry. */
1083 pwd
= getpwuid(getuid());
1089 DBG3("Home directory is '%s'", val
);
1096 * Get user's home directory. Dynamically allocated, must be freed
1100 char *utils_get_user_home_dir(uid_t uid
)
1103 struct passwd
*result
;
1104 char *home_dir
= NULL
;
1109 buflen
= sysconf(_SC_GETPW_R_SIZE_MAX
);
1114 buf
= zmalloc(buflen
);
1119 ret
= getpwuid_r(uid
, &pwd
, buf
, buflen
, &result
);
1120 if (ret
|| !result
) {
1121 if (ret
== ERANGE
) {
1129 home_dir
= strdup(pwd
.pw_dir
);
1136 * Obtain the value of LTTNG_KMOD_PROBES environment variable, if exists.
1137 * Otherwise returns NULL.
1140 char *utils_get_kmod_probes_list(void)
1142 return lttng_secure_getenv(DEFAULT_LTTNG_KMOD_PROBES
);
1146 * Obtain the value of LTTNG_EXTRA_KMOD_PROBES environment variable, if
1147 * exists. Otherwise returns NULL.
1150 char *utils_get_extra_kmod_probes_list(void)
1152 return lttng_secure_getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES
);
1156 * With the given format, fill dst with the time of len maximum siz.
1158 * Return amount of bytes set in the buffer or else 0 on error.
1161 size_t utils_get_current_time_str(const char *format
, char *dst
, size_t len
)
1165 struct tm
*timeinfo
;
1170 /* Get date and time for session path */
1172 timeinfo
= localtime(&rawtime
);
1173 ret
= strftime(dst
, len
, format
, timeinfo
);
1175 ERR("Unable to strftime with format %s at dst %p of len %zu", format
,
1183 * Return the group ID matching name, else 0 if it cannot be found.
1186 gid_t
utils_get_group_id(const char *name
)
1190 grp
= getgrnam(name
);
1192 static volatile int warn_once
;
1195 WARN("No tracing group detected");
1204 * Return a newly allocated option string. This string is to be used as the
1205 * optstring argument of getopt_long(), see GETOPT(3). opt_count is the number
1206 * of elements in the long_options array. Returns NULL if the string's
1210 char *utils_generate_optstring(const struct option
*long_options
,
1214 size_t string_len
= opt_count
, str_pos
= 0;
1218 * Compute the necessary string length. One letter per option, two when an
1219 * argument is necessary, and a trailing NULL.
1221 for (i
= 0; i
< opt_count
; i
++) {
1222 string_len
+= long_options
[i
].has_arg
? 1 : 0;
1225 optstring
= zmalloc(string_len
);
1230 for (i
= 0; i
< opt_count
; i
++) {
1231 if (!long_options
[i
].name
) {
1232 /* Got to the trailing NULL element */
1236 if (long_options
[i
].val
!= '\0') {
1237 optstring
[str_pos
++] = (char) long_options
[i
].val
;
1238 if (long_options
[i
].has_arg
) {
1239 optstring
[str_pos
++] = ':';
1249 * Try to remove a hierarchy of empty directories, recursively. Don't unlink
1250 * any file. Try to rmdir any empty directory within the hierarchy.
1253 int utils_recursive_rmdir(const char *path
)
1257 int dir_fd
, ret
= 0, closeret
, is_empty
= 1;
1258 struct dirent
*entry
;
1260 /* Open directory */
1261 dir
= opendir(path
);
1263 PERROR("Cannot open '%s' path", path
);
1266 dir_fd
= lttng_dirfd(dir
);
1268 PERROR("lttng_dirfd");
1272 path_len
= strlen(path
);
1273 while ((entry
= readdir(dir
))) {
1276 char filename
[PATH_MAX
];
1278 if (!strcmp(entry
->d_name
, ".")
1279 || !strcmp(entry
->d_name
, "..")) {
1283 name_len
= strlen(entry
->d_name
);
1284 if (path_len
+ name_len
+ 2 > sizeof(filename
)) {
1285 ERR("Failed to remove file: path name too long (%s/%s)",
1286 path
, entry
->d_name
);
1289 if (snprintf(filename
, sizeof(filename
), "%s/%s",
1290 path
, entry
->d_name
) < 0) {
1291 ERR("Failed to format path.");
1295 if (stat(filename
, &st
)) {
1300 if (S_ISDIR(st
.st_mode
)) {
1301 char subpath
[PATH_MAX
];
1303 strncpy(subpath
, path
, PATH_MAX
);
1304 subpath
[PATH_MAX
- 1] = '\0';
1305 strncat(subpath
, "/",
1306 PATH_MAX
- strlen(subpath
) - 1);
1307 strncat(subpath
, entry
->d_name
,
1308 PATH_MAX
- strlen(subpath
) - 1);
1309 if (utils_recursive_rmdir(subpath
)) {
1312 } else if (S_ISREG(st
.st_mode
)) {
1320 closeret
= closedir(dir
);
1325 DBG3("Attempting rmdir %s", path
);
1332 int utils_truncate_stream_file(int fd
, off_t length
)
1336 ret
= ftruncate(fd
, length
);
1338 PERROR("ftruncate");
1341 ret
= lseek(fd
, length
, SEEK_SET
);
1350 static const char *get_man_bin_path(void)
1352 char *env_man_path
= lttng_secure_getenv(DEFAULT_MAN_BIN_PATH_ENV
);
1355 return env_man_path
;
1358 return DEFAULT_MAN_BIN_PATH
;
1362 int utils_show_man_page(int section
, const char *page_name
)
1364 char section_string
[8];
1365 const char *man_bin_path
= get_man_bin_path();
1368 /* Section integer -> section string */
1369 ret
= sprintf(section_string
, "%d", section
);
1370 assert(ret
> 0 && ret
< 8);
1373 * Execute man pager.
1375 * We provide --manpath to man here because LTTng-tools can
1376 * be installed outside /usr, in which case its man pages are
1377 * not located in the default /usr/share/man directory.
1379 ret
= execlp(man_bin_path
, "man", "--manpath", MANPATH
,
1380 section_string
, page_name
, NULL
);