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