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"
45 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
46 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
47 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
48 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
50 /* Static functions */
51 static int set_signal_handler(void);
52 static int set_socket_perms(void);
53 static void sighandler(int sig
);
54 static void cleanup(void);
55 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
);
56 static int check_existing_daemon(void);
57 static int notify_apps(const char* name
);
58 static int connect_app(pid_t pid
);
59 static int find_app_by_pid(pid_t pid
);
60 static int init_daemon_socket(void);
61 static int process_client_msg(int sock
, struct lttcomm_session_msg
*);
62 static int send_unix_sock(int sock
, void *buf
, size_t len
);
63 static int setup_data_buffer(char **buf
, size_t size
, struct lttcomm_lttng_msg
*llm
);
65 /* Command function */
66 static void get_list_apps(pid_t
*pids
);
67 static void get_list_sessions(struct lttng_session
*lt
);
69 static void *thread_manage_clients(void *data
);
70 static void *thread_manage_apps(void *data
);
72 static int create_session(char *name
, uuid_t
*session_id
);
73 static int destroy_session(uuid_t
*uuid
);
75 static struct ltt_session
*find_session_by_uuid(uuid_t session_id
);
76 static struct ltt_session
*find_session_by_name(char *name
);
80 const char *opt_tracing_group
;
81 static int opt_sig_parent
;
82 static int opt_daemon
;
85 static int is_root
; /* Set to 1 if the daemon is running as root */
88 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
89 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
91 static int client_socket
;
92 static int apps_socket
;
94 static struct ltt_session
*current_session
;
96 /* Number of element for the list below. */
97 static unsigned int session_count
;
98 static unsigned int traceable_app_count
;
100 /* Init session's list */
101 static struct ltt_session_list ltt_session_list
= {
102 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
105 /* Init ust traceabl application's list */
106 static struct ltt_traceable_app_list ltt_traceable_app_list
= {
107 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
113 * This thread manage the application socket communication
115 static void *thread_manage_apps(void *data
)
118 struct ltt_traceable_app
*lta
;
120 /* TODO: Something more elegant is needed but fine for now */
122 int reg
; /* 1:register, 0:unregister */
127 /* Notify all applications to register */
128 notify_apps(default_global_apps_pipe
);
130 ret
= lttcomm_listen_unix_sock(apps_socket
);
136 /* Blocking call, waiting for transmission */
137 sock
= lttcomm_accept_unix_sock(apps_socket
);
142 /* Basic recv here to handle the very simple data
143 * that the libust send to register (reg_msg).
145 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
151 /* Add application to the global traceable list */
152 if (reg_msg
.reg
== 1) {
154 lta
= malloc(sizeof(struct ltt_traceable_app
));
155 lta
->pid
= reg_msg
.pid
;
156 lta
->uid
= reg_msg
.uid
;
157 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
158 traceable_app_count
++;
161 cds_list_for_each_entry(lta
, <t_traceable_app_list
.head
, list
) {
162 if (lta
->pid
== reg_msg
.pid
&& lta
->uid
== reg_msg
.uid
) {
163 cds_list_del(<a
->list
);
165 /* Check to not overflow here */
166 if (traceable_app_count
!= 0) {
167 traceable_app_count
--;
181 * thread_manage_clients
183 * This thread manage all clients request using the unix
184 * client socket for communication.
186 static void *thread_manage_clients(void *data
)
189 struct lttcomm_session_msg lsm
;
191 ret
= lttcomm_listen_unix_sock(client_socket
);
196 /* Notify parent pid that we are ready
197 * to accept command for client side.
199 if (opt_sig_parent
) {
204 /* Blocking call, waiting for transmission */
205 sock
= lttcomm_accept_unix_sock(client_socket
);
211 * Data is received from the lttng client. The struct
212 * lttcomm_session_msg (lsm) contains the command and data
213 * request of the client.
215 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
220 /* This function dispatch the work to the LTTng or UST libs
221 * and then sends back the response to the client. This is needed
222 * because there might be more then one lttcomm_lttng_msg to
223 * send out so process_client_msg do both jobs.
225 ret
= process_client_msg(sock
, &lsm
);
227 /* Error detected but still accept command */
239 * Send data on a unix socket using the liblttsessiondcomm API.
241 * Return lttcomm error code.
243 static int send_unix_sock(int sock
, void *buf
, size_t len
)
245 /* Check valid length */
250 return lttcomm_send_unix_sock(sock
, buf
, len
);
256 * Return a socket connected to the libust communication socket
257 * of the application identified by the pid.
259 * If the pid is not found in the traceable list,
260 * return -1 to indicate error.
262 static int connect_app(pid_t pid
)
266 ret
= find_app_by_pid(pid
);
271 sock
= ustctl_connect_pid(pid
);
273 ERR("Fail connecting to the PID %d\n", pid
);
282 * Notify apps by writing 42 to a named pipe using name.
283 * Every applications waiting for a ltt-sessiond will be notified
284 * and re-register automatically to the session daemon.
286 * Return open or write error value.
288 static int notify_apps(const char *name
)
293 /* Try opening the global pipe */
294 fd
= open(name
, O_WRONLY
);
299 /* Notify by writing on the pipe */
300 ret
= write(fd
, "42", 2);
312 * Iterate over the traceable apps list.
313 * On success, return 1, else return 0
315 static int find_app_by_pid(pid_t pid
)
317 struct ltt_traceable_app
*iter
;
319 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
320 if (iter
->pid
== pid
) {
330 * find_session_by_uuid
332 * Return a ltt_session structure ptr that matches the uuid.
334 static struct ltt_session
*find_session_by_uuid(uuid_t session_id
)
337 struct ltt_session
*iter
;
339 /* Sanity check for NULL session_id */
340 if (uuid_is_null(session_id
)) {
344 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
345 if (uuid_compare(iter
->uuid
, session_id
) == 0) {
359 * find_session_by_name
361 * Return a ltt_session structure ptr that matches name.
362 * If no session found, NULL is returned.
364 static struct ltt_session
*find_session_by_name(char *name
)
367 struct ltt_session
*iter
;
369 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
370 if (strncmp(iter
->name
, name
, strlen(iter
->name
)) == 0) {
386 * Delete session from the global session list
387 * and free the memory.
389 * Return -1 if no session is found.
390 * On success, return 1;
392 static int destroy_session(uuid_t
*uuid
)
395 struct ltt_session
*iter
;
397 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
398 if (uuid_compare(iter
->uuid
, *uuid
) == 0) {
399 cds_list_del(&iter
->list
);
413 * Create a brand new session and add it to the
414 * global session list.
416 static int create_session(char *name
, uuid_t
*session_id
)
418 struct ltt_session
*new_session
;
420 /* Allocate session data structure */
421 new_session
= malloc(sizeof(struct ltt_session
));
422 if (new_session
== NULL
) {
428 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
432 /* Generate session name based on the session count */
433 if (asprintf(&new_session
->name
, "%s%d", "lttng-", session_count
) < 0) {
438 /* UUID generation */
439 uuid_generate(new_session
->uuid
);
440 uuid_copy(*session_id
, new_session
->uuid
);
442 /* Set consumer (identifier) to 0. This means that there is
443 * NO consumer attach to that session yet.
445 new_session
->ust_consumer
= 0;
446 new_session
->lttng_consumer
= 0;
449 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
450 CDS_INIT_LIST_HEAD(&new_session
->lttng_traces
);
452 /* Add new session to the global session list */
453 cds_list_add(&new_session
->list
, <t_session_list
.head
);
466 * Create an userspace trace using pid.
467 * This trace is then appended to the current session
470 static int ust_create_trace(pid_t pid
)
473 struct ltt_ust_trace
*trace
;
475 trace
= malloc(sizeof(struct ltt_ust_trace
));
486 /* Connect to app using ustctl API */
487 sock
= connect_app(pid
);
489 ret
= LTTCOMM_NO_TRACEABLE
;
493 ret
= ustctl_create_trace(sock
, "auto");
495 ret
= LTTCOMM_CREATE_FAIL
;
499 /* Check if current session is valid */
500 if (current_session
) {
501 cds_list_add(&trace
->list
, ¤t_session
->ust_traces
);
511 * List traceable user-space application and fill an
514 static void get_list_apps(pid_t
*pids
)
517 struct ltt_traceable_app
*iter
;
519 /* TODO: Mutex needed to access this list */
520 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
529 * List sessions and fill the data buffer.
531 static void get_list_sessions(struct lttng_session
*lt
)
534 struct ltt_session
*iter
;
535 struct lttng_session lsess
;
537 /* Iterate over session list and append data after
538 * the control struct in the buffer.
540 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
541 /* Copy name and uuid */
542 uuid_unparse(iter
->uuid
, lsess
.uuid
);
543 strncpy(lsess
.name
, iter
->name
, sizeof(lsess
.name
));
544 lsess
.name
[sizeof(lsess
.name
) - 1] = '\0';
545 memcpy(<
[i
], &lsess
, sizeof(lsess
));
547 /* Reset struct for next pass */
548 memset(&lsess
, 0, sizeof(lsess
));
555 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
557 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
559 llm
->cmd_type
= lsm
->cmd_type
;
563 if (!uuid_is_null(lsm
->session_id
)) {
564 uuid_copy(llm
->session_id
, lsm
->session_id
);
567 strncpy(llm
->trace_name
, lsm
->trace_name
, strlen(llm
->trace_name
));
568 llm
->trace_name
[strlen(llm
->trace_name
) - 1] = '\0';
574 * Setup the outgoing data buffer for the response
575 * data allocating the right amount of memory.
577 * Return total size of the buffer pointed by buf.
579 static int setup_data_buffer(char **buf
, size_t s_data
, struct lttcomm_lttng_msg
*llm
)
584 buf_size
= sizeof(struct lttcomm_lttng_msg
) + s_data
;
585 *buf
= malloc(buf_size
);
592 /* Setup lttcomm_lttng_msg data and copy
593 * it to the newly allocated buffer.
595 llm
->size_payload
= s_data
;
596 memcpy(*buf
, llm
, sizeof(struct lttcomm_lttng_msg
));
607 * This takes the lttcomm_session_msg struct and process the command requested
608 * by the client. It then creates response(s) and send it back to the
609 * given socket (sock).
611 * Return any error encountered or 0 for success.
613 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
617 char *send_buf
= NULL
;
618 struct lttcomm_lttng_msg llm
;
620 /* Copy common data to identify the response
621 * on the lttng client side.
623 copy_common_data(&llm
, lsm
);
625 /* Check command that needs a session */
626 if (lsm
->cmd_type
!= LTTNG_CREATE_SESSION
&&
627 lsm
->cmd_type
!= LTTNG_LIST_SESSIONS
&&
628 lsm
->cmd_type
!= UST_LIST_APPS
)
630 current_session
= find_session_by_uuid(lsm
->session_id
);
631 if (current_session
== NULL
) {
632 ret
= LTTCOMM_SELECT_SESS
;
638 /* Default return code.
639 * In our world, everything is OK... right? ;)
641 llm
.ret_code
= LTTCOMM_OK
;
643 /* Process by command type */
644 switch (lsm
->cmd_type
) {
645 case LTTNG_CREATE_SESSION
:
647 ret
= create_session(lsm
->session_name
, &llm
.session_id
);
652 buf_size
= setup_data_buffer(&send_buf
, 0, &llm
);
660 case LTTNG_DESTROY_SESSION
:
662 ret
= destroy_session(&lsm
->session_id
);
664 ret
= LTTCOMM_NO_SESS
;
669 /* No auxiliary data so only send the llm struct. */
672 case UST_CREATE_TRACE
:
674 ret
= ust_create_trace(lsm
->pid
);
676 ret
= LTTCOMM_CREATE_FAIL
;
680 /* No auxiliary data so only send the llm struct. */
685 /* Stop right now if no apps */
686 if (traceable_app_count
== 0) {
687 ret
= LTTCOMM_NO_APPS
;
691 /* Setup data buffer and details for transmission */
692 buf_size
= setup_data_buffer(&send_buf
,
693 sizeof(pid_t
) * traceable_app_count
, &llm
);
699 get_list_apps((pid_t
*)(send_buf
+ sizeof(struct lttcomm_lttng_msg
)));
703 case LTTNG_LIST_SESSIONS
:
705 /* Stop right now if no session */
706 if (session_count
== 0) {
707 ret
= LTTCOMM_NO_SESS
;
711 /* Setup data buffer and details for transmission */
712 buf_size
= setup_data_buffer(&send_buf
,
713 (sizeof(struct lttng_session
) * session_count
), &llm
);
719 get_list_sessions((struct lttng_session
*)(send_buf
+ sizeof(struct lttcomm_lttng_msg
)));
725 /* Undefined command */
731 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
733 if (send_buf
!= NULL
) {
740 /* Notify client of error */
742 llm
.size_payload
= 0;
743 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
749 * usage function on stderr
751 static void usage(void)
753 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
754 "\t-h, --help\t\tDisplay this usage.\n"
755 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
756 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
757 "\t-d, --daemonize\t\tStart as a daemon.\n"
758 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
759 "\t-V, --version\t\tShow version number.\n"
760 "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n"
761 "\t-q, --quiet\t\tNo output at all.\n",
766 * daemon argument parsing
768 static int parse_args(int argc
, char **argv
)
772 static struct option long_options
[] = {
773 { "client-sock", 1, 0, 'c' },
774 { "apps-sock", 1, 0, 'a' },
775 { "daemonize", 0, 0, 'd' },
776 { "sig-parent", 0, 0, 'S' },
777 { "help", 0, 0, 'h' },
778 { "group", 1, 0, 'g' },
779 { "version", 0, 0, 'V' },
780 { "quiet", 0, 0, 'q' },
785 int option_index
= 0;
786 c
= getopt_long(argc
, argv
, "dhqVS" "a:c:g:s:", long_options
, &option_index
);
793 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
795 fprintf(stderr
, " with arg %s\n", optarg
);
799 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
802 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
808 opt_tracing_group
= strdup(optarg
);
814 fprintf(stdout
, "%s\n", VERSION
);
823 /* Unknown option or other error.
824 * Error is printed by getopt, just return */
835 * Creates the two needed socket by the daemon.
836 * apps_socket - The communication socket for all UST apps.
837 * client_socket - The communication of the cli tool (lttng).
839 static int init_daemon_socket()
844 old_umask
= umask(0);
846 /* Create client tool unix socket */
847 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
848 if (client_socket
< 0) {
853 /* File permission MUST be 660 */
854 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
860 /* Create the application unix socket */
861 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
862 if (apps_socket
< 0) {
867 /* File permission MUST be 660 */
868 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
880 * check_existing_daemon
882 * Check if the global socket is available.
883 * If yes, error is returned.
885 static int check_existing_daemon()
889 ret
= access(client_unix_sock_path
, F_OK
);
891 ret
= access(apps_unix_sock_path
, F_OK
);
900 * Return pointer to home directory path using
901 * the env variable HOME.
905 static const char *get_home_dir(void)
907 const char *home_path
;
909 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
910 home_path
= default_home_dir
;
919 * Set the tracing group gid onto the client socket.
921 static int set_socket_perms(void)
926 /* Decide which group name to use */
927 (opt_tracing_group
!= NULL
) ?
928 (grp
= getgrnam(opt_tracing_group
)) :
929 (grp
= getgrnam(default_tracing_group
));
932 ERR("Missing tracing group. Aborting execution.\n");
937 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
949 * Setup signal handler for :
950 * SIGINT, SIGTERM, SIGPIPE
952 static int set_signal_handler(void)
958 if ((ret
= sigemptyset(&sigset
)) < 0) {
959 perror("sigemptyset");
963 sa
.sa_handler
= sighandler
;
966 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
971 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
976 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
987 * Signal handler for the daemon
989 static void sighandler(int sig
)
1008 * Cleanup the daemon on exit
1010 static void cleanup()
1013 MSG("\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
1014 MSG("%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
1017 unlink(client_unix_sock_path
);
1018 unlink(apps_unix_sock_path
);
1024 int main(int argc
, char **argv
)
1029 pthread_t threads
[2];
1031 /* Parse arguments */
1033 if ((ret
= parse_args(argc
, argv
) < 0)) {
1046 /* Check if daemon is UID = 0 */
1047 is_root
= !getuid();
1049 /* Set all sockets path */
1051 if (strlen(apps_unix_sock_path
) == 0) {
1052 (snprintf(apps_unix_sock_path
, PATH_MAX
,
1053 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
1056 if (strlen(client_unix_sock_path
) == 0) {
1057 (snprintf(client_unix_sock_path
, PATH_MAX
,
1058 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
1061 if (strlen(apps_unix_sock_path
) == 0) {
1062 (snprintf(apps_unix_sock_path
, PATH_MAX
,
1063 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
1066 /* Set the cli tool unix socket path */
1067 if (strlen(client_unix_sock_path
) == 0) {
1068 (snprintf(client_unix_sock_path
, PATH_MAX
,
1069 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
1073 /* See if daemon already exist. If any of the two
1074 * socket needed by the daemon are present, this test fails
1076 if ((ret
= check_existing_daemon()) == 0) {
1077 ERR("Already running daemon.\n");
1081 if (set_signal_handler() < 0) {
1085 /* Setup the two needed unix socket */
1086 if (init_daemon_socket() < 0) {
1090 /* Set credentials to socket */
1091 if (is_root
&& (set_socket_perms() < 0)) {
1095 /* Get parent pid if -S, --sig-parent is specified. */
1096 if (opt_sig_parent
) {
1101 /* Create thread to manage the client socket */
1102 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
1104 perror("pthread_create");
1108 /* Create thread to manage application socket */
1109 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
1111 perror("pthread_create");
1115 for (i
= 0; i
< 2; i
++) {
1116 ret
= pthread_join(threads
[i
], &status
);
1118 perror("pthread_join");
1130 return EXIT_FAILURE
;