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