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