Commit | Line | Data |
---|---|---|
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 | ||
18 | #define _GNU_SOURCE | |
6c1c0768 | 19 | #define _LGPL_SOURCE |
00c76cea JG |
20 | #include <assert.h> |
21 | #include <string.h> | |
22 | ||
23 | #include <lttng/lttng-error.h> | |
24 | #include <lttng/save.h> | |
25 | #include <lttng/save-internal.h> | |
26 | #include <common/sessiond-comm/sessiond-comm.h> | |
27 | ||
28 | #include "lttng-ctl-helper.h" | |
29 | ||
30 | struct lttng_save_session_attr *lttng_save_session_attr_create(void) | |
31 | { | |
32 | return zmalloc(sizeof(struct lttng_save_session_attr)); | |
33 | } | |
34 | ||
35 | void lttng_save_session_attr_destroy(struct lttng_save_session_attr *output) | |
36 | { | |
37 | if (output) { | |
38 | free(output); | |
39 | } | |
40 | } | |
41 | ||
42 | const char *lttng_save_session_attr_get_session_name( | |
43 | struct lttng_save_session_attr *attr) | |
44 | { | |
45 | const char *ret = NULL; | |
46 | ||
47 | if (attr && attr->session_name[0]) { | |
48 | ret = attr->session_name; | |
49 | } | |
50 | ||
51 | return ret; | |
52 | } | |
53 | ||
54 | const char *lttng_save_session_attr_get_output_url( | |
55 | struct lttng_save_session_attr *attr) | |
56 | { | |
57 | const char *ret = NULL; | |
58 | ||
59 | if (attr && attr->configuration_url[0]) { | |
60 | ret = attr->configuration_url; | |
61 | } | |
62 | ||
63 | return ret; | |
64 | } | |
65 | ||
66 | int lttng_save_session_attr_get_overwrite( | |
67 | struct lttng_save_session_attr *attr) | |
68 | { | |
69 | return attr ? attr->overwrite : -LTTNG_ERR_INVALID; | |
70 | } | |
71 | ||
72 | int lttng_save_session_attr_set_session_name( | |
73 | struct lttng_save_session_attr *attr, const char *session_name) | |
74 | { | |
75 | int ret = 0; | |
76 | ||
77 | if (!attr) { | |
78 | ret = -LTTNG_ERR_INVALID; | |
79 | goto error; | |
80 | } | |
81 | ||
82 | if (session_name) { | |
83 | size_t len; | |
84 | ||
85 | len = strlen(session_name); | |
86 | if (len >= NAME_MAX) { | |
87 | ret = -LTTNG_ERR_INVALID; | |
88 | goto error; | |
89 | } | |
90 | ||
91 | strncpy(attr->session_name, session_name, len); | |
92 | } else { | |
93 | attr->session_name[0] = '\0'; | |
94 | } | |
95 | error: | |
96 | return ret; | |
97 | } | |
98 | ||
99 | int lttng_save_session_attr_set_output_url( | |
100 | struct lttng_save_session_attr *attr, const char *url) | |
101 | { | |
102 | int ret = 0; | |
106d4b46 JRJ |
103 | size_t len; |
104 | ssize_t size; | |
b1a95f61 | 105 | struct lttng_uri *uris = NULL; |
00c76cea JG |
106 | |
107 | if (!attr) { | |
108 | ret = -LTTNG_ERR_INVALID; | |
109 | goto error; | |
110 | } | |
111 | ||
b1a95f61 DG |
112 | if (!url) { |
113 | attr->configuration_url[0] = '\0'; | |
114 | ret = 0; | |
115 | goto end; | |
116 | } | |
00c76cea | 117 | |
b1a95f61 DG |
118 | len = strlen(url); |
119 | if (len >= PATH_MAX) { | |
120 | ret = -LTTNG_ERR_INVALID; | |
121 | goto error; | |
122 | } | |
00c76cea | 123 | |
b1a95f61 DG |
124 | size = uri_parse_str_urls(url, NULL, &uris); |
125 | if (size <= 0 || uris[0].dtype != LTTNG_DST_PATH) { | |
126 | ret = -LTTNG_ERR_INVALID; | |
127 | goto error; | |
00c76cea | 128 | } |
b1a95f61 DG |
129 | |
130 | /* Copy string plus the NULL terminated byte. */ | |
131 | lttng_ctl_copy_string(attr->configuration_url, uris[0].dst.path, | |
132 | sizeof(attr->configuration_url)); | |
133 | ||
134 | end: | |
00c76cea | 135 | error: |
b1a95f61 | 136 | free(uris); |
00c76cea JG |
137 | return ret; |
138 | } | |
139 | ||
140 | int lttng_save_session_attr_set_overwrite( | |
141 | struct lttng_save_session_attr *attr, int overwrite) | |
142 | { | |
143 | int ret = 0; | |
144 | ||
145 | if (!attr) { | |
146 | ret = -LTTNG_ERR_INVALID; | |
147 | goto end; | |
148 | } | |
149 | ||
150 | attr->overwrite = !!overwrite; | |
151 | end: | |
152 | return ret; | |
153 | } | |
154 | ||
155 | /* | |
156 | * The lttng-ctl API does not expose all the information needed to save the | |
157 | * session configurations. Thus, we must send a save command to the session | |
158 | * daemon which will, in turn, save its current session configuration. | |
159 | */ | |
160 | int lttng_save_session(struct lttng_save_session_attr *attr) | |
161 | { | |
162 | struct lttcomm_session_msg lsm; | |
163 | int ret; | |
164 | ||
165 | if (!attr) { | |
166 | ret = -LTTNG_ERR_INVALID; | |
167 | goto end; | |
168 | } | |
169 | ||
170 | memset(&lsm, 0, sizeof(lsm)); | |
171 | lsm.cmd_type = LTTNG_SAVE_SESSION; | |
172 | ||
173 | memcpy(&lsm.u.save_session.attr, attr, | |
174 | sizeof(struct lttng_save_session_attr)); | |
175 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
176 | end: | |
177 | return ret; | |
178 | } |