Implement list_cmd_options_argpar
[lttng-tools.git] / src / lib / lttng-ctl / save.c
CommitLineData
00c76cea 1/*
ab5be9fa 2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
00c76cea 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
00c76cea 5 *
00c76cea
JG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
00c76cea
JG
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
19struct lttng_save_session_attr *lttng_save_session_attr_create(void)
20{
21 return zmalloc(sizeof(struct lttng_save_session_attr));
22}
23
24void lttng_save_session_attr_destroy(struct lttng_save_session_attr *output)
25{
26 if (output) {
27 free(output);
28 }
29}
30
31const 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
43const 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
55int lttng_save_session_attr_get_overwrite(
56 struct lttng_save_session_attr *attr)
57{
58 return attr ? attr->overwrite : -LTTNG_ERR_INVALID;
59}
60
30f9c327
JG
61int 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
67int 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
00c76cea
JG
73int 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);
36d2e35d 87 if (len >= LTTNG_NAME_MAX) {
00c76cea
JG
88 ret = -LTTNG_ERR_INVALID;
89 goto error;
90 }
91
3716477e
JG
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 }
00c76cea
JG
98 } else {
99 attr->session_name[0] = '\0';
100 }
101error:
102 return ret;
103}
104
105int lttng_save_session_attr_set_output_url(
106 struct lttng_save_session_attr *attr, const char *url)
107{
108 int ret = 0;
106d4b46
JRJ
109 size_t len;
110 ssize_t size;
b1a95f61 111 struct lttng_uri *uris = NULL;
00c76cea
JG
112
113 if (!attr) {
114 ret = -LTTNG_ERR_INVALID;
115 goto error;
116 }
117
b1a95f61
DG
118 if (!url) {
119 attr->configuration_url[0] = '\0';
120 ret = 0;
121 goto end;
122 }
00c76cea 123
b1a95f61
DG
124 len = strlen(url);
125 if (len >= PATH_MAX) {
126 ret = -LTTNG_ERR_INVALID;
127 goto error;
128 }
00c76cea 129
b1a95f61
DG
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;
00c76cea 134 }
b1a95f61
DG
135
136 /* Copy string plus the NULL terminated byte. */
e1b624d0
JG
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 }
b1a95f61
DG
143
144end:
00c76cea 145error:
b1a95f61 146 free(uris);
00c76cea
JG
147 return ret;
148}
149
150int 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;
161end:
162 return ret;
163}
164
30f9c327
JG
165int 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;
176end:
177 return ret;
178}
179
180int 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;
191end:
192 return ret;
193}
194
00c76cea
JG
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 */
200int 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);
216end:
217 return ret;
218}
This page took 0.054412 seconds and 4 git commands to generate.