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"
44 #include "traceable-app.h"
47 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
48 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
49 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
50 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
52 /* Static functions */
53 static int check_existing_daemon(void);
54 static int connect_app(pid_t pid
);
55 static int init_daemon_socket(void);
56 static int notify_apps(const char* name
);
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 int set_signal_handler(void);
60 static int set_socket_perms(void);
61 static int setup_data_buffer(char **buf
, size_t size
, struct lttcomm_lttng_msg
*llm
);
62 static void cleanup(void);
63 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
);
64 static void sighandler(int sig
);
66 static void *thread_manage_clients(void *data
);
67 static void *thread_manage_apps(void *data
);
73 const char *opt_tracing_group
;
74 static int opt_sig_parent
;
75 static int opt_daemon
;
76 static int is_root
; /* Set to 1 if the daemon is running as root */
79 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
80 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
82 static int client_socket
;
83 static int apps_socket
;
85 static struct ltt_session
*current_session
;
90 * This thread manage the application socket communication
92 static void *thread_manage_apps(void *data
)
96 /* TODO: Something more elegant is needed but fine for now */
98 int reg
; /* 1:register, 0:unregister */
103 /* Notify all applications to register */
104 notify_apps(default_global_apps_pipe
);
106 ret
= lttcomm_listen_unix_sock(apps_socket
);
112 /* Blocking call, waiting for transmission */
113 sock
= lttcomm_accept_unix_sock(apps_socket
);
118 /* Basic recv here to handle the very simple data
119 * that the libust send to register (reg_msg).
121 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
127 /* Add application to the global traceable list */
128 if (reg_msg
.reg
== 1) {
130 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
132 /* register_traceable_app only return an error with
133 * ENOMEM. At this point, we better stop everything.
139 unregister_traceable_app(reg_msg
.pid
);
149 * thread_manage_clients
151 * This thread manage all clients request using the unix
152 * client socket for communication.
154 static void *thread_manage_clients(void *data
)
157 struct lttcomm_session_msg lsm
;
159 ret
= lttcomm_listen_unix_sock(client_socket
);
164 /* Notify parent pid that we are ready
165 * to accept command for client side.
167 if (opt_sig_parent
) {
172 /* Blocking call, waiting for transmission */
173 sock
= lttcomm_accept_unix_sock(client_socket
);
179 * Data is received from the lttng client. The struct
180 * lttcomm_session_msg (lsm) contains the command and data
181 * request of the client.
183 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
188 /* This function dispatch the work to the LTTng or UST libs
189 * and then sends back the response to the client. This is needed
190 * because there might be more then one lttcomm_lttng_msg to
191 * send out so process_client_msg do both jobs.
193 ret
= process_client_msg(sock
, &lsm
);
195 /* Error detected but still accept command */
207 * Send data on a unix socket using the liblttsessiondcomm API.
209 * Return lttcomm error code.
211 static int send_unix_sock(int sock
, void *buf
, size_t len
)
213 /* Check valid length */
218 return lttcomm_send_unix_sock(sock
, buf
, len
);
224 * Return a socket connected to the libust communication socket
225 * of the application identified by the pid.
227 * If the pid is not found in the traceable list,
228 * return -1 to indicate error.
230 static int connect_app(pid_t pid
)
233 struct ltt_traceable_app
*lta
;
235 lta
= find_app_by_pid(pid
);
241 sock
= ustctl_connect_pid(lta
->pid
);
243 ERR("Fail connecting to the PID %d\n", pid
);
252 * Notify apps by writing 42 to a named pipe using name.
253 * Every applications waiting for a ltt-sessiond will be notified
254 * and re-register automatically to the session daemon.
256 * Return open or write error value.
258 static int notify_apps(const char *name
)
263 /* Try opening the global pipe */
264 fd
= open(name
, O_WRONLY
);
269 /* Notify by writing on the pipe */
270 ret
= write(fd
, "42", 2);
282 * Create an userspace trace using pid.
283 * This trace is then appended to the current session
286 static int ust_create_trace(pid_t pid
)
289 struct ltt_ust_trace
*trace
;
291 trace
= malloc(sizeof(struct ltt_ust_trace
));
302 /* Connect to app using ustctl API */
303 sock
= connect_app(pid
);
305 ret
= LTTCOMM_NO_TRACEABLE
;
309 ret
= ustctl_create_trace(sock
, "auto");
311 ret
= LTTCOMM_CREATE_FAIL
;
315 /* Check if current session is valid */
316 if (current_session
) {
317 cds_list_add(&trace
->list
, ¤t_session
->ust_traces
);
327 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
329 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
331 llm
->cmd_type
= lsm
->cmd_type
;
335 if (!uuid_is_null(lsm
->session_id
)) {
336 uuid_copy(llm
->session_id
, lsm
->session_id
);
339 strncpy(llm
->trace_name
, lsm
->trace_name
, strlen(llm
->trace_name
));
340 llm
->trace_name
[strlen(llm
->trace_name
) - 1] = '\0';
346 * Setup the outgoing data buffer for the response
347 * data allocating the right amount of memory.
349 * Return total size of the buffer pointed by buf.
351 static int setup_data_buffer(char **buf
, size_t s_data
, struct lttcomm_lttng_msg
*llm
)
356 buf_size
= sizeof(struct lttcomm_lttng_msg
) + s_data
;
357 *buf
= malloc(buf_size
);
364 /* Setup lttcomm_lttng_msg data and copy
365 * it to the newly allocated buffer.
367 llm
->size_payload
= s_data
;
368 memcpy(*buf
, llm
, sizeof(struct lttcomm_lttng_msg
));
379 * This takes the lttcomm_session_msg struct and process the command requested
380 * by the client. It then creates response(s) and send it back to the
381 * given socket (sock).
383 * Return any error encountered or 0 for success.
385 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
390 char *send_buf
= NULL
;
391 struct lttcomm_lttng_msg llm
;
393 /* Copy common data to identify the response
394 * on the lttng client side.
396 copy_common_data(&llm
, lsm
);
398 /* Check command that needs a session */
399 if (lsm
->cmd_type
!= LTTNG_CREATE_SESSION
&&
400 lsm
->cmd_type
!= LTTNG_LIST_SESSIONS
&&
401 lsm
->cmd_type
!= UST_LIST_APPS
)
403 current_session
= find_session_by_uuid(lsm
->session_id
);
404 if (current_session
== NULL
) {
405 ret
= LTTCOMM_SELECT_SESS
;
410 /* Default return code.
411 * In our world, everything is OK... right? ;)
413 llm
.ret_code
= LTTCOMM_OK
;
415 header_size
= sizeof(struct lttcomm_lttng_msg
);
417 /* Process by command type */
418 switch (lsm
->cmd_type
) {
419 case LTTNG_CREATE_SESSION
:
421 ret
= create_session(lsm
->session_name
, &llm
.session_id
);
424 ret
= LTTCOMM_EXIST_SESS
;
431 buf_size
= setup_data_buffer(&send_buf
, 0, &llm
);
439 case LTTNG_DESTROY_SESSION
:
441 ret
= destroy_session(&lsm
->session_id
);
443 ret
= LTTCOMM_NO_SESS
;
448 /* No auxiliary data so only send the llm struct. */
451 case UST_CREATE_TRACE
:
453 ret
= ust_create_trace(lsm
->pid
);
455 ret
= LTTCOMM_CREATE_FAIL
;
459 /* No auxiliary data so only send the llm struct. */
464 unsigned int app_count
= get_app_count();
465 /* Stop right now if no apps */
466 if (app_count
== 0) {
467 ret
= LTTCOMM_NO_APPS
;
471 /* Setup data buffer and details for transmission */
472 buf_size
= setup_data_buffer(&send_buf
,
473 sizeof(pid_t
) * app_count
, &llm
);
479 get_app_list_pids((pid_t
*)(send_buf
+ header_size
));
483 case LTTNG_LIST_SESSIONS
:
485 unsigned int session_count
= get_session_count();
486 /* Stop right now if no session */
487 if (session_count
== 0) {
488 ret
= LTTCOMM_NO_SESS
;
492 /* Setup data buffer and details for transmission */
493 buf_size
= setup_data_buffer(&send_buf
,
494 (sizeof(struct lttng_session
) * session_count
), &llm
);
500 get_lttng_session((struct lttng_session
*)(send_buf
+ header_size
));
506 /* Undefined command */
512 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
514 if (send_buf
!= NULL
) {
521 /* Notify client of error */
523 llm
.size_payload
= 0;
524 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
530 * usage function on stderr
532 static void usage(void)
534 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
535 fprintf(stderr
, " -h, --help Display this usage.\n");
536 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
537 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket.\n");
538 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
539 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
540 fprintf(stderr
, " -V, --version Show version number.\n");
541 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
542 fprintf(stderr
, " -q, --quiet No output at all.\n");
546 * daemon argument parsing
548 static int parse_args(int argc
, char **argv
)
552 static struct option long_options
[] = {
553 { "client-sock", 1, 0, 'c' },
554 { "apps-sock", 1, 0, 'a' },
555 { "daemonize", 0, 0, 'd' },
556 { "sig-parent", 0, 0, 'S' },
557 { "help", 0, 0, 'h' },
558 { "group", 1, 0, 'g' },
559 { "version", 0, 0, 'V' },
560 { "quiet", 0, 0, 'q' },
565 int option_index
= 0;
566 c
= getopt_long(argc
, argv
, "dhqVS" "a:c:g:s:", long_options
, &option_index
);
573 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
575 fprintf(stderr
, " with arg %s\n", optarg
);
579 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
582 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
588 opt_tracing_group
= strdup(optarg
);
594 fprintf(stdout
, "%s\n", VERSION
);
603 /* Unknown option or other error.
604 * Error is printed by getopt, just return */
615 * Creates the two needed socket by the daemon.
616 * apps_socket - The communication socket for all UST apps.
617 * client_socket - The communication of the cli tool (lttng).
619 static int init_daemon_socket()
624 old_umask
= umask(0);
626 /* Create client tool unix socket */
627 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
628 if (client_socket
< 0) {
633 /* File permission MUST be 660 */
634 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
640 /* Create the application unix socket */
641 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
642 if (apps_socket
< 0) {
647 /* File permission MUST be 660 */
648 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
660 * check_existing_daemon
662 * Check if the global socket is available.
663 * If yes, error is returned.
665 static int check_existing_daemon()
669 ret
= access(client_unix_sock_path
, F_OK
);
671 ret
= access(apps_unix_sock_path
, F_OK
);
680 * Return pointer to home directory path using
681 * the env variable HOME.
685 static const char *get_home_dir(void)
687 const char *home_path
;
689 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
690 home_path
= default_home_dir
;
699 * Set the tracing group gid onto the client socket.
701 static int set_socket_perms(void)
706 /* Decide which group name to use */
707 (opt_tracing_group
!= NULL
) ?
708 (grp
= getgrnam(opt_tracing_group
)) :
709 (grp
= getgrnam(default_tracing_group
));
712 ERR("Missing tracing group. Aborting execution.\n");
717 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
729 * Setup signal handler for :
730 * SIGINT, SIGTERM, SIGPIPE
732 static int set_signal_handler(void)
738 if ((ret
= sigemptyset(&sigset
)) < 0) {
739 perror("sigemptyset");
743 sa
.sa_handler
= sighandler
;
746 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
751 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
756 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
767 * Signal handler for the daemon
769 static void sighandler(int sig
)
788 * Cleanup the daemon on exit
790 static void cleanup()
793 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
794 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
797 unlink(client_unix_sock_path
);
798 unlink(apps_unix_sock_path
);
804 int main(int argc
, char **argv
)
809 pthread_t threads
[2];
811 /* Parse arguments */
813 if ((ret
= parse_args(argc
, argv
) < 0)) {
826 /* Check if daemon is UID = 0 */
829 /* Set all sockets path */
831 if (strlen(apps_unix_sock_path
) == 0) {
832 (snprintf(apps_unix_sock_path
, PATH_MAX
,
833 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
836 if (strlen(client_unix_sock_path
) == 0) {
837 (snprintf(client_unix_sock_path
, PATH_MAX
,
838 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
841 if (strlen(apps_unix_sock_path
) == 0) {
842 (snprintf(apps_unix_sock_path
, PATH_MAX
,
843 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
846 /* Set the cli tool unix socket path */
847 if (strlen(client_unix_sock_path
) == 0) {
848 (snprintf(client_unix_sock_path
, PATH_MAX
,
849 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
853 /* See if daemon already exist. If any of the two
854 * socket needed by the daemon are present, this test fails
856 if ((ret
= check_existing_daemon()) == 0) {
857 ERR("Already running daemon.\n");
858 /* We do not goto error because we must not
859 * cleanup() because a daemon is already working.
864 if (set_signal_handler() < 0) {
868 /* Setup the two needed unix socket */
869 if (init_daemon_socket() < 0) {
873 /* Set credentials to socket */
874 if (is_root
&& (set_socket_perms() < 0)) {
878 /* Get parent pid if -S, --sig-parent is specified. */
879 if (opt_sig_parent
) {
884 /* Create thread to manage the client socket */
885 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
887 perror("pthread_create");
891 /* Create thread to manage application socket */
892 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
894 perror("pthread_create");
898 for (i
= 0; i
< 2; i
++) {
899 ret
= pthread_join(threads
[i
], &status
);
901 perror("pthread_join");