Fix: trigger: erroneous check for success of trigger creation
[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 (!lttng_buffer_view_is_valid(&cmd_header_view)) {
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 if (!lttng_payload_view_is_valid(&probe_location_view)) {
2314 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2315 goto end;
2316 }
2317
2318 /*
2319 * Create a temporary userspace probe location
2320 * to determine the size needed by a "flattened"
2321 * version of that same probe location.
2322 */
2323 ret = lttng_userspace_probe_location_create_from_payload(
2324 &probe_location_view,
2325 &probe_location);
2326 if (ret < 0) {
2327 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2328 goto end;
2329 }
2330
2331 ret = lttng_userspace_probe_location_flatten(
2332 probe_location, NULL);
2333 lttng_userspace_probe_location_destroy(
2334 probe_location);
2335 if (ret < 0) {
2336 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2337 goto end;
2338 }
2339
2340 probe_storage_req = ret;
2341 comm_ext_at += ext_comm->userspace_probe_location_len;
2342 }
2343
2344 storage_req += sizeof(struct lttng_event_extended);
2345 storage_req += ext_comm->filter_len;
2346 storage_req += ext_comm->nb_exclusions *
2347 LTTNG_SYMBOL_NAME_LEN;
2348 /* Padding to ensure the flat probe is aligned. */
2349 storage_req = ALIGN_TO(storage_req, sizeof(uint64_t));
2350 storage_req += probe_storage_req;
2351 }
2352 }
2353
2354 lttng_dynamic_buffer_init(&listing);
2355 /*
2356 * We must ensure that "listing" is never resized so as to preserve
2357 * the validity of the flattened objects.
2358 */
2359 ret = lttng_dynamic_buffer_set_capacity(&listing, storage_req);
2360 if (ret) {
2361 ret = -LTTNG_ERR_NOMEM;
2362 goto end;
2363 }
2364
2365 cmd_payload_view = lttng_buffer_view_from_dynamic_buffer(
2366 &payload_copy.buffer, sizeof(*cmd_header), -1);
2367 flat_events_view = lttng_buffer_view_from_view(&cmd_payload_view, 0,
2368 nb_events * sizeof(struct lttng_event));
2369 ret = lttng_dynamic_buffer_append_view(&listing, &flat_events_view);
2370 if (ret) {
2371 ret = -LTTNG_ERR_NOMEM;
2372 goto free_dynamic_buffer;
2373 }
2374
2375 ext_view = lttng_buffer_view_from_view(&cmd_payload_view,
2376 nb_events * sizeof(struct lttng_event), -1);
2377 comm_ext_at = ext_view.data;
2378
2379 {
2380 struct lttng_payload_view payload_copy_view =
2381 lttng_payload_view_from_payload(
2382 &payload_copy, 0, -1);
2383
2384 for (i = 0; i < nb_events; i++) {
2385 struct lttng_event *event = (typeof(event))(
2386 listing.data +
2387 (sizeof(struct lttng_event) * i));
2388 const struct lttcomm_event_extended_header *ext_comm =
2389 (typeof(ext_comm)) comm_ext_at;
2390 struct lttng_event_extended *event_extended =
2391 (typeof(event_extended))(listing.data +
2392 listing.size);
2393
2394 /* Insert struct lttng_event_extended. */
2395 ret = lttng_dynamic_buffer_set_size(&listing,
2396 listing.size + sizeof(*event_extended));
2397 if (ret) {
2398 ret = -LTTNG_ERR_NOMEM;
2399 goto free_dynamic_buffer;
2400 }
2401 event->extended.ptr = event_extended;
2402
2403 comm_ext_at += sizeof(*ext_comm);
2404
2405 /* Insert filter expression. */
2406 if (ext_comm->filter_len) {
2407 event_extended->filter_expression =
2408 listing.data + listing.size;
2409 ret = lttng_dynamic_buffer_append(&listing,
2410 comm_ext_at,
2411 ext_comm->filter_len);
2412 if (ret) {
2413 ret = -LTTNG_ERR_NOMEM;
2414 goto free_dynamic_buffer;
2415 }
2416 comm_ext_at += ext_comm->filter_len;
2417 }
2418
2419 /* Insert exclusions. */
2420 if (ext_comm->nb_exclusions) {
2421 event_extended->exclusions.count =
2422 ext_comm->nb_exclusions;
2423 event_extended->exclusions.strings =
2424 listing.data + listing.size;
2425
2426 ret = lttng_dynamic_buffer_append(&listing,
2427 comm_ext_at,
2428 ext_comm->nb_exclusions *
2429 LTTNG_SYMBOL_NAME_LEN);
2430 if (ret) {
2431 ret = -LTTNG_ERR_NOMEM;
2432 goto free_dynamic_buffer;
2433 }
2434 comm_ext_at += ext_comm->nb_exclusions *
2435 LTTNG_SYMBOL_NAME_LEN;
2436 }
2437
2438 /* Insert padding to align to 64-bits. */
2439 ret = lttng_dynamic_buffer_set_size(&listing,
2440 ALIGN_TO(listing.size,
2441 sizeof(uint64_t)));
2442 if (ret) {
2443 ret = -LTTNG_ERR_NOMEM;
2444 goto free_dynamic_buffer;
2445 }
2446
2447 /* Insert flattened userspace probe location. */
2448 if (ext_comm->userspace_probe_location_len) {
2449 struct lttng_userspace_probe_location
2450 *probe_location = NULL;
2451 struct lttng_payload_view probe_location_view = lttng_payload_view_from_view(
2452 &payload_copy_view,
2453 (const char *) comm_ext_at -
2454 payload_copy_view.buffer.data,
2455 ext_comm->userspace_probe_location_len);
2456
2457 if (!lttng_payload_view_is_valid(&probe_location_view)) {
2458 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2459 goto free_dynamic_buffer;
2460 }
2461
2462 ret = lttng_userspace_probe_location_create_from_payload(
2463 &probe_location_view,
2464 &probe_location);
2465 if (ret < 0) {
2466 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2467 goto free_dynamic_buffer;
2468 }
2469
2470 event_extended->probe_location = (struct lttng_userspace_probe_location
2471 *) (listing.data +
2472 listing.size);
2473 ret = lttng_userspace_probe_location_flatten(
2474 probe_location, &listing);
2475 lttng_userspace_probe_location_destroy(
2476 probe_location);
2477 if (ret < 0) {
2478 ret = -LTTNG_ERR_PROBE_LOCATION_INVAL;
2479 goto free_dynamic_buffer;
2480 }
2481
2482 comm_ext_at += ext_comm->userspace_probe_location_len;
2483 }
2484 }
2485 }
2486
2487 /* Don't reset listing buffer as we return its content. */
2488 *events = (struct lttng_event *) listing.data;
2489 lttng_dynamic_buffer_init(&listing);
2490 ret = (int) nb_events;
2491 free_dynamic_buffer:
2492 lttng_dynamic_buffer_reset(&listing);
2493 end:
2494 lttng_payload_reset(&payload);
2495 lttng_payload_reset(&payload_copy);
2496 return ret;
2497 }
2498
2499 /*
2500 * Sets the tracing_group variable with name.
2501 * This function allocates memory pointed to by tracing_group.
2502 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
2503 */
2504 int lttng_set_tracing_group(const char *name)
2505 {
2506 if (name == NULL) {
2507 return -LTTNG_ERR_INVALID;
2508 }
2509
2510 if (asprintf(&tracing_group, "%s", name) < 0) {
2511 return -LTTNG_ERR_FATAL;
2512 }
2513
2514 return 0;
2515 }
2516
2517 int lttng_calibrate(struct lttng_handle *handle,
2518 struct lttng_calibrate *calibrate)
2519 {
2520 /*
2521 * This command was removed in LTTng 2.9.
2522 */
2523 return -LTTNG_ERR_UND;
2524 }
2525
2526 /*
2527 * Set default channel attributes.
2528 * If either or both of the arguments are null, attr content is zeroe'd.
2529 */
2530 void lttng_channel_set_default_attr(struct lttng_domain *domain,
2531 struct lttng_channel_attr *attr)
2532 {
2533 struct lttng_channel_extended *extended;
2534
2535 /* Safety check */
2536 if (attr == NULL || domain == NULL) {
2537 return;
2538 }
2539
2540 extended = (struct lttng_channel_extended *) attr->extended.ptr;
2541 memset(attr, 0, sizeof(struct lttng_channel_attr));
2542
2543 /* Same for all domains. */
2544 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
2545 attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE;
2546 attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT;
2547
2548 switch (domain->type) {
2549 case LTTNG_DOMAIN_KERNEL:
2550 attr->switch_timer_interval =
2551 DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
2552 attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
2553 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
2554 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
2555 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
2556 if (extended) {
2557 extended->monitor_timer_interval =
2558 DEFAULT_KERNEL_CHANNEL_MONITOR_TIMER;
2559 extended->blocking_timeout =
2560 DEFAULT_KERNEL_CHANNEL_BLOCKING_TIMEOUT;
2561 }
2562 break;
2563 case LTTNG_DOMAIN_UST:
2564 switch (domain->buf_type) {
2565 case LTTNG_BUFFER_PER_UID:
2566 attr->subbuf_size = default_get_ust_uid_channel_subbuf_size();
2567 attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM;
2568 attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT;
2569 attr->switch_timer_interval =
2570 DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER;
2571 attr->read_timer_interval =
2572 DEFAULT_UST_UID_CHANNEL_READ_TIMER;
2573 if (extended) {
2574 extended->monitor_timer_interval =
2575 DEFAULT_UST_UID_CHANNEL_MONITOR_TIMER;
2576 extended->blocking_timeout =
2577 DEFAULT_UST_UID_CHANNEL_BLOCKING_TIMEOUT;
2578 }
2579 break;
2580 case LTTNG_BUFFER_PER_PID:
2581 default:
2582 attr->subbuf_size = default_get_ust_pid_channel_subbuf_size();
2583 attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM;
2584 attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT;
2585 attr->switch_timer_interval =
2586 DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER;
2587 attr->read_timer_interval =
2588 DEFAULT_UST_PID_CHANNEL_READ_TIMER;
2589 if (extended) {
2590 extended->monitor_timer_interval =
2591 DEFAULT_UST_PID_CHANNEL_MONITOR_TIMER;
2592 extended->blocking_timeout =
2593 DEFAULT_UST_PID_CHANNEL_BLOCKING_TIMEOUT;
2594 }
2595 break;
2596 }
2597 default:
2598 /* Default behavior: leave set to 0. */
2599 break;
2600 }
2601
2602 attr->extended.ptr = extended;
2603 }
2604
2605 int lttng_channel_get_discarded_event_count(struct lttng_channel *channel,
2606 uint64_t *discarded_events)
2607 {
2608 int ret = 0;
2609 struct lttng_channel_extended *chan_ext;
2610
2611 if (!channel || !discarded_events) {
2612 ret = -LTTNG_ERR_INVALID;
2613 goto end;
2614 }
2615
2616 chan_ext = channel->attr.extended.ptr;
2617 if (!chan_ext) {
2618 /*
2619 * This can happen since the lttng_channel structure is
2620 * used for other tasks where this pointer is never set.
2621 */
2622 *discarded_events = 0;
2623 goto end;
2624 }
2625
2626 *discarded_events = chan_ext->discarded_events;
2627 end:
2628 return ret;
2629 }
2630
2631 int lttng_channel_get_lost_packet_count(struct lttng_channel *channel,
2632 uint64_t *lost_packets)
2633 {
2634 int ret = 0;
2635 struct lttng_channel_extended *chan_ext;
2636
2637 if (!channel || !lost_packets) {
2638 ret = -LTTNG_ERR_INVALID;
2639 goto end;
2640 }
2641
2642 chan_ext = channel->attr.extended.ptr;
2643 if (!chan_ext) {
2644 /*
2645 * This can happen since the lttng_channel structure is
2646 * used for other tasks where this pointer is never set.
2647 */
2648 *lost_packets = 0;
2649 goto end;
2650 }
2651
2652 *lost_packets = chan_ext->lost_packets;
2653 end:
2654 return ret;
2655 }
2656
2657 int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan,
2658 uint64_t *monitor_timer_interval)
2659 {
2660 int ret = 0;
2661
2662 if (!chan || !monitor_timer_interval) {
2663 ret = -LTTNG_ERR_INVALID;
2664 goto end;
2665 }
2666
2667 if (!chan->attr.extended.ptr) {
2668 ret = -LTTNG_ERR_INVALID;
2669 goto end;
2670 }
2671
2672 *monitor_timer_interval = ((struct lttng_channel_extended *)
2673 chan->attr.extended.ptr)->monitor_timer_interval;
2674 end:
2675 return ret;
2676 }
2677
2678 int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan,
2679 uint64_t monitor_timer_interval)
2680 {
2681 int ret = 0;
2682
2683 if (!chan || !chan->attr.extended.ptr) {
2684 ret = -LTTNG_ERR_INVALID;
2685 goto end;
2686 }
2687
2688 ((struct lttng_channel_extended *)
2689 chan->attr.extended.ptr)->monitor_timer_interval =
2690 monitor_timer_interval;
2691 end:
2692 return ret;
2693 }
2694
2695 int lttng_channel_get_blocking_timeout(struct lttng_channel *chan,
2696 int64_t *blocking_timeout)
2697 {
2698 int ret = 0;
2699
2700 if (!chan || !blocking_timeout) {
2701 ret = -LTTNG_ERR_INVALID;
2702 goto end;
2703 }
2704
2705 if (!chan->attr.extended.ptr) {
2706 ret = -LTTNG_ERR_INVALID;
2707 goto end;
2708 }
2709
2710 *blocking_timeout = ((struct lttng_channel_extended *)
2711 chan->attr.extended.ptr)->blocking_timeout;
2712 end:
2713 return ret;
2714 }
2715
2716 int lttng_channel_set_blocking_timeout(struct lttng_channel *chan,
2717 int64_t blocking_timeout)
2718 {
2719 int ret = 0;
2720 int64_t msec_timeout;
2721
2722 if (!chan || !chan->attr.extended.ptr) {
2723 ret = -LTTNG_ERR_INVALID;
2724 goto end;
2725 }
2726
2727 if (blocking_timeout < 0 && blocking_timeout != -1) {
2728 ret = -LTTNG_ERR_INVALID;
2729 goto end;
2730 }
2731
2732 /*
2733 * LTTng-ust's use of poll() to implement this timeout mechanism forces
2734 * us to accept a narrower range of values (msecs expressed as a signed
2735 * 32-bit integer).
2736 */
2737 msec_timeout = blocking_timeout / 1000;
2738 if (msec_timeout != (int32_t) msec_timeout) {
2739 ret = -LTTNG_ERR_INVALID;
2740 goto end;
2741 }
2742
2743 ((struct lttng_channel_extended *)
2744 chan->attr.extended.ptr)->blocking_timeout =
2745 blocking_timeout;
2746 end:
2747 return ret;
2748 }
2749
2750 /*
2751 * Check if session daemon is alive.
2752 *
2753 * Return 1 if alive or 0 if not.
2754 * On error returns a negative value.
2755 */
2756 int lttng_session_daemon_alive(void)
2757 {
2758 int ret;
2759
2760 ret = set_session_daemon_path();
2761 if (ret < 0) {
2762 /* Error. */
2763 return ret;
2764 }
2765
2766 if (*sessiond_sock_path == '\0') {
2767 /*
2768 * No socket path set. Weird error which means the constructor
2769 * was not called.
2770 */
2771 assert(0);
2772 }
2773
2774 ret = try_connect_sessiond(sessiond_sock_path);
2775 if (ret < 0) {
2776 /* Not alive. */
2777 return 0;
2778 }
2779
2780 /* Is alive. */
2781 return 1;
2782 }
2783
2784 /*
2785 * Set URL for a consumer for a session and domain.
2786 *
2787 * Return 0 on success, else a negative value.
2788 */
2789 int lttng_set_consumer_url(struct lttng_handle *handle,
2790 const char *control_url, const char *data_url)
2791 {
2792 int ret;
2793 ssize_t size;
2794 struct lttcomm_session_msg lsm;
2795 struct lttng_uri *uris = NULL;
2796
2797 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
2798 return -LTTNG_ERR_INVALID;
2799 }
2800
2801 memset(&lsm, 0, sizeof(lsm));
2802
2803 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
2804
2805 lttng_ctl_copy_string(lsm.session.name, handle->session_name,
2806 sizeof(lsm.session.name));
2807 COPY_DOMAIN_PACKED(lsm.domain, handle->domain);
2808
2809 size = uri_parse_str_urls(control_url, data_url, &uris);
2810 if (size < 0) {
2811 return -LTTNG_ERR_INVALID;
2812 }
2813
2814 lsm.u.uri.size = size;
2815
2816 ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris,
2817 sizeof(struct lttng_uri) * size, NULL);
2818
2819 free(uris);
2820 return ret;
2821 }
2822
2823 /*
2824 * [OBSOLETE]
2825 */
2826 int lttng_enable_consumer(struct lttng_handle *handle);
2827 int lttng_enable_consumer(struct lttng_handle *handle)
2828 {
2829 return -ENOSYS;
2830 }
2831
2832 /*
2833 * [OBSOLETE]
2834 */
2835 int lttng_disable_consumer(struct lttng_handle *handle);
2836 int lttng_disable_consumer(struct lttng_handle *handle)
2837 {
2838 return -ENOSYS;
2839 }
2840
2841 /*
2842 * [OBSOLETE]
2843 */
2844 int _lttng_create_session_ext(const char *name, const char *url,
2845 const char *datetime);
2846 int _lttng_create_session_ext(const char *name, const char *url,
2847 const char *datetime)
2848 {
2849 return -ENOSYS;
2850 }
2851
2852 /*
2853 * For a given session name, this call checks if the data is ready to be read
2854 * or is still being extracted by the consumer(s) hence not ready to be used by
2855 * any readers.
2856 */
2857 int lttng_data_pending(const char *session_name)
2858 {
2859 int ret;
2860 struct lttcomm_session_msg lsm;
2861 uint8_t *pending = NULL;
2862
2863 if (session_name == NULL) {
2864 return -LTTNG_ERR_INVALID;
2865 }
2866
2867 memset(&lsm, 0, sizeof(lsm));
2868 lsm.cmd_type = LTTNG_DATA_PENDING;
2869
2870 lttng_ctl_copy_string(lsm.session.name, session_name,
2871 sizeof(lsm.session.name));
2872
2873 ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending);
2874 if (ret < 0) {
2875 goto end;
2876 } else if (ret != 1) {
2877 /* Unexpected payload size */
2878 ret = -LTTNG_ERR_INVALID;
2879 goto end;
2880 } else if (!pending) {
2881 /* Internal error. */
2882 ret = -LTTNG_ERR_UNK;
2883 goto end;
2884 }
2885
2886 ret = (int) *pending;
2887 end:
2888 free(pending);
2889 return ret;
2890 }
2891
2892 /*
2893 * Regenerate the metadata for a session.
2894 * Return 0 on success, a negative error code on error.
2895 */
2896 int lttng_regenerate_metadata(const char *session_name)
2897 {
2898 int ret;
2899 struct lttcomm_session_msg lsm;
2900
2901 if (!session_name) {
2902 ret = -LTTNG_ERR_INVALID;
2903 goto end;
2904 }
2905
2906 memset(&lsm, 0, sizeof(lsm));
2907 lsm.cmd_type = LTTNG_REGENERATE_METADATA;
2908
2909 lttng_ctl_copy_string(lsm.session.name, session_name,
2910 sizeof(lsm.session.name));
2911
2912 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2913 if (ret < 0) {
2914 goto end;
2915 }
2916
2917 ret = 0;
2918 end:
2919 return ret;
2920 }
2921
2922 /*
2923 * Deprecated, replaced by lttng_regenerate_metadata.
2924 */
2925 int lttng_metadata_regenerate(const char *session_name)
2926 {
2927 return lttng_regenerate_metadata(session_name);
2928 }
2929
2930 /*
2931 * Regenerate the statedump of a session.
2932 * Return 0 on success, a negative error code on error.
2933 */
2934 int lttng_regenerate_statedump(const char *session_name)
2935 {
2936 int ret;
2937 struct lttcomm_session_msg lsm;
2938
2939 if (!session_name) {
2940 ret = -LTTNG_ERR_INVALID;
2941 goto end;
2942 }
2943
2944 memset(&lsm, 0, sizeof(lsm));
2945 lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP;
2946
2947 lttng_ctl_copy_string(lsm.session.name, session_name,
2948 sizeof(lsm.session.name));
2949
2950 ret = lttng_ctl_ask_sessiond(&lsm, NULL);
2951 if (ret < 0) {
2952 goto end;
2953 }
2954
2955 ret = 0;
2956 end:
2957 return ret;
2958 }
2959
2960 int lttng_register_trigger(struct lttng_trigger *trigger)
2961 {
2962 int ret;
2963 struct lttcomm_session_msg lsm = {
2964 .cmd_type = LTTNG_REGISTER_TRIGGER,
2965 };
2966 struct lttcomm_session_msg *message_lsm;
2967 struct lttng_payload message;
2968 struct lttng_payload reply;
2969 struct lttng_trigger *reply_trigger = NULL;
2970 const struct lttng_credentials user_creds = {
2971 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
2972 .gid = LTTNG_OPTIONAL_INIT_UNSET,
2973 };
2974
2975
2976 lttng_payload_init(&message);
2977 lttng_payload_init(&reply);
2978
2979 if (!trigger) {
2980 ret = -LTTNG_ERR_INVALID;
2981 goto end;
2982 }
2983
2984 if (!trigger->creds.uid.is_set) {
2985 /* Use the client's credentials as the trigger credentials. */
2986 lttng_trigger_set_credentials(trigger, &user_creds);
2987 } else {
2988 /*
2989 * Validate that either the current trigger credentials and the
2990 * client credentials are identical or that the current user is
2991 * root. The root user can register, unregister triggers for
2992 * himself and other users.
2993 *
2994 * This check is also present on the sessiond side, using the
2995 * credentials passed on the socket. These check are all
2996 * "safety" checks.
2997 */
2998 const struct lttng_credentials *trigger_creds =
2999 lttng_trigger_get_credentials(trigger);
3000
3001 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3002 if (lttng_credentials_get_uid(&user_creds) != 0) {
3003 ret = -LTTNG_ERR_EPERM;
3004 goto end;
3005 }
3006 }
3007 }
3008
3009 if (!lttng_trigger_validate(trigger)) {
3010 ret = -LTTNG_ERR_INVALID_TRIGGER;
3011 goto end;
3012 }
3013
3014 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3015 if (ret) {
3016 ret = -LTTNG_ERR_NOMEM;
3017 goto end;
3018 }
3019
3020 /*
3021 * This is needed to populate the trigger object size for the command
3022 * header.
3023 */
3024 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3025
3026 ret = lttng_trigger_serialize(trigger, &message);
3027 if (ret < 0) {
3028 ret = -LTTNG_ERR_UNK;
3029 goto end;
3030 }
3031
3032 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3033
3034 {
3035 struct lttng_payload_view message_view =
3036 lttng_payload_view_from_payload(
3037 &message, 0, -1);
3038
3039 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3040 &message_view);
3041 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3042 if (ret < 0) {
3043 goto end;
3044 }
3045 }
3046
3047 {
3048 struct lttng_payload_view reply_view =
3049 lttng_payload_view_from_payload(
3050 &reply, 0, reply.buffer.size);
3051
3052 ret = lttng_trigger_create_from_payload(
3053 &reply_view, &reply_trigger);
3054 if (ret < 0) {
3055 ret = -LTTNG_ERR_FATAL;
3056 goto end;
3057 }
3058 }
3059
3060 ret = lttng_trigger_assign_name(trigger, reply_trigger);
3061 if (ret < 0) {
3062 ret = -LTTNG_ERR_FATAL;
3063 goto end;
3064 }
3065
3066 ret = 0;
3067 end:
3068 lttng_payload_reset(&message);
3069 lttng_payload_reset(&reply);
3070 lttng_trigger_destroy(reply_trigger);
3071 return ret;
3072 }
3073
3074 int lttng_unregister_trigger(struct lttng_trigger *trigger)
3075 {
3076 int ret;
3077 struct lttcomm_session_msg lsm;
3078 struct lttcomm_session_msg *message_lsm;
3079 struct lttng_payload message;
3080 struct lttng_payload reply;
3081 const struct lttng_credentials user_creds = {
3082 .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()),
3083 .gid = LTTNG_OPTIONAL_INIT_UNSET,
3084 };
3085
3086 lttng_payload_init(&message);
3087 lttng_payload_init(&reply);
3088
3089 if (!trigger) {
3090 ret = -LTTNG_ERR_INVALID;
3091 goto end;
3092 }
3093
3094 if (!trigger->creds.uid.is_set) {
3095 /* Use the client's credentials as the trigger credentials. */
3096 lttng_trigger_set_credentials(trigger, &user_creds);
3097 } else {
3098 /*
3099 * Validate that either the current trigger credentials and the
3100 * client credentials are identical or that the current user is
3101 * root. The root user can register, unregister triggers for
3102 * himself and other users.
3103 *
3104 * This check is also present on the sessiond side, using the
3105 * credentials passed on the socket. These check are all
3106 * "safety" checks.
3107 */
3108 const struct lttng_credentials *trigger_creds =
3109 lttng_trigger_get_credentials(trigger);
3110
3111 if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) {
3112 if (lttng_credentials_get_uid(&user_creds) != 0) {
3113 ret = -LTTNG_ERR_EPERM;
3114 goto end;
3115 }
3116 }
3117 }
3118
3119 if (!lttng_trigger_validate(trigger)) {
3120 ret = -LTTNG_ERR_INVALID_TRIGGER;
3121 goto end;
3122 }
3123
3124 memset(&lsm, 0, sizeof(lsm));
3125 lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER;
3126
3127 ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm));
3128 if (ret) {
3129 ret = -LTTNG_ERR_NOMEM;
3130 goto end;
3131 }
3132
3133 /*
3134 * This is needed to populate the trigger object size for the command
3135 * header and number of fds sent.
3136 */
3137 message_lsm = (struct lttcomm_session_msg *) message.buffer.data;
3138
3139 ret = lttng_trigger_serialize(trigger, &message);
3140 if (ret < 0) {
3141 ret = -LTTNG_ERR_UNK;
3142 goto end;
3143 }
3144
3145 message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm);
3146
3147 {
3148 struct lttng_payload_view message_view =
3149 lttng_payload_view_from_payload(
3150 &message, 0, -1);
3151
3152 /*
3153 * Update the message header with the number of fd that will be
3154 * sent.
3155 */
3156 message_lsm->fd_count = lttng_payload_view_get_fd_handle_count(
3157 &message_view);
3158
3159 ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply);
3160 if (ret < 0) {
3161 goto end;
3162 }
3163 }
3164
3165 ret = 0;
3166 end:
3167 lttng_payload_reset(&message);
3168 lttng_payload_reset(&reply);
3169 return ret;
3170 }
3171
3172 /*
3173 * lib constructor.
3174 */
3175 static void __attribute__((constructor)) init(void)
3176 {
3177 /* Set default session group */
3178 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
3179 }
3180
3181 /*
3182 * lib destructor.
3183 */
3184 static void __attribute__((destructor)) lttng_ctl_exit(void)
3185 {
3186 free(tracing_group);
3187 }
This page took 0.091481 seconds and 5 git commands to generate.