Fix: Wrong fd used by kernel_wait_quiescent
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.c
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>
20 #include <urcu/list.h>
21 #include <urcu/uatomic.h>
22
23 #include <common/defaults.h>
24 #include <common/common.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
26 #include <common/relayd/relayd.h>
27
28 #include "channel.h"
29 #include "consumer.h"
30 #include "event.h"
31 #include "kernel.h"
32 #include "kernel-consumer.h"
33 #include "lttng-sessiond.h"
34 #include "utils.h"
35
36 #include "cmd.h"
37
38 /*
39 * Used to keep a unique index for each relayd socket created where this value
40 * is associated with streams on the consumer so it can match the right relayd
41 * to send to.
42 *
43 * This value should be incremented atomically for safety purposes and future
44 * possible concurrent access.
45 */
46 static unsigned int relayd_net_seq_idx;
47
48 /*
49 * Create a session path used by list_lttng_sessions for the case that the
50 * session consumer is on the network.
51 */
52 static int build_network_session_path(char *dst, size_t size,
53 struct ltt_session *session)
54 {
55 int ret, kdata_port, udata_port;
56 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
57 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
58
59 assert(session);
60 assert(dst);
61
62 memset(tmp_urls, 0, sizeof(tmp_urls));
63 memset(tmp_uurl, 0, sizeof(tmp_uurl));
64
65 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
66
67 if (session->kernel_session && session->kernel_session->consumer) {
68 kuri = &session->kernel_session->consumer->dst.net.control;
69 kdata_port = session->kernel_session->consumer->dst.net.data.port;
70 }
71
72 if (session->ust_session && session->ust_session->consumer) {
73 uuri = &session->ust_session->consumer->dst.net.control;
74 udata_port = session->ust_session->consumer->dst.net.data.port;
75 }
76
77 if (uuri == NULL && kuri == NULL) {
78 uri = &session->consumer->dst.net.control;
79 kdata_port = session->consumer->dst.net.data.port;
80 } else if (kuri && uuri) {
81 ret = uri_compare(kuri, uuri);
82 if (ret) {
83 /* Not Equal */
84 uri = kuri;
85 /* Build uuri URL string */
86 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
87 if (ret < 0) {
88 goto error;
89 }
90 } else {
91 uri = kuri;
92 }
93 } else if (kuri && uuri == NULL) {
94 uri = kuri;
95 } else if (uuri && kuri == NULL) {
96 uri = uuri;
97 }
98
99 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
100 if (ret < 0) {
101 goto error;
102 }
103
104 /*
105 * Do we have a UST url set. If yes, this means we have both kernel and UST
106 * to print.
107 */
108 if (strlen(tmp_uurl) > 0) {
109 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
110 tmp_urls, kdata_port, tmp_uurl, udata_port);
111 } else {
112 int dport;
113 if (kuri) {
114 dport = kdata_port;
115 } else {
116 /* No kernel URI, use the UST port. */
117 dport = udata_port;
118 }
119 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
120 }
121
122 error:
123 return ret;
124 }
125
126 /*
127 * Fill lttng_channel array of all channels.
128 */
129 static void list_lttng_channels(int domain, struct ltt_session *session,
130 struct lttng_channel *channels)
131 {
132 int i = 0;
133 struct ltt_kernel_channel *kchan;
134
135 DBG("Listing channels for session %s", session->name);
136
137 switch (domain) {
138 case LTTNG_DOMAIN_KERNEL:
139 /* Kernel channels */
140 if (session->kernel_session != NULL) {
141 cds_list_for_each_entry(kchan,
142 &session->kernel_session->channel_list.head, list) {
143 /* Copy lttng_channel struct to array */
144 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
145 channels[i].enabled = kchan->enabled;
146 i++;
147 }
148 }
149 break;
150 case LTTNG_DOMAIN_UST:
151 {
152 struct lttng_ht_iter iter;
153 struct ltt_ust_channel *uchan;
154
155 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
156 &iter.iter, uchan, node.node) {
157 strncpy(channels[i].name, uchan->name, LTTNG_SYMBOL_NAME_LEN);
158 channels[i].attr.overwrite = uchan->attr.overwrite;
159 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
160 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
161 channels[i].attr.switch_timer_interval =
162 uchan->attr.switch_timer_interval;
163 channels[i].attr.read_timer_interval =
164 uchan->attr.read_timer_interval;
165 channels[i].enabled = uchan->enabled;
166 switch (uchan->attr.output) {
167 case LTTNG_UST_MMAP:
168 default:
169 channels[i].attr.output = LTTNG_EVENT_MMAP;
170 break;
171 }
172 i++;
173 }
174 break;
175 }
176 default:
177 break;
178 }
179 }
180
181 /*
182 * Create a list of ust global domain events.
183 */
184 static int list_lttng_ust_global_events(char *channel_name,
185 struct ltt_ust_domain_global *ust_global, struct lttng_event **events)
186 {
187 int i = 0, ret = 0;
188 unsigned int nb_event = 0;
189 struct lttng_ht_iter iter;
190 struct lttng_ht_node_str *node;
191 struct ltt_ust_channel *uchan;
192 struct ltt_ust_event *uevent;
193 struct lttng_event *tmp;
194
195 DBG("Listing UST global events for channel %s", channel_name);
196
197 rcu_read_lock();
198
199 lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter);
200 node = lttng_ht_iter_get_node_str(&iter);
201 if (node == NULL) {
202 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
203 goto error;
204 }
205
206 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
207
208 nb_event += lttng_ht_get_count(uchan->events);
209
210 if (nb_event == 0) {
211 ret = nb_event;
212 goto error;
213 }
214
215 DBG3("Listing UST global %d events", nb_event);
216
217 tmp = zmalloc(nb_event * sizeof(struct lttng_event));
218 if (tmp == NULL) {
219 ret = LTTNG_ERR_FATAL;
220 goto error;
221 }
222
223 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
224 strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN);
225 tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
226 tmp[i].enabled = uevent->enabled;
227
228 switch (uevent->attr.instrumentation) {
229 case LTTNG_UST_TRACEPOINT:
230 tmp[i].type = LTTNG_EVENT_TRACEPOINT;
231 break;
232 case LTTNG_UST_PROBE:
233 tmp[i].type = LTTNG_EVENT_PROBE;
234 break;
235 case LTTNG_UST_FUNCTION:
236 tmp[i].type = LTTNG_EVENT_FUNCTION;
237 break;
238 }
239
240 tmp[i].loglevel = uevent->attr.loglevel;
241 switch (uevent->attr.loglevel_type) {
242 case LTTNG_UST_LOGLEVEL_ALL:
243 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
244 break;
245 case LTTNG_UST_LOGLEVEL_RANGE:
246 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
247 break;
248 case LTTNG_UST_LOGLEVEL_SINGLE:
249 tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
250 break;
251 }
252 if (uevent->filter) {
253 tmp[i].filter = 1;
254 }
255 i++;
256 }
257
258 ret = nb_event;
259 *events = tmp;
260
261 error:
262 rcu_read_unlock();
263 return ret;
264 }
265
266 /*
267 * Fill lttng_event array of all kernel events in the channel.
268 */
269 static int list_lttng_kernel_events(char *channel_name,
270 struct ltt_kernel_session *kernel_session, struct lttng_event **events)
271 {
272 int i = 0, ret;
273 unsigned int nb_event;
274 struct ltt_kernel_event *event;
275 struct ltt_kernel_channel *kchan;
276
277 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
278 if (kchan == NULL) {
279 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
280 goto error;
281 }
282
283 nb_event = kchan->event_count;
284
285 DBG("Listing events for channel %s", kchan->channel->name);
286
287 if (nb_event == 0) {
288 goto end;
289 }
290
291 *events = zmalloc(nb_event * sizeof(struct lttng_event));
292 if (*events == NULL) {
293 ret = LTTNG_ERR_FATAL;
294 goto error;
295 }
296
297 /* Kernel channels */
298 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
299 strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
300 (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
301 (*events)[i].enabled = event->enabled;
302
303 switch (event->event->instrumentation) {
304 case LTTNG_KERNEL_TRACEPOINT:
305 (*events)[i].type = LTTNG_EVENT_TRACEPOINT;
306 break;
307 case LTTNG_KERNEL_KPROBE:
308 case LTTNG_KERNEL_KRETPROBE:
309 (*events)[i].type = LTTNG_EVENT_PROBE;
310 memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe,
311 sizeof(struct lttng_kernel_kprobe));
312 break;
313 case LTTNG_KERNEL_FUNCTION:
314 (*events)[i].type = LTTNG_EVENT_FUNCTION;
315 memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace,
316 sizeof(struct lttng_kernel_function));
317 break;
318 case LTTNG_KERNEL_NOOP:
319 (*events)[i].type = LTTNG_EVENT_NOOP;
320 break;
321 case LTTNG_KERNEL_SYSCALL:
322 (*events)[i].type = LTTNG_EVENT_SYSCALL;
323 break;
324 case LTTNG_KERNEL_ALL:
325 assert(0);
326 break;
327 }
328 i++;
329 }
330
331 end:
332 return nb_event;
333
334 error:
335 /* Negate the error code to differentiate the size from an error */
336 return -ret;
337 }
338
339 /*
340 * Add URI so the consumer output object. Set the correct path depending on the
341 * domain adding the default trace directory.
342 */
343 static int add_uri_to_consumer(struct consumer_output *consumer,
344 struct lttng_uri *uri, int domain, const char *session_name)
345 {
346 int ret = LTTNG_OK;
347 const char *default_trace_dir;
348
349 assert(uri);
350
351 if (consumer == NULL) {
352 DBG("No consumer detected. Don't add URI. Stopping.");
353 ret = LTTNG_ERR_NO_CONSUMER;
354 goto error;
355 }
356
357 switch (domain) {
358 case LTTNG_DOMAIN_KERNEL:
359 default_trace_dir = DEFAULT_KERNEL_TRACE_DIR;
360 break;
361 case LTTNG_DOMAIN_UST:
362 default_trace_dir = DEFAULT_UST_TRACE_DIR;
363 break;
364 default:
365 /*
366 * This case is possible is we try to add the URI to the global tracing
367 * session consumer object which in this case there is no subdir.
368 */
369 default_trace_dir = "";
370 }
371
372 switch (uri->dtype) {
373 case LTTNG_DST_IPV4:
374 case LTTNG_DST_IPV6:
375 DBG2("Setting network URI to consumer");
376
377 /* Set URI into consumer output object */
378 ret = consumer_set_network_uri(consumer, uri);
379 if (ret < 0) {
380 ret = LTTNG_ERR_FATAL;
381 goto error;
382 } else if (ret == 1) {
383 /*
384 * URI was the same in the consumer so we do not append the subdir
385 * again so to not duplicate output dir.
386 */
387 goto error;
388 }
389
390 if (uri->stype == LTTNG_STREAM_CONTROL && strlen(uri->subdir) == 0) {
391 ret = consumer_set_subdir(consumer, session_name);
392 if (ret < 0) {
393 ret = LTTNG_ERR_FATAL;
394 goto error;
395 }
396 }
397
398 if (uri->stype == LTTNG_STREAM_CONTROL) {
399 /* On a new subdir, reappend the default trace dir. */
400 strncat(consumer->subdir, default_trace_dir,
401 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
402 DBG3("Append domain trace name to subdir %s", consumer->subdir);
403 }
404
405 break;
406 case LTTNG_DST_PATH:
407 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
408 memset(consumer->dst.trace_path, 0,
409 sizeof(consumer->dst.trace_path));
410 strncpy(consumer->dst.trace_path, uri->dst.path,
411 sizeof(consumer->dst.trace_path));
412 /* Append default trace dir */
413 strncat(consumer->dst.trace_path, default_trace_dir,
414 sizeof(consumer->dst.trace_path) -
415 strlen(consumer->dst.trace_path) - 1);
416 /* Flag consumer as local. */
417 consumer->type = CONSUMER_DST_LOCAL;
418 break;
419 }
420
421 error:
422 return ret;
423 }
424
425 /*
426 * Init tracing by creating trace directory and sending fds kernel consumer.
427 */
428 static int init_kernel_tracing(struct ltt_kernel_session *session)
429 {
430 int ret = 0;
431 struct lttng_ht_iter iter;
432 struct consumer_socket *socket;
433
434 assert(session);
435
436 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
437 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
438 socket, node.node) {
439 /* Code flow error */
440 assert(socket->fd >= 0);
441
442 pthread_mutex_lock(socket->lock);
443 ret = kernel_consumer_send_session(socket->fd, session);
444 pthread_mutex_unlock(socket->lock);
445 if (ret < 0) {
446 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
447 goto error;
448 }
449 }
450 }
451
452 error:
453 return ret;
454 }
455
456 /*
457 * Create a socket to the relayd using the URI.
458 *
459 * On success, the relayd_sock pointer is set to the created socket.
460 * Else, it's stays untouched and a lttcomm error code is returned.
461 */
462 static int create_connect_relayd(struct consumer_output *output,
463 const char *session_name, struct lttng_uri *uri,
464 struct lttcomm_sock **relayd_sock)
465 {
466 int ret;
467 struct lttcomm_sock *sock;
468
469 /* Create socket object from URI */
470 sock = lttcomm_alloc_sock_from_uri(uri);
471 if (sock == NULL) {
472 ret = LTTNG_ERR_FATAL;
473 goto error;
474 }
475
476 ret = lttcomm_create_sock(sock);
477 if (ret < 0) {
478 ret = LTTNG_ERR_FATAL;
479 goto error;
480 }
481
482 /* Connect to relayd so we can proceed with a session creation. */
483 ret = relayd_connect(sock);
484 if (ret < 0) {
485 ERR("Unable to reach lttng-relayd");
486 ret = LTTNG_ERR_RELAYD_CONNECT_FAIL;
487 goto free_sock;
488 }
489
490 /* Create socket for control stream. */
491 if (uri->stype == LTTNG_STREAM_CONTROL) {
492 DBG3("Creating relayd stream socket from URI");
493
494 /* Check relayd version */
495 ret = relayd_version_check(sock, RELAYD_VERSION_COMM_MAJOR,
496 RELAYD_VERSION_COMM_MINOR);
497 if (ret < 0) {
498 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
499 goto close_sock;
500 }
501 } else if (uri->stype == LTTNG_STREAM_DATA) {
502 DBG3("Creating relayd data socket from URI");
503 } else {
504 /* Command is not valid */
505 ERR("Relayd invalid stream type: %d", uri->stype);
506 ret = LTTNG_ERR_INVALID;
507 goto close_sock;
508 }
509
510 *relayd_sock = sock;
511
512 return LTTNG_OK;
513
514 close_sock:
515 if (sock) {
516 (void) relayd_close(sock);
517 }
518 free_sock:
519 if (sock) {
520 lttcomm_destroy_sock(sock);
521 }
522 error:
523 return ret;
524 }
525
526 /*
527 * Connect to the relayd using URI and send the socket to the right consumer.
528 */
529 static int send_consumer_relayd_socket(int domain, struct ltt_session *session,
530 struct lttng_uri *relayd_uri, struct consumer_output *consumer,
531 int consumer_fd)
532 {
533 int ret;
534 struct lttcomm_sock *sock = NULL;
535
536 /* Set the network sequence index if not set. */
537 if (consumer->net_seq_index == -1) {
538 /*
539 * Increment net_seq_idx because we are about to transfer the
540 * new relayd socket to the consumer.
541 */
542 uatomic_inc(&relayd_net_seq_idx);
543 /* Assign unique key so the consumer can match streams */
544 uatomic_set(&consumer->net_seq_index,
545 uatomic_read(&relayd_net_seq_idx));
546 }
547
548 /* Connect to relayd and make version check if uri is the control. */
549 ret = create_connect_relayd(consumer, session->name, relayd_uri, &sock);
550 if (ret != LTTNG_OK) {
551 goto close_sock;
552 }
553
554 /* If the control socket is connected, network session is ready */
555 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
556 session->net_handle = 1;
557 }
558
559 /* Send relayd socket to consumer. */
560 ret = consumer_send_relayd_socket(consumer_fd, sock,
561 consumer, relayd_uri->stype);
562 if (ret < 0) {
563 ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
564 goto close_sock;
565 }
566
567 /* Flag that the corresponding socket was sent. */
568 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
569 consumer->dst.net.control_sock_sent = 1;
570 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
571 consumer->dst.net.data_sock_sent = 1;
572 }
573
574 ret = LTTNG_OK;
575
576 /*
577 * Close socket which was dup on the consumer side. The session daemon does
578 * NOT keep track of the relayd socket(s) once transfer to the consumer.
579 */
580
581 close_sock:
582 if (sock) {
583 (void) relayd_close(sock);
584 lttcomm_destroy_sock(sock);
585 }
586
587 return ret;
588 }
589
590 /*
591 * Send both relayd sockets to a specific consumer and domain. This is a
592 * helper function to facilitate sending the information to the consumer for a
593 * session.
594 */
595 static int send_consumer_relayd_sockets(int domain,
596 struct ltt_session *session, struct consumer_output *consumer, int fd)
597 {
598 int ret = LTTNG_OK;
599
600 assert(session);
601 assert(consumer);
602
603 /* Sending control relayd socket. */
604 if (!consumer->dst.net.control_sock_sent) {
605 ret = send_consumer_relayd_socket(domain, session,
606 &consumer->dst.net.control, consumer, fd);
607 if (ret != LTTNG_OK) {
608 goto error;
609 }
610 }
611
612 /* Sending data relayd socket. */
613 if (!consumer->dst.net.data_sock_sent) {
614 ret = send_consumer_relayd_socket(domain, session,
615 &consumer->dst.net.data, consumer, fd);
616 if (ret != LTTNG_OK) {
617 goto error;
618 }
619 }
620
621 error:
622 return ret;
623 }
624
625 /*
626 * Setup relayd connections for a tracing session. First creates the socket to
627 * the relayd and send them to the right domain consumer. Consumer type MUST be
628 * network.
629 */
630 static int setup_relayd(struct ltt_session *session)
631 {
632 int ret = LTTNG_OK;
633 struct ltt_ust_session *usess;
634 struct ltt_kernel_session *ksess;
635 struct consumer_socket *socket;
636 struct lttng_ht_iter iter;
637
638 assert(session);
639
640 usess = session->ust_session;
641 ksess = session->kernel_session;
642
643 DBG2("Setting relayd for session %s", session->name);
644
645 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
646 && usess->consumer->enabled) {
647 /* For each consumer socket, send relayd sockets */
648 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
649 socket, node.node) {
650 /* Code flow error */
651 assert(socket->fd >= 0);
652
653 pthread_mutex_lock(socket->lock);
654 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session,
655 usess->consumer, socket->fd);
656 pthread_mutex_unlock(socket->lock);
657 if (ret != LTTNG_OK) {
658 goto error;
659 }
660 }
661 }
662
663 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
664 && ksess->consumer->enabled) {
665 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
666 socket, node.node) {
667 /* Code flow error */
668 assert(socket->fd >= 0);
669
670 pthread_mutex_lock(socket->lock);
671 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session,
672 ksess->consumer, socket->fd);
673 pthread_mutex_unlock(socket->lock);
674 if (ret != LTTNG_OK) {
675 goto error;
676 }
677 }
678 }
679
680 error:
681 return ret;
682 }
683
684 /*
685 * Start a kernel session by opening all necessary streams.
686 */
687 static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe)
688 {
689 int ret;
690 struct ltt_kernel_channel *kchan;
691
692 /* Open kernel metadata */
693 if (ksess->metadata == NULL) {
694 ret = kernel_open_metadata(ksess);
695 if (ret < 0) {
696 ret = LTTNG_ERR_KERN_META_FAIL;
697 goto error;
698 }
699 }
700
701 /* Open kernel metadata stream */
702 if (ksess->metadata_stream_fd < 0) {
703 ret = kernel_open_metadata_stream(ksess);
704 if (ret < 0) {
705 ERR("Kernel create metadata stream failed");
706 ret = LTTNG_ERR_KERN_STREAM_FAIL;
707 goto error;
708 }
709 }
710
711 /* For each channel */
712 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
713 if (kchan->stream_count == 0) {
714 ret = kernel_open_channel_stream(kchan);
715 if (ret < 0) {
716 ret = LTTNG_ERR_KERN_STREAM_FAIL;
717 goto error;
718 }
719 /* Update the stream global counter */
720 ksess->stream_count_global += ret;
721 }
722 }
723
724 /* Setup kernel consumer socket and send fds to it */
725 ret = init_kernel_tracing(ksess);
726 if (ret < 0) {
727 ret = LTTNG_ERR_KERN_START_FAIL;
728 goto error;
729 }
730
731 /* This start the kernel tracing */
732 ret = kernel_start_session(ksess);
733 if (ret < 0) {
734 ret = LTTNG_ERR_KERN_START_FAIL;
735 goto error;
736 }
737
738 /* Quiescent wait after starting trace */
739 kernel_wait_quiescent(kernel_tracer_fd);
740
741 ksess->started = 1;
742
743 ret = LTTNG_OK;
744
745 error:
746 return ret;
747 }
748
749 /*
750 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
751 */
752 int cmd_disable_channel(struct ltt_session *session, int domain,
753 char *channel_name)
754 {
755 int ret;
756 struct ltt_ust_session *usess;
757
758 usess = session->ust_session;
759
760 switch (domain) {
761 case LTTNG_DOMAIN_KERNEL:
762 {
763 ret = channel_kernel_disable(session->kernel_session,
764 channel_name);
765 if (ret != LTTNG_OK) {
766 goto error;
767 }
768
769 kernel_wait_quiescent(kernel_tracer_fd);
770 break;
771 }
772 case LTTNG_DOMAIN_UST:
773 {
774 struct ltt_ust_channel *uchan;
775 struct lttng_ht *chan_ht;
776
777 chan_ht = usess->domain_global.channels;
778
779 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
780 if (uchan == NULL) {
781 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
782 goto error;
783 }
784
785 ret = channel_ust_disable(usess, domain, uchan);
786 if (ret != LTTNG_OK) {
787 goto error;
788 }
789 break;
790 }
791 #if 0
792 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
793 case LTTNG_DOMAIN_UST_EXEC_NAME:
794 case LTTNG_DOMAIN_UST_PID:
795 #endif
796 default:
797 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
798 goto error;
799 }
800
801 ret = LTTNG_OK;
802
803 error:
804 return ret;
805 }
806
807 /*
808 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
809 *
810 * The wpipe arguments is used as a notifier for the kernel thread.
811 */
812 int cmd_enable_channel(struct ltt_session *session,
813 int domain, struct lttng_channel *attr, int wpipe)
814 {
815 int ret;
816 struct ltt_ust_session *usess = session->ust_session;
817 struct lttng_ht *chan_ht;
818
819 assert(session);
820 assert(attr);
821
822 DBG("Enabling channel %s for session %s", attr->name, session->name);
823
824 switch (domain) {
825 case LTTNG_DOMAIN_KERNEL:
826 {
827 struct ltt_kernel_channel *kchan;
828
829 /* Mandatory for a kernel channel. */
830 assert(wpipe > 0);
831
832 kchan = trace_kernel_get_channel_by_name(attr->name,
833 session->kernel_session);
834 if (kchan == NULL) {
835 ret = channel_kernel_create(session->kernel_session, attr, wpipe);
836 } else {
837 ret = channel_kernel_enable(session->kernel_session, kchan);
838 }
839
840 if (ret != LTTNG_OK) {
841 goto error;
842 }
843
844 kernel_wait_quiescent(kernel_tracer_fd);
845
846 /*
847 * If the session was previously started, start as well this newly
848 * created kernel session so the events/channels enabled *after* the
849 * start actually work.
850 */
851 if (session->started && !session->kernel_session->started) {
852 ret = start_kernel_session(session->kernel_session, wpipe);
853 if (ret != LTTNG_OK) {
854 goto error;
855 }
856 }
857 break;
858 }
859 case LTTNG_DOMAIN_UST:
860 {
861 struct ltt_ust_channel *uchan;
862
863 chan_ht = usess->domain_global.channels;
864
865 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
866 if (uchan == NULL) {
867 ret = channel_ust_create(usess, domain, attr);
868 } else {
869 ret = channel_ust_enable(usess, domain, uchan);
870 }
871
872 /* Start the UST session if the session was already started. */
873 if (session->started && !usess->start_trace) {
874 ret = ust_app_start_trace_all(usess);
875 if (ret < 0) {
876 ret = LTTNG_ERR_UST_START_FAIL;
877 goto error;
878 }
879 ret = LTTNG_OK;
880 usess->start_trace = 1;
881 }
882 break;
883 }
884 #if 0
885 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
886 case LTTNG_DOMAIN_UST_EXEC_NAME:
887 case LTTNG_DOMAIN_UST_PID:
888 #endif
889 default:
890 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
891 goto error;
892 }
893
894 error:
895 return ret;
896 }
897
898
899 /*
900 * Command LTTNG_DISABLE_EVENT processed by the client thread.
901 */
902 int cmd_disable_event(struct ltt_session *session, int domain,
903 char *channel_name, char *event_name)
904 {
905 int ret;
906
907 switch (domain) {
908 case LTTNG_DOMAIN_KERNEL:
909 {
910 struct ltt_kernel_channel *kchan;
911 struct ltt_kernel_session *ksess;
912
913 ksess = session->kernel_session;
914
915 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
916 if (kchan == NULL) {
917 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
918 goto error;
919 }
920
921 ret = event_kernel_disable_tracepoint(ksess, kchan, event_name);
922 if (ret != LTTNG_OK) {
923 goto error;
924 }
925
926 kernel_wait_quiescent(kernel_tracer_fd);
927 break;
928 }
929 case LTTNG_DOMAIN_UST:
930 {
931 struct ltt_ust_channel *uchan;
932 struct ltt_ust_session *usess;
933
934 usess = session->ust_session;
935
936 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
937 channel_name);
938 if (uchan == NULL) {
939 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
940 goto error;
941 }
942
943 ret = event_ust_disable_tracepoint(usess, domain, uchan, event_name);
944 if (ret != LTTNG_OK) {
945 goto error;
946 }
947
948 DBG3("Disable UST event %s in channel %s completed", event_name,
949 channel_name);
950 break;
951 }
952 #if 0
953 case LTTNG_DOMAIN_UST_EXEC_NAME:
954 case LTTNG_DOMAIN_UST_PID:
955 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
956 #endif
957 default:
958 ret = LTTNG_ERR_UND;
959 goto error;
960 }
961
962 ret = LTTNG_OK;
963
964 error:
965 return ret;
966 }
967
968 /*
969 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
970 */
971 int cmd_disable_event_all(struct ltt_session *session, int domain,
972 char *channel_name)
973 {
974 int ret;
975
976 switch (domain) {
977 case LTTNG_DOMAIN_KERNEL:
978 {
979 struct ltt_kernel_session *ksess;
980 struct ltt_kernel_channel *kchan;
981
982 ksess = session->kernel_session;
983
984 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
985 if (kchan == NULL) {
986 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
987 goto error;
988 }
989
990 ret = event_kernel_disable_all(ksess, kchan);
991 if (ret != LTTNG_OK) {
992 goto error;
993 }
994
995 kernel_wait_quiescent(kernel_tracer_fd);
996 break;
997 }
998 case LTTNG_DOMAIN_UST:
999 {
1000 struct ltt_ust_session *usess;
1001 struct ltt_ust_channel *uchan;
1002
1003 usess = session->ust_session;
1004
1005 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1006 channel_name);
1007 if (uchan == NULL) {
1008 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1009 goto error;
1010 }
1011
1012 ret = event_ust_disable_all_tracepoints(usess, domain, uchan);
1013 if (ret != 0) {
1014 goto error;
1015 }
1016
1017 DBG3("Disable all UST events in channel %s completed", channel_name);
1018
1019 break;
1020 }
1021 #if 0
1022 case LTTNG_DOMAIN_UST_EXEC_NAME:
1023 case LTTNG_DOMAIN_UST_PID:
1024 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1025 #endif
1026 default:
1027 ret = LTTNG_ERR_UND;
1028 goto error;
1029 }
1030
1031 ret = LTTNG_OK;
1032
1033 error:
1034 return ret;
1035 }
1036
1037 /*
1038 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1039 */
1040 int cmd_add_context(struct ltt_session *session, int domain,
1041 char *channel_name, char *event_name, struct lttng_event_context *ctx,
1042 int kwpipe)
1043 {
1044 int ret;
1045
1046 switch (domain) {
1047 case LTTNG_DOMAIN_KERNEL:
1048 assert(session->kernel_session);
1049
1050 if (session->kernel_session->channel_count == 0) {
1051 /* Create default channel */
1052 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1053 if (ret != LTTNG_OK) {
1054 goto error;
1055 }
1056 }
1057
1058 /* Add kernel context to kernel tracer */
1059 ret = context_kernel_add(session->kernel_session, ctx,
1060 event_name, channel_name);
1061 if (ret != LTTNG_OK) {
1062 goto error;
1063 }
1064 break;
1065 case LTTNG_DOMAIN_UST:
1066 {
1067 struct ltt_ust_session *usess = session->ust_session;
1068 assert(usess);
1069
1070 unsigned int chan_count =
1071 lttng_ht_get_count(usess->domain_global.channels);
1072 if (chan_count == 0) {
1073 struct lttng_channel *attr;
1074 /* Create default channel */
1075 attr = channel_new_default_attr(domain);
1076 if (attr == NULL) {
1077 ret = LTTNG_ERR_FATAL;
1078 goto error;
1079 }
1080
1081 ret = channel_ust_create(usess, domain, attr);
1082 if (ret != LTTNG_OK) {
1083 free(attr);
1084 goto error;
1085 }
1086 free(attr);
1087 }
1088
1089
1090 ret = context_ust_add(usess, domain, ctx, event_name, channel_name);
1091 if (ret != LTTNG_OK) {
1092 goto error;
1093 }
1094 break;
1095 }
1096 #if 0
1097 case LTTNG_DOMAIN_UST_EXEC_NAME:
1098 case LTTNG_DOMAIN_UST_PID:
1099 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1100 #endif
1101 default:
1102 ret = LTTNG_ERR_UND;
1103 goto error;
1104 }
1105
1106 ret = LTTNG_OK;
1107
1108 error:
1109 return ret;
1110 }
1111
1112 /*
1113 * Command LTTNG_SET_FILTER processed by the client thread.
1114 */
1115 int cmd_set_filter(struct ltt_session *session, int domain,
1116 char *channel_name, char *event_name,
1117 struct lttng_filter_bytecode *bytecode)
1118 {
1119 int ret;
1120
1121 switch (domain) {
1122 case LTTNG_DOMAIN_KERNEL:
1123 ret = LTTNG_ERR_FATAL;
1124 break;
1125 case LTTNG_DOMAIN_UST:
1126 {
1127 struct ltt_ust_session *usess = session->ust_session;
1128
1129 ret = filter_ust_set(usess, domain, bytecode, event_name, channel_name);
1130 if (ret != LTTNG_OK) {
1131 goto error;
1132 }
1133 break;
1134 }
1135 #if 0
1136 case LTTNG_DOMAIN_UST_EXEC_NAME:
1137 case LTTNG_DOMAIN_UST_PID:
1138 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1139 #endif
1140 default:
1141 ret = LTTNG_ERR_UND;
1142 goto error;
1143 }
1144
1145 ret = LTTNG_OK;
1146
1147 error:
1148 return ret;
1149
1150 }
1151
1152
1153 /*
1154 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1155 */
1156 int cmd_enable_event(struct ltt_session *session, int domain,
1157 char *channel_name, struct lttng_event *event, int wpipe)
1158 {
1159 int ret;
1160 struct lttng_channel *attr;
1161
1162 assert(session);
1163 assert(event);
1164 assert(channel_name);
1165
1166 switch (domain) {
1167 case LTTNG_DOMAIN_KERNEL:
1168 {
1169 struct ltt_kernel_channel *kchan;
1170
1171 kchan = trace_kernel_get_channel_by_name(channel_name,
1172 session->kernel_session);
1173 if (kchan == NULL) {
1174 attr = channel_new_default_attr(domain);
1175 if (attr == NULL) {
1176 ret = LTTNG_ERR_FATAL;
1177 goto error;
1178 }
1179 strncpy(attr->name, channel_name, sizeof(attr->name));
1180
1181 ret = cmd_enable_channel(session, domain, attr, wpipe);
1182 if (ret != LTTNG_OK) {
1183 free(attr);
1184 goto error;
1185 }
1186 free(attr);
1187 }
1188
1189 /* Get the newly created kernel channel pointer */
1190 kchan = trace_kernel_get_channel_by_name(channel_name,
1191 session->kernel_session);
1192 if (kchan == NULL) {
1193 /* This sould not happen... */
1194 ret = LTTNG_ERR_FATAL;
1195 goto error;
1196 }
1197
1198 ret = event_kernel_enable_tracepoint(session->kernel_session, kchan,
1199 event);
1200 if (ret != LTTNG_OK) {
1201 goto error;
1202 }
1203
1204 kernel_wait_quiescent(kernel_tracer_fd);
1205 break;
1206 }
1207 case LTTNG_DOMAIN_UST:
1208 {
1209 struct ltt_ust_channel *uchan;
1210 struct ltt_ust_session *usess = session->ust_session;
1211
1212 assert(usess);
1213
1214 /* Get channel from global UST domain */
1215 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1216 channel_name);
1217 if (uchan == NULL) {
1218 /* Create default channel */
1219 attr = channel_new_default_attr(domain);
1220 if (attr == NULL) {
1221 ret = LTTNG_ERR_FATAL;
1222 goto error;
1223 }
1224 strncpy(attr->name, channel_name, sizeof(attr->name));
1225
1226 ret = cmd_enable_channel(session, domain, attr, wpipe);
1227 if (ret != LTTNG_OK) {
1228 free(attr);
1229 goto error;
1230 }
1231 free(attr);
1232
1233 /* Get the newly created channel reference back */
1234 uchan = trace_ust_find_channel_by_name(
1235 usess->domain_global.channels, channel_name);
1236 assert(uchan);
1237 }
1238
1239 /* At this point, the session and channel exist on the tracer */
1240 ret = event_ust_enable_tracepoint(usess, domain, uchan, event);
1241 if (ret != LTTNG_OK) {
1242 goto error;
1243 }
1244 break;
1245 }
1246 #if 0
1247 case LTTNG_DOMAIN_UST_EXEC_NAME:
1248 case LTTNG_DOMAIN_UST_PID:
1249 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1250 #endif
1251 default:
1252 ret = LTTNG_ERR_UND;
1253 goto error;
1254 }
1255
1256 ret = LTTNG_OK;
1257
1258 error:
1259 return ret;
1260 }
1261
1262 /*
1263 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1264 */
1265 int cmd_enable_event_all(struct ltt_session *session, int domain,
1266 char *channel_name, int event_type, int wpipe)
1267 {
1268 int ret;
1269 struct lttng_channel *attr;
1270
1271 assert(session);
1272 assert(channel_name);
1273
1274 switch (domain) {
1275 case LTTNG_DOMAIN_KERNEL:
1276 {
1277 struct ltt_kernel_channel *kchan;
1278
1279 assert(session->kernel_session);
1280
1281 kchan = trace_kernel_get_channel_by_name(channel_name,
1282 session->kernel_session);
1283 if (kchan == NULL) {
1284 /* Create default channel */
1285 attr = channel_new_default_attr(domain);
1286 if (attr == NULL) {
1287 ret = LTTNG_ERR_FATAL;
1288 goto error;
1289 }
1290 strncpy(attr->name, channel_name, sizeof(attr->name));
1291
1292 ret = cmd_enable_channel(session, domain, attr, wpipe);
1293 if (ret != LTTNG_OK) {
1294 free(attr);
1295 goto error;
1296 }
1297 free(attr);
1298
1299 /* Get the newly created kernel channel pointer */
1300 kchan = trace_kernel_get_channel_by_name(channel_name,
1301 session->kernel_session);
1302 assert(kchan);
1303 }
1304
1305 switch (event_type) {
1306 case LTTNG_EVENT_SYSCALL:
1307 ret = event_kernel_enable_all_syscalls(session->kernel_session,
1308 kchan, kernel_tracer_fd);
1309 break;
1310 case LTTNG_EVENT_TRACEPOINT:
1311 /*
1312 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1313 * events already registered to the channel.
1314 */
1315 ret = event_kernel_enable_all_tracepoints(session->kernel_session,
1316 kchan, kernel_tracer_fd);
1317 break;
1318 case LTTNG_EVENT_ALL:
1319 /* Enable syscalls and tracepoints */
1320 ret = event_kernel_enable_all(session->kernel_session,
1321 kchan, kernel_tracer_fd);
1322 break;
1323 default:
1324 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
1325 goto error;
1326 }
1327
1328 /* Manage return value */
1329 if (ret != LTTNG_OK) {
1330 goto error;
1331 }
1332
1333 kernel_wait_quiescent(kernel_tracer_fd);
1334 break;
1335 }
1336 case LTTNG_DOMAIN_UST:
1337 {
1338 struct ltt_ust_channel *uchan;
1339 struct ltt_ust_session *usess = session->ust_session;
1340
1341 assert(usess);
1342
1343 /* Get channel from global UST domain */
1344 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1345 channel_name);
1346 if (uchan == NULL) {
1347 /* Create default channel */
1348 attr = channel_new_default_attr(domain);
1349 if (attr == NULL) {
1350 ret = LTTNG_ERR_FATAL;
1351 goto error;
1352 }
1353 strncpy(attr->name, channel_name, sizeof(attr->name));
1354
1355 ret = cmd_enable_channel(session, domain, attr, wpipe);
1356 if (ret != LTTNG_OK) {
1357 free(attr);
1358 goto error;
1359 }
1360 free(attr);
1361
1362 /* Get the newly created channel reference back */
1363 uchan = trace_ust_find_channel_by_name(
1364 usess->domain_global.channels, channel_name);
1365 assert(uchan);
1366 }
1367
1368 /* At this point, the session and channel exist on the tracer */
1369
1370 switch (event_type) {
1371 case LTTNG_EVENT_ALL:
1372 case LTTNG_EVENT_TRACEPOINT:
1373 ret = event_ust_enable_all_tracepoints(usess, domain, uchan);
1374 if (ret != LTTNG_OK) {
1375 goto error;
1376 }
1377 break;
1378 default:
1379 ret = LTTNG_ERR_UST_ENABLE_FAIL;
1380 goto error;
1381 }
1382
1383 /* Manage return value */
1384 if (ret != LTTNG_OK) {
1385 goto error;
1386 }
1387
1388 break;
1389 }
1390 #if 0
1391 case LTTNG_DOMAIN_UST_EXEC_NAME:
1392 case LTTNG_DOMAIN_UST_PID:
1393 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1394 #endif
1395 default:
1396 ret = LTTNG_ERR_UND;
1397 goto error;
1398 }
1399
1400 ret = LTTNG_OK;
1401
1402 error:
1403 return ret;
1404 }
1405
1406
1407 /*
1408 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1409 */
1410 ssize_t cmd_list_tracepoints(int domain, struct lttng_event **events)
1411 {
1412 int ret;
1413 ssize_t nb_events = 0;
1414
1415 switch (domain) {
1416 case LTTNG_DOMAIN_KERNEL:
1417 nb_events = kernel_list_events(kernel_tracer_fd, events);
1418 if (nb_events < 0) {
1419 ret = LTTNG_ERR_KERN_LIST_FAIL;
1420 goto error;
1421 }
1422 break;
1423 case LTTNG_DOMAIN_UST:
1424 nb_events = ust_app_list_events(events);
1425 if (nb_events < 0) {
1426 ret = LTTNG_ERR_UST_LIST_FAIL;
1427 goto error;
1428 }
1429 break;
1430 default:
1431 ret = LTTNG_ERR_UND;
1432 goto error;
1433 }
1434
1435 return nb_events;
1436
1437 error:
1438 /* Return negative value to differentiate return code */
1439 return -ret;
1440 }
1441
1442 /*
1443 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1444 */
1445 ssize_t cmd_list_tracepoint_fields(int domain,
1446 struct lttng_event_field **fields)
1447 {
1448 int ret;
1449 ssize_t nb_fields = 0;
1450
1451 switch (domain) {
1452 case LTTNG_DOMAIN_UST:
1453 nb_fields = ust_app_list_event_fields(fields);
1454 if (nb_fields < 0) {
1455 ret = LTTNG_ERR_UST_LIST_FAIL;
1456 goto error;
1457 }
1458 break;
1459 case LTTNG_DOMAIN_KERNEL:
1460 default: /* fall-through */
1461 ret = LTTNG_ERR_UND;
1462 goto error;
1463 }
1464
1465 return nb_fields;
1466
1467 error:
1468 /* Return negative value to differentiate return code */
1469 return -ret;
1470 }
1471
1472 /*
1473 * Command LTTNG_START_TRACE processed by the client thread.
1474 */
1475 int cmd_start_trace(struct ltt_session *session)
1476 {
1477 int ret;
1478 struct ltt_kernel_session *ksession;
1479 struct ltt_ust_session *usess;
1480
1481 assert(session);
1482
1483 /* Ease our life a bit ;) */
1484 ksession = session->kernel_session;
1485 usess = session->ust_session;
1486
1487 if (session->enabled) {
1488 /* Already started. */
1489 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1490 goto error;
1491 }
1492
1493 session->enabled = 1;
1494
1495 ret = setup_relayd(session);
1496 if (ret != LTTNG_OK) {
1497 ERR("Error setting up relayd for session %s", session->name);
1498 goto error;
1499 }
1500
1501 /* Kernel tracing */
1502 if (ksession != NULL) {
1503 ret = start_kernel_session(ksession, kernel_tracer_fd);
1504 if (ret != LTTNG_OK) {
1505 goto error;
1506 }
1507 }
1508
1509 /* Flag session that trace should start automatically */
1510 if (usess) {
1511 usess->start_trace = 1;
1512
1513 ret = ust_app_start_trace_all(usess);
1514 if (ret < 0) {
1515 ret = LTTNG_ERR_UST_START_FAIL;
1516 goto error;
1517 }
1518 }
1519
1520 session->started = 1;
1521
1522 ret = LTTNG_OK;
1523
1524 error:
1525 return ret;
1526 }
1527
1528 /*
1529 * Command LTTNG_STOP_TRACE processed by the client thread.
1530 */
1531 int cmd_stop_trace(struct ltt_session *session)
1532 {
1533 int ret;
1534 struct ltt_kernel_channel *kchan;
1535 struct ltt_kernel_session *ksession;
1536 struct ltt_ust_session *usess;
1537
1538 assert(session);
1539
1540 /* Short cut */
1541 ksession = session->kernel_session;
1542 usess = session->ust_session;
1543
1544 if (!session->enabled) {
1545 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
1546 goto error;
1547 }
1548
1549 session->enabled = 0;
1550
1551 /* Kernel tracer */
1552 if (ksession) {
1553 DBG("Stop kernel tracing");
1554
1555 /* Flush metadata if exist */
1556 if (ksession->metadata_stream_fd >= 0) {
1557 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
1558 if (ret < 0) {
1559 ERR("Kernel metadata flush failed");
1560 }
1561 }
1562
1563 /* Flush all buffers before stopping */
1564 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
1565 ret = kernel_flush_buffer(kchan);
1566 if (ret < 0) {
1567 ERR("Kernel flush buffer error");
1568 }
1569 }
1570
1571 ret = kernel_stop_session(ksession);
1572 if (ret < 0) {
1573 ret = LTTNG_ERR_KERN_STOP_FAIL;
1574 goto error;
1575 }
1576
1577 kernel_wait_quiescent(kernel_tracer_fd);
1578
1579 ksession->started = 0;
1580 }
1581
1582 if (usess) {
1583 usess->start_trace = 0;
1584
1585 ret = ust_app_stop_trace_all(usess);
1586 if (ret < 0) {
1587 ret = LTTNG_ERR_UST_STOP_FAIL;
1588 goto error;
1589 }
1590 }
1591
1592 session->started = 0;
1593
1594 ret = LTTNG_OK;
1595
1596 error:
1597 return ret;
1598 }
1599
1600 /*
1601 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1602 */
1603 int cmd_set_consumer_uri(int domain, struct ltt_session *session,
1604 size_t nb_uri, struct lttng_uri *uris)
1605 {
1606 int ret, i;
1607 struct ltt_kernel_session *ksess = session->kernel_session;
1608 struct ltt_ust_session *usess = session->ust_session;
1609 struct consumer_output *consumer = NULL;
1610
1611 assert(session);
1612 assert(uris);
1613 assert(nb_uri > 0);
1614
1615 /* Can't enable consumer after session started. */
1616 if (session->enabled) {
1617 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1618 goto error;
1619 }
1620
1621 /*
1622 * This case switch makes sure the domain session has a temporary consumer
1623 * so the URL can be set.
1624 */
1625 switch (domain) {
1626 case 0:
1627 /* Code flow error. A session MUST always have a consumer object */
1628 assert(session->consumer);
1629 /*
1630 * The URL will be added to the tracing session consumer instead of a
1631 * specific domain consumer.
1632 */
1633 consumer = session->consumer;
1634 break;
1635 case LTTNG_DOMAIN_KERNEL:
1636 /* Code flow error if we don't have a kernel session here. */
1637 assert(ksess);
1638
1639 /* Create consumer output if none exists */
1640 consumer = ksess->tmp_consumer;
1641 if (consumer == NULL) {
1642 consumer = consumer_copy_output(ksess->consumer);
1643 if (consumer == NULL) {
1644 ret = LTTNG_ERR_FATAL;
1645 goto error;
1646 }
1647 /* Trash the consumer subdir, we are about to set a new one. */
1648 memset(consumer->subdir, 0, sizeof(consumer->subdir));
1649 ksess->tmp_consumer = consumer;
1650 }
1651
1652 break;
1653 case LTTNG_DOMAIN_UST:
1654 /* Code flow error if we don't have a kernel session here. */
1655 assert(usess);
1656
1657 /* Create consumer output if none exists */
1658 consumer = usess->tmp_consumer;
1659 if (consumer == NULL) {
1660 consumer = consumer_copy_output(usess->consumer);
1661 if (consumer == NULL) {
1662 ret = LTTNG_ERR_FATAL;
1663 goto error;
1664 }
1665 /* Trash the consumer subdir, we are about to set a new one. */
1666 memset(consumer->subdir, 0, sizeof(consumer->subdir));
1667 usess->tmp_consumer = consumer;
1668 }
1669
1670 break;
1671 }
1672
1673 for (i = 0; i < nb_uri; i++) {
1674 struct consumer_socket *socket;
1675 struct lttng_ht_iter iter;
1676
1677 ret = add_uri_to_consumer(consumer, &uris[i], domain, session->name);
1678 if (ret < 0) {
1679 goto error;
1680 }
1681
1682 /*
1683 * Don't send relayd socket if URI is NOT remote or if the relayd
1684 * socket for the session was already sent.
1685 */
1686 if (uris[i].dtype == LTTNG_DST_PATH ||
1687 (uris[i].stype == LTTNG_STREAM_CONTROL &&
1688 consumer->dst.net.control_sock_sent) ||
1689 (uris[i].stype == LTTNG_STREAM_DATA &&
1690 consumer->dst.net.data_sock_sent)) {
1691 continue;
1692 }
1693
1694 /* Try to send relayd URI to the consumer if exist. */
1695 rcu_read_lock();
1696 cds_lfht_for_each_entry(consumer->socks->ht, &iter.iter,
1697 socket, node.node) {
1698
1699 /* A socket in the HT should never have a negative fd */
1700 assert(socket->fd >= 0);
1701
1702 pthread_mutex_lock(socket->lock);
1703 ret = send_consumer_relayd_socket(domain, session, &uris[i],
1704 consumer, socket->fd);
1705 pthread_mutex_unlock(socket->lock);
1706 if (ret != LTTNG_OK) {
1707 rcu_read_unlock();
1708 goto error;
1709 }
1710 }
1711 rcu_read_unlock();
1712 }
1713
1714 /* All good! */
1715 ret = LTTNG_OK;
1716
1717 error:
1718 return ret;
1719 }
1720
1721 /*
1722 * Command LTTNG_CREATE_SESSION processed by the client thread.
1723 */
1724 int cmd_create_session_uri(char *name, struct lttng_uri *uris,
1725 size_t nb_uri, lttng_sock_cred *creds)
1726 {
1727 int ret;
1728 char *path = NULL;
1729 struct ltt_session *session;
1730
1731 assert(name);
1732
1733 /*
1734 * Verify if the session already exist
1735 *
1736 * XXX: There is no need for the session lock list here since the caller
1737 * (process_client_msg) is holding it. We might want to change that so a
1738 * single command does not lock the entire session list.
1739 */
1740 session = session_find_by_name(name);
1741 if (session != NULL) {
1742 ret = LTTNG_ERR_EXIST_SESS;
1743 goto find_error;
1744 }
1745
1746 /* Create tracing session in the registry */
1747 ret = session_create(name, path, LTTNG_SOCK_GET_UID_CRED(creds),
1748 LTTNG_SOCK_GET_GID_CRED(creds));
1749 if (ret != LTTNG_OK) {
1750 goto session_error;
1751 }
1752
1753 /*
1754 * Get the newly created session pointer back
1755 *
1756 * XXX: There is no need for the session lock list here since the caller
1757 * (process_client_msg) is holding it. We might want to change that so a
1758 * single command does not lock the entire session list.
1759 */
1760 session = session_find_by_name(name);
1761 assert(session);
1762
1763 /* Create default consumer output for the session not yet created. */
1764 session->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
1765 if (session->consumer == NULL) {
1766 ret = LTTNG_ERR_FATAL;
1767 goto consumer_error;
1768 }
1769
1770 /*
1771 * This means that the lttng_create_session call was called with the _path_
1772 * argument set to NULL.
1773 */
1774 if (uris == NULL) {
1775 /*
1776 * At this point, we'll skip the consumer URI setup and create a
1777 * session with a NULL path which will flag the session to NOT spawn a
1778 * consumer.
1779 */
1780 DBG("Create session %s with NO uri, skipping consumer setup", name);
1781 goto end;
1782 }
1783
1784 session->start_consumer = 1;
1785
1786 ret = cmd_set_consumer_uri(0, session, nb_uri, uris);
1787 if (ret != LTTNG_OK) {
1788 goto consumer_error;
1789 }
1790
1791 session->consumer->enabled = 1;
1792
1793 end:
1794 return LTTNG_OK;
1795
1796 consumer_error:
1797 session_destroy(session);
1798 session_error:
1799 find_error:
1800 return ret;
1801 }
1802
1803 /*
1804 * Command LTTNG_DESTROY_SESSION processed by the client thread.
1805 */
1806 int cmd_destroy_session(struct ltt_session *session, int wpipe)
1807 {
1808 int ret;
1809 struct ltt_ust_session *usess;
1810 struct ltt_kernel_session *ksess;
1811
1812 /* Safety net */
1813 assert(session);
1814
1815 usess = session->ust_session;
1816 ksess = session->kernel_session;
1817
1818 /* Clean kernel session teardown */
1819 kernel_destroy_session(ksess);
1820
1821 /* UST session teardown */
1822 if (usess) {
1823 /* Close any relayd session */
1824 consumer_output_send_destroy_relayd(usess->consumer);
1825
1826 /* Destroy every UST application related to this session. */
1827 ret = ust_app_destroy_trace_all(usess);
1828 if (ret) {
1829 ERR("Error in ust_app_destroy_trace_all");
1830 }
1831
1832 /* Clean up the rest. */
1833 trace_ust_destroy_session(usess);
1834 }
1835
1836 /*
1837 * Must notify the kernel thread here to update it's poll set in order to
1838 * remove the channel(s)' fd just destroyed.
1839 */
1840 ret = notify_thread_pipe(wpipe);
1841 if (ret < 0) {
1842 PERROR("write kernel poll pipe");
1843 }
1844
1845 ret = session_destroy(session);
1846
1847 return ret;
1848 }
1849
1850 /*
1851 * Command LTTNG_CALIBRATE processed by the client thread.
1852 */
1853 int cmd_calibrate(int domain, struct lttng_calibrate *calibrate)
1854 {
1855 int ret;
1856
1857 switch (domain) {
1858 case LTTNG_DOMAIN_KERNEL:
1859 {
1860 struct lttng_kernel_calibrate kcalibrate;
1861
1862 kcalibrate.type = calibrate->type;
1863 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
1864 if (ret < 0) {
1865 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
1866 goto error;
1867 }
1868 break;
1869 }
1870 case LTTNG_DOMAIN_UST:
1871 {
1872 struct lttng_ust_calibrate ucalibrate;
1873
1874 ucalibrate.type = calibrate->type;
1875 ret = ust_app_calibrate_glb(&ucalibrate);
1876 if (ret < 0) {
1877 ret = LTTNG_ERR_UST_CALIBRATE_FAIL;
1878 goto error;
1879 }
1880 break;
1881 }
1882 default:
1883 ret = LTTNG_ERR_UND;
1884 goto error;
1885 }
1886
1887 ret = LTTNG_OK;
1888
1889 error:
1890 return ret;
1891 }
1892
1893 /*
1894 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
1895 */
1896 int cmd_register_consumer(struct ltt_session *session, int domain,
1897 const char *sock_path, struct consumer_data *cdata)
1898 {
1899 int ret, sock;
1900 struct consumer_socket *socket;
1901
1902 assert(session);
1903 assert(cdata);
1904 assert(sock_path);
1905
1906 switch (domain) {
1907 case LTTNG_DOMAIN_KERNEL:
1908 {
1909 struct ltt_kernel_session *ksess = session->kernel_session;
1910
1911 assert(ksess);
1912
1913 /* Can't register a consumer if there is already one */
1914 if (ksess->consumer_fds_sent != 0) {
1915 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1916 goto error;
1917 }
1918
1919 sock = lttcomm_connect_unix_sock(sock_path);
1920 if (sock < 0) {
1921 ret = LTTNG_ERR_CONNECT_FAIL;
1922 goto error;
1923 }
1924
1925 socket = consumer_allocate_socket(sock);
1926 if (socket == NULL) {
1927 ret = LTTNG_ERR_FATAL;
1928 close(sock);
1929 goto error;
1930 }
1931
1932 socket->lock = zmalloc(sizeof(pthread_mutex_t));
1933 if (socket->lock == NULL) {
1934 PERROR("zmalloc pthread mutex");
1935 ret = LTTNG_ERR_FATAL;
1936 goto error;
1937 }
1938 pthread_mutex_init(socket->lock, NULL);
1939 socket->registered = 1;
1940
1941 rcu_read_lock();
1942 consumer_add_socket(socket, ksess->consumer);
1943 rcu_read_unlock();
1944
1945 pthread_mutex_lock(&cdata->pid_mutex);
1946 cdata->pid = -1;
1947 pthread_mutex_unlock(&cdata->pid_mutex);
1948
1949 break;
1950 }
1951 default:
1952 /* TODO: Userspace tracing */
1953 ret = LTTNG_ERR_UND;
1954 goto error;
1955 }
1956
1957 ret = LTTNG_OK;
1958
1959 error:
1960 return ret;
1961 }
1962
1963 /*
1964 * Command LTTNG_LIST_DOMAINS processed by the client thread.
1965 */
1966 ssize_t cmd_list_domains(struct ltt_session *session,
1967 struct lttng_domain **domains)
1968 {
1969 int ret, index = 0;
1970 ssize_t nb_dom = 0;
1971
1972 if (session->kernel_session != NULL) {
1973 DBG3("Listing domains found kernel domain");
1974 nb_dom++;
1975 }
1976
1977 if (session->ust_session != NULL) {
1978 DBG3("Listing domains found UST global domain");
1979 nb_dom++;
1980 }
1981
1982 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
1983 if (*domains == NULL) {
1984 ret = LTTNG_ERR_FATAL;
1985 goto error;
1986 }
1987
1988 if (session->kernel_session != NULL) {
1989 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
1990 index++;
1991 }
1992
1993 if (session->ust_session != NULL) {
1994 (*domains)[index].type = LTTNG_DOMAIN_UST;
1995 index++;
1996 }
1997
1998 return nb_dom;
1999
2000 error:
2001 /* Return negative value to differentiate return code */
2002 return -ret;
2003 }
2004
2005
2006 /*
2007 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2008 */
2009 ssize_t cmd_list_channels(int domain, struct ltt_session *session,
2010 struct lttng_channel **channels)
2011 {
2012 int ret;
2013 ssize_t nb_chan = 0;
2014
2015 switch (domain) {
2016 case LTTNG_DOMAIN_KERNEL:
2017 if (session->kernel_session != NULL) {
2018 nb_chan = session->kernel_session->channel_count;
2019 }
2020 DBG3("Number of kernel channels %zd", nb_chan);
2021 if (nb_chan <= 0) {
2022 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
2023 }
2024 break;
2025 case LTTNG_DOMAIN_UST:
2026 if (session->ust_session != NULL) {
2027 nb_chan = lttng_ht_get_count(
2028 session->ust_session->domain_global.channels);
2029 }
2030 DBG3("Number of UST global channels %zd", nb_chan);
2031 if (nb_chan <= 0) {
2032 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2033 }
2034 break;
2035 default:
2036 *channels = NULL;
2037 ret = LTTNG_ERR_UND;
2038 goto error;
2039 }
2040
2041 if (nb_chan > 0) {
2042 *channels = zmalloc(nb_chan * sizeof(struct lttng_channel));
2043 if (*channels == NULL) {
2044 ret = LTTNG_ERR_FATAL;
2045 goto error;
2046 }
2047
2048 list_lttng_channels(domain, session, *channels);
2049 } else {
2050 *channels = NULL;
2051 /* Ret value was set in the domain switch case */
2052 goto error;
2053 }
2054
2055 return nb_chan;
2056
2057 error:
2058 /* Return negative value to differentiate return code */
2059 return -ret;
2060 }
2061
2062 /*
2063 * Command LTTNG_LIST_EVENTS processed by the client thread.
2064 */
2065 ssize_t cmd_list_events(int domain, struct ltt_session *session,
2066 char *channel_name, struct lttng_event **events)
2067 {
2068 int ret = 0;
2069 ssize_t nb_event = 0;
2070
2071 switch (domain) {
2072 case LTTNG_DOMAIN_KERNEL:
2073 if (session->kernel_session != NULL) {
2074 nb_event = list_lttng_kernel_events(channel_name,
2075 session->kernel_session, events);
2076 }
2077 break;
2078 case LTTNG_DOMAIN_UST:
2079 {
2080 if (session->ust_session != NULL) {
2081 nb_event = list_lttng_ust_global_events(channel_name,
2082 &session->ust_session->domain_global, events);
2083 }
2084 break;
2085 }
2086 default:
2087 ret = LTTNG_ERR_UND;
2088 goto error;
2089 }
2090
2091 return nb_event;
2092
2093 error:
2094 /* Return negative value to differentiate return code */
2095 return -ret;
2096 }
2097
2098 /*
2099 * Using the session list, filled a lttng_session array to send back to the
2100 * client for session listing.
2101 *
2102 * The session list lock MUST be acquired before calling this function. Use
2103 * session_lock_list() and session_unlock_list().
2104 */
2105 void cmd_list_lttng_sessions(struct lttng_session *sessions, uid_t uid,
2106 gid_t gid)
2107 {
2108 int ret;
2109 unsigned int i = 0;
2110 struct ltt_session *session;
2111 struct ltt_session_list *list = session_get_list();
2112
2113 DBG("Getting all available session for UID %d GID %d",
2114 uid, gid);
2115 /*
2116 * Iterate over session list and append data after the control struct in
2117 * the buffer.
2118 */
2119 cds_list_for_each_entry(session, &list->head, list) {
2120 /*
2121 * Only list the sessions the user can control.
2122 */
2123 if (!session_access_ok(session, uid, gid)) {
2124 continue;
2125 }
2126
2127 struct ltt_kernel_session *ksess = session->kernel_session;
2128 struct ltt_ust_session *usess = session->ust_session;
2129
2130 if (session->consumer->type == CONSUMER_DST_NET ||
2131 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
2132 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
2133 ret = build_network_session_path(sessions[i].path,
2134 sizeof(session[i].path), session);
2135 } else {
2136 ret = snprintf(sessions[i].path, sizeof(session[i].path), "%s",
2137 session->consumer->dst.trace_path);
2138 }
2139 if (ret < 0) {
2140 PERROR("snprintf session path");
2141 continue;
2142 }
2143
2144 strncpy(sessions[i].name, session->name, NAME_MAX);
2145 sessions[i].name[NAME_MAX - 1] = '\0';
2146 sessions[i].enabled = session->enabled;
2147 i++;
2148 }
2149 }
2150
2151 /*
2152 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
2153 */
2154 int cmd_disable_consumer(int domain, struct ltt_session *session)
2155 {
2156 int ret;
2157 struct ltt_kernel_session *ksess = session->kernel_session;
2158 struct ltt_ust_session *usess = session->ust_session;
2159 struct consumer_output *consumer;
2160
2161 assert(session);
2162
2163 if (session->enabled) {
2164 /* Can't disable consumer on an already started session */
2165 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2166 goto error;
2167 }
2168
2169 if (!session->start_consumer) {
2170 ret = LTTNG_ERR_NO_CONSUMER;
2171 goto error;
2172 }
2173
2174 switch (domain) {
2175 case 0:
2176 DBG("Disable tracing session %s consumer", session->name);
2177 consumer = session->consumer;
2178 break;
2179 case LTTNG_DOMAIN_KERNEL:
2180 /* Code flow error if we don't have a kernel session here. */
2181 assert(ksess);
2182
2183 DBG("Disabling kernel consumer");
2184 consumer = ksess->consumer;
2185
2186 break;
2187 case LTTNG_DOMAIN_UST:
2188 /* Code flow error if we don't have a UST session here. */
2189 assert(usess);
2190
2191 DBG("Disabling UST consumer");
2192 consumer = usess->consumer;
2193
2194 break;
2195 default:
2196 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2197 goto error;
2198 }
2199
2200 if (consumer) {
2201 consumer->enabled = 0;
2202 /* Success at this point */
2203 ret = LTTNG_OK;
2204 } else {
2205 ret = LTTNG_ERR_NO_CONSUMER;
2206 }
2207
2208 error:
2209 return ret;
2210 }
2211
2212 /*
2213 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
2214 */
2215 int cmd_enable_consumer(int domain, struct ltt_session *session)
2216 {
2217 int ret;
2218 struct ltt_kernel_session *ksess = session->kernel_session;
2219 struct ltt_ust_session *usess = session->ust_session;
2220 struct consumer_output *consumer = NULL;
2221
2222 assert(session);
2223
2224 /* Can't enable consumer after session started. */
2225 if (session->enabled) {
2226 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2227 goto error;
2228 }
2229
2230 switch (domain) {
2231 case 0:
2232 assert(session->consumer);
2233 consumer = session->consumer;
2234 break;
2235 case LTTNG_DOMAIN_KERNEL:
2236 /* Code flow error if we don't have a kernel session here. */
2237 assert(ksess);
2238
2239 /*
2240 * Check if we have already sent fds to the consumer. In that case,
2241 * the enable-consumer command can't be used because a start trace
2242 * had previously occured.
2243 */
2244 if (ksess->consumer_fds_sent) {
2245 ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
2246 goto error;
2247 }
2248
2249 consumer = ksess->tmp_consumer;
2250 if (consumer == NULL) {
2251 ret = LTTNG_OK;
2252 /* No temp. consumer output exists. Using the current one. */
2253 DBG3("No temporary consumer. Using default");
2254 consumer = ksess->consumer;
2255 goto error;
2256 }
2257
2258 switch (consumer->type) {
2259 case CONSUMER_DST_LOCAL:
2260 DBG2("Consumer output is local. Creating directory(ies)");
2261
2262 /* Create directory(ies) */
2263 ret = run_as_mkdir_recursive(consumer->dst.trace_path,
2264 S_IRWXU | S_IRWXG, session->uid, session->gid);
2265 if (ret < 0) {
2266 if (ret != -EEXIST) {
2267 ERR("Trace directory creation error");
2268 ret = LTTNG_ERR_FATAL;
2269 goto error;
2270 }
2271 }
2272 break;
2273 case CONSUMER_DST_NET:
2274 DBG2("Consumer output is network. Validating URIs");
2275 /* Validate if we have both control and data path set. */
2276 if (!consumer->dst.net.control_isset) {
2277 ret = LTTNG_ERR_URL_CTRL_MISS;
2278 goto error;
2279 }
2280
2281 if (!consumer->dst.net.data_isset) {
2282 ret = LTTNG_ERR_URL_DATA_MISS;
2283 goto error;
2284 }
2285
2286 /* Check established network session state */
2287 if (session->net_handle == 0) {
2288 ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
2289 ERR("Session network handle is not set on enable-consumer");
2290 goto error;
2291 }
2292
2293 break;
2294 }
2295
2296 /*
2297 * @session-lock
2298 * This is race free for now since the session lock is acquired before
2299 * ending up in this function. No other threads can access this kernel
2300 * session without this lock hence freeing the consumer output object
2301 * is valid.
2302 */
2303 rcu_read_lock();
2304 /* Destroy current consumer. We are about to replace it */
2305 consumer_destroy_output(ksess->consumer);
2306 rcu_read_unlock();
2307 ksess->consumer = consumer;
2308 ksess->tmp_consumer = NULL;
2309
2310 break;
2311 case LTTNG_DOMAIN_UST:
2312 /* Code flow error if we don't have a UST session here. */
2313 assert(usess);
2314
2315 /*
2316 * Check if we have already sent fds to the consumer. In that case,
2317 * the enable-consumer command can't be used because a start trace
2318 * had previously occured.
2319 */
2320 if (usess->start_trace) {
2321 ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
2322 goto error;
2323 }
2324
2325 consumer = usess->tmp_consumer;
2326 if (consumer == NULL) {
2327 ret = LTTNG_OK;
2328 /* No temp. consumer output exists. Using the current one. */
2329 DBG3("No temporary consumer. Using default");
2330 consumer = usess->consumer;
2331 goto error;
2332 }
2333
2334 switch (consumer->type) {
2335 case CONSUMER_DST_LOCAL:
2336 DBG2("Consumer output is local. Creating directory(ies)");
2337
2338 /* Create directory(ies) */
2339 ret = run_as_mkdir_recursive(consumer->dst.trace_path,
2340 S_IRWXU | S_IRWXG, session->uid, session->gid);
2341 if (ret < 0) {
2342 if (ret != -EEXIST) {
2343 ERR("Trace directory creation error");
2344 ret = LTTNG_ERR_FATAL;
2345 goto error;
2346 }
2347 }
2348 break;
2349 case CONSUMER_DST_NET:
2350 DBG2("Consumer output is network. Validating URIs");
2351 /* Validate if we have both control and data path set. */
2352 if (!consumer->dst.net.control_isset) {
2353 ret = LTTNG_ERR_URL_CTRL_MISS;
2354 goto error;
2355 }
2356
2357 if (!consumer->dst.net.data_isset) {
2358 ret = LTTNG_ERR_URL_DATA_MISS;
2359 goto error;
2360 }
2361
2362 /* Check established network session state */
2363 if (session->net_handle == 0) {
2364 ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
2365 DBG2("Session network handle is not set on enable-consumer");
2366 goto error;
2367 }
2368
2369 if (consumer->net_seq_index == -1) {
2370 ret = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
2371 DBG2("Network index is not set on the consumer");
2372 goto error;
2373 }
2374
2375 break;
2376 }
2377
2378 /*
2379 * @session-lock
2380 * This is race free for now since the session lock is acquired before
2381 * ending up in this function. No other threads can access this kernel
2382 * session without this lock hence freeing the consumer output object
2383 * is valid.
2384 */
2385 rcu_read_lock();
2386 /* Destroy current consumer. We are about to replace it */
2387 consumer_destroy_output(usess->consumer);
2388 rcu_read_unlock();
2389 usess->consumer = consumer;
2390 usess->tmp_consumer = NULL;
2391
2392 break;
2393 }
2394
2395 session->start_consumer = 1;
2396
2397 /* Enable it */
2398 if (consumer) {
2399 consumer->enabled = 1;
2400 /* Success at this point */
2401 ret = LTTNG_OK;
2402 } else {
2403 /* Should not really happend... */
2404 ret = LTTNG_ERR_NO_CONSUMER;
2405 }
2406
2407 error:
2408 return ret;
2409 }
2410
2411 /*
2412 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2413 * ready for trace analysis (or anykind of reader) or else 1 for pending data.
2414 */
2415 int cmd_data_pending(struct ltt_session *session)
2416 {
2417 int ret;
2418 struct ltt_kernel_session *ksess = session->kernel_session;
2419 struct ltt_ust_session *usess = session->ust_session;
2420
2421 assert(session);
2422
2423 /* Session MUST be stopped to ask for data availability. */
2424 if (session->enabled) {
2425 ret = LTTNG_ERR_SESSION_STARTED;
2426 goto error;
2427 }
2428
2429 if (ksess && ksess->consumer) {
2430 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
2431 if (ret == 1) {
2432 /* Data is still being extracted for the kernel. */
2433 goto error;
2434 }
2435 }
2436
2437 if (usess && usess->consumer) {
2438 ret = consumer_is_data_pending(usess->id, usess->consumer);
2439 if (ret == 1) {
2440 /* Data is still being extracted for the kernel. */
2441 goto error;
2442 }
2443 }
2444
2445 /* Data is ready to be read by a viewer */
2446 ret = 0;
2447
2448 error:
2449 return ret;
2450 }
2451
2452 /*
2453 * Init command subsystem.
2454 */
2455 void cmd_init(void)
2456 {
2457 /*
2458 * Set network sequence index to 1 for streams to match a relayd socket on
2459 * the consumer side.
2460 */
2461 uatomic_set(&relayd_net_seq_idx, 1);
2462
2463 DBG("Command subsystem initialized");
2464 }
This page took 0.143685 seconds and 4 git commands to generate.