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