7843df300699afded1d46ed8da083f60d8fc4c49
[lttng-tools.git] / src / lib / lttng-ctl / save.cpp
1 /*
2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <string.h>
10
11 #include <lttng/lttng-error.h>
12 #include <lttng/save.h>
13 #include <lttng/save-internal.hpp>
14 #include <common/sessiond-comm/sessiond-comm.hpp>
15
16 #include "lttng-ctl-helper.hpp"
17
18 struct lttng_save_session_attr *lttng_save_session_attr_create(void)
19 {
20 return (lttng_save_session_attr *) zmalloc(sizeof(struct lttng_save_session_attr));
21 }
22
23 void lttng_save_session_attr_destroy(struct lttng_save_session_attr *output)
24 {
25 if (output) {
26 free(output);
27 }
28 }
29
30 const char *lttng_save_session_attr_get_session_name(
31 struct lttng_save_session_attr *attr)
32 {
33 const char *ret = NULL;
34
35 if (attr && attr->session_name[0]) {
36 ret = attr->session_name;
37 }
38
39 return ret;
40 }
41
42 const char *lttng_save_session_attr_get_output_url(
43 struct lttng_save_session_attr *attr)
44 {
45 const char *ret = NULL;
46
47 if (attr && attr->configuration_url[0]) {
48 ret = attr->configuration_url;
49 }
50
51 return ret;
52 }
53
54 int lttng_save_session_attr_get_overwrite(
55 struct lttng_save_session_attr *attr)
56 {
57 return attr ? attr->overwrite : -LTTNG_ERR_INVALID;
58 }
59
60 int lttng_save_session_attr_get_omit_name(
61 struct lttng_save_session_attr *attr)
62 {
63 return attr ? attr->omit_name : -LTTNG_ERR_INVALID;
64 }
65
66 int lttng_save_session_attr_get_omit_output(
67 struct lttng_save_session_attr *attr)
68 {
69 return attr ? attr->omit_output : -LTTNG_ERR_INVALID;
70 }
71
72 int lttng_save_session_attr_set_session_name(
73 struct lttng_save_session_attr *attr, const char *session_name)
74 {
75 int ret = 0;
76
77 if (!attr) {
78 ret = -LTTNG_ERR_INVALID;
79 goto error;
80 }
81
82 if (session_name) {
83 size_t len;
84
85 len = strlen(session_name);
86 if (len >= LTTNG_NAME_MAX) {
87 ret = -LTTNG_ERR_INVALID;
88 goto error;
89 }
90
91 ret = lttng_strncpy(attr->session_name, session_name,
92 sizeof(attr->session_name));
93 if (ret) {
94 ret = -LTTNG_ERR_INVALID;
95 goto error;
96 }
97 } else {
98 attr->session_name[0] = '\0';
99 }
100 error:
101 return ret;
102 }
103
104 int lttng_save_session_attr_set_output_url(
105 struct lttng_save_session_attr *attr, const char *url)
106 {
107 int ret = 0;
108 size_t len;
109 ssize_t size;
110 struct lttng_uri *uris = NULL;
111
112 if (!attr) {
113 ret = -LTTNG_ERR_INVALID;
114 goto error;
115 }
116
117 if (!url) {
118 attr->configuration_url[0] = '\0';
119 ret = 0;
120 goto end;
121 }
122
123 len = strlen(url);
124 if (len >= PATH_MAX) {
125 ret = -LTTNG_ERR_INVALID;
126 goto error;
127 }
128
129 size = uri_parse_str_urls(url, NULL, &uris);
130 if (size <= 0 || uris[0].dtype != LTTNG_DST_PATH) {
131 ret = -LTTNG_ERR_INVALID;
132 goto error;
133 }
134
135 /* Copy string plus the NULL terminated byte. */
136 ret = lttng_strncpy(attr->configuration_url, uris[0].dst.path,
137 sizeof(attr->configuration_url));
138 if (ret) {
139 ret = -LTTNG_ERR_INVALID;
140 goto error;
141 }
142
143 end:
144 error:
145 free(uris);
146 return ret;
147 }
148
149 int lttng_save_session_attr_set_overwrite(
150 struct lttng_save_session_attr *attr, int overwrite)
151 {
152 int ret = 0;
153
154 if (!attr) {
155 ret = -LTTNG_ERR_INVALID;
156 goto end;
157 }
158
159 attr->overwrite = !!overwrite;
160 end:
161 return ret;
162 }
163
164 int lttng_save_session_attr_set_omit_name(
165 struct lttng_save_session_attr *attr, int omit_name)
166 {
167 int ret = 0;
168
169 if (!attr) {
170 ret = -LTTNG_ERR_INVALID;
171 goto end;
172 }
173
174 attr->omit_name = !!omit_name;
175 end:
176 return ret;
177 }
178
179 int lttng_save_session_attr_set_omit_output(
180 struct lttng_save_session_attr *attr, int omit_output)
181 {
182 int ret = 0;
183
184 if (!attr) {
185 ret = -LTTNG_ERR_INVALID;
186 goto end;
187 }
188
189 attr->omit_output = !!omit_output;
190 end:
191 return ret;
192 }
193
194 /*
195 * The lttng-ctl API does not expose all the information needed to save the
196 * session configurations. Thus, we must send a save command to the session
197 * daemon which will, in turn, save its current session configuration.
198 */
199 int lttng_save_session(struct lttng_save_session_attr *attr)
200 {
201 struct lttcomm_session_msg lsm;
202 int ret;
203
204 if (!attr) {
205 ret = -LTTNG_ERR_INVALID;
206 goto end;
207 }
208
209 memset(&lsm, 0, sizeof(lsm));
210 lsm.cmd_type = LTTNG_SAVE_SESSION;
211
212 memcpy(&lsm.u.save_session.attr, attr,
213 sizeof(struct lttng_save_session_attr));
214 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
215 end:
216 return ret;
217 }
This page took 0.033887 seconds and 4 git commands to generate.