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