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