2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
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.
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.
27 #include <semaphore.h>
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
39 #include <ltt-kconsumerd.h>
40 #include <lttng-sessiond-comm.h>
41 #include <lttng/lttng-kconsumerd.h>
45 #include "kernel-ctl.h"
46 #include "ltt-sessiond.h"
47 #include "traceable-app.h"
51 #include "benchmark.h"
54 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
55 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
56 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
57 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
60 int opt_verbose
; /* Not static for lttngerr.h */
61 int opt_verbose_kconsumerd
; /* Not static for lttngerr.h */
62 int opt_quiet
; /* Not static for lttngerr.h */
65 const char *opt_tracing_group
;
66 static int opt_sig_parent
;
67 static int opt_daemon
;
68 static int is_root
; /* Set to 1 if the daemon is running as root */
69 static pid_t ppid
; /* Parent PID for --sig-parent option */
70 static pid_t kconsumerd_pid
;
71 static struct pollfd
*kernel_pollfd
;
73 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
74 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
75 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
76 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
79 static int client_sock
;
81 static int kconsumerd_err_sock
;
82 static int kconsumerd_cmd_sock
;
83 static int kernel_tracer_fd
;
84 static int kernel_poll_pipe
[2];
87 * Quit pipe for all threads. This permits a single cancellation point
88 * for all threads when receiving an event on the pipe.
90 static int thread_quit_pipe
[2];
92 /* Pthread, Mutexes and Semaphores */
93 static pthread_t kconsumerd_thread
;
94 static pthread_t apps_thread
;
95 static pthread_t client_thread
;
96 static pthread_t kernel_thread
;
97 static sem_t kconsumerd_sem
;
99 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
101 static int modprobe_remove_kernel_modules(void);
104 * Pointer initialized before thread creation.
106 * This points to the tracing session list containing the session count and a
107 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
108 * MUST NOT be taken if you call a public function in session.c.
110 * The lock is nested inside the structure: session_list_ptr->lock. Please use
111 * lock_session_list and unlock_session_list for lock acquisition.
113 static struct ltt_session_list
*session_list_ptr
;
115 static gid_t
allowed_group(void)
119 if (opt_tracing_group
) {
120 grp
= getgrnam(opt_tracing_group
);
122 grp
= getgrnam(default_tracing_group
);
134 * Return -1 on error or 0 if all pipes are created.
136 static int init_thread_quit_pipe(void)
140 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
142 perror("thread quit pipe");
151 * Complete teardown of a kernel session. This free all data structure related
152 * to a kernel session and update counter.
154 static void teardown_kernel_session(struct ltt_session
*session
)
156 if (session
->kernel_session
!= NULL
) {
157 DBG("Tearing down kernel session");
160 * If a custom kernel consumer was registered, close the socket before
161 * tearing down the complete kernel session structure
163 if (session
->kernel_session
->consumer_fd
!= kconsumerd_cmd_sock
) {
164 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
167 trace_destroy_kernel_session(session
->kernel_session
);
168 /* Extra precaution */
169 session
->kernel_session
= NULL
;
173 static void stop_threads(void)
175 /* Stopping all threads */
176 DBG("Terminating all threads");
177 close(thread_quit_pipe
[0]);
178 close(thread_quit_pipe
[1]);
184 static void cleanup(void)
188 struct ltt_session
*sess
, *stmp
;
193 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
194 "Matthew, BEET driven development works!%c[%dm",
195 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
198 DBG("Removing %s directory", LTTNG_RUNDIR
);
199 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
201 ERR("asprintf failed. Something is really wrong!");
204 /* Remove lttng run directory */
207 ERR("Unable to clean " LTTNG_RUNDIR
);
210 DBG("Cleaning up all session");
212 /* Destroy session list mutex */
213 if (session_list_ptr
!= NULL
) {
214 pthread_mutex_destroy(&session_list_ptr
->lock
);
216 /* Cleanup ALL session */
217 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
218 teardown_kernel_session(sess
);
219 // TODO complete session cleanup (including UST)
223 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
225 DBG("Closing kernel fd");
226 close(kernel_tracer_fd
);
228 DBG("Unloading kernel modules");
229 modprobe_remove_kernel_modules();
231 /* OUTPUT BENCHMARK RESULTS */
234 bench_print_boot_process();
241 * Send data on a unix socket using the liblttsessiondcomm API.
243 * Return lttcomm error code.
245 static int send_unix_sock(int sock
, void *buf
, size_t len
)
247 /* Check valid length */
252 return lttcomm_send_unix_sock(sock
, buf
, len
);
256 * Free memory of a command context structure.
258 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
260 DBG("Clean command context structure");
262 if ((*cmd_ctx
)->llm
) {
263 free((*cmd_ctx
)->llm
);
265 if ((*cmd_ctx
)->lsm
) {
266 free((*cmd_ctx
)->lsm
);
274 * Send all stream fds of kernel channel to the consumer.
276 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
280 struct ltt_kernel_stream
*stream
;
281 struct lttcomm_kconsumerd_header lkh
;
282 struct lttcomm_kconsumerd_msg lkm
;
284 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
286 nb_fd
= channel
->stream_count
;
289 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
290 lkh
.cmd_type
= ADD_STREAM
;
292 DBG("Sending kconsumerd header");
294 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
296 perror("send kconsumerd header");
300 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
301 if (stream
->fd
!= 0) {
303 lkm
.state
= stream
->state
;
304 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
305 lkm
.output
= channel
->channel
->attr
.output
;
306 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
307 lkm
.path_name
[PATH_MAX
- 1] = '\0';
309 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
311 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
313 perror("send kconsumerd fd");
319 DBG("Kconsumerd channel fds sent");
328 * Send all stream fds of the kernel session to the consumer.
330 static int send_kconsumerd_fds(struct ltt_kernel_session
*session
)
333 struct ltt_kernel_channel
*chan
;
334 struct lttcomm_kconsumerd_header lkh
;
335 struct lttcomm_kconsumerd_msg lkm
;
338 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
339 lkh
.cmd_type
= ADD_STREAM
;
341 DBG("Sending kconsumerd header for metadata");
343 ret
= lttcomm_send_unix_sock(session
->consumer_fd
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
345 perror("send kconsumerd header");
349 DBG("Sending metadata stream fd");
351 /* Extra protection. It's NOT suppose to be set to 0 at this point */
352 if (session
->consumer_fd
== 0) {
353 session
->consumer_fd
= kconsumerd_cmd_sock
;
356 if (session
->metadata_stream_fd
!= 0) {
357 /* Send metadata stream fd first */
358 lkm
.fd
= session
->metadata_stream_fd
;
359 lkm
.state
= ACTIVE_FD
;
360 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
361 lkm
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
362 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
363 lkm
.path_name
[PATH_MAX
- 1] = '\0';
365 ret
= lttcomm_send_fds_unix_sock(session
->consumer_fd
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
367 perror("send kconsumerd fd");
372 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
373 ret
= send_kconsumerd_channel_fds(session
->consumer_fd
, chan
);
379 DBG("Kconsumerd fds (metadata and channel streams) sent");
389 * Return a socket connected to the libust communication socket of the
390 * application identified by the pid.
392 * If the pid is not found in the traceable list, return -1 to indicate error.
394 static int ust_connect_app(pid_t pid
)
397 struct ltt_traceable_app
*lta
;
399 DBG("Connect to application pid %d", pid
);
401 lta
= find_app_by_pid(pid
);
404 DBG("Application pid %d not found", pid
);
408 sock
= ustctl_connect_pid(lta
->pid
);
410 ERR("Fail connecting to the PID %d", pid
);
415 #endif /* DISABLED */
418 * Notify apps by writing 42 to a named pipe using name. Every applications
419 * waiting for a ltt-sessiond will be notified and re-register automatically to
420 * the session daemon.
422 * Return open or write error value.
424 static int notify_apps(const char *name
)
429 DBG("Notify the global application pipe");
431 /* Try opening the global pipe */
432 fd
= open(name
, O_WRONLY
);
437 /* Notify by writing on the pipe */
438 ret
= write(fd
, "42", 2);
448 * Setup the outgoing data buffer for the response (llm) by allocating the
449 * right amount of memory and copying the original information from the lsm
452 * Return total size of the buffer pointed by buf.
454 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
460 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
461 if (cmd_ctx
->llm
== NULL
) {
467 /* Copy common data */
468 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
469 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
471 cmd_ctx
->llm
->data_size
= size
;
472 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
481 * Update the kernel pollfd set of all channel fd available over all tracing
482 * session. Add the wakeup pipe at the end of the set.
484 static int update_kernel_pollfd(void)
488 * The wakup pipe and the quit pipe are needed so the number of fds starts
489 * at 2 for those pipes.
491 unsigned int nb_fd
= 2;
492 struct ltt_session
*session
;
493 struct ltt_kernel_channel
*channel
;
495 DBG("Updating kernel_pollfd");
497 /* Get the number of channel of all kernel session */
499 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
500 lock_session(session
);
501 if (session
->kernel_session
== NULL
) {
502 unlock_session(session
);
505 nb_fd
+= session
->kernel_session
->channel_count
;
506 unlock_session(session
);
509 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
511 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
512 if (kernel_pollfd
== NULL
) {
513 perror("malloc kernel_pollfd");
517 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
518 lock_session(session
);
519 if (session
->kernel_session
== NULL
) {
520 unlock_session(session
);
524 ERR("To much channel for kernel_pollfd size");
525 unlock_session(session
);
528 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
529 kernel_pollfd
[i
].fd
= channel
->fd
;
530 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
533 unlock_session(session
);
535 unlock_session_list();
537 /* Adding wake up pipe */
538 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
539 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
541 /* Adding the quit pipe */
542 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
547 unlock_session_list();
552 * Find the channel fd from 'fd' over all tracing session. When found, check
553 * for new channel stream and send those stream fds to the kernel consumer.
555 * Useful for CPU hotplug feature.
557 static int update_kernel_stream(int fd
)
560 struct ltt_session
*session
;
561 struct ltt_kernel_channel
*channel
;
563 DBG("Updating kernel streams for channel fd %d", fd
);
566 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
567 lock_session(session
);
568 if (session
->kernel_session
== NULL
) {
569 unlock_session(session
);
573 /* This is not suppose to be 0 but this is an extra security check */
574 if (session
->kernel_session
->consumer_fd
== 0) {
575 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
578 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
579 if (channel
->fd
== fd
) {
580 DBG("Channel found, updating kernel streams");
581 ret
= kernel_open_channel_stream(channel
);
587 * Have we already sent fds to the consumer? If yes, it means that
588 * tracing is started so it is safe to send our updated stream fds.
590 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
591 ret
= send_kconsumerd_channel_fds(session
->kernel_session
->consumer_fd
,
600 unlock_session(session
);
604 unlock_session_list();
606 unlock_session(session
);
612 * This thread manage event coming from the kernel.
614 * Features supported in this thread:
617 static void *thread_manage_kernel(void *data
)
619 int ret
, i
, nb_fd
= 0;
621 int update_poll_flag
= 1;
623 tracepoint(sessiond_th_kern_start
);
625 DBG("Thread manage kernel started");
628 if (update_poll_flag
== 1) {
629 nb_fd
= update_kernel_pollfd();
633 update_poll_flag
= 0;
636 DBG("Polling on %d fds", nb_fd
);
638 tracepoint(sessiond_th_kern_poll
);
640 /* Poll infinite value of time */
641 ret
= poll(kernel_pollfd
, nb_fd
, -1);
643 perror("poll kernel thread");
645 } else if (ret
== 0) {
646 /* Should not happen since timeout is infinite */
650 /* Thread quit pipe has been closed. Killing thread. */
651 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
655 DBG("Kernel poll event triggered");
658 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
661 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
663 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
664 update_poll_flag
= 1;
672 for (i
= 0; i
< nb_fd
; i
++) {
673 switch (kernel_pollfd
[i
].revents
) {
675 * New CPU detected by the kernel. Adding kernel stream to kernel
676 * session and updating the kernel consumer
678 case POLLIN
| POLLRDNORM
:
679 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
689 DBG("Kernel thread dying");
694 close(kernel_poll_pipe
[0]);
695 close(kernel_poll_pipe
[1]);
700 * This thread manage the kconsumerd error sent back to the session daemon.
702 static void *thread_manage_kconsumerd(void *data
)
705 enum lttcomm_return_code code
;
706 struct pollfd pollfd
[2];
708 tracepoint(sessiond_th_kcon_start
);
710 DBG("[thread] Manage kconsumerd started");
712 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
717 /* First fd is always the quit pipe */
718 pollfd
[0].fd
= thread_quit_pipe
[0];
721 pollfd
[1].fd
= kconsumerd_err_sock
;
722 pollfd
[1].events
= POLLIN
;
724 tracepoint(sessiond_th_kcon_poll
);
726 /* Inifinite blocking call, waiting for transmission */
727 ret
= poll(pollfd
, 2, -1);
729 perror("poll kconsumerd thread");
733 /* Thread quit pipe has been closed. Killing thread. */
734 if (pollfd
[0].revents
== POLLNVAL
) {
736 } else if (pollfd
[1].revents
== POLLERR
) {
737 ERR("Kconsumerd err socket poll error");
741 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
746 /* Getting status code from kconsumerd */
747 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
752 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
753 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
754 if (kconsumerd_cmd_sock
< 0) {
755 sem_post(&kconsumerd_sem
);
756 perror("kconsumerd connect");
759 /* Signal condition to tell that the kconsumerd is ready */
760 sem_post(&kconsumerd_sem
);
761 DBG("Kconsumerd command socket ready");
763 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
764 lttcomm_get_readable_code(-code
));
768 /* Kconsumerd err socket */
770 pollfd
[1].events
= POLLIN
;
772 /* Inifinite blocking call, waiting for transmission */
773 ret
= poll(pollfd
, 2, -1);
775 perror("poll kconsumerd thread");
779 /* Thread quit pipe has been closed. Killing thread. */
780 if (pollfd
[0].revents
== POLLNVAL
) {
782 } else if (pollfd
[1].revents
== POLLERR
) {
783 ERR("Kconsumerd err socket second poll error");
787 /* Wait for any kconsumerd error */
788 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
790 ERR("Kconsumerd closed the command socket");
794 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
797 DBG("Kconsumerd thread dying");
798 if (kconsumerd_err_sock
) {
799 close(kconsumerd_err_sock
);
801 if (kconsumerd_cmd_sock
) {
802 close(kconsumerd_cmd_sock
);
808 unlink(kconsumerd_err_unix_sock_path
);
809 unlink(kconsumerd_cmd_unix_sock_path
);
816 * This thread manage the application socket communication
818 static void *thread_manage_apps(void *data
)
821 struct pollfd pollfd
[2];
823 tracepoint(sessiond_th_apps_start
);
825 /* TODO: Something more elegant is needed but fine for now */
826 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
827 * for 32-bit vs 64-bit compat processes. */
828 /* replicate in ust with version number */
830 int reg
; /* 1:register, 0:unregister */
835 DBG("[thread] Manage apps started");
837 ret
= lttcomm_listen_unix_sock(apps_sock
);
842 /* First fd is always the quit pipe */
843 pollfd
[0].fd
= thread_quit_pipe
[0];
846 pollfd
[1].fd
= apps_sock
;
847 pollfd
[1].events
= POLLIN
;
849 /* Notify all applications to register */
850 notify_apps(default_global_apps_pipe
);
853 DBG("Accepting application registration");
855 tracepoint(sessiond_th_apps_poll
);
857 /* Inifinite blocking call, waiting for transmission */
858 ret
= poll(pollfd
, 2, -1);
860 perror("poll apps thread");
864 /* Thread quit pipe has been closed. Killing thread. */
865 if (pollfd
[0].revents
== POLLNVAL
) {
867 } else if (pollfd
[1].revents
== POLLERR
) {
868 ERR("Apps socket poll error");
872 sock
= lttcomm_accept_unix_sock(apps_sock
);
878 * Using message-based transmissions to ensure we don't
879 * have to deal with partially received messages.
881 ret
= lttcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
887 /* Add application to the global traceable list */
888 if (reg_msg
.reg
== 1) {
891 * TODO: socket should be either passed to a
892 * listener thread (for more messages) or
893 * closed. It currently leaks.
895 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
897 /* register_traceable_app only return an error with
898 * ENOMEM. At this point, we better stop everything.
904 unregister_traceable_app(reg_msg
.pid
);
909 DBG("Apps thread dying");
917 unlink(apps_unix_sock_path
);
922 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
923 * exec or it will fails.
925 static int spawn_kconsumerd_thread(void)
929 /* Setup semaphore */
930 sem_init(&kconsumerd_sem
, 0, 0);
932 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
934 perror("pthread_create kconsumerd");
938 /* Wait for the kconsumerd thread to be ready */
939 sem_wait(&kconsumerd_sem
);
941 if (kconsumerd_pid
== 0) {
942 ERR("Kconsumerd did not start");
949 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
954 * Join kernel consumer thread
956 static int join_kconsumerd_thread(void)
961 if (kconsumerd_pid
!= 0) {
962 ret
= kill(kconsumerd_pid
, SIGTERM
);
964 ERR("Error killing kconsumerd");
967 return pthread_join(kconsumerd_thread
, &status
);
974 * Fork and exec a kernel consumer daemon (kconsumerd).
976 * Return pid if successful else -1.
978 static pid_t
spawn_kconsumerd(void)
982 const char *verbosity
;
984 DBG("Spawning kconsumerd");
991 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
992 verbosity
= "--verbose";
994 verbosity
= "--quiet";
996 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
998 perror("kernel start consumer exec");
1001 } else if (pid
> 0) {
1005 perror("kernel start consumer fork");
1015 * Spawn the kconsumerd daemon and session daemon thread.
1017 static int start_kconsumerd(void)
1021 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1022 if (kconsumerd_pid
!= 0) {
1023 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1027 ret
= spawn_kconsumerd();
1029 ERR("Spawning kconsumerd failed");
1030 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1031 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1035 /* Setting up the global kconsumerd_pid */
1036 kconsumerd_pid
= ret
;
1037 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1039 DBG("Kconsumerd pid %d", ret
);
1041 DBG("Spawning kconsumerd thread");
1042 ret
= spawn_kconsumerd_thread();
1044 ERR("Fatal error spawning kconsumerd thread");
1056 * modprobe_kernel_modules
1058 static int modprobe_kernel_modules(void)
1063 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1064 ret
= snprintf(modprobe
, sizeof(modprobe
),
1065 "/sbin/modprobe %s%s",
1066 kernel_modules_list
[i
].required
? "" : "--quiet ",
1067 kernel_modules_list
[i
].name
);
1069 perror("snprintf modprobe");
1072 modprobe
[sizeof(modprobe
) - 1] = '\0';
1073 ret
= system(modprobe
);
1075 ERR("Unable to launch modprobe for module %s",
1076 kernel_modules_list
[i
].name
);
1077 } else if (kernel_modules_list
[i
].required
1078 && WEXITSTATUS(ret
) != 0) {
1079 ERR("Unable to load module %s",
1080 kernel_modules_list
[i
].name
);
1082 DBG("Modprobe successfully %s",
1083 kernel_modules_list
[i
].name
);
1092 * modprobe_remove_kernel_modules
1093 * Remove modules in reverse load order.
1095 static int modprobe_remove_kernel_modules(void)
1100 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
1101 ret
= snprintf(modprobe
, sizeof(modprobe
),
1102 "/sbin/modprobe --remove --quiet %s",
1103 kernel_modules_list
[i
].name
);
1105 perror("snprintf modprobe --remove");
1108 modprobe
[sizeof(modprobe
) - 1] = '\0';
1109 ret
= system(modprobe
);
1111 ERR("Unable to launch modprobe --remove for module %s",
1112 kernel_modules_list
[i
].name
);
1113 } else if (kernel_modules_list
[i
].required
1114 && WEXITSTATUS(ret
) != 0) {
1115 ERR("Unable to remove module %s",
1116 kernel_modules_list
[i
].name
);
1118 DBG("Modprobe removal successful %s",
1119 kernel_modules_list
[i
].name
);
1130 static int mount_debugfs(char *path
)
1133 char *type
= "debugfs";
1135 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1140 ret
= mount(type
, path
, type
, 0, NULL
);
1142 perror("mount debugfs");
1146 DBG("Mounted debugfs successfully at %s", path
);
1153 * Setup necessary data for kernel tracer action.
1155 static void init_kernel_tracer(void)
1158 char *proc_mounts
= "/proc/mounts";
1160 char *debugfs_path
= NULL
, *lttng_path
;
1163 /* Detect debugfs */
1164 fp
= fopen(proc_mounts
, "r");
1166 ERR("Unable to probe %s", proc_mounts
);
1170 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1171 if (strstr(line
, "debugfs") != NULL
) {
1172 /* Remove first string */
1174 /* Dup string here so we can reuse line later on */
1175 debugfs_path
= strdup(strtok(NULL
, " "));
1176 DBG("Got debugfs path : %s", debugfs_path
);
1183 /* Mount debugfs if needded */
1184 if (debugfs_path
== NULL
) {
1185 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1187 perror("asprintf debugfs path");
1190 ret
= mount_debugfs(debugfs_path
);
1196 /* Modprobe lttng kernel modules */
1197 ret
= modprobe_kernel_modules();
1202 /* Setup lttng kernel path */
1203 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1205 perror("asprintf lttng path");
1209 /* Open debugfs lttng */
1210 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1211 if (kernel_tracer_fd
< 0) {
1212 DBG("Failed to open %s", lttng_path
);
1218 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1228 WARN("No kernel tracer available");
1229 kernel_tracer_fd
= 0;
1234 * Start tracing by creating trace directory and sending FDs to the kernel
1237 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1241 if (session
->kconsumer_fds_sent
== 0) {
1243 * Assign default kernel consumer if no consumer assigned to the kernel
1244 * session. At this point, it's NOT suppose to be 0 but this is an extra
1247 if (session
->consumer_fd
== 0) {
1248 session
->consumer_fd
= kconsumerd_cmd_sock
;
1251 ret
= send_kconsumerd_fds(session
);
1253 ERR("Send kconsumerd fds failed");
1254 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1258 session
->kconsumer_fds_sent
= 1;
1266 * Notify kernel thread to update it's pollfd.
1268 static int notify_kernel_pollfd(void)
1272 /* Inform kernel thread of the new kernel channel */
1273 ret
= write(kernel_poll_pipe
[1], "!", 1);
1275 perror("write kernel poll pipe");
1282 * Allocate a channel structure and fill it.
1284 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1287 struct lttng_channel
*chan
;
1289 chan
= malloc(sizeof(struct lttng_channel
));
1291 perror("init channel malloc");
1295 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1296 perror("snprintf channel name");
1300 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1301 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1302 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1304 switch (domain_type
) {
1305 case LTTNG_DOMAIN_KERNEL
:
1306 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1307 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1308 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1312 goto error
; /* Not implemented */
1323 * Create a kernel tracer session then create the default channel.
1325 static int create_kernel_session(struct ltt_session
*session
)
1329 DBG("Creating kernel session");
1331 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1333 ret
= LTTCOMM_KERN_SESS_FAIL
;
1337 /* Set kernel consumer socket fd */
1338 if (kconsumerd_cmd_sock
) {
1339 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1342 ret
= asprintf(&session
->kernel_session
->trace_path
, "%s/kernel",
1345 perror("asprintf kernel traces path");
1349 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1350 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1352 if (ret
!= -EEXIST
) {
1353 ERR("Trace directory creation error");
1363 * Using the session list, filled a lttng_session array to send back to the
1364 * client for session listing.
1366 * The session list lock MUST be acquired before calling this function. Use
1367 * lock_session_list() and unlock_session_list().
1369 static void list_lttng_sessions(struct lttng_session
*sessions
)
1372 struct ltt_session
*session
;
1374 DBG("Getting all available session");
1376 * Iterate over session list and append data after the control struct in
1379 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1380 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1381 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1382 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1383 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1389 * Fill lttng_channel array of all channels.
1391 static void list_lttng_channels(struct ltt_session
*session
,
1392 struct lttng_channel
*channels
)
1395 struct ltt_kernel_channel
*kchan
;
1397 DBG("Listing channels for session %s", session
->name
);
1399 /* Kernel channels */
1400 if (session
->kernel_session
!= NULL
) {
1401 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1402 /* Copy lttng_channel struct to array */
1403 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1404 channels
[i
].enabled
= kchan
->enabled
;
1409 /* TODO: Missing UST listing */
1413 * Fill lttng_event array of all events in the channel.
1415 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1416 struct lttng_event
*events
)
1419 * TODO: This is ONLY kernel. Need UST support.
1422 struct ltt_kernel_event
*event
;
1424 DBG("Listing events for channel %s", kchan
->channel
->name
);
1426 /* Kernel channels */
1427 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1428 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1429 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1430 events
[i
].enabled
= event
->enabled
;
1431 switch (event
->event
->instrumentation
) {
1432 case LTTNG_KERNEL_TRACEPOINT
:
1433 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1435 case LTTNG_KERNEL_KPROBE
:
1436 case LTTNG_KERNEL_KRETPROBE
:
1437 events
[i
].type
= LTTNG_EVENT_PROBE
;
1438 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1439 sizeof(struct lttng_kernel_kprobe
));
1441 case LTTNG_KERNEL_FUNCTION
:
1442 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1443 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1444 sizeof(struct lttng_kernel_function
));
1452 * Process the command requested by the lttng client within the command
1453 * context structure. This function make sure that the return structure (llm)
1454 * is set and ready for transmission before returning.
1456 * Return any error encountered or 0 for success.
1458 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1460 int ret
= LTTCOMM_OK
;
1462 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1465 * Commands that DO NOT need a session.
1467 switch (cmd_ctx
->lsm
->cmd_type
) {
1468 case LTTNG_CREATE_SESSION
:
1469 case LTTNG_LIST_SESSIONS
:
1470 case LTTNG_LIST_TRACEPOINTS
:
1471 case LTTNG_CALIBRATE
:
1474 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1475 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1476 if (cmd_ctx
->session
== NULL
) {
1477 /* If session name not found */
1478 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1479 ret
= LTTCOMM_SESS_NOT_FOUND
;
1480 } else { /* If no session name specified */
1481 ret
= LTTCOMM_SELECT_SESS
;
1485 /* Acquire lock for the session */
1486 lock_session(cmd_ctx
->session
);
1492 * Check domain type for specific "pre-action".
1494 switch (cmd_ctx
->lsm
->domain
.type
) {
1495 case LTTNG_DOMAIN_KERNEL
:
1496 /* Kernel tracer check */
1497 if (kernel_tracer_fd
== 0) {
1498 init_kernel_tracer();
1499 if (kernel_tracer_fd
== 0) {
1500 ret
= LTTCOMM_KERN_NA
;
1505 /* Need a session for kernel command */
1506 switch (cmd_ctx
->lsm
->cmd_type
) {
1507 case LTTNG_CALIBRATE
:
1508 case LTTNG_CREATE_SESSION
:
1509 case LTTNG_LIST_SESSIONS
:
1510 case LTTNG_LIST_TRACEPOINTS
:
1513 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1514 ret
= create_kernel_session(cmd_ctx
->session
);
1516 ret
= LTTCOMM_KERN_SESS_FAIL
;
1520 /* Start the kernel consumer daemon */
1522 if (kconsumerd_pid
== 0 &&
1523 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1524 ret
= start_kconsumerd();
1536 /* Process by command type */
1537 switch (cmd_ctx
->lsm
->cmd_type
) {
1538 case LTTNG_ADD_CONTEXT
:
1540 struct lttng_kernel_context kctx
;
1542 /* Setup lttng message with no payload */
1543 ret
= setup_lttng_msg(cmd_ctx
, 0);
1548 switch (cmd_ctx
->lsm
->domain
.type
) {
1549 case LTTNG_DOMAIN_KERNEL
:
1550 /* Create Kernel context */
1551 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1552 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1553 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1554 strncpy(kctx
.u
.perf_counter
.name
,
1555 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1556 LTTNG_SYMBOL_NAME_LEN
);
1557 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1559 /* Add kernel context to kernel tracer. See context.c */
1560 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1561 cmd_ctx
->lsm
->u
.context
.event_name
,
1562 cmd_ctx
->lsm
->u
.context
.channel_name
);
1563 if (ret
!= LTTCOMM_OK
) {
1568 /* TODO: Userspace tracing */
1569 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1576 case LTTNG_DISABLE_CHANNEL
:
1578 struct ltt_kernel_channel
*kchan
;
1580 /* Setup lttng message with no payload */
1581 ret
= setup_lttng_msg(cmd_ctx
, 0);
1586 switch (cmd_ctx
->lsm
->domain
.type
) {
1587 case LTTNG_DOMAIN_KERNEL
:
1588 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1589 cmd_ctx
->session
->kernel_session
);
1590 if (kchan
== NULL
) {
1591 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1593 } else if (kchan
->enabled
== 1) {
1594 ret
= kernel_disable_channel(kchan
);
1596 if (ret
!= EEXIST
) {
1597 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1602 kernel_wait_quiescent(kernel_tracer_fd
);
1605 /* TODO: Userspace tracing */
1606 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1613 case LTTNG_DISABLE_EVENT
:
1615 struct ltt_kernel_channel
*kchan
;
1616 struct ltt_kernel_event
*kevent
;
1618 /* Setup lttng message with no payload */
1619 ret
= setup_lttng_msg(cmd_ctx
, 0);
1624 switch (cmd_ctx
->lsm
->domain
.type
) {
1625 case LTTNG_DOMAIN_KERNEL
:
1626 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1627 cmd_ctx
->session
->kernel_session
);
1628 if (kchan
== NULL
) {
1629 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1633 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1634 if (kevent
!= NULL
) {
1635 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1636 kchan
->channel
->name
);
1637 ret
= kernel_disable_event(kevent
);
1639 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1644 kernel_wait_quiescent(kernel_tracer_fd
);
1647 /* TODO: Userspace tracing */
1648 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1655 case LTTNG_DISABLE_ALL_EVENT
:
1657 struct ltt_kernel_channel
*kchan
;
1658 struct ltt_kernel_event
*kevent
;
1660 /* Setup lttng message with no payload */
1661 ret
= setup_lttng_msg(cmd_ctx
, 0);
1666 switch (cmd_ctx
->lsm
->domain
.type
) {
1667 case LTTNG_DOMAIN_KERNEL
:
1668 DBG("Disabling all enabled kernel events");
1669 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1670 cmd_ctx
->session
->kernel_session
);
1671 if (kchan
== NULL
) {
1672 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1676 /* For each event in the kernel session */
1677 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1678 DBG("Disabling kernel event %s for channel %s.",
1679 kevent
->event
->name
, kchan
->channel
->name
);
1680 ret
= kernel_disable_event(kevent
);
1686 /* Quiescent wait after event disable */
1687 kernel_wait_quiescent(kernel_tracer_fd
);
1690 /* TODO: Userspace tracing */
1691 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1698 case LTTNG_ENABLE_CHANNEL
:
1700 struct ltt_kernel_channel
*kchan
;
1702 /* Setup lttng message with no payload */
1703 ret
= setup_lttng_msg(cmd_ctx
, 0);
1708 switch (cmd_ctx
->lsm
->domain
.type
) {
1709 case LTTNG_DOMAIN_KERNEL
:
1710 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1711 cmd_ctx
->session
->kernel_session
);
1712 if (kchan
== NULL
) {
1713 /* Channel not found, creating it */
1714 DBG("Creating kernel channel");
1716 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1717 &cmd_ctx
->lsm
->u
.channel
.chan
,
1718 cmd_ctx
->session
->kernel_session
->trace_path
);
1720 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1724 /* Notify kernel thread that there is a new channel */
1725 ret
= notify_kernel_pollfd();
1727 ret
= LTTCOMM_FATAL
;
1730 } else if (kchan
->enabled
== 0) {
1731 ret
= kernel_enable_channel(kchan
);
1733 if (ret
!= EEXIST
) {
1734 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1740 kernel_wait_quiescent(kernel_tracer_fd
);
1743 /* TODO: Userspace tracing */
1744 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1751 case LTTNG_ENABLE_EVENT
:
1754 struct ltt_kernel_channel
*kchan
;
1755 struct ltt_kernel_event
*kevent
;
1756 struct lttng_channel
*chan
;
1758 /* Setup lttng message with no payload */
1759 ret
= setup_lttng_msg(cmd_ctx
, 0);
1764 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1766 switch (cmd_ctx
->lsm
->domain
.type
) {
1767 case LTTNG_DOMAIN_KERNEL
:
1768 kchan
= get_kernel_channel_by_name(channel_name
,
1769 cmd_ctx
->session
->kernel_session
);
1770 if (kchan
== NULL
) {
1771 DBG("Channel not found. Creating channel %s", channel_name
);
1773 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
1775 ret
= LTTCOMM_FATAL
;
1779 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1780 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
1782 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1785 kchan
= get_kernel_channel_by_name(channel_name
,
1786 cmd_ctx
->session
->kernel_session
);
1787 if (kchan
== NULL
) {
1788 ERR("Channel %s not found after creation. Internal error, giving up.",
1790 ret
= LTTCOMM_FATAL
;
1795 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
1796 if (kevent
== NULL
) {
1797 DBG("Creating kernel event %s for channel %s.",
1798 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
1799 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
1801 DBG("Enabling kernel event %s for channel %s.",
1802 kevent
->event
->name
, channel_name
);
1803 ret
= kernel_enable_event(kevent
);
1804 if (ret
== -EEXIST
) {
1805 ret
= LTTCOMM_KERN_EVENT_EXIST
;
1811 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1815 kernel_wait_quiescent(kernel_tracer_fd
);
1818 /* TODO: Userspace tracing */
1819 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1825 case LTTNG_ENABLE_ALL_EVENT
:
1829 struct ltt_kernel_channel
*kchan
;
1830 struct ltt_kernel_event
*kevent
;
1831 struct lttng_event
*event_list
;
1832 struct lttng_channel
*chan
;
1834 /* Setup lttng message with no payload */
1835 ret
= setup_lttng_msg(cmd_ctx
, 0);
1840 DBG("Enabling all kernel event");
1842 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1844 switch (cmd_ctx
->lsm
->domain
.type
) {
1845 case LTTNG_DOMAIN_KERNEL
:
1846 kchan
= get_kernel_channel_by_name(channel_name
,
1847 cmd_ctx
->session
->kernel_session
);
1848 if (kchan
== NULL
) {
1849 DBG("Channel not found. Creating channel %s", channel_name
);
1851 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
1853 ret
= LTTCOMM_FATAL
;
1857 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1858 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
1860 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1863 kchan
= get_kernel_channel_by_name(channel_name
,
1864 cmd_ctx
->session
->kernel_session
);
1865 if (kchan
== NULL
) {
1866 ERR("Channel %s not found after creation. Internal error, giving up.",
1868 ret
= LTTCOMM_FATAL
;
1873 /* For each event in the kernel session */
1874 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1875 DBG("Enabling kernel event %s for channel %s.",
1876 kevent
->event
->name
, channel_name
);
1877 ret
= kernel_enable_event(kevent
);
1883 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1885 ret
= LTTCOMM_KERN_LIST_FAIL
;
1889 for (i
= 0; i
< size
; i
++) {
1890 kevent
= get_kernel_event_by_name(event_list
[i
].name
, kchan
);
1891 if (kevent
== NULL
) {
1892 /* Default event type for enable all */
1893 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1894 /* Enable each single tracepoint event */
1895 ret
= kernel_create_event(&event_list
[i
], kchan
);
1897 /* Ignore error here and continue */
1904 /* Quiescent wait after event enable */
1905 kernel_wait_quiescent(kernel_tracer_fd
);
1908 /* TODO: Userspace tracing */
1909 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1916 case LTTNG_LIST_TRACEPOINTS
:
1918 struct lttng_event
*events
;
1919 ssize_t nb_events
= 0;
1921 switch (cmd_ctx
->lsm
->domain
.type
) {
1922 case LTTNG_DOMAIN_KERNEL
:
1923 DBG("Listing kernel events");
1924 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
1925 if (nb_events
< 0) {
1926 ret
= LTTCOMM_KERN_LIST_FAIL
;
1931 /* TODO: Userspace listing */
1932 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1937 * Setup lttng message with payload size set to the event list size in
1938 * bytes and then copy list into the llm payload.
1940 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
1946 /* Copy event list into message payload */
1947 memcpy(cmd_ctx
->llm
->payload
, events
,
1948 sizeof(struct lttng_event
) * nb_events
);
1955 case LTTNG_START_TRACE
:
1957 struct ltt_kernel_channel
*chan
;
1959 /* Setup lttng message with no payload */
1960 ret
= setup_lttng_msg(cmd_ctx
, 0);
1965 /* Kernel tracing */
1966 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1967 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1968 DBG("Open kernel metadata");
1969 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1970 cmd_ctx
->session
->kernel_session
->trace_path
);
1972 ret
= LTTCOMM_KERN_META_FAIL
;
1977 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1978 DBG("Opening kernel metadata stream");
1979 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1980 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1982 ERR("Kernel create metadata stream failed");
1983 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1989 /* For each channel */
1990 cds_list_for_each_entry(chan
,
1991 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1992 if (chan
->stream_count
== 0) {
1993 ret
= kernel_open_channel_stream(chan
);
1995 ERR("Kernel create channel stream failed");
1996 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1999 /* Update the stream global counter */
2000 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2004 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2006 ret
= LTTCOMM_KERN_START_FAIL
;
2010 DBG("Start kernel tracing");
2011 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2013 ERR("Kernel start session failed");
2014 ret
= LTTCOMM_KERN_START_FAIL
;
2018 /* Quiescent wait after starting trace */
2019 kernel_wait_quiescent(kernel_tracer_fd
);
2022 /* TODO: Start all UST traces */
2027 case LTTNG_STOP_TRACE
:
2029 struct ltt_kernel_channel
*chan
;
2030 /* Setup lttng message with no payload */
2031 ret
= setup_lttng_msg(cmd_ctx
, 0);
2037 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2038 DBG("Stop kernel tracing");
2040 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2042 ERR("Kernel metadata flush failed");
2045 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2046 ret
= kernel_flush_buffer(chan
);
2048 ERR("Kernel flush buffer error");
2052 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2054 ERR("Kernel stop session failed");
2055 ret
= LTTCOMM_KERN_STOP_FAIL
;
2059 /* Quiescent wait after stopping trace */
2060 kernel_wait_quiescent(kernel_tracer_fd
);
2063 /* TODO : User-space tracer */
2068 case LTTNG_CREATE_SESSION
:
2070 /* Setup lttng message with no payload */
2071 ret
= setup_lttng_msg(cmd_ctx
, 0);
2076 tracepoint(create_session_start
);
2077 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2078 tracepoint(create_session_end
);
2080 if (ret
== -EEXIST
) {
2081 ret
= LTTCOMM_EXIST_SESS
;
2083 ret
= LTTCOMM_FATAL
;
2091 case LTTNG_DESTROY_SESSION
:
2093 /* Setup lttng message with no payload */
2094 ret
= setup_lttng_msg(cmd_ctx
, 0);
2099 /* Clean kernel session teardown */
2100 teardown_kernel_session(cmd_ctx
->session
);
2102 tracepoint(destroy_session_start
);
2103 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2104 tracepoint(destroy_session_end
);
2106 ret
= LTTCOMM_FATAL
;
2111 * Must notify the kernel thread here to update it's pollfd in order to
2112 * remove the channel(s)' fd just destroyed.
2114 ret
= notify_kernel_pollfd();
2116 ret
= LTTCOMM_FATAL
;
2123 case LTTNG_LIST_DOMAINS
:
2127 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2131 nb_dom
+= cmd_ctx
->session
->ust_trace_count
;
2133 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2138 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2139 LTTNG_DOMAIN_KERNEL
;
2141 /* TODO: User-space tracer domain support */
2145 case LTTNG_LIST_CHANNELS
:
2148 * TODO: Only kernel channels are listed here. UST listing
2149 * is needed on lttng-ust 2.0 release.
2152 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2153 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2156 ret
= setup_lttng_msg(cmd_ctx
,
2157 sizeof(struct lttng_channel
) * nb_chan
);
2162 list_lttng_channels(cmd_ctx
->session
,
2163 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2168 case LTTNG_LIST_EVENTS
:
2171 * TODO: Only kernel events are listed here. UST listing
2172 * is needed on lttng-ust 2.0 release.
2174 size_t nb_event
= 0;
2175 struct ltt_kernel_channel
*kchan
= NULL
;
2177 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2178 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2179 cmd_ctx
->session
->kernel_session
);
2180 if (kchan
== NULL
) {
2181 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2184 nb_event
+= kchan
->event_count
;
2187 ret
= setup_lttng_msg(cmd_ctx
,
2188 sizeof(struct lttng_event
) * nb_event
);
2193 DBG("Listing events (%zu events)", nb_event
);
2195 list_lttng_events(kchan
,
2196 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2201 case LTTNG_LIST_SESSIONS
:
2203 lock_session_list();
2205 if (session_list_ptr
->count
== 0) {
2206 ret
= LTTCOMM_NO_SESSION
;
2207 unlock_session_list();
2211 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2212 session_list_ptr
->count
);
2214 unlock_session_list();
2218 /* Filled the session array */
2219 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2221 unlock_session_list();
2226 case LTTNG_CALIBRATE
:
2228 /* Setup lttng message with no payload */
2229 ret
= setup_lttng_msg(cmd_ctx
, 0);
2234 switch (cmd_ctx
->lsm
->domain
.type
) {
2235 case LTTNG_DOMAIN_KERNEL
:
2237 struct lttng_kernel_calibrate kcalibrate
;
2239 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2240 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2242 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2248 /* TODO: Userspace tracing */
2249 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2255 case LTTNG_REGISTER_CONSUMER
:
2259 /* Setup lttng message with no payload */
2260 ret
= setup_lttng_msg(cmd_ctx
, 0);
2265 switch (cmd_ctx
->lsm
->domain
.type
) {
2266 case LTTNG_DOMAIN_KERNEL
:
2268 /* Can't register a consumer if there is already one */
2269 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2270 ret
= LTTCOMM_CONNECT_FAIL
;
2274 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2276 ret
= LTTCOMM_CONNECT_FAIL
;
2280 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2284 /* TODO: Userspace tracing */
2285 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2294 /* Undefined command */
2295 ret
= setup_lttng_msg(cmd_ctx
, 0);
2304 /* Set return code */
2305 cmd_ctx
->llm
->ret_code
= ret
;
2307 if (cmd_ctx
->session
) {
2308 unlock_session(cmd_ctx
->session
);
2314 if (cmd_ctx
->llm
== NULL
) {
2315 DBG("Missing llm structure. Allocating one.");
2316 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2320 /* Notify client of error */
2321 cmd_ctx
->llm
->ret_code
= ret
;
2324 if (cmd_ctx
->session
) {
2325 unlock_session(cmd_ctx
->session
);
2331 * This thread manage all clients request using the unix client socket for
2334 static void *thread_manage_clients(void *data
)
2337 struct command_ctx
*cmd_ctx
= NULL
;
2338 struct pollfd pollfd
[2];
2340 tracepoint(sessiond_th_cli_start
);
2342 DBG("[thread] Manage client started");
2344 ret
= lttcomm_listen_unix_sock(client_sock
);
2349 /* First fd is always the quit pipe */
2350 pollfd
[0].fd
= thread_quit_pipe
[0];
2353 pollfd
[1].fd
= client_sock
;
2354 pollfd
[1].events
= POLLIN
;
2356 /* Notify parent pid that we are ready
2357 * to accept command for client side.
2359 if (opt_sig_parent
) {
2360 kill(ppid
, SIGCHLD
);
2364 DBG("Accepting client command ...");
2366 tracepoint(sessiond_th_cli_poll
);
2368 /* Inifinite blocking call, waiting for transmission */
2369 ret
= poll(pollfd
, 2, -1);
2371 perror("poll client thread");
2375 /* Thread quit pipe has been closed. Killing thread. */
2376 if (pollfd
[0].revents
== POLLNVAL
) {
2378 } else if (pollfd
[1].revents
== POLLERR
) {
2379 ERR("Client socket poll error");
2383 sock
= lttcomm_accept_unix_sock(client_sock
);
2388 /* Allocate context command to process the client request */
2389 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2391 /* Allocate data buffer for reception */
2392 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2393 cmd_ctx
->llm
= NULL
;
2394 cmd_ctx
->session
= NULL
;
2397 * Data is received from the lttng client. The struct
2398 * lttcomm_session_msg (lsm) contains the command and data request of
2401 DBG("Receiving data from client ...");
2402 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2407 // TODO: Validate cmd_ctx including sanity check for security purpose.
2410 * This function dispatch the work to the kernel or userspace tracer
2411 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2412 * informations for the client. The command context struct contains
2413 * everything this function may needs.
2415 ret
= process_client_msg(cmd_ctx
);
2417 /* TODO: Inform client somehow of the fatal error. At this point,
2418 * ret < 0 means that a malloc failed (ENOMEM). */
2419 /* Error detected but still accept command */
2420 clean_command_ctx(&cmd_ctx
);
2424 DBG("Sending response (size: %d, retcode: %d)",
2425 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2426 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2428 ERR("Failed to send data back to client");
2431 clean_command_ctx(&cmd_ctx
);
2433 /* End of transmission */
2438 DBG("Client thread dying");
2446 unlink(client_unix_sock_path
);
2448 clean_command_ctx(&cmd_ctx
);
2454 * usage function on stderr
2456 static void usage(void)
2458 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2459 fprintf(stderr
, " -h, --help Display this usage.\n");
2460 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2461 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2462 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2463 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2464 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2465 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2466 fprintf(stderr
, " -V, --version Show version number.\n");
2467 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2468 fprintf(stderr
, " -q, --quiet No output at all.\n");
2469 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2470 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2474 * daemon argument parsing
2476 static int parse_args(int argc
, char **argv
)
2480 static struct option long_options
[] = {
2481 { "client-sock", 1, 0, 'c' },
2482 { "apps-sock", 1, 0, 'a' },
2483 { "kconsumerd-cmd-sock", 1, 0, 0 },
2484 { "kconsumerd-err-sock", 1, 0, 0 },
2485 { "daemonize", 0, 0, 'd' },
2486 { "sig-parent", 0, 0, 'S' },
2487 { "help", 0, 0, 'h' },
2488 { "group", 1, 0, 'g' },
2489 { "version", 0, 0, 'V' },
2490 { "quiet", 0, 0, 'q' },
2491 { "verbose", 0, 0, 'v' },
2492 { "verbose-kconsumerd", 0, 0, 'Z' },
2497 int option_index
= 0;
2498 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2505 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2507 fprintf(stderr
, " with arg %s\n", optarg
);
2511 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2514 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2520 opt_tracing_group
= strdup(optarg
);
2526 fprintf(stdout
, "%s\n", VERSION
);
2532 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2535 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2541 /* Verbose level can increase using multiple -v */
2545 opt_verbose_kconsumerd
+= 1;
2548 /* Unknown option or other error.
2549 * Error is printed by getopt, just return */
2558 * Creates the two needed socket by the daemon.
2559 * apps_sock - The communication socket for all UST apps.
2560 * client_sock - The communication of the cli tool (lttng).
2562 static int init_daemon_socket(void)
2567 old_umask
= umask(0);
2569 /* Create client tool unix socket */
2570 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2571 if (client_sock
< 0) {
2572 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2577 /* File permission MUST be 660 */
2578 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2580 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2585 /* Create the application unix socket */
2586 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2587 if (apps_sock
< 0) {
2588 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2593 /* File permission MUST be 666 */
2594 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2596 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2607 * Check if the global socket is available, and if a daemon is answering
2608 * at the other side. If yes, error is returned.
2610 static int check_existing_daemon(void)
2612 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2613 access(apps_unix_sock_path
, F_OK
) < 0)
2615 /* Is there anybody out there ? */
2616 if (lttng_session_daemon_alive())
2623 * Set the tracing group gid onto the client socket.
2625 * Race window between mkdir and chown is OK because we are going from more
2626 * permissive (root.root) to les permissive (root.tracing).
2628 static int set_permissions(void)
2633 gid
= allowed_group();
2636 WARN("No tracing group detected");
2639 ERR("Missing tracing group. Aborting execution.");
2645 /* Set lttng run dir */
2646 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2648 ERR("Unable to set group on " LTTNG_RUNDIR
);
2652 /* lttng client socket path */
2653 ret
= chown(client_unix_sock_path
, 0, gid
);
2655 ERR("Unable to set group on %s", client_unix_sock_path
);
2659 /* kconsumerd error socket path */
2660 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2662 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2666 DBG("All permissions are set");
2673 * Create the pipe used to wake up the kernel thread.
2675 static int create_kernel_poll_pipe(void)
2677 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2681 * Create the lttng run directory needed for all global sockets and pipe.
2683 static int create_lttng_rundir(void)
2687 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2689 if (errno
!= EEXIST
) {
2690 ERR("Unable to create " LTTNG_RUNDIR
);
2702 * Setup sockets and directory needed by the kconsumerd communication with the
2705 static int set_kconsumerd_sockets(void)
2709 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2710 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2713 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2714 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2717 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2719 if (errno
!= EEXIST
) {
2720 ERR("Failed to create " KCONSUMERD_PATH
);
2726 /* Create the kconsumerd error unix socket */
2727 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2728 if (kconsumerd_err_sock
< 0) {
2729 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2734 /* File permission MUST be 660 */
2735 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2737 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2747 * Signal handler for the daemon
2749 * Simply stop all worker threads, leaving main() return gracefully
2750 * after joining all threads and calling cleanup().
2752 static void sighandler(int sig
)
2756 DBG("SIGPIPE catched");
2759 DBG("SIGINT catched");
2763 DBG("SIGTERM catched");
2772 * Setup signal handler for :
2773 * SIGINT, SIGTERM, SIGPIPE
2775 static int set_signal_handler(void)
2778 struct sigaction sa
;
2781 if ((ret
= sigemptyset(&sigset
)) < 0) {
2782 perror("sigemptyset");
2786 sa
.sa_handler
= sighandler
;
2787 sa
.sa_mask
= sigset
;
2789 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2790 perror("sigaction");
2794 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2795 perror("sigaction");
2799 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2800 perror("sigaction");
2804 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2810 * Set open files limit to unlimited. This daemon can open a large number of
2811 * file descriptors in order to consumer multiple kernel traces.
2813 static void set_ulimit(void)
2818 /* The kernel does not allowed an infinite limit for open files */
2819 lim
.rlim_cur
= 65535;
2820 lim
.rlim_max
= 65535;
2822 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2824 perror("failed to set open files limit");
2831 int main(int argc
, char **argv
)
2835 const char *home_path
;
2837 tracepoint(sessiond_boot_start
);
2839 /* Create thread quit pipe */
2840 if ((ret
= init_thread_quit_pipe()) < 0) {
2844 /* Parse arguments */
2846 if ((ret
= parse_args(argc
, argv
) < 0)) {
2859 /* Check if daemon is UID = 0 */
2860 is_root
= !getuid();
2863 ret
= create_lttng_rundir();
2868 if (strlen(apps_unix_sock_path
) == 0) {
2869 snprintf(apps_unix_sock_path
, PATH_MAX
,
2870 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2873 if (strlen(client_unix_sock_path
) == 0) {
2874 snprintf(client_unix_sock_path
, PATH_MAX
,
2875 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2878 home_path
= get_home_dir();
2879 if (home_path
== NULL
) {
2880 /* TODO: Add --socket PATH option */
2881 ERR("Can't get HOME directory for sockets creation.");
2886 if (strlen(apps_unix_sock_path
) == 0) {
2887 snprintf(apps_unix_sock_path
, PATH_MAX
,
2888 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2891 /* Set the cli tool unix socket path */
2892 if (strlen(client_unix_sock_path
) == 0) {
2893 snprintf(client_unix_sock_path
, PATH_MAX
,
2894 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2898 DBG("Client socket path %s", client_unix_sock_path
);
2899 DBG("Application socket path %s", apps_unix_sock_path
);
2902 * See if daemon already exist.
2904 if ((ret
= check_existing_daemon()) < 0) {
2905 ERR("Already running daemon.\n");
2907 * We do not goto exit because we must not cleanup()
2908 * because a daemon is already running.
2913 /* After this point, we can safely call cleanup() so goto error is used */
2916 * These actions must be executed as root. We do that *after* setting up
2917 * the sockets path because we MUST make the check for another daemon using
2918 * those paths *before* trying to set the kernel consumer sockets and init
2922 ret
= set_kconsumerd_sockets();
2927 /* Setup kernel tracer */
2928 init_kernel_tracer();
2930 /* Set ulimit for open files */
2934 if ((ret
= set_signal_handler()) < 0) {
2938 /* Setup the needed unix socket */
2939 if ((ret
= init_daemon_socket()) < 0) {
2943 /* Set credentials to socket */
2944 if (is_root
&& ((ret
= set_permissions()) < 0)) {
2948 /* Get parent pid if -S, --sig-parent is specified. */
2949 if (opt_sig_parent
) {
2953 /* Setup the kernel pipe for waking up the kernel thread */
2954 if ((ret
= create_kernel_poll_pipe()) < 0) {
2959 * Get session list pointer. This pointer MUST NOT be free().
2960 * This list is statically declared in session.c
2962 session_list_ptr
= get_session_list();
2964 /* Create thread to manage the client socket */
2965 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2967 perror("pthread_create");
2971 /* Create thread to manage application socket */
2972 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2974 perror("pthread_create");
2978 /* Create kernel thread to manage kernel event */
2979 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2981 perror("pthread_create");
2985 tracepoint(sessiond_boot_end
);
2987 ret
= pthread_join(kernel_thread
, &status
);
2989 perror("pthread_join");
2990 goto error
; /* join error, exit without cleanup */
2994 ret
= pthread_join(apps_thread
, &status
);
2996 perror("pthread_join");
2997 goto error
; /* join error, exit without cleanup */
3001 ret
= pthread_join(client_thread
, &status
);
3003 perror("pthread_join");
3004 goto error
; /* join error, exit without cleanup */
3007 ret
= join_kconsumerd_thread();
3009 perror("join_kconsumerd");
3010 goto error
; /* join error, exit without cleanup */
3016 * cleanup() is called when no other thread is running.