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