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 init_daemon_socket(void);
60 static int process_client_msg(int sock
, struct lttcomm_session_msg
*);
61 static int send_unix_sock(int sock
, void *buf
, size_t len
);
62 static int setup_data_buffer(char **buf
, size_t size
, struct lttcomm_lttng_msg
*llm
);
64 /* Command function */
65 static void get_list_apps(pid_t
*pids
);
66 static void get_list_sessions(struct lttng_session
*lt
);
68 static void *thread_manage_clients(void *data
);
69 static void *thread_manage_apps(void *data
);
71 static int create_session(char *name
, uuid_t
*session_id
);
72 static void destroy_session(uuid_t session_id
);
74 static struct ltt_session
*find_session(uuid_t session_id
);
78 const char *opt_tracing_group
;
79 static int opt_sig_parent
;
80 static int opt_daemon
;
83 static int is_root
; /* Set to 1 if the daemon is running as root */
86 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
87 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
89 static int client_socket
;
90 static int apps_socket
;
92 static struct ltt_session
*current_session
;
94 /* Number of element for the list below. */
95 static unsigned int session_count
;
96 static unsigned int traceable_app_count
;
98 /* Init session's list */
99 static struct ltt_session_list ltt_session_list
= {
100 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
103 /* Init ust traceabl application's list */
104 static struct ltt_traceable_app_list ltt_traceable_app_list
= {
105 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
111 * This thread manage the application socket communication
113 static void *thread_manage_apps(void *data
)
116 struct ltt_traceable_app
*lta
;
118 /* TODO: Something more elegant is needed but fine for now */
120 int reg
; /* 1:register, 0:unregister */
125 /* Notify all applications to register */
126 notify_apps(default_global_apps_pipe
);
128 ret
= lttcomm_listen_unix_sock(apps_socket
);
134 /* Blocking call, waiting for transmission */
135 sock
= lttcomm_accept_unix_sock(apps_socket
);
140 /* Basic recv here to handle the very simple data
141 * that the libust send to register (reg_msg).
143 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
149 /* Add application to the global traceable list */
150 if (reg_msg
.reg
== 1) {
152 lta
= malloc(sizeof(struct ltt_traceable_app
));
153 lta
->pid
= reg_msg
.pid
;
154 lta
->uid
= reg_msg
.uid
;
155 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
156 traceable_app_count
++;
160 cds_list_for_each_entry(lta
, <t_traceable_app_list
.head
, list
) {
161 if (lta
->pid
== reg_msg
.pid
&& lta
->uid
== reg_msg
.uid
) {
162 cds_list_del(<a
->list
);
163 /* Check to not overflow here */
164 if (traceable_app_count
!= 0) {
165 traceable_app_count
--;
171 /* If an item was found, free it from memory */
184 * thread_manage_clients
186 * This thread manage all clients request using the unix
187 * client socket for communication.
189 static void *thread_manage_clients(void *data
)
192 struct lttcomm_session_msg lsm
;
194 ret
= lttcomm_listen_unix_sock(client_socket
);
199 /* Notify parent pid that we are ready
200 * to accept command for client side.
202 if (opt_sig_parent
) {
207 /* Blocking call, waiting for transmission */
208 sock
= lttcomm_accept_unix_sock(client_socket
);
214 * Data is received from the lttng client. The struct
215 * lttcomm_session_msg (lsm) contains the command and data
216 * request of the client.
218 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
223 /* This function dispatch the work to the LTTng or UST libs
224 * and then sends back the response to the client. This is needed
225 * because there might be more then one lttcomm_lttng_msg to
226 * send out so process_client_msg do both jobs.
228 ret
= process_client_msg(sock
, &lsm
);
230 /* Error detected but still accept command */
242 * Send data on a unix socket using the liblttsessiondcomm API.
244 * Return lttcomm error code.
246 static int send_unix_sock(int sock
, void *buf
, size_t len
)
248 /* Check valid length */
253 return lttcomm_send_unix_sock(sock
, buf
, len
);
259 * Return a socket connected to the libust communication socket
260 * of the application identified by the pid.
262 static int connect_app(pid_t pid
)
266 sock
= ustctl_connect_pid(pid
);
268 ERR("Fail connecting to the PID %d\n", pid
);
277 * Notify apps by writing 42 to a named pipe using name.
278 * Every applications waiting for a ltt-sessiond will be notified
279 * and re-register automatically to the session daemon.
281 * Return open or write error value.
283 static int notify_apps(const char *name
)
288 /* Try opening the global pipe */
289 fd
= open(name
, O_WRONLY
);
294 /* Notify by writing on the pipe */
295 ret
= write(fd
, "42", 2);
307 * Return a ltt_session structure ptr that matches the uuid.
309 static struct ltt_session
*find_session(uuid_t session_id
)
311 struct ltt_session
*iter
= NULL
;
313 /* Sanity check for NULL session_id */
314 if (uuid_is_null(session_id
)) {
318 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
319 if (uuid_compare(iter
->uuid
, session_id
)) {
331 * Delete session from the global session list
332 * and free the memory.
334 static void destroy_session(uuid_t session_id
)
336 struct ltt_session
*iter
= NULL
;
338 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
339 if (uuid_compare(iter
->uuid
, session_id
)) {
340 cds_list_del(&iter
->list
);
354 * Create a brand new session and add it to the
355 * global session list.
357 static int create_session(char *name
, uuid_t
*session_id
)
359 struct ltt_session
*new_session
;
361 /* Allocate session data structure */
362 new_session
= malloc(sizeof(struct ltt_session
));
363 if (new_session
== NULL
) {
369 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
373 /* Generate session name based on the session count */
374 if (asprintf(&new_session
->name
, "%s%d", "auto", session_count
) < 0) {
379 /* UUID generation */
380 uuid_generate(new_session
->uuid
);
381 uuid_copy(*session_id
, new_session
->uuid
);
383 /* Set consumer (identifier) to 0. This means that there is
384 * NO consumer attach to that session yet.
386 new_session
->ust_consumer
= 0;
387 new_session
->lttng_consumer
= 0;
390 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
391 CDS_INIT_LIST_HEAD(&new_session
->lttng_traces
);
393 /* Add new session to the global session list */
394 cds_list_add(&new_session
->list
, <t_session_list
.head
);
407 * List traceable user-space application and fill an
410 static void get_list_apps(pid_t
*pids
)
413 struct ltt_traceable_app
*iter
;
415 /* TODO: Mutex needed to access this list */
416 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
425 * List sessions and fill the data buffer.
427 static void get_list_sessions(struct lttng_session
*lt
)
430 struct ltt_session
*iter
;
431 struct lttng_session lsess
;
433 /* Iterate over session list and append data after
434 * the control struct in the buffer.
436 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
437 /* Copy name and uuid */
438 uuid_unparse(iter
->uuid
, lsess
.uuid
);
439 strncpy(lsess
.name
, iter
->name
, sizeof(lsess
.name
));
440 lsess
.name
[sizeof(lsess
.name
) - 1] = '\0';
441 memcpy(<
[i
], &lsess
, sizeof(lsess
));
443 /* Reset struct for next pass */
444 memset(&lsess
, 0, sizeof(lsess
));
451 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
453 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
455 llm
->cmd_type
= lsm
->cmd_type
;
459 if (lsm
->session_id
!= NULL
) {
460 strncpy(llm
->session_id
, lsm
->session_id
, UUID_STR_LEN
);
463 strncpy(llm
->trace_name
, lsm
->trace_name
, strlen(llm
->trace_name
));
469 * Setup the outgoing data buffer for the response
470 * data allocating the right amount of memory.
472 * Return total size of the buffer pointed by buf.
474 static int setup_data_buffer(char **buf
, size_t s_data
, struct lttcomm_lttng_msg
*llm
)
479 buf_size
= sizeof(struct lttcomm_lttng_msg
) + s_data
;
480 *buf
= malloc(buf_size
);
487 /* Setup lttcomm_lttng_msg data and copy
488 * it to the newly allocated buffer.
490 llm
->size_payload
= s_data
;
491 memcpy(*buf
, llm
, sizeof(struct lttcomm_lttng_msg
));
502 * This takes the lttcomm_session_msg struct and process the command requested
503 * by the client. It then creates response(s) and send it back to the
504 * given socket (sock).
506 * Return any error encountered or 0 for success.
508 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
512 char *send_buf
= NULL
;
513 struct lttcomm_lttng_msg llm
;
515 /* Copy common data to identify the response
516 * on the lttng client side.
518 copy_common_data(&llm
, lsm
);
520 /* Default return code.
521 * In our world, everything is OK... right? ;)
523 llm
.ret_code
= LTTCOMM_OK
;
525 /* Process by command type */
526 switch (lsm
->cmd_type
) {
527 case LTTNG_CREATE_SESSION
:
530 ret
= create_session(lsm
->session_name
, &uuid
);
535 uuid_unparse(uuid
, llm
.session_id
);
537 buf_size
= setup_data_buffer(&send_buf
, 0, &llm
);
548 /* Stop right now if no apps */
549 if (traceable_app_count
== 0) {
550 ret
= LTTCOMM_NO_APPS
;
554 /* Setup data buffer and details for transmission */
555 buf_size
= setup_data_buffer(&send_buf
,
556 sizeof(pid_t
) * traceable_app_count
, &llm
);
562 get_list_apps((pid_t
*)(send_buf
+ sizeof(struct lttcomm_lttng_msg
)));
567 case LTTNG_LIST_SESSIONS
:
569 /* Stop right now if no session */
570 if (session_count
== 0) {
571 ret
= LTTCOMM_NO_SESS
;
575 /* Setup data buffer and details for transmission */
576 buf_size
= setup_data_buffer(&send_buf
,
577 (sizeof(struct lttng_session
) * session_count
), &llm
);
583 get_list_sessions((struct lttng_session
*)(send_buf
+ sizeof(struct lttcomm_lttng_msg
)));
590 /* Undefined command */
597 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
599 if (send_buf
!= NULL
) {
606 /* Notify client of error */
608 llm
.size_payload
= 0;
609 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
615 * usage function on stderr
617 static void usage(void)
619 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
620 "\t-h, --help\t\tDisplay this usage.\n"
621 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
622 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
623 "\t-d, --daemonize\t\tStart as a daemon.\n"
624 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
625 "\t-V, --version\t\tShow version number.\n"
626 "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n"
627 "\t-q, --quiet\t\tNo output at all.\n",
632 * daemon argument parsing
634 static int parse_args(int argc
, char **argv
)
638 static struct option long_options
[] = {
639 { "client-sock", 1, 0, 'c' },
640 { "apps-sock", 1, 0, 'a' },
641 { "daemonize", 0, 0, 'd' },
642 { "sig-parent", 0, 0, 'S' },
643 { "help", 0, 0, 'h' },
644 { "group", 1, 0, 'g' },
645 { "version", 0, 0, 'V' },
646 { "quiet", 0, 0, 'q' },
651 int option_index
= 0;
652 c
= getopt_long(argc
, argv
, "dhqVS" "a:c:g:s:", long_options
, &option_index
);
659 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
661 fprintf(stderr
, " with arg %s\n", optarg
);
665 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
668 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
674 opt_tracing_group
= strdup(optarg
);
680 fprintf(stdout
, "%s\n", VERSION
);
689 /* Unknown option or other error.
690 * Error is printed by getopt, just return */
701 * Creates the two needed socket by the daemon.
702 * apps_socket - The communication socket for all UST apps.
703 * client_socket - The communication of the cli tool (lttng).
705 static int init_daemon_socket()
710 old_umask
= umask(0);
712 /* Create client tool unix socket */
713 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
714 if (client_socket
< 0) {
719 /* File permission MUST be 660 */
720 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
726 /* Create the application unix socket */
727 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
728 if (apps_socket
< 0) {
733 /* File permission MUST be 660 */
734 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
746 * check_existing_daemon
748 * Check if the global socket is available.
749 * If yes, error is returned.
751 static int check_existing_daemon()
755 ret
= access(client_unix_sock_path
, F_OK
);
757 ret
= access(apps_unix_sock_path
, F_OK
);
766 * Return pointer to home directory path using
767 * the env variable HOME.
771 static const char *get_home_dir(void)
773 const char *home_path
;
775 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
776 home_path
= default_home_dir
;
785 * Set the tracing group gid onto the client socket.
787 static int set_socket_perms(void)
792 /* Decide which group name to use */
793 (opt_tracing_group
!= NULL
) ?
794 (grp
= getgrnam(opt_tracing_group
)) :
795 (grp
= getgrnam(default_tracing_group
));
798 ERR("Missing tracing group. Aborting execution.\n");
803 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
815 * Setup signal handler for :
816 * SIGINT, SIGTERM, SIGPIPE
818 static int set_signal_handler(void)
824 if ((ret
= sigemptyset(&sigset
)) < 0) {
825 perror("sigemptyset");
829 sa
.sa_handler
= sighandler
;
832 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
837 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
842 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
853 * Signal handler for the daemon
855 static void sighandler(int sig
)
873 * Cleanup the daemon on exit
875 static void cleanup()
878 MSG("\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
879 MSG("%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
882 unlink(client_unix_sock_path
);
883 unlink(apps_unix_sock_path
);
889 int main(int argc
, char **argv
)
894 pthread_t threads
[2];
896 /* Parse arguments */
898 if ((ret
= parse_args(argc
, argv
) < 0)) {
911 /* Check if daemon is UID = 0 */
914 /* Set all sockets path */
916 if (strlen(apps_unix_sock_path
) == 0) {
917 (snprintf(apps_unix_sock_path
, PATH_MAX
,
918 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
921 if (strlen(client_unix_sock_path
) == 0) {
922 (snprintf(client_unix_sock_path
, PATH_MAX
,
923 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
926 if (strlen(apps_unix_sock_path
) == 0) {
927 (snprintf(apps_unix_sock_path
, PATH_MAX
,
928 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
931 /* Set the cli tool unix socket path */
932 if (strlen(client_unix_sock_path
) == 0) {
933 (snprintf(client_unix_sock_path
, PATH_MAX
,
934 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
938 /* See if daemon already exist. If any of the two
939 * socket needed by the daemon are present, this test fails
941 if ((ret
= check_existing_daemon()) == 0) {
942 ERR("Already running daemon.\n");
946 if (set_signal_handler() < 0) {
950 /* Setup the two needed unix socket */
951 if (init_daemon_socket() < 0) {
955 /* Set credentials to socket */
956 if (is_root
&& (set_socket_perms() < 0)) {
960 /* Get parent pid if -S, --sig-parent is specified. */
961 if (opt_sig_parent
) {
966 /* Create thread to manage the client socket */
967 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
969 perror("pthread_create");
973 /* Create thread to manage application socket */
974 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
976 perror("pthread_create");
980 for (i
= 0; i
< 2; i
++) {
981 ret
= pthread_join(threads
[i
], &status
);
983 perror("pthread_join");