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