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