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