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
;
111 /* Decrement session count */
112 session
->kern_session_count
--;
119 * Cleanup the daemon on exit
121 static void cleanup()
125 struct ltt_session
*sess
;
130 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
131 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
134 /* Stopping all threads */
135 DBG("Terminating all threads");
136 pthread_cancel(client_thread
);
137 pthread_cancel(apps_thread
);
138 if (kconsumerd_pid
!= 0) {
139 pthread_cancel(kconsumerd_thread
);
142 DBG("Unlinking all unix socket");
143 unlink(client_unix_sock_path
);
144 unlink(apps_unix_sock_path
);
145 unlink(kconsumerd_err_unix_sock_path
);
147 DBG("Removing %s directory", LTTNG_RUNDIR
);
148 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
150 ERR("asprintf failed. Something is really wrong!");
153 /* Remove lttng run directory */
156 ERR("Unable to clean " LTTNG_RUNDIR
);
159 DBG("Cleaning up all session");
160 /* Cleanup ALL session */
161 cds_list_for_each_entry(sess
, <t_session_list
.head
, list
) {
162 teardown_kernel_session(sess
);
163 // TODO complete session cleanup (including UST)
166 DBG("Closing kernel fd");
167 close(kernel_tracer_fd
);
173 * Send data on a unix socket using the liblttsessiondcomm API.
175 * Return lttcomm error code.
177 static int send_unix_sock(int sock
, void *buf
, size_t len
)
179 /* Check valid length */
184 return lttcomm_send_unix_sock(sock
, buf
, len
);
190 * Free memory of a command context structure.
192 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
194 DBG("Clean command context structure %p", cmd_ctx
);
208 * send_kconsumerd_fds
210 * Send all stream fds of the kernel session to the consumer.
212 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
216 struct ltt_kernel_stream
*stream
;
217 struct ltt_kernel_channel
*chan
;
218 struct lttcomm_kconsumerd_header lkh
;
219 struct lttcomm_kconsumerd_msg lkm
;
221 nb_fd
= session
->stream_count_global
;
224 lkh
.payload_size
= (nb_fd
+ 1) * sizeof(struct lttcomm_kconsumerd_msg
);
225 lkh
.cmd_type
= ADD_STREAM
;
227 DBG("Sending kconsumerd header");
229 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
231 perror("send kconsumerd header");
235 DBG("Sending metadata stream fd");
237 /* Send metadata stream fd first */
238 lkm
.fd
= session
->metadata_stream_fd
;
239 lkm
.state
= ACTIVE_FD
;
240 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
241 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
243 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
245 perror("send kconsumerd fd");
249 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
250 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
252 lkm
.state
= stream
->state
;
253 lkm
.max_sb_size
= chan
->channel
->attr
.subbuf_size
;
254 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
256 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
258 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
260 perror("send kconsumerd fd");
266 DBG("Kconsumerd fds sent");
277 * Create the trace output directory.
279 static int create_trace_dir(struct ltt_kernel_session
*session
)
282 struct ltt_kernel_channel
*chan
;
284 /* Create all channel directories */
285 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
286 DBG("Creating trace directory at %s", chan
->pathname
);
287 ret
= mkdir_recursive(chan
->pathname
, S_IRWXU
| S_IRWXG
);
290 ERR("Trace directory creation error");
305 * Return a socket connected to the libust communication socket
306 * of the application identified by the pid.
308 * If the pid is not found in the traceable list,
309 * return -1 to indicate error.
311 static int ust_connect_app(pid_t pid
)
314 struct ltt_traceable_app
*lta
;
316 DBG("Connect to application pid %d", pid
);
318 lta
= find_app_by_pid(pid
);
321 DBG("Application pid %d not found", pid
);
325 sock
= ustctl_connect_pid(lta
->pid
);
327 ERR("Fail connecting to the PID %d", pid
);
336 * Notify apps by writing 42 to a named pipe using name.
337 * Every applications waiting for a ltt-sessiond will be notified
338 * and re-register automatically to the session daemon.
340 * Return open or write error value.
342 static int notify_apps(const char *name
)
347 DBG("Notify the global application pipe");
349 /* Try opening the global pipe */
350 fd
= open(name
, O_WRONLY
);
355 /* Notify by writing on the pipe */
356 ret
= write(fd
, "42", 2);
368 * Setup the outgoing data buffer for the response (llm) by allocating the
369 * right amount of memory and copying the original information from the lsm
372 * Return total size of the buffer pointed by buf.
374 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
380 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
381 if (cmd_ctx
->llm
== NULL
) {
387 /* Copy common data */
388 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
389 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
391 cmd_ctx
->llm
->data_size
= size
;
392 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
401 * thread_manage_kconsumerd
403 * This thread manage the kconsumerd error sent
404 * back to the session daemon.
406 static void *thread_manage_kconsumerd(void *data
)
409 enum lttcomm_return_code code
;
411 DBG("[thread] Manage kconsumerd started");
413 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
418 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
423 /* Getting status code from kconsumerd */
424 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
429 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
430 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
431 if (kconsumerd_cmd_sock
< 0) {
432 sem_post(&kconsumerd_sem
);
433 perror("kconsumerd connect");
436 /* Signal condition to tell that the kconsumerd is ready */
437 sem_post(&kconsumerd_sem
);
438 DBG("Kconsumerd command socket ready");
440 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
441 lttcomm_get_readable_code(-code
));
445 /* Wait for any kconsumerd error */
446 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
448 ERR("Kconsumerd closed the command socket");
452 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
456 DBG("Kconsumerd thread dying");
463 * This thread manage the application socket communication
465 static void *thread_manage_apps(void *data
)
469 /* TODO: Something more elegant is needed but fine for now */
470 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
471 * for 32-bit vs 64-bit compat processes. */
472 /* replicate in ust with version number */
474 int reg
; /* 1:register, 0:unregister */
479 DBG("[thread] Manage apps started");
481 ret
= lttcomm_listen_unix_sock(apps_sock
);
486 /* Notify all applications to register */
487 notify_apps(default_global_apps_pipe
);
490 DBG("Accepting application registration");
491 /* Blocking call, waiting for transmission */
492 sock
= lttcomm_accept_unix_sock(apps_sock
);
497 /* Basic recv here to handle the very simple data
498 * that the libust send to register (reg_msg).
500 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
506 /* Add application to the global traceable list */
507 if (reg_msg
.reg
== 1) {
509 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
511 /* register_traceable_app only return an error with
512 * ENOMEM. At this point, we better stop everything.
518 unregister_traceable_app(reg_msg
.pid
);
528 * spawn_kconsumerd_thread
530 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
531 * exec or it will fails.
533 static int spawn_kconsumerd_thread(void)
537 /* Setup semaphore */
538 sem_init(&kconsumerd_sem
, 0, 0);
540 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
542 perror("pthread_create kconsumerd");
546 /* Wait for the kconsumerd thread to be ready */
547 sem_wait(&kconsumerd_sem
);
549 if (kconsumerd_pid
== 0) {
550 ERR("Kconsumerd did not start");
557 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
564 * Fork and exec a kernel consumer daemon (kconsumerd).
566 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
567 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
568 * kernel consumer MUST only be spawned before creating a kernel session.
570 * Return pid if successful else -1.
572 static pid_t
spawn_kconsumerd(void)
577 DBG("Spawning kconsumerd");
584 execlp("kconsumerd", "kconsumerd", "--verbose", NULL
);
586 perror("kernel start consumer exec");
589 } else if (pid
> 0) {
593 perror("kernel start consumer fork");
605 * Spawn the kconsumerd daemon and session daemon thread.
607 static int start_kconsumerd(void)
611 pthread_mutex_lock(&kconsumerd_pid_mutex
);
612 if (kconsumerd_pid
!= 0) {
616 ret
= spawn_kconsumerd();
618 ERR("Spawning kconsumerd failed");
619 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
620 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
624 /* Setting up the global kconsumerd_pid */
625 kconsumerd_pid
= ret
;
626 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
628 DBG("Kconsumerd pid %d", ret
);
630 DBG("Spawning kconsumerd thread");
631 ret
= spawn_kconsumerd_thread();
633 ERR("Fatal error spawning kconsumerd thread");
638 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
646 * modprobe_kernel_modules
648 static int modprobe_kernel_modules(void)
653 while (kernel_modules_list
[i
] != NULL
) {
654 ret
= snprintf(modprobe
, sizeof(modprobe
), "/sbin/modprobe %s",
655 kernel_modules_list
[i
]);
657 perror("snprintf modprobe");
660 ret
= system(modprobe
);
662 ERR("Unable to load module %s", kernel_modules_list
[i
]);
664 DBG("Modprobe successfully %s", kernel_modules_list
[i
]);
675 static int mount_debugfs(char *path
)
678 char *type
= "debugfs";
680 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
);
685 ret
= mount(type
, path
, type
, 0, NULL
);
687 perror("mount debugfs");
691 DBG("Mounted debugfs successfully at %s", path
);
700 * Setup necessary data for kernel tracer action.
702 static void init_kernel_tracer(void)
705 char *proc_mounts
= "/proc/mounts";
707 char *debugfs_path
= NULL
, *lttng_path
;
711 fp
= fopen(proc_mounts
, "r");
713 ERR("Unable to probe %s", proc_mounts
);
717 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
718 if (strstr(line
, "debugfs") != NULL
) {
719 /* Remove first string */
721 /* Dup string here so we can reuse line later on */
722 debugfs_path
= strdup(strtok(NULL
, " "));
723 DBG("Got debugfs path : %s", debugfs_path
);
730 /* Mount debugfs if needded */
731 if (debugfs_path
== NULL
) {
732 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
734 perror("asprintf debugfs path");
737 ret
= mount_debugfs(debugfs_path
);
743 /* Modprobe lttng kernel modules */
744 ret
= modprobe_kernel_modules();
749 /* Setup lttng kernel path */
750 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
752 perror("asprintf lttng path");
756 /* Open debugfs lttng */
757 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
758 if (kernel_tracer_fd
< 0) {
759 DBG("Failed to open %s", lttng_path
);
765 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
775 WARN("No kernel tracer available");
776 kernel_tracer_fd
= 0;
783 * Start tracing by creating trace directory and sending FDs to the kernel
786 static int start_kernel_trace(struct ltt_kernel_session
*session
)
790 /* Create trace directory */
791 ret
= create_trace_dir(session
);
793 if (ret
== -EEXIST
) {
794 ret
= LTTCOMM_KERN_DIR_EXIST
;
796 ret
= LTTCOMM_KERN_DIR_FAIL
;
801 if (session
->kconsumer_fds_sent
== 0) {
802 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
804 ERR("Send kconsumerd fds failed");
805 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
809 session
->kconsumer_fds_sent
= 1;
817 * init_default_channel
819 * Allocate a channel structure and fill it.
821 static struct lttng_channel
*init_default_channel(void)
823 struct lttng_channel
*chan
;
825 chan
= malloc(sizeof(struct lttng_channel
));
827 perror("init channel malloc");
831 if (snprintf(chan
->name
, NAME_MAX
, DEFAULT_CHANNEL_NAME
) < 0) {
832 perror("snprintf defautl channel name");
836 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
837 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
838 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
839 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
840 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
841 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
848 * create_kernel_session
850 * Create a kernel tracer session then create the default channel.
852 static int create_kernel_session(struct ltt_session
*session
)
855 struct lttng_channel
*chan
;
857 DBG("Creating kernel session");
859 ret
= kernel_create_session(session
, kernel_tracer_fd
);
861 ret
= LTTCOMM_KERN_SESS_FAIL
;
865 chan
= init_default_channel();
871 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME
);
873 ret
= kernel_create_channel(session
->kernel_session
, chan
, session
->path
);
875 ret
= LTTCOMM_KERN_CHAN_FAIL
;
886 * Process the command requested by the lttng client within the command
887 * context structure. This function make sure that the return structure (llm)
888 * is set and ready for transmission before returning.
890 * Return any error encountered or 0 for success.
892 static int process_client_msg(struct command_ctx
*cmd_ctx
)
896 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
898 /* Listing commands don't need a session */
899 switch (cmd_ctx
->lsm
->cmd_type
) {
900 case LTTNG_CREATE_SESSION
:
901 case LTTNG_LIST_SESSIONS
:
902 case LTTNG_LIST_EVENTS
:
903 case LTTNG_KERNEL_LIST_EVENTS
:
904 case LTTNG_LIST_TRACEABLE_APPS
:
907 DBG("Getting session %s by name", cmd_ctx
->lsm
->session_name
);
908 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session_name
);
909 if (cmd_ctx
->session
== NULL
) {
910 /* If session name not found */
911 if (cmd_ctx
->lsm
->session_name
!= NULL
) {
912 ret
= LTTCOMM_SESS_NOT_FOUND
;
913 } else { /* If no session name specified */
914 ret
= LTTCOMM_SELECT_SESS
;
922 * Check kernel command for kernel session.
924 switch (cmd_ctx
->lsm
->cmd_type
) {
925 case LTTNG_KERNEL_ADD_CONTEXT
:
926 case LTTNG_KERNEL_CREATE_CHANNEL
:
927 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
928 case LTTNG_KERNEL_DISABLE_CHANNEL
:
929 case LTTNG_KERNEL_DISABLE_EVENT
:
930 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
931 case LTTNG_KERNEL_ENABLE_CHANNEL
:
932 case LTTNG_KERNEL_ENABLE_EVENT
:
933 case LTTNG_KERNEL_LIST_EVENTS
:
934 /* Kernel tracer check */
935 if (kernel_tracer_fd
== 0) {
936 init_kernel_tracer();
937 if (kernel_tracer_fd
== 0) {
938 ret
= LTTCOMM_KERN_NA
;
943 /* Need a session for kernel command */
944 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_KERNEL_LIST_EVENTS
&&
945 cmd_ctx
->session
->kernel_session
== NULL
) {
947 ret
= create_kernel_session(cmd_ctx
->session
);
949 ret
= LTTCOMM_KERN_SESS_FAIL
;
953 /* Start the kernel consumer daemon */
954 if (kconsumerd_pid
== 0) {
955 ret
= start_kconsumerd();
963 /* Connect to ust apps if available pid */
964 if (cmd_ctx
->lsm
->pid
> 0) {
965 /* Connect to app using ustctl API */
966 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
967 if (cmd_ctx
->ust_sock
< 0) {
968 ret
= LTTCOMM_NO_TRACEABLE
;
973 /* Process by command type */
974 switch (cmd_ctx
->lsm
->cmd_type
) {
975 case LTTNG_KERNEL_ADD_CONTEXT
:
977 int found
= 0, no_event
= 0;
978 struct ltt_kernel_channel
*chan
;
979 struct ltt_kernel_event
*event
;
981 /* Setup lttng message with no payload */
982 ret
= setup_lttng_msg(cmd_ctx
, 0);
987 /* Check if event name is given */
988 if (strlen(cmd_ctx
->lsm
->u
.context
.event_name
) == 0) {
992 if (strlen(cmd_ctx
->lsm
->u
.context
.channel_name
) == 0) {
993 /* Go over all channels */
994 DBG("Adding context to all channels");
995 cds_list_for_each_entry(chan
,
996 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
998 ret
= kernel_add_channel_context(chan
,
999 &cmd_ctx
->lsm
->u
.context
.ctx
);
1004 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1005 if (event
!= NULL
) {
1006 ret
= kernel_add_event_context(event
,
1007 &cmd_ctx
->lsm
->u
.context
.ctx
);
1009 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1018 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.context
.channel_name
,
1019 cmd_ctx
->session
->kernel_session
);
1021 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1026 ret
= kernel_add_channel_context(chan
,
1027 &cmd_ctx
->lsm
->u
.context
.ctx
);
1029 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1033 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
1034 if (event
!= NULL
) {
1035 ret
= kernel_add_event_context(event
,
1036 &cmd_ctx
->lsm
->u
.context
.ctx
);
1038 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
1045 if (!found
&& !no_event
) {
1046 ret
= LTTCOMM_NO_EVENT
;
1053 case LTTNG_KERNEL_CREATE_CHANNEL
:
1055 /* Setup lttng message with no payload */
1056 ret
= setup_lttng_msg(cmd_ctx
, 0);
1062 DBG("Creating kernel channel");
1064 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1065 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1067 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1074 case LTTNG_KERNEL_DISABLE_CHANNEL
:
1076 struct ltt_kernel_channel
*chan
;
1078 /* Setup lttng message with no payload */
1079 ret
= setup_lttng_msg(cmd_ctx
, 0);
1084 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1085 cmd_ctx
->session
->kernel_session
);
1087 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1089 } else if (chan
->enabled
== 1) {
1090 ret
= kernel_disable_channel(chan
);
1092 if (ret
!= EEXIST
) {
1093 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1099 kernel_wait_quiescent(kernel_tracer_fd
);
1103 case LTTNG_KERNEL_DISABLE_EVENT
:
1105 struct ltt_kernel_channel
*chan
;
1106 struct ltt_kernel_event
*ev
;
1108 /* Setup lttng message with no payload */
1109 ret
= setup_lttng_msg(cmd_ctx
, 0);
1114 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1115 cmd_ctx
->session
->kernel_session
);
1117 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1121 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, chan
);
1123 DBG("Disabling kernel event %s for channel %s.",
1124 cmd_ctx
->lsm
->u
.disable
.name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1125 ret
= kernel_disable_event(ev
);
1127 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1132 kernel_wait_quiescent(kernel_tracer_fd
);
1136 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1138 struct ltt_kernel_channel
*chan
;
1139 struct ltt_kernel_event
*ev
;
1141 /* Setup lttng message with no payload */
1142 ret
= setup_lttng_msg(cmd_ctx
, 0);
1147 DBG("Disabling all enabled kernel events");
1149 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1150 cmd_ctx
->session
->kernel_session
);
1152 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1156 /* For each event in the kernel session */
1157 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1158 DBG("Disabling kernel event %s for channel %s.",
1159 ev
->event
->name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1160 ret
= kernel_disable_event(ev
);
1166 /* Quiescent wait after event disable */
1167 kernel_wait_quiescent(kernel_tracer_fd
);
1171 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1173 struct ltt_kernel_channel
*chan
;
1175 /* Setup lttng message with no payload */
1176 ret
= setup_lttng_msg(cmd_ctx
, 0);
1181 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1182 cmd_ctx
->session
->kernel_session
);
1184 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1186 } else if (chan
->enabled
== 0) {
1187 ret
= kernel_enable_channel(chan
);
1189 if (ret
!= EEXIST
) {
1190 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1196 kernel_wait_quiescent(kernel_tracer_fd
);
1200 case LTTNG_KERNEL_ENABLE_EVENT
:
1202 struct ltt_kernel_channel
*chan
;
1203 struct ltt_kernel_event
*ev
;
1205 /* Setup lttng message with no payload */
1206 ret
= setup_lttng_msg(cmd_ctx
, 0);
1211 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1212 cmd_ctx
->session
->kernel_session
);
1214 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1218 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
);
1220 DBG("Creating kernel event %s for channel %s.",
1221 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1222 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, chan
);
1224 DBG("Enabling kernel event %s for channel %s.",
1225 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1226 ret
= kernel_enable_event(ev
);
1230 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1234 kernel_wait_quiescent(kernel_tracer_fd
);
1238 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1241 char *event_list
, *event
, *ptr
;
1242 struct ltt_kernel_channel
*chan
;
1243 struct ltt_kernel_event
*ev
;
1244 struct lttng_event ev_attr
;
1246 /* Setup lttng message with no payload */
1247 ret
= setup_lttng_msg(cmd_ctx
, 0);
1252 DBG("Enabling all kernel event");
1254 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1255 cmd_ctx
->session
->kernel_session
);
1257 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1261 /* For each event in the kernel session */
1262 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1263 DBG("Enabling kernel event %s for channel %s.",
1264 ev
->event
->name
, chan
->channel
->name
);
1265 ret
= kernel_enable_event(ev
);
1271 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1273 ret
= LTTCOMM_KERN_LIST_FAIL
;
1278 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
1279 ev
= get_kernel_event_by_name(event
, chan
);
1281 strncpy(ev_attr
.name
, event
, LTTNG_SYM_NAME_LEN
);
1282 /* Default event type for enable all */
1283 ev_attr
.type
= LTTNG_EVENT_TRACEPOINTS
;
1284 /* Enable each single tracepoint event */
1285 ret
= kernel_create_event(&ev_attr
, chan
);
1287 /* Ignore error here and continue */
1291 /* Move pointer to the next line */
1298 /* Quiescent wait after event enable */
1299 kernel_wait_quiescent(kernel_tracer_fd
);
1303 case LTTNG_KERNEL_LIST_EVENTS
:
1308 DBG("Listing kernel events");
1310 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1312 ret
= LTTCOMM_KERN_LIST_FAIL
;
1317 * Setup lttng message with payload size set to the event list size in
1318 * bytes and then copy list into the llm payload.
1320 ret
= setup_lttng_msg(cmd_ctx
, size
);
1325 /* Copy event list into message payload */
1326 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
1333 case LTTNG_START_TRACE
:
1335 struct ltt_kernel_channel
*chan
;
1337 /* Setup lttng message with no payload */
1338 ret
= setup_lttng_msg(cmd_ctx
, 0);
1343 /* Kernel tracing */
1344 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1345 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1346 DBG("Open kernel metadata");
1347 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1348 cmd_ctx
->session
->path
);
1350 ret
= LTTCOMM_KERN_META_FAIL
;
1355 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1356 DBG("Opening kernel metadata stream");
1357 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1358 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1360 ERR("Kernel create metadata stream failed");
1361 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1367 /* For each channel */
1368 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1369 if (chan
->stream_count
== 0) {
1370 ret
= kernel_open_channel_stream(chan
);
1372 ERR("Kernel create channel stream failed");
1373 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1376 /* Update the stream global counter */
1377 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1381 DBG("Start kernel tracing");
1382 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1384 ERR("Kernel start session failed");
1385 ret
= LTTCOMM_KERN_START_FAIL
;
1389 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1391 ret
= LTTCOMM_KERN_START_FAIL
;
1395 /* Quiescent wait after starting trace */
1396 kernel_wait_quiescent(kernel_tracer_fd
);
1399 /* TODO: Start all UST traces */
1404 case LTTNG_STOP_TRACE
:
1406 struct ltt_kernel_channel
*chan
;
1407 /* Setup lttng message with no payload */
1408 ret
= setup_lttng_msg(cmd_ctx
, 0);
1414 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1415 DBG("Stop kernel tracing");
1417 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1419 ERR("Kernel metadata flush failed");
1422 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1423 ret
= kernel_flush_buffer(chan
);
1425 ERR("Kernel flush buffer error");
1429 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1431 ERR("Kernel stop session failed");
1432 ret
= LTTCOMM_KERN_STOP_FAIL
;
1436 /* Quiescent wait after stopping trace */
1437 kernel_wait_quiescent(kernel_tracer_fd
);
1440 /* TODO : User-space tracer */
1445 case LTTNG_CREATE_SESSION
:
1447 /* Setup lttng message with no payload */
1448 ret
= setup_lttng_msg(cmd_ctx
, 0);
1453 ret
= create_session(cmd_ctx
->lsm
->session_name
, cmd_ctx
->lsm
->path
);
1455 if (ret
== -EEXIST
) {
1456 ret
= LTTCOMM_EXIST_SESS
;
1458 ret
= LTTCOMM_FATAL
;
1466 case LTTNG_DESTROY_SESSION
:
1468 /* Setup lttng message with no payload */
1469 ret
= setup_lttng_msg(cmd_ctx
, 0);
1474 /* Clean kernel session teardown */
1475 teardown_kernel_session(cmd_ctx
->session
);
1477 ret
= destroy_session(cmd_ctx
->lsm
->session_name
);
1479 ret
= LTTCOMM_FATAL
;
1487 case LTTNG_LIST_TRACES:
1489 unsigned int trace_count;
1491 trace_count = get_trace_count_per_session(cmd_ctx->session);
1492 if (trace_count == 0) {
1493 ret = LTTCOMM_NO_TRACE;
1497 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1502 get_traces_per_session(cmd_ctx->session,
1503 (struct lttng_trace *)(cmd_ctx->llm->payload));
1510 case UST_CREATE_TRACE:
1512 ret = setup_lttng_msg(cmd_ctx, 0);
1517 ret = ust_create_trace(cmd_ctx);
1524 case LTTNG_LIST_TRACEABLE_APPS
:
1526 unsigned int app_count
;
1528 app_count
= get_app_count();
1529 DBG("Traceable application count : %d", app_count
);
1530 if (app_count
== 0) {
1531 ret
= LTTCOMM_NO_APPS
;
1535 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1540 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1546 case UST_START_TRACE:
1548 ret = setup_lttng_msg(cmd_ctx, 0);
1553 ret = ust_start_trace(cmd_ctx);
1559 case UST_STOP_TRACE:
1561 ret = setup_lttng_msg(cmd_ctx, 0);
1566 ret = ust_stop_trace(cmd_ctx);
1573 case LTTNG_LIST_SESSIONS
:
1575 unsigned int session_count
;
1577 session_count
= get_session_count();
1578 if (session_count
== 0) {
1579 ret
= LTTCOMM_NO_SESSION
;
1583 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1588 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1594 /* Undefined command */
1595 ret
= setup_lttng_msg(cmd_ctx
, 0);
1604 /* Set return code */
1605 cmd_ctx
->llm
->ret_code
= ret
;
1610 if (cmd_ctx
->llm
== NULL
) {
1611 DBG("Missing llm structure. Allocating one.");
1612 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1616 /* Notify client of error */
1617 cmd_ctx
->llm
->ret_code
= ret
;
1624 * thread_manage_clients
1626 * This thread manage all clients request using the unix
1627 * client socket for communication.
1629 static void *thread_manage_clients(void *data
)
1632 struct command_ctx
*cmd_ctx
;
1634 DBG("[thread] Manage client started");
1636 ret
= lttcomm_listen_unix_sock(client_sock
);
1641 /* Notify parent pid that we are ready
1642 * to accept command for client side.
1644 if (opt_sig_parent
) {
1645 kill(ppid
, SIGCHLD
);
1649 /* Blocking call, waiting for transmission */
1650 DBG("Accepting client command ...");
1651 sock
= lttcomm_accept_unix_sock(client_sock
);
1656 /* Allocate context command to process the client request */
1657 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1659 /* Allocate data buffer for reception */
1660 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1661 cmd_ctx
->llm
= NULL
;
1662 cmd_ctx
->session
= NULL
;
1665 * Data is received from the lttng client. The struct
1666 * lttcomm_session_msg (lsm) contains the command and data request of
1669 DBG("Receiving data from client ...");
1670 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1675 // TODO: Validate cmd_ctx including sanity check for security purpose.
1678 * This function dispatch the work to the kernel or userspace tracer
1679 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1680 * informations for the client. The command context struct contains
1681 * everything this function may needs.
1683 ret
= process_client_msg(cmd_ctx
);
1685 /* TODO: Inform client somehow of the fatal error. At this point,
1686 * ret < 0 means that a malloc failed (ENOMEM). */
1687 /* Error detected but still accept command */
1688 clean_command_ctx(cmd_ctx
);
1692 DBG("Sending response (size: %d, retcode: %d)",
1693 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1694 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1696 ERR("Failed to send data back to client");
1699 clean_command_ctx(cmd_ctx
);
1701 /* End of transmission */
1711 * usage function on stderr
1713 static void usage(void)
1715 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1716 fprintf(stderr
, " -h, --help Display this usage.\n");
1717 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1718 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1719 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1720 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1721 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1722 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1723 fprintf(stderr
, " -V, --version Show version number.\n");
1724 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1725 fprintf(stderr
, " -q, --quiet No output at all.\n");
1726 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1730 * daemon argument parsing
1732 static int parse_args(int argc
, char **argv
)
1736 static struct option long_options
[] = {
1737 { "client-sock", 1, 0, 'c' },
1738 { "apps-sock", 1, 0, 'a' },
1739 { "kconsumerd-cmd-sock", 1, 0, 0 },
1740 { "kconsumerd-err-sock", 1, 0, 0 },
1741 { "daemonize", 0, 0, 'd' },
1742 { "sig-parent", 0, 0, 'S' },
1743 { "help", 0, 0, 'h' },
1744 { "group", 1, 0, 'g' },
1745 { "version", 0, 0, 'V' },
1746 { "quiet", 0, 0, 'q' },
1747 { "verbose", 0, 0, 'v' },
1752 int option_index
= 0;
1753 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1760 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1762 fprintf(stderr
, " with arg %s\n", optarg
);
1766 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1769 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1775 opt_tracing_group
= strdup(optarg
);
1781 fprintf(stdout
, "%s\n", VERSION
);
1787 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1790 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1799 /* Unknown option or other error.
1800 * Error is printed by getopt, just return */
1809 * init_daemon_socket
1811 * Creates the two needed socket by the daemon.
1812 * apps_sock - The communication socket for all UST apps.
1813 * client_sock - The communication of the cli tool (lttng).
1815 static int init_daemon_socket()
1820 old_umask
= umask(0);
1822 /* Create client tool unix socket */
1823 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
1824 if (client_sock
< 0) {
1825 ERR("Create unix sock failed: %s", client_unix_sock_path
);
1830 /* File permission MUST be 660 */
1831 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1833 ERR("Set file permissions failed: %s", client_unix_sock_path
);
1838 /* Create the application unix socket */
1839 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
1840 if (apps_sock
< 0) {
1841 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
1846 /* File permission MUST be 666 */
1847 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1849 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
1860 * check_existing_daemon
1862 * Check if the global socket is available.
1863 * If yes, error is returned.
1865 static int check_existing_daemon()
1869 ret
= access(client_unix_sock_path
, F_OK
);
1871 ret
= access(apps_unix_sock_path
, F_OK
);
1880 * Set the tracing group gid onto the client socket.
1882 * Race window between mkdir and chown is OK because we are going from
1883 * more permissive (root.root) to les permissive (root.tracing).
1885 static int set_permissions(void)
1890 /* Decide which group name to use */
1891 (opt_tracing_group
!= NULL
) ?
1892 (grp
= getgrnam(opt_tracing_group
)) :
1893 (grp
= getgrnam(default_tracing_group
));
1897 WARN("No tracing group detected");
1900 ERR("Missing tracing group. Aborting execution.");
1906 /* Set lttng run dir */
1907 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
1909 ERR("Unable to set group on " LTTNG_RUNDIR
);
1913 /* lttng client socket path */
1914 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
1916 ERR("Unable to set group on %s", client_unix_sock_path
);
1920 /* kconsumerd error socket path */
1921 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
1923 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
1927 DBG("All permissions are set");
1934 * create_lttng_rundir
1936 * Create the lttng run directory needed for all
1937 * global sockets and pipe.
1939 static int create_lttng_rundir(void)
1943 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
1945 if (errno
!= EEXIST
) {
1946 ERR("Unable to create " LTTNG_RUNDIR
);
1958 * set_kconsumerd_sockets
1960 * Setup sockets and directory needed by the kconsumerd
1961 * communication with the session daemon.
1963 static int set_kconsumerd_sockets(void)
1967 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1968 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1971 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1972 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1975 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1977 if (errno
!= EEXIST
) {
1978 ERR("Failed to create " KCONSUMERD_PATH
);
1984 /* Create the kconsumerd error unix socket */
1985 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1986 if (kconsumerd_err_sock
< 0) {
1987 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1992 /* File permission MUST be 660 */
1993 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1995 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2007 * Signal handler for the daemon
2009 static void sighandler(int sig
)
2013 DBG("SIGPIPE catched");
2016 DBG("SIGINT catched");
2020 DBG("SIGTERM catched");
2031 * set_signal_handler
2033 * Setup signal handler for :
2034 * SIGINT, SIGTERM, SIGPIPE
2036 static int set_signal_handler(void)
2039 struct sigaction sa
;
2042 if ((ret
= sigemptyset(&sigset
)) < 0) {
2043 perror("sigemptyset");
2047 sa
.sa_handler
= sighandler
;
2048 sa
.sa_mask
= sigset
;
2050 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2051 perror("sigaction");
2055 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2056 perror("sigaction");
2060 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2061 perror("sigaction");
2065 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2073 * Set open files limit to unlimited. This daemon can open a large number of
2074 * file descriptors in order to consumer multiple kernel traces.
2076 static void set_ulimit(void)
2081 lim
.rlim_cur
= 65535;
2082 lim
.rlim_max
= 65535;
2084 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2086 perror("failed to set open files limit");
2093 int main(int argc
, char **argv
)
2097 const char *home_path
;
2099 /* Parse arguments */
2101 if ((ret
= parse_args(argc
, argv
) < 0)) {
2114 /* Check if daemon is UID = 0 */
2115 is_root
= !getuid();
2118 ret
= create_lttng_rundir();
2123 if (strlen(apps_unix_sock_path
) == 0) {
2124 snprintf(apps_unix_sock_path
, PATH_MAX
,
2125 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2128 if (strlen(client_unix_sock_path
) == 0) {
2129 snprintf(client_unix_sock_path
, PATH_MAX
,
2130 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2133 ret
= set_kconsumerd_sockets();
2138 /* Setup kernel tracer */
2139 init_kernel_tracer();
2141 /* Set ulimit for open files */
2144 home_path
= get_home_dir();
2145 if (home_path
== NULL
) {
2146 ERR("Can't get HOME directory for sockets creation.\n \
2147 Please specify --socket PATH.");
2151 if (strlen(apps_unix_sock_path
) == 0) {
2152 snprintf(apps_unix_sock_path
, PATH_MAX
,
2153 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2156 /* Set the cli tool unix socket path */
2157 if (strlen(client_unix_sock_path
) == 0) {
2158 snprintf(client_unix_sock_path
, PATH_MAX
,
2159 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2163 DBG("Client socket path %s", client_unix_sock_path
);
2164 DBG("Application socket path %s", apps_unix_sock_path
);
2166 /* See if daemon already exist. If any of the two
2167 * socket needed by the daemon are present, this test fails
2169 if ((ret
= check_existing_daemon()) == 0) {
2170 ERR("Already running daemon.\n");
2171 /* We do not goto error because we must not
2172 * cleanup() because a daemon is already running.
2177 if (set_signal_handler() < 0) {
2181 /* Setup the needed unix socket */
2182 if (init_daemon_socket() < 0) {
2186 /* Set credentials to socket */
2187 if (is_root
&& (set_permissions() < 0)) {
2191 /* Get parent pid if -S, --sig-parent is specified. */
2192 if (opt_sig_parent
) {
2197 /* Create thread to manage the client socket */
2198 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2200 perror("pthread_create");
2204 /* Create thread to manage application socket */
2205 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2207 perror("pthread_create");
2211 ret
= pthread_join(client_thread
, &status
);
2213 perror("pthread_join");