fix: if tracing group does not exist, do not report a client error
[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
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #define _GNU_SOURCE
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 /* Socket to session daemon for communication */
37 static int sessiond_socket;
38 static char sessiond_sock_path[PATH_MAX];
39
40 /* Variables */
41 static char *tracing_group;
42 static int connected;
43
44 /*
45 * Copy string from src to dst and enforce null terminated byte.
46 */
47 static void copy_string(char *dst, const char *src, size_t len)
48 {
49 if (src && dst) {
50 strncpy(dst, src, len);
51 /* Enforce the NULL terminated byte */
52 dst[len - 1] = '\0';
53 } else if (dst) {
54 dst[0] = '\0';
55 }
56 }
57
58 /*
59 * Copy domain to lttcomm_session_msg domain.
60 *
61 * If domain is unknown, default domain will be the kernel.
62 */
63 static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
64 {
65 if (src && dst) {
66 switch (src->type) {
67 case LTTNG_DOMAIN_KERNEL:
68 case LTTNG_DOMAIN_UST:
69 /*
70 case LTTNG_DOMAIN_UST_EXEC_NAME:
71 case LTTNG_DOMAIN_UST_PID:
72 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
73 */
74 memcpy(dst, src, sizeof(struct lttng_domain));
75 break;
76 default:
77 memset(dst, 0, sizeof(struct lttng_domain));
78 dst->type = LTTNG_DOMAIN_KERNEL;
79 break;
80 }
81 }
82 }
83
84 /*
85 * Send lttcomm_session_msg to the session daemon.
86 *
87 * On success, returns the number of bytes sent (>=0)
88 * On error, returns -1
89 */
90 static int send_session_msg(struct lttcomm_session_msg *lsm)
91 {
92 int ret;
93
94 if (!connected) {
95 ret = -ENOTCONN;
96 goto end;
97 }
98
99 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
100 sizeof(struct lttcomm_session_msg));
101
102 end:
103 return ret;
104 }
105
106 /*
107 * Receive data from the sessiond socket.
108 *
109 * On success, returns the number of bytes received (>=0)
110 * On error, returns -1 (recvmsg() error) or -ENOTCONN
111 */
112 static int recv_data_sessiond(void *buf, size_t len)
113 {
114 int ret;
115
116 if (!connected) {
117 ret = -ENOTCONN;
118 goto end;
119 }
120
121 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
122
123 end:
124 return ret;
125 }
126
127 /*
128 * Check if we are in the specified group.
129 *
130 * If yes return 1, else return -1.
131 */
132 static int check_tracing_group(const char *grp_name)
133 {
134 struct group *grp_tracing; /* no free(). See getgrnam(3) */
135 gid_t *grp_list;
136 int grp_list_size, grp_id, i;
137 int ret = -1;
138
139 /* Get GID of group 'tracing' */
140 grp_tracing = getgrnam(grp_name);
141 if (!grp_tracing) {
142 /* If grp_tracing is NULL, the group does not exist. */
143 goto end;
144 }
145
146 /* Get number of supplementary group IDs */
147 grp_list_size = getgroups(0, NULL);
148 if (grp_list_size < 0) {
149 perror("getgroups");
150 goto end;
151 }
152
153 /* Alloc group list of the right size */
154 grp_list = malloc(grp_list_size * sizeof(gid_t));
155 if (!grp_list) {
156 perror("malloc");
157 goto end;
158 }
159 grp_id = getgroups(grp_list_size, grp_list);
160 if (grp_id < 0) {
161 perror("getgroups");
162 goto free_list;
163 }
164
165 for (i = 0; i < grp_list_size; i++) {
166 if (grp_list[i] == grp_tracing->gr_gid) {
167 ret = 1;
168 break;
169 }
170 }
171
172 free_list:
173 free(grp_list);
174
175 end:
176 return ret;
177 }
178
179 /*
180 * Try connect to session daemon with sock_path.
181 *
182 * Return 0 on success, else -1
183 */
184 static int try_connect_sessiond(const char *sock_path)
185 {
186 int ret;
187
188 /* If socket exist, we check if the daemon listens for connect. */
189 ret = access(sock_path, F_OK);
190 if (ret < 0) {
191 /* Not alive */
192 return -1;
193 }
194
195 ret = lttcomm_connect_unix_sock(sock_path);
196 if (ret < 0) {
197 /* Not alive */
198 return -1;
199 }
200
201 ret = lttcomm_close_unix_sock(ret);
202 if (ret < 0) {
203 perror("lttcomm_close_unix_sock");
204 }
205
206 return 0;
207 }
208
209 /*
210 * Set sessiond socket path by putting it in the global
211 * sessiond_sock_path variable.
212 * Returns 0 on success,
213 * -ENOMEM on failure (the sessiond socket path is somehow too long)
214 */
215 static int set_session_daemon_path(void)
216 {
217 int ret;
218 int in_tgroup = 0; /* In tracing group */
219 uid_t uid;
220
221 uid = getuid();
222
223 if (uid != 0) {
224 /* Are we in the tracing group ? */
225 in_tgroup = check_tracing_group(tracing_group);
226 }
227
228 if ((uid == 0) || in_tgroup) {
229 copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
230 sizeof(sessiond_sock_path));
231 }
232
233 if (uid != 0) {
234 if (in_tgroup) {
235 /* Tracing group */
236 ret = try_connect_sessiond(sessiond_sock_path);
237 if (ret >= 0) {
238 goto end;
239 }
240 /* Global session daemon not available... */
241 }
242 /* ...or not in tracing group (and not root), default */
243
244 /*
245 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
246 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
247 */
248 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
249 DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
250 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
251 return -ENOMEM;
252 }
253 }
254 end:
255 return 0;
256 }
257
258 /*
259 * Connect to the LTTng session daemon.
260 *
261 * On success, return 0. On error, return -1.
262 */
263 static int connect_sessiond(void)
264 {
265 int ret;
266
267 ret = set_session_daemon_path();
268 if (ret < 0) {
269 return -1; /* set_session_daemon_path() returns -ENOMEM */
270 }
271
272 /* Connect to the sesssion daemon */
273 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
274 if (ret < 0) {
275 return ret;
276 }
277
278 sessiond_socket = ret;
279 connected = 1;
280
281 return 0;
282 }
283
284 /*
285 * Clean disconnect from the session daemon.
286 * On success, return 0. On error, return -1.
287 */
288 static int disconnect_sessiond(void)
289 {
290 int ret = 0;
291
292 if (connected) {
293 ret = lttcomm_close_unix_sock(sessiond_socket);
294 sessiond_socket = 0;
295 connected = 0;
296 }
297
298 return ret;
299 }
300
301 /*
302 * Ask the session daemon a specific command and put the data into buf.
303 *
304 * Return size of data (only payload, not header) or a negative error code.
305 */
306 static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
307 {
308 int ret;
309 size_t size;
310 void *data = NULL;
311 struct lttcomm_lttng_msg llm;
312
313 ret = connect_sessiond();
314 if (ret < 0) {
315 goto end;
316 }
317
318 /* Send command to session daemon */
319 ret = send_session_msg(lsm);
320 if (ret < 0) {
321 goto end;
322 }
323
324 /* Get header from data transmission */
325 ret = recv_data_sessiond(&llm, sizeof(llm));
326 if (ret < 0) {
327 goto end;
328 }
329
330 /* Check error code if OK */
331 if (llm.ret_code != LTTCOMM_OK) {
332 ret = -llm.ret_code;
333 goto end;
334 }
335
336 size = llm.data_size;
337 if (size == 0) {
338 /* If client free with size 0 */
339 if (buf != NULL) {
340 *buf = NULL;
341 }
342 ret = 0;
343 goto end;
344 }
345
346 data = (void*) malloc(size);
347
348 /* Get payload data */
349 ret = recv_data_sessiond(data, size);
350 if (ret < 0) {
351 free(data);
352 goto end;
353 }
354
355 /*
356 * Extra protection not to dereference a NULL pointer. If buf is NULL at
357 * this point, an error is returned and data is freed.
358 */
359 if (buf == NULL) {
360 ret = -1;
361 free(data);
362 goto end;
363 }
364
365 *buf = data;
366 ret = size;
367
368 end:
369 disconnect_sessiond();
370 return ret;
371 }
372
373 /*
374 * Create lttng handle and return pointer.
375 * The returned pointer will be NULL in case of malloc() error.
376 */
377 struct lttng_handle *lttng_create_handle(const char *session_name,
378 struct lttng_domain *domain)
379 {
380 struct lttng_handle *handle;
381
382 handle = malloc(sizeof(struct lttng_handle));
383 if (handle == NULL) {
384 perror("malloc handle");
385 goto end;
386 }
387
388 /* Copy session name */
389 copy_string(handle->session_name, session_name,
390 sizeof(handle->session_name));
391
392 /* Copy lttng domain */
393 copy_lttng_domain(&handle->domain, domain);
394
395 end:
396 return handle;
397 }
398
399 /*
400 * Destroy handle by free(3) the pointer.
401 */
402 void lttng_destroy_handle(struct lttng_handle *handle)
403 {
404 if (handle) {
405 free(handle);
406 }
407 }
408
409 /*
410 * Register an outside consumer.
411 * Returns size of returned session payload data or a negative error code.
412 */
413 int lttng_register_consumer(struct lttng_handle *handle,
414 const char *socket_path)
415 {
416 struct lttcomm_session_msg lsm;
417
418 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
419 copy_string(lsm.session.name, handle->session_name,
420 sizeof(lsm.session.name));
421 copy_lttng_domain(&lsm.domain, &handle->domain);
422
423 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
424
425 return ask_sessiond(&lsm, NULL);
426 }
427
428 /*
429 * Start tracing for all traces of the session.
430 * Returns size of returned session payload data or a negative error code.
431 */
432 int lttng_start_tracing(const char *session_name)
433 {
434 struct lttcomm_session_msg lsm;
435
436 if (session_name == NULL) {
437 return -1;
438 }
439
440 lsm.cmd_type = LTTNG_START_TRACE;
441
442 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
443
444 return ask_sessiond(&lsm, NULL);
445 }
446
447 /*
448 * Stop tracing for all traces of the session.
449 * Returns size of returned session payload data or a negative error code.
450 */
451 int lttng_stop_tracing(const char *session_name)
452 {
453 struct lttcomm_session_msg lsm;
454
455 if (session_name == NULL) {
456 return -1;
457 }
458
459 lsm.cmd_type = LTTNG_STOP_TRACE;
460
461 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
462
463 return ask_sessiond(&lsm, NULL);
464 }
465
466 /*
467 * Add context to event and/or channel.
468 * If event_name is NULL, the context is applied to all events of the channel.
469 * If channel_name is NULL, a lookup of the event's channel is done.
470 * If both are NULL, the context is applied to all events of all channels.
471 *
472 * Returns the size of the returned payload data or a negative error code.
473 */
474 int lttng_add_context(struct lttng_handle *handle,
475 struct lttng_event_context *ctx, const char *event_name,
476 const char *channel_name)
477 {
478 struct lttcomm_session_msg lsm;
479
480 /* Safety check. Both are mandatory */
481 if (handle == NULL || ctx == NULL) {
482 return -1;
483 }
484
485 memset(&lsm, 0, sizeof(lsm));
486
487 lsm.cmd_type = LTTNG_ADD_CONTEXT;
488
489 /* Copy channel name */
490 copy_string(lsm.u.context.channel_name, channel_name,
491 sizeof(lsm.u.context.channel_name));
492 /* Copy event name */
493 copy_string(lsm.u.context.event_name, event_name,
494 sizeof(lsm.u.context.event_name));
495
496 copy_lttng_domain(&lsm.domain, &handle->domain);
497
498 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
499
500 copy_string(lsm.session.name, handle->session_name,
501 sizeof(lsm.session.name));
502
503 return ask_sessiond(&lsm, NULL);
504 }
505
506 /*
507 * Enable event(s) for a channel.
508 * If no event name is specified, all events are enabled.
509 * If no channel name is specified, the default 'channel0' is used.
510 * Returns size of returned session payload data or a negative error code.
511 */
512 int lttng_enable_event(struct lttng_handle *handle,
513 struct lttng_event *ev, const char *channel_name)
514 {
515 struct lttcomm_session_msg lsm;
516
517 if (handle == NULL || ev == NULL) {
518 return -1;
519 }
520
521 memset(&lsm, 0, sizeof(lsm));
522
523 /* If no channel name, we put the default name */
524 if (channel_name == NULL) {
525 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
526 sizeof(lsm.u.enable.channel_name));
527 } else {
528 copy_string(lsm.u.enable.channel_name, channel_name,
529 sizeof(lsm.u.enable.channel_name));
530 }
531
532 copy_lttng_domain(&lsm.domain, &handle->domain);
533
534 if (ev->name[0] != '\0') {
535 lsm.cmd_type = LTTNG_ENABLE_EVENT;
536 } else {
537 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
538 }
539 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
540
541 copy_string(lsm.session.name, handle->session_name,
542 sizeof(lsm.session.name));
543
544 return ask_sessiond(&lsm, NULL);
545 }
546
547 /*
548 * Disable event(s) of a channel and domain.
549 * If no event name is specified, all events are disabled.
550 * If no channel name is specified, the default 'channel0' is used.
551 * Returns size of returned session payload data or a negative error code.
552 */
553 int lttng_disable_event(struct lttng_handle *handle, const char *name,
554 const char *channel_name)
555 {
556 struct lttcomm_session_msg lsm;
557
558 if (handle == NULL) {
559 return -1;
560 }
561
562 memset(&lsm, 0, sizeof(lsm));
563
564 if (channel_name) {
565 copy_string(lsm.u.disable.channel_name, channel_name,
566 sizeof(lsm.u.disable.channel_name));
567 } else {
568 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
569 sizeof(lsm.u.disable.channel_name));
570 }
571
572 copy_lttng_domain(&lsm.domain, &handle->domain);
573
574 if (name != NULL) {
575 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
576 lsm.cmd_type = LTTNG_DISABLE_EVENT;
577 } else {
578 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
579 }
580
581 copy_string(lsm.session.name, handle->session_name,
582 sizeof(lsm.session.name));
583
584 return ask_sessiond(&lsm, NULL);
585 }
586
587 /*
588 * Enable channel per domain
589 * Returns size of returned session payload data or a negative error code.
590 */
591 int lttng_enable_channel(struct lttng_handle *handle,
592 struct lttng_channel *chan)
593 {
594 struct lttcomm_session_msg lsm;
595
596 /*
597 * NULL arguments are forbidden. No default values.
598 */
599 if (handle == NULL || chan == NULL) {
600 return -1;
601 }
602
603 memset(&lsm, 0, sizeof(lsm));
604
605 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
606
607 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
608
609 copy_lttng_domain(&lsm.domain, &handle->domain);
610
611 copy_string(lsm.session.name, handle->session_name,
612 sizeof(lsm.session.name));
613
614 return ask_sessiond(&lsm, NULL);
615 }
616
617 /*
618 * All tracing will be stopped for registered events of the channel.
619 * Returns size of returned session payload data or a negative error code.
620 */
621 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
622 {
623 struct lttcomm_session_msg lsm;
624
625 /* Safety check. Both are mandatory */
626 if (handle == NULL || name == NULL) {
627 return -1;
628 }
629
630 memset(&lsm, 0, sizeof(lsm));
631
632 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
633
634 copy_string(lsm.u.disable.channel_name, name,
635 sizeof(lsm.u.disable.channel_name));
636
637 copy_lttng_domain(&lsm.domain, &handle->domain);
638
639 copy_string(lsm.session.name, handle->session_name,
640 sizeof(lsm.session.name));
641
642 return ask_sessiond(&lsm, NULL);
643 }
644
645 /*
646 * Lists all available tracepoints of domain.
647 * Sets the contents of the events array.
648 * Returns the number of lttng_event entries in events;
649 * on error, returns a negative value.
650 */
651 int lttng_list_tracepoints(struct lttng_handle *handle,
652 struct lttng_event **events)
653 {
654 int ret;
655 struct lttcomm_session_msg lsm;
656
657 if (handle == NULL) {
658 return -1;
659 }
660
661 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
662 copy_lttng_domain(&lsm.domain, &handle->domain);
663
664 ret = ask_sessiond(&lsm, (void **) events);
665 if (ret < 0) {
666 return ret;
667 }
668
669 return ret / sizeof(struct lttng_event);
670 }
671
672 /*
673 * Returns a human readable string describing
674 * the error code (a negative value).
675 */
676 const char *lttng_strerror(int code)
677 {
678 /* lttcomm error codes range from -LTTCOMM_OK down to -LTTCOMM_NR */
679 if (code > -LTTCOMM_OK) {
680 return "Ended with errors";
681 }
682
683 return lttcomm_get_readable_code(code);
684 }
685
686 /*
687 * Create a brand new session using name and path.
688 * Returns size of returned session payload data or a negative error code.
689 */
690 int lttng_create_session(const char *name, const char *path)
691 {
692 struct lttcomm_session_msg lsm;
693
694 lsm.cmd_type = LTTNG_CREATE_SESSION;
695 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
696 copy_string(lsm.session.path, path, sizeof(lsm.session.path));
697
698 return ask_sessiond(&lsm, NULL);
699 }
700
701 /*
702 * Destroy session using name.
703 * Returns size of returned session payload data or a negative error code.
704 */
705 int lttng_destroy_session(const char *session_name)
706 {
707 struct lttcomm_session_msg lsm;
708
709 if (session_name == NULL) {
710 return -1;
711 }
712
713 lsm.cmd_type = LTTNG_DESTROY_SESSION;
714
715 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
716
717 return ask_sessiond(&lsm, NULL);
718 }
719
720 /*
721 * Ask the session daemon for all available sessions.
722 * Sets the contents of the sessions array.
723 * Returns the number of lttng_session entries in sessions;
724 * on error, returns a negative value.
725 */
726 int lttng_list_sessions(struct lttng_session **sessions)
727 {
728 int ret;
729 struct lttcomm_session_msg lsm;
730
731 lsm.cmd_type = LTTNG_LIST_SESSIONS;
732 ret = ask_sessiond(&lsm, (void**) sessions);
733 if (ret < 0) {
734 return ret;
735 }
736
737 return ret / sizeof(struct lttng_session);
738 }
739
740 /*
741 * Ask the session daemon for all available domains of a session.
742 * Sets the contents of the domains array.
743 * Returns the number of lttng_domain entries in domains;
744 * on error, returns a negative value.
745 */
746 int lttng_list_domains(const char *session_name,
747 struct lttng_domain **domains)
748 {
749 int ret;
750 struct lttcomm_session_msg lsm;
751
752 if (session_name == NULL) {
753 return -1;
754 }
755
756 lsm.cmd_type = LTTNG_LIST_DOMAINS;
757
758 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
759
760 ret = ask_sessiond(&lsm, (void**) domains);
761 if (ret < 0) {
762 return ret;
763 }
764
765 return ret / sizeof(struct lttng_domain);
766 }
767
768 /*
769 * Ask the session daemon for all available channels of a session.
770 * Sets the contents of the channels array.
771 * Returns the number of lttng_channel entries in channels;
772 * on error, returns a negative value.
773 */
774 int lttng_list_channels(struct lttng_handle *handle,
775 struct lttng_channel **channels)
776 {
777 int ret;
778 struct lttcomm_session_msg lsm;
779
780 if (handle == NULL) {
781 return -1;
782 }
783
784 lsm.cmd_type = LTTNG_LIST_CHANNELS;
785 copy_string(lsm.session.name, handle->session_name,
786 sizeof(lsm.session.name));
787
788 copy_lttng_domain(&lsm.domain, &handle->domain);
789
790 ret = ask_sessiond(&lsm, (void**) channels);
791 if (ret < 0) {
792 return ret;
793 }
794
795 return ret / sizeof(struct lttng_channel);
796 }
797
798 /*
799 * Ask the session daemon for all available events of a session channel.
800 * Sets the contents of the events array.
801 * Returns the number of lttng_event entries in events;
802 * on error, returns a negative value.
803 */
804 int lttng_list_events(struct lttng_handle *handle,
805 const char *channel_name, struct lttng_event **events)
806 {
807 int ret;
808 struct lttcomm_session_msg lsm;
809
810 /* Safety check. An handle and channel name are mandatory */
811 if (handle == NULL || channel_name == NULL) {
812 return -1;
813 }
814
815 lsm.cmd_type = LTTNG_LIST_EVENTS;
816 copy_string(lsm.session.name, handle->session_name,
817 sizeof(lsm.session.name));
818 copy_string(lsm.u.list.channel_name, channel_name,
819 sizeof(lsm.u.list.channel_name));
820
821 copy_lttng_domain(&lsm.domain, &handle->domain);
822
823 ret = ask_sessiond(&lsm, (void**) events);
824 if (ret < 0) {
825 return ret;
826 }
827
828 return ret / sizeof(struct lttng_event);
829 }
830
831 /*
832 * Sets the tracing_group variable with name.
833 * This function allocates memory pointed to by tracing_group.
834 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
835 */
836 int lttng_set_tracing_group(const char *name)
837 {
838 if (name == NULL) {
839 return -1;
840 }
841
842 if (asprintf(&tracing_group, "%s", name) < 0) {
843 return -ENOMEM;
844 }
845
846 return 0;
847 }
848
849 /*
850 * Returns size of returned session payload data or a negative error code.
851 */
852 int lttng_calibrate(struct lttng_handle *handle,
853 struct lttng_calibrate *calibrate)
854 {
855 struct lttcomm_session_msg lsm;
856
857 /* Safety check. NULL pointer are forbidden */
858 if (handle == NULL || calibrate == NULL) {
859 return -1;
860 }
861
862 lsm.cmd_type = LTTNG_CALIBRATE;
863 copy_lttng_domain(&lsm.domain, &handle->domain);
864
865 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
866
867 return ask_sessiond(&lsm, NULL);
868 }
869
870 /*
871 * Set default channel attributes.
872 * If either or both of the arguments are null, attr content is zeroe'd.
873 */
874 void lttng_channel_set_default_attr(struct lttng_domain *domain,
875 struct lttng_channel_attr *attr)
876 {
877 memset(attr, 0, sizeof(struct lttng_channel_attr));
878
879 /* Safety check */
880 if (attr == NULL || domain == NULL) {
881 return;
882 }
883
884 switch (domain->type) {
885 case LTTNG_DOMAIN_KERNEL:
886 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
887 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
888 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
889
890 attr->subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
891 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
892 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
893 break;
894 case LTTNG_DOMAIN_UST:
895 #if 0
896 case LTTNG_DOMAIN_UST_EXEC_NAME:
897 case LTTNG_DOMAIN_UST_PID:
898 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
899 #endif
900 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
901 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
902 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
903
904 attr->subbuf_size = DEFAULT_UST_CHANNEL_SUBBUF_SIZE;
905 attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
906 attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
907 break;
908 default:
909 /* Default behavior: leave set to 0. */
910 break;
911 }
912 }
913
914 /*
915 * Check if session daemon is alive.
916 *
917 * Return 1 if alive or 0 if not.
918 * On error returns a negative value.
919 */
920 int lttng_session_daemon_alive(void)
921 {
922 int ret;
923
924 ret = set_session_daemon_path();
925 if (ret < 0) {
926 /* Error */
927 return ret;
928 }
929
930 if (strlen(sessiond_sock_path) == 0) {
931 /* No socket path set. Weird error */
932 return -1;
933 }
934
935 ret = try_connect_sessiond(sessiond_sock_path);
936 if (ret < 0) {
937 /* Not alive */
938 return 0;
939 }
940
941 /* Is alive */
942 return 1;
943 }
944
945 /*
946 * lib constructor
947 */
948 static void __attribute__((constructor)) init()
949 {
950 /* Set default session group */
951 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
952 }
This page took 0.048385 seconds and 4 git commands to generate.