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