Commit | Line | Data |
---|---|---|
81b86775 DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
66495845 | 3 | * Copyright (C) 2013 - Raphaël Beamonte <raphael.beamonte@gmail.com> |
8db0dc00 | 4 | * Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
81b86775 DG |
5 | * |
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. | |
9 | * | |
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 | |
13 | * more details. | |
14 | * | |
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. | |
18 | */ | |
19 | ||
6c1c0768 | 20 | #define _LGPL_SOURCE |
35f90c40 | 21 | #include <assert.h> |
81b86775 DG |
22 | #include <ctype.h> |
23 | #include <fcntl.h> | |
24 | #include <limits.h> | |
25 | #include <stdlib.h> | |
2d851108 | 26 | #include <sys/stat.h> |
0c7bcad5 | 27 | #include <sys/types.h> |
2d851108 | 28 | #include <unistd.h> |
fe4477ee | 29 | #include <inttypes.h> |
6c71277b | 30 | #include <grp.h> |
fb198a11 | 31 | #include <pwd.h> |
c9cb3e7d | 32 | #include <sys/file.h> |
a98e236e | 33 | #include <unistd.h> |
81b86775 DG |
34 | |
35 | #include <common/common.h> | |
fe4477ee | 36 | #include <common/runas.h> |
e8fa9fb0 | 37 | #include <common/compat/getenv.h> |
f5436bfc | 38 | #include <common/compat/string.h> |
5a2451c9 | 39 | #include <common/compat/dirent.h> |
d7c23421 | 40 | #include <lttng/constant.h> |
81b86775 DG |
41 | |
42 | #include "utils.h" | |
feb0f3e5 | 43 | #include "defaults.h" |
81b86775 | 44 | |
5154230f RB |
45 | /* |
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 | |
54 | * allocate. | |
55 | */ | |
56 | LTTNG_HIDDEN | |
57 | char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) | |
58 | { | |
9482daac | 59 | char *cut_path = NULL, *try_path = NULL, *try_path_prev = NULL; |
5154230f RB |
60 | const char *next, *prev, *end; |
61 | ||
62 | /* Safety net */ | |
63 | if (path == NULL) { | |
64 | goto error; | |
65 | } | |
66 | ||
67 | /* | |
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 | |
72 | */ | |
73 | end = path + strlen(path); | |
74 | if (*(end-1) == '/') { | |
75 | end--; | |
76 | } | |
77 | ||
78 | /* Initiate the values of the pointers before looping */ | |
79 | next = path; | |
80 | prev = next; | |
81 | /* Only to ensure try_path is not NULL to enter the while */ | |
82 | try_path = (char *)next; | |
83 | ||
84 | /* Resolve the canonical path of the first part of the path */ | |
85 | while (try_path != NULL && next != end) { | |
d7c23421 JG |
86 | char *try_path_buf = NULL; |
87 | ||
5154230f RB |
88 | /* |
89 | * If there is not any '/' left, we want to try with | |
90 | * the full path | |
91 | */ | |
92 | next = strpbrk(next + 1, "/"); | |
93 | if (next == NULL) { | |
94 | next = end; | |
95 | } | |
96 | ||
97 | /* Cut the part we will be trying to resolve */ | |
f5436bfc | 98 | cut_path = lttng_strndup(path, next - path); |
d9dbcf5e | 99 | if (cut_path == NULL) { |
f5436bfc | 100 | PERROR("lttng_strndup"); |
d9dbcf5e MD |
101 | goto error; |
102 | } | |
5154230f | 103 | |
d7c23421 JG |
104 | try_path_buf = zmalloc(LTTNG_PATH_MAX); |
105 | if (!try_path_buf) { | |
106 | PERROR("zmalloc"); | |
107 | goto error; | |
108 | } | |
109 | ||
5154230f | 110 | /* Try to resolve this part */ |
f3472d9a | 111 | try_path = realpath((char *) cut_path, try_path_buf); |
5154230f | 112 | if (try_path == NULL) { |
d7c23421 | 113 | free(try_path_buf); |
5154230f RB |
114 | /* |
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 | |
118 | */ | |
119 | switch (errno) { | |
120 | case ENOENT: | |
121 | /* Ignore the error */ | |
122 | break; | |
123 | default: | |
124 | PERROR("realpath (partial_realpath)"); | |
125 | goto error; | |
126 | break; | |
127 | } | |
128 | } else { | |
129 | /* Save the place we are before trying the next step */ | |
d7c23421 | 130 | try_path_buf = NULL; |
5154230f RB |
131 | free(try_path_prev); |
132 | try_path_prev = try_path; | |
133 | prev = next; | |
134 | } | |
135 | ||
136 | /* Free the allocated memory */ | |
137 | free(cut_path); | |
c14cc491 | 138 | cut_path = NULL; |
494a8e99 | 139 | } |
5154230f RB |
140 | |
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"); | |
146 | goto error; | |
147 | } | |
148 | } | |
149 | ||
150 | /* | |
151 | * If we were able to solve at least partially the path, we can concatenate | |
152 | * what worked and what didn't work | |
153 | */ | |
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'; | |
158 | } | |
159 | ||
160 | /* | |
161 | * Duplicate the memory used by prev in case resolved_path and | |
162 | * path are pointers for the same memory space | |
163 | */ | |
164 | cut_path = strdup(prev); | |
d9dbcf5e MD |
165 | if (cut_path == NULL) { |
166 | PERROR("strdup"); | |
167 | goto error; | |
168 | } | |
5154230f RB |
169 | |
170 | /* Concatenate the strings */ | |
171 | snprintf(resolved_path, size, "%s%s", try_path_prev, cut_path); | |
172 | ||
173 | /* Free the allocated memory */ | |
174 | free(cut_path); | |
175 | free(try_path_prev); | |
494a8e99 JG |
176 | cut_path = NULL; |
177 | try_path_prev = NULL; | |
5154230f RB |
178 | /* |
179 | * Else, we just copy the path in our resolved_path to | |
180 | * return it as is | |
181 | */ | |
182 | } else { | |
183 | strncpy(resolved_path, path, size); | |
184 | } | |
185 | ||
186 | /* Then we return the 'partially' resolved path */ | |
187 | return resolved_path; | |
188 | ||
189 | error: | |
190 | free(resolved_path); | |
9482daac | 191 | free(cut_path); |
1a068457 MJ |
192 | free(try_path); |
193 | free(try_path_prev); | |
5154230f RB |
194 | return NULL; |
195 | } | |
196 | ||
81b86775 | 197 | /* |
3d229795 RB |
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. | |
81b86775 | 206 | */ |
90e535ef | 207 | LTTNG_HIDDEN |
81b86775 DG |
208 | char *utils_expand_path(const char *path) |
209 | { | |
3d229795 | 210 | char *next, *previous, *slash, *start_path, *absolute_path = NULL; |
5de083f4 RB |
211 | char *last_token; |
212 | int is_dot, is_dotdot; | |
81b86775 DG |
213 | |
214 | /* Safety net */ | |
215 | if (path == NULL) { | |
216 | goto error; | |
217 | } | |
218 | ||
3d229795 RB |
219 | /* Allocate memory for the absolute_path */ |
220 | absolute_path = zmalloc(PATH_MAX); | |
221 | if (absolute_path == NULL) { | |
81b86775 DG |
222 | PERROR("zmalloc expand path"); |
223 | goto error; | |
224 | } | |
225 | ||
3d229795 RB |
226 | /* |
227 | * If the path is not already absolute nor explicitly relative, | |
228 | * consider we're in the current directory | |
229 | */ | |
230 | if (*path != '/' && strncmp(path, "./", 2) != 0 && | |
231 | strncmp(path, "../", 3) != 0) { | |
232 | snprintf(absolute_path, PATH_MAX, "./%s", path); | |
2dcd84b7 | 233 | /* Else, we just copy the path */ |
116f95d9 | 234 | } else { |
3d229795 RB |
235 | strncpy(absolute_path, path, PATH_MAX); |
236 | } | |
116f95d9 | 237 | |
3d229795 RB |
238 | /* Resolve partially our path */ |
239 | absolute_path = utils_partial_realpath(absolute_path, | |
240 | absolute_path, PATH_MAX); | |
116f95d9 | 241 | |
3d229795 RB |
242 | /* As long as we find '/./' in the working_path string */ |
243 | while ((next = strstr(absolute_path, "/./"))) { | |
116f95d9 | 244 | |
3d229795 | 245 | /* We prepare the start_path not containing it */ |
f5436bfc | 246 | start_path = lttng_strndup(absolute_path, next - absolute_path); |
d9dbcf5e | 247 | if (!start_path) { |
f5436bfc | 248 | PERROR("lttng_strndup"); |
d9dbcf5e MD |
249 | goto error; |
250 | } | |
3d229795 RB |
251 | /* And we concatenate it with the part after this string */ |
252 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 2); | |
116f95d9 | 253 | |
3d229795 RB |
254 | free(start_path); |
255 | } | |
116f95d9 | 256 | |
3d229795 RB |
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; | |
81b86775 | 263 | } |
81b86775 | 264 | |
3d229795 | 265 | /* Then we prepare the start_path not containing it */ |
f5436bfc | 266 | start_path = lttng_strndup(absolute_path, previous - absolute_path); |
d9dbcf5e | 267 | if (!start_path) { |
f5436bfc | 268 | PERROR("lttng_strndup"); |
d9dbcf5e MD |
269 | goto error; |
270 | } | |
3d229795 RB |
271 | |
272 | /* And we concatenate it with the part after the '/../' */ | |
273 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 4); | |
274 | ||
275 | /* We can free the memory used for the start path*/ | |
276 | free(start_path); | |
277 | ||
278 | /* Then we verify for symlinks using partial_realpath */ | |
279 | absolute_path = utils_partial_realpath(absolute_path, | |
280 | absolute_path, PATH_MAX); | |
116f95d9 | 281 | } |
81b86775 | 282 | |
5de083f4 RB |
283 | /* Identify the last token */ |
284 | last_token = strrchr(absolute_path, '/'); | |
285 | ||
286 | /* Verify that this token is not a relative path */ | |
287 | is_dotdot = (strcmp(last_token, "/..") == 0); | |
288 | is_dot = (strcmp(last_token, "/.") == 0); | |
289 | ||
290 | /* If it is, take action */ | |
291 | if (is_dot || is_dotdot) { | |
292 | /* For both, remove this token */ | |
293 | *last_token = '\0'; | |
294 | ||
295 | /* If it was a reference to parent directory, go back one more time */ | |
296 | if (is_dotdot) { | |
297 | last_token = strrchr(absolute_path, '/'); | |
298 | ||
299 | /* If there was only one level left, we keep the first '/' */ | |
300 | if (last_token == absolute_path) { | |
301 | last_token++; | |
302 | } | |
303 | ||
304 | *last_token = '\0'; | |
305 | } | |
306 | } | |
307 | ||
3d229795 | 308 | return absolute_path; |
81b86775 DG |
309 | |
310 | error: | |
3d229795 | 311 | free(absolute_path); |
81b86775 DG |
312 | return NULL; |
313 | } | |
314 | ||
315 | /* | |
316 | * Create a pipe in dst. | |
317 | */ | |
90e535ef | 318 | LTTNG_HIDDEN |
81b86775 DG |
319 | int utils_create_pipe(int *dst) |
320 | { | |
321 | int ret; | |
322 | ||
323 | if (dst == NULL) { | |
324 | return -1; | |
325 | } | |
326 | ||
327 | ret = pipe(dst); | |
328 | if (ret < 0) { | |
329 | PERROR("create pipe"); | |
330 | } | |
331 | ||
332 | return ret; | |
333 | } | |
334 | ||
335 | /* | |
336 | * Create pipe and set CLOEXEC flag to both fd. | |
337 | * | |
338 | * Make sure the pipe opened by this function are closed at some point. Use | |
339 | * utils_close_pipe(). | |
340 | */ | |
90e535ef | 341 | LTTNG_HIDDEN |
81b86775 DG |
342 | int utils_create_pipe_cloexec(int *dst) |
343 | { | |
344 | int ret, i; | |
345 | ||
346 | if (dst == NULL) { | |
347 | return -1; | |
348 | } | |
349 | ||
350 | ret = utils_create_pipe(dst); | |
351 | if (ret < 0) { | |
352 | goto error; | |
353 | } | |
354 | ||
355 | for (i = 0; i < 2; i++) { | |
356 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); | |
357 | if (ret < 0) { | |
358 | PERROR("fcntl pipe cloexec"); | |
359 | goto error; | |
360 | } | |
361 | } | |
362 | ||
363 | error: | |
364 | return ret; | |
365 | } | |
366 | ||
094f381c MD |
367 | /* |
368 | * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK. | |
369 | * | |
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+. | |
373 | */ | |
374 | LTTNG_HIDDEN | |
375 | int utils_create_pipe_cloexec_nonblock(int *dst) | |
376 | { | |
377 | int ret, i; | |
378 | ||
379 | if (dst == NULL) { | |
380 | return -1; | |
381 | } | |
382 | ||
383 | ret = utils_create_pipe(dst); | |
384 | if (ret < 0) { | |
385 | goto error; | |
386 | } | |
387 | ||
388 | for (i = 0; i < 2; i++) { | |
389 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); | |
390 | if (ret < 0) { | |
391 | PERROR("fcntl pipe cloexec"); | |
392 | goto error; | |
393 | } | |
394 | /* | |
395 | * Note: we override any flag that could have been | |
396 | * previously set on the fd. | |
397 | */ | |
398 | ret = fcntl(dst[i], F_SETFL, O_NONBLOCK); | |
399 | if (ret < 0) { | |
400 | PERROR("fcntl pipe nonblock"); | |
401 | goto error; | |
402 | } | |
403 | } | |
404 | ||
405 | error: | |
406 | return ret; | |
407 | } | |
408 | ||
81b86775 DG |
409 | /* |
410 | * Close both read and write side of the pipe. | |
411 | */ | |
90e535ef | 412 | LTTNG_HIDDEN |
81b86775 DG |
413 | void utils_close_pipe(int *src) |
414 | { | |
415 | int i, ret; | |
416 | ||
417 | if (src == NULL) { | |
418 | return; | |
419 | } | |
420 | ||
421 | for (i = 0; i < 2; i++) { | |
422 | /* Safety check */ | |
423 | if (src[i] < 0) { | |
424 | continue; | |
425 | } | |
426 | ||
427 | ret = close(src[i]); | |
428 | if (ret) { | |
429 | PERROR("close pipe"); | |
430 | } | |
431 | } | |
432 | } | |
a4b92340 DG |
433 | |
434 | /* | |
435 | * Create a new string using two strings range. | |
436 | */ | |
90e535ef | 437 | LTTNG_HIDDEN |
a4b92340 DG |
438 | char *utils_strdupdelim(const char *begin, const char *end) |
439 | { | |
440 | char *str; | |
441 | ||
442 | str = zmalloc(end - begin + 1); | |
443 | if (str == NULL) { | |
444 | PERROR("zmalloc strdupdelim"); | |
445 | goto error; | |
446 | } | |
447 | ||
448 | memcpy(str, begin, end - begin); | |
449 | str[end - begin] = '\0'; | |
450 | ||
451 | error: | |
452 | return str; | |
453 | } | |
b662582b DG |
454 | |
455 | /* | |
456 | * Set CLOEXEC flag to the give file descriptor. | |
457 | */ | |
90e535ef | 458 | LTTNG_HIDDEN |
b662582b DG |
459 | int utils_set_fd_cloexec(int fd) |
460 | { | |
461 | int ret; | |
462 | ||
463 | if (fd < 0) { | |
464 | ret = -EINVAL; | |
465 | goto end; | |
466 | } | |
467 | ||
468 | ret = fcntl(fd, F_SETFD, FD_CLOEXEC); | |
469 | if (ret < 0) { | |
470 | PERROR("fcntl cloexec"); | |
471 | ret = -errno; | |
472 | } | |
473 | ||
474 | end: | |
475 | return ret; | |
476 | } | |
35f90c40 DG |
477 | |
478 | /* | |
479 | * Create pid file to the given path and filename. | |
480 | */ | |
90e535ef | 481 | LTTNG_HIDDEN |
35f90c40 DG |
482 | int utils_create_pid_file(pid_t pid, const char *filepath) |
483 | { | |
484 | int ret; | |
485 | FILE *fp; | |
486 | ||
487 | assert(filepath); | |
488 | ||
489 | fp = fopen(filepath, "w"); | |
490 | if (fp == NULL) { | |
491 | PERROR("open pid file %s", filepath); | |
492 | ret = -1; | |
493 | goto error; | |
494 | } | |
495 | ||
d1f721c5 | 496 | ret = fprintf(fp, "%d\n", (int) pid); |
35f90c40 DG |
497 | if (ret < 0) { |
498 | PERROR("fprintf pid file"); | |
e205d79b | 499 | goto error; |
35f90c40 DG |
500 | } |
501 | ||
e205d79b MD |
502 | if (fclose(fp)) { |
503 | PERROR("fclose"); | |
504 | } | |
d1f721c5 | 505 | DBG("Pid %d written in file %s", (int) pid, filepath); |
e205d79b | 506 | ret = 0; |
35f90c40 DG |
507 | error: |
508 | return ret; | |
509 | } | |
2d851108 | 510 | |
c9cb3e7d JG |
511 | /* |
512 | * Create lock file to the given path and filename. | |
513 | * Returns the associated file descriptor, -1 on error. | |
514 | */ | |
515 | LTTNG_HIDDEN | |
516 | int utils_create_lock_file(const char *filepath) | |
517 | { | |
518 | int ret; | |
519 | int fd; | |
77e7fddf | 520 | struct flock lock; |
c9cb3e7d JG |
521 | |
522 | assert(filepath); | |
523 | ||
77e7fddf MJ |
524 | memset(&lock, 0, sizeof(lock)); |
525 | fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | | |
526 | S_IRGRP | S_IWGRP); | |
c9cb3e7d JG |
527 | if (fd < 0) { |
528 | PERROR("open lock file %s", filepath); | |
529 | ret = -1; | |
530 | goto error; | |
531 | } | |
532 | ||
533 | /* | |
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. | |
537 | */ | |
77e7fddf MJ |
538 | lock.l_whence = SEEK_SET; |
539 | lock.l_type = F_WRLCK; | |
540 | ||
541 | ret = fcntl(fd, F_SETLK, &lock); | |
542 | if (ret == -1) { | |
543 | PERROR("fcntl lock file"); | |
208ff148 | 544 | ERR("Could not get lock file %s, another instance is running.", |
c9cb3e7d | 545 | filepath); |
ffb0b851 JG |
546 | if (close(fd)) { |
547 | PERROR("close lock file"); | |
548 | } | |
c9cb3e7d JG |
549 | fd = ret; |
550 | goto error; | |
551 | } | |
552 | ||
553 | error: | |
554 | return fd; | |
555 | } | |
556 | ||
a98e236e JG |
557 | /* |
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", ...). | |
563 | * | |
564 | * Performing a stat(...) on the path to check for existence allows us to | |
565 | * work around this behaviour. | |
566 | */ | |
567 | static | |
568 | int mkdir_check_exists(const char *path, mode_t mode) | |
569 | { | |
570 | int ret = 0; | |
571 | struct stat st; | |
572 | ||
573 | ret = stat(path, &st); | |
574 | if (ret == 0) { | |
575 | if (S_ISDIR(st.st_mode)) { | |
576 | /* Directory exists, skip. */ | |
577 | goto end; | |
578 | } else { | |
579 | /* Exists, but is not a directory. */ | |
580 | errno = ENOTDIR; | |
581 | ret = -1; | |
582 | goto end; | |
583 | } | |
584 | } | |
585 | ||
586 | /* | |
587 | * Let mkdir handle other errors as the caller expects mkdir | |
588 | * semantics. | |
589 | */ | |
590 | ret = mkdir(path, mode); | |
591 | end: | |
592 | return ret; | |
593 | } | |
594 | ||
2d851108 | 595 | /* |
d77dded2 | 596 | * Create directory using the given path and mode. |
2d851108 DG |
597 | * |
598 | * On success, return 0 else a negative error code. | |
599 | */ | |
90e535ef | 600 | LTTNG_HIDDEN |
d77dded2 JG |
601 | int utils_mkdir(const char *path, mode_t mode, int uid, int gid) |
602 | { | |
603 | int ret; | |
604 | ||
605 | if (uid < 0 || gid < 0) { | |
a98e236e | 606 | ret = mkdir_check_exists(path, mode); |
d77dded2 JG |
607 | } else { |
608 | ret = run_as_mkdir(path, mode, uid, gid); | |
609 | } | |
610 | if (ret < 0) { | |
611 | if (errno != EEXIST) { | |
612 | PERROR("mkdir %s, uid %d, gid %d", path ? path : "NULL", | |
613 | uid, gid); | |
614 | } else { | |
615 | ret = 0; | |
616 | } | |
617 | } | |
618 | ||
619 | return ret; | |
620 | } | |
621 | ||
622 | /* | |
623 | * Internal version of mkdir_recursive. Runs as the current user. | |
624 | * Don't call directly; use utils_mkdir_recursive(). | |
625 | * | |
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. | |
629 | */ | |
630 | LTTNG_HIDDEN | |
631 | int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode) | |
2d851108 DG |
632 | { |
633 | char *p, tmp[PATH_MAX]; | |
2d851108 DG |
634 | size_t len; |
635 | int ret; | |
636 | ||
637 | assert(path); | |
638 | ||
639 | ret = snprintf(tmp, sizeof(tmp), "%s", path); | |
640 | if (ret < 0) { | |
641 | PERROR("snprintf mkdir"); | |
642 | goto error; | |
643 | } | |
644 | ||
645 | len = ret; | |
646 | if (tmp[len - 1] == '/') { | |
647 | tmp[len - 1] = 0; | |
648 | } | |
649 | ||
650 | for (p = tmp + 1; *p; p++) { | |
651 | if (*p == '/') { | |
652 | *p = 0; | |
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)", | |
657 | tmp); | |
658 | ret = -1; | |
659 | goto error; | |
660 | } | |
a98e236e | 661 | ret = mkdir_check_exists(tmp, mode); |
2d851108 | 662 | if (ret < 0) { |
a98e236e | 663 | if (errno != EACCES) { |
0c7bcad5 MD |
664 | PERROR("mkdir recursive"); |
665 | ret = -errno; | |
666 | goto error; | |
2d851108 DG |
667 | } |
668 | } | |
669 | *p = '/'; | |
670 | } | |
671 | } | |
672 | ||
a98e236e | 673 | ret = mkdir_check_exists(tmp, mode); |
2d851108 | 674 | if (ret < 0) { |
a98e236e JG |
675 | PERROR("mkdir recursive last element"); |
676 | ret = -errno; | |
2d851108 DG |
677 | } |
678 | ||
679 | error: | |
680 | return ret; | |
681 | } | |
fe4477ee | 682 | |
d77dded2 JG |
683 | /* |
684 | * Recursively create directory using the given path and mode, under the | |
685 | * provided uid and gid. | |
686 | * | |
687 | * On success, return 0 else a negative error code. | |
688 | */ | |
689 | LTTNG_HIDDEN | |
690 | int utils_mkdir_recursive(const char *path, mode_t mode, int uid, int gid) | |
691 | { | |
692 | int ret; | |
693 | ||
694 | if (uid < 0 || gid < 0) { | |
695 | /* Run as current user. */ | |
696 | ret = _utils_mkdir_recursive_unsafe(path, mode); | |
697 | } else { | |
698 | ret = run_as_mkdir_recursive(path, mode, uid, gid); | |
699 | } | |
700 | if (ret < 0) { | |
701 | PERROR("mkdir %s, uid %d, gid %d", path ? path : "NULL", | |
702 | uid, gid); | |
703 | } | |
704 | ||
705 | return ret; | |
706 | } | |
707 | ||
fe4477ee | 708 | /* |
d77dded2 | 709 | * path is the output parameter. It needs to be PATH_MAX len. |
fe4477ee JD |
710 | * |
711 | * Return 0 on success or else a negative value. | |
712 | */ | |
7591bab1 MD |
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, | |
716 | const char *suffix) | |
fe4477ee | 717 | { |
7591bab1 MD |
718 | int ret; |
719 | char full_path[PATH_MAX]; | |
720 | char *path_name_suffix = NULL; | |
309167d2 | 721 | char *extra = NULL; |
fe4477ee | 722 | |
fe4477ee JD |
723 | ret = snprintf(full_path, sizeof(full_path), "%s/%s", |
724 | path_name, file_name); | |
725 | if (ret < 0) { | |
726 | PERROR("snprintf create output file"); | |
727 | goto error; | |
728 | } | |
729 | ||
309167d2 JD |
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); | |
735 | } else if (suffix) { | |
736 | ret = asprintf(&extra, "%s", suffix); | |
737 | } | |
738 | if (ret < 0) { | |
739 | PERROR("Allocating extra string to name"); | |
740 | goto error; | |
741 | } | |
742 | ||
fe4477ee | 743 | /* |
7591bab1 MD |
744 | * If we split the trace in multiple files, we have to add the count at |
745 | * the end of the tracefile name. | |
fe4477ee | 746 | */ |
309167d2 JD |
747 | if (extra) { |
748 | ret = asprintf(&path_name_suffix, "%s%s", full_path, extra); | |
fe4477ee | 749 | if (ret < 0) { |
309167d2 JD |
750 | PERROR("Allocating path name with extra string"); |
751 | goto error_free_suffix; | |
fe4477ee | 752 | } |
7591bab1 MD |
753 | strncpy(path, path_name_suffix, PATH_MAX - 1); |
754 | path[PATH_MAX - 1] = '\0'; | |
fe4477ee | 755 | } else { |
7591bab1 MD |
756 | strncpy(path, full_path, PATH_MAX - 1); |
757 | } | |
758 | path[PATH_MAX - 1] = '\0'; | |
759 | ret = 0; | |
760 | ||
761 | free(path_name_suffix); | |
762 | error_free_suffix: | |
763 | free(extra); | |
764 | error: | |
765 | return ret; | |
766 | } | |
767 | ||
768 | /* | |
769 | * Create the stream file on disk. | |
770 | * | |
771 | * Return 0 on success or else a negative value. | |
772 | */ | |
773 | LTTNG_HIDDEN | |
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) | |
776 | { | |
777 | int ret, flags, mode; | |
778 | char path[PATH_MAX]; | |
779 | ||
780 | ret = utils_stream_file_name(path, path_name, file_name, | |
781 | size, count, suffix); | |
782 | if (ret < 0) { | |
783 | goto error; | |
fe4477ee JD |
784 | } |
785 | ||
be96a7d1 | 786 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
0f907de1 | 787 | /* Open with 660 mode */ |
be96a7d1 DG |
788 | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
789 | ||
790 | if (uid < 0 || gid < 0) { | |
7591bab1 | 791 | ret = open(path, flags, mode); |
be96a7d1 | 792 | } else { |
7591bab1 | 793 | ret = run_as_open(path, flags, mode, uid, gid); |
be96a7d1 | 794 | } |
7591bab1 | 795 | if (ret < 0) { |
fe4477ee | 796 | PERROR("open stream path %s", path); |
fe4477ee | 797 | } |
7591bab1 MD |
798 | error: |
799 | return ret; | |
800 | } | |
fe4477ee | 801 | |
7591bab1 MD |
802 | /* |
803 | * Unlink the stream tracefile from disk. | |
804 | * | |
805 | * Return 0 on success or else a negative value. | |
806 | */ | |
807 | LTTNG_HIDDEN | |
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) | |
810 | { | |
811 | int ret; | |
812 | char path[PATH_MAX]; | |
813 | ||
814 | ret = utils_stream_file_name(path, path_name, file_name, | |
815 | size, count, suffix); | |
816 | if (ret < 0) { | |
817 | goto error; | |
818 | } | |
819 | if (uid < 0 || gid < 0) { | |
820 | ret = unlink(path); | |
821 | } else { | |
822 | ret = run_as_unlink(path, uid, gid); | |
7591bab1 MD |
823 | } |
824 | if (ret < 0) { | |
825 | goto error; | |
826 | } | |
fe4477ee | 827 | error: |
7591bab1 | 828 | DBG("utils_unlink_stream_file %s returns %d", path, ret); |
fe4477ee JD |
829 | return ret; |
830 | } | |
831 | ||
832 | /* | |
833 | * Change the output tracefile according to the given size and count The | |
834 | * new_count pointer is set during this operation. | |
835 | * | |
836 | * From the consumer, the stream lock MUST be held before calling this function | |
837 | * because we are modifying the stream status. | |
838 | * | |
839 | * Return 0 on success or else a negative value. | |
840 | */ | |
bc182241 | 841 | LTTNG_HIDDEN |
fe4477ee | 842 | int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size, |
309167d2 JD |
843 | uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count, |
844 | int *stream_fd) | |
fe4477ee JD |
845 | { |
846 | int ret; | |
847 | ||
309167d2 JD |
848 | assert(stream_fd); |
849 | ||
fe4477ee JD |
850 | ret = close(out_fd); |
851 | if (ret < 0) { | |
852 | PERROR("Closing tracefile"); | |
853 | goto error; | |
854 | } | |
855 | ||
856 | if (count > 0) { | |
7591bab1 MD |
857 | /* |
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 | |
868 | * achieves this. | |
869 | */ | |
93ec662e JD |
870 | if (new_count) { |
871 | *new_count = (*new_count + 1) % count; | |
872 | } | |
873 | ret = utils_unlink_stream_file(path_name, file_name, size, | |
874 | new_count ? *new_count : 0, uid, gid, 0); | |
7591bab1 MD |
875 | if (ret < 0 && errno != ENOENT) { |
876 | goto error; | |
877 | } | |
fe4477ee | 878 | } else { |
93ec662e JD |
879 | if (new_count) { |
880 | (*new_count)++; | |
881 | } | |
fe4477ee JD |
882 | } |
883 | ||
93ec662e JD |
884 | ret = utils_create_stream_file(path_name, file_name, size, |
885 | new_count ? *new_count : 0, uid, gid, 0); | |
309167d2 JD |
886 | if (ret < 0) { |
887 | goto error; | |
888 | } | |
889 | *stream_fd = ret; | |
890 | ||
891 | /* Success. */ | |
892 | ret = 0; | |
893 | ||
fe4477ee JD |
894 | error: |
895 | return ret; | |
896 | } | |
70d0b120 | 897 | |
70d0b120 SM |
898 | |
899 | /** | |
900 | * Parse a string that represents a size in human readable format. It | |
5983a922 | 901 | * supports decimal integers suffixed by 'k', 'K', 'M' or 'G'. |
70d0b120 SM |
902 | * |
903 | * The suffix multiply the integer by: | |
904 | * 'k': 1024 | |
905 | * 'M': 1024^2 | |
906 | * 'G': 1024^3 | |
907 | * | |
908 | * @param str The string to parse. | |
5983a922 | 909 | * @param size Pointer to a uint64_t that will be filled with the |
cfa9a5a2 | 910 | * resulting size. |
70d0b120 SM |
911 | * |
912 | * @return 0 on success, -1 on failure. | |
913 | */ | |
00a52467 | 914 | LTTNG_HIDDEN |
5983a922 | 915 | int utils_parse_size_suffix(const char * const str, uint64_t * const size) |
70d0b120 | 916 | { |
70d0b120 | 917 | int ret; |
5983a922 | 918 | uint64_t base_size; |
70d0b120 | 919 | long shift = 0; |
5983a922 SM |
920 | const char *str_end; |
921 | char *num_end; | |
70d0b120 SM |
922 | |
923 | if (!str) { | |
5983a922 | 924 | DBG("utils_parse_size_suffix: received a NULL string."); |
70d0b120 SM |
925 | ret = -1; |
926 | goto end; | |
927 | } | |
928 | ||
5983a922 SM |
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 '-'."); | |
70d0b120 | 932 | ret = -1; |
5983a922 | 933 | goto end; |
70d0b120 SM |
934 | } |
935 | ||
5983a922 SM |
936 | /* str_end will point to the \0 */ |
937 | str_end = str + strlen(str); | |
70d0b120 | 938 | errno = 0; |
5983a922 | 939 | base_size = strtoull(str, &num_end, 0); |
70d0b120 | 940 | if (errno != 0) { |
5983a922 | 941 | PERROR("utils_parse_size_suffix strtoull"); |
70d0b120 | 942 | ret = -1; |
5983a922 SM |
943 | goto end; |
944 | } | |
945 | ||
946 | if (num_end == str) { | |
947 | /* strtoull parsed nothing, not good. */ | |
948 | DBG("utils_parse_size_suffix: strtoull had nothing good to parse."); | |
949 | ret = -1; | |
950 | goto end; | |
951 | } | |
952 | ||
953 | /* Check if a prefix is present. */ | |
954 | switch (*num_end) { | |
955 | case 'G': | |
956 | shift = GIBI_LOG2; | |
957 | num_end++; | |
958 | break; | |
959 | case 'M': /* */ | |
960 | shift = MEBI_LOG2; | |
961 | num_end++; | |
962 | break; | |
963 | case 'K': | |
964 | case 'k': | |
965 | shift = KIBI_LOG2; | |
966 | num_end++; | |
967 | break; | |
968 | case '\0': | |
969 | break; | |
970 | default: | |
971 | DBG("utils_parse_size_suffix: invalid suffix."); | |
972 | ret = -1; | |
973 | goto end; | |
974 | } | |
975 | ||
976 | /* Check for garbage after the valid input. */ | |
977 | if (num_end != str_end) { | |
978 | DBG("utils_parse_size_suffix: Garbage after size string."); | |
979 | ret = -1; | |
980 | goto end; | |
70d0b120 SM |
981 | } |
982 | ||
983 | *size = base_size << shift; | |
984 | ||
985 | /* Check for overflow */ | |
986 | if ((*size >> shift) != base_size) { | |
5983a922 | 987 | DBG("utils_parse_size_suffix: oops, overflow detected."); |
70d0b120 | 988 | ret = -1; |
5983a922 | 989 | goto end; |
70d0b120 SM |
990 | } |
991 | ||
992 | ret = 0; | |
70d0b120 SM |
993 | end: |
994 | return ret; | |
995 | } | |
cfa9a5a2 DG |
996 | |
997 | /* | |
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). | |
1001 | */ | |
1002 | #if defined(__i386) || defined(__x86_64) | |
1003 | static inline unsigned int fls_u32(uint32_t x) | |
1004 | { | |
1005 | int r; | |
1006 | ||
1007 | asm("bsrl %1,%0\n\t" | |
1008 | "jnz 1f\n\t" | |
1009 | "movl $-1,%0\n\t" | |
1010 | "1:\n\t" | |
1011 | : "=r" (r) : "rm" (x)); | |
1012 | return r + 1; | |
1013 | } | |
1014 | #define HAS_FLS_U32 | |
1015 | #endif | |
1016 | ||
1017 | #ifndef HAS_FLS_U32 | |
1018 | static __attribute__((unused)) unsigned int fls_u32(uint32_t x) | |
1019 | { | |
1020 | unsigned int r = 32; | |
1021 | ||
1022 | if (!x) { | |
1023 | return 0; | |
1024 | } | |
1025 | if (!(x & 0xFFFF0000U)) { | |
1026 | x <<= 16; | |
1027 | r -= 16; | |
1028 | } | |
1029 | if (!(x & 0xFF000000U)) { | |
1030 | x <<= 8; | |
1031 | r -= 8; | |
1032 | } | |
1033 | if (!(x & 0xF0000000U)) { | |
1034 | x <<= 4; | |
1035 | r -= 4; | |
1036 | } | |
1037 | if (!(x & 0xC0000000U)) { | |
1038 | x <<= 2; | |
1039 | r -= 2; | |
1040 | } | |
1041 | if (!(x & 0x80000000U)) { | |
1042 | x <<= 1; | |
1043 | r -= 1; | |
1044 | } | |
1045 | return r; | |
1046 | } | |
1047 | #endif | |
1048 | ||
1049 | /* | |
1050 | * Return the minimum order for which x <= (1UL << order). | |
1051 | * Return -1 if x is 0. | |
1052 | */ | |
1053 | LTTNG_HIDDEN | |
1054 | int utils_get_count_order_u32(uint32_t x) | |
1055 | { | |
1056 | if (!x) { | |
1057 | return -1; | |
1058 | } | |
1059 | ||
1060 | return fls_u32(x - 1); | |
1061 | } | |
feb0f3e5 AM |
1062 | |
1063 | /** | |
1064 | * Obtain the value of LTTNG_HOME environment variable, if exists. | |
1065 | * Otherwise returns the value of HOME. | |
1066 | */ | |
00a52467 | 1067 | LTTNG_HIDDEN |
feb0f3e5 AM |
1068 | char *utils_get_home_dir(void) |
1069 | { | |
1070 | char *val = NULL; | |
04135dbd DG |
1071 | struct passwd *pwd; |
1072 | ||
e8fa9fb0 | 1073 | val = lttng_secure_getenv(DEFAULT_LTTNG_HOME_ENV_VAR); |
feb0f3e5 | 1074 | if (val != NULL) { |
04135dbd DG |
1075 | goto end; |
1076 | } | |
e8fa9fb0 | 1077 | val = lttng_secure_getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); |
04135dbd DG |
1078 | if (val != NULL) { |
1079 | goto end; | |
feb0f3e5 | 1080 | } |
04135dbd DG |
1081 | |
1082 | /* Fallback on the password file entry. */ | |
1083 | pwd = getpwuid(getuid()); | |
1084 | if (!pwd) { | |
1085 | goto end; | |
1086 | } | |
1087 | val = pwd->pw_dir; | |
1088 | ||
1089 | DBG3("Home directory is '%s'", val); | |
1090 | ||
1091 | end: | |
1092 | return val; | |
feb0f3e5 | 1093 | } |
26fe5938 | 1094 | |
fb198a11 JG |
1095 | /** |
1096 | * Get user's home directory. Dynamically allocated, must be freed | |
1097 | * by the caller. | |
1098 | */ | |
1099 | LTTNG_HIDDEN | |
1100 | char *utils_get_user_home_dir(uid_t uid) | |
1101 | { | |
1102 | struct passwd pwd; | |
1103 | struct passwd *result; | |
1104 | char *home_dir = NULL; | |
1105 | char *buf = NULL; | |
1106 | long buflen; | |
1107 | int ret; | |
1108 | ||
1109 | buflen = sysconf(_SC_GETPW_R_SIZE_MAX); | |
1110 | if (buflen == -1) { | |
1111 | goto end; | |
1112 | } | |
1113 | retry: | |
1114 | buf = zmalloc(buflen); | |
1115 | if (!buf) { | |
1116 | goto end; | |
1117 | } | |
1118 | ||
1119 | ret = getpwuid_r(uid, &pwd, buf, buflen, &result); | |
1120 | if (ret || !result) { | |
1121 | if (ret == ERANGE) { | |
1122 | free(buf); | |
1123 | buflen *= 2; | |
1124 | goto retry; | |
1125 | } | |
1126 | goto end; | |
1127 | } | |
1128 | ||
1129 | home_dir = strdup(pwd.pw_dir); | |
1130 | end: | |
1131 | free(buf); | |
1132 | return home_dir; | |
1133 | } | |
1134 | ||
fbb9748b JG |
1135 | /* |
1136 | * Obtain the value of LTTNG_KMOD_PROBES environment variable, if exists. | |
c9d42407 | 1137 | * Otherwise returns NULL. |
fbb9748b JG |
1138 | */ |
1139 | LTTNG_HIDDEN | |
1140 | char *utils_get_kmod_probes_list(void) | |
1141 | { | |
e8fa9fb0 | 1142 | return lttng_secure_getenv(DEFAULT_LTTNG_KMOD_PROBES); |
fbb9748b JG |
1143 | } |
1144 | ||
c9d42407 PP |
1145 | /* |
1146 | * Obtain the value of LTTNG_EXTRA_KMOD_PROBES environment variable, if | |
1147 | * exists. Otherwise returns NULL. | |
1148 | */ | |
1149 | LTTNG_HIDDEN | |
1150 | char *utils_get_extra_kmod_probes_list(void) | |
1151 | { | |
e8fa9fb0 | 1152 | return lttng_secure_getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES); |
c9d42407 PP |
1153 | } |
1154 | ||
26fe5938 DG |
1155 | /* |
1156 | * With the given format, fill dst with the time of len maximum siz. | |
1157 | * | |
1158 | * Return amount of bytes set in the buffer or else 0 on error. | |
1159 | */ | |
1160 | LTTNG_HIDDEN | |
1161 | size_t utils_get_current_time_str(const char *format, char *dst, size_t len) | |
1162 | { | |
1163 | size_t ret; | |
1164 | time_t rawtime; | |
1165 | struct tm *timeinfo; | |
1166 | ||
1167 | assert(format); | |
1168 | assert(dst); | |
1169 | ||
1170 | /* Get date and time for session path */ | |
1171 | time(&rawtime); | |
1172 | timeinfo = localtime(&rawtime); | |
1173 | ret = strftime(dst, len, format, timeinfo); | |
1174 | if (ret == 0) { | |
68e6efdd | 1175 | ERR("Unable to strftime with format %s at dst %p of len %zu", format, |
26fe5938 DG |
1176 | dst, len); |
1177 | } | |
1178 | ||
1179 | return ret; | |
1180 | } | |
6c71277b MD |
1181 | |
1182 | /* | |
1183 | * Return the group ID matching name, else 0 if it cannot be found. | |
1184 | */ | |
1185 | LTTNG_HIDDEN | |
1186 | gid_t utils_get_group_id(const char *name) | |
1187 | { | |
1188 | struct group *grp; | |
1189 | ||
1190 | grp = getgrnam(name); | |
1191 | if (!grp) { | |
1192 | static volatile int warn_once; | |
1193 | ||
1194 | if (!warn_once) { | |
1195 | WARN("No tracing group detected"); | |
1196 | warn_once = 1; | |
1197 | } | |
1198 | return 0; | |
1199 | } | |
1200 | return grp->gr_gid; | |
1201 | } | |
8db0dc00 JG |
1202 | |
1203 | /* | |
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 | |
1207 | * allocation fails. | |
1208 | */ | |
1209 | LTTNG_HIDDEN | |
1210 | char *utils_generate_optstring(const struct option *long_options, | |
1211 | size_t opt_count) | |
1212 | { | |
1213 | int i; | |
1214 | size_t string_len = opt_count, str_pos = 0; | |
1215 | char *optstring; | |
1216 | ||
1217 | /* | |
1218 | * Compute the necessary string length. One letter per option, two when an | |
1219 | * argument is necessary, and a trailing NULL. | |
1220 | */ | |
1221 | for (i = 0; i < opt_count; i++) { | |
1222 | string_len += long_options[i].has_arg ? 1 : 0; | |
1223 | } | |
1224 | ||
1225 | optstring = zmalloc(string_len); | |
1226 | if (!optstring) { | |
1227 | goto end; | |
1228 | } | |
1229 | ||
1230 | for (i = 0; i < opt_count; i++) { | |
1231 | if (!long_options[i].name) { | |
1232 | /* Got to the trailing NULL element */ | |
1233 | break; | |
1234 | } | |
1235 | ||
a596dcb9 JG |
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++] = ':'; | |
1240 | } | |
8db0dc00 JG |
1241 | } |
1242 | } | |
1243 | ||
1244 | end: | |
1245 | return optstring; | |
1246 | } | |
3d071855 MD |
1247 | |
1248 | /* | |
1249 | * Try to remove a hierarchy of empty directories, recursively. Don't unlink | |
9529ec1b | 1250 | * any file. Try to rmdir any empty directory within the hierarchy. |
3d071855 MD |
1251 | */ |
1252 | LTTNG_HIDDEN | |
1253 | int utils_recursive_rmdir(const char *path) | |
1254 | { | |
1255 | DIR *dir; | |
7a946beb | 1256 | size_t path_len; |
9529ec1b | 1257 | int dir_fd, ret = 0, closeret, is_empty = 1; |
3d071855 MD |
1258 | struct dirent *entry; |
1259 | ||
1260 | /* Open directory */ | |
1261 | dir = opendir(path); | |
1262 | if (!dir) { | |
1263 | PERROR("Cannot open '%s' path", path); | |
1264 | return -1; | |
1265 | } | |
5a2451c9 | 1266 | dir_fd = lttng_dirfd(dir); |
3d071855 | 1267 | if (dir_fd < 0) { |
5a2451c9 | 1268 | PERROR("lttng_dirfd"); |
3d071855 MD |
1269 | return -1; |
1270 | } | |
1271 | ||
7a946beb | 1272 | path_len = strlen(path); |
3d071855 | 1273 | while ((entry = readdir(dir))) { |
7a946beb MJ |
1274 | struct stat st; |
1275 | size_t name_len; | |
1276 | char filename[PATH_MAX]; | |
1277 | ||
3763af87 JG |
1278 | if (!strcmp(entry->d_name, ".") |
1279 | || !strcmp(entry->d_name, "..")) { | |
1280 | continue; | |
1281 | } | |
1282 | ||
7a946beb MJ |
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); | |
1287 | continue; | |
1288 | } | |
1289 | if (snprintf(filename, sizeof(filename), "%s/%s", | |
1290 | path, entry->d_name) < 0) { | |
1291 | ERR("Failed to format path."); | |
1292 | continue; | |
1293 | } | |
1294 | ||
1295 | if (stat(filename, &st)) { | |
1296 | PERROR("stat"); | |
1297 | continue; | |
1298 | } | |
1299 | ||
1300 | if (S_ISDIR(st.st_mode)) { | |
3d071855 MD |
1301 | char subpath[PATH_MAX]; |
1302 | ||
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); | |
9529ec1b MD |
1309 | if (utils_recursive_rmdir(subpath)) { |
1310 | is_empty = 0; | |
3d071855 | 1311 | } |
7a946beb | 1312 | } else if (S_ISREG(st.st_mode)) { |
9529ec1b | 1313 | is_empty = 0; |
7a946beb | 1314 | } else { |
3d071855 MD |
1315 | ret = -EINVAL; |
1316 | goto end; | |
1317 | } | |
1318 | } | |
1319 | end: | |
1320 | closeret = closedir(dir); | |
1321 | if (closeret) { | |
1322 | PERROR("closedir"); | |
1323 | } | |
9529ec1b | 1324 | if (is_empty) { |
3d071855 MD |
1325 | DBG3("Attempting rmdir %s", path); |
1326 | ret = rmdir(path); | |
1327 | } | |
1328 | return ret; | |
1329 | } | |
93ec662e JD |
1330 | |
1331 | LTTNG_HIDDEN | |
1332 | int utils_truncate_stream_file(int fd, off_t length) | |
1333 | { | |
1334 | int ret; | |
1335 | ||
1336 | ret = ftruncate(fd, length); | |
1337 | if (ret < 0) { | |
1338 | PERROR("ftruncate"); | |
1339 | goto end; | |
1340 | } | |
1341 | ret = lseek(fd, length, SEEK_SET); | |
1342 | if (ret < 0) { | |
1343 | PERROR("lseek"); | |
1344 | goto end; | |
1345 | } | |
93ec662e JD |
1346 | end: |
1347 | return ret; | |
1348 | } | |
4ba92f18 PP |
1349 | |
1350 | static const char *get_man_bin_path(void) | |
1351 | { | |
89600047 | 1352 | char *env_man_path = lttng_secure_getenv(DEFAULT_MAN_BIN_PATH_ENV); |
4ba92f18 PP |
1353 | |
1354 | if (env_man_path) { | |
1355 | return env_man_path; | |
1356 | } | |
1357 | ||
1358 | return DEFAULT_MAN_BIN_PATH; | |
1359 | } | |
1360 | ||
1361 | LTTNG_HIDDEN | |
1362 | int utils_show_man_page(int section, const char *page_name) | |
1363 | { | |
1364 | char section_string[8]; | |
1365 | const char *man_bin_path = get_man_bin_path(); | |
1366 | int ret; | |
1367 | ||
1368 | /* Section integer -> section string */ | |
1369 | ret = sprintf(section_string, "%d", section); | |
1370 | assert(ret > 0 && ret < 8); | |
1371 | ||
1372 | /* | |
1373 | * Execute man pager. | |
1374 | * | |
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. | |
1378 | */ | |
1379 | ret = execlp(man_bin_path, "man", "--manpath", MANPATH, | |
1380 | section_string, page_name, NULL); | |
1381 | return ret; | |
1382 | } |