Add missing error handling in liblttngctl
[lttng-tools.git] / liblttngctl / lttngctl.c
... / ...
CommitLineData
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 <errno.h>
25#include <grp.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
31#include <lttng/lttng.h>
32
33#include <lttng-sessiond-comm.h>
34#include "lttngerr.h"
35#include "lttng-share.h"
36
37/* Socket to session daemon for communication */
38static int sessiond_socket;
39static char sessiond_sock_path[PATH_MAX];
40
41/* Variables */
42static char *tracing_group;
43static int connected;
44
45/*
46 * Copy string from src to dst and enforce null terminated byte.
47 */
48static void copy_string(char *dst, const char *src, size_t len)
49{
50 if (src && dst) {
51 strncpy(dst, src, len);
52 /* Enforce the NULL terminated byte */
53 dst[len - 1] = '\0';
54 } else if (dst) {
55 dst[0] = '\0';
56 }
57}
58
59/*
60 * Copy domain to lttcomm_session_msg domain.
61 *
62 * If domain is unknown, default domain will be the kernel.
63 */
64static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
65{
66 if (src && dst) {
67 switch (src->type) {
68 case LTTNG_DOMAIN_KERNEL:
69 case LTTNG_DOMAIN_UST:
70 case LTTNG_DOMAIN_UST_EXEC_NAME:
71 case LTTNG_DOMAIN_UST_PID:
72 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
73 memcpy(dst, src, sizeof(struct lttng_domain));
74 break;
75 default:
76 dst->type = LTTNG_DOMAIN_KERNEL;
77 break;
78 }
79 }
80}
81
82/*
83 * Send lttcomm_session_msg to the session daemon.
84 *
85 * On success, return 0
86 * On error, return error code
87 */
88static int send_session_msg(struct lttcomm_session_msg *lsm)
89{
90 int ret;
91
92 if (!connected) {
93 ret = -ENOTCONN;
94 goto end;
95 }
96
97 ret = lttcomm_send_unix_sock(sessiond_socket, lsm,
98 sizeof(struct lttcomm_session_msg));
99
100end:
101 return ret;
102}
103
104/*
105 * Receive data from the sessiond socket.
106 *
107 * On success, return 0
108 * On error, return recv() error code
109 */
110static int recv_data_sessiond(void *buf, size_t len)
111{
112 int ret;
113
114 if (!connected) {
115 ret = -ENOTCONN;
116 goto end;
117 }
118
119 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
120
121end:
122 return ret;
123}
124
125/*
126 * Check if the specified group name exist.
127 *
128 * If yes return 1, else return -1.
129 */
130static int check_tracing_group(const char *grp_name)
131{
132 struct group *grp_tracing; /* no free(). See getgrnam(3) */
133 gid_t *grp_list;
134 int grp_list_size, grp_id, i;
135 int ret = -1;
136
137 /* Get GID of group 'tracing' */
138 grp_tracing = getgrnam(grp_name);
139 if (grp_tracing == NULL) {
140 /* NULL means not found also. getgrnam(3) */
141 if (errno != 0) {
142 perror("getgrnam");
143 }
144 goto end;
145 }
146
147 /* Get number of supplementary group IDs */
148 grp_list_size = getgroups(0, NULL);
149 if (grp_list_size < 0) {
150 perror("getgroups");
151 goto end;
152 }
153
154 /* Alloc group list of the right size */
155 grp_list = malloc(grp_list_size * sizeof(gid_t));
156 if (!grp_list) {
157 ret = -1;
158 goto end;
159 }
160 grp_id = getgroups(grp_list_size, grp_list);
161 if (grp_id < -1) {
162 perror("getgroups");
163 goto free_list;
164 }
165
166 for (i = 0; i < grp_list_size; i++) {
167 if (grp_list[i] == grp_tracing->gr_gid) {
168 ret = 1;
169 break;
170 }
171 }
172
173free_list:
174 free(grp_list);
175
176end:
177 return ret;
178}
179
180/*
181 * Try connect to session daemon with sock_path.
182 *
183 * Return 0 on success, else -1
184 */
185static int try_connect_sessiond(const char *sock_path)
186{
187 int ret;
188
189 /* If socket exist, we check if the daemon listens for connect. */
190 ret = access(sock_path, F_OK);
191 if (ret < 0) {
192 /* Not alive */
193 return -1;
194 }
195
196 ret = lttcomm_connect_unix_sock(sock_path);
197 if (ret < 0) {
198 /* Not alive */
199 return -1;
200 }
201
202 ret = lttcomm_close_unix_sock(ret);
203 if (ret < 0) {
204 perror("lttcomm_close_unix_sock");
205 }
206
207 return 0;
208}
209
210/*
211 * Set sessiond socket path by putting it in the global sessiond_sock_path
212 * variable.
213 */
214static int set_session_daemon_path(void)
215{
216 int ret;
217 int in_tgroup = 0; /* In tracing group */
218 uid_t uid;
219
220 uid = getuid();
221
222 if (uid != 0) {
223 /* Are we in the tracing group ? */
224 in_tgroup = check_tracing_group(tracing_group);
225 }
226
227 if (uid == 0) {
228 /* Root */
229 copy_string(sessiond_sock_path,
230 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
231 sizeof(sessiond_sock_path));
232 } else if (in_tgroup) {
233 /* Tracing group */
234 copy_string(sessiond_sock_path,
235 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
236 sizeof(sessiond_sock_path));
237
238 ret = try_connect_sessiond(sessiond_sock_path);
239 if (ret < 0) {
240 /* Global session daemon not available */
241 if (snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
242 DEFAULT_HOME_CLIENT_UNIX_SOCK,
243 getenv("HOME")) < 0) {
244 return -ENOMEM;
245 }
246 }
247 } else {
248 /* Not in tracing group and not root, default */
249 if (snprintf(sessiond_sock_path, PATH_MAX,
250 DEFAULT_HOME_CLIENT_UNIX_SOCK,
251 getenv("HOME")) < 0) {
252 return -ENOMEM;
253 }
254 }
255
256 return 0;
257}
258
259/*
260 * Connect to the LTTng session daemon.
261 *
262 * On success, return 0. On error, return -1.
263 */
264static int connect_sessiond(void)
265{
266 int ret;
267
268 ret = set_session_daemon_path();
269 if (ret < 0) {
270 return ret;
271 }
272
273 /* Connect to the sesssion daemon */
274 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
275 if (ret < 0) {
276 return ret;
277 }
278
279 sessiond_socket = ret;
280 connected = 1;
281
282 return 0;
283}
284
285/*
286 * Clean disconnect the session daemon.
287 */
288static 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).
305 */
306static 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 ret = 0;
339 goto end;
340 }
341
342 data = (void*) malloc(size);
343
344 /* Get payload data */
345 ret = recv_data_sessiond(data, size);
346 if (ret < 0) {
347 free(data);
348 goto end;
349 }
350
351 *buf = data;
352 ret = size;
353
354end:
355 disconnect_sessiond();
356 return ret;
357}
358
359/*
360 * Create lttng handle and return pointer.
361 */
362struct lttng_handle *lttng_create_handle(const char *session_name,
363 struct lttng_domain *domain)
364{
365 struct lttng_handle *handle;
366
367 handle = malloc(sizeof(struct lttng_handle));
368 if (handle == NULL) {
369 perror("malloc handle");
370 goto end;
371 }
372
373 /* Copy session name */
374 copy_string(handle->session_name, session_name,
375 sizeof(handle->session_name));
376
377 /* Copy lttng domain */
378 copy_lttng_domain(&handle->domain, domain);
379
380end:
381 return handle;
382}
383
384/*
385 * Destroy handle by free(3) the pointer.
386 */
387void lttng_destroy_handle(struct lttng_handle *handle)
388{
389 if (handle) {
390 free(handle);
391 }
392}
393
394/*
395 * Register an outside consumer.
396 */
397int lttng_register_consumer(struct lttng_handle *handle,
398 const char *socket_path)
399{
400 struct lttcomm_session_msg lsm;
401
402 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
403 copy_string(lsm.session.name, handle->session_name,
404 sizeof(lsm.session.name));
405 copy_lttng_domain(&lsm.domain, &handle->domain);
406
407 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
408
409 return ask_sessiond(&lsm, NULL);
410}
411
412/*
413 * Start tracing for all trace of the session.
414 */
415int lttng_start_tracing(struct lttng_handle *handle)
416{
417 struct lttcomm_session_msg lsm;
418
419 if (!handle) {
420 return -1;
421 }
422
423 lsm.cmd_type = LTTNG_START_TRACE;
424 copy_string(lsm.session.name, handle->session_name,
425 sizeof(lsm.session.name));
426
427 return ask_sessiond(&lsm, NULL);
428}
429
430/*
431 * Stop tracing for all trace of the session.
432 */
433int lttng_stop_tracing(struct lttng_handle *handle)
434{
435 struct lttcomm_session_msg lsm;
436
437 lsm.cmd_type = LTTNG_STOP_TRACE;
438 copy_string(lsm.session.name, handle->session_name,
439 sizeof(lsm.session.name));
440
441 return ask_sessiond(&lsm, NULL);
442}
443
444/*
445 * Add context to event or/and channel.
446 */
447int lttng_add_context(struct lttng_handle *handle,
448 struct lttng_event_context *ctx, const char *event_name,
449 const char *channel_name)
450{
451 struct lttcomm_session_msg lsm;
452
453 if (!handle) {
454 return -1;
455 }
456
457 lsm.cmd_type = LTTNG_ADD_CONTEXT;
458
459 /* Copy channel name */
460 copy_string(lsm.u.context.channel_name, channel_name,
461 sizeof(lsm.u.context.channel_name));
462 /* Copy event name */
463 copy_string(lsm.u.context.event_name, event_name,
464 sizeof(lsm.u.context.event_name));
465
466 copy_lttng_domain(&lsm.domain, &handle->domain);
467
468 if (ctx) {
469 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
470 }
471
472 copy_string(lsm.session.name, handle->session_name,
473 sizeof(lsm.session.name));
474
475 return ask_sessiond(&lsm, NULL);
476}
477
478/*
479 * Enable event
480 */
481int lttng_enable_event(struct lttng_handle *handle,
482 struct lttng_event *ev, const char *channel_name)
483{
484 struct lttcomm_session_msg lsm;
485
486 if (!handle) {
487 return -1;
488 }
489
490 if (channel_name == NULL) {
491 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
492 sizeof(lsm.u.enable.channel_name));
493 } else {
494 copy_string(lsm.u.enable.channel_name, channel_name,
495 sizeof(lsm.u.enable.channel_name));
496 }
497
498 copy_lttng_domain(&lsm.domain, &handle->domain);
499
500 if (ev) {
501 lsm.cmd_type = LTTNG_ENABLE_EVENT;
502 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
503 } else {
504 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
505 }
506
507 copy_string(lsm.session.name, handle->session_name,
508 sizeof(lsm.session.name));
509
510 return ask_sessiond(&lsm, NULL);
511}
512
513/*
514 * Disable event of a channel and domain.
515 */
516int lttng_disable_event(struct lttng_handle *handle, const char *name,
517 const char *channel_name)
518{
519 struct lttcomm_session_msg lsm;
520
521 if (!handle) {
522 return -1;
523 }
524
525 if (channel_name) {
526 copy_string(lsm.u.disable.channel_name, channel_name,
527 sizeof(lsm.u.disable.channel_name));
528 } else {
529 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
530 sizeof(lsm.u.disable.channel_name));
531 }
532
533 copy_lttng_domain(&lsm.domain, &handle->domain);
534
535 if (name == NULL) {
536 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
537 lsm.cmd_type = LTTNG_DISABLE_EVENT;
538 } else {
539 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
540 }
541
542 copy_string(lsm.session.name, handle->session_name,
543 sizeof(lsm.session.name));
544
545 return ask_sessiond(&lsm, NULL);
546}
547
548/*
549 * Enable channel per domain
550 */
551int lttng_enable_channel(struct lttng_handle *handle,
552 struct lttng_channel *chan)
553{
554 struct lttcomm_session_msg lsm;
555
556 if (!handle) {
557 return -1;
558 }
559
560 if (chan) {
561 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
562 }
563
564 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
565
566 copy_lttng_domain(&lsm.domain, &handle->domain);
567
568 copy_string(lsm.session.name, handle->session_name,
569 sizeof(lsm.session.name));
570
571 return ask_sessiond(&lsm, NULL);
572}
573
574/*
575 * All tracing will be stopped for registered events of the channel.
576 */
577int lttng_disable_channel(struct lttng_handle *handle, const char *name)
578{
579 struct lttcomm_session_msg lsm;
580
581 if (!handle) {
582 return -1;
583 }
584
585 if (name) {
586 copy_string(lsm.u.disable.channel_name, name,
587 sizeof(lsm.u.disable.channel_name));
588 }
589
590 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
591
592 copy_lttng_domain(&lsm.domain, &handle->domain);
593
594 copy_string(lsm.session.name, handle->session_name,
595 sizeof(lsm.session.name));
596
597 return ask_sessiond(&lsm, NULL);
598}
599
600/*
601 * List all available tracepoints of domain.
602 *
603 * Return the size (bytes) of the list and set the events array.
604 * On error, return negative value.
605 */
606int lttng_list_tracepoints(struct lttng_handle *handle,
607 struct lttng_event **events)
608{
609 int ret;
610 struct lttcomm_session_msg lsm;
611
612 if (!handle) {
613 return -1;
614 }
615
616 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
617 copy_lttng_domain(&lsm.domain, &handle->domain);
618
619 ret = ask_sessiond(&lsm, (void **) events);
620 if (ret < 0) {
621 return ret;
622 }
623
624 return ret / sizeof(struct lttng_event);
625}
626
627/*
628 * Return a human readable string of code
629 */
630const char *lttng_get_readable_code(int code)
631{
632 if (code > -LTTCOMM_OK) {
633 return "Ended with errors";
634 }
635
636 return lttcomm_get_readable_code(code);
637}
638
639/*
640 * Create a brand new session using name.
641 */
642int lttng_create_session(const char *name, const char *path)
643{
644 struct lttcomm_session_msg lsm;
645
646 lsm.cmd_type = LTTNG_CREATE_SESSION;
647 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
648 copy_string(lsm.session.path, path, sizeof(lsm.session.path));
649
650 return ask_sessiond(&lsm, NULL);
651}
652
653/*
654 * Destroy session using name.
655 */
656int lttng_destroy_session(struct lttng_handle *handle)
657{
658 struct lttcomm_session_msg lsm;
659
660 if (!handle) {
661 return -1;
662 }
663
664 lsm.cmd_type = LTTNG_DESTROY_SESSION;
665 copy_string(lsm.session.name, handle->session_name,
666 sizeof(lsm.session.name));
667
668 return ask_sessiond(&lsm, NULL);
669}
670
671/*
672 * Ask the session daemon for all available sessions.
673 *
674 * Return number of session.
675 * On error, return negative value.
676 */
677int lttng_list_sessions(struct lttng_session **sessions)
678{
679 int ret;
680 struct lttcomm_session_msg lsm;
681
682 lsm.cmd_type = LTTNG_LIST_SESSIONS;
683 ret = ask_sessiond(&lsm, (void**) sessions);
684 if (ret < 0) {
685 return ret;
686 }
687
688 return ret / sizeof(struct lttng_session);
689}
690
691/*
692 * List domain of a session.
693 */
694int lttng_list_domains(struct lttng_handle *handle,
695 struct lttng_domain **domains)
696{
697 int ret;
698 struct lttcomm_session_msg lsm;
699
700 if (!handle) {
701 return -1;
702 }
703
704 lsm.cmd_type = LTTNG_LIST_DOMAINS;
705
706 copy_string(lsm.session.name, handle->session_name,
707 sizeof(lsm.session.name));
708
709 ret = ask_sessiond(&lsm, (void**) domains);
710 if (ret < 0) {
711 return ret;
712 }
713
714 return ret / sizeof(struct lttng_domain);
715}
716
717/*
718 * List channels of a session
719 */
720int lttng_list_channels(struct lttng_handle *handle,
721 struct lttng_channel **channels)
722{
723 int ret;
724 struct lttcomm_session_msg lsm;
725
726 if (!handle) {
727 return -1;
728 }
729
730 lsm.cmd_type = LTTNG_LIST_CHANNELS;
731 copy_string(lsm.session.name, handle->session_name,
732 sizeof(lsm.session.name));
733
734 copy_lttng_domain(&lsm.domain, &handle->domain);
735
736 ret = ask_sessiond(&lsm, (void**) channels);
737 if (ret < 0) {
738 return ret;
739 }
740
741 return ret / sizeof(struct lttng_channel);
742}
743
744/*
745 * List events of a session channel.
746 */
747int lttng_list_events(struct lttng_handle *handle,
748 const char *channel_name, struct lttng_event **events)
749{
750 int ret;
751 struct lttcomm_session_msg lsm;
752
753 if (!handle) {
754 return -1;
755 }
756
757 lsm.cmd_type = LTTNG_LIST_EVENTS;
758 copy_string(lsm.session.name, handle->session_name,
759 sizeof(lsm.session.name));
760 copy_string(lsm.u.list.channel_name, channel_name,
761 sizeof(lsm.u.list.channel_name));
762
763 copy_lttng_domain(&lsm.domain, &handle->domain);
764
765 ret = ask_sessiond(&lsm, (void**) events);
766 if (ret < 0) {
767 return ret;
768 }
769
770 return ret / sizeof(struct lttng_event);
771}
772
773/*
774 * lttng_set_tracing_group
775 *
776 * Set tracing group variable with name. This function
777 * allocate memory pointed by tracing_group.
778 */
779int lttng_set_tracing_group(const char *name)
780{
781 if (asprintf(&tracing_group, "%s", name) < 0) {
782 return -ENOMEM;
783 }
784
785 return 0;
786}
787
788/*
789 * lttng_calibrate
790 */
791int lttng_calibrate(struct lttng_handle *handle,
792 struct lttng_calibrate *calibrate)
793{
794 struct lttcomm_session_msg lsm;
795
796 if (!handle) {
797 return -1;
798 }
799
800 lsm.cmd_type = LTTNG_CALIBRATE;
801 copy_lttng_domain(&lsm.domain, &handle->domain);
802
803 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
804
805 return ask_sessiond(&lsm, NULL);
806}
807
808/*
809 * Check if session daemon is alive.
810 *
811 * Return 1 if alive or 0 if not.
812 * On error return -1
813 */
814int lttng_session_daemon_alive(void)
815{
816 int ret;
817
818 ret = set_session_daemon_path();
819 if (ret < 0) {
820 /* Error */
821 return ret;
822 }
823
824 if (strlen(sessiond_sock_path) == 0) {
825 /* No socket path set. Weird error */
826 return -1;
827 }
828
829 ret = try_connect_sessiond(sessiond_sock_path);
830 if (ret < 0) {
831 /* Not alive */
832 return 0;
833 }
834
835 /* Is alive */
836 return 1;
837}
838
839/*
840 * lib constructor
841 */
842static void __attribute__((constructor)) init()
843{
844 /* Set default session group */
845 lttng_set_tracing_group(LTTNG_DEFAULT_TRACING_GROUP);
846}
This page took 0.025413 seconds and 4 git commands to generate.