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.
31 #include <sys/socket.h>
33 #include <sys/types.h>
36 #include <urcu/list.h> /* URCU list library (-lurcu) */
37 #include <ust/ustctl.h> /* UST control lib (-lust) */
38 #include <lttng/lttng.h>
40 #include "liblttsessiondcomm.h"
41 #include "ltt-sessiond.h"
45 #include "traceable-app.h"
49 * teardown: signal SIGTERM handler -> write into pipe. Threads waits
50 * with epoll on pipe and on other pipes/sockets for commands. Main
51 * simply waits on pthread join.
55 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
56 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
57 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
58 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
60 /* Static functions */
61 static int check_existing_daemon(void);
62 static int ust_connect_app(pid_t pid
);
63 static int init_daemon_socket(void);
64 static int notify_apps(const char* name
);
65 static int process_client_msg(int sock
, struct lttcomm_session_msg
*);
66 static int send_unix_sock(int sock
, void *buf
, size_t len
);
67 static int set_signal_handler(void);
68 static int set_permissions(void);
69 static int setup_data_buffer(char **buf
, size_t size
, struct lttcomm_lttng_header
*llh
);
70 static int create_lttng_rundir(void);
71 static int set_kconsumerd_sockets(void);
72 static void cleanup(void);
73 static void copy_common_data(struct lttcomm_lttng_header
*llh
, struct lttcomm_session_msg
*lsm
);
74 static void sighandler(int sig
);
76 static void *thread_manage_clients(void *data
);
77 static void *thread_manage_apps(void *data
);
78 static void *thread_manage_kconsumerd(void *data
);
84 const char *opt_tracing_group
;
85 static int opt_sig_parent
;
86 static int opt_daemon
;
87 static int is_root
; /* Set to 1 if the daemon is running as root */
90 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
91 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
92 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
93 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
95 static int client_sock
;
97 static int kconsumerd_err_sock
;
99 /* Extern in session.h */
100 struct ltt_session
*current_session
;
103 * thread_manage_kconsumerd
105 * This thread manage the kconsumerd error sent
106 * back to the session daemon.
108 static void *thread_manage_kconsumerd(void *data
)
112 DBG("[thread] Manage kconsumerd started");
114 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
119 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
125 //ret = lttcomm_recv_unix_sock(sock, &lsm, sizeof(lsm));
127 /* TODO: Consumerd died? */
139 * This thread manage the application socket communication
141 static void *thread_manage_apps(void *data
)
145 /* TODO: Something more elegant is needed but fine for now */
146 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
147 * for 32-bit vs 64-bit compat processes. */
148 /* replicate in ust with version number */
150 int reg
; /* 1:register, 0:unregister */
155 DBG("[thread] Manage apps started");
157 ret
= lttcomm_listen_unix_sock(apps_sock
);
162 /* Notify all applications to register */
163 notify_apps(default_global_apps_pipe
);
166 /* Blocking call, waiting for transmission */
167 sock
= lttcomm_accept_unix_sock(apps_sock
);
172 /* Basic recv here to handle the very simple data
173 * that the libust send to register (reg_msg).
175 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
181 /* Add application to the global traceable list */
182 if (reg_msg
.reg
== 1) {
184 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
186 /* register_traceable_app only return an error with
187 * ENOMEM. At this point, we better stop everything.
193 unregister_traceable_app(reg_msg
.pid
);
203 * thread_manage_clients
205 * This thread manage all clients request using the unix
206 * client socket for communication.
208 static void *thread_manage_clients(void *data
)
211 struct lttcomm_session_msg lsm
;
213 DBG("[thread] Manage client started");
215 ret
= lttcomm_listen_unix_sock(client_sock
);
220 /* Notify parent pid that we are ready
221 * to accept command for client side.
223 if (opt_sig_parent
) {
228 /* Blocking call, waiting for transmission */
229 sock
= lttcomm_accept_unix_sock(client_sock
);
235 * Data is received from the lttng client. The struct
236 * lttcomm_session_msg (lsm) contains the command and data
237 * request of the client.
239 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
244 /* This function dispatch the work to the LTTng or UST libs
245 * and then sends back the response to the client. This is needed
246 * because there might be more then one lttcomm_lttng_header to
247 * send out so process_client_msg do both jobs.
249 ret
= process_client_msg(sock
, &lsm
);
251 /* Error detected but still accept command */
263 * Send data on a unix socket using the liblttsessiondcomm API.
265 * Return lttcomm error code.
267 static int send_unix_sock(int sock
, void *buf
, size_t len
)
269 /* Check valid length */
274 return lttcomm_send_unix_sock(sock
, buf
, len
);
280 * Return a socket connected to the libust communication socket
281 * of the application identified by the pid.
283 * If the pid is not found in the traceable list,
284 * return -1 to indicate error.
286 static int ust_connect_app(pid_t pid
)
289 struct ltt_traceable_app
*lta
;
291 DBG("Connect to application pid %d", pid
);
293 lta
= find_app_by_pid(pid
);
296 DBG("Application pid %d not found", pid
);
300 sock
= ustctl_connect_pid(lta
->pid
);
302 ERR("Fail connecting to the PID %d\n", pid
);
311 * Notify apps by writing 42 to a named pipe using name.
312 * Every applications waiting for a ltt-sessiond will be notified
313 * and re-register automatically to the session daemon.
315 * Return open or write error value.
317 static int notify_apps(const char *name
)
322 DBG("Notify the global application pipe");
324 /* Try opening the global pipe */
325 fd
= open(name
, O_WRONLY
);
330 /* Notify by writing on the pipe */
331 ret
= write(fd
, "42", 2);
343 * Copy common data between lttcomm_lttng_header and lttcomm_session_msg
345 static void copy_common_data(struct lttcomm_lttng_header
*llh
, struct lttcomm_session_msg
*lsm
)
347 llh
->cmd_type
= lsm
->cmd_type
;
351 if (!uuid_is_null(lsm
->session_id
)) {
352 uuid_copy(llh
->session_id
, lsm
->session_id
);
355 strncpy(llh
->trace_name
, lsm
->trace_name
, strlen(llh
->trace_name
));
356 llh
->trace_name
[strlen(llh
->trace_name
) - 1] = '\0';
362 * Setup the outgoing data buffer for the response
363 * data allocating the right amount of memory.
365 * Return total size of the buffer pointed by buf.
367 static int setup_data_buffer(char **buf
, size_t s_data
, struct lttcomm_lttng_header
*llh
)
372 buf_size
= sizeof(struct lttcomm_lttng_header
) + s_data
;
373 *buf
= malloc(buf_size
);
380 /* Setup lttcomm_lttng_header data and copy
381 * it to the newly allocated buffer.
383 llh
->payload_size
= s_data
;
384 memcpy(*buf
, llh
, sizeof(struct lttcomm_lttng_header
));
395 * This takes the lttcomm_session_msg struct and process the command requested
396 * by the client. It then creates response(s) and send it back to the
397 * given socket (sock).
399 * Return any error encountered or 0 for success.
401 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
403 int ust_sock
, ret
, buf_size
;
405 char *send_buf
= NULL
;
406 struct lttcomm_lttng_header llh
;
408 DBG("Processing client message");
410 /* Copy common data to identify the response
411 * on the lttng client side.
413 copy_common_data(&llh
, lsm
);
415 /* Check command that needs a session */
416 switch (lsm
->cmd_type
) {
417 case LTTNG_CREATE_SESSION
:
418 case LTTNG_LIST_SESSIONS
:
422 current_session
= find_session_by_uuid(lsm
->session_id
);
423 if (current_session
== NULL
) {
424 ret
= LTTCOMM_SELECT_SESS
;
430 /* Default return code.
431 * In our world, everything is OK... right? ;)
433 llh
.ret_code
= LTTCOMM_OK
;
435 header_size
= sizeof(struct lttcomm_lttng_header
);
437 /* Connect to ust apps if available pid */
439 /* Connect to app using ustctl API */
440 ust_sock
= ust_connect_app(lsm
->pid
);
442 ret
= LTTCOMM_NO_TRACEABLE
;
447 /* Process by command type */
448 switch (lsm
->cmd_type
) {
449 case LTTNG_CREATE_SESSION
:
451 ret
= create_session(lsm
->session_name
, &llh
.session_id
);
454 ret
= LTTCOMM_EXIST_SESS
;
461 buf_size
= setup_data_buffer(&send_buf
, 0, &llh
);
469 case LTTNG_DESTROY_SESSION
:
471 ret
= destroy_session(&lsm
->session_id
);
473 ret
= LTTCOMM_NO_SESS
;
478 /* No auxiliary data so only send the llh struct. */
481 case LTTNG_LIST_TRACES
:
483 unsigned int trace_count
= get_trace_count_per_session(current_session
);
485 if (trace_count
== 0) {
486 ret
= LTTCOMM_NO_TRACE
;
490 buf_size
= setup_data_buffer(&send_buf
,
491 sizeof(struct lttng_trace
) * trace_count
, &llh
);
497 get_traces_per_session(current_session
, (struct lttng_trace
*)(send_buf
+ header_size
));
500 case UST_CREATE_TRACE
:
502 ret
= ust_create_trace(ust_sock
, lsm
->pid
);
504 /* If -1 is returned from ust_create_trace, malloc
505 * failed so it's pretty much a fatal error.
511 /* No auxiliary data so only send the llh struct. */
516 unsigned int app_count
= get_app_count();
517 /* Stop right now if no apps */
518 if (app_count
== 0) {
519 ret
= LTTCOMM_NO_APPS
;
523 /* Setup data buffer and details for transmission */
524 buf_size
= setup_data_buffer(&send_buf
,
525 sizeof(pid_t
) * app_count
, &llh
);
531 get_app_list_pids((pid_t
*)(send_buf
+ header_size
));
535 case UST_START_TRACE
:
537 ret
= ust_start_trace(ust_sock
, lsm
->pid
);
539 /* No auxiliary data so only send the llh struct. */
544 ret
= ust_stop_trace(ust_sock
, lsm
->pid
);
546 /* No auxiliary data so only send the llh struct. */
549 case LTTNG_LIST_SESSIONS
:
551 unsigned int session_count
= get_session_count();
552 /* Stop right now if no session */
553 if (session_count
== 0) {
554 ret
= LTTCOMM_NO_SESS
;
558 /* Setup data buffer and details for transmission */
559 buf_size
= setup_data_buffer(&send_buf
,
560 (sizeof(struct lttng_session
) * session_count
), &llh
);
566 get_lttng_session((struct lttng_session
*)(send_buf
+ header_size
));
571 /* Undefined command */
576 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
578 if (send_buf
!= NULL
) {
585 DBG("Return code to client %d", ret
);
586 /* Notify client of error */
588 llh
.payload_size
= 0;
589 send_unix_sock(sock
, (void*) &llh
, sizeof(llh
));
595 * usage function on stderr
597 static void usage(void)
599 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
600 fprintf(stderr
, " -h, --help Display this usage.\n");
601 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
602 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
603 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
604 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
605 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
606 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
607 fprintf(stderr
, " -V, --version Show version number.\n");
608 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
609 fprintf(stderr
, " -q, --quiet No output at all.\n");
610 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
614 * daemon argument parsing
616 static int parse_args(int argc
, char **argv
)
620 static struct option long_options
[] = {
621 { "client-sock", 1, 0, 'c' },
622 { "apps-sock", 1, 0, 'a' },
623 { "kconsumerd-cmd-sock", 1, 0, 0 },
624 { "kconsumerd-err-sock", 1, 0, 0 },
625 { "daemonize", 0, 0, 'd' },
626 { "sig-parent", 0, 0, 'S' },
627 { "help", 0, 0, 'h' },
628 { "group", 1, 0, 'g' },
629 { "version", 0, 0, 'V' },
630 { "quiet", 0, 0, 'q' },
631 { "verbose", 0, 0, 'v' },
636 int option_index
= 0;
637 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
644 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
646 fprintf(stderr
, " with arg %s\n", optarg
);
650 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
653 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
659 opt_tracing_group
= strdup(optarg
);
665 fprintf(stdout
, "%s\n", VERSION
);
671 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
674 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
683 /* Unknown option or other error.
684 * Error is printed by getopt, just return */
695 * Creates the two needed socket by the daemon.
696 * apps_sock - The communication socket for all UST apps.
697 * client_sock - The communication of the cli tool (lttng).
699 static int init_daemon_socket()
704 old_umask
= umask(0);
706 /* Create client tool unix socket */
707 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
708 if (client_sock
< 0) {
709 ERR("Create unix sock failed: %s", client_unix_sock_path
);
714 /* File permission MUST be 660 */
715 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
717 ERR("Set file permissions failed: %s", client_unix_sock_path
);
722 /* Create the application unix socket */
723 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
725 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
730 /* File permission MUST be 666 */
731 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
733 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
744 * check_existing_daemon
746 * Check if the global socket is available.
747 * If yes, error is returned.
749 static int check_existing_daemon()
753 ret
= access(client_unix_sock_path
, F_OK
);
755 ret
= access(apps_unix_sock_path
, F_OK
);
764 * Return pointer to home directory path using
765 * the env variable HOME.
769 static const char *get_home_dir(void)
771 const char *home_path
;
773 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
774 home_path
= default_home_dir
;
783 * Set the tracing group gid onto the client socket.
785 * Race window between mkdir and chown is OK because we are going from
786 * less permissive (root.root) to more permissive (root.tracing).
788 static int set_permissions(void)
793 /* Decide which group name to use */
794 (opt_tracing_group
!= NULL
) ?
795 (grp
= getgrnam(opt_tracing_group
)) :
796 (grp
= getgrnam(default_tracing_group
));
799 ERR("Missing tracing group. Aborting execution.\n");
804 /* Set lttng run dir */
805 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
807 ERR("Unable to set group on " LTTNG_RUNDIR
);
811 /* lttng client socket path */
812 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
814 ERR("Unable to set group on %s", client_unix_sock_path
);
818 /* kconsumerd error socket path */
819 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
821 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
825 DBG("All permissions are set");
832 * create_lttng_rundir
834 * Create the lttng run directory needed for all
835 * global sockets and pipe.
837 static int create_lttng_rundir(void)
841 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
843 ERR("Unable to create " LTTNG_RUNDIR
);
852 * set_kconsumerd_sockets
854 * Setup sockets and directory needed by the kconsumerd
855 * communication with the session daemon.
857 static int set_kconsumerd_sockets(void)
861 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
862 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
865 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
866 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
869 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
871 ERR("Failed to create " KCONSUMERD_PATH
);
875 /* Create the kconsumerd error unix socket */
876 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
877 if (kconsumerd_err_sock
< 0) {
878 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
883 /* File permission MUST be 660 */
884 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
886 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
898 * Setup signal handler for :
899 * SIGINT, SIGTERM, SIGPIPE
901 static int set_signal_handler(void)
907 if ((ret
= sigemptyset(&sigset
)) < 0) {
908 perror("sigemptyset");
912 sa
.sa_handler
= sighandler
;
915 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
920 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
925 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
930 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
938 * Signal handler for the daemon
940 static void sighandler(int sig
)
944 DBG("SIGPIPE catched");
947 DBG("SIGINT catched");
951 DBG("SIGTERM catched");
964 * Cleanup the daemon on exit
966 static void cleanup()
974 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
975 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
978 unlink(client_unix_sock_path
);
979 unlink(apps_unix_sock_path
);
980 unlink(kconsumerd_err_unix_sock_path
);
982 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
984 ERR("asprintf failed. Something is really wrong!");
987 /* Remove lttng run directory */
990 ERR("Unable to clean " LTTNG_RUNDIR
);
997 int main(int argc
, char **argv
)
1002 pthread_t threads
[2];
1004 /* Parse arguments */
1006 if ((ret
= parse_args(argc
, argv
) < 0)) {
1019 /* Check if daemon is UID = 0 */
1020 is_root
= !getuid();
1022 /* Set all sockets path */
1024 ret
= create_lttng_rundir();
1029 if (strlen(apps_unix_sock_path
) == 0) {
1030 snprintf(apps_unix_sock_path
, PATH_MAX
,
1031 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
1034 if (strlen(client_unix_sock_path
) == 0) {
1035 snprintf(client_unix_sock_path
, PATH_MAX
,
1036 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
1039 ret
= set_kconsumerd_sockets();
1044 if (strlen(apps_unix_sock_path
) == 0) {
1045 snprintf(apps_unix_sock_path
, PATH_MAX
,
1046 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir());
1049 /* Set the cli tool unix socket path */
1050 if (strlen(client_unix_sock_path
) == 0) {
1051 snprintf(client_unix_sock_path
, PATH_MAX
,
1052 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir());
1056 /* See if daemon already exist. If any of the two
1057 * socket needed by the daemon are present, this test fails
1059 if ((ret
= check_existing_daemon()) == 0) {
1060 ERR("Already running daemon.\n");
1061 /* We do not goto error because we must not
1062 * cleanup() because a daemon is already running.
1067 if (set_signal_handler() < 0) {
1071 /* Setup the needed unix socket */
1072 if (init_daemon_socket() < 0) {
1076 /* Set credentials to socket */
1077 if (is_root
&& (set_permissions() < 0)) {
1081 /* Get parent pid if -S, --sig-parent is specified. */
1082 if (opt_sig_parent
) {
1087 /* Create thread to manage the client socket */
1088 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
1090 perror("pthread_create");
1094 /* Create thread to manage application socket */
1095 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
1097 perror("pthread_create");
1101 for (i
= 0; i
< 2; i
++) {
1102 ret
= pthread_join(threads
[i
], &status
);
1104 perror("pthread_join");