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 JG |
917 | int ret; |
918 | size_t len = 0; | |
919 | char *buf = NULL; | |
cd80958d DG |
920 | struct lttcomm_session_msg lsm; |
921 | ||
9ae110e2 | 922 | /* Safety check. Both are mandatory. */ |
9d697d3d | 923 | if (handle == NULL || ctx == NULL) { |
2001793c JG |
924 | ret = -LTTNG_ERR_INVALID; |
925 | goto end; | |
cd80958d DG |
926 | } |
927 | ||
441c16a7 | 928 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d DG |
929 | lsm.cmd_type = LTTNG_ADD_CONTEXT; |
930 | ||
85076754 | 931 | /* If no channel name, send empty string. */ |
e1b624d0 JG |
932 | ret = lttng_strncpy(lsm.u.context.channel_name, channel_name ?: "", |
933 | sizeof(lsm.u.context.channel_name)); | |
934 | if (ret) { | |
935 | ret = -LTTNG_ERR_INVALID; | |
936 | goto end; | |
85076754 | 937 | } |
cd80958d | 938 | |
60160d2a | 939 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
e1b624d0 | 940 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
cd80958d | 941 | sizeof(lsm.session.name)); |
e1b624d0 JG |
942 | if (ret) { |
943 | ret = -LTTNG_ERR_INVALID; | |
944 | goto end; | |
945 | } | |
cd80958d | 946 | |
2001793c JG |
947 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
948 | size_t provider_len, ctx_len; | |
949 | const char *provider_name = ctx->u.app_ctx.provider_name; | |
950 | const char *ctx_name = ctx->u.app_ctx.ctx_name; | |
951 | ||
952 | if (!provider_name || !ctx_name) { | |
953 | ret = -LTTNG_ERR_INVALID; | |
954 | goto end; | |
955 | } | |
956 | ||
957 | provider_len = strlen(provider_name); | |
958 | if (provider_len == 0) { | |
959 | ret = -LTTNG_ERR_INVALID; | |
960 | goto end; | |
961 | } | |
962 | lsm.u.context.provider_name_len = provider_len; | |
963 | ||
964 | ctx_len = strlen(ctx_name); | |
965 | if (ctx_len == 0) { | |
966 | ret = -LTTNG_ERR_INVALID; | |
967 | goto end; | |
968 | } | |
969 | lsm.u.context.context_name_len = ctx_len; | |
970 | ||
971 | len = provider_len + ctx_len; | |
4bd69c5f | 972 | buf = (char *) zmalloc(len); |
2001793c JG |
973 | if (!buf) { |
974 | ret = -LTTNG_ERR_NOMEM; | |
975 | goto end; | |
976 | } | |
977 | ||
978 | memcpy(buf, provider_name, provider_len); | |
979 | memcpy(buf + provider_len, ctx_name, ctx_len); | |
980 | } | |
981 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); | |
46f44e2a JR |
982 | |
983 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { | |
984 | /* | |
985 | * Don't leak application addresses to the sessiond. | |
986 | * This is only necessary when ctx is for an app ctx otherwise | |
987 | * the values inside the union (type & config) are overwritten. | |
988 | */ | |
989 | lsm.u.context.ctx.u.app_ctx.provider_name = NULL; | |
990 | lsm.u.context.ctx.u.app_ctx.ctx_name = NULL; | |
991 | } | |
2001793c | 992 | |
795a978d | 993 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, buf, len, NULL); |
2001793c JG |
994 | end: |
995 | free(buf); | |
996 | return ret; | |
d65106b1 DG |
997 | } |
998 | ||
f3ed775e | 999 | /* |
9ae110e2 JG |
1000 | * Enable event(s) for a channel. |
1001 | * | |
1002 | * If no event name is specified, all events are enabled. | |
1003 | * If no channel name is specified, the default 'channel0' is used. | |
1004 | * | |
1005 | * Returns size of returned session payload data or a negative error code. | |
f3ed775e | 1006 | */ |
cd80958d | 1007 | int lttng_enable_event(struct lttng_handle *handle, |
38057ed1 | 1008 | struct lttng_event *ev, const char *channel_name) |
1df4dedd | 1009 | { |
f8a96544 JI |
1010 | return lttng_enable_event_with_exclusions(handle, ev, channel_name, |
1011 | NULL, 0, NULL); | |
1df4dedd DG |
1012 | } |
1013 | ||
53a80697 | 1014 | /* |
025faf73 | 1015 | * Create or enable an event with a filter expression. |
178191b3 | 1016 | * |
53a80697 MD |
1017 | * Return negative error value on error. |
1018 | * Return size of returned session payload data if OK. | |
1019 | */ | |
025faf73 | 1020 | int lttng_enable_event_with_filter(struct lttng_handle *handle, |
178191b3 | 1021 | struct lttng_event *event, const char *channel_name, |
53a80697 MD |
1022 | const char *filter_expression) |
1023 | { | |
f8a96544 JI |
1024 | return lttng_enable_event_with_exclusions(handle, event, channel_name, |
1025 | filter_expression, 0, NULL); | |
53a80697 MD |
1026 | } |
1027 | ||
347c5ab5 | 1028 | /* |
0e115563 | 1029 | * Depending on the event, return a newly allocated agent filter expression or |
347c5ab5 DG |
1030 | * NULL if not applicable. |
1031 | * | |
1032 | * An event with NO loglevel and the name is * will return NULL. | |
1033 | */ | |
0e115563 | 1034 | static char *set_agent_filter(const char *filter, struct lttng_event *ev) |
347c5ab5 DG |
1035 | { |
1036 | int err; | |
0e115563 | 1037 | char *agent_filter = NULL; |
347c5ab5 | 1038 | |
a0377dfe | 1039 | LTTNG_ASSERT(ev); |
347c5ab5 DG |
1040 | |
1041 | /* Don't add filter for the '*' event. */ | |
9f449915 | 1042 | if (strcmp(ev->name, "*") != 0) { |
347c5ab5 | 1043 | if (filter) { |
0e115563 | 1044 | err = asprintf(&agent_filter, "(%s) && (logger_name == \"%s\")", filter, |
347c5ab5 DG |
1045 | ev->name); |
1046 | } else { | |
0e115563 | 1047 | err = asprintf(&agent_filter, "logger_name == \"%s\"", ev->name); |
347c5ab5 DG |
1048 | } |
1049 | if (err < 0) { | |
1050 | PERROR("asprintf"); | |
6a556f7b | 1051 | goto error; |
347c5ab5 DG |
1052 | } |
1053 | } | |
1054 | ||
1055 | /* Add loglevel filtering if any for the JUL domain. */ | |
1056 | if (ev->loglevel_type != LTTNG_EVENT_LOGLEVEL_ALL) { | |
b53d4e59 | 1057 | const char *op; |
347c5ab5 DG |
1058 | |
1059 | if (ev->loglevel_type == LTTNG_EVENT_LOGLEVEL_RANGE) { | |
1060 | op = ">="; | |
1061 | } else { | |
1062 | op = "=="; | |
1063 | } | |
1064 | ||
0e115563 | 1065 | if (filter || agent_filter) { |
6a556f7b JG |
1066 | char *new_filter; |
1067 | ||
fb0edb23 | 1068 | err = asprintf(&new_filter, "(%s) && (int_loglevel %s %d)", |
0e115563 | 1069 | agent_filter ? agent_filter : filter, op, |
347c5ab5 | 1070 | ev->loglevel); |
0e115563 DG |
1071 | if (agent_filter) { |
1072 | free(agent_filter); | |
6a556f7b | 1073 | } |
0e115563 | 1074 | agent_filter = new_filter; |
347c5ab5 | 1075 | } else { |
0e115563 | 1076 | err = asprintf(&agent_filter, "int_loglevel %s %d", op, |
347c5ab5 DG |
1077 | ev->loglevel); |
1078 | } | |
1079 | if (err < 0) { | |
1080 | PERROR("asprintf"); | |
6a556f7b | 1081 | goto error; |
347c5ab5 DG |
1082 | } |
1083 | } | |
1084 | ||
0e115563 | 1085 | return agent_filter; |
6a556f7b | 1086 | error: |
0e115563 | 1087 | free(agent_filter); |
6a556f7b | 1088 | return NULL; |
347c5ab5 DG |
1089 | } |
1090 | ||
93deb080 JI |
1091 | /* |
1092 | * Enable event(s) for a channel, possibly with exclusions and a filter. | |
1093 | * If no event name is specified, all events are enabled. | |
1094 | * If no channel name is specified, the default name is used. | |
1095 | * If filter expression is not NULL, the filter is set for the event. | |
1096 | * If exclusion count is not zero, the exclusions are set for the event. | |
1097 | * Returns size of returned session payload data or a negative error code. | |
1098 | */ | |
1099 | int lttng_enable_event_with_exclusions(struct lttng_handle *handle, | |
1100 | struct lttng_event *ev, const char *channel_name, | |
e9efbcd3 | 1101 | const char *original_filter_expression, |
93deb080 JI |
1102 | int exclusion_count, char **exclusion_list) |
1103 | { | |
8ddd72ef | 1104 | struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_ENABLE_EVENT }; |
e368fb43 | 1105 | struct lttng_payload payload; |
8ddd72ef | 1106 | int ret = 0; |
137b9942 | 1107 | unsigned int free_filter_expression = 0; |
93deb080 | 1108 | struct filter_parser_ctx *ctx = NULL; |
8ddd72ef | 1109 | size_t bytecode_len = 0; |
ec005ec6 | 1110 | |
6a0838b7 YL |
1111 | /* |
1112 | * We have either a filter or some exclusions, so we need to set up | |
e368fb43 | 1113 | * a variable-length payload from where to send the data. |
6a0838b7 | 1114 | */ |
e368fb43 | 1115 | lttng_payload_init(&payload); |
ec005ec6 | 1116 | |
e9efbcd3 JG |
1117 | /* |
1118 | * Cast as non-const since we may replace the filter expression | |
1119 | * by a dynamically allocated string. Otherwise, the original | |
1120 | * string is not modified. | |
1121 | */ | |
1122 | char *filter_expression = (char *) original_filter_expression; | |
93deb080 JI |
1123 | |
1124 | if (handle == NULL || ev == NULL) { | |
137b9942 DG |
1125 | ret = -LTTNG_ERR_INVALID; |
1126 | goto error; | |
93deb080 JI |
1127 | } |
1128 | ||
9ae110e2 JG |
1129 | /* |
1130 | * Empty filter string will always be rejected by the parser | |
93deb080 JI |
1131 | * anyway, so treat this corner-case early to eliminate |
1132 | * lttng_fmemopen error for 0-byte allocation. | |
1133 | */ | |
1134 | if (filter_expression && filter_expression[0] == '\0') { | |
137b9942 DG |
1135 | ret = -LTTNG_ERR_INVALID; |
1136 | goto error; | |
93deb080 JI |
1137 | } |
1138 | ||
18a720cd | 1139 | if (ev->name[0] == '\0') { |
e1b624d0 JG |
1140 | /* Enable all events. */ |
1141 | ret = lttng_strncpy(ev->name, "*", sizeof(ev->name)); | |
a0377dfe | 1142 | LTTNG_ASSERT(ret == 0); |
6565421f | 1143 | } |
93deb080 | 1144 | |
9ae110e2 | 1145 | /* Parse filter expression. */ |
5cdb6027 | 1146 | if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL |
0e115563 DG |
1147 | || handle->domain.type == LTTNG_DOMAIN_LOG4J |
1148 | || handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
5cdb6027 | 1149 | if (handle->domain.type == LTTNG_DOMAIN_JUL || |
0e115563 DG |
1150 | handle->domain.type == LTTNG_DOMAIN_LOG4J || |
1151 | handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
1152 | char *agent_filter; | |
64226865 | 1153 | |
8ddd72ef | 1154 | /* Setup agent filter if needed. */ |
0e115563 DG |
1155 | agent_filter = set_agent_filter(filter_expression, ev); |
1156 | if (!agent_filter) { | |
137b9942 | 1157 | if (!filter_expression) { |
9ae110e2 JG |
1158 | /* |
1159 | * No JUL and no filter, just skip | |
1160 | * everything below. | |
1161 | */ | |
8ddd72ef | 1162 | goto serialize; |
137b9942 | 1163 | } |
64226865 DG |
1164 | } else { |
1165 | /* | |
9ae110e2 JG |
1166 | * With an agent filter, the original filter has |
1167 | * been added to it thus replace the filter | |
1168 | * expression. | |
64226865 | 1169 | */ |
0e115563 | 1170 | filter_expression = agent_filter; |
e9efbcd3 | 1171 | free_filter_expression = 1; |
9b21e6d5 | 1172 | } |
9b21e6d5 | 1173 | } |
93deb080 | 1174 | |
8ddd72ef JR |
1175 | if (strnlen(filter_expression, LTTNG_FILTER_MAX_LEN) == |
1176 | LTTNG_FILTER_MAX_LEN) { | |
1177 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1178 | goto error; | |
1179 | } | |
1180 | ||
e4d2f27a | 1181 | ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx); |
93deb080 | 1182 | if (ret) { |
8ddd72ef | 1183 | goto error; |
93deb080 | 1184 | } |
e4d2f27a | 1185 | |
8ddd72ef JR |
1186 | bytecode_len = bytecode_get_len(&ctx->bytecode->b) + |
1187 | sizeof(ctx->bytecode->b); | |
1188 | if (bytecode_len > LTTNG_FILTER_MAX_LEN) { | |
1189 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1190 | goto error; | |
1191 | } | |
6b453b5e JG |
1192 | } |
1193 | ||
8ddd72ef JR |
1194 | serialize: |
1195 | ret = lttng_event_serialize(ev, exclusion_count, exclusion_list, | |
1196 | filter_expression, bytecode_len, | |
1197 | (ctx && bytecode_len) ? &ctx->bytecode->b : NULL, | |
1198 | &payload); | |
15e37663 | 1199 | if (ret) { |
8ddd72ef JR |
1200 | ret = -LTTNG_ERR_INVALID; |
1201 | goto error; | |
6b453b5e | 1202 | } |
137b9942 | 1203 | |
8ddd72ef JR |
1204 | /* If no channel name, send empty string. */ |
1205 | ret = lttng_strncpy(lsm.u.enable.channel_name, channel_name ?: "", | |
1206 | sizeof(lsm.u.enable.channel_name)); | |
1207 | if (ret) { | |
1208 | ret = -LTTNG_ERR_INVALID; | |
1209 | goto error; | |
6b453b5e | 1210 | } |
15e37663 | 1211 | |
8ddd72ef JR |
1212 | /* Domain */ |
1213 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); | |
15e37663 | 1214 | |
8ddd72ef JR |
1215 | /* Session name */ |
1216 | ret = lttng_strncpy(lsm.session.name, handle->session_name, | |
1217 | sizeof(lsm.session.name)); | |
1218 | if (ret) { | |
1219 | ret = -LTTNG_ERR_INVALID; | |
1220 | goto error; | |
93deb080 JI |
1221 | } |
1222 | ||
8ddd72ef JR |
1223 | /* Length of the serialized event. */ |
1224 | lsm.u.enable.length = (uint32_t) payload.buffer.size; | |
1225 | ||
e368fb43 JG |
1226 | { |
1227 | struct lttng_payload_view view = lttng_payload_view_from_payload( | |
1228 | &payload, 0, -1); | |
fe489250 | 1229 | int fd_count = lttng_payload_view_get_fd_handle_count(&view); |
e368fb43 JG |
1230 | int fd_to_send; |
1231 | ||
1232 | if (fd_count < 0) { | |
8ddd72ef | 1233 | goto error; |
e368fb43 JG |
1234 | } |
1235 | ||
a0377dfe | 1236 | LTTNG_ASSERT(fd_count == 0 || fd_count == 1); |
e368fb43 | 1237 | if (fd_count == 1) { |
e2fc8e9d | 1238 | struct fd_handle *h = |
fe489250 JG |
1239 | lttng_payload_view_pop_fd_handle(&view); |
1240 | ||
e2fc8e9d | 1241 | if (!h) { |
8ddd72ef | 1242 | goto error; |
e368fb43 JG |
1243 | } |
1244 | ||
e2fc8e9d SM |
1245 | fd_to_send = fd_handle_get_fd(h); |
1246 | fd_handle_put(h); | |
e368fb43 JG |
1247 | } |
1248 | ||
8ddd72ef JR |
1249 | lsm.fd_count = fd_count; |
1250 | ||
e368fb43 JG |
1251 | ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, |
1252 | fd_count ? &fd_to_send : NULL, fd_count, | |
1253 | view.buffer.size ? view.buffer.data : NULL, | |
1254 | view.buffer.size, NULL, NULL, 0); | |
1255 | } | |
93deb080 | 1256 | |
8ddd72ef | 1257 | error: |
7ca1dc6f | 1258 | if (filter_expression && ctx) { |
93deb080 JI |
1259 | filter_bytecode_free(ctx); |
1260 | filter_ir_free(ctx); | |
1261 | filter_parser_ctx_free(ctx); | |
7ca1dc6f | 1262 | } |
7ca1dc6f DG |
1263 | if (free_filter_expression) { |
1264 | /* | |
9ae110e2 JG |
1265 | * The filter expression has been replaced and must be freed as |
1266 | * it is not the original filter expression received as a | |
1267 | * parameter. | |
7ca1dc6f DG |
1268 | */ |
1269 | free(filter_expression); | |
93deb080 | 1270 | } |
137b9942 | 1271 | /* |
9ae110e2 JG |
1272 | * Return directly to the caller and don't ask the sessiond since |
1273 | * something went wrong in the parsing of data above. | |
137b9942 | 1274 | */ |
e368fb43 | 1275 | lttng_payload_reset(&payload); |
93deb080 JI |
1276 | return ret; |
1277 | } | |
1278 | ||
6e911cad MD |
1279 | int lttng_disable_event_ext(struct lttng_handle *handle, |
1280 | struct lttng_event *ev, const char *channel_name, | |
1281 | const char *original_filter_expression) | |
1df4dedd | 1282 | { |
8ddd72ef JR |
1283 | struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_DISABLE_EVENT }; |
1284 | struct lttng_payload payload; | |
6e911cad MD |
1285 | int ret = 0; |
1286 | unsigned int free_filter_expression = 0; | |
1287 | struct filter_parser_ctx *ctx = NULL; | |
8ddd72ef JR |
1288 | size_t bytecode_len = 0; |
1289 | ||
1290 | /* | |
1291 | * We have either a filter or some exclusions, so we need to set up | |
1292 | * a variable-length payload from where to send the data. | |
1293 | */ | |
1294 | lttng_payload_init(&payload); | |
1295 | ||
6e911cad MD |
1296 | /* |
1297 | * Cast as non-const since we may replace the filter expression | |
1298 | * by a dynamically allocated string. Otherwise, the original | |
1299 | * string is not modified. | |
1300 | */ | |
1301 | char *filter_expression = (char *) original_filter_expression; | |
1df4dedd | 1302 | |
6e911cad MD |
1303 | if (handle == NULL || ev == NULL) { |
1304 | ret = -LTTNG_ERR_INVALID; | |
1305 | goto error; | |
1306 | } | |
1307 | ||
9ae110e2 JG |
1308 | /* |
1309 | * Empty filter string will always be rejected by the parser | |
6e911cad MD |
1310 | * anyway, so treat this corner-case early to eliminate |
1311 | * lttng_fmemopen error for 0-byte allocation. | |
1312 | */ | |
1313 | if (filter_expression && filter_expression[0] == '\0') { | |
1314 | ret = -LTTNG_ERR_INVALID; | |
1315 | goto error; | |
cd80958d DG |
1316 | } |
1317 | ||
8ddd72ef | 1318 | /* Parse filter expression. */ |
6e911cad | 1319 | if (filter_expression != NULL || handle->domain.type == LTTNG_DOMAIN_JUL |
0e115563 DG |
1320 | || handle->domain.type == LTTNG_DOMAIN_LOG4J |
1321 | || handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
6e911cad | 1322 | if (handle->domain.type == LTTNG_DOMAIN_JUL || |
0e115563 DG |
1323 | handle->domain.type == LTTNG_DOMAIN_LOG4J || |
1324 | handle->domain.type == LTTNG_DOMAIN_PYTHON) { | |
1325 | char *agent_filter; | |
6e911cad | 1326 | |
8ddd72ef | 1327 | /* Setup agent filter if needed. */ |
0e115563 DG |
1328 | agent_filter = set_agent_filter(filter_expression, ev); |
1329 | if (!agent_filter) { | |
6e911cad | 1330 | if (!filter_expression) { |
9ae110e2 JG |
1331 | /* |
1332 | * No JUL and no filter, just skip | |
1333 | * everything below. | |
1334 | */ | |
8ddd72ef | 1335 | goto serialize; |
6e911cad MD |
1336 | } |
1337 | } else { | |
1338 | /* | |
8ddd72ef | 1339 | * With an agent filter, the original filter has |
9ae110e2 JG |
1340 | * been added to it thus replace the filter |
1341 | * expression. | |
6e911cad | 1342 | */ |
0e115563 | 1343 | filter_expression = agent_filter; |
6e911cad MD |
1344 | free_filter_expression = 1; |
1345 | } | |
1346 | } | |
1347 | ||
8ddd72ef JR |
1348 | if (strnlen(filter_expression, LTTNG_FILTER_MAX_LEN) == |
1349 | LTTNG_FILTER_MAX_LEN) { | |
1350 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1351 | goto error; | |
1352 | } | |
1353 | ||
e4d2f27a | 1354 | ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx); |
6e911cad | 1355 | if (ret) { |
8ddd72ef | 1356 | goto error; |
6e911cad | 1357 | } |
e4d2f27a | 1358 | |
8ddd72ef JR |
1359 | bytecode_len = bytecode_get_len(&ctx->bytecode->b) + |
1360 | sizeof(ctx->bytecode->b); | |
1361 | if (bytecode_len > LTTNG_FILTER_MAX_LEN) { | |
1362 | ret = -LTTNG_ERR_FILTER_INVAL; | |
1363 | goto error; | |
1364 | } | |
6e911cad MD |
1365 | } |
1366 | ||
8ddd72ef JR |
1367 | serialize: |
1368 | ret = lttng_event_serialize(ev, 0, NULL, | |
1369 | filter_expression, bytecode_len, | |
1370 | (ctx && bytecode_len) ? &ctx->bytecode->b : NULL, | |
1371 | &payload); | |
1372 | if (ret) { | |
1373 | ret = -LTTNG_ERR_INVALID; | |
1374 | goto error; | |
6e911cad MD |
1375 | } |
1376 | ||
8ddd72ef JR |
1377 | /* If no channel name, send empty string. */ |
1378 | ret = lttng_strncpy(lsm.u.disable.channel_name, channel_name ?: "", | |
1379 | sizeof(lsm.u.disable.channel_name)); | |
1380 | if (ret) { | |
1381 | ret = -LTTNG_ERR_INVALID; | |
1382 | goto error; | |
6e911cad | 1383 | } |
8ddd72ef JR |
1384 | |
1385 | /* Domain */ | |
1386 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); | |
1387 | ||
1388 | /* Session name */ | |
1389 | ret = lttng_strncpy(lsm.session.name, handle->session_name, | |
1390 | sizeof(lsm.session.name)); | |
1391 | if (ret) { | |
1392 | ret = -LTTNG_ERR_INVALID; | |
1393 | goto error; | |
6e911cad MD |
1394 | } |
1395 | ||
8ddd72ef JR |
1396 | /* Length of the serialized event. */ |
1397 | lsm.u.disable.length = (uint32_t) payload.buffer.size; | |
1398 | ||
1399 | { | |
1400 | struct lttng_payload_view view = lttng_payload_view_from_payload( | |
1401 | &payload, 0, -1); | |
1402 | int fd_count = lttng_payload_view_get_fd_handle_count(&view); | |
1403 | int fd_to_send; | |
1404 | ||
1405 | if (fd_count < 0) { | |
1406 | goto error; | |
1407 | } | |
1408 | ||
1409 | LTTNG_ASSERT(fd_count == 0 || fd_count == 1); | |
1410 | if (fd_count == 1) { | |
1411 | struct fd_handle *h = | |
1412 | lttng_payload_view_pop_fd_handle(&view); | |
6e911cad | 1413 | |
8ddd72ef JR |
1414 | if (!h) { |
1415 | goto error; | |
1416 | } | |
1417 | ||
1418 | fd_to_send = fd_handle_get_fd(h); | |
1419 | fd_handle_put(h); | |
1420 | } | |
1421 | ||
1422 | ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, | |
1423 | fd_count ? &fd_to_send : NULL, fd_count, | |
1424 | view.buffer.size ? view.buffer.data : NULL, | |
1425 | view.buffer.size, NULL, NULL, 0); | |
1426 | } | |
1427 | ||
1428 | error: | |
6e911cad MD |
1429 | if (filter_expression && ctx) { |
1430 | filter_bytecode_free(ctx); | |
1431 | filter_ir_free(ctx); | |
1432 | filter_parser_ctx_free(ctx); | |
1433 | } | |
6e911cad MD |
1434 | if (free_filter_expression) { |
1435 | /* | |
9ae110e2 JG |
1436 | * The filter expression has been replaced and must be freed as |
1437 | * it is not the original filter expression received as a | |
1438 | * parameter. | |
6e911cad MD |
1439 | */ |
1440 | free(filter_expression); | |
1441 | } | |
6e911cad | 1442 | /* |
9ae110e2 JG |
1443 | * Return directly to the caller and don't ask the sessiond since |
1444 | * something went wrong in the parsing of data above. | |
6e911cad | 1445 | */ |
8ddd72ef | 1446 | lttng_payload_reset(&payload); |
6e911cad MD |
1447 | return ret; |
1448 | } | |
1449 | ||
1450 | /* | |
9ae110e2 JG |
1451 | * Disable event(s) of a channel and domain. |
1452 | * If no event name is specified, all events are disabled. | |
1453 | * If no channel name is specified, the default 'channel0' is used. | |
1454 | * Returns size of returned session payload data or a negative error code. | |
6e911cad MD |
1455 | */ |
1456 | int lttng_disable_event(struct lttng_handle *handle, const char *name, | |
1457 | const char *channel_name) | |
1458 | { | |
e1b624d0 | 1459 | int ret; |
6e911cad MD |
1460 | struct lttng_event ev; |
1461 | ||
1462 | memset(&ev, 0, sizeof(ev)); | |
9b7431cf | 1463 | ev.loglevel = -1; |
6e911cad | 1464 | ev.type = LTTNG_EVENT_ALL; |
e1b624d0 JG |
1465 | ret = lttng_strncpy(ev.name, name ?: "", sizeof(ev.name)); |
1466 | if (ret) { | |
1467 | ret = -LTTNG_ERR_INVALID; | |
1468 | goto end; | |
1469 | } | |
1470 | ||
1471 | ret = lttng_disable_event_ext(handle, &ev, channel_name, NULL); | |
1472 | end: | |
1473 | return ret; | |
1df4dedd DG |
1474 | } |
1475 | ||
cf0bcb51 JG |
1476 | struct lttng_channel *lttng_channel_create(struct lttng_domain *domain) |
1477 | { | |
1478 | struct lttng_channel *channel = NULL; | |
cf0bcb51 JG |
1479 | |
1480 | if (!domain) { | |
999af9c1 | 1481 | goto end; |
cf0bcb51 JG |
1482 | } |
1483 | ||
1484 | /* Validate domain. */ | |
1485 | switch (domain->type) { | |
1486 | case LTTNG_DOMAIN_UST: | |
1487 | switch (domain->buf_type) { | |
1488 | case LTTNG_BUFFER_PER_UID: | |
1489 | case LTTNG_BUFFER_PER_PID: | |
1490 | break; | |
1491 | default: | |
999af9c1 | 1492 | goto end; |
cf0bcb51 JG |
1493 | } |
1494 | break; | |
1495 | case LTTNG_DOMAIN_KERNEL: | |
1496 | if (domain->buf_type != LTTNG_BUFFER_GLOBAL) { | |
999af9c1 | 1497 | goto end; |
cf0bcb51 JG |
1498 | } |
1499 | break; | |
1500 | default: | |
999af9c1 | 1501 | goto end; |
cf0bcb51 JG |
1502 | } |
1503 | ||
999af9c1 | 1504 | channel = lttng_channel_create_internal(); |
cf0bcb51 | 1505 | if (!channel) { |
999af9c1 | 1506 | goto end; |
cf0bcb51 JG |
1507 | } |
1508 | ||
cf0bcb51 | 1509 | lttng_channel_set_default_attr(domain, &channel->attr); |
999af9c1 | 1510 | end: |
cf0bcb51 | 1511 | return channel; |
cf0bcb51 JG |
1512 | } |
1513 | ||
1514 | void lttng_channel_destroy(struct lttng_channel *channel) | |
1515 | { | |
1516 | if (!channel) { | |
1517 | return; | |
1518 | } | |
1519 | ||
1520 | if (channel->attr.extended.ptr) { | |
1521 | free(channel->attr.extended.ptr); | |
1522 | } | |
1523 | free(channel); | |
1524 | } | |
1525 | ||
1df4dedd | 1526 | /* |
9ae110e2 JG |
1527 | * Enable channel per domain |
1528 | * Returns size of returned session payload data or a negative error code. | |
a5c5a2bd | 1529 | */ |
cd80958d | 1530 | int lttng_enable_channel(struct lttng_handle *handle, |
cf0bcb51 | 1531 | struct lttng_channel *in_chan) |
a5c5a2bd | 1532 | { |
13dd7782 | 1533 | enum lttng_error_code ret_code; |
e1b624d0 | 1534 | int ret; |
999af9c1 | 1535 | struct lttng_dynamic_buffer buffer; |
cd80958d | 1536 | struct lttcomm_session_msg lsm; |
13dd7782 | 1537 | uint64_t total_buffer_size_needed_per_cpu = 0; |
999af9c1 JR |
1538 | struct lttng_channel *channel = NULL; |
1539 | ||
1540 | lttng_dynamic_buffer_init(&buffer); | |
cd80958d | 1541 | |
9ae110e2 | 1542 | /* NULL arguments are forbidden. No default values. */ |
cf0bcb51 | 1543 | if (handle == NULL || in_chan == NULL) { |
999af9c1 JR |
1544 | ret = -LTTNG_ERR_INVALID; |
1545 | goto end; | |
cf0bcb51 | 1546 | } |
cd80958d | 1547 | |
09b72f7a FD |
1548 | /* |
1549 | * Verify that the amount of memory required to create the requested | |
1550 | * buffer is available on the system at the moment. | |
1551 | */ | |
999af9c1 JR |
1552 | if (in_chan->attr.num_subbuf > |
1553 | UINT64_MAX / in_chan->attr.subbuf_size) { | |
13dd7782 JR |
1554 | /* Overflow */ |
1555 | ret = -LTTNG_ERR_OVERFLOW; | |
1556 | goto end; | |
1557 | } | |
1558 | ||
999af9c1 JR |
1559 | total_buffer_size_needed_per_cpu = |
1560 | in_chan->attr.num_subbuf * in_chan->attr.subbuf_size; | |
13dd7782 JR |
1561 | ret_code = check_enough_available_memory( |
1562 | total_buffer_size_needed_per_cpu); | |
1563 | if (ret_code != LTTNG_OK) { | |
1564 | ret = -ret_code; | |
1565 | goto end; | |
09b72f7a FD |
1566 | } |
1567 | ||
999af9c1 JR |
1568 | /* Copy the channel for easier manipulation. */ |
1569 | channel = lttng_channel_copy(in_chan); | |
1570 | if (!channel) { | |
1571 | ret = -LTTNG_ERR_NOMEM; | |
1572 | goto end; | |
1573 | } | |
1574 | ||
1575 | /* Populate the channel extended attribute if necessary. */ | |
1576 | if (!channel->attr.extended.ptr) { | |
1577 | struct lttng_channel_extended *extended = | |
1578 | (struct lttng_channel_extended *) zmalloc( | |
1579 | sizeof(*extended)); | |
1580 | ||
1581 | if (!extended) { | |
1582 | ret = -LTTNG_ERR_NOMEM; | |
1583 | goto end; | |
1584 | } | |
1585 | lttng_channel_set_default_extended_attr( | |
1586 | &handle->domain, extended); | |
1587 | channel->attr.extended.ptr = extended; | |
1588 | } | |
1589 | ||
1590 | /* Prepare the payload */ | |
1591 | memset(&lsm, 0, sizeof(lsm)); | |
1592 | ||
cf0bcb51 | 1593 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
60160d2a | 1594 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
7d29a247 | 1595 | |
e1b624d0 JG |
1596 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
1597 | sizeof(lsm.session.name)); | |
1598 | if (ret) { | |
1599 | ret = -LTTNG_ERR_INVALID; | |
1600 | goto end; | |
1601 | } | |
cd80958d | 1602 | |
999af9c1 JR |
1603 | ret = lttng_channel_serialize(channel, &buffer); |
1604 | if (ret) { | |
1605 | ret = -LTTNG_ERR_FATAL; | |
1606 | goto end; | |
1607 | } | |
1608 | ||
1609 | lsm.u.channel.length = buffer.size; | |
1610 | ||
1611 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header( | |
1612 | &lsm, buffer.data, buffer.size, NULL); | |
e1b624d0 | 1613 | end: |
999af9c1 JR |
1614 | lttng_channel_destroy(channel); |
1615 | lttng_dynamic_buffer_reset(&buffer); | |
e1b624d0 | 1616 | return ret; |
8c0faa1d | 1617 | } |
1df4dedd | 1618 | |
2ef84c95 | 1619 | /* |
9ae110e2 JG |
1620 | * All tracing will be stopped for registered events of the channel. |
1621 | * Returns size of returned session payload data or a negative error code. | |
2ef84c95 | 1622 | */ |
cd80958d | 1623 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
2ef84c95 | 1624 | { |
e1b624d0 | 1625 | int ret; |
cd80958d DG |
1626 | struct lttcomm_session_msg lsm; |
1627 | ||
9ae110e2 | 1628 | /* Safety check. Both are mandatory. */ |
9d697d3d | 1629 | if (handle == NULL || name == NULL) { |
2f70b271 | 1630 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1631 | } |
1632 | ||
441c16a7 MD |
1633 | memset(&lsm, 0, sizeof(lsm)); |
1634 | ||
cd80958d | 1635 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
1df4dedd | 1636 | |
e1b624d0 | 1637 | ret = lttng_strncpy(lsm.u.disable.channel_name, name, |
9d697d3d | 1638 | sizeof(lsm.u.disable.channel_name)); |
e1b624d0 JG |
1639 | if (ret) { |
1640 | ret = -LTTNG_ERR_INVALID; | |
1641 | goto end; | |
1642 | } | |
9d697d3d | 1643 | |
60160d2a | 1644 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
cd80958d | 1645 | |
e1b624d0 JG |
1646 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
1647 | sizeof(lsm.session.name)); | |
1648 | if (ret) { | |
1649 | ret = -LTTNG_ERR_INVALID; | |
1650 | goto end; | |
1651 | } | |
cd80958d | 1652 | |
e1b624d0 JG |
1653 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
1654 | end: | |
1655 | return ret; | |
ca95a216 DG |
1656 | } |
1657 | ||
fac6795d | 1658 | /* |
9ae110e2 JG |
1659 | * Lists all available tracepoints of domain. |
1660 | * Sets the contents of the events array. | |
1661 | * Returns the number of lttng_event entries in events; | |
1662 | * on error, returns a negative value. | |
fac6795d | 1663 | */ |
cd80958d | 1664 | int lttng_list_tracepoints(struct lttng_handle *handle, |
2a71efd5 | 1665 | struct lttng_event **events) |
fac6795d | 1666 | { |
8ddd72ef JR |
1667 | enum lttng_error_code ret_code; |
1668 | int ret, total_payload_received; | |
1669 | char *reception_buffer = NULL; | |
1670 | struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_LIST_TRACEPOINTS }; | |
1671 | struct lttcomm_list_command_header *cmd_header = NULL; | |
1672 | size_t cmd_header_len; | |
1673 | unsigned int nb_events = 0; | |
1674 | ||
1675 | if (handle == NULL) { | |
1676 | ret = -LTTNG_ERR_INVALID; | |
1677 | goto end; | |
1678 | } | |
1679 | ||
1680 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); | |
1681 | ||
1682 | ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0, | |
1683 | (void **) &reception_buffer, (void **) &cmd_header, | |
1684 | &cmd_header_len); | |
1685 | if (ret < 0) { | |
1686 | goto end; | |
1687 | } | |
1688 | ||
1689 | total_payload_received = ret; | |
1690 | ||
1691 | if (!cmd_header) { | |
1692 | ret = -LTTNG_ERR_UNK; | |
1693 | goto end; | |
1694 | } | |
1695 | ||
1696 | if (cmd_header->count > INT_MAX) { | |
1697 | ret = -LTTNG_ERR_OVERFLOW; | |
1698 | goto end; | |
1699 | } | |
1700 | ||
1701 | nb_events = (unsigned int) cmd_header->count; | |
fac6795d | 1702 | |
8ddd72ef JR |
1703 | { |
1704 | struct lttng_buffer_view events_view = | |
1705 | lttng_buffer_view_init(reception_buffer, 0, | |
1706 | total_payload_received); | |
1707 | struct lttng_payload_view events_payload_view = | |
1708 | lttng_payload_view_from_buffer_view( | |
1709 | &events_view, 0, -1); | |
2a71efd5 | 1710 | |
8ddd72ef JR |
1711 | ret_code = lttng_events_create_and_flatten_from_payload( |
1712 | &events_payload_view, nb_events, events); | |
1713 | if (ret_code != LTTNG_OK) { | |
1714 | ret = -ret_code; | |
1715 | goto end; | |
1716 | } | |
eb354453 | 1717 | } |
fac6795d | 1718 | |
8ddd72ef JR |
1719 | ret = (int) nb_events; |
1720 | ||
1721 | end: | |
1722 | free(cmd_header); | |
1723 | free(reception_buffer); | |
1724 | return ret; | |
fac6795d DG |
1725 | } |
1726 | ||
f37d259d | 1727 | /* |
9ae110e2 JG |
1728 | * Lists all available tracepoint fields of domain. |
1729 | * Sets the contents of the event field array. | |
1730 | * Returns the number of lttng_event_field entries in events; | |
1731 | * on error, returns a negative value. | |
f37d259d MD |
1732 | */ |
1733 | int lttng_list_tracepoint_fields(struct lttng_handle *handle, | |
1734 | struct lttng_event_field **fields) | |
1735 | { | |
1736 | int ret; | |
1737 | struct lttcomm_session_msg lsm; | |
1738 | ||
1739 | if (handle == NULL) { | |
2f70b271 | 1740 | return -LTTNG_ERR_INVALID; |
f37d259d MD |
1741 | } |
1742 | ||
53efb85a | 1743 | memset(&lsm, 0, sizeof(lsm)); |
f37d259d | 1744 | lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS; |
60160d2a | 1745 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
f37d259d | 1746 | |
cac3069d | 1747 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields); |
f37d259d MD |
1748 | if (ret < 0) { |
1749 | return ret; | |
1750 | } | |
1751 | ||
1752 | return ret / sizeof(struct lttng_event_field); | |
1753 | } | |
1754 | ||
834978fd | 1755 | /* |
9ae110e2 JG |
1756 | * Lists all available kernel system calls. Allocates and sets the contents of |
1757 | * the events array. | |
834978fd | 1758 | * |
9ae110e2 JG |
1759 | * Returns the number of lttng_event entries in events; on error, returns a |
1760 | * negative value. | |
834978fd DG |
1761 | */ |
1762 | int lttng_list_syscalls(struct lttng_event **events) | |
1763 | { | |
8ddd72ef JR |
1764 | enum lttng_error_code ret_code; |
1765 | int ret, total_payload_received; | |
1766 | char *reception_buffer = NULL; | |
1767 | struct lttcomm_session_msg lsm = {}; | |
1768 | struct lttcomm_list_command_header *cmd_header = NULL; | |
1769 | size_t cmd_header_len; | |
1770 | uint32_t nb_events = 0; | |
1771 | ||
1772 | if (!events) { | |
1773 | ret = -LTTNG_ERR_INVALID; | |
1774 | goto end; | |
1775 | } | |
1776 | ||
1777 | lsm.cmd_type = LTTNG_LIST_SYSCALLS; | |
1778 | /* Force kernel domain for system calls. */ | |
1779 | lsm.domain.type = LTTNG_DOMAIN_KERNEL; | |
1780 | ||
1781 | ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0, | |
1782 | (void **) &reception_buffer, (void **) &cmd_header, | |
1783 | &cmd_header_len); | |
1784 | if (ret < 0) { | |
1785 | goto end; | |
1786 | } | |
1787 | total_payload_received = ret; | |
1788 | ||
1789 | if (!cmd_header) { | |
1790 | ret = -LTTNG_ERR_UNK; | |
1791 | goto end; | |
1792 | } | |
1793 | ||
1794 | if (cmd_header->count > INT_MAX) { | |
1795 | ret = -LTTNG_ERR_OVERFLOW; | |
1796 | goto end; | |
1797 | } | |
1798 | ||
1799 | nb_events = (unsigned int) cmd_header->count; | |
1800 | ||
1801 | { | |
1802 | const struct lttng_buffer_view events_view = | |
1803 | lttng_buffer_view_init(reception_buffer, 0, | |
1804 | total_payload_received); | |
1805 | struct lttng_payload_view events_payload_view = | |
1806 | lttng_payload_view_from_buffer_view( | |
1807 | &events_view, 0, -1); | |
1808 | ||
1809 | ret_code = lttng_events_create_and_flatten_from_payload( | |
1810 | &events_payload_view, nb_events, events); | |
1811 | if (ret_code != LTTNG_OK) { | |
1812 | ret = -ret_code; | |
1813 | goto end; | |
1814 | } | |
1815 | } | |
1816 | ||
1817 | ret = (int) nb_events; | |
834978fd | 1818 | |
8ddd72ef JR |
1819 | end: |
1820 | free(reception_buffer); | |
1821 | free(cmd_header); | |
1822 | return ret; | |
834978fd DG |
1823 | } |
1824 | ||
1657e9bb | 1825 | /* |
9ae110e2 | 1826 | * Returns a human readable string describing |
8346beb6 | 1827 | * the error code (positive or negative value). |
1657e9bb | 1828 | */ |
9a745bc7 | 1829 | const char *lttng_strerror(int code) |
1657e9bb | 1830 | { |
8346beb6 PP |
1831 | if (code > 0) { |
1832 | code = -code; | |
1833 | } | |
1834 | ||
f73fabfd | 1835 | return error_get_str(code); |
1657e9bb DG |
1836 | } |
1837 | ||
b178f53e JG |
1838 | enum lttng_error_code lttng_create_session_ext( |
1839 | struct lttng_session_descriptor *session_descriptor) | |
1840 | { | |
1841 | enum lttng_error_code ret_code; | |
1842 | struct lttcomm_session_msg lsm = { | |
1843 | .cmd_type = LTTNG_CREATE_SESSION_EXT, | |
1844 | }; | |
1845 | void *reply = NULL; | |
1846 | struct lttng_buffer_view reply_view; | |
1847 | int reply_ret; | |
1848 | bool sessiond_must_generate_ouput; | |
1849 | struct lttng_dynamic_buffer payload; | |
1850 | int ret; | |
1851 | size_t descriptor_size; | |
1852 | struct lttng_session_descriptor *descriptor_reply = NULL; | |
1853 | ||
1854 | lttng_dynamic_buffer_init(&payload); | |
1855 | if (!session_descriptor) { | |
1856 | ret_code = LTTNG_ERR_INVALID; | |
1857 | goto end; | |
1858 | } | |
1859 | ||
1860 | sessiond_must_generate_ouput = | |
1861 | !lttng_session_descriptor_is_output_destination_initialized( | |
1862 | session_descriptor); | |
1863 | if (sessiond_must_generate_ouput) { | |
1864 | const char *home_dir = utils_get_home_dir(); | |
1865 | size_t home_dir_len = home_dir ? strlen(home_dir) + 1 : 0; | |
1866 | ||
1867 | if (!home_dir || home_dir_len > LTTNG_PATH_MAX) { | |
1868 | ret_code = LTTNG_ERR_FATAL; | |
1869 | goto end; | |
1870 | } | |
1871 | ||
1872 | lsm.u.create_session.home_dir_size = (uint16_t) home_dir_len; | |
1873 | ret = lttng_dynamic_buffer_append(&payload, home_dir, | |
1874 | home_dir_len); | |
1875 | if (ret) { | |
1876 | ret_code = LTTNG_ERR_NOMEM; | |
1877 | goto end; | |
1878 | } | |
1879 | } | |
1880 | ||
1881 | descriptor_size = payload.size; | |
1882 | ret = lttng_session_descriptor_serialize(session_descriptor, | |
1883 | &payload); | |
1884 | if (ret) { | |
1885 | ret_code = LTTNG_ERR_INVALID; | |
1886 | goto end; | |
1887 | } | |
1888 | descriptor_size = payload.size - descriptor_size; | |
1889 | lsm.u.create_session.session_descriptor_size = descriptor_size; | |
1890 | ||
1891 | /* Command returns a session descriptor on success. */ | |
1892 | reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, payload.data, | |
1893 | payload.size, &reply); | |
1894 | if (reply_ret < 0) { | |
4bd69c5f | 1895 | ret_code = (lttng_error_code) -reply_ret; |
b178f53e JG |
1896 | goto end; |
1897 | } else if (reply_ret == 0) { | |
1898 | /* Socket unexpectedly closed by the session daemon. */ | |
1899 | ret_code = LTTNG_ERR_FATAL; | |
1900 | goto end; | |
1901 | } | |
1902 | ||
4bd69c5f | 1903 | reply_view = lttng_buffer_view_init((const char *) reply, 0, reply_ret); |
b178f53e JG |
1904 | ret = lttng_session_descriptor_create_from_buffer(&reply_view, |
1905 | &descriptor_reply); | |
1906 | if (ret < 0) { | |
1907 | ret_code = LTTNG_ERR_FATAL; | |
1908 | goto end; | |
1909 | } | |
1910 | ret_code = LTTNG_OK; | |
1911 | lttng_session_descriptor_assign(session_descriptor, descriptor_reply); | |
1912 | end: | |
1913 | free(reply); | |
1914 | lttng_dynamic_buffer_reset(&payload); | |
1915 | lttng_session_descriptor_destroy(descriptor_reply); | |
1916 | return ret_code; | |
1917 | } | |
1918 | ||
aaf97519 | 1919 | /* |
b178f53e | 1920 | * Create a new session using name and url for destination. |
a4b92340 | 1921 | * |
780d4bb8 | 1922 | * Return 0 on success else a negative LTTng error code. |
00e2e675 | 1923 | */ |
a4b92340 | 1924 | int lttng_create_session(const char *name, const char *url) |
00e2e675 | 1925 | { |
3dd05a85 | 1926 | int ret; |
a4b92340 | 1927 | ssize_t size; |
a4b92340 | 1928 | struct lttng_uri *uris = NULL; |
b178f53e JG |
1929 | struct lttng_session_descriptor *descriptor = NULL; |
1930 | enum lttng_error_code ret_code; | |
00e2e675 | 1931 | |
b178f53e JG |
1932 | if (!name) { |
1933 | ret = -LTTNG_ERR_INVALID; | |
1934 | goto end; | |
00e2e675 DG |
1935 | } |
1936 | ||
bc894455 | 1937 | size = uri_parse_str_urls(url, NULL, &uris); |
a4b92340 | 1938 | if (size < 0) { |
b178f53e JG |
1939 | ret = -LTTNG_ERR_INVALID; |
1940 | goto end; | |
1941 | } | |
1942 | switch (size) { | |
1943 | case 0: | |
1944 | descriptor = lttng_session_descriptor_create(name); | |
1945 | break; | |
1946 | case 1: | |
1947 | if (uris[0].dtype != LTTNG_DST_PATH) { | |
1948 | ret = -LTTNG_ERR_INVALID; | |
1949 | goto end; | |
1950 | } | |
1951 | descriptor = lttng_session_descriptor_local_create(name, | |
1952 | uris[0].dst.path); | |
1953 | break; | |
1954 | case 2: | |
1955 | descriptor = lttng_session_descriptor_network_create(name, url, | |
1956 | NULL); | |
1957 | break; | |
1958 | default: | |
1959 | ret = -LTTNG_ERR_INVALID; | |
1960 | goto end; | |
1961 | } | |
1962 | if (!descriptor) { | |
1963 | ret = -LTTNG_ERR_INVALID; | |
1964 | goto end; | |
00e2e675 | 1965 | } |
b178f53e JG |
1966 | ret_code = lttng_create_session_ext(descriptor); |
1967 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; | |
1968 | end: | |
1969 | lttng_session_descriptor_destroy(descriptor); | |
1970 | free(uris); | |
1971 | return ret; | |
1972 | } | |
00e2e675 | 1973 | |
b178f53e JG |
1974 | /* |
1975 | * Create a session exclusively used for snapshot. | |
1976 | * | |
780d4bb8 | 1977 | * Return 0 on success else a negative LTTng error code. |
b178f53e JG |
1978 | */ |
1979 | int lttng_create_session_snapshot(const char *name, const char *snapshot_url) | |
1980 | { | |
1981 | int ret; | |
1982 | enum lttng_error_code ret_code; | |
1983 | ssize_t size; | |
1984 | struct lttng_uri *uris = NULL; | |
1985 | struct lttng_session_descriptor *descriptor = NULL; | |
a4b92340 | 1986 | |
b178f53e JG |
1987 | if (!name) { |
1988 | ret = -LTTNG_ERR_INVALID; | |
1989 | goto end; | |
1990 | } | |
1991 | ||
1992 | size = uri_parse_str_urls(snapshot_url, NULL, &uris); | |
1993 | if (size < 0) { | |
1994 | ret = -LTTNG_ERR_INVALID; | |
1995 | goto end; | |
1996 | } | |
1997 | /* | |
1998 | * If the user does not specify a custom subdir, use the session name. | |
1999 | */ | |
2000 | if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && | |
2001 | strlen(uris[0].subdir) == 0) { | |
2002 | ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", | |
2003 | name); | |
2004 | if (ret < 0) { | |
2005 | PERROR("Failed to set session name as network destination sub-directory"); | |
2006 | ret = -LTTNG_ERR_FATAL; | |
2007 | goto end; | |
2008 | } else if (ret >= sizeof(uris[0].subdir)) { | |
2009 | /* Truncated output. */ | |
2010 | ret = -LTTNG_ERR_INVALID; | |
2011 | goto end; | |
2012 | } | |
2013 | } | |
3dd05a85 | 2014 | |
b178f53e JG |
2015 | switch (size) { |
2016 | case 0: | |
2017 | descriptor = lttng_session_descriptor_snapshot_create(name); | |
2018 | break; | |
2019 | case 1: | |
2020 | if (uris[0].dtype != LTTNG_DST_PATH) { | |
2021 | ret = -LTTNG_ERR_INVALID; | |
2022 | goto end; | |
2023 | } | |
2024 | descriptor = lttng_session_descriptor_snapshot_local_create( | |
2025 | name, | |
2026 | uris[0].dst.path); | |
2027 | break; | |
2028 | case 2: | |
2029 | descriptor = lttng_session_descriptor_snapshot_network_create( | |
2030 | name, | |
2031 | snapshot_url, | |
2032 | NULL); | |
2033 | break; | |
2034 | default: | |
2035 | ret = -LTTNG_ERR_INVALID; | |
2036 | goto end; | |
2037 | } | |
2038 | if (!descriptor) { | |
2039 | ret = -LTTNG_ERR_INVALID; | |
2040 | goto end; | |
2041 | } | |
2042 | ret_code = lttng_create_session_ext(descriptor); | |
2043 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; | |
2044 | end: | |
2045 | lttng_session_descriptor_destroy(descriptor); | |
3dd05a85 DG |
2046 | free(uris); |
2047 | return ret; | |
00e2e675 DG |
2048 | } |
2049 | ||
b178f53e JG |
2050 | /* |
2051 | * Create a session exclusively used for live. | |
2052 | * | |
780d4bb8 | 2053 | * Return 0 on success else a negative LTTng error code. |
b178f53e JG |
2054 | */ |
2055 | int lttng_create_session_live(const char *name, const char *url, | |
2056 | unsigned int timer_interval) | |
2057 | { | |
2058 | int ret; | |
2059 | enum lttng_error_code ret_code; | |
2060 | struct lttng_session_descriptor *descriptor = NULL; | |
2061 | ||
2062 | if (!name) { | |
2063 | ret = -LTTNG_ERR_INVALID; | |
2064 | goto end; | |
2065 | } | |
2066 | ||
2067 | if (url) { | |
2068 | descriptor = lttng_session_descriptor_live_network_create( | |
2069 | name, url, NULL, timer_interval); | |
2070 | } else { | |
2071 | descriptor = lttng_session_descriptor_live_create( | |
2072 | name, timer_interval); | |
2073 | } | |
2074 | if (!descriptor) { | |
2075 | ret = -LTTNG_ERR_INVALID; | |
2076 | goto end; | |
2077 | } | |
2078 | ret_code = lttng_create_session_ext(descriptor); | |
2079 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; | |
2080 | end: | |
2081 | lttng_session_descriptor_destroy(descriptor); | |
2082 | return ret; | |
2083 | } | |
2084 | ||
e20ee7c2 JD |
2085 | /* |
2086 | * Stop the session and wait for the data before destroying it | |
780d4bb8 MD |
2087 | * |
2088 | * Return 0 on success else a negative LTTng error code. | |
e20ee7c2 JD |
2089 | */ |
2090 | int lttng_destroy_session(const char *session_name) | |
2091 | { | |
2092 | int ret; | |
3e3665b8 JG |
2093 | enum lttng_error_code ret_code; |
2094 | enum lttng_destruction_handle_status status; | |
2095 | struct lttng_destruction_handle *handle = NULL; | |
e20ee7c2 JD |
2096 | |
2097 | /* | |
3e3665b8 JG |
2098 | * Stop the tracing and wait for the data to be |
2099 | * consumed. | |
e20ee7c2 JD |
2100 | */ |
2101 | ret = _lttng_stop_tracing(session_name, 1); | |
2102 | if (ret && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { | |
2103 | goto end; | |
2104 | } | |
2105 | ||
3e3665b8 JG |
2106 | ret_code = lttng_destroy_session_ext(session_name, &handle); |
2107 | if (ret_code != LTTNG_OK) { | |
2108 | ret = (int) -ret_code; | |
2109 | goto end; | |
2110 | } | |
a0377dfe | 2111 | LTTNG_ASSERT(handle); |
3e3665b8 JG |
2112 | |
2113 | /* Block until the completion of the destruction of the session. */ | |
2114 | status = lttng_destruction_handle_wait_for_completion(handle, -1); | |
2115 | if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_COMPLETED) { | |
2116 | ret = -LTTNG_ERR_UNK; | |
2117 | goto end; | |
2118 | } | |
2119 | ||
2120 | status = lttng_destruction_handle_get_result(handle, &ret_code); | |
2121 | if (status != LTTNG_DESTRUCTION_HANDLE_STATUS_OK) { | |
2122 | ret = -LTTNG_ERR_UNK; | |
2123 | goto end; | |
2124 | } | |
780d4bb8 | 2125 | ret = ret_code == LTTNG_OK ? 0 : -ret_code; |
e20ee7c2 | 2126 | end: |
3e3665b8 | 2127 | lttng_destruction_handle_destroy(handle); |
e20ee7c2 JD |
2128 | return ret; |
2129 | } | |
2130 | ||
2131 | /* | |
2132 | * Destroy the session without waiting for the data. | |
2133 | */ | |
2134 | int lttng_destroy_session_no_wait(const char *session_name) | |
2135 | { | |
3e3665b8 | 2136 | enum lttng_error_code ret_code; |
e20ee7c2 | 2137 | |
3e3665b8 | 2138 | ret_code = lttng_destroy_session_ext(session_name, NULL); |
3204017e | 2139 | return ret_code == LTTNG_OK ? 0 : -ret_code; |
e20ee7c2 JD |
2140 | } |
2141 | ||
57167058 | 2142 | /* |
9ae110e2 JG |
2143 | * Ask the session daemon for all available sessions. |
2144 | * Sets the contents of the sessions array. | |
2145 | * Returns the number of lttng_session entries in sessions; | |
2146 | * on error, returns a negative value. | |
57167058 | 2147 | */ |
b178f53e | 2148 | int lttng_list_sessions(struct lttng_session **out_sessions) |
57167058 | 2149 | { |
ca95a216 | 2150 | int ret; |
cd80958d | 2151 | struct lttcomm_session_msg lsm; |
b178f53e JG |
2152 | const size_t session_size = sizeof(struct lttng_session) + |
2153 | sizeof(struct lttng_session_extended); | |
2154 | size_t session_count, i; | |
2155 | struct lttng_session_extended *sessions_extended_begin; | |
2156 | struct lttng_session *sessions = NULL; | |
57167058 | 2157 | |
53efb85a | 2158 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 2159 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
985aea18 MD |
2160 | /* |
2161 | * Initialize out_sessions to NULL so it is initialized when | |
2162 | * lttng_list_sessions returns 0, thus allowing *out_sessions to | |
2163 | * be subsequently freed. | |
2164 | */ | |
2165 | *out_sessions = NULL; | |
b178f53e JG |
2166 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) &sessions); |
2167 | if (ret <= 0) { | |
b178f53e JG |
2168 | goto end; |
2169 | } | |
2170 | if (!sessions) { | |
2171 | ret = -LTTNG_ERR_FATAL; | |
2172 | goto end; | |
2173 | } | |
2174 | ||
2175 | if (ret % session_size) { | |
2176 | ret = -LTTNG_ERR_UNK; | |
2177 | free(sessions); | |
b178f53e | 2178 | goto end; |
57167058 | 2179 | } |
b178f53e JG |
2180 | session_count = (size_t) ret / session_size; |
2181 | sessions_extended_begin = (struct lttng_session_extended *) | |
2182 | (&sessions[session_count]); | |
57167058 | 2183 | |
b178f53e JG |
2184 | /* Set extended session info pointers. */ |
2185 | for (i = 0; i < session_count; i++) { | |
2186 | struct lttng_session *session = &sessions[i]; | |
2187 | struct lttng_session_extended *extended = | |
2188 | &(sessions_extended_begin[i]); | |
2189 | ||
2190 | session->extended.ptr = extended; | |
2191 | } | |
2192 | ||
2193 | ret = (int) session_count; | |
2194 | *out_sessions = sessions; | |
2195 | end: | |
2196 | return ret; | |
2197 | } | |
2198 | ||
2199 | enum lttng_error_code lttng_session_get_creation_time( | |
2200 | const struct lttng_session *session, uint64_t *creation_time) | |
2201 | { | |
2202 | enum lttng_error_code ret = LTTNG_OK; | |
2203 | struct lttng_session_extended *extended; | |
2204 | ||
2205 | if (!session || !creation_time || !session->extended.ptr) { | |
2206 | ret = LTTNG_ERR_INVALID; | |
2207 | goto end; | |
2208 | } | |
2209 | ||
4bd69c5f | 2210 | extended = (lttng_session_extended *) session->extended.ptr; |
b178f53e JG |
2211 | if (!extended->creation_time.is_set) { |
2212 | /* Not created on the session daemon yet. */ | |
2213 | ret = LTTNG_ERR_SESSION_NOT_EXIST; | |
2214 | goto end; | |
2215 | } | |
2216 | *creation_time = extended->creation_time.value; | |
2217 | end: | |
2218 | return ret; | |
57167058 DG |
2219 | } |
2220 | ||
d7ba1388 MD |
2221 | int lttng_set_session_shm_path(const char *session_name, |
2222 | const char *shm_path) | |
2223 | { | |
e1b624d0 | 2224 | int ret; |
d7ba1388 MD |
2225 | struct lttcomm_session_msg lsm; |
2226 | ||
2227 | if (session_name == NULL) { | |
2228 | return -LTTNG_ERR_INVALID; | |
2229 | } | |
2230 | ||
2231 | memset(&lsm, 0, sizeof(lsm)); | |
2232 | lsm.cmd_type = LTTNG_SET_SESSION_SHM_PATH; | |
2233 | ||
e1b624d0 | 2234 | ret = lttng_strncpy(lsm.session.name, session_name, |
d7ba1388 | 2235 | sizeof(lsm.session.name)); |
e1b624d0 JG |
2236 | if (ret) { |
2237 | ret = -LTTNG_ERR_INVALID; | |
2238 | goto end; | |
2239 | } | |
2240 | ||
2241 | ret = lttng_strncpy(lsm.u.set_shm_path.shm_path, shm_path ?: "", | |
d7ba1388 | 2242 | sizeof(lsm.u.set_shm_path.shm_path)); |
e1b624d0 JG |
2243 | if (ret) { |
2244 | ret = -LTTNG_ERR_INVALID; | |
2245 | goto end; | |
2246 | } | |
d7ba1388 | 2247 | |
e1b624d0 JG |
2248 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
2249 | end: | |
2250 | return ret; | |
d7ba1388 MD |
2251 | } |
2252 | ||
9f19cc17 | 2253 | /* |
9ae110e2 JG |
2254 | * Ask the session daemon for all available domains of a session. |
2255 | * Sets the contents of the domains array. | |
2256 | * Returns the number of lttng_domain entries in domains; | |
2257 | * on error, returns a negative value. | |
9f19cc17 | 2258 | */ |
330be774 | 2259 | int lttng_list_domains(const char *session_name, |
cd80958d | 2260 | struct lttng_domain **domains) |
9f19cc17 DG |
2261 | { |
2262 | int ret; | |
cd80958d DG |
2263 | struct lttcomm_session_msg lsm; |
2264 | ||
330be774 | 2265 | if (session_name == NULL) { |
e1b624d0 JG |
2266 | ret = -LTTNG_ERR_INVALID; |
2267 | goto error; | |
cd80958d | 2268 | } |
9f19cc17 | 2269 | |
53efb85a | 2270 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d DG |
2271 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
2272 | ||
e1b624d0 | 2273 | ret = lttng_strncpy(lsm.session.name, session_name, |
cac3069d | 2274 | sizeof(lsm.session.name)); |
e1b624d0 JG |
2275 | if (ret) { |
2276 | ret = -LTTNG_ERR_INVALID; | |
2277 | goto error; | |
2278 | } | |
cd80958d | 2279 | |
cac3069d | 2280 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains); |
9f19cc17 | 2281 | if (ret < 0) { |
e1b624d0 | 2282 | goto error; |
9f19cc17 DG |
2283 | } |
2284 | ||
2285 | return ret / sizeof(struct lttng_domain); | |
e1b624d0 JG |
2286 | error: |
2287 | return ret; | |
9f19cc17 DG |
2288 | } |
2289 | ||
2290 | /* | |
9ae110e2 JG |
2291 | * Ask the session daemon for all available channels of a session. |
2292 | * Sets the contents of the channels array. | |
2293 | * Returns the number of lttng_channel entries in channels; | |
2294 | * on error, returns a negative value. | |
9f19cc17 | 2295 | */ |
cd80958d DG |
2296 | int lttng_list_channels(struct lttng_handle *handle, |
2297 | struct lttng_channel **channels) | |
9f19cc17 | 2298 | { |
999af9c1 | 2299 | int ret, total_payload_received; |
cd80958d | 2300 | struct lttcomm_session_msg lsm; |
999af9c1 JR |
2301 | char *reception_buffer = NULL; |
2302 | size_t cmd_header_len = 0; | |
2303 | struct lttcomm_list_command_header *cmd_header = NULL; | |
2304 | struct lttng_dynamic_buffer tmp_buffer; | |
2305 | ||
2306 | lttng_dynamic_buffer_init(&tmp_buffer); | |
cd80958d | 2307 | |
9d697d3d | 2308 | if (handle == NULL) { |
53e367f9 JG |
2309 | ret = -LTTNG_ERR_INVALID; |
2310 | goto end; | |
cd80958d DG |
2311 | } |
2312 | ||
53efb85a | 2313 | memset(&lsm, 0, sizeof(lsm)); |
cd80958d | 2314 | lsm.cmd_type = LTTNG_LIST_CHANNELS; |
e1b624d0 | 2315 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
cd80958d | 2316 | sizeof(lsm.session.name)); |
e1b624d0 JG |
2317 | if (ret) { |
2318 | ret = -LTTNG_ERR_INVALID; | |
2319 | goto end; | |
2320 | } | |
9f19cc17 | 2321 | |
60160d2a | 2322 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
9f19cc17 | 2323 | |
999af9c1 JR |
2324 | ret = lttng_ctl_ask_sessiond_fds_varlen(&lsm, NULL, 0, NULL, 0, |
2325 | (void **) &reception_buffer, (void **) &cmd_header, | |
2326 | &cmd_header_len); | |
9f19cc17 | 2327 | if (ret < 0) { |
53e367f9 JG |
2328 | goto end; |
2329 | } | |
2330 | ||
999af9c1 JR |
2331 | total_payload_received = ret; |
2332 | ||
2333 | if (cmd_header_len != sizeof(*cmd_header)) { | |
2334 | ret = -LTTNG_ERR_FATAL; | |
53e367f9 | 2335 | goto end; |
9f19cc17 DG |
2336 | } |
2337 | ||
999af9c1 JR |
2338 | if (!cmd_header) { |
2339 | ret = LTTNG_ERR_UNK; | |
2340 | goto end; | |
2341 | } | |
2342 | ||
2343 | if (cmd_header->count > INT_MAX) { | |
2344 | ret = -LTTNG_ERR_OVERFLOW; | |
2345 | goto end; | |
2346 | } | |
53e367f9 | 2347 | |
999af9c1 JR |
2348 | { |
2349 | enum lttng_error_code ret_code; | |
2350 | const struct lttng_buffer_view events_view = | |
2351 | lttng_buffer_view_init(reception_buffer, 0, | |
2352 | total_payload_received); | |
2353 | ||
2354 | ret_code = lttng_channels_create_and_flatten_from_buffer( | |
2355 | &events_view, cmd_header->count, channels); | |
2356 | if (ret_code != LTTNG_OK) { | |
2357 | ret = -ret_code; | |
2358 | goto end; | |
2359 | } | |
53e367f9 JG |
2360 | } |
2361 | ||
999af9c1 | 2362 | ret = (int) cmd_header->count; |
53e367f9 | 2363 | end: |
999af9c1 JR |
2364 | free(cmd_header); |
2365 | free(reception_buffer); | |
53e367f9 | 2366 | return ret; |
9f19cc17 DG |
2367 | } |
2368 | ||
2369 | /* | |
9ae110e2 JG |
2370 | * Ask the session daemon for all available events of a session channel. |
2371 | * Sets the contents of the events array. | |
2372 | * Returns the number of lttng_event entries in events; | |
2373 | * on error, returns a negative value. | |
9f19cc17 | 2374 | */ |
cd80958d DG |
2375 | int lttng_list_events(struct lttng_handle *handle, |
2376 | const char *channel_name, struct lttng_event **events) | |
9f19cc17 DG |
2377 | { |
2378 | int ret; | |
e368fb43 | 2379 | struct lttcomm_session_msg lsm = {}; |
8ddd72ef | 2380 | struct lttng_payload reply; |
e368fb43 JG |
2381 | struct lttng_payload_view lsm_view = |
2382 | lttng_payload_view_init_from_buffer( | |
2383 | (const char *) &lsm, 0, sizeof(lsm)); | |
8ddd72ef | 2384 | unsigned int nb_events = 0; |
9f19cc17 | 2385 | |
8ddd72ef | 2386 | /* Safety check. An handle and channel name are mandatory. */ |
9d697d3d | 2387 | if (handle == NULL || channel_name == NULL) { |
e368fb43 JG |
2388 | ret = -LTTNG_ERR_INVALID; |
2389 | goto end; | |
cd80958d DG |
2390 | } |
2391 | ||
8ddd72ef | 2392 | lttng_payload_init(&reply); |
e368fb43 | 2393 | |
8ddd72ef | 2394 | /* Initialize command parameters. */ |
cd80958d | 2395 | lsm.cmd_type = LTTNG_LIST_EVENTS; |
e1b624d0 | 2396 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
cd80958d | 2397 | sizeof(lsm.session.name)); |
e1b624d0 JG |
2398 | if (ret) { |
2399 | ret = -LTTNG_ERR_INVALID; | |
2400 | goto end; | |
2401 | } | |
2402 | ||
2403 | ret = lttng_strncpy(lsm.u.list.channel_name, channel_name, | |
cd80958d | 2404 | sizeof(lsm.u.list.channel_name)); |
e1b624d0 JG |
2405 | if (ret) { |
2406 | ret = -LTTNG_ERR_INVALID; | |
2407 | goto end; | |
2408 | } | |
2409 | ||
60160d2a | 2410 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
9f19cc17 | 2411 | |
8ddd72ef JR |
2412 | /* Execute command against the session daemon. */ |
2413 | ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply); | |
9f19cc17 | 2414 | if (ret < 0) { |
de453daa | 2415 | goto end; |
9f19cc17 DG |
2416 | } |
2417 | ||
e368fb43 | 2418 | { |
8ddd72ef JR |
2419 | const struct lttcomm_list_command_header *cmd_reply_header = |
2420 | NULL; | |
2421 | const lttng_payload_view cmd_reply_header_view = | |
2422 | lttng_payload_view_from_payload(&reply, 0, | |
2423 | sizeof(*cmd_reply_header)); | |
2424 | ||
2425 | if (!lttng_payload_view_is_valid(&cmd_reply_header_view)) { | |
2426 | ret = -LTTNG_ERR_INVALID_PROTOCOL; | |
2427 | goto end; | |
56f0bc67 | 2428 | } |
b4e3ceb9 | 2429 | |
8ddd72ef JR |
2430 | cmd_reply_header = (const struct lttcomm_list_command_header *) |
2431 | cmd_reply_header_view.buffer | |
2432 | .data; | |
2433 | if (cmd_reply_header->count > INT_MAX) { | |
2434 | ret = -LTTNG_ERR_OVERFLOW; | |
2435 | goto end; | |
2436 | } | |
56f0bc67 | 2437 | |
8ddd72ef | 2438 | nb_events = (unsigned int) cmd_reply_header->count; |
56f0bc67 | 2439 | } |
de453daa | 2440 | |
e368fb43 | 2441 | { |
8ddd72ef JR |
2442 | enum lttng_error_code ret_code; |
2443 | lttng_payload_view cmd_reply_payload = lttng_payload_view_from_payload( | |
2444 | &reply, | |
2445 | sizeof(struct lttcomm_list_command_header), -1); | |
56f0bc67 | 2446 | |
8ddd72ef JR |
2447 | ret_code = lttng_events_create_and_flatten_from_payload( |
2448 | &cmd_reply_payload, nb_events, events); | |
2449 | if (ret_code != LTTNG_OK) { | |
2450 | ret = -((int) ret_code); | |
2451 | goto end; | |
56f0bc67 | 2452 | } |
de453daa JG |
2453 | } |
2454 | ||
de453daa JG |
2455 | ret = (int) nb_events; |
2456 | end: | |
8ddd72ef | 2457 | lttng_payload_reset(&reply); |
b4e3ceb9 | 2458 | return ret; |
9f19cc17 DG |
2459 | } |
2460 | ||
fac6795d | 2461 | /* |
1c8d13c8 TD |
2462 | * Sets the tracing_group variable with name. |
2463 | * This function allocates memory pointed to by tracing_group. | |
2464 | * On success, returns 0, on error, returns -1 (null name) or -ENOMEM. | |
fac6795d DG |
2465 | */ |
2466 | int lttng_set_tracing_group(const char *name) | |
2467 | { | |
e32a3d0f | 2468 | int ret = 0; |
76da29b6 | 2469 | char *new_group; |
e32a3d0f | 2470 | |
9d697d3d | 2471 | if (name == NULL) { |
e32a3d0f JG |
2472 | ret = -LTTNG_ERR_INVALID; |
2473 | goto end; | |
9d697d3d DG |
2474 | } |
2475 | ||
e32a3d0f JG |
2476 | new_group = strdup(name); |
2477 | if (!new_group) { | |
2478 | ret = -LTTNG_ERR_FATAL; | |
2479 | goto end; | |
fac6795d DG |
2480 | } |
2481 | ||
76da29b6 JR |
2482 | free(tracing_group); |
2483 | tracing_group = new_group; | |
2484 | new_group = NULL; | |
2485 | ||
e32a3d0f JG |
2486 | end: |
2487 | return ret; | |
fac6795d DG |
2488 | } |
2489 | ||
cd80958d | 2490 | int lttng_calibrate(struct lttng_handle *handle, |
d0254c7c MD |
2491 | struct lttng_calibrate *calibrate) |
2492 | { | |
b812e5ca PP |
2493 | /* |
2494 | * This command was removed in LTTng 2.9. | |
2495 | */ | |
2496 | return -LTTNG_ERR_UND; | |
d0254c7c MD |
2497 | } |
2498 | ||
5edd7e09 DG |
2499 | /* |
2500 | * Set default channel attributes. | |
441c16a7 | 2501 | * If either or both of the arguments are null, attr content is zeroe'd. |
5edd7e09 DG |
2502 | */ |
2503 | void lttng_channel_set_default_attr(struct lttng_domain *domain, | |
2504 | struct lttng_channel_attr *attr) | |
2505 | { | |
294851b0 | 2506 | struct lttng_channel_extended *extended; |
cf0bcb51 | 2507 | |
5edd7e09 DG |
2508 | /* Safety check */ |
2509 | if (attr == NULL || domain == NULL) { | |
2510 | return; | |
2511 | } | |
2512 | ||
999af9c1 | 2513 | /* Save the pointer for later use */ |
294851b0 | 2514 | extended = (struct lttng_channel_extended *) attr->extended.ptr; |
eacaa7a6 DS |
2515 | memset(attr, 0, sizeof(struct lttng_channel_attr)); |
2516 | ||
0a9c6494 DG |
2517 | /* Same for all domains. */ |
2518 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
2519 | attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE; | |
2520 | attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT; | |
2521 | ||
5edd7e09 DG |
2522 | switch (domain->type) { |
2523 | case LTTNG_DOMAIN_KERNEL: | |
9ae110e2 JG |
2524 | attr->switch_timer_interval = |
2525 | DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; | |
d92ff3ef | 2526 | attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; |
3e230f92 | 2527 | attr->subbuf_size = default_get_kernel_channel_subbuf_size(); |
5edd7e09 DG |
2528 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
2529 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
2530 | break; | |
2531 | case LTTNG_DOMAIN_UST: | |
0a9c6494 DG |
2532 | switch (domain->buf_type) { |
2533 | case LTTNG_BUFFER_PER_UID: | |
2534 | attr->subbuf_size = default_get_ust_uid_channel_subbuf_size(); | |
2535 | attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM; | |
2536 | attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT; | |
9ae110e2 JG |
2537 | attr->switch_timer_interval = |
2538 | DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER; | |
2539 | attr->read_timer_interval = | |
2540 | DEFAULT_UST_UID_CHANNEL_READ_TIMER; | |
0a9c6494 DG |
2541 | break; |
2542 | case LTTNG_BUFFER_PER_PID: | |
2543 | default: | |
2544 | attr->subbuf_size = default_get_ust_pid_channel_subbuf_size(); | |
2545 | attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM; | |
2546 | attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT; | |
9ae110e2 JG |
2547 | attr->switch_timer_interval = |
2548 | DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER; | |
2549 | attr->read_timer_interval = | |
2550 | DEFAULT_UST_PID_CHANNEL_READ_TIMER; | |
0a9c6494 DG |
2551 | break; |
2552 | } | |
5edd7e09 | 2553 | default: |
441c16a7 | 2554 | /* Default behavior: leave set to 0. */ |
5edd7e09 DG |
2555 | break; |
2556 | } | |
cf0bcb51 | 2557 | |
999af9c1 JR |
2558 | if (extended) { |
2559 | lttng_channel_set_default_extended_attr(domain, extended); | |
2560 | } | |
2561 | ||
2562 | /* Reassign the extended pointer. */ | |
cf0bcb51 | 2563 | attr->extended.ptr = extended; |
5edd7e09 DG |
2564 | } |
2565 | ||
5ba3702f JG |
2566 | int lttng_channel_get_discarded_event_count(struct lttng_channel *channel, |
2567 | uint64_t *discarded_events) | |
2568 | { | |
2569 | int ret = 0; | |
cf0bcb51 | 2570 | struct lttng_channel_extended *chan_ext; |
5ba3702f JG |
2571 | |
2572 | if (!channel || !discarded_events) { | |
2573 | ret = -LTTNG_ERR_INVALID; | |
2574 | goto end; | |
2575 | } | |
2576 | ||
4bd69c5f | 2577 | chan_ext = (lttng_channel_extended *) channel->attr.extended.ptr; |
5ba3702f JG |
2578 | if (!chan_ext) { |
2579 | /* | |
2580 | * This can happen since the lttng_channel structure is | |
2581 | * used for other tasks where this pointer is never set. | |
2582 | */ | |
2583 | *discarded_events = 0; | |
2584 | goto end; | |
2585 | } | |
2586 | ||
2587 | *discarded_events = chan_ext->discarded_events; | |
2588 | end: | |
2589 | return ret; | |
2590 | } | |
2591 | ||
2592 | int lttng_channel_get_lost_packet_count(struct lttng_channel *channel, | |
2593 | uint64_t *lost_packets) | |
2594 | { | |
2595 | int ret = 0; | |
cf0bcb51 | 2596 | struct lttng_channel_extended *chan_ext; |
5ba3702f JG |
2597 | |
2598 | if (!channel || !lost_packets) { | |
2599 | ret = -LTTNG_ERR_INVALID; | |
2600 | goto end; | |
2601 | } | |
2602 | ||
4bd69c5f | 2603 | chan_ext = (lttng_channel_extended *) channel->attr.extended.ptr; |
5ba3702f JG |
2604 | if (!chan_ext) { |
2605 | /* | |
2606 | * This can happen since the lttng_channel structure is | |
2607 | * used for other tasks where this pointer is never set. | |
2608 | */ | |
2609 | *lost_packets = 0; | |
2610 | goto end; | |
2611 | } | |
2612 | ||
2613 | *lost_packets = chan_ext->lost_packets; | |
2614 | end: | |
2615 | return ret; | |
2616 | } | |
2617 | ||
cf0bcb51 JG |
2618 | int lttng_channel_get_monitor_timer_interval(struct lttng_channel *chan, |
2619 | uint64_t *monitor_timer_interval) | |
2620 | { | |
2621 | int ret = 0; | |
2622 | ||
2623 | if (!chan || !monitor_timer_interval) { | |
2624 | ret = -LTTNG_ERR_INVALID; | |
2625 | goto end; | |
2626 | } | |
2627 | ||
2628 | if (!chan->attr.extended.ptr) { | |
2629 | ret = -LTTNG_ERR_INVALID; | |
2630 | goto end; | |
2631 | } | |
2632 | ||
2633 | *monitor_timer_interval = ((struct lttng_channel_extended *) | |
2634 | chan->attr.extended.ptr)->monitor_timer_interval; | |
2635 | end: | |
2636 | return ret; | |
2637 | } | |
2638 | ||
2639 | int lttng_channel_set_monitor_timer_interval(struct lttng_channel *chan, | |
2640 | uint64_t monitor_timer_interval) | |
2641 | { | |
2642 | int ret = 0; | |
2643 | ||
2644 | if (!chan || !chan->attr.extended.ptr) { | |
2645 | ret = -LTTNG_ERR_INVALID; | |
2646 | goto end; | |
2647 | } | |
2648 | ||
2649 | ((struct lttng_channel_extended *) | |
2650 | chan->attr.extended.ptr)->monitor_timer_interval = | |
2651 | monitor_timer_interval; | |
2652 | end: | |
2653 | return ret; | |
2654 | } | |
2655 | ||
491d1539 MD |
2656 | int lttng_channel_get_blocking_timeout(struct lttng_channel *chan, |
2657 | int64_t *blocking_timeout) | |
2658 | { | |
2659 | int ret = 0; | |
2660 | ||
2661 | if (!chan || !blocking_timeout) { | |
2662 | ret = -LTTNG_ERR_INVALID; | |
2663 | goto end; | |
2664 | } | |
2665 | ||
2666 | if (!chan->attr.extended.ptr) { | |
2667 | ret = -LTTNG_ERR_INVALID; | |
2668 | goto end; | |
2669 | } | |
2670 | ||
2671 | *blocking_timeout = ((struct lttng_channel_extended *) | |
2672 | chan->attr.extended.ptr)->blocking_timeout; | |
2673 | end: | |
2674 | return ret; | |
2675 | } | |
2676 | ||
2677 | int lttng_channel_set_blocking_timeout(struct lttng_channel *chan, | |
2678 | int64_t blocking_timeout) | |
2679 | { | |
2680 | int ret = 0; | |
2681 | int64_t msec_timeout; | |
2682 | ||
2683 | if (!chan || !chan->attr.extended.ptr) { | |
2684 | ret = -LTTNG_ERR_INVALID; | |
2685 | goto end; | |
2686 | } | |
2687 | ||
2688 | if (blocking_timeout < 0 && blocking_timeout != -1) { | |
2689 | ret = -LTTNG_ERR_INVALID; | |
2690 | goto end; | |
2691 | } | |
2692 | ||
2693 | /* | |
2694 | * LTTng-ust's use of poll() to implement this timeout mechanism forces | |
2695 | * us to accept a narrower range of values (msecs expressed as a signed | |
2696 | * 32-bit integer). | |
2697 | */ | |
2698 | msec_timeout = blocking_timeout / 1000; | |
2699 | if (msec_timeout != (int32_t) msec_timeout) { | |
2700 | ret = -LTTNG_ERR_INVALID; | |
2701 | goto end; | |
2702 | } | |
2703 | ||
2704 | ((struct lttng_channel_extended *) | |
2705 | chan->attr.extended.ptr)->blocking_timeout = | |
2706 | blocking_timeout; | |
2707 | end: | |
2708 | return ret; | |
2709 | } | |
2710 | ||
fac6795d | 2711 | /* |
2269e89e | 2712 | * Check if session daemon is alive. |
fac6795d | 2713 | * |
2269e89e | 2714 | * Return 1 if alive or 0 if not. |
1c8d13c8 | 2715 | * On error returns a negative value. |
fac6795d | 2716 | */ |
947308c4 | 2717 | int lttng_session_daemon_alive(void) |
fac6795d DG |
2718 | { |
2719 | int ret; | |
2720 | ||
2721 | ret = set_session_daemon_path(); | |
2722 | if (ret < 0) { | |
9ae110e2 | 2723 | /* Error. */ |
fac6795d DG |
2724 | return ret; |
2725 | } | |
2726 | ||
9d035200 | 2727 | if (*sessiond_sock_path == '\0') { |
2f70b271 | 2728 | /* |
9ae110e2 JG |
2729 | * No socket path set. Weird error which means the constructor |
2730 | * was not called. | |
2f70b271 | 2731 | */ |
a0377dfe | 2732 | abort(); |
fac6795d DG |
2733 | } |
2734 | ||
2269e89e | 2735 | ret = try_connect_sessiond(sessiond_sock_path); |
7d8234d9 | 2736 | if (ret < 0) { |
9ae110e2 | 2737 | /* Not alive. */ |
7d8234d9 MD |
2738 | return 0; |
2739 | } | |
7d8234d9 | 2740 | |
9ae110e2 | 2741 | /* Is alive. */ |
947308c4 | 2742 | return 1; |
fac6795d DG |
2743 | } |
2744 | ||
00e2e675 | 2745 | /* |
a4b92340 | 2746 | * Set URL for a consumer for a session and domain. |
00e2e675 DG |
2747 | * |
2748 | * Return 0 on success, else a negative value. | |
2749 | */ | |
a4b92340 DG |
2750 | int lttng_set_consumer_url(struct lttng_handle *handle, |
2751 | const char *control_url, const char *data_url) | |
00e2e675 | 2752 | { |
3dd05a85 | 2753 | int ret; |
a4b92340 | 2754 | ssize_t size; |
00e2e675 | 2755 | struct lttcomm_session_msg lsm; |
a4b92340 | 2756 | struct lttng_uri *uris = NULL; |
00e2e675 | 2757 | |
a4b92340 | 2758 | if (handle == NULL || (control_url == NULL && data_url == NULL)) { |
e1b624d0 JG |
2759 | ret = -LTTNG_ERR_INVALID; |
2760 | goto error; | |
00e2e675 DG |
2761 | } |
2762 | ||
a4b92340 DG |
2763 | memset(&lsm, 0, sizeof(lsm)); |
2764 | ||
00e2e675 DG |
2765 | lsm.cmd_type = LTTNG_SET_CONSUMER_URI; |
2766 | ||
e1b624d0 | 2767 | ret = lttng_strncpy(lsm.session.name, handle->session_name, |
00e2e675 | 2768 | sizeof(lsm.session.name)); |
e1b624d0 JG |
2769 | if (ret) { |
2770 | ret = -LTTNG_ERR_INVALID; | |
2771 | goto error; | |
2772 | } | |
2773 | ||
60160d2a | 2774 | COPY_DOMAIN_PACKED(lsm.domain, handle->domain); |
00e2e675 | 2775 | |
bc894455 | 2776 | size = uri_parse_str_urls(control_url, data_url, &uris); |
a4b92340 | 2777 | if (size < 0) { |
e1b624d0 JG |
2778 | ret = -LTTNG_ERR_INVALID; |
2779 | goto error; | |
a4b92340 DG |
2780 | } |
2781 | ||
2782 | lsm.u.uri.size = size; | |
00e2e675 | 2783 | |
795a978d | 2784 | ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, uris, |
cac3069d | 2785 | sizeof(struct lttng_uri) * size, NULL); |
3dd05a85 DG |
2786 | |
2787 | free(uris); | |
e1b624d0 | 2788 | error: |
3dd05a85 | 2789 | return ret; |
00e2e675 DG |
2790 | } |
2791 | ||
2792 | /* | |
9c6bda17 | 2793 | * [OBSOLETE] |
00e2e675 | 2794 | */ |
4bd69c5f SM |
2795 | extern "C" |
2796 | LTTNG_EXPORT int lttng_enable_consumer(struct lttng_handle *handle); | |
00e2e675 DG |
2797 | int lttng_enable_consumer(struct lttng_handle *handle) |
2798 | { | |
785d2d0d | 2799 | return -ENOSYS; |
00e2e675 DG |
2800 | } |
2801 | ||
2802 | /* | |
9c6bda17 | 2803 | * [OBSOLETE] |
00e2e675 | 2804 | */ |
4bd69c5f SM |
2805 | extern "C" |
2806 | LTTNG_EXPORT int lttng_disable_consumer(struct lttng_handle *handle); | |
00e2e675 DG |
2807 | int lttng_disable_consumer(struct lttng_handle *handle) |
2808 | { | |
785d2d0d | 2809 | return -ENOSYS; |
00e2e675 DG |
2810 | } |
2811 | ||
07424f16 | 2812 | /* |
b178f53e | 2813 | * [OBSOLETE] |
07424f16 | 2814 | */ |
4bd69c5f SM |
2815 | extern "C" |
2816 | LTTNG_EXPORT int _lttng_create_session_ext(const char *name, const char *url, | |
2ec40111 | 2817 | const char *datetime); |
07424f16 DG |
2818 | int _lttng_create_session_ext(const char *name, const char *url, |
2819 | const char *datetime) | |
2820 | { | |
b178f53e | 2821 | return -ENOSYS; |
07424f16 DG |
2822 | } |
2823 | ||
806e2684 DG |
2824 | /* |
2825 | * For a given session name, this call checks if the data is ready to be read | |
2826 | * or is still being extracted by the consumer(s) hence not ready to be used by | |
2827 | * any readers. | |
2828 | */ | |
6d805429 | 2829 | int lttng_data_pending(const char *session_name) |
806e2684 DG |
2830 | { |
2831 | int ret; | |
2832 | struct lttcomm_session_msg lsm; | |
f6151c55 | 2833 | uint8_t *pending = NULL; |
806e2684 DG |
2834 | |
2835 | if (session_name == NULL) { | |
2836 | return -LTTNG_ERR_INVALID; | |
2837 | } | |
2838 | ||
53efb85a | 2839 | memset(&lsm, 0, sizeof(lsm)); |
6d805429 | 2840 | lsm.cmd_type = LTTNG_DATA_PENDING; |
806e2684 | 2841 | |
e1b624d0 | 2842 | ret = lttng_strncpy(lsm.session.name, session_name, |
cac3069d | 2843 | sizeof(lsm.session.name)); |
e1b624d0 JG |
2844 | if (ret) { |
2845 | ret = -LTTNG_ERR_INVALID; | |
2846 | goto end; | |
2847 | } | |
806e2684 | 2848 | |
f6151c55 JG |
2849 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) &pending); |
2850 | if (ret < 0) { | |
2851 | goto end; | |
2852 | } else if (ret != 1) { | |
2853 | /* Unexpected payload size */ | |
2854 | ret = -LTTNG_ERR_INVALID; | |
2855 | goto end; | |
e848d36d JG |
2856 | } else if (!pending) { |
2857 | /* Internal error. */ | |
2858 | ret = -LTTNG_ERR_UNK; | |
2859 | goto end; | |
806e2684 DG |
2860 | } |
2861 | ||
f6151c55 JG |
2862 | ret = (int) *pending; |
2863 | end: | |
2864 | free(pending); | |
806e2684 DG |
2865 | return ret; |
2866 | } | |
2867 | ||
93ec662e JD |
2868 | /* |
2869 | * Regenerate the metadata for a session. | |
2870 | * Return 0 on success, a negative error code on error. | |
2871 | */ | |
eded6438 | 2872 | int lttng_regenerate_metadata(const char *session_name) |
93ec662e JD |
2873 | { |
2874 | int ret; | |
2875 | struct lttcomm_session_msg lsm; | |
2876 | ||
2877 | if (!session_name) { | |
2878 | ret = -LTTNG_ERR_INVALID; | |
2879 | goto end; | |
2880 | } | |
2881 | ||
2882 | memset(&lsm, 0, sizeof(lsm)); | |
eded6438 | 2883 | lsm.cmd_type = LTTNG_REGENERATE_METADATA; |
93ec662e | 2884 | |
e1b624d0 | 2885 | ret = lttng_strncpy(lsm.session.name, session_name, |
93ec662e | 2886 | sizeof(lsm.session.name)); |
e1b624d0 JG |
2887 | if (ret) { |
2888 | ret = -LTTNG_ERR_INVALID; | |
2889 | goto end; | |
2890 | } | |
93ec662e JD |
2891 | |
2892 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
2893 | if (ret < 0) { | |
2894 | goto end; | |
2895 | } | |
2896 | ||
2897 | ret = 0; | |
2898 | end: | |
2899 | return ret; | |
2900 | } | |
2901 | ||
eded6438 JD |
2902 | /* |
2903 | * Deprecated, replaced by lttng_regenerate_metadata. | |
2904 | */ | |
2905 | int lttng_metadata_regenerate(const char *session_name) | |
2906 | { | |
2907 | return lttng_regenerate_metadata(session_name); | |
2908 | } | |
2909 | ||
c2561365 JD |
2910 | /* |
2911 | * Regenerate the statedump of a session. | |
2912 | * Return 0 on success, a negative error code on error. | |
2913 | */ | |
2914 | int lttng_regenerate_statedump(const char *session_name) | |
2915 | { | |
2916 | int ret; | |
2917 | struct lttcomm_session_msg lsm; | |
2918 | ||
2919 | if (!session_name) { | |
2920 | ret = -LTTNG_ERR_INVALID; | |
2921 | goto end; | |
2922 | } | |
2923 | ||
2924 | memset(&lsm, 0, sizeof(lsm)); | |
2925 | lsm.cmd_type = LTTNG_REGENERATE_STATEDUMP; | |
2926 | ||
e1b624d0 | 2927 | ret = lttng_strncpy(lsm.session.name, session_name, |
c2561365 | 2928 | sizeof(lsm.session.name)); |
e1b624d0 JG |
2929 | if (ret) { |
2930 | ret = -LTTNG_ERR_INVALID; | |
2931 | goto end; | |
2932 | } | |
c2561365 JD |
2933 | |
2934 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); | |
2935 | if (ret < 0) { | |
2936 | goto end; | |
2937 | } | |
2938 | ||
2939 | ret = 0; | |
2940 | end: | |
2941 | return ret; | |
2942 | } | |
2943 | ||
a5c2d2a7 JG |
2944 | static |
2945 | int _lttng_register_trigger(struct lttng_trigger *trigger, const char *name, | |
2946 | bool generate_name) | |
a58c490f JG |
2947 | { |
2948 | int ret; | |
99608320 JR |
2949 | struct lttcomm_session_msg lsm = { |
2950 | .cmd_type = LTTNG_REGISTER_TRIGGER, | |
2951 | }; | |
4bd69c5f | 2952 | lsm.u.trigger.is_trigger_anonymous = !name && !generate_name; |
99608320 JR |
2953 | struct lttcomm_session_msg *message_lsm; |
2954 | struct lttng_payload message; | |
2955 | struct lttng_payload reply; | |
242388e4 | 2956 | struct lttng_trigger *reply_trigger = NULL; |
9124c630 | 2957 | enum lttng_domain_type domain_type; |
64eafdf6 JR |
2958 | const struct lttng_credentials user_creds = { |
2959 | .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()), | |
2960 | .gid = LTTNG_OPTIONAL_INIT_UNSET, | |
2961 | }; | |
a5c2d2a7 JG |
2962 | const char *unused_trigger_name = NULL; |
2963 | enum lttng_trigger_status trigger_status; | |
99608320 JR |
2964 | |
2965 | lttng_payload_init(&message); | |
2966 | lttng_payload_init(&reply); | |
a58c490f JG |
2967 | |
2968 | if (!trigger) { | |
2969 | ret = -LTTNG_ERR_INVALID; | |
2970 | goto end; | |
2971 | } | |
2972 | ||
a5c2d2a7 JG |
2973 | trigger_status = lttng_trigger_get_name(trigger, &unused_trigger_name); |
2974 | if (trigger_status != LTTNG_TRIGGER_STATUS_UNSET) { | |
2975 | /* Re-using already registered trigger. */ | |
2976 | ret = -LTTNG_ERR_INVALID; | |
2977 | goto end; | |
2978 | } | |
2979 | ||
2980 | if (name) { | |
2981 | trigger_status = lttng_trigger_set_name(trigger, name); | |
2982 | if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { | |
2983 | ret = -LTTNG_ERR_NOMEM; | |
2984 | goto end; | |
2985 | } | |
2986 | } | |
2987 | ||
64eafdf6 JR |
2988 | if (!trigger->creds.uid.is_set) { |
2989 | /* Use the client's credentials as the trigger credentials. */ | |
2990 | lttng_trigger_set_credentials(trigger, &user_creds); | |
2991 | } else { | |
2992 | /* | |
2993 | * Validate that either the current trigger credentials and the | |
2994 | * client credentials are identical or that the current user is | |
2995 | * root. The root user can register, unregister triggers for | |
2996 | * himself and other users. | |
2997 | * | |
2998 | * This check is also present on the sessiond side, using the | |
2999 | * credentials passed on the socket. These check are all | |
3000 | * "safety" checks. | |
3001 | */ | |
3002 | const struct lttng_credentials *trigger_creds = | |
3003 | lttng_trigger_get_credentials(trigger); | |
3004 | ||
3005 | if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) { | |
3006 | if (lttng_credentials_get_uid(&user_creds) != 0) { | |
3007 | ret = -LTTNG_ERR_EPERM; | |
a5c2d2a7 | 3008 | goto end_unset_name; |
64eafdf6 JR |
3009 | } |
3010 | } | |
3011 | } | |
3012 | ||
a58c490f | 3013 | if (!lttng_trigger_validate(trigger)) { |
eac4828d | 3014 | ret = -LTTNG_ERR_INVALID_TRIGGER; |
a5c2d2a7 | 3015 | goto end_unset_name; |
a58c490f JG |
3016 | } |
3017 | ||
9124c630 JR |
3018 | domain_type = lttng_trigger_get_underlying_domain_type_restriction( |
3019 | trigger); | |
3020 | ||
3021 | lsm.domain.type = domain_type; | |
3022 | ||
97285430 JG |
3023 | ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm)); |
3024 | if (ret) { | |
3025 | ret = -LTTNG_ERR_NOMEM; | |
a5c2d2a7 | 3026 | goto end_unset_name; |
97285430 | 3027 | } |
99608320 | 3028 | |
99608320 | 3029 | ret = lttng_trigger_serialize(trigger, &message); |
3647288f | 3030 | if (ret < 0) { |
a58c490f | 3031 | ret = -LTTNG_ERR_UNK; |
a5c2d2a7 | 3032 | goto end_unset_name; |
a58c490f JG |
3033 | } |
3034 | ||
b22f4f54 JG |
3035 | /* |
3036 | * This is needed to populate the trigger object size for the command | |
3037 | * header. | |
3038 | */ | |
3039 | message_lsm = (struct lttcomm_session_msg *) message.buffer.data; | |
3040 | ||
99608320 JR |
3041 | message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm); |
3042 | ||
3043 | { | |
3044 | struct lttng_payload_view message_view = | |
3045 | lttng_payload_view_from_payload( | |
3046 | &message, 0, -1); | |
3047 | ||
3048 | message_lsm->fd_count = lttng_payload_view_get_fd_handle_count( | |
3049 | &message_view); | |
3050 | ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); | |
3051 | if (ret < 0) { | |
a5c2d2a7 | 3052 | goto end_unset_name; |
99608320 JR |
3053 | } |
3054 | } | |
3055 | ||
242388e4 JR |
3056 | { |
3057 | struct lttng_payload_view reply_view = | |
3058 | lttng_payload_view_from_payload( | |
3059 | &reply, 0, reply.buffer.size); | |
3060 | ||
3061 | ret = lttng_trigger_create_from_payload( | |
3062 | &reply_view, &reply_trigger); | |
3063 | if (ret < 0) { | |
a5c2d2a7 JG |
3064 | ret = -LTTNG_ERR_INVALID_PROTOCOL; |
3065 | goto end_unset_name; | |
242388e4 JR |
3066 | } |
3067 | } | |
3068 | ||
a5c2d2a7 JG |
3069 | if (name || generate_name) { |
3070 | ret = lttng_trigger_assign_name(trigger, reply_trigger); | |
3071 | if (ret < 0) { | |
3072 | ret = -LTTNG_ERR_NOMEM; | |
3073 | goto end; | |
3074 | } | |
242388e4 JR |
3075 | } |
3076 | ||
99608320 | 3077 | ret = 0; |
a5c2d2a7 JG |
3078 | goto end; |
3079 | ||
3080 | end_unset_name: | |
3081 | trigger_status = lttng_trigger_set_name(trigger, NULL); | |
3082 | if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { | |
3083 | ret = -LTTNG_ERR_UNK; | |
3084 | } | |
a58c490f | 3085 | end: |
99608320 JR |
3086 | lttng_payload_reset(&message); |
3087 | lttng_payload_reset(&reply); | |
242388e4 | 3088 | lttng_trigger_destroy(reply_trigger); |
a58c490f JG |
3089 | return ret; |
3090 | } | |
3091 | ||
a5c2d2a7 JG |
3092 | int lttng_register_trigger(struct lttng_trigger *trigger) |
3093 | { | |
3094 | /* Register an anonymous trigger. */ | |
3095 | return _lttng_register_trigger(trigger, NULL, false); | |
3096 | } | |
3097 | ||
3098 | enum lttng_error_code lttng_register_trigger_with_name( | |
3099 | struct lttng_trigger *trigger, const char *name) | |
3100 | { | |
3101 | const int ret = _lttng_register_trigger(trigger, name, false); | |
3102 | ||
3103 | return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret; | |
3104 | } | |
3105 | ||
3106 | enum lttng_error_code lttng_register_trigger_with_automatic_name( | |
3107 | struct lttng_trigger *trigger) | |
3108 | { | |
4bd69c5f | 3109 | const int ret = _lttng_register_trigger(trigger, nullptr, true); |
a5c2d2a7 JG |
3110 | |
3111 | return ret == 0 ? LTTNG_OK : (enum lttng_error_code) -ret; | |
3112 | } | |
3113 | ||
b99a0cb3 JG |
3114 | enum lttng_error_code lttng_error_query_execute( |
3115 | const struct lttng_error_query *query, | |
3116 | const struct lttng_endpoint *endpoint, | |
3117 | struct lttng_error_query_results **results) | |
3118 | { | |
3119 | int ret; | |
3120 | enum lttng_error_code ret_code; | |
3121 | struct lttcomm_session_msg lsm = { | |
3122 | .cmd_type = LTTNG_EXECUTE_ERROR_QUERY, | |
3123 | }; | |
3124 | struct lttng_payload message; | |
3125 | struct lttng_payload reply; | |
3126 | struct lttcomm_session_msg *message_lsm; | |
3127 | ||
3128 | lttng_payload_init(&message); | |
3129 | lttng_payload_init(&reply); | |
3130 | ||
3131 | if (!query || !results) { | |
3132 | ret_code = LTTNG_ERR_INVALID; | |
3133 | goto end; | |
3134 | } | |
3135 | ||
3136 | if (endpoint != lttng_session_daemon_command_endpoint) { | |
3137 | ret_code = LTTNG_ERR_INVALID_ERROR_QUERY_TARGET; | |
3138 | goto end; | |
3139 | } | |
3140 | ||
3141 | ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm)); | |
3142 | if (ret) { | |
3143 | ret_code = LTTNG_ERR_NOMEM; | |
3144 | goto end; | |
3145 | } | |
3146 | ||
3147 | ret = lttng_error_query_serialize(query, &message); | |
3148 | if (ret) { | |
3149 | ret_code = LTTNG_ERR_UNK; | |
3150 | goto end; | |
3151 | } | |
3152 | ||
3153 | message_lsm = (struct lttcomm_session_msg *) message.buffer.data; | |
3154 | message_lsm->u.error_query.length = | |
3155 | (uint32_t) message.buffer.size - sizeof(lsm); | |
3156 | ||
3157 | { | |
3158 | struct lttng_payload_view message_view = | |
3159 | lttng_payload_view_from_payload( | |
3160 | &message, 0, -1); | |
3161 | ||
3162 | message_lsm->fd_count = lttng_payload_view_get_fd_handle_count( | |
3163 | &message_view); | |
3164 | ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); | |
3165 | if (ret < 0) { | |
4bd69c5f | 3166 | ret_code =(lttng_error_code) -ret; |
b99a0cb3 JG |
3167 | goto end; |
3168 | } | |
3169 | } | |
3170 | ||
3171 | { | |
3172 | ssize_t reply_create_ret; | |
3173 | struct lttng_payload_view reply_view = | |
3174 | lttng_payload_view_from_payload( | |
3175 | &reply, 0, reply.buffer.size); | |
3176 | ||
3177 | reply_create_ret = lttng_error_query_results_create_from_payload( | |
3178 | &reply_view, results); | |
3179 | if (reply_create_ret < 0) { | |
3180 | ret_code = LTTNG_ERR_INVALID_PROTOCOL; | |
3181 | goto end; | |
3182 | } | |
3183 | } | |
3184 | ||
3185 | ret_code = LTTNG_OK; | |
3186 | end: | |
3187 | lttng_payload_reset(&message); | |
3188 | lttng_payload_reset(&reply); | |
3189 | return ret_code; | |
3190 | } | |
3191 | ||
b61776fb | 3192 | int lttng_unregister_trigger(const struct lttng_trigger *trigger) |
a58c490f JG |
3193 | { |
3194 | int ret; | |
3195 | struct lttcomm_session_msg lsm; | |
99608320 JR |
3196 | struct lttcomm_session_msg *message_lsm; |
3197 | struct lttng_payload message; | |
3198 | struct lttng_payload reply; | |
b61776fb | 3199 | struct lttng_trigger *copy = NULL; |
64eafdf6 JR |
3200 | const struct lttng_credentials user_creds = { |
3201 | .uid = LTTNG_OPTIONAL_INIT_VALUE(geteuid()), | |
3202 | .gid = LTTNG_OPTIONAL_INIT_UNSET, | |
3203 | }; | |
99608320 JR |
3204 | |
3205 | lttng_payload_init(&message); | |
3206 | lttng_payload_init(&reply); | |
a58c490f JG |
3207 | |
3208 | if (!trigger) { | |
3209 | ret = -LTTNG_ERR_INVALID; | |
3210 | goto end; | |
3211 | } | |
3212 | ||
b61776fb SM |
3213 | copy = lttng_trigger_copy(trigger); |
3214 | if (!copy) { | |
3215 | ret = -LTTNG_ERR_UNK; | |
3216 | goto end; | |
3217 | } | |
3218 | ||
3219 | if (!copy->creds.uid.is_set) { | |
3220 | /* Use the client credentials as the trigger credentials */ | |
3221 | lttng_trigger_set_credentials(copy, &user_creds); | |
64eafdf6 JR |
3222 | } else { |
3223 | /* | |
3224 | * Validate that either the current trigger credentials and the | |
3225 | * client credentials are identical or that the current user is | |
3226 | * root. The root user can register, unregister triggers for | |
3227 | * himself and other users. | |
3228 | * | |
3229 | * This check is also present on the sessiond side, using the | |
3230 | * credentials passed on the socket. These check are all | |
3231 | * "safety" checks. | |
3232 | */ | |
3233 | const struct lttng_credentials *trigger_creds = | |
b61776fb | 3234 | lttng_trigger_get_credentials(copy); |
64eafdf6 JR |
3235 | if (!lttng_credentials_is_equal_uid(trigger_creds, &user_creds)) { |
3236 | if (lttng_credentials_get_uid(&user_creds) != 0) { | |
3237 | ret = -LTTNG_ERR_EPERM; | |
3238 | goto end; | |
3239 | } | |
3240 | } | |
3241 | } | |
3242 | ||
b61776fb | 3243 | if (!lttng_trigger_validate(copy)) { |
3647288f | 3244 | ret = -LTTNG_ERR_INVALID_TRIGGER; |
a58c490f JG |
3245 | goto end; |
3246 | } | |
3247 | ||
99608320 JR |
3248 | memset(&lsm, 0, sizeof(lsm)); |
3249 | lsm.cmd_type = LTTNG_UNREGISTER_TRIGGER; | |
3250 | ||
97285430 JG |
3251 | ret = lttng_dynamic_buffer_append(&message.buffer, &lsm, sizeof(lsm)); |
3252 | if (ret) { | |
3253 | ret = -LTTNG_ERR_NOMEM; | |
3254 | goto end; | |
3255 | } | |
99608320 | 3256 | |
b61776fb | 3257 | ret = lttng_trigger_serialize(copy, &message); |
3647288f | 3258 | if (ret < 0) { |
a58c490f JG |
3259 | ret = -LTTNG_ERR_UNK; |
3260 | goto end; | |
3261 | } | |
3262 | ||
b0a79813 FD |
3263 | /* |
3264 | * This is needed to populate the trigger object size for the command | |
3265 | * header and number of fds sent. | |
3266 | */ | |
3267 | message_lsm = (struct lttcomm_session_msg *) message.buffer.data; | |
3268 | ||
99608320 JR |
3269 | message_lsm->u.trigger.length = (uint32_t) message.buffer.size - sizeof(lsm); |
3270 | ||
3271 | { | |
3272 | struct lttng_payload_view message_view = | |
3273 | lttng_payload_view_from_payload( | |
3274 | &message, 0, -1); | |
3275 | ||
3276 | /* | |
3277 | * Update the message header with the number of fd that will be | |
3278 | * sent. | |
3279 | */ | |
3280 | message_lsm->fd_count = lttng_payload_view_get_fd_handle_count( | |
3281 | &message_view); | |
3282 | ||
3283 | ret = lttng_ctl_ask_sessiond_payload(&message_view, &reply); | |
3284 | if (ret < 0) { | |
3285 | goto end; | |
3286 | } | |
3287 | } | |
3288 | ||
3289 | ret = 0; | |
a58c490f | 3290 | end: |
b61776fb | 3291 | lttng_trigger_destroy(copy); |
99608320 JR |
3292 | lttng_payload_reset(&message); |
3293 | lttng_payload_reset(&reply); | |
a58c490f JG |
3294 | return ret; |
3295 | } | |
3296 | ||
fbc9f37d JR |
3297 | /* |
3298 | * Ask the session daemon for all registered triggers for the current user. | |
3299 | * | |
3300 | * Allocates and return an lttng_triggers set. | |
3301 | * On error, returns a suitable lttng_error_code. | |
3302 | */ | |
3303 | enum lttng_error_code lttng_list_triggers(struct lttng_triggers **triggers) | |
3304 | { | |
3305 | int ret; | |
3306 | enum lttng_error_code ret_code = LTTNG_OK; | |
3307 | struct lttcomm_session_msg lsm = { .cmd_type = LTTNG_LIST_TRIGGERS }; | |
3308 | struct lttng_triggers *local_triggers = NULL; | |
3309 | struct lttng_payload reply; | |
3310 | struct lttng_payload_view lsm_view = | |
3311 | lttng_payload_view_init_from_buffer( | |
3312 | (const char *) &lsm, 0, sizeof(lsm)); | |
3313 | ||
3314 | lttng_payload_init(&reply); | |
3315 | ||
3316 | ret = lttng_ctl_ask_sessiond_payload(&lsm_view, &reply); | |
3317 | if (ret < 0) { | |
3318 | ret_code = (enum lttng_error_code) -ret; | |
3319 | goto end; | |
3320 | } | |
3321 | ||
3322 | { | |
3323 | struct lttng_payload_view reply_view = | |
3324 | lttng_payload_view_from_payload( | |
3325 | &reply, 0, reply.buffer.size); | |
3326 | ||
3327 | ret = lttng_triggers_create_from_payload( | |
3328 | &reply_view, &local_triggers); | |
3329 | if (ret < 0) { | |
3330 | ret_code = LTTNG_ERR_FATAL; | |
3331 | goto end; | |
3332 | } | |
3333 | } | |
3334 | ||
3335 | *triggers = local_triggers; | |
3336 | local_triggers = NULL; | |
3337 | end: | |
3338 | lttng_payload_reset(&reply); | |
3339 | lttng_triggers_destroy(local_triggers); | |
3340 | return ret_code; | |
3341 | } | |
3342 | ||
fac6795d | 3343 | /* |
9ae110e2 | 3344 | * lib constructor. |
fac6795d | 3345 | */ |
b2b89e8a | 3346 | static void __attribute__((constructor)) init(void) |
fac6795d DG |
3347 | { |
3348 | /* Set default session group */ | |
bbccc3d2 | 3349 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
fac6795d | 3350 | } |
49cca668 DG |
3351 | |
3352 | /* | |
9ae110e2 | 3353 | * lib destructor. |
49cca668 | 3354 | */ |
b2b89e8a | 3355 | static void __attribute__((destructor)) lttng_ctl_exit(void) |
49cca668 DG |
3356 | { |
3357 | free(tracing_group); | |
3358 | } |