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 set_signal_handler(void);
54 static int set_socket_perms(void);
55 static void sighandler(int sig
);
56 static void cleanup(void);
57 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
);
58 static int check_existing_daemon(void);
59 static int notify_apps(const char* name
);
60 static int connect_app(pid_t pid
);
61 static int init_daemon_socket(void);
62 static int process_client_msg(int sock
, struct lttcomm_session_msg
*);
63 static int send_unix_sock(int sock
, void *buf
, size_t len
);
64 static int setup_data_buffer(char **buf
, size_t size
, struct lttcomm_lttng_msg
*llm
);
66 /* Command function */
68 static void *thread_manage_clients(void *data
);
69 static void *thread_manage_apps(void *data
);
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
;
92 * This thread manage the application socket communication
94 static void *thread_manage_apps(void *data
)
98 /* TODO: Something more elegant is needed but fine for now */
100 int reg
; /* 1:register, 0:unregister */
105 /* Notify all applications to register */
106 notify_apps(default_global_apps_pipe
);
108 ret
= lttcomm_listen_unix_sock(apps_socket
);
114 /* Blocking call, waiting for transmission */
115 sock
= lttcomm_accept_unix_sock(apps_socket
);
120 /* Basic recv here to handle the very simple data
121 * that the libust send to register (reg_msg).
123 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
129 /* Add application to the global traceable list */
130 if (reg_msg
.reg
== 1) {
132 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
134 /* register_traceable_app only return an error with
135 * ENOMEM. At this point, we better stop everything.
141 unregister_traceable_app(reg_msg
.pid
);
151 * thread_manage_clients
153 * This thread manage all clients request using the unix
154 * client socket for communication.
156 static void *thread_manage_clients(void *data
)
159 struct lttcomm_session_msg lsm
;
161 ret
= lttcomm_listen_unix_sock(client_socket
);
166 /* Notify parent pid that we are ready
167 * to accept command for client side.
169 if (opt_sig_parent
) {
174 /* Blocking call, waiting for transmission */
175 sock
= lttcomm_accept_unix_sock(client_socket
);
181 * Data is received from the lttng client. The struct
182 * lttcomm_session_msg (lsm) contains the command and data
183 * request of the client.
185 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
190 /* This function dispatch the work to the LTTng or UST libs
191 * and then sends back the response to the client. This is needed
192 * because there might be more then one lttcomm_lttng_msg to
193 * send out so process_client_msg do both jobs.
195 ret
= process_client_msg(sock
, &lsm
);
197 /* Error detected but still accept command */
209 * Send data on a unix socket using the liblttsessiondcomm API.
211 * Return lttcomm error code.
213 static int send_unix_sock(int sock
, void *buf
, size_t len
)
215 /* Check valid length */
220 return lttcomm_send_unix_sock(sock
, buf
, len
);
226 * Return a socket connected to the libust communication socket
227 * of the application identified by the pid.
229 * If the pid is not found in the traceable list,
230 * return -1 to indicate error.
232 static int connect_app(pid_t pid
)
235 struct ltt_traceable_app
*lta
;
237 lta
= find_app_by_pid(pid
);
243 sock
= ustctl_connect_pid(lta
->pid
);
245 ERR("Fail connecting to the PID %d\n", pid
);
254 * Notify apps by writing 42 to a named pipe using name.
255 * Every applications waiting for a ltt-sessiond will be notified
256 * and re-register automatically to the session daemon.
258 * Return open or write error value.
260 static int notify_apps(const char *name
)
265 /* Try opening the global pipe */
266 fd
= open(name
, O_WRONLY
);
271 /* Notify by writing on the pipe */
272 ret
= write(fd
, "42", 2);
284 * Create an userspace trace using pid.
285 * This trace is then appended to the current session
288 static int ust_create_trace(pid_t pid
)
291 struct ltt_ust_trace
*trace
;
293 trace
= malloc(sizeof(struct ltt_ust_trace
));
304 /* Connect to app using ustctl API */
305 sock
= connect_app(pid
);
307 ret
= LTTCOMM_NO_TRACEABLE
;
311 ret
= ustctl_create_trace(sock
, "auto");
313 ret
= LTTCOMM_CREATE_FAIL
;
317 /* Check if current session is valid */
318 if (current_session
) {
319 cds_list_add(&trace
->list
, ¤t_session
->ust_traces
);
329 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
331 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
333 llm
->cmd_type
= lsm
->cmd_type
;
337 if (!uuid_is_null(lsm
->session_id
)) {
338 uuid_copy(llm
->session_id
, lsm
->session_id
);
341 strncpy(llm
->trace_name
, lsm
->trace_name
, strlen(llm
->trace_name
));
342 llm
->trace_name
[strlen(llm
->trace_name
) - 1] = '\0';
348 * Setup the outgoing data buffer for the response
349 * data allocating the right amount of memory.
351 * Return total size of the buffer pointed by buf.
353 static int setup_data_buffer(char **buf
, size_t s_data
, struct lttcomm_lttng_msg
*llm
)
358 buf_size
= sizeof(struct lttcomm_lttng_msg
) + s_data
;
359 *buf
= malloc(buf_size
);
366 /* Setup lttcomm_lttng_msg data and copy
367 * it to the newly allocated buffer.
369 llm
->size_payload
= s_data
;
370 memcpy(*buf
, llm
, sizeof(struct lttcomm_lttng_msg
));
381 * This takes the lttcomm_session_msg struct and process the command requested
382 * by the client. It then creates response(s) and send it back to the
383 * given socket (sock).
385 * Return any error encountered or 0 for success.
387 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
392 char *send_buf
= NULL
;
393 struct lttcomm_lttng_msg llm
;
395 /* Copy common data to identify the response
396 * on the lttng client side.
398 copy_common_data(&llm
, lsm
);
400 /* Check command that needs a session */
401 if (lsm
->cmd_type
!= LTTNG_CREATE_SESSION
&&
402 lsm
->cmd_type
!= LTTNG_LIST_SESSIONS
&&
403 lsm
->cmd_type
!= UST_LIST_APPS
)
405 current_session
= find_session_by_uuid(lsm
->session_id
);
406 if (current_session
== NULL
) {
407 ret
= LTTCOMM_SELECT_SESS
;
412 /* Default return code.
413 * In our world, everything is OK... right? ;)
415 llm
.ret_code
= LTTCOMM_OK
;
417 header_size
= sizeof(struct lttcomm_lttng_msg
);
419 /* Process by command type */
420 switch (lsm
->cmd_type
) {
421 case LTTNG_CREATE_SESSION
:
423 ret
= create_session(lsm
->session_name
, &llm
.session_id
);
426 ret
= LTTCOMM_EXIST_SESS
;
433 buf_size
= setup_data_buffer(&send_buf
, 0, &llm
);
441 case LTTNG_DESTROY_SESSION
:
443 ret
= destroy_session(&lsm
->session_id
);
445 ret
= LTTCOMM_NO_SESS
;
450 /* No auxiliary data so only send the llm struct. */
453 case UST_CREATE_TRACE
:
455 ret
= ust_create_trace(lsm
->pid
);
457 ret
= LTTCOMM_CREATE_FAIL
;
461 /* No auxiliary data so only send the llm struct. */
466 unsigned int app_count
= get_app_count();
467 /* Stop right now if no apps */
468 if (app_count
== 0) {
469 ret
= LTTCOMM_NO_APPS
;
473 /* Setup data buffer and details for transmission */
474 buf_size
= setup_data_buffer(&send_buf
,
475 sizeof(pid_t
) * app_count
, &llm
);
481 get_app_list_pids((pid_t
*)(send_buf
+ header_size
));
485 case LTTNG_LIST_SESSIONS
:
487 unsigned int session_count
= get_session_count();
488 /* Stop right now if no session */
489 if (session_count
== 0) {
490 ret
= LTTCOMM_NO_SESS
;
494 /* Setup data buffer and details for transmission */
495 buf_size
= setup_data_buffer(&send_buf
,
496 (sizeof(struct lttng_session
) * session_count
), &llm
);
502 get_lttng_session((struct lttng_session
*)(send_buf
+ header_size
));
508 /* Undefined command */
514 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
516 if (send_buf
!= NULL
) {
523 /* Notify client of error */
525 llm
.size_payload
= 0;
526 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
532 * usage function on stderr
534 static void usage(void)
536 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
537 fprintf(stderr
, " -h, --help Display this usage.\n");
538 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
539 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket.\n");
540 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
541 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
542 fprintf(stderr
, " -V, --version Show version number.\n");
543 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
544 fprintf(stderr
, " -q, --quiet No output at all.\n");
548 * daemon argument parsing
550 static int parse_args(int argc
, char **argv
)
554 static struct option long_options
[] = {
555 { "client-sock", 1, 0, 'c' },
556 { "apps-sock", 1, 0, 'a' },
557 { "daemonize", 0, 0, 'd' },
558 { "sig-parent", 0, 0, 'S' },
559 { "help", 0, 0, 'h' },
560 { "group", 1, 0, 'g' },
561 { "version", 0, 0, 'V' },
562 { "quiet", 0, 0, 'q' },
567 int option_index
= 0;
568 c
= getopt_long(argc
, argv
, "dhqVS" "a:c:g:s:", long_options
, &option_index
);
575 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
577 fprintf(stderr
, " with arg %s\n", optarg
);
581 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
584 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
590 opt_tracing_group
= strdup(optarg
);
596 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 ERR("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
)
790 * Cleanup the daemon on exit
792 static void cleanup()
795 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
796 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",27,1,33,27,0);
799 unlink(client_unix_sock_path
);
800 unlink(apps_unix_sock_path
);
806 int main(int argc
, char **argv
)
811 pthread_t threads
[2];
813 /* Parse arguments */
815 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 ERR("Already running daemon.\n");
860 /* We do not goto error because we must not
861 * cleanup() because a daemon is already working.
866 if (set_signal_handler() < 0) {
870 /* Setup the two needed unix socket */
871 if (init_daemon_socket() < 0) {
875 /* Set credentials to socket */
876 if (is_root
&& (set_socket_perms() < 0)) {
880 /* Get parent pid if -S, --sig-parent is specified. */
881 if (opt_sig_parent
) {
886 /* Create thread to manage the client socket */
887 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
889 perror("pthread_create");
893 /* Create thread to manage application socket */
894 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
896 perror("pthread_create");
900 for (i
= 0; i
< 2; i
++) {
901 ret
= pthread_join(threads
[i
], &status
);
903 perror("pthread_join");