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