Commit | Line | Data |
---|---|---|
826d496d | 1 | /* |
82a3637f DG |
2 | * liblttngctl.c |
3 | * | |
4 | * Linux Trace Toolkit Control Library | |
5 | * | |
826d496d | 6 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
fac6795d | 7 | * |
d14d33bf AM |
8 | * This library is free software; you can redistribute it and/or modify it |
9 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
10 | * as published by the Free Software Foundation. | |
82a3637f DG |
11 | * |
12 | * This library is distributed in the hope that it will be useful, | |
fac6795d | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
82a3637f DG |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | * Lesser General Public License for more details. | |
16 | * | |
d14d33bf AM |
17 | * You should have received a copy of the GNU Lesser General Public License |
18 | * along with this library; if not, write to the Free Software Foundation, | |
19 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
fac6795d DG |
20 | */ |
21 | ||
22 | #define _GNU_SOURCE | |
44a5e5eb | 23 | #include <assert.h> |
fac6795d | 24 | #include <grp.h> |
1e307fab | 25 | #include <errno.h> |
fac6795d DG |
26 | #include <stdio.h> |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <unistd.h> | |
30 | ||
990570ed DG |
31 | #include <common/common.h> |
32 | #include <common/defaults.h> | |
db758600 | 33 | #include <common/sessiond-comm/sessiond-comm.h> |
a4b92340 | 34 | #include <common/uri.h> |
feb0f3e5 | 35 | #include <common/utils.h> |
1e307fab | 36 | #include <lttng/lttng.h> |
fac6795d | 37 | |
d00c599e DG |
38 | #include "filter/filter-ast.h" |
39 | #include "filter/filter-parser.h" | |
40 | #include "filter/filter-bytecode.h" | |
41 | #include "filter/memstream.h" | |
cac3069d | 42 | #include "lttng-ctl-helper.h" |
53a80697 MD |
43 | |
44 | #ifdef DEBUG | |
d00c599e | 45 | static const int print_xml = 1; |
53a80697 MD |
46 | #define dbg_printf(fmt, args...) \ |
47 | printf("[debug liblttng-ctl] " fmt, ## args) | |
48 | #else | |
d00c599e | 49 | static const int print_xml = 0; |
53a80697 MD |
50 | #define dbg_printf(fmt, args...) \ |
51 | do { \ | |
52 | /* do nothing but check printf format */ \ | |
53 | if (0) \ | |
54 | printf("[debug liblttnctl] " fmt, ## args); \ | |
55 | } while (0) | |
56 | #endif | |
57 | ||
58 | ||
fac6795d DG |
59 | /* Socket to session daemon for communication */ |
60 | static int sessiond_socket; | |
61 | static char sessiond_sock_path[PATH_MAX]; | |
44a5e5eb | 62 | static char health_sock_path[PATH_MAX]; |
fac6795d | 63 | |
fac6795d DG |
64 | /* Variables */ |
65 | static char *tracing_group; | |
66 | static int connected; | |
67 | ||
97e19046 DG |
68 | /* Global */ |
69 | ||
70 | /* | |
71 | * Those two variables are used by error.h to silent or control the verbosity of | |
72 | * error message. They are global to the library so application linking with it | |
73 | * are able to compile correctly and also control verbosity of the library. | |
97e19046 DG |
74 | */ |
75 | int lttng_opt_quiet; | |
76 | int lttng_opt_verbose; | |
77 | ||
4f50c803 DG |
78 | /* |
79 | * Compare two URL destination. | |
80 | * | |
81 | * Return 0 is equal else is not equal. | |
82 | */ | |
83 | static int compare_destination(struct lttng_uri *ctrl, struct lttng_uri *data) | |
84 | { | |
85 | int ret; | |
86 | ||
87 | assert(ctrl); | |
88 | assert(data); | |
89 | ||
90 | switch (ctrl->dtype) { | |
91 | case LTTNG_DST_IPV4: | |
92 | ret = strncmp(ctrl->dst.ipv4, data->dst.ipv4, sizeof(ctrl->dst.ipv4)); | |
93 | break; | |
94 | case LTTNG_DST_IPV6: | |
95 | ret = strncmp(ctrl->dst.ipv6, data->dst.ipv6, sizeof(ctrl->dst.ipv6)); | |
96 | break; | |
97 | default: | |
98 | ret = -1; | |
99 | break; | |
100 | } | |
101 | ||
102 | return ret; | |
103 | } | |
104 | ||
a4b92340 DG |
105 | static void set_default_url_attr(struct lttng_uri *uri, |
106 | enum lttng_stream_type stype) | |
107 | { | |
108 | uri->stype = stype; | |
109 | if (uri->dtype != LTTNG_DST_PATH && uri->port == 0) { | |
110 | uri->port = (stype == LTTNG_STREAM_CONTROL) ? | |
111 | DEFAULT_NETWORK_CONTROL_PORT : DEFAULT_NETWORK_DATA_PORT; | |
112 | } | |
113 | } | |
114 | ||
115 | /* | |
116 | * Parse a string URL and creates URI(s) returning the size of the populated | |
117 | * array. | |
118 | */ | |
119 | static ssize_t parse_str_urls_to_uri(const char *ctrl_url, const char *data_url, | |
120 | struct lttng_uri **uris) | |
121 | { | |
a4b92340 DG |
122 | unsigned int equal = 1, idx = 0; |
123 | /* Add the "file://" size to the URL maximum size */ | |
124 | char url[PATH_MAX + 7]; | |
125 | ssize_t size_ctrl = 0, size_data = 0, size; | |
126 | struct lttng_uri *ctrl_uris = NULL, *data_uris = NULL; | |
127 | struct lttng_uri *tmp_uris = NULL; | |
128 | ||
129 | /* No URL(s) is allowed. This means that the consumer will be disabled. */ | |
130 | if (ctrl_url == NULL && data_url == NULL) { | |
131 | return 0; | |
132 | } | |
133 | ||
134 | /* Check if URLs are equal and if so, only use the control URL */ | |
135 | if (ctrl_url && data_url) { | |
136 | equal = !strcmp(ctrl_url, data_url); | |
137 | } | |
138 | ||
139 | /* | |
140 | * Since we allow the str_url to be a full local filesystem path, we are | |
141 | * going to create a valid file:// URL if it's the case. | |
142 | * | |
143 | * Check if first character is a '/' or else reject the URL. | |
144 | */ | |
145 | if (ctrl_url && ctrl_url[0] == '/') { | |
c617c0c6 MD |
146 | int ret; |
147 | ||
a4b92340 DG |
148 | ret = snprintf(url, sizeof(url), "file://%s", ctrl_url); |
149 | if (ret < 0) { | |
150 | PERROR("snprintf file url"); | |
151 | goto parse_error; | |
152 | } | |
153 | ctrl_url = url; | |
154 | } | |
155 | ||
156 | /* Parse the control URL if there is one */ | |
157 | if (ctrl_url) { | |
158 | size_ctrl = uri_parse(ctrl_url, &ctrl_uris); | |
159 | if (size_ctrl < 1) { | |
160 | ERR("Unable to parse the URL %s", ctrl_url); | |
161 | goto parse_error; | |
162 | } | |
163 | ||
164 | /* At this point, we know there is at least one URI in the array */ | |
165 | set_default_url_attr(&ctrl_uris[0], LTTNG_STREAM_CONTROL); | |
166 | ||
167 | if (ctrl_uris[0].dtype == LTTNG_DST_PATH && data_url) { | |
168 | ERR("Can not have a data URL when destination is file://"); | |
169 | goto error; | |
170 | } | |
171 | ||
172 | /* URL are not equal but the control URL uses a net:// protocol */ | |
173 | if (size_ctrl == 2) { | |
174 | if (!equal) { | |
175 | ERR("Control URL uses the net:// protocol and the data URL is " | |
176 | "different. Not allowed."); | |
177 | goto error; | |
178 | } else { | |
179 | set_default_url_attr(&ctrl_uris[1], LTTNG_STREAM_DATA); | |
180 | /* | |
181 | * The data_url and ctrl_url are equal and the ctrl_url | |
182 | * contains a net:// protocol so we just skip the data part. | |
183 | */ | |
184 | data_url = NULL; | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
189 | if (data_url) { | |
4f50c803 DG |
190 | int ret; |
191 | ||
a4b92340 DG |
192 | /* We have to parse the data URL in this case */ |
193 | size_data = uri_parse(data_url, &data_uris); | |
194 | if (size_data < 1) { | |
195 | ERR("Unable to parse the URL %s", data_url); | |
196 | goto error; | |
197 | } else if (size_data == 2) { | |
198 | ERR("Data URL can not be set with the net[4|6]:// protocol"); | |
199 | goto error; | |
200 | } | |
201 | ||
202 | set_default_url_attr(&data_uris[0], LTTNG_STREAM_DATA); | |
4f50c803 DG |
203 | |
204 | ret = compare_destination(&ctrl_uris[0], &data_uris[0]); | |
205 | if (ret != 0) { | |
206 | ERR("Control and data destination mismatch"); | |
207 | goto error; | |
208 | } | |
a4b92340 DG |
209 | } |
210 | ||
211 | /* Compute total size */ | |
212 | size = size_ctrl + size_data; | |
213 | ||
214 | tmp_uris = zmalloc(sizeof(struct lttng_uri) * size); | |
215 | if (tmp_uris == NULL) { | |
216 | PERROR("zmalloc uris"); | |
217 | goto error; | |
218 | } | |
219 | ||
220 | if (ctrl_uris) { | |
221 | /* It's possible the control URIs array contains more than one URI */ | |
222 | memcpy(tmp_uris, ctrl_uris, sizeof(struct lttng_uri) * size_ctrl); | |
223 | ++idx; | |
3dd05a85 | 224 | free(ctrl_uris); |
a4b92340 DG |
225 | } |
226 | ||
227 | if (data_uris) { | |
228 | memcpy(&tmp_uris[idx], data_uris, sizeof(struct lttng_uri)); | |
3dd05a85 | 229 | free(data_uris); |
a4b92340 DG |
230 | } |
231 | ||
232 | *uris = tmp_uris; | |
233 | ||
234 | return size; | |
235 | ||
236 | error: | |
237 | free(ctrl_uris); | |
238 | free(data_uris); | |
239 | free(tmp_uris); | |
240 | parse_error: | |
241 | return -1; | |
242 | } | |
243 | ||
99497cd0 MD |
244 | /* |
245 | * Copy string from src to dst and enforce null terminated byte. | |
246 | */ | |
cac3069d DG |
247 | LTTNG_HIDDEN |
248 | void lttng_ctl_copy_string(char *dst, const char *src, size_t len) | |
99497cd0 | 249 | { |
e7d6716d | 250 | if (src && dst) { |
99497cd0 MD |
251 | strncpy(dst, src, len); |
252 | /* Enforce the NULL terminated byte */ | |
253 | dst[len - 1] = '\0'; | |
cd80958d DG |
254 | } else if (dst) { |
255 | dst[0] = '\0'; | |
99497cd0 MD |
256 | } |
257 | } | |
258 | ||
fac6795d | 259 | /* |
cd80958d | 260 | * Copy domain to lttcomm_session_msg domain. |
fac6795d | 261 | * |
cd80958d DG |
262 | * If domain is unknown, default domain will be the kernel. |
263 | */ | |
cac3069d DG |
264 | LTTNG_HIDDEN |
265 | void lttng_ctl_copy_lttng_domain(struct lttng_domain *dst, | |
266 | struct lttng_domain *src) | |
cd80958d DG |
267 | { |
268 | if (src && dst) { | |
269 | switch (src->type) { | |
00e2e675 DG |
270 | case LTTNG_DOMAIN_KERNEL: |
271 | case LTTNG_DOMAIN_UST: | |
00e2e675 DG |
272 | memcpy(dst, src, sizeof(struct lttng_domain)); |
273 | break; | |
274 | default: | |
275 | memset(dst, 0, sizeof(struct lttng_domain)); | |
00e2e675 | 276 | break; |
cd80958d DG |
277 | } |
278 | } | |
279 | } | |
280 | ||
281 | /* | |
282 | * Send lttcomm_session_msg to the session daemon. | |
fac6795d | 283 | * |
1c8d13c8 TD |
284 | * On success, returns the number of bytes sent (>=0) |
285 | * On error, returns -1 | |
fac6795d | 286 | */ |
cd80958d | 287 | static int send_session_msg(struct lttcomm_session_msg *lsm) |
fac6795d DG |
288 | { |
289 | int ret; | |
290 | ||
291 | if (!connected) { | |
2f70b271 | 292 | ret = -LTTNG_ERR_NO_SESSIOND; |
e065084a | 293 | goto end; |
fac6795d DG |
294 | } |
295 | ||
a4b92340 DG |
296 | DBG("LSM cmd type : %d", lsm->cmd_type); |
297 | ||
be040666 | 298 | ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm, |
cd80958d | 299 | sizeof(struct lttcomm_session_msg)); |
2f70b271 DG |
300 | if (ret < 0) { |
301 | ret = -LTTNG_ERR_FATAL; | |
302 | } | |
e065084a DG |
303 | |
304 | end: | |
305 | return ret; | |
306 | } | |
307 | ||
53a80697 MD |
308 | /* |
309 | * Send var len data to the session daemon. | |
310 | * | |
311 | * On success, returns the number of bytes sent (>=0) | |
312 | * On error, returns -1 | |
313 | */ | |
314 | static int send_session_varlen(void *data, size_t len) | |
315 | { | |
316 | int ret; | |
317 | ||
318 | if (!connected) { | |
2f70b271 | 319 | ret = -LTTNG_ERR_NO_SESSIOND; |
53a80697 MD |
320 | goto end; |
321 | } | |
a4b92340 | 322 | |
53a80697 MD |
323 | if (!data || !len) { |
324 | ret = 0; | |
325 | goto end; | |
326 | } | |
327 | ||
328 | ret = lttcomm_send_unix_sock(sessiond_socket, data, len); | |
2f70b271 DG |
329 | if (ret < 0) { |
330 | ret = -LTTNG_ERR_FATAL; | |
331 | } | |
53a80697 MD |
332 | |
333 | end: | |
334 | return ret; | |
335 | } | |
336 | ||
e065084a | 337 | /* |
cd80958d | 338 | * Receive data from the sessiond socket. |
e065084a | 339 | * |
1c8d13c8 TD |
340 | * On success, returns the number of bytes received (>=0) |
341 | * On error, returns -1 (recvmsg() error) or -ENOTCONN | |
e065084a | 342 | */ |
ca95a216 | 343 | static int recv_data_sessiond(void *buf, size_t len) |
e065084a DG |
344 | { |
345 | int ret; | |
346 | ||
347 | if (!connected) { | |
2f70b271 | 348 | ret = -LTTNG_ERR_NO_SESSIOND; |
e065084a | 349 | goto end; |
fac6795d DG |
350 | } |
351 | ||
ca95a216 | 352 | ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len); |
2f70b271 DG |
353 | if (ret < 0) { |
354 | ret = -LTTNG_ERR_FATAL; | |
355 | } | |
fac6795d | 356 | |
e065084a | 357 | end: |
fac6795d DG |
358 | return ret; |
359 | } | |
360 | ||
361 | /* | |
1c8d13c8 | 362 | * Check if we are in the specified group. |
65beb5ff | 363 | * |
2269e89e | 364 | * If yes return 1, else return -1. |
947308c4 DG |
365 | */ |
366 | static int check_tracing_group(const char *grp_name) | |
367 | { | |
368 | struct group *grp_tracing; /* no free(). See getgrnam(3) */ | |
369 | gid_t *grp_list; | |
370 | int grp_list_size, grp_id, i; | |
371 | int ret = -1; | |
372 | ||
373 | /* Get GID of group 'tracing' */ | |
374 | grp_tracing = getgrnam(grp_name); | |
b4d8603b MD |
375 | if (!grp_tracing) { |
376 | /* If grp_tracing is NULL, the group does not exist. */ | |
947308c4 DG |
377 | goto end; |
378 | } | |
379 | ||
380 | /* Get number of supplementary group IDs */ | |
381 | grp_list_size = getgroups(0, NULL); | |
382 | if (grp_list_size < 0) { | |
383 | perror("getgroups"); | |
384 | goto end; | |
385 | } | |
386 | ||
387 | /* Alloc group list of the right size */ | |
388 | grp_list = malloc(grp_list_size * sizeof(gid_t)); | |
00795392 | 389 | if (!grp_list) { |
1c8d13c8 | 390 | perror("malloc"); |
00795392 MD |
391 | goto end; |
392 | } | |
947308c4 | 393 | grp_id = getgroups(grp_list_size, grp_list); |
1c8d13c8 | 394 | if (grp_id < 0) { |
947308c4 DG |
395 | perror("getgroups"); |
396 | goto free_list; | |
397 | } | |
398 | ||
399 | for (i = 0; i < grp_list_size; i++) { | |
400 | if (grp_list[i] == grp_tracing->gr_gid) { | |
2269e89e | 401 | ret = 1; |
947308c4 DG |
402 | break; |
403 | } | |
404 | } | |
405 | ||
406 | free_list: | |
407 | free(grp_list); | |
408 | ||
409 | end: | |
410 | return ret; | |
411 | } | |
412 | ||
413 | /* | |
2269e89e DG |
414 | * Try connect to session daemon with sock_path. |
415 | * | |
416 | * Return 0 on success, else -1 | |
417 | */ | |
418 | static int try_connect_sessiond(const char *sock_path) | |
419 | { | |
420 | int ret; | |
421 | ||
422 | /* If socket exist, we check if the daemon listens for connect. */ | |
423 | ret = access(sock_path, F_OK); | |
424 | if (ret < 0) { | |
425 | /* Not alive */ | |
2f70b271 | 426 | goto error; |
2269e89e DG |
427 | } |
428 | ||
429 | ret = lttcomm_connect_unix_sock(sock_path); | |
430 | if (ret < 0) { | |
431 | /* Not alive */ | |
2f70b271 | 432 | goto error; |
2269e89e DG |
433 | } |
434 | ||
435 | ret = lttcomm_close_unix_sock(ret); | |
436 | if (ret < 0) { | |
437 | perror("lttcomm_close_unix_sock"); | |
438 | } | |
439 | ||
440 | return 0; | |
2f70b271 DG |
441 | |
442 | error: | |
443 | return -1; | |
2269e89e DG |
444 | } |
445 | ||
446 | /* | |
2f70b271 DG |
447 | * Set sessiond socket path by putting it in the global sessiond_sock_path |
448 | * variable. | |
449 | * | |
450 | * Returns 0 on success, negative value on failure (the sessiond socket path | |
451 | * is somehow too long or ENOMEM). | |
947308c4 DG |
452 | */ |
453 | static int set_session_daemon_path(void) | |
454 | { | |
2269e89e DG |
455 | int in_tgroup = 0; /* In tracing group */ |
456 | uid_t uid; | |
457 | ||
458 | uid = getuid(); | |
947308c4 | 459 | |
2269e89e DG |
460 | if (uid != 0) { |
461 | /* Are we in the tracing group ? */ | |
462 | in_tgroup = check_tracing_group(tracing_group); | |
463 | } | |
464 | ||
08a9c49f | 465 | if ((uid == 0) || in_tgroup) { |
cac3069d DG |
466 | lttng_ctl_copy_string(sessiond_sock_path, |
467 | DEFAULT_GLOBAL_CLIENT_UNIX_SOCK, sizeof(sessiond_sock_path)); | |
08a9c49f | 468 | } |
2269e89e | 469 | |
08a9c49f | 470 | if (uid != 0) { |
c617c0c6 MD |
471 | int ret; |
472 | ||
08a9c49f TD |
473 | if (in_tgroup) { |
474 | /* Tracing group */ | |
475 | ret = try_connect_sessiond(sessiond_sock_path); | |
476 | if (ret >= 0) { | |
477 | goto end; | |
2269e89e | 478 | } |
08a9c49f | 479 | /* Global session daemon not available... */ |
2269e89e | 480 | } |
08a9c49f TD |
481 | /* ...or not in tracing group (and not root), default */ |
482 | ||
483 | /* | |
484 | * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small; | |
485 | * With GNU C >= 2.1, snprintf returns the required size (excluding closing null) | |
486 | */ | |
487 | ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), | |
feb0f3e5 | 488 | DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir()); |
08a9c49f | 489 | if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) { |
2f70b271 | 490 | goto error; |
947308c4 | 491 | } |
947308c4 | 492 | } |
08a9c49f | 493 | end: |
947308c4 | 494 | return 0; |
2f70b271 DG |
495 | |
496 | error: | |
497 | return -1; | |
947308c4 DG |
498 | } |
499 | ||
65beb5ff DG |
500 | /* |
501 | * Connect to the LTTng session daemon. | |
502 | * | |
503 | * On success, return 0. On error, return -1. | |
504 | */ | |
505 | static int connect_sessiond(void) | |
506 | { | |
507 | int ret; | |
508 | ||
509 | ret = set_session_daemon_path(); | |
510 | if (ret < 0) { | |
2f70b271 | 511 | goto error; |
65beb5ff DG |
512 | } |
513 | ||
514 | /* Connect to the sesssion daemon */ | |
515 | ret = lttcomm_connect_unix_sock(sessiond_sock_path); | |
516 | if (ret < 0) { | |
2f70b271 | 517 | goto error; |
65beb5ff DG |
518 | } |
519 | ||
520 | sessiond_socket = ret; | |
521 | connected = 1; | |
522 | ||
523 | return 0; | |
2f70b271 DG |
524 | |
525 | error: | |
526 | return -1; | |
65beb5ff DG |
527 | } |
528 | ||
529 | /* | |
1c8d13c8 TD |
530 | * Clean disconnect from the session daemon. |
531 | * On success, return 0. On error, return -1. | |
65beb5ff DG |
532 | */ |
533 | static int disconnect_sessiond(void) | |
534 | { | |
535 | int ret = 0; | |
536 | ||
537 | if (connected) { | |
538 | ret = lttcomm_close_unix_sock(sessiond_socket); | |
539 | sessiond_socket = 0; | |
540 | connected = 0; | |
541 | } | |
542 | ||
543 | return ret; | |
544 | } | |
545 | ||
35a6fdb7 | 546 | /* |
cd80958d | 547 | * Ask the session daemon a specific command and put the data into buf. |
53a80697 | 548 | * Takes extra var. len. data as input to send to the session daemon. |
65beb5ff | 549 | * |
af87c45a | 550 | * Return size of data (only payload, not header) or a negative error code. |
65beb5ff | 551 | */ |
cac3069d DG |
552 | LTTNG_HIDDEN |
553 | int lttng_ctl_ask_sessiond_varlen(struct lttcomm_session_msg *lsm, | |
a4b92340 | 554 | void *vardata, size_t varlen, void **buf) |
65beb5ff DG |
555 | { |
556 | int ret; | |
557 | size_t size; | |
558 | void *data = NULL; | |
cd80958d | 559 | struct lttcomm_lttng_msg llm; |
65beb5ff DG |
560 | |
561 | ret = connect_sessiond(); | |
562 | if (ret < 0) { | |
2f70b271 | 563 | ret = -LTTNG_ERR_NO_SESSIOND; |
65beb5ff DG |
564 | goto end; |
565 | } | |
566 | ||
65beb5ff | 567 | /* Send command to session daemon */ |
cd80958d | 568 | ret = send_session_msg(lsm); |
65beb5ff | 569 | if (ret < 0) { |
2f70b271 | 570 | /* Ret value is a valid lttng error code. */ |
65beb5ff DG |
571 | goto end; |
572 | } | |
53a80697 MD |
573 | /* Send var len data */ |
574 | ret = send_session_varlen(vardata, varlen); | |
575 | if (ret < 0) { | |
2f70b271 | 576 | /* Ret value is a valid lttng error code. */ |
53a80697 MD |
577 | goto end; |
578 | } | |
65beb5ff DG |
579 | |
580 | /* Get header from data transmission */ | |
581 | ret = recv_data_sessiond(&llm, sizeof(llm)); | |
582 | if (ret < 0) { | |
2f70b271 | 583 | /* Ret value is a valid lttng error code. */ |
65beb5ff DG |
584 | goto end; |
585 | } | |
586 | ||
587 | /* Check error code if OK */ | |
f73fabfd | 588 | if (llm.ret_code != LTTNG_OK) { |
65beb5ff DG |
589 | ret = -llm.ret_code; |
590 | goto end; | |
591 | } | |
592 | ||
593 | size = llm.data_size; | |
594 | if (size == 0) { | |
874d3f84 | 595 | /* If client free with size 0 */ |
a45d5536 DG |
596 | if (buf != NULL) { |
597 | *buf = NULL; | |
598 | } | |
7d29a247 | 599 | ret = 0; |
65beb5ff DG |
600 | goto end; |
601 | } | |
602 | ||
603 | data = (void*) malloc(size); | |
604 | ||
605 | /* Get payload data */ | |
606 | ret = recv_data_sessiond(data, size); | |
607 | if (ret < 0) { | |
608 | free(data); | |
609 | goto end; | |
610 | } | |
611 | ||
83009e5e DG |
612 | /* |
613 | * Extra protection not to dereference a NULL pointer. If buf is NULL at | |
614 | * this point, an error is returned and data is freed. | |
615 | */ | |
616 | if (buf == NULL) { | |
2f70b271 | 617 | ret = -LTTNG_ERR_INVALID; |
83009e5e DG |
618 | free(data); |
619 | goto end; | |
620 | } | |
621 | ||
65beb5ff DG |
622 | *buf = data; |
623 | ret = size; | |
624 | ||
625 | end: | |
626 | disconnect_sessiond(); | |
627 | return ret; | |
628 | } | |
629 | ||
9f19cc17 | 630 | /* |
cd80958d | 631 | * Create lttng handle and return pointer. |
1c8d13c8 | 632 | * The returned pointer will be NULL in case of malloc() error. |
9f19cc17 | 633 | */ |
cd80958d DG |
634 | struct lttng_handle *lttng_create_handle(const char *session_name, |
635 | struct lttng_domain *domain) | |
9f19cc17 | 636 | { |
2f70b271 DG |
637 | struct lttng_handle *handle = NULL; |
638 | ||
639 | if (domain == NULL) { | |
640 | goto end; | |
641 | } | |
cd80958d DG |
642 | |
643 | handle = malloc(sizeof(struct lttng_handle)); | |
644 | if (handle == NULL) { | |
2f70b271 | 645 | PERROR("malloc handle"); |
cd80958d DG |
646 | goto end; |
647 | } | |
648 | ||
649 | /* Copy session name */ | |
cac3069d | 650 | lttng_ctl_copy_string(handle->session_name, session_name, |
cd80958d DG |
651 | sizeof(handle->session_name)); |
652 | ||
653 | /* Copy lttng domain */ | |
cac3069d | 654 | lttng_ctl_copy_lttng_domain(&handle->domain, domain); |
cd80958d DG |
655 | |
656 | end: | |
657 | return handle; | |
658 | } | |
659 | ||
660 | /* | |
661 | * Destroy handle by free(3) the pointer. | |
662 | */ | |
663 | void lttng_destroy_handle(struct lttng_handle *handle) | |
664 | { | |
0e428499 | 665 | free(handle); |
eb354453 DG |
666 | } |
667 | ||
d9800920 DG |
668 | /* |
669 | * Register an outside consumer. | |
1c8d13c8 | 670 | * Returns size of returned session payload data or a negative error code. |
d9800920 DG |
671 | */ |
672 | int lttng_register_consumer(struct lttng_handle *handle, | |
673 | const char *socket_path) | |
674 | { | |
675 | struct lttcomm_session_msg lsm; | |
676 | ||
2f70b271 DG |
677 | if (handle == NULL || socket_path == NULL) { |
678 | return -LTTNG_ERR_INVALID; | |
679 | } | |
680 | ||
d9800920 | 681 | lsm.cmd_type = LTTNG_REGISTER_CONSUMER; |
cac3069d | 682 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
d9800920 | 683 | sizeof(lsm.session.name)); |
cac3069d | 684 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
d9800920 | 685 | |
cac3069d | 686 | lttng_ctl_copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path)); |
d9800920 | 687 | |
cac3069d | 688 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
d9800920 DG |
689 | } |
690 | ||
1df4dedd | 691 | /* |
1c8d13c8 TD |
692 | * Start tracing for all traces of the session. |
693 | * Returns size of returned session payload data or a negative error code. | |
1df4dedd | 694 | */ |
6a4f824d | 695 | int lttng_start_tracing(const char *session_name) |
f3ed775e | 696 | { |
cd80958d DG |
697 | struct lttcomm_session_msg lsm; |
698 | ||
6a4f824d | 699 | if (session_name == NULL) { |
2f70b271 | 700 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
701 | } |
702 | ||
703 | lsm.cmd_type = LTTNG_START_TRACE; | |
6a4f824d | 704 | |
cac3069d DG |
705 | lttng_ctl_copy_string(lsm.session.name, session_name, |
706 | sizeof(lsm.session.name)); | |
cd80958d | 707 | |
cac3069d | 708 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
f3ed775e | 709 | } |
1df4dedd DG |
710 | |
711 | /* | |
38ee087f | 712 | * Stop tracing for all traces of the session. |
f3ed775e | 713 | */ |
38ee087f | 714 | static int _lttng_stop_tracing(const char *session_name, int wait) |
f3ed775e | 715 | { |
38ee087f | 716 | int ret, data_ret; |
cd80958d DG |
717 | struct lttcomm_session_msg lsm; |
718 | ||
6a4f824d | 719 | if (session_name == NULL) { |
2f70b271 | 720 | return -LTTNG_ERR_INVALID; |
6a4f824d DG |
721 | } |
722 | ||
cd80958d | 723 | lsm.cmd_type = LTTNG_STOP_TRACE; |
6a4f824d | 724 | |
cac3069d DG |
725 | lttng_ctl_copy_string(lsm.session.name, session_name, |
726 | sizeof(lsm.session.name)); | |
cd80958d | 727 | |
cac3069d | 728 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
38ee087f DG |
729 | if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) { |
730 | goto error; | |
731 | } | |
732 | ||
733 | if (!wait) { | |
734 | goto end; | |
735 | } | |
736 | ||
737 | _MSG("Waiting for data availability"); | |
738 | ||
739 | /* Check for data availability */ | |
740 | do { | |
6d805429 | 741 | data_ret = lttng_data_pending(session_name); |
38ee087f DG |
742 | if (data_ret < 0) { |
743 | /* Return the data available call error. */ | |
744 | ret = data_ret; | |
745 | goto error; | |
746 | } | |
747 | ||
748 | /* | |
749 | * Data sleep time before retrying (in usec). Don't sleep if the call | |
750 | * returned value indicates availability. | |
751 | */ | |
6d805429 | 752 | if (data_ret) { |
38ee087f DG |
753 | usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME); |
754 | _MSG("."); | |
755 | } | |
6d805429 | 756 | } while (data_ret != 0); |
38ee087f DG |
757 | |
758 | MSG(""); | |
759 | ||
760 | end: | |
761 | error: | |
762 | return ret; | |
763 | } | |
764 | ||
765 | /* | |
766 | * Stop tracing and wait for data availability. | |
767 | */ | |
768 | int lttng_stop_tracing(const char *session_name) | |
769 | { | |
770 | return _lttng_stop_tracing(session_name, 1); | |
771 | } | |
772 | ||
773 | /* | |
774 | * Stop tracing but _don't_ wait for data availability. | |
775 | */ | |
776 | int lttng_stop_tracing_no_wait(const char *session_name) | |
777 | { | |
778 | return _lttng_stop_tracing(session_name, 0); | |
f3ed775e DG |
779 | } |
780 | ||
781 | /* | |
601d5acf DG |
782 | * Add context to a channel. |
783 | * | |
784 | * If the given channel is NULL, add the contexts to all channels. | |
785 | * The event_name param is ignored. | |
af87c45a DG |
786 | * |
787 | * Returns the size of the returned payload data or a negative error code. | |
1df4dedd | 788 | */ |
cd80958d | 789 | int lttng_add_context(struct lttng_handle *handle, |
38057ed1 DG |
790 | struct lttng_event_context *ctx, const char *event_name, |
791 | const char *channel_name) | |
d65106b1 | 792 | { |
cd80958d DG |
793 | struct lttcomm_session_msg lsm; |
794 | ||
9d697d3d DG |
795 | /* Safety check. Both are mandatory */ |
796 | if (handle == NULL || ctx == NULL) { | |
2f70b271 | 797 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
798 | } |
799 | ||
441c16a7 MD |
800 | memset(&lsm, 0, sizeof(lsm)); |
801 | ||
cd80958d DG |
802 | lsm.cmd_type = LTTNG_ADD_CONTEXT; |
803 | ||
804 | /* Copy channel name */ | |
cac3069d | 805 | lttng_ctl_copy_string(lsm.u.context.channel_name, channel_name, |
cd80958d | 806 | sizeof(lsm.u.context.channel_name)); |
cd80958d | 807 | |
cac3069d | 808 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
d65106b1 | 809 | |
9d697d3d | 810 | memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context)); |
d65106b1 | 811 | |
cac3069d | 812 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d DG |
813 | sizeof(lsm.session.name)); |
814 | ||
cac3069d | 815 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
d65106b1 DG |
816 | } |
817 | ||
f3ed775e | 818 | /* |
1c8d13c8 TD |
819 | * Enable event(s) for a channel. |
820 | * If no event name is specified, all events are enabled. | |
821 | * If no channel name is specified, the default 'channel0' is used. | |
822 | * Returns size of returned session payload data or a negative error code. | |
f3ed775e | 823 | */ |
cd80958d | 824 | int lttng_enable_event(struct lttng_handle *handle, |
38057ed1 | 825 | struct lttng_event *ev, const char *channel_name) |
1df4dedd | 826 | { |
cd80958d DG |
827 | struct lttcomm_session_msg lsm; |
828 | ||
5117eeec | 829 | if (handle == NULL || ev == NULL) { |
2f70b271 | 830 | return -LTTNG_ERR_INVALID; |
cd80958d | 831 | } |
33a2b854 | 832 | |
441c16a7 MD |
833 | memset(&lsm, 0, sizeof(lsm)); |
834 | ||
5117eeec | 835 | /* If no channel name, we put the default name */ |
94cf3c47 | 836 | if (channel_name == NULL) { |
cac3069d | 837 | lttng_ctl_copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME, |
cd80958d | 838 | sizeof(lsm.u.enable.channel_name)); |
33a2b854 | 839 | } else { |
cac3069d | 840 | lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name, |
cd80958d | 841 | sizeof(lsm.u.enable.channel_name)); |
eb354453 DG |
842 | } |
843 | ||
cac3069d | 844 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
0d0c377a | 845 | |
8c9ae521 | 846 | if (ev->name[0] != '\0') { |
cd80958d | 847 | lsm.cmd_type = LTTNG_ENABLE_EVENT; |
0d0c377a | 848 | } else { |
cd80958d | 849 | lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT; |
f3ed775e | 850 | } |
8c9ae521 | 851 | memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event)); |
f3ed775e | 852 | |
cac3069d | 853 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d DG |
854 | sizeof(lsm.session.name)); |
855 | ||
cac3069d | 856 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
1df4dedd DG |
857 | } |
858 | ||
53a80697 | 859 | /* |
025faf73 | 860 | * Create or enable an event with a filter expression. |
178191b3 | 861 | * |
53a80697 MD |
862 | * Return negative error value on error. |
863 | * Return size of returned session payload data if OK. | |
864 | */ | |
025faf73 | 865 | int lttng_enable_event_with_filter(struct lttng_handle *handle, |
178191b3 | 866 | struct lttng_event *event, const char *channel_name, |
53a80697 MD |
867 | const char *filter_expression) |
868 | { | |
869 | struct lttcomm_session_msg lsm; | |
870 | struct filter_parser_ctx *ctx; | |
871 | FILE *fmem; | |
872 | int ret = 0; | |
873 | ||
52df2401 MD |
874 | if (!filter_expression) { |
875 | /* | |
876 | * Fall back to normal event enabling if no filter | |
877 | * specified. | |
878 | */ | |
879 | return lttng_enable_event(handle, event, channel_name); | |
880 | } | |
881 | ||
882 | /* | |
883 | * Empty filter string will always be rejected by the parser | |
884 | * anyway, so treat this corner-case early to eliminate | |
885 | * lttng_fmemopen error for 0-byte allocation. | |
886 | */ | |
887 | if (handle == NULL || filter_expression[0] == '\0') { | |
2f70b271 | 888 | return -LTTNG_ERR_INVALID; |
53a80697 MD |
889 | } |
890 | ||
53a80697 MD |
891 | /* |
892 | * casting const to non-const, as the underlying function will | |
893 | * use it in read-only mode. | |
894 | */ | |
895 | fmem = lttng_fmemopen((void *) filter_expression, | |
896 | strlen(filter_expression), "r"); | |
897 | if (!fmem) { | |
898 | fprintf(stderr, "Error opening memory as stream\n"); | |
ff7ef7b8 | 899 | return -LTTNG_ERR_FILTER_NOMEM; |
53a80697 MD |
900 | } |
901 | ctx = filter_parser_ctx_alloc(fmem); | |
902 | if (!ctx) { | |
903 | fprintf(stderr, "Error allocating parser\n"); | |
ff7ef7b8 | 904 | ret = -LTTNG_ERR_FILTER_NOMEM; |
53a80697 MD |
905 | goto alloc_error; |
906 | } | |
907 | ret = filter_parser_ctx_append_ast(ctx); | |
908 | if (ret) { | |
909 | fprintf(stderr, "Parse error\n"); | |
ff7ef7b8 | 910 | ret = -LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
911 | goto parse_error; |
912 | } | |
913 | ret = filter_visitor_set_parent(ctx); | |
914 | if (ret) { | |
915 | fprintf(stderr, "Set parent error\n"); | |
ff7ef7b8 | 916 | ret = -LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
917 | goto parse_error; |
918 | } | |
919 | if (print_xml) { | |
920 | ret = filter_visitor_print_xml(ctx, stdout, 0); | |
921 | if (ret) { | |
922 | fflush(stdout); | |
923 | fprintf(stderr, "XML print error\n"); | |
ff7ef7b8 | 924 | ret = -LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
925 | goto parse_error; |
926 | } | |
927 | } | |
928 | ||
929 | dbg_printf("Generating IR... "); | |
930 | fflush(stdout); | |
931 | ret = filter_visitor_ir_generate(ctx); | |
932 | if (ret) { | |
933 | fprintf(stderr, "Generate IR error\n"); | |
ff7ef7b8 | 934 | ret = -LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
935 | goto parse_error; |
936 | } | |
937 | dbg_printf("done\n"); | |
938 | ||
939 | dbg_printf("Validating IR... "); | |
940 | fflush(stdout); | |
941 | ret = filter_visitor_ir_check_binary_op_nesting(ctx); | |
942 | if (ret) { | |
ff7ef7b8 | 943 | ret = -LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
944 | goto parse_error; |
945 | } | |
946 | dbg_printf("done\n"); | |
947 | ||
948 | dbg_printf("Generating bytecode... "); | |
949 | fflush(stdout); | |
950 | ret = filter_visitor_bytecode_generate(ctx); | |
951 | if (ret) { | |
952 | fprintf(stderr, "Generate bytecode error\n"); | |
ff7ef7b8 | 953 | ret = -LTTNG_ERR_FILTER_INVAL; |
53a80697 MD |
954 | goto parse_error; |
955 | } | |
956 | dbg_printf("done\n"); | |
957 | dbg_printf("Size of bytecode generated: %u bytes.\n", | |
958 | bytecode_get_len(&ctx->bytecode->b)); | |
959 | ||
960 | memset(&lsm, 0, sizeof(lsm)); | |
961 | ||
025faf73 | 962 | lsm.cmd_type = LTTNG_ENABLE_EVENT_WITH_FILTER; |
53a80697 MD |
963 | |
964 | /* Copy channel name */ | |
cac3069d | 965 | lttng_ctl_copy_string(lsm.u.enable.channel_name, channel_name, |
025faf73 | 966 | sizeof(lsm.u.enable.channel_name)); |
53a80697 | 967 | /* Copy event name */ |
178191b3 DG |
968 | if (event) { |
969 | memcpy(&lsm.u.enable.event, event, sizeof(lsm.u.enable.event)); | |
970 | } | |
971 | ||
025faf73 | 972 | lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b) |
53a80697 MD |
973 | + bytecode_get_len(&ctx->bytecode->b); |
974 | ||
cac3069d | 975 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
53a80697 | 976 | |
cac3069d | 977 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
53a80697 MD |
978 | sizeof(lsm.session.name)); |
979 | ||
cac3069d | 980 | ret = lttng_ctl_ask_sessiond_varlen(&lsm, &ctx->bytecode->b, |
025faf73 | 981 | lsm.u.enable.bytecode_len, NULL); |
53a80697 MD |
982 | |
983 | filter_bytecode_free(ctx); | |
984 | filter_ir_free(ctx); | |
985 | filter_parser_ctx_free(ctx); | |
986 | if (fclose(fmem) != 0) { | |
987 | perror("fclose"); | |
988 | } | |
989 | return ret; | |
990 | ||
991 | parse_error: | |
992 | filter_bytecode_free(ctx); | |
993 | filter_ir_free(ctx); | |
994 | filter_parser_ctx_free(ctx); | |
995 | alloc_error: | |
996 | if (fclose(fmem) != 0) { | |
997 | perror("fclose"); | |
998 | } | |
999 | return ret; | |
1000 | } | |
1001 | ||
1df4dedd | 1002 | /* |
1c8d13c8 TD |
1003 | * Disable event(s) of a channel and domain. |
1004 | * If no event name is specified, all events are disabled. | |
1005 | * If no channel name is specified, the default 'channel0' is used. | |
1006 | * Returns size of returned session payload data or a negative error code. | |
1df4dedd | 1007 | */ |
cd80958d | 1008 | int lttng_disable_event(struct lttng_handle *handle, const char *name, |
38057ed1 | 1009 | const char *channel_name) |
1df4dedd | 1010 | { |
cd80958d | 1011 | struct lttcomm_session_msg lsm; |
1df4dedd | 1012 | |
9d697d3d | 1013 | if (handle == NULL) { |
2f70b271 | 1014 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1015 | } |
1016 | ||
441c16a7 MD |
1017 | memset(&lsm, 0, sizeof(lsm)); |
1018 | ||
cd80958d | 1019 | if (channel_name) { |
cac3069d | 1020 | lttng_ctl_copy_string(lsm.u.disable.channel_name, channel_name, |
cd80958d | 1021 | sizeof(lsm.u.disable.channel_name)); |
f3ed775e | 1022 | } else { |
cac3069d | 1023 | lttng_ctl_copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME, |
cd80958d | 1024 | sizeof(lsm.u.disable.channel_name)); |
eb354453 DG |
1025 | } |
1026 | ||
cac3069d | 1027 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
f5177a38 | 1028 | |
f84efadf | 1029 | if (name != NULL) { |
cac3069d DG |
1030 | lttng_ctl_copy_string(lsm.u.disable.name, name, |
1031 | sizeof(lsm.u.disable.name)); | |
cd80958d | 1032 | lsm.cmd_type = LTTNG_DISABLE_EVENT; |
f5177a38 | 1033 | } else { |
cd80958d | 1034 | lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT; |
f3ed775e DG |
1035 | } |
1036 | ||
cac3069d | 1037 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d DG |
1038 | sizeof(lsm.session.name)); |
1039 | ||
cac3069d | 1040 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
1df4dedd DG |
1041 | } |
1042 | ||
1043 | /* | |
1c8d13c8 TD |
1044 | * Enable channel per domain |
1045 | * Returns size of returned session payload data or a negative error code. | |
a5c5a2bd | 1046 | */ |
cd80958d | 1047 | int lttng_enable_channel(struct lttng_handle *handle, |
38057ed1 | 1048 | struct lttng_channel *chan) |
a5c5a2bd | 1049 | { |
cd80958d DG |
1050 | struct lttcomm_session_msg lsm; |
1051 | ||
5117eeec DG |
1052 | /* |
1053 | * NULL arguments are forbidden. No default values. | |
1054 | */ | |
1055 | if (handle == NULL || chan == NULL) { | |
2f70b271 | 1056 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1057 | } |
1058 | ||
441c16a7 MD |
1059 | memset(&lsm, 0, sizeof(lsm)); |
1060 | ||
5117eeec | 1061 | memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan)); |
7d29a247 | 1062 | |
cd80958d DG |
1063 | lsm.cmd_type = LTTNG_ENABLE_CHANNEL; |
1064 | ||
cac3069d | 1065 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
7d29a247 | 1066 | |
cac3069d | 1067 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d DG |
1068 | sizeof(lsm.session.name)); |
1069 | ||
cac3069d | 1070 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
8c0faa1d | 1071 | } |
1df4dedd | 1072 | |
2ef84c95 | 1073 | /* |
1c8d13c8 TD |
1074 | * All tracing will be stopped for registered events of the channel. |
1075 | * Returns size of returned session payload data or a negative error code. | |
2ef84c95 | 1076 | */ |
cd80958d | 1077 | int lttng_disable_channel(struct lttng_handle *handle, const char *name) |
2ef84c95 | 1078 | { |
cd80958d DG |
1079 | struct lttcomm_session_msg lsm; |
1080 | ||
9d697d3d DG |
1081 | /* Safety check. Both are mandatory */ |
1082 | if (handle == NULL || name == NULL) { | |
2f70b271 | 1083 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1084 | } |
1085 | ||
441c16a7 MD |
1086 | memset(&lsm, 0, sizeof(lsm)); |
1087 | ||
cd80958d | 1088 | lsm.cmd_type = LTTNG_DISABLE_CHANNEL; |
1df4dedd | 1089 | |
cac3069d | 1090 | lttng_ctl_copy_string(lsm.u.disable.channel_name, name, |
9d697d3d DG |
1091 | sizeof(lsm.u.disable.channel_name)); |
1092 | ||
cac3069d | 1093 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
cd80958d | 1094 | |
cac3069d | 1095 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d DG |
1096 | sizeof(lsm.session.name)); |
1097 | ||
cac3069d | 1098 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
ca95a216 DG |
1099 | } |
1100 | ||
fac6795d | 1101 | /* |
1c8d13c8 TD |
1102 | * Lists all available tracepoints of domain. |
1103 | * Sets the contents of the events array. | |
1104 | * Returns the number of lttng_event entries in events; | |
1105 | * on error, returns a negative value. | |
fac6795d | 1106 | */ |
cd80958d | 1107 | int lttng_list_tracepoints(struct lttng_handle *handle, |
2a71efd5 | 1108 | struct lttng_event **events) |
fac6795d | 1109 | { |
052da939 | 1110 | int ret; |
cd80958d DG |
1111 | struct lttcomm_session_msg lsm; |
1112 | ||
9d697d3d | 1113 | if (handle == NULL) { |
2f70b271 | 1114 | return -LTTNG_ERR_INVALID; |
cd80958d | 1115 | } |
fac6795d | 1116 | |
cd80958d | 1117 | lsm.cmd_type = LTTNG_LIST_TRACEPOINTS; |
cac3069d | 1118 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
2a71efd5 | 1119 | |
cac3069d | 1120 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) events); |
052da939 DG |
1121 | if (ret < 0) { |
1122 | return ret; | |
eb354453 | 1123 | } |
fac6795d | 1124 | |
9f19cc17 | 1125 | return ret / sizeof(struct lttng_event); |
fac6795d DG |
1126 | } |
1127 | ||
f37d259d MD |
1128 | /* |
1129 | * Lists all available tracepoint fields of domain. | |
1130 | * Sets the contents of the event field array. | |
1131 | * Returns the number of lttng_event_field entries in events; | |
1132 | * on error, returns a negative value. | |
1133 | */ | |
1134 | int lttng_list_tracepoint_fields(struct lttng_handle *handle, | |
1135 | struct lttng_event_field **fields) | |
1136 | { | |
1137 | int ret; | |
1138 | struct lttcomm_session_msg lsm; | |
1139 | ||
1140 | if (handle == NULL) { | |
2f70b271 | 1141 | return -LTTNG_ERR_INVALID; |
f37d259d MD |
1142 | } |
1143 | ||
1144 | lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS; | |
cac3069d | 1145 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
f37d259d | 1146 | |
cac3069d | 1147 | ret = lttng_ctl_ask_sessiond(&lsm, (void **) fields); |
f37d259d MD |
1148 | if (ret < 0) { |
1149 | return ret; | |
1150 | } | |
1151 | ||
1152 | return ret / sizeof(struct lttng_event_field); | |
1153 | } | |
1154 | ||
1657e9bb | 1155 | /* |
1c8d13c8 TD |
1156 | * Returns a human readable string describing |
1157 | * the error code (a negative value). | |
1657e9bb | 1158 | */ |
9a745bc7 | 1159 | const char *lttng_strerror(int code) |
1657e9bb | 1160 | { |
f73fabfd | 1161 | return error_get_str(code); |
1657e9bb DG |
1162 | } |
1163 | ||
aaf97519 | 1164 | /* |
a4b92340 DG |
1165 | * Create a brand new session using name and url for destination. |
1166 | * | |
f73fabfd | 1167 | * Returns LTTNG_OK on success or a negative error code. |
00e2e675 | 1168 | */ |
a4b92340 | 1169 | int lttng_create_session(const char *name, const char *url) |
00e2e675 | 1170 | { |
3dd05a85 | 1171 | int ret; |
a4b92340 | 1172 | ssize_t size; |
00e2e675 | 1173 | struct lttcomm_session_msg lsm; |
a4b92340 | 1174 | struct lttng_uri *uris = NULL; |
00e2e675 | 1175 | |
a4b92340 | 1176 | if (name == NULL) { |
2f70b271 | 1177 | return -LTTNG_ERR_INVALID; |
00e2e675 DG |
1178 | } |
1179 | ||
a4b92340 | 1180 | memset(&lsm, 0, sizeof(lsm)); |
00e2e675 | 1181 | |
a4b92340 | 1182 | lsm.cmd_type = LTTNG_CREATE_SESSION; |
cac3069d | 1183 | lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name)); |
a4b92340 DG |
1184 | |
1185 | /* There should never be a data URL */ | |
1186 | size = parse_str_urls_to_uri(url, NULL, &uris); | |
1187 | if (size < 0) { | |
2f70b271 | 1188 | return -LTTNG_ERR_INVALID; |
00e2e675 DG |
1189 | } |
1190 | ||
a4b92340 DG |
1191 | lsm.u.uri.size = size; |
1192 | ||
cac3069d DG |
1193 | ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris, |
1194 | sizeof(struct lttng_uri) * size, NULL); | |
3dd05a85 DG |
1195 | |
1196 | free(uris); | |
1197 | return ret; | |
00e2e675 DG |
1198 | } |
1199 | ||
8028d920 | 1200 | /* |
8028d920 | 1201 | * Destroy session using name. |
1c8d13c8 | 1202 | * Returns size of returned session payload data or a negative error code. |
8028d920 | 1203 | */ |
843f5df9 | 1204 | int lttng_destroy_session(const char *session_name) |
8028d920 | 1205 | { |
cd80958d DG |
1206 | struct lttcomm_session_msg lsm; |
1207 | ||
843f5df9 | 1208 | if (session_name == NULL) { |
2f70b271 | 1209 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1210 | } |
1211 | ||
1212 | lsm.cmd_type = LTTNG_DESTROY_SESSION; | |
843f5df9 | 1213 | |
cac3069d DG |
1214 | lttng_ctl_copy_string(lsm.session.name, session_name, |
1215 | sizeof(lsm.session.name)); | |
cd80958d | 1216 | |
cac3069d | 1217 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
aaf97519 DG |
1218 | } |
1219 | ||
57167058 | 1220 | /* |
57167058 | 1221 | * Ask the session daemon for all available sessions. |
1c8d13c8 TD |
1222 | * Sets the contents of the sessions array. |
1223 | * Returns the number of lttng_session entries in sessions; | |
1224 | * on error, returns a negative value. | |
57167058 | 1225 | */ |
ca95a216 | 1226 | int lttng_list_sessions(struct lttng_session **sessions) |
57167058 | 1227 | { |
ca95a216 | 1228 | int ret; |
cd80958d | 1229 | struct lttcomm_session_msg lsm; |
57167058 | 1230 | |
cd80958d | 1231 | lsm.cmd_type = LTTNG_LIST_SESSIONS; |
cac3069d | 1232 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) sessions); |
57167058 | 1233 | if (ret < 0) { |
ca95a216 | 1234 | return ret; |
57167058 DG |
1235 | } |
1236 | ||
ca95a216 | 1237 | return ret / sizeof(struct lttng_session); |
57167058 DG |
1238 | } |
1239 | ||
9f19cc17 | 1240 | /* |
1c8d13c8 TD |
1241 | * Ask the session daemon for all available domains of a session. |
1242 | * Sets the contents of the domains array. | |
1243 | * Returns the number of lttng_domain entries in domains; | |
1244 | * on error, returns a negative value. | |
9f19cc17 | 1245 | */ |
330be774 | 1246 | int lttng_list_domains(const char *session_name, |
cd80958d | 1247 | struct lttng_domain **domains) |
9f19cc17 DG |
1248 | { |
1249 | int ret; | |
cd80958d DG |
1250 | struct lttcomm_session_msg lsm; |
1251 | ||
330be774 | 1252 | if (session_name == NULL) { |
2f70b271 | 1253 | return -LTTNG_ERR_INVALID; |
cd80958d | 1254 | } |
9f19cc17 | 1255 | |
cd80958d DG |
1256 | lsm.cmd_type = LTTNG_LIST_DOMAINS; |
1257 | ||
cac3069d DG |
1258 | lttng_ctl_copy_string(lsm.session.name, session_name, |
1259 | sizeof(lsm.session.name)); | |
cd80958d | 1260 | |
cac3069d | 1261 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) domains); |
9f19cc17 DG |
1262 | if (ret < 0) { |
1263 | return ret; | |
1264 | } | |
1265 | ||
1266 | return ret / sizeof(struct lttng_domain); | |
1267 | } | |
1268 | ||
1269 | /* | |
1c8d13c8 TD |
1270 | * Ask the session daemon for all available channels of a session. |
1271 | * Sets the contents of the channels array. | |
1272 | * Returns the number of lttng_channel entries in channels; | |
1273 | * on error, returns a negative value. | |
9f19cc17 | 1274 | */ |
cd80958d DG |
1275 | int lttng_list_channels(struct lttng_handle *handle, |
1276 | struct lttng_channel **channels) | |
9f19cc17 DG |
1277 | { |
1278 | int ret; | |
cd80958d DG |
1279 | struct lttcomm_session_msg lsm; |
1280 | ||
9d697d3d | 1281 | if (handle == NULL) { |
2f70b271 | 1282 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1283 | } |
1284 | ||
1285 | lsm.cmd_type = LTTNG_LIST_CHANNELS; | |
cac3069d | 1286 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d | 1287 | sizeof(lsm.session.name)); |
9f19cc17 | 1288 | |
cac3069d | 1289 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
9f19cc17 | 1290 | |
cac3069d | 1291 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) channels); |
9f19cc17 DG |
1292 | if (ret < 0) { |
1293 | return ret; | |
1294 | } | |
1295 | ||
1296 | return ret / sizeof(struct lttng_channel); | |
1297 | } | |
1298 | ||
1299 | /* | |
1c8d13c8 TD |
1300 | * Ask the session daemon for all available events of a session channel. |
1301 | * Sets the contents of the events array. | |
1302 | * Returns the number of lttng_event entries in events; | |
1303 | * on error, returns a negative value. | |
9f19cc17 | 1304 | */ |
cd80958d DG |
1305 | int lttng_list_events(struct lttng_handle *handle, |
1306 | const char *channel_name, struct lttng_event **events) | |
9f19cc17 DG |
1307 | { |
1308 | int ret; | |
cd80958d | 1309 | struct lttcomm_session_msg lsm; |
9f19cc17 | 1310 | |
9d697d3d DG |
1311 | /* Safety check. An handle and channel name are mandatory */ |
1312 | if (handle == NULL || channel_name == NULL) { | |
2f70b271 | 1313 | return -LTTNG_ERR_INVALID; |
cd80958d DG |
1314 | } |
1315 | ||
1316 | lsm.cmd_type = LTTNG_LIST_EVENTS; | |
cac3069d | 1317 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
cd80958d | 1318 | sizeof(lsm.session.name)); |
cac3069d | 1319 | lttng_ctl_copy_string(lsm.u.list.channel_name, channel_name, |
cd80958d DG |
1320 | sizeof(lsm.u.list.channel_name)); |
1321 | ||
cac3069d | 1322 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
9f19cc17 | 1323 | |
cac3069d | 1324 | ret = lttng_ctl_ask_sessiond(&lsm, (void**) events); |
9f19cc17 DG |
1325 | if (ret < 0) { |
1326 | return ret; | |
1327 | } | |
1328 | ||
1329 | return ret / sizeof(struct lttng_event); | |
1330 | } | |
1331 | ||
fac6795d | 1332 | /* |
1c8d13c8 TD |
1333 | * Sets the tracing_group variable with name. |
1334 | * This function allocates memory pointed to by tracing_group. | |
1335 | * On success, returns 0, on error, returns -1 (null name) or -ENOMEM. | |
fac6795d DG |
1336 | */ |
1337 | int lttng_set_tracing_group(const char *name) | |
1338 | { | |
9d697d3d | 1339 | if (name == NULL) { |
2f70b271 | 1340 | return -LTTNG_ERR_INVALID; |
9d697d3d DG |
1341 | } |
1342 | ||
fac6795d | 1343 | if (asprintf(&tracing_group, "%s", name) < 0) { |
2f70b271 | 1344 | return -LTTNG_ERR_FATAL; |
fac6795d DG |
1345 | } |
1346 | ||
1347 | return 0; | |
1348 | } | |
1349 | ||
d0254c7c | 1350 | /* |
af87c45a | 1351 | * Returns size of returned session payload data or a negative error code. |
d0254c7c | 1352 | */ |
cd80958d | 1353 | int lttng_calibrate(struct lttng_handle *handle, |
d0254c7c MD |
1354 | struct lttng_calibrate *calibrate) |
1355 | { | |
cd80958d | 1356 | struct lttcomm_session_msg lsm; |
d0254c7c | 1357 | |
9d697d3d DG |
1358 | /* Safety check. NULL pointer are forbidden */ |
1359 | if (handle == NULL || calibrate == NULL) { | |
2f70b271 | 1360 | return -LTTNG_ERR_INVALID; |
cd80958d | 1361 | } |
d0254c7c | 1362 | |
cd80958d | 1363 | lsm.cmd_type = LTTNG_CALIBRATE; |
cac3069d | 1364 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
d0254c7c | 1365 | |
cd80958d DG |
1366 | memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate)); |
1367 | ||
cac3069d | 1368 | return lttng_ctl_ask_sessiond(&lsm, NULL); |
d0254c7c MD |
1369 | } |
1370 | ||
5edd7e09 DG |
1371 | /* |
1372 | * Set default channel attributes. | |
441c16a7 | 1373 | * If either or both of the arguments are null, attr content is zeroe'd. |
5edd7e09 DG |
1374 | */ |
1375 | void lttng_channel_set_default_attr(struct lttng_domain *domain, | |
1376 | struct lttng_channel_attr *attr) | |
1377 | { | |
1378 | /* Safety check */ | |
1379 | if (attr == NULL || domain == NULL) { | |
1380 | return; | |
1381 | } | |
1382 | ||
eacaa7a6 DS |
1383 | memset(attr, 0, sizeof(struct lttng_channel_attr)); |
1384 | ||
0a9c6494 DG |
1385 | /* Same for all domains. */ |
1386 | attr->overwrite = DEFAULT_CHANNEL_OVERWRITE; | |
1387 | attr->tracefile_size = DEFAULT_CHANNEL_TRACEFILE_SIZE; | |
1388 | attr->tracefile_count = DEFAULT_CHANNEL_TRACEFILE_COUNT; | |
1389 | ||
5edd7e09 DG |
1390 | switch (domain->type) { |
1391 | case LTTNG_DOMAIN_KERNEL: | |
d92ff3ef DG |
1392 | attr->switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER; |
1393 | attr->read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER; | |
3e230f92 | 1394 | attr->subbuf_size = default_get_kernel_channel_subbuf_size(); |
5edd7e09 DG |
1395 | attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM; |
1396 | attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT; | |
1397 | break; | |
1398 | case LTTNG_DOMAIN_UST: | |
0a9c6494 DG |
1399 | switch (domain->buf_type) { |
1400 | case LTTNG_BUFFER_PER_UID: | |
1401 | attr->subbuf_size = default_get_ust_uid_channel_subbuf_size(); | |
1402 | attr->num_subbuf = DEFAULT_UST_UID_CHANNEL_SUBBUF_NUM; | |
1403 | attr->output = DEFAULT_UST_UID_CHANNEL_OUTPUT; | |
1404 | attr->switch_timer_interval = DEFAULT_UST_UID_CHANNEL_SWITCH_TIMER; | |
1405 | attr->read_timer_interval = DEFAULT_UST_UID_CHANNEL_READ_TIMER; | |
1406 | break; | |
1407 | case LTTNG_BUFFER_PER_PID: | |
1408 | default: | |
1409 | attr->subbuf_size = default_get_ust_pid_channel_subbuf_size(); | |
1410 | attr->num_subbuf = DEFAULT_UST_PID_CHANNEL_SUBBUF_NUM; | |
1411 | attr->output = DEFAULT_UST_PID_CHANNEL_OUTPUT; | |
1412 | attr->switch_timer_interval = DEFAULT_UST_PID_CHANNEL_SWITCH_TIMER; | |
1413 | attr->read_timer_interval = DEFAULT_UST_PID_CHANNEL_READ_TIMER; | |
1414 | break; | |
1415 | } | |
5edd7e09 | 1416 | default: |
441c16a7 | 1417 | /* Default behavior: leave set to 0. */ |
5edd7e09 DG |
1418 | break; |
1419 | } | |
1420 | } | |
1421 | ||
fac6795d | 1422 | /* |
2269e89e | 1423 | * Check if session daemon is alive. |
fac6795d | 1424 | * |
2269e89e | 1425 | * Return 1 if alive or 0 if not. |
1c8d13c8 | 1426 | * On error returns a negative value. |
fac6795d | 1427 | */ |
947308c4 | 1428 | int lttng_session_daemon_alive(void) |
fac6795d DG |
1429 | { |
1430 | int ret; | |
1431 | ||
1432 | ret = set_session_daemon_path(); | |
1433 | if (ret < 0) { | |
947308c4 | 1434 | /* Error */ |
fac6795d DG |
1435 | return ret; |
1436 | } | |
1437 | ||
9d035200 | 1438 | if (*sessiond_sock_path == '\0') { |
2f70b271 DG |
1439 | /* |
1440 | * No socket path set. Weird error which means the constructor was not | |
1441 | * called. | |
1442 | */ | |
1443 | assert(0); | |
fac6795d DG |
1444 | } |
1445 | ||
2269e89e | 1446 | ret = try_connect_sessiond(sessiond_sock_path); |
7d8234d9 MD |
1447 | if (ret < 0) { |
1448 | /* Not alive */ | |
1449 | return 0; | |
1450 | } | |
7d8234d9 | 1451 | |
947308c4 DG |
1452 | /* Is alive */ |
1453 | return 1; | |
fac6795d DG |
1454 | } |
1455 | ||
00e2e675 | 1456 | /* |
a4b92340 | 1457 | * Set URL for a consumer for a session and domain. |
00e2e675 DG |
1458 | * |
1459 | * Return 0 on success, else a negative value. | |
1460 | */ | |
a4b92340 DG |
1461 | int lttng_set_consumer_url(struct lttng_handle *handle, |
1462 | const char *control_url, const char *data_url) | |
00e2e675 | 1463 | { |
3dd05a85 | 1464 | int ret; |
a4b92340 | 1465 | ssize_t size; |
00e2e675 | 1466 | struct lttcomm_session_msg lsm; |
a4b92340 | 1467 | struct lttng_uri *uris = NULL; |
00e2e675 | 1468 | |
a4b92340 | 1469 | if (handle == NULL || (control_url == NULL && data_url == NULL)) { |
2f70b271 | 1470 | return -LTTNG_ERR_INVALID; |
00e2e675 DG |
1471 | } |
1472 | ||
a4b92340 DG |
1473 | memset(&lsm, 0, sizeof(lsm)); |
1474 | ||
00e2e675 DG |
1475 | lsm.cmd_type = LTTNG_SET_CONSUMER_URI; |
1476 | ||
cac3069d | 1477 | lttng_ctl_copy_string(lsm.session.name, handle->session_name, |
00e2e675 | 1478 | sizeof(lsm.session.name)); |
cac3069d | 1479 | lttng_ctl_copy_lttng_domain(&lsm.domain, &handle->domain); |
00e2e675 | 1480 | |
a4b92340 DG |
1481 | size = parse_str_urls_to_uri(control_url, data_url, &uris); |
1482 | if (size < 0) { | |
2f70b271 | 1483 | return -LTTNG_ERR_INVALID; |
a4b92340 DG |
1484 | } |
1485 | ||
1486 | lsm.u.uri.size = size; | |
00e2e675 | 1487 | |
cac3069d DG |
1488 | ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris, |
1489 | sizeof(struct lttng_uri) * size, NULL); | |
3dd05a85 DG |
1490 | |
1491 | free(uris); | |
1492 | return ret; | |
00e2e675 DG |
1493 | } |
1494 | ||
1495 | /* | |
9c6bda17 | 1496 | * [OBSOLETE] |
00e2e675 DG |
1497 | */ |
1498 | int lttng_enable_consumer(struct lttng_handle *handle) | |
1499 | { | |
785d2d0d | 1500 | return -ENOSYS; |
00e2e675 DG |
1501 | } |
1502 | ||
1503 | /* | |
9c6bda17 | 1504 | * [OBSOLETE] |
00e2e675 DG |
1505 | */ |
1506 | int lttng_disable_consumer(struct lttng_handle *handle) | |
1507 | { | |
785d2d0d | 1508 | return -ENOSYS; |
00e2e675 DG |
1509 | } |
1510 | ||
44a5e5eb DG |
1511 | /* |
1512 | * Set health socket path by putting it in the global health_sock_path | |
1513 | * variable. | |
1514 | * | |
1515 | * Returns 0 on success or assert(0) on ENOMEM. | |
1516 | */ | |
1517 | static int set_health_socket_path(void) | |
1518 | { | |
44a5e5eb DG |
1519 | int in_tgroup = 0; /* In tracing group */ |
1520 | uid_t uid; | |
1521 | const char *home; | |
1522 | ||
1523 | uid = getuid(); | |
1524 | ||
1525 | if (uid != 0) { | |
1526 | /* Are we in the tracing group ? */ | |
1527 | in_tgroup = check_tracing_group(tracing_group); | |
1528 | } | |
1529 | ||
1530 | if ((uid == 0) || in_tgroup) { | |
cac3069d DG |
1531 | lttng_ctl_copy_string(health_sock_path, |
1532 | DEFAULT_GLOBAL_HEALTH_UNIX_SOCK, sizeof(health_sock_path)); | |
44a5e5eb DG |
1533 | } |
1534 | ||
1535 | if (uid != 0) { | |
c617c0c6 MD |
1536 | int ret; |
1537 | ||
44a5e5eb DG |
1538 | /* |
1539 | * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small; | |
1540 | * With GNU C >= 2.1, snprintf returns the required size (excluding closing null) | |
1541 | */ | |
feb0f3e5 | 1542 | home = utils_get_home_dir(); |
44a5e5eb DG |
1543 | if (home == NULL) { |
1544 | /* Fallback in /tmp .. */ | |
1545 | home = "/tmp"; | |
1546 | } | |
1547 | ||
1548 | ret = snprintf(health_sock_path, sizeof(health_sock_path), | |
1549 | DEFAULT_HOME_HEALTH_UNIX_SOCK, home); | |
1550 | if ((ret < 0) || (ret >= sizeof(health_sock_path))) { | |
1551 | /* ENOMEM at this point... just kill the control lib. */ | |
1552 | assert(0); | |
1553 | } | |
1554 | } | |
1555 | ||
1556 | return 0; | |
1557 | } | |
1558 | ||
1559 | /* | |
1560 | * Check session daemon health for a specific health component. | |
1561 | * | |
2f70b271 | 1562 | * Return 0 if health is OK or else 1 if BAD. |
44a5e5eb | 1563 | * |
2f70b271 | 1564 | * Any other negative value is a lttng error code which can be translated with |
44a5e5eb DG |
1565 | * lttng_strerror(). |
1566 | */ | |
1567 | int lttng_health_check(enum lttng_health_component c) | |
1568 | { | |
1569 | int sock, ret; | |
1570 | struct lttcomm_health_msg msg; | |
1571 | struct lttcomm_health_data reply; | |
1572 | ||
1573 | /* Connect to the sesssion daemon */ | |
1574 | sock = lttcomm_connect_unix_sock(health_sock_path); | |
1575 | if (sock < 0) { | |
2f70b271 | 1576 | ret = -LTTNG_ERR_NO_SESSIOND; |
44a5e5eb DG |
1577 | goto error; |
1578 | } | |
1579 | ||
1580 | msg.cmd = LTTNG_HEALTH_CHECK; | |
1581 | msg.component = c; | |
1582 | ||
1583 | ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg)); | |
1584 | if (ret < 0) { | |
2f70b271 | 1585 | ret = -LTTNG_ERR_FATAL; |
44a5e5eb DG |
1586 | goto close_error; |
1587 | } | |
1588 | ||
1589 | ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply)); | |
1590 | if (ret < 0) { | |
2f70b271 | 1591 | ret = -LTTNG_ERR_FATAL; |
44a5e5eb DG |
1592 | goto close_error; |
1593 | } | |
1594 | ||
1595 | ret = reply.ret_code; | |
1596 | ||
1597 | close_error: | |
67ef15b7 MD |
1598 | { |
1599 | int closeret; | |
1600 | ||
1601 | closeret = close(sock); | |
1602 | assert(!closeret); | |
1603 | } | |
44a5e5eb DG |
1604 | |
1605 | error: | |
1606 | return ret; | |
1607 | } | |
1608 | ||
07424f16 DG |
1609 | /* |
1610 | * This is an extension of create session that is ONLY and SHOULD only be used | |
1611 | * by the lttng command line program. It exists to avoid using URI parsing in | |
1612 | * the lttng client. | |
1613 | * | |
1614 | * We need the date and time for the trace path subdirectory for the case where | |
1615 | * the user does NOT define one using either -o or -U. Using the normal | |
1616 | * lttng_create_session API call, we have no clue on the session daemon side if | |
1617 | * the URL was generated automatically by the client or define by the user. | |
1618 | * | |
1619 | * So this function "wrapper" is hidden from the public API, takes the datetime | |
1620 | * string and appends it if necessary to the URI subdirectory before sending it | |
1621 | * to the session daemon. | |
1622 | * | |
1623 | * With this extra function, the lttng_create_session call behavior is not | |
1624 | * changed and the timestamp is appended to the URI on the session daemon side | |
1625 | * if necessary. | |
1626 | */ | |
1627 | int _lttng_create_session_ext(const char *name, const char *url, | |
1628 | const char *datetime) | |
1629 | { | |
3dd05a85 | 1630 | int ret; |
07424f16 DG |
1631 | ssize_t size; |
1632 | struct lttcomm_session_msg lsm; | |
1633 | struct lttng_uri *uris = NULL; | |
1634 | ||
d0b96690 | 1635 | if (name == NULL || datetime == NULL || url == NULL) { |
2f70b271 | 1636 | return -LTTNG_ERR_INVALID; |
07424f16 DG |
1637 | } |
1638 | ||
1639 | memset(&lsm, 0, sizeof(lsm)); | |
1640 | ||
1641 | lsm.cmd_type = LTTNG_CREATE_SESSION; | |
cac3069d | 1642 | lttng_ctl_copy_string(lsm.session.name, name, sizeof(lsm.session.name)); |
07424f16 DG |
1643 | |
1644 | /* There should never be a data URL */ | |
1645 | size = parse_str_urls_to_uri(url, NULL, &uris); | |
1646 | if (size < 0) { | |
3dd05a85 DG |
1647 | ret = -LTTNG_ERR_INVALID; |
1648 | goto error; | |
07424f16 DG |
1649 | } |
1650 | ||
1651 | lsm.u.uri.size = size; | |
1652 | ||
4b35a6b3 | 1653 | if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) { |
da4aa2b8 DG |
1654 | /* Don't append datetime if the name was automatically created. */ |
1655 | if (strncmp(name, DEFAULT_SESSION_NAME "-", | |
1656 | strlen(DEFAULT_SESSION_NAME) + 1)) { | |
1657 | ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s", | |
1658 | name, datetime); | |
1659 | } else { | |
1660 | ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s", name); | |
1661 | } | |
07424f16 DG |
1662 | if (ret < 0) { |
1663 | PERROR("snprintf uri subdir"); | |
3dd05a85 DG |
1664 | ret = -LTTNG_ERR_FATAL; |
1665 | goto error; | |
07424f16 DG |
1666 | } |
1667 | } | |
1668 | ||
cac3069d DG |
1669 | ret = lttng_ctl_ask_sessiond_varlen(&lsm, uris, |
1670 | sizeof(struct lttng_uri) * size, NULL); | |
3dd05a85 DG |
1671 | |
1672 | error: | |
1673 | free(uris); | |
1674 | return ret; | |
07424f16 DG |
1675 | } |
1676 | ||
806e2684 DG |
1677 | /* |
1678 | * For a given session name, this call checks if the data is ready to be read | |
1679 | * or is still being extracted by the consumer(s) hence not ready to be used by | |
1680 | * any readers. | |
1681 | */ | |
6d805429 | 1682 | int lttng_data_pending(const char *session_name) |
806e2684 DG |
1683 | { |
1684 | int ret; | |
1685 | struct lttcomm_session_msg lsm; | |
1686 | ||
1687 | if (session_name == NULL) { | |
1688 | return -LTTNG_ERR_INVALID; | |
1689 | } | |
1690 | ||
6d805429 | 1691 | lsm.cmd_type = LTTNG_DATA_PENDING; |
806e2684 | 1692 | |
cac3069d DG |
1693 | lttng_ctl_copy_string(lsm.session.name, session_name, |
1694 | sizeof(lsm.session.name)); | |
806e2684 | 1695 | |
cac3069d | 1696 | ret = lttng_ctl_ask_sessiond(&lsm, NULL); |
806e2684 DG |
1697 | |
1698 | /* | |
cac3069d DG |
1699 | * The lttng_ctl_ask_sessiond function negate the return code if it's not |
1700 | * LTTNG_OK so getting -1 means that the reply ret_code was 1 thus meaning | |
1701 | * that the data is available. Yes it is hackish but for now this is the | |
1702 | * only way. | |
806e2684 DG |
1703 | */ |
1704 | if (ret == -1) { | |
1705 | ret = 1; | |
1706 | } | |
1707 | ||
1708 | return ret; | |
1709 | } | |
1710 | ||
fac6795d DG |
1711 | /* |
1712 | * lib constructor | |
1713 | */ | |
1714 | static void __attribute__((constructor)) init() | |
1715 | { | |
1716 | /* Set default session group */ | |
bbccc3d2 | 1717 | lttng_set_tracing_group(DEFAULT_TRACING_GROUP); |
44a5e5eb DG |
1718 | /* Set socket for health check */ |
1719 | (void) set_health_socket_path(); | |
fac6795d | 1720 | } |
49cca668 DG |
1721 | |
1722 | /* | |
1723 | * lib destructor | |
1724 | */ | |
1725 | static void __attribute__((destructor)) lttng_ctl_exit() | |
1726 | { | |
1727 | free(tracing_group); | |
1728 | } |