Fix: consumer: dangling chunk on buffer allocation failure
[lttng-tools.git] / src / bin / lttng-sessiond / channel.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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 <inttypes.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include <common/common.h>
15 #include <common/defaults.h>
16 #include <common/sessiond-comm/sessiond-comm.h>
17
18 #include "channel.h"
19 #include "lttng-sessiond.h"
20 #include "kernel.h"
21 #include "lttng-ust-ctl.h"
22 #include "lttng-ust-error.h"
23 #include "utils.h"
24 #include "ust-app.h"
25 #include "agent.h"
26
27 /*
28 * Return allocated channel attributes.
29 */
30 struct lttng_channel *channel_new_default_attr(int dom,
31 enum lttng_buffer_type type)
32 {
33 struct lttng_channel *chan;
34 const char *channel_name = DEFAULT_CHANNEL_NAME;
35 struct lttng_channel_extended *extended_attr = NULL;
36
37 chan = zmalloc(sizeof(struct lttng_channel));
38 if (chan == NULL) {
39 PERROR("zmalloc channel init");
40 goto error_alloc;
41 }
42
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
51 /* Same for all domains. */
52 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
53 chan->attr.tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
54 chan->attr.tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
55
56 switch (dom) {
57 case LTTNG_DOMAIN_KERNEL:
58 assert(type == LTTNG_BUFFER_GLOBAL);
59 chan->attr.subbuf_size =
60 default_get_kernel_channel_subbuf_size();
61 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
62 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
63 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
64 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
65 chan->attr.live_timer_interval = DEFAULT_KERNEL_CHANNEL_LIVE_TIMER;
66 extended_attr->blocking_timeout = DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
67 extended_attr->monitor_timer_interval =
68 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
69 break;
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;
79 case LTTNG_DOMAIN_UST:
80 common_ust:
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;
90 chan->attr.live_timer_interval =
91 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER;
92 extended_attr->blocking_timeout = DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
93 extended_attr->monitor_timer_interval =
94 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
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;
105 chan->attr.live_timer_interval =
106 DEFAULT_UST_PID_CHANNEL_LIVE_TIMER;
107 extended_attr->blocking_timeout = DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
108 extended_attr->monitor_timer_interval =
109 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
110 break;
111 }
112 break;
113 default:
114 goto error; /* Not implemented */
115 }
116
117 if (snprintf(chan->name, sizeof(chan->name), "%s",
118 channel_name) < 0) {
119 PERROR("snprintf default channel name");
120 goto error;
121 }
122 return chan;
123
124 error:
125 free(extended_attr);
126 free(chan);
127 error_alloc:
128 return NULL;
129 }
130
131 void 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
140 /*
141 * Disable kernel channel of the kernel session.
142 */
143 int channel_kernel_disable(struct ltt_kernel_session *ksession,
144 char *channel_name)
145 {
146 int ret;
147 struct ltt_kernel_channel *kchan;
148
149 assert(ksession);
150 assert(channel_name);
151
152 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
153 if (kchan == NULL) {
154 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
155 goto error;
156 }
157
158 /* Only if channel is enabled disable it. */
159 if (kchan->enabled == 1) {
160 ret = kernel_disable_channel(kchan);
161 if (ret < 0 && ret != -EEXIST) {
162 ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL;
163 goto error;
164 }
165 }
166
167 ret = LTTNG_OK;
168
169 error:
170 return ret;
171 }
172
173 /*
174 * Enable kernel channel of the kernel session.
175 */
176 int channel_kernel_enable(struct ltt_kernel_session *ksession,
177 struct ltt_kernel_channel *kchan)
178 {
179 int ret;
180
181 assert(ksession);
182 assert(kchan);
183
184 if (kchan->enabled == 0) {
185 ret = kernel_enable_channel(kchan);
186 if (ret < 0) {
187 ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL;
188 goto error;
189 }
190 } else {
191 ret = LTTNG_ERR_KERN_CHAN_EXIST;
192 goto error;
193 }
194
195 ret = LTTNG_OK;
196
197 error:
198 return ret;
199 }
200
201 static 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
214 static 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
223 /*
224 * Create kernel channel of the kernel session and notify kernel thread.
225 */
226 int channel_kernel_create(struct ltt_kernel_session *ksession,
227 struct lttng_channel *attr, int kernel_pipe)
228 {
229 int ret;
230 struct lttng_channel *defattr = NULL;
231
232 assert(ksession);
233
234 /* Creating channel attributes if needed */
235 if (attr == NULL) {
236 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
237 LTTNG_BUFFER_GLOBAL);
238 if (defattr == NULL) {
239 ret = LTTNG_ERR_FATAL;
240 goto error;
241 }
242 attr = defattr;
243 }
244
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
253 /* Validate common channel properties. */
254 if (channel_validate(attr) < 0) {
255 ret = LTTNG_ERR_INVALID;
256 goto error;
257 }
258
259 if (channel_validate_kernel(attr) < 0) {
260 ret = LTTNG_ERR_INVALID;
261 goto error;
262 }
263
264 /* Channel not found, creating it */
265 ret = kernel_create_channel(ksession, attr);
266 if (ret < 0) {
267 ret = LTTNG_ERR_KERN_CHAN_FAIL;
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) {
274 ret = LTTNG_ERR_FATAL;
275 goto error;
276 }
277
278 ret = LTTNG_OK;
279 error:
280 channel_attr_destroy(defattr);
281 return ret;
282 }
283
284 /*
285 * Enable UST channel for session and domain.
286 */
287 int channel_ust_enable(struct ltt_ust_session *usess,
288 struct ltt_ust_channel *uchan)
289 {
290 int ret = LTTNG_OK;
291
292 assert(usess);
293 assert(uchan);
294
295 /* If already enabled, everything is OK */
296 if (uchan->enabled) {
297 DBG3("Channel %s already enabled. Skipping", uchan->name);
298 ret = LTTNG_ERR_UST_CHAN_EXIST;
299 goto end;
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;
312 }
313
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 */
324 (void) ust_app_enable_channel_glb(usess, uchan);
325
326
327 end:
328 return ret;
329 }
330
331 /*
332 * Create UST channel for session and domain.
333 */
334 int channel_ust_create(struct ltt_ust_session *usess,
335 struct lttng_channel *attr, enum lttng_buffer_type type)
336 {
337 int ret = LTTNG_OK;
338 struct ltt_ust_channel *uchan = NULL;
339 struct lttng_channel *defattr = NULL;
340 enum lttng_domain_type domain = LTTNG_DOMAIN_UST;
341 bool chan_published = false;
342
343 assert(usess);
344
345 /* Creating channel attributes if needed */
346 if (attr == NULL) {
347 defattr = channel_new_default_attr(LTTNG_DOMAIN_UST, type);
348 if (defattr == NULL) {
349 ret = LTTNG_ERR_FATAL;
350 goto error;
351 }
352 attr = defattr;
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 }
365 }
366
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. */
376 if (usess->snapshot_mode) {
377 attr->attr.output = LTTNG_EVENT_MMAP;
378 }
379
380 /* Validate common channel properties. */
381 if (channel_validate(attr) < 0) {
382 ret = LTTNG_ERR_INVALID;
383 goto error;
384 }
385
386 /*
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).
390 */
391 if (!attr->attr.subbuf_size ||
392 (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
393 ret = LTTNG_ERR_INVALID;
394 goto error;
395 }
396
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
405 if (!attr->attr.num_subbuf ||
406 (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
407 ret = LTTNG_ERR_INVALID;
408 goto error;
409 }
410
411 if (attr->attr.output != LTTNG_EVENT_MMAP) {
412 ret = LTTNG_ERR_NOT_SUPPORTED;
413 goto error;
414 }
415
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
426 /* Validate buffer type. */
427 switch (type) {
428 case LTTNG_BUFFER_PER_PID:
429 break;
430 case LTTNG_BUFFER_PER_UID:
431 break;
432 default:
433 ret = LTTNG_ERR_BUFFER_NOT_SUPPORTED;
434 goto error;
435 }
436
437 /* Create UST channel */
438 uchan = trace_ust_create_channel(attr, domain);
439 if (uchan == NULL) {
440 ret = LTTNG_ERR_FATAL;
441 goto error;
442 }
443
444 uchan->enabled = 1;
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;
461 goto error_free_chan;
462 }
463
464 /* Adding the channel to the channel hash table. */
465 rcu_read_lock();
466 if (strncmp(uchan->name, DEFAULT_METADATA_NAME,
467 sizeof(uchan->name))) {
468 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
469 chan_published = true;
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));
478 }
479 rcu_read_unlock();
480
481 DBG2("Channel %s created successfully", uchan->name);
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;
489 goto error_remove_chan;
490 }
491 agent_add(agt, usess->agents);
492 }
493 }
494
495 channel_attr_destroy(defattr);
496 return LTTNG_OK;
497
498 error_remove_chan:
499 if (chan_published) {
500 trace_ust_delete_channel(usess->domain_global.channels, uchan);
501 }
502 error_free_chan:
503 trace_ust_destroy_channel(uchan);
504 error:
505 channel_attr_destroy(defattr);
506 return ret;
507 }
508
509 /*
510 * Disable UST channel for session and domain.
511 */
512 int channel_ust_disable(struct ltt_ust_session *usess,
513 struct ltt_ust_channel *uchan)
514 {
515 int ret = LTTNG_OK;
516
517 assert(usess);
518 assert(uchan);
519
520 /* Already disabled */
521 if (uchan->enabled == 0) {
522 DBG2("Channel UST %s already disabled", uchan->name);
523 goto end;
524 }
525 if (!usess->active) {
526 goto end;
527 }
528
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);
532 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
533 ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL;
534 goto error;
535 }
536
537 uchan->enabled = 0;
538
539 DBG2("Channel %s disabled successfully", uchan->name);
540
541 return LTTNG_OK;
542
543 end:
544 error:
545 return ret;
546 }
This page took 0.092304 seconds and 4 git commands to generate.