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