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