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
);
63 /* Command function */
64 static void get_list_apps(void *buf
);
65 static void get_list_sessions(void *buf
);
67 static void *thread_manage_clients(void *);
68 static void *thread_manage_apps(void *);
70 static int create_session(const char*, uuid_t
*);
71 static void destroy_session(uuid_t
);
73 static struct ltt_session
*find_session(uuid_t
);
77 const char *opt_tracing_group
;
78 static int opt_sig_parent
;
79 static int opt_daemon
;
82 static int is_root
; /* Set to 1 if the daemon is running as root */
85 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
86 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
88 static int client_socket
;
89 static int apps_socket
;
91 static struct ltt_session
*current_session
;
93 /* Number of element for the list below. */
94 static unsigned int session_count
;
95 static unsigned int traceable_app_count
;
97 /* Init session's list */
98 static struct ltt_session_list ltt_session_list
= {
99 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
102 /* Init ust traceabl application's list */
103 static struct ltt_traceable_app_list ltt_traceable_app_list
= {
104 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
110 * This thread manage the application socket communication
112 static void *thread_manage_apps(void *data
)
115 struct ltt_traceable_app
*lta
;
117 /* TODO: Something more elegant is needed but fine for now */
119 int reg
; /* 1:register, 0:unregister */
124 /* Notify all applications to register */
125 notify_apps(default_global_apps_pipe
);
127 ret
= lttcomm_listen_unix_sock(apps_socket
);
133 /* Blocking call, waiting for transmission */
134 sock
= lttcomm_accept_unix_sock(apps_socket
);
139 /* Basic recv here to handle the very simple data
140 * that the libust send to register (reg_msg).
142 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
148 /* Add application to the global traceable list */
149 if (reg_msg
.reg
== 1) {
151 lta
= malloc(sizeof(struct ltt_traceable_app
));
152 lta
->pid
= reg_msg
.pid
;
153 lta
->uid
= reg_msg
.uid
;
154 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
155 traceable_app_count
++;
159 cds_list_for_each_entry(lta
, <t_traceable_app_list
.head
, list
) {
160 if (lta
->pid
== reg_msg
.pid
&& lta
->uid
== reg_msg
.uid
) {
161 cds_list_del(<a
->list
);
162 /* Check to not overflow here */
163 if (traceable_app_count
!= 0) {
164 traceable_app_count
--;
170 /* If an item was found, free it from memory */
183 * thread_manage_clients
185 * This thread manage all clients request using the unix
186 * client socket for communication.
188 static void *thread_manage_clients(void *data
)
191 struct lttcomm_session_msg lsm
;
193 ret
= lttcomm_listen_unix_sock(client_socket
);
198 /* Notify parent pid that we are ready
199 * to accept command for client side.
201 if (opt_sig_parent
) {
206 /* Blocking call, waiting for transmission */
207 sock
= lttcomm_accept_unix_sock(client_socket
);
213 * Data is received from the lttng client. The struct
214 * lttcomm_session_msg (lsm) contains the command and data
215 * request of the client.
217 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
222 /* This function dispatch the work to the LTTng or UST libs
223 * and then sends back the response to the client. This is needed
224 * because there might be more then one lttcomm_lttng_msg to
225 * send out so process_client_msg do both jobs.
227 ret
= process_client_msg(sock
, &lsm
);
229 /* Error detected but still accept command */
241 * Send data on a unix socket using the liblttsessiondcomm API.
243 * Return lttcomm error code.
245 static int send_unix_sock(int sock
, void *buf
, size_t len
)
247 /* Check valid length */
252 return lttcomm_send_unix_sock(sock
, buf
, len
);
258 * Return a socket connected to the libust communication socket
259 * of the application identified by the pid.
261 static int connect_app(pid_t pid
)
265 sock
= ustctl_connect_pid(pid
);
267 ERR("Fail connecting to the PID %d\n", pid
);
276 * Notify apps by writing 42 to a named pipe using name.
277 * Every applications waiting for a ltt-sessiond will be notified
278 * and re-register automatically to the session daemon.
280 * Return open or write error value.
282 static int notify_apps(const char *name
)
287 /* Try opening the global pipe */
288 fd
= open(name
, O_WRONLY
);
293 /* Notify by writing on the pipe */
294 ret
= write(fd
, "42", 2);
306 * Return a ltt_session structure ptr that matches the uuid.
308 static struct ltt_session
*find_session(uuid_t session_id
)
310 struct ltt_session
*iter
= NULL
;
312 /* Sanity check for NULL session_id */
313 if (uuid_is_null(session_id
)) {
317 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
318 if (uuid_compare(iter
->uuid
, session_id
)) {
330 * Delete session from the global session list
331 * and free the memory.
333 static void destroy_session(uuid_t session_id
)
335 struct ltt_session
*iter
= NULL
;
337 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
338 if (uuid_compare(iter
->uuid
, session_id
)) {
339 cds_list_del(&iter
->list
);
353 * Create a brand new session,
355 static int create_session(const char *name
, uuid_t
*session_id
)
357 struct ltt_session
*new_session
;
359 /* Allocate session data structure */
360 new_session
= malloc(sizeof(struct ltt_session
));
361 if (new_session
== NULL
) {
367 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
371 /* Generate session name based on the session count */
372 if (asprintf(&new_session
->name
, "%s%d", "auto", session_count
) < 0) {
377 /* UUID generation */
378 uuid_generate(new_session
->uuid
);
379 uuid_copy(*session_id
, new_session
->uuid
);
381 /* Set consumer (identifier) to 0. This means that there is
382 * NO consumer attach to that session yet.
384 new_session
->ust_consumer
= 0;
385 new_session
->lttng_consumer
= 0;
388 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
389 CDS_INIT_LIST_HEAD(&new_session
->lttng_traces
);
391 /* Add new session to the global session list */
392 cds_list_add(&new_session
->list
, <t_session_list
.head
);
405 * List traceable user-space application and fill an
408 static void get_list_apps(void *buf
)
411 struct ltt_traceable_app
*iter
= NULL
;
412 pid_t
*pids
= (pid_t
*) buf
;
414 /* TODO: Mutex needed to access this list */
415 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
416 pids
[index
] = iter
->pid
;
424 * List sessions and fill the data buffer.
426 static void get_list_sessions(void *buf
)
429 struct ltt_session
*iter
= NULL
;
430 struct lttng_session lsess
;
431 struct lttng_session
*data
= (struct lttng_session
*) buf
;
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 uuid_unparse(iter
->uuid
, lsess
.uuid
);
438 strncpy(lsess
.name
, iter
->name
, sizeof(lsess
.name
));
439 memcpy(data
+ (i
* sizeof(struct lttng_session
)), &lsess
, sizeof(lsess
));
441 /* Reset struct for next pass */
442 memset(&lsess
, 0, sizeof(lsess
));
449 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
451 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
453 llm
->cmd_type
= lsm
->cmd_type
;
455 if (!uuid_is_null(lsm
->session_id
)) {
456 uuid_copy(llm
->session_id
, lsm
->session_id
);
458 strncpy(llm
->trace_name
, lsm
->trace_name
, sizeof(llm
->trace_name
));
464 * Setup the outgoing data buffer for the response
465 * data allocating the right amount of memory.
467 * Return total size of the buffer pointed by buf.
469 static int setup_data_buffer(void **buf
, size_t s_data
, struct lttcomm_lttng_msg
*llm
)
475 buf_size
= sizeof(struct lttcomm_lttng_msg
) + s_data
;
476 new_buf
= malloc(buf_size
);
477 if (new_buf
== NULL
) {
483 /* Setup lttcomm_lttng_msg data and copy
484 * it to the newly allocated buffer.
486 llm
->size_payload
= s_data
;
487 memcpy(new_buf
, llm
, sizeof(struct lttcomm_lttng_msg
));
500 * This takes the lttcomm_session_msg struct and process the command requested
501 * by the client. It then creates response(s) and send it back to the
502 * given socket (sock).
504 * Return any error encountered or 0 for success.
506 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
509 struct lttcomm_lttng_msg llm
;
510 void *send_buf
= NULL
;
513 /* Copy common data to identify the response
514 * on the lttng client side.
516 copy_common_data(&llm
, lsm
);
518 /* Default return code.
519 * In our world, everything is OK... right? ;)
521 llm
.ret_code
= LTTCOMM_OK
;
523 /* Process by command type */
524 switch (lsm
->cmd_type
) {
527 /* Stop right now if no apps */
528 if (traceable_app_count
== 0) {
529 ret
= LTTCOMM_NO_APPS
;
533 /* Setup data buffer and details for transmission */
534 buf_size
= setup_data_buffer(&send_buf
,
535 sizeof(pid_t
) * traceable_app_count
, &llm
);
541 get_list_apps(send_buf
+ sizeof(struct lttcomm_lttng_msg
));
543 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
550 case LTTNG_LIST_SESSIONS
:
552 /* Stop right now if no session */
553 if (session_count
== 0) {
554 ret
= LTTCOMM_NO_SESS
;
558 /* Setup data buffer and details for transmission */
559 buf_size
= setup_data_buffer(&send_buf
,
560 (sizeof(struct lttng_session
) * session_count
), &llm
);
566 get_list_sessions(send_buf
+ sizeof(struct lttcomm_lttng_msg
));
568 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
577 /* Undefined command */
583 if (send_buf
!= NULL
) {
593 /* Notify client of error */
595 llm
.size_payload
= 0;
596 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
602 * usage function on stderr
604 static void usage(void)
606 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
607 "\t-h, --help\t\tDisplay this usage.\n"
608 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
609 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
610 "\t-d, --daemonize\t\tStart as a daemon.\n"
611 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
612 "\t-V, --version\t\tShow version number.\n"
613 "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n"
614 "\t-q, --quiet\t\tNo output at all.\n",
619 * daemon argument parsing
621 static int parse_args(int argc
, char **argv
)
625 static struct option long_options
[] = {
626 { "client-sock", 1, 0, 'c' },
627 { "apps-sock", 1, 0, 'a' },
628 { "daemonize", 0, 0, 'd' },
629 { "sig-parent", 0, 0, 'S' },
630 { "help", 0, 0, 'h' },
631 { "group", 1, 0, 'g' },
632 { "version", 0, 0, 'V' },
633 { "quiet", 0, 0, 'q' },
638 int option_index
= 0;
639 c
= getopt_long(argc
, argv
, "dhqVS" "a:c:g:s:", long_options
, &option_index
);
646 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
648 fprintf(stderr
, " with arg %s\n", optarg
);
652 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
655 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
661 opt_tracing_group
= strdup(optarg
);
667 fprintf(stdout
, "%s\n", VERSION
);
676 /* Unknown option or other error.
677 * Error is printed by getopt, just return */
688 * Creates the two needed socket by the daemon.
689 * apps_socket - The communication socket for all UST apps.
690 * client_socket - The communication of the cli tool (lttng).
692 static int init_daemon_socket()
697 old_umask
= umask(0);
699 /* Create client tool unix socket */
700 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
701 if (client_socket
< 0) {
706 /* File permission MUST be 660 */
707 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
713 /* Create the application unix socket */
714 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
715 if (apps_socket
< 0) {
720 /* File permission MUST be 660 */
721 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
733 * check_existing_daemon
735 * Check if the global socket is available.
736 * If yes, error is returned.
738 static int check_existing_daemon()
742 ret
= access(client_unix_sock_path
, F_OK
);
744 ret
= access(apps_unix_sock_path
, F_OK
);
753 * Return pointer to home directory path using
754 * the env variable HOME.
758 static const char *get_home_dir(void)
760 const char *home_path
;
762 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
763 home_path
= default_home_dir
;
772 * Set the tracing group gid onto the client socket.
774 static int set_socket_perms(void)
779 /* Decide which group name to use */
780 (opt_tracing_group
!= NULL
) ?
781 (grp
= getgrnam(opt_tracing_group
)) :
782 (grp
= getgrnam(default_tracing_group
));
785 ERR("Missing tracing group. Aborting execution.\n");
790 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
802 * Setup signal handler for :
803 * SIGINT, SIGTERM, SIGPIPE
805 static int set_signal_handler(void)
811 if ((ret
= sigemptyset(&sigset
)) < 0) {
812 perror("sigemptyset");
816 sa
.sa_handler
= sighandler
;
819 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
824 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
829 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
840 * Signal handler for the daemon
842 static void sighandler(int sig
)
860 * Cleanup the daemon on exit
862 static void cleanup()
865 MSG("\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
866 MSG("%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
869 unlink(client_unix_sock_path
);
870 unlink(apps_unix_sock_path
);
876 int main(int argc
, char **argv
)
881 pthread_t threads
[2];
883 /* Parse arguments */
885 if ((ret
= parse_args(argc
, argv
) < 0)) {
898 /* Check if daemon is UID = 0 */
901 /* Set all sockets path */
903 if (strlen(apps_unix_sock_path
) == 0) {
904 (snprintf(apps_unix_sock_path
, PATH_MAX
,
905 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
908 if (strlen(client_unix_sock_path
) == 0) {
909 (snprintf(client_unix_sock_path
, PATH_MAX
,
910 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
913 if (strlen(apps_unix_sock_path
) == 0) {
914 (snprintf(apps_unix_sock_path
, PATH_MAX
,
915 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
918 /* Set the cli tool unix socket path */
919 if (strlen(client_unix_sock_path
) == 0) {
920 (snprintf(client_unix_sock_path
, PATH_MAX
,
921 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
925 /* See if daemon already exist. If any of the two
926 * socket needed by the daemon are present, this test fails
928 if ((ret
= check_existing_daemon()) == 0) {
929 ERR("Already running daemon.\n");
933 if (set_signal_handler() < 0) {
937 /* Setup the two needed unix socket */
938 if (init_daemon_socket() < 0) {
942 /* Set credentials to socket */
943 if (is_root
&& (set_socket_perms() < 0)) {
947 /* Get parent pid if -S, --sig-parent is specified. */
948 if (opt_sig_parent
) {
953 /* Create thread to manage the client socket */
954 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
956 perror("pthread_create");
960 /* Create thread to manage application socket */
961 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
963 perror("pthread_create");
967 for (i
= 0; i
< 2; i
++) {
968 ret
= pthread_join(threads
[i
], &status
);
970 perror("pthread_join");