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