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);
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*);
58 static int connect_app(pid_t
);
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 *);
69 static void *thread_manage_apps(void *);
71 static int create_session(char *name
, uuid_t
*session_id
);
72 static void destroy_session(uuid_t
);
74 static struct ltt_session
*find_session(uuid_t
);
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
);
543 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
552 /* Stop right now if no apps */
553 if (traceable_app_count
== 0) {
554 ret
= LTTCOMM_NO_APPS
;
558 /* Setup data buffer and details for transmission */
559 buf_size
= setup_data_buffer(&send_buf
,
560 sizeof(pid_t
) * traceable_app_count
, &llm
);
566 get_list_apps((pid_t
*)(send_buf
+ sizeof(struct lttcomm_lttng_msg
)));
568 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
575 case LTTNG_LIST_SESSIONS
:
577 /* Stop right now if no session */
578 if (session_count
== 0) {
579 ret
= LTTCOMM_NO_SESS
;
583 /* Setup data buffer and details for transmission */
584 buf_size
= setup_data_buffer(&send_buf
,
585 (sizeof(struct lttng_session
) * session_count
), &llm
);
591 get_list_sessions((struct lttng_session
*)(send_buf
+ sizeof(struct lttcomm_lttng_msg
)));
593 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
602 /* Undefined command */
608 if (send_buf
!= NULL
) {
618 /* Notify client of error */
620 llm
.size_payload
= 0;
621 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
627 * usage function on stderr
629 static void usage(void)
631 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
632 "\t-h, --help\t\tDisplay this usage.\n"
633 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
634 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
635 "\t-d, --daemonize\t\tStart as a daemon.\n"
636 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
637 "\t-V, --version\t\tShow version number.\n"
638 "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n"
639 "\t-q, --quiet\t\tNo output at all.\n",
644 * daemon argument parsing
646 static int parse_args(int argc
, char **argv
)
650 static struct option long_options
[] = {
651 { "client-sock", 1, 0, 'c' },
652 { "apps-sock", 1, 0, 'a' },
653 { "daemonize", 0, 0, 'd' },
654 { "sig-parent", 0, 0, 'S' },
655 { "help", 0, 0, 'h' },
656 { "group", 1, 0, 'g' },
657 { "version", 0, 0, 'V' },
658 { "quiet", 0, 0, 'q' },
663 int option_index
= 0;
664 c
= getopt_long(argc
, argv
, "dhqVS" "a:c:g:s:", long_options
, &option_index
);
671 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
673 fprintf(stderr
, " with arg %s\n", optarg
);
677 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
680 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
686 opt_tracing_group
= strdup(optarg
);
692 fprintf(stdout
, "%s\n", VERSION
);
701 /* Unknown option or other error.
702 * Error is printed by getopt, just return */
713 * Creates the two needed socket by the daemon.
714 * apps_socket - The communication socket for all UST apps.
715 * client_socket - The communication of the cli tool (lttng).
717 static int init_daemon_socket()
722 old_umask
= umask(0);
724 /* Create client tool unix socket */
725 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
726 if (client_socket
< 0) {
731 /* File permission MUST be 660 */
732 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
738 /* Create the application unix socket */
739 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
740 if (apps_socket
< 0) {
745 /* File permission MUST be 660 */
746 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
758 * check_existing_daemon
760 * Check if the global socket is available.
761 * If yes, error is returned.
763 static int check_existing_daemon()
767 ret
= access(client_unix_sock_path
, F_OK
);
769 ret
= access(apps_unix_sock_path
, F_OK
);
778 * Return pointer to home directory path using
779 * the env variable HOME.
783 static const char *get_home_dir(void)
785 const char *home_path
;
787 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
788 home_path
= default_home_dir
;
797 * Set the tracing group gid onto the client socket.
799 static int set_socket_perms(void)
804 /* Decide which group name to use */
805 (opt_tracing_group
!= NULL
) ?
806 (grp
= getgrnam(opt_tracing_group
)) :
807 (grp
= getgrnam(default_tracing_group
));
810 ERR("Missing tracing group. Aborting execution.\n");
815 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
827 * Setup signal handler for :
828 * SIGINT, SIGTERM, SIGPIPE
830 static int set_signal_handler(void)
836 if ((ret
= sigemptyset(&sigset
)) < 0) {
837 perror("sigemptyset");
841 sa
.sa_handler
= sighandler
;
844 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
849 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
854 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
865 * Signal handler for the daemon
867 static void sighandler(int sig
)
885 * Cleanup the daemon on exit
887 static void cleanup()
890 MSG("\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
891 MSG("%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
894 unlink(client_unix_sock_path
);
895 unlink(apps_unix_sock_path
);
901 int main(int argc
, char **argv
)
906 pthread_t threads
[2];
908 /* Parse arguments */
910 if ((ret
= parse_args(argc
, argv
) < 0)) {
923 /* Check if daemon is UID = 0 */
926 /* Set all sockets path */
928 if (strlen(apps_unix_sock_path
) == 0) {
929 (snprintf(apps_unix_sock_path
, PATH_MAX
,
930 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
933 if (strlen(client_unix_sock_path
) == 0) {
934 (snprintf(client_unix_sock_path
, PATH_MAX
,
935 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
938 if (strlen(apps_unix_sock_path
) == 0) {
939 (snprintf(apps_unix_sock_path
, PATH_MAX
,
940 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
943 /* Set the cli tool unix socket path */
944 if (strlen(client_unix_sock_path
) == 0) {
945 (snprintf(client_unix_sock_path
, PATH_MAX
,
946 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
950 /* See if daemon already exist. If any of the two
951 * socket needed by the daemon are present, this test fails
953 if ((ret
= check_existing_daemon()) == 0) {
954 ERR("Already running daemon.\n");
958 if (set_signal_handler() < 0) {
962 /* Setup the two needed unix socket */
963 if (init_daemon_socket() < 0) {
967 /* Set credentials to socket */
968 if (is_root
&& (set_socket_perms() < 0)) {
972 /* Get parent pid if -S, --sig-parent is specified. */
973 if (opt_sig_parent
) {
978 /* Create thread to manage the client socket */
979 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
981 perror("pthread_create");
985 /* Create thread to manage application socket */
986 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
988 perror("pthread_create");
992 for (i
= 0; i
< 2; i
++) {
993 ret
= pthread_join(threads
[i
], &status
);
995 perror("pthread_join");
1007 return EXIT_FAILURE
;