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