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