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