2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <semaphore.h>
31 #include <sys/mount.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
37 #include <sys/resource.h>
40 #include <urcu/list.h> /* URCU list library (-lurcu) */
41 #include <lttng/lttng.h>
43 #include "liblttsessiondcomm.h"
44 #include "ltt-sessiond.h"
46 #include "kernel-ctl.h"
49 #include "traceable-app.h"
50 #include "lttng-kconsumerd.h"
51 #include "libustctl.h"
56 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
57 * with epoll on pipe and on other pipes/sockets for commands. Main
58 * simply waits on pthread join.
62 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
63 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
64 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
65 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
68 int opt_verbose
; /* Not static for lttngerr.h */
69 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
;
78 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
79 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
80 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
81 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
84 static int client_sock
;
86 static int kconsumerd_err_sock
;
87 static int kconsumerd_cmd_sock
;
88 static int kernel_tracer_fd
;
90 /* Pthread, Mutexes and Semaphores */
91 static pthread_t kconsumerd_thread
;
92 static pthread_t apps_thread
;
93 static pthread_t client_thread
;
94 static sem_t kconsumerd_sem
;
96 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
99 * teardown_kernel_session
101 * Complete teardown of a kernel session. This free all data structure
102 * related to a kernel session and update counter.
104 static void teardown_kernel_session(struct ltt_session
*session
)
106 if (session
->kernel_session
!= NULL
) {
107 DBG("Tearing down kernel session");
108 trace_destroy_kernel_session(session
->kernel_session
);
109 /* Extra precaution */
110 session
->kernel_session
= NULL
;
117 * Cleanup the daemon on exit
119 static void cleanup()
123 struct ltt_session
*sess
;
128 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
129 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
132 /* Stopping all threads */
133 DBG("Terminating all threads");
134 pthread_cancel(client_thread
);
135 pthread_cancel(apps_thread
);
136 if (kconsumerd_pid
!= 0) {
137 pthread_cancel(kconsumerd_thread
);
140 DBG("Unlinking all unix socket");
141 unlink(client_unix_sock_path
);
142 unlink(apps_unix_sock_path
);
143 unlink(kconsumerd_err_unix_sock_path
);
145 DBG("Removing %s directory", LTTNG_RUNDIR
);
146 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
148 ERR("asprintf failed. Something is really wrong!");
151 /* Remove lttng run directory */
154 ERR("Unable to clean " LTTNG_RUNDIR
);
157 DBG("Cleaning up all session");
158 /* Cleanup ALL session */
159 cds_list_for_each_entry(sess
, <t_session_list
.head
, list
) {
160 teardown_kernel_session(sess
);
161 // TODO complete session cleanup (including UST)
164 DBG("Closing kernel fd");
165 close(kernel_tracer_fd
);
171 * Send data on a unix socket using the liblttsessiondcomm API.
173 * Return lttcomm error code.
175 static int send_unix_sock(int sock
, void *buf
, size_t len
)
177 /* Check valid length */
182 return lttcomm_send_unix_sock(sock
, buf
, len
);
188 * Free memory of a command context structure.
190 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
192 DBG("Clean command context structure %p", cmd_ctx
);
206 * send_kconsumerd_fds
208 * Send all stream fds of the kernel session to the consumer.
210 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
214 struct ltt_kernel_stream
*stream
;
215 struct ltt_kernel_channel
*chan
;
216 struct lttcomm_kconsumerd_header lkh
;
217 struct lttcomm_kconsumerd_msg lkm
;
219 nb_fd
= session
->stream_count_global
;
222 lkh
.payload_size
= (nb_fd
+ 1) * sizeof(struct lttcomm_kconsumerd_msg
);
223 lkh
.cmd_type
= ADD_STREAM
;
225 DBG("Sending kconsumerd header");
227 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
229 perror("send kconsumerd header");
233 DBG("Sending metadata stream fd");
235 /* Send metadata stream fd first */
236 lkm
.fd
= session
->metadata_stream_fd
;
237 lkm
.state
= ACTIVE_FD
;
238 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
239 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
241 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
243 perror("send kconsumerd fd");
247 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
248 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
250 lkm
.state
= stream
->state
;
251 lkm
.max_sb_size
= chan
->channel
->attr
.subbuf_size
;
252 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
254 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
256 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
258 perror("send kconsumerd fd");
264 DBG("Kconsumerd fds sent");
275 * Create the trace output directory.
277 static int create_trace_dir(struct ltt_kernel_session
*session
)
280 struct ltt_kernel_channel
*chan
;
282 /* Create all channel directories */
283 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
284 DBG("Creating trace directory at %s", chan
->pathname
);
285 ret
= mkdir_recursive(chan
->pathname
, S_IRWXU
| S_IRWXG
);
288 ERR("Trace directory creation error");
303 * Return a socket connected to the libust communication socket
304 * of the application identified by the pid.
306 * If the pid is not found in the traceable list,
307 * return -1 to indicate error.
309 static int ust_connect_app(pid_t pid
)
312 struct ltt_traceable_app
*lta
;
314 DBG("Connect to application pid %d", pid
);
316 lta
= find_app_by_pid(pid
);
319 DBG("Application pid %d not found", pid
);
323 sock
= ustctl_connect_pid(lta
->pid
);
325 ERR("Fail connecting to the PID %d", pid
);
334 * Notify apps by writing 42 to a named pipe using name.
335 * Every applications waiting for a ltt-sessiond will be notified
336 * and re-register automatically to the session daemon.
338 * Return open or write error value.
340 static int notify_apps(const char *name
)
345 DBG("Notify the global application pipe");
347 /* Try opening the global pipe */
348 fd
= open(name
, O_WRONLY
);
353 /* Notify by writing on the pipe */
354 ret
= write(fd
, "42", 2);
366 * Setup the outgoing data buffer for the response (llm) by allocating the
367 * right amount of memory and copying the original information from the lsm
370 * Return total size of the buffer pointed by buf.
372 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
378 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
379 if (cmd_ctx
->llm
== NULL
) {
385 /* Copy common data */
386 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
387 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
389 cmd_ctx
->llm
->data_size
= size
;
390 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
399 * thread_manage_kconsumerd
401 * This thread manage the kconsumerd error sent
402 * back to the session daemon.
404 static void *thread_manage_kconsumerd(void *data
)
407 enum lttcomm_return_code code
;
409 DBG("[thread] Manage kconsumerd started");
411 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
416 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
421 /* Getting status code from kconsumerd */
422 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
427 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
428 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
429 if (kconsumerd_cmd_sock
< 0) {
430 sem_post(&kconsumerd_sem
);
431 perror("kconsumerd connect");
434 /* Signal condition to tell that the kconsumerd is ready */
435 sem_post(&kconsumerd_sem
);
436 DBG("Kconsumerd command socket ready");
438 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
439 lttcomm_get_readable_code(-code
));
443 /* Wait for any kconsumerd error */
444 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
446 ERR("Kconsumerd closed the command socket");
450 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
454 DBG("Kconsumerd thread dying");
461 * This thread manage the application socket communication
463 static void *thread_manage_apps(void *data
)
467 /* TODO: Something more elegant is needed but fine for now */
468 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
469 * for 32-bit vs 64-bit compat processes. */
470 /* replicate in ust with version number */
472 int reg
; /* 1:register, 0:unregister */
477 DBG("[thread] Manage apps started");
479 ret
= lttcomm_listen_unix_sock(apps_sock
);
484 /* Notify all applications to register */
485 notify_apps(default_global_apps_pipe
);
488 DBG("Accepting application registration");
489 /* Blocking call, waiting for transmission */
490 sock
= lttcomm_accept_unix_sock(apps_sock
);
495 /* Basic recv here to handle the very simple data
496 * that the libust send to register (reg_msg).
498 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
504 /* Add application to the global traceable list */
505 if (reg_msg
.reg
== 1) {
507 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
509 /* register_traceable_app only return an error with
510 * ENOMEM. At this point, we better stop everything.
516 unregister_traceable_app(reg_msg
.pid
);
526 * spawn_kconsumerd_thread
528 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
529 * exec or it will fails.
531 static int spawn_kconsumerd_thread(void)
535 /* Setup semaphore */
536 sem_init(&kconsumerd_sem
, 0, 0);
538 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
540 perror("pthread_create kconsumerd");
544 /* Wait for the kconsumerd thread to be ready */
545 sem_wait(&kconsumerd_sem
);
547 if (kconsumerd_pid
== 0) {
548 ERR("Kconsumerd did not start");
555 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
562 * Fork and exec a kernel consumer daemon (kconsumerd).
564 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
565 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
566 * kernel consumer MUST only be spawned before creating a kernel session.
568 * Return pid if successful else -1.
570 static pid_t
spawn_kconsumerd(void)
575 DBG("Spawning kconsumerd");
582 execlp("ltt-kconsumerd", "ltt-kconsumerd", "--verbose", NULL
);
584 perror("kernel start consumer exec");
587 } else if (pid
> 0) {
591 perror("kernel start consumer fork");
603 * Spawn the kconsumerd daemon and session daemon thread.
605 static int start_kconsumerd(void)
609 pthread_mutex_lock(&kconsumerd_pid_mutex
);
610 if (kconsumerd_pid
!= 0) {
614 ret
= spawn_kconsumerd();
616 ERR("Spawning kconsumerd failed");
617 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
618 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
622 /* Setting up the global kconsumerd_pid */
623 kconsumerd_pid
= ret
;
624 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
626 DBG("Kconsumerd pid %d", ret
);
628 DBG("Spawning kconsumerd thread");
629 ret
= spawn_kconsumerd_thread();
631 ERR("Fatal error spawning kconsumerd thread");
636 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
644 * modprobe_kernel_modules
646 static int modprobe_kernel_modules(void)
651 while (kernel_modules_list
[i
] != NULL
) {
652 ret
= snprintf(modprobe
, sizeof(modprobe
), "/sbin/modprobe %s",
653 kernel_modules_list
[i
]);
655 perror("snprintf modprobe");
658 ret
= system(modprobe
);
660 ERR("Unable to load module %s", kernel_modules_list
[i
]);
662 DBG("Modprobe successfully %s", kernel_modules_list
[i
]);
673 static int mount_debugfs(char *path
)
676 char *type
= "debugfs";
678 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
);
683 ret
= mount(type
, path
, type
, 0, NULL
);
685 perror("mount debugfs");
689 DBG("Mounted debugfs successfully at %s", path
);
698 * Setup necessary data for kernel tracer action.
700 static void init_kernel_tracer(void)
703 char *proc_mounts
= "/proc/mounts";
705 char *debugfs_path
= NULL
, *lttng_path
;
709 fp
= fopen(proc_mounts
, "r");
711 ERR("Unable to probe %s", proc_mounts
);
715 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
716 if (strstr(line
, "debugfs") != NULL
) {
717 /* Remove first string */
719 /* Dup string here so we can reuse line later on */
720 debugfs_path
= strdup(strtok(NULL
, " "));
721 DBG("Got debugfs path : %s", debugfs_path
);
728 /* Mount debugfs if needded */
729 if (debugfs_path
== NULL
) {
730 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
732 perror("asprintf debugfs path");
735 ret
= mount_debugfs(debugfs_path
);
741 /* Modprobe lttng kernel modules */
742 ret
= modprobe_kernel_modules();
747 /* Setup lttng kernel path */
748 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
750 perror("asprintf lttng path");
754 /* Open debugfs lttng */
755 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
756 if (kernel_tracer_fd
< 0) {
757 DBG("Failed to open %s", lttng_path
);
763 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
773 WARN("No kernel tracer available");
774 kernel_tracer_fd
= 0;
781 * Start tracing by creating trace directory and sending FDs to the kernel
784 static int start_kernel_trace(struct ltt_kernel_session
*session
)
788 /* Create trace directory */
789 ret
= create_trace_dir(session
);
791 if (ret
== -EEXIST
) {
792 ret
= LTTCOMM_KERN_DIR_EXIST
;
794 ret
= LTTCOMM_KERN_DIR_FAIL
;
799 if (session
->kconsumer_fds_sent
== 0) {
800 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
802 ERR("Send kconsumerd fds failed");
803 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
807 session
->kconsumer_fds_sent
= 1;
815 * init_default_channel
817 * Allocate a channel structure and fill it.
819 static struct lttng_channel
*init_default_channel(void)
821 struct lttng_channel
*chan
;
823 chan
= malloc(sizeof(struct lttng_channel
));
825 perror("init channel malloc");
829 if (snprintf(chan
->name
, NAME_MAX
, DEFAULT_CHANNEL_NAME
) < 0) {
830 perror("snprintf defautl channel name");
834 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
835 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
836 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
837 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
838 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
839 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
846 * create_kernel_session
848 * Create a kernel tracer session then create the default channel.
850 static int create_kernel_session(struct ltt_session
*session
)
853 struct lttng_channel
*chan
;
855 DBG("Creating kernel session");
857 ret
= kernel_create_session(session
, kernel_tracer_fd
);
859 ret
= LTTCOMM_KERN_SESS_FAIL
;
863 chan
= init_default_channel();
869 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME
);
871 ret
= kernel_create_channel(session
->kernel_session
, chan
, session
->path
);
873 ret
= LTTCOMM_KERN_CHAN_FAIL
;
884 * Process the command requested by the lttng client within the command
885 * context structure. This function make sure that the return structure (llm)
886 * is set and ready for transmission before returning.
888 * Return any error encountered or 0 for success.
890 static int process_client_msg(struct command_ctx
*cmd_ctx
)
894 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
896 /* Listing commands don't need a session */
897 switch (cmd_ctx
->lsm
->cmd_type
) {
898 case LTTNG_CREATE_SESSION
:
899 case LTTNG_LIST_SESSIONS
:
900 case LTTNG_LIST_EVENTS
:
901 case LTTNG_KERNEL_LIST_EVENTS
:
902 case LTTNG_LIST_TRACEABLE_APPS
:
905 DBG("Getting session %s by name", cmd_ctx
->lsm
->session_name
);
906 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session_name
);
907 if (cmd_ctx
->session
== NULL
) {
908 /* If session name not found */
909 if (cmd_ctx
->lsm
->session_name
!= NULL
) {
910 ret
= LTTCOMM_SESS_NOT_FOUND
;
911 } else { /* If no session name specified */
912 ret
= LTTCOMM_SELECT_SESS
;
920 * Check kernel command for kernel session.
922 switch (cmd_ctx
->lsm
->cmd_type
) {
923 case LTTNG_KERNEL_ADD_CONTEXT
:
924 case LTTNG_KERNEL_CREATE_CHANNEL
:
925 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
926 case LTTNG_KERNEL_DISABLE_CHANNEL
:
927 case LTTNG_KERNEL_DISABLE_EVENT
:
928 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
929 case LTTNG_KERNEL_ENABLE_CHANNEL
:
930 case LTTNG_KERNEL_ENABLE_EVENT
:
931 case LTTNG_KERNEL_LIST_EVENTS
:
932 /* Kernel tracer check */
933 if (kernel_tracer_fd
== 0) {
934 init_kernel_tracer();
935 if (kernel_tracer_fd
== 0) {
936 ret
= LTTCOMM_KERN_NA
;
941 /* Need a session for kernel command */
942 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_KERNEL_LIST_EVENTS
&&
943 cmd_ctx
->session
->kernel_session
== NULL
) {
945 ret
= create_kernel_session(cmd_ctx
->session
);
947 ret
= LTTCOMM_KERN_SESS_FAIL
;
951 /* Start the kernel consumer daemon */
952 if (kconsumerd_pid
== 0) {
953 ret
= start_kconsumerd();
961 /* Connect to ust apps if available pid */
962 if (cmd_ctx
->lsm
->pid
> 0) {
963 /* Connect to app using ustctl API */
964 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
965 if (cmd_ctx
->ust_sock
< 0) {
966 ret
= LTTCOMM_NO_TRACEABLE
;
971 /* Process by command type */
972 switch (cmd_ctx
->lsm
->cmd_type
) {
973 case LTTNG_KERNEL_ADD_CONTEXT
:
975 int found
= 0, no_event
= 0;
976 struct ltt_kernel_channel
*chan
;
977 struct ltt_kernel_event
*event
;
979 /* Setup lttng message with no payload */
980 ret
= setup_lttng_msg(cmd_ctx
, 0);
985 /* Check if event name is given */
986 if (strlen(cmd_ctx
->lsm
->u
.context
.event_name
) == 0) {
990 if (strlen(cmd_ctx
->lsm
->u
.context
.channel_name
) == 0) {
991 /* Go over all channels */
992 DBG("Adding context to all channels");
993 cds_list_for_each_entry(chan
,
994 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
996 ret
= kernel_add_channel_context(chan
,
997 &cmd_ctx
->lsm
->u
.context
.ctx
);
1002 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1003 if (event
!= NULL
) {
1004 ret
= kernel_add_event_context(event
,
1005 &cmd_ctx
->lsm
->u
.context
.ctx
);
1007 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1016 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.context
.channel_name
,
1017 cmd_ctx
->session
->kernel_session
);
1019 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1024 ret
= kernel_add_channel_context(chan
,
1025 &cmd_ctx
->lsm
->u
.context
.ctx
);
1027 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1031 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1032 if (event
!= NULL
) {
1033 ret
= kernel_add_event_context(event
,
1034 &cmd_ctx
->lsm
->u
.context
.ctx
);
1036 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1043 if (!found
&& !no_event
) {
1044 ret
= LTTCOMM_NO_EVENT
;
1051 case LTTNG_KERNEL_CREATE_CHANNEL
:
1053 /* Setup lttng message with no payload */
1054 ret
= setup_lttng_msg(cmd_ctx
, 0);
1060 DBG("Creating kernel channel");
1062 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1063 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1065 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1072 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1074 struct ltt_kernel_channel
*chan
;
1076 /* Setup lttng message with no payload */
1077 ret
= setup_lttng_msg(cmd_ctx
, 0);
1082 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1083 cmd_ctx
->session
->kernel_session
);
1085 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1087 } else if (chan
->enabled
== 1) {
1088 ret
= kernel_disable_channel(chan
);
1090 if (ret
!= EEXIST
) {
1091 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1097 kernel_wait_quiescent(kernel_tracer_fd
);
1101 case LTTNG_KERNEL_DISABLE_EVENT
:
1103 struct ltt_kernel_channel
*chan
;
1104 struct ltt_kernel_event
*ev
;
1106 /* Setup lttng message with no payload */
1107 ret
= setup_lttng_msg(cmd_ctx
, 0);
1112 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1113 cmd_ctx
->session
->kernel_session
);
1115 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1119 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, chan
);
1121 DBG("Disabling kernel event %s for channel %s.",
1122 cmd_ctx
->lsm
->u
.disable
.name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1123 ret
= kernel_disable_event(ev
);
1125 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1130 kernel_wait_quiescent(kernel_tracer_fd
);
1134 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1136 struct ltt_kernel_channel
*chan
;
1137 struct ltt_kernel_event
*ev
;
1139 /* Setup lttng message with no payload */
1140 ret
= setup_lttng_msg(cmd_ctx
, 0);
1145 DBG("Disabling all enabled kernel events");
1147 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1148 cmd_ctx
->session
->kernel_session
);
1150 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1154 /* For each event in the kernel session */
1155 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1156 DBG("Disabling kernel event %s for channel %s.",
1157 ev
->event
->name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1158 ret
= kernel_disable_event(ev
);
1164 /* Quiescent wait after event disable */
1165 kernel_wait_quiescent(kernel_tracer_fd
);
1169 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1171 struct ltt_kernel_channel
*chan
;
1173 /* Setup lttng message with no payload */
1174 ret
= setup_lttng_msg(cmd_ctx
, 0);
1179 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1180 cmd_ctx
->session
->kernel_session
);
1182 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1184 } else if (chan
->enabled
== 0) {
1185 ret
= kernel_enable_channel(chan
);
1187 if (ret
!= EEXIST
) {
1188 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1194 kernel_wait_quiescent(kernel_tracer_fd
);
1198 case LTTNG_KERNEL_ENABLE_EVENT
:
1200 struct ltt_kernel_channel
*chan
;
1201 struct ltt_kernel_event
*ev
;
1203 /* Setup lttng message with no payload */
1204 ret
= setup_lttng_msg(cmd_ctx
, 0);
1209 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1210 cmd_ctx
->session
->kernel_session
);
1212 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1216 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
);
1218 DBG("Creating kernel event %s for channel %s.",
1219 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1220 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, chan
);
1222 DBG("Enabling kernel event %s for channel %s.",
1223 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1224 ret
= kernel_enable_event(ev
);
1228 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1232 kernel_wait_quiescent(kernel_tracer_fd
);
1236 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1239 char *event_list
, *event
, *ptr
;
1240 struct ltt_kernel_channel
*chan
;
1241 struct ltt_kernel_event
*ev
;
1242 struct lttng_event ev_attr
;
1244 /* Setup lttng message with no payload */
1245 ret
= setup_lttng_msg(cmd_ctx
, 0);
1250 DBG("Enabling all kernel event");
1252 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1253 cmd_ctx
->session
->kernel_session
);
1255 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1259 /* For each event in the kernel session */
1260 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1261 DBG("Enabling kernel event %s for channel %s.",
1262 ev
->event
->name
, chan
->channel
->name
);
1263 ret
= kernel_enable_event(ev
);
1269 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1271 ret
= LTTCOMM_KERN_LIST_FAIL
;
1276 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
1277 ev
= get_kernel_event_by_name(event
, chan
);
1279 strncpy(ev_attr
.name
, event
, LTTNG_SYM_NAME_LEN
);
1280 /* Default event type for enable all */
1281 ev_attr
.type
= LTTNG_EVENT_TRACEPOINTS
;
1282 /* Enable each single tracepoint event */
1283 ret
= kernel_create_event(&ev_attr
, chan
);
1285 /* Ignore error here and continue */
1289 /* Move pointer to the next line */
1296 /* Quiescent wait after event enable */
1297 kernel_wait_quiescent(kernel_tracer_fd
);
1301 case LTTNG_KERNEL_LIST_EVENTS
:
1306 DBG("Listing kernel events");
1308 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1310 ret
= LTTCOMM_KERN_LIST_FAIL
;
1315 * Setup lttng message with payload size set to the event list size in
1316 * bytes and then copy list into the llm payload.
1318 ret
= setup_lttng_msg(cmd_ctx
, size
);
1323 /* Copy event list into message payload */
1324 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
1331 case LTTNG_START_TRACE
:
1333 struct ltt_kernel_channel
*chan
;
1335 /* Setup lttng message with no payload */
1336 ret
= setup_lttng_msg(cmd_ctx
, 0);
1341 /* Kernel tracing */
1342 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1343 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1344 DBG("Open kernel metadata");
1345 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1346 cmd_ctx
->session
->path
);
1348 ret
= LTTCOMM_KERN_META_FAIL
;
1353 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1354 DBG("Opening kernel metadata stream");
1355 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1356 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1358 ERR("Kernel create metadata stream failed");
1359 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1365 /* For each channel */
1366 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1367 if (chan
->stream_count
== 0) {
1368 ret
= kernel_open_channel_stream(chan
);
1370 ERR("Kernel create channel stream failed");
1371 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1374 /* Update the stream global counter */
1375 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1379 DBG("Start kernel tracing");
1380 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1382 ERR("Kernel start session failed");
1383 ret
= LTTCOMM_KERN_START_FAIL
;
1387 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1389 ret
= LTTCOMM_KERN_START_FAIL
;
1393 /* Quiescent wait after starting trace */
1394 kernel_wait_quiescent(kernel_tracer_fd
);
1397 /* TODO: Start all UST traces */
1402 case LTTNG_STOP_TRACE
:
1404 struct ltt_kernel_channel
*chan
;
1405 /* Setup lttng message with no payload */
1406 ret
= setup_lttng_msg(cmd_ctx
, 0);
1412 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1413 DBG("Stop kernel tracing");
1415 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1417 ERR("Kernel metadata flush failed");
1420 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1421 ret
= kernel_flush_buffer(chan
);
1423 ERR("Kernel flush buffer error");
1427 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1429 ERR("Kernel stop session failed");
1430 ret
= LTTCOMM_KERN_STOP_FAIL
;
1434 /* Quiescent wait after stopping trace */
1435 kernel_wait_quiescent(kernel_tracer_fd
);
1438 /* TODO : User-space tracer */
1443 case LTTNG_CREATE_SESSION
:
1445 /* Setup lttng message with no payload */
1446 ret
= setup_lttng_msg(cmd_ctx
, 0);
1451 ret
= create_session(cmd_ctx
->lsm
->session_name
, cmd_ctx
->lsm
->path
);
1453 if (ret
== -EEXIST
) {
1454 ret
= LTTCOMM_EXIST_SESS
;
1456 ret
= LTTCOMM_FATAL
;
1464 case LTTNG_DESTROY_SESSION
:
1466 /* Setup lttng message with no payload */
1467 ret
= setup_lttng_msg(cmd_ctx
, 0);
1472 /* Clean kernel session teardown */
1473 teardown_kernel_session(cmd_ctx
->session
);
1475 ret
= destroy_session(cmd_ctx
->lsm
->session_name
);
1477 ret
= LTTCOMM_FATAL
;
1485 case LTTNG_LIST_TRACES:
1487 unsigned int trace_count;
1489 trace_count = get_trace_count_per_session(cmd_ctx->session);
1490 if (trace_count == 0) {
1491 ret = LTTCOMM_NO_TRACE;
1495 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1500 get_traces_per_session(cmd_ctx->session,
1501 (struct lttng_trace *)(cmd_ctx->llm->payload));
1508 case UST_CREATE_TRACE:
1510 ret = setup_lttng_msg(cmd_ctx, 0);
1515 ret = ust_create_trace(cmd_ctx);
1522 case LTTNG_LIST_TRACEABLE_APPS
:
1524 unsigned int app_count
;
1526 app_count
= get_app_count();
1527 DBG("Traceable application count : %d", app_count
);
1528 if (app_count
== 0) {
1529 ret
= LTTCOMM_NO_APPS
;
1533 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1538 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1544 case UST_START_TRACE:
1546 ret = setup_lttng_msg(cmd_ctx, 0);
1551 ret = ust_start_trace(cmd_ctx);
1557 case UST_STOP_TRACE:
1559 ret = setup_lttng_msg(cmd_ctx, 0);
1564 ret = ust_stop_trace(cmd_ctx);
1571 case LTTNG_LIST_SESSIONS
:
1573 unsigned int session_count
;
1575 session_count
= get_session_count();
1576 if (session_count
== 0) {
1577 ret
= LTTCOMM_NO_SESSION
;
1581 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1586 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1592 /* Undefined command */
1593 ret
= setup_lttng_msg(cmd_ctx
, 0);
1602 /* Set return code */
1603 cmd_ctx
->llm
->ret_code
= ret
;
1608 if (cmd_ctx
->llm
== NULL
) {
1609 DBG("Missing llm structure. Allocating one.");
1610 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1614 /* Notify client of error */
1615 cmd_ctx
->llm
->ret_code
= ret
;
1622 * thread_manage_clients
1624 * This thread manage all clients request using the unix
1625 * client socket for communication.
1627 static void *thread_manage_clients(void *data
)
1630 struct command_ctx
*cmd_ctx
;
1632 DBG("[thread] Manage client started");
1634 ret
= lttcomm_listen_unix_sock(client_sock
);
1639 /* Notify parent pid that we are ready
1640 * to accept command for client side.
1642 if (opt_sig_parent
) {
1643 kill(ppid
, SIGCHLD
);
1647 /* Blocking call, waiting for transmission */
1648 DBG("Accepting client command ...");
1649 sock
= lttcomm_accept_unix_sock(client_sock
);
1654 /* Allocate context command to process the client request */
1655 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1657 /* Allocate data buffer for reception */
1658 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1659 cmd_ctx
->llm
= NULL
;
1660 cmd_ctx
->session
= NULL
;
1663 * Data is received from the lttng client. The struct
1664 * lttcomm_session_msg (lsm) contains the command and data request of
1667 DBG("Receiving data from client ...");
1668 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1673 // TODO: Validate cmd_ctx including sanity check for security purpose.
1676 * This function dispatch the work to the kernel or userspace tracer
1677 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1678 * informations for the client. The command context struct contains
1679 * everything this function may needs.
1681 ret
= process_client_msg(cmd_ctx
);
1683 /* TODO: Inform client somehow of the fatal error. At this point,
1684 * ret < 0 means that a malloc failed (ENOMEM). */
1685 /* Error detected but still accept command */
1686 clean_command_ctx(cmd_ctx
);
1690 DBG("Sending response (size: %d, retcode: %d)",
1691 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1692 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1694 ERR("Failed to send data back to client");
1697 clean_command_ctx(cmd_ctx
);
1699 /* End of transmission */
1709 * usage function on stderr
1711 static void usage(void)
1713 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1714 fprintf(stderr
, " -h, --help Display this usage.\n");
1715 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1716 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1717 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1718 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1719 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1720 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1721 fprintf(stderr
, " -V, --version Show version number.\n");
1722 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1723 fprintf(stderr
, " -q, --quiet No output at all.\n");
1724 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1728 * daemon argument parsing
1730 static int parse_args(int argc
, char **argv
)
1734 static struct option long_options
[] = {
1735 { "client-sock", 1, 0, 'c' },
1736 { "apps-sock", 1, 0, 'a' },
1737 { "kconsumerd-cmd-sock", 1, 0, 0 },
1738 { "kconsumerd-err-sock", 1, 0, 0 },
1739 { "daemonize", 0, 0, 'd' },
1740 { "sig-parent", 0, 0, 'S' },
1741 { "help", 0, 0, 'h' },
1742 { "group", 1, 0, 'g' },
1743 { "version", 0, 0, 'V' },
1744 { "quiet", 0, 0, 'q' },
1745 { "verbose", 0, 0, 'v' },
1750 int option_index
= 0;
1751 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1758 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1760 fprintf(stderr
, " with arg %s\n", optarg
);
1764 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1767 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1773 opt_tracing_group
= strdup(optarg
);
1779 fprintf(stdout
, "%s\n", VERSION
);
1785 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1788 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1797 /* Unknown option or other error.
1798 * Error is printed by getopt, just return */
1807 * init_daemon_socket
1809 * Creates the two needed socket by the daemon.
1810 * apps_sock - The communication socket for all UST apps.
1811 * client_sock - The communication of the cli tool (lttng).
1813 static int init_daemon_socket()
1818 old_umask
= umask(0);
1820 /* Create client tool unix socket */
1821 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
1822 if (client_sock
< 0) {
1823 ERR("Create unix sock failed: %s", client_unix_sock_path
);
1828 /* File permission MUST be 660 */
1829 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1831 ERR("Set file permissions failed: %s", client_unix_sock_path
);
1836 /* Create the application unix socket */
1837 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
1838 if (apps_sock
< 0) {
1839 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
1844 /* File permission MUST be 666 */
1845 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1847 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
1858 * check_existing_daemon
1860 * Check if the global socket is available.
1861 * If yes, error is returned.
1863 static int check_existing_daemon()
1867 ret
= access(client_unix_sock_path
, F_OK
);
1869 ret
= access(apps_unix_sock_path
, F_OK
);
1878 * Set the tracing group gid onto the client socket.
1880 * Race window between mkdir and chown is OK because we are going from
1881 * more permissive (root.root) to les permissive (root.tracing).
1883 static int set_permissions(void)
1888 /* Decide which group name to use */
1889 (opt_tracing_group
!= NULL
) ?
1890 (grp
= getgrnam(opt_tracing_group
)) :
1891 (grp
= getgrnam(default_tracing_group
));
1895 WARN("No tracing group detected");
1898 ERR("Missing tracing group. Aborting execution.");
1904 /* Set lttng run dir */
1905 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
1907 ERR("Unable to set group on " LTTNG_RUNDIR
);
1911 /* lttng client socket path */
1912 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
1914 ERR("Unable to set group on %s", client_unix_sock_path
);
1918 /* kconsumerd error socket path */
1919 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
1921 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
1925 DBG("All permissions are set");
1932 * create_lttng_rundir
1934 * Create the lttng run directory needed for all
1935 * global sockets and pipe.
1937 static int create_lttng_rundir(void)
1941 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
1943 if (errno
!= EEXIST
) {
1944 ERR("Unable to create " LTTNG_RUNDIR
);
1956 * set_kconsumerd_sockets
1958 * Setup sockets and directory needed by the kconsumerd
1959 * communication with the session daemon.
1961 static int set_kconsumerd_sockets(void)
1965 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1966 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1969 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1970 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1973 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1975 if (errno
!= EEXIST
) {
1976 ERR("Failed to create " KCONSUMERD_PATH
);
1982 /* Create the kconsumerd error unix socket */
1983 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1984 if (kconsumerd_err_sock
< 0) {
1985 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1990 /* File permission MUST be 660 */
1991 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1993 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2005 * Signal handler for the daemon
2007 static void sighandler(int sig
)
2011 DBG("SIGPIPE catched");
2014 DBG("SIGINT catched");
2018 DBG("SIGTERM catched");
2029 * set_signal_handler
2031 * Setup signal handler for :
2032 * SIGINT, SIGTERM, SIGPIPE
2034 static int set_signal_handler(void)
2037 struct sigaction sa
;
2040 if ((ret
= sigemptyset(&sigset
)) < 0) {
2041 perror("sigemptyset");
2045 sa
.sa_handler
= sighandler
;
2046 sa
.sa_mask
= sigset
;
2048 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2049 perror("sigaction");
2053 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2054 perror("sigaction");
2058 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2059 perror("sigaction");
2063 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2071 * Set open files limit to unlimited. This daemon can open a large number of
2072 * file descriptors in order to consumer multiple kernel traces.
2074 static void set_ulimit(void)
2079 lim
.rlim_cur
= 65535;
2080 lim
.rlim_max
= 65535;
2082 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2084 perror("failed to set open files limit");
2091 int main(int argc
, char **argv
)
2095 const char *home_path
;
2097 /* Parse arguments */
2099 if ((ret
= parse_args(argc
, argv
) < 0)) {
2112 /* Check if daemon is UID = 0 */
2113 is_root
= !getuid();
2116 ret
= create_lttng_rundir();
2121 if (strlen(apps_unix_sock_path
) == 0) {
2122 snprintf(apps_unix_sock_path
, PATH_MAX
,
2123 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2126 if (strlen(client_unix_sock_path
) == 0) {
2127 snprintf(client_unix_sock_path
, PATH_MAX
,
2128 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2131 ret
= set_kconsumerd_sockets();
2136 /* Setup kernel tracer */
2137 init_kernel_tracer();
2139 /* Set ulimit for open files */
2142 home_path
= get_home_dir();
2143 if (home_path
== NULL
) {
2144 ERR("Can't get HOME directory for sockets creation.\n \
2145 Please specify --socket PATH.");
2149 if (strlen(apps_unix_sock_path
) == 0) {
2150 snprintf(apps_unix_sock_path
, PATH_MAX
,
2151 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2154 /* Set the cli tool unix socket path */
2155 if (strlen(client_unix_sock_path
) == 0) {
2156 snprintf(client_unix_sock_path
, PATH_MAX
,
2157 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2161 DBG("Client socket path %s", client_unix_sock_path
);
2162 DBG("Application socket path %s", apps_unix_sock_path
);
2164 /* See if daemon already exist. If any of the two
2165 * socket needed by the daemon are present, this test fails
2167 if ((ret
= check_existing_daemon()) == 0) {
2168 ERR("Already running daemon.\n");
2169 /* We do not goto error because we must not
2170 * cleanup() because a daemon is already running.
2175 if (set_signal_handler() < 0) {
2179 /* Setup the needed unix socket */
2180 if (init_daemon_socket() < 0) {
2184 /* Set credentials to socket */
2185 if (is_root
&& (set_permissions() < 0)) {
2189 /* Get parent pid if -S, --sig-parent is specified. */
2190 if (opt_sig_parent
) {
2195 /* Create thread to manage the client socket */
2196 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2198 perror("pthread_create");
2202 /* Create thread to manage application socket */
2203 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2205 perror("pthread_create");
2209 ret
= pthread_join(client_thread
, &status
);
2211 perror("pthread_join");