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>
32 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <sys/resource.h>
39 #include <urcu/list.h> /* URCU list library (-lurcu) */
40 #include <lttng/lttng.h>
42 #include "liblttsessiondcomm.h"
43 #include "ltt-sessiond.h"
45 #include "kernel-ctl.h"
48 #include "traceable-app.h"
49 #include "lttng-kconsumerd.h"
50 #include "libustctl.h"
55 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
56 * with epoll on pipe and on other pipes/sockets for commands. Main
57 * simply waits on pthread join.
61 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
62 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
63 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
64 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
67 int opt_verbose
; /* Not static for lttngerr.h */
68 int opt_quiet
; /* Not static for lttngerr.h */
70 const char *opt_tracing_group
;
71 static int opt_sig_parent
;
72 static int opt_daemon
;
73 static int is_root
; /* Set to 1 if the daemon is running as root */
74 static pid_t ppid
; /* Parent PID for --sig-parent option */
75 static pid_t kconsumerd_pid
;
77 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
78 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
79 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
80 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
83 static int client_sock
;
85 static int kconsumerd_err_sock
;
86 static int kconsumerd_cmd_sock
;
87 static int kernel_tracer_fd
;
89 /* Pthread, Mutexes and Semaphores */
90 static pthread_t kconsumerd_thread
;
91 static pthread_t apps_thread
;
92 static pthread_t client_thread
;
93 static sem_t kconsumerd_sem
;
95 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
98 * teardown_kernel_session
100 * Complete teardown of a kernel session. This free all data structure
101 * related to a kernel session and update counter.
103 static void teardown_kernel_session(struct ltt_session
*session
)
105 if (session
->kernel_session
!= NULL
) {
106 DBG("Tearing down kernel session");
107 trace_destroy_kernel_session(session
->kernel_session
);
108 /* Extra precaution */
109 session
->kernel_session
= NULL
;
110 /* Decrement session count */
111 session
->kern_session_count
--;
118 * Cleanup the daemon on exit
120 static void cleanup()
124 struct ltt_session
*sess
;
129 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
130 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
133 /* Stopping all threads */
134 DBG("Terminating all threads");
135 pthread_cancel(client_thread
);
136 pthread_cancel(apps_thread
);
137 if (kconsumerd_pid
!= 0) {
138 pthread_cancel(kconsumerd_thread
);
141 DBG("Unlinking all unix socket");
142 unlink(client_unix_sock_path
);
143 unlink(apps_unix_sock_path
);
144 unlink(kconsumerd_err_unix_sock_path
);
146 DBG("Removing %s directory", LTTNG_RUNDIR
);
147 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
149 ERR("asprintf failed. Something is really wrong!");
152 /* Remove lttng run directory */
155 ERR("Unable to clean " LTTNG_RUNDIR
);
158 DBG("Cleaning up all session");
159 /* Cleanup ALL session */
160 cds_list_for_each_entry(sess
, <t_session_list
.head
, list
) {
161 teardown_kernel_session(sess
);
162 // TODO complete session cleanup (including UST)
165 DBG("Closing kernel fd");
166 close(kernel_tracer_fd
);
172 * Send data on a unix socket using the liblttsessiondcomm API.
174 * Return lttcomm error code.
176 static int send_unix_sock(int sock
, void *buf
, size_t len
)
178 /* Check valid length */
183 return lttcomm_send_unix_sock(sock
, buf
, len
);
189 * Free memory of a command context structure.
191 static void clean_command_ctx(struct command_ctx
*cmd_ctx
)
193 DBG("Clean command context structure %p", cmd_ctx
);
207 * send_kconsumerd_fds
209 * Send all stream fds of the kernel session to the consumer.
211 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
215 struct ltt_kernel_stream
*stream
;
216 struct ltt_kernel_channel
*chan
;
217 struct lttcomm_kconsumerd_header lkh
;
218 struct lttcomm_kconsumerd_msg lkm
;
220 nb_fd
= session
->stream_count_global
;
223 lkh
.payload_size
= (nb_fd
+ 1) * sizeof(struct lttcomm_kconsumerd_msg
);
224 lkh
.cmd_type
= ADD_STREAM
;
226 DBG("Sending kconsumerd header");
228 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
230 perror("send kconsumerd header");
234 DBG("Sending metadata stream fd");
236 /* Send metadata stream fd first */
237 lkm
.fd
= session
->metadata_stream_fd
;
238 lkm
.state
= ACTIVE_FD
;
239 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
240 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
242 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
244 perror("send kconsumerd fd");
248 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
249 cds_list_for_each_entry(stream
, &chan
->stream_list
.head
, list
) {
251 lkm
.state
= stream
->state
;
252 lkm
.max_sb_size
= chan
->channel
->attr
.subbuf_size
;
253 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
255 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
257 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
259 perror("send kconsumerd fd");
265 DBG("Kconsumerd fds sent");
276 * Create the trace output directory.
278 static int create_trace_dir(struct ltt_kernel_session
*session
)
281 struct ltt_kernel_channel
*chan
;
283 /* Create all channel directories */
284 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
285 DBG("Creating trace directory at %s", chan
->pathname
);
286 ret
= mkdir_recursive(chan
->pathname
, S_IRWXU
| S_IRWXG
);
289 ERR("Trace directory creation error");
304 * Return a socket connected to the libust communication socket
305 * of the application identified by the pid.
307 * If the pid is not found in the traceable list,
308 * return -1 to indicate error.
310 static int ust_connect_app(pid_t pid
)
313 struct ltt_traceable_app
*lta
;
315 DBG("Connect to application pid %d", pid
);
317 lta
= find_app_by_pid(pid
);
320 DBG("Application pid %d not found", pid
);
324 sock
= ustctl_connect_pid(lta
->pid
);
326 ERR("Fail connecting to the PID %d", pid
);
335 * Notify apps by writing 42 to a named pipe using name.
336 * Every applications waiting for a ltt-sessiond will be notified
337 * and re-register automatically to the session daemon.
339 * Return open or write error value.
341 static int notify_apps(const char *name
)
346 DBG("Notify the global application pipe");
348 /* Try opening the global pipe */
349 fd
= open(name
, O_WRONLY
);
354 /* Notify by writing on the pipe */
355 ret
= write(fd
, "42", 2);
367 * Setup the outgoing data buffer for the response (llm) by allocating the
368 * right amount of memory and copying the original information from the lsm
371 * Return total size of the buffer pointed by buf.
373 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
379 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
380 if (cmd_ctx
->llm
== NULL
) {
386 /* Copy common data */
387 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
388 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->pid
;
390 cmd_ctx
->llm
->data_size
= size
;
391 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
400 * thread_manage_kconsumerd
402 * This thread manage the kconsumerd error sent
403 * back to the session daemon.
405 static void *thread_manage_kconsumerd(void *data
)
408 enum lttcomm_return_code code
;
410 DBG("[thread] Manage kconsumerd started");
412 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
417 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
422 /* Getting status code from kconsumerd */
423 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
428 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
429 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
430 if (kconsumerd_cmd_sock
< 0) {
431 sem_post(&kconsumerd_sem
);
432 perror("kconsumerd connect");
435 /* Signal condition to tell that the kconsumerd is ready */
436 sem_post(&kconsumerd_sem
);
437 DBG("Kconsumerd command socket ready");
439 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
440 lttcomm_get_readable_code(-code
));
444 /* Wait for any kconsumerd error */
445 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
447 ERR("Kconsumerd closed the command socket");
451 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
455 DBG("Kconsumerd thread dying");
462 * This thread manage the application socket communication
464 static void *thread_manage_apps(void *data
)
468 /* TODO: Something more elegant is needed but fine for now */
469 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
470 * for 32-bit vs 64-bit compat processes. */
471 /* replicate in ust with version number */
473 int reg
; /* 1:register, 0:unregister */
478 DBG("[thread] Manage apps started");
480 ret
= lttcomm_listen_unix_sock(apps_sock
);
485 /* Notify all applications to register */
486 notify_apps(default_global_apps_pipe
);
489 DBG("Accepting application registration");
490 /* Blocking call, waiting for transmission */
491 sock
= lttcomm_accept_unix_sock(apps_sock
);
496 /* Basic recv here to handle the very simple data
497 * that the libust send to register (reg_msg).
499 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
505 /* Add application to the global traceable list */
506 if (reg_msg
.reg
== 1) {
508 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
510 /* register_traceable_app only return an error with
511 * ENOMEM. At this point, we better stop everything.
517 unregister_traceable_app(reg_msg
.pid
);
527 * spawn_kconsumerd_thread
529 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
530 * exec or it will fails.
532 static int spawn_kconsumerd_thread(void)
536 /* Setup semaphore */
537 sem_init(&kconsumerd_sem
, 0, 0);
539 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
541 perror("pthread_create kconsumerd");
545 /* Wait for the kconsumerd thread to be ready */
546 sem_wait(&kconsumerd_sem
);
548 if (kconsumerd_pid
== 0) {
549 ERR("Kconsumerd did not start");
556 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
563 * Fork and exec a kernel consumer daemon (kconsumerd).
565 * NOTE: It is very important to fork a kconsumerd BEFORE opening any kernel
566 * file descriptor using the libkernelctl or kernel-ctl functions. So, a
567 * kernel consumer MUST only be spawned before creating a kernel session.
569 * Return pid if successful else -1.
571 static pid_t
spawn_kconsumerd(void)
576 DBG("Spawning kconsumerd");
583 execlp("kconsumerd", "kconsumerd", "--verbose", NULL
);
585 perror("kernel start consumer exec");
588 } else if (pid
> 0) {
592 perror("kernel start consumer fork");
604 * Spawn the kconsumerd daemon and session daemon thread.
606 static int start_kconsumerd(void)
610 pthread_mutex_lock(&kconsumerd_pid_mutex
);
611 if (kconsumerd_pid
!= 0) {
615 ret
= spawn_kconsumerd();
617 ERR("Spawning kconsumerd failed");
618 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
619 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
623 /* Setting up the global kconsumerd_pid */
624 kconsumerd_pid
= ret
;
625 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
627 DBG("Kconsumerd pid %d", ret
);
629 DBG("Spawning kconsumerd thread");
630 ret
= spawn_kconsumerd_thread();
632 ERR("Fatal error spawning kconsumerd thread");
637 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
647 * Setup necessary data for kernel tracer action.
649 static void init_kernel_tracer(void)
651 /* Set the global kernel tracer fd */
652 kernel_tracer_fd
= open(DEFAULT_KERNEL_TRACER_PATH
, O_RDWR
);
653 if (kernel_tracer_fd
< 0) {
654 WARN("No kernel tracer available");
655 kernel_tracer_fd
= 0;
658 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
664 * Start tracing by creating trace directory and sending FDs to the kernel
667 static int start_kernel_trace(struct ltt_kernel_session
*session
)
671 /* Create trace directory */
672 ret
= create_trace_dir(session
);
674 if (ret
== -EEXIST
) {
675 ret
= LTTCOMM_KERN_DIR_EXIST
;
677 ret
= LTTCOMM_KERN_DIR_FAIL
;
682 if (session
->kconsumer_fds_sent
== 0) {
683 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
685 ERR("Send kconsumerd fds failed");
686 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
690 session
->kconsumer_fds_sent
= 1;
698 * init_default_channel
700 * Allocate a channel structure and fill it.
702 static struct lttng_channel
*init_default_channel(void)
704 struct lttng_channel
*chan
;
706 chan
= malloc(sizeof(struct lttng_channel
));
708 perror("init channel malloc");
712 if (snprintf(chan
->name
, NAME_MAX
, DEFAULT_CHANNEL_NAME
) < 0) {
713 perror("snprintf defautl channel name");
717 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
718 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
719 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
720 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
721 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
722 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
729 * create_kernel_session
731 * Create a kernel tracer session then create the default channel.
733 static int create_kernel_session(struct ltt_session
*session
)
736 struct lttng_channel
*chan
;
738 DBG("Creating kernel session");
740 ret
= kernel_create_session(session
, kernel_tracer_fd
);
742 ret
= LTTCOMM_KERN_SESS_FAIL
;
746 chan
= init_default_channel();
752 DBG("Creating default kernel channel %s", DEFAULT_CHANNEL_NAME
);
754 ret
= kernel_create_channel(session
->kernel_session
, chan
, session
->path
);
756 ret
= LTTCOMM_KERN_CHAN_FAIL
;
767 * Process the command requested by the lttng client within the command
768 * context structure. This function make sure that the return structure (llm)
769 * is set and ready for transmission before returning.
771 * Return any error encountered or 0 for success.
773 static int process_client_msg(struct command_ctx
*cmd_ctx
)
777 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
779 /* Listing commands don't need a session */
780 switch (cmd_ctx
->lsm
->cmd_type
) {
781 case LTTNG_CREATE_SESSION
:
782 case LTTNG_LIST_SESSIONS
:
783 case LTTNG_LIST_EVENTS
:
784 case LTTNG_KERNEL_LIST_EVENTS
:
785 case LTTNG_LIST_TRACEABLE_APPS
:
788 DBG("Getting session %s by name", cmd_ctx
->lsm
->session_name
);
789 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session_name
);
790 if (cmd_ctx
->session
== NULL
) {
791 /* If session name not found */
792 if (cmd_ctx
->lsm
->session_name
!= NULL
) {
793 ret
= LTTCOMM_SESS_NOT_FOUND
;
794 } else { /* If no session name specified */
795 ret
= LTTCOMM_SELECT_SESS
;
803 * Check kernel command for kernel session.
805 switch (cmd_ctx
->lsm
->cmd_type
) {
806 case LTTNG_KERNEL_ADD_CONTEXT
:
807 case LTTNG_KERNEL_CREATE_CHANNEL
:
808 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
809 case LTTNG_KERNEL_DISABLE_CHANNEL
:
810 case LTTNG_KERNEL_DISABLE_EVENT
:
811 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
812 case LTTNG_KERNEL_ENABLE_CHANNEL
:
813 case LTTNG_KERNEL_ENABLE_EVENT
:
814 case LTTNG_KERNEL_LIST_EVENTS
:
815 /* Kernel tracer check */
816 if (kernel_tracer_fd
== 0) {
817 init_kernel_tracer();
818 if (kernel_tracer_fd
== 0) {
819 ret
= LTTCOMM_KERN_NA
;
824 /* Need a session for kernel command */
825 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_KERNEL_LIST_EVENTS
&&
826 cmd_ctx
->session
->kernel_session
== NULL
) {
828 ret
= create_kernel_session(cmd_ctx
->session
);
830 ret
= LTTCOMM_KERN_SESS_FAIL
;
834 /* Start the kernel consumer daemon */
835 if (kconsumerd_pid
== 0) {
836 ret
= start_kconsumerd();
844 /* Connect to ust apps if available pid */
845 if (cmd_ctx
->lsm
->pid
> 0) {
846 /* Connect to app using ustctl API */
847 cmd_ctx
->ust_sock
= ust_connect_app(cmd_ctx
->lsm
->pid
);
848 if (cmd_ctx
->ust_sock
< 0) {
849 ret
= LTTCOMM_NO_TRACEABLE
;
854 /* Process by command type */
855 switch (cmd_ctx
->lsm
->cmd_type
) {
856 case LTTNG_KERNEL_ADD_CONTEXT
:
858 int found
= 0, no_event
= 0;
859 struct ltt_kernel_channel
*chan
;
860 struct ltt_kernel_event
*event
;
862 /* Setup lttng message with no payload */
863 ret
= setup_lttng_msg(cmd_ctx
, 0);
868 /* Check if event name is given */
869 if (strlen(cmd_ctx
->lsm
->u
.context
.event_name
) == 0) {
873 if (strlen(cmd_ctx
->lsm
->u
.context
.channel_name
) == 0) {
874 /* Go over all channels */
875 DBG("Adding context to all channels");
876 cds_list_for_each_entry(chan
,
877 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
879 ret
= kernel_add_channel_context(chan
,
880 &cmd_ctx
->lsm
->u
.context
.ctx
);
885 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
887 ret
= kernel_add_event_context(event
,
888 &cmd_ctx
->lsm
->u
.context
.ctx
);
890 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
899 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.context
.channel_name
,
900 cmd_ctx
->session
->kernel_session
);
902 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
907 ret
= kernel_add_channel_context(chan
,
908 &cmd_ctx
->lsm
->u
.context
.ctx
);
910 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
914 event
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.context
.event_name
, chan
);
916 ret
= kernel_add_event_context(event
,
917 &cmd_ctx
->lsm
->u
.context
.ctx
);
919 ret
= LTTCOMM_KERN_CONTEXT_FAIL
;
926 if (!found
&& !no_event
) {
927 ret
= LTTCOMM_NO_EVENT
;
934 case LTTNG_KERNEL_CREATE_CHANNEL
:
936 /* Setup lttng message with no payload */
937 ret
= setup_lttng_msg(cmd_ctx
, 0);
943 DBG("Creating kernel channel");
945 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
946 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
948 ret
= LTTCOMM_KERN_CHAN_FAIL
;
955 case LTTNG_KERNEL_DISABLE_CHANNEL
:
957 struct ltt_kernel_channel
*chan
;
959 /* Setup lttng message with no payload */
960 ret
= setup_lttng_msg(cmd_ctx
, 0);
965 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
966 cmd_ctx
->session
->kernel_session
);
968 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
970 } else if (chan
->enabled
== 1) {
971 ret
= kernel_disable_channel(chan
);
974 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
980 kernel_wait_quiescent(kernel_tracer_fd
);
984 case LTTNG_KERNEL_DISABLE_EVENT
:
986 struct ltt_kernel_channel
*chan
;
987 struct ltt_kernel_event
*ev
;
989 /* Setup lttng message with no payload */
990 ret
= setup_lttng_msg(cmd_ctx
, 0);
995 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
996 cmd_ctx
->session
->kernel_session
);
998 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1002 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, chan
);
1004 DBG("Disabling kernel event %s for channel %s.",
1005 cmd_ctx
->lsm
->u
.disable
.name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1006 ret
= kernel_disable_event(ev
);
1008 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1013 kernel_wait_quiescent(kernel_tracer_fd
);
1017 case LTTNG_KERNEL_DISABLE_ALL_EVENT
:
1019 struct ltt_kernel_channel
*chan
;
1020 struct ltt_kernel_event
*ev
;
1022 /* Setup lttng message with no payload */
1023 ret
= setup_lttng_msg(cmd_ctx
, 0);
1028 DBG("Disabling all enabled kernel events");
1030 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1031 cmd_ctx
->session
->kernel_session
);
1033 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1037 /* For each event in the kernel session */
1038 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1039 DBG("Disabling kernel event %s for channel %s.",
1040 ev
->event
->name
, cmd_ctx
->lsm
->u
.disable
.channel_name
);
1041 ret
= kernel_disable_event(ev
);
1047 /* Quiescent wait after event disable */
1048 kernel_wait_quiescent(kernel_tracer_fd
);
1052 case LTTNG_KERNEL_ENABLE_CHANNEL
:
1054 struct ltt_kernel_channel
*chan
;
1056 /* Setup lttng message with no payload */
1057 ret
= setup_lttng_msg(cmd_ctx
, 0);
1062 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1063 cmd_ctx
->session
->kernel_session
);
1065 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1067 } else if (chan
->enabled
== 0) {
1068 ret
= kernel_enable_channel(chan
);
1070 if (ret
!= EEXIST
) {
1071 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1077 kernel_wait_quiescent(kernel_tracer_fd
);
1081 case LTTNG_KERNEL_ENABLE_EVENT
:
1083 struct ltt_kernel_channel
*chan
;
1084 struct ltt_kernel_event
*ev
;
1086 /* Setup lttng message with no payload */
1087 ret
= setup_lttng_msg(cmd_ctx
, 0);
1092 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1093 cmd_ctx
->session
->kernel_session
);
1095 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1099 ev
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
);
1101 DBG("Creating kernel event %s for channel %s.",
1102 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1103 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, chan
);
1105 DBG("Enabling kernel event %s for channel %s.",
1106 cmd_ctx
->lsm
->u
.enable
.event
.name
, chan
->channel
->name
);
1107 ret
= kernel_enable_event(ev
);
1111 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1115 kernel_wait_quiescent(kernel_tracer_fd
);
1119 case LTTNG_KERNEL_ENABLE_ALL_EVENT
:
1122 char *event_list
, *event
, *ptr
;
1123 struct ltt_kernel_channel
*chan
;
1124 struct ltt_kernel_event
*ev
;
1125 struct lttng_event ev_attr
;
1127 /* Setup lttng message with no payload */
1128 ret
= setup_lttng_msg(cmd_ctx
, 0);
1133 DBG("Enabling all kernel event");
1135 chan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1136 cmd_ctx
->session
->kernel_session
);
1138 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1142 /* For each event in the kernel session */
1143 cds_list_for_each_entry(ev
, &chan
->events_list
.head
, list
) {
1144 DBG("Enabling kernel event %s for channel %s.",
1145 ev
->event
->name
, chan
->channel
->name
);
1146 ret
= kernel_enable_event(ev
);
1152 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1154 ret
= LTTCOMM_KERN_LIST_FAIL
;
1159 while ((size
= sscanf(ptr
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
1160 ev
= get_kernel_event_by_name(event
, chan
);
1162 strncpy(ev_attr
.name
, event
, LTTNG_SYM_NAME_LEN
);
1163 /* Default event type for enable all */
1164 ev_attr
.type
= LTTNG_EVENT_TRACEPOINTS
;
1165 /* Enable each single tracepoint event */
1166 ret
= kernel_create_event(&ev_attr
, chan
);
1168 /* Ignore error here and continue */
1172 /* Move pointer to the next line */
1179 /* Quiescent wait after event enable */
1180 kernel_wait_quiescent(kernel_tracer_fd
);
1184 case LTTNG_KERNEL_LIST_EVENTS
:
1189 DBG("Listing kernel events");
1191 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1193 ret
= LTTCOMM_KERN_LIST_FAIL
;
1198 * Setup lttng message with payload size set to the event list size in
1199 * bytes and then copy list into the llm payload.
1201 ret
= setup_lttng_msg(cmd_ctx
, size
);
1206 /* Copy event list into message payload */
1207 memcpy(cmd_ctx
->llm
->payload
, event_list
, size
);
1214 case LTTNG_START_TRACE
:
1216 struct ltt_kernel_channel
*chan
;
1218 /* Setup lttng message with no payload */
1219 ret
= setup_lttng_msg(cmd_ctx
, 0);
1224 /* Kernel tracing */
1225 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1226 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1227 DBG("Open kernel metadata");
1228 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1229 cmd_ctx
->session
->path
);
1231 ret
= LTTCOMM_KERN_META_FAIL
;
1236 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1237 DBG("Opening kernel metadata stream");
1238 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1239 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1241 ERR("Kernel create metadata stream failed");
1242 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1248 /* For each channel */
1249 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1250 if (chan
->stream_count
== 0) {
1251 ret
= kernel_open_channel_stream(chan
);
1253 ERR("Kernel create channel stream failed");
1254 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1257 /* Update the stream global counter */
1258 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1262 DBG("Start kernel tracing");
1263 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1265 ERR("Kernel start session failed");
1266 ret
= LTTCOMM_KERN_START_FAIL
;
1270 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1272 ret
= LTTCOMM_KERN_START_FAIL
;
1276 /* Quiescent wait after starting trace */
1277 kernel_wait_quiescent(kernel_tracer_fd
);
1280 /* TODO: Start all UST traces */
1285 case LTTNG_STOP_TRACE
:
1287 struct ltt_kernel_channel
*chan
;
1288 /* Setup lttng message with no payload */
1289 ret
= setup_lttng_msg(cmd_ctx
, 0);
1295 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1296 DBG("Stop kernel tracing");
1298 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1300 ERR("Kernel metadata flush failed");
1303 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1304 ret
= kernel_flush_buffer(chan
);
1306 ERR("Kernel flush buffer error");
1310 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1312 ERR("Kernel stop session failed");
1313 ret
= LTTCOMM_KERN_STOP_FAIL
;
1317 /* Quiescent wait after stopping trace */
1318 kernel_wait_quiescent(kernel_tracer_fd
);
1321 /* TODO : User-space tracer */
1326 case LTTNG_CREATE_SESSION
:
1328 /* Setup lttng message with no payload */
1329 ret
= setup_lttng_msg(cmd_ctx
, 0);
1334 ret
= create_session(cmd_ctx
->lsm
->session_name
, cmd_ctx
->lsm
->path
);
1336 if (ret
== -EEXIST
) {
1337 ret
= LTTCOMM_EXIST_SESS
;
1339 ret
= LTTCOMM_FATAL
;
1347 case LTTNG_DESTROY_SESSION
:
1349 /* Setup lttng message with no payload */
1350 ret
= setup_lttng_msg(cmd_ctx
, 0);
1355 /* Clean kernel session teardown */
1356 teardown_kernel_session(cmd_ctx
->session
);
1358 ret
= destroy_session(cmd_ctx
->lsm
->session_name
);
1360 ret
= LTTCOMM_FATAL
;
1368 case LTTNG_LIST_TRACES:
1370 unsigned int trace_count;
1372 trace_count = get_trace_count_per_session(cmd_ctx->session);
1373 if (trace_count == 0) {
1374 ret = LTTCOMM_NO_TRACE;
1378 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_trace) * trace_count);
1383 get_traces_per_session(cmd_ctx->session,
1384 (struct lttng_trace *)(cmd_ctx->llm->payload));
1391 case UST_CREATE_TRACE:
1393 ret = setup_lttng_msg(cmd_ctx, 0);
1398 ret = ust_create_trace(cmd_ctx);
1405 case LTTNG_LIST_TRACEABLE_APPS
:
1407 unsigned int app_count
;
1409 app_count
= get_app_count();
1410 DBG("Traceable application count : %d", app_count
);
1411 if (app_count
== 0) {
1412 ret
= LTTCOMM_NO_APPS
;
1416 ret
= setup_lttng_msg(cmd_ctx
, sizeof(pid_t
) * app_count
);
1421 get_app_list_pids((pid_t
*)(cmd_ctx
->llm
->payload
));
1427 case UST_START_TRACE:
1429 ret = setup_lttng_msg(cmd_ctx, 0);
1434 ret = ust_start_trace(cmd_ctx);
1440 case UST_STOP_TRACE:
1442 ret = setup_lttng_msg(cmd_ctx, 0);
1447 ret = ust_stop_trace(cmd_ctx);
1454 case LTTNG_LIST_SESSIONS
:
1456 unsigned int session_count
;
1458 session_count
= get_session_count();
1459 if (session_count
== 0) {
1460 ret
= LTTCOMM_NO_SESSION
;
1464 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) * session_count
);
1469 get_lttng_session((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1475 /* Undefined command */
1476 ret
= setup_lttng_msg(cmd_ctx
, 0);
1485 /* Set return code */
1486 cmd_ctx
->llm
->ret_code
= ret
;
1491 if (cmd_ctx
->llm
== NULL
) {
1492 DBG("Missing llm structure. Allocating one.");
1493 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
1497 /* Notify client of error */
1498 cmd_ctx
->llm
->ret_code
= ret
;
1505 * thread_manage_clients
1507 * This thread manage all clients request using the unix
1508 * client socket for communication.
1510 static void *thread_manage_clients(void *data
)
1513 struct command_ctx
*cmd_ctx
;
1515 DBG("[thread] Manage client started");
1517 ret
= lttcomm_listen_unix_sock(client_sock
);
1522 /* Notify parent pid that we are ready
1523 * to accept command for client side.
1525 if (opt_sig_parent
) {
1526 kill(ppid
, SIGCHLD
);
1530 /* Blocking call, waiting for transmission */
1531 DBG("Accepting client command ...");
1532 sock
= lttcomm_accept_unix_sock(client_sock
);
1537 /* Allocate context command to process the client request */
1538 cmd_ctx
= malloc(sizeof(struct command_ctx
));
1540 /* Allocate data buffer for reception */
1541 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
1542 cmd_ctx
->llm
= NULL
;
1543 cmd_ctx
->session
= NULL
;
1546 * Data is received from the lttng client. The struct
1547 * lttcomm_session_msg (lsm) contains the command and data request of
1550 DBG("Receiving data from client ...");
1551 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
1556 // TODO: Validate cmd_ctx including sanity check for security purpose.
1559 * This function dispatch the work to the kernel or userspace tracer
1560 * libs and fill the lttcomm_lttng_msg data structure of all the needed
1561 * informations for the client. The command context struct contains
1562 * everything this function may needs.
1564 ret
= process_client_msg(cmd_ctx
);
1566 /* TODO: Inform client somehow of the fatal error. At this point,
1567 * ret < 0 means that a malloc failed (ENOMEM). */
1568 /* Error detected but still accept command */
1569 clean_command_ctx(cmd_ctx
);
1573 DBG("Sending response (size: %d, retcode: %d)",
1574 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
1575 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
1577 ERR("Failed to send data back to client");
1580 clean_command_ctx(cmd_ctx
);
1582 /* End of transmission */
1592 * usage function on stderr
1594 static void usage(void)
1596 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
1597 fprintf(stderr
, " -h, --help Display this usage.\n");
1598 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
1599 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
1600 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
1601 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
1602 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
1603 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
1604 fprintf(stderr
, " -V, --version Show version number.\n");
1605 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
1606 fprintf(stderr
, " -q, --quiet No output at all.\n");
1607 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
1611 * daemon argument parsing
1613 static int parse_args(int argc
, char **argv
)
1617 static struct option long_options
[] = {
1618 { "client-sock", 1, 0, 'c' },
1619 { "apps-sock", 1, 0, 'a' },
1620 { "kconsumerd-cmd-sock", 1, 0, 0 },
1621 { "kconsumerd-err-sock", 1, 0, 0 },
1622 { "daemonize", 0, 0, 'd' },
1623 { "sig-parent", 0, 0, 'S' },
1624 { "help", 0, 0, 'h' },
1625 { "group", 1, 0, 'g' },
1626 { "version", 0, 0, 'V' },
1627 { "quiet", 0, 0, 'q' },
1628 { "verbose", 0, 0, 'v' },
1633 int option_index
= 0;
1634 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
1641 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
1643 fprintf(stderr
, " with arg %s\n", optarg
);
1647 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1650 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1656 opt_tracing_group
= strdup(optarg
);
1662 fprintf(stdout
, "%s\n", VERSION
);
1668 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1671 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
1680 /* Unknown option or other error.
1681 * Error is printed by getopt, just return */
1690 * init_daemon_socket
1692 * Creates the two needed socket by the daemon.
1693 * apps_sock - The communication socket for all UST apps.
1694 * client_sock - The communication of the cli tool (lttng).
1696 static int init_daemon_socket()
1701 old_umask
= umask(0);
1703 /* Create client tool unix socket */
1704 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
1705 if (client_sock
< 0) {
1706 ERR("Create unix sock failed: %s", client_unix_sock_path
);
1711 /* File permission MUST be 660 */
1712 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1714 ERR("Set file permissions failed: %s", client_unix_sock_path
);
1719 /* Create the application unix socket */
1720 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
1721 if (apps_sock
< 0) {
1722 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
1727 /* File permission MUST be 666 */
1728 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
1730 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
1741 * check_existing_daemon
1743 * Check if the global socket is available.
1744 * If yes, error is returned.
1746 static int check_existing_daemon()
1750 ret
= access(client_unix_sock_path
, F_OK
);
1752 ret
= access(apps_unix_sock_path
, F_OK
);
1761 * Set the tracing group gid onto the client socket.
1763 * Race window between mkdir and chown is OK because we are going from
1764 * less permissive (root.root) to more permissive (root.tracing).
1766 static int set_permissions(void)
1771 /* Decide which group name to use */
1772 (opt_tracing_group
!= NULL
) ?
1773 (grp
= getgrnam(opt_tracing_group
)) :
1774 (grp
= getgrnam(default_tracing_group
));
1777 ERR("Missing tracing group. Aborting execution.\n");
1782 /* Set lttng run dir */
1783 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
1785 ERR("Unable to set group on " LTTNG_RUNDIR
);
1789 /* lttng client socket path */
1790 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
1792 ERR("Unable to set group on %s", client_unix_sock_path
);
1796 /* kconsumerd error socket path */
1797 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
1799 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
1803 DBG("All permissions are set");
1810 * create_lttng_rundir
1812 * Create the lttng run directory needed for all
1813 * global sockets and pipe.
1815 static int create_lttng_rundir(void)
1819 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
1821 if (errno
!= EEXIST
) {
1822 ERR("Unable to create " LTTNG_RUNDIR
);
1834 * set_kconsumerd_sockets
1836 * Setup sockets and directory needed by the kconsumerd
1837 * communication with the session daemon.
1839 static int set_kconsumerd_sockets(void)
1843 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
1844 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
1847 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
1848 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
1851 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
1853 if (errno
!= EEXIST
) {
1854 ERR("Failed to create " KCONSUMERD_PATH
);
1860 /* Create the kconsumerd error unix socket */
1861 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
1862 if (kconsumerd_err_sock
< 0) {
1863 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
1868 /* File permission MUST be 660 */
1869 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1871 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
1883 * Signal handler for the daemon
1885 static void sighandler(int sig
)
1889 DBG("SIGPIPE catched");
1892 DBG("SIGINT catched");
1896 DBG("SIGTERM catched");
1907 * set_signal_handler
1909 * Setup signal handler for :
1910 * SIGINT, SIGTERM, SIGPIPE
1912 static int set_signal_handler(void)
1915 struct sigaction sa
;
1918 if ((ret
= sigemptyset(&sigset
)) < 0) {
1919 perror("sigemptyset");
1923 sa
.sa_handler
= sighandler
;
1924 sa
.sa_mask
= sigset
;
1926 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1927 perror("sigaction");
1931 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1932 perror("sigaction");
1936 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
1937 perror("sigaction");
1941 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
1949 * Set open files limit to unlimited. This daemon can open a large number of
1950 * file descriptors in order to consumer multiple kernel traces.
1952 static void set_ulimit(void)
1957 lim
.rlim_cur
= 65535;
1958 lim
.rlim_max
= 65535;
1960 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
1962 perror("failed to set open files limit");
1969 int main(int argc
, char **argv
)
1973 const char *home_path
;
1975 /* Parse arguments */
1977 if ((ret
= parse_args(argc
, argv
) < 0)) {
1990 /* Check if daemon is UID = 0 */
1991 is_root
= !getuid();
1993 /* Set all sockets path */
1995 ret
= create_lttng_rundir();
2000 if (strlen(apps_unix_sock_path
) == 0) {
2001 snprintf(apps_unix_sock_path
, PATH_MAX
,
2002 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2005 if (strlen(client_unix_sock_path
) == 0) {
2006 snprintf(client_unix_sock_path
, PATH_MAX
,
2007 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2010 ret
= set_kconsumerd_sockets();
2015 /* Setup kernel tracer */
2016 init_kernel_tracer();
2018 /* Set ulimit for open files */
2021 home_path
= get_home_dir();
2022 if (home_path
== NULL
) {
2023 ERR("Can't get HOME directory for sockets creation.\n \
2024 Please specify --socket PATH.");
2028 if (strlen(apps_unix_sock_path
) == 0) {
2029 snprintf(apps_unix_sock_path
, PATH_MAX
,
2030 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2033 /* Set the cli tool unix socket path */
2034 if (strlen(client_unix_sock_path
) == 0) {
2035 snprintf(client_unix_sock_path
, PATH_MAX
,
2036 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2040 DBG("Client socket path %s", client_unix_sock_path
);
2041 DBG("Application socket path %s", apps_unix_sock_path
);
2043 /* See if daemon already exist. If any of the two
2044 * socket needed by the daemon are present, this test fails
2046 if ((ret
= check_existing_daemon()) == 0) {
2047 ERR("Already running daemon.\n");
2048 /* We do not goto error because we must not
2049 * cleanup() because a daemon is already running.
2054 if (set_signal_handler() < 0) {
2058 /* Setup the needed unix socket */
2059 if (init_daemon_socket() < 0) {
2063 /* Set credentials to socket */
2064 if (is_root
&& (set_permissions() < 0)) {
2068 /* Get parent pid if -S, --sig-parent is specified. */
2069 if (opt_sig_parent
) {
2074 /* Create thread to manage the client socket */
2075 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2077 perror("pthread_create");
2081 /* Create thread to manage application socket */
2082 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2084 perror("pthread_create");
2088 ret
= pthread_join(client_thread
, &status
);
2090 perror("pthread_join");