Session daemon health check support
[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 <lttng/lttng.h>
35
36 #include "filter-parser.h"
37 #include "filter-ast.h"
38 #include "filter-bytecode.h"
39 #include "memstream.h"
40
41 #ifdef DEBUG
42 const int print_xml = 1;
43 #define dbg_printf(fmt, args...) \
44 printf("[debug liblttng-ctl] " fmt, ## args)
45 #else
46 const int print_xml = 0;
47 #define dbg_printf(fmt, args...) \
48 do { \
49 /* do nothing but check printf format */ \
50 if (0) \
51 printf("[debug liblttnctl] " fmt, ## args); \
52 } while (0)
53 #endif
54
55
56 /* Socket to session daemon for communication */
57 static int sessiond_socket;
58 static char sessiond_sock_path[PATH_MAX];
59 static char health_sock_path[PATH_MAX];
60
61 /* Variables */
62 static char *tracing_group;
63 static int connected;
64
65 /* Global */
66
67 /*
68 * Those two variables are used by error.h to silent or control the verbosity of
69 * error message. They are global to the library so application linking with it
70 * are able to compile correctly and also control verbosity of the library.
71 *
72 * Note that it is *not* possible to silent ERR() and PERROR() macros.
73 */
74 int lttng_opt_quiet;
75 int lttng_opt_verbose;
76
77 /*
78 * Copy string from src to dst and enforce null terminated byte.
79 */
80 static void copy_string(char *dst, const char *src, size_t len)
81 {
82 if (src && dst) {
83 strncpy(dst, src, len);
84 /* Enforce the NULL terminated byte */
85 dst[len - 1] = '\0';
86 } else if (dst) {
87 dst[0] = '\0';
88 }
89 }
90
91 /*
92 * Copy domain to lttcomm_session_msg domain.
93 *
94 * If domain is unknown, default domain will be the kernel.
95 */
96 static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
97 {
98 if (src && dst) {
99 switch (src->type) {
100 case LTTNG_DOMAIN_KERNEL:
101 case LTTNG_DOMAIN_UST:
102 /*
103 case LTTNG_DOMAIN_UST_EXEC_NAME:
104 case LTTNG_DOMAIN_UST_PID:
105 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
106 */
107 memcpy(dst, src, sizeof(struct lttng_domain));
108 break;
109 default:
110 memset(dst, 0, sizeof(struct lttng_domain));
111 dst->type = LTTNG_DOMAIN_KERNEL;
112 break;
113 }
114 }
115 }
116
117 /*
118 * Send lttcomm_session_msg to the session daemon.
119 *
120 * On success, returns the number of bytes sent (>=0)
121 * On error, returns -1
122 */
123 static int send_session_msg(struct lttcomm_session_msg *lsm)
124 {
125 int ret;
126
127 if (!connected) {
128 ret = -ENOTCONN;
129 goto end;
130 }
131
132 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
133 sizeof(struct lttcomm_session_msg));
134
135 end:
136 return ret;
137 }
138
139 /*
140 * Send var len data to the session daemon.
141 *
142 * On success, returns the number of bytes sent (>=0)
143 * On error, returns -1
144 */
145 static int send_session_varlen(void *data, size_t len)
146 {
147 int ret;
148
149 if (!connected) {
150 ret = -ENOTCONN;
151 goto end;
152 }
153 if (!data || !len) {
154 ret = 0;
155 goto end;
156 }
157
158 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
159
160 end:
161 return ret;
162 }
163
164 /*
165 * Receive data from the sessiond socket.
166 *
167 * On success, returns the number of bytes received (>=0)
168 * On error, returns -1 (recvmsg() error) or -ENOTCONN
169 */
170 static int recv_data_sessiond(void *buf, size_t len)
171 {
172 int ret;
173
174 if (!connected) {
175 ret = -ENOTCONN;
176 goto end;
177 }
178
179 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
180
181 end:
182 return ret;
183 }
184
185 /*
186 * Check if we are in the specified group.
187 *
188 * If yes return 1, else return -1.
189 */
190 static int check_tracing_group(const char *grp_name)
191 {
192 struct group *grp_tracing; /* no free(). See getgrnam(3) */
193 gid_t *grp_list;
194 int grp_list_size, grp_id, i;
195 int ret = -1;
196
197 /* Get GID of group 'tracing' */
198 grp_tracing = getgrnam(grp_name);
199 if (!grp_tracing) {
200 /* If grp_tracing is NULL, the group does not exist. */
201 goto end;
202 }
203
204 /* Get number of supplementary group IDs */
205 grp_list_size = getgroups(0, NULL);
206 if (grp_list_size < 0) {
207 perror("getgroups");
208 goto end;
209 }
210
211 /* Alloc group list of the right size */
212 grp_list = malloc(grp_list_size * sizeof(gid_t));
213 if (!grp_list) {
214 perror("malloc");
215 goto end;
216 }
217 grp_id = getgroups(grp_list_size, grp_list);
218 if (grp_id < 0) {
219 perror("getgroups");
220 goto free_list;
221 }
222
223 for (i = 0; i < grp_list_size; i++) {
224 if (grp_list[i] == grp_tracing->gr_gid) {
225 ret = 1;
226 break;
227 }
228 }
229
230 free_list:
231 free(grp_list);
232
233 end:
234 return ret;
235 }
236
237 /*
238 * Try connect to session daemon with sock_path.
239 *
240 * Return 0 on success, else -1
241 */
242 static int try_connect_sessiond(const char *sock_path)
243 {
244 int ret;
245
246 /* If socket exist, we check if the daemon listens for connect. */
247 ret = access(sock_path, F_OK);
248 if (ret < 0) {
249 /* Not alive */
250 return -1;
251 }
252
253 ret = lttcomm_connect_unix_sock(sock_path);
254 if (ret < 0) {
255 /* Not alive */
256 return -1;
257 }
258
259 ret = lttcomm_close_unix_sock(ret);
260 if (ret < 0) {
261 perror("lttcomm_close_unix_sock");
262 }
263
264 return 0;
265 }
266
267 /*
268 * Set sessiond socket path by putting it in the global
269 * sessiond_sock_path variable.
270 * Returns 0 on success,
271 * -ENOMEM on failure (the sessiond socket path is somehow too long)
272 */
273 static int set_session_daemon_path(void)
274 {
275 int ret;
276 int in_tgroup = 0; /* In tracing group */
277 uid_t uid;
278
279 uid = getuid();
280
281 if (uid != 0) {
282 /* Are we in the tracing group ? */
283 in_tgroup = check_tracing_group(tracing_group);
284 }
285
286 if ((uid == 0) || in_tgroup) {
287 copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
288 sizeof(sessiond_sock_path));
289 }
290
291 if (uid != 0) {
292 if (in_tgroup) {
293 /* Tracing group */
294 ret = try_connect_sessiond(sessiond_sock_path);
295 if (ret >= 0) {
296 goto end;
297 }
298 /* Global session daemon not available... */
299 }
300 /* ...or not in tracing group (and not root), default */
301
302 /*
303 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
304 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
305 */
306 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
307 DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
308 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
309 return -ENOMEM;
310 }
311 }
312 end:
313 return 0;
314 }
315
316 /*
317 * Connect to the LTTng session daemon.
318 *
319 * On success, return 0. On error, return -1.
320 */
321 static int connect_sessiond(void)
322 {
323 int ret;
324
325 ret = set_session_daemon_path();
326 if (ret < 0) {
327 return -1; /* set_session_daemon_path() returns -ENOMEM */
328 }
329
330 /* Connect to the sesssion daemon */
331 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
332 if (ret < 0) {
333 return ret;
334 }
335
336 sessiond_socket = ret;
337 connected = 1;
338
339 return 0;
340 }
341
342 /*
343 * Clean disconnect from the session daemon.
344 * On success, return 0. On error, return -1.
345 */
346 static int disconnect_sessiond(void)
347 {
348 int ret = 0;
349
350 if (connected) {
351 ret = lttcomm_close_unix_sock(sessiond_socket);
352 sessiond_socket = 0;
353 connected = 0;
354 }
355
356 return ret;
357 }
358
359 /*
360 * Ask the session daemon a specific command and put the data into buf.
361 * Takes extra var. len. data as input to send to the session daemon.
362 *
363 * Return size of data (only payload, not header) or a negative error code.
364 */
365 static int ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
366 void *vardata,
367 size_t varlen,
368 void **buf)
369 {
370 int ret;
371 size_t size;
372 void *data = NULL;
373 struct lttcomm_lttng_msg llm;
374
375 ret = connect_sessiond();
376 if (ret < 0) {
377 goto end;
378 }
379
380 /* Send command to session daemon */
381 ret = send_session_msg(lsm);
382 if (ret < 0) {
383 goto end;
384 }
385 /* Send var len data */
386 ret = send_session_varlen(vardata, varlen);
387 if (ret < 0) {
388 goto end;
389 }
390
391 /* Get header from data transmission */
392 ret = recv_data_sessiond(&llm, sizeof(llm));
393 if (ret < 0) {
394 goto end;
395 }
396
397 /* Check error code if OK */
398 if (llm.ret_code != LTTCOMM_OK) {
399 ret = -llm.ret_code;
400 goto end;
401 }
402
403 size = llm.data_size;
404 if (size == 0) {
405 /* If client free with size 0 */
406 if (buf != NULL) {
407 *buf = NULL;
408 }
409 ret = 0;
410 goto end;
411 }
412
413 data = (void*) malloc(size);
414
415 /* Get payload data */
416 ret = recv_data_sessiond(data, size);
417 if (ret < 0) {
418 free(data);
419 goto end;
420 }
421
422 /*
423 * Extra protection not to dereference a NULL pointer. If buf is NULL at
424 * this point, an error is returned and data is freed.
425 */
426 if (buf == NULL) {
427 ret = -1;
428 free(data);
429 goto end;
430 }
431
432 *buf = data;
433 ret = size;
434
435 end:
436 disconnect_sessiond();
437 return ret;
438 }
439
440 /*
441 * Ask the session daemon a specific command and put the data into buf.
442 *
443 * Return size of data (only payload, not header) or a negative error code.
444 */
445 static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
446 {
447 return ask_sessiond_varlen(lsm, NULL, 0, buf);
448 }
449
450 /*
451 * Create lttng handle and return pointer.
452 * The returned pointer will be NULL in case of malloc() error.
453 */
454 struct lttng_handle *lttng_create_handle(const char *session_name,
455 struct lttng_domain *domain)
456 {
457 struct lttng_handle *handle;
458
459 handle = malloc(sizeof(struct lttng_handle));
460 if (handle == NULL) {
461 perror("malloc handle");
462 goto end;
463 }
464
465 /* Copy session name */
466 copy_string(handle->session_name, session_name,
467 sizeof(handle->session_name));
468
469 /* Copy lttng domain */
470 copy_lttng_domain(&handle->domain, domain);
471
472 end:
473 return handle;
474 }
475
476 /*
477 * Destroy handle by free(3) the pointer.
478 */
479 void lttng_destroy_handle(struct lttng_handle *handle)
480 {
481 if (handle) {
482 free(handle);
483 }
484 }
485
486 /*
487 * Register an outside consumer.
488 * Returns size of returned session payload data or a negative error code.
489 */
490 int lttng_register_consumer(struct lttng_handle *handle,
491 const char *socket_path)
492 {
493 struct lttcomm_session_msg lsm;
494
495 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
496 copy_string(lsm.session.name, handle->session_name,
497 sizeof(lsm.session.name));
498 copy_lttng_domain(&lsm.domain, &handle->domain);
499
500 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
501
502 return ask_sessiond(&lsm, NULL);
503 }
504
505 /*
506 * Start tracing for all traces of the session.
507 * Returns size of returned session payload data or a negative error code.
508 */
509 int lttng_start_tracing(const char *session_name)
510 {
511 struct lttcomm_session_msg lsm;
512
513 if (session_name == NULL) {
514 return -1;
515 }
516
517 lsm.cmd_type = LTTNG_START_TRACE;
518
519 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
520
521 return ask_sessiond(&lsm, NULL);
522 }
523
524 /*
525 * Stop tracing for all traces of the session.
526 * Returns size of returned session payload data or a negative error code.
527 */
528 int lttng_stop_tracing(const char *session_name)
529 {
530 struct lttcomm_session_msg lsm;
531
532 if (session_name == NULL) {
533 return -1;
534 }
535
536 lsm.cmd_type = LTTNG_STOP_TRACE;
537
538 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
539
540 return ask_sessiond(&lsm, NULL);
541 }
542
543 /*
544 * Add context to event and/or channel.
545 * If event_name is NULL, the context is applied to all events of the channel.
546 * If channel_name is NULL, a lookup of the event's channel is done.
547 * If both are NULL, the context is applied to all events of all channels.
548 *
549 * Returns the size of the returned payload data or a negative error code.
550 */
551 int lttng_add_context(struct lttng_handle *handle,
552 struct lttng_event_context *ctx, const char *event_name,
553 const char *channel_name)
554 {
555 struct lttcomm_session_msg lsm;
556
557 /* Safety check. Both are mandatory */
558 if (handle == NULL || ctx == NULL) {
559 return -1;
560 }
561
562 memset(&lsm, 0, sizeof(lsm));
563
564 lsm.cmd_type = LTTNG_ADD_CONTEXT;
565
566 /* Copy channel name */
567 copy_string(lsm.u.context.channel_name, channel_name,
568 sizeof(lsm.u.context.channel_name));
569 /* Copy event name */
570 copy_string(lsm.u.context.event_name, event_name,
571 sizeof(lsm.u.context.event_name));
572
573 copy_lttng_domain(&lsm.domain, &handle->domain);
574
575 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
576
577 copy_string(lsm.session.name, handle->session_name,
578 sizeof(lsm.session.name));
579
580 return ask_sessiond(&lsm, NULL);
581 }
582
583 /*
584 * Enable event(s) for a channel.
585 * If no event name is specified, all events are enabled.
586 * If no channel name is specified, the default 'channel0' is used.
587 * Returns size of returned session payload data or a negative error code.
588 */
589 int lttng_enable_event(struct lttng_handle *handle,
590 struct lttng_event *ev, const char *channel_name)
591 {
592 struct lttcomm_session_msg lsm;
593
594 if (handle == NULL || ev == NULL) {
595 return -1;
596 }
597
598 memset(&lsm, 0, sizeof(lsm));
599
600 /* If no channel name, we put the default name */
601 if (channel_name == NULL) {
602 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
603 sizeof(lsm.u.enable.channel_name));
604 } else {
605 copy_string(lsm.u.enable.channel_name, channel_name,
606 sizeof(lsm.u.enable.channel_name));
607 }
608
609 copy_lttng_domain(&lsm.domain, &handle->domain);
610
611 if (ev->name[0] != '\0') {
612 lsm.cmd_type = LTTNG_ENABLE_EVENT;
613 } else {
614 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
615 }
616 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
617
618 copy_string(lsm.session.name, handle->session_name,
619 sizeof(lsm.session.name));
620
621 return ask_sessiond(&lsm, NULL);
622 }
623
624 /*
625 * set filter for an event
626 * Return negative error value on error.
627 * Return size of returned session payload data if OK.
628 */
629
630 int lttng_set_event_filter(struct lttng_handle *handle,
631 const char *event_name, const char *channel_name,
632 const char *filter_expression)
633 {
634 struct lttcomm_session_msg lsm;
635 struct filter_parser_ctx *ctx;
636 FILE *fmem;
637 int ret = 0;
638
639 /* Safety check. */
640 if (handle == NULL) {
641 return -1;
642 }
643
644 if (!filter_expression) {
645 return 0;
646 }
647
648 /*
649 * casting const to non-const, as the underlying function will
650 * use it in read-only mode.
651 */
652 fmem = lttng_fmemopen((void *) filter_expression,
653 strlen(filter_expression), "r");
654 if (!fmem) {
655 fprintf(stderr, "Error opening memory as stream\n");
656 return -ENOMEM;
657 }
658 ctx = filter_parser_ctx_alloc(fmem);
659 if (!ctx) {
660 fprintf(stderr, "Error allocating parser\n");
661 ret = -ENOMEM;
662 goto alloc_error;
663 }
664 ret = filter_parser_ctx_append_ast(ctx);
665 if (ret) {
666 fprintf(stderr, "Parse error\n");
667 ret = -EINVAL;
668 goto parse_error;
669 }
670 ret = filter_visitor_set_parent(ctx);
671 if (ret) {
672 fprintf(stderr, "Set parent error\n");
673 ret = -EINVAL;
674 goto parse_error;
675 }
676 if (print_xml) {
677 ret = filter_visitor_print_xml(ctx, stdout, 0);
678 if (ret) {
679 fflush(stdout);
680 fprintf(stderr, "XML print error\n");
681 ret = -EINVAL;
682 goto parse_error;
683 }
684 }
685
686 dbg_printf("Generating IR... ");
687 fflush(stdout);
688 ret = filter_visitor_ir_generate(ctx);
689 if (ret) {
690 fprintf(stderr, "Generate IR error\n");
691 ret = -EINVAL;
692 goto parse_error;
693 }
694 dbg_printf("done\n");
695
696 dbg_printf("Validating IR... ");
697 fflush(stdout);
698 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
699 if (ret) {
700 ret = -EINVAL;
701 goto parse_error;
702 }
703 dbg_printf("done\n");
704
705 dbg_printf("Generating bytecode... ");
706 fflush(stdout);
707 ret = filter_visitor_bytecode_generate(ctx);
708 if (ret) {
709 fprintf(stderr, "Generate bytecode error\n");
710 ret = -EINVAL;
711 goto parse_error;
712 }
713 dbg_printf("done\n");
714 dbg_printf("Size of bytecode generated: %u bytes.\n",
715 bytecode_get_len(&ctx->bytecode->b));
716
717 memset(&lsm, 0, sizeof(lsm));
718
719 lsm.cmd_type = LTTNG_SET_FILTER;
720
721 /* Copy channel name */
722 copy_string(lsm.u.filter.channel_name, channel_name,
723 sizeof(lsm.u.filter.channel_name));
724 /* Copy event name */
725 copy_string(lsm.u.filter.event_name, event_name,
726 sizeof(lsm.u.filter.event_name));
727 lsm.u.filter.bytecode_len = sizeof(ctx->bytecode->b)
728 + bytecode_get_len(&ctx->bytecode->b);
729
730 copy_lttng_domain(&lsm.domain, &handle->domain);
731
732 copy_string(lsm.session.name, handle->session_name,
733 sizeof(lsm.session.name));
734
735 ret = ask_sessiond_varlen(&lsm, &ctx->bytecode->b,
736 lsm.u.filter.bytecode_len, NULL);
737
738 filter_bytecode_free(ctx);
739 filter_ir_free(ctx);
740 filter_parser_ctx_free(ctx);
741 if (fclose(fmem) != 0) {
742 perror("fclose");
743 }
744 return ret;
745
746 parse_error:
747 filter_bytecode_free(ctx);
748 filter_ir_free(ctx);
749 filter_parser_ctx_free(ctx);
750 alloc_error:
751 if (fclose(fmem) != 0) {
752 perror("fclose");
753 }
754 return ret;
755 }
756
757 /*
758 * Disable event(s) of a channel and domain.
759 * If no event name is specified, all events are disabled.
760 * If no channel name is specified, the default 'channel0' is used.
761 * Returns size of returned session payload data or a negative error code.
762 */
763 int lttng_disable_event(struct lttng_handle *handle, const char *name,
764 const char *channel_name)
765 {
766 struct lttcomm_session_msg lsm;
767
768 if (handle == NULL) {
769 return -1;
770 }
771
772 memset(&lsm, 0, sizeof(lsm));
773
774 if (channel_name) {
775 copy_string(lsm.u.disable.channel_name, channel_name,
776 sizeof(lsm.u.disable.channel_name));
777 } else {
778 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
779 sizeof(lsm.u.disable.channel_name));
780 }
781
782 copy_lttng_domain(&lsm.domain, &handle->domain);
783
784 if (name != NULL) {
785 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
786 lsm.cmd_type = LTTNG_DISABLE_EVENT;
787 } else {
788 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
789 }
790
791 copy_string(lsm.session.name, handle->session_name,
792 sizeof(lsm.session.name));
793
794 return ask_sessiond(&lsm, NULL);
795 }
796
797 /*
798 * Enable channel per domain
799 * Returns size of returned session payload data or a negative error code.
800 */
801 int lttng_enable_channel(struct lttng_handle *handle,
802 struct lttng_channel *chan)
803 {
804 struct lttcomm_session_msg lsm;
805
806 /*
807 * NULL arguments are forbidden. No default values.
808 */
809 if (handle == NULL || chan == NULL) {
810 return -1;
811 }
812
813 memset(&lsm, 0, sizeof(lsm));
814
815 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
816
817 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
818
819 copy_lttng_domain(&lsm.domain, &handle->domain);
820
821 copy_string(lsm.session.name, handle->session_name,
822 sizeof(lsm.session.name));
823
824 return ask_sessiond(&lsm, NULL);
825 }
826
827 /*
828 * All tracing will be stopped for registered events of the channel.
829 * Returns size of returned session payload data or a negative error code.
830 */
831 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
832 {
833 struct lttcomm_session_msg lsm;
834
835 /* Safety check. Both are mandatory */
836 if (handle == NULL || name == NULL) {
837 return -1;
838 }
839
840 memset(&lsm, 0, sizeof(lsm));
841
842 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
843
844 copy_string(lsm.u.disable.channel_name, name,
845 sizeof(lsm.u.disable.channel_name));
846
847 copy_lttng_domain(&lsm.domain, &handle->domain);
848
849 copy_string(lsm.session.name, handle->session_name,
850 sizeof(lsm.session.name));
851
852 return ask_sessiond(&lsm, NULL);
853 }
854
855 /*
856 * Lists all available tracepoints of domain.
857 * Sets the contents of the events array.
858 * Returns the number of lttng_event entries in events;
859 * on error, returns a negative value.
860 */
861 int lttng_list_tracepoints(struct lttng_handle *handle,
862 struct lttng_event **events)
863 {
864 int ret;
865 struct lttcomm_session_msg lsm;
866
867 if (handle == NULL) {
868 return -1;
869 }
870
871 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
872 copy_lttng_domain(&lsm.domain, &handle->domain);
873
874 ret = ask_sessiond(&lsm, (void **) events);
875 if (ret < 0) {
876 return ret;
877 }
878
879 return ret / sizeof(struct lttng_event);
880 }
881
882 /*
883 * Lists all available tracepoint fields of domain.
884 * Sets the contents of the event field array.
885 * Returns the number of lttng_event_field entries in events;
886 * on error, returns a negative value.
887 */
888 int lttng_list_tracepoint_fields(struct lttng_handle *handle,
889 struct lttng_event_field **fields)
890 {
891 int ret;
892 struct lttcomm_session_msg lsm;
893
894 if (handle == NULL) {
895 return -1;
896 }
897
898 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
899 copy_lttng_domain(&lsm.domain, &handle->domain);
900
901 ret = ask_sessiond(&lsm, (void **) fields);
902 if (ret < 0) {
903 return ret;
904 }
905
906 return ret / sizeof(struct lttng_event_field);
907 }
908
909 /*
910 * Returns a human readable string describing
911 * the error code (a negative value).
912 */
913 const char *lttng_strerror(int code)
914 {
915 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
916 if (code > -LTTCOMM_OK) {
917 return "Ended with errors";
918 }
919
920 return lttcomm_get_readable_code(code);
921 }
922
923 /*
924 * Create a brand new session using name and path.
925 * Returns size of returned session payload data or a negative error code.
926 */
927 int lttng_create_session(const char *name, const char *path)
928 {
929 struct lttcomm_session_msg lsm;
930
931 lsm.cmd_type = LTTNG_CREATE_SESSION;
932 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
933 copy_string(lsm.session.path, path, sizeof(lsm.session.path));
934
935 return ask_sessiond(&lsm, NULL);
936 }
937
938 /*
939 * Create a new tracing session using a name, URIs and a consumer enable flag.
940 */
941 int lttng_create_session_uri(const char *name, struct lttng_uri *ctrl_uri,
942 struct lttng_uri *data_uri, unsigned int enable_consumer)
943 {
944 struct lttcomm_session_msg lsm;
945
946 /* Name and ctrl_uri are mandatory */
947 if (name == NULL || ctrl_uri == NULL) {
948 return -1;
949 }
950
951 lsm.cmd_type = LTTNG_CREATE_SESSION_URI;
952
953 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
954 /* Anything bigger than zero, the consumer(s) will be enabled */
955 lsm.u.create_uri.enable_consumer = enable_consumer;
956 memcpy(&lsm.u.create_uri.ctrl_uri, ctrl_uri,
957 sizeof(lsm.u.create_uri.ctrl_uri));
958 if (data_uri) {
959 /*
960 * The only possible scenario where data_uri is NULL is for a local
961 * consumer where the output is at a specified path name on the
962 * filesystem.
963 */
964 memcpy(&lsm.u.create_uri.data_uri, data_uri,
965 sizeof(lsm.u.create_uri.data_uri));
966 }
967
968 return ask_sessiond(&lsm, NULL);
969 }
970
971 /*
972 * Destroy session using name.
973 * Returns size of returned session payload data or a negative error code.
974 */
975 int lttng_destroy_session(const char *session_name)
976 {
977 struct lttcomm_session_msg lsm;
978
979 if (session_name == NULL) {
980 return -1;
981 }
982
983 lsm.cmd_type = LTTNG_DESTROY_SESSION;
984
985 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
986
987 return ask_sessiond(&lsm, NULL);
988 }
989
990 /*
991 * Ask the session daemon for all available sessions.
992 * Sets the contents of the sessions array.
993 * Returns the number of lttng_session entries in sessions;
994 * on error, returns a negative value.
995 */
996 int lttng_list_sessions(struct lttng_session **sessions)
997 {
998 int ret;
999 struct lttcomm_session_msg lsm;
1000
1001 lsm.cmd_type = LTTNG_LIST_SESSIONS;
1002 ret = ask_sessiond(&lsm, (void**) sessions);
1003 if (ret < 0) {
1004 return ret;
1005 }
1006
1007 return ret / sizeof(struct lttng_session);
1008 }
1009
1010 /*
1011 * Ask the session daemon for all available domains of a session.
1012 * Sets the contents of the domains array.
1013 * Returns the number of lttng_domain entries in domains;
1014 * on error, returns a negative value.
1015 */
1016 int lttng_list_domains(const char *session_name,
1017 struct lttng_domain **domains)
1018 {
1019 int ret;
1020 struct lttcomm_session_msg lsm;
1021
1022 if (session_name == NULL) {
1023 return -1;
1024 }
1025
1026 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1027
1028 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
1029
1030 ret = ask_sessiond(&lsm, (void**) domains);
1031 if (ret < 0) {
1032 return ret;
1033 }
1034
1035 return ret / sizeof(struct lttng_domain);
1036 }
1037
1038 /*
1039 * Ask the session daemon for all available channels of a session.
1040 * Sets the contents of the channels array.
1041 * Returns the number of lttng_channel entries in channels;
1042 * on error, returns a negative value.
1043 */
1044 int lttng_list_channels(struct lttng_handle *handle,
1045 struct lttng_channel **channels)
1046 {
1047 int ret;
1048 struct lttcomm_session_msg lsm;
1049
1050 if (handle == NULL) {
1051 return -1;
1052 }
1053
1054 lsm.cmd_type = LTTNG_LIST_CHANNELS;
1055 copy_string(lsm.session.name, handle->session_name,
1056 sizeof(lsm.session.name));
1057
1058 copy_lttng_domain(&lsm.domain, &handle->domain);
1059
1060 ret = ask_sessiond(&lsm, (void**) channels);
1061 if (ret < 0) {
1062 return ret;
1063 }
1064
1065 return ret / sizeof(struct lttng_channel);
1066 }
1067
1068 /*
1069 * Ask the session daemon for all available events of a session channel.
1070 * Sets the contents of the events array.
1071 * Returns the number of lttng_event entries in events;
1072 * on error, returns a negative value.
1073 */
1074 int lttng_list_events(struct lttng_handle *handle,
1075 const char *channel_name, struct lttng_event **events)
1076 {
1077 int ret;
1078 struct lttcomm_session_msg lsm;
1079
1080 /* Safety check. An handle and channel name are mandatory */
1081 if (handle == NULL || channel_name == NULL) {
1082 return -1;
1083 }
1084
1085 lsm.cmd_type = LTTNG_LIST_EVENTS;
1086 copy_string(lsm.session.name, handle->session_name,
1087 sizeof(lsm.session.name));
1088 copy_string(lsm.u.list.channel_name, channel_name,
1089 sizeof(lsm.u.list.channel_name));
1090
1091 copy_lttng_domain(&lsm.domain, &handle->domain);
1092
1093 ret = ask_sessiond(&lsm, (void**) events);
1094 if (ret < 0) {
1095 return ret;
1096 }
1097
1098 return ret / sizeof(struct lttng_event);
1099 }
1100
1101 /*
1102 * Sets the tracing_group variable with name.
1103 * This function allocates memory pointed to by tracing_group.
1104 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1105 */
1106 int lttng_set_tracing_group(const char *name)
1107 {
1108 if (name == NULL) {
1109 return -1;
1110 }
1111
1112 if (asprintf(&tracing_group, "%s", name) < 0) {
1113 return -ENOMEM;
1114 }
1115
1116 return 0;
1117 }
1118
1119 /*
1120 * Returns size of returned session payload data or a negative error code.
1121 */
1122 int lttng_calibrate(struct lttng_handle *handle,
1123 struct lttng_calibrate *calibrate)
1124 {
1125 struct lttcomm_session_msg lsm;
1126
1127 /* Safety check. NULL pointer are forbidden */
1128 if (handle == NULL || calibrate == NULL) {
1129 return -1;
1130 }
1131
1132 lsm.cmd_type = LTTNG_CALIBRATE;
1133 copy_lttng_domain(&lsm.domain, &handle->domain);
1134
1135 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1136
1137 return ask_sessiond(&lsm, NULL);
1138 }
1139
1140 /*
1141 * Set default channel attributes.
1142 * If either or both of the arguments are null, attr content is zeroe'd.
1143 */
1144 void lttng_channel_set_default_attr(struct lttng_domain *domain,
1145 struct lttng_channel_attr *attr)
1146 {
1147 /* Safety check */
1148 if (attr == NULL || domain == NULL) {
1149 return;
1150 }
1151
1152 memset(attr, 0, sizeof(struct lttng_channel_attr));
1153
1154 switch (domain->type) {
1155 case LTTNG_DOMAIN_KERNEL:
1156 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1157 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1158 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1159
1160 attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
1161 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1162 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1163 break;
1164 case LTTNG_DOMAIN_UST:
1165 #if 0
1166 case LTTNG_DOMAIN_UST_EXEC_NAME:
1167 case LTTNG_DOMAIN_UST_PID:
1168 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1169 #endif
1170 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1171 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1172 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1173
1174 attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
1175 attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
1176 attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
1177 break;
1178 default:
1179 /* Default behavior: leave set to 0. */
1180 break;
1181 }
1182 }
1183
1184 /*
1185 * Check if session daemon is alive.
1186 *
1187 * Return 1 if alive or 0 if not.
1188 * On error returns a negative value.
1189 */
1190 int lttng_session_daemon_alive(void)
1191 {
1192 int ret;
1193
1194 ret = set_session_daemon_path();
1195 if (ret < 0) {
1196 /* Error */
1197 return ret;
1198 }
1199
1200 if (strlen(sessiond_sock_path) == 0) {
1201 /* No socket path set. Weird error */
1202 return -1;
1203 }
1204
1205 ret = try_connect_sessiond(sessiond_sock_path);
1206 if (ret < 0) {
1207 /* Not alive */
1208 return 0;
1209 }
1210
1211 /* Is alive */
1212 return 1;
1213 }
1214
1215 /*
1216 * Set URI for a consumer for a session and domain.
1217 *
1218 * Return 0 on success, else a negative value.
1219 */
1220 int lttng_set_consumer_uri(struct lttng_handle *handle, struct lttng_uri *uri)
1221 {
1222 struct lttcomm_session_msg lsm;
1223
1224 if (handle == NULL || uri == NULL) {
1225 return -1;
1226 }
1227
1228 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1229
1230 copy_string(lsm.session.name, handle->session_name,
1231 sizeof(lsm.session.name));
1232 copy_lttng_domain(&lsm.domain, &handle->domain);
1233
1234 memcpy(&lsm.u.uri, uri, sizeof(lsm.u.uri));
1235
1236 return ask_sessiond(&lsm, NULL);
1237 }
1238
1239 /*
1240 * Enable consumer for a session and domain.
1241 *
1242 * Return 0 on success, else a negative value.
1243 */
1244 int lttng_enable_consumer(struct lttng_handle *handle)
1245 {
1246 struct lttcomm_session_msg lsm;
1247
1248 if (handle == NULL) {
1249 return -1;
1250 }
1251
1252 lsm.cmd_type = LTTNG_ENABLE_CONSUMER;
1253
1254 copy_string(lsm.session.name, handle->session_name,
1255 sizeof(lsm.session.name));
1256 copy_lttng_domain(&lsm.domain, &handle->domain);
1257
1258 return ask_sessiond(&lsm, NULL);
1259 }
1260
1261 /*
1262 * Disable consumer for a session and domain.
1263 *
1264 * Return 0 on success, else a negative value.
1265 */
1266 int lttng_disable_consumer(struct lttng_handle *handle)
1267 {
1268 struct lttcomm_session_msg lsm;
1269
1270 if (handle == NULL) {
1271 return -1;
1272 }
1273
1274 lsm.cmd_type = LTTNG_DISABLE_CONSUMER;
1275
1276 copy_string(lsm.session.name, handle->session_name,
1277 sizeof(lsm.session.name));
1278 copy_lttng_domain(&lsm.domain, &handle->domain);
1279
1280 return ask_sessiond(&lsm, NULL);
1281 }
1282
1283 /*
1284 * Set health socket path by putting it in the global health_sock_path
1285 * variable.
1286 *
1287 * Returns 0 on success or assert(0) on ENOMEM.
1288 */
1289 static int set_health_socket_path(void)
1290 {
1291 int ret;
1292 int in_tgroup = 0; /* In tracing group */
1293 uid_t uid;
1294 const char *home;
1295
1296 uid = getuid();
1297
1298 if (uid != 0) {
1299 /* Are we in the tracing group ? */
1300 in_tgroup = check_tracing_group(tracing_group);
1301 }
1302
1303 if ((uid == 0) || in_tgroup) {
1304 copy_string(health_sock_path, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK,
1305 sizeof(health_sock_path));
1306 }
1307
1308 if (uid != 0) {
1309 /*
1310 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1311 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1312 */
1313 home = getenv("HOME");
1314 if (home == NULL) {
1315 /* Fallback in /tmp .. */
1316 home = "/tmp";
1317 }
1318
1319 ret = snprintf(health_sock_path, sizeof(health_sock_path),
1320 DEFAULT_HOME_HEALTH_UNIX_SOCK, home);
1321 if ((ret < 0) || (ret >= sizeof(health_sock_path))) {
1322 /* ENOMEM at this point... just kill the control lib. */
1323 assert(0);
1324 }
1325 }
1326
1327 return 0;
1328 }
1329
1330 /*
1331 * Check session daemon health for a specific health component.
1332 *
1333 * Return 0 if health is OK or else 1 if BAD. A return value of -1 indicate
1334 * that the control library was not able to connect to the session daemon
1335 * health socket.
1336 *
1337 * Any other positive value is an lttcomm error which can be translate with
1338 * lttng_strerror().
1339 */
1340 int lttng_health_check(enum lttng_health_component c)
1341 {
1342 int sock, ret;
1343 struct lttcomm_health_msg msg;
1344 struct lttcomm_health_data reply;
1345
1346 /* Connect to the sesssion daemon */
1347 sock = lttcomm_connect_unix_sock(health_sock_path);
1348 if (sock < 0) {
1349 ret = -1;
1350 goto error;
1351 }
1352
1353 msg.cmd = LTTNG_HEALTH_CHECK;
1354 msg.component = c;
1355
1356 ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg));
1357 if (ret < 0) {
1358 goto close_error;
1359 }
1360
1361 ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply));
1362 if (ret < 0) {
1363 goto close_error;
1364 }
1365
1366 ret = reply.ret_code;
1367
1368 close_error:
1369 close(sock);
1370
1371 error:
1372 return ret;
1373 }
1374
1375 /*
1376 * lib constructor
1377 */
1378 static void __attribute__((constructor)) init()
1379 {
1380 /* Set default session group */
1381 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
1382 /* Set socket for health check */
1383 (void) set_health_socket_path();
1384 }
This page took 0.096831 seconds and 4 git commands to generate.