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 | ||
20 | #define _GNU_SOURCE | |
6c1c0768 | 21 | #define _LGPL_SOURCE |
35f90c40 | 22 | #include <assert.h> |
81b86775 DG |
23 | #include <ctype.h> |
24 | #include <fcntl.h> | |
25 | #include <limits.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
2d851108 | 28 | #include <sys/stat.h> |
0c7bcad5 | 29 | #include <sys/types.h> |
2d851108 | 30 | #include <unistd.h> |
fe4477ee | 31 | #include <inttypes.h> |
6c71277b | 32 | #include <grp.h> |
fb198a11 | 33 | #include <pwd.h> |
c9cb3e7d | 34 | #include <sys/file.h> |
81b86775 DG |
35 | |
36 | #include <common/common.h> | |
fe4477ee | 37 | #include <common/runas.h> |
e8fa9fb0 | 38 | #include <common/compat/getenv.h> |
81b86775 DG |
39 | |
40 | #include "utils.h" | |
feb0f3e5 | 41 | #include "defaults.h" |
81b86775 | 42 | |
5154230f RB |
43 | /* |
44 | * Return a partial realpath(3) of the path even if the full path does not | |
45 | * exist. For instance, with /tmp/test1/test2/test3, if test2/ does not exist | |
46 | * but the /tmp/test1 does, the real path for /tmp/test1 is concatened with | |
47 | * /test2/test3 then returned. In normal time, realpath(3) fails if the end | |
48 | * point directory does not exist. | |
49 | * In case resolved_path is NULL, the string returned was allocated in the | |
50 | * function and thus need to be freed by the caller. The size argument allows | |
51 | * to specify the size of the resolved_path argument if given, or the size to | |
52 | * allocate. | |
53 | */ | |
54 | LTTNG_HIDDEN | |
55 | char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) | |
56 | { | |
57 | char *cut_path, *try_path = NULL, *try_path_prev = NULL; | |
58 | const char *next, *prev, *end; | |
59 | ||
60 | /* Safety net */ | |
61 | if (path == NULL) { | |
62 | goto error; | |
63 | } | |
64 | ||
65 | /* | |
66 | * Identify the end of the path, we don't want to treat the | |
67 | * last char if it is a '/', we will just keep it on the side | |
68 | * to be added at the end, and return a value coherent with | |
69 | * the path given as argument | |
70 | */ | |
71 | end = path + strlen(path); | |
72 | if (*(end-1) == '/') { | |
73 | end--; | |
74 | } | |
75 | ||
76 | /* Initiate the values of the pointers before looping */ | |
77 | next = path; | |
78 | prev = next; | |
79 | /* Only to ensure try_path is not NULL to enter the while */ | |
80 | try_path = (char *)next; | |
81 | ||
82 | /* Resolve the canonical path of the first part of the path */ | |
83 | while (try_path != NULL && next != end) { | |
84 | /* | |
85 | * If there is not any '/' left, we want to try with | |
86 | * the full path | |
87 | */ | |
88 | next = strpbrk(next + 1, "/"); | |
89 | if (next == NULL) { | |
90 | next = end; | |
91 | } | |
92 | ||
93 | /* Cut the part we will be trying to resolve */ | |
94 | cut_path = strndup(path, next - path); | |
d9dbcf5e MD |
95 | if (cut_path == NULL) { |
96 | PERROR("strndup"); | |
97 | goto error; | |
98 | } | |
5154230f RB |
99 | |
100 | /* Try to resolve this part */ | |
101 | try_path = realpath((char *)cut_path, NULL); | |
102 | if (try_path == NULL) { | |
103 | /* | |
104 | * There was an error, we just want to be assured it | |
105 | * is linked to an unexistent directory, if it's another | |
106 | * reason, we spawn an error | |
107 | */ | |
108 | switch (errno) { | |
109 | case ENOENT: | |
110 | /* Ignore the error */ | |
111 | break; | |
112 | default: | |
113 | PERROR("realpath (partial_realpath)"); | |
114 | goto error; | |
115 | break; | |
116 | } | |
117 | } else { | |
118 | /* Save the place we are before trying the next step */ | |
119 | free(try_path_prev); | |
120 | try_path_prev = try_path; | |
121 | prev = next; | |
122 | } | |
123 | ||
124 | /* Free the allocated memory */ | |
125 | free(cut_path); | |
126 | }; | |
127 | ||
128 | /* Allocate memory for the resolved path if necessary */ | |
129 | if (resolved_path == NULL) { | |
130 | resolved_path = zmalloc(size); | |
131 | if (resolved_path == NULL) { | |
132 | PERROR("zmalloc resolved path"); | |
133 | goto error; | |
134 | } | |
135 | } | |
136 | ||
137 | /* | |
138 | * If we were able to solve at least partially the path, we can concatenate | |
139 | * what worked and what didn't work | |
140 | */ | |
141 | if (try_path_prev != NULL) { | |
142 | /* If we risk to concatenate two '/', we remove one of them */ | |
143 | if (try_path_prev[strlen(try_path_prev) - 1] == '/' && prev[0] == '/') { | |
144 | try_path_prev[strlen(try_path_prev) - 1] = '\0'; | |
145 | } | |
146 | ||
147 | /* | |
148 | * Duplicate the memory used by prev in case resolved_path and | |
149 | * path are pointers for the same memory space | |
150 | */ | |
151 | cut_path = strdup(prev); | |
d9dbcf5e MD |
152 | if (cut_path == NULL) { |
153 | PERROR("strdup"); | |
154 | goto error; | |
155 | } | |
5154230f RB |
156 | |
157 | /* Concatenate the strings */ | |
158 | snprintf(resolved_path, size, "%s%s", try_path_prev, cut_path); | |
159 | ||
160 | /* Free the allocated memory */ | |
161 | free(cut_path); | |
162 | free(try_path_prev); | |
163 | /* | |
164 | * Else, we just copy the path in our resolved_path to | |
165 | * return it as is | |
166 | */ | |
167 | } else { | |
168 | strncpy(resolved_path, path, size); | |
169 | } | |
170 | ||
171 | /* Then we return the 'partially' resolved path */ | |
172 | return resolved_path; | |
173 | ||
174 | error: | |
175 | free(resolved_path); | |
176 | return NULL; | |
177 | } | |
178 | ||
81b86775 | 179 | /* |
3d229795 RB |
180 | * Make a full resolution of the given path even if it doesn't exist. |
181 | * This function uses the utils_partial_realpath function to resolve | |
182 | * symlinks and relatives paths at the start of the string, and | |
183 | * implements functionnalities to resolve the './' and '../' strings | |
184 | * in the middle of a path. This function is only necessary because | |
185 | * realpath(3) does not accept to resolve unexistent paths. | |
186 | * The returned string was allocated in the function, it is thus of | |
187 | * the responsibility of the caller to free this memory. | |
81b86775 | 188 | */ |
90e535ef | 189 | LTTNG_HIDDEN |
81b86775 DG |
190 | char *utils_expand_path(const char *path) |
191 | { | |
3d229795 | 192 | char *next, *previous, *slash, *start_path, *absolute_path = NULL; |
5de083f4 RB |
193 | char *last_token; |
194 | int is_dot, is_dotdot; | |
81b86775 DG |
195 | |
196 | /* Safety net */ | |
197 | if (path == NULL) { | |
198 | goto error; | |
199 | } | |
200 | ||
3d229795 RB |
201 | /* Allocate memory for the absolute_path */ |
202 | absolute_path = zmalloc(PATH_MAX); | |
203 | if (absolute_path == NULL) { | |
81b86775 DG |
204 | PERROR("zmalloc expand path"); |
205 | goto error; | |
206 | } | |
207 | ||
3d229795 RB |
208 | /* |
209 | * If the path is not already absolute nor explicitly relative, | |
210 | * consider we're in the current directory | |
211 | */ | |
212 | if (*path != '/' && strncmp(path, "./", 2) != 0 && | |
213 | strncmp(path, "../", 3) != 0) { | |
214 | snprintf(absolute_path, PATH_MAX, "./%s", path); | |
2dcd84b7 | 215 | /* Else, we just copy the path */ |
116f95d9 | 216 | } else { |
3d229795 RB |
217 | strncpy(absolute_path, path, PATH_MAX); |
218 | } | |
116f95d9 | 219 | |
3d229795 RB |
220 | /* Resolve partially our path */ |
221 | absolute_path = utils_partial_realpath(absolute_path, | |
222 | absolute_path, PATH_MAX); | |
116f95d9 | 223 | |
3d229795 RB |
224 | /* As long as we find '/./' in the working_path string */ |
225 | while ((next = strstr(absolute_path, "/./"))) { | |
116f95d9 | 226 | |
3d229795 RB |
227 | /* We prepare the start_path not containing it */ |
228 | start_path = strndup(absolute_path, next - absolute_path); | |
d9dbcf5e MD |
229 | if (!start_path) { |
230 | PERROR("strndup"); | |
231 | goto error; | |
232 | } | |
3d229795 RB |
233 | /* And we concatenate it with the part after this string */ |
234 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 2); | |
116f95d9 | 235 | |
3d229795 RB |
236 | free(start_path); |
237 | } | |
116f95d9 | 238 | |
3d229795 RB |
239 | /* As long as we find '/../' in the working_path string */ |
240 | while ((next = strstr(absolute_path, "/../"))) { | |
241 | /* We find the last level of directory */ | |
242 | previous = absolute_path; | |
243 | while ((slash = strpbrk(previous, "/")) && slash != next) { | |
244 | previous = slash + 1; | |
81b86775 | 245 | } |
81b86775 | 246 | |
3d229795 RB |
247 | /* Then we prepare the start_path not containing it */ |
248 | start_path = strndup(absolute_path, previous - absolute_path); | |
d9dbcf5e MD |
249 | if (!start_path) { |
250 | PERROR("strndup"); | |
251 | goto error; | |
252 | } | |
3d229795 RB |
253 | |
254 | /* And we concatenate it with the part after the '/../' */ | |
255 | snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 4); | |
256 | ||
257 | /* We can free the memory used for the start path*/ | |
258 | free(start_path); | |
259 | ||
260 | /* Then we verify for symlinks using partial_realpath */ | |
261 | absolute_path = utils_partial_realpath(absolute_path, | |
262 | absolute_path, PATH_MAX); | |
116f95d9 | 263 | } |
81b86775 | 264 | |
5de083f4 RB |
265 | /* Identify the last token */ |
266 | last_token = strrchr(absolute_path, '/'); | |
267 | ||
268 | /* Verify that this token is not a relative path */ | |
269 | is_dotdot = (strcmp(last_token, "/..") == 0); | |
270 | is_dot = (strcmp(last_token, "/.") == 0); | |
271 | ||
272 | /* If it is, take action */ | |
273 | if (is_dot || is_dotdot) { | |
274 | /* For both, remove this token */ | |
275 | *last_token = '\0'; | |
276 | ||
277 | /* If it was a reference to parent directory, go back one more time */ | |
278 | if (is_dotdot) { | |
279 | last_token = strrchr(absolute_path, '/'); | |
280 | ||
281 | /* If there was only one level left, we keep the first '/' */ | |
282 | if (last_token == absolute_path) { | |
283 | last_token++; | |
284 | } | |
285 | ||
286 | *last_token = '\0'; | |
287 | } | |
288 | } | |
289 | ||
3d229795 | 290 | return absolute_path; |
81b86775 DG |
291 | |
292 | error: | |
3d229795 | 293 | free(absolute_path); |
81b86775 DG |
294 | return NULL; |
295 | } | |
296 | ||
297 | /* | |
298 | * Create a pipe in dst. | |
299 | */ | |
90e535ef | 300 | LTTNG_HIDDEN |
81b86775 DG |
301 | int utils_create_pipe(int *dst) |
302 | { | |
303 | int ret; | |
304 | ||
305 | if (dst == NULL) { | |
306 | return -1; | |
307 | } | |
308 | ||
309 | ret = pipe(dst); | |
310 | if (ret < 0) { | |
311 | PERROR("create pipe"); | |
312 | } | |
313 | ||
314 | return ret; | |
315 | } | |
316 | ||
317 | /* | |
318 | * Create pipe and set CLOEXEC flag to both fd. | |
319 | * | |
320 | * Make sure the pipe opened by this function are closed at some point. Use | |
321 | * utils_close_pipe(). | |
322 | */ | |
90e535ef | 323 | LTTNG_HIDDEN |
81b86775 DG |
324 | int utils_create_pipe_cloexec(int *dst) |
325 | { | |
326 | int ret, i; | |
327 | ||
328 | if (dst == NULL) { | |
329 | return -1; | |
330 | } | |
331 | ||
332 | ret = utils_create_pipe(dst); | |
333 | if (ret < 0) { | |
334 | goto error; | |
335 | } | |
336 | ||
337 | for (i = 0; i < 2; i++) { | |
338 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); | |
339 | if (ret < 0) { | |
340 | PERROR("fcntl pipe cloexec"); | |
341 | goto error; | |
342 | } | |
343 | } | |
344 | ||
345 | error: | |
346 | return ret; | |
347 | } | |
348 | ||
094f381c MD |
349 | /* |
350 | * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK. | |
351 | * | |
352 | * Make sure the pipe opened by this function are closed at some point. Use | |
353 | * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to | |
354 | * support OSes other than Linux 2.6.23+. | |
355 | */ | |
356 | LTTNG_HIDDEN | |
357 | int utils_create_pipe_cloexec_nonblock(int *dst) | |
358 | { | |
359 | int ret, i; | |
360 | ||
361 | if (dst == NULL) { | |
362 | return -1; | |
363 | } | |
364 | ||
365 | ret = utils_create_pipe(dst); | |
366 | if (ret < 0) { | |
367 | goto error; | |
368 | } | |
369 | ||
370 | for (i = 0; i < 2; i++) { | |
371 | ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC); | |
372 | if (ret < 0) { | |
373 | PERROR("fcntl pipe cloexec"); | |
374 | goto error; | |
375 | } | |
376 | /* | |
377 | * Note: we override any flag that could have been | |
378 | * previously set on the fd. | |
379 | */ | |
380 | ret = fcntl(dst[i], F_SETFL, O_NONBLOCK); | |
381 | if (ret < 0) { | |
382 | PERROR("fcntl pipe nonblock"); | |
383 | goto error; | |
384 | } | |
385 | } | |
386 | ||
387 | error: | |
388 | return ret; | |
389 | } | |
390 | ||
81b86775 DG |
391 | /* |
392 | * Close both read and write side of the pipe. | |
393 | */ | |
90e535ef | 394 | LTTNG_HIDDEN |
81b86775 DG |
395 | void utils_close_pipe(int *src) |
396 | { | |
397 | int i, ret; | |
398 | ||
399 | if (src == NULL) { | |
400 | return; | |
401 | } | |
402 | ||
403 | for (i = 0; i < 2; i++) { | |
404 | /* Safety check */ | |
405 | if (src[i] < 0) { | |
406 | continue; | |
407 | } | |
408 | ||
409 | ret = close(src[i]); | |
410 | if (ret) { | |
411 | PERROR("close pipe"); | |
412 | } | |
413 | } | |
414 | } | |
a4b92340 DG |
415 | |
416 | /* | |
417 | * Create a new string using two strings range. | |
418 | */ | |
90e535ef | 419 | LTTNG_HIDDEN |
a4b92340 DG |
420 | char *utils_strdupdelim(const char *begin, const char *end) |
421 | { | |
422 | char *str; | |
423 | ||
424 | str = zmalloc(end - begin + 1); | |
425 | if (str == NULL) { | |
426 | PERROR("zmalloc strdupdelim"); | |
427 | goto error; | |
428 | } | |
429 | ||
430 | memcpy(str, begin, end - begin); | |
431 | str[end - begin] = '\0'; | |
432 | ||
433 | error: | |
434 | return str; | |
435 | } | |
b662582b DG |
436 | |
437 | /* | |
438 | * Set CLOEXEC flag to the give file descriptor. | |
439 | */ | |
90e535ef | 440 | LTTNG_HIDDEN |
b662582b DG |
441 | int utils_set_fd_cloexec(int fd) |
442 | { | |
443 | int ret; | |
444 | ||
445 | if (fd < 0) { | |
446 | ret = -EINVAL; | |
447 | goto end; | |
448 | } | |
449 | ||
450 | ret = fcntl(fd, F_SETFD, FD_CLOEXEC); | |
451 | if (ret < 0) { | |
452 | PERROR("fcntl cloexec"); | |
453 | ret = -errno; | |
454 | } | |
455 | ||
456 | end: | |
457 | return ret; | |
458 | } | |
35f90c40 DG |
459 | |
460 | /* | |
461 | * Create pid file to the given path and filename. | |
462 | */ | |
90e535ef | 463 | LTTNG_HIDDEN |
35f90c40 DG |
464 | int utils_create_pid_file(pid_t pid, const char *filepath) |
465 | { | |
466 | int ret; | |
467 | FILE *fp; | |
468 | ||
469 | assert(filepath); | |
470 | ||
471 | fp = fopen(filepath, "w"); | |
472 | if (fp == NULL) { | |
473 | PERROR("open pid file %s", filepath); | |
474 | ret = -1; | |
475 | goto error; | |
476 | } | |
477 | ||
478 | ret = fprintf(fp, "%d\n", pid); | |
479 | if (ret < 0) { | |
480 | PERROR("fprintf pid file"); | |
e205d79b | 481 | goto error; |
35f90c40 DG |
482 | } |
483 | ||
e205d79b MD |
484 | if (fclose(fp)) { |
485 | PERROR("fclose"); | |
486 | } | |
35f90c40 | 487 | DBG("Pid %d written in file %s", pid, filepath); |
e205d79b | 488 | ret = 0; |
35f90c40 DG |
489 | error: |
490 | return ret; | |
491 | } | |
2d851108 | 492 | |
c9cb3e7d JG |
493 | /* |
494 | * Create lock file to the given path and filename. | |
495 | * Returns the associated file descriptor, -1 on error. | |
496 | */ | |
497 | LTTNG_HIDDEN | |
498 | int utils_create_lock_file(const char *filepath) | |
499 | { | |
500 | int ret; | |
501 | int fd; | |
502 | ||
503 | assert(filepath); | |
504 | ||
505 | fd = open(filepath, O_CREAT, | |
506 | O_WRONLY | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
507 | if (fd < 0) { | |
508 | PERROR("open lock file %s", filepath); | |
509 | ret = -1; | |
510 | goto error; | |
511 | } | |
512 | ||
513 | /* | |
514 | * Attempt to lock the file. If this fails, there is | |
515 | * already a process using the same lock file running | |
516 | * and we should exit. | |
517 | */ | |
518 | ret = flock(fd, LOCK_EX | LOCK_NB); | |
519 | if (ret) { | |
208ff148 | 520 | ERR("Could not get lock file %s, another instance is running.", |
c9cb3e7d | 521 | filepath); |
ffb0b851 JG |
522 | if (close(fd)) { |
523 | PERROR("close lock file"); | |
524 | } | |
c9cb3e7d JG |
525 | fd = ret; |
526 | goto error; | |
527 | } | |
528 | ||
529 | error: | |
530 | return fd; | |
531 | } | |
532 | ||
2d851108 DG |
533 | /* |
534 | * Recursively create directory using the given path and mode. | |
535 | * | |
536 | * On success, return 0 else a negative error code. | |
537 | */ | |
90e535ef | 538 | LTTNG_HIDDEN |
2d851108 DG |
539 | int utils_mkdir_recursive(const char *path, mode_t mode) |
540 | { | |
541 | char *p, tmp[PATH_MAX]; | |
2d851108 DG |
542 | size_t len; |
543 | int ret; | |
544 | ||
545 | assert(path); | |
546 | ||
547 | ret = snprintf(tmp, sizeof(tmp), "%s", path); | |
548 | if (ret < 0) { | |
549 | PERROR("snprintf mkdir"); | |
550 | goto error; | |
551 | } | |
552 | ||
553 | len = ret; | |
554 | if (tmp[len - 1] == '/') { | |
555 | tmp[len - 1] = 0; | |
556 | } | |
557 | ||
558 | for (p = tmp + 1; *p; p++) { | |
559 | if (*p == '/') { | |
560 | *p = 0; | |
561 | if (tmp[strlen(tmp) - 1] == '.' && | |
562 | tmp[strlen(tmp) - 2] == '.' && | |
563 | tmp[strlen(tmp) - 3] == '/') { | |
564 | ERR("Using '/../' is not permitted in the trace path (%s)", | |
565 | tmp); | |
566 | ret = -1; | |
567 | goto error; | |
568 | } | |
0c7bcad5 | 569 | ret = mkdir(tmp, mode); |
2d851108 | 570 | if (ret < 0) { |
0c7bcad5 MD |
571 | if (errno != EEXIST) { |
572 | PERROR("mkdir recursive"); | |
573 | ret = -errno; | |
574 | goto error; | |
2d851108 DG |
575 | } |
576 | } | |
577 | *p = '/'; | |
578 | } | |
579 | } | |
580 | ||
581 | ret = mkdir(tmp, mode); | |
582 | if (ret < 0) { | |
583 | if (errno != EEXIST) { | |
584 | PERROR("mkdir recursive last piece"); | |
585 | ret = -errno; | |
586 | } else { | |
587 | ret = 0; | |
588 | } | |
589 | } | |
590 | ||
591 | error: | |
592 | return ret; | |
593 | } | |
fe4477ee JD |
594 | |
595 | /* | |
596 | * Create the stream tracefile on disk. | |
597 | * | |
598 | * Return 0 on success or else a negative value. | |
599 | */ | |
bc182241 | 600 | LTTNG_HIDDEN |
07b86b52 | 601 | int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size, |
309167d2 | 602 | uint64_t count, int uid, int gid, char *suffix) |
fe4477ee | 603 | { |
be96a7d1 | 604 | int ret, out_fd, flags, mode; |
309167d2 JD |
605 | char full_path[PATH_MAX], *path_name_suffix = NULL, *path; |
606 | char *extra = NULL; | |
fe4477ee JD |
607 | |
608 | assert(path_name); | |
609 | assert(file_name); | |
610 | ||
611 | ret = snprintf(full_path, sizeof(full_path), "%s/%s", | |
612 | path_name, file_name); | |
613 | if (ret < 0) { | |
614 | PERROR("snprintf create output file"); | |
615 | goto error; | |
616 | } | |
617 | ||
309167d2 JD |
618 | /* Setup extra string if suffix or/and a count is needed. */ |
619 | if (size > 0 && suffix) { | |
620 | ret = asprintf(&extra, "_%" PRIu64 "%s", count, suffix); | |
621 | } else if (size > 0) { | |
622 | ret = asprintf(&extra, "_%" PRIu64, count); | |
623 | } else if (suffix) { | |
624 | ret = asprintf(&extra, "%s", suffix); | |
625 | } | |
626 | if (ret < 0) { | |
627 | PERROR("Allocating extra string to name"); | |
628 | goto error; | |
629 | } | |
630 | ||
fe4477ee JD |
631 | /* |
632 | * If we split the trace in multiple files, we have to add the count at the | |
633 | * end of the tracefile name | |
634 | */ | |
309167d2 JD |
635 | if (extra) { |
636 | ret = asprintf(&path_name_suffix, "%s%s", full_path, extra); | |
fe4477ee | 637 | if (ret < 0) { |
309167d2 JD |
638 | PERROR("Allocating path name with extra string"); |
639 | goto error_free_suffix; | |
fe4477ee | 640 | } |
309167d2 | 641 | path = path_name_suffix; |
fe4477ee JD |
642 | } else { |
643 | path = full_path; | |
644 | } | |
645 | ||
be96a7d1 | 646 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
0f907de1 | 647 | /* Open with 660 mode */ |
be96a7d1 DG |
648 | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; |
649 | ||
650 | if (uid < 0 || gid < 0) { | |
651 | out_fd = open(path, flags, mode); | |
652 | } else { | |
653 | out_fd = run_as_open(path, flags, mode, uid, gid); | |
654 | } | |
fe4477ee JD |
655 | if (out_fd < 0) { |
656 | PERROR("open stream path %s", path); | |
657 | goto error_open; | |
658 | } | |
659 | ret = out_fd; | |
660 | ||
661 | error_open: | |
309167d2 JD |
662 | free(path_name_suffix); |
663 | error_free_suffix: | |
664 | free(extra); | |
fe4477ee JD |
665 | error: |
666 | return ret; | |
667 | } | |
668 | ||
669 | /* | |
670 | * Change the output tracefile according to the given size and count The | |
671 | * new_count pointer is set during this operation. | |
672 | * | |
673 | * From the consumer, the stream lock MUST be held before calling this function | |
674 | * because we are modifying the stream status. | |
675 | * | |
676 | * Return 0 on success or else a negative value. | |
677 | */ | |
bc182241 | 678 | LTTNG_HIDDEN |
fe4477ee | 679 | int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size, |
309167d2 JD |
680 | uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count, |
681 | int *stream_fd) | |
fe4477ee JD |
682 | { |
683 | int ret; | |
684 | ||
309167d2 JD |
685 | assert(new_count); |
686 | assert(stream_fd); | |
687 | ||
fe4477ee JD |
688 | ret = close(out_fd); |
689 | if (ret < 0) { | |
690 | PERROR("Closing tracefile"); | |
691 | goto error; | |
692 | } | |
693 | ||
694 | if (count > 0) { | |
695 | *new_count = (*new_count + 1) % count; | |
696 | } else { | |
697 | (*new_count)++; | |
698 | } | |
699 | ||
309167d2 JD |
700 | ret = utils_create_stream_file(path_name, file_name, size, *new_count, |
701 | uid, gid, 0); | |
702 | if (ret < 0) { | |
703 | goto error; | |
704 | } | |
705 | *stream_fd = ret; | |
706 | ||
707 | /* Success. */ | |
708 | ret = 0; | |
709 | ||
fe4477ee JD |
710 | error: |
711 | return ret; | |
712 | } | |
70d0b120 | 713 | |
70d0b120 SM |
714 | |
715 | /** | |
716 | * Parse a string that represents a size in human readable format. It | |
5983a922 | 717 | * supports decimal integers suffixed by 'k', 'K', 'M' or 'G'. |
70d0b120 SM |
718 | * |
719 | * The suffix multiply the integer by: | |
720 | * 'k': 1024 | |
721 | * 'M': 1024^2 | |
722 | * 'G': 1024^3 | |
723 | * | |
724 | * @param str The string to parse. | |
5983a922 | 725 | * @param size Pointer to a uint64_t that will be filled with the |
cfa9a5a2 | 726 | * resulting size. |
70d0b120 SM |
727 | * |
728 | * @return 0 on success, -1 on failure. | |
729 | */ | |
00a52467 | 730 | LTTNG_HIDDEN |
5983a922 | 731 | int utils_parse_size_suffix(const char * const str, uint64_t * const size) |
70d0b120 | 732 | { |
70d0b120 | 733 | int ret; |
5983a922 | 734 | uint64_t base_size; |
70d0b120 | 735 | long shift = 0; |
5983a922 SM |
736 | const char *str_end; |
737 | char *num_end; | |
70d0b120 SM |
738 | |
739 | if (!str) { | |
5983a922 | 740 | DBG("utils_parse_size_suffix: received a NULL string."); |
70d0b120 SM |
741 | ret = -1; |
742 | goto end; | |
743 | } | |
744 | ||
5983a922 SM |
745 | /* strtoull will accept a negative number, but we don't want to. */ |
746 | if (strchr(str, '-') != NULL) { | |
747 | DBG("utils_parse_size_suffix: invalid size string, should not contain '-'."); | |
70d0b120 | 748 | ret = -1; |
5983a922 | 749 | goto end; |
70d0b120 SM |
750 | } |
751 | ||
5983a922 SM |
752 | /* str_end will point to the \0 */ |
753 | str_end = str + strlen(str); | |
70d0b120 | 754 | errno = 0; |
5983a922 | 755 | base_size = strtoull(str, &num_end, 0); |
70d0b120 | 756 | if (errno != 0) { |
5983a922 | 757 | PERROR("utils_parse_size_suffix strtoull"); |
70d0b120 | 758 | ret = -1; |
5983a922 SM |
759 | goto end; |
760 | } | |
761 | ||
762 | if (num_end == str) { | |
763 | /* strtoull parsed nothing, not good. */ | |
764 | DBG("utils_parse_size_suffix: strtoull had nothing good to parse."); | |
765 | ret = -1; | |
766 | goto end; | |
767 | } | |
768 | ||
769 | /* Check if a prefix is present. */ | |
770 | switch (*num_end) { | |
771 | case 'G': | |
772 | shift = GIBI_LOG2; | |
773 | num_end++; | |
774 | break; | |
775 | case 'M': /* */ | |
776 | shift = MEBI_LOG2; | |
777 | num_end++; | |
778 | break; | |
779 | case 'K': | |
780 | case 'k': | |
781 | shift = KIBI_LOG2; | |
782 | num_end++; | |
783 | break; | |
784 | case '\0': | |
785 | break; | |
786 | default: | |
787 | DBG("utils_parse_size_suffix: invalid suffix."); | |
788 | ret = -1; | |
789 | goto end; | |
790 | } | |
791 | ||
792 | /* Check for garbage after the valid input. */ | |
793 | if (num_end != str_end) { | |
794 | DBG("utils_parse_size_suffix: Garbage after size string."); | |
795 | ret = -1; | |
796 | goto end; | |
70d0b120 SM |
797 | } |
798 | ||
799 | *size = base_size << shift; | |
800 | ||
801 | /* Check for overflow */ | |
802 | if ((*size >> shift) != base_size) { | |
5983a922 | 803 | DBG("utils_parse_size_suffix: oops, overflow detected."); |
70d0b120 | 804 | ret = -1; |
5983a922 | 805 | goto end; |
70d0b120 SM |
806 | } |
807 | ||
808 | ret = 0; | |
70d0b120 SM |
809 | end: |
810 | return ret; | |
811 | } | |
cfa9a5a2 DG |
812 | |
813 | /* | |
814 | * fls: returns the position of the most significant bit. | |
815 | * Returns 0 if no bit is set, else returns the position of the most | |
816 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). | |
817 | */ | |
818 | #if defined(__i386) || defined(__x86_64) | |
819 | static inline unsigned int fls_u32(uint32_t x) | |
820 | { | |
821 | int r; | |
822 | ||
823 | asm("bsrl %1,%0\n\t" | |
824 | "jnz 1f\n\t" | |
825 | "movl $-1,%0\n\t" | |
826 | "1:\n\t" | |
827 | : "=r" (r) : "rm" (x)); | |
828 | return r + 1; | |
829 | } | |
830 | #define HAS_FLS_U32 | |
831 | #endif | |
832 | ||
833 | #ifndef HAS_FLS_U32 | |
834 | static __attribute__((unused)) unsigned int fls_u32(uint32_t x) | |
835 | { | |
836 | unsigned int r = 32; | |
837 | ||
838 | if (!x) { | |
839 | return 0; | |
840 | } | |
841 | if (!(x & 0xFFFF0000U)) { | |
842 | x <<= 16; | |
843 | r -= 16; | |
844 | } | |
845 | if (!(x & 0xFF000000U)) { | |
846 | x <<= 8; | |
847 | r -= 8; | |
848 | } | |
849 | if (!(x & 0xF0000000U)) { | |
850 | x <<= 4; | |
851 | r -= 4; | |
852 | } | |
853 | if (!(x & 0xC0000000U)) { | |
854 | x <<= 2; | |
855 | r -= 2; | |
856 | } | |
857 | if (!(x & 0x80000000U)) { | |
858 | x <<= 1; | |
859 | r -= 1; | |
860 | } | |
861 | return r; | |
862 | } | |
863 | #endif | |
864 | ||
865 | /* | |
866 | * Return the minimum order for which x <= (1UL << order). | |
867 | * Return -1 if x is 0. | |
868 | */ | |
869 | LTTNG_HIDDEN | |
870 | int utils_get_count_order_u32(uint32_t x) | |
871 | { | |
872 | if (!x) { | |
873 | return -1; | |
874 | } | |
875 | ||
876 | return fls_u32(x - 1); | |
877 | } | |
feb0f3e5 AM |
878 | |
879 | /** | |
880 | * Obtain the value of LTTNG_HOME environment variable, if exists. | |
881 | * Otherwise returns the value of HOME. | |
882 | */ | |
00a52467 | 883 | LTTNG_HIDDEN |
feb0f3e5 AM |
884 | char *utils_get_home_dir(void) |
885 | { | |
886 | char *val = NULL; | |
04135dbd DG |
887 | struct passwd *pwd; |
888 | ||
e8fa9fb0 | 889 | val = lttng_secure_getenv(DEFAULT_LTTNG_HOME_ENV_VAR); |
feb0f3e5 | 890 | if (val != NULL) { |
04135dbd DG |
891 | goto end; |
892 | } | |
e8fa9fb0 | 893 | val = lttng_secure_getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); |
04135dbd DG |
894 | if (val != NULL) { |
895 | goto end; | |
feb0f3e5 | 896 | } |
04135dbd DG |
897 | |
898 | /* Fallback on the password file entry. */ | |
899 | pwd = getpwuid(getuid()); | |
900 | if (!pwd) { | |
901 | goto end; | |
902 | } | |
903 | val = pwd->pw_dir; | |
904 | ||
905 | DBG3("Home directory is '%s'", val); | |
906 | ||
907 | end: | |
908 | return val; | |
feb0f3e5 | 909 | } |
26fe5938 | 910 | |
fb198a11 JG |
911 | /** |
912 | * Get user's home directory. Dynamically allocated, must be freed | |
913 | * by the caller. | |
914 | */ | |
915 | LTTNG_HIDDEN | |
916 | char *utils_get_user_home_dir(uid_t uid) | |
917 | { | |
918 | struct passwd pwd; | |
919 | struct passwd *result; | |
920 | char *home_dir = NULL; | |
921 | char *buf = NULL; | |
922 | long buflen; | |
923 | int ret; | |
924 | ||
925 | buflen = sysconf(_SC_GETPW_R_SIZE_MAX); | |
926 | if (buflen == -1) { | |
927 | goto end; | |
928 | } | |
929 | retry: | |
930 | buf = zmalloc(buflen); | |
931 | if (!buf) { | |
932 | goto end; | |
933 | } | |
934 | ||
935 | ret = getpwuid_r(uid, &pwd, buf, buflen, &result); | |
936 | if (ret || !result) { | |
937 | if (ret == ERANGE) { | |
938 | free(buf); | |
939 | buflen *= 2; | |
940 | goto retry; | |
941 | } | |
942 | goto end; | |
943 | } | |
944 | ||
945 | home_dir = strdup(pwd.pw_dir); | |
946 | end: | |
947 | free(buf); | |
948 | return home_dir; | |
949 | } | |
950 | ||
fbb9748b JG |
951 | /* |
952 | * Obtain the value of LTTNG_KMOD_PROBES environment variable, if exists. | |
c9d42407 | 953 | * Otherwise returns NULL. |
fbb9748b JG |
954 | */ |
955 | LTTNG_HIDDEN | |
956 | char *utils_get_kmod_probes_list(void) | |
957 | { | |
e8fa9fb0 | 958 | return lttng_secure_getenv(DEFAULT_LTTNG_KMOD_PROBES); |
fbb9748b JG |
959 | } |
960 | ||
c9d42407 PP |
961 | /* |
962 | * Obtain the value of LTTNG_EXTRA_KMOD_PROBES environment variable, if | |
963 | * exists. Otherwise returns NULL. | |
964 | */ | |
965 | LTTNG_HIDDEN | |
966 | char *utils_get_extra_kmod_probes_list(void) | |
967 | { | |
e8fa9fb0 | 968 | return lttng_secure_getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES); |
c9d42407 PP |
969 | } |
970 | ||
26fe5938 DG |
971 | /* |
972 | * With the given format, fill dst with the time of len maximum siz. | |
973 | * | |
974 | * Return amount of bytes set in the buffer or else 0 on error. | |
975 | */ | |
976 | LTTNG_HIDDEN | |
977 | size_t utils_get_current_time_str(const char *format, char *dst, size_t len) | |
978 | { | |
979 | size_t ret; | |
980 | time_t rawtime; | |
981 | struct tm *timeinfo; | |
982 | ||
983 | assert(format); | |
984 | assert(dst); | |
985 | ||
986 | /* Get date and time for session path */ | |
987 | time(&rawtime); | |
988 | timeinfo = localtime(&rawtime); | |
989 | ret = strftime(dst, len, format, timeinfo); | |
990 | if (ret == 0) { | |
68e6efdd | 991 | ERR("Unable to strftime with format %s at dst %p of len %zu", format, |
26fe5938 DG |
992 | dst, len); |
993 | } | |
994 | ||
995 | return ret; | |
996 | } | |
6c71277b MD |
997 | |
998 | /* | |
999 | * Return the group ID matching name, else 0 if it cannot be found. | |
1000 | */ | |
1001 | LTTNG_HIDDEN | |
1002 | gid_t utils_get_group_id(const char *name) | |
1003 | { | |
1004 | struct group *grp; | |
1005 | ||
1006 | grp = getgrnam(name); | |
1007 | if (!grp) { | |
1008 | static volatile int warn_once; | |
1009 | ||
1010 | if (!warn_once) { | |
1011 | WARN("No tracing group detected"); | |
1012 | warn_once = 1; | |
1013 | } | |
1014 | return 0; | |
1015 | } | |
1016 | return grp->gr_gid; | |
1017 | } | |
8db0dc00 JG |
1018 | |
1019 | /* | |
1020 | * Return a newly allocated option string. This string is to be used as the | |
1021 | * optstring argument of getopt_long(), see GETOPT(3). opt_count is the number | |
1022 | * of elements in the long_options array. Returns NULL if the string's | |
1023 | * allocation fails. | |
1024 | */ | |
1025 | LTTNG_HIDDEN | |
1026 | char *utils_generate_optstring(const struct option *long_options, | |
1027 | size_t opt_count) | |
1028 | { | |
1029 | int i; | |
1030 | size_t string_len = opt_count, str_pos = 0; | |
1031 | char *optstring; | |
1032 | ||
1033 | /* | |
1034 | * Compute the necessary string length. One letter per option, two when an | |
1035 | * argument is necessary, and a trailing NULL. | |
1036 | */ | |
1037 | for (i = 0; i < opt_count; i++) { | |
1038 | string_len += long_options[i].has_arg ? 1 : 0; | |
1039 | } | |
1040 | ||
1041 | optstring = zmalloc(string_len); | |
1042 | if (!optstring) { | |
1043 | goto end; | |
1044 | } | |
1045 | ||
1046 | for (i = 0; i < opt_count; i++) { | |
1047 | if (!long_options[i].name) { | |
1048 | /* Got to the trailing NULL element */ | |
1049 | break; | |
1050 | } | |
1051 | ||
1052 | optstring[str_pos++] = (char)long_options[i].val; | |
1053 | if (long_options[i].has_arg) { | |
1054 | optstring[str_pos++] = ':'; | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | end: | |
1059 | return optstring; | |
1060 | } |