sessiond: query relayd configuration for availability of clear feature
[lttng-tools.git] / src / bin / lttng-sessiond / channel.c
CommitLineData
54d01ffb
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
54d01ffb 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
54d01ffb 8 *
d14d33bf
AM
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
54d01ffb 13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
54d01ffb
DG
17 */
18
6c1c0768 19#define _LGPL_SOURCE
7972aab2 20#include <inttypes.h>
56fff090 21#include <string.h>
54d01ffb
DG
22#include <unistd.h>
23
990570ed
DG
24#include <common/common.h>
25#include <common/defaults.h>
db758600 26#include <common/sessiond-comm/sessiond-comm.h>
54d01ffb
DG
27
28#include "channel.h"
12744796 29#include "lttng-sessiond.h"
4771f025 30#include "kernel.h"
75018ab6
JG
31#include "lttng-ust-ctl.h"
32#include "lttng-ust-error.h"
54d01ffb 33#include "utils.h"
7885e399 34#include "ust-app.h"
b63d638b 35#include "agent.h"
54d01ffb
DG
36
37/*
38 * Return allocated channel attributes.
39 */
0a9c6494
DG
40struct lttng_channel *channel_new_default_attr(int dom,
41 enum lttng_buffer_type type)
54d01ffb
DG
42{
43 struct lttng_channel *chan;
bdf64013 44 const char *channel_name = DEFAULT_CHANNEL_NAME;
e9404c27 45 struct lttng_channel_extended *extended_attr = NULL;
54d01ffb
DG
46
47 chan = zmalloc(sizeof(struct lttng_channel));
48 if (chan == NULL) {
7885e399 49 PERROR("zmalloc channel init");
54d01ffb
DG
50 goto error_alloc;
51 }
52
e9404c27
JG
53 extended_attr = zmalloc(sizeof(struct lttng_channel_extended));
54 if (!extended_attr) {
55 PERROR("zmalloc channel extended init");
56 goto error;
57 }
58
59 chan->attr.extended.ptr = extended_attr;
60
0a9c6494 61 /* Same for all domains. */
54d01ffb 62 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
0a9c6494
DG
63 chan->attr.tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
64 chan->attr.tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
54d01ffb
DG
65
66 switch (dom) {
1b1c65fa 67 case LTTNG_DOMAIN_KERNEL:
0a9c6494 68 assert(type == LTTNG_BUFFER_GLOBAL);
3e230f92
SM
69 chan->attr.subbuf_size =
70 default_get_kernel_channel_subbuf_size();
1b1c65fa
MD
71 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
72 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
6bb9e85f
MD
73 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
74 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
ecc48a90 75 chan->attr.live_timer_interval = DEFAULT_KERNEL_CHANNEL_LIVE_TIMER;
491d1539 76 extended_attr->blocking_timeout = DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
e9404c27
JG
77 extended_attr->monitor_timer_interval =
78 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
1b1c65fa 79 break;
bdf64013
JG
80 case LTTNG_DOMAIN_JUL:
81 channel_name = DEFAULT_JUL_CHANNEL_NAME;
82 goto common_ust;
83 case LTTNG_DOMAIN_LOG4J:
84 channel_name = DEFAULT_LOG4J_CHANNEL_NAME;
85 goto common_ust;
86 case LTTNG_DOMAIN_PYTHON:
87 channel_name = DEFAULT_PYTHON_CHANNEL_NAME;
88 goto common_ust;
1b1c65fa 89 case LTTNG_DOMAIN_UST:
bdf64013 90common_ust:
0a9c6494
DG
91 switch (type) {
92 case LTTNG_BUFFER_PER_UID:
93 chan->attr.subbuf_size = default_get_ust_uid_channel_subbuf_size();
94 chan->attr.num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
95 chan->attr.output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
96 chan->attr.switch_timer_interval =
97 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
98 chan->attr.read_timer_interval =
99 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
ecc48a90
JD
100 chan->attr.live_timer_interval =
101 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER;
491d1539 102 extended_attr->blocking_timeout = DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
e9404c27
JG
103 extended_attr->monitor_timer_interval =
104 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
0a9c6494
DG
105 break;
106 case LTTNG_BUFFER_PER_PID:
107 default:
108 chan->attr.subbuf_size = default_get_ust_pid_channel_subbuf_size();
109 chan->attr.num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
110 chan->attr.output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
111 chan->attr.switch_timer_interval =
112 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
113 chan->attr.read_timer_interval =
114 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
ecc48a90 115 chan->attr.live_timer_interval =
e9404c27 116 DEFAULT_UST_PID_CHANNEL_LIVE_TIMER;
491d1539 117 extended_attr->blocking_timeout = DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
e9404c27
JG
118 extended_attr->monitor_timer_interval =
119 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
0a9c6494
DG
120 break;
121 }
1b1c65fa
MD
122 break;
123 default:
124 goto error; /* Not implemented */
54d01ffb
DG
125 }
126
bdf64013
JG
127 if (snprintf(chan->name, sizeof(chan->name), "%s",
128 channel_name) < 0) {
129 PERROR("snprintf default channel name");
130 goto error;
131 }
54d01ffb
DG
132 return chan;
133
134error:
e9404c27 135 free(extended_attr);
54d01ffb
DG
136 free(chan);
137error_alloc:
138 return NULL;
139}
140
e9404c27
JG
141void channel_attr_destroy(struct lttng_channel *channel)
142{
143 if (!channel) {
144 return;
145 }
146 free(channel->attr.extended.ptr);
147 free(channel);
148}
149
54d01ffb
DG
150/*
151 * Disable kernel channel of the kernel session.
152 */
153int channel_kernel_disable(struct ltt_kernel_session *ksession,
154 char *channel_name)
155{
156 int ret;
157 struct ltt_kernel_channel *kchan;
158
0525e9ae
DG
159 assert(ksession);
160 assert(channel_name);
161
54d01ffb
DG
162 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
163 if (kchan == NULL) {
f73fabfd 164 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
54d01ffb 165 goto error;
0525e9ae
DG
166 }
167
168 /* Only if channel is enabled disable it. */
169 if (kchan->enabled == 1) {
54d01ffb 170 ret = kernel_disable_channel(kchan);
7885e399 171 if (ret < 0 && ret != -EEXIST) {
f73fabfd 172 ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL;
54d01ffb
DG
173 goto error;
174 }
175 }
176
f73fabfd 177 ret = LTTNG_OK;
54d01ffb
DG
178
179error:
180 return ret;
181}
182
183/*
184 * Enable kernel channel of the kernel session.
185 */
186int channel_kernel_enable(struct ltt_kernel_session *ksession,
187 struct ltt_kernel_channel *kchan)
188{
189 int ret;
190
0525e9ae
DG
191 assert(ksession);
192 assert(kchan);
193
54d01ffb
DG
194 if (kchan->enabled == 0) {
195 ret = kernel_enable_channel(kchan);
196 if (ret < 0) {
f73fabfd 197 ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL;
54d01ffb
DG
198 goto error;
199 }
42224349 200 } else {
f73fabfd 201 ret = LTTNG_ERR_KERN_CHAN_EXIST;
42224349 202 goto error;
54d01ffb
DG
203 }
204
f73fabfd 205 ret = LTTNG_OK;
54d01ffb
DG
206
207error:
208 return ret;
209}
210
da9d9d9c
MD
211static int channel_validate(struct lttng_channel *attr)
212{
213 /*
214 * The ringbuffer (both in user space and kernel) behaves badly
215 * in overwrite mode and with less than 2 subbuffers so block it
216 * right away and send back an invalid attribute error.
217 */
218 if (attr->attr.overwrite && attr->attr.num_subbuf < 2) {
219 return -1;
220 }
221 return 0;
222}
223
491d1539
MD
224static int channel_validate_kernel(struct lttng_channel *attr)
225{
226 /* Kernel channels do not support blocking timeout. */
227 if (((struct lttng_channel_extended *)attr->attr.extended.ptr)->blocking_timeout) {
228 return -1;
229 }
230 return 0;
231}
232
54d01ffb
DG
233/*
234 * Create kernel channel of the kernel session and notify kernel thread.
235 */
236int channel_kernel_create(struct ltt_kernel_session *ksession,
ff4d74e6 237 struct lttng_channel *attr, int kernel_pipe)
54d01ffb
DG
238{
239 int ret;
ff4d74e6 240 struct lttng_channel *defattr = NULL;
54d01ffb 241
0525e9ae
DG
242 assert(ksession);
243
54d01ffb
DG
244 /* Creating channel attributes if needed */
245 if (attr == NULL) {
0a9c6494
DG
246 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
247 LTTNG_BUFFER_GLOBAL);
ff4d74e6 248 if (defattr == NULL) {
f73fabfd 249 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
250 goto error;
251 }
ff4d74e6 252 attr = defattr;
54d01ffb
DG
253 }
254
8d5841ea
MD
255 /*
256 * Set the overwrite mode for this channel based on the session
257 * type unless the client explicitly overrides the channel mode.
258 */
259 if (attr->attr.overwrite == DEFAULT_CHANNEL_OVERWRITE) {
260 attr->attr.overwrite = !!ksession->snapshot_mode;
261 }
262
da9d9d9c
MD
263 /* Validate common channel properties. */
264 if (channel_validate(attr) < 0) {
265 ret = LTTNG_ERR_INVALID;
266 goto error;
267 }
268
491d1539
MD
269 if (channel_validate_kernel(attr) < 0) {
270 ret = LTTNG_ERR_INVALID;
271 goto error;
272 }
273
54d01ffb 274 /* Channel not found, creating it */
fdd9eb17 275 ret = kernel_create_channel(ksession, attr);
54d01ffb 276 if (ret < 0) {
f73fabfd 277 ret = LTTNG_ERR_KERN_CHAN_FAIL;
54d01ffb
DG
278 goto error;
279 }
280
281 /* Notify kernel thread that there is a new channel */
282 ret = notify_thread_pipe(kernel_pipe);
283 if (ret < 0) {
f73fabfd 284 ret = LTTNG_ERR_FATAL;
54d01ffb
DG
285 goto error;
286 }
287
f73fabfd 288 ret = LTTNG_OK;
54d01ffb 289error:
e9404c27 290 channel_attr_destroy(defattr);
54d01ffb
DG
291 return ret;
292}
7885e399
DG
293
294/*
295 * Enable UST channel for session and domain.
296 */
7972aab2 297int channel_ust_enable(struct ltt_ust_session *usess,
7885e399
DG
298 struct ltt_ust_channel *uchan)
299{
f73fabfd 300 int ret = LTTNG_OK;
7885e399 301
0525e9ae
DG
302 assert(usess);
303 assert(uchan);
304
7885e399
DG
305 /* If already enabled, everything is OK */
306 if (uchan->enabled) {
307 DBG3("Channel %s already enabled. Skipping", uchan->name);
f73fabfd 308 ret = LTTNG_ERR_UST_CHAN_EXIST;
7885e399 309 goto end;
88e3c2f5
JG
310 } else {
311 uchan->enabled = 1;
312 DBG2("Channel %s enabled successfully", uchan->name);
313 }
314
315 if (!usess->active) {
316 /*
317 * The channel will be activated against the apps
318 * when the session is started as part of the
319 * application channel "synchronize" operation.
320 */
321 goto end;
7885e399
DG
322 }
323
7972aab2
DG
324 DBG2("Channel %s being enabled in UST domain", uchan->name);
325
326 /*
327 * Enable channel for UST global domain on all applications. Ignore return
328 * value here since whatever error we got, it means that the channel was
329 * not created on one or many registered applications and we can not report
330 * this to the user yet. However, at this stage, the channel was
331 * successfully created on the session daemon side so the enable-channel
332 * command is a success.
333 */
d54b4440 334 (void) ust_app_enable_channel_glb(usess, uchan);
7885e399 335
7885e399
DG
336
337end:
7885e399
DG
338 return ret;
339}
340
341/*
342 * Create UST channel for session and domain.
343 */
7972aab2
DG
344int channel_ust_create(struct ltt_ust_session *usess,
345 struct lttng_channel *attr, enum lttng_buffer_type type)
7885e399 346{
f73fabfd 347 int ret = LTTNG_OK;
7885e399
DG
348 struct ltt_ust_channel *uchan = NULL;
349 struct lttng_channel *defattr = NULL;
b63d638b 350 enum lttng_domain_type domain = LTTNG_DOMAIN_UST;
f86e086c 351 bool chan_published = false;
7885e399 352
0525e9ae
DG
353 assert(usess);
354
7885e399
DG
355 /* Creating channel attributes if needed */
356 if (attr == NULL) {
0a9c6494 357 defattr = channel_new_default_attr(LTTNG_DOMAIN_UST, type);
7885e399 358 if (defattr == NULL) {
f73fabfd 359 ret = LTTNG_ERR_FATAL;
7885e399
DG
360 goto error;
361 }
362 attr = defattr;
b63d638b
JG
363 } else {
364 /*
365 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
366 * based on the default name.
367 */
368 if (!strcmp(attr->name, DEFAULT_JUL_CHANNEL_NAME)) {
369 domain = LTTNG_DOMAIN_JUL;
370 } else if (!strcmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME)) {
371 domain = LTTNG_DOMAIN_LOG4J;
372 } else if (!strcmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME)) {
373 domain = LTTNG_DOMAIN_PYTHON;
374 }
7885e399
DG
375 }
376
8d5841ea
MD
377 /*
378 * Set the overwrite mode for this channel based on the session
379 * type unless the client explicitly overrides the channel mode.
380 */
381 if (attr->attr.overwrite == DEFAULT_CHANNEL_OVERWRITE) {
382 attr->attr.overwrite = !!usess->snapshot_mode;
383 }
384
385 /* Enforce mmap output for snapshot sessions. */
27babd3a 386 if (usess->snapshot_mode) {
27babd3a
DG
387 attr->attr.output = LTTNG_EVENT_MMAP;
388 }
389
da9d9d9c
MD
390 /* Validate common channel properties. */
391 if (channel_validate(attr) < 0) {
392 ret = LTTNG_ERR_INVALID;
393 goto error;
394 }
395
b024d072 396 /*
0525e9ae
DG
397 * Validate UST buffer size and number of buffers: must both be power of 2
398 * and nonzero. We validate right here for UST, because applications will
399 * not report the error to the user (unlike kernel tracing).
b024d072 400 */
0525e9ae
DG
401 if (!attr->attr.subbuf_size ||
402 (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
f73fabfd 403 ret = LTTNG_ERR_INVALID;
b024d072
MD
404 goto error;
405 }
0525e9ae 406
12744796
DG
407 /*
408 * Invalid subbuffer size if it's lower then the page size.
409 */
410 if (attr->attr.subbuf_size < page_size) {
411 ret = LTTNG_ERR_INVALID;
412 goto error;
413 }
414
0525e9ae
DG
415 if (!attr->attr.num_subbuf ||
416 (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
f73fabfd 417 ret = LTTNG_ERR_INVALID;
b024d072
MD
418 goto error;
419 }
420
a79d84dd
DG
421 if (attr->attr.output != LTTNG_EVENT_MMAP) {
422 ret = LTTNG_ERR_NOT_SUPPORTED;
423 goto error;
424 }
425
1624d5b7
JD
426 /*
427 * The tracefile_size should not be < to the subbuf_size, otherwise
428 * we won't be able to write the packets on disk
429 */
430 if ((attr->attr.tracefile_size > 0) &&
431 (attr->attr.tracefile_size < attr->attr.subbuf_size)) {
432 ret = LTTNG_ERR_INVALID;
433 goto error;
434 }
435
2e8269f7
DG
436 /* Validate buffer type. */
437 switch (type) {
438 case LTTNG_BUFFER_PER_PID:
0a9c6494 439 break;
2e8269f7
DG
440 case LTTNG_BUFFER_PER_UID:
441 break;
442 default:
443 ret = LTTNG_ERR_BUFFER_NOT_SUPPORTED;
444 goto error;
445 }
446
7885e399 447 /* Create UST channel */
b63d638b 448 uchan = trace_ust_create_channel(attr, domain);
7885e399 449 if (uchan == NULL) {
f73fabfd 450 ret = LTTNG_ERR_FATAL;
7885e399
DG
451 goto error;
452 }
51755dc8 453
58f3ca76 454 uchan->enabled = 1;
7972aab2
DG
455 if (trace_ust_is_max_id(usess->used_channel_id)) {
456 ret = LTTNG_ERR_UST_CHAN_FAIL;
457 goto error;
458 }
459 uchan->id = trace_ust_get_next_chan_id(usess);
460
461 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64,
462 uchan->name, type, uchan->id);
463
464 /* Flag session buffer type. */
465 if (!usess->buffer_type_changed) {
466 usess->buffer_type = type;
467 usess->buffer_type_changed = 1;
468 } else if (usess->buffer_type != type) {
469 /* Buffer type was already set. Refuse to create channel. */
470 ret = LTTNG_ERR_BUFFER_TYPE_MISMATCH;
7885e399
DG
471 goto error_free_chan;
472 }
473
88e3c2f5
JG
474 if (usess->active) {
475 /* Enable channel for global domain */
476 ret = ust_app_create_channel_glb(usess, uchan);
477 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
478 ret = LTTNG_ERR_UST_CHAN_FAIL;
479 goto error_free_chan;
480 }
7885e399
DG
481 }
482
fc34caaa
DG
483 /* Adding the channel to the channel hash table. */
484 rcu_read_lock();
ad7a9107
DG
485 if (strncmp(uchan->name, DEFAULT_METADATA_NAME,
486 sizeof(uchan->name))) {
487 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
f86e086c 488 chan_published = true;
84ad93e8
DG
489 } else {
490 /*
491 * Copy channel attribute to session if this is metadata so if NO
492 * application exists we can access that data in the shadow copy during
493 * the global update of newly registered application.
494 */
495 memcpy(&usess->metadata_attr, &uchan->attr,
496 sizeof(usess->metadata_attr));
ad7a9107 497 }
fc34caaa
DG
498 rcu_read_unlock();
499
7885e399 500 DBG2("Channel %s created successfully", uchan->name);
b63d638b
JG
501 if (domain != LTTNG_DOMAIN_UST) {
502 struct agent *agt = trace_ust_find_agent(usess, domain);
503
504 if (!agt) {
505 agt = agent_create(domain);
506 if (!agt) {
507 ret = LTTNG_ERR_NOMEM;
f86e086c 508 goto error_remove_chan;
b63d638b
JG
509 }
510 agent_add(agt, usess->agents);
511 }
512 }
7885e399 513
e9404c27 514 channel_attr_destroy(defattr);
f73fabfd 515 return LTTNG_OK;
7885e399 516
f86e086c
MD
517error_remove_chan:
518 if (chan_published) {
519 trace_ust_delete_channel(usess->domain_global.channels, uchan);
520 }
7885e399
DG
521error_free_chan:
522 trace_ust_destroy_channel(uchan);
523error:
e9404c27 524 channel_attr_destroy(defattr);
7885e399
DG
525 return ret;
526}
527
528/*
529 * Disable UST channel for session and domain.
530 */
7972aab2 531int channel_ust_disable(struct ltt_ust_session *usess,
7885e399
DG
532 struct ltt_ust_channel *uchan)
533{
f73fabfd 534 int ret = LTTNG_OK;
7885e399 535
0525e9ae
DG
536 assert(usess);
537 assert(uchan);
538
7885e399
DG
539 /* Already disabled */
540 if (uchan->enabled == 0) {
541 DBG2("Channel UST %s already disabled", uchan->name);
542 goto end;
543 }
88e3c2f5
JG
544 if (!usess->active) {
545 goto end;
546 }
7885e399 547
7972aab2
DG
548 DBG2("Channel %s being disabled in UST global domain", uchan->name);
549 /* Disable channel for global domain */
550 ret = ust_app_disable_channel_glb(usess, uchan);
49c336c1 551 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
f73fabfd 552 ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL;
7885e399
DG
553 goto error;
554 }
555
556 uchan->enabled = 0;
557
558 DBG2("Channel %s disabled successfully", uchan->name);
559
f73fabfd 560 return LTTNG_OK;
7885e399
DG
561
562end:
563error:
564 return ret;
565}
This page took 0.089243 seconds and 4 git commands to generate.