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