0da8f6ad100eef2217254382c9c7e1651aa339eb
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.cpp
1 /*
2 * lttng-ctl.c
3 *
4 * Linux Trace Toolkit Control Library
5 *
6 * Copyright (C) 2011 EfficiOS Inc.
7 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 *
9 * SPDX-License-Identifier: LGPL-2.1-only
10 *
11 */
12
13 #define _LGPL_SOURCE
14 #include <grp.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdint.h>
18 #include <string.h>
19 #include <unistd.h>
20
21 #include <common/bytecode/bytecode.h>
22 #include <common/align.h>
23 #include <common/common.h>
24 #include <common/compat/errno.h>
25 #include <common/compat/string.h>
26 #include <common/defaults.h>
27 #include <common/dynamic-array.h>
28 #include <common/dynamic-buffer.h>
29 #include <common/payload-view.h>
30 #include <common/payload.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <common/tracker.h>
33 #include <common/unix.h>
34 #include <common/uri.h>
35 #include <common/utils.h>
36 #include <lttng/channel-internal.h>
37 #include <lttng/destruction-handle.h>
38 #include <lttng/endpoint.h>
39 #include <lttng/error-query-internal.h>
40 #include <lttng/event-internal.h>
41 #include <lttng/health-internal.h>
42 #include <lttng/lttng-error.h>
43 #include <lttng/lttng.h>
44 #include <lttng/session-descriptor-internal.h>
45 #include <lttng/session-internal.h>
46 #include <lttng/trigger/trigger-internal.h>
47 #include <lttng/userspace-probe-internal.h>
48
49 #include "lttng-ctl-helper.h"
50 #include <common/filter/filter-ast.h>
51 #include <common/filter/filter-parser.hpp>
52 #include <common/filter/memstream.h>
53
54 #define COPY_DOMAIN_PACKED(dst, src) \
55 do { \
56 struct lttng_domain _tmp_domain; \
57 \
58 lttng_ctl_copy_lttng_domain(&_tmp_domain, &src); \
59 dst = _tmp_domain; \
60 } while (0)
61
62 /* Socket to session daemon for communication */
63 static int sessiond_socket = -1;
64 static char sessiond_sock_path[PATH_MAX];
65
66 /* Variables */
67 static char *tracing_group;
68 static int connected;
69
70 /* Global */
71
72 /*
73 * Those two variables are used by error.h to silent or control the verbosity of
74 * error message. They are global to the library so application linking with it
75 * are able to compile correctly and also control verbosity of the library.
76 */
77 LTTNG_EXPORT int lttng_opt_quiet;
78 LTTNG_EXPORT int lttng_opt_verbose;
79 LTTNG_EXPORT int lttng_opt_mi;
80
81 /*
82 * Copy domain to lttcomm_session_msg domain.
83 *
84 * If domain is unknown, default domain will be the kernel.
85 */
86 void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
87 struct lttng_domain *src)
88 {
89 if (src && dst) {
90 switch (src->type) {
91 case LTTNG_DOMAIN_KERNEL:
92 case LTTNG_DOMAIN_UST:
93 case LTTNG_DOMAIN_JUL:
94 case LTTNG_DOMAIN_LOG4J:
95 case LTTNG_DOMAIN_PYTHON:
96 memcpy(dst, src, sizeof(struct lttng_domain));
97 break;
98 default:
99 memset(dst, 0, sizeof(struct lttng_domain));
100 break;
101 }
102 }
103 }
104
105 /*
106 * Send lttcomm_session_msg to the session daemon.
107 *
108 * On success, returns the number of bytes sent (>=0)
109 * On error, returns -1
110 */
111 static int send_session_msg(struct lttcomm_session_msg *lsm)
112 {
113 int ret;
114
115 if (!connected) {
116 ret = -LTTNG_ERR_NO_SESSIOND;
117 goto end;
118 }
119
120 DBG("LSM cmd type: '%s' (%d)", lttcomm_sessiond_command_str((lttcomm_sessiond_command) lsm->cmd_type),
121 lsm->cmd_type);
122
123 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
124 sizeof(struct lttcomm_session_msg));
125 if (ret < 0) {
126 ret = -LTTNG_ERR_FATAL;
127 }
128
129 end:
130 return ret;
131 }
132
133 /*
134 * Send var len data to the session daemon.
135 *
136 * On success, returns the number of bytes sent (>=0)
137 * On error, returns -1
138 */
139 static int send_session_varlen(const void *data, size_t len)
140 {
141 int ret;
142
143 if (!connected) {
144 ret = -LTTNG_ERR_NO_SESSIOND;
145 goto end;
146 }
147
148 if (!data || !len) {
149 ret = 0;
150 goto end;
151 }
152
153 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
154 if (ret < 0) {
155 ret = -LTTNG_ERR_FATAL;
156 }
157
158 end:
159 return ret;
160 }
161
162 /*
163 * Send file descriptors to the session daemon.
164 *
165 * On success, returns the number of bytes sent (>=0)
166 * On error, returns -1
167 */
168 static int send_session_fds(const int *fds, size_t nb_fd)
169 {
170 int ret;
171
172 if (!connected) {
173 ret = -LTTNG_ERR_NO_SESSIOND;
174 goto end;
175 }
176
177 if (!fds || !nb_fd) {
178 ret = 0;
179 goto end;
180 }
181
182 ret = lttcomm_send_fds_unix_sock(sessiond_socket, fds, nb_fd);
183 if (ret < 0) {
184 ret = -LTTNG_ERR_FATAL;
185 }
186
187 end:
188 return ret;
189 }
190
191 /*
192 * Receive data from the sessiond socket.
193 *
194 * On success, returns the number of bytes received (>=0)
195 * On error, returns a negative lttng_error_code.
196 */
197 static int recv_data_sessiond(void *buf, size_t len)
198 {
199 int ret;
200
201 LTTNG_ASSERT(len > 0);
202
203 if (!connected) {
204 ret = -LTTNG_ERR_NO_SESSIOND;
205 goto end;
206 }
207
208 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
209 if (ret < 0) {
210 ret = -LTTNG_ERR_FATAL;
211 } else if (ret == 0) {
212 ret = -LTTNG_ERR_NO_SESSIOND;
213 }
214
215 end:
216 return ret;
217 }
218
219 /*
220 * Receive a payload from the session daemon by appending to an existing
221 * payload.
222 * On success, returns the number of bytes received (>=0)
223 * On error, returns a negative lttng_error_code.
224 */
225 static int recv_payload_sessiond(struct lttng_payload *payload, size_t len)
226 {
227 int ret;
228 const size_t original_payload_size = payload->buffer.size;
229
230 ret = lttng_dynamic_buffer_set_size(
231 &payload->buffer, payload->buffer.size + len);
232 if (ret) {
233 ret = -LTTNG_ERR_NOMEM;
234 goto end;
235 }
236
237 ret = recv_data_sessiond(
238 payload->buffer.data + original_payload_size, len);
239 end:
240 return ret;
241 }
242
243 /*
244 * Check if we are in the specified group.
245 *
246 * If yes return 1, else return -1.
247 */
248 int lttng_check_tracing_group(void)
249 {
250 gid_t *grp_list, tracing_gid;
251 int grp_list_size, grp_id, i;
252 int ret = -1;
253 const char *grp_name = tracing_group;
254
255 /* Get GID of group 'tracing' */
256 if (utils_get_group_id(grp_name, false, &tracing_gid)) {
257 /* If grp_tracing is NULL, the group does not exist. */
258 goto end;
259 }
260
261 /* Get number of supplementary group IDs */
262 grp_list_size = getgroups(0, NULL);
263 if (grp_list_size < 0) {
264 PERROR("getgroups");
265 goto end;
266 }
267
268 /* Alloc group list of the right size */
269 grp_list = (gid_t *) zmalloc(grp_list_size * sizeof(gid_t));
270 if (!grp_list) {
271 PERROR("malloc");
272 goto end;
273 }
274 grp_id = getgroups(grp_list_size, grp_list);
275 if (grp_id < 0) {
276 PERROR("getgroups");
277 goto free_list;
278 }
279
280 for (i = 0; i < grp_list_size; i++) {
281 if (grp_list[i] == tracing_gid) {
282 ret = 1;
283 break;
284 }
285 }
286
287 free_list:
288 free(grp_list);
289
290 end:
291 return ret;
292 }
293
294 static enum lttng_error_code check_enough_available_memory(
295 uint64_t num_bytes_requested_per_cpu)
296 {
297 int ret;
298 enum lttng_error_code ret_code;
299 long num_cpu;
300 uint64_t best_mem_info;
301 uint64_t num_bytes_requested_total;
302
303 /*
304 * Get the number of CPU currently online to compute the amount of
305 * memory needed to create a buffer for every CPU.
306 */
307 num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
308 if (num_cpu == -1) {
309 ret_code = LTTNG_ERR_FATAL;
310 goto end;
311 }
312
313 if (num_bytes_requested_per_cpu > UINT64_MAX / (uint64_t) num_cpu) {
314 /* Overflow */
315 ret_code = LTTNG_ERR_OVERFLOW;
316 goto end;
317 }
318
319 num_bytes_requested_total =
320 num_bytes_requested_per_cpu * (uint64_t) num_cpu;
321
322 /*
323 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
324 * reliable estimate we can get but it is only exposed by the kernel
325 * since 3.14. (See Linux kernel commit:
326 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
327 */
328 ret = utils_get_memory_available(&best_mem_info);
329 if (ret >= 0) {
330 goto success;
331 }
332
333 /*
334 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
335 * is a sanity check for obvious user error.
336 */
337 ret = utils_get_memory_total(&best_mem_info);
338 if (ret >= 0) {
339 goto success;
340 }
341
342 /* No valid source of information. */
343 ret_code = LTTNG_ERR_NOMEM;
344 goto end;
345
346 success:
347 if (best_mem_info >= num_bytes_requested_total) {
348 ret_code = LTTNG_OK;
349 } else {
350 ret_code = LTTNG_ERR_NOMEM;
351 }
352 end:
353 return ret_code;
354 }
355
356 /*
357 * Try connect to session daemon with sock_path.
358 *
359 * Return 0 on success, else -1
360 */
361 static int try_connect_sessiond(const char *sock_path)
362 {
363 int ret;
364
365 /* If socket exist, we check if the daemon listens for connect. */
366 ret = access(sock_path, F_OK);
367 if (ret < 0) {
368 /* Not alive */
369 goto error;
370 }
371
372 ret = lttcomm_connect_unix_sock(sock_path);
373 if (ret < 0) {
374 /* Not alive. */
375 goto error;
376 }
377
378 ret = lttcomm_close_unix_sock(ret);
379 if (ret < 0) {
380 PERROR("lttcomm_close_unix_sock");
381 }
382
383 return 0;
384
385 error:
386 return -1;
387 }
388
389 /*
390 * Set sessiond socket path by putting it in the global sessiond_sock_path
391 * variable.
392 *
393 * Returns 0 on success, negative value on failure (the sessiond socket path
394 * is somehow too long or ENOMEM).
395 */
396 static int set_session_daemon_path(void)
397 {
398 int in_tgroup = 0; /* In tracing group. */
399 uid_t uid;
400
401 uid = getuid();
402
403 if (uid != 0) {
404 /* Are we in the tracing group ? */
405 in_tgroup = lttng_check_tracing_group();
406 }
407
408 if ((uid == 0) || in_tgroup == 1) {
409 const int ret = lttng_strncpy(sessiond_sock_path,
410 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
411 sizeof(sessiond_sock_path));
412
413 if (ret) {
414 goto error;
415 }
416 }
417
418 if (uid != 0) {
419 int ret;
420
421 if (in_tgroup) {
422 /* Tracing group. */
423 ret = try_connect_sessiond(sessiond_sock_path);
424 if (ret >= 0) {
425 goto end;
426 }
427 /* Global session daemon not available... */
428 }
429 /* ...or not in tracing group (and not root), default */
430
431 /*
432 * With GNU C < 2.1, snprintf returns -1 if the target buffer
433 * is too small;
434 * With GNU C >= 2.1, snprintf returns the required size
435 * (excluding closing null)
436 */
437 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
438 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
439 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
440 goto error;
441 }
442 }
443 end:
444 return 0;
445
446 error:
447 return -1;
448 }
449
450 /*
451 * Connect to the LTTng session daemon.
452 *
453 * On success, return the socket's file descriptor. On error, return -1.
454 */
455 int connect_sessiond(void)
456 {
457 int ret;
458
459 ret = set_session_daemon_path();
460 if (ret < 0) {
461 goto error;
462 }
463
464 /* Connect to the sesssion daemon. */
465 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
466 if (ret < 0) {
467 goto error;
468 }
469
470 return ret;
471
472 error:
473 return -1;
474 }
475
476 static void reset_global_sessiond_connection_state(void)
477 {
478 sessiond_socket = -1;
479 connected = 0;
480 }
481
482 /*
483 * Clean disconnect from the session daemon.
484 *
485 * On success, return 0. On error, return -1.
486 */
487 static int disconnect_sessiond(void)
488 {
489 int ret = 0;
490
491 if (connected) {
492 ret = lttcomm_close_unix_sock(sessiond_socket);
493 reset_global_sessiond_connection_state();
494 }
495
496 return ret;
497 }
498
499 static int recv_sessiond_optional_data(size_t len, void **user_buf,
500 size_t *user_len)
501 {
502 int ret = 0;
503 void *buf = NULL;
504
505 if (len) {
506 if (!user_len) {
507 ret = -LTTNG_ERR_INVALID;
508 goto end;
509 }
510
511 buf = zmalloc(len);
512 if (!buf) {
513 ret = -ENOMEM;
514 goto end;
515 }
516
517 ret = recv_data_sessiond(buf, len);
518 if (ret < 0) {
519 goto end;
520 }
521
522 if (!user_buf) {
523 ret = -LTTNG_ERR_INVALID;
524 goto end;
525 }
526
527 /* Move ownership of command header buffer to user. */
528 *user_buf = buf;
529 buf = NULL;
530 *user_len = len;
531 } else {
532 /* No command header. */
533 if (user_len) {
534 *user_len = 0;
535 }
536
537 if (user_buf) {
538 *user_buf = NULL;
539 }
540 }
541
542 end:
543 free(buf);
544 return ret;
545 }
546
547 /*
548 * Ask the session daemon a specific command and put the data into buf.
549 * Takes extra var. len. data and file descriptors as input to send to the
550 * session daemon.
551 *
552 * Return size of data (only payload, not header) or a negative error code.
553 */
554 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm,
555 const int *fds, size_t nb_fd, const void *vardata,
556 size_t vardata_len, void **user_payload_buf,
557 void **user_cmd_header_buf, size_t *user_cmd_header_len)
558 {
559 int ret;
560 size_t payload_len;
561 struct lttcomm_lttng_msg llm;
562
563 ret = connect_sessiond();
564 if (ret < 0) {
565 ret = -LTTNG_ERR_NO_SESSIOND;
566 goto end;
567 } else {
568 sessiond_socket = ret;
569 connected = 1;
570 }
571
572 ret = send_session_msg(lsm);
573 if (ret < 0) {
574 /* Ret value is a valid lttng error code. */
575 goto end;
576 }
577 /* Send var len data */
578 ret = send_session_varlen(vardata, vardata_len);
579 if (ret < 0) {
580 /* Ret value is a valid lttng error code. */
581 goto end;
582 }
583
584 /* Send fds */
585 ret = send_session_fds(fds, nb_fd);
586 if (ret < 0) {
587 /* Ret value is a valid lttng error code. */
588 goto end;
589 }
590
591 /* Get header from data transmission */
592 ret = recv_data_sessiond(&llm, sizeof(llm));
593 if (ret < 0) {
594 /* Ret value is a valid lttng error code. */
595 goto end;
596 }
597
598 /* Check error code if OK */
599 if (llm.ret_code != LTTNG_OK) {
600 ret = -llm.ret_code;
601 goto end;
602 }
603
604 /* Get command header from data transmission */
605 ret = recv_sessiond_optional_data(llm.cmd_header_size,
606 user_cmd_header_buf, user_cmd_header_len);
607 if (ret < 0) {
608 goto end;
609 }
610
611 /* Get payload from data transmission */
612 ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf,
613 &payload_len);
614 if (ret < 0) {
615 goto end;
616 }
617
618 ret = llm.data_size;
619
620 end:
621 disconnect_sessiond();
622 return ret;
623 }
624
625 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message,
626 struct lttng_payload *reply)
627 {
628 int ret;
629 struct lttcomm_lttng_msg llm;
630 const int fd_count = lttng_payload_view_get_fd_handle_count(message);
631
632 LTTNG_ASSERT(reply->buffer.size == 0);
633 LTTNG_ASSERT(lttng_dynamic_pointer_array_get_count(&reply->_fd_handles) == 0);
634
635 ret = connect_sessiond();
636 if (ret < 0) {
637 ret = -LTTNG_ERR_NO_SESSIOND;
638 goto end;
639 } else {
640 sessiond_socket = ret;
641 connected = 1;
642 }
643
644 /* Send command to session daemon */
645 ret = lttcomm_send_creds_unix_sock(sessiond_socket, message->buffer.data,
646 message->buffer.size);
647 if (ret < 0) {
648 ret = -LTTNG_ERR_FATAL;
649 goto end;
650 }
651
652 if (fd_count > 0) {
653 ret = lttcomm_send_payload_view_fds_unix_sock(sessiond_socket,
654 message);
655 if (ret < 0) {
656 ret = -LTTNG_ERR_FATAL;
657 goto end;
658 }
659 }
660
661 /* Get header from data transmission */
662 ret = recv_payload_sessiond(reply, sizeof(llm));
663 if (ret < 0) {
664 /* Ret value is a valid lttng error code. */
665 goto end;
666 }
667
668 llm = *((typeof(llm) *) reply->buffer.data);
669
670 /* Check error code if OK */
671 if (llm.ret_code != LTTNG_OK) {
672 if (llm.ret_code < LTTNG_OK || llm.ret_code >= LTTNG_ERR_NR) {
673 /* Invalid error code received. */
674 ret = -LTTNG_ERR_UNK;
675 } else {
676 ret = -llm.ret_code;
677 }
678 goto end;
679 }
680
681 if (llm.cmd_header_size > 0) {
682 ret = recv_payload_sessiond(reply, llm.cmd_header_size);
683 if (ret < 0) {
684 goto end;
685 }
686 }
687
688 /* Get command header from data transmission */
689 if (llm.data_size > 0) {
690 ret = recv_payload_sessiond(reply, llm.data_size);
691 if (ret < 0) {
692 goto end;
693 }
694 }
695
696 if (llm.fd_count > 0) {
697 ret = lttcomm_recv_payload_fds_unix_sock(
698 sessiond_socket, llm.fd_count, reply);
699 if (ret < 0) {
700 goto end;
701 }
702 }
703
704 /* Don't return the llm header to the caller. */
705 memmove(reply->buffer.data, reply->buffer.data + sizeof(llm),
706 reply->buffer.size - sizeof(llm));
707 ret = lttng_dynamic_buffer_set_size(
708 &reply->buffer, reply->buffer.size - sizeof(llm));
709 if (ret) {
710 /* Can't happen as size is reduced. */
711 abort();
712 }
713
714 ret = reply->buffer.size;
715
716 end:
717 disconnect_sessiond();
718 return ret;
719 }
720
721 /*
722 * Create lttng handle and return pointer.
723 *
724 * The returned pointer will be NULL in case of malloc() error.
725 */
726 struct lttng_handle *lttng_create_handle(const char *session_name,
727 struct lttng_domain *domain)
728 {
729 int ret;
730 struct lttng_handle *handle = NULL;
731
732 handle = (lttng_handle *) zmalloc(sizeof(struct lttng_handle));
733 if (handle == NULL) {
734 PERROR("malloc handle");
735 goto end;
736 }
737
738 /* Copy session name */
739 ret = lttng_strncpy(handle->session_name, session_name ? : "",
740 sizeof(handle->session_name));
741 if (ret) {
742 goto error;
743 }
744
745 /* Copy lttng domain or leave initialized to 0. */
746 if (domain) {
747 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
748 }
749
750 end:
751 return handle;
752 error:
753 free(handle);
754 return NULL;
755 }
756
757 /*
758 * Destroy handle by free(3) the pointer.
759 */
760 void lttng_destroy_handle(struct lttng_handle *handle)
761 {
762 free(handle);
763 }
764
765 /*
766 * Register an outside consumer.
767 *
768 * Returns size of returned session payload data or a negative error code.
769 */
770 int lttng_register_consumer(struct lttng_handle *handle,
771 const char *socket_path)
772 {
773 int ret;
774 struct lttcomm_session_msg lsm;
775
776 if (handle == NULL || socket_path == NULL) {
777 ret = -LTTNG_ERR_INVALID;
778 goto end;
779 }
780
781 memset(&lsm, 0, sizeof(lsm));
782 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
783 ret = lttng_strncpy(lsm.session.name, handle->session_name,
784 sizeof(lsm.session.name));
785 if (ret) {
786 ret = -LTTNG_ERR_INVALID;
787 goto end;
788 }
789
790 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
791
792 ret = lttng_strncpy(lsm.u.reg.path, socket_path,
793 sizeof(lsm.u.reg.path));
794 if (ret) {
795 ret = -LTTNG_ERR_INVALID;
796 goto end;
797 }
798
799 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
800 end:
801 return ret;
802 }
803
804 /*
805 * Start tracing for all traces of the session.
806 *
807 * Returns size of returned session payload data or a negative error code.
808 */
809 int lttng_start_tracing(const char *session_name)
810 {
811 int ret;
812 struct lttcomm_session_msg lsm;
813
814 if (session_name == NULL) {
815 ret = -LTTNG_ERR_INVALID;
816 goto end;
817 }
818
819 memset(&lsm, 0, sizeof(lsm));
820 lsm.cmd_type = LTTNG_START_TRACE;
821
822 ret = lttng_strncpy(lsm.session.name, session_name,
823 sizeof(lsm.session.name));
824 if (ret) {
825 ret = -LTTNG_ERR_INVALID;
826 goto end;
827 }
828
829 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
830 end:
831 return ret;
832 }
833
834 /*
835 * Stop tracing for all traces of the session.
836 */
837 static int _lttng_stop_tracing(const char *session_name, int wait)
838 {
839 int ret, data_ret;
840 struct lttcomm_session_msg lsm;
841
842 if (session_name == NULL) {
843 ret = -LTTNG_ERR_INVALID;
844 goto error;
845 }
846
847 memset(&lsm, 0, sizeof(lsm));
848 lsm.cmd_type = LTTNG_STOP_TRACE;
849
850 ret = lttng_strncpy(lsm.session.name, session_name,
851 sizeof(lsm.session.name));
852 if (ret) {
853 ret = -LTTNG_ERR_INVALID;
854 goto error;
855 }
856
857 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
858 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
859 goto error;
860 }
861
862 if (!wait) {
863 goto end;
864 }
865
866 /* Check for data availability */
867 do {
868 data_ret = lttng_data_pending(session_name);
869 if (data_ret < 0) {
870 /* Return the data available call error. */
871 ret = data_ret;
872 goto error;
873 }
874
875 /*
876 * Data sleep time before retrying (in usec). Don't sleep if the
877 * call returned value indicates availability.
878 */
879 if (data_ret) {
880 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
881 }
882 } while (data_ret != 0);
883
884 end:
885 error:
886 return ret;
887 }
888
889 /*
890 * Stop tracing and wait for data availability.
891 */
892 int lttng_stop_tracing(const char *session_name)
893 {
894 return _lttng_stop_tracing(session_name, 1);
895 }
896
897 /*
898 * Stop tracing but _don't_ wait for data availability.
899 */
900 int lttng_stop_tracing_no_wait(const char *session_name)
901 {
902 return _lttng_stop_tracing(session_name, 0);
903 }
904
905 /*
906 * Add context to a channel.
907 *
908 * If the given channel is NULL, add the contexts to all channels.
909 * The event_name param is ignored.
910 *
911 * Returns the size of the returned payload data or a negative error code.
912 */
913 int lttng_add_context(struct lttng_handle *handle,
914 struct lttng_event_context *ctx,
915 const char *event_name __attribute__((unused)),
916 const char *channel_name)
917 {
918 int ret;
919 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_ADD_CONTEXT };
920 struct lttng_payload payload;
921
922 lttng_payload_init(&payload);
923
924 /* Safety check. Both are mandatory. */
925 if (handle == NULL || ctx == NULL) {
926 ret = -LTTNG_ERR_INVALID;
927 goto end;
928 }
929
930 ret = lttng_dynamic_buffer_set_size(&payload.buffer, sizeof(lsm));
931 if (ret) {
932 ret = -LTTNG_ERR_NOMEM;
933 goto end;
934 }
935
936 /* If no channel name, send empty string. */
937 ret = lttng_strncpy(lsm.u.context.channel_name, channel_name ?: "",
938 sizeof(lsm.u.context.channel_name));
939 if (ret) {
940 ret = -LTTNG_ERR_INVALID;
941 goto end;
942 }
943
944 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
945 ret = lttng_strncpy(lsm.session.name, handle->session_name,
946 sizeof(lsm.session.name));
947 if (ret) {
948 ret = -LTTNG_ERR_INVALID;
949 goto end;
950 }
951
952 ret = lttng_event_context_serialize(ctx, &payload);
953 if (ret) {
954 ret = -LTTNG_ERR_INVALID;
955 goto end;
956 }
957
958 lsm.u.context.length = payload.buffer.size - sizeof(lsm);
959
960 /* Update message header. */
961 memcpy(payload.buffer.data, &lsm, sizeof(lsm));
962
963 {
964 struct lttng_payload reply;
965 struct lttng_payload_view payload_view =
966 lttng_payload_view_from_payload(&payload, 0,
967 -1);
968
969 lttng_payload_init(&reply);
970 ret = lttng_ctl_ask_sessiond_payload(&payload_view, &reply);
971 lttng_payload_reset(&reply);
972 if (ret) {
973 goto end;
974 }
975 }
976
977 end:
978 lttng_payload_reset(&payload);
979 return ret;
980 }
981
982 /*
983 * Enable event(s) for a channel.
984 *
985 * If no event name is specified, all events are enabled.
986 * If no channel name is specified, the default 'channel0' is used.
987 *
988 * Returns size of returned session payload data or a negative error code.
989 */
990 int lttng_enable_event(struct lttng_handle *handle,
991 struct lttng_event *ev, const char *channel_name)
992 {
993 return lttng_enable_event_with_exclusions(handle, ev, channel_name,
994 NULL, 0, NULL);
995 }
996
997 /*
998 * Create or enable an event with a filter expression.
999 *
1000 * Return negative error value on error.
1001 * Return size of returned session payload data if OK.
1002 */
1003 int lttng_enable_event_with_filter(struct lttng_handle *handle,
1004 struct lttng_event *event, const char *channel_name,
1005 const char *filter_expression)
1006 {
1007 return lttng_enable_event_with_exclusions(handle, event, channel_name,
1008 filter_expression, 0, NULL);
1009 }
1010
1011 /*
1012 * Depending on the event, return a newly allocated agent filter expression or
1013 * NULL if not applicable.
1014 *
1015 * An event with NO loglevel and the name is * will return NULL.
1016 */
1017 static char *set_agent_filter(const char *filter, struct lttng_event *ev)
1018 {
1019 int err;
1020 char *agent_filter = NULL;
1021
1022 LTTNG_ASSERT(ev);
1023
1024 /* Don't add filter for the '*' event. */
1025 if (strcmp(ev->name, "*") != 0) {
1026 if (filter) {
1027 err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter,
1028 ev->name);
1029 } else {
1030 err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name);
1031 }
1032 if (err < 0) {
1033 PERROR("asprintf");
1034 goto error;
1035 }
1036 }
1037
1038 /* Add loglevel filtering if any for the JUL domain. */
1039 if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
1040 const char *op;
1041
1042 if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) {
1043 op = ">=";
1044 } else {
1045 op = "==";
1046 }
1047
1048 if (filter || agent_filter) {
1049 char *new_filter;
1050
1051 err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)",
1052 agent_filter ? agent_filter : filter, op,
1053 ev->loglevel);
1054 if (agent_filter) {
1055 free(agent_filter);
1056 }
1057 agent_filter = new_filter;
1058 } else {
1059 err = asprintf(&agent_filter, "int_loglevel %s %d", op,
1060 ev->loglevel);
1061 }
1062 if (err < 0) {
1063 PERROR("asprintf");
1064 goto error;
1065 }
1066 }
1067
1068 return agent_filter;
1069 error:
1070 free(agent_filter);
1071 return NULL;
1072 }
1073
1074 /*
1075 * Enable event(s) for a channel, possibly with exclusions and a filter.
1076 * If no event name is specified, all events are enabled.
1077 * If no channel name is specified, the default name is used.
1078 * If filter expression is not NULL, the filter is set for the event.
1079 * If exclusion count is not zero, the exclusions are set for the event.
1080 * Returns size of returned session payload data or a negative error code.
1081 */
1082 int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
1083 struct lttng_event *ev, const char *channel_name,
1084 const char *original_filter_expression,
1085 int exclusion_count, char **exclusion_list)
1086 {
1087 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_ENABLE_EVENT };
1088 struct lttng_payload payload;
1089 int ret = 0;
1090 unsigned int free_filter_expression = 0;
1091 struct filter_parser_ctx *ctx = NULL;
1092 size_t bytecode_len = 0;
1093
1094 /*
1095 * We have either a filter or some exclusions, so we need to set up
1096 * a variable-length payload from where to send the data.
1097 */
1098 lttng_payload_init(&payload);
1099
1100 /*
1101 * Cast as non-const since we may replace the filter expression
1102 * by a dynamically allocated string. Otherwise, the original
1103 * string is not modified.
1104 */
1105 char *filter_expression = (char *) original_filter_expression;
1106
1107 if (handle == NULL || ev == NULL) {
1108 ret = -LTTNG_ERR_INVALID;
1109 goto error;
1110 }
1111
1112 /*
1113 * Empty filter string will always be rejected by the parser
1114 * anyway, so treat this corner-case early to eliminate
1115 * lttng_fmemopen error for 0-byte allocation.
1116 */
1117 if (filter_expression && filter_expression[0] == '\0') {
1118 ret = -LTTNG_ERR_INVALID;
1119 goto error;
1120 }
1121
1122 if (ev->name[0] == '\0') {
1123 /* Enable all events. */
1124 ret = lttng_strncpy(ev->name, "*", sizeof(ev->name));
1125 LTTNG_ASSERT(ret == 0);
1126 }
1127
1128 /* Parse filter expression. */
1129 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1130 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1131 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1132 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1133 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1134 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1135 char *agent_filter;
1136
1137 /* Setup agent filter if needed. */
1138 agent_filter = set_agent_filter(filter_expression, ev);
1139 if (!agent_filter) {
1140 if (!filter_expression) {
1141 /*
1142 * No JUL and no filter, just skip
1143 * everything below.
1144 */
1145 goto serialize;
1146 }
1147 } else {
1148 /*
1149 * With an agent filter, the original filter has
1150 * been added to it thus replace the filter
1151 * expression.
1152 */
1153 filter_expression = agent_filter;
1154 free_filter_expression = 1;
1155 }
1156 }
1157
1158 if (strnlen(filter_expression, LTTNG_FILTER_MAX_LEN) ==
1159 LTTNG_FILTER_MAX_LEN) {
1160 ret = -LTTNG_ERR_FILTER_INVAL;
1161 goto error;
1162 }
1163
1164 ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx);
1165 if (ret) {
1166 goto error;
1167 }
1168
1169 bytecode_len = bytecode_get_len(&ctx->bytecode->b) +
1170 sizeof(ctx->bytecode->b);
1171 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
1172 ret = -LTTNG_ERR_FILTER_INVAL;
1173 goto error;
1174 }
1175 }
1176
1177 serialize:
1178 ret = lttng_event_serialize(ev, exclusion_count, exclusion_list,
1179 filter_expression, bytecode_len,
1180 (ctx && bytecode_len) ? &ctx->bytecode->b : NULL,
1181 &payload);
1182 if (ret) {
1183 ret = -LTTNG_ERR_INVALID;
1184 goto error;
1185 }
1186
1187 /* If no channel name, send empty string. */
1188 ret = lttng_strncpy(lsm.u.enable.channel_name, channel_name ?: "",
1189 sizeof(lsm.u.enable.channel_name));
1190 if (ret) {
1191 ret = -LTTNG_ERR_INVALID;
1192 goto error;
1193 }
1194
1195 /* Domain */
1196 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1197
1198 /* Session name */
1199 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1200 sizeof(lsm.session.name));
1201 if (ret) {
1202 ret = -LTTNG_ERR_INVALID;
1203 goto error;
1204 }
1205
1206 /* Length of the serialized event. */
1207 lsm.u.enable.length = (uint32_t) payload.buffer.size;
1208
1209 {
1210 struct lttng_payload_view view = lttng_payload_view_from_payload(
1211 &payload, 0, -1);
1212 int fd_count = lttng_payload_view_get_fd_handle_count(&view);
1213 int fd_to_send;
1214
1215 if (fd_count < 0) {
1216 goto error;
1217 }
1218
1219 LTTNG_ASSERT(fd_count == 0 || fd_count == 1);
1220 if (fd_count == 1) {
1221 struct fd_handle *h =
1222 lttng_payload_view_pop_fd_handle(&view);
1223
1224 if (!h) {
1225 goto error;
1226 }
1227
1228 fd_to_send = fd_handle_get_fd(h);
1229 fd_handle_put(h);
1230 }
1231
1232 lsm.fd_count = fd_count;
1233
1234 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
1235 fd_count ? &fd_to_send : NULL, fd_count,
1236 view.buffer.size ? view.buffer.data : NULL,
1237 view.buffer.size, NULL, NULL, 0);
1238 }
1239
1240 error:
1241 if (filter_expression && ctx) {
1242 filter_bytecode_free(ctx);
1243 filter_ir_free(ctx);
1244 filter_parser_ctx_free(ctx);
1245 }
1246 if (free_filter_expression) {
1247 /*
1248 * The filter expression has been replaced and must be freed as
1249 * it is not the original filter expression received as a
1250 * parameter.
1251 */
1252 free(filter_expression);
1253 }
1254 /*
1255 * Return directly to the caller and don't ask the sessiond since
1256 * something went wrong in the parsing of data above.
1257 */
1258 lttng_payload_reset(&payload);
1259 return ret;
1260 }
1261
1262 int lttng_disable_event_ext(struct lttng_handle *handle,
1263 struct lttng_event *ev, const char *channel_name,
1264 const char *original_filter_expression)
1265 {
1266 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_DISABLE_EVENT };
1267 struct lttng_payload payload;
1268 int ret = 0;
1269 unsigned int free_filter_expression = 0;
1270 struct filter_parser_ctx *ctx = NULL;
1271 size_t bytecode_len = 0;
1272
1273 /*
1274 * We have either a filter or some exclusions, so we need to set up
1275 * a variable-length payload from where to send the data.
1276 */
1277 lttng_payload_init(&payload);
1278
1279 /*
1280 * Cast as non-const since we may replace the filter expression
1281 * by a dynamically allocated string. Otherwise, the original
1282 * string is not modified.
1283 */
1284 char *filter_expression = (char *) original_filter_expression;
1285
1286 if (handle == NULL || ev == NULL) {
1287 ret = -LTTNG_ERR_INVALID;
1288 goto error;
1289 }
1290
1291 /*
1292 * Empty filter string will always be rejected by the parser
1293 * anyway, so treat this corner-case early to eliminate
1294 * lttng_fmemopen error for 0-byte allocation.
1295 */
1296 if (filter_expression && filter_expression[0] == '\0') {
1297 ret = -LTTNG_ERR_INVALID;
1298 goto error;
1299 }
1300
1301 /* Parse filter expression. */
1302 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1303 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1304 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1305 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1306 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1307 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1308 char *agent_filter;
1309
1310 /* Setup agent filter if needed. */
1311 agent_filter = set_agent_filter(filter_expression, ev);
1312 if (!agent_filter) {
1313 if (!filter_expression) {
1314 /*
1315 * No JUL and no filter, just skip
1316 * everything below.
1317 */
1318 goto serialize;
1319 }
1320 } else {
1321 /*
1322 * With an agent filter, the original filter has
1323 * been added to it thus replace the filter
1324 * expression.
1325 */
1326 filter_expression = agent_filter;
1327 free_filter_expression = 1;
1328 }
1329 }
1330
1331 if (strnlen(filter_expression, LTTNG_FILTER_MAX_LEN) ==
1332 LTTNG_FILTER_MAX_LEN) {
1333 ret = -LTTNG_ERR_FILTER_INVAL;
1334 goto error;
1335 }
1336
1337 ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx);
1338 if (ret) {
1339 goto error;
1340 }
1341
1342 bytecode_len = bytecode_get_len(&ctx->bytecode->b) +
1343 sizeof(ctx->bytecode->b);
1344 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
1345 ret = -LTTNG_ERR_FILTER_INVAL;
1346 goto error;
1347 }
1348 }
1349
1350 serialize:
1351 ret = lttng_event_serialize(ev, 0, NULL,
1352 filter_expression, bytecode_len,
1353 (ctx && bytecode_len) ? &ctx->bytecode->b : NULL,
1354 &payload);
1355 if (ret) {
1356 ret = -LTTNG_ERR_INVALID;
1357 goto error;
1358 }
1359
1360 /* If no channel name, send empty string. */
1361 ret = lttng_strncpy(lsm.u.disable.channel_name, channel_name ?: "",
1362 sizeof(lsm.u.disable.channel_name));
1363 if (ret) {
1364 ret = -LTTNG_ERR_INVALID;
1365 goto error;
1366 }
1367
1368 /* Domain */
1369 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1370
1371 /* Session name */
1372 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1373 sizeof(lsm.session.name));
1374 if (ret) {
1375 ret = -LTTNG_ERR_INVALID;
1376 goto error;
1377 }
1378
1379 /* Length of the serialized event. */
1380 lsm.u.disable.length = (uint32_t) payload.buffer.size;
1381
1382 {
1383 struct lttng_payload_view view = lttng_payload_view_from_payload(
1384 &payload, 0, -1);
1385 int fd_count = lttng_payload_view_get_fd_handle_count(&view);
1386 int fd_to_send;
1387
1388 if (fd_count < 0) {
1389 goto error;
1390 }
1391
1392 LTTNG_ASSERT(fd_count == 0 || fd_count == 1);
1393 if (fd_count == 1) {
1394 struct fd_handle *h =
1395 lttng_payload_view_pop_fd_handle(&view);
1396
1397 if (!h) {
1398 goto error;
1399 }
1400
1401 fd_to_send = fd_handle_get_fd(h);
1402 fd_handle_put(h);
1403 }
1404
1405 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
1406 fd_count ? &fd_to_send : NULL, fd_count,
1407 view.buffer.size ? view.buffer.data : NULL,
1408 view.buffer.size, NULL, NULL, 0);
1409 }
1410
1411 error:
1412 if (filter_expression && ctx) {
1413 filter_bytecode_free(ctx);
1414 filter_ir_free(ctx);
1415 filter_parser_ctx_free(ctx);
1416 }
1417 if (free_filter_expression) {
1418 /*
1419 * The filter expression has been replaced and must be freed as
1420 * it is not the original filter expression received as a
1421 * parameter.
1422 */
1423 free(filter_expression);
1424 }
1425 /*
1426 * Return directly to the caller and don't ask the sessiond since
1427 * something went wrong in the parsing of data above.
1428 */
1429 lttng_payload_reset(&payload);
1430 return ret;
1431 }
1432
1433 /*
1434 * Disable event(s) of a channel and domain.
1435 * If no event name is specified, all events are disabled.
1436 * If no channel name is specified, the default 'channel0' is used.
1437 * Returns size of returned session payload data or a negative error code.
1438 */
1439 int lttng_disable_event(struct lttng_handle *handle, const char *name,
1440 const char *channel_name)
1441 {
1442 int ret;
1443 struct lttng_event ev;
1444
1445 memset(&ev, 0, sizeof(ev));
1446 ev.loglevel = -1;
1447 ev.type = LTTNG_EVENT_ALL;
1448 ret = lttng_strncpy(ev.name, name ?: "", sizeof(ev.name));
1449 if (ret) {
1450 ret = -LTTNG_ERR_INVALID;
1451 goto end;
1452 }
1453
1454 ret = lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1455 end:
1456 return ret;
1457 }
1458
1459 struct lttng_channel *lttng_channel_create(struct lttng_domain *domain)
1460 {
1461 struct lttng_channel *channel = NULL;
1462
1463 if (!domain) {
1464 goto end;
1465 }
1466
1467 /* Validate domain. */
1468 switch (domain->type) {
1469 case LTTNG_DOMAIN_UST:
1470 switch (domain->buf_type) {
1471 case LTTNG_BUFFER_PER_UID:
1472 case LTTNG_BUFFER_PER_PID:
1473 break;
1474 default:
1475 goto end;
1476 }
1477 break;
1478 case LTTNG_DOMAIN_KERNEL:
1479 if (domain->buf_type != LTTNG_BUFFER_GLOBAL) {
1480 goto end;
1481 }
1482 break;
1483 default:
1484 goto end;
1485 }
1486
1487 channel = lttng_channel_create_internal();
1488 if (!channel) {
1489 goto end;
1490 }
1491
1492 lttng_channel_set_default_attr(domain, &channel->attr);
1493 end:
1494 return channel;
1495 }
1496
1497 void lttng_channel_destroy(struct lttng_channel *channel)
1498 {
1499 if (!channel) {
1500 return;
1501 }
1502
1503 if (channel->attr.extended.ptr) {
1504 free(channel->attr.extended.ptr);
1505 }
1506 free(channel);
1507 }
1508
1509 /*
1510 * Enable channel per domain
1511 * Returns size of returned session payload data or a negative error code.
1512 */
1513 int lttng_enable_channel(struct lttng_handle *handle,
1514 struct lttng_channel *in_chan)
1515 {
1516 enum lttng_error_code ret_code;
1517 int ret;
1518 struct lttng_dynamic_buffer buffer;
1519 struct lttcomm_session_msg lsm;
1520 uint64_t total_buffer_size_needed_per_cpu = 0;
1521 struct lttng_channel *channel = NULL;
1522
1523 lttng_dynamic_buffer_init(&buffer);
1524
1525 /* NULL arguments are forbidden. No default values. */
1526 if (handle == NULL || in_chan == NULL) {
1527 ret = -LTTNG_ERR_INVALID;
1528 goto end;
1529 }
1530
1531 /*
1532 * Verify that the amount of memory required to create the requested
1533 * buffer is available on the system at the moment.
1534 */
1535 if (in_chan->attr.num_subbuf >
1536 UINT64_MAX / in_chan->attr.subbuf_size) {
1537 /* Overflow */
1538 ret = -LTTNG_ERR_OVERFLOW;
1539 goto end;
1540 }
1541
1542 total_buffer_size_needed_per_cpu =
1543 in_chan->attr.num_subbuf * in_chan->attr.subbuf_size;
1544 ret_code = check_enough_available_memory(
1545 total_buffer_size_needed_per_cpu);
1546 if (ret_code != LTTNG_OK) {
1547 ret = -ret_code;
1548 goto end;
1549 }
1550
1551 /* Copy the channel for easier manipulation. */
1552 channel = lttng_channel_copy(in_chan);
1553 if (!channel) {
1554 ret = -LTTNG_ERR_NOMEM;
1555 goto end;
1556 }
1557
1558 /* Populate the channel extended attribute if necessary. */
1559 if (!channel->attr.extended.ptr) {
1560 struct lttng_channel_extended *extended =
1561 (struct lttng_channel_extended *) zmalloc(
1562 sizeof(*extended));
1563
1564 if (!extended) {
1565 ret = -LTTNG_ERR_NOMEM;
1566 goto end;
1567 }
1568 lttng_channel_set_default_extended_attr(
1569 &handle->domain, extended);
1570 channel->attr.extended.ptr = extended;
1571 }
1572
1573 /* Prepare the payload */
1574 memset(&lsm, 0, sizeof(lsm));
1575
1576 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
1577 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1578
1579 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1580 sizeof(lsm.session.name));
1581 if (ret) {
1582 ret = -LTTNG_ERR_INVALID;
1583 goto end;
1584 }
1585
1586 ret = lttng_channel_serialize(channel, &buffer);
1587 if (ret) {
1588 ret = -LTTNG_ERR_FATAL;
1589 goto end;
1590 }
1591
1592 lsm.u.channel.length = buffer.size;
1593
1594 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1595 &lsm, buffer.data, buffer.size, NULL);
1596 end:
1597 lttng_channel_destroy(channel);
1598 lttng_dynamic_buffer_reset(&buffer);
1599 return ret;
1600 }
1601
1602 /*
1603 * All tracing will be stopped for registered events of the channel.
1604 * Returns size of returned session payload data or a negative error code.
1605 */
1606 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
1607 {
1608 int ret;
1609 struct lttcomm_session_msg lsm;
1610
1611 /* Safety check. Both are mandatory. */
1612 if (handle == NULL || name == NULL) {
1613 return -LTTNG_ERR_INVALID;
1614 }
1615
1616 memset(&lsm, 0, sizeof(lsm));
1617
1618 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1619
1620 ret = lttng_strncpy(lsm.u.disable.channel_name, name,
1621 sizeof(lsm.u.disable.channel_name));
1622 if (ret) {
1623 ret = -LTTNG_ERR_INVALID;
1624 goto end;
1625 }
1626
1627 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1628
1629 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1630 sizeof(lsm.session.name));
1631 if (ret) {
1632 ret = -LTTNG_ERR_INVALID;
1633 goto end;
1634 }
1635
1636 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1637 end:
1638 return ret;
1639 }
1640
1641 /*
1642 * Lists all available tracepoints of domain.
1643 * Sets the contents of the events array.
1644 * Returns the number of lttng_event entries in events;
1645 * on error, returns a negative value.
1646 */
1647 int lttng_list_tracepoints(struct lttng_handle *handle,
1648 struct lttng_event **events)
1649 {
1650 enum lttng_error_code ret_code;
1651 int ret, total_payload_received;
1652 char *reception_buffer = NULL;
1653 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_LIST_TRACEPOINTS };
1654 struct lttcomm_list_command_header *cmd_header = NULL;
1655 size_t cmd_header_len;
1656 unsigned int nb_events = 0;
1657
1658 if (handle == NULL) {
1659 ret = -LTTNG_ERR_INVALID;
1660 goto end;
1661 }
1662
1663 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1664
1665 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
1666 (void **) &reception_buffer, (void **) &cmd_header,
1667 &cmd_header_len);
1668 if (ret < 0) {
1669 goto end;
1670 }
1671
1672 total_payload_received = ret;
1673
1674 if (!cmd_header) {
1675 ret = -LTTNG_ERR_UNK;
1676 goto end;
1677 }
1678
1679 if (cmd_header->count > INT_MAX) {
1680 ret = -LTTNG_ERR_OVERFLOW;
1681 goto end;
1682 }
1683
1684 nb_events = (unsigned int) cmd_header->count;
1685
1686 {
1687 struct lttng_buffer_view events_view =
1688 lttng_buffer_view_init(reception_buffer, 0,
1689 total_payload_received);
1690 struct lttng_payload_view events_payload_view =
1691 lttng_payload_view_from_buffer_view(
1692 &events_view, 0, -1);
1693
1694 ret_code = lttng_events_create_and_flatten_from_payload(
1695 &events_payload_view, nb_events, events);
1696 if (ret_code != LTTNG_OK) {
1697 ret = -ret_code;
1698 goto end;
1699 }
1700 }
1701
1702 ret = (int) nb_events;
1703
1704 end:
1705 free(cmd_header);
1706 free(reception_buffer);
1707 return ret;
1708 }
1709
1710 /*
1711 * Lists all available tracepoint fields of domain.
1712 * Sets the contents of the event field array.
1713 * Returns the number of lttng_event_field entries in events;
1714 * on error, returns a negative value.
1715 */
1716 int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1717 struct lttng_event_field **fields)
1718 {
1719 enum lttng_error_code ret_code;
1720 int ret;
1721 struct lttcomm_session_msg lsm;
1722 const struct lttcomm_list_command_header *cmd_header = NULL;
1723 unsigned int nb_event_fields = 0;
1724 struct lttng_payload reply;
1725
1726 if (handle == NULL) {
1727 ret = -LTTNG_ERR_INVALID;
1728 goto end;
1729 }
1730
1731 lttng_payload_init(&reply);
1732
1733 memset(&lsm, 0, sizeof(lsm));
1734 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
1735 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1736
1737 {
1738 lttng_payload_view message_view =
1739 lttng_payload_view_init_from_buffer(
1740 (const char *) &lsm, 0,
1741 sizeof(lsm));
1742
1743 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
1744 if (ret < 0) {
1745 goto end;
1746 }
1747 }
1748
1749 {
1750 const lttng_buffer_view cmd_header_view =
1751 lttng_buffer_view_from_dynamic_buffer(
1752 &reply.buffer, 0, sizeof(*cmd_header));
1753
1754 if (!lttng_buffer_view_is_valid(&cmd_header_view)) {
1755 ret = -LTTNG_ERR_INVALID_PROTOCOL;
1756 goto end;
1757 }
1758
1759 cmd_header = (struct lttcomm_list_command_header *)
1760 cmd_header_view.data;
1761 }
1762
1763 if (cmd_header->count > INT_MAX) {
1764 ret = -LTTNG_ERR_OVERFLOW;
1765 goto end;
1766 }
1767
1768 nb_event_fields = cmd_header->count;
1769
1770 {
1771 lttng_payload_view reply_view =
1772 lttng_payload_view_from_payload(&reply,
1773 sizeof(*cmd_header), -1);
1774
1775 ret_code = lttng_event_fields_create_and_flatten_from_payload(
1776 &reply_view, nb_event_fields, fields);
1777 if (ret_code != LTTNG_OK) {
1778 ret = -ret_code;
1779 goto end;
1780 }
1781 }
1782
1783 ret = nb_event_fields;
1784
1785 end:
1786 return ret;
1787 }
1788
1789 /*
1790 * Lists all available kernel system calls. Allocates and sets the contents of
1791 * the events array.
1792 *
1793 * Returns the number of lttng_event entries in events; on error, returns a
1794 * negative value.
1795 */
1796 int lttng_list_syscalls(struct lttng_event **events)
1797 {
1798 enum lttng_error_code ret_code;
1799 int ret, total_payload_received;
1800 char *reception_buffer = NULL;
1801 struct lttcomm_session_msg lsm = {};
1802 struct lttcomm_list_command_header *cmd_header = NULL;
1803 size_t cmd_header_len;
1804 uint32_t nb_events = 0;
1805
1806 if (!events) {
1807 ret = -LTTNG_ERR_INVALID;
1808 goto end;
1809 }
1810
1811 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1812 /* Force kernel domain for system calls. */
1813 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1814
1815 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
1816 (void **) &reception_buffer, (void **) &cmd_header,
1817 &cmd_header_len);
1818 if (ret < 0) {
1819 goto end;
1820 }
1821 total_payload_received = ret;
1822
1823 if (!cmd_header) {
1824 ret = -LTTNG_ERR_UNK;
1825 goto end;
1826 }
1827
1828 if (cmd_header->count > INT_MAX) {
1829 ret = -LTTNG_ERR_OVERFLOW;
1830 goto end;
1831 }
1832
1833 nb_events = (unsigned int) cmd_header->count;
1834
1835 {
1836 const struct lttng_buffer_view events_view =
1837 lttng_buffer_view_init(reception_buffer, 0,
1838 total_payload_received);
1839 struct lttng_payload_view events_payload_view =
1840 lttng_payload_view_from_buffer_view(
1841 &events_view, 0, -1);
1842
1843 ret_code = lttng_events_create_and_flatten_from_payload(
1844 &events_payload_view, nb_events, events);
1845 if (ret_code != LTTNG_OK) {
1846 ret = -ret_code;
1847 goto end;
1848 }
1849 }
1850
1851 ret = (int) nb_events;
1852
1853 end:
1854 free(reception_buffer);
1855 free(cmd_header);
1856 return ret;
1857 }
1858
1859 /*
1860 * Returns a human readable string describing
1861 * the error code (positive or negative value).
1862 */
1863 const char *lttng_strerror(int code)
1864 {
1865 if (code > 0) {
1866 code = -code;
1867 }
1868
1869 return error_get_str(code);
1870 }
1871
1872 enum lttng_error_code lttng_create_session_ext(
1873 struct lttng_session_descriptor *session_descriptor)
1874 {
1875 enum lttng_error_code ret_code;
1876 struct lttcomm_session_msg lsm = {
1877 .cmd_type = LTTNG_CREATE_SESSION_EXT,
1878 };
1879 void *reply = NULL;
1880 struct lttng_buffer_view reply_view;
1881 int reply_ret;
1882 bool sessiond_must_generate_ouput;
1883 struct lttng_dynamic_buffer payload;
1884 int ret;
1885 size_t descriptor_size;
1886 struct lttng_session_descriptor *descriptor_reply = NULL;
1887
1888 lttng_dynamic_buffer_init(&payload);
1889 if (!session_descriptor) {
1890 ret_code = LTTNG_ERR_INVALID;
1891 goto end;
1892 }
1893
1894 sessiond_must_generate_ouput =
1895 !lttng_session_descriptor_is_output_destination_initialized(
1896 session_descriptor);
1897 if (sessiond_must_generate_ouput) {
1898 const char *home_dir = utils_get_home_dir();
1899 size_t home_dir_len = home_dir ? strlen(home_dir) + 1 : 0;
1900
1901 if (!home_dir || home_dir_len > LTTNG_PATH_MAX) {
1902 ret_code = LTTNG_ERR_FATAL;
1903 goto end;
1904 }
1905
1906 lsm.u.create_session.home_dir_size = (uint16_t) home_dir_len;
1907 ret = lttng_dynamic_buffer_append(&payload, home_dir,
1908 home_dir_len);
1909 if (ret) {
1910 ret_code = LTTNG_ERR_NOMEM;
1911 goto end;
1912 }
1913 }
1914
1915 descriptor_size = payload.size;
1916 ret = lttng_session_descriptor_serialize(session_descriptor,
1917 &payload);
1918 if (ret) {
1919 ret_code = LTTNG_ERR_INVALID;
1920 goto end;
1921 }
1922 descriptor_size = payload.size - descriptor_size;
1923 lsm.u.create_session.session_descriptor_size = descriptor_size;
1924
1925 /* Command returns a session descriptor on success. */
1926 reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, payload.data,
1927 payload.size, &reply);
1928 if (reply_ret < 0) {
1929 ret_code = (lttng_error_code) -reply_ret;
1930 goto end;
1931 } else if (reply_ret == 0) {
1932 /* Socket unexpectedly closed by the session daemon. */
1933 ret_code = LTTNG_ERR_FATAL;
1934 goto end;
1935 }
1936
1937 reply_view = lttng_buffer_view_init((const char *) reply, 0, reply_ret);
1938 ret = lttng_session_descriptor_create_from_buffer(&reply_view,
1939 &descriptor_reply);
1940 if (ret < 0) {
1941 ret_code = LTTNG_ERR_FATAL;
1942 goto end;
1943 }
1944 ret_code = LTTNG_OK;
1945 lttng_session_descriptor_assign(session_descriptor, descriptor_reply);
1946 end:
1947 free(reply);
1948 lttng_dynamic_buffer_reset(&payload);
1949 lttng_session_descriptor_destroy(descriptor_reply);
1950 return ret_code;
1951 }
1952
1953 /*
1954 * Create a new session using name and url for destination.
1955 *
1956 * Return 0 on success else a negative LTTng error code.
1957 */
1958 int lttng_create_session(const char *name, const char *url)
1959 {
1960 int ret;
1961 ssize_t size;
1962 struct lttng_uri *uris = NULL;
1963 struct lttng_session_descriptor *descriptor = NULL;
1964 enum lttng_error_code ret_code;
1965
1966 if (!name) {
1967 ret = -LTTNG_ERR_INVALID;
1968 goto end;
1969 }
1970
1971 size = uri_parse_str_urls(url, NULL, &uris);
1972 if (size < 0) {
1973 ret = -LTTNG_ERR_INVALID;
1974 goto end;
1975 }
1976 switch (size) {
1977 case 0:
1978 descriptor = lttng_session_descriptor_create(name);
1979 break;
1980 case 1:
1981 if (uris[0].dtype != LTTNG_DST_PATH) {
1982 ret = -LTTNG_ERR_INVALID;
1983 goto end;
1984 }
1985 descriptor = lttng_session_descriptor_local_create(name,
1986 uris[0].dst.path);
1987 break;
1988 case 2:
1989 descriptor = lttng_session_descriptor_network_create(name, url,
1990 NULL);
1991 break;
1992 default:
1993 ret = -LTTNG_ERR_INVALID;
1994 goto end;
1995 }
1996 if (!descriptor) {
1997 ret = -LTTNG_ERR_INVALID;
1998 goto end;
1999 }
2000 ret_code = lttng_create_session_ext(descriptor);
2001 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
2002 end:
2003 lttng_session_descriptor_destroy(descriptor);
2004 free(uris);
2005 return ret;
2006 }
2007
2008 /*
2009 * Create a session exclusively used for snapshot.
2010 *
2011 * Return 0 on success else a negative LTTng error code.
2012 */
2013 int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
2014 {
2015 int ret;
2016 enum lttng_error_code ret_code;
2017 ssize_t size;
2018 struct lttng_uri *uris = NULL;
2019 struct lttng_session_descriptor *descriptor = NULL;
2020
2021 if (!name) {
2022 ret = -LTTNG_ERR_INVALID;
2023 goto end;
2024 }
2025
2026 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
2027 if (size < 0) {
2028 ret = -LTTNG_ERR_INVALID;
2029 goto end;
2030 }
2031 /*
2032 * If the user does not specify a custom subdir, use the session name.
2033 */
2034 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH &&
2035 strlen(uris[0].subdir) == 0) {
2036 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s",
2037 name);
2038 if (ret < 0) {
2039 PERROR("Failed to set session name as network destination sub-directory");
2040 ret = -LTTNG_ERR_FATAL;
2041 goto end;
2042 } else if (ret >= sizeof(uris[0].subdir)) {
2043 /* Truncated output. */
2044 ret = -LTTNG_ERR_INVALID;
2045 goto end;
2046 }
2047 }
2048
2049 switch (size) {
2050 case 0:
2051 descriptor = lttng_session_descriptor_snapshot_create(name);
2052 break;
2053 case 1:
2054 if (uris[0].dtype != LTTNG_DST_PATH) {
2055 ret = -LTTNG_ERR_INVALID;
2056 goto end;
2057 }
2058 descriptor = lttng_session_descriptor_snapshot_local_create(
2059 name,
2060 uris[0].dst.path);
2061 break;
2062 case 2:
2063 descriptor = lttng_session_descriptor_snapshot_network_create(
2064 name,
2065 snapshot_url,
2066 NULL);
2067 break;
2068 default:
2069 ret = -LTTNG_ERR_INVALID;
2070 goto end;
2071 }
2072 if (!descriptor) {
2073 ret = -LTTNG_ERR_INVALID;
2074 goto end;
2075 }
2076 ret_code = lttng_create_session_ext(descriptor);
2077 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
2078 end:
2079 lttng_session_descriptor_destroy(descriptor);
2080 free(uris);
2081 return ret;
2082 }
2083
2084 /*
2085 * Create a session exclusively used for live.
2086 *
2087 * Return 0 on success else a negative LTTng error code.
2088 */
2089 int lttng_create_session_live(const char *name, const char *url,
2090 unsigned int timer_interval)
2091 {
2092 int ret;
2093 enum lttng_error_code ret_code;
2094 struct lttng_session_descriptor *descriptor = NULL;
2095
2096 if (!name) {
2097 ret = -LTTNG_ERR_INVALID;
2098 goto end;
2099 }
2100
2101 if (url) {
2102 descriptor = lttng_session_descriptor_live_network_create(
2103 name, url, NULL, timer_interval);
2104 } else {
2105 descriptor = lttng_session_descriptor_live_create(
2106 name, timer_interval);
2107 }
2108 if (!descriptor) {
2109 ret = -LTTNG_ERR_INVALID;
2110 goto end;
2111 }
2112 ret_code = lttng_create_session_ext(descriptor);
2113 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
2114 end:
2115 lttng_session_descriptor_destroy(descriptor);
2116 return ret;
2117 }
2118
2119 /*
2120 * Stop the session and wait for the data before destroying it
2121 *
2122 * Return 0 on success else a negative LTTng error code.
2123 */
2124 int lttng_destroy_session(const char *session_name)
2125 {
2126 int ret;
2127 enum lttng_error_code ret_code;
2128 enum lttng_destruction_handle_status status;
2129 struct lttng_destruction_handle *handle = NULL;
2130
2131 /*
2132 * Stop the tracing and wait for the data to be
2133 * consumed.
2134 */
2135 ret = _lttng_stop_tracing(session_name, 1);
2136 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
2137 goto end;
2138 }
2139
2140 ret_code = lttng_destroy_session_ext(session_name, &handle);
2141 if (ret_code != LTTNG_OK) {
2142 ret = (int) -ret_code;
2143 goto end;
2144 }
2145 LTTNG_ASSERT(handle);
2146
2147 /* Block until the completion of the destruction of the session. */
2148 status = lttng_destruction_handle_wait_for_completion(handle, -1);
2149 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED) {
2150 ret = -LTTNG_ERR_UNK;
2151 goto end;
2152 }
2153
2154 status = lttng_destruction_handle_get_result(handle, &ret_code);
2155 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
2156 ret = -LTTNG_ERR_UNK;
2157 goto end;
2158 }
2159 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
2160 end:
2161 lttng_destruction_handle_destroy(handle);
2162 return ret;
2163 }
2164
2165 /*
2166 * Destroy the session without waiting for the data.
2167 */
2168 int lttng_destroy_session_no_wait(const char *session_name)
2169 {
2170 enum lttng_error_code ret_code;
2171
2172 ret_code = lttng_destroy_session_ext(session_name, NULL);
2173 return ret_code == LTTNG_OK ? 0 : -ret_code;
2174 }
2175
2176 /*
2177 * Ask the session daemon for all available sessions.
2178 * Sets the contents of the sessions array.
2179 * Returns the number of lttng_session entries in sessions;
2180 * on error, returns a negative value.
2181 */
2182 int lttng_list_sessions(struct lttng_session **out_sessions)
2183 {
2184 int ret;
2185 struct lttcomm_session_msg lsm;
2186 const size_t session_size = sizeof(struct lttng_session) +
2187 sizeof(struct lttng_session_extended);
2188 size_t session_count, i;
2189 struct lttng_session_extended *sessions_extended_begin;
2190 struct lttng_session *sessions = NULL;
2191
2192 memset(&lsm, 0, sizeof(lsm));
2193 lsm.cmd_type = LTTNG_LIST_SESSIONS;
2194 /*
2195 * Initialize out_sessions to NULL so it is initialized when
2196 * lttng_list_sessions returns 0, thus allowing *out_sessions to
2197 * be subsequently freed.
2198 */
2199 *out_sessions = NULL;
2200 ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions);
2201 if (ret <= 0) {
2202 goto end;
2203 }
2204 if (!sessions) {
2205 ret = -LTTNG_ERR_FATAL;
2206 goto end;
2207 }
2208
2209 if (ret % session_size) {
2210 ret = -LTTNG_ERR_UNK;
2211 free(sessions);
2212 goto end;
2213 }
2214 session_count = (size_t) ret / session_size;
2215 sessions_extended_begin = (struct lttng_session_extended *)
2216 (&sessions[session_count]);
2217
2218 /* Set extended session info pointers. */
2219 for (i = 0; i < session_count; i++) {
2220 struct lttng_session *session = &sessions[i];
2221 struct lttng_session_extended *extended =
2222 &(sessions_extended_begin[i]);
2223
2224 session->extended.ptr = extended;
2225 }
2226
2227 ret = (int) session_count;
2228 *out_sessions = sessions;
2229 end:
2230 return ret;
2231 }
2232
2233 enum lttng_error_code lttng_session_get_creation_time(
2234 const struct lttng_session *session, uint64_t *creation_time)
2235 {
2236 enum lttng_error_code ret = LTTNG_OK;
2237 struct lttng_session_extended *extended;
2238
2239 if (!session || !creation_time || !session->extended.ptr) {
2240 ret = LTTNG_ERR_INVALID;
2241 goto end;
2242 }
2243
2244 extended = (lttng_session_extended *) session->extended.ptr;
2245 if (!extended->creation_time.is_set) {
2246 /* Not created on the session daemon yet. */
2247 ret = LTTNG_ERR_SESSION_NOT_EXIST;
2248 goto end;
2249 }
2250 *creation_time = extended->creation_time.value;
2251 end:
2252 return ret;
2253 }
2254
2255 int lttng_set_session_shm_path(const char *session_name,
2256 const char *shm_path)
2257 {
2258 int ret;
2259 struct lttcomm_session_msg lsm;
2260
2261 if (session_name == NULL) {
2262 return -LTTNG_ERR_INVALID;
2263 }
2264
2265 memset(&lsm, 0, sizeof(lsm));
2266 lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
2267
2268 ret = lttng_strncpy(lsm.session.name, session_name,
2269 sizeof(lsm.session.name));
2270 if (ret) {
2271 ret = -LTTNG_ERR_INVALID;
2272 goto end;
2273 }
2274
2275 ret = lttng_strncpy(lsm.u.set_shm_path.shm_path, shm_path ?: "",
2276 sizeof(lsm.u.set_shm_path.shm_path));
2277 if (ret) {
2278 ret = -LTTNG_ERR_INVALID;
2279 goto end;
2280 }
2281
2282 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2283 end:
2284 return ret;
2285 }
2286
2287 /*
2288 * Ask the session daemon for all available domains of a session.
2289 * Sets the contents of the domains array.
2290 * Returns the number of lttng_domain entries in domains;
2291 * on error, returns a negative value.
2292 */
2293 int lttng_list_domains(const char *session_name,
2294 struct lttng_domain **domains)
2295 {
2296 int ret;
2297 struct lttcomm_session_msg lsm;
2298
2299 if (session_name == NULL) {
2300 ret = -LTTNG_ERR_INVALID;
2301 goto error;
2302 }
2303
2304 memset(&lsm, 0, sizeof(lsm));
2305 lsm.cmd_type = LTTNG_LIST_DOMAINS;
2306
2307 ret = lttng_strncpy(lsm.session.name, session_name,
2308 sizeof(lsm.session.name));
2309 if (ret) {
2310 ret = -LTTNG_ERR_INVALID;
2311 goto error;
2312 }
2313
2314 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
2315 if (ret < 0) {
2316 goto error;
2317 }
2318
2319 return ret / sizeof(struct lttng_domain);
2320 error:
2321 return ret;
2322 }
2323
2324 /*
2325 * Ask the session daemon for all available channels of a session.
2326 * Sets the contents of the channels array.
2327 * Returns the number of lttng_channel entries in channels;
2328 * on error, returns a negative value.
2329 */
2330 int lttng_list_channels(struct lttng_handle *handle,
2331 struct lttng_channel **channels)
2332 {
2333 int ret, total_payload_received;
2334 struct lttcomm_session_msg lsm;
2335 char *reception_buffer = NULL;
2336 size_t cmd_header_len = 0;
2337 struct lttcomm_list_command_header *cmd_header = NULL;
2338 struct lttng_dynamic_buffer tmp_buffer;
2339
2340 lttng_dynamic_buffer_init(&tmp_buffer);
2341
2342 if (handle == NULL) {
2343 ret = -LTTNG_ERR_INVALID;
2344 goto end;
2345 }
2346
2347 memset(&lsm, 0, sizeof(lsm));
2348 lsm.cmd_type = LTTNG_LIST_CHANNELS;
2349 ret = lttng_strncpy(lsm.session.name, handle->session_name,
2350 sizeof(lsm.session.name));
2351 if (ret) {
2352 ret = -LTTNG_ERR_INVALID;
2353 goto end;
2354 }
2355
2356 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
2357
2358 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
2359 (void **) &reception_buffer, (void **) &cmd_header,
2360 &cmd_header_len);
2361 if (ret < 0) {
2362 goto end;
2363 }
2364
2365 total_payload_received = ret;
2366
2367 if (cmd_header_len != sizeof(*cmd_header)) {
2368 ret = -LTTNG_ERR_FATAL;
2369 goto end;
2370 }
2371
2372 if (!cmd_header) {
2373 ret = LTTNG_ERR_UNK;
2374 goto end;
2375 }
2376
2377 if (cmd_header->count > INT_MAX) {
2378 ret = -LTTNG_ERR_OVERFLOW;
2379 goto end;
2380 }
2381
2382 {
2383 enum lttng_error_code ret_code;
2384 const struct lttng_buffer_view events_view =
2385 lttng_buffer_view_init(reception_buffer, 0,
2386 total_payload_received);
2387
2388 ret_code = lttng_channels_create_and_flatten_from_buffer(
2389 &events_view, cmd_header->count, channels);
2390 if (ret_code != LTTNG_OK) {
2391 ret = -ret_code;
2392 goto end;
2393 }
2394 }
2395
2396 ret = (int) cmd_header->count;
2397 end:
2398 free(cmd_header);
2399 free(reception_buffer);
2400 return ret;
2401 }
2402
2403 /*
2404 * Ask the session daemon for all available events of a session channel.
2405 * Sets the contents of the events array.
2406 * Returns the number of lttng_event entries in events;
2407 * on error, returns a negative value.
2408 */
2409 int lttng_list_events(struct lttng_handle *handle,
2410 const char *channel_name, struct lttng_event **events)
2411 {
2412 int ret;
2413 struct lttcomm_session_msg lsm = {};
2414 struct lttng_payload reply;
2415 struct lttng_payload_view lsm_view =
2416 lttng_payload_view_init_from_buffer(
2417 (const char *) &lsm, 0, sizeof(lsm));
2418 unsigned int nb_events = 0;
2419
2420 lttng_payload_init(&reply);
2421
2422 /* Safety check. An handle and channel name are mandatory. */
2423 if (handle == NULL || channel_name == NULL) {
2424 ret = -LTTNG_ERR_INVALID;
2425 goto end;
2426 }
2427
2428 /* Initialize command parameters. */
2429 lsm.cmd_type = LTTNG_LIST_EVENTS;
2430 ret = lttng_strncpy(lsm.session.name, handle->session_name,
2431 sizeof(lsm.session.name));
2432 if (ret) {
2433 ret = -LTTNG_ERR_INVALID;
2434 goto end;
2435 }
2436
2437 ret = lttng_strncpy(lsm.u.list.channel_name, channel_name,
2438 sizeof(lsm.u.list.channel_name));
2439 if (ret) {
2440 ret = -LTTNG_ERR_INVALID;
2441 goto end;
2442 }
2443
2444 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
2445
2446 /* Execute command against the session daemon. */
2447 ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply);
2448 if (ret < 0) {
2449 goto end;
2450 }
2451
2452 {
2453 const struct lttcomm_list_command_header *cmd_reply_header =
2454 NULL;
2455 const lttng_payload_view cmd_reply_header_view =
2456 lttng_payload_view_from_payload(&reply, 0,
2457 sizeof(*cmd_reply_header));
2458
2459 if (!lttng_payload_view_is_valid(&cmd_reply_header_view)) {
2460 ret = -LTTNG_ERR_INVALID_PROTOCOL;
2461 goto end;
2462 }
2463
2464 cmd_reply_header = (const struct lttcomm_list_command_header *)
2465 cmd_reply_header_view.buffer
2466 .data;
2467 if (cmd_reply_header->count > INT_MAX) {
2468 ret = -LTTNG_ERR_OVERFLOW;
2469 goto end;
2470 }
2471
2472 nb_events = (unsigned int) cmd_reply_header->count;
2473 }
2474
2475 {
2476 enum lttng_error_code ret_code;
2477 lttng_payload_view cmd_reply_payload = lttng_payload_view_from_payload(
2478 &reply,
2479 sizeof(struct lttcomm_list_command_header), -1);
2480
2481 ret_code = lttng_events_create_and_flatten_from_payload(
2482 &cmd_reply_payload, nb_events, events);
2483 if (ret_code != LTTNG_OK) {
2484 ret = -((int) ret_code);
2485 goto end;
2486 }
2487 }
2488
2489 ret = (int) nb_events;
2490 end:
2491 lttng_payload_reset(&reply);
2492 return ret;
2493 }
2494
2495 /*
2496 * Sets the tracing_group variable with name.
2497 * This function allocates memory pointed to by tracing_group.
2498 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2499 */
2500 int lttng_set_tracing_group(const char *name)
2501 {
2502 int ret = 0;
2503 char *new_group;
2504
2505 if (name == NULL) {
2506 ret = -LTTNG_ERR_INVALID;
2507 goto end;
2508 }
2509
2510 new_group = strdup(name);
2511 if (!new_group) {
2512 ret = -LTTNG_ERR_FATAL;
2513 goto end;
2514 }
2515
2516 free(tracing_group);
2517 tracing_group = new_group;
2518 new_group = NULL;
2519
2520 end:
2521 return ret;
2522 }
2523
2524 int lttng_calibrate(struct lttng_handle *handle __attribute__((unused)),
2525 struct lttng_calibrate *calibrate __attribute__((unused)))
2526 {
2527 /*
2528 * This command was removed in LTTng 2.9.
2529 */
2530 return -LTTNG_ERR_UND;
2531 }
2532
2533 /*
2534 * Set default channel attributes.
2535 * If either or both of the arguments are null, attr content is zeroe'd.
2536 */
2537 void lttng_channel_set_default_attr(struct lttng_domain *domain,
2538 struct lttng_channel_attr *attr)
2539 {
2540 struct lttng_channel_extended *extended;
2541
2542 /* Safety check */
2543 if (attr == NULL || domain == NULL) {
2544 return;
2545 }
2546
2547 /* Save the pointer for later use */
2548 extended = (struct lttng_channel_extended *) attr->extended.ptr;
2549 memset(attr, 0, sizeof(struct lttng_channel_attr));
2550
2551 /* Same for all domains. */
2552 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
2553 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
2554 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
2555
2556 switch (domain->type) {
2557 case LTTNG_DOMAIN_KERNEL:
2558 attr->switch_timer_interval =
2559 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
2560 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
2561 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
2562 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
2563 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
2564 break;
2565 case LTTNG_DOMAIN_UST:
2566 switch (domain->buf_type) {
2567 case LTTNG_BUFFER_PER_UID:
2568 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
2569 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
2570 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
2571 attr->switch_timer_interval =
2572 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
2573 attr->read_timer_interval =
2574 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
2575 break;
2576 case LTTNG_BUFFER_PER_PID:
2577 default:
2578 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
2579 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
2580 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
2581 attr->switch_timer_interval =
2582 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
2583 attr->read_timer_interval =
2584 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
2585 break;
2586 }
2587 default:
2588 /* Default behavior: leave set to 0. */
2589 break;
2590 }
2591
2592 if (extended) {
2593 lttng_channel_set_default_extended_attr(domain, extended);
2594 }
2595
2596 /* Reassign the extended pointer. */
2597 attr->extended.ptr = extended;
2598 }
2599
2600 int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
2601 uint64_t *discarded_events)
2602 {
2603 int ret = 0;
2604 struct lttng_channel_extended *chan_ext;
2605
2606 if (!channel || !discarded_events) {
2607 ret = -LTTNG_ERR_INVALID;
2608 goto end;
2609 }
2610
2611 chan_ext = (lttng_channel_extended *) channel->attr.extended.ptr;
2612 if (!chan_ext) {
2613 /*
2614 * This can happen since the lttng_channel structure is
2615 * used for other tasks where this pointer is never set.
2616 */
2617 *discarded_events = 0;
2618 goto end;
2619 }
2620
2621 *discarded_events = chan_ext->discarded_events;
2622 end:
2623 return ret;
2624 }
2625
2626 int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2627 uint64_t *lost_packets)
2628 {
2629 int ret = 0;
2630 struct lttng_channel_extended *chan_ext;
2631
2632 if (!channel || !lost_packets) {
2633 ret = -LTTNG_ERR_INVALID;
2634 goto end;
2635 }
2636
2637 chan_ext = (lttng_channel_extended *) channel->attr.extended.ptr;
2638 if (!chan_ext) {
2639 /*
2640 * This can happen since the lttng_channel structure is
2641 * used for other tasks where this pointer is never set.
2642 */
2643 *lost_packets = 0;
2644 goto end;
2645 }
2646
2647 *lost_packets = chan_ext->lost_packets;
2648 end:
2649 return ret;
2650 }
2651
2652 int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
2653 uint64_t *monitor_timer_interval)
2654 {
2655 int ret = 0;
2656
2657 if (!chan || !monitor_timer_interval) {
2658 ret = -LTTNG_ERR_INVALID;
2659 goto end;
2660 }
2661
2662 if (!chan->attr.extended.ptr) {
2663 ret = -LTTNG_ERR_INVALID;
2664 goto end;
2665 }
2666
2667 *monitor_timer_interval = ((struct lttng_channel_extended *)
2668 chan->attr.extended.ptr)->monitor_timer_interval;
2669 end:
2670 return ret;
2671 }
2672
2673 int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
2674 uint64_t monitor_timer_interval)
2675 {
2676 int ret = 0;
2677
2678 if (!chan || !chan->attr.extended.ptr) {
2679 ret = -LTTNG_ERR_INVALID;
2680 goto end;
2681 }
2682
2683 ((struct lttng_channel_extended *)
2684 chan->attr.extended.ptr)->monitor_timer_interval =
2685 monitor_timer_interval;
2686 end:
2687 return ret;
2688 }
2689
2690 int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
2691 int64_t *blocking_timeout)
2692 {
2693 int ret = 0;
2694
2695 if (!chan || !blocking_timeout) {
2696 ret = -LTTNG_ERR_INVALID;
2697 goto end;
2698 }
2699
2700 if (!chan->attr.extended.ptr) {
2701 ret = -LTTNG_ERR_INVALID;
2702 goto end;
2703 }
2704
2705 *blocking_timeout = ((struct lttng_channel_extended *)
2706 chan->attr.extended.ptr)->blocking_timeout;
2707 end:
2708 return ret;
2709 }
2710
2711 int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
2712 int64_t blocking_timeout)
2713 {
2714 int ret = 0;
2715 int64_t msec_timeout;
2716
2717 if (!chan || !chan->attr.extended.ptr) {
2718 ret = -LTTNG_ERR_INVALID;
2719 goto end;
2720 }
2721
2722 if (blocking_timeout < 0 && blocking_timeout != -1) {
2723 ret = -LTTNG_ERR_INVALID;
2724 goto end;
2725 }
2726
2727 /*
2728 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2729 * us to accept a narrower range of values (msecs expressed as a signed
2730 * 32-bit integer).
2731 */
2732 msec_timeout = blocking_timeout / 1000;
2733 if (msec_timeout != (int32_t) msec_timeout) {
2734 ret = -LTTNG_ERR_INVALID;
2735 goto end;
2736 }
2737
2738 ((struct lttng_channel_extended *)
2739 chan->attr.extended.ptr)->blocking_timeout =
2740 blocking_timeout;
2741 end:
2742 return ret;
2743 }
2744
2745 /*
2746 * Check if session daemon is alive.
2747 *
2748 * Return 1 if alive or 0 if not.
2749 * On error returns a negative value.
2750 */
2751 int lttng_session_daemon_alive(void)
2752 {
2753 int ret;
2754
2755 ret = set_session_daemon_path();
2756 if (ret < 0) {
2757 /* Error. */
2758 return ret;
2759 }
2760
2761 if (*sessiond_sock_path == '\0') {
2762 /*
2763 * No socket path set. Weird error which means the constructor
2764 * was not called.
2765 */
2766 abort();
2767 }
2768
2769 ret = try_connect_sessiond(sessiond_sock_path);
2770 if (ret < 0) {
2771 /* Not alive. */
2772 return 0;
2773 }
2774
2775 /* Is alive. */
2776 return 1;
2777 }
2778
2779 /*
2780 * Set URL for a consumer for a session and domain.
2781 *
2782 * Return 0 on success, else a negative value.
2783 */
2784 int lttng_set_consumer_url(struct lttng_handle *handle,
2785 const char *control_url, const char *data_url)
2786 {
2787 int ret;
2788 ssize_t size;
2789 struct lttcomm_session_msg lsm;
2790 struct lttng_uri *uris = NULL;
2791
2792 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2793 ret = -LTTNG_ERR_INVALID;
2794 goto error;
2795 }
2796
2797 memset(&lsm, 0, sizeof(lsm));
2798
2799 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2800
2801 ret = lttng_strncpy(lsm.session.name, handle->session_name,
2802 sizeof(lsm.session.name));
2803 if (ret) {
2804 ret = -LTTNG_ERR_INVALID;
2805 goto error;
2806 }
2807
2808 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
2809
2810 size = uri_parse_str_urls(control_url, data_url, &uris);
2811 if (size < 0) {
2812 ret = -LTTNG_ERR_INVALID;
2813 goto error;
2814 }
2815
2816 lsm.u.uri.size = size;
2817
2818 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2819 sizeof(struct lttng_uri) * size, NULL);
2820
2821 free(uris);
2822 error:
2823 return ret;
2824 }
2825
2826 /*
2827 * [OBSOLETE]
2828 */
2829 extern "C"
2830 LTTNG_EXPORT int lttng_enable_consumer(struct lttng_handle *handle);
2831 int lttng_enable_consumer(struct lttng_handle *handle __attribute__((unused)))
2832 {
2833 return -ENOSYS;
2834 }
2835
2836 /*
2837 * [OBSOLETE]
2838 */
2839 extern "C"
2840 LTTNG_EXPORT int lttng_disable_consumer(struct lttng_handle *handle);
2841 int lttng_disable_consumer(struct lttng_handle *handle __attribute__((unused)))
2842 {
2843 return -ENOSYS;
2844 }
2845
2846 /*
2847 * [OBSOLETE]
2848 */
2849 extern "C"
2850 LTTNG_EXPORT int _lttng_create_session_ext(const char *name, const char *url,
2851 const char *datetime);
2852 int _lttng_create_session_ext(const char *name __attribute__((unused)),
2853 const char *url __attribute__((unused)),
2854 const char *datetime __attribute__((unused)))
2855 {
2856 return -ENOSYS;
2857 }
2858
2859 /*
2860 * For a given session name, this call checks if the data is ready to be read
2861 * or is still being extracted by the consumer(s) hence not ready to be used by
2862 * any readers.
2863 */
2864 int lttng_data_pending(const char *session_name)
2865 {
2866 int ret;
2867 struct lttcomm_session_msg lsm;
2868 uint8_t *pending = NULL;
2869
2870 if (session_name == NULL) {
2871 return -LTTNG_ERR_INVALID;
2872 }
2873
2874 memset(&lsm, 0, sizeof(lsm));
2875 lsm.cmd_type = LTTNG_DATA_PENDING;
2876
2877 ret = lttng_strncpy(lsm.session.name, session_name,
2878 sizeof(lsm.session.name));
2879 if (ret) {
2880 ret = -LTTNG_ERR_INVALID;
2881 goto end;
2882 }
2883
2884 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2885 if (ret < 0) {
2886 goto end;
2887 } else if (ret != 1) {
2888 /* Unexpected payload size */
2889 ret = -LTTNG_ERR_INVALID;
2890 goto end;
2891 } else if (!pending) {
2892 /* Internal error. */
2893 ret = -LTTNG_ERR_UNK;
2894 goto end;
2895 }
2896
2897 ret = (int) *pending;
2898 end:
2899 free(pending);
2900 return ret;
2901 }
2902
2903 /*
2904 * Regenerate the metadata for a session.
2905 * Return 0 on success, a negative error code on error.
2906 */
2907 int lttng_regenerate_metadata(const char *session_name)
2908 {
2909 int ret;
2910 struct lttcomm_session_msg lsm;
2911
2912 if (!session_name) {
2913 ret = -LTTNG_ERR_INVALID;
2914 goto end;
2915 }
2916
2917 memset(&lsm, 0, sizeof(lsm));
2918 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
2919
2920 ret = lttng_strncpy(lsm.session.name, session_name,
2921 sizeof(lsm.session.name));
2922 if (ret) {
2923 ret = -LTTNG_ERR_INVALID;
2924 goto end;
2925 }
2926
2927 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2928 if (ret < 0) {
2929 goto end;
2930 }
2931
2932 ret = 0;
2933 end:
2934 return ret;
2935 }
2936
2937 /*
2938 * Deprecated, replaced by lttng_regenerate_metadata.
2939 */
2940 int lttng_metadata_regenerate(const char *session_name)
2941 {
2942 return lttng_regenerate_metadata(session_name);
2943 }
2944
2945 /*
2946 * Regenerate the statedump of a session.
2947 * Return 0 on success, a negative error code on error.
2948 */
2949 int lttng_regenerate_statedump(const char *session_name)
2950 {
2951 int ret;
2952 struct lttcomm_session_msg lsm;
2953
2954 if (!session_name) {
2955 ret = -LTTNG_ERR_INVALID;
2956 goto end;
2957 }
2958
2959 memset(&lsm, 0, sizeof(lsm));
2960 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
2961
2962 ret = lttng_strncpy(lsm.session.name, session_name,
2963 sizeof(lsm.session.name));
2964 if (ret) {
2965 ret = -LTTNG_ERR_INVALID;
2966 goto end;
2967 }
2968
2969 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2970 if (ret < 0) {
2971 goto end;
2972 }
2973
2974 ret = 0;
2975 end:
2976 return ret;
2977 }
2978
2979 static
2980 int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name,
2981 bool generate_name)
2982 {
2983 int ret;
2984 struct lttcomm_session_msg lsm = {
2985 .cmd_type = LTTNG_REGISTER_TRIGGER,
2986 };
2987 lsm.u.trigger.is_trigger_anonymous = !name && !generate_name;
2988 struct lttcomm_session_msg *message_lsm;
2989 struct lttng_payload message;
2990 struct lttng_payload reply;
2991 struct lttng_trigger *reply_trigger = NULL;
2992 enum lttng_domain_type domain_type;
2993 const struct lttng_credentials user_creds = {
2994 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
2995 .gid = LTTNG_OPTIONAL_INIT_UNSET,
2996 };
2997 const char *unused_trigger_name = NULL;
2998 enum lttng_trigger_status trigger_status;
2999
3000 lttng_payload_init(&message);
3001 lttng_payload_init(&reply);
3002
3003 if (!trigger) {
3004 ret = -LTTNG_ERR_INVALID;
3005 goto end;
3006 }
3007
3008 trigger_status = lttng_trigger_get_name(trigger, &unused_trigger_name);
3009 if (trigger_status != LTTNG_TRIGGER_STATUS_UNSET) {
3010 /* Re-using already registered trigger. */
3011 ret = -LTTNG_ERR_INVALID;
3012 goto end;
3013 }
3014
3015 if (name) {
3016 trigger_status = lttng_trigger_set_name(trigger, name);
3017 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
3018 ret = -LTTNG_ERR_NOMEM;
3019 goto end;
3020 }
3021 }
3022
3023 if (!trigger->creds.uid.is_set) {
3024 /* Use the client's credentials as the trigger credentials. */
3025 lttng_trigger_set_credentials(trigger, &user_creds);
3026 } else {
3027 /*
3028 * Validate that either the current trigger credentials and the
3029 * client credentials are identical or that the current user is
3030 * root. The root user can register, unregister triggers for
3031 * himself and other users.
3032 *
3033 * This check is also present on the sessiond side, using the
3034 * credentials passed on the socket. These check are all
3035 * "safety" checks.
3036 */
3037 const struct lttng_credentials *trigger_creds =
3038 lttng_trigger_get_credentials(trigger);
3039
3040 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3041 if (lttng_credentials_get_uid(&user_creds) != 0) {
3042 ret = -LTTNG_ERR_EPERM;
3043 goto end_unset_name;
3044 }
3045 }
3046 }
3047
3048 if (!lttng_trigger_validate(trigger)) {
3049 ret = -LTTNG_ERR_INVALID_TRIGGER;
3050 goto end_unset_name;
3051 }
3052
3053 domain_type = lttng_trigger_get_underlying_domain_type_restriction(
3054 trigger);
3055
3056 lsm.domain.type = domain_type;
3057
3058 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3059 if (ret) {
3060 ret = -LTTNG_ERR_NOMEM;
3061 goto end_unset_name;
3062 }
3063
3064 ret = lttng_trigger_serialize(trigger, &message);
3065 if (ret < 0) {
3066 ret = -LTTNG_ERR_UNK;
3067 goto end_unset_name;
3068 }
3069
3070 /*
3071 * This is needed to populate the trigger object size for the command
3072 * header.
3073 */
3074 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3075
3076 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3077
3078 {
3079 struct lttng_payload_view message_view =
3080 lttng_payload_view_from_payload(
3081 &message, 0, -1);
3082
3083 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3084 &message_view);
3085 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3086 if (ret < 0) {
3087 goto end_unset_name;
3088 }
3089 }
3090
3091 {
3092 struct lttng_payload_view reply_view =
3093 lttng_payload_view_from_payload(
3094 &reply, 0, reply.buffer.size);
3095
3096 ret = lttng_trigger_create_from_payload(
3097 &reply_view, &reply_trigger);
3098 if (ret < 0) {
3099 ret = -LTTNG_ERR_INVALID_PROTOCOL;
3100 goto end_unset_name;
3101 }
3102 }
3103
3104 if (name || generate_name) {
3105 ret = lttng_trigger_assign_name(trigger, reply_trigger);
3106 if (ret < 0) {
3107 ret = -LTTNG_ERR_NOMEM;
3108 goto end;
3109 }
3110 }
3111
3112 ret = 0;
3113 goto end;
3114
3115 end_unset_name:
3116 trigger_status = lttng_trigger_set_name(trigger, NULL);
3117 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
3118 ret = -LTTNG_ERR_UNK;
3119 }
3120 end:
3121 lttng_payload_reset(&message);
3122 lttng_payload_reset(&reply);
3123 lttng_trigger_destroy(reply_trigger);
3124 return ret;
3125 }
3126
3127 int lttng_register_trigger(struct lttng_trigger *trigger)
3128 {
3129 /* Register an anonymous trigger. */
3130 return _lttng_register_trigger(trigger, NULL, false);
3131 }
3132
3133 enum lttng_error_code lttng_register_trigger_with_name(
3134 struct lttng_trigger *trigger, const char *name)
3135 {
3136 const int ret = _lttng_register_trigger(trigger, name, false);
3137
3138 return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret;
3139 }
3140
3141 enum lttng_error_code lttng_register_trigger_with_automatic_name(
3142 struct lttng_trigger *trigger)
3143 {
3144 const int ret = _lttng_register_trigger(trigger, nullptr, true);
3145
3146 return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret;
3147 }
3148
3149 enum lttng_error_code lttng_error_query_execute(
3150 const struct lttng_error_query *query,
3151 const struct lttng_endpoint *endpoint,
3152 struct lttng_error_query_results **results)
3153 {
3154 int ret;
3155 enum lttng_error_code ret_code;
3156 struct lttcomm_session_msg lsm = {
3157 .cmd_type = LTTNG_EXECUTE_ERROR_QUERY,
3158 };
3159 struct lttng_payload message;
3160 struct lttng_payload reply;
3161 struct lttcomm_session_msg *message_lsm;
3162
3163 lttng_payload_init(&message);
3164 lttng_payload_init(&reply);
3165
3166 if (!query || !results) {
3167 ret_code = LTTNG_ERR_INVALID;
3168 goto end;
3169 }
3170
3171 if (endpoint != lttng_session_daemon_command_endpoint) {
3172 ret_code = LTTNG_ERR_INVALID_ERROR_QUERY_TARGET;
3173 goto end;
3174 }
3175
3176 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3177 if (ret) {
3178 ret_code = LTTNG_ERR_NOMEM;
3179 goto end;
3180 }
3181
3182 ret = lttng_error_query_serialize(query, &message);
3183 if (ret) {
3184 ret_code = LTTNG_ERR_UNK;
3185 goto end;
3186 }
3187
3188 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3189 message_lsm->u.error_query.length =
3190 (uint32_t) message.buffer.size - sizeof(lsm);
3191
3192 {
3193 struct lttng_payload_view message_view =
3194 lttng_payload_view_from_payload(
3195 &message, 0, -1);
3196
3197 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3198 &message_view);
3199 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3200 if (ret < 0) {
3201 ret_code =(lttng_error_code) -ret;
3202 goto end;
3203 }
3204 }
3205
3206 {
3207 ssize_t reply_create_ret;
3208 struct lttng_payload_view reply_view =
3209 lttng_payload_view_from_payload(
3210 &reply, 0, reply.buffer.size);
3211
3212 reply_create_ret = lttng_error_query_results_create_from_payload(
3213 &reply_view, results);
3214 if (reply_create_ret < 0) {
3215 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3216 goto end;
3217 }
3218 }
3219
3220 ret_code = LTTNG_OK;
3221 end:
3222 lttng_payload_reset(&message);
3223 lttng_payload_reset(&reply);
3224 return ret_code;
3225 }
3226
3227 int lttng_unregister_trigger(const struct lttng_trigger *trigger)
3228 {
3229 int ret;
3230 struct lttcomm_session_msg lsm;
3231 struct lttcomm_session_msg *message_lsm;
3232 struct lttng_payload message;
3233 struct lttng_payload reply;
3234 struct lttng_trigger *copy = NULL;
3235 const struct lttng_credentials user_creds = {
3236 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3237 .gid = LTTNG_OPTIONAL_INIT_UNSET,
3238 };
3239
3240 lttng_payload_init(&message);
3241 lttng_payload_init(&reply);
3242
3243 if (!trigger) {
3244 ret = -LTTNG_ERR_INVALID;
3245 goto end;
3246 }
3247
3248 copy = lttng_trigger_copy(trigger);
3249 if (!copy) {
3250 ret = -LTTNG_ERR_UNK;
3251 goto end;
3252 }
3253
3254 if (!copy->creds.uid.is_set) {
3255 /* Use the client credentials as the trigger credentials */
3256 lttng_trigger_set_credentials(copy, &user_creds);
3257 } else {
3258 /*
3259 * Validate that either the current trigger credentials and the
3260 * client credentials are identical or that the current user is
3261 * root. The root user can register, unregister triggers for
3262 * himself and other users.
3263 *
3264 * This check is also present on the sessiond side, using the
3265 * credentials passed on the socket. These check are all
3266 * "safety" checks.
3267 */
3268 const struct lttng_credentials *trigger_creds =
3269 lttng_trigger_get_credentials(copy);
3270 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3271 if (lttng_credentials_get_uid(&user_creds) != 0) {
3272 ret = -LTTNG_ERR_EPERM;
3273 goto end;
3274 }
3275 }
3276 }
3277
3278 if (!lttng_trigger_validate(copy)) {
3279 ret = -LTTNG_ERR_INVALID_TRIGGER;
3280 goto end;
3281 }
3282
3283 memset(&lsm, 0, sizeof(lsm));
3284 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
3285
3286 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3287 if (ret) {
3288 ret = -LTTNG_ERR_NOMEM;
3289 goto end;
3290 }
3291
3292 ret = lttng_trigger_serialize(copy, &message);
3293 if (ret < 0) {
3294 ret = -LTTNG_ERR_UNK;
3295 goto end;
3296 }
3297
3298 /*
3299 * This is needed to populate the trigger object size for the command
3300 * header and number of fds sent.
3301 */
3302 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3303
3304 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3305
3306 {
3307 struct lttng_payload_view message_view =
3308 lttng_payload_view_from_payload(
3309 &message, 0, -1);
3310
3311 /*
3312 * Update the message header with the number of fd that will be
3313 * sent.
3314 */
3315 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3316 &message_view);
3317
3318 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3319 if (ret < 0) {
3320 goto end;
3321 }
3322 }
3323
3324 ret = 0;
3325 end:
3326 lttng_trigger_destroy(copy);
3327 lttng_payload_reset(&message);
3328 lttng_payload_reset(&reply);
3329 return ret;
3330 }
3331
3332 /*
3333 * Ask the session daemon for all registered triggers for the current user.
3334 *
3335 * Allocates and return an lttng_triggers set.
3336 * On error, returns a suitable lttng_error_code.
3337 */
3338 enum lttng_error_code lttng_list_triggers(struct lttng_triggers **triggers)
3339 {
3340 int ret;
3341 enum lttng_error_code ret_code = LTTNG_OK;
3342 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_LIST_TRIGGERS };
3343 struct lttng_triggers *local_triggers = NULL;
3344 struct lttng_payload reply;
3345 struct lttng_payload_view lsm_view =
3346 lttng_payload_view_init_from_buffer(
3347 (const char *) &lsm, 0, sizeof(lsm));
3348
3349 lttng_payload_init(&reply);
3350
3351 ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply);
3352 if (ret < 0) {
3353 ret_code = (enum lttng_error_code) -ret;
3354 goto end;
3355 }
3356
3357 {
3358 struct lttng_payload_view reply_view =
3359 lttng_payload_view_from_payload(
3360 &reply, 0, reply.buffer.size);
3361
3362 ret = lttng_triggers_create_from_payload(
3363 &reply_view, &local_triggers);
3364 if (ret < 0) {
3365 ret_code = LTTNG_ERR_FATAL;
3366 goto end;
3367 }
3368 }
3369
3370 *triggers = local_triggers;
3371 local_triggers = NULL;
3372 end:
3373 lttng_payload_reset(&reply);
3374 lttng_triggers_destroy(local_triggers);
3375 return ret_code;
3376 }
3377
3378 /*
3379 * lib constructor.
3380 */
3381 static void __attribute__((constructor)) init(void)
3382 {
3383 /* Set default session group */
3384 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
3385 }
3386
3387 /*
3388 * lib destructor.
3389 */
3390 static void __attribute__((destructor)) lttng_ctl_exit(void)
3391 {
3392 free(tracing_group);
3393 }
This page took 0.102109 seconds and 4 git commands to generate.