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