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"
44 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
45 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
46 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
47 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
49 /* Static functions */
50 static int set_signal_handler(void);
51 static int set_socket_perms(void);
52 static void sighandler(int);
53 static void cleanup(void);
54 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
);
55 static int check_existing_daemon(void);
56 static int notify_apps(const char*);
57 static int connect_app(pid_t
);
58 static int init_daemon_socket(void);
59 static int process_client_msg(int sock
, struct lttcomm_session_msg
*);
60 static int send_unix_sock(int sock
, void *buf
, size_t len
);
61 static size_t ust_list_apps(pid_t
**pids
);
63 static void *thread_manage_clients(void *);
64 static void *thread_manage_apps(void *);
66 static int create_session(const char*, uuid_t
*);
67 static void destroy_session(uuid_t
);
69 static struct ltt_session
*find_session(uuid_t
);
73 const char *opt_tracing_group
;
74 static int opt_sig_parent
;
75 static int opt_daemon
;
78 static int is_root
; /* Set to 1 if the daemon is running as root */
81 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
82 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
84 static int client_socket
;
85 static int apps_socket
;
87 static struct ltt_session
*current_session
;
89 /* Number of element for the list below. */
90 static unsigned int session_count
;
91 static unsigned int traceable_app_count
;
93 /* Init session's list */
94 static struct ltt_session_list ltt_session_list
= {
95 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
98 /* Init ust traceabl application's list */
99 static struct ltt_traceable_app_list ltt_traceable_app_list
= {
100 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
106 * This thread manage the application socket communication
108 static void *thread_manage_apps(void *data
)
111 struct ltt_traceable_app
*lta
;
113 /* TODO: Something more elegant is needed but fine for now */
115 int reg
; /* 1:register, 0:unregister */
120 /* Notify all applications to register */
121 notify_apps(default_global_apps_pipe
);
123 ret
= lttcomm_listen_unix_sock(apps_socket
);
129 /* Blocking call, waiting for transmission */
130 sock
= lttcomm_accept_unix_sock(apps_socket
);
135 /* Basic recv here to handle the very simple data
136 * that the libust send to register (reg_msg).
138 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
144 /* Add application to the global traceable list */
145 if (reg_msg
.reg
== 1) {
147 lta
= malloc(sizeof(struct ltt_traceable_app
));
148 lta
->pid
= reg_msg
.pid
;
149 lta
->uid
= reg_msg
.uid
;
150 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
151 traceable_app_count
++;
155 cds_list_for_each_entry(lta
, <t_traceable_app_list
.head
, list
) {
156 if (lta
->pid
== reg_msg
.pid
&& lta
->uid
== reg_msg
.uid
) {
157 cds_list_del(<a
->list
);
158 /* Check to not overflow here */
159 if (traceable_app_count
!= 0) {
160 traceable_app_count
--;
166 /* If an item was found, free it from memory */
179 * thread_manage_clients
181 * This thread manage all clients request using the unix
182 * client socket for communication.
184 static void *thread_manage_clients(void *data
)
187 struct lttcomm_session_msg lsm
;
189 ret
= lttcomm_listen_unix_sock(client_socket
);
194 /* Notify parent pid that we are ready
195 * to accept command for client side.
197 if (opt_sig_parent
) {
202 /* Blocking call, waiting for transmission */
203 sock
= lttcomm_accept_unix_sock(client_socket
);
209 * Data is received from the lttng client. The struct
210 * lttcomm_session_msg (lsm) contains the command and data
211 * request of the client.
213 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
218 /* This function dispatch the work to the LTTng or UST libs
219 * and then sends back the response to the client. This is needed
220 * because there might be more then one lttcomm_lttng_msg to
221 * send out so process_client_msg do both jobs.
223 ret
= process_client_msg(sock
, &lsm
);
225 /* Error detected but still accept command */
237 * Send data on a unix socket using the liblttsessiondcomm API.
239 * Return lttcomm error code.
241 static int send_unix_sock(int sock
, void *buf
, size_t len
)
243 /* Check valid length */
248 return lttcomm_send_unix_sock(sock
, buf
, len
);
254 * Return a socket connected to the libust communication socket
255 * of the application identified by the pid.
257 static int connect_app(pid_t pid
)
261 sock
= ustctl_connect_pid(pid
);
263 ERR("Fail connecting to the PID %d\n", pid
);
272 * Notify apps by writing 42 to a named pipe using name.
273 * Every applications waiting for a ltt-sessiond will be notified
274 * and re-register automatically to the session daemon.
276 * Return open or write error value.
278 static int notify_apps(const char *name
)
283 /* Try opening the global pipe */
284 fd
= open(name
, O_WRONLY
);
289 /* Notify by writing on the pipe */
290 ret
= write(fd
, "42", 2);
302 * Return a ltt_session structure ptr that matches the uuid.
304 static struct ltt_session
*find_session(uuid_t session_id
)
306 struct ltt_session
*iter
= NULL
;
308 /* Sanity check for NULL session_id */
309 if (uuid_is_null(session_id
)) {
313 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
314 if (uuid_compare(iter
->uuid
, session_id
)) {
326 * Delete session from the global session list
327 * and free the memory.
329 static void destroy_session(uuid_t session_id
)
331 struct ltt_session
*iter
= NULL
;
333 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
334 if (uuid_compare(iter
->uuid
, session_id
)) {
335 cds_list_del(&iter
->list
);
349 * Create a brand new session,
351 static int create_session(const char *name
, uuid_t
*session_id
)
353 struct ltt_session
*new_session
;
355 /* Allocate session data structure */
356 new_session
= malloc(sizeof(struct ltt_session
));
357 if (new_session
== NULL
) {
363 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
367 /* Generate session name based on the session count */
368 if (asprintf(&new_session
->name
, "%s%d", "auto", session_count
) < 0) {
373 /* UUID generation */
374 uuid_generate(new_session
->uuid
);
375 uuid_copy(*session_id
, new_session
->uuid
);
377 /* Set consumer (identifier) to 0. This means that there is
378 * NO consumer attach to that session yet.
380 new_session
->ust_consumer
= 0;
381 new_session
->lttng_consumer
= 0;
384 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
385 CDS_INIT_LIST_HEAD(&new_session
->lttng_traces
);
387 /* Add new session to the global session list */
388 cds_list_add(&new_session
->list
, <t_session_list
.head
);
401 * List traceable user-space application and fill an
404 * Return size of the array.
406 static size_t ust_list_apps(pid_t
**pids
)
409 struct ltt_traceable_app
*iter
= NULL
;
412 if (traceable_app_count
== 0) {
413 /* No dynamic allocation is done */
417 p
= malloc(sizeof(pid_t
) * traceable_app_count
);
419 /* TODO: Mutex needed to access this list */
420 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
434 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
436 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
438 llm
->cmd_type
= lsm
->cmd_type
;
440 if (!uuid_is_null(lsm
->session_id
)) {
441 uuid_copy(llm
->session_id
, lsm
->session_id
);
443 strncpy(llm
->trace_name
, lsm
->trace_name
, sizeof(llm
->trace_name
));
449 * This takes the lttcomm_session_msg struct and process the command requested
450 * by the client. It then creates response(s) and send it back to the
451 * given socket (sock).
453 * Return any error encountered or 0 for success.
455 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
458 struct lttcomm_lttng_msg llm
;
460 /* Copy common data to identify the response
461 * on the lttng client side.
463 copy_common_data(&llm
, lsm
);
465 /* Default return code.
466 * In our world, everything is OK... right? ;)
468 llm
.ret_code
= LTTCOMM_OK
;
470 /* Process by command type */
471 switch (lsm
->cmd_type
) {
475 llm
.num_pckt
= ust_list_apps(&pids
);
476 if (llm
.num_pckt
== 0) {
477 ret
= LTTCOMM_NO_APPS
;
481 /* Send all packets */
482 while (llm
.num_pckt
!= 0) {
483 llm
.u
.list_apps
.pid
= pids
[traceable_app_count
- llm
.num_pckt
];
484 ret
= send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
490 /* Allocated array by ust_list_apps() */
495 case LTTNG_LIST_SESSIONS
:
497 struct ltt_session
*iter
= NULL
;
499 llm
.num_pckt
= session_count
;
500 if (llm
.num_pckt
== 0) {
501 ret
= LTTCOMM_NO_SESS
;
505 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
506 uuid_unparse(iter
->uuid
, llm
.u
.list_sessions
.uuid
);
507 strncpy(llm
.u
.list_sessions
.name
, iter
->name
,
508 sizeof(llm
.u
.list_sessions
.name
));
509 ret
= send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
520 /* Undefined command */
532 /* Notify client of error */
534 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
540 * usage function on stderr
542 static void usage(void)
544 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
545 "\t-h, --help\t\tDisplay this usage.\n"
546 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
547 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
548 "\t-d, --daemonize\t\tStart as a daemon.\n"
549 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
550 "\t-V, --version\t\tShow version number.\n"
551 "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n"
552 "\t-q, --quiet\t\tNo output at all.\n",
557 * daemon argument parsing
559 static int parse_args(int argc
, char **argv
)
563 static struct option long_options
[] = {
564 { "client-sock", 1, 0, 'c' },
565 { "apps-sock", 1, 0, 'a' },
566 { "daemonize", 0, 0, 'd' },
567 { "sig-parent", 0, 0, 'S' },
568 { "help", 0, 0, 'h' },
569 { "group", 1, 0, 'g' },
570 { "version", 0, 0, 'V' },
571 { "quiet", 0, 0, 'q' },
576 int option_index
= 0;
577 c
= getopt_long(argc
, argv
, "dhqVS" "a:c:g:s:", long_options
, &option_index
);
584 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
586 fprintf(stderr
, " with arg %s\n", optarg
);
590 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
593 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
599 opt_tracing_group
= strdup(optarg
);
605 fprintf(stdout
, "%s\n", VERSION
);
614 /* Unknown option or other error.
615 * Error is printed by getopt, just return */
626 * Creates the two needed socket by the daemon.
627 * apps_socket - The communication socket for all UST apps.
628 * client_socket - The communication of the cli tool (lttng).
630 static int init_daemon_socket()
635 old_umask
= umask(0);
637 /* Create client tool unix socket */
638 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
639 if (client_socket
< 0) {
644 /* File permission MUST be 660 */
645 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
651 /* Create the application unix socket */
652 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
653 if (apps_socket
< 0) {
658 /* File permission MUST be 660 */
659 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
671 * check_existing_daemon
673 * Check if the global socket is available.
674 * If yes, error is returned.
676 static int check_existing_daemon()
680 ret
= access(client_unix_sock_path
, F_OK
);
682 ret
= access(apps_unix_sock_path
, F_OK
);
691 * Return pointer to home directory path using
692 * the env variable HOME.
696 static const char *get_home_dir(void)
698 const char *home_path
;
700 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
701 home_path
= default_home_dir
;
710 * Set the tracing group gid onto the client socket.
712 static int set_socket_perms(void)
717 /* Decide which group name to use */
718 (opt_tracing_group
!= NULL
) ?
719 (grp
= getgrnam(opt_tracing_group
)) :
720 (grp
= getgrnam(default_tracing_group
));
723 ERR("Missing tracing group. Aborting execution.\n");
728 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
740 * Setup signal handler for :
741 * SIGINT, SIGTERM, SIGPIPE
743 static int set_signal_handler(void)
749 if ((ret
= sigemptyset(&sigset
)) < 0) {
750 perror("sigemptyset");
754 sa
.sa_handler
= sighandler
;
757 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
762 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
767 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
778 * Signal handler for the daemon
780 static void sighandler(int sig
)
798 * Cleanup the daemon on exit
800 static void cleanup()
803 MSG("\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
804 MSG("%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
807 unlink(client_unix_sock_path
);
808 unlink(apps_unix_sock_path
);
814 int main(int argc
, char **argv
)
819 pthread_t threads
[2];
821 /* Parse arguments */
823 if ((ret
= parse_args(argc
, argv
) < 0)) {
836 /* Check if daemon is UID = 0 */
839 /* Set all sockets path */
841 if (strlen(apps_unix_sock_path
) == 0) {
842 (snprintf(apps_unix_sock_path
, PATH_MAX
,
843 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
846 if (strlen(client_unix_sock_path
) == 0) {
847 (snprintf(client_unix_sock_path
, PATH_MAX
,
848 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
851 if (strlen(apps_unix_sock_path
) == 0) {
852 (snprintf(apps_unix_sock_path
, PATH_MAX
,
853 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
856 /* Set the cli tool unix socket path */
857 if (strlen(client_unix_sock_path
) == 0) {
858 (snprintf(client_unix_sock_path
, PATH_MAX
,
859 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
863 /* See if daemon already exist. If any of the two
864 * socket needed by the daemon are present, this test fails
866 if ((ret
= check_existing_daemon()) == 0) {
867 ERR("Already running daemon.\n");
871 if (set_signal_handler() < 0) {
875 /* Setup the two needed unix socket */
876 if (init_daemon_socket() < 0) {
880 /* Set credentials to socket */
881 if (is_root
&& (set_socket_perms() < 0)) {
885 /* Get parent pid if -S, --sig-parent is specified. */
886 if (opt_sig_parent
) {
891 /* Create thread to manage the client socket */
892 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
894 perror("pthread_create");
898 /* Create thread to manage application socket */
899 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
901 perror("pthread_create");
905 for (i
= 0; i
< 2; i
++) {
906 ret
= pthread_join(threads
[i
], &status
);
908 perror("pthread_join");