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>
33 #include <sys/mount.h>
35 #include <sys/socket.h>
37 #include <sys/types.h>
39 #include <sys/resource.h>
42 #include <urcu/list.h> /* URCU list library (-lurcu) */
43 #include <lttng/lttng.h>
44 #include <lttng/lttng-kconsumerd.h>
45 #include <lttng-sessiond-comm.h>
48 #include "ltt-sessiond.h"
50 #include "kernel-ctl.h"
53 #include "traceable-app.h"
54 #include "ltt-kconsumerd.h"
57 #include "benchmark.h"
60 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
61 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
62 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
63 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
66 int opt_verbose
; /* Not static for lttngerr.h */
67 int opt_verbose_kconsumerd
; /* Not static for lttngerr.h */
68 int opt_quiet
; /* Not static for lttngerr.h */
71 const char *opt_tracing_group
;
72 static int opt_sig_parent
;
73 static int opt_daemon
;
74 static int is_root
; /* Set to 1 if the daemon is running as root */
75 static pid_t ppid
; /* Parent PID for --sig-parent option */
76 static pid_t kconsumerd_pid
;
77 static struct pollfd
*kernel_pollfd
;
79 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
80 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
81 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
82 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
85 static int client_sock
;
87 static int kconsumerd_err_sock
;
88 static int kconsumerd_cmd_sock
;
89 static int kernel_tracer_fd
;
90 static int kernel_poll_pipe
[2];
93 * Quit pipe for all threads. This permits a single cancellation point
94 * for all threads when receiving an event on the pipe.
96 static int thread_quit_pipe
[2];
98 /* Pthread, Mutexes and Semaphores */
99 static pthread_t kconsumerd_thread
;
100 static pthread_t apps_thread
;
101 static pthread_t client_thread
;
102 static pthread_t kernel_thread
;
103 static sem_t kconsumerd_sem
;
105 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
107 static int modprobe_remove_kernel_modules(void);
110 * Pointer initialized before thread creation.
112 * This points to the tracing session list containing the session count and a
113 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
114 * MUST NOT be taken if you call a public function in session.c.
116 * The lock is nested inside the structure: session_list_ptr->lock. Please use
117 * lock_session_list and unlock_session_list for lock acquisition.
119 static struct ltt_session_list
*session_list_ptr
;
121 static gid_t
allowed_group(void)
125 if (opt_tracing_group
) {
126 grp
= getgrnam(opt_tracing_group
);
128 grp
= getgrnam(default_tracing_group
);
140 * Return -1 on error or 0 if all pipes are created.
142 static int init_thread_quit_pipe(void)
146 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
148 perror("thread quit pipe");
157 * Complete teardown of a kernel session. This free all data structure related
158 * to a kernel session and update counter.
160 static void teardown_kernel_session(struct ltt_session
*session
)
162 if (session
->kernel_session
!= NULL
) {
163 DBG("Tearing down kernel session");
166 * If a custom kernel consumer was registered, close the socket before
167 * tearing down the complete kernel session structure
169 if (session
->kernel_session
->consumer_fd
!= kconsumerd_cmd_sock
) {
170 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
173 trace_destroy_kernel_session(session
->kernel_session
);
174 /* Extra precaution */
175 session
->kernel_session
= NULL
;
179 static void stop_threads(void)
181 /* Stopping all threads */
182 DBG("Terminating all threads");
183 close(thread_quit_pipe
[0]);
184 close(thread_quit_pipe
[1]);
190 static void cleanup(void)
194 struct ltt_session
*sess
, *stmp
;
199 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
200 "Matthew, BEET driven development works!%c[%dm",
201 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
204 DBG("Removing %s directory", LTTNG_RUNDIR
);
205 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
207 ERR("asprintf failed. Something is really wrong!");
210 /* Remove lttng run directory */
213 ERR("Unable to clean " LTTNG_RUNDIR
);
216 DBG("Cleaning up all session");
218 /* Destroy session list mutex */
219 if (session_list_ptr
!= NULL
) {
220 pthread_mutex_destroy(&session_list_ptr
->lock
);
222 /* Cleanup ALL session */
223 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
224 teardown_kernel_session(sess
);
225 // TODO complete session cleanup (including UST)
229 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
231 DBG("Closing kernel fd");
232 close(kernel_tracer_fd
);
234 DBG("Unloading kernel modules");
235 modprobe_remove_kernel_modules();
237 benchmark_print_boot_results();
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 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2078 if (ret
== -EEXIST
) {
2079 ret
= LTTCOMM_EXIST_SESS
;
2081 ret
= LTTCOMM_FATAL
;
2089 case LTTNG_DESTROY_SESSION
:
2091 /* Setup lttng message with no payload */
2092 ret
= setup_lttng_msg(cmd_ctx
, 0);
2097 /* Clean kernel session teardown */
2098 teardown_kernel_session(cmd_ctx
->session
);
2100 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2102 ret
= LTTCOMM_FATAL
;
2107 * Must notify the kernel thread here to update it's pollfd in order to
2108 * remove the channel(s)' fd just destroyed.
2110 ret
= notify_kernel_pollfd();
2112 ret
= LTTCOMM_FATAL
;
2119 case LTTNG_LIST_DOMAINS
:
2123 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2127 nb_dom
+= cmd_ctx
->session
->ust_trace_count
;
2129 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2134 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2135 LTTNG_DOMAIN_KERNEL
;
2137 /* TODO: User-space tracer domain support */
2141 case LTTNG_LIST_CHANNELS
:
2144 * TODO: Only kernel channels are listed here. UST listing
2145 * is needed on lttng-ust 2.0 release.
2148 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2149 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2152 ret
= setup_lttng_msg(cmd_ctx
,
2153 sizeof(struct lttng_channel
) * nb_chan
);
2158 list_lttng_channels(cmd_ctx
->session
,
2159 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2164 case LTTNG_LIST_EVENTS
:
2167 * TODO: Only kernel events are listed here. UST listing
2168 * is needed on lttng-ust 2.0 release.
2170 size_t nb_event
= 0;
2171 struct ltt_kernel_channel
*kchan
= NULL
;
2173 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2174 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2175 cmd_ctx
->session
->kernel_session
);
2176 if (kchan
== NULL
) {
2177 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2180 nb_event
+= kchan
->event_count
;
2183 ret
= setup_lttng_msg(cmd_ctx
,
2184 sizeof(struct lttng_event
) * nb_event
);
2189 DBG("Listing events (%zu events)", nb_event
);
2191 list_lttng_events(kchan
,
2192 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2197 case LTTNG_LIST_SESSIONS
:
2199 lock_session_list();
2201 if (session_list_ptr
->count
== 0) {
2202 ret
= LTTCOMM_NO_SESSION
;
2203 unlock_session_list();
2207 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2208 session_list_ptr
->count
);
2210 unlock_session_list();
2214 /* Filled the session array */
2215 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2217 unlock_session_list();
2222 case LTTNG_CALIBRATE
:
2224 /* Setup lttng message with no payload */
2225 ret
= setup_lttng_msg(cmd_ctx
, 0);
2230 switch (cmd_ctx
->lsm
->domain
.type
) {
2231 case LTTNG_DOMAIN_KERNEL
:
2233 struct lttng_kernel_calibrate kcalibrate
;
2235 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2236 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2238 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2244 /* TODO: Userspace tracing */
2245 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2251 case LTTNG_REGISTER_CONSUMER
:
2255 /* Setup lttng message with no payload */
2256 ret
= setup_lttng_msg(cmd_ctx
, 0);
2261 switch (cmd_ctx
->lsm
->domain
.type
) {
2262 case LTTNG_DOMAIN_KERNEL
:
2264 /* Can't register a consumer if there is already one */
2265 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2266 ret
= LTTCOMM_CONNECT_FAIL
;
2270 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2272 ret
= LTTCOMM_CONNECT_FAIL
;
2276 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2280 /* TODO: Userspace tracing */
2281 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2290 /* Undefined command */
2291 ret
= setup_lttng_msg(cmd_ctx
, 0);
2300 /* Set return code */
2301 cmd_ctx
->llm
->ret_code
= ret
;
2303 if (cmd_ctx
->session
) {
2304 unlock_session(cmd_ctx
->session
);
2310 if (cmd_ctx
->llm
== NULL
) {
2311 DBG("Missing llm structure. Allocating one.");
2312 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2316 /* Notify client of error */
2317 cmd_ctx
->llm
->ret_code
= ret
;
2320 if (cmd_ctx
->session
) {
2321 unlock_session(cmd_ctx
->session
);
2327 * This thread manage all clients request using the unix client socket for
2330 static void *thread_manage_clients(void *data
)
2333 struct command_ctx
*cmd_ctx
= NULL
;
2334 struct pollfd pollfd
[2];
2336 tracepoint(sessiond_th_cli_start
);
2338 DBG("[thread] Manage client started");
2340 ret
= lttcomm_listen_unix_sock(client_sock
);
2345 /* First fd is always the quit pipe */
2346 pollfd
[0].fd
= thread_quit_pipe
[0];
2349 pollfd
[1].fd
= client_sock
;
2350 pollfd
[1].events
= POLLIN
;
2352 /* Notify parent pid that we are ready
2353 * to accept command for client side.
2355 if (opt_sig_parent
) {
2356 kill(ppid
, SIGCHLD
);
2360 DBG("Accepting client command ...");
2362 tracepoint(sessiond_th_cli_poll
);
2364 /* Inifinite blocking call, waiting for transmission */
2365 ret
= poll(pollfd
, 2, -1);
2367 perror("poll client thread");
2371 /* Thread quit pipe has been closed. Killing thread. */
2372 if (pollfd
[0].revents
== POLLNVAL
) {
2374 } else if (pollfd
[1].revents
== POLLERR
) {
2375 ERR("Client socket poll error");
2379 sock
= lttcomm_accept_unix_sock(client_sock
);
2384 /* Allocate context command to process the client request */
2385 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2387 /* Allocate data buffer for reception */
2388 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2389 cmd_ctx
->llm
= NULL
;
2390 cmd_ctx
->session
= NULL
;
2393 * Data is received from the lttng client. The struct
2394 * lttcomm_session_msg (lsm) contains the command and data request of
2397 DBG("Receiving data from client ...");
2398 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2403 // TODO: Validate cmd_ctx including sanity check for security purpose.
2406 * This function dispatch the work to the kernel or userspace tracer
2407 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2408 * informations for the client. The command context struct contains
2409 * everything this function may needs.
2411 ret
= process_client_msg(cmd_ctx
);
2413 /* TODO: Inform client somehow of the fatal error. At this point,
2414 * ret < 0 means that a malloc failed (ENOMEM). */
2415 /* Error detected but still accept command */
2416 clean_command_ctx(&cmd_ctx
);
2420 DBG("Sending response (size: %d, retcode: %d)",
2421 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2422 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2424 ERR("Failed to send data back to client");
2427 clean_command_ctx(&cmd_ctx
);
2429 /* End of transmission */
2434 DBG("Client thread dying");
2442 unlink(client_unix_sock_path
);
2444 clean_command_ctx(&cmd_ctx
);
2450 * usage function on stderr
2452 static void usage(void)
2454 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2455 fprintf(stderr
, " -h, --help Display this usage.\n");
2456 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2457 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2458 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2459 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2460 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2461 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2462 fprintf(stderr
, " -V, --version Show version number.\n");
2463 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2464 fprintf(stderr
, " -q, --quiet No output at all.\n");
2465 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2466 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2470 * daemon argument parsing
2472 static int parse_args(int argc
, char **argv
)
2476 static struct option long_options
[] = {
2477 { "client-sock", 1, 0, 'c' },
2478 { "apps-sock", 1, 0, 'a' },
2479 { "kconsumerd-cmd-sock", 1, 0, 0 },
2480 { "kconsumerd-err-sock", 1, 0, 0 },
2481 { "daemonize", 0, 0, 'd' },
2482 { "sig-parent", 0, 0, 'S' },
2483 { "help", 0, 0, 'h' },
2484 { "group", 1, 0, 'g' },
2485 { "version", 0, 0, 'V' },
2486 { "quiet", 0, 0, 'q' },
2487 { "verbose", 0, 0, 'v' },
2488 { "verbose-kconsumerd", 0, 0, 'Z' },
2493 int option_index
= 0;
2494 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2501 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2503 fprintf(stderr
, " with arg %s\n", optarg
);
2507 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2510 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2516 opt_tracing_group
= strdup(optarg
);
2522 fprintf(stdout
, "%s\n", VERSION
);
2528 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2531 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2537 /* Verbose level can increase using multiple -v */
2541 opt_verbose_kconsumerd
+= 1;
2544 /* Unknown option or other error.
2545 * Error is printed by getopt, just return */
2554 * Creates the two needed socket by the daemon.
2555 * apps_sock - The communication socket for all UST apps.
2556 * client_sock - The communication of the cli tool (lttng).
2558 static int init_daemon_socket(void)
2563 old_umask
= umask(0);
2565 /* Create client tool unix socket */
2566 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2567 if (client_sock
< 0) {
2568 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2573 /* File permission MUST be 660 */
2574 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2576 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2581 /* Create the application unix socket */
2582 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2583 if (apps_sock
< 0) {
2584 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2589 /* File permission MUST be 666 */
2590 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2592 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2603 * Check if the global socket is available, and if a daemon is answering
2604 * at the other side. If yes, error is returned.
2606 static int check_existing_daemon(void)
2608 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2609 access(apps_unix_sock_path
, F_OK
) < 0)
2611 /* Is there anybody out there ? */
2612 if (lttng_session_daemon_alive())
2619 * Set the tracing group gid onto the client socket.
2621 * Race window between mkdir and chown is OK because we are going from more
2622 * permissive (root.root) to les permissive (root.tracing).
2624 static int set_permissions(void)
2629 gid
= allowed_group();
2632 WARN("No tracing group detected");
2635 ERR("Missing tracing group. Aborting execution.");
2641 /* Set lttng run dir */
2642 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2644 ERR("Unable to set group on " LTTNG_RUNDIR
);
2648 /* lttng client socket path */
2649 ret
= chown(client_unix_sock_path
, 0, gid
);
2651 ERR("Unable to set group on %s", client_unix_sock_path
);
2655 /* kconsumerd error socket path */
2656 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2658 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2662 DBG("All permissions are set");
2669 * Create the pipe used to wake up the kernel thread.
2671 static int create_kernel_poll_pipe(void)
2673 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2677 * Create the lttng run directory needed for all global sockets and pipe.
2679 static int create_lttng_rundir(void)
2683 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2685 if (errno
!= EEXIST
) {
2686 ERR("Unable to create " LTTNG_RUNDIR
);
2698 * Setup sockets and directory needed by the kconsumerd communication with the
2701 static int set_kconsumerd_sockets(void)
2705 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2706 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2709 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2710 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2713 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2715 if (errno
!= EEXIST
) {
2716 ERR("Failed to create " KCONSUMERD_PATH
);
2722 /* Create the kconsumerd error unix socket */
2723 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2724 if (kconsumerd_err_sock
< 0) {
2725 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2730 /* File permission MUST be 660 */
2731 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2733 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2743 * Signal handler for the daemon
2745 * Simply stop all worker threads, leaving main() return gracefully
2746 * after joining all threads and calling cleanup().
2748 static void sighandler(int sig
)
2752 DBG("SIGPIPE catched");
2755 DBG("SIGINT catched");
2759 DBG("SIGTERM catched");
2768 * Setup signal handler for :
2769 * SIGINT, SIGTERM, SIGPIPE
2771 static int set_signal_handler(void)
2774 struct sigaction sa
;
2777 if ((ret
= sigemptyset(&sigset
)) < 0) {
2778 perror("sigemptyset");
2782 sa
.sa_handler
= sighandler
;
2783 sa
.sa_mask
= sigset
;
2785 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2786 perror("sigaction");
2790 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2791 perror("sigaction");
2795 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2796 perror("sigaction");
2800 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2806 * Set open files limit to unlimited. This daemon can open a large number of
2807 * file descriptors in order to consumer multiple kernel traces.
2809 static void set_ulimit(void)
2814 /* The kernel does not allowed an infinite limit for open files */
2815 lim
.rlim_cur
= 65535;
2816 lim
.rlim_max
= 65535;
2818 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2820 perror("failed to set open files limit");
2827 int main(int argc
, char **argv
)
2831 const char *home_path
;
2833 tracepoint(sessiond_boot_start
);
2835 /* Create thread quit pipe */
2836 if ((ret
= init_thread_quit_pipe()) < 0) {
2840 /* Parse arguments */
2842 if ((ret
= parse_args(argc
, argv
) < 0)) {
2855 /* Check if daemon is UID = 0 */
2856 is_root
= !getuid();
2859 ret
= create_lttng_rundir();
2864 if (strlen(apps_unix_sock_path
) == 0) {
2865 snprintf(apps_unix_sock_path
, PATH_MAX
,
2866 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2869 if (strlen(client_unix_sock_path
) == 0) {
2870 snprintf(client_unix_sock_path
, PATH_MAX
,
2871 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2874 home_path
= get_home_dir();
2875 if (home_path
== NULL
) {
2876 /* TODO: Add --socket PATH option */
2877 ERR("Can't get HOME directory for sockets creation.");
2882 if (strlen(apps_unix_sock_path
) == 0) {
2883 snprintf(apps_unix_sock_path
, PATH_MAX
,
2884 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2887 /* Set the cli tool unix socket path */
2888 if (strlen(client_unix_sock_path
) == 0) {
2889 snprintf(client_unix_sock_path
, PATH_MAX
,
2890 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2894 DBG("Client socket path %s", client_unix_sock_path
);
2895 DBG("Application socket path %s", apps_unix_sock_path
);
2898 * See if daemon already exist.
2900 if ((ret
= check_existing_daemon()) < 0) {
2901 ERR("Already running daemon.\n");
2903 * We do not goto exit because we must not cleanup()
2904 * because a daemon is already running.
2909 /* After this point, we can safely call cleanup() so goto error is used */
2912 * These actions must be executed as root. We do that *after* setting up
2913 * the sockets path because we MUST make the check for another daemon using
2914 * those paths *before* trying to set the kernel consumer sockets and init
2918 ret
= set_kconsumerd_sockets();
2923 /* Setup kernel tracer */
2924 init_kernel_tracer();
2926 /* Set ulimit for open files */
2930 if ((ret
= set_signal_handler()) < 0) {
2934 /* Setup the needed unix socket */
2935 if ((ret
= init_daemon_socket()) < 0) {
2939 /* Set credentials to socket */
2940 if (is_root
&& ((ret
= set_permissions()) < 0)) {
2944 /* Get parent pid if -S, --sig-parent is specified. */
2945 if (opt_sig_parent
) {
2949 /* Setup the kernel pipe for waking up the kernel thread */
2950 if ((ret
= create_kernel_poll_pipe()) < 0) {
2955 * Get session list pointer. This pointer MUST NOT be free().
2956 * This list is statically declared in session.c
2958 session_list_ptr
= get_session_list();
2960 /* Create thread to manage the client socket */
2961 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2963 perror("pthread_create");
2967 /* Create thread to manage application socket */
2968 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2970 perror("pthread_create");
2974 /* Create kernel thread to manage kernel event */
2975 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2977 perror("pthread_create");
2981 tracepoint(sessiond_boot_end
);
2983 ret
= pthread_join(kernel_thread
, &status
);
2985 perror("pthread_join");
2986 goto error
; /* join error, exit without cleanup */
2990 ret
= pthread_join(apps_thread
, &status
);
2992 perror("pthread_join");
2993 goto error
; /* join error, exit without cleanup */
2997 ret
= pthread_join(client_thread
, &status
);
2999 perror("pthread_join");
3000 goto error
; /* join error, exit without cleanup */
3003 ret
= join_kconsumerd_thread();
3005 perror("join_kconsumerd");
3006 goto error
; /* join error, exit without cleanup */
3012 * cleanup() is called when no other thread is running.