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