Fix: use ssize_type for the return value of uri_parse_str_urls
[lttng-tools.git] / src / lib / lttng-ctl / load.c
CommitLineData
9245bd0e
DG
1/*
2 * Copyright (C) 2014 - David Goulet <dgoulet@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
18#define _GNU_SOURCE
6c1c0768 19#define _LGPL_SOURCE
9245bd0e
DG
20#include <assert.h>
21#include <string.h>
22
23#include <lttng/lttng-error.h>
24#include <lttng/load.h>
25#include <lttng/load-internal.h>
26#include <common/sessiond-comm/sessiond-comm.h>
27#include <common/config/config.h>
28
29#include "lttng-ctl-helper.h"
30
31struct lttng_load_session_attr *lttng_load_session_attr_create(void)
32{
33 return zmalloc(sizeof(struct lttng_load_session_attr));
34}
35
36void lttng_load_session_attr_destroy(struct lttng_load_session_attr *attr)
37{
38 if (attr) {
39 free(attr);
40 }
41}
42
43const char *lttng_load_session_attr_get_session_name(
44 struct lttng_load_session_attr *attr)
45{
46 const char *ret = NULL;
47
48 if (attr && attr->session_name[0]) {
49 ret = attr->session_name;
50 }
51
52 return ret;
53}
54
55const char *lttng_load_session_attr_get_input_url(
56 struct lttng_load_session_attr *attr)
57{
58 const char *ret = NULL;
59
60 if (attr && attr->input_url[0]) {
61 ret = attr->input_url;
62 }
63
64 return ret;
65}
66
67int lttng_load_session_attr_get_overwrite(
68 struct lttng_load_session_attr *attr)
69{
70 return attr ? attr->overwrite : -LTTNG_ERR_INVALID;
71}
72
73int lttng_load_session_attr_set_session_name(
74 struct lttng_load_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 >= NAME_MAX) {
88 ret = -LTTNG_ERR_INVALID;
89 goto error;
90 }
91
92 strncpy(attr->session_name, session_name, len);
93 } else {
94 attr->session_name[0] = '\0';
95 }
96error:
97 return ret;
98}
99
100int lttng_load_session_attr_set_input_url(
101 struct lttng_load_session_attr *attr, const char *url)
102{
103 int ret = 0;
01262f7e
JR
104 size_t len;
105 ssize_t size;
9245bd0e
DG
106 struct lttng_uri *uris = NULL;
107
108 if (!attr) {
109 ret = -LTTNG_ERR_INVALID;
110 goto error;
111 }
112
113 if (!url) {
114 attr->input_url[0] = '\0';
115 ret = 0;
116 goto end;
117 }
118
119 len = strlen(url);
120 if (len >= PATH_MAX) {
121 ret = -LTTNG_ERR_INVALID;
122 goto error;
123 }
124
125 size = uri_parse_str_urls(url, NULL, &uris);
126 if (size <= 0 || uris[0].dtype != LTTNG_DST_PATH) {
127 ret = -LTTNG_ERR_INVALID;
128 goto error;
129 }
130
131 /* Copy string plus the NULL terminated byte. */
132 lttng_ctl_copy_string(attr->input_url, uris[0].dst.path,
133 sizeof(attr->input_url));
134
135end:
136error:
137 free(uris);
138 return ret;
139}
140
141int lttng_load_session_attr_set_overwrite(
142 struct lttng_load_session_attr *attr, int overwrite)
143{
144 int ret = 0;
145
146 if (!attr) {
147 ret = -LTTNG_ERR_INVALID;
148 goto end;
149 }
150
151 attr->overwrite = !!overwrite;
152end:
153 return ret;
154}
155
156/*
157 * The lttng-ctl API does not expose all the information needed to load the
158 * session configurations. Thus, we must send a load command to the session
159 * daemon which will, in turn, load its current session configuration.
160 */
161int lttng_load_session(struct lttng_load_session_attr *attr)
162{
163 int ret;
c5c3a403 164 const char *url, *session_name;
9245bd0e
DG
165
166 if (!attr) {
167 ret = -LTTNG_ERR_INVALID;
168 goto end;
169 }
170
c5c3a403
JG
171 url = attr->input_url[0] != '\0' ? attr->input_url : NULL;
172 session_name = attr->session_name[0] != '\0' ?
173 attr->session_name : NULL;
174
175 ret = config_load_session(url, session_name, attr->overwrite, 0);
9245bd0e
DG
176
177end:
178 return ret;
179}
This page took 0.032074 seconds and 4 git commands to generate.