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