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 cleanup(void);
52 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
);
53 static int check_existing_daemon(void);
54 static int notify_apps(const char*);
55 static int connect_app(pid_t
);
56 static int init_daemon_socket(void);
57 static int process_client_msg(int sock
, struct lttcomm_session_msg
*);
58 static int send_unix_sock(int sock
, void *buf
, size_t len
);
59 static size_t ust_list_apps(pid_t
**pids
);
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() */
491 case LTTNG_LIST_SESSIONS
:
493 struct ltt_session
*iter
= NULL
;
495 llm
.num_pckt
= session_count
;
496 if (llm
.num_pckt
== 0) {
497 ret
= LTTCOMM_NO_SESS
;
501 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
502 uuid_unparse(iter
->uuid
, llm
.u
.list_sessions
.uuid
);
503 strncpy(llm
.u
.list_sessions
.name
, iter
->name
,
504 sizeof(llm
.u
.list_sessions
.name
));
505 ret
= send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
516 /* Undefined command */
528 /* Notify client of error */
530 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
536 * usage function on stderr
538 static void usage(void)
540 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
541 "\t-h, --help\t\tDisplay this usage.\n"
542 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
543 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
544 "\t-d, --daemonize\t\tStart as a daemon.\n"
545 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
546 "\t-V, --version\t\tShow version number.\n"
547 "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n",
552 * daemon argument parsing
554 static int parse_args(int argc
, char **argv
)
558 static struct option long_options
[] = {
559 { "client-sock", 1, 0, 'c' },
560 { "apps-sock", 1, 0, 'a' },
561 { "daemonize", 0, 0, 'd' },
562 { "sig-parent", 0, 0, 'S' },
563 { "help", 0, 0, 'h' },
564 { "group", 1, 0, 'g' },
565 { "version", 0, 0, 'V' },
570 int option_index
= 0;
571 c
= getopt_long(argc
, argv
, "dhVS" "a:c:g:s:", long_options
, &option_index
);
578 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
580 fprintf(stderr
, " with arg %s\n", optarg
);
584 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
587 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
593 opt_tracing_group
= strdup(optarg
);
599 fprintf(stdout
, "%s\n", VERSION
);
605 /* Unknown option or other error.
606 * Error is printed by getopt, just return */
617 * Creates the two needed socket by the daemon.
618 * apps_socket - The communication socket for all UST apps.
619 * client_socket - The communication of the cli tool (lttng).
621 static int init_daemon_socket()
626 old_umask
= umask(0);
628 /* Create client tool unix socket */
629 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
630 if (client_socket
< 0) {
635 /* File permission MUST be 660 */
636 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
642 /* Create the application unix socket */
643 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
644 if (apps_socket
< 0) {
649 /* File permission MUST be 660 */
650 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
662 * check_existing_daemon
664 * Check if the global socket is available.
665 * If yes, error is returned.
667 static int check_existing_daemon()
671 ret
= access(client_unix_sock_path
, F_OK
);
673 ret
= access(apps_unix_sock_path
, F_OK
);
682 * Return pointer to home directory path using
683 * the env variable HOME.
687 static const char *get_home_dir(void)
689 const char *home_path
;
691 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
692 home_path
= default_home_dir
;
701 * Set the tracing group gid onto the client socket.
703 static int set_socket_perms(void)
708 /* Decide which group name to use */
709 (opt_tracing_group
!= NULL
) ?
710 (grp
= getgrnam(opt_tracing_group
)) :
711 (grp
= getgrnam(default_tracing_group
));
714 fprintf(stderr
, "Missing tracing group. Aborting execution.\n");
719 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
731 * Setup signal handler for :
732 * SIGINT, SIGTERM, SIGPIPE
734 static int set_signal_handler(void)
740 if ((ret
= sigemptyset(&sigset
)) < 0) {
741 perror("sigemptyset");
745 sa
.sa_handler
= sighandler
;
748 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
753 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
758 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
769 * Signal handler for the daemon
771 static void sighandler(int sig
)
789 * Cleanup the daemon on exit
791 static void cleanup()
794 fprintf(stdout
, "\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
795 fprintf(stdout
, "%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
798 unlink(client_unix_sock_path
);
799 unlink(apps_unix_sock_path
);
805 int main(int argc
, char **argv
)
810 pthread_t threads
[2];
812 /* Parse arguments */
814 if ((ret
= parse_args(argc
, argv
) < 0)) {
827 /* Check if daemon is UID = 0 */
830 /* Set all sockets path */
832 if (strlen(apps_unix_sock_path
) == 0) {
833 (snprintf(apps_unix_sock_path
, PATH_MAX
,
834 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
837 if (strlen(client_unix_sock_path
) == 0) {
838 (snprintf(client_unix_sock_path
, PATH_MAX
,
839 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
842 if (strlen(apps_unix_sock_path
) == 0) {
843 (snprintf(apps_unix_sock_path
, PATH_MAX
,
844 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
847 /* Set the cli tool unix socket path */
848 if (strlen(client_unix_sock_path
) == 0) {
849 (snprintf(client_unix_sock_path
, PATH_MAX
,
850 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
854 /* See if daemon already exist. If any of the two
855 * socket needed by the daemon are present, this test fails
857 if ((ret
= check_existing_daemon()) == 0) {
858 fprintf(stderr
, "Already running daemon.\n");
862 if (set_signal_handler() < 0) {
866 /* Setup the two needed unix socket */
867 if (init_daemon_socket() < 0) {
871 /* Set credentials to socket */
872 if (is_root
&& (set_socket_perms() < 0)) {
876 /* Get parent pid if -S, --sig-parent is specified. */
877 if (opt_sig_parent
) {
882 /* Create thread to manage the client socket */
883 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
885 perror("pthread_create");
889 /* Create thread to manage application socket */
890 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
892 perror("pthread_create");
896 for (i
= 0; i
< 2; i
++) {
897 ret
= pthread_join(threads
[i
], &status
);
899 perror("pthread_join");