Commit | Line | Data |
---|---|---|
9245bd0e DG |
1 | /* |
2 | * Copyright (C) 2014 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This library is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
11 | * for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public License | |
14 | * along with this library; if not, write to the Free Software Foundation, | |
15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
18 | #define _GNU_SOURCE | |
6c1c0768 | 19 | #define _LGPL_SOURCE |
9245bd0e DG |
20 | #include <assert.h> |
21 | #include <string.h> | |
22 | ||
23 | #include <lttng/lttng-error.h> | |
24 | #include <lttng/load.h> | |
25 | #include <lttng/load-internal.h> | |
26 | #include <common/sessiond-comm/sessiond-comm.h> | |
27 | #include <common/config/config.h> | |
28 | ||
29 | #include "lttng-ctl-helper.h" | |
30 | ||
31 | struct lttng_load_session_attr *lttng_load_session_attr_create(void) | |
32 | { | |
33 | return zmalloc(sizeof(struct lttng_load_session_attr)); | |
34 | } | |
35 | ||
36 | void lttng_load_session_attr_destroy(struct lttng_load_session_attr *attr) | |
37 | { | |
38 | if (attr) { | |
39 | free(attr); | |
40 | } | |
41 | } | |
42 | ||
43 | const char *lttng_load_session_attr_get_session_name( | |
44 | struct lttng_load_session_attr *attr) | |
45 | { | |
46 | const char *ret = NULL; | |
47 | ||
48 | if (attr && attr->session_name[0]) { | |
49 | ret = attr->session_name; | |
50 | } | |
51 | ||
52 | return ret; | |
53 | } | |
54 | ||
55 | const char *lttng_load_session_attr_get_input_url( | |
56 | struct lttng_load_session_attr *attr) | |
57 | { | |
58 | const char *ret = NULL; | |
59 | ||
60 | if (attr && attr->input_url[0]) { | |
61 | ret = attr->input_url; | |
62 | } | |
63 | ||
64 | return ret; | |
65 | } | |
66 | ||
67 | int lttng_load_session_attr_get_overwrite( | |
68 | struct lttng_load_session_attr *attr) | |
69 | { | |
70 | return attr ? attr->overwrite : -LTTNG_ERR_INVALID; | |
71 | } | |
72 | ||
73 | int lttng_load_session_attr_set_session_name( | |
74 | struct lttng_load_session_attr *attr, const char *session_name) | |
75 | { | |
76 | int ret = 0; | |
77 | ||
78 | if (!attr) { | |
79 | ret = -LTTNG_ERR_INVALID; | |
80 | goto error; | |
81 | } | |
82 | ||
83 | if (session_name) { | |
84 | size_t len; | |
85 | ||
86 | len = strlen(session_name); | |
87 | if (len >= NAME_MAX) { | |
88 | ret = -LTTNG_ERR_INVALID; | |
89 | goto error; | |
90 | } | |
91 | ||
92 | strncpy(attr->session_name, session_name, len); | |
93 | } else { | |
94 | attr->session_name[0] = '\0'; | |
95 | } | |
96 | error: | |
97 | return ret; | |
98 | } | |
99 | ||
100 | int lttng_load_session_attr_set_input_url( | |
101 | struct lttng_load_session_attr *attr, const char *url) | |
102 | { | |
103 | int ret = 0; | |
104 | size_t len, size; | |
105 | struct lttng_uri *uris = NULL; | |
106 | ||
107 | if (!attr) { | |
108 | ret = -LTTNG_ERR_INVALID; | |
109 | goto error; | |
110 | } | |
111 | ||
112 | if (!url) { | |
113 | attr->input_url[0] = '\0'; | |
114 | ret = 0; | |
115 | goto end; | |
116 | } | |
117 | ||
118 | len = strlen(url); | |
119 | if (len >= PATH_MAX) { | |
120 | ret = -LTTNG_ERR_INVALID; | |
121 | goto error; | |
122 | } | |
123 | ||
124 | size = uri_parse_str_urls(url, NULL, &uris); | |
125 | if (size <= 0 || uris[0].dtype != LTTNG_DST_PATH) { | |
126 | ret = -LTTNG_ERR_INVALID; | |
127 | goto error; | |
128 | } | |
129 | ||
130 | /* Copy string plus the NULL terminated byte. */ | |
131 | lttng_ctl_copy_string(attr->input_url, uris[0].dst.path, | |
132 | sizeof(attr->input_url)); | |
133 | ||
134 | end: | |
135 | error: | |
136 | free(uris); | |
137 | return ret; | |
138 | } | |
139 | ||
140 | int lttng_load_session_attr_set_overwrite( | |
141 | struct lttng_load_session_attr *attr, int overwrite) | |
142 | { | |
143 | int ret = 0; | |
144 | ||
145 | if (!attr) { | |
146 | ret = -LTTNG_ERR_INVALID; | |
147 | goto end; | |
148 | } | |
149 | ||
150 | attr->overwrite = !!overwrite; | |
151 | end: | |
152 | return ret; | |
153 | } | |
154 | ||
155 | /* | |
156 | * The lttng-ctl API does not expose all the information needed to load the | |
157 | * session configurations. Thus, we must send a load command to the session | |
158 | * daemon which will, in turn, load its current session configuration. | |
159 | */ | |
160 | int lttng_load_session(struct lttng_load_session_attr *attr) | |
161 | { | |
162 | int ret; | |
163 | ||
164 | if (!attr) { | |
165 | ret = -LTTNG_ERR_INVALID; | |
166 | goto end; | |
167 | } | |
168 | ||
169 | ret = config_load_session(attr->input_url, attr->session_name, | |
ab38c13f | 170 | attr->overwrite, 0); |
9245bd0e DG |
171 | |
172 | end: | |
173 | return ret; | |
174 | } |