Fix: liblttng-ctl: leak of payload on field listing
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
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 <assert.h>
15 #include <grp.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #include <common/bytecode/bytecode.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.h>
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 int lttng_opt_quiet;
78 int lttng_opt_verbose;
79 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 LTTNG_HIDDEN
87 void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst,
88 struct lttng_domain *src)
89 {
90 if (src && dst) {
91 switch (src->type) {
92 case LTTNG_DOMAIN_KERNEL:
93 case LTTNG_DOMAIN_UST:
94 case LTTNG_DOMAIN_JUL:
95 case LTTNG_DOMAIN_LOG4J:
96 case LTTNG_DOMAIN_PYTHON:
97 memcpy(dst, src, sizeof(struct lttng_domain));
98 break;
99 default:
100 memset(dst, 0, sizeof(struct lttng_domain));
101 break;
102 }
103 }
104 }
105
106 /*
107 * Send lttcomm_session_msg to the session daemon.
108 *
109 * On success, returns the number of bytes sent (>=0)
110 * On error, returns -1
111 */
112 static int send_session_msg(struct lttcomm_session_msg *lsm)
113 {
114 int ret;
115
116 if (!connected) {
117 ret = -LTTNG_ERR_NO_SESSIOND;
118 goto end;
119 }
120
121 DBG("LSM cmd type: '%s' (%d)", lttcomm_sessiond_command_str(lsm->cmd_type),
122 lsm->cmd_type);
123
124 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
125 sizeof(struct lttcomm_session_msg));
126 if (ret < 0) {
127 ret = -LTTNG_ERR_FATAL;
128 }
129
130 end:
131 return ret;
132 }
133
134 /*
135 * Send var len data to the session daemon.
136 *
137 * On success, returns the number of bytes sent (>=0)
138 * On error, returns -1
139 */
140 static int send_session_varlen(const void *data, size_t len)
141 {
142 int ret;
143
144 if (!connected) {
145 ret = -LTTNG_ERR_NO_SESSIOND;
146 goto end;
147 }
148
149 if (!data || !len) {
150 ret = 0;
151 goto end;
152 }
153
154 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
155 if (ret < 0) {
156 ret = -LTTNG_ERR_FATAL;
157 }
158
159 end:
160 return ret;
161 }
162
163 /*
164 * Send file descriptors to the session daemon.
165 *
166 * On success, returns the number of bytes sent (>=0)
167 * On error, returns -1
168 */
169 static int send_session_fds(const int *fds, size_t nb_fd)
170 {
171 int ret;
172
173 if (!connected) {
174 ret = -LTTNG_ERR_NO_SESSIOND;
175 goto end;
176 }
177
178 if (!fds || !nb_fd) {
179 ret = 0;
180 goto end;
181 }
182
183 ret = lttcomm_send_fds_unix_sock(sessiond_socket, fds, nb_fd);
184 if (ret < 0) {
185 ret = -LTTNG_ERR_FATAL;
186 }
187
188 end:
189 return ret;
190 }
191
192 /*
193 * Receive data from the sessiond socket.
194 *
195 * On success, returns the number of bytes received (>=0)
196 * On error, returns a negative lttng_error_code.
197 */
198 static int recv_data_sessiond(void *buf, size_t len)
199 {
200 int ret;
201
202 assert(len > 0);
203
204 if (!connected) {
205 ret = -LTTNG_ERR_NO_SESSIOND;
206 goto end;
207 }
208
209 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
210 if (ret < 0) {
211 ret = -LTTNG_ERR_FATAL;
212 } else if (ret == 0) {
213 ret = -LTTNG_ERR_NO_SESSIOND;
214 }
215
216 end:
217 return ret;
218 }
219
220 /*
221 * Receive a payload from the session daemon by appending to an existing
222 * payload.
223 * On success, returns the number of bytes received (>=0)
224 * On error, returns a negative lttng_error_code.
225 */
226 static int recv_payload_sessiond(struct lttng_payload *payload, size_t len)
227 {
228 int ret;
229 const size_t original_payload_size = payload->buffer.size;
230
231 ret = lttng_dynamic_buffer_set_size(
232 &payload->buffer, payload->buffer.size + len);
233 if (ret) {
234 ret = -LTTNG_ERR_NOMEM;
235 goto end;
236 }
237
238 ret = recv_data_sessiond(
239 payload->buffer.data + original_payload_size, len);
240 end:
241 return ret;
242 }
243
244 /*
245 * Check if we are in the specified group.
246 *
247 * If yes return 1, else return -1.
248 */
249 LTTNG_HIDDEN
250 int lttng_check_tracing_group(void)
251 {
252 gid_t *grp_list, tracing_gid;
253 int grp_list_size, grp_id, i;
254 int ret = -1;
255 const char *grp_name = tracing_group;
256
257 /* Get GID of group 'tracing' */
258 if (utils_get_group_id(grp_name, false, &tracing_gid)) {
259 /* If grp_tracing is NULL, the group does not exist. */
260 goto end;
261 }
262
263 /* Get number of supplementary group IDs */
264 grp_list_size = getgroups(0, NULL);
265 if (grp_list_size < 0) {
266 PERROR("getgroups");
267 goto end;
268 }
269
270 /* Alloc group list of the right size */
271 grp_list = zmalloc(grp_list_size * sizeof(gid_t));
272 if (!grp_list) {
273 PERROR("malloc");
274 goto end;
275 }
276 grp_id = getgroups(grp_list_size, grp_list);
277 if (grp_id < 0) {
278 PERROR("getgroups");
279 goto free_list;
280 }
281
282 for (i = 0; i < grp_list_size; i++) {
283 if (grp_list[i] == tracing_gid) {
284 ret = 1;
285 break;
286 }
287 }
288
289 free_list:
290 free(grp_list);
291
292 end:
293 return ret;
294 }
295
296 static enum lttng_error_code check_enough_available_memory(
297 uint64_t num_bytes_requested_per_cpu)
298 {
299 int ret;
300 enum lttng_error_code ret_code;
301 long num_cpu;
302 uint64_t best_mem_info;
303 uint64_t num_bytes_requested_total;
304
305 /*
306 * Get the number of CPU currently online to compute the amount of
307 * memory needed to create a buffer for every CPU.
308 */
309 num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
310 if (num_cpu == -1) {
311 ret_code = LTTNG_ERR_FATAL;
312 goto end;
313 }
314
315 if (num_bytes_requested_per_cpu > UINT64_MAX / (uint64_t) num_cpu) {
316 /* Overflow */
317 ret_code = LTTNG_ERR_OVERFLOW;
318 goto end;
319 }
320
321 num_bytes_requested_total =
322 num_bytes_requested_per_cpu * (uint64_t) num_cpu;
323
324 /*
325 * Try to get the `MemAvail` field of `/proc/meminfo`. This is the most
326 * reliable estimate we can get but it is only exposed by the kernel
327 * since 3.14. (See Linux kernel commit:
328 * 34e431b0ae398fc54ea69ff85ec700722c9da773)
329 */
330 ret = utils_get_memory_available(&best_mem_info);
331 if (ret >= 0) {
332 goto success;
333 }
334
335 /*
336 * As a backup plan, use `MemTotal` field of `/proc/meminfo`. This
337 * is a sanity check for obvious user error.
338 */
339 ret = utils_get_memory_total(&best_mem_info);
340 if (ret >= 0) {
341 goto success;
342 }
343
344 /* No valid source of information. */
345 ret_code = LTTNG_ERR_NOMEM;
346 goto end;
347
348 success:
349 if (best_mem_info >= num_bytes_requested_total) {
350 ret_code = LTTNG_OK;
351 } else {
352 ret_code = LTTNG_ERR_NOMEM;
353 }
354 end:
355 return ret_code;
356 }
357
358 /*
359 * Try connect to session daemon with sock_path.
360 *
361 * Return 0 on success, else -1
362 */
363 static int try_connect_sessiond(const char *sock_path)
364 {
365 int ret;
366
367 /* If socket exist, we check if the daemon listens for connect. */
368 ret = access(sock_path, F_OK);
369 if (ret < 0) {
370 /* Not alive */
371 goto error;
372 }
373
374 ret = lttcomm_connect_unix_sock(sock_path);
375 if (ret < 0) {
376 /* Not alive. */
377 goto error;
378 }
379
380 ret = lttcomm_close_unix_sock(ret);
381 if (ret < 0) {
382 PERROR("lttcomm_close_unix_sock");
383 }
384
385 return 0;
386
387 error:
388 return -1;
389 }
390
391 /*
392 * Set sessiond socket path by putting it in the global sessiond_sock_path
393 * variable.
394 *
395 * Returns 0 on success, negative value on failure (the sessiond socket path
396 * is somehow too long or ENOMEM).
397 */
398 static int set_session_daemon_path(void)
399 {
400 int in_tgroup = 0; /* In tracing group. */
401 uid_t uid;
402
403 uid = getuid();
404
405 if (uid != 0) {
406 /* Are we in the tracing group ? */
407 in_tgroup = lttng_check_tracing_group();
408 }
409
410 if ((uid == 0) || in_tgroup == 1) {
411 const int ret = lttng_strncpy(sessiond_sock_path,
412 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
413 sizeof(sessiond_sock_path));
414
415 if (ret) {
416 goto error;
417 }
418 }
419
420 if (uid != 0) {
421 int ret;
422
423 if (in_tgroup) {
424 /* Tracing group. */
425 ret = try_connect_sessiond(sessiond_sock_path);
426 if (ret >= 0) {
427 goto end;
428 }
429 /* Global session daemon not available... */
430 }
431 /* ...or not in tracing group (and not root), default */
432
433 /*
434 * With GNU C < 2.1, snprintf returns -1 if the target buffer
435 * is too small;
436 * With GNU C >= 2.1, snprintf returns the required size
437 * (excluding closing null)
438 */
439 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
440 DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir());
441 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
442 goto error;
443 }
444 }
445 end:
446 return 0;
447
448 error:
449 return -1;
450 }
451
452 /*
453 * Connect to the LTTng session daemon.
454 *
455 * On success, return the socket's file descriptor. On error, return -1.
456 */
457 LTTNG_HIDDEN int connect_sessiond(void)
458 {
459 int ret;
460
461 ret = set_session_daemon_path();
462 if (ret < 0) {
463 goto error;
464 }
465
466 /* Connect to the sesssion daemon. */
467 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
468 if (ret < 0) {
469 goto error;
470 }
471
472 return ret;
473
474 error:
475 return -1;
476 }
477
478 static void reset_global_sessiond_connection_state(void)
479 {
480 sessiond_socket = -1;
481 connected = 0;
482 }
483
484 /*
485 * Clean disconnect from the session daemon.
486 *
487 * On success, return 0. On error, return -1.
488 */
489 static int disconnect_sessiond(void)
490 {
491 int ret = 0;
492
493 if (connected) {
494 ret = lttcomm_close_unix_sock(sessiond_socket);
495 reset_global_sessiond_connection_state();
496 }
497
498 return ret;
499 }
500
501 static int recv_sessiond_optional_data(size_t len, void **user_buf,
502 size_t *user_len)
503 {
504 int ret = 0;
505 void *buf = NULL;
506
507 if (len) {
508 if (!user_len) {
509 ret = -LTTNG_ERR_INVALID;
510 goto end;
511 }
512
513 buf = zmalloc(len);
514 if (!buf) {
515 ret = -ENOMEM;
516 goto end;
517 }
518
519 ret = recv_data_sessiond(buf, len);
520 if (ret < 0) {
521 goto end;
522 }
523
524 if (!user_buf) {
525 ret = -LTTNG_ERR_INVALID;
526 goto end;
527 }
528
529 /* Move ownership of command header buffer to user. */
530 *user_buf = buf;
531 buf = NULL;
532 *user_len = len;
533 } else {
534 /* No command header. */
535 if (user_len) {
536 *user_len = 0;
537 }
538
539 if (user_buf) {
540 *user_buf = NULL;
541 }
542 }
543
544 end:
545 free(buf);
546 return ret;
547 }
548
549 /*
550 * Ask the session daemon a specific command and put the data into buf.
551 * Takes extra var. len. data and file descriptors as input to send to the
552 * session daemon.
553 *
554 * Return size of data (only payload, not header) or a negative error code.
555 */
556 LTTNG_HIDDEN
557 int lttng_ctl_ask_sessiond_fds_varlen(struct lttcomm_session_msg *lsm,
558 const int *fds, size_t nb_fd, const void *vardata,
559 size_t vardata_len, void **user_payload_buf,
560 void **user_cmd_header_buf, size_t *user_cmd_header_len)
561 {
562 int ret;
563 size_t payload_len;
564 struct lttcomm_lttng_msg llm;
565
566 ret = connect_sessiond();
567 if (ret < 0) {
568 ret = -LTTNG_ERR_NO_SESSIOND;
569 goto end;
570 } else {
571 sessiond_socket = ret;
572 connected = 1;
573 }
574
575 ret = send_session_msg(lsm);
576 if (ret < 0) {
577 /* Ret value is a valid lttng error code. */
578 goto end;
579 }
580 /* Send var len data */
581 ret = send_session_varlen(vardata, vardata_len);
582 if (ret < 0) {
583 /* Ret value is a valid lttng error code. */
584 goto end;
585 }
586
587 /* Send fds */
588 ret = send_session_fds(fds, nb_fd);
589 if (ret < 0) {
590 /* Ret value is a valid lttng error code. */
591 goto end;
592 }
593
594 /* Get header from data transmission */
595 ret = recv_data_sessiond(&llm, sizeof(llm));
596 if (ret < 0) {
597 /* Ret value is a valid lttng error code. */
598 goto end;
599 }
600
601 /* Check error code if OK */
602 if (llm.ret_code != LTTNG_OK) {
603 ret = -llm.ret_code;
604 goto end;
605 }
606
607 /* Get command header from data transmission */
608 ret = recv_sessiond_optional_data(llm.cmd_header_size,
609 user_cmd_header_buf, user_cmd_header_len);
610 if (ret < 0) {
611 goto end;
612 }
613
614 /* Get payload from data transmission */
615 ret = recv_sessiond_optional_data(llm.data_size, user_payload_buf,
616 &payload_len);
617 if (ret < 0) {
618 goto end;
619 }
620
621 ret = llm.data_size;
622
623 end:
624 disconnect_sessiond();
625 return ret;
626 }
627
628 LTTNG_HIDDEN
629 int lttng_ctl_ask_sessiond_payload(struct lttng_payload_view *message,
630 struct lttng_payload *reply)
631 {
632 int ret;
633 struct lttcomm_lttng_msg llm;
634 const int fd_count = lttng_payload_view_get_fd_handle_count(message);
635
636 assert(reply->buffer.size == 0);
637 assert(lttng_dynamic_pointer_array_get_count(&reply->_fd_handles) == 0);
638
639 ret = connect_sessiond();
640 if (ret < 0) {
641 ret = -LTTNG_ERR_NO_SESSIOND;
642 goto end;
643 } else {
644 sessiond_socket = ret;
645 connected = 1;
646 }
647
648 /* Send command to session daemon */
649 ret = lttcomm_send_creds_unix_sock(sessiond_socket, message->buffer.data,
650 message->buffer.size);
651 if (ret < 0) {
652 ret = -LTTNG_ERR_FATAL;
653 goto end;
654 }
655
656 if (fd_count > 0) {
657 ret = lttcomm_send_payload_view_fds_unix_sock(sessiond_socket,
658 message);
659 if (ret < 0) {
660 ret = -LTTNG_ERR_FATAL;
661 goto end;
662 }
663 }
664
665 /* Get header from data transmission */
666 ret = recv_payload_sessiond(reply, sizeof(llm));
667 if (ret < 0) {
668 /* Ret value is a valid lttng error code. */
669 goto end;
670 }
671
672 llm = *((typeof(llm) *) reply->buffer.data);
673
674 /* Check error code if OK */
675 if (llm.ret_code != LTTNG_OK) {
676 if (llm.ret_code < LTTNG_OK || llm.ret_code >= LTTNG_ERR_NR) {
677 /* Invalid error code received. */
678 ret = -LTTNG_ERR_UNK;
679 } else {
680 ret = -llm.ret_code;
681 }
682 goto end;
683 }
684
685 if (llm.cmd_header_size > 0) {
686 ret = recv_payload_sessiond(reply, llm.cmd_header_size);
687 if (ret < 0) {
688 goto end;
689 }
690 }
691
692 /* Get command header from data transmission */
693 if (llm.data_size > 0) {
694 ret = recv_payload_sessiond(reply, llm.data_size);
695 if (ret < 0) {
696 goto end;
697 }
698 }
699
700 if (llm.fd_count > 0) {
701 ret = lttcomm_recv_payload_fds_unix_sock(
702 sessiond_socket, llm.fd_count, reply);
703 if (ret < 0) {
704 goto end;
705 }
706 }
707
708 /* Don't return the llm header to the caller. */
709 memmove(reply->buffer.data, reply->buffer.data + sizeof(llm),
710 reply->buffer.size - sizeof(llm));
711 ret = lttng_dynamic_buffer_set_size(
712 &reply->buffer, reply->buffer.size - sizeof(llm));
713 if (ret) {
714 /* Can't happen as size is reduced. */
715 abort();
716 }
717
718 ret = reply->buffer.size;
719
720 end:
721 disconnect_sessiond();
722 return ret;
723 }
724
725 /*
726 * Create lttng handle and return pointer.
727 *
728 * The returned pointer will be NULL in case of malloc() error.
729 */
730 struct lttng_handle *lttng_create_handle(const char *session_name,
731 struct lttng_domain *domain)
732 {
733 int ret;
734 struct lttng_handle *handle = NULL;
735
736 handle = zmalloc(sizeof(struct lttng_handle));
737 if (handle == NULL) {
738 PERROR("malloc handle");
739 goto end;
740 }
741
742 /* Copy session name */
743 ret = lttng_strncpy(handle->session_name, session_name ? : "",
744 sizeof(handle->session_name));
745 if (ret) {
746 goto error;
747 }
748
749 /* Copy lttng domain or leave initialized to 0. */
750 if (domain) {
751 lttng_ctl_copy_lttng_domain(&handle->domain, domain);
752 }
753
754 end:
755 return handle;
756 error:
757 free(handle);
758 return NULL;
759 }
760
761 /*
762 * Destroy handle by free(3) the pointer.
763 */
764 void lttng_destroy_handle(struct lttng_handle *handle)
765 {
766 free(handle);
767 }
768
769 /*
770 * Register an outside consumer.
771 *
772 * Returns size of returned session payload data or a negative error code.
773 */
774 int lttng_register_consumer(struct lttng_handle *handle,
775 const char *socket_path)
776 {
777 int ret;
778 struct lttcomm_session_msg lsm;
779
780 if (handle == NULL || socket_path == NULL) {
781 ret = -LTTNG_ERR_INVALID;
782 goto end;
783 }
784
785 memset(&lsm, 0, sizeof(lsm));
786 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
787 ret = lttng_strncpy(lsm.session.name, handle->session_name,
788 sizeof(lsm.session.name));
789 if (ret) {
790 ret = -LTTNG_ERR_INVALID;
791 goto end;
792 }
793
794 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
795
796 ret = lttng_strncpy(lsm.u.reg.path, socket_path,
797 sizeof(lsm.u.reg.path));
798 if (ret) {
799 ret = -LTTNG_ERR_INVALID;
800 goto end;
801 }
802
803 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
804 end:
805 return ret;
806 }
807
808 /*
809 * Start tracing for all traces of the session.
810 *
811 * Returns size of returned session payload data or a negative error code.
812 */
813 int lttng_start_tracing(const char *session_name)
814 {
815 int ret;
816 struct lttcomm_session_msg lsm;
817
818 if (session_name == NULL) {
819 ret = -LTTNG_ERR_INVALID;
820 goto end;
821 }
822
823 memset(&lsm, 0, sizeof(lsm));
824 lsm.cmd_type = LTTNG_START_TRACE;
825
826 ret = lttng_strncpy(lsm.session.name, session_name,
827 sizeof(lsm.session.name));
828 if (ret) {
829 ret = -LTTNG_ERR_INVALID;
830 goto end;
831 }
832
833 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
834 end:
835 return ret;
836 }
837
838 /*
839 * Stop tracing for all traces of the session.
840 */
841 static int _lttng_stop_tracing(const char *session_name, int wait)
842 {
843 int ret, data_ret;
844 struct lttcomm_session_msg lsm;
845
846 if (session_name == NULL) {
847 ret = -LTTNG_ERR_INVALID;
848 goto error;
849 }
850
851 memset(&lsm, 0, sizeof(lsm));
852 lsm.cmd_type = LTTNG_STOP_TRACE;
853
854 ret = lttng_strncpy(lsm.session.name, session_name,
855 sizeof(lsm.session.name));
856 if (ret) {
857 ret = -LTTNG_ERR_INVALID;
858 goto error;
859 }
860
861 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
862 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
863 goto error;
864 }
865
866 if (!wait) {
867 goto end;
868 }
869
870 /* Check for data availability */
871 do {
872 data_ret = lttng_data_pending(session_name);
873 if (data_ret < 0) {
874 /* Return the data available call error. */
875 ret = data_ret;
876 goto error;
877 }
878
879 /*
880 * Data sleep time before retrying (in usec). Don't sleep if the
881 * call returned value indicates availability.
882 */
883 if (data_ret) {
884 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME_US);
885 }
886 } while (data_ret != 0);
887
888 end:
889 error:
890 return ret;
891 }
892
893 /*
894 * Stop tracing and wait for data availability.
895 */
896 int lttng_stop_tracing(const char *session_name)
897 {
898 return _lttng_stop_tracing(session_name, 1);
899 }
900
901 /*
902 * Stop tracing but _don't_ wait for data availability.
903 */
904 int lttng_stop_tracing_no_wait(const char *session_name)
905 {
906 return _lttng_stop_tracing(session_name, 0);
907 }
908
909 /*
910 * Add context to a channel.
911 *
912 * If the given channel is NULL, add the contexts to all channels.
913 * The event_name param is ignored.
914 *
915 * Returns the size of the returned payload data or a negative error code.
916 */
917 int lttng_add_context(struct lttng_handle *handle,
918 struct lttng_event_context *ctx, const char *event_name,
919 const char *channel_name)
920 {
921 int ret;
922 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_ADD_CONTEXT };
923 struct lttng_payload payload;
924
925 lttng_payload_init(&payload);
926
927 /* Safety check. Both are mandatory. */
928 if (handle == NULL || ctx == NULL) {
929 ret = -LTTNG_ERR_INVALID;
930 goto end;
931 }
932
933 ret = lttng_dynamic_buffer_set_size(&payload.buffer, sizeof(lsm));
934 if (ret) {
935 ret = -LTTNG_ERR_NOMEM;
936 goto end;
937 }
938
939 /* If no channel name, send empty string. */
940 ret = lttng_strncpy(lsm.u.context.channel_name, channel_name ?: "",
941 sizeof(lsm.u.context.channel_name));
942 if (ret) {
943 ret = -LTTNG_ERR_INVALID;
944 goto end;
945 }
946
947 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
948 ret = lttng_strncpy(lsm.session.name, handle->session_name,
949 sizeof(lsm.session.name));
950 if (ret) {
951 ret = -LTTNG_ERR_INVALID;
952 goto end;
953 }
954
955 ret = lttng_event_context_serialize(ctx, &payload);
956 if (ret) {
957 ret = -LTTNG_ERR_INVALID;
958 goto end;
959 }
960
961 lsm.u.context.length = payload.buffer.size - sizeof(lsm);
962
963 /* Update message header. */
964 memcpy(payload.buffer.data, &lsm, sizeof(lsm));
965
966 {
967 struct lttng_payload reply;
968 struct lttng_payload_view payload_view =
969 lttng_payload_view_from_payload(&payload, 0,
970 -1);
971
972 lttng_payload_init(&reply);
973 ret = lttng_ctl_ask_sessiond_payload(&payload_view, &reply);
974 lttng_payload_reset(&reply);
975 if (ret) {
976 goto end;
977 }
978 }
979
980 end:
981 lttng_payload_reset(&payload);
982 return ret;
983 }
984
985 /*
986 * Enable event(s) for a channel.
987 *
988 * If no event name is specified, all events are enabled.
989 * If no channel name is specified, the default 'channel0' is used.
990 *
991 * Returns size of returned session payload data or a negative error code.
992 */
993 int lttng_enable_event(struct lttng_handle *handle,
994 struct lttng_event *ev, const char *channel_name)
995 {
996 return lttng_enable_event_with_exclusions(handle, ev, channel_name,
997 NULL, 0, NULL);
998 }
999
1000 /*
1001 * Create or enable an event with a filter expression.
1002 *
1003 * Return negative error value on error.
1004 * Return size of returned session payload data if OK.
1005 */
1006 int lttng_enable_event_with_filter(struct lttng_handle *handle,
1007 struct lttng_event *event, const char *channel_name,
1008 const char *filter_expression)
1009 {
1010 return lttng_enable_event_with_exclusions(handle, event, channel_name,
1011 filter_expression, 0, NULL);
1012 }
1013
1014 /*
1015 * Depending on the event, return a newly allocated agent filter expression or
1016 * NULL if not applicable.
1017 *
1018 * An event with NO loglevel and the name is * will return NULL.
1019 */
1020 static char *set_agent_filter(const char *filter, struct lttng_event *ev)
1021 {
1022 int err;
1023 char *agent_filter = NULL;
1024
1025 assert(ev);
1026
1027 /* Don't add filter for the '*' event. */
1028 if (strcmp(ev->name, "*") != 0) {
1029 if (filter) {
1030 err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter,
1031 ev->name);
1032 } else {
1033 err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name);
1034 }
1035 if (err < 0) {
1036 PERROR("asprintf");
1037 goto error;
1038 }
1039 }
1040
1041 /* Add loglevel filtering if any for the JUL domain. */
1042 if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) {
1043 const char *op;
1044
1045 if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) {
1046 op = ">=";
1047 } else {
1048 op = "==";
1049 }
1050
1051 if (filter || agent_filter) {
1052 char *new_filter;
1053
1054 err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)",
1055 agent_filter ? agent_filter : filter, op,
1056 ev->loglevel);
1057 if (agent_filter) {
1058 free(agent_filter);
1059 }
1060 agent_filter = new_filter;
1061 } else {
1062 err = asprintf(&agent_filter, "int_loglevel %s %d", op,
1063 ev->loglevel);
1064 }
1065 if (err < 0) {
1066 PERROR("asprintf");
1067 goto error;
1068 }
1069 }
1070
1071 return agent_filter;
1072 error:
1073 free(agent_filter);
1074 return NULL;
1075 }
1076
1077 /*
1078 * Enable event(s) for a channel, possibly with exclusions and a filter.
1079 * If no event name is specified, all events are enabled.
1080 * If no channel name is specified, the default name is used.
1081 * If filter expression is not NULL, the filter is set for the event.
1082 * If exclusion count is not zero, the exclusions are set for the event.
1083 * Returns size of returned session payload data or a negative error code.
1084 */
1085 int lttng_enable_event_with_exclusions(struct lttng_handle *handle,
1086 struct lttng_event *ev, const char *channel_name,
1087 const char *original_filter_expression,
1088 int exclusion_count, char **exclusion_list)
1089 {
1090 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_ENABLE_EVENT };
1091 struct lttng_payload payload;
1092 int ret = 0;
1093 unsigned int free_filter_expression = 0;
1094 struct filter_parser_ctx *ctx = NULL;
1095 size_t bytecode_len = 0;
1096
1097 /*
1098 * We have either a filter or some exclusions, so we need to set up
1099 * a variable-length payload from where to send the data.
1100 */
1101 lttng_payload_init(&payload);
1102
1103 /*
1104 * Cast as non-const since we may replace the filter expression
1105 * by a dynamically allocated string. Otherwise, the original
1106 * string is not modified.
1107 */
1108 char *filter_expression = (char *) original_filter_expression;
1109
1110 if (handle == NULL || ev == NULL) {
1111 ret = -LTTNG_ERR_INVALID;
1112 goto error;
1113 }
1114
1115 /*
1116 * Empty filter string will always be rejected by the parser
1117 * anyway, so treat this corner-case early to eliminate
1118 * lttng_fmemopen error for 0-byte allocation.
1119 */
1120 if (filter_expression && filter_expression[0] == '\0') {
1121 ret = -LTTNG_ERR_INVALID;
1122 goto error;
1123 }
1124
1125 if (ev->name[0] == '\0') {
1126 /* Enable all events. */
1127 ret = lttng_strncpy(ev->name, "*", sizeof(ev->name));
1128 assert(ret == 0);
1129 }
1130
1131 /* Parse filter expression. */
1132 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1133 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1134 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1135 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1136 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1137 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1138 char *agent_filter;
1139
1140 /* Setup agent filter if needed. */
1141 agent_filter = set_agent_filter(filter_expression, ev);
1142 if (!agent_filter) {
1143 if (!filter_expression) {
1144 /*
1145 * No JUL and no filter, just skip
1146 * everything below.
1147 */
1148 goto serialize;
1149 }
1150 } else {
1151 /*
1152 * With an agent filter, the original filter has
1153 * been added to it thus replace the filter
1154 * expression.
1155 */
1156 filter_expression = agent_filter;
1157 free_filter_expression = 1;
1158 }
1159 }
1160
1161 if (strnlen(filter_expression, LTTNG_FILTER_MAX_LEN) ==
1162 LTTNG_FILTER_MAX_LEN) {
1163 ret = -LTTNG_ERR_FILTER_INVAL;
1164 goto error;
1165 }
1166
1167 ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx);
1168 if (ret) {
1169 goto error;
1170 }
1171
1172 bytecode_len = bytecode_get_len(&ctx->bytecode->b) +
1173 sizeof(ctx->bytecode->b);
1174 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
1175 ret = -LTTNG_ERR_FILTER_INVAL;
1176 goto error;
1177 }
1178 }
1179
1180 serialize:
1181 ret = lttng_event_serialize(ev, exclusion_count, exclusion_list,
1182 filter_expression, bytecode_len,
1183 (ctx && bytecode_len) ? &ctx->bytecode->b : NULL,
1184 &payload);
1185 if (ret) {
1186 ret = -LTTNG_ERR_INVALID;
1187 goto error;
1188 }
1189
1190 /* If no channel name, send empty string. */
1191 ret = lttng_strncpy(lsm.u.enable.channel_name, channel_name ?: "",
1192 sizeof(lsm.u.enable.channel_name));
1193 if (ret) {
1194 ret = -LTTNG_ERR_INVALID;
1195 goto error;
1196 }
1197
1198 /* Domain */
1199 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1200
1201 /* Session name */
1202 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1203 sizeof(lsm.session.name));
1204 if (ret) {
1205 ret = -LTTNG_ERR_INVALID;
1206 goto error;
1207 }
1208
1209 /* Length of the serialized event. */
1210 lsm.u.enable.length = (uint32_t) payload.buffer.size;
1211
1212 {
1213 struct lttng_payload_view view = lttng_payload_view_from_payload(
1214 &payload, 0, -1);
1215 int fd_count = lttng_payload_view_get_fd_handle_count(&view);
1216 int fd_to_send;
1217
1218 if (fd_count < 0) {
1219 goto error;
1220 }
1221
1222 assert(fd_count == 0 || fd_count == 1);
1223 if (fd_count == 1) {
1224 struct fd_handle *h =
1225 lttng_payload_view_pop_fd_handle(&view);
1226
1227 if (!h) {
1228 goto error;
1229 }
1230
1231 fd_to_send = fd_handle_get_fd(h);
1232 fd_handle_put(h);
1233 }
1234
1235 lsm.fd_count = fd_count;
1236
1237 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
1238 fd_count ? &fd_to_send : NULL, fd_count,
1239 view.buffer.size ? view.buffer.data : NULL,
1240 view.buffer.size, NULL, NULL, 0);
1241 }
1242
1243 error:
1244 if (filter_expression && ctx) {
1245 filter_bytecode_free(ctx);
1246 filter_ir_free(ctx);
1247 filter_parser_ctx_free(ctx);
1248 }
1249 if (free_filter_expression) {
1250 /*
1251 * The filter expression has been replaced and must be freed as
1252 * it is not the original filter expression received as a
1253 * parameter.
1254 */
1255 free(filter_expression);
1256 }
1257 /*
1258 * Return directly to the caller and don't ask the sessiond since
1259 * something went wrong in the parsing of data above.
1260 */
1261 lttng_payload_reset(&payload);
1262 return ret;
1263 }
1264
1265 int lttng_disable_event_ext(struct lttng_handle *handle,
1266 struct lttng_event *ev, const char *channel_name,
1267 const char *original_filter_expression)
1268 {
1269 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_DISABLE_EVENT };
1270 struct lttng_payload payload;
1271 int ret = 0;
1272 unsigned int free_filter_expression = 0;
1273 struct filter_parser_ctx *ctx = NULL;
1274 size_t bytecode_len = 0;
1275
1276 /*
1277 * We have either a filter or some exclusions, so we need to set up
1278 * a variable-length payload from where to send the data.
1279 */
1280 lttng_payload_init(&payload);
1281
1282 /*
1283 * Cast as non-const since we may replace the filter expression
1284 * by a dynamically allocated string. Otherwise, the original
1285 * string is not modified.
1286 */
1287 char *filter_expression = (char *) original_filter_expression;
1288
1289 if (handle == NULL || ev == NULL) {
1290 ret = -LTTNG_ERR_INVALID;
1291 goto error;
1292 }
1293
1294 /*
1295 * Empty filter string will always be rejected by the parser
1296 * anyway, so treat this corner-case early to eliminate
1297 * lttng_fmemopen error for 0-byte allocation.
1298 */
1299 if (filter_expression && filter_expression[0] == '\0') {
1300 ret = -LTTNG_ERR_INVALID;
1301 goto error;
1302 }
1303
1304 /* Parse filter expression. */
1305 if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL
1306 || handle->domain.type == LTTNG_DOMAIN_LOG4J
1307 || handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1308 if (handle->domain.type == LTTNG_DOMAIN_JUL ||
1309 handle->domain.type == LTTNG_DOMAIN_LOG4J ||
1310 handle->domain.type == LTTNG_DOMAIN_PYTHON) {
1311 char *agent_filter;
1312
1313 /* Setup agent filter if needed. */
1314 agent_filter = set_agent_filter(filter_expression, ev);
1315 if (!agent_filter) {
1316 if (!filter_expression) {
1317 /*
1318 * No JUL and no filter, just skip
1319 * everything below.
1320 */
1321 goto serialize;
1322 }
1323 } else {
1324 /*
1325 * With an agent filter, the original filter has
1326 * been added to it thus replace the filter
1327 * expression.
1328 */
1329 filter_expression = agent_filter;
1330 free_filter_expression = 1;
1331 }
1332 }
1333
1334 if (strnlen(filter_expression, LTTNG_FILTER_MAX_LEN) ==
1335 LTTNG_FILTER_MAX_LEN) {
1336 ret = -LTTNG_ERR_FILTER_INVAL;
1337 goto error;
1338 }
1339
1340 ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx);
1341 if (ret) {
1342 goto error;
1343 }
1344
1345 bytecode_len = bytecode_get_len(&ctx->bytecode->b) +
1346 sizeof(ctx->bytecode->b);
1347 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
1348 ret = -LTTNG_ERR_FILTER_INVAL;
1349 goto error;
1350 }
1351 }
1352
1353 serialize:
1354 ret = lttng_event_serialize(ev, 0, NULL,
1355 filter_expression, bytecode_len,
1356 (ctx && bytecode_len) ? &ctx->bytecode->b : NULL,
1357 &payload);
1358 if (ret) {
1359 ret = -LTTNG_ERR_INVALID;
1360 goto error;
1361 }
1362
1363 /* If no channel name, send empty string. */
1364 ret = lttng_strncpy(lsm.u.disable.channel_name, channel_name ?: "",
1365 sizeof(lsm.u.disable.channel_name));
1366 if (ret) {
1367 ret = -LTTNG_ERR_INVALID;
1368 goto error;
1369 }
1370
1371 /* Domain */
1372 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1373
1374 /* Session name */
1375 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1376 sizeof(lsm.session.name));
1377 if (ret) {
1378 ret = -LTTNG_ERR_INVALID;
1379 goto error;
1380 }
1381
1382 /* Length of the serialized event. */
1383 lsm.u.disable.length = (uint32_t) payload.buffer.size;
1384
1385 {
1386 struct lttng_payload_view view = lttng_payload_view_from_payload(
1387 &payload, 0, -1);
1388 int fd_count = lttng_payload_view_get_fd_handle_count(&view);
1389 int fd_to_send;
1390
1391 if (fd_count < 0) {
1392 goto error;
1393 }
1394
1395 assert(fd_count == 0 || fd_count == 1);
1396 if (fd_count == 1) {
1397 struct fd_handle *h =
1398 lttng_payload_view_pop_fd_handle(&view);
1399
1400 if (!h) {
1401 goto error;
1402 }
1403
1404 fd_to_send = fd_handle_get_fd(h);
1405 fd_handle_put(h);
1406 }
1407
1408 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm,
1409 fd_count ? &fd_to_send : NULL, fd_count,
1410 view.buffer.size ? view.buffer.data : NULL,
1411 view.buffer.size, NULL, NULL, 0);
1412 }
1413
1414 error:
1415 if (filter_expression && ctx) {
1416 filter_bytecode_free(ctx);
1417 filter_ir_free(ctx);
1418 filter_parser_ctx_free(ctx);
1419 }
1420 if (free_filter_expression) {
1421 /*
1422 * The filter expression has been replaced and must be freed as
1423 * it is not the original filter expression received as a
1424 * parameter.
1425 */
1426 free(filter_expression);
1427 }
1428 /*
1429 * Return directly to the caller and don't ask the sessiond since
1430 * something went wrong in the parsing of data above.
1431 */
1432 lttng_payload_reset(&payload);
1433 return ret;
1434 }
1435
1436 /*
1437 * Disable event(s) of a channel and domain.
1438 * If no event name is specified, all events are disabled.
1439 * If no channel name is specified, the default 'channel0' is used.
1440 * Returns size of returned session payload data or a negative error code.
1441 */
1442 int lttng_disable_event(struct lttng_handle *handle, const char *name,
1443 const char *channel_name)
1444 {
1445 int ret;
1446 struct lttng_event ev;
1447
1448 memset(&ev, 0, sizeof(ev));
1449 ev.loglevel = -1;
1450 ev.type = LTTNG_EVENT_ALL;
1451 ret = lttng_strncpy(ev.name, name ?: "", sizeof(ev.name));
1452 if (ret) {
1453 ret = -LTTNG_ERR_INVALID;
1454 goto end;
1455 }
1456
1457 ret = lttng_disable_event_ext(handle, &ev, channel_name, NULL);
1458 end:
1459 return ret;
1460 }
1461
1462 struct lttng_channel *lttng_channel_create(struct lttng_domain *domain)
1463 {
1464 struct lttng_channel *channel = NULL;
1465
1466 if (!domain) {
1467 goto end;
1468 }
1469
1470 /* Validate domain. */
1471 switch (domain->type) {
1472 case LTTNG_DOMAIN_UST:
1473 switch (domain->buf_type) {
1474 case LTTNG_BUFFER_PER_UID:
1475 case LTTNG_BUFFER_PER_PID:
1476 break;
1477 default:
1478 goto end;
1479 }
1480 break;
1481 case LTTNG_DOMAIN_KERNEL:
1482 if (domain->buf_type != LTTNG_BUFFER_GLOBAL) {
1483 goto end;
1484 }
1485 break;
1486 default:
1487 goto end;
1488 }
1489
1490 channel = lttng_channel_create_internal();
1491 if (!channel) {
1492 goto end;
1493 }
1494
1495 lttng_channel_set_default_attr(domain, &channel->attr);
1496 end:
1497 return channel;
1498 }
1499
1500 void lttng_channel_destroy(struct lttng_channel *channel)
1501 {
1502 if (!channel) {
1503 return;
1504 }
1505
1506 if (channel->attr.extended.ptr) {
1507 free(channel->attr.extended.ptr);
1508 }
1509 free(channel);
1510 }
1511
1512 /*
1513 * Enable channel per domain
1514 * Returns size of returned session payload data or a negative error code.
1515 */
1516 int lttng_enable_channel(struct lttng_handle *handle,
1517 struct lttng_channel *in_chan)
1518 {
1519 enum lttng_error_code ret_code;
1520 int ret;
1521 struct lttng_dynamic_buffer buffer;
1522 struct lttcomm_session_msg lsm;
1523 uint64_t total_buffer_size_needed_per_cpu = 0;
1524 struct lttng_channel *channel = NULL;
1525
1526 lttng_dynamic_buffer_init(&buffer);
1527
1528 /* NULL arguments are forbidden. No default values. */
1529 if (handle == NULL || in_chan == NULL) {
1530 ret = -LTTNG_ERR_INVALID;
1531 goto end;
1532 }
1533
1534 /*
1535 * Verify that the amount of memory required to create the requested
1536 * buffer is available on the system at the moment.
1537 */
1538 if (in_chan->attr.num_subbuf >
1539 UINT64_MAX / in_chan->attr.subbuf_size) {
1540 /* Overflow */
1541 ret = -LTTNG_ERR_OVERFLOW;
1542 goto end;
1543 }
1544
1545 total_buffer_size_needed_per_cpu =
1546 in_chan->attr.num_subbuf * in_chan->attr.subbuf_size;
1547 ret_code = check_enough_available_memory(
1548 total_buffer_size_needed_per_cpu);
1549 if (ret_code != LTTNG_OK) {
1550 ret = -ret_code;
1551 goto end;
1552 }
1553
1554 /* Copy the channel for easier manipulation. */
1555 channel = lttng_channel_copy(in_chan);
1556 if (!channel) {
1557 ret = -LTTNG_ERR_NOMEM;
1558 goto end;
1559 }
1560
1561 /* Populate the channel extended attribute if necessary. */
1562 if (!channel->attr.extended.ptr) {
1563 struct lttng_channel_extended *extended =
1564 (struct lttng_channel_extended *) zmalloc(
1565 sizeof(*extended));
1566
1567 if (!extended) {
1568 ret = -LTTNG_ERR_NOMEM;
1569 goto end;
1570 }
1571 lttng_channel_set_default_extended_attr(
1572 &handle->domain, extended);
1573 channel->attr.extended.ptr = extended;
1574 }
1575
1576 /* Prepare the payload */
1577 memset(&lsm, 0, sizeof(lsm));
1578
1579 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
1580 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1581
1582 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1583 sizeof(lsm.session.name));
1584 if (ret) {
1585 ret = -LTTNG_ERR_INVALID;
1586 goto end;
1587 }
1588
1589 ret = lttng_channel_serialize(channel, &buffer);
1590 if (ret) {
1591 ret = -LTTNG_ERR_FATAL;
1592 goto end;
1593 }
1594
1595 lsm.u.channel.length = buffer.size;
1596
1597 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(
1598 &lsm, buffer.data, buffer.size, NULL);
1599 end:
1600 lttng_channel_destroy(channel);
1601 lttng_dynamic_buffer_reset(&buffer);
1602 return ret;
1603 }
1604
1605 /*
1606 * All tracing will be stopped for registered events of the channel.
1607 * Returns size of returned session payload data or a negative error code.
1608 */
1609 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
1610 {
1611 int ret;
1612 struct lttcomm_session_msg lsm;
1613
1614 /* Safety check. Both are mandatory. */
1615 if (handle == NULL || name == NULL) {
1616 return -LTTNG_ERR_INVALID;
1617 }
1618
1619 memset(&lsm, 0, sizeof(lsm));
1620
1621 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1622
1623 ret = lttng_strncpy(lsm.u.disable.channel_name, name,
1624 sizeof(lsm.u.disable.channel_name));
1625 if (ret) {
1626 ret = -LTTNG_ERR_INVALID;
1627 goto end;
1628 }
1629
1630 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1631
1632 ret = lttng_strncpy(lsm.session.name, handle->session_name,
1633 sizeof(lsm.session.name));
1634 if (ret) {
1635 ret = -LTTNG_ERR_INVALID;
1636 goto end;
1637 }
1638
1639 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
1640 end:
1641 return ret;
1642 }
1643
1644 /*
1645 * Lists all available tracepoints of domain.
1646 * Sets the contents of the events array.
1647 * Returns the number of lttng_event entries in events;
1648 * on error, returns a negative value.
1649 */
1650 int lttng_list_tracepoints(struct lttng_handle *handle,
1651 struct lttng_event **events)
1652 {
1653 enum lttng_error_code ret_code;
1654 int ret, total_payload_received;
1655 char *reception_buffer = NULL;
1656 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_LIST_TRACEPOINTS };
1657 struct lttcomm_list_command_header *cmd_header = NULL;
1658 size_t cmd_header_len;
1659 unsigned int nb_events = 0;
1660
1661 if (handle == NULL) {
1662 ret = -LTTNG_ERR_INVALID;
1663 goto end;
1664 }
1665
1666 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1667
1668 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
1669 (void **) &reception_buffer, (void **) &cmd_header,
1670 &cmd_header_len);
1671 if (ret < 0) {
1672 goto end;
1673 }
1674
1675 total_payload_received = ret;
1676
1677 if (!cmd_header) {
1678 ret = -LTTNG_ERR_UNK;
1679 goto end;
1680 }
1681
1682 if (cmd_header->count > INT_MAX) {
1683 ret = -LTTNG_ERR_OVERFLOW;
1684 goto end;
1685 }
1686
1687 nb_events = (unsigned int) cmd_header->count;
1688
1689 {
1690 struct lttng_buffer_view events_view =
1691 lttng_buffer_view_init(reception_buffer, 0,
1692 total_payload_received);
1693 struct lttng_payload_view events_payload_view =
1694 lttng_payload_view_from_buffer_view(
1695 &events_view, 0, -1);
1696
1697 ret_code = lttng_events_create_and_flatten_from_payload(
1698 &events_payload_view, nb_events, events);
1699 if (ret_code != LTTNG_OK) {
1700 ret = -ret_code;
1701 goto end;
1702 }
1703 }
1704
1705 ret = (int) nb_events;
1706
1707 end:
1708 free(cmd_header);
1709 free(reception_buffer);
1710 return ret;
1711 }
1712
1713 /*
1714 * Lists all available tracepoint fields of domain.
1715 * Sets the contents of the event field array.
1716 * Returns the number of lttng_event_field entries in events;
1717 * on error, returns a negative value.
1718 */
1719 int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1720 struct lttng_event_field **fields)
1721 {
1722 enum lttng_error_code ret_code;
1723 int ret;
1724 struct lttcomm_session_msg lsm;
1725 const struct lttcomm_list_command_header *cmd_header = NULL;
1726 unsigned int nb_event_fields = 0;
1727 struct lttng_payload reply;
1728
1729 lttng_payload_init(&reply);
1730
1731 if (handle == NULL) {
1732 ret = -LTTNG_ERR_INVALID;
1733 goto end;
1734 }
1735
1736 memset(&lsm, 0, sizeof(lsm));
1737 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
1738 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
1739
1740 {
1741 struct lttng_payload_view message_view =
1742 lttng_payload_view_init_from_buffer(
1743 (const char *) &lsm, 0,
1744 sizeof(lsm));
1745
1746 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
1747 if (ret < 0) {
1748 goto end;
1749 }
1750 }
1751
1752 {
1753 const struct lttng_buffer_view cmd_header_view =
1754 lttng_buffer_view_from_dynamic_buffer(
1755 &reply.buffer, 0, sizeof(*cmd_header));
1756
1757 if (!lttng_buffer_view_is_valid(&cmd_header_view)) {
1758 ret = -LTTNG_ERR_INVALID_PROTOCOL;
1759 goto end;
1760 }
1761
1762 cmd_header = (struct lttcomm_list_command_header *)
1763 cmd_header_view.data;
1764 }
1765
1766 if (cmd_header->count > INT_MAX) {
1767 ret = -LTTNG_ERR_OVERFLOW;
1768 goto end;
1769 }
1770
1771 nb_event_fields = cmd_header->count;
1772
1773 {
1774 struct lttng_payload_view reply_view =
1775 lttng_payload_view_from_payload(&reply,
1776 sizeof(*cmd_header), -1);
1777
1778 ret_code = lttng_event_fields_create_and_flatten_from_payload(
1779 &reply_view, nb_event_fields, fields);
1780 if (ret_code != LTTNG_OK) {
1781 ret = -ret_code;
1782 goto end;
1783 }
1784 }
1785
1786 ret = nb_event_fields;
1787
1788 end:
1789 lttng_payload_reset(&reply);
1790 return ret;
1791 }
1792
1793 /*
1794 * Lists all available kernel system calls. Allocates and sets the contents of
1795 * the events array.
1796 *
1797 * Returns the number of lttng_event entries in events; on error, returns a
1798 * negative value.
1799 */
1800 int lttng_list_syscalls(struct lttng_event **events)
1801 {
1802 enum lttng_error_code ret_code;
1803 int ret, total_payload_received;
1804 char *reception_buffer = NULL;
1805 struct lttcomm_session_msg lsm = {};
1806 struct lttcomm_list_command_header *cmd_header = NULL;
1807 size_t cmd_header_len;
1808 uint32_t nb_events = 0;
1809
1810 if (!events) {
1811 ret = -LTTNG_ERR_INVALID;
1812 goto end;
1813 }
1814
1815 lsm.cmd_type = LTTNG_LIST_SYSCALLS;
1816 /* Force kernel domain for system calls. */
1817 lsm.domain.type = LTTNG_DOMAIN_KERNEL;
1818
1819 ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0,
1820 (void **) &reception_buffer, (void **) &cmd_header,
1821 &cmd_header_len);
1822 if (ret < 0) {
1823 goto end;
1824 }
1825 total_payload_received = ret;
1826
1827 if (!cmd_header) {
1828 ret = -LTTNG_ERR_UNK;
1829 goto end;
1830 }
1831
1832 if (cmd_header->count > INT_MAX) {
1833 ret = -LTTNG_ERR_OVERFLOW;
1834 goto end;
1835 }
1836
1837 nb_events = (unsigned int) cmd_header->count;
1838
1839 {
1840 const struct lttng_buffer_view events_view =
1841 lttng_buffer_view_init(reception_buffer, 0,
1842 total_payload_received);
1843 struct lttng_payload_view events_payload_view =
1844 lttng_payload_view_from_buffer_view(
1845 &events_view, 0, -1);
1846
1847 ret_code = lttng_events_create_and_flatten_from_payload(
1848 &events_payload_view, nb_events, events);
1849 if (ret_code != LTTNG_OK) {
1850 ret = -ret_code;
1851 goto end;
1852 }
1853 }
1854
1855 ret = (int) nb_events;
1856
1857 end:
1858 free(reception_buffer);
1859 free(cmd_header);
1860 return ret;
1861 }
1862
1863 /*
1864 * Returns a human readable string describing
1865 * the error code (a negative value).
1866 */
1867 const char *lttng_strerror(int code)
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 = -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(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 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 = 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 struct 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 struct 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 char *new_group;
2503 if (name == NULL) {
2504 return -LTTNG_ERR_INVALID;
2505 }
2506
2507 if (asprintf(&new_group, "%s", name) < 0) {
2508 return -LTTNG_ERR_FATAL;
2509 }
2510
2511 free(tracing_group);
2512 tracing_group = new_group;
2513 new_group = NULL;
2514
2515 return 0;
2516 }
2517
2518 int lttng_calibrate(struct lttng_handle *handle,
2519 struct lttng_calibrate *calibrate)
2520 {
2521 /*
2522 * This command was removed in LTTng 2.9.
2523 */
2524 return -LTTNG_ERR_UND;
2525 }
2526
2527 /*
2528 * Set default channel attributes.
2529 * If either or both of the arguments are null, attr content is zeroe'd.
2530 */
2531 void lttng_channel_set_default_attr(struct lttng_domain *domain,
2532 struct lttng_channel_attr *attr)
2533 {
2534 struct lttng_channel_extended *extended;
2535
2536 /* Safety check */
2537 if (attr == NULL || domain == NULL) {
2538 return;
2539 }
2540
2541 /* Save the pointer for later use */
2542 extended = (struct lttng_channel_extended *) attr->extended.ptr;
2543 memset(attr, 0, sizeof(struct lttng_channel_attr));
2544
2545 /* Same for all domains. */
2546 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
2547 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
2548 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
2549
2550 switch (domain->type) {
2551 case LTTNG_DOMAIN_KERNEL:
2552 attr->switch_timer_interval =
2553 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
2554 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
2555 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
2556 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
2557 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
2558 break;
2559 case LTTNG_DOMAIN_UST:
2560 switch (domain->buf_type) {
2561 case LTTNG_BUFFER_PER_UID:
2562 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
2563 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
2564 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
2565 attr->switch_timer_interval =
2566 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
2567 attr->read_timer_interval =
2568 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
2569 break;
2570 case LTTNG_BUFFER_PER_PID:
2571 default:
2572 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
2573 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
2574 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
2575 attr->switch_timer_interval =
2576 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
2577 attr->read_timer_interval =
2578 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
2579 break;
2580 }
2581 default:
2582 /* Default behavior: leave set to 0. */
2583 break;
2584 }
2585
2586 if (extended) {
2587 lttng_channel_set_default_extended_attr(domain, extended);
2588 }
2589
2590 /* Reassign the extended pointer. */
2591 attr->extended.ptr = extended;
2592 }
2593
2594 int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
2595 uint64_t *discarded_events)
2596 {
2597 int ret = 0;
2598 struct lttng_channel_extended *chan_ext;
2599
2600 if (!channel || !discarded_events) {
2601 ret = -LTTNG_ERR_INVALID;
2602 goto end;
2603 }
2604
2605 chan_ext = channel->attr.extended.ptr;
2606 if (!chan_ext) {
2607 /*
2608 * This can happen since the lttng_channel structure is
2609 * used for other tasks where this pointer is never set.
2610 */
2611 *discarded_events = 0;
2612 goto end;
2613 }
2614
2615 *discarded_events = chan_ext->discarded_events;
2616 end:
2617 return ret;
2618 }
2619
2620 int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2621 uint64_t *lost_packets)
2622 {
2623 int ret = 0;
2624 struct lttng_channel_extended *chan_ext;
2625
2626 if (!channel || !lost_packets) {
2627 ret = -LTTNG_ERR_INVALID;
2628 goto end;
2629 }
2630
2631 chan_ext = channel->attr.extended.ptr;
2632 if (!chan_ext) {
2633 /*
2634 * This can happen since the lttng_channel structure is
2635 * used for other tasks where this pointer is never set.
2636 */
2637 *lost_packets = 0;
2638 goto end;
2639 }
2640
2641 *lost_packets = chan_ext->lost_packets;
2642 end:
2643 return ret;
2644 }
2645
2646 int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
2647 uint64_t *monitor_timer_interval)
2648 {
2649 int ret = 0;
2650
2651 if (!chan || !monitor_timer_interval) {
2652 ret = -LTTNG_ERR_INVALID;
2653 goto end;
2654 }
2655
2656 if (!chan->attr.extended.ptr) {
2657 ret = -LTTNG_ERR_INVALID;
2658 goto end;
2659 }
2660
2661 *monitor_timer_interval = ((struct lttng_channel_extended *)
2662 chan->attr.extended.ptr)->monitor_timer_interval;
2663 end:
2664 return ret;
2665 }
2666
2667 int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
2668 uint64_t monitor_timer_interval)
2669 {
2670 int ret = 0;
2671
2672 if (!chan || !chan->attr.extended.ptr) {
2673 ret = -LTTNG_ERR_INVALID;
2674 goto end;
2675 }
2676
2677 ((struct lttng_channel_extended *)
2678 chan->attr.extended.ptr)->monitor_timer_interval =
2679 monitor_timer_interval;
2680 end:
2681 return ret;
2682 }
2683
2684 int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
2685 int64_t *blocking_timeout)
2686 {
2687 int ret = 0;
2688
2689 if (!chan || !blocking_timeout) {
2690 ret = -LTTNG_ERR_INVALID;
2691 goto end;
2692 }
2693
2694 if (!chan->attr.extended.ptr) {
2695 ret = -LTTNG_ERR_INVALID;
2696 goto end;
2697 }
2698
2699 *blocking_timeout = ((struct lttng_channel_extended *)
2700 chan->attr.extended.ptr)->blocking_timeout;
2701 end:
2702 return ret;
2703 }
2704
2705 int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
2706 int64_t blocking_timeout)
2707 {
2708 int ret = 0;
2709 int64_t msec_timeout;
2710
2711 if (!chan || !chan->attr.extended.ptr) {
2712 ret = -LTTNG_ERR_INVALID;
2713 goto end;
2714 }
2715
2716 if (blocking_timeout < 0 && blocking_timeout != -1) {
2717 ret = -LTTNG_ERR_INVALID;
2718 goto end;
2719 }
2720
2721 /*
2722 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2723 * us to accept a narrower range of values (msecs expressed as a signed
2724 * 32-bit integer).
2725 */
2726 msec_timeout = blocking_timeout / 1000;
2727 if (msec_timeout != (int32_t) msec_timeout) {
2728 ret = -LTTNG_ERR_INVALID;
2729 goto end;
2730 }
2731
2732 ((struct lttng_channel_extended *)
2733 chan->attr.extended.ptr)->blocking_timeout =
2734 blocking_timeout;
2735 end:
2736 return ret;
2737 }
2738
2739 /*
2740 * Check if session daemon is alive.
2741 *
2742 * Return 1 if alive or 0 if not.
2743 * On error returns a negative value.
2744 */
2745 int lttng_session_daemon_alive(void)
2746 {
2747 int ret;
2748
2749 ret = set_session_daemon_path();
2750 if (ret < 0) {
2751 /* Error. */
2752 return ret;
2753 }
2754
2755 if (*sessiond_sock_path == '\0') {
2756 /*
2757 * No socket path set. Weird error which means the constructor
2758 * was not called.
2759 */
2760 assert(0);
2761 }
2762
2763 ret = try_connect_sessiond(sessiond_sock_path);
2764 if (ret < 0) {
2765 /* Not alive. */
2766 return 0;
2767 }
2768
2769 /* Is alive. */
2770 return 1;
2771 }
2772
2773 /*
2774 * Set URL for a consumer for a session and domain.
2775 *
2776 * Return 0 on success, else a negative value.
2777 */
2778 int lttng_set_consumer_url(struct lttng_handle *handle,
2779 const char *control_url, const char *data_url)
2780 {
2781 int ret;
2782 ssize_t size;
2783 struct lttcomm_session_msg lsm;
2784 struct lttng_uri *uris = NULL;
2785
2786 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2787 ret = -LTTNG_ERR_INVALID;
2788 goto error;
2789 }
2790
2791 memset(&lsm, 0, sizeof(lsm));
2792
2793 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2794
2795 ret = lttng_strncpy(lsm.session.name, handle->session_name,
2796 sizeof(lsm.session.name));
2797 if (ret) {
2798 ret = -LTTNG_ERR_INVALID;
2799 goto error;
2800 }
2801
2802 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
2803
2804 size = uri_parse_str_urls(control_url, data_url, &uris);
2805 if (size < 0) {
2806 ret = -LTTNG_ERR_INVALID;
2807 goto error;
2808 }
2809
2810 lsm.u.uri.size = size;
2811
2812 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2813 sizeof(struct lttng_uri) * size, NULL);
2814
2815 free(uris);
2816 error:
2817 return ret;
2818 }
2819
2820 /*
2821 * [OBSOLETE]
2822 */
2823 int lttng_enable_consumer(struct lttng_handle *handle);
2824 int lttng_enable_consumer(struct lttng_handle *handle)
2825 {
2826 return -ENOSYS;
2827 }
2828
2829 /*
2830 * [OBSOLETE]
2831 */
2832 int lttng_disable_consumer(struct lttng_handle *handle);
2833 int lttng_disable_consumer(struct lttng_handle *handle)
2834 {
2835 return -ENOSYS;
2836 }
2837
2838 /*
2839 * [OBSOLETE]
2840 */
2841 int _lttng_create_session_ext(const char *name, const char *url,
2842 const char *datetime);
2843 int _lttng_create_session_ext(const char *name, const char *url,
2844 const char *datetime)
2845 {
2846 return -ENOSYS;
2847 }
2848
2849 /*
2850 * For a given session name, this call checks if the data is ready to be read
2851 * or is still being extracted by the consumer(s) hence not ready to be used by
2852 * any readers.
2853 */
2854 int lttng_data_pending(const char *session_name)
2855 {
2856 int ret;
2857 struct lttcomm_session_msg lsm;
2858 uint8_t *pending = NULL;
2859
2860 if (session_name == NULL) {
2861 return -LTTNG_ERR_INVALID;
2862 }
2863
2864 memset(&lsm, 0, sizeof(lsm));
2865 lsm.cmd_type = LTTNG_DATA_PENDING;
2866
2867 ret = lttng_strncpy(lsm.session.name, session_name,
2868 sizeof(lsm.session.name));
2869 if (ret) {
2870 ret = -LTTNG_ERR_INVALID;
2871 goto end;
2872 }
2873
2874 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2875 if (ret < 0) {
2876 goto end;
2877 } else if (ret != 1) {
2878 /* Unexpected payload size */
2879 ret = -LTTNG_ERR_INVALID;
2880 goto end;
2881 } else if (!pending) {
2882 /* Internal error. */
2883 ret = -LTTNG_ERR_UNK;
2884 goto end;
2885 }
2886
2887 ret = (int) *pending;
2888 end:
2889 free(pending);
2890 return ret;
2891 }
2892
2893 /*
2894 * Regenerate the metadata for a session.
2895 * Return 0 on success, a negative error code on error.
2896 */
2897 int lttng_regenerate_metadata(const char *session_name)
2898 {
2899 int ret;
2900 struct lttcomm_session_msg lsm;
2901
2902 if (!session_name) {
2903 ret = -LTTNG_ERR_INVALID;
2904 goto end;
2905 }
2906
2907 memset(&lsm, 0, sizeof(lsm));
2908 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
2909
2910 ret = lttng_strncpy(lsm.session.name, session_name,
2911 sizeof(lsm.session.name));
2912 if (ret) {
2913 ret = -LTTNG_ERR_INVALID;
2914 goto end;
2915 }
2916
2917 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2918 if (ret < 0) {
2919 goto end;
2920 }
2921
2922 ret = 0;
2923 end:
2924 return ret;
2925 }
2926
2927 /*
2928 * Deprecated, replaced by lttng_regenerate_metadata.
2929 */
2930 int lttng_metadata_regenerate(const char *session_name)
2931 {
2932 return lttng_regenerate_metadata(session_name);
2933 }
2934
2935 /*
2936 * Regenerate the statedump of a session.
2937 * Return 0 on success, a negative error code on error.
2938 */
2939 int lttng_regenerate_statedump(const char *session_name)
2940 {
2941 int ret;
2942 struct lttcomm_session_msg lsm;
2943
2944 if (!session_name) {
2945 ret = -LTTNG_ERR_INVALID;
2946 goto end;
2947 }
2948
2949 memset(&lsm, 0, sizeof(lsm));
2950 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
2951
2952 ret = lttng_strncpy(lsm.session.name, session_name,
2953 sizeof(lsm.session.name));
2954 if (ret) {
2955 ret = -LTTNG_ERR_INVALID;
2956 goto end;
2957 }
2958
2959 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2960 if (ret < 0) {
2961 goto end;
2962 }
2963
2964 ret = 0;
2965 end:
2966 return ret;
2967 }
2968
2969 static
2970 int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name,
2971 bool generate_name)
2972 {
2973 int ret;
2974 struct lttcomm_session_msg lsm = {
2975 .cmd_type = LTTNG_REGISTER_TRIGGER,
2976 .u.trigger.is_trigger_anonymous = !name && !generate_name,
2977 };
2978 struct lttcomm_session_msg *message_lsm;
2979 struct lttng_payload message;
2980 struct lttng_payload reply;
2981 struct lttng_trigger *reply_trigger = NULL;
2982 enum lttng_domain_type domain_type;
2983 const struct lttng_credentials user_creds = {
2984 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
2985 .gid = LTTNG_OPTIONAL_INIT_UNSET,
2986 };
2987 const char *unused_trigger_name = NULL;
2988 enum lttng_trigger_status trigger_status;
2989
2990 lttng_payload_init(&message);
2991 lttng_payload_init(&reply);
2992
2993 if (!trigger) {
2994 ret = -LTTNG_ERR_INVALID;
2995 goto end;
2996 }
2997
2998 trigger_status = lttng_trigger_get_name(trigger, &unused_trigger_name);
2999 if (trigger_status != LTTNG_TRIGGER_STATUS_UNSET) {
3000 /* Re-using already registered trigger. */
3001 ret = -LTTNG_ERR_INVALID;
3002 goto end;
3003 }
3004
3005 if (name) {
3006 trigger_status = lttng_trigger_set_name(trigger, name);
3007 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
3008 ret = -LTTNG_ERR_NOMEM;
3009 goto end;
3010 }
3011 }
3012
3013 if (!trigger->creds.uid.is_set) {
3014 /* Use the client's credentials as the trigger credentials. */
3015 lttng_trigger_set_credentials(trigger, &user_creds);
3016 } else {
3017 /*
3018 * Validate that either the current trigger credentials and the
3019 * client credentials are identical or that the current user is
3020 * root. The root user can register, unregister triggers for
3021 * himself and other users.
3022 *
3023 * This check is also present on the sessiond side, using the
3024 * credentials passed on the socket. These check are all
3025 * "safety" checks.
3026 */
3027 const struct lttng_credentials *trigger_creds =
3028 lttng_trigger_get_credentials(trigger);
3029
3030 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3031 if (lttng_credentials_get_uid(&user_creds) != 0) {
3032 ret = -LTTNG_ERR_EPERM;
3033 goto end_unset_name;
3034 }
3035 }
3036 }
3037
3038 if (!lttng_trigger_validate(trigger)) {
3039 ret = -LTTNG_ERR_INVALID_TRIGGER;
3040 goto end_unset_name;
3041 }
3042
3043 domain_type = lttng_trigger_get_underlying_domain_type_restriction(
3044 trigger);
3045
3046 lsm.domain.type = domain_type;
3047
3048 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3049 if (ret) {
3050 ret = -LTTNG_ERR_NOMEM;
3051 goto end_unset_name;
3052 }
3053
3054 ret = lttng_trigger_serialize(trigger, &message);
3055 if (ret < 0) {
3056 ret = -LTTNG_ERR_UNK;
3057 goto end_unset_name;
3058 }
3059
3060 /*
3061 * This is needed to populate the trigger object size for the command
3062 * header.
3063 */
3064 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3065
3066 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3067
3068 {
3069 struct lttng_payload_view message_view =
3070 lttng_payload_view_from_payload(
3071 &message, 0, -1);
3072
3073 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3074 &message_view);
3075 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3076 if (ret < 0) {
3077 goto end_unset_name;
3078 }
3079 }
3080
3081 {
3082 struct lttng_payload_view reply_view =
3083 lttng_payload_view_from_payload(
3084 &reply, 0, reply.buffer.size);
3085
3086 ret = lttng_trigger_create_from_payload(
3087 &reply_view, &reply_trigger);
3088 if (ret < 0) {
3089 ret = -LTTNG_ERR_INVALID_PROTOCOL;
3090 goto end_unset_name;
3091 }
3092 }
3093
3094 if (name || generate_name) {
3095 ret = lttng_trigger_assign_name(trigger, reply_trigger);
3096 if (ret < 0) {
3097 ret = -LTTNG_ERR_NOMEM;
3098 goto end;
3099 }
3100 }
3101
3102 ret = 0;
3103 goto end;
3104
3105 end_unset_name:
3106 trigger_status = lttng_trigger_set_name(trigger, NULL);
3107 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
3108 ret = -LTTNG_ERR_UNK;
3109 }
3110 end:
3111 lttng_payload_reset(&message);
3112 lttng_payload_reset(&reply);
3113 lttng_trigger_destroy(reply_trigger);
3114 return ret;
3115 }
3116
3117 int lttng_register_trigger(struct lttng_trigger *trigger)
3118 {
3119 /* Register an anonymous trigger. */
3120 return _lttng_register_trigger(trigger, NULL, false);
3121 }
3122
3123 enum lttng_error_code lttng_register_trigger_with_name(
3124 struct lttng_trigger *trigger, const char *name)
3125 {
3126 const int ret = _lttng_register_trigger(trigger, name, false);
3127
3128 return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret;
3129 }
3130
3131 enum lttng_error_code lttng_register_trigger_with_automatic_name(
3132 struct lttng_trigger *trigger)
3133 {
3134 const int ret = _lttng_register_trigger(trigger, false, true);
3135
3136 return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret;
3137 }
3138
3139 enum lttng_error_code lttng_error_query_execute(
3140 const struct lttng_error_query *query,
3141 const struct lttng_endpoint *endpoint,
3142 struct lttng_error_query_results **results)
3143 {
3144 int ret;
3145 enum lttng_error_code ret_code;
3146 struct lttcomm_session_msg lsm = {
3147 .cmd_type = LTTNG_EXECUTE_ERROR_QUERY,
3148 };
3149 struct lttng_payload message;
3150 struct lttng_payload reply;
3151 struct lttcomm_session_msg *message_lsm;
3152
3153 lttng_payload_init(&message);
3154 lttng_payload_init(&reply);
3155
3156 if (!query || !results) {
3157 ret_code = LTTNG_ERR_INVALID;
3158 goto end;
3159 }
3160
3161 if (endpoint != lttng_session_daemon_command_endpoint) {
3162 ret_code = LTTNG_ERR_INVALID_ERROR_QUERY_TARGET;
3163 goto end;
3164 }
3165
3166 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3167 if (ret) {
3168 ret_code = LTTNG_ERR_NOMEM;
3169 goto end;
3170 }
3171
3172 ret = lttng_error_query_serialize(query, &message);
3173 if (ret) {
3174 ret_code = LTTNG_ERR_UNK;
3175 goto end;
3176 }
3177
3178 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3179 message_lsm->u.error_query.length =
3180 (uint32_t) message.buffer.size - sizeof(lsm);
3181
3182 {
3183 struct lttng_payload_view message_view =
3184 lttng_payload_view_from_payload(
3185 &message, 0, -1);
3186
3187 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3188 &message_view);
3189 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3190 if (ret < 0) {
3191 ret_code = -ret;
3192 goto end;
3193 }
3194 }
3195
3196 {
3197 ssize_t reply_create_ret;
3198 struct lttng_payload_view reply_view =
3199 lttng_payload_view_from_payload(
3200 &reply, 0, reply.buffer.size);
3201
3202 reply_create_ret = lttng_error_query_results_create_from_payload(
3203 &reply_view, results);
3204 if (reply_create_ret < 0) {
3205 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3206 goto end;
3207 }
3208 }
3209
3210 ret_code = LTTNG_OK;
3211 end:
3212 lttng_payload_reset(&message);
3213 lttng_payload_reset(&reply);
3214 return ret_code;
3215 }
3216
3217 int lttng_unregister_trigger(const struct lttng_trigger *trigger)
3218 {
3219 int ret;
3220 struct lttcomm_session_msg lsm;
3221 struct lttcomm_session_msg *message_lsm;
3222 struct lttng_payload message;
3223 struct lttng_payload reply;
3224 struct lttng_trigger *copy = NULL;
3225 const struct lttng_credentials user_creds = {
3226 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3227 .gid = LTTNG_OPTIONAL_INIT_UNSET,
3228 };
3229
3230 lttng_payload_init(&message);
3231 lttng_payload_init(&reply);
3232
3233 if (!trigger) {
3234 ret = -LTTNG_ERR_INVALID;
3235 goto end;
3236 }
3237
3238 copy = lttng_trigger_copy(trigger);
3239 if (!copy) {
3240 ret = -LTTNG_ERR_UNK;
3241 goto end;
3242 }
3243
3244 if (!copy->creds.uid.is_set) {
3245 /* Use the client credentials as the trigger credentials */
3246 lttng_trigger_set_credentials(copy, &user_creds);
3247 } else {
3248 /*
3249 * Validate that either the current trigger credentials and the
3250 * client credentials are identical or that the current user is
3251 * root. The root user can register, unregister triggers for
3252 * himself and other users.
3253 *
3254 * This check is also present on the sessiond side, using the
3255 * credentials passed on the socket. These check are all
3256 * "safety" checks.
3257 */
3258 const struct lttng_credentials *trigger_creds =
3259 lttng_trigger_get_credentials(copy);
3260 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3261 if (lttng_credentials_get_uid(&user_creds) != 0) {
3262 ret = -LTTNG_ERR_EPERM;
3263 goto end;
3264 }
3265 }
3266 }
3267
3268 if (!lttng_trigger_validate(copy)) {
3269 ret = -LTTNG_ERR_INVALID_TRIGGER;
3270 goto end;
3271 }
3272
3273 memset(&lsm, 0, sizeof(lsm));
3274 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
3275
3276 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3277 if (ret) {
3278 ret = -LTTNG_ERR_NOMEM;
3279 goto end;
3280 }
3281
3282 ret = lttng_trigger_serialize(copy, &message);
3283 if (ret < 0) {
3284 ret = -LTTNG_ERR_UNK;
3285 goto end;
3286 }
3287
3288 /*
3289 * This is needed to populate the trigger object size for the command
3290 * header and number of fds sent.
3291 */
3292 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3293
3294 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3295
3296 {
3297 struct lttng_payload_view message_view =
3298 lttng_payload_view_from_payload(
3299 &message, 0, -1);
3300
3301 /*
3302 * Update the message header with the number of fd that will be
3303 * sent.
3304 */
3305 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3306 &message_view);
3307
3308 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3309 if (ret < 0) {
3310 goto end;
3311 }
3312 }
3313
3314 ret = 0;
3315 end:
3316 lttng_trigger_destroy(copy);
3317 lttng_payload_reset(&message);
3318 lttng_payload_reset(&reply);
3319 return ret;
3320 }
3321
3322 /*
3323 * Ask the session daemon for all registered triggers for the current user.
3324 *
3325 * Allocates and return an lttng_triggers set.
3326 * On error, returns a suitable lttng_error_code.
3327 */
3328 enum lttng_error_code lttng_list_triggers(struct lttng_triggers **triggers)
3329 {
3330 int ret;
3331 enum lttng_error_code ret_code = LTTNG_OK;
3332 struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_LIST_TRIGGERS };
3333 struct lttng_triggers *local_triggers = NULL;
3334 struct lttng_payload reply;
3335 struct lttng_payload_view lsm_view =
3336 lttng_payload_view_init_from_buffer(
3337 (const char *) &lsm, 0, sizeof(lsm));
3338
3339 lttng_payload_init(&reply);
3340
3341 ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply);
3342 if (ret < 0) {
3343 ret_code = (enum lttng_error_code) -ret;
3344 goto end;
3345 }
3346
3347 {
3348 struct lttng_payload_view reply_view =
3349 lttng_payload_view_from_payload(
3350 &reply, 0, reply.buffer.size);
3351
3352 ret = lttng_triggers_create_from_payload(
3353 &reply_view, &local_triggers);
3354 if (ret < 0) {
3355 ret_code = LTTNG_ERR_FATAL;
3356 goto end;
3357 }
3358 }
3359
3360 *triggers = local_triggers;
3361 local_triggers = NULL;
3362 end:
3363 lttng_payload_reset(&reply);
3364 lttng_triggers_destroy(local_triggers);
3365 return ret_code;
3366 }
3367
3368 /*
3369 * lib constructor.
3370 */
3371 static void __attribute__((constructor)) init(void)
3372 {
3373 /* Set default session group */
3374 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
3375 }
3376
3377 /*
3378 * lib destructor.
3379 */
3380 static void __attribute__((destructor)) lttng_ctl_exit(void)
3381 {
3382 free(tracing_group);
3383 }
This page took 0.147582 seconds and 4 git commands to generate.