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