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