Merge branch 'master' into benchmark
[lttng-tools.git] / ltt-sessiond / main.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#define _GNU_SOURCE
21#include <fcntl.h>
22#include <getopt.h>
23#include <grp.h>
24#include <limits.h>
25#include <poll.h>
26#include <pthread.h>
27#include <semaphore.h>
28#include <signal.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/mount.h>
33#include <sys/resource.h>
34#include <sys/socket.h>
35#include <sys/stat.h>
36#include <sys/types.h>
37#include <unistd.h>
38
39#include <ltt-kconsumerd.h>
40#include <lttng-sessiond-comm.h>
41#include <lttng/lttng-kconsumerd.h>
42#include <lttngerr.h>
43
44#include "context.h"
45#include "kernel-ctl.h"
46#include "ltt-sessiond.h"
47#include "traceable-app.h"
48#include "ust-ctl.h"
49#include "utils.h"
50
51#include "benchmark.h"
52
53/* Const values */
54const char default_home_dir[] = DEFAULT_HOME_DIR;
55const char default_tracing_group[] = LTTNG_DEFAULT_TRACING_GROUP;
56const char default_ust_sock_dir[] = DEFAULT_UST_SOCK_DIR;
57const char default_global_apps_pipe[] = DEFAULT_GLOBAL_APPS_PIPE;
58
59/* Variables */
60int opt_verbose; /* Not static for lttngerr.h */
61int opt_verbose_kconsumerd; /* Not static for lttngerr.h */
62int opt_quiet; /* Not static for lttngerr.h */
63
64const char *progname;
65const char *opt_tracing_group;
66static int opt_sig_parent;
67static int opt_daemon;
68static int is_root; /* Set to 1 if the daemon is running as root */
69static pid_t ppid; /* Parent PID for --sig-parent option */
70static pid_t kconsumerd_pid;
71static struct pollfd *kernel_pollfd;
72
73static char apps_unix_sock_path[PATH_MAX]; /* Global application Unix socket path */
74static char client_unix_sock_path[PATH_MAX]; /* Global client Unix socket path */
75static char kconsumerd_err_unix_sock_path[PATH_MAX]; /* kconsumerd error Unix socket path */
76static char kconsumerd_cmd_unix_sock_path[PATH_MAX]; /* kconsumerd command Unix socket path */
77
78/* Sockets and FDs */
79static int client_sock;
80static int apps_sock;
81static int kconsumerd_err_sock;
82static int kconsumerd_cmd_sock;
83static int kernel_tracer_fd;
84static int kernel_poll_pipe[2];
85
86/*
87 * Quit pipe for all threads. This permits a single cancellation point
88 * for all threads when receiving an event on the pipe.
89 */
90static int thread_quit_pipe[2];
91
92/* Pthread, Mutexes and Semaphores */
93static pthread_t kconsumerd_thread;
94static pthread_t apps_thread;
95static pthread_t client_thread;
96static pthread_t kernel_thread;
97static sem_t kconsumerd_sem;
98
99static pthread_mutex_t kconsumerd_pid_mutex; /* Mutex to control kconsumerd pid assignation */
100
101static int modprobe_remove_kernel_modules(void);
102
103/*
104 * Pointer initialized before thread creation.
105 *
106 * This points to the tracing session list containing the session count and a
107 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
108 * MUST NOT be taken if you call a public function in session.c.
109 *
110 * The lock is nested inside the structure: session_list_ptr->lock. Please use
111 * lock_session_list and unlock_session_list for lock acquisition.
112 */
113static struct ltt_session_list *session_list_ptr;
114
115static gid_t allowed_group(void)
116{
117 struct group *grp;
118
119 if (opt_tracing_group) {
120 grp = getgrnam(opt_tracing_group);
121 } else {
122 grp = getgrnam(default_tracing_group);
123 }
124 if (!grp) {
125 return -1;
126 } else {
127 return grp->gr_gid;
128 }
129}
130
131/*
132 * Init quit pipe.
133 *
134 * Return -1 on error or 0 if all pipes are created.
135 */
136static int init_thread_quit_pipe(void)
137{
138 int ret;
139
140 ret = pipe2(thread_quit_pipe, O_CLOEXEC);
141 if (ret < 0) {
142 perror("thread quit pipe");
143 goto error;
144 }
145
146error:
147 return ret;
148}
149
150/*
151 * Complete teardown of a kernel session. This free all data structure related
152 * to a kernel session and update counter.
153 */
154static void teardown_kernel_session(struct ltt_session *session)
155{
156 if (session->kernel_session != NULL) {
157 DBG("Tearing down kernel session");
158
159 /*
160 * If a custom kernel consumer was registered, close the socket before
161 * tearing down the complete kernel session structure
162 */
163 if (session->kernel_session->consumer_fd != kconsumerd_cmd_sock) {
164 lttcomm_close_unix_sock(session->kernel_session->consumer_fd);
165 }
166
167 trace_destroy_kernel_session(session->kernel_session);
168 /* Extra precaution */
169 session->kernel_session = NULL;
170 }
171}
172
173static void stop_threads(void)
174{
175 /* Stopping all threads */
176 DBG("Terminating all threads");
177 close(thread_quit_pipe[0]);
178 close(thread_quit_pipe[1]);
179}
180
181/*
182 * Cleanup the daemon
183 */
184static void cleanup(void)
185{
186 int ret;
187 char *cmd;
188 struct ltt_session *sess, *stmp;
189
190 DBG("Cleaning up");
191
192 /* <fun> */
193 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
194 "Matthew, BEET driven development works!%c[%dm",
195 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
196 /* </fun> */
197
198 DBG("Removing %s directory", LTTNG_RUNDIR);
199 ret = asprintf(&cmd, "rm -rf " LTTNG_RUNDIR);
200 if (ret < 0) {
201 ERR("asprintf failed. Something is really wrong!");
202 }
203
204 /* Remove lttng run directory */
205 ret = system(cmd);
206 if (ret < 0) {
207 ERR("Unable to clean " LTTNG_RUNDIR);
208 }
209
210 DBG("Cleaning up all session");
211
212 /* Destroy session list mutex */
213 if (session_list_ptr != NULL) {
214 pthread_mutex_destroy(&session_list_ptr->lock);
215
216 /* Cleanup ALL session */
217 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
218 teardown_kernel_session(sess);
219 // TODO complete session cleanup (including UST)
220 }
221 }
222
223 pthread_mutex_destroy(&kconsumerd_pid_mutex);
224
225 DBG("Closing kernel fd");
226 close(kernel_tracer_fd);
227
228 DBG("Unloading kernel modules");
229 modprobe_remove_kernel_modules();
230
231 /* OUTPUT BENCHMARK RESULTS */
232 bench_init();
233
234 bench_print_boot_process();
235
236 bench_close();
237 /* END BENCHMARK */
238}
239
240/*
241 * Send data on a unix socket using the liblttsessiondcomm API.
242 *
243 * Return lttcomm error code.
244 */
245static int send_unix_sock(int sock, void *buf, size_t len)
246{
247 /* Check valid length */
248 if (len <= 0) {
249 return -1;
250 }
251
252 return lttcomm_send_unix_sock(sock, buf, len);
253}
254
255/*
256 * Free memory of a command context structure.
257 */
258static void clean_command_ctx(struct command_ctx **cmd_ctx)
259{
260 DBG("Clean command context structure");
261 if (*cmd_ctx) {
262 if ((*cmd_ctx)->llm) {
263 free((*cmd_ctx)->llm);
264 }
265 if ((*cmd_ctx)->lsm) {
266 free((*cmd_ctx)->lsm);
267 }
268 free(*cmd_ctx);
269 *cmd_ctx = NULL;
270 }
271}
272
273/*
274 * Send all stream fds of kernel channel to the consumer.
275 */
276static int send_kconsumerd_channel_fds(int sock, struct ltt_kernel_channel *channel)
277{
278 int ret;
279 size_t nb_fd;
280 struct ltt_kernel_stream *stream;
281 struct lttcomm_kconsumerd_header lkh;
282 struct lttcomm_kconsumerd_msg lkm;
283
284 DBG("Sending fds of channel %s to kernel consumer", channel->channel->name);
285
286 nb_fd = channel->stream_count;
287
288 /* Setup header */
289 lkh.payload_size = nb_fd * sizeof(struct lttcomm_kconsumerd_msg);
290 lkh.cmd_type = ADD_STREAM;
291
292 DBG("Sending kconsumerd header");
293
294 ret = lttcomm_send_unix_sock(sock, &lkh, sizeof(struct lttcomm_kconsumerd_header));
295 if (ret < 0) {
296 perror("send kconsumerd header");
297 goto error;
298 }
299
300 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
301 if (stream->fd != 0) {
302 lkm.fd = stream->fd;
303 lkm.state = stream->state;
304 lkm.max_sb_size = channel->channel->attr.subbuf_size;
305 lkm.output = channel->channel->attr.output;
306 strncpy(lkm.path_name, stream->pathname, PATH_MAX);
307 lkm.path_name[PATH_MAX - 1] = '\0';
308
309 DBG("Sending fd %d to kconsumerd", lkm.fd);
310
311 ret = lttcomm_send_fds_unix_sock(sock, &lkm, &lkm.fd, 1, sizeof(lkm));
312 if (ret < 0) {
313 perror("send kconsumerd fd");
314 goto error;
315 }
316 }
317 }
318
319 DBG("Kconsumerd channel fds sent");
320
321 return 0;
322
323error:
324 return ret;
325}
326
327/*
328 * Send all stream fds of the kernel session to the consumer.
329 */
330static int send_kconsumerd_fds(struct ltt_kernel_session *session)
331{
332 int ret;
333 struct ltt_kernel_channel *chan;
334 struct lttcomm_kconsumerd_header lkh;
335 struct lttcomm_kconsumerd_msg lkm;
336
337 /* Setup header */
338 lkh.payload_size = sizeof(struct lttcomm_kconsumerd_msg);
339 lkh.cmd_type = ADD_STREAM;
340
341 DBG("Sending kconsumerd header for metadata");
342
343 ret = lttcomm_send_unix_sock(session->consumer_fd, &lkh, sizeof(struct lttcomm_kconsumerd_header));
344 if (ret < 0) {
345 perror("send kconsumerd header");
346 goto error;
347 }
348
349 DBG("Sending metadata stream fd");
350
351 /* Extra protection. It's NOT suppose to be set to 0 at this point */
352 if (session->consumer_fd == 0) {
353 session->consumer_fd = kconsumerd_cmd_sock;
354 }
355
356 if (session->metadata_stream_fd != 0) {
357 /* Send metadata stream fd first */
358 lkm.fd = session->metadata_stream_fd;
359 lkm.state = ACTIVE_FD;
360 lkm.max_sb_size = session->metadata->conf->attr.subbuf_size;
361 lkm.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
362 strncpy(lkm.path_name, session->metadata->pathname, PATH_MAX);
363 lkm.path_name[PATH_MAX - 1] = '\0';
364
365 ret = lttcomm_send_fds_unix_sock(session->consumer_fd, &lkm, &lkm.fd, 1, sizeof(lkm));
366 if (ret < 0) {
367 perror("send kconsumerd fd");
368 goto error;
369 }
370 }
371
372 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
373 ret = send_kconsumerd_channel_fds(session->consumer_fd, chan);
374 if (ret < 0) {
375 goto error;
376 }
377 }
378
379 DBG("Kconsumerd fds (metadata and channel streams) sent");
380
381 return 0;
382
383error:
384 return ret;
385}
386
387#ifdef DISABLED
388/*
389 * Return a socket connected to the libust communication socket of the
390 * application identified by the pid.
391 *
392 * If the pid is not found in the traceable list, return -1 to indicate error.
393 */
394static int ust_connect_app(pid_t pid)
395{
396 int sock;
397 struct ltt_traceable_app *lta;
398
399 DBG("Connect to application pid %d", pid);
400
401 lta = find_app_by_pid(pid);
402 if (lta == NULL) {
403 /* App not found */
404 DBG("Application pid %d not found", pid);
405 return -1;
406 }
407
408 sock = ustctl_connect_pid(lta->pid);
409 if (sock < 0) {
410 ERR("Fail connecting to the PID %d", pid);
411 }
412
413 return sock;
414}
415#endif /* DISABLED */
416
417/*
418 * Notify apps by writing 42 to a named pipe using name. Every applications
419 * waiting for a ltt-sessiond will be notified and re-register automatically to
420 * the session daemon.
421 *
422 * Return open or write error value.
423 */
424static int notify_apps(const char *name)
425{
426 int fd;
427 int ret = -1;
428
429 DBG("Notify the global application pipe");
430
431 /* Try opening the global pipe */
432 fd = open(name, O_WRONLY);
433 if (fd < 0) {
434 goto error;
435 }
436
437 /* Notify by writing on the pipe */
438 ret = write(fd, "42", 2);
439 if (ret < 0) {
440 perror("write");
441 }
442
443error:
444 return ret;
445}
446
447/*
448 * Setup the outgoing data buffer for the response (llm) by allocating the
449 * right amount of memory and copying the original information from the lsm
450 * structure.
451 *
452 * Return total size of the buffer pointed by buf.
453 */
454static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
455{
456 int ret, buf_size;
457
458 buf_size = size;
459
460 cmd_ctx->llm = malloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
461 if (cmd_ctx->llm == NULL) {
462 perror("malloc");
463 ret = -ENOMEM;
464 goto error;
465 }
466
467 /* Copy common data */
468 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
469 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
470
471 cmd_ctx->llm->data_size = size;
472 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
473
474 return buf_size;
475
476error:
477 return ret;
478}
479
480/*
481 * Update the kernel pollfd set of all channel fd available over all tracing
482 * session. Add the wakeup pipe at the end of the set.
483 */
484static int update_kernel_pollfd(void)
485{
486 int i = 0;
487 /*
488 * The wakup pipe and the quit pipe are needed so the number of fds starts
489 * at 2 for those pipes.
490 */
491 unsigned int nb_fd = 2;
492 struct ltt_session *session;
493 struct ltt_kernel_channel *channel;
494
495 DBG("Updating kernel_pollfd");
496
497 /* Get the number of channel of all kernel session */
498 lock_session_list();
499 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
500 lock_session(session);
501 if (session->kernel_session == NULL) {
502 unlock_session(session);
503 continue;
504 }
505 nb_fd += session->kernel_session->channel_count;
506 unlock_session(session);
507 }
508
509 DBG("Resizing kernel_pollfd to size %d", nb_fd);
510
511 kernel_pollfd = realloc(kernel_pollfd, nb_fd * sizeof(struct pollfd));
512 if (kernel_pollfd == NULL) {
513 perror("malloc kernel_pollfd");
514 goto error;
515 }
516
517 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
518 lock_session(session);
519 if (session->kernel_session == NULL) {
520 unlock_session(session);
521 continue;
522 }
523 if (i >= nb_fd) {
524 ERR("To much channel for kernel_pollfd size");
525 unlock_session(session);
526 break;
527 }
528 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
529 kernel_pollfd[i].fd = channel->fd;
530 kernel_pollfd[i].events = POLLIN | POLLRDNORM;
531 i++;
532 }
533 unlock_session(session);
534 }
535 unlock_session_list();
536
537 /* Adding wake up pipe */
538 kernel_pollfd[nb_fd - 2].fd = kernel_poll_pipe[0];
539 kernel_pollfd[nb_fd - 2].events = POLLIN;
540
541 /* Adding the quit pipe */
542 kernel_pollfd[nb_fd - 1].fd = thread_quit_pipe[0];
543
544 return nb_fd;
545
546error:
547 unlock_session_list();
548 return -1;
549}
550
551/*
552 * Find the channel fd from 'fd' over all tracing session. When found, check
553 * for new channel stream and send those stream fds to the kernel consumer.
554 *
555 * Useful for CPU hotplug feature.
556 */
557static int update_kernel_stream(int fd)
558{
559 int ret = 0;
560 struct ltt_session *session;
561 struct ltt_kernel_channel *channel;
562
563 DBG("Updating kernel streams for channel fd %d", fd);
564
565 lock_session_list();
566 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
567 lock_session(session);
568 if (session->kernel_session == NULL) {
569 unlock_session(session);
570 continue;
571 }
572
573 /* This is not suppose to be 0 but this is an extra security check */
574 if (session->kernel_session->consumer_fd == 0) {
575 session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
576 }
577
578 cds_list_for_each_entry(channel, &session->kernel_session->channel_list.head, list) {
579 if (channel->fd == fd) {
580 DBG("Channel found, updating kernel streams");
581 ret = kernel_open_channel_stream(channel);
582 if (ret < 0) {
583 goto end;
584 }
585
586 /*
587 * Have we already sent fds to the consumer? If yes, it means that
588 * tracing is started so it is safe to send our updated stream fds.
589 */
590 if (session->kernel_session->kconsumer_fds_sent == 1) {
591 ret = send_kconsumerd_channel_fds(session->kernel_session->consumer_fd,
592 channel);
593 if (ret < 0) {
594 goto end;
595 }
596 }
597 goto end;
598 }
599 }
600 unlock_session(session);
601 }
602
603end:
604 unlock_session_list();
605 if (session) {
606 unlock_session(session);
607 }
608 return ret;
609}
610
611/*
612 * This thread manage event coming from the kernel.
613 *
614 * Features supported in this thread:
615 * -) CPU Hotplug
616 */
617static void *thread_manage_kernel(void *data)
618{
619 int ret, i, nb_fd = 0;
620 char tmp;
621 int update_poll_flag = 1;
622
623 tracepoint(sessiond_th_kern_start);
624
625 DBG("Thread manage kernel started");
626
627 while (1) {
628 if (update_poll_flag == 1) {
629 nb_fd = update_kernel_pollfd();
630 if (nb_fd < 0) {
631 goto error;
632 }
633 update_poll_flag = 0;
634 }
635
636 DBG("Polling on %d fds", nb_fd);
637
638 tracepoint(sessiond_th_kern_poll);
639
640 /* Poll infinite value of time */
641 ret = poll(kernel_pollfd, nb_fd, -1);
642 if (ret < 0) {
643 perror("poll kernel thread");
644 goto error;
645 } else if (ret == 0) {
646 /* Should not happen since timeout is infinite */
647 continue;
648 }
649
650 /* Thread quit pipe has been closed. Killing thread. */
651 if (kernel_pollfd[nb_fd - 1].revents == POLLNVAL) {
652 goto error;
653 }
654
655 DBG("Kernel poll event triggered");
656
657 /*
658 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
659 * must be updated.
660 */
661 switch (kernel_pollfd[nb_fd - 2].revents) {
662 case POLLIN:
663 ret = read(kernel_poll_pipe[0], &tmp, 1);
664 update_poll_flag = 1;
665 continue;
666 case POLLERR:
667 goto error;
668 default:
669 break;
670 }
671
672 for (i = 0; i < nb_fd; i++) {
673 switch (kernel_pollfd[i].revents) {
674 /*
675 * New CPU detected by the kernel. Adding kernel stream to kernel
676 * session and updating the kernel consumer
677 */
678 case POLLIN | POLLRDNORM:
679 ret = update_kernel_stream(kernel_pollfd[i].fd);
680 if (ret < 0) {
681 continue;
682 }
683 break;
684 }
685 }
686 }
687
688error:
689 DBG("Kernel thread dying");
690 if (kernel_pollfd) {
691 free(kernel_pollfd);
692 }
693
694 close(kernel_poll_pipe[0]);
695 close(kernel_poll_pipe[1]);
696 return NULL;
697}
698
699/*
700 * This thread manage the kconsumerd error sent back to the session daemon.
701 */
702static void *thread_manage_kconsumerd(void *data)
703{
704 int sock = 0, ret;
705 enum lttcomm_return_code code;
706 struct pollfd pollfd[2];
707
708 tracepoint(sessiond_th_kcon_start);
709
710 DBG("[thread] Manage kconsumerd started");
711
712 ret = lttcomm_listen_unix_sock(kconsumerd_err_sock);
713 if (ret < 0) {
714 goto error;
715 }
716
717 /* First fd is always the quit pipe */
718 pollfd[0].fd = thread_quit_pipe[0];
719
720 /* Apps socket */
721 pollfd[1].fd = kconsumerd_err_sock;
722 pollfd[1].events = POLLIN;
723
724 tracepoint(sessiond_th_kcon_poll);
725
726 /* Inifinite blocking call, waiting for transmission */
727 ret = poll(pollfd, 2, -1);
728 if (ret < 0) {
729 perror("poll kconsumerd thread");
730 goto error;
731 }
732
733 /* Thread quit pipe has been closed. Killing thread. */
734 if (pollfd[0].revents == POLLNVAL) {
735 goto error;
736 } else if (pollfd[1].revents == POLLERR) {
737 ERR("Kconsumerd err socket poll error");
738 goto error;
739 }
740
741 sock = lttcomm_accept_unix_sock(kconsumerd_err_sock);
742 if (sock < 0) {
743 goto error;
744 }
745
746 /* Getting status code from kconsumerd */
747 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
748 if (ret <= 0) {
749 goto error;
750 }
751
752 if (code == KCONSUMERD_COMMAND_SOCK_READY) {
753 kconsumerd_cmd_sock = lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path);
754 if (kconsumerd_cmd_sock < 0) {
755 sem_post(&kconsumerd_sem);
756 perror("kconsumerd connect");
757 goto error;
758 }
759 /* Signal condition to tell that the kconsumerd is ready */
760 sem_post(&kconsumerd_sem);
761 DBG("Kconsumerd command socket ready");
762 } else {
763 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
764 lttcomm_get_readable_code(-code));
765 goto error;
766 }
767
768 /* Kconsumerd err socket */
769 pollfd[1].fd = sock;
770 pollfd[1].events = POLLIN;
771
772 /* Inifinite blocking call, waiting for transmission */
773 ret = poll(pollfd, 2, -1);
774 if (ret < 0) {
775 perror("poll kconsumerd thread");
776 goto error;
777 }
778
779 /* Thread quit pipe has been closed. Killing thread. */
780 if (pollfd[0].revents == POLLNVAL) {
781 goto error;
782 } else if (pollfd[1].revents == POLLERR) {
783 ERR("Kconsumerd err socket second poll error");
784 goto error;
785 }
786
787 /* Wait for any kconsumerd error */
788 ret = lttcomm_recv_unix_sock(sock, &code, sizeof(enum lttcomm_return_code));
789 if (ret <= 0) {
790 ERR("Kconsumerd closed the command socket");
791 goto error;
792 }
793
794 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code));
795
796error:
797 DBG("Kconsumerd thread dying");
798 if (kconsumerd_err_sock) {
799 close(kconsumerd_err_sock);
800 }
801 if (kconsumerd_cmd_sock) {
802 close(kconsumerd_cmd_sock);
803 }
804 if (sock) {
805 close(sock);
806 }
807
808 unlink(kconsumerd_err_unix_sock_path);
809 unlink(kconsumerd_cmd_unix_sock_path);
810
811 kconsumerd_pid = 0;
812 return NULL;
813}
814
815/*
816 * This thread manage the application socket communication
817 */
818static void *thread_manage_apps(void *data)
819{
820 int sock = 0, ret;
821 struct pollfd pollfd[2];
822
823 tracepoint(sessiond_th_apps_start);
824
825 /* TODO: Something more elegant is needed but fine for now */
826 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
827 * for 32-bit vs 64-bit compat processes. */
828 /* replicate in ust with version number */
829 struct {
830 int reg; /* 1:register, 0:unregister */
831 pid_t pid;
832 uid_t uid;
833 } reg_msg;
834
835 DBG("[thread] Manage apps started");
836
837 ret = lttcomm_listen_unix_sock(apps_sock);
838 if (ret < 0) {
839 goto error;
840 }
841
842 /* First fd is always the quit pipe */
843 pollfd[0].fd = thread_quit_pipe[0];
844
845 /* Apps socket */
846 pollfd[1].fd = apps_sock;
847 pollfd[1].events = POLLIN;
848
849 /* Notify all applications to register */
850 notify_apps(default_global_apps_pipe);
851
852 while (1) {
853 DBG("Accepting application registration");
854
855 tracepoint(sessiond_th_apps_poll);
856
857 /* Inifinite blocking call, waiting for transmission */
858 ret = poll(pollfd, 2, -1);
859 if (ret < 0) {
860 perror("poll apps thread");
861 goto error;
862 }
863
864 /* Thread quit pipe has been closed. Killing thread. */
865 if (pollfd[0].revents == POLLNVAL) {
866 goto error;
867 } else if (pollfd[1].revents == POLLERR) {
868 ERR("Apps socket poll error");
869 goto error;
870 }
871
872 sock = lttcomm_accept_unix_sock(apps_sock);
873 if (sock < 0) {
874 goto error;
875 }
876
877 /*
878 * Using message-based transmissions to ensure we don't
879 * have to deal with partially received messages.
880 */
881 ret = lttcomm_recv_unix_sock(sock, &reg_msg, sizeof(reg_msg));
882 if (ret < 0) {
883 perror("recv");
884 continue;
885 }
886
887 /* Add application to the global traceable list */
888 if (reg_msg.reg == 1) {
889 /* Registering */
890 /*
891 * TODO: socket should be either passed to a
892 * listener thread (for more messages) or
893 * closed. It currently leaks.
894 */
895 ret = register_traceable_app(reg_msg.pid, reg_msg.uid);
896 if (ret < 0) {
897 /* register_traceable_app only return an error with
898 * ENOMEM. At this point, we better stop everything.
899 */
900 goto error;
901 }
902 } else {
903 /* Unregistering */
904 unregister_traceable_app(reg_msg.pid);
905 }
906 }
907
908error:
909 DBG("Apps thread dying");
910 if (apps_sock) {
911 close(apps_sock);
912 }
913 if (sock) {
914 close(sock);
915 }
916
917 unlink(apps_unix_sock_path);
918 return NULL;
919}
920
921/*
922 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
923 * exec or it will fails.
924 */
925static int spawn_kconsumerd_thread(void)
926{
927 int ret;
928
929 /* Setup semaphore */
930 sem_init(&kconsumerd_sem, 0, 0);
931
932 ret = pthread_create(&kconsumerd_thread, NULL, thread_manage_kconsumerd, (void *) NULL);
933 if (ret != 0) {
934 perror("pthread_create kconsumerd");
935 goto error;
936 }
937
938 /* Wait for the kconsumerd thread to be ready */
939 sem_wait(&kconsumerd_sem);
940
941 if (kconsumerd_pid == 0) {
942 ERR("Kconsumerd did not start");
943 goto error;
944 }
945
946 return 0;
947
948error:
949 ret = LTTCOMM_KERN_CONSUMER_FAIL;
950 return ret;
951}
952
953/*
954 * Join kernel consumer thread
955 */
956static int join_kconsumerd_thread(void)
957{
958 void *status;
959 int ret;
960
961 if (kconsumerd_pid != 0) {
962 ret = kill(kconsumerd_pid, SIGTERM);
963 if (ret) {
964 ERR("Error killing kconsumerd");
965 return ret;
966 }
967 return pthread_join(kconsumerd_thread, &status);
968 } else {
969 return 0;
970 }
971}
972
973/*
974 * Fork and exec a kernel consumer daemon (kconsumerd).
975 *
976 * Return pid if successful else -1.
977 */
978static pid_t spawn_kconsumerd(void)
979{
980 int ret;
981 pid_t pid;
982 const char *verbosity;
983
984 DBG("Spawning kconsumerd");
985
986 pid = fork();
987 if (pid == 0) {
988 /*
989 * Exec kconsumerd.
990 */
991 if (opt_verbose > 1 || opt_verbose_kconsumerd) {
992 verbosity = "--verbose";
993 } else {
994 verbosity = "--quiet";
995 }
996 execl(INSTALL_BIN_PATH "/ltt-kconsumerd", "ltt-kconsumerd", verbosity, NULL);
997 if (errno != 0) {
998 perror("kernel start consumer exec");
999 }
1000 exit(EXIT_FAILURE);
1001 } else if (pid > 0) {
1002 ret = pid;
1003 goto error;
1004 } else {
1005 perror("kernel start consumer fork");
1006 ret = -errno;
1007 goto error;
1008 }
1009
1010error:
1011 return ret;
1012}
1013
1014/*
1015 * Spawn the kconsumerd daemon and session daemon thread.
1016 */
1017static int start_kconsumerd(void)
1018{
1019 int ret;
1020
1021 pthread_mutex_lock(&kconsumerd_pid_mutex);
1022 if (kconsumerd_pid != 0) {
1023 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1024 goto end;
1025 }
1026
1027 ret = spawn_kconsumerd();
1028 if (ret < 0) {
1029 ERR("Spawning kconsumerd failed");
1030 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1031 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1032 goto error;
1033 }
1034
1035 /* Setting up the global kconsumerd_pid */
1036 kconsumerd_pid = ret;
1037 pthread_mutex_unlock(&kconsumerd_pid_mutex);
1038
1039 DBG("Kconsumerd pid %d", ret);
1040
1041 DBG("Spawning kconsumerd thread");
1042 ret = spawn_kconsumerd_thread();
1043 if (ret < 0) {
1044 ERR("Fatal error spawning kconsumerd thread");
1045 goto error;
1046 }
1047
1048end:
1049 return 0;
1050
1051error:
1052 return ret;
1053}
1054
1055/*
1056 * modprobe_kernel_modules
1057 */
1058static int modprobe_kernel_modules(void)
1059{
1060 int ret = 0, i;
1061 char modprobe[256];
1062
1063 for (i = 0; i < ARRAY_SIZE(kernel_modules_list); i++) {
1064 ret = snprintf(modprobe, sizeof(modprobe),
1065 "/sbin/modprobe %s%s",
1066 kernel_modules_list[i].required ? "" : "--quiet ",
1067 kernel_modules_list[i].name);
1068 if (ret < 0) {
1069 perror("snprintf modprobe");
1070 goto error;
1071 }
1072 modprobe[sizeof(modprobe) - 1] = '\0';
1073 ret = system(modprobe);
1074 if (ret == -1) {
1075 ERR("Unable to launch modprobe for module %s",
1076 kernel_modules_list[i].name);
1077 } else if (kernel_modules_list[i].required
1078 && WEXITSTATUS(ret) != 0) {
1079 ERR("Unable to load module %s",
1080 kernel_modules_list[i].name);
1081 } else {
1082 DBG("Modprobe successfully %s",
1083 kernel_modules_list[i].name);
1084 }
1085 }
1086
1087error:
1088 return ret;
1089}
1090
1091/*
1092 * modprobe_remove_kernel_modules
1093 * Remove modules in reverse load order.
1094 */
1095static int modprobe_remove_kernel_modules(void)
1096{
1097 int ret = 0, i;
1098 char modprobe[256];
1099
1100 for (i = ARRAY_SIZE(kernel_modules_list) - 1; i >= 0; i--) {
1101 ret = snprintf(modprobe, sizeof(modprobe),
1102 "/sbin/modprobe --remove --quiet %s",
1103 kernel_modules_list[i].name);
1104 if (ret < 0) {
1105 perror("snprintf modprobe --remove");
1106 goto error;
1107 }
1108 modprobe[sizeof(modprobe) - 1] = '\0';
1109 ret = system(modprobe);
1110 if (ret == -1) {
1111 ERR("Unable to launch modprobe --remove for module %s",
1112 kernel_modules_list[i].name);
1113 } else if (kernel_modules_list[i].required
1114 && WEXITSTATUS(ret) != 0) {
1115 ERR("Unable to remove module %s",
1116 kernel_modules_list[i].name);
1117 } else {
1118 DBG("Modprobe removal successful %s",
1119 kernel_modules_list[i].name);
1120 }
1121 }
1122
1123error:
1124 return ret;
1125}
1126
1127/*
1128 * mount_debugfs
1129 */
1130static int mount_debugfs(char *path)
1131{
1132 int ret;
1133 char *type = "debugfs";
1134
1135 ret = mkdir_recursive(path, S_IRWXU | S_IRWXG, geteuid(), getegid());
1136 if (ret < 0) {
1137 goto error;
1138 }
1139
1140 ret = mount(type, path, type, 0, NULL);
1141 if (ret < 0) {
1142 perror("mount debugfs");
1143 goto error;
1144 }
1145
1146 DBG("Mounted debugfs successfully at %s", path);
1147
1148error:
1149 return ret;
1150}
1151
1152/*
1153 * Setup necessary data for kernel tracer action.
1154 */
1155static void init_kernel_tracer(void)
1156{
1157 int ret;
1158 char *proc_mounts = "/proc/mounts";
1159 char line[256];
1160 char *debugfs_path = NULL, *lttng_path;
1161 FILE *fp;
1162
1163 /* Detect debugfs */
1164 fp = fopen(proc_mounts, "r");
1165 if (fp == NULL) {
1166 ERR("Unable to probe %s", proc_mounts);
1167 goto error;
1168 }
1169
1170 while (fgets(line, sizeof(line), fp) != NULL) {
1171 if (strstr(line, "debugfs") != NULL) {
1172 /* Remove first string */
1173 strtok(line, " ");
1174 /* Dup string here so we can reuse line later on */
1175 debugfs_path = strdup(strtok(NULL, " "));
1176 DBG("Got debugfs path : %s", debugfs_path);
1177 break;
1178 }
1179 }
1180
1181 fclose(fp);
1182
1183 /* Mount debugfs if needded */
1184 if (debugfs_path == NULL) {
1185 ret = asprintf(&debugfs_path, "/mnt/debugfs");
1186 if (ret < 0) {
1187 perror("asprintf debugfs path");
1188 goto error;
1189 }
1190 ret = mount_debugfs(debugfs_path);
1191 if (ret < 0) {
1192 goto error;
1193 }
1194 }
1195
1196 /* Modprobe lttng kernel modules */
1197 ret = modprobe_kernel_modules();
1198 if (ret < 0) {
1199 goto error;
1200 }
1201
1202 /* Setup lttng kernel path */
1203 ret = asprintf(&lttng_path, "%s/lttng", debugfs_path);
1204 if (ret < 0) {
1205 perror("asprintf lttng path");
1206 goto error;
1207 }
1208
1209 /* Open debugfs lttng */
1210 kernel_tracer_fd = open(lttng_path, O_RDWR);
1211 if (kernel_tracer_fd < 0) {
1212 DBG("Failed to open %s", lttng_path);
1213 goto error;
1214 }
1215
1216 free(lttng_path);
1217 free(debugfs_path);
1218 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1219 return;
1220
1221error:
1222 if (lttng_path) {
1223 free(lttng_path);
1224 }
1225 if (debugfs_path) {
1226 free(debugfs_path);
1227 }
1228 WARN("No kernel tracer available");
1229 kernel_tracer_fd = 0;
1230 return;
1231}
1232
1233/*
1234 * Start tracing by creating trace directory and sending FDs to the kernel
1235 * consumer.
1236 */
1237static int start_kernel_trace(struct ltt_kernel_session *session)
1238{
1239 int ret = 0;
1240
1241 if (session->kconsumer_fds_sent == 0) {
1242 /*
1243 * Assign default kernel consumer if no consumer assigned to the kernel
1244 * session. At this point, it's NOT suppose to be 0 but this is an extra
1245 * security check.
1246 */
1247 if (session->consumer_fd == 0) {
1248 session->consumer_fd = kconsumerd_cmd_sock;
1249 }
1250
1251 ret = send_kconsumerd_fds(session);
1252 if (ret < 0) {
1253 ERR("Send kconsumerd fds failed");
1254 ret = LTTCOMM_KERN_CONSUMER_FAIL;
1255 goto error;
1256 }
1257
1258 session->kconsumer_fds_sent = 1;
1259 }
1260
1261error:
1262 return ret;
1263}
1264
1265/*
1266 * Notify kernel thread to update it's pollfd.
1267 */
1268static int notify_kernel_pollfd(void)
1269{
1270 int ret;
1271
1272 /* Inform kernel thread of the new kernel channel */
1273 ret = write(kernel_poll_pipe[1], "!", 1);
1274 if (ret < 0) {
1275 perror("write kernel poll pipe");
1276 }
1277
1278 return ret;
1279}
1280
1281/*
1282 * Allocate a channel structure and fill it.
1283 */
1284static struct lttng_channel *init_default_channel(enum lttng_domain_type domain_type,
1285 char *name)
1286{
1287 struct lttng_channel *chan;
1288
1289 chan = malloc(sizeof(struct lttng_channel));
1290 if (chan == NULL) {
1291 perror("init channel malloc");
1292 goto error;
1293 }
1294
1295 if (snprintf(chan->name, NAME_MAX, "%s", name) < 0) {
1296 perror("snprintf channel name");
1297 goto error;
1298 }
1299
1300 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
1301 chan->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1302 chan->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1303
1304 switch (domain_type) {
1305 case LTTNG_DOMAIN_KERNEL:
1306 chan->attr.subbuf_size = DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE;
1307 chan->attr.num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1308 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1309 break;
1310 /* TODO: add UST */
1311 default:
1312 goto error; /* Not implemented */
1313 }
1314
1315 return chan;
1316
1317error:
1318 free(chan);
1319 return NULL;
1320}
1321
1322/*
1323 * Create a kernel tracer session then create the default channel.
1324 */
1325static int create_kernel_session(struct ltt_session *session)
1326{
1327 int ret;
1328
1329 DBG("Creating kernel session");
1330
1331 ret = kernel_create_session(session, kernel_tracer_fd);
1332 if (ret < 0) {
1333 ret = LTTCOMM_KERN_SESS_FAIL;
1334 goto error;
1335 }
1336
1337 /* Set kernel consumer socket fd */
1338 if (kconsumerd_cmd_sock) {
1339 session->kernel_session->consumer_fd = kconsumerd_cmd_sock;
1340 }
1341
1342 ret = asprintf(&session->kernel_session->trace_path, "%s/kernel",
1343 session->path);
1344 if (ret < 0) {
1345 perror("asprintf kernel traces path");
1346 goto error;
1347 }
1348
1349 ret = mkdir_recursive(session->kernel_session->trace_path,
1350 S_IRWXU | S_IRWXG, geteuid(), allowed_group());
1351 if (ret < 0) {
1352 if (ret != -EEXIST) {
1353 ERR("Trace directory creation error");
1354 goto error;
1355 }
1356 }
1357
1358error:
1359 return ret;
1360}
1361
1362/*
1363 * Using the session list, filled a lttng_session array to send back to the
1364 * client for session listing.
1365 *
1366 * The session list lock MUST be acquired before calling this function. Use
1367 * lock_session_list() and unlock_session_list().
1368 */
1369static void list_lttng_sessions(struct lttng_session *sessions)
1370{
1371 int i = 0;
1372 struct ltt_session *session;
1373
1374 DBG("Getting all available session");
1375 /*
1376 * Iterate over session list and append data after the control struct in
1377 * the buffer.
1378 */
1379 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
1380 strncpy(sessions[i].path, session->path, PATH_MAX);
1381 sessions[i].path[PATH_MAX - 1] = '\0';
1382 strncpy(sessions[i].name, session->name, NAME_MAX);
1383 sessions[i].name[NAME_MAX - 1] = '\0';
1384 i++;
1385 }
1386}
1387
1388/*
1389 * Fill lttng_channel array of all channels.
1390 */
1391static void list_lttng_channels(struct ltt_session *session,
1392 struct lttng_channel *channels)
1393{
1394 int i = 0;
1395 struct ltt_kernel_channel *kchan;
1396
1397 DBG("Listing channels for session %s", session->name);
1398
1399 /* Kernel channels */
1400 if (session->kernel_session != NULL) {
1401 cds_list_for_each_entry(kchan, &session->kernel_session->channel_list.head, list) {
1402 /* Copy lttng_channel struct to array */
1403 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
1404 channels[i].enabled = kchan->enabled;
1405 i++;
1406 }
1407 }
1408
1409 /* TODO: Missing UST listing */
1410}
1411
1412/*
1413 * Fill lttng_event array of all events in the channel.
1414 */
1415static void list_lttng_events(struct ltt_kernel_channel *kchan,
1416 struct lttng_event *events)
1417{
1418 /*
1419 * TODO: This is ONLY kernel. Need UST support.
1420 */
1421 int i = 0;
1422 struct ltt_kernel_event *event;
1423
1424 DBG("Listing events for channel %s", kchan->channel->name);
1425
1426 /* Kernel channels */
1427 cds_list_for_each_entry(event, &kchan->events_list.head , list) {
1428 strncpy(events[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN);
1429 events[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1430 events[i].enabled = event->enabled;
1431 switch (event->event->instrumentation) {
1432 case LTTNG_KERNEL_TRACEPOINT:
1433 events[i].type = LTTNG_EVENT_TRACEPOINT;
1434 break;
1435 case LTTNG_KERNEL_KPROBE:
1436 case LTTNG_KERNEL_KRETPROBE:
1437 events[i].type = LTTNG_EVENT_PROBE;
1438 memcpy(&events[i].attr.probe, &event->event->u.kprobe,
1439 sizeof(struct lttng_kernel_kprobe));
1440 break;
1441 case LTTNG_KERNEL_FUNCTION:
1442 events[i].type = LTTNG_EVENT_FUNCTION;
1443 memcpy(&events[i].attr.ftrace, &event->event->u.ftrace,
1444 sizeof(struct lttng_kernel_function));
1445 break;
1446 }
1447 i++;
1448 }
1449}
1450
1451/*
1452 * Process the command requested by the lttng client within the command
1453 * context structure. This function make sure that the return structure (llm)
1454 * is set and ready for transmission before returning.
1455 *
1456 * Return any error encountered or 0 for success.
1457 */
1458static int process_client_msg(struct command_ctx *cmd_ctx)
1459{
1460 int ret = LTTCOMM_OK;
1461
1462 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
1463
1464 /*
1465 * Commands that DO NOT need a session.
1466 */
1467 switch (cmd_ctx->lsm->cmd_type) {
1468 case LTTNG_CREATE_SESSION:
1469 case LTTNG_LIST_SESSIONS:
1470 case LTTNG_LIST_TRACEPOINTS:
1471 case LTTNG_CALIBRATE:
1472 break;
1473 default:
1474 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
1475 cmd_ctx->session = find_session_by_name(cmd_ctx->lsm->session.name);
1476 if (cmd_ctx->session == NULL) {
1477 /* If session name not found */
1478 if (cmd_ctx->lsm->session.name != NULL) {
1479 ret = LTTCOMM_SESS_NOT_FOUND;
1480 } else { /* If no session name specified */
1481 ret = LTTCOMM_SELECT_SESS;
1482 }
1483 goto error;
1484 } else {
1485 /* Acquire lock for the session */
1486 lock_session(cmd_ctx->session);
1487 }
1488 break;
1489 }
1490
1491 /*
1492 * Check domain type for specific "pre-action".
1493 */
1494 switch (cmd_ctx->lsm->domain.type) {
1495 case LTTNG_DOMAIN_KERNEL:
1496 /* Kernel tracer check */
1497 if (kernel_tracer_fd == 0) {
1498 init_kernel_tracer();
1499 if (kernel_tracer_fd == 0) {
1500 ret = LTTCOMM_KERN_NA;
1501 goto error;
1502 }
1503 }
1504
1505 /* Need a session for kernel command */
1506 switch (cmd_ctx->lsm->cmd_type) {
1507 case LTTNG_CALIBRATE:
1508 case LTTNG_CREATE_SESSION:
1509 case LTTNG_LIST_SESSIONS:
1510 case LTTNG_LIST_TRACEPOINTS:
1511 break;
1512 default:
1513 if (cmd_ctx->session->kernel_session == NULL) {
1514 ret = create_kernel_session(cmd_ctx->session);
1515 if (ret < 0) {
1516 ret = LTTCOMM_KERN_SESS_FAIL;
1517 goto error;
1518 }
1519
1520 /* Start the kernel consumer daemon */
1521
1522 if (kconsumerd_pid == 0 &&
1523 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
1524 ret = start_kconsumerd();
1525 if (ret < 0) {
1526 goto error;
1527 }
1528 }
1529 }
1530 }
1531 break;
1532 default:
1533 break;
1534 }
1535
1536 /* Process by command type */
1537 switch (cmd_ctx->lsm->cmd_type) {
1538 case LTTNG_ADD_CONTEXT:
1539 {
1540 struct lttng_kernel_context kctx;
1541
1542 /* Setup lttng message with no payload */
1543 ret = setup_lttng_msg(cmd_ctx, 0);
1544 if (ret < 0) {
1545 goto setup_error;
1546 }
1547
1548 switch (cmd_ctx->lsm->domain.type) {
1549 case LTTNG_DOMAIN_KERNEL:
1550 /* Create Kernel context */
1551 kctx.ctx = cmd_ctx->lsm->u.context.ctx.ctx;
1552 kctx.u.perf_counter.type = cmd_ctx->lsm->u.context.ctx.u.perf_counter.type;
1553 kctx.u.perf_counter.config = cmd_ctx->lsm->u.context.ctx.u.perf_counter.config;
1554 strncpy(kctx.u.perf_counter.name,
1555 cmd_ctx->lsm->u.context.ctx.u.perf_counter.name,
1556 LTTNG_SYMBOL_NAME_LEN);
1557 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1558
1559 /* Add kernel context to kernel tracer. See context.c */
1560 ret = add_kernel_context(cmd_ctx->session->kernel_session, &kctx,
1561 cmd_ctx->lsm->u.context.event_name,
1562 cmd_ctx->lsm->u.context.channel_name);
1563 if (ret != LTTCOMM_OK) {
1564 goto error;
1565 }
1566 break;
1567 default:
1568 /* TODO: Userspace tracing */
1569 ret = LTTCOMM_NOT_IMPLEMENTED;
1570 goto error;
1571 }
1572
1573 ret = LTTCOMM_OK;
1574 break;
1575 }
1576 case LTTNG_DISABLE_CHANNEL:
1577 {
1578 struct ltt_kernel_channel *kchan;
1579
1580 /* Setup lttng message with no payload */
1581 ret = setup_lttng_msg(cmd_ctx, 0);
1582 if (ret < 0) {
1583 goto setup_error;
1584 }
1585
1586 switch (cmd_ctx->lsm->domain.type) {
1587 case LTTNG_DOMAIN_KERNEL:
1588 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1589 cmd_ctx->session->kernel_session);
1590 if (kchan == NULL) {
1591 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1592 goto error;
1593 } else if (kchan->enabled == 1) {
1594 ret = kernel_disable_channel(kchan);
1595 if (ret < 0) {
1596 if (ret != EEXIST) {
1597 ret = LTTCOMM_KERN_CHAN_DISABLE_FAIL;
1598 }
1599 goto error;
1600 }
1601 }
1602 kernel_wait_quiescent(kernel_tracer_fd);
1603 break;
1604 default:
1605 /* TODO: Userspace tracing */
1606 ret = LTTCOMM_NOT_IMPLEMENTED;
1607 goto error;
1608 }
1609
1610 ret = LTTCOMM_OK;
1611 break;
1612 }
1613 case LTTNG_DISABLE_EVENT:
1614 {
1615 struct ltt_kernel_channel *kchan;
1616 struct ltt_kernel_event *kevent;
1617
1618 /* Setup lttng message with no payload */
1619 ret = setup_lttng_msg(cmd_ctx, 0);
1620 if (ret < 0) {
1621 goto setup_error;
1622 }
1623
1624 switch (cmd_ctx->lsm->domain.type) {
1625 case LTTNG_DOMAIN_KERNEL:
1626 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1627 cmd_ctx->session->kernel_session);
1628 if (kchan == NULL) {
1629 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1630 goto error;
1631 }
1632
1633 kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.disable.name, kchan);
1634 if (kevent != NULL) {
1635 DBG("Disabling kernel event %s for channel %s.", kevent->event->name,
1636 kchan->channel->name);
1637 ret = kernel_disable_event(kevent);
1638 if (ret < 0) {
1639 ret = LTTCOMM_KERN_ENABLE_FAIL;
1640 goto error;
1641 }
1642 }
1643
1644 kernel_wait_quiescent(kernel_tracer_fd);
1645 break;
1646 default:
1647 /* TODO: Userspace tracing */
1648 ret = LTTCOMM_NOT_IMPLEMENTED;
1649 goto error;
1650 }
1651
1652 ret = LTTCOMM_OK;
1653 break;
1654 }
1655 case LTTNG_DISABLE_ALL_EVENT:
1656 {
1657 struct ltt_kernel_channel *kchan;
1658 struct ltt_kernel_event *kevent;
1659
1660 /* Setup lttng message with no payload */
1661 ret = setup_lttng_msg(cmd_ctx, 0);
1662 if (ret < 0) {
1663 goto setup_error;
1664 }
1665
1666 switch (cmd_ctx->lsm->domain.type) {
1667 case LTTNG_DOMAIN_KERNEL:
1668 DBG("Disabling all enabled kernel events");
1669 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.disable.channel_name,
1670 cmd_ctx->session->kernel_session);
1671 if (kchan == NULL) {
1672 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
1673 goto error;
1674 }
1675
1676 /* For each event in the kernel session */
1677 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
1678 DBG("Disabling kernel event %s for channel %s.",
1679 kevent->event->name, kchan->channel->name);
1680 ret = kernel_disable_event(kevent);
1681 if (ret < 0) {
1682 continue;
1683 }
1684 }
1685
1686 /* Quiescent wait after event disable */
1687 kernel_wait_quiescent(kernel_tracer_fd);
1688 break;
1689 default:
1690 /* TODO: Userspace tracing */
1691 ret = LTTCOMM_NOT_IMPLEMENTED;
1692 goto error;
1693 }
1694
1695 ret = LTTCOMM_OK;
1696 break;
1697 }
1698 case LTTNG_ENABLE_CHANNEL:
1699 {
1700 struct ltt_kernel_channel *kchan;
1701
1702 /* Setup lttng message with no payload */
1703 ret = setup_lttng_msg(cmd_ctx, 0);
1704 if (ret < 0) {
1705 goto setup_error;
1706 }
1707
1708 switch (cmd_ctx->lsm->domain.type) {
1709 case LTTNG_DOMAIN_KERNEL:
1710 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.enable.channel_name,
1711 cmd_ctx->session->kernel_session);
1712 if (kchan == NULL) {
1713 /* Channel not found, creating it */
1714 DBG("Creating kernel channel");
1715
1716 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1717 &cmd_ctx->lsm->u.channel.chan,
1718 cmd_ctx->session->kernel_session->trace_path);
1719 if (ret < 0) {
1720 ret = LTTCOMM_KERN_CHAN_FAIL;
1721 goto error;
1722 }
1723
1724 /* Notify kernel thread that there is a new channel */
1725 ret = notify_kernel_pollfd();
1726 if (ret < 0) {
1727 ret = LTTCOMM_FATAL;
1728 goto error;
1729 }
1730 } else if (kchan->enabled == 0) {
1731 ret = kernel_enable_channel(kchan);
1732 if (ret < 0) {
1733 if (ret != EEXIST) {
1734 ret = LTTCOMM_KERN_CHAN_ENABLE_FAIL;
1735 }
1736 goto error;
1737 }
1738 }
1739
1740 kernel_wait_quiescent(kernel_tracer_fd);
1741 break;
1742 default:
1743 /* TODO: Userspace tracing */
1744 ret = LTTCOMM_NOT_IMPLEMENTED;
1745 goto error;
1746 }
1747
1748 ret = LTTCOMM_OK;
1749 break;
1750 }
1751 case LTTNG_ENABLE_EVENT:
1752 {
1753 char *channel_name;
1754 struct ltt_kernel_channel *kchan;
1755 struct ltt_kernel_event *kevent;
1756 struct lttng_channel *chan;
1757
1758 /* Setup lttng message with no payload */
1759 ret = setup_lttng_msg(cmd_ctx, 0);
1760 if (ret < 0) {
1761 goto setup_error;
1762 }
1763
1764 channel_name = cmd_ctx->lsm->u.enable.channel_name;
1765
1766 switch (cmd_ctx->lsm->domain.type) {
1767 case LTTNG_DOMAIN_KERNEL:
1768 kchan = get_kernel_channel_by_name(channel_name,
1769 cmd_ctx->session->kernel_session);
1770 if (kchan == NULL) {
1771 DBG("Channel not found. Creating channel %s", channel_name);
1772
1773 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
1774 if (chan == NULL) {
1775 ret = LTTCOMM_FATAL;
1776 goto error;
1777 }
1778
1779 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1780 chan, cmd_ctx->session->kernel_session->trace_path);
1781 if (ret < 0) {
1782 ret = LTTCOMM_KERN_CHAN_FAIL;
1783 goto error;
1784 }
1785 kchan = get_kernel_channel_by_name(channel_name,
1786 cmd_ctx->session->kernel_session);
1787 if (kchan == NULL) {
1788 ERR("Channel %s not found after creation. Internal error, giving up.",
1789 channel_name);
1790 ret = LTTCOMM_FATAL;
1791 goto error;
1792 }
1793 }
1794
1795 kevent = get_kernel_event_by_name(cmd_ctx->lsm->u.enable.event.name, kchan);
1796 if (kevent == NULL) {
1797 DBG("Creating kernel event %s for channel %s.",
1798 cmd_ctx->lsm->u.enable.event.name, channel_name);
1799 ret = kernel_create_event(&cmd_ctx->lsm->u.enable.event, kchan);
1800 } else {
1801 DBG("Enabling kernel event %s for channel %s.",
1802 kevent->event->name, channel_name);
1803 ret = kernel_enable_event(kevent);
1804 if (ret == -EEXIST) {
1805 ret = LTTCOMM_KERN_EVENT_EXIST;
1806 goto error;
1807 }
1808 }
1809
1810 if (ret < 0) {
1811 ret = LTTCOMM_KERN_ENABLE_FAIL;
1812 goto error;
1813 }
1814
1815 kernel_wait_quiescent(kernel_tracer_fd);
1816 break;
1817 default:
1818 /* TODO: Userspace tracing */
1819 ret = LTTCOMM_NOT_IMPLEMENTED;
1820 goto error;
1821 }
1822 ret = LTTCOMM_OK;
1823 break;
1824 }
1825 case LTTNG_ENABLE_ALL_EVENT:
1826 {
1827 int size, i;
1828 char *channel_name;
1829 struct ltt_kernel_channel *kchan;
1830 struct ltt_kernel_event *kevent;
1831 struct lttng_event *event_list;
1832 struct lttng_channel *chan;
1833
1834 /* Setup lttng message with no payload */
1835 ret = setup_lttng_msg(cmd_ctx, 0);
1836 if (ret < 0) {
1837 goto setup_error;
1838 }
1839
1840 DBG("Enabling all kernel event");
1841
1842 channel_name = cmd_ctx->lsm->u.enable.channel_name;
1843
1844 switch (cmd_ctx->lsm->domain.type) {
1845 case LTTNG_DOMAIN_KERNEL:
1846 kchan = get_kernel_channel_by_name(channel_name,
1847 cmd_ctx->session->kernel_session);
1848 if (kchan == NULL) {
1849 DBG("Channel not found. Creating channel %s", channel_name);
1850
1851 chan = init_default_channel(cmd_ctx->lsm->domain.type, channel_name);
1852 if (chan == NULL) {
1853 ret = LTTCOMM_FATAL;
1854 goto error;
1855 }
1856
1857 ret = kernel_create_channel(cmd_ctx->session->kernel_session,
1858 chan, cmd_ctx->session->kernel_session->trace_path);
1859 if (ret < 0) {
1860 ret = LTTCOMM_KERN_CHAN_FAIL;
1861 goto error;
1862 }
1863 kchan = get_kernel_channel_by_name(channel_name,
1864 cmd_ctx->session->kernel_session);
1865 if (kchan == NULL) {
1866 ERR("Channel %s not found after creation. Internal error, giving up.",
1867 channel_name);
1868 ret = LTTCOMM_FATAL;
1869 goto error;
1870 }
1871 }
1872
1873 /* For each event in the kernel session */
1874 cds_list_for_each_entry(kevent, &kchan->events_list.head, list) {
1875 DBG("Enabling kernel event %s for channel %s.",
1876 kevent->event->name, channel_name);
1877 ret = kernel_enable_event(kevent);
1878 if (ret < 0) {
1879 continue;
1880 }
1881 }
1882
1883 size = kernel_list_events(kernel_tracer_fd, &event_list);
1884 if (size < 0) {
1885 ret = LTTCOMM_KERN_LIST_FAIL;
1886 goto error;
1887 }
1888
1889 for (i = 0; i < size; i++) {
1890 kevent = get_kernel_event_by_name(event_list[i].name, kchan);
1891 if (kevent == NULL) {
1892 /* Default event type for enable all */
1893 event_list[i].type = LTTNG_EVENT_TRACEPOINT;
1894 /* Enable each single tracepoint event */
1895 ret = kernel_create_event(&event_list[i], kchan);
1896 if (ret < 0) {
1897 /* Ignore error here and continue */
1898 }
1899 }
1900 }
1901
1902 free(event_list);
1903
1904 /* Quiescent wait after event enable */
1905 kernel_wait_quiescent(kernel_tracer_fd);
1906 break;
1907 default:
1908 /* TODO: Userspace tracing */
1909 ret = LTTCOMM_NOT_IMPLEMENTED;
1910 goto error;
1911 }
1912
1913 ret = LTTCOMM_OK;
1914 break;
1915 }
1916 case LTTNG_LIST_TRACEPOINTS:
1917 {
1918 struct lttng_event *events;
1919 ssize_t nb_events = 0;
1920
1921 switch (cmd_ctx->lsm->domain.type) {
1922 case LTTNG_DOMAIN_KERNEL:
1923 DBG("Listing kernel events");
1924 nb_events = kernel_list_events(kernel_tracer_fd, &events);
1925 if (nb_events < 0) {
1926 ret = LTTCOMM_KERN_LIST_FAIL;
1927 goto error;
1928 }
1929 break;
1930 default:
1931 /* TODO: Userspace listing */
1932 ret = LTTCOMM_NOT_IMPLEMENTED;
1933 break;
1934 }
1935
1936 /*
1937 * Setup lttng message with payload size set to the event list size in
1938 * bytes and then copy list into the llm payload.
1939 */
1940 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
1941 if (ret < 0) {
1942 free(events);
1943 goto setup_error;
1944 }
1945
1946 /* Copy event list into message payload */
1947 memcpy(cmd_ctx->llm->payload, events,
1948 sizeof(struct lttng_event) * nb_events);
1949
1950 free(events);
1951
1952 ret = LTTCOMM_OK;
1953 break;
1954 }
1955 case LTTNG_START_TRACE:
1956 {
1957 struct ltt_kernel_channel *chan;
1958
1959 /* Setup lttng message with no payload */
1960 ret = setup_lttng_msg(cmd_ctx, 0);
1961 if (ret < 0) {
1962 goto setup_error;
1963 }
1964
1965 /* Kernel tracing */
1966 if (cmd_ctx->session->kernel_session != NULL) {
1967 if (cmd_ctx->session->kernel_session->metadata == NULL) {
1968 DBG("Open kernel metadata");
1969 ret = kernel_open_metadata(cmd_ctx->session->kernel_session,
1970 cmd_ctx->session->kernel_session->trace_path);
1971 if (ret < 0) {
1972 ret = LTTCOMM_KERN_META_FAIL;
1973 goto error;
1974 }
1975 }
1976
1977 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1978 DBG("Opening kernel metadata stream");
1979 if (cmd_ctx->session->kernel_session->metadata_stream_fd == 0) {
1980 ret = kernel_open_metadata_stream(cmd_ctx->session->kernel_session);
1981 if (ret < 0) {
1982 ERR("Kernel create metadata stream failed");
1983 ret = LTTCOMM_KERN_STREAM_FAIL;
1984 goto error;
1985 }
1986 }
1987 }
1988
1989 /* For each channel */
1990 cds_list_for_each_entry(chan,
1991 &cmd_ctx->session->kernel_session->channel_list.head, list) {
1992 if (chan->stream_count == 0) {
1993 ret = kernel_open_channel_stream(chan);
1994 if (ret < 0) {
1995 ERR("Kernel create channel stream failed");
1996 ret = LTTCOMM_KERN_STREAM_FAIL;
1997 goto error;
1998 }
1999 /* Update the stream global counter */
2000 cmd_ctx->session->kernel_session->stream_count_global += ret;
2001 }
2002 }
2003
2004 ret = start_kernel_trace(cmd_ctx->session->kernel_session);
2005 if (ret < 0) {
2006 ret = LTTCOMM_KERN_START_FAIL;
2007 goto error;
2008 }
2009
2010 DBG("Start kernel tracing");
2011 ret = kernel_start_session(cmd_ctx->session->kernel_session);
2012 if (ret < 0) {
2013 ERR("Kernel start session failed");
2014 ret = LTTCOMM_KERN_START_FAIL;
2015 goto error;
2016 }
2017
2018 /* Quiescent wait after starting trace */
2019 kernel_wait_quiescent(kernel_tracer_fd);
2020 }
2021
2022 /* TODO: Start all UST traces */
2023
2024 ret = LTTCOMM_OK;
2025 break;
2026 }
2027 case LTTNG_STOP_TRACE:
2028 {
2029 struct ltt_kernel_channel *chan;
2030 /* Setup lttng message with no payload */
2031 ret = setup_lttng_msg(cmd_ctx, 0);
2032 if (ret < 0) {
2033 goto setup_error;
2034 }
2035
2036 /* Kernel tracer */
2037 if (cmd_ctx->session->kernel_session != NULL) {
2038 DBG("Stop kernel tracing");
2039
2040 ret = kernel_metadata_flush_buffer(cmd_ctx->session->kernel_session->metadata_stream_fd);
2041 if (ret < 0) {
2042 ERR("Kernel metadata flush failed");
2043 }
2044
2045 cds_list_for_each_entry(chan, &cmd_ctx->session->kernel_session->channel_list.head, list) {
2046 ret = kernel_flush_buffer(chan);
2047 if (ret < 0) {
2048 ERR("Kernel flush buffer error");
2049 }
2050 }
2051
2052 ret = kernel_stop_session(cmd_ctx->session->kernel_session);
2053 if (ret < 0) {
2054 ERR("Kernel stop session failed");
2055 ret = LTTCOMM_KERN_STOP_FAIL;
2056 goto error;
2057 }
2058
2059 /* Quiescent wait after stopping trace */
2060 kernel_wait_quiescent(kernel_tracer_fd);
2061 }
2062
2063 /* TODO : User-space tracer */
2064
2065 ret = LTTCOMM_OK;
2066 break;
2067 }
2068 case LTTNG_CREATE_SESSION:
2069 {
2070 /* Setup lttng message with no payload */
2071 ret = setup_lttng_msg(cmd_ctx, 0);
2072 if (ret < 0) {
2073 goto setup_error;
2074 }
2075
2076 tracepoint(create_session_start);
2077 ret = create_session(cmd_ctx->lsm->session.name, cmd_ctx->lsm->session.path);
2078 tracepoint(create_session_end);
2079 if (ret < 0) {
2080 if (ret == -EEXIST) {
2081 ret = LTTCOMM_EXIST_SESS;
2082 } else {
2083 ret = LTTCOMM_FATAL;
2084 }
2085 goto error;
2086 }
2087
2088 ret = LTTCOMM_OK;
2089 break;
2090 }
2091 case LTTNG_DESTROY_SESSION:
2092 {
2093 /* Setup lttng message with no payload */
2094 ret = setup_lttng_msg(cmd_ctx, 0);
2095 if (ret < 0) {
2096 goto setup_error;
2097 }
2098
2099 /* Clean kernel session teardown */
2100 teardown_kernel_session(cmd_ctx->session);
2101
2102 tracepoint(destroy_session_start);
2103 ret = destroy_session(cmd_ctx->lsm->session.name);
2104 tracepoint(destroy_session_end);
2105 if (ret < 0) {
2106 ret = LTTCOMM_FATAL;
2107 goto error;
2108 }
2109
2110 /*
2111 * Must notify the kernel thread here to update it's pollfd in order to
2112 * remove the channel(s)' fd just destroyed.
2113 */
2114 ret = notify_kernel_pollfd();
2115 if (ret < 0) {
2116 ret = LTTCOMM_FATAL;
2117 goto error;
2118 }
2119
2120 ret = LTTCOMM_OK;
2121 break;
2122 }
2123 case LTTNG_LIST_DOMAINS:
2124 {
2125 size_t nb_dom = 0;
2126
2127 if (cmd_ctx->session->kernel_session != NULL) {
2128 nb_dom++;
2129 }
2130
2131 nb_dom += cmd_ctx->session->ust_trace_count;
2132
2133 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_domain) * nb_dom);
2134 if (ret < 0) {
2135 goto setup_error;
2136 }
2137
2138 ((struct lttng_domain *)(cmd_ctx->llm->payload))[0].type =
2139 LTTNG_DOMAIN_KERNEL;
2140
2141 /* TODO: User-space tracer domain support */
2142 ret = LTTCOMM_OK;
2143 break;
2144 }
2145 case LTTNG_LIST_CHANNELS:
2146 {
2147 /*
2148 * TODO: Only kernel channels are listed here. UST listing
2149 * is needed on lttng-ust 2.0 release.
2150 */
2151 size_t nb_chan = 0;
2152 if (cmd_ctx->session->kernel_session != NULL) {
2153 nb_chan += cmd_ctx->session->kernel_session->channel_count;
2154 }
2155
2156 ret = setup_lttng_msg(cmd_ctx,
2157 sizeof(struct lttng_channel) * nb_chan);
2158 if (ret < 0) {
2159 goto setup_error;
2160 }
2161
2162 list_lttng_channels(cmd_ctx->session,
2163 (struct lttng_channel *)(cmd_ctx->llm->payload));
2164
2165 ret = LTTCOMM_OK;
2166 break;
2167 }
2168 case LTTNG_LIST_EVENTS:
2169 {
2170 /*
2171 * TODO: Only kernel events are listed here. UST listing
2172 * is needed on lttng-ust 2.0 release.
2173 */
2174 size_t nb_event = 0;
2175 struct ltt_kernel_channel *kchan = NULL;
2176
2177 if (cmd_ctx->session->kernel_session != NULL) {
2178 kchan = get_kernel_channel_by_name(cmd_ctx->lsm->u.list.channel_name,
2179 cmd_ctx->session->kernel_session);
2180 if (kchan == NULL) {
2181 ret = LTTCOMM_KERN_CHAN_NOT_FOUND;
2182 goto error;
2183 }
2184 nb_event += kchan->event_count;
2185 }
2186
2187 ret = setup_lttng_msg(cmd_ctx,
2188 sizeof(struct lttng_event) * nb_event);
2189 if (ret < 0) {
2190 goto setup_error;
2191 }
2192
2193 DBG("Listing events (%zu events)", nb_event);
2194
2195 list_lttng_events(kchan,
2196 (struct lttng_event *)(cmd_ctx->llm->payload));
2197
2198 ret = LTTCOMM_OK;
2199 break;
2200 }
2201 case LTTNG_LIST_SESSIONS:
2202 {
2203 lock_session_list();
2204
2205 if (session_list_ptr->count == 0) {
2206 ret = LTTCOMM_NO_SESSION;
2207 unlock_session_list();
2208 goto error;
2209 }
2210
2211 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) *
2212 session_list_ptr->count);
2213 if (ret < 0) {
2214 unlock_session_list();
2215 goto setup_error;
2216 }
2217
2218 /* Filled the session array */
2219 list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload));
2220
2221 unlock_session_list();
2222
2223 ret = LTTCOMM_OK;
2224 break;
2225 }
2226 case LTTNG_CALIBRATE:
2227 {
2228 /* Setup lttng message with no payload */
2229 ret = setup_lttng_msg(cmd_ctx, 0);
2230 if (ret < 0) {
2231 goto setup_error;
2232 }
2233
2234 switch (cmd_ctx->lsm->domain.type) {
2235 case LTTNG_DOMAIN_KERNEL:
2236 {
2237 struct lttng_kernel_calibrate kcalibrate;
2238
2239 kcalibrate.type = cmd_ctx->lsm->u.calibrate.type;
2240 ret = kernel_calibrate(kernel_tracer_fd, &kcalibrate);
2241 if (ret < 0) {
2242 ret = LTTCOMM_KERN_ENABLE_FAIL;
2243 goto error;
2244 }
2245 break;
2246 }
2247 default:
2248 /* TODO: Userspace tracing */
2249 ret = LTTCOMM_NOT_IMPLEMENTED;
2250 goto error;
2251 }
2252 ret = LTTCOMM_OK;
2253 break;
2254 }
2255 case LTTNG_REGISTER_CONSUMER:
2256 {
2257 int sock;
2258
2259 /* Setup lttng message with no payload */
2260 ret = setup_lttng_msg(cmd_ctx, 0);
2261 if (ret < 0) {
2262 goto setup_error;
2263 }
2264
2265 switch (cmd_ctx->lsm->domain.type) {
2266 case LTTNG_DOMAIN_KERNEL:
2267 {
2268 /* Can't register a consumer if there is already one */
2269 if (cmd_ctx->session->kernel_session->consumer_fd != 0) {
2270 ret = LTTCOMM_CONNECT_FAIL;
2271 goto error;
2272 }
2273
2274 sock = lttcomm_connect_unix_sock(cmd_ctx->lsm->u.reg.path);
2275 if (sock < 0) {
2276 ret = LTTCOMM_CONNECT_FAIL;
2277 goto error;
2278 }
2279
2280 cmd_ctx->session->kernel_session->consumer_fd = sock;
2281 break;
2282 }
2283 default:
2284 /* TODO: Userspace tracing */
2285 ret = LTTCOMM_NOT_IMPLEMENTED;
2286 goto error;
2287 }
2288
2289 ret = LTTCOMM_OK;
2290 break;
2291 }
2292
2293 default:
2294 /* Undefined command */
2295 ret = setup_lttng_msg(cmd_ctx, 0);
2296 if (ret < 0) {
2297 goto setup_error;
2298 }
2299
2300 ret = LTTCOMM_UND;
2301 break;
2302 }
2303
2304 /* Set return code */
2305 cmd_ctx->llm->ret_code = ret;
2306
2307 if (cmd_ctx->session) {
2308 unlock_session(cmd_ctx->session);
2309 }
2310
2311 return ret;
2312
2313error:
2314 if (cmd_ctx->llm == NULL) {
2315 DBG("Missing llm structure. Allocating one.");
2316 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
2317 goto setup_error;
2318 }
2319 }
2320 /* Notify client of error */
2321 cmd_ctx->llm->ret_code = ret;
2322
2323setup_error:
2324 if (cmd_ctx->session) {
2325 unlock_session(cmd_ctx->session);
2326 }
2327 return ret;
2328}
2329
2330/*
2331 * This thread manage all clients request using the unix client socket for
2332 * communication.
2333 */
2334static void *thread_manage_clients(void *data)
2335{
2336 int sock = 0, ret;
2337 struct command_ctx *cmd_ctx = NULL;
2338 struct pollfd pollfd[2];
2339
2340 tracepoint(sessiond_th_cli_start);
2341
2342 DBG("[thread] Manage client started");
2343
2344 ret = lttcomm_listen_unix_sock(client_sock);
2345 if (ret < 0) {
2346 goto error;
2347 }
2348
2349 /* First fd is always the quit pipe */
2350 pollfd[0].fd = thread_quit_pipe[0];
2351
2352 /* Apps socket */
2353 pollfd[1].fd = client_sock;
2354 pollfd[1].events = POLLIN;
2355
2356 /* Notify parent pid that we are ready
2357 * to accept command for client side.
2358 */
2359 if (opt_sig_parent) {
2360 kill(ppid, SIGCHLD);
2361 }
2362
2363 while (1) {
2364 DBG("Accepting client command ...");
2365
2366 tracepoint(sessiond_th_cli_poll);
2367
2368 /* Inifinite blocking call, waiting for transmission */
2369 ret = poll(pollfd, 2, -1);
2370 if (ret < 0) {
2371 perror("poll client thread");
2372 goto error;
2373 }
2374
2375 /* Thread quit pipe has been closed. Killing thread. */
2376 if (pollfd[0].revents == POLLNVAL) {
2377 goto error;
2378 } else if (pollfd[1].revents == POLLERR) {
2379 ERR("Client socket poll error");
2380 goto error;
2381 }
2382
2383 sock = lttcomm_accept_unix_sock(client_sock);
2384 if (sock < 0) {
2385 goto error;
2386 }
2387
2388 /* Allocate context command to process the client request */
2389 cmd_ctx = malloc(sizeof(struct command_ctx));
2390
2391 /* Allocate data buffer for reception */
2392 cmd_ctx->lsm = malloc(sizeof(struct lttcomm_session_msg));
2393 cmd_ctx->llm = NULL;
2394 cmd_ctx->session = NULL;
2395
2396 /*
2397 * Data is received from the lttng client. The struct
2398 * lttcomm_session_msg (lsm) contains the command and data request of
2399 * the client.
2400 */
2401 DBG("Receiving data from client ...");
2402 ret = lttcomm_recv_unix_sock(sock, cmd_ctx->lsm, sizeof(struct lttcomm_session_msg));
2403 if (ret <= 0) {
2404 continue;
2405 }
2406
2407 // TODO: Validate cmd_ctx including sanity check for security purpose.
2408
2409 /*
2410 * This function dispatch the work to the kernel or userspace tracer
2411 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2412 * informations for the client. The command context struct contains
2413 * everything this function may needs.
2414 */
2415 ret = process_client_msg(cmd_ctx);
2416 if (ret < 0) {
2417 /* TODO: Inform client somehow of the fatal error. At this point,
2418 * ret < 0 means that a malloc failed (ENOMEM). */
2419 /* Error detected but still accept command */
2420 clean_command_ctx(&cmd_ctx);
2421 continue;
2422 }
2423
2424 DBG("Sending response (size: %d, retcode: %d)",
2425 cmd_ctx->lttng_msg_size, cmd_ctx->llm->ret_code);
2426 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
2427 if (ret < 0) {
2428 ERR("Failed to send data back to client");
2429 }
2430
2431 clean_command_ctx(&cmd_ctx);
2432
2433 /* End of transmission */
2434 close(sock);
2435 }
2436
2437error:
2438 DBG("Client thread dying");
2439 if (client_sock) {
2440 close(client_sock);
2441 }
2442 if (sock) {
2443 close(sock);
2444 }
2445
2446 unlink(client_unix_sock_path);
2447
2448 clean_command_ctx(&cmd_ctx);
2449 return NULL;
2450}
2451
2452
2453/*
2454 * usage function on stderr
2455 */
2456static void usage(void)
2457{
2458 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
2459 fprintf(stderr, " -h, --help Display this usage.\n");
2460 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
2461 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2462 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2463 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2464 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
2465 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2466 fprintf(stderr, " -V, --version Show version number.\n");
2467 fprintf(stderr, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2468 fprintf(stderr, " -q, --quiet No output at all.\n");
2469 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2470 fprintf(stderr, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2471}
2472
2473/*
2474 * daemon argument parsing
2475 */
2476static int parse_args(int argc, char **argv)
2477{
2478 int c;
2479
2480 static struct option long_options[] = {
2481 { "client-sock", 1, 0, 'c' },
2482 { "apps-sock", 1, 0, 'a' },
2483 { "kconsumerd-cmd-sock", 1, 0, 0 },
2484 { "kconsumerd-err-sock", 1, 0, 0 },
2485 { "daemonize", 0, 0, 'd' },
2486 { "sig-parent", 0, 0, 'S' },
2487 { "help", 0, 0, 'h' },
2488 { "group", 1, 0, 'g' },
2489 { "version", 0, 0, 'V' },
2490 { "quiet", 0, 0, 'q' },
2491 { "verbose", 0, 0, 'v' },
2492 { "verbose-kconsumerd", 0, 0, 'Z' },
2493 { NULL, 0, 0, 0 }
2494 };
2495
2496 while (1) {
2497 int option_index = 0;
2498 c = getopt_long(argc, argv, "dhqvVS" "a:c:g:s:E:C:Z", long_options, &option_index);
2499 if (c == -1) {
2500 break;
2501 }
2502
2503 switch (c) {
2504 case 0:
2505 fprintf(stderr, "option %s", long_options[option_index].name);
2506 if (optarg) {
2507 fprintf(stderr, " with arg %s\n", optarg);
2508 }
2509 break;
2510 case 'c':
2511 snprintf(client_unix_sock_path, PATH_MAX, "%s", optarg);
2512 break;
2513 case 'a':
2514 snprintf(apps_unix_sock_path, PATH_MAX, "%s", optarg);
2515 break;
2516 case 'd':
2517 opt_daemon = 1;
2518 break;
2519 case 'g':
2520 opt_tracing_group = strdup(optarg);
2521 break;
2522 case 'h':
2523 usage();
2524 exit(EXIT_FAILURE);
2525 case 'V':
2526 fprintf(stdout, "%s\n", VERSION);
2527 exit(EXIT_SUCCESS);
2528 case 'S':
2529 opt_sig_parent = 1;
2530 break;
2531 case 'E':
2532 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, "%s", optarg);
2533 break;
2534 case 'C':
2535 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, "%s", optarg);
2536 break;
2537 case 'q':
2538 opt_quiet = 1;
2539 break;
2540 case 'v':
2541 /* Verbose level can increase using multiple -v */
2542 opt_verbose += 1;
2543 break;
2544 case 'Z':
2545 opt_verbose_kconsumerd += 1;
2546 break;
2547 default:
2548 /* Unknown option or other error.
2549 * Error is printed by getopt, just return */
2550 return -1;
2551 }
2552 }
2553
2554 return 0;
2555}
2556
2557/*
2558 * Creates the two needed socket by the daemon.
2559 * apps_sock - The communication socket for all UST apps.
2560 * client_sock - The communication of the cli tool (lttng).
2561 */
2562static int init_daemon_socket(void)
2563{
2564 int ret = 0;
2565 mode_t old_umask;
2566
2567 old_umask = umask(0);
2568
2569 /* Create client tool unix socket */
2570 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
2571 if (client_sock < 0) {
2572 ERR("Create unix sock failed: %s", client_unix_sock_path);
2573 ret = -1;
2574 goto end;
2575 }
2576
2577 /* File permission MUST be 660 */
2578 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2579 if (ret < 0) {
2580 ERR("Set file permissions failed: %s", client_unix_sock_path);
2581 perror("chmod");
2582 goto end;
2583 }
2584
2585 /* Create the application unix socket */
2586 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
2587 if (apps_sock < 0) {
2588 ERR("Create unix sock failed: %s", apps_unix_sock_path);
2589 ret = -1;
2590 goto end;
2591 }
2592
2593 /* File permission MUST be 666 */
2594 ret = chmod(apps_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
2595 if (ret < 0) {
2596 ERR("Set file permissions failed: %s", apps_unix_sock_path);
2597 perror("chmod");
2598 goto end;
2599 }
2600
2601end:
2602 umask(old_umask);
2603 return ret;
2604}
2605
2606/*
2607 * Check if the global socket is available, and if a daemon is answering
2608 * at the other side. If yes, error is returned.
2609 */
2610static int check_existing_daemon(void)
2611{
2612 if (access(client_unix_sock_path, F_OK) < 0 &&
2613 access(apps_unix_sock_path, F_OK) < 0)
2614 return 0;
2615 /* Is there anybody out there ? */
2616 if (lttng_session_daemon_alive())
2617 return -EEXIST;
2618 else
2619 return 0;
2620}
2621
2622/*
2623 * Set the tracing group gid onto the client socket.
2624 *
2625 * Race window between mkdir and chown is OK because we are going from more
2626 * permissive (root.root) to les permissive (root.tracing).
2627 */
2628static int set_permissions(void)
2629{
2630 int ret;
2631 gid_t gid;
2632
2633 gid = allowed_group();
2634 if (gid < 0) {
2635 if (is_root) {
2636 WARN("No tracing group detected");
2637 ret = 0;
2638 } else {
2639 ERR("Missing tracing group. Aborting execution.");
2640 ret = -1;
2641 }
2642 goto end;
2643 }
2644
2645 /* Set lttng run dir */
2646 ret = chown(LTTNG_RUNDIR, 0, gid);
2647 if (ret < 0) {
2648 ERR("Unable to set group on " LTTNG_RUNDIR);
2649 perror("chown");
2650 }
2651
2652 /* lttng client socket path */
2653 ret = chown(client_unix_sock_path, 0, gid);
2654 if (ret < 0) {
2655 ERR("Unable to set group on %s", client_unix_sock_path);
2656 perror("chown");
2657 }
2658
2659 /* kconsumerd error socket path */
2660 ret = chown(kconsumerd_err_unix_sock_path, 0, gid);
2661 if (ret < 0) {
2662 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path);
2663 perror("chown");
2664 }
2665
2666 DBG("All permissions are set");
2667
2668end:
2669 return ret;
2670}
2671
2672/*
2673 * Create the pipe used to wake up the kernel thread.
2674 */
2675static int create_kernel_poll_pipe(void)
2676{
2677 return pipe2(kernel_poll_pipe, O_CLOEXEC);
2678}
2679
2680/*
2681 * Create the lttng run directory needed for all global sockets and pipe.
2682 */
2683static int create_lttng_rundir(void)
2684{
2685 int ret;
2686
2687 ret = mkdir(LTTNG_RUNDIR, S_IRWXU | S_IRWXG );
2688 if (ret < 0) {
2689 if (errno != EEXIST) {
2690 ERR("Unable to create " LTTNG_RUNDIR);
2691 goto error;
2692 } else {
2693 ret = 0;
2694 }
2695 }
2696
2697error:
2698 return ret;
2699}
2700
2701/*
2702 * Setup sockets and directory needed by the kconsumerd communication with the
2703 * session daemon.
2704 */
2705static int set_kconsumerd_sockets(void)
2706{
2707 int ret;
2708
2709 if (strlen(kconsumerd_err_unix_sock_path) == 0) {
2710 snprintf(kconsumerd_err_unix_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH);
2711 }
2712
2713 if (strlen(kconsumerd_cmd_unix_sock_path) == 0) {
2714 snprintf(kconsumerd_cmd_unix_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH);
2715 }
2716
2717 ret = mkdir(KCONSUMERD_PATH, S_IRWXU | S_IRWXG);
2718 if (ret < 0) {
2719 if (errno != EEXIST) {
2720 ERR("Failed to create " KCONSUMERD_PATH);
2721 goto error;
2722 }
2723 ret = 0;
2724 }
2725
2726 /* Create the kconsumerd error unix socket */
2727 kconsumerd_err_sock = lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path);
2728 if (kconsumerd_err_sock < 0) {
2729 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path);
2730 ret = -1;
2731 goto error;
2732 }
2733
2734 /* File permission MUST be 660 */
2735 ret = chmod(kconsumerd_err_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
2736 if (ret < 0) {
2737 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path);
2738 perror("chmod");
2739 goto error;
2740 }
2741
2742error:
2743 return ret;
2744}
2745
2746/*
2747 * Signal handler for the daemon
2748 *
2749 * Simply stop all worker threads, leaving main() return gracefully
2750 * after joining all threads and calling cleanup().
2751 */
2752static void sighandler(int sig)
2753{
2754 switch (sig) {
2755 case SIGPIPE:
2756 DBG("SIGPIPE catched");
2757 return;
2758 case SIGINT:
2759 DBG("SIGINT catched");
2760 stop_threads();
2761 break;
2762 case SIGTERM:
2763 DBG("SIGTERM catched");
2764 stop_threads();
2765 break;
2766 default:
2767 break;
2768 }
2769}
2770
2771/*
2772 * Setup signal handler for :
2773 * SIGINT, SIGTERM, SIGPIPE
2774 */
2775static int set_signal_handler(void)
2776{
2777 int ret = 0;
2778 struct sigaction sa;
2779 sigset_t sigset;
2780
2781 if ((ret = sigemptyset(&sigset)) < 0) {
2782 perror("sigemptyset");
2783 return ret;
2784 }
2785
2786 sa.sa_handler = sighandler;
2787 sa.sa_mask = sigset;
2788 sa.sa_flags = 0;
2789 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
2790 perror("sigaction");
2791 return ret;
2792 }
2793
2794 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
2795 perror("sigaction");
2796 return ret;
2797 }
2798
2799 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
2800 perror("sigaction");
2801 return ret;
2802 }
2803
2804 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2805
2806 return ret;
2807}
2808
2809/*
2810 * Set open files limit to unlimited. This daemon can open a large number of
2811 * file descriptors in order to consumer multiple kernel traces.
2812 */
2813static void set_ulimit(void)
2814{
2815 int ret;
2816 struct rlimit lim;
2817
2818 /* The kernel does not allowed an infinite limit for open files */
2819 lim.rlim_cur = 65535;
2820 lim.rlim_max = 65535;
2821
2822 ret = setrlimit(RLIMIT_NOFILE, &lim);
2823 if (ret < 0) {
2824 perror("failed to set open files limit");
2825 }
2826}
2827
2828/*
2829 * main
2830 */
2831int main(int argc, char **argv)
2832{
2833 int ret = 0;
2834 void *status;
2835 const char *home_path;
2836
2837 tracepoint(sessiond_boot_start);
2838
2839 /* Create thread quit pipe */
2840 if ((ret = init_thread_quit_pipe()) < 0) {
2841 goto error;
2842 }
2843
2844 /* Parse arguments */
2845 progname = argv[0];
2846 if ((ret = parse_args(argc, argv) < 0)) {
2847 goto error;
2848 }
2849
2850 /* Daemonize */
2851 if (opt_daemon) {
2852 ret = daemon(0, 0);
2853 if (ret < 0) {
2854 perror("daemon");
2855 goto error;
2856 }
2857 }
2858
2859 /* Check if daemon is UID = 0 */
2860 is_root = !getuid();
2861
2862 if (is_root) {
2863 ret = create_lttng_rundir();
2864 if (ret < 0) {
2865 goto error;
2866 }
2867
2868 if (strlen(apps_unix_sock_path) == 0) {
2869 snprintf(apps_unix_sock_path, PATH_MAX,
2870 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
2871 }
2872
2873 if (strlen(client_unix_sock_path) == 0) {
2874 snprintf(client_unix_sock_path, PATH_MAX,
2875 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
2876 }
2877 } else {
2878 home_path = get_home_dir();
2879 if (home_path == NULL) {
2880 /* TODO: Add --socket PATH option */
2881 ERR("Can't get HOME directory for sockets creation.");
2882 ret = -EPERM;
2883 goto error;
2884 }
2885
2886 if (strlen(apps_unix_sock_path) == 0) {
2887 snprintf(apps_unix_sock_path, PATH_MAX,
2888 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
2889 }
2890
2891 /* Set the cli tool unix socket path */
2892 if (strlen(client_unix_sock_path) == 0) {
2893 snprintf(client_unix_sock_path, PATH_MAX,
2894 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
2895 }
2896 }
2897
2898 DBG("Client socket path %s", client_unix_sock_path);
2899 DBG("Application socket path %s", apps_unix_sock_path);
2900
2901 /*
2902 * See if daemon already exist.
2903 */
2904 if ((ret = check_existing_daemon()) < 0) {
2905 ERR("Already running daemon.\n");
2906 /*
2907 * We do not goto exit because we must not cleanup()
2908 * because a daemon is already running.
2909 */
2910 goto error;
2911 }
2912
2913 /* After this point, we can safely call cleanup() so goto error is used */
2914
2915 /*
2916 * These actions must be executed as root. We do that *after* setting up
2917 * the sockets path because we MUST make the check for another daemon using
2918 * those paths *before* trying to set the kernel consumer sockets and init
2919 * kernel tracer.
2920 */
2921 if (is_root) {
2922 ret = set_kconsumerd_sockets();
2923 if (ret < 0) {
2924 goto exit;
2925 }
2926
2927 /* Setup kernel tracer */
2928 init_kernel_tracer();
2929
2930 /* Set ulimit for open files */
2931 set_ulimit();
2932 }
2933
2934 if ((ret = set_signal_handler()) < 0) {
2935 goto exit;
2936 }
2937
2938 /* Setup the needed unix socket */
2939 if ((ret = init_daemon_socket()) < 0) {
2940 goto exit;
2941 }
2942
2943 /* Set credentials to socket */
2944 if (is_root && ((ret = set_permissions()) < 0)) {
2945 goto exit;
2946 }
2947
2948 /* Get parent pid if -S, --sig-parent is specified. */
2949 if (opt_sig_parent) {
2950 ppid = getppid();
2951 }
2952
2953 /* Setup the kernel pipe for waking up the kernel thread */
2954 if ((ret = create_kernel_poll_pipe()) < 0) {
2955 goto exit;
2956 }
2957
2958 /*
2959 * Get session list pointer. This pointer MUST NOT be free().
2960 * This list is statically declared in session.c
2961 */
2962 session_list_ptr = get_session_list();
2963
2964 /* Create thread to manage the client socket */
2965 ret = pthread_create(&client_thread, NULL, thread_manage_clients, (void *) NULL);
2966 if (ret != 0) {
2967 perror("pthread_create");
2968 goto exit_client;
2969 }
2970
2971 /* Create thread to manage application socket */
2972 ret = pthread_create(&apps_thread, NULL, thread_manage_apps, (void *) NULL);
2973 if (ret != 0) {
2974 perror("pthread_create");
2975 goto exit_apps;
2976 }
2977
2978 /* Create kernel thread to manage kernel event */
2979 ret = pthread_create(&kernel_thread, NULL, thread_manage_kernel, (void *) NULL);
2980 if (ret != 0) {
2981 perror("pthread_create");
2982 goto exit_kernel;
2983 }
2984
2985 tracepoint(sessiond_boot_end);
2986
2987 ret = pthread_join(kernel_thread, &status);
2988 if (ret != 0) {
2989 perror("pthread_join");
2990 goto error; /* join error, exit without cleanup */
2991 }
2992
2993exit_kernel:
2994 ret = pthread_join(apps_thread, &status);
2995 if (ret != 0) {
2996 perror("pthread_join");
2997 goto error; /* join error, exit without cleanup */
2998 }
2999
3000exit_apps:
3001 ret = pthread_join(client_thread, &status);
3002 if (ret != 0) {
3003 perror("pthread_join");
3004 goto error; /* join error, exit without cleanup */
3005 }
3006
3007 ret = join_kconsumerd_thread();
3008 if (ret != 0) {
3009 perror("join_kconsumerd");
3010 goto error; /* join error, exit without cleanup */
3011 }
3012
3013exit_client:
3014exit:
3015 /*
3016 * cleanup() is called when no other thread is running.
3017 */
3018 cleanup();
3019 if (!ret)
3020 exit(EXIT_SUCCESS);
3021error:
3022 exit(EXIT_FAILURE);
3023}
This page took 0.073331 seconds and 4 git commands to generate.