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_daemon
;
73 static int is_root
; /* Set to 1 if the daemon is running as root */
75 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
76 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
78 static int client_socket
;
79 static int apps_socket
;
81 static struct ltt_session
*current_session
;
83 /* Number of element for the list below. */
84 static unsigned int session_count
;
85 static unsigned int traceable_app_count
;
87 /* Init session's list */
88 static struct ltt_session_list ltt_session_list
= {
89 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
92 /* Init ust traceabl application's list */
93 static struct ltt_traceable_app_list ltt_traceable_app_list
= {
94 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
100 * This thread manage the application socket communication
102 static void *thread_manage_apps(void *data
)
105 struct ltt_traceable_app
*lta
;
107 /* TODO: Something more elegant is needed but fine for now */
109 int reg
; /* 1:register, 0:unregister */
114 /* Notify all applications to register */
115 notify_apps(default_global_apps_pipe
);
117 ret
= lttcomm_listen_unix_sock(apps_socket
);
123 /* Blocking call, waiting for transmission */
124 sock
= lttcomm_accept_unix_sock(apps_socket
);
129 /* Basic recv here to handle the very simple data
130 * that the libust send to register (reg_msg).
132 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
138 /* Add application to the global traceable list */
139 if (reg_msg
.reg
== 1) {
141 lta
= malloc(sizeof(struct ltt_traceable_app
));
142 lta
->pid
= reg_msg
.pid
;
143 lta
->uid
= reg_msg
.uid
;
144 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
145 traceable_app_count
++;
149 cds_list_for_each_entry(lta
, <t_traceable_app_list
.head
, list
) {
150 if (lta
->pid
== reg_msg
.pid
&& lta
->uid
== reg_msg
.uid
) {
151 cds_list_del(<a
->list
);
152 /* Check to not overflow here */
153 if (traceable_app_count
!= 0) {
154 traceable_app_count
--;
160 /* If an item was found, free it from memory */
173 * thread_manage_clients
175 * This thread manage all clients request using the unix
176 * client socket for communication.
178 static void *thread_manage_clients(void *data
)
181 struct lttcomm_session_msg lsm
;
183 ret
= lttcomm_listen_unix_sock(client_socket
);
189 /* Blocking call, waiting for transmission */
190 sock
= lttcomm_accept_unix_sock(client_socket
);
196 * Data is received from the lttng client. The struct
197 * lttcomm_session_msg (lsm) contains the command and data
198 * request of the client.
200 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
205 /* This function dispatch the work to the LTTng or UST libs
206 * and then sends back the response to the client. This is needed
207 * because there might be more then one lttcomm_lttng_msg to
208 * send out so process_client_msg do both jobs.
210 ret
= process_client_msg(sock
, &lsm
);
212 /* Error detected but still accept command */
224 * Send data on a unix socket using the liblttsessiondcomm API.
226 * Return lttcomm error code.
228 static int send_unix_sock(int sock
, void *buf
, size_t len
)
230 /* Check valid length */
235 return lttcomm_send_unix_sock(sock
, buf
, len
);
241 * Return a socket connected to the libust communication socket
242 * of the application identified by the pid.
244 static int connect_app(pid_t pid
)
248 sock
= ustctl_connect_pid(pid
);
250 fprintf(stderr
, "Fail connecting to the PID %d\n", pid
);
259 * Notify apps by writing 42 to a named pipe using name.
260 * Every applications waiting for a ltt-sessiond will be notified
261 * and re-register automatically to the session daemon.
263 * Return open or write error value.
265 static int notify_apps(const char *name
)
270 /* Try opening the global pipe */
271 fd
= open(name
, O_WRONLY
);
276 /* Notify by writing on the pipe */
277 ret
= write(fd
, "42", 2);
289 * Return a ltt_session structure ptr that matches the uuid.
291 static struct ltt_session
*find_session(uuid_t session_id
)
293 struct ltt_session
*iter
= NULL
;
295 /* Sanity check for NULL session_id */
296 if (uuid_is_null(session_id
)) {
300 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
301 if (uuid_compare(iter
->uuid
, session_id
)) {
313 * Delete session from the global session list
314 * and free the memory.
316 static void destroy_session(uuid_t session_id
)
318 struct ltt_session
*iter
= NULL
;
320 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
321 if (uuid_compare(iter
->uuid
, session_id
)) {
322 cds_list_del(&iter
->list
);
336 * Create a brand new session,
338 static int create_session(const char *name
, uuid_t
*session_id
)
340 struct ltt_session
*new_session
;
342 /* Allocate session data structure */
343 new_session
= malloc(sizeof(struct ltt_session
));
344 if (new_session
== NULL
) {
350 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
354 /* Generate session name based on the session count */
355 if (asprintf(&new_session
->name
, "%s%d", "auto", session_count
) < 0) {
360 /* UUID generation */
361 uuid_generate(new_session
->uuid
);
362 uuid_copy(*session_id
, new_session
->uuid
);
364 /* Set consumer (identifier) to 0. This means that there is
365 * NO consumer attach to that session yet.
367 new_session
->ust_consumer
= 0;
368 new_session
->lttng_consumer
= 0;
371 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
372 CDS_INIT_LIST_HEAD(&new_session
->lttng_traces
);
374 /* Add new session to the global session list */
375 cds_list_add(&new_session
->list
, <t_session_list
.head
);
388 * List traceable user-space application and fill an
391 * Return size of the array.
393 static size_t ust_list_apps(pid_t
**pids
)
396 struct ltt_traceable_app
*iter
= NULL
;
399 if (traceable_app_count
== 0) {
400 /* No dynamic allocation is done */
404 p
= malloc(sizeof(pid_t
) * traceable_app_count
);
406 /* TODO: Mutex needed to access this list */
407 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
421 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
423 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
425 llm
->cmd_type
= lsm
->cmd_type
;
427 if (!uuid_is_null(lsm
->session_id
)) {
428 uuid_copy(llm
->session_id
, lsm
->session_id
);
430 strncpy(llm
->trace_name
, lsm
->trace_name
, sizeof(llm
->trace_name
));
436 * This takes the lttcomm_session_msg struct and process the command requested
437 * by the client. It then creates response(s) and send it back to the
438 * given socket (sock).
440 * Return any error encountered or 0 for success.
442 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
445 struct lttcomm_lttng_msg llm
;
447 /* Copy common data to identify the response
448 * on the lttng client side.
450 copy_common_data(&llm
, lsm
);
452 /* Default return code.
453 * In our world, everything is OK... right? ;)
455 llm
.ret_code
= LTTCOMM_OK
;
457 /* Process by command type */
458 switch (lsm
->cmd_type
) {
462 llm
.num_pckt
= ust_list_apps(&pids
);
463 if (llm
.num_pckt
== 0) {
464 ret
= LTTCOMM_NO_APPS
;
468 /* Send all packets */
469 while (llm
.num_pckt
!= 0) {
470 llm
.u
.list_apps
.pid
= pids
[traceable_app_count
- llm
.num_pckt
];
471 ret
= send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
477 /* Allocated array by ust_list_apps() */
484 /* Undefined command */
496 /* Notify client of error */
498 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
504 * usage function on stderr
506 static void usage(void)
508 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
509 "\t-h, --help\t\tDisplay this usage.\n"
510 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
511 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
512 "\t-d, --daemonize\t\tStart as a daemon.\n"
513 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
514 "\t-V, --version\t\tShow version number.\n",
519 * daemon argument parsing
521 static int parse_args(int argc
, char **argv
)
525 static struct option long_options
[] = {
526 { "client-sock", 1, 0, 'c' },
527 { "apps-sock", 1, 0, 'a' },
528 { "daemonize", 0, 0, 'd' },
529 { "help", 0, 0, 'h' },
530 { "group", 1, 0, 'g' },
531 { "version", 0, 0, 'V' },
536 int option_index
= 0;
537 c
= getopt_long(argc
, argv
, "dhV" "a:c:g:s:", long_options
, &option_index
);
544 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
546 fprintf(stderr
, " with arg %s\n", optarg
);
550 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
553 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
559 opt_tracing_group
= strdup(optarg
);
565 fprintf(stdout
, "%s\n", VERSION
);
568 /* Unknown option or other error.
569 * Error is printed by getopt, just return */
580 * Creates the two needed socket by the daemon.
581 * apps_socket - The communication socket for all UST apps.
582 * client_socket - The communication of the cli tool (lttng).
584 static int init_daemon_socket()
589 old_umask
= umask(0);
591 /* Create client tool unix socket */
592 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
593 if (client_socket
< 0) {
598 /* File permission MUST be 660 */
599 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
605 /* Create the application unix socket */
606 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
607 if (apps_socket
< 0) {
612 /* File permission MUST be 660 */
613 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
625 * check_existing_daemon
627 * Check if the global socket is available.
628 * If yes, error is returned.
630 static int check_existing_daemon()
634 ret
= access(client_unix_sock_path
, F_OK
);
636 ret
= access(apps_unix_sock_path
, F_OK
);
645 * Return pointer to home directory path using
646 * the env variable HOME.
650 static const char *get_home_dir(void)
652 const char *home_path
;
654 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
655 home_path
= default_home_dir
;
664 * Set the tracing group gid onto the client socket.
666 static int set_socket_perms(void)
671 /* Decide which group name to use */
672 (opt_tracing_group
!= NULL
) ?
673 (grp
= getgrnam(opt_tracing_group
)) :
674 (grp
= getgrnam(default_tracing_group
));
677 fprintf(stderr
, "Missing tracing group. Aborting execution.\n");
682 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
694 * Daemonize ltt-sessiond.
696 static void daemonize(void)
699 const char *home_dir
= get_home_dir();
701 /* Fork off the parent process */
702 if ((pid
= fork()) < 0) {
707 /* Parent can now exit */
712 /* Change the file mode mask */
715 /* Create a new SID for the child process */
716 if ((sid
= setsid()) < 0) {
721 /* Change the current working directory */
722 if ((chdir(home_dir
)) < 0) {
727 /* Close out the standard file descriptors */
729 close(STDOUT_FILENO
);
730 close(STDERR_FILENO
);
736 * Setup signal handler for :
737 * SIGINT, SIGTERM, SIGPIPE
739 static int set_signal_handler(void)
745 if ((ret
= sigemptyset(&sigset
)) < 0) {
746 perror("sigemptyset");
750 sa
.sa_handler
= sighandler
;
753 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
758 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
763 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
774 * Signal handler for the daemon
776 static void sighandler(int sig
)
794 * Cleanup the daemon on exit
796 static void cleanup()
799 fprintf(stdout
, "\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
800 fprintf(stdout
, "%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
803 unlink(client_unix_sock_path
);
804 unlink(apps_unix_sock_path
);
810 int main(int argc
, char **argv
)
815 pthread_t threads
[2];
817 /* Parse arguments */
819 if ((ret
= parse_args(argc
, argv
) < 0)) {
828 /* Check if daemon is UID = 0 */
831 /* Set all sockets path */
833 if (strlen(apps_unix_sock_path
) == 0) {
834 (snprintf(apps_unix_sock_path
, PATH_MAX
,
835 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
838 if (strlen(client_unix_sock_path
) == 0) {
839 (snprintf(client_unix_sock_path
, PATH_MAX
,
840 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
843 if (strlen(apps_unix_sock_path
) == 0) {
844 (snprintf(apps_unix_sock_path
, PATH_MAX
,
845 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
848 /* Set the cli tool unix socket path */
849 if (strlen(client_unix_sock_path
) == 0) {
850 (snprintf(client_unix_sock_path
, PATH_MAX
,
851 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
855 /* See if daemon already exist. If any of the two
856 * socket needed by the daemon are present, this test fails
858 if ((ret
= check_existing_daemon()) == 0) {
859 fprintf(stderr
, "Already running daemon.\n");
863 if (set_signal_handler() < 0) {
867 /* Setup the two needed unix socket */
868 if (init_daemon_socket() < 0) {
872 /* Set credentials to socket */
873 if (is_root
&& (set_socket_perms() < 0)) {
878 /* Create thread to manage the client socket */
879 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
881 perror("pthread_create");
885 /* Create thread to manage application socket */
886 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
888 perror("pthread_create");
892 for (i
= 0; i
< 2; i
++) {
893 ret
= pthread_join(threads
[i
], &status
);
895 perror("pthread_join");