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/liblttngctl.h>
40 #include "liblttsessiondcomm.h"
41 #include "ltt-sessiond.h"
46 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
47 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
48 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
49 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
51 /* Static functions */
52 static int set_signal_handler(void);
53 static int set_socket_perms(void);
54 static void sighandler(int sig
);
55 static void cleanup(void);
56 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
);
57 static int check_existing_daemon(void);
58 static int notify_apps(const char* name
);
59 static int connect_app(pid_t pid
);
60 static int find_app_by_pid(pid_t pid
);
61 static int init_daemon_socket(void);
62 static int process_client_msg(int sock
, struct lttcomm_session_msg
*);
63 static int send_unix_sock(int sock
, void *buf
, size_t len
);
64 static int setup_data_buffer(char **buf
, size_t size
, struct lttcomm_lttng_msg
*llm
);
65 static void add_traceable_app(struct ltt_traceable_app
*lta
);
66 static void del_traceable_app(struct ltt_traceable_app
*lta
);
68 /* Command function */
69 static void get_list_apps(pid_t
*pids
);
71 static void *thread_manage_clients(void *data
);
72 static void *thread_manage_apps(void *data
);
76 const char *opt_tracing_group
;
77 static int opt_sig_parent
;
78 static int opt_daemon
;
81 static int is_root
; /* Set to 1 if the daemon is running as root */
84 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
85 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
87 static int client_socket
;
88 static int apps_socket
;
90 static struct ltt_session
*current_session
;
92 /* Number of element for the list below. */
93 static unsigned int traceable_app_count
;
95 /* Init ust traceabl application's list */
96 static struct ltt_traceable_app_list ltt_traceable_app_list
= {
97 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
101 pthread_mutex_t ltt_traceable_app_list_mutex
;
106 * This thread manage the application socket communication
108 static void *thread_manage_apps(void *data
)
111 struct ltt_traceable_app
*lta
;
113 /* TODO: Something more elegant is needed but fine for now */
115 int reg
; /* 1:register, 0:unregister */
120 /* Notify all applications to register */
121 notify_apps(default_global_apps_pipe
);
123 ret
= lttcomm_listen_unix_sock(apps_socket
);
129 /* Blocking call, waiting for transmission */
130 sock
= lttcomm_accept_unix_sock(apps_socket
);
135 /* Basic recv here to handle the very simple data
136 * that the libust send to register (reg_msg).
138 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
144 /* Add application to the global traceable list */
145 if (reg_msg
.reg
== 1) {
147 lta
= malloc(sizeof(struct ltt_traceable_app
));
148 lta
->pid
= reg_msg
.pid
;
149 lta
->uid
= reg_msg
.uid
;
150 add_traceable_app(lta
);
153 cds_list_for_each_entry(lta
, <t_traceable_app_list
.head
, list
) {
154 if (lta
->pid
== reg_msg
.pid
&& lta
->uid
== reg_msg
.uid
) {
155 del_traceable_app(lta
);
169 * thread_manage_clients
171 * This thread manage all clients request using the unix
172 * client socket for communication.
174 static void *thread_manage_clients(void *data
)
177 struct lttcomm_session_msg lsm
;
179 ret
= lttcomm_listen_unix_sock(client_socket
);
184 /* Notify parent pid that we are ready
185 * to accept command for client side.
187 if (opt_sig_parent
) {
192 /* Blocking call, waiting for transmission */
193 sock
= lttcomm_accept_unix_sock(client_socket
);
199 * Data is received from the lttng client. The struct
200 * lttcomm_session_msg (lsm) contains the command and data
201 * request of the client.
203 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
208 /* This function dispatch the work to the LTTng or UST libs
209 * and then sends back the response to the client. This is needed
210 * because there might be more then one lttcomm_lttng_msg to
211 * send out so process_client_msg do both jobs.
213 ret
= process_client_msg(sock
, &lsm
);
215 /* Error detected but still accept command */
227 * Add a traceable application structure to the global
228 * list protected by a mutex.
230 static void add_traceable_app(struct ltt_traceable_app
*lta
)
232 pthread_mutex_lock(<t_traceable_app_list_mutex
);
233 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
234 traceable_app_count
++;
235 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
241 * Delete a traceable application structure from the
242 * global list protected by a mutex.
244 static void del_traceable_app(struct ltt_traceable_app
*lta
)
246 pthread_mutex_lock(<t_traceable_app_list_mutex
);
247 cds_list_del(<a
->list
);
249 if (traceable_app_count
!= 0) {
250 traceable_app_count
--;
252 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
258 * Send data on a unix socket using the liblttsessiondcomm API.
260 * Return lttcomm error code.
262 static int send_unix_sock(int sock
, void *buf
, size_t len
)
264 /* Check valid length */
269 return lttcomm_send_unix_sock(sock
, buf
, len
);
275 * Return a socket connected to the libust communication socket
276 * of the application identified by the pid.
278 * If the pid is not found in the traceable list,
279 * return -1 to indicate error.
281 static int connect_app(pid_t pid
)
285 ret
= find_app_by_pid(pid
);
290 sock
= ustctl_connect_pid(pid
);
292 ERR("Fail connecting to the PID %d\n", pid
);
301 * Notify apps by writing 42 to a named pipe using name.
302 * Every applications waiting for a ltt-sessiond will be notified
303 * and re-register automatically to the session daemon.
305 * Return open or write error value.
307 static int notify_apps(const char *name
)
312 /* Try opening the global pipe */
313 fd
= open(name
, O_WRONLY
);
318 /* Notify by writing on the pipe */
319 ret
= write(fd
, "42", 2);
331 * Iterate over the traceable apps list.
332 * On success, return 1, else return 0
334 static int find_app_by_pid(pid_t pid
)
336 struct ltt_traceable_app
*iter
;
338 pthread_mutex_lock(<t_traceable_app_list_mutex
);
339 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
340 if (iter
->pid
== pid
) {
341 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
346 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
354 * Create an userspace trace using pid.
355 * This trace is then appended to the current session
358 static int ust_create_trace(pid_t pid
)
361 struct ltt_ust_trace
*trace
;
363 trace
= malloc(sizeof(struct ltt_ust_trace
));
374 /* Connect to app using ustctl API */
375 sock
= connect_app(pid
);
377 ret
= LTTCOMM_NO_TRACEABLE
;
381 ret
= ustctl_create_trace(sock
, "auto");
383 ret
= LTTCOMM_CREATE_FAIL
;
387 /* Check if current session is valid */
388 if (current_session
) {
389 cds_list_add(&trace
->list
, ¤t_session
->ust_traces
);
399 * List traceable user-space application and fill an
402 static void get_list_apps(pid_t
*pids
)
405 struct ltt_traceable_app
*iter
;
407 /* Protected by a mutex here because the threads manage_client
408 * and manage_apps can access this list.
410 pthread_mutex_lock(<t_traceable_app_list_mutex
);
411 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
415 pthread_mutex_unlock(<t_traceable_app_list_mutex
);
421 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
423 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
425 llm
->cmd_type
= lsm
->cmd_type
;
429 if (!uuid_is_null(lsm
->session_id
)) {
430 uuid_copy(llm
->session_id
, lsm
->session_id
);
433 strncpy(llm
->trace_name
, lsm
->trace_name
, strlen(llm
->trace_name
));
434 llm
->trace_name
[strlen(llm
->trace_name
) - 1] = '\0';
440 * Setup the outgoing data buffer for the response
441 * data allocating the right amount of memory.
443 * Return total size of the buffer pointed by buf.
445 static int setup_data_buffer(char **buf
, size_t s_data
, struct lttcomm_lttng_msg
*llm
)
450 buf_size
= sizeof(struct lttcomm_lttng_msg
) + s_data
;
451 *buf
= malloc(buf_size
);
458 /* Setup lttcomm_lttng_msg data and copy
459 * it to the newly allocated buffer.
461 llm
->size_payload
= s_data
;
462 memcpy(*buf
, llm
, sizeof(struct lttcomm_lttng_msg
));
473 * This takes the lttcomm_session_msg struct and process the command requested
474 * by the client. It then creates response(s) and send it back to the
475 * given socket (sock).
477 * Return any error encountered or 0 for success.
479 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
484 char *send_buf
= NULL
;
485 struct lttcomm_lttng_msg llm
;
487 /* Copy common data to identify the response
488 * on the lttng client side.
490 copy_common_data(&llm
, lsm
);
492 /* Check command that needs a session */
493 if (lsm
->cmd_type
!= LTTNG_CREATE_SESSION
&&
494 lsm
->cmd_type
!= LTTNG_LIST_SESSIONS
&&
495 lsm
->cmd_type
!= UST_LIST_APPS
)
497 current_session
= find_session_by_uuid(lsm
->session_id
);
498 if (current_session
== NULL
) {
499 ret
= LTTCOMM_SELECT_SESS
;
504 /* Default return code.
505 * In our world, everything is OK... right? ;)
507 llm
.ret_code
= LTTCOMM_OK
;
509 header_size
= sizeof(struct lttcomm_lttng_msg
);
511 /* Process by command type */
512 switch (lsm
->cmd_type
) {
513 case LTTNG_CREATE_SESSION
:
515 ret
= create_session(lsm
->session_name
, &llm
.session_id
);
518 ret
= LTTCOMM_EXIST_SESS
;
525 buf_size
= setup_data_buffer(&send_buf
, 0, &llm
);
533 case LTTNG_DESTROY_SESSION
:
535 ret
= destroy_session(&lsm
->session_id
);
537 ret
= LTTCOMM_NO_SESS
;
542 /* No auxiliary data so only send the llm struct. */
545 case UST_CREATE_TRACE
:
547 ret
= ust_create_trace(lsm
->pid
);
549 ret
= LTTCOMM_CREATE_FAIL
;
553 /* No auxiliary data so only send the llm struct. */
558 /* Stop right now if no apps */
559 if (traceable_app_count
== 0) {
560 ret
= LTTCOMM_NO_APPS
;
564 /* Setup data buffer and details for transmission */
565 buf_size
= setup_data_buffer(&send_buf
,
566 sizeof(pid_t
) * traceable_app_count
, &llm
);
572 get_list_apps((pid_t
*)(send_buf
+ header_size
));
576 case LTTNG_LIST_SESSIONS
:
578 unsigned int session_count
= get_session_count();
579 /* Stop right now if no session */
580 if (session_count
== 0) {
581 ret
= LTTCOMM_NO_SESS
;
585 /* Setup data buffer and details for transmission */
586 buf_size
= setup_data_buffer(&send_buf
,
587 (sizeof(struct lttng_session
) * session_count
), &llm
);
593 get_lttng_session((struct lttng_session
*)(send_buf
+ header_size
));
599 /* Undefined command */
605 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
607 if (send_buf
!= NULL
) {
614 /* Notify client of error */
616 llm
.size_payload
= 0;
617 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
623 * usage function on stderr
625 static void usage(void)
627 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
628 fprintf(stderr
, " -h, --help Display this usage.\n");
629 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
630 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket.\n");
631 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
632 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
633 fprintf(stderr
, " -V, --version Show version number.\n");
634 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
635 fprintf(stderr
, " -q, --quiet No output at all.\n");
639 * daemon argument parsing
641 static int parse_args(int argc
, char **argv
)
645 static struct option long_options
[] = {
646 { "client-sock", 1, 0, 'c' },
647 { "apps-sock", 1, 0, 'a' },
648 { "daemonize", 0, 0, 'd' },
649 { "sig-parent", 0, 0, 'S' },
650 { "help", 0, 0, 'h' },
651 { "group", 1, 0, 'g' },
652 { "version", 0, 0, 'V' },
653 { "quiet", 0, 0, 'q' },
658 int option_index
= 0;
659 c
= getopt_long(argc
, argv
, "dhqVS" "a:c:g:s:", long_options
, &option_index
);
666 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
668 fprintf(stderr
, " with arg %s\n", optarg
);
672 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
675 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
681 opt_tracing_group
= strdup(optarg
);
687 fprintf(stdout
, "%s\n", VERSION
);
696 /* Unknown option or other error.
697 * Error is printed by getopt, just return */
708 * Creates the two needed socket by the daemon.
709 * apps_socket - The communication socket for all UST apps.
710 * client_socket - The communication of the cli tool (lttng).
712 static int init_daemon_socket()
717 old_umask
= umask(0);
719 /* Create client tool unix socket */
720 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
721 if (client_socket
< 0) {
726 /* File permission MUST be 660 */
727 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
733 /* Create the application unix socket */
734 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
735 if (apps_socket
< 0) {
740 /* File permission MUST be 660 */
741 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
753 * check_existing_daemon
755 * Check if the global socket is available.
756 * If yes, error is returned.
758 static int check_existing_daemon()
762 ret
= access(client_unix_sock_path
, F_OK
);
764 ret
= access(apps_unix_sock_path
, F_OK
);
773 * Return pointer to home directory path using
774 * the env variable HOME.
778 static const char *get_home_dir(void)
780 const char *home_path
;
782 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
783 home_path
= default_home_dir
;
792 * Set the tracing group gid onto the client socket.
794 static int set_socket_perms(void)
799 /* Decide which group name to use */
800 (opt_tracing_group
!= NULL
) ?
801 (grp
= getgrnam(opt_tracing_group
)) :
802 (grp
= getgrnam(default_tracing_group
));
805 ERR("Missing tracing group. Aborting execution.\n");
810 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
822 * Setup signal handler for :
823 * SIGINT, SIGTERM, SIGPIPE
825 static int set_signal_handler(void)
831 if ((ret
= sigemptyset(&sigset
)) < 0) {
832 perror("sigemptyset");
836 sa
.sa_handler
= sighandler
;
839 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
844 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
849 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
860 * Signal handler for the daemon
862 static void sighandler(int sig
)
881 * Cleanup the daemon on exit
883 static void cleanup()
886 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
887 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
890 unlink(client_unix_sock_path
);
891 unlink(apps_unix_sock_path
);
897 int main(int argc
, char **argv
)
902 pthread_t threads
[2];
904 /* Parse arguments */
906 if ((ret
= parse_args(argc
, argv
) < 0)) {
919 /* Check if daemon is UID = 0 */
922 /* Set all sockets path */
924 if (strlen(apps_unix_sock_path
) == 0) {
925 (snprintf(apps_unix_sock_path
, PATH_MAX
,
926 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
929 if (strlen(client_unix_sock_path
) == 0) {
930 (snprintf(client_unix_sock_path
, PATH_MAX
,
931 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
934 if (strlen(apps_unix_sock_path
) == 0) {
935 (snprintf(apps_unix_sock_path
, PATH_MAX
,
936 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
939 /* Set the cli tool unix socket path */
940 if (strlen(client_unix_sock_path
) == 0) {
941 (snprintf(client_unix_sock_path
, PATH_MAX
,
942 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
946 /* See if daemon already exist. If any of the two
947 * socket needed by the daemon are present, this test fails
949 if ((ret
= check_existing_daemon()) == 0) {
950 ERR("Already running daemon.\n");
951 /* We do not goto error because we must not
952 * cleanup() because a daemon is already working.
957 if (set_signal_handler() < 0) {
961 /* Setup the two needed unix socket */
962 if (init_daemon_socket() < 0) {
966 /* Set credentials to socket */
967 if (is_root
&& (set_socket_perms() < 0)) {
971 /* Get parent pid if -S, --sig-parent is specified. */
972 if (opt_sig_parent
) {
977 /* Create thread to manage the client socket */
978 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
980 perror("pthread_create");
984 /* Create thread to manage application socket */
985 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
987 perror("pthread_create");
991 for (i
= 0; i
< 2; i
++) {
992 ret
= pthread_join(threads
[i
], &status
);
994 perror("pthread_join");
1006 return EXIT_FAILURE
;