Fix: Don't set filter if enable event fails
[lttng-tools.git] / src / bin / lttng-sessiond / channel.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <string.h>
20 #include <unistd.h>
21
22 #include <common/common.h>
23 #include <common/defaults.h>
24 #include <common/sessiond-comm/sessiond-comm.h>
25
26 #include "channel.h"
27 #include "kernel.h"
28 #include "ust-ctl.h"
29 #include "utils.h"
30 #include "ust-app.h"
31
32 /*
33 * Return allocated channel attributes.
34 */
35 struct lttng_channel *channel_new_default_attr(int dom)
36 {
37 struct lttng_channel *chan;
38
39 chan = zmalloc(sizeof(struct lttng_channel));
40 if (chan == NULL) {
41 PERROR("zmalloc channel init");
42 goto error_alloc;
43 }
44
45 if (snprintf(chan->name, sizeof(chan->name), "%s",
46 DEFAULT_CHANNEL_NAME) < 0) {
47 PERROR("snprintf default channel name");
48 goto error;
49 }
50
51 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
52 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
53 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
54
55 switch (dom) {
56 case LTTNG_DOMAIN_KERNEL:
57 chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
58 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
59 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
60 break;
61 case LTTNG_DOMAIN_UST:
62 #if 0
63 case LTTNG_DOMAIN_UST_PID:
64 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
65 case LTTNG_DOMAIN_UST_EXEC_NAME:
66 #endif
67 chan->attr.subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
68 chan->attr.num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
69 chan->attr.output = DEFAULT_UST_CHANNEL_OUTPUT;
70 break;
71 default:
72 goto error; /* Not implemented */
73 }
74
75 return chan;
76
77 error:
78 free(chan);
79 error_alloc:
80 return NULL;
81 }
82
83 /*
84 * Disable kernel channel of the kernel session.
85 */
86 int channel_kernel_disable(struct ltt_kernel_session *ksession,
87 char *channel_name)
88 {
89 int ret;
90 struct ltt_kernel_channel *kchan;
91
92 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
93 if (kchan == NULL) {
94 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
95 goto error;
96 } else if (kchan->enabled == 1) {
97 ret = kernel_disable_channel(kchan);
98 if (ret < 0 && ret != -EEXIST) {
99 ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL;
100 goto error;
101 }
102 }
103
104 ret = LTTNG_OK;
105
106 error:
107 return ret;
108 }
109
110 /*
111 * Enable kernel channel of the kernel session.
112 */
113 int channel_kernel_enable(struct ltt_kernel_session *ksession,
114 struct ltt_kernel_channel *kchan)
115 {
116 int ret;
117
118 if (kchan->enabled == 0) {
119 ret = kernel_enable_channel(kchan);
120 if (ret < 0) {
121 ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL;
122 goto error;
123 }
124 } else {
125 ret = LTTNG_ERR_KERN_CHAN_EXIST;
126 goto error;
127 }
128
129 ret = LTTNG_OK;
130
131 error:
132 return ret;
133 }
134
135 /*
136 * Create kernel channel of the kernel session and notify kernel thread.
137 */
138 int channel_kernel_create(struct ltt_kernel_session *ksession,
139 struct lttng_channel *attr, int kernel_pipe)
140 {
141 int ret;
142 struct lttng_channel *defattr = NULL;
143
144 /* Creating channel attributes if needed */
145 if (attr == NULL) {
146 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL);
147 if (defattr == NULL) {
148 ret = LTTNG_ERR_FATAL;
149 goto error;
150 }
151 attr = defattr;
152 }
153
154 /* Channel not found, creating it */
155 ret = kernel_create_channel(ksession, attr, ksession->trace_path);
156 if (ret < 0) {
157 ret = LTTNG_ERR_KERN_CHAN_FAIL;
158 goto error;
159 }
160
161 /* Notify kernel thread that there is a new channel */
162 ret = notify_thread_pipe(kernel_pipe);
163 if (ret < 0) {
164 ret = LTTNG_ERR_FATAL;
165 goto error;
166 }
167
168 ret = LTTNG_OK;
169 error:
170 free(defattr);
171 return ret;
172 }
173
174 /*
175 * Enable UST channel for session and domain.
176 */
177 int channel_ust_enable(struct ltt_ust_session *usess, int domain,
178 struct ltt_ust_channel *uchan)
179 {
180 int ret = LTTNG_OK;
181
182 /* If already enabled, everything is OK */
183 if (uchan->enabled) {
184 DBG3("Channel %s already enabled. Skipping", uchan->name);
185 ret = LTTNG_ERR_UST_CHAN_EXIST;
186 goto end;
187 }
188
189 switch (domain) {
190 case LTTNG_DOMAIN_UST:
191 DBG2("Channel %s being enabled in UST global domain", uchan->name);
192 /* Enable channel for global domain */
193 ret = ust_app_enable_channel_glb(usess, uchan);
194 break;
195 #if 0
196 case LTTNG_DOMAIN_UST_PID:
197 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
198 case LTTNG_DOMAIN_UST_EXEC_NAME:
199 #endif
200 default:
201 ret = LTTNG_ERR_UND;
202 goto error;
203 }
204
205 if (ret < 0) {
206 if (ret != -EEXIST) {
207 ret = LTTNG_ERR_UST_CHAN_ENABLE_FAIL;
208 goto error;
209 } else {
210 ret = LTTNG_OK;
211 }
212 }
213
214 uchan->enabled = 1;
215 DBG2("Channel %s enabled successfully", uchan->name);
216
217 end:
218 error:
219 return ret;
220 }
221
222 /*
223 * Create UST channel for session and domain.
224 */
225 int channel_ust_create(struct ltt_ust_session *usess, int domain,
226 struct lttng_channel *attr)
227 {
228 int ret = LTTNG_OK;
229 struct ltt_ust_channel *uchan = NULL;
230 struct lttng_channel *defattr = NULL;
231
232 /* Creating channel attributes if needed */
233 if (attr == NULL) {
234 defattr = channel_new_default_attr(domain);
235 if (defattr == NULL) {
236 ret = LTTNG_ERR_FATAL;
237 goto error;
238 }
239 attr = defattr;
240 }
241
242 /*
243 * Validate UST buffer size and number of buffers: must both be
244 * power of 2 and nonzero. We validate right here for UST,
245 * because applications will not report the error to the user
246 * (unlike kernel tracing).
247 */
248 if (!attr->attr.subbuf_size || (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
249 ret = LTTNG_ERR_INVALID;
250 goto error;
251 }
252 if (!attr->attr.num_subbuf || (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
253 ret = LTTNG_ERR_INVALID;
254 goto error;
255 }
256
257 if (attr->attr.output != LTTNG_EVENT_MMAP) {
258 ret = LTTNG_ERR_NOT_SUPPORTED;
259 goto error;
260 }
261
262 /* Create UST channel */
263 uchan = trace_ust_create_channel(attr, usess->pathname);
264 if (uchan == NULL) {
265 ret = LTTNG_ERR_FATAL;
266 goto error;
267 }
268 uchan->enabled = 1;
269
270 switch (domain) {
271 case LTTNG_DOMAIN_UST:
272 DBG2("Channel %s being created in UST global domain", uchan->name);
273
274 /* Enable channel for global domain */
275 ret = ust_app_create_channel_glb(usess, uchan);
276 break;
277 #if 0
278 case LTTNG_DOMAIN_UST_PID:
279 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
280 case LTTNG_DOMAIN_UST_EXEC_NAME:
281 #endif
282 default:
283 ret = LTTNG_ERR_UND;
284 goto error_free_chan;
285 }
286
287 if (ret < 0 && ret != -EEXIST) {
288 ret = LTTNG_ERR_UST_CHAN_FAIL;
289 goto error_free_chan;
290 }
291
292 /* Adding the channel to the channel hash table. */
293 rcu_read_lock();
294 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
295 rcu_read_unlock();
296
297 DBG2("Channel %s created successfully", uchan->name);
298
299 free(defattr);
300 return LTTNG_OK;
301
302 error_free_chan:
303 /*
304 * No need to remove the channel from the hash table because at this point
305 * it was not added hence the direct call and no call_rcu().
306 */
307 trace_ust_destroy_channel(uchan);
308 error:
309 free(defattr);
310 return ret;
311 }
312
313 /*
314 * Disable UST channel for session and domain.
315 */
316 int channel_ust_disable(struct ltt_ust_session *usess, int domain,
317 struct ltt_ust_channel *uchan)
318 {
319 int ret = LTTNG_OK;
320
321 /* Already disabled */
322 if (uchan->enabled == 0) {
323 DBG2("Channel UST %s already disabled", uchan->name);
324 goto end;
325 }
326
327 /* Get the right channel's hashtable */
328 switch (domain) {
329 case LTTNG_DOMAIN_UST:
330 DBG2("Channel %s being disabled in UST global domain", uchan->name);
331 /* Disable channel for global domain */
332 ret = ust_app_disable_channel_glb(usess, uchan);
333 break;
334 #if 0
335 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
336 case LTTNG_DOMAIN_UST_EXEC_NAME:
337 case LTTNG_DOMAIN_UST_PID:
338 #endif
339 default:
340 ret = LTTNG_ERR_UND;
341 goto error;
342 }
343
344 if (ret < 0 && ret != -EEXIST) {
345 ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL;
346 goto error;
347 }
348
349 uchan->enabled = 0;
350
351 DBG2("Channel %s disabled successfully", uchan->name);
352
353 return LTTNG_OK;
354
355 end:
356 error:
357 return ret;
358 }
This page took 0.037583 seconds and 4 git commands to generate.