Relayd add_stream command handle tracefile rotation
[lttng-tools.git] / src / bin / lttng-relayd / utils.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <common/common.h>
26 #include <common/defaults.h>
27 #include <common/utils.h>
28
29 #include "lttng-relayd.h"
30 #include "utils.h"
31
32 /*
33 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
34 */
35 static char *get_default_path(void)
36 {
37 return getenv("HOME");
38 }
39
40 static char *create_output_path_auto(char *path_name)
41 {
42 int ret;
43 char *traces_path = NULL;
44 char *alloc_path = NULL;
45 char *default_path;
46
47 default_path = get_default_path();
48 if (default_path == NULL) {
49 ERR("Home path not found.\n \
50 Please specify an output path using -o, --output PATH");
51 goto exit;
52 }
53 alloc_path = strdup(default_path);
54 if (alloc_path == NULL) {
55 PERROR("Path allocation");
56 goto exit;
57 }
58 ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME
59 "/%s", alloc_path, path_name);
60 if (ret < 0) {
61 PERROR("asprintf trace dir name");
62 goto exit;
63 }
64 exit:
65 free(alloc_path);
66 return traces_path;
67 }
68
69 static char *create_output_path_noauto(char *path_name)
70 {
71 int ret;
72 char *traces_path = NULL;
73 char *full_path;
74
75 full_path = utils_expand_path(opt_output_path);
76 if (!full_path) {
77 goto exit;
78 }
79
80 ret = asprintf(&traces_path, "%s/%s", full_path, path_name);
81 if (ret < 0) {
82 PERROR("asprintf trace dir name");
83 goto exit;
84 }
85 exit:
86 free(full_path);
87 return traces_path;
88 }
89
90 /*
91 * Create the output trace directory path name string.
92 *
93 * Return the allocated string containing the path name or else NULL.
94 */
95 char *create_output_path(char *path_name)
96 {
97 assert(path_name);
98
99 if (opt_output_path == NULL) {
100 return create_output_path_auto(path_name);
101 } else {
102 return create_output_path_noauto(path_name);
103 }
104 }
This page took 0.033749 seconds and 6 git commands to generate.