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