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