Basic serialization/deserialization for lttng_session
[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.hpp>
22 #include <common/align.hpp>
23 #include <common/common.hpp>
24 #include <common/compat/errno.hpp>
25 #include <common/compat/string.hpp>
26 #include <common/defaults.hpp>
27 #include <common/dynamic-array.hpp>
28 #include <common/dynamic-buffer.hpp>
29 #include <common/payload-view.hpp>
30 #include <common/payload.hpp>
31 #include <common/sessiond-comm/sessiond-comm.hpp>
32 #include <common/tracker.hpp>
33 #include <common/unix.hpp>
34 #include <common/uri.hpp>
35 #include <common/utils.hpp>
36 #include <lttng/channel-internal.hpp>
37 #include <lttng/destruction-handle.h>
38 #include <lttng/endpoint.h>
39 #include <lttng/error-query-internal.hpp>
40 #include <lttng/event-internal.hpp>
41 #include <lttng/health-internal.hpp>
42 #include <lttng/lttng-error.h>
43 #include <lttng/lttng.h>
44 #include <lttng/session-descriptor-internal.hpp>
45 #include <lttng/session-internal.hpp>
46 #include <lttng/trigger/trigger-internal.hpp>
47 #include <lttng/userspace-probe-internal.hpp>
48
49 #include "lttng-ctl-helper.hpp"
50 #include <common/filter/filter-ast.hpp>
51 #include <common/filter/filter-parser.hpp>
52 #include <common/filter/memstream.hpp>
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 = calloc<gid_t>(grp_list_size);
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 char *buf = NULL;
504
505 if (len) {
506 if (!user_len) {
507 ret = -LTTNG_ERR_INVALID;
508 goto end;
509 }
510
511 buf = zmalloc<char>(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 = zmalloc<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 = {
920 .cmd_type = LTTNG_ADD_CONTEXT,
921 .session = {},
922 .domain = {},
923 .u = {},
924 .fd_count = 0,
925 };
926 struct lttng_payload payload;
927
928 lttng_payload_init(&payload);
929
930 /* Safety check. Both are mandatory. */
931 if (handle == NULL || ctx == NULL) {
932 ret = -LTTNG_ERR_INVALID;
933 goto end;
934 }
935
936 ret = lttng_dynamic_buffer_set_size(&payload.buffer, sizeof(lsm));
937 if (ret) {
938 ret = -LTTNG_ERR_NOMEM;
939 goto end;
940 }
941
942 /* If no channel name, send empty string. */
943 ret = lttng_strncpy(lsm.u.context.channel_name, channel_name ?: "",
944 sizeof(lsm.u.context.channel_name));
945 if (ret) {
946 ret = -LTTNG_ERR_INVALID;
947 goto end;
948 }
949
950 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
951 ret = lttng_strncpy(lsm.session.name, handle->session_name,
952 sizeof(lsm.session.name));
953 if (ret) {
954 ret = -LTTNG_ERR_INVALID;
955 goto end;
956 }
957
958 ret = lttng_event_context_serialize(ctx, &payload);
959 if (ret) {
960 ret = -LTTNG_ERR_INVALID;
961 goto end;
962 }
963
964 lsm.u.context.length = payload.buffer.size - sizeof(lsm);
965
966 /* Update message header. */
967 memcpy(payload.buffer.data, &lsm, sizeof(lsm));
968
969 {
970 struct lttng_payload reply;
971 struct lttng_payload_view payload_view =
972 lttng_payload_view_from_payload(&payload, 0,
973 -1);
974
975 lttng_payload_init(&reply);
976 ret = lttng_ctl_ask_sessiond_payload(&payload_view, &reply);
977 lttng_payload_reset(&reply);
978 if (ret) {
979 goto end;
980 }
981 }
982
983 end:
984 lttng_payload_reset(&payload);
985 return ret;
986 }
987
988 /*
989 * Enable event(s) for a channel.
990 *
991 * If no event name is specified, all events are enabled.
992 * If no channel name is specified, the default 'channel0' is used.
993 *
994 * Returns size of returned session payload data or a negative error code.
995 */
996 int lttng_enable_event(struct lttng_handle *handle,
997 struct lttng_event *ev, const char *channel_name)
998 {
999 return lttng_enable_event_with_exclusions(handle, ev, channel_name,
1000 NULL, 0, NULL);
1001 }
1002
1003 /*
1004 * Create or enable an event with a filter expression.
1005 *
1006 * Return negative error value on error.
1007 * Return size of returned session payload data if OK.
1008 */
1009 int lttng_enable_event_with_filter(struct lttng_handle *handle,
1010 struct lttng_event *event, const char *channel_name,
1011 const char *filter_expression)
1012 {
1013 return lttng_enable_event_with_exclusions(handle, event, channel_name,
1014 filter_expression, 0, NULL);
1015 }
1016
1017 /*
1018 * Depending on the event, return a newly allocated agent filter expression or
1019 * NULL if not applicable.
1020 *
1021 * An event with NO loglevel and the name is * will return NULL.
1022 */
1023 static char *set_agent_filter(const char *filter, struct lttng_event *ev)
1024 {
1025 int err;
1026 char *agent_filter = NULL;
1027
1028 LTTNG_ASSERT(ev);
1029
1030 /* Don't add filter for the '*' event. */
1031 if (strcmp(ev->name, "*") != 0) {
1032 if (filter) {
1033 err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter,
1034 ev->name);
1035 } else {
1036 err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name);
1037 }
1038 if (err < 0) {
1039 PERROR("asprintf");
1040 goto error;
1041 }
1042 }
1043
1044 /* Add loglevel filtering if any for the JUL domain. */
1045 if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
1046 const char *op;
1047
1048 if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) {
1049 op = ">=";
1050 } else {
1051 op = "==";
1052 }
1053
1054 if (filter || agent_filter) {
1055 char *new_filter;
1056
1057 err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)",
1058 agent_filter ? agent_filter : filter, op,
1059 ev->loglevel);
1060 if (agent_filter) {
1061 free(agent_filter);
1062 }
1063 agent_filter = new_filter;
1064 } else {
1065 err = asprintf(&agent_filter, "int_loglevel %s %d", op,
1066 ev->loglevel);
1067 }
1068 if (err < 0) {
1069 PERROR("asprintf");
1070 goto error;
1071 }
1072 }
1073
1074 return agent_filter;
1075 error:
1076 free(agent_filter);
1077 return NULL;
1078 }
1079
1080 /*
1081 * Enable event(s) for a channel, possibly with exclusions and a filter.
1082 * If no event name is specified, all events are enabled.
1083 * If no channel name is specified, the default name is used.
1084 * If filter expression is not NULL, the filter is set for the event.
1085 * If exclusion count is not zero, the exclusions are set for the event.
1086 * Returns size of returned session payload data or a negative error code.
1087 */
1088 int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
1089 struct lttng_event *ev, const char *channel_name,
1090 const char *original_filter_expression,
1091 int exclusion_count, char **exclusion_list)
1092 {
1093 struct lttcomm_session_msg lsm = {
1094 .cmd_type = LTTNG_ENABLE_EVENT,
1095 .session = {},
1096 .domain = {},
1097 .u = {},
1098 .fd_count = 0,
1099 };
1100 struct lttng_payload payload;
1101 int ret = 0;
1102 unsigned int free_filter_expression = 0;
1103 struct filter_parser_ctx *ctx = NULL;
1104 size_t bytecode_len = 0;
1105
1106 /*
1107 * We have either a filter or some exclusions, so we need to set up
1108 * a variable-length payload from where to send the data.
1109 */
1110 lttng_payload_init(&payload);
1111
1112 /*
1113 * Cast as non-const since we may replace the filter expression
1114 * by a dynamically allocated string. Otherwise, the original
1115 * string is not modified.
1116 */
1117 char *filter_expression = (char *) original_filter_expression;
1118
1119 if (handle == NULL || ev == NULL) {
1120 ret = -LTTNG_ERR_INVALID;
1121 goto error;
1122 }
1123
1124 /*
1125 * Empty filter string will always be rejected by the parser
1126 * anyway, so treat this corner-case early to eliminate
1127 * lttng_fmemopen error for 0-byte allocation.
1128 */
1129 if (filter_expression && filter_expression[0] == '\0') {
1130 ret = -LTTNG_ERR_INVALID;
1131 goto error;
1132 }
1133
1134 if (ev->name[0] == '\0') {
1135 /* Enable all events. */
1136 ret = lttng_strncpy(ev->name, "*", sizeof(ev->name));
1137 LTTNG_ASSERT(ret == 0);
1138 }
1139
1140 /* Parse filter expression. */
1141 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1142 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1143 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1144 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1145 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1146 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1147 char *agent_filter;
1148
1149 /* Setup agent filter if needed. */
1150 agent_filter = set_agent_filter(filter_expression, ev);
1151 if (!agent_filter) {
1152 if (!filter_expression) {
1153 /*
1154 * No JUL and no filter, just skip
1155 * everything below.
1156 */
1157 goto serialize;
1158 }
1159 } else {
1160 /*
1161 * With an agent filter, the original filter has
1162 * been added to it thus replace the filter
1163 * expression.
1164 */
1165 filter_expression = agent_filter;
1166 free_filter_expression = 1;
1167 }
1168 }
1169
1170 if (strnlen(filter_expression, LTTNG_FILTER_MAX_LEN) ==
1171 LTTNG_FILTER_MAX_LEN) {
1172 ret = -LTTNG_ERR_FILTER_INVAL;
1173 goto error;
1174 }
1175
1176 ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx);
1177 if (ret) {
1178 goto error;
1179 }
1180
1181 bytecode_len = bytecode_get_len(&ctx->bytecode->b) +
1182 sizeof(ctx->bytecode->b);
1183 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
1184 ret = -LTTNG_ERR_FILTER_INVAL;
1185 goto error;
1186 }
1187 }
1188
1189 serialize:
1190 ret = lttng_event_serialize(ev, exclusion_count, exclusion_list,
1191 filter_expression, bytecode_len,
1192 (ctx && bytecode_len) ? &ctx->bytecode->b : NULL,
1193 &payload);
1194 if (ret) {
1195 ret = -LTTNG_ERR_INVALID;
1196 goto error;
1197 }
1198
1199 /* If no channel name, send empty string. */
1200 ret = lttng_strncpy(lsm.u.enable.channel_name, channel_name ?: "",
1201 sizeof(lsm.u.enable.channel_name));
1202 if (ret) {
1203 ret = -LTTNG_ERR_INVALID;
1204 goto error;
1205 }
1206
1207 /* Domain */
1208 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1209
1210 /* Session name */
1211 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1212 sizeof(lsm.session.name));
1213 if (ret) {
1214 ret = -LTTNG_ERR_INVALID;
1215 goto error;
1216 }
1217
1218 /* Length of the serialized event. */
1219 lsm.u.enable.length = (uint32_t) payload.buffer.size;
1220
1221 {
1222 struct lttng_payload_view view = lttng_payload_view_from_payload(
1223 &payload, 0, -1);
1224 int fd_count = lttng_payload_view_get_fd_handle_count(&view);
1225 int fd_to_send;
1226
1227 if (fd_count < 0) {
1228 goto error;
1229 }
1230
1231 LTTNG_ASSERT(fd_count == 0 || fd_count == 1);
1232 if (fd_count == 1) {
1233 struct fd_handle *h =
1234 lttng_payload_view_pop_fd_handle(&view);
1235
1236 if (!h) {
1237 goto error;
1238 }
1239
1240 fd_to_send = fd_handle_get_fd(h);
1241 fd_handle_put(h);
1242 }
1243
1244 lsm.fd_count = fd_count;
1245
1246 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
1247 fd_count ? &fd_to_send : NULL, fd_count,
1248 view.buffer.size ? view.buffer.data : NULL,
1249 view.buffer.size, NULL, NULL, 0);
1250 }
1251
1252 error:
1253 if (filter_expression && ctx) {
1254 filter_bytecode_free(ctx);
1255 filter_ir_free(ctx);
1256 filter_parser_ctx_free(ctx);
1257 }
1258 if (free_filter_expression) {
1259 /*
1260 * The filter expression has been replaced and must be freed as
1261 * it is not the original filter expression received as a
1262 * parameter.
1263 */
1264 free(filter_expression);
1265 }
1266 /*
1267 * Return directly to the caller and don't ask the sessiond since
1268 * something went wrong in the parsing of data above.
1269 */
1270 lttng_payload_reset(&payload);
1271 return ret;
1272 }
1273
1274 int lttng_disable_event_ext(struct lttng_handle *handle,
1275 struct lttng_event *ev, const char *channel_name,
1276 const char *original_filter_expression)
1277 {
1278 struct lttcomm_session_msg lsm = {
1279 .cmd_type = LTTNG_DISABLE_EVENT,
1280 .session = {},
1281 .domain = {},
1282 .u = {},
1283 .fd_count = 0,
1284 };
1285 struct lttng_payload payload;
1286 int ret = 0;
1287 unsigned int free_filter_expression = 0;
1288 struct filter_parser_ctx *ctx = NULL;
1289 size_t bytecode_len = 0;
1290
1291 /*
1292 * We have either a filter or some exclusions, so we need to set up
1293 * a variable-length payload from where to send the data.
1294 */
1295 lttng_payload_init(&payload);
1296
1297 /*
1298 * Cast as non-const since we may replace the filter expression
1299 * by a dynamically allocated string. Otherwise, the original
1300 * string is not modified.
1301 */
1302 char *filter_expression = (char *) original_filter_expression;
1303
1304 if (handle == NULL || ev == NULL) {
1305 ret = -LTTNG_ERR_INVALID;
1306 goto error;
1307 }
1308
1309 /*
1310 * Empty filter string will always be rejected by the parser
1311 * anyway, so treat this corner-case early to eliminate
1312 * lttng_fmemopen error for 0-byte allocation.
1313 */
1314 if (filter_expression && filter_expression[0] == '\0') {
1315 ret = -LTTNG_ERR_INVALID;
1316 goto error;
1317 }
1318
1319 /* Parse filter expression. */
1320 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1321 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1322 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1323 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1324 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1325 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1326 char *agent_filter;
1327
1328 /* Setup agent filter if needed. */
1329 agent_filter = set_agent_filter(filter_expression, ev);
1330 if (!agent_filter) {
1331 if (!filter_expression) {
1332 /*
1333 * No JUL and no filter, just skip
1334 * everything below.
1335 */
1336 goto serialize;
1337 }
1338 } else {
1339 /*
1340 * With an agent filter, the original filter has
1341 * been added to it thus replace the filter
1342 * expression.
1343 */
1344 filter_expression = agent_filter;
1345 free_filter_expression = 1;
1346 }
1347 }
1348
1349 if (strnlen(filter_expression, LTTNG_FILTER_MAX_LEN) ==
1350 LTTNG_FILTER_MAX_LEN) {
1351 ret = -LTTNG_ERR_FILTER_INVAL;
1352 goto error;
1353 }
1354
1355 ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx);
1356 if (ret) {
1357 goto error;
1358 }
1359
1360 bytecode_len = bytecode_get_len(&ctx->bytecode->b) +
1361 sizeof(ctx->bytecode->b);
1362 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
1363 ret = -LTTNG_ERR_FILTER_INVAL;
1364 goto error;
1365 }
1366 }
1367
1368 serialize:
1369 ret = lttng_event_serialize(ev, 0, NULL,
1370 filter_expression, bytecode_len,
1371 (ctx && bytecode_len) ? &ctx->bytecode->b : NULL,
1372 &payload);
1373 if (ret) {
1374 ret = -LTTNG_ERR_INVALID;
1375 goto error;
1376 }
1377
1378 /* If no channel name, send empty string. */
1379 ret = lttng_strncpy(lsm.u.disable.channel_name, channel_name ?: "",
1380 sizeof(lsm.u.disable.channel_name));
1381 if (ret) {
1382 ret = -LTTNG_ERR_INVALID;
1383 goto error;
1384 }
1385
1386 /* Domain */
1387 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1388
1389 /* Session name */
1390 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1391 sizeof(lsm.session.name));
1392 if (ret) {
1393 ret = -LTTNG_ERR_INVALID;
1394 goto error;
1395 }
1396
1397 /* Length of the serialized event. */
1398 lsm.u.disable.length = (uint32_t) payload.buffer.size;
1399
1400 {
1401 struct lttng_payload_view view = lttng_payload_view_from_payload(
1402 &payload, 0, -1);
1403 int fd_count = lttng_payload_view_get_fd_handle_count(&view);
1404 int fd_to_send;
1405
1406 if (fd_count < 0) {
1407 goto error;
1408 }
1409
1410 LTTNG_ASSERT(fd_count == 0 || fd_count == 1);
1411 if (fd_count == 1) {
1412 struct fd_handle *h =
1413 lttng_payload_view_pop_fd_handle(&view);
1414
1415 if (!h) {
1416 goto error;
1417 }
1418
1419 fd_to_send = fd_handle_get_fd(h);
1420 fd_handle_put(h);
1421 }
1422
1423 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
1424 fd_count ? &fd_to_send : NULL, fd_count,
1425 view.buffer.size ? view.buffer.data : NULL,
1426 view.buffer.size, NULL, NULL, 0);
1427 }
1428
1429 error:
1430 if (filter_expression && ctx) {
1431 filter_bytecode_free(ctx);
1432 filter_ir_free(ctx);
1433 filter_parser_ctx_free(ctx);
1434 }
1435 if (free_filter_expression) {
1436 /*
1437 * The filter expression has been replaced and must be freed as
1438 * it is not the original filter expression received as a
1439 * parameter.
1440 */
1441 free(filter_expression);
1442 }
1443 /*
1444 * Return directly to the caller and don't ask the sessiond since
1445 * something went wrong in the parsing of data above.
1446 */
1447 lttng_payload_reset(&payload);
1448 return ret;
1449 }
1450
1451 /*
1452 * Disable event(s) of a channel and domain.
1453 * If no event name is specified, all events are disabled.
1454 * If no channel name is specified, the default 'channel0' is used.
1455 * Returns size of returned session payload data or a negative error code.
1456 */
1457 int lttng_disable_event(struct lttng_handle *handle, const char *name,
1458 const char *channel_name)
1459 {
1460 int ret;
1461 struct lttng_event ev;
1462
1463 memset(&ev, 0, sizeof(ev));
1464 ev.loglevel = -1;
1465 ev.type = LTTNG_EVENT_ALL;
1466 ret = lttng_strncpy(ev.name, name ?: "", sizeof(ev.name));
1467 if (ret) {
1468 ret = -LTTNG_ERR_INVALID;
1469 goto end;
1470 }
1471
1472 ret = lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1473 end:
1474 return ret;
1475 }
1476
1477 struct lttng_channel *lttng_channel_create(struct lttng_domain *domain)
1478 {
1479 struct lttng_channel *channel = NULL;
1480
1481 if (!domain) {
1482 goto end;
1483 }
1484
1485 /* Validate domain. */
1486 switch (domain->type) {
1487 case LTTNG_DOMAIN_UST:
1488 switch (domain->buf_type) {
1489 case LTTNG_BUFFER_PER_UID:
1490 case LTTNG_BUFFER_PER_PID:
1491 break;
1492 default:
1493 goto end;
1494 }
1495 break;
1496 case LTTNG_DOMAIN_KERNEL:
1497 if (domain->buf_type != LTTNG_BUFFER_GLOBAL) {
1498 goto end;
1499 }
1500 break;
1501 default:
1502 goto end;
1503 }
1504
1505 channel = lttng_channel_create_internal();
1506 if (!channel) {
1507 goto end;
1508 }
1509
1510 lttng_channel_set_default_attr(domain, &channel->attr);
1511 end:
1512 return channel;
1513 }
1514
1515 void lttng_channel_destroy(struct lttng_channel *channel)
1516 {
1517 if (!channel) {
1518 return;
1519 }
1520
1521 if (channel->attr.extended.ptr) {
1522 free(channel->attr.extended.ptr);
1523 }
1524 free(channel);
1525 }
1526
1527 /*
1528 * Enable channel per domain
1529 * Returns size of returned session payload data or a negative error code.
1530 */
1531 int lttng_enable_channel(struct lttng_handle *handle,
1532 struct lttng_channel *in_chan)
1533 {
1534 enum lttng_error_code ret_code;
1535 int ret;
1536 struct lttng_dynamic_buffer buffer;
1537 struct lttcomm_session_msg lsm;
1538 uint64_t total_buffer_size_needed_per_cpu = 0;
1539 struct lttng_channel *channel = NULL;
1540
1541 lttng_dynamic_buffer_init(&buffer);
1542
1543 /* NULL arguments are forbidden. No default values. */
1544 if (handle == NULL || in_chan == NULL) {
1545 ret = -LTTNG_ERR_INVALID;
1546 goto end;
1547 }
1548
1549 /*
1550 * Verify that the amount of memory required to create the requested
1551 * buffer is available on the system at the moment.
1552 */
1553 if (in_chan->attr.num_subbuf >
1554 UINT64_MAX / in_chan->attr.subbuf_size) {
1555 /* Overflow */
1556 ret = -LTTNG_ERR_OVERFLOW;
1557 goto end;
1558 }
1559
1560 total_buffer_size_needed_per_cpu =
1561 in_chan->attr.num_subbuf * in_chan->attr.subbuf_size;
1562 ret_code = check_enough_available_memory(
1563 total_buffer_size_needed_per_cpu);
1564 if (ret_code != LTTNG_OK) {
1565 ret = -ret_code;
1566 goto end;
1567 }
1568
1569 /* Copy the channel for easier manipulation. */
1570 channel = lttng_channel_copy(in_chan);
1571 if (!channel) {
1572 ret = -LTTNG_ERR_NOMEM;
1573 goto end;
1574 }
1575
1576 /* Populate the channel extended attribute if necessary. */
1577 if (!channel->attr.extended.ptr) {
1578 struct lttng_channel_extended *extended =
1579 zmalloc<lttng_channel_extended>();
1580
1581 if (!extended) {
1582 ret = -LTTNG_ERR_NOMEM;
1583 goto end;
1584 }
1585
1586 lttng_channel_set_default_extended_attr(
1587 &handle->domain, extended);
1588 channel->attr.extended.ptr = extended;
1589 }
1590
1591 /* Prepare the payload */
1592 memset(&lsm, 0, sizeof(lsm));
1593
1594 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
1595 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1596
1597 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1598 sizeof(lsm.session.name));
1599 if (ret) {
1600 ret = -LTTNG_ERR_INVALID;
1601 goto end;
1602 }
1603
1604 ret = lttng_channel_serialize(channel, &buffer);
1605 if (ret) {
1606 ret = -LTTNG_ERR_FATAL;
1607 goto end;
1608 }
1609
1610 lsm.u.channel.length = buffer.size;
1611
1612 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1613 &lsm, buffer.data, buffer.size, NULL);
1614 end:
1615 lttng_channel_destroy(channel);
1616 lttng_dynamic_buffer_reset(&buffer);
1617 return ret;
1618 }
1619
1620 /*
1621 * All tracing will be stopped for registered events of the channel.
1622 * Returns size of returned session payload data or a negative error code.
1623 */
1624 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
1625 {
1626 int ret;
1627 struct lttcomm_session_msg lsm;
1628
1629 /* Safety check. Both are mandatory. */
1630 if (handle == NULL || name == NULL) {
1631 return -LTTNG_ERR_INVALID;
1632 }
1633
1634 memset(&lsm, 0, sizeof(lsm));
1635
1636 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1637
1638 ret = lttng_strncpy(lsm.u.disable.channel_name, name,
1639 sizeof(lsm.u.disable.channel_name));
1640 if (ret) {
1641 ret = -LTTNG_ERR_INVALID;
1642 goto end;
1643 }
1644
1645 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1646
1647 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1648 sizeof(lsm.session.name));
1649 if (ret) {
1650 ret = -LTTNG_ERR_INVALID;
1651 goto end;
1652 }
1653
1654 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1655 end:
1656 return ret;
1657 }
1658
1659 /*
1660 * Lists all available tracepoints of domain.
1661 * Sets the contents of the events array.
1662 * Returns the number of lttng_event entries in events;
1663 * on error, returns a negative value.
1664 */
1665 int lttng_list_tracepoints(struct lttng_handle *handle,
1666 struct lttng_event **events)
1667 {
1668 enum lttng_error_code ret_code;
1669 int ret, total_payload_received;
1670 char *reception_buffer = NULL;
1671 struct lttcomm_session_msg lsm = {
1672 .cmd_type = LTTNG_LIST_TRACEPOINTS,
1673 .session = {},
1674 .domain = {},
1675 .u = {},
1676 .fd_count = 0,
1677 };
1678 struct lttcomm_list_command_header *cmd_header = NULL;
1679 size_t cmd_header_len;
1680 unsigned int nb_events = 0;
1681
1682 if (handle == NULL) {
1683 ret = -LTTNG_ERR_INVALID;
1684 goto end;
1685 }
1686
1687 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1688
1689 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
1690 (void **) &reception_buffer, (void **) &cmd_header,
1691 &cmd_header_len);
1692 if (ret < 0) {
1693 goto end;
1694 }
1695
1696 total_payload_received = ret;
1697
1698 if (!cmd_header) {
1699 ret = -LTTNG_ERR_UNK;
1700 goto end;
1701 }
1702
1703 if (cmd_header->count > INT_MAX) {
1704 ret = -LTTNG_ERR_OVERFLOW;
1705 goto end;
1706 }
1707
1708 nb_events = (unsigned int) cmd_header->count;
1709
1710 {
1711 struct lttng_buffer_view events_view =
1712 lttng_buffer_view_init(reception_buffer, 0,
1713 total_payload_received);
1714 struct lttng_payload_view events_payload_view =
1715 lttng_payload_view_from_buffer_view(
1716 &events_view, 0, -1);
1717
1718 ret_code = lttng_events_create_and_flatten_from_payload(
1719 &events_payload_view, nb_events, events);
1720 if (ret_code != LTTNG_OK) {
1721 ret = -ret_code;
1722 goto end;
1723 }
1724 }
1725
1726 ret = (int) nb_events;
1727
1728 end:
1729 free(cmd_header);
1730 free(reception_buffer);
1731 return ret;
1732 }
1733
1734 /*
1735 * Lists all available tracepoint fields of domain.
1736 * Sets the contents of the event field array.
1737 * Returns the number of lttng_event_field entries in events;
1738 * on error, returns a negative value.
1739 */
1740 int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1741 struct lttng_event_field **fields)
1742 {
1743 enum lttng_error_code ret_code;
1744 int ret;
1745 struct lttcomm_session_msg lsm;
1746 const struct lttcomm_list_command_header *cmd_header = NULL;
1747 unsigned int nb_event_fields = 0;
1748 struct lttng_payload reply;
1749
1750 lttng_payload_init(&reply);
1751
1752 if (handle == NULL) {
1753 ret = -LTTNG_ERR_INVALID;
1754 goto end;
1755 }
1756
1757 memset(&lsm, 0, sizeof(lsm));
1758 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
1759 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1760
1761 {
1762 lttng_payload_view message_view =
1763 lttng_payload_view_init_from_buffer(
1764 (const char *) &lsm, 0,
1765 sizeof(lsm));
1766
1767 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
1768 if (ret < 0) {
1769 goto end;
1770 }
1771 }
1772
1773 {
1774 const lttng_buffer_view cmd_header_view =
1775 lttng_buffer_view_from_dynamic_buffer(
1776 &reply.buffer, 0, sizeof(*cmd_header));
1777
1778 if (!lttng_buffer_view_is_valid(&cmd_header_view)) {
1779 ret = -LTTNG_ERR_INVALID_PROTOCOL;
1780 goto end;
1781 }
1782
1783 cmd_header = (struct lttcomm_list_command_header *)
1784 cmd_header_view.data;
1785 }
1786
1787 if (cmd_header->count > INT_MAX) {
1788 ret = -LTTNG_ERR_OVERFLOW;
1789 goto end;
1790 }
1791
1792 nb_event_fields = cmd_header->count;
1793
1794 {
1795 lttng_payload_view reply_view =
1796 lttng_payload_view_from_payload(&reply,
1797 sizeof(*cmd_header), -1);
1798
1799 ret_code = lttng_event_fields_create_and_flatten_from_payload(
1800 &reply_view, nb_event_fields, fields);
1801 if (ret_code != LTTNG_OK) {
1802 ret = -ret_code;
1803 goto end;
1804 }
1805 }
1806
1807 ret = nb_event_fields;
1808
1809 end:
1810 lttng_payload_reset(&reply);
1811 return ret;
1812 }
1813
1814 /*
1815 * Lists all available kernel system calls. Allocates and sets the contents of
1816 * the events array.
1817 *
1818 * Returns the number of lttng_event entries in events; on error, returns a
1819 * negative value.
1820 */
1821 int lttng_list_syscalls(struct lttng_event **events)
1822 {
1823 enum lttng_error_code ret_code;
1824 int ret, total_payload_received;
1825 char *reception_buffer = NULL;
1826 struct lttcomm_session_msg lsm = {};
1827 struct lttcomm_list_command_header *cmd_header = NULL;
1828 size_t cmd_header_len;
1829 uint32_t nb_events = 0;
1830
1831 if (!events) {
1832 ret = -LTTNG_ERR_INVALID;
1833 goto end;
1834 }
1835
1836 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1837 /* Force kernel domain for system calls. */
1838 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1839
1840 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
1841 (void **) &reception_buffer, (void **) &cmd_header,
1842 &cmd_header_len);
1843 if (ret < 0) {
1844 goto end;
1845 }
1846 total_payload_received = ret;
1847
1848 if (!cmd_header) {
1849 ret = -LTTNG_ERR_UNK;
1850 goto end;
1851 }
1852
1853 if (cmd_header->count > INT_MAX) {
1854 ret = -LTTNG_ERR_OVERFLOW;
1855 goto end;
1856 }
1857
1858 nb_events = (unsigned int) cmd_header->count;
1859
1860 {
1861 const struct lttng_buffer_view events_view =
1862 lttng_buffer_view_init(reception_buffer, 0,
1863 total_payload_received);
1864 struct lttng_payload_view events_payload_view =
1865 lttng_payload_view_from_buffer_view(
1866 &events_view, 0, -1);
1867
1868 ret_code = lttng_events_create_and_flatten_from_payload(
1869 &events_payload_view, nb_events, events);
1870 if (ret_code != LTTNG_OK) {
1871 ret = -ret_code;
1872 goto end;
1873 }
1874 }
1875
1876 ret = (int) nb_events;
1877
1878 end:
1879 free(reception_buffer);
1880 free(cmd_header);
1881 return ret;
1882 }
1883
1884 /*
1885 * Returns a human readable string describing
1886 * the error code (positive or negative value).
1887 */
1888 const char *lttng_strerror(int code)
1889 {
1890 if (code > 0) {
1891 code = -code;
1892 }
1893
1894 return error_get_str(code);
1895 }
1896
1897 enum lttng_error_code lttng_create_session_ext(
1898 struct lttng_session_descriptor *session_descriptor)
1899 {
1900 enum lttng_error_code ret_code;
1901 struct lttcomm_session_msg lsm = {
1902 .cmd_type = LTTNG_CREATE_SESSION_EXT,
1903 .session = {},
1904 .domain = {},
1905 .u = {},
1906 .fd_count = 0,
1907 };
1908 void *reply = NULL;
1909 struct lttng_buffer_view reply_view;
1910 int reply_ret;
1911 bool sessiond_must_generate_ouput;
1912 struct lttng_dynamic_buffer payload;
1913 int ret;
1914 size_t descriptor_size;
1915 struct lttng_session_descriptor *descriptor_reply = NULL;
1916
1917 lttng_dynamic_buffer_init(&payload);
1918 if (!session_descriptor) {
1919 ret_code = LTTNG_ERR_INVALID;
1920 goto end;
1921 }
1922
1923 sessiond_must_generate_ouput =
1924 !lttng_session_descriptor_is_output_destination_initialized(
1925 session_descriptor);
1926 if (sessiond_must_generate_ouput) {
1927 const char *home_dir = utils_get_home_dir();
1928 size_t home_dir_len = home_dir ? strlen(home_dir) + 1 : 0;
1929
1930 if (!home_dir || home_dir_len > LTTNG_PATH_MAX) {
1931 ret_code = LTTNG_ERR_FATAL;
1932 goto end;
1933 }
1934
1935 lsm.u.create_session.home_dir_size = (uint16_t) home_dir_len;
1936 ret = lttng_dynamic_buffer_append(&payload, home_dir,
1937 home_dir_len);
1938 if (ret) {
1939 ret_code = LTTNG_ERR_NOMEM;
1940 goto end;
1941 }
1942 }
1943
1944 descriptor_size = payload.size;
1945 ret = lttng_session_descriptor_serialize(session_descriptor,
1946 &payload);
1947 if (ret) {
1948 ret_code = LTTNG_ERR_INVALID;
1949 goto end;
1950 }
1951 descriptor_size = payload.size - descriptor_size;
1952 lsm.u.create_session.session_descriptor_size = descriptor_size;
1953
1954 /* Command returns a session descriptor on success. */
1955 reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, payload.data,
1956 payload.size, &reply);
1957 if (reply_ret < 0) {
1958 ret_code = (lttng_error_code) -reply_ret;
1959 goto end;
1960 } else if (reply_ret == 0) {
1961 /* Socket unexpectedly closed by the session daemon. */
1962 ret_code = LTTNG_ERR_FATAL;
1963 goto end;
1964 }
1965
1966 reply_view = lttng_buffer_view_init((const char *) reply, 0, reply_ret);
1967 ret = lttng_session_descriptor_create_from_buffer(&reply_view,
1968 &descriptor_reply);
1969 if (ret < 0) {
1970 ret_code = LTTNG_ERR_FATAL;
1971 goto end;
1972 }
1973 ret_code = LTTNG_OK;
1974 lttng_session_descriptor_assign(session_descriptor, descriptor_reply);
1975 end:
1976 free(reply);
1977 lttng_dynamic_buffer_reset(&payload);
1978 lttng_session_descriptor_destroy(descriptor_reply);
1979 return ret_code;
1980 }
1981
1982 /*
1983 * Create a new session using name and url for destination.
1984 *
1985 * Return 0 on success else a negative LTTng error code.
1986 */
1987 int lttng_create_session(const char *name, const char *url)
1988 {
1989 int ret;
1990 ssize_t size;
1991 struct lttng_uri *uris = NULL;
1992 struct lttng_session_descriptor *descriptor = NULL;
1993 enum lttng_error_code ret_code;
1994
1995 if (!name) {
1996 ret = -LTTNG_ERR_INVALID;
1997 goto end;
1998 }
1999
2000 size = uri_parse_str_urls(url, NULL, &uris);
2001 if (size < 0) {
2002 ret = -LTTNG_ERR_INVALID;
2003 goto end;
2004 }
2005 switch (size) {
2006 case 0:
2007 descriptor = lttng_session_descriptor_create(name);
2008 break;
2009 case 1:
2010 if (uris[0].dtype != LTTNG_DST_PATH) {
2011 ret = -LTTNG_ERR_INVALID;
2012 goto end;
2013 }
2014 descriptor = lttng_session_descriptor_local_create(name,
2015 uris[0].dst.path);
2016 break;
2017 case 2:
2018 descriptor = lttng_session_descriptor_network_create(name, url,
2019 NULL);
2020 break;
2021 default:
2022 ret = -LTTNG_ERR_INVALID;
2023 goto end;
2024 }
2025 if (!descriptor) {
2026 ret = -LTTNG_ERR_INVALID;
2027 goto end;
2028 }
2029 ret_code = lttng_create_session_ext(descriptor);
2030 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
2031 end:
2032 lttng_session_descriptor_destroy(descriptor);
2033 free(uris);
2034 return ret;
2035 }
2036
2037 /*
2038 * Create a session exclusively used for snapshot.
2039 *
2040 * Return 0 on success else a negative LTTng error code.
2041 */
2042 int lttng_create_session_snapshot(const char *name, const char *snapshot_url)
2043 {
2044 int ret;
2045 enum lttng_error_code ret_code;
2046 ssize_t size;
2047 struct lttng_uri *uris = NULL;
2048 struct lttng_session_descriptor *descriptor = NULL;
2049
2050 if (!name) {
2051 ret = -LTTNG_ERR_INVALID;
2052 goto end;
2053 }
2054
2055 size = uri_parse_str_urls(snapshot_url, NULL, &uris);
2056 if (size < 0) {
2057 ret = -LTTNG_ERR_INVALID;
2058 goto end;
2059 }
2060 /*
2061 * If the user does not specify a custom subdir, use the session name.
2062 */
2063 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH &&
2064 strlen(uris[0].subdir) == 0) {
2065 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s",
2066 name);
2067 if (ret < 0) {
2068 PERROR("Failed to set session name as network destination sub-directory");
2069 ret = -LTTNG_ERR_FATAL;
2070 goto end;
2071 } else if (ret >= sizeof(uris[0].subdir)) {
2072 /* Truncated output. */
2073 ret = -LTTNG_ERR_INVALID;
2074 goto end;
2075 }
2076 }
2077
2078 switch (size) {
2079 case 0:
2080 descriptor = lttng_session_descriptor_snapshot_create(name);
2081 break;
2082 case 1:
2083 if (uris[0].dtype != LTTNG_DST_PATH) {
2084 ret = -LTTNG_ERR_INVALID;
2085 goto end;
2086 }
2087 descriptor = lttng_session_descriptor_snapshot_local_create(
2088 name,
2089 uris[0].dst.path);
2090 break;
2091 case 2:
2092 descriptor = lttng_session_descriptor_snapshot_network_create(
2093 name,
2094 snapshot_url,
2095 NULL);
2096 break;
2097 default:
2098 ret = -LTTNG_ERR_INVALID;
2099 goto end;
2100 }
2101 if (!descriptor) {
2102 ret = -LTTNG_ERR_INVALID;
2103 goto end;
2104 }
2105 ret_code = lttng_create_session_ext(descriptor);
2106 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
2107 end:
2108 lttng_session_descriptor_destroy(descriptor);
2109 free(uris);
2110 return ret;
2111 }
2112
2113 /*
2114 * Create a session exclusively used for live.
2115 *
2116 * Return 0 on success else a negative LTTng error code.
2117 */
2118 int lttng_create_session_live(const char *name, const char *url,
2119 unsigned int timer_interval)
2120 {
2121 int ret;
2122 enum lttng_error_code ret_code;
2123 struct lttng_session_descriptor *descriptor = NULL;
2124
2125 if (!name) {
2126 ret = -LTTNG_ERR_INVALID;
2127 goto end;
2128 }
2129
2130 if (url) {
2131 descriptor = lttng_session_descriptor_live_network_create(
2132 name, url, NULL, timer_interval);
2133 } else {
2134 descriptor = lttng_session_descriptor_live_create(
2135 name, timer_interval);
2136 }
2137 if (!descriptor) {
2138 ret = -LTTNG_ERR_INVALID;
2139 goto end;
2140 }
2141 ret_code = lttng_create_session_ext(descriptor);
2142 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
2143 end:
2144 lttng_session_descriptor_destroy(descriptor);
2145 return ret;
2146 }
2147
2148 /*
2149 * Stop the session and wait for the data before destroying it
2150 *
2151 * Return 0 on success else a negative LTTng error code.
2152 */
2153 int lttng_destroy_session(const char *session_name)
2154 {
2155 int ret;
2156 enum lttng_error_code ret_code;
2157 enum lttng_destruction_handle_status status;
2158 struct lttng_destruction_handle *handle = NULL;
2159
2160 /*
2161 * Stop the tracing and wait for the data to be
2162 * consumed.
2163 */
2164 ret = _lttng_stop_tracing(session_name, 1);
2165 if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
2166 goto end;
2167 }
2168
2169 ret_code = lttng_destroy_session_ext(session_name, &handle);
2170 if (ret_code != LTTNG_OK) {
2171 ret = (int) -ret_code;
2172 goto end;
2173 }
2174 LTTNG_ASSERT(handle);
2175
2176 /* Block until the completion of the destruction of the session. */
2177 status = lttng_destruction_handle_wait_for_completion(handle, -1);
2178 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED) {
2179 ret = -LTTNG_ERR_UNK;
2180 goto end;
2181 }
2182
2183 status = lttng_destruction_handle_get_result(handle, &ret_code);
2184 if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) {
2185 ret = -LTTNG_ERR_UNK;
2186 goto end;
2187 }
2188 ret = ret_code == LTTNG_OK ? 0 : -ret_code;
2189 end:
2190 lttng_destruction_handle_destroy(handle);
2191 return ret;
2192 }
2193
2194 /*
2195 * Destroy the session without waiting for the data.
2196 */
2197 int lttng_destroy_session_no_wait(const char *session_name)
2198 {
2199 enum lttng_error_code ret_code;
2200
2201 ret_code = lttng_destroy_session_ext(session_name, NULL);
2202 return ret_code == LTTNG_OK ? 0 : -ret_code;
2203 }
2204
2205 /*
2206 * Ask the session daemon for all available sessions.
2207 * Sets the contents of the sessions array.
2208 * Returns the number of lttng_session entries in sessions;
2209 * on error, returns a negative value.
2210 */
2211 int lttng_list_sessions(struct lttng_session **out_sessions)
2212 {
2213 int ret;
2214 struct lttcomm_session_msg lsm = {};
2215 struct lttng_payload reply;
2216 struct lttng_payload_view lsm_view =
2217 lttng_payload_view_init_from_buffer((const char *) &lsm, 0, sizeof(lsm));
2218 unsigned int nb_sessions = 0;
2219
2220 lttng_payload_init(&reply);
2221
2222 /* Initialize command parameters. */
2223 lsm.cmd_type = LTTNG_LIST_SESSIONS;
2224
2225 /* Execute command against the session daemon. */
2226 ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply);
2227 if (ret < 0) {
2228 goto end;
2229 }
2230
2231 {
2232 const struct lttcomm_list_command_header *cmd_reply_header = NULL;
2233 const lttng_payload_view cmd_reply_header_view = lttng_payload_view_from_payload(
2234 &reply, 0, sizeof(*cmd_reply_header));
2235
2236 if (!lttng_payload_view_is_valid(&cmd_reply_header_view)) {
2237 ret = -LTTNG_ERR_INVALID_PROTOCOL;
2238 goto end;
2239 }
2240
2241 cmd_reply_header = (const struct lttcomm_list_command_header *)
2242 cmd_reply_header_view.buffer.data;
2243 if (cmd_reply_header->count > INT_MAX) {
2244 ret = -LTTNG_ERR_OVERFLOW;
2245 goto end;
2246 }
2247
2248 nb_sessions = (unsigned int) cmd_reply_header->count;
2249 }
2250
2251 {
2252 enum lttng_error_code ret_code;
2253 lttng_payload_view cmd_reply_payload = lttng_payload_view_from_payload(
2254 &reply, sizeof(struct lttcomm_list_command_header), -1);
2255
2256 ret_code = lttng_sessions_create_and_flatten_from_payload(
2257 &cmd_reply_payload, nb_sessions, out_sessions);
2258 if (ret_code != LTTNG_OK) {
2259 ret = -((int) ret_code);
2260 goto end;
2261 }
2262 }
2263
2264 ret = (int) nb_sessions;
2265 end:
2266 lttng_payload_reset(&reply);
2267 return ret;
2268 }
2269
2270 enum lttng_error_code lttng_session_get_creation_time(
2271 const struct lttng_session *session, uint64_t *creation_time)
2272 {
2273 enum lttng_error_code ret = LTTNG_OK;
2274 struct lttng_session_extended *extended;
2275
2276 if (!session || !creation_time || !session->extended.ptr) {
2277 ret = LTTNG_ERR_INVALID;
2278 goto end;
2279 }
2280
2281 extended = (lttng_session_extended *) session->extended.ptr;
2282 if (!extended->creation_time.is_set) {
2283 /* Not created on the session daemon yet. */
2284 ret = LTTNG_ERR_SESSION_NOT_EXIST;
2285 goto end;
2286 }
2287 *creation_time = extended->creation_time.value;
2288 end:
2289 return ret;
2290 }
2291
2292 int lttng_set_session_shm_path(const char *session_name,
2293 const char *shm_path)
2294 {
2295 int ret;
2296 struct lttcomm_session_msg lsm;
2297
2298 if (session_name == NULL) {
2299 return -LTTNG_ERR_INVALID;
2300 }
2301
2302 memset(&lsm, 0, sizeof(lsm));
2303 lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH;
2304
2305 ret = lttng_strncpy(lsm.session.name, session_name,
2306 sizeof(lsm.session.name));
2307 if (ret) {
2308 ret = -LTTNG_ERR_INVALID;
2309 goto end;
2310 }
2311
2312 ret = lttng_strncpy(lsm.u.set_shm_path.shm_path, shm_path ?: "",
2313 sizeof(lsm.u.set_shm_path.shm_path));
2314 if (ret) {
2315 ret = -LTTNG_ERR_INVALID;
2316 goto end;
2317 }
2318
2319 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2320 end:
2321 return ret;
2322 }
2323
2324 /*
2325 * Ask the session daemon for all available domains of a session.
2326 * Sets the contents of the domains array.
2327 * Returns the number of lttng_domain entries in domains;
2328 * on error, returns a negative value.
2329 */
2330 int lttng_list_domains(const char *session_name,
2331 struct lttng_domain **domains)
2332 {
2333 int ret;
2334 struct lttcomm_session_msg lsm;
2335
2336 if (session_name == NULL) {
2337 ret = -LTTNG_ERR_INVALID;
2338 goto error;
2339 }
2340
2341 memset(&lsm, 0, sizeof(lsm));
2342 lsm.cmd_type = LTTNG_LIST_DOMAINS;
2343
2344 ret = lttng_strncpy(lsm.session.name, session_name,
2345 sizeof(lsm.session.name));
2346 if (ret) {
2347 ret = -LTTNG_ERR_INVALID;
2348 goto error;
2349 }
2350
2351 ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains);
2352 if (ret < 0) {
2353 goto error;
2354 }
2355
2356 return ret / sizeof(struct lttng_domain);
2357 error:
2358 return ret;
2359 }
2360
2361 /*
2362 * Ask the session daemon for all available channels of a session.
2363 * Sets the contents of the channels array.
2364 * Returns the number of lttng_channel entries in channels;
2365 * on error, returns a negative value.
2366 */
2367 int lttng_list_channels(struct lttng_handle *handle,
2368 struct lttng_channel **channels)
2369 {
2370 int ret, total_payload_received;
2371 struct lttcomm_session_msg lsm;
2372 char *reception_buffer = NULL;
2373 size_t cmd_header_len = 0;
2374 struct lttcomm_list_command_header *cmd_header = NULL;
2375 struct lttng_dynamic_buffer tmp_buffer;
2376
2377 lttng_dynamic_buffer_init(&tmp_buffer);
2378
2379 if (handle == NULL) {
2380 ret = -LTTNG_ERR_INVALID;
2381 goto end;
2382 }
2383
2384 memset(&lsm, 0, sizeof(lsm));
2385 lsm.cmd_type = LTTNG_LIST_CHANNELS;
2386 ret = lttng_strncpy(lsm.session.name, handle->session_name,
2387 sizeof(lsm.session.name));
2388 if (ret) {
2389 ret = -LTTNG_ERR_INVALID;
2390 goto end;
2391 }
2392
2393 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
2394
2395 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
2396 (void **) &reception_buffer, (void **) &cmd_header,
2397 &cmd_header_len);
2398 if (ret < 0) {
2399 goto end;
2400 }
2401
2402 total_payload_received = ret;
2403
2404 if (cmd_header_len != sizeof(*cmd_header)) {
2405 ret = -LTTNG_ERR_FATAL;
2406 goto end;
2407 }
2408
2409 if (!cmd_header) {
2410 ret = LTTNG_ERR_UNK;
2411 goto end;
2412 }
2413
2414 if (cmd_header->count > INT_MAX) {
2415 ret = -LTTNG_ERR_OVERFLOW;
2416 goto end;
2417 }
2418
2419 {
2420 enum lttng_error_code ret_code;
2421 const struct lttng_buffer_view events_view =
2422 lttng_buffer_view_init(reception_buffer, 0,
2423 total_payload_received);
2424
2425 ret_code = lttng_channels_create_and_flatten_from_buffer(
2426 &events_view, cmd_header->count, channels);
2427 if (ret_code != LTTNG_OK) {
2428 ret = -ret_code;
2429 goto end;
2430 }
2431 }
2432
2433 ret = (int) cmd_header->count;
2434 end:
2435 free(cmd_header);
2436 free(reception_buffer);
2437 return ret;
2438 }
2439
2440 /*
2441 * Ask the session daemon for all available events of a session channel.
2442 * Sets the contents of the events array.
2443 * Returns the number of lttng_event entries in events;
2444 * on error, returns a negative value.
2445 */
2446 int lttng_list_events(struct lttng_handle *handle,
2447 const char *channel_name, struct lttng_event **events)
2448 {
2449 int ret;
2450 struct lttcomm_session_msg lsm = {};
2451 struct lttng_payload reply;
2452 struct lttng_payload_view lsm_view =
2453 lttng_payload_view_init_from_buffer(
2454 (const char *) &lsm, 0, sizeof(lsm));
2455 unsigned int nb_events = 0;
2456
2457 lttng_payload_init(&reply);
2458
2459 /* Safety check. An handle and channel name are mandatory. */
2460 if (handle == NULL || channel_name == NULL) {
2461 ret = -LTTNG_ERR_INVALID;
2462 goto end;
2463 }
2464
2465 /* Initialize command parameters. */
2466 lsm.cmd_type = LTTNG_LIST_EVENTS;
2467 ret = lttng_strncpy(lsm.session.name, handle->session_name,
2468 sizeof(lsm.session.name));
2469 if (ret) {
2470 ret = -LTTNG_ERR_INVALID;
2471 goto end;
2472 }
2473
2474 ret = lttng_strncpy(lsm.u.list.channel_name, channel_name,
2475 sizeof(lsm.u.list.channel_name));
2476 if (ret) {
2477 ret = -LTTNG_ERR_INVALID;
2478 goto end;
2479 }
2480
2481 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
2482
2483 /* Execute command against the session daemon. */
2484 ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply);
2485 if (ret < 0) {
2486 goto end;
2487 }
2488
2489 {
2490 const struct lttcomm_list_command_header *cmd_reply_header =
2491 NULL;
2492 const lttng_payload_view cmd_reply_header_view =
2493 lttng_payload_view_from_payload(&reply, 0,
2494 sizeof(*cmd_reply_header));
2495
2496 if (!lttng_payload_view_is_valid(&cmd_reply_header_view)) {
2497 ret = -LTTNG_ERR_INVALID_PROTOCOL;
2498 goto end;
2499 }
2500
2501 cmd_reply_header = (const struct lttcomm_list_command_header *)
2502 cmd_reply_header_view.buffer
2503 .data;
2504 if (cmd_reply_header->count > INT_MAX) {
2505 ret = -LTTNG_ERR_OVERFLOW;
2506 goto end;
2507 }
2508
2509 nb_events = (unsigned int) cmd_reply_header->count;
2510 }
2511
2512 {
2513 enum lttng_error_code ret_code;
2514 lttng_payload_view cmd_reply_payload = lttng_payload_view_from_payload(
2515 &reply,
2516 sizeof(struct lttcomm_list_command_header), -1);
2517
2518 ret_code = lttng_events_create_and_flatten_from_payload(
2519 &cmd_reply_payload, nb_events, events);
2520 if (ret_code != LTTNG_OK) {
2521 ret = -((int) ret_code);
2522 goto end;
2523 }
2524 }
2525
2526 ret = (int) nb_events;
2527 end:
2528 lttng_payload_reset(&reply);
2529 return ret;
2530 }
2531
2532 /*
2533 * Sets the tracing_group variable with name.
2534 * This function allocates memory pointed to by tracing_group.
2535 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2536 */
2537 int lttng_set_tracing_group(const char *name)
2538 {
2539 int ret = 0;
2540 char *new_group;
2541
2542 if (name == NULL) {
2543 ret = -LTTNG_ERR_INVALID;
2544 goto end;
2545 }
2546
2547 new_group = strdup(name);
2548 if (!new_group) {
2549 ret = -LTTNG_ERR_FATAL;
2550 goto end;
2551 }
2552
2553 free(tracing_group);
2554 tracing_group = new_group;
2555 new_group = NULL;
2556
2557 end:
2558 return ret;
2559 }
2560
2561 int lttng_calibrate(struct lttng_handle *handle __attribute__((unused)),
2562 struct lttng_calibrate *calibrate __attribute__((unused)))
2563 {
2564 /*
2565 * This command was removed in LTTng 2.9.
2566 */
2567 return -LTTNG_ERR_UND;
2568 }
2569
2570 /*
2571 * Set default channel attributes.
2572 * If either or both of the arguments are null, attr content is zeroe'd.
2573 */
2574 void lttng_channel_set_default_attr(struct lttng_domain *domain,
2575 struct lttng_channel_attr *attr)
2576 {
2577 struct lttng_channel_extended *extended;
2578
2579 /* Safety check */
2580 if (attr == NULL || domain == NULL) {
2581 return;
2582 }
2583
2584 /* Save the pointer for later use */
2585 extended = (struct lttng_channel_extended *) attr->extended.ptr;
2586 memset(attr, 0, sizeof(struct lttng_channel_attr));
2587
2588 /* Same for all domains. */
2589 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
2590 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
2591 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
2592
2593 switch (domain->type) {
2594 case LTTNG_DOMAIN_KERNEL:
2595 attr->switch_timer_interval =
2596 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
2597 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
2598 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
2599 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
2600 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
2601 break;
2602 case LTTNG_DOMAIN_UST:
2603 switch (domain->buf_type) {
2604 case LTTNG_BUFFER_PER_UID:
2605 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
2606 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
2607 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
2608 attr->switch_timer_interval =
2609 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
2610 attr->read_timer_interval =
2611 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
2612 break;
2613 case LTTNG_BUFFER_PER_PID:
2614 default:
2615 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
2616 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
2617 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
2618 attr->switch_timer_interval =
2619 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
2620 attr->read_timer_interval =
2621 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
2622 break;
2623 }
2624 default:
2625 /* Default behavior: leave set to 0. */
2626 break;
2627 }
2628
2629 if (extended) {
2630 lttng_channel_set_default_extended_attr(domain, extended);
2631 }
2632
2633 /* Reassign the extended pointer. */
2634 attr->extended.ptr = extended;
2635 }
2636
2637 int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
2638 uint64_t *discarded_events)
2639 {
2640 int ret = 0;
2641 struct lttng_channel_extended *chan_ext;
2642
2643 if (!channel || !discarded_events) {
2644 ret = -LTTNG_ERR_INVALID;
2645 goto end;
2646 }
2647
2648 chan_ext = (lttng_channel_extended *) channel->attr.extended.ptr;
2649 if (!chan_ext) {
2650 /*
2651 * This can happen since the lttng_channel structure is
2652 * used for other tasks where this pointer is never set.
2653 */
2654 *discarded_events = 0;
2655 goto end;
2656 }
2657
2658 *discarded_events = chan_ext->discarded_events;
2659 end:
2660 return ret;
2661 }
2662
2663 int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2664 uint64_t *lost_packets)
2665 {
2666 int ret = 0;
2667 struct lttng_channel_extended *chan_ext;
2668
2669 if (!channel || !lost_packets) {
2670 ret = -LTTNG_ERR_INVALID;
2671 goto end;
2672 }
2673
2674 chan_ext = (lttng_channel_extended *) channel->attr.extended.ptr;
2675 if (!chan_ext) {
2676 /*
2677 * This can happen since the lttng_channel structure is
2678 * used for other tasks where this pointer is never set.
2679 */
2680 *lost_packets = 0;
2681 goto end;
2682 }
2683
2684 *lost_packets = chan_ext->lost_packets;
2685 end:
2686 return ret;
2687 }
2688
2689 int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
2690 uint64_t *monitor_timer_interval)
2691 {
2692 int ret = 0;
2693
2694 if (!chan || !monitor_timer_interval) {
2695 ret = -LTTNG_ERR_INVALID;
2696 goto end;
2697 }
2698
2699 if (!chan->attr.extended.ptr) {
2700 ret = -LTTNG_ERR_INVALID;
2701 goto end;
2702 }
2703
2704 *monitor_timer_interval = ((struct lttng_channel_extended *)
2705 chan->attr.extended.ptr)->monitor_timer_interval;
2706 end:
2707 return ret;
2708 }
2709
2710 int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
2711 uint64_t monitor_timer_interval)
2712 {
2713 int ret = 0;
2714
2715 if (!chan || !chan->attr.extended.ptr) {
2716 ret = -LTTNG_ERR_INVALID;
2717 goto end;
2718 }
2719
2720 ((struct lttng_channel_extended *)
2721 chan->attr.extended.ptr)->monitor_timer_interval =
2722 monitor_timer_interval;
2723 end:
2724 return ret;
2725 }
2726
2727 int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
2728 int64_t *blocking_timeout)
2729 {
2730 int ret = 0;
2731
2732 if (!chan || !blocking_timeout) {
2733 ret = -LTTNG_ERR_INVALID;
2734 goto end;
2735 }
2736
2737 if (!chan->attr.extended.ptr) {
2738 ret = -LTTNG_ERR_INVALID;
2739 goto end;
2740 }
2741
2742 *blocking_timeout = ((struct lttng_channel_extended *)
2743 chan->attr.extended.ptr)->blocking_timeout;
2744 end:
2745 return ret;
2746 }
2747
2748 int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
2749 int64_t blocking_timeout)
2750 {
2751 int ret = 0;
2752 int64_t msec_timeout;
2753
2754 if (!chan || !chan->attr.extended.ptr) {
2755 ret = -LTTNG_ERR_INVALID;
2756 goto end;
2757 }
2758
2759 if (blocking_timeout < 0 && blocking_timeout != -1) {
2760 ret = -LTTNG_ERR_INVALID;
2761 goto end;
2762 }
2763
2764 /*
2765 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2766 * us to accept a narrower range of values (msecs expressed as a signed
2767 * 32-bit integer).
2768 */
2769 msec_timeout = blocking_timeout / 1000;
2770 if (msec_timeout != (int32_t) msec_timeout) {
2771 ret = -LTTNG_ERR_INVALID;
2772 goto end;
2773 }
2774
2775 ((struct lttng_channel_extended *)
2776 chan->attr.extended.ptr)->blocking_timeout =
2777 blocking_timeout;
2778 end:
2779 return ret;
2780 }
2781
2782 /*
2783 * Check if session daemon is alive.
2784 *
2785 * Return 1 if alive or 0 if not.
2786 * On error returns a negative value.
2787 */
2788 int lttng_session_daemon_alive(void)
2789 {
2790 int ret;
2791
2792 ret = set_session_daemon_path();
2793 if (ret < 0) {
2794 /* Error. */
2795 return ret;
2796 }
2797
2798 if (*sessiond_sock_path == '\0') {
2799 /*
2800 * No socket path set. Weird error which means the constructor
2801 * was not called.
2802 */
2803 abort();
2804 }
2805
2806 ret = try_connect_sessiond(sessiond_sock_path);
2807 if (ret < 0) {
2808 /* Not alive. */
2809 return 0;
2810 }
2811
2812 /* Is alive. */
2813 return 1;
2814 }
2815
2816 /*
2817 * Set URL for a consumer for a session and domain.
2818 *
2819 * Return 0 on success, else a negative value.
2820 */
2821 int lttng_set_consumer_url(struct lttng_handle *handle,
2822 const char *control_url, const char *data_url)
2823 {
2824 int ret;
2825 ssize_t size;
2826 struct lttcomm_session_msg lsm;
2827 struct lttng_uri *uris = NULL;
2828
2829 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2830 ret = -LTTNG_ERR_INVALID;
2831 goto error;
2832 }
2833
2834 memset(&lsm, 0, sizeof(lsm));
2835
2836 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2837
2838 ret = lttng_strncpy(lsm.session.name, handle->session_name,
2839 sizeof(lsm.session.name));
2840 if (ret) {
2841 ret = -LTTNG_ERR_INVALID;
2842 goto error;
2843 }
2844
2845 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
2846
2847 size = uri_parse_str_urls(control_url, data_url, &uris);
2848 if (size < 0) {
2849 ret = -LTTNG_ERR_INVALID;
2850 goto error;
2851 }
2852
2853 lsm.u.uri.size = size;
2854
2855 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2856 sizeof(struct lttng_uri) * size, NULL);
2857
2858 free(uris);
2859 error:
2860 return ret;
2861 }
2862
2863 /*
2864 * [OBSOLETE]
2865 */
2866 extern "C"
2867 LTTNG_EXPORT int lttng_enable_consumer(struct lttng_handle *handle);
2868 int lttng_enable_consumer(struct lttng_handle *handle __attribute__((unused)))
2869 {
2870 return -ENOSYS;
2871 }
2872
2873 /*
2874 * [OBSOLETE]
2875 */
2876 extern "C"
2877 LTTNG_EXPORT int lttng_disable_consumer(struct lttng_handle *handle);
2878 int lttng_disable_consumer(struct lttng_handle *handle __attribute__((unused)))
2879 {
2880 return -ENOSYS;
2881 }
2882
2883 /*
2884 * [OBSOLETE]
2885 */
2886 extern "C"
2887 LTTNG_EXPORT int _lttng_create_session_ext(const char *name, const char *url,
2888 const char *datetime);
2889 int _lttng_create_session_ext(const char *name __attribute__((unused)),
2890 const char *url __attribute__((unused)),
2891 const char *datetime __attribute__((unused)))
2892 {
2893 return -ENOSYS;
2894 }
2895
2896 /*
2897 * For a given session name, this call checks if the data is ready to be read
2898 * or is still being extracted by the consumer(s) hence not ready to be used by
2899 * any readers.
2900 */
2901 int lttng_data_pending(const char *session_name)
2902 {
2903 int ret;
2904 struct lttcomm_session_msg lsm;
2905 uint8_t *pending = NULL;
2906
2907 if (session_name == NULL) {
2908 return -LTTNG_ERR_INVALID;
2909 }
2910
2911 memset(&lsm, 0, sizeof(lsm));
2912 lsm.cmd_type = LTTNG_DATA_PENDING;
2913
2914 ret = lttng_strncpy(lsm.session.name, session_name,
2915 sizeof(lsm.session.name));
2916 if (ret) {
2917 ret = -LTTNG_ERR_INVALID;
2918 goto end;
2919 }
2920
2921 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2922 if (ret < 0) {
2923 goto end;
2924 } else if (ret != 1) {
2925 /* Unexpected payload size */
2926 ret = -LTTNG_ERR_INVALID;
2927 goto end;
2928 } else if (!pending) {
2929 /* Internal error. */
2930 ret = -LTTNG_ERR_UNK;
2931 goto end;
2932 }
2933
2934 ret = (int) *pending;
2935 end:
2936 free(pending);
2937 return ret;
2938 }
2939
2940 /*
2941 * Regenerate the metadata for a session.
2942 * Return 0 on success, a negative error code on error.
2943 */
2944 int lttng_regenerate_metadata(const char *session_name)
2945 {
2946 int ret;
2947 struct lttcomm_session_msg lsm;
2948
2949 if (!session_name) {
2950 ret = -LTTNG_ERR_INVALID;
2951 goto end;
2952 }
2953
2954 memset(&lsm, 0, sizeof(lsm));
2955 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
2956
2957 ret = lttng_strncpy(lsm.session.name, session_name,
2958 sizeof(lsm.session.name));
2959 if (ret) {
2960 ret = -LTTNG_ERR_INVALID;
2961 goto end;
2962 }
2963
2964 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2965 if (ret < 0) {
2966 goto end;
2967 }
2968
2969 ret = 0;
2970 end:
2971 return ret;
2972 }
2973
2974 /*
2975 * Deprecated, replaced by lttng_regenerate_metadata.
2976 */
2977 int lttng_metadata_regenerate(const char *session_name)
2978 {
2979 return lttng_regenerate_metadata(session_name);
2980 }
2981
2982 /*
2983 * Regenerate the statedump of a session.
2984 * Return 0 on success, a negative error code on error.
2985 */
2986 int lttng_regenerate_statedump(const char *session_name)
2987 {
2988 int ret;
2989 struct lttcomm_session_msg lsm;
2990
2991 if (!session_name) {
2992 ret = -LTTNG_ERR_INVALID;
2993 goto end;
2994 }
2995
2996 memset(&lsm, 0, sizeof(lsm));
2997 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
2998
2999 ret = lttng_strncpy(lsm.session.name, session_name,
3000 sizeof(lsm.session.name));
3001 if (ret) {
3002 ret = -LTTNG_ERR_INVALID;
3003 goto end;
3004 }
3005
3006 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
3007 if (ret < 0) {
3008 goto end;
3009 }
3010
3011 ret = 0;
3012 end:
3013 return ret;
3014 }
3015
3016 static
3017 int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name,
3018 bool generate_name)
3019 {
3020 int ret;
3021 struct lttcomm_session_msg lsm = {
3022 .cmd_type = LTTNG_REGISTER_TRIGGER,
3023 .session = {},
3024 .domain = {},
3025 .u = {},
3026 .fd_count = 0,
3027 };
3028 lsm.u.trigger.is_trigger_anonymous = !name && !generate_name;
3029 struct lttcomm_session_msg *message_lsm;
3030 struct lttng_payload message;
3031 struct lttng_payload reply;
3032 struct lttng_trigger *reply_trigger = NULL;
3033 enum lttng_domain_type domain_type;
3034 const struct lttng_credentials user_creds = {
3035 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3036 .gid = LTTNG_OPTIONAL_INIT_UNSET,
3037 };
3038 const char *unused_trigger_name = NULL;
3039 enum lttng_trigger_status trigger_status;
3040
3041 lttng_payload_init(&message);
3042 lttng_payload_init(&reply);
3043
3044 if (!trigger) {
3045 ret = -LTTNG_ERR_INVALID;
3046 goto end;
3047 }
3048
3049 trigger_status = lttng_trigger_get_name(trigger, &unused_trigger_name);
3050 if (trigger_status != LTTNG_TRIGGER_STATUS_UNSET) {
3051 /* Re-using already registered trigger. */
3052 ret = -LTTNG_ERR_INVALID;
3053 goto end;
3054 }
3055
3056 if (name) {
3057 trigger_status = lttng_trigger_set_name(trigger, name);
3058 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
3059 ret = -LTTNG_ERR_NOMEM;
3060 goto end;
3061 }
3062 }
3063
3064 if (!trigger->creds.uid.is_set) {
3065 /* Use the client's credentials as the trigger credentials. */
3066 lttng_trigger_set_credentials(trigger, &user_creds);
3067 } else {
3068 /*
3069 * Validate that either the current trigger credentials and the
3070 * client credentials are identical or that the current user is
3071 * root. The root user can register, unregister triggers for
3072 * himself and other users.
3073 *
3074 * This check is also present on the sessiond side, using the
3075 * credentials passed on the socket. These check are all
3076 * "safety" checks.
3077 */
3078 const struct lttng_credentials *trigger_creds =
3079 lttng_trigger_get_credentials(trigger);
3080
3081 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3082 if (lttng_credentials_get_uid(&user_creds) != 0) {
3083 ret = -LTTNG_ERR_EPERM;
3084 goto end_unset_name;
3085 }
3086 }
3087 }
3088
3089 if (!lttng_trigger_validate(trigger)) {
3090 ret = -LTTNG_ERR_INVALID_TRIGGER;
3091 goto end_unset_name;
3092 }
3093
3094 domain_type = lttng_trigger_get_underlying_domain_type_restriction(
3095 trigger);
3096
3097 lsm.domain.type = domain_type;
3098
3099 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3100 if (ret) {
3101 ret = -LTTNG_ERR_NOMEM;
3102 goto end_unset_name;
3103 }
3104
3105 ret = lttng_trigger_serialize(trigger, &message);
3106 if (ret < 0) {
3107 ret = -LTTNG_ERR_UNK;
3108 goto end_unset_name;
3109 }
3110
3111 /*
3112 * This is needed to populate the trigger object size for the command
3113 * header.
3114 */
3115 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3116
3117 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3118
3119 {
3120 struct lttng_payload_view message_view =
3121 lttng_payload_view_from_payload(
3122 &message, 0, -1);
3123
3124 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3125 &message_view);
3126 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3127 if (ret < 0) {
3128 goto end_unset_name;
3129 }
3130 }
3131
3132 {
3133 struct lttng_payload_view reply_view =
3134 lttng_payload_view_from_payload(
3135 &reply, 0, reply.buffer.size);
3136
3137 ret = lttng_trigger_create_from_payload(
3138 &reply_view, &reply_trigger);
3139 if (ret < 0) {
3140 ret = -LTTNG_ERR_INVALID_PROTOCOL;
3141 goto end_unset_name;
3142 }
3143 }
3144
3145 if (name || generate_name) {
3146 ret = lttng_trigger_assign_name(trigger, reply_trigger);
3147 if (ret < 0) {
3148 ret = -LTTNG_ERR_NOMEM;
3149 goto end;
3150 }
3151 }
3152
3153 ret = 0;
3154 goto end;
3155
3156 end_unset_name:
3157 trigger_status = lttng_trigger_set_name(trigger, NULL);
3158 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
3159 ret = -LTTNG_ERR_UNK;
3160 }
3161 end:
3162 lttng_payload_reset(&message);
3163 lttng_payload_reset(&reply);
3164 lttng_trigger_destroy(reply_trigger);
3165 return ret;
3166 }
3167
3168 int lttng_register_trigger(struct lttng_trigger *trigger)
3169 {
3170 /* Register an anonymous trigger. */
3171 return _lttng_register_trigger(trigger, NULL, false);
3172 }
3173
3174 enum lttng_error_code lttng_register_trigger_with_name(
3175 struct lttng_trigger *trigger, const char *name)
3176 {
3177 const int ret = _lttng_register_trigger(trigger, name, false);
3178
3179 return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret;
3180 }
3181
3182 enum lttng_error_code lttng_register_trigger_with_automatic_name(
3183 struct lttng_trigger *trigger)
3184 {
3185 const int ret = _lttng_register_trigger(trigger, nullptr, true);
3186
3187 return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret;
3188 }
3189
3190 enum lttng_error_code lttng_error_query_execute(
3191 const struct lttng_error_query *query,
3192 const struct lttng_endpoint *endpoint,
3193 struct lttng_error_query_results **results)
3194 {
3195 int ret;
3196 enum lttng_error_code ret_code;
3197 struct lttcomm_session_msg lsm = {
3198 .cmd_type = LTTNG_EXECUTE_ERROR_QUERY,
3199 .session = {},
3200 .domain = {},
3201 .u = {},
3202 .fd_count = 0,
3203 };
3204 struct lttng_payload message;
3205 struct lttng_payload reply;
3206 struct lttcomm_session_msg *message_lsm;
3207
3208 lttng_payload_init(&message);
3209 lttng_payload_init(&reply);
3210
3211 if (!query || !results) {
3212 ret_code = LTTNG_ERR_INVALID;
3213 goto end;
3214 }
3215
3216 if (endpoint != lttng_session_daemon_command_endpoint) {
3217 ret_code = LTTNG_ERR_INVALID_ERROR_QUERY_TARGET;
3218 goto end;
3219 }
3220
3221 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3222 if (ret) {
3223 ret_code = LTTNG_ERR_NOMEM;
3224 goto end;
3225 }
3226
3227 ret = lttng_error_query_serialize(query, &message);
3228 if (ret) {
3229 ret_code = LTTNG_ERR_UNK;
3230 goto end;
3231 }
3232
3233 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3234 message_lsm->u.error_query.length =
3235 (uint32_t) message.buffer.size - sizeof(lsm);
3236
3237 {
3238 struct lttng_payload_view message_view =
3239 lttng_payload_view_from_payload(
3240 &message, 0, -1);
3241
3242 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3243 &message_view);
3244 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3245 if (ret < 0) {
3246 ret_code =(lttng_error_code) -ret;
3247 goto end;
3248 }
3249 }
3250
3251 {
3252 ssize_t reply_create_ret;
3253 struct lttng_payload_view reply_view =
3254 lttng_payload_view_from_payload(
3255 &reply, 0, reply.buffer.size);
3256
3257 reply_create_ret = lttng_error_query_results_create_from_payload(
3258 &reply_view, results);
3259 if (reply_create_ret < 0) {
3260 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3261 goto end;
3262 }
3263 }
3264
3265 ret_code = LTTNG_OK;
3266 end:
3267 lttng_payload_reset(&message);
3268 lttng_payload_reset(&reply);
3269 return ret_code;
3270 }
3271
3272 int lttng_unregister_trigger(const struct lttng_trigger *trigger)
3273 {
3274 int ret;
3275 struct lttcomm_session_msg lsm;
3276 struct lttcomm_session_msg *message_lsm;
3277 struct lttng_payload message;
3278 struct lttng_payload reply;
3279 struct lttng_trigger *copy = NULL;
3280 const struct lttng_credentials user_creds = {
3281 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3282 .gid = LTTNG_OPTIONAL_INIT_UNSET,
3283 };
3284
3285 lttng_payload_init(&message);
3286 lttng_payload_init(&reply);
3287
3288 if (!trigger) {
3289 ret = -LTTNG_ERR_INVALID;
3290 goto end;
3291 }
3292
3293 copy = lttng_trigger_copy(trigger);
3294 if (!copy) {
3295 ret = -LTTNG_ERR_UNK;
3296 goto end;
3297 }
3298
3299 if (!copy->creds.uid.is_set) {
3300 /* Use the client credentials as the trigger credentials */
3301 lttng_trigger_set_credentials(copy, &user_creds);
3302 } else {
3303 /*
3304 * Validate that either the current trigger credentials and the
3305 * client credentials are identical or that the current user is
3306 * root. The root user can register, unregister triggers for
3307 * himself and other users.
3308 *
3309 * This check is also present on the sessiond side, using the
3310 * credentials passed on the socket. These check are all
3311 * "safety" checks.
3312 */
3313 const struct lttng_credentials *trigger_creds =
3314 lttng_trigger_get_credentials(copy);
3315 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3316 if (lttng_credentials_get_uid(&user_creds) != 0) {
3317 ret = -LTTNG_ERR_EPERM;
3318 goto end;
3319 }
3320 }
3321 }
3322
3323 if (!lttng_trigger_validate(copy)) {
3324 ret = -LTTNG_ERR_INVALID_TRIGGER;
3325 goto end;
3326 }
3327
3328 memset(&lsm, 0, sizeof(lsm));
3329 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
3330
3331 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3332 if (ret) {
3333 ret = -LTTNG_ERR_NOMEM;
3334 goto end;
3335 }
3336
3337 ret = lttng_trigger_serialize(copy, &message);
3338 if (ret < 0) {
3339 ret = -LTTNG_ERR_UNK;
3340 goto end;
3341 }
3342
3343 /*
3344 * This is needed to populate the trigger object size for the command
3345 * header and number of fds sent.
3346 */
3347 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3348
3349 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3350
3351 {
3352 struct lttng_payload_view message_view =
3353 lttng_payload_view_from_payload(
3354 &message, 0, -1);
3355
3356 /*
3357 * Update the message header with the number of fd that will be
3358 * sent.
3359 */
3360 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3361 &message_view);
3362
3363 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3364 if (ret < 0) {
3365 goto end;
3366 }
3367 }
3368
3369 ret = 0;
3370 end:
3371 lttng_trigger_destroy(copy);
3372 lttng_payload_reset(&message);
3373 lttng_payload_reset(&reply);
3374 return ret;
3375 }
3376
3377 /*
3378 * Ask the session daemon for all registered triggers for the current user.
3379 *
3380 * Allocates and return an lttng_triggers set.
3381 * On error, returns a suitable lttng_error_code.
3382 */
3383 enum lttng_error_code lttng_list_triggers(struct lttng_triggers **triggers)
3384 {
3385 int ret;
3386 enum lttng_error_code ret_code = LTTNG_OK;
3387 struct lttcomm_session_msg lsm = {
3388 .cmd_type = LTTNG_LIST_TRIGGERS,
3389 .session = {},
3390 .domain = {},
3391 .u = {},
3392 .fd_count = 0,
3393 };
3394 struct lttng_triggers *local_triggers = NULL;
3395 struct lttng_payload reply;
3396 struct lttng_payload_view lsm_view =
3397 lttng_payload_view_init_from_buffer(
3398 (const char *) &lsm, 0, sizeof(lsm));
3399
3400 lttng_payload_init(&reply);
3401
3402 ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply);
3403 if (ret < 0) {
3404 ret_code = (enum lttng_error_code) -ret;
3405 goto end;
3406 }
3407
3408 {
3409 struct lttng_payload_view reply_view =
3410 lttng_payload_view_from_payload(
3411 &reply, 0, reply.buffer.size);
3412
3413 ret = lttng_triggers_create_from_payload(
3414 &reply_view, &local_triggers);
3415 if (ret < 0) {
3416 ret_code = LTTNG_ERR_FATAL;
3417 goto end;
3418 }
3419 }
3420
3421 *triggers = local_triggers;
3422 local_triggers = NULL;
3423 end:
3424 lttng_payload_reset(&reply);
3425 lttng_triggers_destroy(local_triggers);
3426 return ret_code;
3427 }
3428
3429 /*
3430 * lib constructor.
3431 */
3432 static void __attribute__((constructor)) init(void)
3433 {
3434 /* Set default session group */
3435 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
3436 }
3437
3438 /*
3439 * lib destructor.
3440 */
3441 static void __attribute__((destructor)) lttng_ctl_exit(void)
3442 {
3443 free(tracing_group);
3444 }
This page took 0.104468 seconds and 5 git commands to generate.