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 int destroy_session(uuid_t
*uuid
);
74 static struct ltt_session
*find_session_by_uuid(uuid_t session_id
);
75 static struct ltt_session
*find_session_by_name(char *name
);
79 const char *opt_tracing_group
;
80 static int opt_sig_parent
;
81 static int opt_daemon
;
84 static int is_root
; /* Set to 1 if the daemon is running as root */
87 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
88 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
90 static int client_socket
;
91 static int apps_socket
;
93 static struct ltt_session
*current_session
;
95 /* Number of element for the list below. */
96 static unsigned int session_count
;
97 static unsigned int traceable_app_count
;
99 /* Init session's list */
100 static struct ltt_session_list ltt_session_list
= {
101 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
104 /* Init ust traceabl application's list */
105 static struct ltt_traceable_app_list ltt_traceable_app_list
= {
106 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
112 * This thread manage the application socket communication
114 static void *thread_manage_apps(void *data
)
117 struct ltt_traceable_app
*lta
;
119 /* TODO: Something more elegant is needed but fine for now */
121 int reg
; /* 1:register, 0:unregister */
126 /* Notify all applications to register */
127 notify_apps(default_global_apps_pipe
);
129 ret
= lttcomm_listen_unix_sock(apps_socket
);
135 /* Blocking call, waiting for transmission */
136 sock
= lttcomm_accept_unix_sock(apps_socket
);
141 /* Basic recv here to handle the very simple data
142 * that the libust send to register (reg_msg).
144 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
150 /* Add application to the global traceable list */
151 if (reg_msg
.reg
== 1) {
153 lta
= malloc(sizeof(struct ltt_traceable_app
));
154 lta
->pid
= reg_msg
.pid
;
155 lta
->uid
= reg_msg
.uid
;
156 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
157 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
);
164 /* Check to not overflow here */
165 if (traceable_app_count
!= 0) {
166 traceable_app_count
--;
172 /* If an item was found, free it from memory */
185 * thread_manage_clients
187 * This thread manage all clients request using the unix
188 * client socket for communication.
190 static void *thread_manage_clients(void *data
)
193 struct lttcomm_session_msg lsm
;
195 ret
= lttcomm_listen_unix_sock(client_socket
);
200 /* Notify parent pid that we are ready
201 * to accept command for client side.
203 if (opt_sig_parent
) {
208 /* Blocking call, waiting for transmission */
209 sock
= lttcomm_accept_unix_sock(client_socket
);
215 * Data is received from the lttng client. The struct
216 * lttcomm_session_msg (lsm) contains the command and data
217 * request of the client.
219 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
224 /* This function dispatch the work to the LTTng or UST libs
225 * and then sends back the response to the client. This is needed
226 * because there might be more then one lttcomm_lttng_msg to
227 * send out so process_client_msg do both jobs.
229 ret
= process_client_msg(sock
, &lsm
);
231 /* Error detected but still accept command */
243 * Send data on a unix socket using the liblttsessiondcomm API.
245 * Return lttcomm error code.
247 static int send_unix_sock(int sock
, void *buf
, size_t len
)
249 /* Check valid length */
254 return lttcomm_send_unix_sock(sock
, buf
, len
);
260 * Return a socket connected to the libust communication socket
261 * of the application identified by the pid.
263 static int connect_app(pid_t pid
)
267 sock
= ustctl_connect_pid(pid
);
269 ERR("Fail connecting to the PID %d\n", pid
);
278 * Notify apps by writing 42 to a named pipe using name.
279 * Every applications waiting for a ltt-sessiond will be notified
280 * and re-register automatically to the session daemon.
282 * Return open or write error value.
284 static int notify_apps(const char *name
)
289 /* Try opening the global pipe */
290 fd
= open(name
, O_WRONLY
);
295 /* Notify by writing on the pipe */
296 ret
= write(fd
, "42", 2);
306 * find_session_by_uuid
308 * Return a ltt_session structure ptr that matches the uuid.
310 static struct ltt_session
*find_session_by_uuid(uuid_t session_id
)
313 struct ltt_session
*iter
;
315 /* Sanity check for NULL session_id */
316 if (uuid_is_null(session_id
)) {
320 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
321 if (uuid_compare(iter
->uuid
, session_id
) == 0) {
335 * find_session_by_name
337 * Return a ltt_session structure ptr that matches name.
338 * If no session found, NULL is returned.
340 static struct ltt_session
*find_session_by_name(char *name
)
343 struct ltt_session
*iter
;
345 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
346 if (strncmp(iter
->name
, name
, strlen(iter
->name
)) == 0) {
362 * Delete session from the global session list
363 * and free the memory.
365 * Return -1 if no session is found.
366 * On success, return 1;
368 static int destroy_session(uuid_t
*uuid
)
371 struct ltt_session
*iter
;
373 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
374 if (uuid_compare(iter
->uuid
, *uuid
) == 0) {
375 cds_list_del(&iter
->list
);
389 * Create a brand new session and add it to the
390 * global session list.
392 static int create_session(char *name
, uuid_t
*session_id
)
394 struct ltt_session
*new_session
;
396 /* Allocate session data structure */
397 new_session
= malloc(sizeof(struct ltt_session
));
398 if (new_session
== NULL
) {
404 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
408 /* Generate session name based on the session count */
409 if (asprintf(&new_session
->name
, "%s%d", "lttng-", session_count
) < 0) {
414 /* UUID generation */
415 uuid_generate(new_session
->uuid
);
416 uuid_copy(*session_id
, new_session
->uuid
);
418 /* Set consumer (identifier) to 0. This means that there is
419 * NO consumer attach to that session yet.
421 new_session
->ust_consumer
= 0;
422 new_session
->lttng_consumer
= 0;
425 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
426 CDS_INIT_LIST_HEAD(&new_session
->lttng_traces
);
428 /* Add new session to the global session list */
429 cds_list_add(&new_session
->list
, <t_session_list
.head
);
442 * List traceable user-space application and fill an
445 static void get_list_apps(pid_t
*pids
)
448 struct ltt_traceable_app
*iter
;
450 /* TODO: Mutex needed to access this list */
451 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
460 * List sessions and fill the data buffer.
462 static void get_list_sessions(struct lttng_session
*lt
)
465 struct ltt_session
*iter
;
466 struct lttng_session lsess
;
468 /* Iterate over session list and append data after
469 * the control struct in the buffer.
471 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
472 /* Copy name and uuid */
473 uuid_unparse(iter
->uuid
, lsess
.uuid
);
474 strncpy(lsess
.name
, iter
->name
, sizeof(lsess
.name
));
475 lsess
.name
[sizeof(lsess
.name
) - 1] = '\0';
476 memcpy(<
[i
], &lsess
, sizeof(lsess
));
478 /* Reset struct for next pass */
479 memset(&lsess
, 0, sizeof(lsess
));
486 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
488 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
490 llm
->cmd_type
= lsm
->cmd_type
;
494 if (!uuid_is_null(lsm
->session_id
)) {
495 uuid_copy(llm
->session_id
, lsm
->session_id
);
498 strncpy(llm
->trace_name
, lsm
->trace_name
, strlen(llm
->trace_name
));
499 llm
->trace_name
[strlen(llm
->trace_name
) - 1] = '\0';
505 * Setup the outgoing data buffer for the response
506 * data allocating the right amount of memory.
508 * Return total size of the buffer pointed by buf.
510 static int setup_data_buffer(char **buf
, size_t s_data
, struct lttcomm_lttng_msg
*llm
)
515 buf_size
= sizeof(struct lttcomm_lttng_msg
) + s_data
;
516 *buf
= malloc(buf_size
);
523 /* Setup lttcomm_lttng_msg data and copy
524 * it to the newly allocated buffer.
526 llm
->size_payload
= s_data
;
527 memcpy(*buf
, llm
, sizeof(struct lttcomm_lttng_msg
));
538 * This takes the lttcomm_session_msg struct and process the command requested
539 * by the client. It then creates response(s) and send it back to the
540 * given socket (sock).
542 * Return any error encountered or 0 for success.
544 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
548 char *send_buf
= NULL
;
549 struct lttcomm_lttng_msg llm
;
551 /* Copy common data to identify the response
552 * on the lttng client side.
554 copy_common_data(&llm
, lsm
);
556 /* Default return code.
557 * In our world, everything is OK... right? ;)
559 llm
.ret_code
= LTTCOMM_OK
;
561 /* Process by command type */
562 switch (lsm
->cmd_type
) {
563 case LTTNG_CREATE_SESSION
:
565 ret
= create_session(lsm
->session_name
, &llm
.session_id
);
570 buf_size
= setup_data_buffer(&send_buf
, 0, &llm
);
578 case LTTNG_DESTROY_SESSION
:
580 ret
= destroy_session(&lsm
->session_id
);
582 ret
= LTTCOMM_NO_SESS
;
587 /* No auxiliary data so only send the llm struct. */
592 /* Stop right now if no apps */
593 if (traceable_app_count
== 0) {
594 ret
= LTTCOMM_NO_APPS
;
598 /* Setup data buffer and details for transmission */
599 buf_size
= setup_data_buffer(&send_buf
,
600 sizeof(pid_t
) * traceable_app_count
, &llm
);
606 get_list_apps((pid_t
*)(send_buf
+ sizeof(struct lttcomm_lttng_msg
)));
610 case LTTNG_LIST_SESSIONS
:
612 /* Stop right now if no session */
613 if (session_count
== 0) {
614 ret
= LTTCOMM_NO_SESS
;
618 /* Setup data buffer and details for transmission */
619 buf_size
= setup_data_buffer(&send_buf
,
620 (sizeof(struct lttng_session
) * session_count
), &llm
);
626 get_list_sessions((struct lttng_session
*)(send_buf
+ sizeof(struct lttcomm_lttng_msg
)));
632 /* Undefined command */
638 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
640 if (send_buf
!= NULL
) {
647 /* Notify client of error */
649 llm
.size_payload
= 0;
650 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
656 * usage function on stderr
658 static void usage(void)
660 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
661 "\t-h, --help\t\tDisplay this usage.\n"
662 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
663 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
664 "\t-d, --daemonize\t\tStart as a daemon.\n"
665 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
666 "\t-V, --version\t\tShow version number.\n"
667 "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n"
668 "\t-q, --quiet\t\tNo output at all.\n",
673 * daemon argument parsing
675 static int parse_args(int argc
, char **argv
)
679 static struct option long_options
[] = {
680 { "client-sock", 1, 0, 'c' },
681 { "apps-sock", 1, 0, 'a' },
682 { "daemonize", 0, 0, 'd' },
683 { "sig-parent", 0, 0, 'S' },
684 { "help", 0, 0, 'h' },
685 { "group", 1, 0, 'g' },
686 { "version", 0, 0, 'V' },
687 { "quiet", 0, 0, 'q' },
692 int option_index
= 0;
693 c
= getopt_long(argc
, argv
, "dhqVS" "a:c:g:s:", long_options
, &option_index
);
700 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
702 fprintf(stderr
, " with arg %s\n", optarg
);
706 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
709 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
715 opt_tracing_group
= strdup(optarg
);
721 fprintf(stdout
, "%s\n", VERSION
);
730 /* Unknown option or other error.
731 * Error is printed by getopt, just return */
742 * Creates the two needed socket by the daemon.
743 * apps_socket - The communication socket for all UST apps.
744 * client_socket - The communication of the cli tool (lttng).
746 static int init_daemon_socket()
751 old_umask
= umask(0);
753 /* Create client tool unix socket */
754 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
755 if (client_socket
< 0) {
760 /* File permission MUST be 660 */
761 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
767 /* Create the application unix socket */
768 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
769 if (apps_socket
< 0) {
774 /* File permission MUST be 660 */
775 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
787 * check_existing_daemon
789 * Check if the global socket is available.
790 * If yes, error is returned.
792 static int check_existing_daemon()
796 ret
= access(client_unix_sock_path
, F_OK
);
798 ret
= access(apps_unix_sock_path
, F_OK
);
807 * Return pointer to home directory path using
808 * the env variable HOME.
812 static const char *get_home_dir(void)
814 const char *home_path
;
816 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
817 home_path
= default_home_dir
;
826 * Set the tracing group gid onto the client socket.
828 static int set_socket_perms(void)
833 /* Decide which group name to use */
834 (opt_tracing_group
!= NULL
) ?
835 (grp
= getgrnam(opt_tracing_group
)) :
836 (grp
= getgrnam(default_tracing_group
));
839 ERR("Missing tracing group. Aborting execution.\n");
844 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
856 * Setup signal handler for :
857 * SIGINT, SIGTERM, SIGPIPE
859 static int set_signal_handler(void)
865 if ((ret
= sigemptyset(&sigset
)) < 0) {
866 perror("sigemptyset");
870 sa
.sa_handler
= sighandler
;
873 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
878 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
883 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
894 * Signal handler for the daemon
896 static void sighandler(int sig
)
915 * Cleanup the daemon on exit
917 static void cleanup()
920 MSG("\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
921 MSG("%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
924 unlink(client_unix_sock_path
);
925 unlink(apps_unix_sock_path
);
931 int main(int argc
, char **argv
)
936 pthread_t threads
[2];
938 /* Parse arguments */
940 if ((ret
= parse_args(argc
, argv
) < 0)) {
953 /* Check if daemon is UID = 0 */
956 /* Set all sockets path */
958 if (strlen(apps_unix_sock_path
) == 0) {
959 (snprintf(apps_unix_sock_path
, PATH_MAX
,
960 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
963 if (strlen(client_unix_sock_path
) == 0) {
964 (snprintf(client_unix_sock_path
, PATH_MAX
,
965 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
968 if (strlen(apps_unix_sock_path
) == 0) {
969 (snprintf(apps_unix_sock_path
, PATH_MAX
,
970 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
973 /* Set the cli tool unix socket path */
974 if (strlen(client_unix_sock_path
) == 0) {
975 (snprintf(client_unix_sock_path
, PATH_MAX
,
976 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
980 /* See if daemon already exist. If any of the two
981 * socket needed by the daemon are present, this test fails
983 if ((ret
= check_existing_daemon()) == 0) {
984 ERR("Already running daemon.\n");
988 if (set_signal_handler() < 0) {
992 /* Setup the two needed unix socket */
993 if (init_daemon_socket() < 0) {
997 /* Set credentials to socket */
998 if (is_root
&& (set_socket_perms() < 0)) {
1002 /* Get parent pid if -S, --sig-parent is specified. */
1003 if (opt_sig_parent
) {
1008 /* Create thread to manage the client socket */
1009 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
1011 perror("pthread_create");
1015 /* Create thread to manage application socket */
1016 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
1018 perror("pthread_create");
1022 for (i
= 0; i
< 2; i
++) {
1023 ret
= pthread_join(threads
[i
], &status
);
1025 perror("pthread_join");
1037 return EXIT_FAILURE
;