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"
45 #include "traceable-app.h"
48 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
49 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
50 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
51 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
53 /* Static functions */
54 static int check_existing_daemon(void);
55 static int connect_app(pid_t pid
);
56 static int init_daemon_socket(void);
57 static int notify_apps(const char* name
);
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
);
60 static int set_signal_handler(void);
61 static int set_socket_perms(void);
62 static int setup_data_buffer(char **buf
, size_t size
, struct lttcomm_lttng_msg
*llm
);
63 static void cleanup(void);
64 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
);
65 static void sighandler(int sig
);
67 static void *thread_manage_clients(void *data
);
68 static void *thread_manage_apps(void *data
);
74 const char *opt_tracing_group
;
75 static int opt_sig_parent
;
76 static int opt_daemon
;
77 static int is_root
; /* Set to 1 if the daemon is running as root */
80 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
81 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
83 static int client_socket
;
84 static int apps_socket
;
86 static struct ltt_session
*current_session
;
91 * This thread manage the application socket communication
93 static void *thread_manage_apps(void *data
)
97 /* TODO: Something more elegant is needed but fine for now */
99 int reg
; /* 1:register, 0:unregister */
104 /* Notify all applications to register */
105 notify_apps(default_global_apps_pipe
);
107 ret
= lttcomm_listen_unix_sock(apps_socket
);
113 /* Blocking call, waiting for transmission */
114 sock
= lttcomm_accept_unix_sock(apps_socket
);
119 /* Basic recv here to handle the very simple data
120 * that the libust send to register (reg_msg).
122 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
128 /* Add application to the global traceable list */
129 if (reg_msg
.reg
== 1) {
131 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
133 /* register_traceable_app only return an error with
134 * ENOMEM. At this point, we better stop everything.
140 unregister_traceable_app(reg_msg
.pid
);
150 * thread_manage_clients
152 * This thread manage all clients request using the unix
153 * client socket for communication.
155 static void *thread_manage_clients(void *data
)
158 struct lttcomm_session_msg lsm
;
160 ret
= lttcomm_listen_unix_sock(client_socket
);
165 /* Notify parent pid that we are ready
166 * to accept command for client side.
168 if (opt_sig_parent
) {
173 /* Blocking call, waiting for transmission */
174 sock
= lttcomm_accept_unix_sock(client_socket
);
180 * Data is received from the lttng client. The struct
181 * lttcomm_session_msg (lsm) contains the command and data
182 * request of the client.
184 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
189 /* This function dispatch the work to the LTTng or UST libs
190 * and then sends back the response to the client. This is needed
191 * because there might be more then one lttcomm_lttng_msg to
192 * send out so process_client_msg do both jobs.
194 ret
= process_client_msg(sock
, &lsm
);
196 /* Error detected but still accept command */
208 * Send data on a unix socket using the liblttsessiondcomm API.
210 * Return lttcomm error code.
212 static int send_unix_sock(int sock
, void *buf
, size_t len
)
214 /* Check valid length */
219 return lttcomm_send_unix_sock(sock
, buf
, len
);
225 * Return a socket connected to the libust communication socket
226 * of the application identified by the pid.
228 * If the pid is not found in the traceable list,
229 * return -1 to indicate error.
231 static int connect_app(pid_t pid
)
234 struct ltt_traceable_app
*lta
;
236 lta
= find_app_by_pid(pid
);
242 sock
= ustctl_connect_pid(lta
->pid
);
244 ERR("Fail connecting to the PID %d\n", pid
);
253 * Notify apps by writing 42 to a named pipe using name.
254 * Every applications waiting for a ltt-sessiond will be notified
255 * and re-register automatically to the session daemon.
257 * Return open or write error value.
259 static int notify_apps(const char *name
)
264 /* Try opening the global pipe */
265 fd
= open(name
, O_WRONLY
);
270 /* Notify by writing on the pipe */
271 ret
= write(fd
, "42", 2);
283 * Create an userspace trace using pid.
284 * This trace is then appended to the current session
287 static int ust_create_trace(pid_t pid
)
290 struct ltt_ust_trace
*trace
;
292 trace
= malloc(sizeof(struct ltt_ust_trace
));
303 /* Connect to app using ustctl API */
304 sock
= connect_app(pid
);
306 ret
= LTTCOMM_NO_TRACEABLE
;
310 ret
= ustctl_create_trace(sock
, "auto");
312 ret
= LTTCOMM_CREATE_FAIL
;
316 /* Check if current session is valid */
317 if (current_session
) {
318 cds_list_add(&trace
->list
, ¤t_session
->ust_traces
);
328 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
330 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
332 llm
->cmd_type
= lsm
->cmd_type
;
336 if (!uuid_is_null(lsm
->session_id
)) {
337 uuid_copy(llm
->session_id
, lsm
->session_id
);
340 strncpy(llm
->trace_name
, lsm
->trace_name
, strlen(llm
->trace_name
));
341 llm
->trace_name
[strlen(llm
->trace_name
) - 1] = '\0';
347 * Setup the outgoing data buffer for the response
348 * data allocating the right amount of memory.
350 * Return total size of the buffer pointed by buf.
352 static int setup_data_buffer(char **buf
, size_t s_data
, struct lttcomm_lttng_msg
*llm
)
357 buf_size
= sizeof(struct lttcomm_lttng_msg
) + s_data
;
358 *buf
= malloc(buf_size
);
365 /* Setup lttcomm_lttng_msg data and copy
366 * it to the newly allocated buffer.
368 llm
->size_payload
= s_data
;
369 memcpy(*buf
, llm
, sizeof(struct lttcomm_lttng_msg
));
380 * This takes the lttcomm_session_msg struct and process the command requested
381 * by the client. It then creates response(s) and send it back to the
382 * given socket (sock).
384 * Return any error encountered or 0 for success.
386 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
391 char *send_buf
= NULL
;
392 struct lttcomm_lttng_msg llm
;
394 /* Copy common data to identify the response
395 * on the lttng client side.
397 copy_common_data(&llm
, lsm
);
399 /* Check command that needs a session */
400 if (lsm
->cmd_type
!= LTTNG_CREATE_SESSION
&&
401 lsm
->cmd_type
!= LTTNG_LIST_SESSIONS
&&
402 lsm
->cmd_type
!= UST_LIST_APPS
)
404 current_session
= find_session_by_uuid(lsm
->session_id
);
405 if (current_session
== NULL
) {
406 ret
= LTTCOMM_SELECT_SESS
;
411 /* Default return code.
412 * In our world, everything is OK... right? ;)
414 llm
.ret_code
= LTTCOMM_OK
;
416 header_size
= sizeof(struct lttcomm_lttng_msg
);
418 /* Process by command type */
419 switch (lsm
->cmd_type
) {
420 case LTTNG_CREATE_SESSION
:
422 ret
= create_session(lsm
->session_name
, &llm
.session_id
);
425 ret
= LTTCOMM_EXIST_SESS
;
432 buf_size
= setup_data_buffer(&send_buf
, 0, &llm
);
440 case LTTNG_DESTROY_SESSION
:
442 ret
= destroy_session(&lsm
->session_id
);
444 ret
= LTTCOMM_NO_SESS
;
449 /* No auxiliary data so only send the llm struct. */
452 case UST_CREATE_TRACE
:
454 ret
= ust_create_trace(lsm
->pid
);
456 ret
= LTTCOMM_CREATE_FAIL
;
460 /* No auxiliary data so only send the llm struct. */
465 unsigned int app_count
= get_app_count();
466 /* Stop right now if no apps */
467 if (app_count
== 0) {
468 ret
= LTTCOMM_NO_APPS
;
472 /* Setup data buffer and details for transmission */
473 buf_size
= setup_data_buffer(&send_buf
,
474 sizeof(pid_t
) * app_count
, &llm
);
480 get_app_list_pids((pid_t
*)(send_buf
+ header_size
));
484 case LTTNG_LIST_SESSIONS
:
486 unsigned int session_count
= get_session_count();
487 /* Stop right now if no session */
488 if (session_count
== 0) {
489 ret
= LTTCOMM_NO_SESS
;
493 /* Setup data buffer and details for transmission */
494 buf_size
= setup_data_buffer(&send_buf
,
495 (sizeof(struct lttng_session
) * session_count
), &llm
);
501 get_lttng_session((struct lttng_session
*)(send_buf
+ header_size
));
507 /* Undefined command */
513 ret
= send_unix_sock(sock
, send_buf
, buf_size
);
515 if (send_buf
!= NULL
) {
522 /* Notify client of error */
524 llm
.size_payload
= 0;
525 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
531 * usage function on stderr
533 static void usage(void)
535 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
536 fprintf(stderr
, " -h, --help Display this usage.\n");
537 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
538 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket.\n");
539 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
540 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
541 fprintf(stderr
, " -V, --version Show version number.\n");
542 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
543 fprintf(stderr
, " -q, --quiet No output at all.\n");
547 * daemon argument parsing
549 static int parse_args(int argc
, char **argv
)
553 static struct option long_options
[] = {
554 { "client-sock", 1, 0, 'c' },
555 { "apps-sock", 1, 0, 'a' },
556 { "daemonize", 0, 0, 'd' },
557 { "sig-parent", 0, 0, 'S' },
558 { "help", 0, 0, 'h' },
559 { "group", 1, 0, 'g' },
560 { "version", 0, 0, 'V' },
561 { "quiet", 0, 0, 'q' },
566 int option_index
= 0;
567 c
= getopt_long(argc
, argv
, "dhqVS" "a:c:g:s:", long_options
, &option_index
);
574 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
576 fprintf(stderr
, " with arg %s\n", optarg
);
580 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
583 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
589 opt_tracing_group
= strdup(optarg
);
595 fprintf(stdout
, "%s\n", VERSION
);
604 /* Unknown option or other error.
605 * Error is printed by getopt, just return */
616 * Creates the two needed socket by the daemon.
617 * apps_socket - The communication socket for all UST apps.
618 * client_socket - The communication of the cli tool (lttng).
620 static int init_daemon_socket()
625 old_umask
= umask(0);
627 /* Create client tool unix socket */
628 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
629 if (client_socket
< 0) {
634 /* File permission MUST be 660 */
635 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
641 /* Create the application unix socket */
642 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
643 if (apps_socket
< 0) {
648 /* File permission MUST be 660 */
649 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
661 * check_existing_daemon
663 * Check if the global socket is available.
664 * If yes, error is returned.
666 static int check_existing_daemon()
670 ret
= access(client_unix_sock_path
, F_OK
);
672 ret
= access(apps_unix_sock_path
, F_OK
);
681 * Return pointer to home directory path using
682 * the env variable HOME.
686 static const char *get_home_dir(void)
688 const char *home_path
;
690 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
691 home_path
= default_home_dir
;
700 * Set the tracing group gid onto the client socket.
702 static int set_socket_perms(void)
707 /* Decide which group name to use */
708 (opt_tracing_group
!= NULL
) ?
709 (grp
= getgrnam(opt_tracing_group
)) :
710 (grp
= getgrnam(default_tracing_group
));
713 ERR("Missing tracing group. Aborting execution.\n");
718 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
730 * Setup signal handler for :
731 * SIGINT, SIGTERM, SIGPIPE
733 static int set_signal_handler(void)
739 if ((ret
= sigemptyset(&sigset
)) < 0) {
740 perror("sigemptyset");
744 sa
.sa_handler
= sighandler
;
747 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
752 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
757 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
768 * Signal handler for the daemon
770 static void sighandler(int sig
)
789 * Cleanup the daemon on exit
791 static void cleanup()
794 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
795 MSG("%c[%d;%dmMatthew, BEET driven development works!%c[%dm",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 ERR("Already running daemon.\n");
859 /* We do not goto error because we must not
860 * cleanup() because a daemon is already working.
865 if (set_signal_handler() < 0) {
869 /* Setup the two needed unix socket */
870 if (init_daemon_socket() < 0) {
874 /* Set credentials to socket */
875 if (is_root
&& (set_socket_perms() < 0)) {
879 /* Get parent pid if -S, --sig-parent is specified. */
880 if (opt_sig_parent
) {
885 /* Create thread to manage the client socket */
886 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
888 perror("pthread_create");
892 /* Create thread to manage application socket */
893 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
895 perror("pthread_create");
899 for (i
= 0; i
< 2; i
++) {
900 ret
= pthread_join(threads
[i
], &status
);
902 perror("pthread_join");