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); |
b86d5f3f | 192 | free(try_path); |
32bd4678 MJ |
193 | if (try_path_prev != try_path) { |
194 | free(try_path_prev); | |
195 | } | |
5154230f RB |
196 | return NULL; |
197 | } | |
198 | ||
81b86775 | 199 | /* |
3d229795 RB |
200 | * Make a full resolution of the given path even if it doesn't exist. |
201 | * This function uses the utils_partial_realpath function to resolve | |
202 | * symlinks and relatives paths at the start of the string, and | |
203 | * implements functionnalities to resolve the './' and '../' strings | |
204 | * in the middle of a path. This function is only necessary because | |
205 | * realpath(3) does not accept to resolve unexistent paths. | |
206 | * The returned string was allocated in the function, it is thus of | |
207 | * the responsibility of the caller to free this memory. | |
81b86775 | 208 | */ |
90e535ef | 209 | LTTNG_HIDDEN |
81b86775 DG |
210 | char *utils_expand_path(const char *path) |
211 | { | |
3d229795 | 212 | char *next, *previous, *slash, *start_path, *absolute_path = NULL; |
5de083f4 RB |
213 | char *last_token; |
214 | int is_dot, is_dotdot; | |
81b86775 DG |
215 | |
216 | /* Safety net */ | |
217 | if (path == NULL) { | |
218 | goto error; | |
219 | } | |
220 | ||
3d229795 RB |
221 | /* Allocate memory for the absolute_path */ |
222 | absolute_path = zmalloc(PATH_MAX); | |
223 | if (absolute_path == NULL) { | |
81b86775 DG |
224 | PERROR("zmalloc expand path"); |
225 | goto error; | |
226 | } | |
227 | ||
3d229795 RB |
228 | /* |
229 | * If the path is not already absolute nor explicitly relative, | |
230 | * consider we're in the current directory | |
231 | */ | |
232 | if (*path != '/' && strncmp(path, "./", 2) != 0 && | |
233 | strncmp(path, "../", 3) != 0) { | |
234 | snprintf(absolute_path, PATH_MAX, "./%s", path); | |
2dcd84b7 | 235 | /* Else, we just copy the path */ |
116f95d9 | 236 | } else { |
3d229795 RB |
237 | strncpy(absolute_path, path, PATH_MAX); |
238 | } | |
116f95d9 | 239 | |
3d229795 RB |
240 | /* Resolve partially our path */ |
241 | absolute_path = utils_partial_realpath(absolute_path, | |
242 | absolute_path, PATH_MAX); | |
116f95d9 | 243 | |
3d229795 RB |
244 | /* As long as we find '/./' in the working_path string */ |
245 | while ((next = strstr(absolute_path, "/./"))) { | |
116f95d9 | 246 | |
3d229795 | 247 | /* We prepare the start_path not containing it */ |
f5436bfc | 248 | start_path = lttng_strndup(absolute_path, next - absolute_path); |
d9dbcf5e | 249 | if (!start_path) { |
f5436bfc | 250 | PERROR("lttng_strndup"); |
d9dbcf5e MD |
251 | goto error; |
252 | } | |
3d229795 RB |
253 | /* And we concatenate it with the part after this string */ |
254 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 2); | |
116f95d9 | 255 | |
3d229795 RB |
256 | free(start_path); |
257 | } | |
116f95d9 | 258 | |
3d229795 RB |
259 | /* As long as we find '/../' in the working_path string */ |
260 | while ((next = strstr(absolute_path, "/../"))) { | |
261 | /* We find the last level of directory */ | |
262 | previous = absolute_path; | |
263 | while ((slash = strpbrk(previous, "/")) && slash != next) { | |
264 | previous = slash + 1; | |
81b86775 | 265 | } |
81b86775 | 266 | |
3d229795 | 267 | /* Then we prepare the start_path not containing it */ |
f5436bfc | 268 | start_path = lttng_strndup(absolute_path, previous - absolute_path); |
d9dbcf5e | 269 | if (!start_path) { |
f5436bfc | 270 | PERROR("lttng_strndup"); |
d9dbcf5e MD |
271 | goto error; |
272 | } | |
3d229795 RB |
273 | |
274 | /* And we concatenate it with the part after the '/../' */ | |
275 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 4); | |
276 | ||
277 | /* We can free the memory used for the start path*/ | |
278 | free(start_path); | |
279 | ||
280 | /* Then we verify for symlinks using partial_realpath */ | |
281 | absolute_path = utils_partial_realpath(absolute_path, | |
282 | absolute_path, PATH_MAX); | |
116f95d9 | 283 | } |
81b86775 | 284 | |
5de083f4 RB |
285 | /* Identify the last token */ |
286 | last_token = strrchr(absolute_path, '/'); | |
287 | ||
288 | /* Verify that this token is not a relative path */ | |
289 | is_dotdot = (strcmp(last_token, "/..") == 0); | |
290 | is_dot = (strcmp(last_token, "/.") == 0); | |
291 | ||
292 | /* If it is, take action */ | |
293 | if (is_dot || is_dotdot) { | |
294 | /* For both, remove this token */ | |
295 | *last_token = '\0'; | |
296 | ||
297 | /* If it was a reference to parent directory, go back one more time */ | |
298 | if (is_dotdot) { | |
299 | last_token = strrchr(absolute_path, '/'); | |
300 | ||
301 | /* If there was only one level left, we keep the first '/' */ | |
302 | if (last_token == absolute_path) { | |
303 | last_token++; | |
304 | } | |
305 | ||
306 | *last_token = '\0'; | |
307 | } | |
308 | } | |
309 | ||
3d229795 | 310 | return absolute_path; |
81b86775 DG |
311 | |
312 | error: | |
3d229795 | 313 | free(absolute_path); |
81b86775 DG |
314 | return NULL; |
315 | } | |
316 | ||
317 | /* | |
318 | * Create a pipe in dst. | |
319 | */ | |
90e535ef | 320 | LTTNG_HIDDEN |
81b86775 DG |
321 | int utils_create_pipe(int *dst) |
322 | { | |
323 | int ret; | |
324 | ||
325 | if (dst == NULL) { | |
326 | return -1; | |
327 | } | |
328 | ||
329 | ret = pipe(dst); | |
330 | if (ret < 0) { | |
331 | PERROR("create pipe"); | |
332 | } | |
333 | ||
334 | return ret; | |
335 | } | |
336 | ||
337 | /* | |
338 | * Create pipe and set CLOEXEC flag to both fd. | |
339 | * | |
340 | * Make sure the pipe opened by this function are closed at some point. Use | |
341 | * utils_close_pipe(). | |
342 | */ | |
90e535ef | 343 | LTTNG_HIDDEN |
81b86775 DG |
344 | int utils_create_pipe_cloexec(int *dst) |
345 | { | |
346 | int ret, i; | |
347 | ||
348 | if (dst == NULL) { | |
349 | return -1; | |
350 | } | |
351 | ||
352 | ret = utils_create_pipe(dst); | |
353 | if (ret < 0) { | |
354 | goto error; | |
355 | } | |
356 | ||
357 | for (i = 0; i < 2; i++) { | |
358 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); | |
359 | if (ret < 0) { | |
360 | PERROR("fcntl pipe cloexec"); | |
361 | goto error; | |
362 | } | |
363 | } | |
364 | ||
365 | error: | |
366 | return ret; | |
367 | } | |
368 | ||
094f381c MD |
369 | /* |
370 | * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK. | |
371 | * | |
372 | * Make sure the pipe opened by this function are closed at some point. Use | |
373 | * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to | |
374 | * support OSes other than Linux 2.6.23+. | |
375 | */ | |
376 | LTTNG_HIDDEN | |
377 | int utils_create_pipe_cloexec_nonblock(int *dst) | |
378 | { | |
379 | int ret, i; | |
380 | ||
381 | if (dst == NULL) { | |
382 | return -1; | |
383 | } | |
384 | ||
385 | ret = utils_create_pipe(dst); | |
386 | if (ret < 0) { | |
387 | goto error; | |
388 | } | |
389 | ||
390 | for (i = 0; i < 2; i++) { | |
391 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); | |
392 | if (ret < 0) { | |
393 | PERROR("fcntl pipe cloexec"); | |
394 | goto error; | |
395 | } | |
396 | /* | |
397 | * Note: we override any flag that could have been | |
398 | * previously set on the fd. | |
399 | */ | |
400 | ret = fcntl(dst[i], F_SETFL, O_NONBLOCK); | |
401 | if (ret < 0) { | |
402 | PERROR("fcntl pipe nonblock"); | |
403 | goto error; | |
404 | } | |
405 | } | |
406 | ||
407 | error: | |
408 | return ret; | |
409 | } | |
410 | ||
81b86775 DG |
411 | /* |
412 | * Close both read and write side of the pipe. | |
413 | */ | |
90e535ef | 414 | LTTNG_HIDDEN |
81b86775 DG |
415 | void utils_close_pipe(int *src) |
416 | { | |
417 | int i, ret; | |
418 | ||
419 | if (src == NULL) { | |
420 | return; | |
421 | } | |
422 | ||
423 | for (i = 0; i < 2; i++) { | |
424 | /* Safety check */ | |
425 | if (src[i] < 0) { | |
426 | continue; | |
427 | } | |
428 | ||
429 | ret = close(src[i]); | |
430 | if (ret) { | |
431 | PERROR("close pipe"); | |
432 | } | |
433 | } | |
434 | } | |
a4b92340 DG |
435 | |
436 | /* | |
437 | * Create a new string using two strings range. | |
438 | */ | |
90e535ef | 439 | LTTNG_HIDDEN |
a4b92340 DG |
440 | char *utils_strdupdelim(const char *begin, const char *end) |
441 | { | |
442 | char *str; | |
443 | ||
444 | str = zmalloc(end - begin + 1); | |
445 | if (str == NULL) { | |
446 | PERROR("zmalloc strdupdelim"); | |
447 | goto error; | |
448 | } | |
449 | ||
450 | memcpy(str, begin, end - begin); | |
451 | str[end - begin] = '\0'; | |
452 | ||
453 | error: | |
454 | return str; | |
455 | } | |
b662582b DG |
456 | |
457 | /* | |
458 | * Set CLOEXEC flag to the give file descriptor. | |
459 | */ | |
90e535ef | 460 | LTTNG_HIDDEN |
b662582b DG |
461 | int utils_set_fd_cloexec(int fd) |
462 | { | |
463 | int ret; | |
464 | ||
465 | if (fd < 0) { | |
466 | ret = -EINVAL; | |
467 | goto end; | |
468 | } | |
469 | ||
470 | ret = fcntl(fd, F_SETFD, FD_CLOEXEC); | |
471 | if (ret < 0) { | |
472 | PERROR("fcntl cloexec"); | |
473 | ret = -errno; | |
474 | } | |
475 | ||
476 | end: | |
477 | return ret; | |
478 | } | |
35f90c40 DG |
479 | |
480 | /* | |
481 | * Create pid file to the given path and filename. | |
482 | */ | |
90e535ef | 483 | LTTNG_HIDDEN |
35f90c40 DG |
484 | int utils_create_pid_file(pid_t pid, const char *filepath) |
485 | { | |
486 | int ret; | |
487 | FILE *fp; | |
488 | ||
489 | assert(filepath); | |
490 | ||
491 | fp = fopen(filepath, "w"); | |
492 | if (fp == NULL) { | |
493 | PERROR("open pid file %s", filepath); | |
494 | ret = -1; | |
495 | goto error; | |
496 | } | |
497 | ||
d1f721c5 | 498 | ret = fprintf(fp, "%d\n", (int) pid); |
35f90c40 DG |
499 | if (ret < 0) { |
500 | PERROR("fprintf pid file"); | |
e205d79b | 501 | goto error; |
35f90c40 DG |
502 | } |
503 | ||
e205d79b MD |
504 | if (fclose(fp)) { |
505 | PERROR("fclose"); | |
506 | } | |
d1f721c5 | 507 | DBG("Pid %d written in file %s", (int) pid, filepath); |
e205d79b | 508 | ret = 0; |
35f90c40 DG |
509 | error: |
510 | return ret; | |
511 | } | |
2d851108 | 512 | |
c9cb3e7d JG |
513 | /* |
514 | * Create lock file to the given path and filename. | |
515 | * Returns the associated file descriptor, -1 on error. | |
516 | */ | |
517 | LTTNG_HIDDEN | |
518 | int utils_create_lock_file(const char *filepath) | |
519 | { | |
520 | int ret; | |
521 | int fd; | |
77e7fddf | 522 | struct flock lock; |
c9cb3e7d JG |
523 | |
524 | assert(filepath); | |
525 | ||
77e7fddf MJ |
526 | memset(&lock, 0, sizeof(lock)); |
527 | fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | | |
528 | S_IRGRP | S_IWGRP); | |
c9cb3e7d JG |
529 | if (fd < 0) { |
530 | PERROR("open lock file %s", filepath); | |
531 | ret = -1; | |
532 | goto error; | |
533 | } | |
534 | ||
535 | /* | |
536 | * Attempt to lock the file. If this fails, there is | |
537 | * already a process using the same lock file running | |
538 | * and we should exit. | |
539 | */ | |
77e7fddf MJ |
540 | lock.l_whence = SEEK_SET; |
541 | lock.l_type = F_WRLCK; | |
542 | ||
543 | ret = fcntl(fd, F_SETLK, &lock); | |
544 | if (ret == -1) { | |
545 | PERROR("fcntl lock file"); | |
208ff148 | 546 | ERR("Could not get lock file %s, another instance is running.", |
c9cb3e7d | 547 | filepath); |
ffb0b851 JG |
548 | if (close(fd)) { |
549 | PERROR("close lock file"); | |
550 | } | |
c9cb3e7d JG |
551 | fd = ret; |
552 | goto error; | |
553 | } | |
554 | ||
555 | error: | |
556 | return fd; | |
557 | } | |
558 | ||
a98e236e JG |
559 | /* |
560 | * On some filesystems (e.g. nfs), mkdir will validate access rights before | |
561 | * checking for the existence of the path element. This means that on a setup | |
562 | * where "/home/" is a mounted NFS share, and running as an unpriviledged user, | |
563 | * recursively creating a path of the form "/home/my_user/trace/" will fail with | |
564 | * EACCES on mkdir("/home", ...). | |
565 | * | |
566 | * Performing a stat(...) on the path to check for existence allows us to | |
567 | * work around this behaviour. | |
568 | */ | |
569 | static | |
570 | int mkdir_check_exists(const char *path, mode_t mode) | |
571 | { | |
572 | int ret = 0; | |
573 | struct stat st; | |
574 | ||
575 | ret = stat(path, &st); | |
576 | if (ret == 0) { | |
577 | if (S_ISDIR(st.st_mode)) { | |
578 | /* Directory exists, skip. */ | |
579 | goto end; | |
580 | } else { | |
581 | /* Exists, but is not a directory. */ | |
582 | errno = ENOTDIR; | |
583 | ret = -1; | |
584 | goto end; | |
585 | } | |
586 | } | |
587 | ||
588 | /* | |
589 | * Let mkdir handle other errors as the caller expects mkdir | |
590 | * semantics. | |
591 | */ | |
592 | ret = mkdir(path, mode); | |
593 | end: | |
594 | return ret; | |
595 | } | |
596 | ||
2d851108 | 597 | /* |
d77dded2 | 598 | * Create directory using the given path and mode. |
2d851108 DG |
599 | * |
600 | * On success, return 0 else a negative error code. | |
601 | */ | |
90e535ef | 602 | LTTNG_HIDDEN |
d77dded2 JG |
603 | int utils_mkdir(const char *path, mode_t mode, int uid, int gid) |
604 | { | |
605 | int ret; | |
606 | ||
607 | if (uid < 0 || gid < 0) { | |
a98e236e | 608 | ret = mkdir_check_exists(path, mode); |
d77dded2 JG |
609 | } else { |
610 | ret = run_as_mkdir(path, mode, uid, gid); | |
611 | } | |
612 | if (ret < 0) { | |
613 | if (errno != EEXIST) { | |
614 | PERROR("mkdir %s, uid %d, gid %d", path ? path : "NULL", | |
615 | uid, gid); | |
616 | } else { | |
617 | ret = 0; | |
618 | } | |
619 | } | |
620 | ||
621 | return ret; | |
622 | } | |
623 | ||
624 | /* | |
625 | * Internal version of mkdir_recursive. Runs as the current user. | |
626 | * Don't call directly; use utils_mkdir_recursive(). | |
627 | * | |
628 | * This function is ominously marked as "unsafe" since it should only | |
629 | * be called by a caller that has transitioned to the uid and gid under which | |
630 | * the directory creation should occur. | |
631 | */ | |
632 | LTTNG_HIDDEN | |
633 | int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode) | |
2d851108 DG |
634 | { |
635 | char *p, tmp[PATH_MAX]; | |
2d851108 DG |
636 | size_t len; |
637 | int ret; | |
638 | ||
639 | assert(path); | |
640 | ||
641 | ret = snprintf(tmp, sizeof(tmp), "%s", path); | |
642 | if (ret < 0) { | |
643 | PERROR("snprintf mkdir"); | |
644 | goto error; | |
645 | } | |
646 | ||
647 | len = ret; | |
648 | if (tmp[len - 1] == '/') { | |
649 | tmp[len - 1] = 0; | |
650 | } | |
651 | ||
652 | for (p = tmp + 1; *p; p++) { | |
653 | if (*p == '/') { | |
654 | *p = 0; | |
655 | if (tmp[strlen(tmp) - 1] == '.' && | |
656 | tmp[strlen(tmp) - 2] == '.' && | |
657 | tmp[strlen(tmp) - 3] == '/') { | |
658 | ERR("Using '/../' is not permitted in the trace path (%s)", | |
659 | tmp); | |
660 | ret = -1; | |
661 | goto error; | |
662 | } | |
a98e236e | 663 | ret = mkdir_check_exists(tmp, mode); |
2d851108 | 664 | if (ret < 0) { |
a98e236e | 665 | if (errno != EACCES) { |
0c7bcad5 MD |
666 | PERROR("mkdir recursive"); |
667 | ret = -errno; | |
668 | goto error; | |
2d851108 DG |
669 | } |
670 | } | |
671 | *p = '/'; | |
672 | } | |
673 | } | |
674 | ||
a98e236e | 675 | ret = mkdir_check_exists(tmp, mode); |
2d851108 | 676 | if (ret < 0) { |
a98e236e JG |
677 | PERROR("mkdir recursive last element"); |
678 | ret = -errno; | |
2d851108 DG |
679 | } |
680 | ||
681 | error: | |
682 | return ret; | |
683 | } | |
fe4477ee | 684 | |
d77dded2 JG |
685 | /* |
686 | * Recursively create directory using the given path and mode, under the | |
687 | * provided uid and gid. | |
688 | * | |
689 | * On success, return 0 else a negative error code. | |
690 | */ | |
691 | LTTNG_HIDDEN | |
692 | int utils_mkdir_recursive(const char *path, mode_t mode, int uid, int gid) | |
693 | { | |
694 | int ret; | |
695 | ||
696 | if (uid < 0 || gid < 0) { | |
697 | /* Run as current user. */ | |
698 | ret = _utils_mkdir_recursive_unsafe(path, mode); | |
699 | } else { | |
700 | ret = run_as_mkdir_recursive(path, mode, uid, gid); | |
701 | } | |
702 | if (ret < 0) { | |
703 | PERROR("mkdir %s, uid %d, gid %d", path ? path : "NULL", | |
704 | uid, gid); | |
705 | } | |
706 | ||
707 | return ret; | |
708 | } | |
709 | ||
fe4477ee | 710 | /* |
d77dded2 | 711 | * path is the output parameter. It needs to be PATH_MAX len. |
fe4477ee JD |
712 | * |
713 | * Return 0 on success or else a negative value. | |
714 | */ | |
7591bab1 MD |
715 | static int utils_stream_file_name(char *path, |
716 | const char *path_name, const char *file_name, | |
717 | uint64_t size, uint64_t count, | |
718 | const char *suffix) | |
fe4477ee | 719 | { |
7591bab1 MD |
720 | int ret; |
721 | char full_path[PATH_MAX]; | |
722 | char *path_name_suffix = NULL; | |
309167d2 | 723 | char *extra = NULL; |
fe4477ee | 724 | |
fe4477ee JD |
725 | ret = snprintf(full_path, sizeof(full_path), "%s/%s", |
726 | path_name, file_name); | |
727 | if (ret < 0) { | |
728 | PERROR("snprintf create output file"); | |
729 | goto error; | |
730 | } | |
731 | ||
309167d2 JD |
732 | /* Setup extra string if suffix or/and a count is needed. */ |
733 | if (size > 0 && suffix) { | |
734 | ret = asprintf(&extra, "_%" PRIu64 "%s", count, suffix); | |
735 | } else if (size > 0) { | |
736 | ret = asprintf(&extra, "_%" PRIu64, count); | |
737 | } else if (suffix) { | |
738 | ret = asprintf(&extra, "%s", suffix); | |
739 | } | |
740 | if (ret < 0) { | |
741 | PERROR("Allocating extra string to name"); | |
742 | goto error; | |
743 | } | |
744 | ||
fe4477ee | 745 | /* |
7591bab1 MD |
746 | * If we split the trace in multiple files, we have to add the count at |
747 | * the end of the tracefile name. | |
fe4477ee | 748 | */ |
309167d2 JD |
749 | if (extra) { |
750 | ret = asprintf(&path_name_suffix, "%s%s", full_path, extra); | |
fe4477ee | 751 | if (ret < 0) { |
309167d2 JD |
752 | PERROR("Allocating path name with extra string"); |
753 | goto error_free_suffix; | |
fe4477ee | 754 | } |
7591bab1 MD |
755 | strncpy(path, path_name_suffix, PATH_MAX - 1); |
756 | path[PATH_MAX - 1] = '\0'; | |
fe4477ee | 757 | } else { |
7591bab1 MD |
758 | strncpy(path, full_path, PATH_MAX - 1); |
759 | } | |
760 | path[PATH_MAX - 1] = '\0'; | |
761 | ret = 0; | |
762 | ||
763 | free(path_name_suffix); | |
764 | error_free_suffix: | |
765 | free(extra); | |
766 | error: | |
767 | return ret; | |
768 | } | |
769 | ||
770 | /* | |
771 | * Create the stream file on disk. | |
772 | * | |
773 | * Return 0 on success or else a negative value. | |
774 | */ | |
775 | LTTNG_HIDDEN | |
776 | int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size, | |
777 | uint64_t count, int uid, int gid, char *suffix) | |
778 | { | |
779 | int ret, flags, mode; | |
780 | char path[PATH_MAX]; | |
781 | ||
782 | ret = utils_stream_file_name(path, path_name, file_name, | |
783 | size, count, suffix); | |
784 | if (ret < 0) { | |
785 | goto error; | |
fe4477ee JD |
786 | } |
787 | ||
be96a7d1 | 788 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
0f907de1 | 789 | /* Open with 660 mode */ |
be96a7d1 DG |
790 | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
791 | ||
792 | if (uid < 0 || gid < 0) { | |
7591bab1 | 793 | ret = open(path, flags, mode); |
be96a7d1 | 794 | } else { |
7591bab1 | 795 | ret = run_as_open(path, flags, mode, uid, gid); |
be96a7d1 | 796 | } |
7591bab1 | 797 | if (ret < 0) { |
fe4477ee | 798 | PERROR("open stream path %s", path); |
fe4477ee | 799 | } |
7591bab1 MD |
800 | error: |
801 | return ret; | |
802 | } | |
fe4477ee | 803 | |
7591bab1 MD |
804 | /* |
805 | * Unlink the stream tracefile from disk. | |
806 | * | |
807 | * Return 0 on success or else a negative value. | |
808 | */ | |
809 | LTTNG_HIDDEN | |
810 | int utils_unlink_stream_file(const char *path_name, char *file_name, uint64_t size, | |
811 | uint64_t count, int uid, int gid, char *suffix) | |
812 | { | |
813 | int ret; | |
814 | char path[PATH_MAX]; | |
815 | ||
816 | ret = utils_stream_file_name(path, path_name, file_name, | |
817 | size, count, suffix); | |
818 | if (ret < 0) { | |
819 | goto error; | |
820 | } | |
821 | if (uid < 0 || gid < 0) { | |
822 | ret = unlink(path); | |
823 | } else { | |
824 | ret = run_as_unlink(path, uid, gid); | |
7591bab1 MD |
825 | } |
826 | if (ret < 0) { | |
827 | goto error; | |
828 | } | |
fe4477ee | 829 | error: |
7591bab1 | 830 | DBG("utils_unlink_stream_file %s returns %d", path, ret); |
fe4477ee JD |
831 | return ret; |
832 | } | |
833 | ||
834 | /* | |
835 | * Change the output tracefile according to the given size and count The | |
836 | * new_count pointer is set during this operation. | |
837 | * | |
838 | * From the consumer, the stream lock MUST be held before calling this function | |
839 | * because we are modifying the stream status. | |
840 | * | |
841 | * Return 0 on success or else a negative value. | |
842 | */ | |
bc182241 | 843 | LTTNG_HIDDEN |
fe4477ee | 844 | int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size, |
309167d2 JD |
845 | uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count, |
846 | int *stream_fd) | |
fe4477ee JD |
847 | { |
848 | int ret; | |
849 | ||
309167d2 JD |
850 | assert(stream_fd); |
851 | ||
fe4477ee JD |
852 | ret = close(out_fd); |
853 | if (ret < 0) { | |
854 | PERROR("Closing tracefile"); | |
855 | goto error; | |
856 | } | |
b5b0c181 | 857 | *stream_fd = -1; |
fe4477ee JD |
858 | |
859 | if (count > 0) { | |
7591bab1 MD |
860 | /* |
861 | * In tracefile rotation, for the relay daemon we need | |
862 | * to unlink the old file if present, because it may | |
863 | * still be open in reading by the live thread, and we | |
864 | * need to ensure that we do not overwrite the content | |
865 | * between get_index and get_packet. Since we have no | |
866 | * way to verify integrity of the data content compared | |
867 | * to the associated index, we need to ensure the reader | |
868 | * has exclusive access to the file content, and that | |
869 | * the open of the data file is performed in get_index. | |
870 | * Unlinking the old file rather than overwriting it | |
871 | * achieves this. | |
872 | */ | |
93ec662e JD |
873 | if (new_count) { |
874 | *new_count = (*new_count + 1) % count; | |
875 | } | |
876 | ret = utils_unlink_stream_file(path_name, file_name, size, | |
877 | new_count ? *new_count : 0, uid, gid, 0); | |
7591bab1 MD |
878 | if (ret < 0 && errno != ENOENT) { |
879 | goto error; | |
880 | } | |
fe4477ee | 881 | } else { |
93ec662e JD |
882 | if (new_count) { |
883 | (*new_count)++; | |
884 | } | |
fe4477ee JD |
885 | } |
886 | ||
93ec662e JD |
887 | ret = utils_create_stream_file(path_name, file_name, size, |
888 | new_count ? *new_count : 0, uid, gid, 0); | |
309167d2 JD |
889 | if (ret < 0) { |
890 | goto error; | |
891 | } | |
892 | *stream_fd = ret; | |
893 | ||
894 | /* Success. */ | |
895 | ret = 0; | |
896 | ||
fe4477ee JD |
897 | error: |
898 | return ret; | |
899 | } | |
70d0b120 | 900 | |
70d0b120 SM |
901 | |
902 | /** | |
903 | * Parse a string that represents a size in human readable format. It | |
5983a922 | 904 | * supports decimal integers suffixed by 'k', 'K', 'M' or 'G'. |
70d0b120 SM |
905 | * |
906 | * The suffix multiply the integer by: | |
907 | * 'k': 1024 | |
908 | * 'M': 1024^2 | |
909 | * 'G': 1024^3 | |
910 | * | |
911 | * @param str The string to parse. | |
5983a922 | 912 | * @param size Pointer to a uint64_t that will be filled with the |
cfa9a5a2 | 913 | * resulting size. |
70d0b120 SM |
914 | * |
915 | * @return 0 on success, -1 on failure. | |
916 | */ | |
00a52467 | 917 | LTTNG_HIDDEN |
5983a922 | 918 | int utils_parse_size_suffix(const char * const str, uint64_t * const size) |
70d0b120 | 919 | { |
70d0b120 | 920 | int ret; |
5983a922 | 921 | uint64_t base_size; |
70d0b120 | 922 | long shift = 0; |
5983a922 SM |
923 | const char *str_end; |
924 | char *num_end; | |
70d0b120 SM |
925 | |
926 | if (!str) { | |
5983a922 | 927 | DBG("utils_parse_size_suffix: received a NULL string."); |
70d0b120 SM |
928 | ret = -1; |
929 | goto end; | |
930 | } | |
931 | ||
5983a922 SM |
932 | /* strtoull will accept a negative number, but we don't want to. */ |
933 | if (strchr(str, '-') != NULL) { | |
934 | DBG("utils_parse_size_suffix: invalid size string, should not contain '-'."); | |
70d0b120 | 935 | ret = -1; |
5983a922 | 936 | goto end; |
70d0b120 SM |
937 | } |
938 | ||
5983a922 SM |
939 | /* str_end will point to the \0 */ |
940 | str_end = str + strlen(str); | |
70d0b120 | 941 | errno = 0; |
5983a922 | 942 | base_size = strtoull(str, &num_end, 0); |
70d0b120 | 943 | if (errno != 0) { |
5983a922 | 944 | PERROR("utils_parse_size_suffix strtoull"); |
70d0b120 | 945 | ret = -1; |
5983a922 SM |
946 | goto end; |
947 | } | |
948 | ||
949 | if (num_end == str) { | |
950 | /* strtoull parsed nothing, not good. */ | |
951 | DBG("utils_parse_size_suffix: strtoull had nothing good to parse."); | |
952 | ret = -1; | |
953 | goto end; | |
954 | } | |
955 | ||
956 | /* Check if a prefix is present. */ | |
957 | switch (*num_end) { | |
958 | case 'G': | |
959 | shift = GIBI_LOG2; | |
960 | num_end++; | |
961 | break; | |
962 | case 'M': /* */ | |
963 | shift = MEBI_LOG2; | |
964 | num_end++; | |
965 | break; | |
966 | case 'K': | |
967 | case 'k': | |
968 | shift = KIBI_LOG2; | |
969 | num_end++; | |
970 | break; | |
971 | case '\0': | |
972 | break; | |
973 | default: | |
974 | DBG("utils_parse_size_suffix: invalid suffix."); | |
975 | ret = -1; | |
976 | goto end; | |
977 | } | |
978 | ||
979 | /* Check for garbage after the valid input. */ | |
980 | if (num_end != str_end) { | |
981 | DBG("utils_parse_size_suffix: Garbage after size string."); | |
982 | ret = -1; | |
983 | goto end; | |
70d0b120 SM |
984 | } |
985 | ||
986 | *size = base_size << shift; | |
987 | ||
988 | /* Check for overflow */ | |
989 | if ((*size >> shift) != base_size) { | |
5983a922 | 990 | DBG("utils_parse_size_suffix: oops, overflow detected."); |
70d0b120 | 991 | ret = -1; |
5983a922 | 992 | goto end; |
70d0b120 SM |
993 | } |
994 | ||
995 | ret = 0; | |
70d0b120 SM |
996 | end: |
997 | return ret; | |
998 | } | |
cfa9a5a2 DG |
999 | |
1000 | /* | |
1001 | * fls: returns the position of the most significant bit. | |
1002 | * Returns 0 if no bit is set, else returns the position of the most | |
1003 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). | |
1004 | */ | |
1005 | #if defined(__i386) || defined(__x86_64) | |
1006 | static inline unsigned int fls_u32(uint32_t x) | |
1007 | { | |
1008 | int r; | |
1009 | ||
1010 | asm("bsrl %1,%0\n\t" | |
1011 | "jnz 1f\n\t" | |
1012 | "movl $-1,%0\n\t" | |
1013 | "1:\n\t" | |
1014 | : "=r" (r) : "rm" (x)); | |
1015 | return r + 1; | |
1016 | } | |
1017 | #define HAS_FLS_U32 | |
1018 | #endif | |
1019 | ||
db5be0a3 JG |
1020 | #if defined(__x86_64) |
1021 | static inline | |
1022 | unsigned int fls_u64(uint64_t x) | |
1023 | { | |
1024 | long r; | |
1025 | ||
1026 | asm("bsrq %1,%0\n\t" | |
1027 | "jnz 1f\n\t" | |
1028 | "movq $-1,%0\n\t" | |
1029 | "1:\n\t" | |
1030 | : "=r" (r) : "rm" (x)); | |
1031 | return r + 1; | |
1032 | } | |
1033 | #define HAS_FLS_U64 | |
1034 | #endif | |
1035 | ||
1036 | #ifndef HAS_FLS_U64 | |
1037 | static __attribute__((unused)) | |
1038 | unsigned int fls_u64(uint64_t x) | |
1039 | { | |
1040 | unsigned int r = 64; | |
1041 | ||
1042 | if (!x) | |
1043 | return 0; | |
1044 | ||
1045 | if (!(x & 0xFFFFFFFF00000000ULL)) { | |
1046 | x <<= 32; | |
1047 | r -= 32; | |
1048 | } | |
1049 | if (!(x & 0xFFFF000000000000ULL)) { | |
1050 | x <<= 16; | |
1051 | r -= 16; | |
1052 | } | |
1053 | if (!(x & 0xFF00000000000000ULL)) { | |
1054 | x <<= 8; | |
1055 | r -= 8; | |
1056 | } | |
1057 | if (!(x & 0xF000000000000000ULL)) { | |
1058 | x <<= 4; | |
1059 | r -= 4; | |
1060 | } | |
1061 | if (!(x & 0xC000000000000000ULL)) { | |
1062 | x <<= 2; | |
1063 | r -= 2; | |
1064 | } | |
1065 | if (!(x & 0x8000000000000000ULL)) { | |
1066 | x <<= 1; | |
1067 | r -= 1; | |
1068 | } | |
1069 | return r; | |
1070 | } | |
1071 | #endif | |
1072 | ||
cfa9a5a2 DG |
1073 | #ifndef HAS_FLS_U32 |
1074 | static __attribute__((unused)) unsigned int fls_u32(uint32_t x) | |
1075 | { | |
1076 | unsigned int r = 32; | |
1077 | ||
1078 | if (!x) { | |
1079 | return 0; | |
1080 | } | |
1081 | if (!(x & 0xFFFF0000U)) { | |
1082 | x <<= 16; | |
1083 | r -= 16; | |
1084 | } | |
1085 | if (!(x & 0xFF000000U)) { | |
1086 | x <<= 8; | |
1087 | r -= 8; | |
1088 | } | |
1089 | if (!(x & 0xF0000000U)) { | |
1090 | x <<= 4; | |
1091 | r -= 4; | |
1092 | } | |
1093 | if (!(x & 0xC0000000U)) { | |
1094 | x <<= 2; | |
1095 | r -= 2; | |
1096 | } | |
1097 | if (!(x & 0x80000000U)) { | |
1098 | x <<= 1; | |
1099 | r -= 1; | |
1100 | } | |
1101 | return r; | |
1102 | } | |
1103 | #endif | |
1104 | ||
1105 | /* | |
1106 | * Return the minimum order for which x <= (1UL << order). | |
1107 | * Return -1 if x is 0. | |
1108 | */ | |
1109 | LTTNG_HIDDEN | |
1110 | int utils_get_count_order_u32(uint32_t x) | |
1111 | { | |
1112 | if (!x) { | |
1113 | return -1; | |
1114 | } | |
1115 | ||
1116 | return fls_u32(x - 1); | |
1117 | } | |
feb0f3e5 | 1118 | |
db5be0a3 JG |
1119 | /* |
1120 | * Return the minimum order for which x <= (1UL << order). | |
1121 | * Return -1 if x is 0. | |
1122 | */ | |
1123 | LTTNG_HIDDEN | |
1124 | int utils_get_count_order_u64(uint64_t x) | |
1125 | { | |
1126 | if (!x) { | |
1127 | return -1; | |
1128 | } | |
1129 | ||
1130 | return fls_u64(x - 1); | |
1131 | } | |
1132 | ||
feb0f3e5 AM |
1133 | /** |
1134 | * Obtain the value of LTTNG_HOME environment variable, if exists. | |
1135 | * Otherwise returns the value of HOME. | |
1136 | */ | |
00a52467 | 1137 | LTTNG_HIDDEN |
feb0f3e5 AM |
1138 | char *utils_get_home_dir(void) |
1139 | { | |
1140 | char *val = NULL; | |
04135dbd DG |
1141 | struct passwd *pwd; |
1142 | ||
e8fa9fb0 | 1143 | val = lttng_secure_getenv(DEFAULT_LTTNG_HOME_ENV_VAR); |
feb0f3e5 | 1144 | if (val != NULL) { |
04135dbd DG |
1145 | goto end; |
1146 | } | |
e8fa9fb0 | 1147 | val = lttng_secure_getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); |
04135dbd DG |
1148 | if (val != NULL) { |
1149 | goto end; | |
feb0f3e5 | 1150 | } |
04135dbd DG |
1151 | |
1152 | /* Fallback on the password file entry. */ | |
1153 | pwd = getpwuid(getuid()); | |
1154 | if (!pwd) { | |
1155 | goto end; | |
1156 | } | |
1157 | val = pwd->pw_dir; | |
1158 | ||
1159 | DBG3("Home directory is '%s'", val); | |
1160 | ||
1161 | end: | |
1162 | return val; | |
feb0f3e5 | 1163 | } |
26fe5938 | 1164 | |
fb198a11 JG |
1165 | /** |
1166 | * Get user's home directory. Dynamically allocated, must be freed | |
1167 | * by the caller. | |
1168 | */ | |
1169 | LTTNG_HIDDEN | |
1170 | char *utils_get_user_home_dir(uid_t uid) | |
1171 | { | |
1172 | struct passwd pwd; | |
1173 | struct passwd *result; | |
1174 | char *home_dir = NULL; | |
1175 | char *buf = NULL; | |
1176 | long buflen; | |
1177 | int ret; | |
1178 | ||
1179 | buflen = sysconf(_SC_GETPW_R_SIZE_MAX); | |
1180 | if (buflen == -1) { | |
1181 | goto end; | |
1182 | } | |
1183 | retry: | |
1184 | buf = zmalloc(buflen); | |
1185 | if (!buf) { | |
1186 | goto end; | |
1187 | } | |
1188 | ||
1189 | ret = getpwuid_r(uid, &pwd, buf, buflen, &result); | |
1190 | if (ret || !result) { | |
1191 | if (ret == ERANGE) { | |
1192 | free(buf); | |
1193 | buflen *= 2; | |
1194 | goto retry; | |
1195 | } | |
1196 | goto end; | |
1197 | } | |
1198 | ||
1199 | home_dir = strdup(pwd.pw_dir); | |
1200 | end: | |
1201 | free(buf); | |
1202 | return home_dir; | |
1203 | } | |
1204 | ||
fbb9748b JG |
1205 | /* |
1206 | * Obtain the value of LTTNG_KMOD_PROBES environment variable, if exists. | |
c9d42407 | 1207 | * Otherwise returns NULL. |
fbb9748b JG |
1208 | */ |
1209 | LTTNG_HIDDEN | |
1210 | char *utils_get_kmod_probes_list(void) | |
1211 | { | |
e8fa9fb0 | 1212 | return lttng_secure_getenv(DEFAULT_LTTNG_KMOD_PROBES); |
fbb9748b JG |
1213 | } |
1214 | ||
c9d42407 PP |
1215 | /* |
1216 | * Obtain the value of LTTNG_EXTRA_KMOD_PROBES environment variable, if | |
1217 | * exists. Otherwise returns NULL. | |
1218 | */ | |
1219 | LTTNG_HIDDEN | |
1220 | char *utils_get_extra_kmod_probes_list(void) | |
1221 | { | |
e8fa9fb0 | 1222 | return lttng_secure_getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES); |
c9d42407 PP |
1223 | } |
1224 | ||
26fe5938 DG |
1225 | /* |
1226 | * With the given format, fill dst with the time of len maximum siz. | |
1227 | * | |
1228 | * Return amount of bytes set in the buffer or else 0 on error. | |
1229 | */ | |
1230 | LTTNG_HIDDEN | |
1231 | size_t utils_get_current_time_str(const char *format, char *dst, size_t len) | |
1232 | { | |
1233 | size_t ret; | |
1234 | time_t rawtime; | |
1235 | struct tm *timeinfo; | |
1236 | ||
1237 | assert(format); | |
1238 | assert(dst); | |
1239 | ||
1240 | /* Get date and time for session path */ | |
1241 | time(&rawtime); | |
1242 | timeinfo = localtime(&rawtime); | |
1243 | ret = strftime(dst, len, format, timeinfo); | |
1244 | if (ret == 0) { | |
68e6efdd | 1245 | ERR("Unable to strftime with format %s at dst %p of len %zu", format, |
26fe5938 DG |
1246 | dst, len); |
1247 | } | |
1248 | ||
1249 | return ret; | |
1250 | } | |
6c71277b MD |
1251 | |
1252 | /* | |
1253 | * Return the group ID matching name, else 0 if it cannot be found. | |
1254 | */ | |
1255 | LTTNG_HIDDEN | |
1256 | gid_t utils_get_group_id(const char *name) | |
1257 | { | |
1258 | struct group *grp; | |
1259 | ||
1260 | grp = getgrnam(name); | |
1261 | if (!grp) { | |
1262 | static volatile int warn_once; | |
1263 | ||
1264 | if (!warn_once) { | |
1265 | WARN("No tracing group detected"); | |
1266 | warn_once = 1; | |
1267 | } | |
1268 | return 0; | |
1269 | } | |
1270 | return grp->gr_gid; | |
1271 | } | |
8db0dc00 JG |
1272 | |
1273 | /* | |
1274 | * Return a newly allocated option string. This string is to be used as the | |
1275 | * optstring argument of getopt_long(), see GETOPT(3). opt_count is the number | |
1276 | * of elements in the long_options array. Returns NULL if the string's | |
1277 | * allocation fails. | |
1278 | */ | |
1279 | LTTNG_HIDDEN | |
1280 | char *utils_generate_optstring(const struct option *long_options, | |
1281 | size_t opt_count) | |
1282 | { | |
1283 | int i; | |
1284 | size_t string_len = opt_count, str_pos = 0; | |
1285 | char *optstring; | |
1286 | ||
1287 | /* | |
1288 | * Compute the necessary string length. One letter per option, two when an | |
1289 | * argument is necessary, and a trailing NULL. | |
1290 | */ | |
1291 | for (i = 0; i < opt_count; i++) { | |
1292 | string_len += long_options[i].has_arg ? 1 : 0; | |
1293 | } | |
1294 | ||
1295 | optstring = zmalloc(string_len); | |
1296 | if (!optstring) { | |
1297 | goto end; | |
1298 | } | |
1299 | ||
1300 | for (i = 0; i < opt_count; i++) { | |
1301 | if (!long_options[i].name) { | |
1302 | /* Got to the trailing NULL element */ | |
1303 | break; | |
1304 | } | |
1305 | ||
a596dcb9 JG |
1306 | if (long_options[i].val != '\0') { |
1307 | optstring[str_pos++] = (char) long_options[i].val; | |
1308 | if (long_options[i].has_arg) { | |
1309 | optstring[str_pos++] = ':'; | |
1310 | } | |
8db0dc00 JG |
1311 | } |
1312 | } | |
1313 | ||
1314 | end: | |
1315 | return optstring; | |
1316 | } | |
3d071855 MD |
1317 | |
1318 | /* | |
1319 | * Try to remove a hierarchy of empty directories, recursively. Don't unlink | |
9529ec1b | 1320 | * any file. Try to rmdir any empty directory within the hierarchy. |
3d071855 MD |
1321 | */ |
1322 | LTTNG_HIDDEN | |
1323 | int utils_recursive_rmdir(const char *path) | |
1324 | { | |
1325 | DIR *dir; | |
7a946beb | 1326 | size_t path_len; |
9529ec1b | 1327 | int dir_fd, ret = 0, closeret, is_empty = 1; |
3d071855 MD |
1328 | struct dirent *entry; |
1329 | ||
1330 | /* Open directory */ | |
1331 | dir = opendir(path); | |
1332 | if (!dir) { | |
1333 | PERROR("Cannot open '%s' path", path); | |
1334 | return -1; | |
1335 | } | |
5a2451c9 | 1336 | dir_fd = lttng_dirfd(dir); |
3d071855 | 1337 | if (dir_fd < 0) { |
5a2451c9 | 1338 | PERROR("lttng_dirfd"); |
3d071855 MD |
1339 | return -1; |
1340 | } | |
1341 | ||
7a946beb | 1342 | path_len = strlen(path); |
3d071855 | 1343 | while ((entry = readdir(dir))) { |
7a946beb MJ |
1344 | struct stat st; |
1345 | size_t name_len; | |
1346 | char filename[PATH_MAX]; | |
1347 | ||
3763af87 JG |
1348 | if (!strcmp(entry->d_name, ".") |
1349 | || !strcmp(entry->d_name, "..")) { | |
1350 | continue; | |
1351 | } | |
1352 | ||
7a946beb MJ |
1353 | name_len = strlen(entry->d_name); |
1354 | if (path_len + name_len + 2 > sizeof(filename)) { | |
1355 | ERR("Failed to remove file: path name too long (%s/%s)", | |
1356 | path, entry->d_name); | |
1357 | continue; | |
1358 | } | |
1359 | if (snprintf(filename, sizeof(filename), "%s/%s", | |
1360 | path, entry->d_name) < 0) { | |
1361 | ERR("Failed to format path."); | |
1362 | continue; | |
1363 | } | |
1364 | ||
1365 | if (stat(filename, &st)) { | |
1366 | PERROR("stat"); | |
1367 | continue; | |
1368 | } | |
1369 | ||
1370 | if (S_ISDIR(st.st_mode)) { | |
3d071855 MD |
1371 | char subpath[PATH_MAX]; |
1372 | ||
1373 | strncpy(subpath, path, PATH_MAX); | |
1374 | subpath[PATH_MAX - 1] = '\0'; | |
1375 | strncat(subpath, "/", | |
1376 | PATH_MAX - strlen(subpath) - 1); | |
1377 | strncat(subpath, entry->d_name, | |
1378 | PATH_MAX - strlen(subpath) - 1); | |
9529ec1b MD |
1379 | if (utils_recursive_rmdir(subpath)) { |
1380 | is_empty = 0; | |
3d071855 | 1381 | } |
7a946beb | 1382 | } else if (S_ISREG(st.st_mode)) { |
9529ec1b | 1383 | is_empty = 0; |
7a946beb | 1384 | } else { |
3d071855 MD |
1385 | ret = -EINVAL; |
1386 | goto end; | |
1387 | } | |
1388 | } | |
1389 | end: | |
1390 | closeret = closedir(dir); | |
1391 | if (closeret) { | |
1392 | PERROR("closedir"); | |
1393 | } | |
9529ec1b | 1394 | if (is_empty) { |
3d071855 MD |
1395 | DBG3("Attempting rmdir %s", path); |
1396 | ret = rmdir(path); | |
1397 | } | |
1398 | return ret; | |
1399 | } | |
93ec662e JD |
1400 | |
1401 | LTTNG_HIDDEN | |
1402 | int utils_truncate_stream_file(int fd, off_t length) | |
1403 | { | |
1404 | int ret; | |
1405 | ||
1406 | ret = ftruncate(fd, length); | |
1407 | if (ret < 0) { | |
1408 | PERROR("ftruncate"); | |
1409 | goto end; | |
1410 | } | |
1411 | ret = lseek(fd, length, SEEK_SET); | |
1412 | if (ret < 0) { | |
1413 | PERROR("lseek"); | |
1414 | goto end; | |
1415 | } | |
93ec662e JD |
1416 | end: |
1417 | return ret; | |
1418 | } | |
4ba92f18 PP |
1419 | |
1420 | static const char *get_man_bin_path(void) | |
1421 | { | |
b7dce40d | 1422 | char *env_man_path = lttng_secure_getenv(DEFAULT_MAN_BIN_PATH_ENV); |
4ba92f18 PP |
1423 | |
1424 | if (env_man_path) { | |
1425 | return env_man_path; | |
1426 | } | |
1427 | ||
1428 | return DEFAULT_MAN_BIN_PATH; | |
1429 | } | |
1430 | ||
1431 | LTTNG_HIDDEN | |
4fc83d94 PP |
1432 | int utils_show_help(int section, const char *page_name, |
1433 | const char *help_msg) | |
4ba92f18 PP |
1434 | { |
1435 | char section_string[8]; | |
1436 | const char *man_bin_path = get_man_bin_path(); | |
4fc83d94 PP |
1437 | int ret = 0; |
1438 | ||
1439 | if (help_msg) { | |
1440 | printf("%s", help_msg); | |
1441 | goto end; | |
1442 | } | |
4ba92f18 PP |
1443 | |
1444 | /* Section integer -> section string */ | |
1445 | ret = sprintf(section_string, "%d", section); | |
1446 | assert(ret > 0 && ret < 8); | |
1447 | ||
1448 | /* | |
1449 | * Execute man pager. | |
1450 | * | |
b07e7ef0 | 1451 | * We provide -M to man here because LTTng-tools can |
4ba92f18 PP |
1452 | * be installed outside /usr, in which case its man pages are |
1453 | * not located in the default /usr/share/man directory. | |
1454 | */ | |
b07e7ef0 | 1455 | ret = execlp(man_bin_path, "man", "-M", MANPATH, |
4ba92f18 | 1456 | section_string, page_name, NULL); |
4fc83d94 PP |
1457 | |
1458 | end: | |
4ba92f18 PP |
1459 | return ret; |
1460 | } |