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