Store the relay protocol version in the consumer_output
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
CommitLineData
2f77fc4b
DG
1/*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
2f77fc4b
DG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
6c1c0768 19#define _LGPL_SOURCE
2f77fc4b 20#include <assert.h>
6dc3064a 21#include <inttypes.h>
2f77fc4b
DG
22#include <urcu/list.h>
23#include <urcu/uatomic.h>
24
25#include <common/defaults.h>
26#include <common/common.h>
27#include <common/sessiond-comm/sessiond-comm.h>
28#include <common/relayd/relayd.h>
10a50311 29#include <common/utils.h>
f5436bfc 30#include <common/compat/string.h>
2f77fc4b
DG
31
32#include "channel.h"
33#include "consumer.h"
34#include "event.h"
8782cc74 35#include "health-sessiond.h"
2f77fc4b
DG
36#include "kernel.h"
37#include "kernel-consumer.h"
38#include "lttng-sessiond.h"
39#include "utils.h"
834978fd 40#include "syscall.h"
7c1d2758 41#include "agent.h"
2f77fc4b
DG
42
43#include "cmd.h"
44
45/*
46 * Used to keep a unique index for each relayd socket created where this value
47 * is associated with streams on the consumer so it can match the right relayd
d88aee68
DG
48 * to send to. It must be accessed with the relayd_net_seq_idx_lock
49 * held.
2f77fc4b 50 */
d88aee68
DG
51static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
52static uint64_t relayd_net_seq_idx;
2f77fc4b 53
7076b56e
JG
54static int validate_event_name(const char *);
55static int validate_ust_event_name(const char *);
56static int cmd_enable_event_internal(struct ltt_session *session,
57 struct lttng_domain *domain,
58 char *channel_name, struct lttng_event *event,
59 char *filter_expression,
60 struct lttng_filter_bytecode *filter,
61 struct lttng_event_exclusion *exclusion,
62 int wpipe);
63
2f77fc4b
DG
64/*
65 * Create a session path used by list_lttng_sessions for the case that the
66 * session consumer is on the network.
67 */
68static int build_network_session_path(char *dst, size_t size,
69 struct ltt_session *session)
70{
71 int ret, kdata_port, udata_port;
72 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
73 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
74
75 assert(session);
76 assert(dst);
77
78 memset(tmp_urls, 0, sizeof(tmp_urls));
79 memset(tmp_uurl, 0, sizeof(tmp_uurl));
80
81 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
82
83 if (session->kernel_session && session->kernel_session->consumer) {
84 kuri = &session->kernel_session->consumer->dst.net.control;
85 kdata_port = session->kernel_session->consumer->dst.net.data.port;
86 }
87
88 if (session->ust_session && session->ust_session->consumer) {
89 uuri = &session->ust_session->consumer->dst.net.control;
90 udata_port = session->ust_session->consumer->dst.net.data.port;
91 }
92
93 if (uuri == NULL && kuri == NULL) {
94 uri = &session->consumer->dst.net.control;
95 kdata_port = session->consumer->dst.net.data.port;
96 } else if (kuri && uuri) {
97 ret = uri_compare(kuri, uuri);
98 if (ret) {
99 /* Not Equal */
100 uri = kuri;
101 /* Build uuri URL string */
102 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
103 if (ret < 0) {
104 goto error;
105 }
106 } else {
107 uri = kuri;
108 }
109 } else if (kuri && uuri == NULL) {
110 uri = kuri;
111 } else if (uuri && kuri == NULL) {
112 uri = uuri;
113 }
114
115 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
116 if (ret < 0) {
117 goto error;
118 }
119
9aa9f900
DG
120 /*
121 * Do we have a UST url set. If yes, this means we have both kernel and UST
122 * to print.
123 */
9d035200 124 if (*tmp_uurl != '\0') {
2f77fc4b
DG
125 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
126 tmp_urls, kdata_port, tmp_uurl, udata_port);
127 } else {
9aa9f900 128 int dport;
bef08707 129 if (kuri || (!kuri && !uuri)) {
9aa9f900
DG
130 dport = kdata_port;
131 } else {
132 /* No kernel URI, use the UST port. */
133 dport = udata_port;
134 }
135 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
2f77fc4b
DG
136 }
137
138error:
139 return ret;
140}
141
fb83fe64
JD
142/*
143 * Get run-time attributes if the session has been started (discarded events,
144 * lost packets).
145 */
146static int get_kernel_runtime_stats(struct ltt_session *session,
147 struct ltt_kernel_channel *kchan, uint64_t *discarded_events,
148 uint64_t *lost_packets)
149{
150 int ret;
151
152 if (!session->has_been_started) {
153 ret = 0;
154 *discarded_events = 0;
155 *lost_packets = 0;
156 goto end;
157 }
158
159 ret = consumer_get_discarded_events(session->id, kchan->fd,
160 session->kernel_session->consumer,
161 discarded_events);
162 if (ret < 0) {
163 goto end;
164 }
165
166 ret = consumer_get_lost_packets(session->id, kchan->fd,
167 session->kernel_session->consumer,
168 lost_packets);
169 if (ret < 0) {
170 goto end;
171 }
172
173end:
174 return ret;
175}
176
177/*
178 * Get run-time attributes if the session has been started (discarded events,
179 * lost packets).
180 */
181static int get_ust_runtime_stats(struct ltt_session *session,
182 struct ltt_ust_channel *uchan, uint64_t *discarded_events,
183 uint64_t *lost_packets)
184{
185 int ret;
186 struct ltt_ust_session *usess;
187
188 usess = session->ust_session;
189
190 if (!usess || !session->has_been_started) {
191 *discarded_events = 0;
192 *lost_packets = 0;
193 ret = 0;
194 goto end;
195 }
196
197 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
198 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
199 &usess->buffer_reg_uid_list,
200 usess->consumer, uchan->id,
201 uchan->attr.overwrite,
202 discarded_events,
203 lost_packets);
204 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
205 ret = ust_app_pid_get_channel_runtime_stats(usess,
206 uchan, usess->consumer,
207 uchan->attr.overwrite,
208 discarded_events,
209 lost_packets);
210 if (ret < 0) {
211 goto end;
212 }
213 *discarded_events += uchan->per_pid_closed_app_discarded;
214 *lost_packets += uchan->per_pid_closed_app_lost;
215 } else {
216 ERR("Unsupported buffer type");
217 ret = -1;
218 goto end;
219 }
220
221end:
222 return ret;
223}
224
2f77fc4b
DG
225/*
226 * Fill lttng_channel array of all channels.
227 */
56a37563 228static void list_lttng_channels(enum lttng_domain_type domain,
fb83fe64
JD
229 struct ltt_session *session, struct lttng_channel *channels,
230 struct lttcomm_channel_extended *chan_exts)
2f77fc4b 231{
fb83fe64 232 int i = 0, ret;
2f77fc4b
DG
233 struct ltt_kernel_channel *kchan;
234
235 DBG("Listing channels for session %s", session->name);
236
237 switch (domain) {
238 case LTTNG_DOMAIN_KERNEL:
239 /* Kernel channels */
240 if (session->kernel_session != NULL) {
241 cds_list_for_each_entry(kchan,
242 &session->kernel_session->channel_list.head, list) {
fb83fe64
JD
243 uint64_t discarded_events, lost_packets;
244
245 ret = get_kernel_runtime_stats(session, kchan,
246 &discarded_events, &lost_packets);
247 if (ret < 0) {
248 goto end;
249 }
2f77fc4b
DG
250 /* Copy lttng_channel struct to array */
251 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
252 channels[i].enabled = kchan->enabled;
fb83fe64
JD
253 chan_exts[i].discarded_events =
254 discarded_events;
255 chan_exts[i].lost_packets = lost_packets;
2f77fc4b
DG
256 i++;
257 }
258 }
259 break;
260 case LTTNG_DOMAIN_UST:
261 {
262 struct lttng_ht_iter iter;
263 struct ltt_ust_channel *uchan;
264
e7fe706f 265 rcu_read_lock();
2f77fc4b
DG
266 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
267 &iter.iter, uchan, node.node) {
fb83fe64
JD
268 uint64_t discarded_events, lost_packets;
269
2f77fc4b
DG
270 strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
271 channels[i].attr.overwrite = uchan->attr.overwrite;
272 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
273 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
274 channels[i].attr.switch_timer_interval =
275 uchan->attr.switch_timer_interval;
276 channels[i].attr.read_timer_interval =
277 uchan->attr.read_timer_interval;
278 channels[i].enabled = uchan->enabled;
2f785fe7
DG
279 channels[i].attr.tracefile_size = uchan->tracefile_size;
280 channels[i].attr.tracefile_count = uchan->tracefile_count;
5e4435a4
JG
281
282 /*
283 * Map enum lttng_ust_output to enum lttng_event_output.
284 */
2f77fc4b
DG
285 switch (uchan->attr.output) {
286 case LTTNG_UST_MMAP:
2f77fc4b
DG
287 channels[i].attr.output = LTTNG_EVENT_MMAP;
288 break;
5e4435a4
JG
289 default:
290 /*
291 * LTTNG_UST_MMAP is the only supported UST
292 * output mode.
293 */
294 assert(0);
295 break;
2f77fc4b 296 }
fb83fe64
JD
297
298 ret = get_ust_runtime_stats(session, uchan,
299 &discarded_events, &lost_packets);
300 if (ret < 0) {
301 break;
302 }
303 chan_exts[i].discarded_events = discarded_events;
304 chan_exts[i].lost_packets = lost_packets;
2f77fc4b
DG
305 i++;
306 }
e7fe706f 307 rcu_read_unlock();
2f77fc4b
DG
308 break;
309 }
310 default:
311 break;
312 }
fb83fe64
JD
313
314end:
315 return;
2f77fc4b
DG
316}
317
b4e3ceb9 318static void increment_extended_len(const char *filter_expression,
795d57ce 319 struct lttng_event_exclusion *exclusion, size_t *extended_len)
b4e3ceb9
PP
320{
321 *extended_len += sizeof(struct lttcomm_event_extended_header);
322
323 if (filter_expression) {
324 *extended_len += strlen(filter_expression) + 1;
325 }
795d57ce
PP
326
327 if (exclusion) {
328 *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN;
329 }
b4e3ceb9
PP
330}
331
332static void append_extended_info(const char *filter_expression,
795d57ce 333 struct lttng_event_exclusion *exclusion, void **extended_at)
b4e3ceb9
PP
334{
335 struct lttcomm_event_extended_header extended_header;
336 size_t filter_len = 0;
795d57ce 337 size_t nb_exclusions = 0;
b4e3ceb9
PP
338
339 if (filter_expression) {
340 filter_len = strlen(filter_expression) + 1;
341 }
342
795d57ce
PP
343 if (exclusion) {
344 nb_exclusions = exclusion->count;
345 }
346
347 /* Set header fields */
b4e3ceb9 348 extended_header.filter_len = filter_len;
795d57ce
PP
349 extended_header.nb_exclusions = nb_exclusions;
350
351 /* Copy header */
b4e3ceb9
PP
352 memcpy(*extended_at, &extended_header, sizeof(extended_header));
353 *extended_at += sizeof(extended_header);
795d57ce
PP
354
355 /* Copy filter string */
356 if (filter_expression) {
357 memcpy(*extended_at, filter_expression, filter_len);
358 *extended_at += filter_len;
359 }
360
361 /* Copy exclusion names */
362 if (exclusion) {
363 size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN;
364
365 memcpy(*extended_at, &exclusion->names, len);
366 *extended_at += len;
367 }
b4e3ceb9
PP
368}
369
3c6a091f 370/*
022d91ba 371 * Create a list of agent domain events.
3c6a091f
DG
372 *
373 * Return number of events in list on success or else a negative value.
374 */
022d91ba 375static int list_lttng_agent_events(struct agent *agt,
b4e3ceb9 376 struct lttng_event **events, size_t *total_size)
3c6a091f
DG
377{
378 int i = 0, ret = 0;
379 unsigned int nb_event = 0;
022d91ba 380 struct agent_event *event;
3c6a091f
DG
381 struct lttng_event *tmp_events;
382 struct lttng_ht_iter iter;
b4e3ceb9
PP
383 size_t extended_len = 0;
384 void *extended_at;
3c6a091f 385
022d91ba 386 assert(agt);
3c6a091f
DG
387 assert(events);
388
022d91ba 389 DBG3("Listing agent events");
3c6a091f 390
e5bbf678 391 rcu_read_lock();
022d91ba 392 nb_event = lttng_ht_get_count(agt->events);
e5bbf678 393 rcu_read_unlock();
3c6a091f
DG
394 if (nb_event == 0) {
395 ret = nb_event;
b4e3ceb9 396 *total_size = 0;
3c6a091f
DG
397 goto error;
398 }
399
b4e3ceb9
PP
400 /* Compute required extended infos size */
401 extended_len = nb_event * sizeof(struct lttcomm_event_extended_header);
402
403 /*
404 * This is only valid because the commands which add events are
405 * processed in the same thread as the listing.
406 */
407 rcu_read_lock();
408 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
795d57ce
PP
409 increment_extended_len(event->filter_expression, NULL,
410 &extended_len);
b4e3ceb9
PP
411 }
412 rcu_read_unlock();
413
414 *total_size = nb_event * sizeof(*tmp_events) + extended_len;
415 tmp_events = zmalloc(*total_size);
3c6a091f 416 if (!tmp_events) {
022d91ba 417 PERROR("zmalloc agent events session");
3c6a091f
DG
418 ret = -LTTNG_ERR_FATAL;
419 goto error;
420 }
421
b4e3ceb9
PP
422 extended_at = ((uint8_t *) tmp_events) +
423 nb_event * sizeof(struct lttng_event);
424
3c6a091f 425 rcu_read_lock();
022d91ba 426 cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) {
3c6a091f
DG
427 strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name));
428 tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0';
429 tmp_events[i].enabled = event->enabled;
2106efa0 430 tmp_events[i].loglevel = event->loglevel_value;
ff94328f 431 tmp_events[i].loglevel_type = event->loglevel_type;
3c6a091f 432 i++;
b4e3ceb9
PP
433
434 /* Append extended info */
795d57ce
PP
435 append_extended_info(event->filter_expression, NULL,
436 &extended_at);
3c6a091f
DG
437 }
438 rcu_read_unlock();
439
440 *events = tmp_events;
441 ret = nb_event;
442
443error:
444 assert(nb_event == i);
445 return ret;
446}
447
2f77fc4b
DG
448/*
449 * Create a list of ust global domain events.
450 */
451static int list_lttng_ust_global_events(char *channel_name,
b4e3ceb9
PP
452 struct ltt_ust_domain_global *ust_global,
453 struct lttng_event **events, size_t *total_size)
2f77fc4b
DG
454{
455 int i = 0, ret = 0;
456 unsigned int nb_event = 0;
457 struct lttng_ht_iter iter;
458 struct lttng_ht_node_str *node;
459 struct ltt_ust_channel *uchan;
460 struct ltt_ust_event *uevent;
461 struct lttng_event *tmp;
b4e3ceb9
PP
462 size_t extended_len = 0;
463 void *extended_at;
2f77fc4b
DG
464
465 DBG("Listing UST global events for channel %s", channel_name);
466
467 rcu_read_lock();
468
469 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
470 node = lttng_ht_iter_get_node_str(&iter);
471 if (node == NULL) {
f73fabfd 472 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
d31d3e8c 473 goto end;
2f77fc4b
DG
474 }
475
476 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
477
3c442be3 478 nb_event = lttng_ht_get_count(uchan->events);
2f77fc4b
DG
479 if (nb_event == 0) {
480 ret = nb_event;
b4e3ceb9 481 *total_size = 0;
d31d3e8c 482 goto end;
2f77fc4b
DG
483 }
484
485 DBG3("Listing UST global %d events", nb_event);
486
b4e3ceb9
PP
487 /* Compute required extended infos size */
488 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
489 if (uevent->internal) {
490 nb_event--;
491 continue;
492 }
493
494 increment_extended_len(uevent->filter_expression,
795d57ce 495 uevent->exclusion, &extended_len);
b4e3ceb9 496 }
d31d3e8c
PP
497 if (nb_event == 0) {
498 /* All events are internal, skip. */
499 ret = 0;
500 *total_size = 0;
501 goto end;
502 }
b4e3ceb9
PP
503
504 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
505 tmp = zmalloc(*total_size);
2f77fc4b 506 if (tmp == NULL) {
b12c38df
JG
507 ret = -LTTNG_ERR_FATAL;
508 goto end;
2f77fc4b
DG
509 }
510
b4e3ceb9
PP
511 extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event);
512
2f77fc4b 513 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
3c442be3
JG
514 if (uevent->internal) {
515 /* This event should remain hidden from clients */
3c442be3
JG
516 continue;
517 }
2f77fc4b
DG
518 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
519 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
520 tmp[i].enabled = uevent->enabled;
521
522 switch (uevent->attr.instrumentation) {
523 case LTTNG_UST_TRACEPOINT:
524 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
525 break;
526 case LTTNG_UST_PROBE:
527 tmp[i].type = LTTNG_EVENT_PROBE;
528 break;
529 case LTTNG_UST_FUNCTION:
530 tmp[i].type = LTTNG_EVENT_FUNCTION;
531 break;
532 }
533
534 tmp[i].loglevel = uevent->attr.loglevel;
535 switch (uevent->attr.loglevel_type) {
536 case LTTNG_UST_LOGLEVEL_ALL:
537 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
538 break;
539 case LTTNG_UST_LOGLEVEL_RANGE:
540 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
541 break;
542 case LTTNG_UST_LOGLEVEL_SINGLE:
543 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
544 break;
545 }
546 if (uevent->filter) {
547 tmp[i].filter = 1;
548 }
4634f12e
JI
549 if (uevent->exclusion) {
550 tmp[i].exclusion = 1;
551 }
2f77fc4b 552 i++;
b4e3ceb9
PP
553
554 /* Append extended info */
795d57ce
PP
555 append_extended_info(uevent->filter_expression,
556 uevent->exclusion, &extended_at);
2f77fc4b
DG
557 }
558
559 ret = nb_event;
560 *events = tmp;
d31d3e8c 561end:
2f77fc4b
DG
562 rcu_read_unlock();
563 return ret;
564}
565
566/*
567 * Fill lttng_event array of all kernel events in the channel.
568 */
569static int list_lttng_kernel_events(char *channel_name,
b4e3ceb9
PP
570 struct ltt_kernel_session *kernel_session,
571 struct lttng_event **events, size_t *total_size)
2f77fc4b
DG
572{
573 int i = 0, ret;
574 unsigned int nb_event;
575 struct ltt_kernel_event *event;
576 struct ltt_kernel_channel *kchan;
b4e3ceb9
PP
577 size_t extended_len = 0;
578 void *extended_at;
2f77fc4b
DG
579
580 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
581 if (kchan == NULL) {
f73fabfd 582 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2f77fc4b
DG
583 goto error;
584 }
585
586 nb_event = kchan->event_count;
587
588 DBG("Listing events for channel %s", kchan->channel->name);
589
590 if (nb_event == 0) {
b4e3ceb9 591 *total_size = 0;
834978fd 592 *events = NULL;
db906c12 593 goto end;
2f77fc4b
DG
594 }
595
b4e3ceb9
PP
596 /* Compute required extended infos size */
597 cds_list_for_each_entry(event, &kchan->events_list.head, list) {
795d57ce
PP
598 increment_extended_len(event->filter_expression, NULL,
599 &extended_len);
b4e3ceb9
PP
600 }
601
602 *total_size = nb_event * sizeof(struct lttng_event) + extended_len;
603 *events = zmalloc(*total_size);
2f77fc4b 604 if (*events == NULL) {
f73fabfd 605 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
606 goto error;
607 }
608
ab4aa612 609 extended_at = ((void *) *events) +
b4e3ceb9
PP
610 nb_event * sizeof(struct lttng_event);
611
2f77fc4b
DG
612 /* Kernel channels */
613 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
614 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
615 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
616 (*events)[i].enabled = event->enabled;
00f992f2
JG
617 (*events)[i].filter =
618 (unsigned char) !!event->filter_expression;
2f77fc4b
DG
619
620 switch (event->event->instrumentation) {
621 case LTTNG_KERNEL_TRACEPOINT:
622 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
623 break;
2f77fc4b 624 case LTTNG_KERNEL_KRETPROBE:
1896972b
DG
625 (*events)[i].type = LTTNG_EVENT_FUNCTION;
626 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
627 sizeof(struct lttng_kernel_kprobe));
628 break;
629 case LTTNG_KERNEL_KPROBE:
2f77fc4b
DG
630 (*events)[i].type = LTTNG_EVENT_PROBE;
631 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
632 sizeof(struct lttng_kernel_kprobe));
633 break;
634 case LTTNG_KERNEL_FUNCTION:
635 (*events)[i].type = LTTNG_EVENT_FUNCTION;
636 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
637 sizeof(struct lttng_kernel_function));
638 break;
639 case LTTNG_KERNEL_NOOP:
640 (*events)[i].type = LTTNG_EVENT_NOOP;
641 break;
642 case LTTNG_KERNEL_SYSCALL:
643 (*events)[i].type = LTTNG_EVENT_SYSCALL;
644 break;
645 case LTTNG_KERNEL_ALL:
646 assert(0);
647 break;
648 }
649 i++;
b4e3ceb9
PP
650
651 /* Append extended info */
795d57ce
PP
652 append_extended_info(event->filter_expression, NULL,
653 &extended_at);
2f77fc4b
DG
654 }
655
db906c12 656end:
2f77fc4b
DG
657 return nb_event;
658
659error:
f73fabfd
DG
660 /* Negate the error code to differentiate the size from an error */
661 return -ret;
2f77fc4b
DG
662}
663
664/*
665 * Add URI so the consumer output object. Set the correct path depending on the
666 * domain adding the default trace directory.
667 */
668static int add_uri_to_consumer(struct consumer_output *consumer,
56a37563
JG
669 struct lttng_uri *uri, enum lttng_domain_type domain,
670 const char *session_name)
2f77fc4b 671{
f73fabfd 672 int ret = LTTNG_OK;
2f77fc4b
DG
673 const char *default_trace_dir;
674
675 assert(uri);
676
677 if (consumer == NULL) {
678 DBG("No consumer detected. Don't add URI. Stopping.");
f73fabfd 679 ret = LTTNG_ERR_NO_CONSUMER;
2f77fc4b
DG
680 goto error;
681 }
682
683 switch (domain) {
684 case LTTNG_DOMAIN_KERNEL:
685 default_trace_dir = DEFAULT_KERNEL_TRACE_DIR;
686 break;
687 case LTTNG_DOMAIN_UST:
688 default_trace_dir = DEFAULT_UST_TRACE_DIR;
689 break;
690 default:
691 /*
692 * This case is possible is we try to add the URI to the global tracing
693 * session consumer object which in this case there is no subdir.
694 */
695 default_trace_dir = "";
696 }
697
698 switch (uri->dtype) {
699 case LTTNG_DST_IPV4:
700 case LTTNG_DST_IPV6:
701 DBG2("Setting network URI to consumer");
702
df75acac
DG
703 if (consumer->type == CONSUMER_DST_NET) {
704 if ((uri->stype == LTTNG_STREAM_CONTROL &&
785d2d0d
DG
705 consumer->dst.net.control_isset) ||
706 (uri->stype == LTTNG_STREAM_DATA &&
707 consumer->dst.net.data_isset)) {
df75acac
DG
708 ret = LTTNG_ERR_URL_EXIST;
709 goto error;
710 }
711 } else {
712 memset(&consumer->dst.net, 0, sizeof(consumer->dst.net));
785d2d0d
DG
713 }
714
df75acac
DG
715 consumer->type = CONSUMER_DST_NET;
716
2f77fc4b
DG
717 /* Set URI into consumer output object */
718 ret = consumer_set_network_uri(consumer, uri);
719 if (ret < 0) {
a74934ba 720 ret = -ret;
2f77fc4b
DG
721 goto error;
722 } else if (ret == 1) {
723 /*
724 * URI was the same in the consumer so we do not append the subdir
725 * again so to not duplicate output dir.
726 */
f86c9f4e 727 ret = LTTNG_OK;
2f77fc4b
DG
728 goto error;
729 }
730
731 if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) {
732 ret = consumer_set_subdir(consumer, session_name);
733 if (ret < 0) {
f73fabfd 734 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
735 goto error;
736 }
737 }
738
739 if (uri->stype == LTTNG_STREAM_CONTROL) {
740 /* On a new subdir, reappend the default trace dir. */
c30ce0b3
CB
741 strncat(consumer->subdir, default_trace_dir,
742 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2f77fc4b
DG
743 DBG3("Append domain trace name to subdir %s", consumer->subdir);
744 }
745
746 break;
747 case LTTNG_DST_PATH:
748 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
749 memset(consumer->dst.trace_path, 0,
750 sizeof(consumer->dst.trace_path));
751 strncpy(consumer->dst.trace_path, uri->dst.path,
752 sizeof(consumer->dst.trace_path));
753 /* Append default trace dir */
754 strncat(consumer->dst.trace_path, default_trace_dir,
c30ce0b3
CB
755 sizeof(consumer->dst.trace_path) -
756 strlen(consumer->dst.trace_path) - 1);
2f77fc4b
DG
757 /* Flag consumer as local. */
758 consumer->type = CONSUMER_DST_LOCAL;
759 break;
760 }
761
a74934ba
DG
762 ret = LTTNG_OK;
763
2f77fc4b
DG
764error:
765 return ret;
766}
767
768/*
769 * Init tracing by creating trace directory and sending fds kernel consumer.
770 */
771static int init_kernel_tracing(struct ltt_kernel_session *session)
772{
773 int ret = 0;
774 struct lttng_ht_iter iter;
775 struct consumer_socket *socket;
776
777 assert(session);
778
e7fe706f
DG
779 rcu_read_lock();
780
2f77fc4b
DG
781 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
782 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
783 socket, node.node) {
2f77fc4b 784 pthread_mutex_lock(socket->lock);
f50f23d9 785 ret = kernel_consumer_send_session(socket, session);
2f77fc4b
DG
786 pthread_mutex_unlock(socket->lock);
787 if (ret < 0) {
f73fabfd 788 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
789 goto error;
790 }
791 }
792 }
793
794error:
e7fe706f 795 rcu_read_unlock();
2f77fc4b
DG
796 return ret;
797}
798
799/*
800 * Create a socket to the relayd using the URI.
801 *
802 * On success, the relayd_sock pointer is set to the created socket.
803 * Else, it's stays untouched and a lttcomm error code is returned.
804 */
6151a90f 805static int create_connect_relayd(struct lttng_uri *uri,
b31610f2
JD
806 struct lttcomm_relayd_sock **relayd_sock,
807 struct consumer_output *consumer)
2f77fc4b
DG
808{
809 int ret;
6151a90f 810 struct lttcomm_relayd_sock *rsock;
2f77fc4b 811
6151a90f
JD
812 rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR,
813 RELAYD_VERSION_COMM_MINOR);
814 if (!rsock) {
f73fabfd 815 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
816 goto error;
817 }
818
ffe60014
DG
819 /*
820 * Connect to relayd so we can proceed with a session creation. This call
821 * can possibly block for an arbitrary amount of time to set the health
822 * state to be in poll execution.
823 */
824 health_poll_entry();
6151a90f 825 ret = relayd_connect(rsock);
ffe60014 826 health_poll_exit();
2f77fc4b
DG
827 if (ret < 0) {
828 ERR("Unable to reach lttng-relayd");
f73fabfd 829 ret = LTTNG_ERR_RELAYD_CONNECT_FAIL;
2f77fc4b
DG
830 goto free_sock;
831 }
832
833 /* Create socket for control stream. */
834 if (uri->stype == LTTNG_STREAM_CONTROL) {
835 DBG3("Creating relayd stream socket from URI");
836
837 /* Check relayd version */
6151a90f 838 ret = relayd_version_check(rsock);
2f77fc4b 839 if (ret < 0) {
f73fabfd 840 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
2f77fc4b
DG
841 goto close_sock;
842 }
b31610f2
JD
843 consumer->relay_major_version = rsock->major;
844 consumer->relay_minor_version = rsock->minor;
2f77fc4b
DG
845 } else if (uri->stype == LTTNG_STREAM_DATA) {
846 DBG3("Creating relayd data socket from URI");
847 } else {
848 /* Command is not valid */
849 ERR("Relayd invalid stream type: %d", uri->stype);
f73fabfd 850 ret = LTTNG_ERR_INVALID;
2f77fc4b
DG
851 goto close_sock;
852 }
853
6151a90f 854 *relayd_sock = rsock;
2f77fc4b 855
f73fabfd 856 return LTTNG_OK;
2f77fc4b
DG
857
858close_sock:
6151a90f
JD
859 /* The returned value is not useful since we are on an error path. */
860 (void) relayd_close(rsock);
2f77fc4b 861free_sock:
6151a90f 862 free(rsock);
2f77fc4b
DG
863error:
864 return ret;
865}
866
867/*
868 * Connect to the relayd using URI and send the socket to the right consumer.
869 */
56a37563
JG
870static int send_consumer_relayd_socket(enum lttng_domain_type domain,
871 unsigned int session_id, struct lttng_uri *relayd_uri,
872 struct consumer_output *consumer,
d3e2ba59
JD
873 struct consumer_socket *consumer_sock,
874 char *session_name, char *hostname, int session_live_timer)
2f77fc4b
DG
875{
876 int ret;
6151a90f 877 struct lttcomm_relayd_sock *rsock = NULL;
2f77fc4b 878
ffe60014 879 /* Connect to relayd and make version check if uri is the control. */
b31610f2 880 ret = create_connect_relayd(relayd_uri, &rsock, consumer);
ffe60014 881 if (ret != LTTNG_OK) {
6151a90f 882 goto error;
ffe60014 883 }
6151a90f 884 assert(rsock);
ffe60014 885
2f77fc4b 886 /* Set the network sequence index if not set. */
d88aee68
DG
887 if (consumer->net_seq_index == (uint64_t) -1ULL) {
888 pthread_mutex_lock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
889 /*
890 * Increment net_seq_idx because we are about to transfer the
891 * new relayd socket to the consumer.
d88aee68 892 * Assign unique key so the consumer can match streams.
2f77fc4b 893 */
d88aee68
DG
894 consumer->net_seq_index = ++relayd_net_seq_idx;
895 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
896 }
897
2f77fc4b 898 /* Send relayd socket to consumer. */
6151a90f 899 ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
d3e2ba59
JD
900 relayd_uri->stype, session_id,
901 session_name, hostname, session_live_timer);
2f77fc4b 902 if (ret < 0) {
f73fabfd 903 ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
2f77fc4b
DG
904 goto close_sock;
905 }
906
c890b720
DG
907 /* Flag that the corresponding socket was sent. */
908 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
ffe60014 909 consumer_sock->control_sock_sent = 1;
c890b720 910 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
ffe60014 911 consumer_sock->data_sock_sent = 1;
c890b720
DG
912 }
913
f73fabfd 914 ret = LTTNG_OK;
2f77fc4b
DG
915
916 /*
917 * Close socket which was dup on the consumer side. The session daemon does
918 * NOT keep track of the relayd socket(s) once transfer to the consumer.
919 */
920
921close_sock:
6151a90f
JD
922 (void) relayd_close(rsock);
923 free(rsock);
2f77fc4b 924
6151a90f 925error:
ffe60014
DG
926 if (ret != LTTNG_OK) {
927 /*
d9078d0c
DG
928 * The consumer output for this session should not be used anymore
929 * since the relayd connection failed thus making any tracing or/and
930 * streaming not usable.
ffe60014 931 */
d9078d0c 932 consumer->enabled = 0;
ffe60014 933 }
2f77fc4b
DG
934 return ret;
935}
936
937/*
938 * Send both relayd sockets to a specific consumer and domain. This is a
939 * helper function to facilitate sending the information to the consumer for a
940 * session.
941 */
56a37563
JG
942static int send_consumer_relayd_sockets(enum lttng_domain_type domain,
943 unsigned int session_id, struct consumer_output *consumer,
944 struct consumer_socket *sock, char *session_name,
945 char *hostname, int session_live_timer)
2f77fc4b 946{
dda67f6c 947 int ret = LTTNG_OK;
2f77fc4b 948
2f77fc4b 949 assert(consumer);
6dc3064a 950 assert(sock);
2f77fc4b 951
2f77fc4b 952 /* Sending control relayd socket. */
ffe60014 953 if (!sock->control_sock_sent) {
6dc3064a 954 ret = send_consumer_relayd_socket(domain, session_id,
d3e2ba59
JD
955 &consumer->dst.net.control, consumer, sock,
956 session_name, hostname, session_live_timer);
c890b720
DG
957 if (ret != LTTNG_OK) {
958 goto error;
959 }
2f77fc4b
DG
960 }
961
962 /* Sending data relayd socket. */
ffe60014 963 if (!sock->data_sock_sent) {
6dc3064a 964 ret = send_consumer_relayd_socket(domain, session_id,
d3e2ba59
JD
965 &consumer->dst.net.data, consumer, sock,
966 session_name, hostname, session_live_timer);
c890b720
DG
967 if (ret != LTTNG_OK) {
968 goto error;
969 }
2f77fc4b
DG
970 }
971
2f77fc4b
DG
972error:
973 return ret;
974}
975
976/*
977 * Setup relayd connections for a tracing session. First creates the socket to
978 * the relayd and send them to the right domain consumer. Consumer type MUST be
979 * network.
980 */
ffe60014 981int cmd_setup_relayd(struct ltt_session *session)
2f77fc4b 982{
f73fabfd 983 int ret = LTTNG_OK;
2f77fc4b
DG
984 struct ltt_ust_session *usess;
985 struct ltt_kernel_session *ksess;
986 struct consumer_socket *socket;
987 struct lttng_ht_iter iter;
988
989 assert(session);
990
991 usess = session->ust_session;
992 ksess = session->kernel_session;
993
785d2d0d 994 DBG("Setting relayd for session %s", session->name);
2f77fc4b 995
e7fe706f
DG
996 rcu_read_lock();
997
2f77fc4b
DG
998 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
999 && usess->consumer->enabled) {
1000 /* For each consumer socket, send relayd sockets */
1001 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
1002 socket, node.node) {
2f77fc4b 1003 pthread_mutex_lock(socket->lock);
6dc3064a 1004 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
d3e2ba59
JD
1005 usess->consumer, socket,
1006 session->name, session->hostname,
1007 session->live_timer);
2f77fc4b 1008 pthread_mutex_unlock(socket->lock);
f73fabfd 1009 if (ret != LTTNG_OK) {
2f77fc4b
DG
1010 goto error;
1011 }
6dc3064a
DG
1012 /* Session is now ready for network streaming. */
1013 session->net_handle = 1;
2f77fc4b 1014 }
b31610f2
JD
1015 session->consumer->relay_major_version =
1016 usess->consumer->relay_major_version;
1017 session->consumer->relay_minor_version =
1018 usess->consumer->relay_minor_version;
2f77fc4b
DG
1019 }
1020
1021 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
1022 && ksess->consumer->enabled) {
1023 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1024 socket, node.node) {
2f77fc4b 1025 pthread_mutex_lock(socket->lock);
6dc3064a 1026 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
d3e2ba59
JD
1027 ksess->consumer, socket,
1028 session->name, session->hostname,
1029 session->live_timer);
2f77fc4b 1030 pthread_mutex_unlock(socket->lock);
f73fabfd 1031 if (ret != LTTNG_OK) {
2f77fc4b
DG
1032 goto error;
1033 }
6dc3064a
DG
1034 /* Session is now ready for network streaming. */
1035 session->net_handle = 1;
2f77fc4b 1036 }
b31610f2
JD
1037 session->consumer->relay_major_version =
1038 ksess->consumer->relay_major_version;
1039 session->consumer->relay_minor_version =
1040 ksess->consumer->relay_minor_version;
2f77fc4b
DG
1041 }
1042
1043error:
e7fe706f 1044 rcu_read_unlock();
2f77fc4b
DG
1045 return ret;
1046}
1047
9b6c7ec5
DG
1048/*
1049 * Start a kernel session by opening all necessary streams.
1050 */
1051static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe)
1052{
1053 int ret;
1054 struct ltt_kernel_channel *kchan;
1055
1056 /* Open kernel metadata */
07b86b52 1057 if (ksess->metadata == NULL && ksess->output_traces) {
9b6c7ec5
DG
1058 ret = kernel_open_metadata(ksess);
1059 if (ret < 0) {
1060 ret = LTTNG_ERR_KERN_META_FAIL;
1061 goto error;
1062 }
1063 }
1064
1065 /* Open kernel metadata stream */
07b86b52 1066 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
9b6c7ec5
DG
1067 ret = kernel_open_metadata_stream(ksess);
1068 if (ret < 0) {
1069 ERR("Kernel create metadata stream failed");
1070 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1071 goto error;
1072 }
1073 }
1074
1075 /* For each channel */
1076 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1077 if (kchan->stream_count == 0) {
1078 ret = kernel_open_channel_stream(kchan);
1079 if (ret < 0) {
1080 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1081 goto error;
1082 }
1083 /* Update the stream global counter */
1084 ksess->stream_count_global += ret;
1085 }
1086 }
1087
1088 /* Setup kernel consumer socket and send fds to it */
1089 ret = init_kernel_tracing(ksess);
e43c41c5 1090 if (ret != 0) {
9b6c7ec5
DG
1091 ret = LTTNG_ERR_KERN_START_FAIL;
1092 goto error;
1093 }
1094
1095 /* This start the kernel tracing */
1096 ret = kernel_start_session(ksess);
1097 if (ret < 0) {
1098 ret = LTTNG_ERR_KERN_START_FAIL;
1099 goto error;
1100 }
1101
1102 /* Quiescent wait after starting trace */
784303b0 1103 kernel_wait_quiescent(kernel_tracer_fd);
9b6c7ec5 1104
14fb1ebe 1105 ksess->active = 1;
9b6c7ec5
DG
1106
1107 ret = LTTNG_OK;
1108
1109error:
1110 return ret;
1111}
1112
2f77fc4b
DG
1113/*
1114 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1115 */
56a37563
JG
1116int cmd_disable_channel(struct ltt_session *session,
1117 enum lttng_domain_type domain, char *channel_name)
2f77fc4b
DG
1118{
1119 int ret;
1120 struct ltt_ust_session *usess;
1121
1122 usess = session->ust_session;
1123
2223c96f
DG
1124 rcu_read_lock();
1125
2f77fc4b
DG
1126 switch (domain) {
1127 case LTTNG_DOMAIN_KERNEL:
1128 {
1129 ret = channel_kernel_disable(session->kernel_session,
1130 channel_name);
f73fabfd 1131 if (ret != LTTNG_OK) {
2f77fc4b
DG
1132 goto error;
1133 }
1134
1135 kernel_wait_quiescent(kernel_tracer_fd);
1136 break;
1137 }
1138 case LTTNG_DOMAIN_UST:
1139 {
1140 struct ltt_ust_channel *uchan;
1141 struct lttng_ht *chan_ht;
1142
1143 chan_ht = usess->domain_global.channels;
1144
1145 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1146 if (uchan == NULL) {
f73fabfd 1147 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2f77fc4b
DG
1148 goto error;
1149 }
1150
7972aab2 1151 ret = channel_ust_disable(usess, uchan);
f73fabfd 1152 if (ret != LTTNG_OK) {
2f77fc4b
DG
1153 goto error;
1154 }
1155 break;
1156 }
2f77fc4b 1157 default:
f73fabfd 1158 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1159 goto error;
1160 }
1161
f73fabfd 1162 ret = LTTNG_OK;
2f77fc4b
DG
1163
1164error:
2223c96f 1165 rcu_read_unlock();
2f77fc4b
DG
1166 return ret;
1167}
1168
ccf10263
MD
1169/*
1170 * Command LTTNG_TRACK_PID processed by the client thread.
a9ad0c8f
MD
1171 *
1172 * Called with session lock held.
ccf10263 1173 */
56a37563
JG
1174int cmd_track_pid(struct ltt_session *session, enum lttng_domain_type domain,
1175 int pid)
ccf10263
MD
1176{
1177 int ret;
1178
1179 rcu_read_lock();
1180
1181 switch (domain) {
1182 case LTTNG_DOMAIN_KERNEL:
1183 {
1184 struct ltt_kernel_session *ksess;
1185
1186 ksess = session->kernel_session;
1187
1188 ret = kernel_track_pid(ksess, pid);
1189 if (ret != LTTNG_OK) {
1190 goto error;
1191 }
1192
1193 kernel_wait_quiescent(kernel_tracer_fd);
1194 break;
1195 }
ccf10263 1196 case LTTNG_DOMAIN_UST:
a9ad0c8f
MD
1197 {
1198 struct ltt_ust_session *usess;
1199
1200 usess = session->ust_session;
1201
1202 ret = trace_ust_track_pid(usess, pid);
1203 if (ret != LTTNG_OK) {
1204 goto error;
1205 }
1206 break;
1207 }
ccf10263
MD
1208 default:
1209 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1210 goto error;
1211 }
1212
1213 ret = LTTNG_OK;
1214
1215error:
1216 rcu_read_unlock();
1217 return ret;
1218}
1219
1220/*
1221 * Command LTTNG_UNTRACK_PID processed by the client thread.
a9ad0c8f
MD
1222 *
1223 * Called with session lock held.
ccf10263 1224 */
56a37563
JG
1225int cmd_untrack_pid(struct ltt_session *session, enum lttng_domain_type domain,
1226 int pid)
ccf10263
MD
1227{
1228 int ret;
1229
1230 rcu_read_lock();
1231
1232 switch (domain) {
1233 case LTTNG_DOMAIN_KERNEL:
1234 {
1235 struct ltt_kernel_session *ksess;
1236
1237 ksess = session->kernel_session;
1238
1239 ret = kernel_untrack_pid(ksess, pid);
1240 if (ret != LTTNG_OK) {
1241 goto error;
1242 }
1243
1244 kernel_wait_quiescent(kernel_tracer_fd);
1245 break;
1246 }
ccf10263 1247 case LTTNG_DOMAIN_UST:
a9ad0c8f
MD
1248 {
1249 struct ltt_ust_session *usess;
1250
1251 usess = session->ust_session;
1252
1253 ret = trace_ust_untrack_pid(usess, pid);
1254 if (ret != LTTNG_OK) {
1255 goto error;
1256 }
1257 break;
1258 }
ccf10263
MD
1259 default:
1260 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1261 goto error;
1262 }
1263
1264 ret = LTTNG_OK;
1265
1266error:
1267 rcu_read_unlock();
1268 return ret;
1269}
1270
2f77fc4b
DG
1271/*
1272 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1273 *
1274 * The wpipe arguments is used as a notifier for the kernel thread.
1275 */
1276int cmd_enable_channel(struct ltt_session *session,
7972aab2 1277 struct lttng_domain *domain, struct lttng_channel *attr, int wpipe)
2f77fc4b
DG
1278{
1279 int ret;
1280 struct ltt_ust_session *usess = session->ust_session;
1281 struct lttng_ht *chan_ht;
1f345e94 1282 size_t len;
2f77fc4b
DG
1283
1284 assert(session);
1285 assert(attr);
7972aab2 1286 assert(domain);
2f77fc4b 1287
f5436bfc 1288 len = lttng_strnlen(attr->name, sizeof(attr->name));
1f345e94
PP
1289
1290 /* Validate channel name */
1291 if (attr->name[0] == '.' ||
1292 memchr(attr->name, '/', len) != NULL) {
1293 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1294 goto end;
1295 }
1296
2f77fc4b
DG
1297 DBG("Enabling channel %s for session %s", attr->name, session->name);
1298
03b4fdcf
DG
1299 rcu_read_lock();
1300
ecc48a90
JD
1301 /*
1302 * Don't try to enable a channel if the session has been started at
1303 * some point in time before. The tracer does not allow it.
1304 */
8382cf6f 1305 if (session->has_been_started) {
ecc48a90
JD
1306 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1307 goto error;
1308 }
1309
1310 /*
1311 * If the session is a live session, remove the switch timer, the
1312 * live timer does the same thing but sends also synchronisation
1313 * beacons for inactive streams.
1314 */
1315 if (session->live_timer > 0) {
1316 attr->attr.live_timer_interval = session->live_timer;
1317 attr->attr.switch_timer_interval = 0;
1318 }
1319
33fbd469
DG
1320 /*
1321 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1322 * mode and with less than 2 subbuf so block it right away and send back an
1323 * invalid attribute error.
1324 */
1325 if (attr->attr.overwrite && attr->attr.num_subbuf < 2) {
1326 ret = LTTNG_ERR_INVALID;
1327 goto error;
1328 }
1329
7972aab2 1330 switch (domain->type) {
2f77fc4b
DG
1331 case LTTNG_DOMAIN_KERNEL:
1332 {
1333 struct ltt_kernel_channel *kchan;
1334
2f77fc4b
DG
1335 kchan = trace_kernel_get_channel_by_name(attr->name,
1336 session->kernel_session);
1337 if (kchan == NULL) {
1338 ret = channel_kernel_create(session->kernel_session, attr, wpipe);
85076754
MD
1339 if (attr->name[0] != '\0') {
1340 session->kernel_session->has_non_default_channel = 1;
1341 }
2f77fc4b
DG
1342 } else {
1343 ret = channel_kernel_enable(session->kernel_session, kchan);
1344 }
1345
f73fabfd 1346 if (ret != LTTNG_OK) {
2f77fc4b
DG
1347 goto error;
1348 }
1349
784303b0 1350 kernel_wait_quiescent(kernel_tracer_fd);
2f77fc4b
DG
1351 break;
1352 }
1353 case LTTNG_DOMAIN_UST:
9232818f
JG
1354 case LTTNG_DOMAIN_JUL:
1355 case LTTNG_DOMAIN_LOG4J:
1356 case LTTNG_DOMAIN_PYTHON:
2f77fc4b
DG
1357 {
1358 struct ltt_ust_channel *uchan;
1359
9232818f
JG
1360 /*
1361 * FIXME
1362 *
1363 * Current agent implementation limitations force us to allow
1364 * only one channel at once in "agent" subdomains. Each
1365 * subdomain has a default channel name which must be strictly
1366 * adhered to.
1367 */
1368 if (domain->type == LTTNG_DOMAIN_JUL) {
1369 if (strncmp(attr->name, DEFAULT_JUL_CHANNEL_NAME,
1370 LTTNG_SYMBOL_NAME_LEN)) {
1371 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1372 goto error;
1373 }
1374 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
1375 if (strncmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME,
1376 LTTNG_SYMBOL_NAME_LEN)) {
1377 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1378 goto error;
1379 }
1380 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
1381 if (strncmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME,
1382 LTTNG_SYMBOL_NAME_LEN)) {
1383 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1384 goto error;
1385 }
1386 }
1387
2f77fc4b
DG
1388 chan_ht = usess->domain_global.channels;
1389
1390 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
1391 if (uchan == NULL) {
7972aab2 1392 ret = channel_ust_create(usess, attr, domain->buf_type);
85076754
MD
1393 if (attr->name[0] != '\0') {
1394 usess->has_non_default_channel = 1;
1395 }
2f77fc4b 1396 } else {
7972aab2 1397 ret = channel_ust_enable(usess, uchan);
2f77fc4b
DG
1398 }
1399 break;
1400 }
2f77fc4b 1401 default:
f73fabfd 1402 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1403 goto error;
1404 }
1405
1406error:
2223c96f 1407 rcu_read_unlock();
1f345e94 1408end:
2f77fc4b
DG
1409 return ret;
1410}
1411
2f77fc4b
DG
1412/*
1413 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1414 */
56a37563
JG
1415int cmd_disable_event(struct ltt_session *session,
1416 enum lttng_domain_type domain, char *channel_name,
6e911cad 1417 struct lttng_event *event)
2f77fc4b
DG
1418{
1419 int ret;
6e911cad
MD
1420 char *event_name;
1421
18a720cd
MD
1422 DBG("Disable event command for event \'%s\'", event->name);
1423
6e911cad 1424 event_name = event->name;
7076b56e
JG
1425 if (validate_event_name(event_name)) {
1426 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1427 goto error;
1428 }
6e911cad 1429
9b7431cf
JG
1430 /* Error out on unhandled search criteria */
1431 if (event->loglevel_type || event->loglevel != -1 || event->enabled
6e911cad 1432 || event->pid || event->filter || event->exclusion) {
7076b56e
JG
1433 ret = LTTNG_ERR_UNK;
1434 goto error;
6e911cad 1435 }
2f77fc4b 1436
2223c96f
DG
1437 rcu_read_lock();
1438
2f77fc4b
DG
1439 switch (domain) {
1440 case LTTNG_DOMAIN_KERNEL:
1441 {
1442 struct ltt_kernel_channel *kchan;
1443 struct ltt_kernel_session *ksess;
1444
1445 ksess = session->kernel_session;
1446
85076754
MD
1447 /*
1448 * If a non-default channel has been created in the
1449 * session, explicitely require that -c chan_name needs
1450 * to be provided.
1451 */
1452 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1453 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1454 goto error_unlock;
85076754
MD
1455 }
1456
2f77fc4b
DG
1457 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1458 if (kchan == NULL) {
f73fabfd 1459 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
7076b56e 1460 goto error_unlock;
2f77fc4b
DG
1461 }
1462
6e911cad
MD
1463 switch (event->type) {
1464 case LTTNG_EVENT_ALL:
9550ee81 1465 case LTTNG_EVENT_TRACEPOINT:
d0ae4ea8 1466 case LTTNG_EVENT_SYSCALL:
9550ee81
JR
1467 case LTTNG_EVENT_PROBE:
1468 case LTTNG_EVENT_FUNCTION:
1469 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1470 if (event_name[0] == '\0') {
1471 ret = event_kernel_disable_event(kchan,
1472 NULL, event->type);
29c62722 1473 } else {
d0ae4ea8 1474 ret = event_kernel_disable_event(kchan,
9550ee81 1475 event_name, event->type);
29c62722 1476 }
6e911cad 1477 if (ret != LTTNG_OK) {
7076b56e 1478 goto error_unlock;
6e911cad
MD
1479 }
1480 break;
6e911cad
MD
1481 default:
1482 ret = LTTNG_ERR_UNK;
7076b56e 1483 goto error_unlock;
2f77fc4b
DG
1484 }
1485
1486 kernel_wait_quiescent(kernel_tracer_fd);
1487 break;
1488 }
1489 case LTTNG_DOMAIN_UST:
1490 {
1491 struct ltt_ust_channel *uchan;
1492 struct ltt_ust_session *usess;
1493
1494 usess = session->ust_session;
1495
7076b56e
JG
1496 if (validate_ust_event_name(event_name)) {
1497 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1498 goto error_unlock;
1499 }
1500
85076754
MD
1501 /*
1502 * If a non-default channel has been created in the
9550ee81 1503 * session, explicitly require that -c chan_name needs
85076754
MD
1504 * to be provided.
1505 */
1506 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1507 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1508 goto error_unlock;
85076754
MD
1509 }
1510
2f77fc4b
DG
1511 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1512 channel_name);
1513 if (uchan == NULL) {
f73fabfd 1514 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
7076b56e 1515 goto error_unlock;
2f77fc4b
DG
1516 }
1517
6e911cad
MD
1518 switch (event->type) {
1519 case LTTNG_EVENT_ALL:
b3639870
JR
1520 /*
1521 * An empty event name means that everything
1522 * should be disabled.
1523 */
1524 if (event->name[0] == '\0') {
1525 ret = event_ust_disable_all_tracepoints(usess, uchan);
77d536b2
JR
1526 } else {
1527 ret = event_ust_disable_tracepoint(usess, uchan,
1528 event_name);
1529 }
6e911cad 1530 if (ret != LTTNG_OK) {
7076b56e 1531 goto error_unlock;
6e911cad
MD
1532 }
1533 break;
1534 default:
1535 ret = LTTNG_ERR_UNK;
7076b56e 1536 goto error_unlock;
2f77fc4b
DG
1537 }
1538
1539 DBG3("Disable UST event %s in channel %s completed", event_name,
1540 channel_name);
1541 break;
1542 }
5cdb6027 1543 case LTTNG_DOMAIN_LOG4J:
f20baf8e 1544 case LTTNG_DOMAIN_JUL:
0e115563 1545 case LTTNG_DOMAIN_PYTHON:
f20baf8e 1546 {
fefd409b 1547 struct agent *agt;
f20baf8e
DG
1548 struct ltt_ust_session *usess = session->ust_session;
1549
1550 assert(usess);
1551
6e911cad
MD
1552 switch (event->type) {
1553 case LTTNG_EVENT_ALL:
1554 break;
1555 default:
1556 ret = LTTNG_ERR_UNK;
7076b56e 1557 goto error_unlock;
6e911cad
MD
1558 }
1559
5cdb6027 1560 agt = trace_ust_find_agent(usess, domain);
fefd409b
DG
1561 if (!agt) {
1562 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
7076b56e 1563 goto error_unlock;
fefd409b 1564 }
b3639870
JR
1565 /*
1566 * An empty event name means that everything
1567 * should be disabled.
1568 */
1569 if (event->name[0] == '\0') {
18a720cd
MD
1570 ret = event_agent_disable_all(usess, agt);
1571 } else {
1572 ret = event_agent_disable(usess, agt, event_name);
1573 }
f20baf8e 1574 if (ret != LTTNG_OK) {
7076b56e 1575 goto error_unlock;
f20baf8e
DG
1576 }
1577
1578 break;
1579 }
2f77fc4b 1580 default:
f73fabfd 1581 ret = LTTNG_ERR_UND;
7076b56e 1582 goto error_unlock;
2f77fc4b
DG
1583 }
1584
f73fabfd 1585 ret = LTTNG_OK;
2f77fc4b 1586
7076b56e 1587error_unlock:
2223c96f 1588 rcu_read_unlock();
7076b56e 1589error:
2f77fc4b
DG
1590 return ret;
1591}
1592
2f77fc4b
DG
1593/*
1594 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1595 */
56a37563 1596int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
601d5acf 1597 char *channel_name, struct lttng_event_context *ctx, int kwpipe)
2f77fc4b 1598{
d5979e4a 1599 int ret, chan_kern_created = 0, chan_ust_created = 0;
bdf64013
JG
1600 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
1601
1602 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1603 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
1604 app_ctx_name = ctx->u.app_ctx.ctx_name;
1605 }
2f77fc4b
DG
1606
1607 switch (domain) {
1608 case LTTNG_DOMAIN_KERNEL:
979e618e
DG
1609 assert(session->kernel_session);
1610
1611 if (session->kernel_session->channel_count == 0) {
1612 /* Create default channel */
1613 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1614 if (ret != LTTNG_OK) {
1615 goto error;
1616 }
d5979e4a 1617 chan_kern_created = 1;
979e618e 1618 }
2f77fc4b 1619 /* Add kernel context to kernel tracer */
601d5acf 1620 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
f73fabfd 1621 if (ret != LTTNG_OK) {
2f77fc4b
DG
1622 goto error;
1623 }
1624 break;
bdf64013
JG
1625 case LTTNG_DOMAIN_JUL:
1626 case LTTNG_DOMAIN_LOG4J:
1627 {
1628 /*
1629 * Validate channel name.
1630 * If no channel name is given and the domain is JUL or LOG4J,
1631 * set it to the appropriate domain-specific channel name. If
1632 * a name is provided but does not match the expexted channel
1633 * name, return an error.
1634 */
1635 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1636 strcmp(channel_name,
1637 DEFAULT_JUL_CHANNEL_NAME)) {
1638 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1639 goto error;
1640 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1641 strcmp(channel_name,
1642 DEFAULT_LOG4J_CHANNEL_NAME)) {
1643 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1644 goto error;
1645 }
a93b3916 1646 /* break is _not_ missing here. */
bdf64013 1647 }
2f77fc4b
DG
1648 case LTTNG_DOMAIN_UST:
1649 {
1650 struct ltt_ust_session *usess = session->ust_session;
85076754
MD
1651 unsigned int chan_count;
1652
2f77fc4b
DG
1653 assert(usess);
1654
85076754 1655 chan_count = lttng_ht_get_count(usess->domain_global.channels);
979e618e
DG
1656 if (chan_count == 0) {
1657 struct lttng_channel *attr;
1658 /* Create default channel */
0a9c6494 1659 attr = channel_new_default_attr(domain, usess->buffer_type);
979e618e
DG
1660 if (attr == NULL) {
1661 ret = LTTNG_ERR_FATAL;
1662 goto error;
1663 }
1664
7972aab2 1665 ret = channel_ust_create(usess, attr, usess->buffer_type);
979e618e
DG
1666 if (ret != LTTNG_OK) {
1667 free(attr);
1668 goto error;
1669 }
1670 free(attr);
d5979e4a 1671 chan_ust_created = 1;
979e618e
DG
1672 }
1673
601d5acf 1674 ret = context_ust_add(usess, domain, ctx, channel_name);
bdf64013
JG
1675 free(app_ctx_provider_name);
1676 free(app_ctx_name);
1677 app_ctx_name = NULL;
1678 app_ctx_provider_name = NULL;
f73fabfd 1679 if (ret != LTTNG_OK) {
2f77fc4b
DG
1680 goto error;
1681 }
1682 break;
1683 }
2f77fc4b 1684 default:
f73fabfd 1685 ret = LTTNG_ERR_UND;
2f77fc4b
DG
1686 goto error;
1687 }
1688
bdf64013
JG
1689 ret = LTTNG_OK;
1690 goto end;
2f77fc4b
DG
1691
1692error:
d5979e4a
DG
1693 if (chan_kern_created) {
1694 struct ltt_kernel_channel *kchan =
1695 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
1696 session->kernel_session);
1697 /* Created previously, this should NOT fail. */
1698 assert(kchan);
1699 kernel_destroy_channel(kchan);
1700 }
1701
1702 if (chan_ust_created) {
1703 struct ltt_ust_channel *uchan =
1704 trace_ust_find_channel_by_name(
1705 session->ust_session->domain_global.channels,
1706 DEFAULT_CHANNEL_NAME);
1707 /* Created previously, this should NOT fail. */
1708 assert(uchan);
1709 /* Remove from the channel list of the session. */
1710 trace_ust_delete_channel(session->ust_session->domain_global.channels,
1711 uchan);
1712 trace_ust_destroy_channel(uchan);
1713 }
bdf64013
JG
1714end:
1715 free(app_ctx_provider_name);
1716 free(app_ctx_name);
2f77fc4b
DG
1717 return ret;
1718}
1719
930a2e99
JG
1720static int validate_event_name(const char *name)
1721{
1722 int ret = 0;
1723 const char *c = name;
1724 const char *event_name_end = c + LTTNG_SYMBOL_NAME_LEN;
bf9807db 1725 bool null_terminated = false;
930a2e99
JG
1726
1727 /*
1728 * Make sure that unescaped wildcards are only used as the last
1729 * character of the event name.
1730 */
1731 while (c < event_name_end) {
1732 switch (*c) {
1733 case '\0':
bf9807db 1734 null_terminated = true;
930a2e99
JG
1735 goto end;
1736 case '\\':
1737 c++;
1738 break;
1739 case '*':
1740 if ((c + 1) < event_name_end && *(c + 1)) {
1741 /* Wildcard is not the last character */
1742 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1743 goto end;
1744 }
1745 default:
1746 break;
1747 }
1748 c++;
1749 }
1750end:
bf9807db
JG
1751 if (!ret && !null_terminated) {
1752 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1753 }
930a2e99
JG
1754 return ret;
1755}
1756
dac8e046
JG
1757static inline bool name_starts_with(const char *name, const char *prefix)
1758{
1759 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
1760
1761 return !strncmp(name, prefix, max_cmp_len);
1762}
1763
1764/* Perform userspace-specific event name validation */
1765static int validate_ust_event_name(const char *name)
1766{
1767 int ret = 0;
1768
1769 if (!name) {
1770 ret = -1;
1771 goto end;
1772 }
1773
1774 /*
1775 * Check name against all internal UST event component namespaces used
1776 * by the agents.
1777 */
1778 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
1779 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
1780 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
1781 ret = -1;
1782 }
1783
1784end:
1785 return ret;
1786}
88f06f15 1787
2f77fc4b 1788/*
88f06f15
JG
1789 * Internal version of cmd_enable_event() with a supplemental
1790 * "internal_event" flag which is used to enable internal events which should
1791 * be hidden from clients. Such events are used in the agent implementation to
1792 * enable the events through which all "agent" events are funeled.
2f77fc4b 1793 */
88f06f15
JG
1794static int _cmd_enable_event(struct ltt_session *session,
1795 struct lttng_domain *domain,
025faf73 1796 char *channel_name, struct lttng_event *event,
6b453b5e 1797 char *filter_expression,
db8f1377
JI
1798 struct lttng_filter_bytecode *filter,
1799 struct lttng_event_exclusion *exclusion,
88f06f15 1800 int wpipe, bool internal_event)
2f77fc4b 1801{
e5f5db7f 1802 int ret, channel_created = 0;
2f77fc4b
DG
1803 struct lttng_channel *attr;
1804
1805 assert(session);
1806 assert(event);
1807 assert(channel_name);
1808
2a385866
JG
1809 /* If we have a filter, we must have its filter expression */
1810 assert(!(!!filter_expression ^ !!filter));
1811
18a720cd
MD
1812 DBG("Enable event command for event \'%s\'", event->name);
1813
f5ac4bd7
MD
1814 rcu_read_lock();
1815
930a2e99
JG
1816 ret = validate_event_name(event->name);
1817 if (ret) {
1818 goto error;
1819 }
1820
7972aab2 1821 switch (domain->type) {
2f77fc4b
DG
1822 case LTTNG_DOMAIN_KERNEL:
1823 {
1824 struct ltt_kernel_channel *kchan;
1825
85076754
MD
1826 /*
1827 * If a non-default channel has been created in the
1828 * session, explicitely require that -c chan_name needs
1829 * to be provided.
1830 */
1831 if (session->kernel_session->has_non_default_channel
1832 && channel_name[0] == '\0') {
1833 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1834 goto error;
1835 }
1836
2f77fc4b
DG
1837 kchan = trace_kernel_get_channel_by_name(channel_name,
1838 session->kernel_session);
1839 if (kchan == NULL) {
0a9c6494
DG
1840 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
1841 LTTNG_BUFFER_GLOBAL);
2f77fc4b 1842 if (attr == NULL) {
f73fabfd 1843 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1844 goto error;
1845 }
1846 strncpy(attr->name, channel_name, sizeof(attr->name));
1847
1848 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 1849 if (ret != LTTNG_OK) {
2f77fc4b
DG
1850 free(attr);
1851 goto error;
1852 }
1853 free(attr);
e5f5db7f
DG
1854
1855 channel_created = 1;
2f77fc4b
DG
1856 }
1857
1858 /* Get the newly created kernel channel pointer */
1859 kchan = trace_kernel_get_channel_by_name(channel_name,
1860 session->kernel_session);
1861 if (kchan == NULL) {
1862 /* This sould not happen... */
f73fabfd 1863 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1864 goto error;
1865 }
1866
6e911cad
MD
1867 switch (event->type) {
1868 case LTTNG_EVENT_ALL:
29c62722 1869 {
00a62084
MD
1870 char *filter_expression_a = NULL;
1871 struct lttng_filter_bytecode *filter_a = NULL;
1872
1873 /*
1874 * We need to duplicate filter_expression and filter,
1875 * because ownership is passed to first enable
1876 * event.
1877 */
1878 if (filter_expression) {
1879 filter_expression_a = strdup(filter_expression);
1880 if (!filter_expression_a) {
1881 ret = LTTNG_ERR_FATAL;
1882 goto error;
1883 }
1884 }
1885 if (filter) {
1886 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
1887 if (!filter_a) {
1888 free(filter_expression_a);
1889 ret = LTTNG_ERR_FATAL;
1890 goto error;
1891 }
1892 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
1893 }
29c62722 1894 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
00a62084
MD
1895 ret = event_kernel_enable_event(kchan, event,
1896 filter_expression, filter);
a969e101
MD
1897 /* We have passed ownership */
1898 filter_expression = NULL;
1899 filter = NULL;
29c62722
MD
1900 if (ret != LTTNG_OK) {
1901 if (channel_created) {
1902 /* Let's not leak a useless channel. */
1903 kernel_destroy_channel(kchan);
1904 }
00a62084
MD
1905 free(filter_expression_a);
1906 free(filter_a);
29c62722
MD
1907 goto error;
1908 }
1909 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
00a62084
MD
1910 ret = event_kernel_enable_event(kchan, event,
1911 filter_expression_a, filter_a);
60d21fa2
AB
1912 /* We have passed ownership */
1913 filter_expression_a = NULL;
1914 filter_a = NULL;
29c62722
MD
1915 if (ret != LTTNG_OK) {
1916 goto error;
1917 }
1918 break;
1919 }
6e6ef3d7
DG
1920 case LTTNG_EVENT_PROBE:
1921 case LTTNG_EVENT_FUNCTION:
1922 case LTTNG_EVENT_FUNCTION_ENTRY:
6e911cad 1923 case LTTNG_EVENT_TRACEPOINT:
00a62084
MD
1924 ret = event_kernel_enable_event(kchan, event,
1925 filter_expression, filter);
a969e101
MD
1926 /* We have passed ownership */
1927 filter_expression = NULL;
1928 filter = NULL;
6e911cad
MD
1929 if (ret != LTTNG_OK) {
1930 if (channel_created) {
1931 /* Let's not leak a useless channel. */
1932 kernel_destroy_channel(kchan);
1933 }
1934 goto error;
e5f5db7f 1935 }
6e911cad
MD
1936 break;
1937 case LTTNG_EVENT_SYSCALL:
00a62084
MD
1938 ret = event_kernel_enable_event(kchan, event,
1939 filter_expression, filter);
a969e101
MD
1940 /* We have passed ownership */
1941 filter_expression = NULL;
1942 filter = NULL;
e2b957af
MD
1943 if (ret != LTTNG_OK) {
1944 goto error;
1945 }
6e911cad
MD
1946 break;
1947 default:
1948 ret = LTTNG_ERR_UNK;
2f77fc4b
DG
1949 goto error;
1950 }
1951
1952 kernel_wait_quiescent(kernel_tracer_fd);
1953 break;
1954 }
1955 case LTTNG_DOMAIN_UST:
1956 {
1957 struct ltt_ust_channel *uchan;
1958 struct ltt_ust_session *usess = session->ust_session;
1959
1960 assert(usess);
1961
85076754
MD
1962 /*
1963 * If a non-default channel has been created in the
1964 * session, explicitely require that -c chan_name needs
1965 * to be provided.
1966 */
1967 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1968 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1969 goto error;
1970 }
1971
2f77fc4b
DG
1972 /* Get channel from global UST domain */
1973 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1974 channel_name);
1975 if (uchan == NULL) {
1976 /* Create default channel */
0a9c6494
DG
1977 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
1978 usess->buffer_type);
2f77fc4b 1979 if (attr == NULL) {
f73fabfd 1980 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
1981 goto error;
1982 }
1983 strncpy(attr->name, channel_name, sizeof(attr->name));
1984
1985 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 1986 if (ret != LTTNG_OK) {
2f77fc4b
DG
1987 free(attr);
1988 goto error;
1989 }
1990 free(attr);
1991
1992 /* Get the newly created channel reference back */
1993 uchan = trace_ust_find_channel_by_name(
1994 usess->domain_global.channels, channel_name);
1995 assert(uchan);
1996 }
1997
141feb8c
JG
1998 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
1999 /*
2000 * Don't allow users to add UST events to channels which
2001 * are assigned to a userspace subdomain (JUL, Log4J,
2002 * Python, etc.).
2003 */
2004 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2005 goto error;
2006 }
2007
dac8e046
JG
2008 if (!internal_event) {
2009 /*
2010 * Ensure the event name is not reserved for internal
2011 * use.
2012 */
2013 ret = validate_ust_event_name(event->name);
2014 if (ret) {
2015 WARN("Userspace event name %s failed validation.",
2016 event->name ?
2017 event->name : "NULL");
2018 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2019 goto error;
2020 }
2021 }
2022
2f77fc4b 2023 /* At this point, the session and channel exist on the tracer */
6b453b5e 2024 ret = event_ust_enable_tracepoint(usess, uchan, event,
88f06f15
JG
2025 filter_expression, filter, exclusion,
2026 internal_event);
49d21f93
MD
2027 /* We have passed ownership */
2028 filter_expression = NULL;
2029 filter = NULL;
2030 exclusion = NULL;
94382e15
JG
2031 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2032 goto already_enabled;
2033 } else if (ret != LTTNG_OK) {
2f77fc4b
DG
2034 goto error;
2035 }
2036 break;
2037 }
5cdb6027 2038 case LTTNG_DOMAIN_LOG4J:
f20baf8e 2039 case LTTNG_DOMAIN_JUL:
0e115563 2040 case LTTNG_DOMAIN_PYTHON:
f20baf8e 2041 {
da6c3a50 2042 const char *default_event_name, *default_chan_name;
fefd409b 2043 struct agent *agt;
f20baf8e
DG
2044 struct lttng_event uevent;
2045 struct lttng_domain tmp_dom;
2046 struct ltt_ust_session *usess = session->ust_session;
2047
2048 assert(usess);
2049
5cdb6027 2050 agt = trace_ust_find_agent(usess, domain->type);
fefd409b 2051 if (!agt) {
5cdb6027 2052 agt = agent_create(domain->type);
fefd409b 2053 if (!agt) {
e5b3c48c 2054 ret = LTTNG_ERR_NOMEM;
fefd409b
DG
2055 goto error;
2056 }
2057 agent_add(agt, usess->agents);
2058 }
2059
022d91ba 2060 /* Create the default tracepoint. */
996de3c7 2061 memset(&uevent, 0, sizeof(uevent));
f20baf8e
DG
2062 uevent.type = LTTNG_EVENT_TRACEPOINT;
2063 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
51755dc8
JG
2064 default_event_name = event_get_default_agent_ust_name(
2065 domain->type);
da6c3a50 2066 if (!default_event_name) {
e5b3c48c 2067 ret = LTTNG_ERR_FATAL;
da6c3a50 2068 goto error;
f43f95a9 2069 }
da6c3a50 2070 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
f20baf8e
DG
2071 uevent.name[sizeof(uevent.name) - 1] = '\0';
2072
2073 /*
2074 * The domain type is changed because we are about to enable the
2075 * default channel and event for the JUL domain that are hardcoded.
2076 * This happens in the UST domain.
2077 */
2078 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2079 tmp_dom.type = LTTNG_DOMAIN_UST;
2080
0e115563
DG
2081 switch (domain->type) {
2082 case LTTNG_DOMAIN_LOG4J:
da6c3a50 2083 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
0e115563
DG
2084 break;
2085 case LTTNG_DOMAIN_JUL:
da6c3a50 2086 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
0e115563
DG
2087 break;
2088 case LTTNG_DOMAIN_PYTHON:
2089 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2090 break;
2091 default:
e98a44b0 2092 /* The switch/case we are in makes this impossible */
0e115563 2093 assert(0);
da6c3a50
DG
2094 }
2095
971da06a 2096 {
8404118c 2097 char *filter_expression_copy = NULL;
51755dc8 2098 struct lttng_filter_bytecode *filter_copy = NULL;
971da06a
JG
2099
2100 if (filter) {
51755dc8
JG
2101 const size_t filter_size = sizeof(
2102 struct lttng_filter_bytecode)
2103 + filter->len;
2104
2105 filter_copy = zmalloc(filter_size);
971da06a 2106 if (!filter_copy) {
018096a4 2107 ret = LTTNG_ERR_NOMEM;
b742e3e2 2108 goto error;
971da06a 2109 }
51755dc8 2110 memcpy(filter_copy, filter, filter_size);
971da06a 2111
8404118c
JG
2112 filter_expression_copy =
2113 strdup(filter_expression);
2114 if (!filter_expression) {
2115 ret = LTTNG_ERR_NOMEM;
51755dc8
JG
2116 }
2117
2118 if (!filter_expression_copy || !filter_copy) {
2119 free(filter_expression_copy);
2120 free(filter_copy);
2121 goto error;
8404118c 2122 }
971da06a
JG
2123 }
2124
88f06f15 2125 ret = cmd_enable_event_internal(session, &tmp_dom,
971da06a 2126 (char *) default_chan_name,
8404118c
JG
2127 &uevent, filter_expression_copy,
2128 filter_copy, NULL, wpipe);
971da06a
JG
2129 }
2130
94382e15
JG
2131 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2132 goto already_enabled;
2133 } else if (ret != LTTNG_OK) {
f20baf8e
DG
2134 goto error;
2135 }
2136
2137 /* The wild card * means that everything should be enabled. */
2138 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
8404118c
JG
2139 ret = event_agent_enable_all(usess, agt, event, filter,
2140 filter_expression);
f20baf8e 2141 } else {
8404118c
JG
2142 ret = event_agent_enable(usess, agt, event, filter,
2143 filter_expression);
f20baf8e 2144 }
51755dc8
JG
2145 filter = NULL;
2146 filter_expression = NULL;
f20baf8e
DG
2147 if (ret != LTTNG_OK) {
2148 goto error;
2149 }
2150
2151 break;
2152 }
2f77fc4b 2153 default:
f73fabfd 2154 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2155 goto error;
2156 }
2157
f73fabfd 2158 ret = LTTNG_OK;
2f77fc4b 2159
94382e15 2160already_enabled:
2f77fc4b 2161error:
49d21f93
MD
2162 free(filter_expression);
2163 free(filter);
2164 free(exclusion);
2223c96f 2165 rcu_read_unlock();
2f77fc4b
DG
2166 return ret;
2167}
2168
88f06f15
JG
2169/*
2170 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2171 * We own filter, exclusion, and filter_expression.
2172 */
2173int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain,
2174 char *channel_name, struct lttng_event *event,
2175 char *filter_expression,
2176 struct lttng_filter_bytecode *filter,
2177 struct lttng_event_exclusion *exclusion,
2178 int wpipe)
2179{
2180 return _cmd_enable_event(session, domain, channel_name, event,
2181 filter_expression, filter, exclusion, wpipe, false);
2182}
2183
2184/*
2185 * Enable an event which is internal to LTTng. An internal should
2186 * never be made visible to clients and are immune to checks such as
2187 * reserved names.
2188 */
2189static int cmd_enable_event_internal(struct ltt_session *session,
2190 struct lttng_domain *domain,
2191 char *channel_name, struct lttng_event *event,
2192 char *filter_expression,
2193 struct lttng_filter_bytecode *filter,
2194 struct lttng_event_exclusion *exclusion,
2195 int wpipe)
2196{
2197 return _cmd_enable_event(session, domain, channel_name, event,
2198 filter_expression, filter, exclusion, wpipe, true);
2199}
2200
2f77fc4b
DG
2201/*
2202 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2203 */
56a37563
JG
2204ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2205 struct lttng_event **events)
2f77fc4b
DG
2206{
2207 int ret;
2208 ssize_t nb_events = 0;
2209
2210 switch (domain) {
2211 case LTTNG_DOMAIN_KERNEL:
2212 nb_events = kernel_list_events(kernel_tracer_fd, events);
2213 if (nb_events < 0) {
f73fabfd 2214 ret = LTTNG_ERR_KERN_LIST_FAIL;
2f77fc4b
DG
2215 goto error;
2216 }
2217 break;
2218 case LTTNG_DOMAIN_UST:
2219 nb_events = ust_app_list_events(events);
2220 if (nb_events < 0) {
f73fabfd 2221 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2222 goto error;
2223 }
2224 break;
5cdb6027 2225 case LTTNG_DOMAIN_LOG4J:
3c6a091f 2226 case LTTNG_DOMAIN_JUL:
0e115563 2227 case LTTNG_DOMAIN_PYTHON:
f60140a1 2228 nb_events = agent_list_events(events, domain);
3c6a091f
DG
2229 if (nb_events < 0) {
2230 ret = LTTNG_ERR_UST_LIST_FAIL;
2231 goto error;
2232 }
2233 break;
2f77fc4b 2234 default:
f73fabfd 2235 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2236 goto error;
2237 }
2238
2239 return nb_events;
2240
2241error:
2242 /* Return negative value to differentiate return code */
2243 return -ret;
2244}
2245
2246/*
2247 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2248 */
56a37563 2249ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2f77fc4b
DG
2250 struct lttng_event_field **fields)
2251{
2252 int ret;
2253 ssize_t nb_fields = 0;
2254
2255 switch (domain) {
2256 case LTTNG_DOMAIN_UST:
2257 nb_fields = ust_app_list_event_fields(fields);
2258 if (nb_fields < 0) {
f73fabfd 2259 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2260 goto error;
2261 }
2262 break;
2263 case LTTNG_DOMAIN_KERNEL:
2264 default: /* fall-through */
f73fabfd 2265 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2266 goto error;
2267 }
2268
2269 return nb_fields;
2270
2271error:
2272 /* Return negative value to differentiate return code */
2273 return -ret;
2274}
2275
834978fd
DG
2276ssize_t cmd_list_syscalls(struct lttng_event **events)
2277{
2278 return syscall_table_list(events);
2279}
2280
a5dfbb9d
MD
2281/*
2282 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2283 *
2284 * Called with session lock held.
2285 */
2286ssize_t cmd_list_tracker_pids(struct ltt_session *session,
56a37563 2287 enum lttng_domain_type domain, int32_t **pids)
a5dfbb9d
MD
2288{
2289 int ret;
2290 ssize_t nr_pids = 0;
2291
2292 switch (domain) {
2293 case LTTNG_DOMAIN_KERNEL:
2294 {
2295 struct ltt_kernel_session *ksess;
2296
2297 ksess = session->kernel_session;
2298 nr_pids = kernel_list_tracker_pids(ksess, pids);
2299 if (nr_pids < 0) {
2300 ret = LTTNG_ERR_KERN_LIST_FAIL;
2301 goto error;
2302 }
2303 break;
2304 }
2305 case LTTNG_DOMAIN_UST:
2306 {
2307 struct ltt_ust_session *usess;
2308
2309 usess = session->ust_session;
2310 nr_pids = trace_ust_list_tracker_pids(usess, pids);
2311 if (nr_pids < 0) {
2312 ret = LTTNG_ERR_UST_LIST_FAIL;
2313 goto error;
2314 }
2315 break;
2316 }
2317 case LTTNG_DOMAIN_LOG4J:
2318 case LTTNG_DOMAIN_JUL:
2319 case LTTNG_DOMAIN_PYTHON:
2320 default:
2321 ret = LTTNG_ERR_UND;
2322 goto error;
2323 }
2324
2325 return nr_pids;
2326
2327error:
2328 /* Return negative value to differentiate return code */
2329 return -ret;
2330}
2331
2f77fc4b
DG
2332/*
2333 * Command LTTNG_START_TRACE processed by the client thread.
a9ad0c8f
MD
2334 *
2335 * Called with session mutex held.
2f77fc4b
DG
2336 */
2337int cmd_start_trace(struct ltt_session *session)
2338{
2339 int ret;
cde3e505 2340 unsigned long nb_chan = 0;
2f77fc4b
DG
2341 struct ltt_kernel_session *ksession;
2342 struct ltt_ust_session *usess;
2f77fc4b
DG
2343
2344 assert(session);
2345
2346 /* Ease our life a bit ;) */
2347 ksession = session->kernel_session;
2348 usess = session->ust_session;
2349
8382cf6f
DG
2350 /* Is the session already started? */
2351 if (session->active) {
f73fabfd 2352 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2f77fc4b
DG
2353 goto error;
2354 }
2355
cde3e505
DG
2356 /*
2357 * Starting a session without channel is useless since after that it's not
2358 * possible to enable channel thus inform the client.
2359 */
2360 if (usess && usess->domain_global.channels) {
2361 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2362 }
2363 if (ksession) {
2364 nb_chan += ksession->channel_count;
2365 }
2366 if (!nb_chan) {
2367 ret = LTTNG_ERR_NO_CHANNEL;
2368 goto error;
2369 }
2370
2f77fc4b
DG
2371 /* Kernel tracing */
2372 if (ksession != NULL) {
9b6c7ec5
DG
2373 ret = start_kernel_session(ksession, kernel_tracer_fd);
2374 if (ret != LTTNG_OK) {
2f77fc4b
DG
2375 goto error;
2376 }
2f77fc4b
DG
2377 }
2378
2379 /* Flag session that trace should start automatically */
2380 if (usess) {
14fb1ebe
DG
2381 /*
2382 * Even though the start trace might fail, flag this session active so
2383 * other application coming in are started by default.
2384 */
2385 usess->active = 1;
2f77fc4b
DG
2386
2387 ret = ust_app_start_trace_all(usess);
2388 if (ret < 0) {
f73fabfd 2389 ret = LTTNG_ERR_UST_START_FAIL;
2f77fc4b
DG
2390 goto error;
2391 }
2392 }
2393
8382cf6f
DG
2394 /* Flag this after a successful start. */
2395 session->has_been_started = 1;
2396 session->active = 1;
9b6c7ec5 2397
f73fabfd 2398 ret = LTTNG_OK;
2f77fc4b
DG
2399
2400error:
2401 return ret;
2402}
2403
2404/*
2405 * Command LTTNG_STOP_TRACE processed by the client thread.
2406 */
2407int cmd_stop_trace(struct ltt_session *session)
2408{
2409 int ret;
2410 struct ltt_kernel_channel *kchan;
2411 struct ltt_kernel_session *ksession;
2412 struct ltt_ust_session *usess;
2413
2414 assert(session);
2415
2416 /* Short cut */
2417 ksession = session->kernel_session;
2418 usess = session->ust_session;
2419
8382cf6f
DG
2420 /* Session is not active. Skip everythong and inform the client. */
2421 if (!session->active) {
f73fabfd 2422 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2f77fc4b
DG
2423 goto error;
2424 }
2425
2f77fc4b 2426 /* Kernel tracer */
14fb1ebe 2427 if (ksession && ksession->active) {
2f77fc4b
DG
2428 DBG("Stop kernel tracing");
2429
2430 /* Flush metadata if exist */
2431 if (ksession->metadata_stream_fd >= 0) {
2432 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
2433 if (ret < 0) {
2434 ERR("Kernel metadata flush failed");
2435 }
2436 }
2437
2438 /* Flush all buffers before stopping */
2439 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
2440 ret = kernel_flush_buffer(kchan);
2441 if (ret < 0) {
2442 ERR("Kernel flush buffer error");
2443 }
2444 }
2445
2446 ret = kernel_stop_session(ksession);
2447 if (ret < 0) {
f73fabfd 2448 ret = LTTNG_ERR_KERN_STOP_FAIL;
2f77fc4b
DG
2449 goto error;
2450 }
2451
2452 kernel_wait_quiescent(kernel_tracer_fd);
9b6c7ec5 2453
14fb1ebe 2454 ksession->active = 0;
2f77fc4b
DG
2455 }
2456
14fb1ebe
DG
2457 if (usess && usess->active) {
2458 /*
2459 * Even though the stop trace might fail, flag this session inactive so
2460 * other application coming in are not started by default.
2461 */
2462 usess->active = 0;
2f77fc4b
DG
2463
2464 ret = ust_app_stop_trace_all(usess);
2465 if (ret < 0) {
f73fabfd 2466 ret = LTTNG_ERR_UST_STOP_FAIL;
2f77fc4b
DG
2467 goto error;
2468 }
2469 }
2470
8382cf6f
DG
2471 /* Flag inactive after a successful stop. */
2472 session->active = 0;
f73fabfd 2473 ret = LTTNG_OK;
2f77fc4b
DG
2474
2475error:
2476 return ret;
2477}
2478
2479/*
2480 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2481 */
bda32d56
JG
2482int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2483 struct lttng_uri *uris)
2f77fc4b
DG
2484{
2485 int ret, i;
2486 struct ltt_kernel_session *ksess = session->kernel_session;
2487 struct ltt_ust_session *usess = session->ust_session;
2f77fc4b
DG
2488
2489 assert(session);
2490 assert(uris);
2491 assert(nb_uri > 0);
2492
8382cf6f
DG
2493 /* Can't set consumer URI if the session is active. */
2494 if (session->active) {
f73fabfd 2495 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2f77fc4b
DG
2496 goto error;
2497 }
2498
bda32d56 2499 /* Set the "global" consumer URIs */
2f77fc4b 2500 for (i = 0; i < nb_uri; i++) {
bda32d56
JG
2501 ret = add_uri_to_consumer(session->consumer,
2502 &uris[i], 0, session->name);
a74934ba 2503 if (ret != LTTNG_OK) {
2f77fc4b
DG
2504 goto error;
2505 }
2f77fc4b
DG
2506 }
2507
bda32d56
JG
2508 /* Set UST session URIs */
2509 if (session->ust_session) {
2510 for (i = 0; i < nb_uri; i++) {
2511 ret = add_uri_to_consumer(
2512 session->ust_session->consumer,
2513 &uris[i], LTTNG_DOMAIN_UST,
2514 session->name);
2515 if (ret != LTTNG_OK) {
2516 goto error;
2517 }
2518 }
2519 }
2520
2521 /* Set kernel session URIs */
2522 if (session->kernel_session) {
2523 for (i = 0; i < nb_uri; i++) {
2524 ret = add_uri_to_consumer(
2525 session->kernel_session->consumer,
2526 &uris[i], LTTNG_DOMAIN_KERNEL,
2527 session->name);
2528 if (ret != LTTNG_OK) {
2529 goto error;
2530 }
2531 }
2532 }
2533
7ab70fe0
DG
2534 /*
2535 * Make sure to set the session in output mode after we set URI since a
2536 * session can be created without URL (thus flagged in no output mode).
2537 */
2538 session->output_traces = 1;
2539 if (ksess) {
2540 ksess->output_traces = 1;
bda32d56
JG
2541 }
2542
2543 if (usess) {
7ab70fe0
DG
2544 usess->output_traces = 1;
2545 }
2546
2f77fc4b 2547 /* All good! */
f73fabfd 2548 ret = LTTNG_OK;
2f77fc4b
DG
2549
2550error:
2551 return ret;
2552}
2553
2554/*
2555 * Command LTTNG_CREATE_SESSION processed by the client thread.
2556 */
2557int cmd_create_session_uri(char *name, struct lttng_uri *uris,
ecc48a90 2558 size_t nb_uri, lttng_sock_cred *creds, unsigned int live_timer)
2f77fc4b
DG
2559{
2560 int ret;
2f77fc4b
DG
2561 struct ltt_session *session;
2562
2563 assert(name);
2bba9e53 2564 assert(creds);
785d2d0d 2565
2f77fc4b
DG
2566 /*
2567 * Verify if the session already exist
2568 *
2569 * XXX: There is no need for the session lock list here since the caller
2570 * (process_client_msg) is holding it. We might want to change that so a
2571 * single command does not lock the entire session list.
2572 */
2573 session = session_find_by_name(name);
2574 if (session != NULL) {
f73fabfd 2575 ret = LTTNG_ERR_EXIST_SESS;
2f77fc4b
DG
2576 goto find_error;
2577 }
2578
2579 /* Create tracing session in the registry */
dec56f6c 2580 ret = session_create(name, LTTNG_SOCK_GET_UID_CRED(creds),
2f77fc4b 2581 LTTNG_SOCK_GET_GID_CRED(creds));
f73fabfd 2582 if (ret != LTTNG_OK) {
2f77fc4b
DG
2583 goto session_error;
2584 }
2585
2586 /*
2587 * Get the newly created session pointer back
2588 *
2589 * XXX: There is no need for the session lock list here since the caller
2590 * (process_client_msg) is holding it. We might want to change that so a
2591 * single command does not lock the entire session list.
2592 */
2593 session = session_find_by_name(name);
2594 assert(session);
2595
ecc48a90 2596 session->live_timer = live_timer;
2f77fc4b
DG
2597 /* Create default consumer output for the session not yet created. */
2598 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
2599 if (session->consumer == NULL) {
f73fabfd 2600 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2601 goto consumer_error;
2602 }
2603
2bba9e53 2604 if (uris) {
bda32d56 2605 ret = cmd_set_consumer_uri(session, nb_uri, uris);
2bba9e53
DG
2606 if (ret != LTTNG_OK) {
2607 goto consumer_error;
2608 }
2609 session->output_traces = 1;
2610 } else {
2611 session->output_traces = 0;
2612 DBG2("Session %s created with no output", session->name);
2f77fc4b
DG
2613 }
2614
2615 session->consumer->enabled = 1;
2616
f73fabfd 2617 return LTTNG_OK;
2f77fc4b
DG
2618
2619consumer_error:
2620 session_destroy(session);
2621session_error:
2622find_error:
2623 return ret;
2624}
2625
27babd3a
DG
2626/*
2627 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2628 */
2629int cmd_create_session_snapshot(char *name, struct lttng_uri *uris,
2630 size_t nb_uri, lttng_sock_cred *creds)
2631{
2632 int ret;
2633 struct ltt_session *session;
2634 struct snapshot_output *new_output = NULL;
2635
2636 assert(name);
2637 assert(creds);
2638
2639 /*
2640 * Create session in no output mode with URIs set to NULL. The uris we've
2641 * received are for a default snapshot output if one.
2642 */
ae58b817 2643 ret = cmd_create_session_uri(name, NULL, 0, creds, 0);
27babd3a
DG
2644 if (ret != LTTNG_OK) {
2645 goto error;
2646 }
2647
2648 /* Get the newly created session pointer back. This should NEVER fail. */
2649 session = session_find_by_name(name);
2650 assert(session);
2651
2652 /* Flag session for snapshot mode. */
2653 session->snapshot_mode = 1;
2654
2655 /* Skip snapshot output creation if no URI is given. */
2656 if (nb_uri == 0) {
2657 goto end;
2658 }
2659
2660 new_output = snapshot_output_alloc();
2661 if (!new_output) {
2662 ret = LTTNG_ERR_NOMEM;
2663 goto error_snapshot_alloc;
2664 }
2665
2666 ret = snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE, NULL,
2667 uris, nb_uri, session->consumer, new_output, &session->snapshot);
2668 if (ret < 0) {
2669 if (ret == -ENOMEM) {
2670 ret = LTTNG_ERR_NOMEM;
2671 } else {
2672 ret = LTTNG_ERR_INVALID;
2673 }
2674 goto error_snapshot;
2675 }
2676
2677 rcu_read_lock();
2678 snapshot_add_output(&session->snapshot, new_output);
2679 rcu_read_unlock();
2680
2681end:
2682 return LTTNG_OK;
2683
2684error_snapshot:
2685 snapshot_output_destroy(new_output);
2686error_snapshot_alloc:
2687 session_destroy(session);
2688error:
2689 return ret;
2690}
2691
2f77fc4b
DG
2692/*
2693 * Command LTTNG_DESTROY_SESSION processed by the client thread.
a9ad0c8f
MD
2694 *
2695 * Called with session lock held.
2f77fc4b
DG
2696 */
2697int cmd_destroy_session(struct ltt_session *session, int wpipe)
2698{
2699 int ret;
2700 struct ltt_ust_session *usess;
2701 struct ltt_kernel_session *ksess;
2702
2703 /* Safety net */
2704 assert(session);
2705
2706 usess = session->ust_session;
2707 ksess = session->kernel_session;
2708
2709 /* Clean kernel session teardown */
2710 kernel_destroy_session(ksess);
2711
2712 /* UST session teardown */
2713 if (usess) {
2714 /* Close any relayd session */
2715 consumer_output_send_destroy_relayd(usess->consumer);
2716
2717 /* Destroy every UST application related to this session. */
2718 ret = ust_app_destroy_trace_all(usess);
2719 if (ret) {
2720 ERR("Error in ust_app_destroy_trace_all");
2721 }
2722
2723 /* Clean up the rest. */
2724 trace_ust_destroy_session(usess);
2725 }
2726
2727 /*
2728 * Must notify the kernel thread here to update it's poll set in order to
2729 * remove the channel(s)' fd just destroyed.
2730 */
2731 ret = notify_thread_pipe(wpipe);
2732 if (ret < 0) {
2733 PERROR("write kernel poll pipe");
2734 }
2735
2736 ret = session_destroy(session);
2737
2738 return ret;
2739}
2740
2741/*
2742 * Command LTTNG_CALIBRATE processed by the client thread.
2743 */
56a37563
JG
2744int cmd_calibrate(enum lttng_domain_type domain,
2745 struct lttng_calibrate *calibrate)
2f77fc4b
DG
2746{
2747 int ret;
2748
2749 switch (domain) {
2750 case LTTNG_DOMAIN_KERNEL:
2751 {
2752 struct lttng_kernel_calibrate kcalibrate;
2753
2e84128e
DG
2754 switch (calibrate->type) {
2755 case LTTNG_CALIBRATE_FUNCTION:
2756 default:
2757 /* Default and only possible calibrate option. */
2758 kcalibrate.type = LTTNG_KERNEL_CALIBRATE_KRETPROBE;
2759 break;
2760 }
2761
2f77fc4b
DG
2762 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2763 if (ret < 0) {
f73fabfd 2764 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2f77fc4b
DG
2765 goto error;
2766 }
2767 break;
2768 }
2769 case LTTNG_DOMAIN_UST:
2770 {
2771 struct lttng_ust_calibrate ucalibrate;
2772
2e84128e
DG
2773 switch (calibrate->type) {
2774 case LTTNG_CALIBRATE_FUNCTION:
2775 default:
2776 /* Default and only possible calibrate option. */
2777 ucalibrate.type = LTTNG_UST_CALIBRATE_TRACEPOINT;
2778 break;
2779 }
2780
2f77fc4b
DG
2781 ret = ust_app_calibrate_glb(&ucalibrate);
2782 if (ret < 0) {
f73fabfd 2783 ret = LTTNG_ERR_UST_CALIBRATE_FAIL;
2f77fc4b
DG
2784 goto error;
2785 }
2786 break;
2787 }
2788 default:
f73fabfd 2789 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2790 goto error;
2791 }
2792
f73fabfd 2793 ret = LTTNG_OK;
2f77fc4b
DG
2794
2795error:
2796 return ret;
2797}
2798
2799/*
2800 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2801 */
56a37563
JG
2802int cmd_register_consumer(struct ltt_session *session,
2803 enum lttng_domain_type domain, const char *sock_path,
2804 struct consumer_data *cdata)
2f77fc4b
DG
2805{
2806 int ret, sock;
dd81b457 2807 struct consumer_socket *socket = NULL;
2f77fc4b
DG
2808
2809 assert(session);
2810 assert(cdata);
2811 assert(sock_path);
2812
2813 switch (domain) {
2814 case LTTNG_DOMAIN_KERNEL:
2815 {
2816 struct ltt_kernel_session *ksess = session->kernel_session;
2817
2818 assert(ksess);
2819
2820 /* Can't register a consumer if there is already one */
2821 if (ksess->consumer_fds_sent != 0) {
f73fabfd 2822 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
2823 goto error;
2824 }
2825
2826 sock = lttcomm_connect_unix_sock(sock_path);
2827 if (sock < 0) {
f73fabfd 2828 ret = LTTNG_ERR_CONNECT_FAIL;
2f77fc4b
DG
2829 goto error;
2830 }
4ce514c4 2831 cdata->cmd_sock = sock;
2f77fc4b 2832
4ce514c4 2833 socket = consumer_allocate_socket(&cdata->cmd_sock);
2f77fc4b 2834 if (socket == NULL) {
f66c074c
DG
2835 ret = close(sock);
2836 if (ret < 0) {
2837 PERROR("close register consumer");
2838 }
4ce514c4 2839 cdata->cmd_sock = -1;
f73fabfd 2840 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2841 goto error;
2842 }
2843
2844 socket->lock = zmalloc(sizeof(pthread_mutex_t));
2845 if (socket->lock == NULL) {
2846 PERROR("zmalloc pthread mutex");
f73fabfd 2847 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2848 goto error;
2849 }
2850 pthread_mutex_init(socket->lock, NULL);
2851 socket->registered = 1;
2852
2853 rcu_read_lock();
2854 consumer_add_socket(socket, ksess->consumer);
2855 rcu_read_unlock();
2856
2857 pthread_mutex_lock(&cdata->pid_mutex);
2858 cdata->pid = -1;
2859 pthread_mutex_unlock(&cdata->pid_mutex);
2860
2861 break;
2862 }
2863 default:
2864 /* TODO: Userspace tracing */
f73fabfd 2865 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2866 goto error;
2867 }
2868
dd81b457 2869 return LTTNG_OK;
2f77fc4b
DG
2870
2871error:
dd81b457
DG
2872 if (socket) {
2873 consumer_destroy_socket(socket);
2874 }
2f77fc4b
DG
2875 return ret;
2876}
2877
2878/*
2879 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2880 */
2881ssize_t cmd_list_domains(struct ltt_session *session,
2882 struct lttng_domain **domains)
2883{
2884 int ret, index = 0;
2885 ssize_t nb_dom = 0;
fefd409b
DG
2886 struct agent *agt;
2887 struct lttng_ht_iter iter;
2f77fc4b
DG
2888
2889 if (session->kernel_session != NULL) {
2890 DBG3("Listing domains found kernel domain");
2891 nb_dom++;
2892 }
2893
2894 if (session->ust_session != NULL) {
2895 DBG3("Listing domains found UST global domain");
2896 nb_dom++;
3c6a091f 2897
e0a74f01 2898 rcu_read_lock();
fefd409b
DG
2899 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2900 agt, node.node) {
2901 if (agt->being_used) {
2902 nb_dom++;
2903 }
3c6a091f 2904 }
e0a74f01 2905 rcu_read_unlock();
2f77fc4b
DG
2906 }
2907
fa64dfb4
JG
2908 if (!nb_dom) {
2909 goto end;
2910 }
2911
2f77fc4b
DG
2912 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
2913 if (*domains == NULL) {
f73fabfd 2914 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2915 goto error;
2916 }
2917
2918 if (session->kernel_session != NULL) {
2919 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
b5edb9e8
PP
2920
2921 /* Kernel session buffer type is always GLOBAL */
2922 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
2923
2f77fc4b
DG
2924 index++;
2925 }
2926
2927 if (session->ust_session != NULL) {
2928 (*domains)[index].type = LTTNG_DOMAIN_UST;
88c5f0d8 2929 (*domains)[index].buf_type = session->ust_session->buffer_type;
2f77fc4b 2930 index++;
3c6a091f 2931
e0a74f01 2932 rcu_read_lock();
fefd409b
DG
2933 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
2934 agt, node.node) {
2935 if (agt->being_used) {
2936 (*domains)[index].type = agt->domain;
2937 (*domains)[index].buf_type = session->ust_session->buffer_type;
2938 index++;
2939 }
3c6a091f 2940 }
e0a74f01 2941 rcu_read_unlock();
2f77fc4b 2942 }
fa64dfb4 2943end:
2f77fc4b
DG
2944 return nb_dom;
2945
2946error:
f73fabfd
DG
2947 /* Return negative value to differentiate return code */
2948 return -ret;
2f77fc4b
DG
2949}
2950
2951
2952/*
2953 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2954 */
56a37563
JG
2955ssize_t cmd_list_channels(enum lttng_domain_type domain,
2956 struct ltt_session *session, struct lttng_channel **channels)
2f77fc4b 2957{
53e367f9 2958 ssize_t nb_chan = 0, payload_size = 0, ret;
2f77fc4b
DG
2959
2960 switch (domain) {
2961 case LTTNG_DOMAIN_KERNEL:
2962 if (session->kernel_session != NULL) {
2963 nb_chan = session->kernel_session->channel_count;
2964 }
2965 DBG3("Number of kernel channels %zd", nb_chan);
c7d620a2 2966 if (nb_chan <= 0) {
53e367f9
JG
2967 ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2968 goto end;
c7d620a2 2969 }
2f77fc4b
DG
2970 break;
2971 case LTTNG_DOMAIN_UST:
2972 if (session->ust_session != NULL) {
7ffc31a2 2973 rcu_read_lock();
2f77fc4b 2974 nb_chan = lttng_ht_get_count(
7ffc31a2
JG
2975 session->ust_session->domain_global.channels);
2976 rcu_read_unlock();
2f77fc4b
DG
2977 }
2978 DBG3("Number of UST global channels %zd", nb_chan);
ade7ce52 2979 if (nb_chan < 0) {
53e367f9
JG
2980 ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND;
2981 goto end;
c7d620a2 2982 }
2f77fc4b
DG
2983 break;
2984 default:
53e367f9
JG
2985 ret = -LTTNG_ERR_UND;
2986 goto end;
2f77fc4b
DG
2987 }
2988
2989 if (nb_chan > 0) {
53e367f9
JG
2990 const size_t channel_size = sizeof(struct lttng_channel) +
2991 sizeof(struct lttcomm_channel_extended);
2992 struct lttcomm_channel_extended *channel_exts;
2993
2994 payload_size = nb_chan * channel_size;
2995 *channels = zmalloc(payload_size);
2f77fc4b 2996 if (*channels == NULL) {
53e367f9
JG
2997 ret = -LTTNG_ERR_FATAL;
2998 goto end;
2f77fc4b
DG
2999 }
3000
53e367f9
JG
3001 channel_exts = ((void *) *channels) +
3002 (nb_chan * sizeof(struct lttng_channel));
3003 list_lttng_channels(domain, session, *channels, channel_exts);
3004 } else {
3005 *channels = NULL;
2f77fc4b
DG
3006 }
3007
53e367f9
JG
3008 ret = payload_size;
3009end:
3010 return ret;
2f77fc4b
DG
3011}
3012
3013/*
3014 * Command LTTNG_LIST_EVENTS processed by the client thread.
3015 */
56a37563
JG
3016ssize_t cmd_list_events(enum lttng_domain_type domain,
3017 struct ltt_session *session, char *channel_name,
b4e3ceb9 3018 struct lttng_event **events, size_t *total_size)
2f77fc4b
DG
3019{
3020 int ret = 0;
3021 ssize_t nb_event = 0;
3022
3023 switch (domain) {
3024 case LTTNG_DOMAIN_KERNEL:
3025 if (session->kernel_session != NULL) {
3026 nb_event = list_lttng_kernel_events(channel_name,
b4e3ceb9
PP
3027 session->kernel_session, events,
3028 total_size);
2f77fc4b
DG
3029 }
3030 break;
3031 case LTTNG_DOMAIN_UST:
3032 {
3033 if (session->ust_session != NULL) {
3034 nb_event = list_lttng_ust_global_events(channel_name,
b4e3ceb9
PP
3035 &session->ust_session->domain_global, events,
3036 total_size);
2f77fc4b
DG
3037 }
3038 break;
3039 }
5cdb6027 3040 case LTTNG_DOMAIN_LOG4J:
3c6a091f 3041 case LTTNG_DOMAIN_JUL:
0e115563 3042 case LTTNG_DOMAIN_PYTHON:
3c6a091f 3043 if (session->ust_session) {
fefd409b
DG
3044 struct lttng_ht_iter iter;
3045 struct agent *agt;
3046
b11feea5 3047 rcu_read_lock();
fefd409b
DG
3048 cds_lfht_for_each_entry(session->ust_session->agents->ht,
3049 &iter.iter, agt, node.node) {
1dfd9906
JG
3050 if (agt->domain == domain) {
3051 nb_event = list_lttng_agent_events(
b4e3ceb9
PP
3052 agt, events,
3053 total_size);
1dfd9906
JG
3054 break;
3055 }
fefd409b 3056 }
b11feea5 3057 rcu_read_unlock();
3c6a091f
DG
3058 }
3059 break;
2f77fc4b 3060 default:
f73fabfd 3061 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3062 goto error;
3063 }
3064
f73fabfd 3065 return nb_event;
2f77fc4b
DG
3066
3067error:
f73fabfd
DG
3068 /* Return negative value to differentiate return code */
3069 return -ret;
2f77fc4b
DG
3070}
3071
3072/*
3073 * Using the session list, filled a lttng_session array to send back to the
3074 * client for session listing.
3075 *
3076 * The session list lock MUST be acquired before calling this function. Use
3077 * session_lock_list() and session_unlock_list().
3078 */
3079void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
3080 gid_t gid)
3081{
3082 int ret;
3083 unsigned int i = 0;
3084 struct ltt_session *session;
3085 struct ltt_session_list *list = session_get_list();
3086
3087 DBG("Getting all available session for UID %d GID %d",
3088 uid, gid);
3089 /*
3090 * Iterate over session list and append data after the control struct in
3091 * the buffer.
3092 */
3093 cds_list_for_each_entry(session, &list->head, list) {
3094 /*
3095 * Only list the sessions the user can control.
3096 */
3097 if (!session_access_ok(session, uid, gid)) {
3098 continue;
3099 }
3100
3101 struct ltt_kernel_session *ksess = session->kernel_session;
3102 struct ltt_ust_session *usess = session->ust_session;
3103
3104 if (session->consumer->type == CONSUMER_DST_NET ||
3105 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3106 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3107 ret = build_network_session_path(sessions[i].path,
dec56f6c 3108 sizeof(sessions[i].path), session);
2f77fc4b 3109 } else {
dec56f6c 3110 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
2f77fc4b
DG
3111 session->consumer->dst.trace_path);
3112 }
3113 if (ret < 0) {
3114 PERROR("snprintf session path");
3115 continue;
3116 }
3117
3118 strncpy(sessions[i].name, session->name, NAME_MAX);
3119 sessions[i].name[NAME_MAX - 1] = '\0';
8382cf6f 3120 sessions[i].enabled = session->active;
2cbf8fed 3121 sessions[i].snapshot_mode = session->snapshot_mode;
8960e9cd 3122 sessions[i].live_timer_interval = session->live_timer;
2f77fc4b
DG
3123 i++;
3124 }
3125}
3126
806e2684 3127/*
6d805429 3128 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
d3f14b8a 3129 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
806e2684 3130 */
6d805429 3131int cmd_data_pending(struct ltt_session *session)
806e2684
DG
3132{
3133 int ret;
3134 struct ltt_kernel_session *ksess = session->kernel_session;
3135 struct ltt_ust_session *usess = session->ust_session;
3136
3137 assert(session);
3138
3139 /* Session MUST be stopped to ask for data availability. */
8382cf6f 3140 if (session->active) {
806e2684
DG
3141 ret = LTTNG_ERR_SESSION_STARTED;
3142 goto error;
3a89d11a
DG
3143 } else {
3144 /*
3145 * If stopped, just make sure we've started before else the above call
3146 * will always send that there is data pending.
3147 *
3148 * The consumer assumes that when the data pending command is received,
3149 * the trace has been started before or else no output data is written
3150 * by the streams which is a condition for data pending. So, this is
3151 * *VERY* important that we don't ask the consumer before a start
3152 * trace.
3153 */
8382cf6f 3154 if (!session->has_been_started) {
3a89d11a
DG
3155 ret = 0;
3156 goto error;
3157 }
806e2684
DG
3158 }
3159
3160 if (ksess && ksess->consumer) {
6d805429
DG
3161 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
3162 if (ret == 1) {
806e2684
DG
3163 /* Data is still being extracted for the kernel. */
3164 goto error;
3165 }
3166 }
3167
3168 if (usess && usess->consumer) {
6d805429
DG
3169 ret = consumer_is_data_pending(usess->id, usess->consumer);
3170 if (ret == 1) {
806e2684
DG
3171 /* Data is still being extracted for the kernel. */
3172 goto error;
3173 }
3174 }
3175
3176 /* Data is ready to be read by a viewer */
6d805429 3177 ret = 0;
806e2684
DG
3178
3179error:
3180 return ret;
3181}
3182
6dc3064a
DG
3183/*
3184 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3185 *
3186 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3187 */
3188int cmd_snapshot_add_output(struct ltt_session *session,
3189 struct lttng_snapshot_output *output, uint32_t *id)
3190{
3191 int ret;
3192 struct snapshot_output *new_output;
3193
3194 assert(session);
3195 assert(output);
3196
3197 DBG("Cmd snapshot add output for session %s", session->name);
3198
3199 /*
d3f14b8a
MD
3200 * Permission denied to create an output if the session is not
3201 * set in no output mode.
6dc3064a
DG
3202 */
3203 if (session->output_traces) {
3204 ret = LTTNG_ERR_EPERM;
3205 goto error;
3206 }
3207
3208 /* Only one output is allowed until we have the "tee" feature. */
3209 if (session->snapshot.nb_output == 1) {
3210 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
3211 goto error;
3212 }
3213
3214 new_output = snapshot_output_alloc();
3215 if (!new_output) {
3216 ret = LTTNG_ERR_NOMEM;
3217 goto error;
3218 }
3219
3220 ret = snapshot_output_init(output->max_size, output->name,
3221 output->ctrl_url, output->data_url, session->consumer, new_output,
3222 &session->snapshot);
3223 if (ret < 0) {
3224 if (ret == -ENOMEM) {
3225 ret = LTTNG_ERR_NOMEM;
3226 } else {
3227 ret = LTTNG_ERR_INVALID;
3228 }
3229 goto free_error;
3230 }
3231
6dc3064a
DG
3232 rcu_read_lock();
3233 snapshot_add_output(&session->snapshot, new_output);
3234 if (id) {
3235 *id = new_output->id;
3236 }
3237 rcu_read_unlock();
3238
3239 return LTTNG_OK;
3240
3241free_error:
3242 snapshot_output_destroy(new_output);
3243error:
3244 return ret;
3245}
3246
3247/*
3248 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3249 *
3250 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3251 */
3252int cmd_snapshot_del_output(struct ltt_session *session,
3253 struct lttng_snapshot_output *output)
3254{
3255 int ret;
eb240553 3256 struct snapshot_output *sout = NULL;
6dc3064a
DG
3257
3258 assert(session);
3259 assert(output);
3260
6dc3064a
DG
3261 rcu_read_lock();
3262
3263 /*
d3f14b8a
MD
3264 * Permission denied to create an output if the session is not
3265 * set in no output mode.
6dc3064a
DG
3266 */
3267 if (session->output_traces) {
3268 ret = LTTNG_ERR_EPERM;
3269 goto error;
3270 }
3271
eb240553
DG
3272 if (output->id) {
3273 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
3274 session->name);
3275 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
3276 } else if (*output->name != '\0') {
3277 DBG("Cmd snapshot del output name %s for session %s", output->name,
3278 session->name);
3279 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
3280 }
6dc3064a
DG
3281 if (!sout) {
3282 ret = LTTNG_ERR_INVALID;
3283 goto error;
3284 }
3285
3286 snapshot_delete_output(&session->snapshot, sout);
3287 snapshot_output_destroy(sout);
3288 ret = LTTNG_OK;
3289
3290error:
3291 rcu_read_unlock();
3292 return ret;
3293}
3294
3295/*
3296 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3297 *
3298 * If no output is available, outputs is untouched and 0 is returned.
3299 *
3300 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3301 */
3302ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
3303 struct lttng_snapshot_output **outputs)
3304{
3305 int ret, idx = 0;
b223ca94 3306 struct lttng_snapshot_output *list = NULL;
6dc3064a
DG
3307 struct lttng_ht_iter iter;
3308 struct snapshot_output *output;
3309
3310 assert(session);
3311 assert(outputs);
3312
3313 DBG("Cmd snapshot list outputs for session %s", session->name);
3314
3315 /*
d3f14b8a
MD
3316 * Permission denied to create an output if the session is not
3317 * set in no output mode.
6dc3064a
DG
3318 */
3319 if (session->output_traces) {
b223ca94 3320 ret = -LTTNG_ERR_EPERM;
6dc3064a
DG
3321 goto error;
3322 }
3323
3324 if (session->snapshot.nb_output == 0) {
3325 ret = 0;
3326 goto error;
3327 }
3328
3329 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
3330 if (!list) {
b223ca94 3331 ret = -LTTNG_ERR_NOMEM;
6dc3064a
DG
3332 goto error;
3333 }
3334
3335 /* Copy list from session to the new list object. */
b223ca94 3336 rcu_read_lock();
6dc3064a
DG
3337 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
3338 output, node.node) {
3339 assert(output->consumer);
3340 list[idx].id = output->id;
3341 list[idx].max_size = output->max_size;
3342 strncpy(list[idx].name, output->name, sizeof(list[idx].name));
3343 if (output->consumer->type == CONSUMER_DST_LOCAL) {
3344 strncpy(list[idx].ctrl_url, output->consumer->dst.trace_path,
3345 sizeof(list[idx].ctrl_url));
3346 } else {
3347 /* Control URI. */
3348 ret = uri_to_str_url(&output->consumer->dst.net.control,
3349 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
3350 if (ret < 0) {
b223ca94
JG
3351 ret = -LTTNG_ERR_NOMEM;
3352 goto error;
6dc3064a
DG
3353 }
3354
3355 /* Data URI. */
3356 ret = uri_to_str_url(&output->consumer->dst.net.data,
3357 list[idx].data_url, sizeof(list[idx].data_url));
3358 if (ret < 0) {
b223ca94
JG
3359 ret = -LTTNG_ERR_NOMEM;
3360 goto error;
6dc3064a
DG
3361 }
3362 }
3363 idx++;
3364 }
3365
3366 *outputs = list;
b223ca94
JG
3367 list = NULL;
3368 ret = session->snapshot.nb_output;
6dc3064a 3369error:
b223ca94
JG
3370 free(list);
3371 rcu_read_unlock();
3372 return ret;
6dc3064a
DG
3373}
3374
3375/*
3376 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3377 * snapshot output is *not* set with a remote destination.
3378 *
a934f4af 3379 * Return 0 on success or a LTTNG_ERR code.
6dc3064a
DG
3380 */
3381static int set_relayd_for_snapshot(struct consumer_output *consumer,
3382 struct snapshot_output *snap_output, struct ltt_session *session)
3383{
a934f4af 3384 int ret = LTTNG_OK;
6dc3064a
DG
3385 struct lttng_ht_iter iter;
3386 struct consumer_socket *socket;
3387
3388 assert(consumer);
3389 assert(snap_output);
3390 assert(session);
3391
3392 DBG2("Set relayd object from snapshot output");
3393
3394 /* Ignore if snapshot consumer output is not network. */
3395 if (snap_output->consumer->type != CONSUMER_DST_NET) {
3396 goto error;
3397 }
3398
3399 /*
3400 * For each consumer socket, create and send the relayd object of the
3401 * snapshot output.
3402 */
3403 rcu_read_lock();
5eecee74
DG
3404 cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter,
3405 socket, node.node) {
6dc3064a 3406 ret = send_consumer_relayd_sockets(0, session->id,
d3e2ba59
JD
3407 snap_output->consumer, socket,
3408 session->name, session->hostname,
3409 session->live_timer);
a934f4af 3410 if (ret != LTTNG_OK) {
6dc3064a
DG
3411 rcu_read_unlock();
3412 goto error;
3413 }
3414 }
3415 rcu_read_unlock();
3416
3417error:
3418 return ret;
3419}
3420
3421/*
3422 * Record a kernel snapshot.
3423 *
fac41e72 3424 * Return LTTNG_OK on success or a LTTNG_ERR code.
6dc3064a
DG
3425 */
3426static int record_kernel_snapshot(struct ltt_kernel_session *ksess,
5c786ded 3427 struct snapshot_output *output, struct ltt_session *session,
d07ceecd 3428 int wait, uint64_t nb_packets_per_stream)
6dc3064a
DG
3429{
3430 int ret;
3431
3432 assert(ksess);
3433 assert(output);
3434 assert(session);
3435
10a50311
JD
3436 /* Get the datetime for the snapshot output directory. */
3437 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
3438 sizeof(output->datetime));
3439 if (!ret) {
a934f4af 3440 ret = LTTNG_ERR_INVALID;
10a50311
JD
3441 goto error;
3442 }
3443
af706bb7
DG
3444 /*
3445 * Copy kernel session sockets so we can communicate with the right
3446 * consumer for the snapshot record command.
3447 */
3448 ret = consumer_copy_sockets(output->consumer, ksess->consumer);
3449 if (ret < 0) {
a934f4af 3450 ret = LTTNG_ERR_NOMEM;
af706bb7 3451 goto error;
6dc3064a
DG
3452 }
3453
3454 ret = set_relayd_for_snapshot(ksess->consumer, output, session);
a934f4af 3455 if (ret != LTTNG_OK) {
af706bb7 3456 goto error_snapshot;
6dc3064a
DG
3457 }
3458
d07ceecd 3459 ret = kernel_snapshot_record(ksess, output, wait, nb_packets_per_stream);
2a06df8d 3460 if (ret != LTTNG_OK) {
af706bb7 3461 goto error_snapshot;
6dc3064a
DG
3462 }
3463
a934f4af 3464 ret = LTTNG_OK;
1371fc12 3465 goto end;
a934f4af 3466
af706bb7
DG
3467error_snapshot:
3468 /* Clean up copied sockets so this output can use some other later on. */
3469 consumer_destroy_output_sockets(output->consumer);
6dc3064a 3470error:
1371fc12 3471end:
6dc3064a
DG
3472 return ret;
3473}
3474
3475/*
3476 * Record a UST snapshot.
3477 *
a934f4af 3478 * Return 0 on success or a LTTNG_ERR error code.
6dc3064a
DG
3479 */
3480static int record_ust_snapshot(struct ltt_ust_session *usess,
5c786ded 3481 struct snapshot_output *output, struct ltt_session *session,
d07ceecd 3482 int wait, uint64_t nb_packets_per_stream)
6dc3064a
DG
3483{
3484 int ret;
3485
3486 assert(usess);
3487 assert(output);
3488 assert(session);
3489
10a50311
JD
3490 /* Get the datetime for the snapshot output directory. */
3491 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", output->datetime,
3492 sizeof(output->datetime));
3493 if (!ret) {
a934f4af 3494 ret = LTTNG_ERR_INVALID;
10a50311
JD
3495 goto error;
3496 }
3497
af706bb7 3498 /*
d3f14b8a 3499 * Copy UST session sockets so we can communicate with the right
af706bb7
DG
3500 * consumer for the snapshot record command.
3501 */
3502 ret = consumer_copy_sockets(output->consumer, usess->consumer);
3503 if (ret < 0) {
a934f4af 3504 ret = LTTNG_ERR_NOMEM;
af706bb7 3505 goto error;
6dc3064a
DG
3506 }
3507
3508 ret = set_relayd_for_snapshot(usess->consumer, output, session);
a934f4af 3509 if (ret != LTTNG_OK) {
af706bb7 3510 goto error_snapshot;
6dc3064a
DG
3511 }
3512
d07ceecd 3513 ret = ust_app_snapshot_record(usess, output, wait, nb_packets_per_stream);
6dc3064a 3514 if (ret < 0) {
7badf927
DG
3515 switch (-ret) {
3516 case EINVAL:
a934f4af 3517 ret = LTTNG_ERR_INVALID;
7badf927
DG
3518 break;
3519 case ENODATA:
3520 ret = LTTNG_ERR_SNAPSHOT_NODATA;
3521 break;
3522 default:
3523 ret = LTTNG_ERR_SNAPSHOT_FAIL;
3524 break;
5c786ded 3525 }
af706bb7 3526 goto error_snapshot;
6dc3064a
DG
3527 }
3528
a934f4af
DG
3529 ret = LTTNG_OK;
3530
af706bb7
DG
3531error_snapshot:
3532 /* Clean up copied sockets so this output can use some other later on. */
3533 consumer_destroy_output_sockets(output->consumer);
6dc3064a
DG
3534error:
3535 return ret;
3536}
3537
d07ceecd
MD
3538static
3539uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session *session,
3540 uint64_t cur_nr_packets)
68808f4e 3541{
d07ceecd 3542 uint64_t tot_size = 0;
68808f4e
DG
3543
3544 if (session->kernel_session) {
3545 struct ltt_kernel_channel *chan;
3546 struct ltt_kernel_session *ksess = session->kernel_session;
3547
68808f4e 3548 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
d07ceecd
MD
3549 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
3550 /*
3551 * Don't take channel into account if we
3552 * already grab all its packets.
3553 */
3554 continue;
68808f4e 3555 }
d07ceecd
MD
3556 tot_size += chan->channel->attr.subbuf_size
3557 * chan->stream_count;
68808f4e
DG
3558 }
3559 }
3560
3561 if (session->ust_session) {
68808f4e
DG
3562 struct ltt_ust_session *usess = session->ust_session;
3563
d07ceecd
MD
3564 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
3565 cur_nr_packets);
68808f4e
DG
3566 }
3567
d07ceecd 3568 return tot_size;
68808f4e
DG
3569}
3570
5c786ded 3571/*
d07ceecd
MD
3572 * Calculate the number of packets we can grab from each stream that
3573 * fits within the overall snapshot max size.
3574 *
3575 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3576 * the number of packets per stream.
3577 *
3578 * TODO: this approach is not perfect: we consider the worse case
3579 * (packet filling the sub-buffers) as an upper bound, but we could do
3580 * better if we do this calculation while we actually grab the packet
3581 * content: we would know how much padding we don't actually store into
3582 * the file.
3583 *
3584 * This algorithm is currently bounded by the number of packets per
3585 * stream.
3586 *
3587 * Since we call this algorithm before actually grabbing the data, it's
3588 * an approximation: for instance, applications could appear/disappear
3589 * in between this call and actually grabbing data.
5c786ded 3590 */
d07ceecd
MD
3591static
3592int64_t get_session_nb_packets_per_stream(struct ltt_session *session, uint64_t max_size)
5c786ded 3593{
d07ceecd
MD
3594 int64_t size_left;
3595 uint64_t cur_nb_packets = 0;
5c786ded 3596
d07ceecd
MD
3597 if (!max_size) {
3598 return 0; /* Infinite */
5c786ded
JD
3599 }
3600
d07ceecd
MD
3601 size_left = max_size;
3602 for (;;) {
3603 uint64_t one_more_packet_tot_size;
5c786ded 3604
d07ceecd
MD
3605 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(session,
3606 cur_nb_packets);
3607 if (!one_more_packet_tot_size) {
3608 /* We are already grabbing all packets. */
3609 break;
3610 }
3611 size_left -= one_more_packet_tot_size;
3612 if (size_left < 0) {
3613 break;
3614 }
3615 cur_nb_packets++;
5c786ded 3616 }
d07ceecd
MD
3617 if (!cur_nb_packets) {
3618 /* Not enough room to grab one packet of each stream, error. */
3619 return -1;
3620 }
3621 return cur_nb_packets;
5c786ded
JD
3622}
3623
6dc3064a
DG
3624/*
3625 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3626 *
3627 * The wait parameter is ignored so this call always wait for the snapshot to
3628 * complete before returning.
3629 *
3630 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3631 */
3632int cmd_snapshot_record(struct ltt_session *session,
3633 struct lttng_snapshot_output *output, int wait)
3634{
3635 int ret = LTTNG_OK;
e1986656
DG
3636 unsigned int use_tmp_output = 0;
3637 struct snapshot_output tmp_output;
00e1dfc4 3638 unsigned int snapshot_success = 0;
6dc3064a
DG
3639
3640 assert(session);
ba45d9f0 3641 assert(output);
6dc3064a
DG
3642
3643 DBG("Cmd snapshot record for session %s", session->name);
3644
3645 /*
d3f14b8a
MD
3646 * Permission denied to create an output if the session is not
3647 * set in no output mode.
6dc3064a
DG
3648 */
3649 if (session->output_traces) {
3650 ret = LTTNG_ERR_EPERM;
3651 goto error;
3652 }
3653
3654 /* The session needs to be started at least once. */
8382cf6f 3655 if (!session->has_been_started) {
6dc3064a
DG
3656 ret = LTTNG_ERR_START_SESSION_ONCE;
3657 goto error;
3658 }
3659
3660 /* Use temporary output for the session. */
ba45d9f0 3661 if (*output->ctrl_url != '\0') {
6dc3064a
DG
3662 ret = snapshot_output_init(output->max_size, output->name,
3663 output->ctrl_url, output->data_url, session->consumer,
e1986656 3664 &tmp_output, NULL);
6dc3064a
DG
3665 if (ret < 0) {
3666 if (ret == -ENOMEM) {
3667 ret = LTTNG_ERR_NOMEM;
3668 } else {
3669 ret = LTTNG_ERR_INVALID;
3670 }
3671 goto error;
3672 }
1bfe7328
DG
3673 /* Use the global session count for the temporary snapshot. */
3674 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
e1986656 3675 use_tmp_output = 1;
6dc3064a
DG
3676 }
3677
3678 if (session->kernel_session) {
3679 struct ltt_kernel_session *ksess = session->kernel_session;
3680
e1986656 3681 if (use_tmp_output) {
d07ceecd
MD
3682 int64_t nb_packets_per_stream;
3683
3684 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3685 tmp_output.max_size);
3686 if (nb_packets_per_stream < 0) {
3687 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3688 goto error;
3689 }
e1986656 3690 ret = record_kernel_snapshot(ksess, &tmp_output, session,
d07ceecd 3691 wait, nb_packets_per_stream);
a934f4af 3692 if (ret != LTTNG_OK) {
6dc3064a
DG
3693 goto error;
3694 }
1bfe7328 3695 snapshot_success = 1;
6dc3064a
DG
3696 } else {
3697 struct snapshot_output *sout;
3698 struct lttng_ht_iter iter;
3699
3700 rcu_read_lock();
3701 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3702 &iter.iter, sout, node.node) {
d07ceecd
MD
3703 int64_t nb_packets_per_stream;
3704
e1986656
DG
3705 /*
3706 * Make a local copy of the output and assign the possible
3707 * temporary value given by the caller.
3708 */
3709 memset(&tmp_output, 0, sizeof(tmp_output));
3710 memcpy(&tmp_output, sout, sizeof(tmp_output));
3711
e1986656
DG
3712 if (output->max_size != (uint64_t) -1ULL) {
3713 tmp_output.max_size = output->max_size;
3714 }
3715
d07ceecd
MD
3716 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3717 tmp_output.max_size);
3718 if (nb_packets_per_stream < 0) {
68808f4e
DG
3719 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3720 goto error;
3721 }
3722
e1986656
DG
3723 /* Use temporary name. */
3724 if (*output->name != '\0') {
3725 strncpy(tmp_output.name, output->name,
3726 sizeof(tmp_output.name));
3727 }
3728
1bfe7328
DG
3729 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3730
e1986656 3731 ret = record_kernel_snapshot(ksess, &tmp_output,
d07ceecd 3732 session, wait, nb_packets_per_stream);
a934f4af 3733 if (ret != LTTNG_OK) {
6dc3064a
DG
3734 rcu_read_unlock();
3735 goto error;
3736 }
1bfe7328 3737 snapshot_success = 1;
6dc3064a
DG
3738 }
3739 rcu_read_unlock();
3740 }
3741 }
3742
3743 if (session->ust_session) {
3744 struct ltt_ust_session *usess = session->ust_session;
3745
e1986656 3746 if (use_tmp_output) {
d07ceecd
MD
3747 int64_t nb_packets_per_stream;
3748
3749 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3750 tmp_output.max_size);
3751 if (nb_packets_per_stream < 0) {
3752 ret = LTTNG_ERR_MAX_SIZE_INVALID;
3753 goto error;
3754 }
e1986656 3755 ret = record_ust_snapshot(usess, &tmp_output, session,
d07ceecd 3756 wait, nb_packets_per_stream);
a934f4af 3757 if (ret != LTTNG_OK) {
6dc3064a
DG
3758 goto error;
3759 }
1bfe7328 3760 snapshot_success = 1;
6dc3064a
DG
3761 } else {
3762 struct snapshot_output *sout;
3763 struct lttng_ht_iter iter;
3764
3765 rcu_read_lock();
3766 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
3767 &iter.iter, sout, node.node) {
d07ceecd
MD
3768 int64_t nb_packets_per_stream;
3769
e1986656
DG
3770 /*
3771 * Make a local copy of the output and assign the possible
3772 * temporary value given by the caller.
3773 */
3774 memset(&tmp_output, 0, sizeof(tmp_output));
3775 memcpy(&tmp_output, sout, sizeof(tmp_output));
3776
e1986656
DG
3777 if (output->max_size != (uint64_t) -1ULL) {
3778 tmp_output.max_size = output->max_size;
3779 }
3780
d07ceecd
MD
3781 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
3782 tmp_output.max_size);
3783 if (nb_packets_per_stream < 0) {
68808f4e 3784 ret = LTTNG_ERR_MAX_SIZE_INVALID;
d07ceecd 3785 rcu_read_unlock();
68808f4e
DG
3786 goto error;
3787 }
3788
e1986656
DG
3789 /* Use temporary name. */
3790 if (*output->name != '\0') {
3791 strncpy(tmp_output.name, output->name,
3792 sizeof(tmp_output.name));
3793 }
3794
1bfe7328
DG
3795 tmp_output.nb_snapshot = session->snapshot.nb_snapshot;
3796
e1986656 3797 ret = record_ust_snapshot(usess, &tmp_output, session,
d07ceecd 3798 wait, nb_packets_per_stream);
a934f4af 3799 if (ret != LTTNG_OK) {
6dc3064a
DG
3800 rcu_read_unlock();
3801 goto error;
3802 }
1bfe7328 3803 snapshot_success = 1;
6dc3064a
DG
3804 }
3805 rcu_read_unlock();
3806 }
3807 }
3808
1bfe7328
DG
3809 if (snapshot_success) {
3810 session->snapshot.nb_snapshot++;
b67578cb
MD
3811 } else {
3812 ret = LTTNG_ERR_SNAPSHOT_FAIL;
1bfe7328
DG
3813 }
3814
6dc3064a 3815error:
6dc3064a
DG
3816 return ret;
3817}
3818
d7ba1388
MD
3819/*
3820 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3821 */
3822int cmd_set_session_shm_path(struct ltt_session *session,
3823 const char *shm_path)
3824{
3825 /* Safety net */
3826 assert(session);
3827
3828 /*
3829 * Can only set shm path before session is started.
3830 */
3831 if (session->has_been_started) {
3832 return LTTNG_ERR_SESSION_STARTED;
3833 }
3834
3835 strncpy(session->shm_path, shm_path,
3836 sizeof(session->shm_path));
3837 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
3838
3839 return 0;
3840}
3841
2f77fc4b
DG
3842/*
3843 * Init command subsystem.
3844 */
3845void cmd_init(void)
3846{
3847 /*
d88aee68
DG
3848 * Set network sequence index to 1 for streams to match a relayd
3849 * socket on the consumer side.
2f77fc4b 3850 */
d88aee68
DG
3851 pthread_mutex_lock(&relayd_net_seq_idx_lock);
3852 relayd_net_seq_idx = 1;
3853 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
3854
3855 DBG("Command subsystem initialized");
3856}
This page took 0.2893 seconds and 4 git commands to generate.