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"
48 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
49 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
50 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
51 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
53 /* Static functions */
54 static int check_existing_daemon(void);
55 static int connect_app(pid_t pid
);
56 static int init_daemon_socket(void);
57 static int notify_apps(const char* name
);
58 static int process_client_msg(int sock
, struct lttcomm_session_msg
*);
59 static int send_unix_sock(int sock
, void *buf
, size_t len
);
60 static int set_signal_handler(void);
61 static int set_socket_perms(void);
62 static int setup_data_buffer(char **buf
, size_t size
, struct lttcomm_lttng_msg
*llm
);
63 static void cleanup(void);
64 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
);
65 static void sighandler(int sig
);
67 static void *thread_manage_clients(void *data
);
68 static void *thread_manage_apps(void *data
);
74 const char *opt_tracing_group
;
75 static int opt_sig_parent
;
76 static int opt_daemon
;
77 static int is_root
; /* Set to 1 if the daemon is running as root */
80 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
81 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
83 static int client_socket
;
84 static int apps_socket
;
86 static struct ltt_session
*current_session
;
91 * This thread manage the application socket communication
93 static void *thread_manage_apps(void *data
)
97 /* TODO: Something more elegant is needed but fine for now */
99 int reg
; /* 1:register, 0:unregister */
104 DBG("[thread] Manage apps started");
106 /* Notify all applications to register */
107 notify_apps(default_global_apps_pipe
);
109 ret
= lttcomm_listen_unix_sock(apps_socket
);
115 /* Blocking call, waiting for transmission */
116 sock
= lttcomm_accept_unix_sock(apps_socket
);
121 /* Basic recv here to handle the very simple data
122 * that the libust send to register (reg_msg).
124 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
130 /* Add application to the global traceable list */
131 if (reg_msg
.reg
== 1) {
133 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
135 /* register_traceable_app only return an error with
136 * ENOMEM. At this point, we better stop everything.
142 unregister_traceable_app(reg_msg
.pid
);
152 * thread_manage_clients
154 * This thread manage all clients request using the unix
155 * client socket for communication.
157 static void *thread_manage_clients(void *data
)
160 struct lttcomm_session_msg lsm
;
162 DBG("[thread] Manage client started");
164 ret
= lttcomm_listen_unix_sock(client_socket
);
169 /* Notify parent pid that we are ready
170 * to accept command for client side.
172 if (opt_sig_parent
) {
177 /* Blocking call, waiting for transmission */
178 sock
= lttcomm_accept_unix_sock(client_socket
);
184 * Data is received from the lttng client. The struct
185 * lttcomm_session_msg (lsm) contains the command and data
186 * request of the client.
188 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
193 /* This function dispatch the work to the LTTng or UST libs
194 * and then sends back the response to the client. This is needed
195 * because there might be more then one lttcomm_lttng_msg to
196 * send out so process_client_msg do both jobs.
198 ret
= process_client_msg(sock
, &lsm
);
200 /* Error detected but still accept command */
212 * Send data on a unix socket using the liblttsessiondcomm API.
214 * Return lttcomm error code.
216 static int send_unix_sock(int sock
, void *buf
, size_t len
)
218 /* Check valid length */
223 return lttcomm_send_unix_sock(sock
, buf
, len
);
229 * Return a socket connected to the libust communication socket
230 * of the application identified by the pid.
232 * If the pid is not found in the traceable list,
233 * return -1 to indicate error.
235 static int connect_app(pid_t pid
)
238 struct ltt_traceable_app
*lta
;
240 DBG("Connect to application pid %d", pid
);
242 lta
= find_app_by_pid(pid
);
245 DBG("Application pid %d not found", pid
);
249 sock
= ustctl_connect_pid(lta
->pid
);
251 ERR("Fail connecting to the PID %d\n", pid
);
260 * Notify apps by writing 42 to a named pipe using name.
261 * Every applications waiting for a ltt-sessiond will be notified
262 * and re-register automatically to the session daemon.
264 * Return open or write error value.
266 static int notify_apps(const char *name
)
271 DBG("Notify the global application pipe");
273 /* Try opening the global pipe */
274 fd
= open(name
, O_WRONLY
);
279 /* Notify by writing on the pipe */
280 ret
= write(fd
, "42", 2);
292 * Create an userspace trace using pid.
293 * This trace is then appended to the current session
296 static int ust_create_trace(pid_t pid
)
299 struct ltt_ust_trace
*trace
;
301 DBG("Creating trace for pid %d", pid
);
303 trace
= malloc(sizeof(struct ltt_ust_trace
));
313 /* NOTE: to be removed. Trace name will no longer be
314 * required for LTTng userspace tracer. For now, we set it
315 * to 'auto' for API compliance.
317 snprintf(trace
->name
, 5, "auto");
319 /* Connect to app using ustctl API */
320 sock
= connect_app(pid
);
322 ret
= LTTCOMM_NO_TRACEABLE
;
326 ret
= ustctl_create_trace(sock
, trace
->name
);
328 ret
= LTTCOMM_CREATE_FAIL
;
332 /* Check if current session is valid */
333 if (current_session
) {
334 cds_list_add(&trace
->list
, ¤t_session
->ust_traces
);
335 current_session
->ust_trace_count
++;
345 * Start a trace. This trace, identified by the pid, must be
346 * in the current session ust_traces list.
348 static int ust_start_trace(pid_t pid
)
351 struct ltt_ust_trace
*trace
;
353 DBG("Starting trace for pid %d", pid
);
355 trace
= find_session_ust_trace_by_pid(current_session
, pid
);
357 ret
= LTTCOMM_NO_TRACE
;
361 /* Connect to app using ustctl API */
362 sock
= connect_app(pid
);
364 ret
= LTTCOMM_NO_TRACEABLE
;
368 ret
= ustctl_start_trace(sock
, "auto");
370 ret
= LTTCOMM_START_FAIL
;
381 * Stop a trace. This trace, identified by the pid, must be
382 * in the current session ust_traces list.
384 static int ust_stop_trace(pid_t pid
)
387 struct ltt_ust_trace
*trace
;
389 DBG("Stopping trace for pid %d", pid
);
391 trace
= find_session_ust_trace_by_pid(current_session
, pid
);
393 ret
= LTTCOMM_NO_TRACE
;
397 /* Connect to app using ustctl API */
398 sock
= connect_app(pid
);
400 ret
= LTTCOMM_NO_TRACEABLE
;
404 ret
= ustctl_stop_trace(sock
, trace
->name
);
406 ret
= LTTCOMM_STOP_FAIL
;
417 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
419 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
421 llm
->cmd_type
= lsm
->cmd_type
;
425 if (!uuid_is_null(lsm
->session_id
)) {
426 uuid_copy(llm
->session_id
, lsm
->session_id
);
429 strncpy(llm
->trace_name
, lsm
->trace_name
, strlen(llm
->trace_name
));
430 llm
->trace_name
[strlen(llm
->trace_name
) - 1] = '\0';
436 * Setup the outgoing data buffer for the response
437 * data allocating the right amount of memory.
439 * Return total size of the buffer pointed by buf.
441 static int setup_data_buffer(char **buf
, size_t s_data
, struct lttcomm_lttng_msg
*llm
)
446 buf_size
= sizeof(struct lttcomm_lttng_msg
) + s_data
;
447 *buf
= malloc(buf_size
);
454 /* Setup lttcomm_lttng_msg data and copy
455 * it to the newly allocated buffer.
457 llm
->size_payload
= s_data
;
458 memcpy(*buf
, llm
, sizeof(struct lttcomm_lttng_msg
));
469 * This takes the lttcomm_session_msg struct and process the command requested
470 * by the client. It then creates response(s) and send it back to the
471 * given socket (sock).
473 * Return any error encountered or 0 for success.
475 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
480 char *send_buf
= NULL
;
481 struct lttcomm_lttng_msg llm
;
483 DBG("Processing client message");
485 /* Copy common data to identify the response
486 * on the lttng client side.
488 copy_common_data(&llm
, lsm
);
490 /* Check command that needs a session */
491 if (lsm
->cmd_type
!= LTTNG_CREATE_SESSION
&&
492 lsm
->cmd_type
!= LTTNG_LIST_SESSIONS
&&
493 lsm
->cmd_type
!= UST_LIST_APPS
)
495 current_session
= find_session_by_uuid(lsm
->session_id
);
496 if (current_session
== NULL
) {
497 ret
= LTTCOMM_SELECT_SESS
;
502 /* Default return code.
503 * In our world, everything is OK... right? ;)
505 llm
.ret_code
= LTTCOMM_OK
;
507 header_size
= sizeof(struct lttcomm_lttng_msg
);
509 /* Process by command type */
510 switch (lsm
->cmd_type
) {
511 case LTTNG_CREATE_SESSION
:
513 ret
= create_session(lsm
->session_name
, &llm
.session_id
);
516 ret
= LTTCOMM_EXIST_SESS
;
523 buf_size
= setup_data_buffer(&send_buf
, 0, &llm
);
531 case LTTNG_DESTROY_SESSION
:
533 ret
= destroy_session(&lsm
->session_id
);
535 ret
= LTTCOMM_NO_SESS
;
540 /* No auxiliary data so only send the llm struct. */
543 case LTTNG_LIST_TRACES
:
545 unsigned int trace_count
= get_trace_count_per_session(current_session
);
547 if (trace_count
== 0) {
548 ret
= LTTCOMM_NO_TRACE
;
552 buf_size
= setup_data_buffer(&send_buf
,
553 sizeof(struct lttng_trace
) * trace_count
, &llm
);
559 get_traces_per_session(current_session
, (struct lttng_trace
*)(send_buf
+ header_size
));
562 case UST_CREATE_TRACE
:
564 ret
= ust_create_trace(lsm
->pid
);
566 /* If -1 is returned from ust_create_trace, malloc
567 * failed so it's pretty much a fatal error.
573 /* No auxiliary data so only send the llm struct. */
578 unsigned int app_count
= get_app_count();
579 /* Stop right now if no apps */
580 if (app_count
== 0) {
581 ret
= LTTCOMM_NO_APPS
;
585 /* Setup data buffer and details for transmission */
586 buf_size
= setup_data_buffer(&send_buf
,
587 sizeof(pid_t
) * app_count
, &llm
);
593 get_app_list_pids((pid_t
*)(send_buf
+ header_size
));
597 case UST_START_TRACE
:
599 ret
= ust_start_trace(lsm
->pid
);
601 /* No auxiliary data so only send the llm struct. */
606 ret
= ust_stop_trace(lsm
->pid
);
608 /* No auxiliary data so only send the llm struct. */
611 case LTTNG_LIST_SESSIONS
:
613 unsigned int session_count
= get_session_count();
614 /* Stop right now if no session */
615 if (session_count
== 0) {
616 ret
= LTTCOMM_NO_SESS
;
620 /* Setup data buffer and details for transmission */
621 buf_size
= setup_data_buffer(&send_buf
,
622 (sizeof(struct lttng_session
) * session_count
), &llm
);
628 get_lttng_session((struct lttng_session
*)(send_buf
+ header_size
));
634 /* Undefined command */
640 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
642 if (send_buf
!= NULL
) {
649 DBG("Return code to client %d", ret
);
650 /* Notify client of error */
652 llm
.size_payload
= 0;
653 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
659 * usage function on stderr
661 static void usage(void)
663 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
664 fprintf(stderr
, " -h, --help Display this usage.\n");
665 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
666 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket.\n");
667 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
668 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
669 fprintf(stderr
, " -V, --version Show version number.\n");
670 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
671 fprintf(stderr
, " -q, --quiet No output at all.\n");
672 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
676 * daemon argument parsing
678 static int parse_args(int argc
, char **argv
)
682 static struct option long_options
[] = {
683 { "client-sock", 1, 0, 'c' },
684 { "apps-sock", 1, 0, 'a' },
685 { "daemonize", 0, 0, 'd' },
686 { "sig-parent", 0, 0, 'S' },
687 { "help", 0, 0, 'h' },
688 { "group", 1, 0, 'g' },
689 { "version", 0, 0, 'V' },
690 { "quiet", 0, 0, 'q' },
691 { "verbose", 0, 0, 'v' },
696 int option_index
= 0;
697 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:", long_options
, &option_index
);
704 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
706 fprintf(stderr
, " with arg %s\n", optarg
);
710 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
713 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
719 opt_tracing_group
= strdup(optarg
);
725 fprintf(stdout
, "%s\n", VERSION
);
737 /* Unknown option or other error.
738 * Error is printed by getopt, just return */
749 * Creates the two needed socket by the daemon.
750 * apps_socket - The communication socket for all UST apps.
751 * client_socket - The communication of the cli tool (lttng).
753 static int init_daemon_socket()
758 old_umask
= umask(0);
760 /* Create client tool unix socket */
761 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
762 if (client_socket
< 0) {
767 /* File permission MUST be 660 */
768 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
774 /* Create the application unix socket */
775 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
776 if (apps_socket
< 0) {
781 /* File permission MUST be 660 */
782 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
794 * check_existing_daemon
796 * Check if the global socket is available.
797 * If yes, error is returned.
799 static int check_existing_daemon()
803 ret
= access(client_unix_sock_path
, F_OK
);
805 ret
= access(apps_unix_sock_path
, F_OK
);
814 * Return pointer to home directory path using
815 * the env variable HOME.
819 static const char *get_home_dir(void)
821 const char *home_path
;
823 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
824 home_path
= default_home_dir
;
833 * Set the tracing group gid onto the client socket.
835 static int set_socket_perms(void)
840 /* Decide which group name to use */
841 (opt_tracing_group
!= NULL
) ?
842 (grp
= getgrnam(opt_tracing_group
)) :
843 (grp
= getgrnam(default_tracing_group
));
846 ERR("Missing tracing group. Aborting execution.\n");
851 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
856 DBG("Sockets permissions set");
865 * Setup signal handler for :
866 * SIGINT, SIGTERM, SIGPIPE
868 static int set_signal_handler(void)
874 if ((ret
= sigemptyset(&sigset
)) < 0) {
875 perror("sigemptyset");
879 sa
.sa_handler
= sighandler
;
882 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
887 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
892 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
897 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
905 * Signal handler for the daemon
907 static void sighandler(int sig
)
911 DBG("SIGPIPE catched");
914 DBG("SIGINT catched");
918 DBG("SIGTERM catched");
931 * Cleanup the daemon on exit
933 static void cleanup()
938 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
939 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
942 unlink(client_unix_sock_path
);
943 unlink(apps_unix_sock_path
);
949 int main(int argc
, char **argv
)
954 pthread_t threads
[2];
956 /* Parse arguments */
958 if ((ret
= parse_args(argc
, argv
) < 0)) {
971 /* Check if daemon is UID = 0 */
974 /* Set all sockets path */
976 if (strlen(apps_unix_sock_path
) == 0) {
977 (snprintf(apps_unix_sock_path
, PATH_MAX
,
978 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
981 if (strlen(client_unix_sock_path
) == 0) {
982 (snprintf(client_unix_sock_path
, PATH_MAX
,
983 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
986 if (strlen(apps_unix_sock_path
) == 0) {
987 (snprintf(apps_unix_sock_path
, PATH_MAX
,
988 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
991 /* Set the cli tool unix socket path */
992 if (strlen(client_unix_sock_path
) == 0) {
993 (snprintf(client_unix_sock_path
, PATH_MAX
,
994 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
998 /* See if daemon already exist. If any of the two
999 * socket needed by the daemon are present, this test fails
1001 if ((ret
= check_existing_daemon()) == 0) {
1002 ERR("Already running daemon.\n");
1003 /* We do not goto error because we must not
1004 * cleanup() because a daemon is already working.
1006 return EXIT_FAILURE
;
1009 if (set_signal_handler() < 0) {
1013 /* Setup the two needed unix socket */
1014 if (init_daemon_socket() < 0) {
1018 /* Set credentials to socket */
1019 if (is_root
&& (set_socket_perms() < 0)) {
1023 /* Get parent pid if -S, --sig-parent is specified. */
1024 if (opt_sig_parent
) {
1029 /* Create thread to manage the client socket */
1030 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
1032 perror("pthread_create");
1036 /* Create thread to manage application socket */
1037 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
1039 perror("pthread_create");
1043 for (i
= 0; i
< 2; i
++) {
1044 ret
= pthread_join(threads
[i
], &status
);
1046 perror("pthread_join");
1058 return EXIT_FAILURE
;