Fix: validate number of subbuffers after tweaking properties
[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
45 chan = zmalloc(sizeof(struct lttng_channel));
46 if (chan == NULL) {
47 PERROR("zmalloc channel init");
48 goto error_alloc;
49 }
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 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 =
84 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
85 chan->attr.read_timer_interval =
86 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
87 chan->attr.live_timer_interval =
88 DEFAULT_UST_UID_CHANNEL_LIVE_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 =
96 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
97 chan->attr.read_timer_interval =
98 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
99 chan->attr.live_timer_interval =
100 DEFAULT_UST_UID_CHANNEL_LIVE_TIMER;
101 break;
102 }
103 break;
104 default:
105 goto error; /* Not implemented */
106 }
107
108 if (snprintf(chan->name, sizeof(chan->name), "%s",
109 channel_name) < 0) {
110 PERROR("snprintf default channel name");
111 goto error;
112 }
113 return chan;
114
115 error:
116 free(chan);
117 error_alloc:
118 return NULL;
119 }
120
121 /*
122 * Disable kernel channel of the kernel session.
123 */
124 int channel_kernel_disable(struct ltt_kernel_session *ksession,
125 char *channel_name)
126 {
127 int ret;
128 struct ltt_kernel_channel *kchan;
129
130 assert(ksession);
131 assert(channel_name);
132
133 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
134 if (kchan == NULL) {
135 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
136 goto error;
137 }
138
139 /* Only if channel is enabled disable it. */
140 if (kchan->enabled == 1) {
141 ret = kernel_disable_channel(kchan);
142 if (ret < 0 && ret != -EEXIST) {
143 ret = LTTNG_ERR_KERN_CHAN_DISABLE_FAIL;
144 goto error;
145 }
146 }
147
148 ret = LTTNG_OK;
149
150 error:
151 return ret;
152 }
153
154 /*
155 * Enable kernel channel of the kernel session.
156 */
157 int channel_kernel_enable(struct ltt_kernel_session *ksession,
158 struct ltt_kernel_channel *kchan)
159 {
160 int ret;
161
162 assert(ksession);
163 assert(kchan);
164
165 if (kchan->enabled == 0) {
166 ret = kernel_enable_channel(kchan);
167 if (ret < 0) {
168 ret = LTTNG_ERR_KERN_CHAN_ENABLE_FAIL;
169 goto error;
170 }
171 } else {
172 ret = LTTNG_ERR_KERN_CHAN_EXIST;
173 goto error;
174 }
175
176 ret = LTTNG_OK;
177
178 error:
179 return ret;
180 }
181
182 static int channel_validate(struct lttng_channel *attr)
183 {
184 /*
185 * The ringbuffer (both in user space and kernel) behaves badly
186 * in overwrite mode and with less than 2 subbuffers so block it
187 * right away and send back an invalid attribute error.
188 */
189 if (attr->attr.overwrite && attr->attr.num_subbuf < 2) {
190 return -1;
191 }
192 return 0;
193 }
194
195 /*
196 * Create kernel channel of the kernel session and notify kernel thread.
197 */
198 int channel_kernel_create(struct ltt_kernel_session *ksession,
199 struct lttng_channel *attr, int kernel_pipe)
200 {
201 int ret;
202 struct lttng_channel *defattr = NULL;
203
204 assert(ksession);
205
206 /* Creating channel attributes if needed */
207 if (attr == NULL) {
208 defattr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
209 LTTNG_BUFFER_GLOBAL);
210 if (defattr == NULL) {
211 ret = LTTNG_ERR_FATAL;
212 goto error;
213 }
214 attr = defattr;
215 }
216
217 if (ksession->snapshot_mode) {
218 /* Force channel attribute for snapshot mode. */
219 attr->attr.overwrite = 1;
220 attr->attr.output = LTTNG_EVENT_MMAP;
221 }
222
223 /* Validate common channel properties. */
224 if (channel_validate(attr) < 0) {
225 ret = LTTNG_ERR_INVALID;
226 goto error;
227 }
228
229 /* Channel not found, creating it */
230 ret = kernel_create_channel(ksession, attr);
231 if (ret < 0) {
232 ret = LTTNG_ERR_KERN_CHAN_FAIL;
233 goto error;
234 }
235
236 /* Notify kernel thread that there is a new channel */
237 ret = notify_thread_pipe(kernel_pipe);
238 if (ret < 0) {
239 ret = LTTNG_ERR_FATAL;
240 goto error;
241 }
242
243 ret = LTTNG_OK;
244 error:
245 free(defattr);
246 return ret;
247 }
248
249 /*
250 * Enable UST channel for session and domain.
251 */
252 int channel_ust_enable(struct ltt_ust_session *usess,
253 struct ltt_ust_channel *uchan)
254 {
255 int ret = LTTNG_OK;
256
257 assert(usess);
258 assert(uchan);
259
260 /* If already enabled, everything is OK */
261 if (uchan->enabled) {
262 DBG3("Channel %s already enabled. Skipping", uchan->name);
263 ret = LTTNG_ERR_UST_CHAN_EXIST;
264 goto end;
265 }
266
267 DBG2("Channel %s being enabled in UST domain", uchan->name);
268
269 /*
270 * Enable channel for UST global domain on all applications. Ignore return
271 * value here since whatever error we got, it means that the channel was
272 * not created on one or many registered applications and we can not report
273 * this to the user yet. However, at this stage, the channel was
274 * successfully created on the session daemon side so the enable-channel
275 * command is a success.
276 */
277 (void) ust_app_enable_channel_glb(usess, uchan);
278
279 uchan->enabled = 1;
280 DBG2("Channel %s enabled successfully", uchan->name);
281
282 end:
283 return ret;
284 }
285
286 /*
287 * Create UST channel for session and domain.
288 */
289 int channel_ust_create(struct ltt_ust_session *usess,
290 struct lttng_channel *attr, enum lttng_buffer_type type)
291 {
292 int ret = LTTNG_OK;
293 struct ltt_ust_channel *uchan = NULL;
294 struct lttng_channel *defattr = NULL;
295 enum lttng_domain_type domain = LTTNG_DOMAIN_UST;
296
297 assert(usess);
298
299 /* Creating channel attributes if needed */
300 if (attr == NULL) {
301 defattr = channel_new_default_attr(LTTNG_DOMAIN_UST, type);
302 if (defattr == NULL) {
303 ret = LTTNG_ERR_FATAL;
304 goto error;
305 }
306 attr = defattr;
307 } else {
308 /*
309 * HACK: Set the channel's subdomain (JUL, Log4j, Python, etc.)
310 * based on the default name.
311 */
312 if (!strcmp(attr->name, DEFAULT_JUL_CHANNEL_NAME)) {
313 domain = LTTNG_DOMAIN_JUL;
314 } else if (!strcmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME)) {
315 domain = LTTNG_DOMAIN_LOG4J;
316 } else if (!strcmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME)) {
317 domain = LTTNG_DOMAIN_PYTHON;
318 }
319 }
320
321 if (usess->snapshot_mode) {
322 /* Force channel attribute for snapshot mode. */
323 attr->attr.overwrite = 1;
324 attr->attr.output = LTTNG_EVENT_MMAP;
325 }
326
327 /* Validate common channel properties. */
328 if (channel_validate(attr) < 0) {
329 ret = LTTNG_ERR_INVALID;
330 goto error;
331 }
332
333 /*
334 * Validate UST buffer size and number of buffers: must both be power of 2
335 * and nonzero. We validate right here for UST, because applications will
336 * not report the error to the user (unlike kernel tracing).
337 */
338 if (!attr->attr.subbuf_size ||
339 (attr->attr.subbuf_size & (attr->attr.subbuf_size - 1))) {
340 ret = LTTNG_ERR_INVALID;
341 goto error;
342 }
343
344 /*
345 * Invalid subbuffer size if it's lower then the page size.
346 */
347 if (attr->attr.subbuf_size < page_size) {
348 ret = LTTNG_ERR_INVALID;
349 goto error;
350 }
351
352 if (!attr->attr.num_subbuf ||
353 (attr->attr.num_subbuf & (attr->attr.num_subbuf - 1))) {
354 ret = LTTNG_ERR_INVALID;
355 goto error;
356 }
357
358 if (attr->attr.output != LTTNG_EVENT_MMAP) {
359 ret = LTTNG_ERR_NOT_SUPPORTED;
360 goto error;
361 }
362
363 /*
364 * The tracefile_size should not be < to the subbuf_size, otherwise
365 * we won't be able to write the packets on disk
366 */
367 if ((attr->attr.tracefile_size > 0) &&
368 (attr->attr.tracefile_size < attr->attr.subbuf_size)) {
369 ret = LTTNG_ERR_INVALID;
370 goto error;
371 }
372
373 /* Validate buffer type. */
374 switch (type) {
375 case LTTNG_BUFFER_PER_PID:
376 break;
377 case LTTNG_BUFFER_PER_UID:
378 break;
379 default:
380 ret = LTTNG_ERR_BUFFER_NOT_SUPPORTED;
381 goto error;
382 }
383
384 /* Create UST channel */
385 uchan = trace_ust_create_channel(attr, domain);
386 if (uchan == NULL) {
387 ret = LTTNG_ERR_FATAL;
388 goto error;
389 }
390
391 uchan->enabled = 1;
392 if (trace_ust_is_max_id(usess->used_channel_id)) {
393 ret = LTTNG_ERR_UST_CHAN_FAIL;
394 goto error;
395 }
396 uchan->id = trace_ust_get_next_chan_id(usess);
397
398 DBG2("Channel %s is being created for UST with buffer %d and id %" PRIu64,
399 uchan->name, type, uchan->id);
400
401 /* Flag session buffer type. */
402 if (!usess->buffer_type_changed) {
403 usess->buffer_type = type;
404 usess->buffer_type_changed = 1;
405 } else if (usess->buffer_type != type) {
406 /* Buffer type was already set. Refuse to create channel. */
407 ret = LTTNG_ERR_BUFFER_TYPE_MISMATCH;
408 goto error_free_chan;
409 }
410
411 /* Enable channel for global domain */
412 ret = ust_app_create_channel_glb(usess, uchan);
413 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
414 ret = LTTNG_ERR_UST_CHAN_FAIL;
415 goto error_free_chan;
416 }
417
418 /* Adding the channel to the channel hash table. */
419 rcu_read_lock();
420 if (strncmp(uchan->name, DEFAULT_METADATA_NAME,
421 sizeof(uchan->name))) {
422 lttng_ht_add_unique_str(usess->domain_global.channels, &uchan->node);
423 } else {
424 /*
425 * Copy channel attribute to session if this is metadata so if NO
426 * application exists we can access that data in the shadow copy during
427 * the global update of newly registered application.
428 */
429 memcpy(&usess->metadata_attr, &uchan->attr,
430 sizeof(usess->metadata_attr));
431 }
432 rcu_read_unlock();
433
434 DBG2("Channel %s created successfully", uchan->name);
435 if (domain != LTTNG_DOMAIN_UST) {
436 struct agent *agt = trace_ust_find_agent(usess, domain);
437
438 if (!agt) {
439 agt = agent_create(domain);
440 if (!agt) {
441 ret = LTTNG_ERR_NOMEM;
442 goto error_free_chan;
443 }
444 agent_add(agt, usess->agents);
445 }
446 }
447
448 free(defattr);
449 return LTTNG_OK;
450
451 error_free_chan:
452 /*
453 * No need to remove the channel from the hash table because at this point
454 * it was not added hence the direct call and no call_rcu().
455 */
456 trace_ust_destroy_channel(uchan);
457 error:
458 free(defattr);
459 return ret;
460 }
461
462 /*
463 * Disable UST channel for session and domain.
464 */
465 int channel_ust_disable(struct ltt_ust_session *usess,
466 struct ltt_ust_channel *uchan)
467 {
468 int ret = LTTNG_OK;
469
470 assert(usess);
471 assert(uchan);
472
473 /* Already disabled */
474 if (uchan->enabled == 0) {
475 DBG2("Channel UST %s already disabled", uchan->name);
476 goto end;
477 }
478
479 DBG2("Channel %s being disabled in UST global domain", uchan->name);
480 /* Disable channel for global domain */
481 ret = ust_app_disable_channel_glb(usess, uchan);
482 if (ret < 0 && ret != -LTTNG_UST_ERR_EXIST) {
483 ret = LTTNG_ERR_UST_CHAN_DISABLE_FAIL;
484 goto error;
485 }
486
487 uchan->enabled = 0;
488
489 DBG2("Channel %s disabled successfully", uchan->name);
490
491 return LTTNG_OK;
492
493 end:
494 error:
495 return ret;
496 }
This page took 0.039473 seconds and 4 git commands to generate.