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) */
39 #include "liblttsessiondcomm.h"
40 #include "ltt-sessiond.h"
42 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
43 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
44 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
45 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
47 /* Static functions */
48 static int set_signal_handler(void);
49 static int set_socket_perms(void);
50 static void sighandler(int);
51 static void daemonize(void);
52 static void cleanup(void);
53 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
);
54 static int check_existing_daemon(void);
55 static int notify_apps(const char*);
56 static int connect_app(pid_t
);
57 static int init_daemon_socket(void);
58 static int process_client_msg(int sock
, struct lttcomm_session_msg
*);
59 static int send_unix_sock(int sock
, void *buf
, size_t len
);
61 static void *thread_manage_clients(void *);
62 static void *thread_manage_apps(void *);
64 static int create_session(const char*, uuid_t
*);
65 static void destroy_session(uuid_t
);
67 static struct ltt_session
*find_session(uuid_t
);
71 const char *opt_tracing_group
;
72 static int opt_sig_parent
;
73 static int opt_daemon
;
74 static int is_root
; /* Set to 1 if the daemon is running as root */
77 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
78 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
80 static int client_socket
;
81 static int apps_socket
;
83 static struct ltt_session
*current_session
;
85 /* Number of element for the list below. */
86 static unsigned int session_count
;
87 static unsigned int traceable_app_count
;
89 /* Init session's list */
90 static struct ltt_session_list ltt_session_list
= {
91 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
94 /* Init ust traceabl application's list */
95 static struct ltt_traceable_app_list ltt_traceable_app_list
= {
96 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
102 * This thread manage the application socket communication
104 static void *thread_manage_apps(void *data
)
107 struct ltt_traceable_app
*lta
;
109 /* TODO: Something more elegant is needed but fine for now */
111 int reg
; /* 1:register, 0:unregister */
116 /* Notify all applications to register */
117 notify_apps(default_global_apps_pipe
);
119 ret
= lttcomm_listen_unix_sock(apps_socket
);
125 /* Blocking call, waiting for transmission */
126 sock
= lttcomm_accept_unix_sock(apps_socket
);
131 /* Basic recv here to handle the very simple data
132 * that the libust send to register (reg_msg).
134 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
140 /* Add application to the global traceable list */
141 if (reg_msg
.reg
== 1) {
143 lta
= malloc(sizeof(struct ltt_traceable_app
));
144 lta
->pid
= reg_msg
.pid
;
145 lta
->uid
= reg_msg
.uid
;
146 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
147 traceable_app_count
++;
151 cds_list_for_each_entry(lta
, <t_traceable_app_list
.head
, list
) {
152 if (lta
->pid
== reg_msg
.pid
&& lta
->uid
== reg_msg
.uid
) {
153 cds_list_del(<a
->list
);
154 /* Check to not overflow here */
155 if (traceable_app_count
!= 0) {
156 traceable_app_count
--;
162 /* If an item was found, free it from memory */
175 * thread_manage_clients
177 * This thread manage all clients request using the unix
178 * client socket for communication.
180 static void *thread_manage_clients(void *data
)
183 struct lttcomm_session_msg lsm
;
185 ret
= lttcomm_listen_unix_sock(client_socket
);
190 /* Notify parent pid that we are ready
191 * to accept command for client side.
193 if (opt_sig_parent
) {
198 /* Blocking call, waiting for transmission */
199 sock
= lttcomm_accept_unix_sock(client_socket
);
205 * Data is received from the lttng client. The struct
206 * lttcomm_session_msg (lsm) contains the command and data
207 * request of the client.
209 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
214 /* This function dispatch the work to the LTTng or UST libs
215 * and then sends back the response to the client. This is needed
216 * because there might be more then one lttcomm_lttng_msg to
217 * send out so process_client_msg do both jobs.
219 ret
= process_client_msg(sock
, &lsm
);
221 /* Error detected but still accept command */
233 * Send data on a unix socket using the liblttsessiondcomm API.
235 * Return lttcomm error code.
237 static int send_unix_sock(int sock
, void *buf
, size_t len
)
239 /* Check valid length */
244 return lttcomm_send_unix_sock(sock
, buf
, len
);
250 * Return a socket connected to the libust communication socket
251 * of the application identified by the pid.
253 static int connect_app(pid_t pid
)
257 sock
= ustctl_connect_pid(pid
);
259 fprintf(stderr
, "Fail connecting to the PID %d\n", pid
);
268 * Notify apps by writing 42 to a named pipe using name.
269 * Every applications waiting for a ltt-sessiond will be notified
270 * and re-register automatically to the session daemon.
272 * Return open or write error value.
274 static int notify_apps(const char *name
)
279 /* Try opening the global pipe */
280 fd
= open(name
, O_WRONLY
);
285 /* Notify by writing on the pipe */
286 ret
= write(fd
, "42", 2);
298 * Return a ltt_session structure ptr that matches the uuid.
300 static struct ltt_session
*find_session(uuid_t session_id
)
302 struct ltt_session
*iter
= NULL
;
304 /* Sanity check for NULL session_id */
305 if (uuid_is_null(session_id
)) {
309 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
310 if (uuid_compare(iter
->uuid
, session_id
)) {
322 * Delete session from the global session list
323 * and free the memory.
325 static void destroy_session(uuid_t session_id
)
327 struct ltt_session
*iter
= NULL
;
329 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
330 if (uuid_compare(iter
->uuid
, session_id
)) {
331 cds_list_del(&iter
->list
);
345 * Create a brand new session,
347 static int create_session(const char *name
, uuid_t
*session_id
)
349 struct ltt_session
*new_session
;
351 /* Allocate session data structure */
352 new_session
= malloc(sizeof(struct ltt_session
));
353 if (new_session
== NULL
) {
359 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
363 /* Generate session name based on the session count */
364 if (asprintf(&new_session
->name
, "%s%d", "auto", session_count
) < 0) {
369 /* UUID generation */
370 uuid_generate(new_session
->uuid
);
371 uuid_copy(*session_id
, new_session
->uuid
);
373 /* Set consumer (identifier) to 0. This means that there is
374 * NO consumer attach to that session yet.
376 new_session
->ust_consumer
= 0;
377 new_session
->lttng_consumer
= 0;
380 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
381 CDS_INIT_LIST_HEAD(&new_session
->lttng_traces
);
383 /* Add new session to the global session list */
384 cds_list_add(&new_session
->list
, <t_session_list
.head
);
397 * List traceable user-space application and fill an
400 * Return size of the array.
402 static size_t ust_list_apps(pid_t
**pids
)
405 struct ltt_traceable_app
*iter
= NULL
;
408 if (traceable_app_count
== 0) {
409 /* No dynamic allocation is done */
413 p
= malloc(sizeof(pid_t
) * traceable_app_count
);
415 /* TODO: Mutex needed to access this list */
416 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
430 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
432 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
434 llm
->cmd_type
= lsm
->cmd_type
;
436 if (!uuid_is_null(lsm
->session_id
)) {
437 uuid_copy(llm
->session_id
, lsm
->session_id
);
439 strncpy(llm
->trace_name
, lsm
->trace_name
, sizeof(llm
->trace_name
));
445 * This takes the lttcomm_session_msg struct and process the command requested
446 * by the client. It then creates response(s) and send it back to the
447 * given socket (sock).
449 * Return any error encountered or 0 for success.
451 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
454 struct lttcomm_lttng_msg llm
;
456 /* Copy common data to identify the response
457 * on the lttng client side.
459 copy_common_data(&llm
, lsm
);
461 /* Default return code.
462 * In our world, everything is OK... right? ;)
464 llm
.ret_code
= LTTCOMM_OK
;
466 /* Process by command type */
467 switch (lsm
->cmd_type
) {
471 llm
.num_pckt
= ust_list_apps(&pids
);
472 if (llm
.num_pckt
== 0) {
473 ret
= LTTCOMM_NO_APPS
;
477 /* Send all packets */
478 while (llm
.num_pckt
!= 0) {
479 llm
.u
.list_apps
.pid
= pids
[traceable_app_count
- llm
.num_pckt
];
480 ret
= send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
486 /* Allocated array by ust_list_apps() */
493 /* Undefined command */
505 /* Notify client of error */
507 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
513 * usage function on stderr
515 static void usage(void)
517 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
518 "\t-h, --help\t\tDisplay this usage.\n"
519 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
520 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
521 "\t-d, --daemonize\t\tStart as a daemon.\n"
522 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
523 "\t-V, --version\t\tShow version number.\n"
524 "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n",
529 * daemon argument parsing
531 static int parse_args(int argc
, char **argv
)
535 static struct option long_options
[] = {
536 { "client-sock", 1, 0, 'c' },
537 { "apps-sock", 1, 0, 'a' },
538 { "daemonize", 0, 0, 'd' },
539 { "sig-parent", 0, 0, 'S' },
540 { "help", 0, 0, 'h' },
541 { "group", 1, 0, 'g' },
542 { "version", 0, 0, 'V' },
547 int option_index
= 0;
548 c
= getopt_long(argc
, argv
, "dhVS" "a:c:g:s:", long_options
, &option_index
);
555 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
557 fprintf(stderr
, " with arg %s\n", optarg
);
561 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
564 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
570 opt_tracing_group
= strdup(optarg
);
576 fprintf(stdout
, "%s\n", VERSION
);
582 /* Unknown option or other error.
583 * Error is printed by getopt, just return */
594 * Creates the two needed socket by the daemon.
595 * apps_socket - The communication socket for all UST apps.
596 * client_socket - The communication of the cli tool (lttng).
598 static int init_daemon_socket()
603 old_umask
= umask(0);
605 /* Create client tool unix socket */
606 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
607 if (client_socket
< 0) {
612 /* File permission MUST be 660 */
613 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
619 /* Create the application unix socket */
620 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
621 if (apps_socket
< 0) {
626 /* File permission MUST be 660 */
627 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
639 * check_existing_daemon
641 * Check if the global socket is available.
642 * If yes, error is returned.
644 static int check_existing_daemon()
648 ret
= access(client_unix_sock_path
, F_OK
);
650 ret
= access(apps_unix_sock_path
, F_OK
);
659 * Return pointer to home directory path using
660 * the env variable HOME.
664 static const char *get_home_dir(void)
666 const char *home_path
;
668 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
669 home_path
= default_home_dir
;
678 * Set the tracing group gid onto the client socket.
680 static int set_socket_perms(void)
685 /* Decide which group name to use */
686 (opt_tracing_group
!= NULL
) ?
687 (grp
= getgrnam(opt_tracing_group
)) :
688 (grp
= getgrnam(default_tracing_group
));
691 fprintf(stderr
, "Missing tracing group. Aborting execution.\n");
696 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
708 * Daemonize ltt-sessiond.
710 static void daemonize(void)
713 const char *home_dir
= get_home_dir();
715 /* Fork off the parent process */
716 if ((pid
= fork()) < 0) {
721 /* Parent can now exit */
726 /* Change the file mode mask */
729 /* Create a new SID for the child process */
730 if ((sid
= setsid()) < 0) {
735 /* Change the current working directory */
736 if ((chdir(home_dir
)) < 0) {
741 /* Close out the standard file descriptors */
743 close(STDOUT_FILENO
);
744 close(STDERR_FILENO
);
750 * Setup signal handler for :
751 * SIGINT, SIGTERM, SIGPIPE
753 static int set_signal_handler(void)
759 if ((ret
= sigemptyset(&sigset
)) < 0) {
760 perror("sigemptyset");
764 sa
.sa_handler
= sighandler
;
767 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
772 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
777 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
788 * Signal handler for the daemon
790 static void sighandler(int sig
)
808 * Cleanup the daemon on exit
810 static void cleanup()
813 fprintf(stdout
, "\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
814 fprintf(stdout
, "%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
817 unlink(client_unix_sock_path
);
818 unlink(apps_unix_sock_path
);
824 int main(int argc
, char **argv
)
829 pthread_t threads
[2];
831 /* Parse arguments */
833 if ((ret
= parse_args(argc
, argv
) < 0)) {
842 /* Check if daemon is UID = 0 */
845 /* Set all sockets path */
847 if (strlen(apps_unix_sock_path
) == 0) {
848 (snprintf(apps_unix_sock_path
, PATH_MAX
,
849 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
852 if (strlen(client_unix_sock_path
) == 0) {
853 (snprintf(client_unix_sock_path
, PATH_MAX
,
854 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
857 if (strlen(apps_unix_sock_path
) == 0) {
858 (snprintf(apps_unix_sock_path
, PATH_MAX
,
859 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
862 /* Set the cli tool unix socket path */
863 if (strlen(client_unix_sock_path
) == 0) {
864 (snprintf(client_unix_sock_path
, PATH_MAX
,
865 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
869 /* See if daemon already exist. If any of the two
870 * socket needed by the daemon are present, this test fails
872 if ((ret
= check_existing_daemon()) == 0) {
873 fprintf(stderr
, "Already running daemon.\n");
877 if (set_signal_handler() < 0) {
881 /* Setup the two needed unix socket */
882 if (init_daemon_socket() < 0) {
886 /* Set credentials to socket */
887 if (is_root
&& (set_socket_perms() < 0)) {
891 /* Get parent pid if -S, --sig-parent is specified. */
892 if (opt_sig_parent
) {
897 /* Create thread to manage the client socket */
898 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
900 perror("pthread_create");
904 /* Create thread to manage application socket */
905 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
907 perror("pthread_create");
911 for (i
= 0; i
< 2; i
++) {
912 ret
= pthread_join(threads
[i
], &status
);
914 perror("pthread_join");