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
);
60 static void *thread_manage_clients(void *);
61 static void *thread_manage_apps(void *);
63 static int create_session(const char*, uuid_t
*);
64 static void destroy_session(uuid_t
);
66 static struct ltt_session
*find_session(uuid_t
);
70 const char *opt_tracing_group
;
71 static int opt_sig_parent
;
72 static int opt_daemon
;
73 static int is_root
; /* Set to 1 if the daemon is running as root */
76 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
77 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
79 static int client_socket
;
80 static int apps_socket
;
82 static struct ltt_session
*current_session
;
84 /* Number of element for the list below. */
85 static unsigned int session_count
;
86 static unsigned int traceable_app_count
;
88 /* Init session's list */
89 static struct ltt_session_list ltt_session_list
= {
90 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
93 /* Init ust traceabl application's list */
94 static struct ltt_traceable_app_list ltt_traceable_app_list
= {
95 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
101 * This thread manage the application socket communication
103 static void *thread_manage_apps(void *data
)
106 struct ltt_traceable_app
*lta
;
108 /* TODO: Something more elegant is needed but fine for now */
110 int reg
; /* 1:register, 0:unregister */
115 /* Notify all applications to register */
116 notify_apps(default_global_apps_pipe
);
118 ret
= lttcomm_listen_unix_sock(apps_socket
);
124 /* Blocking call, waiting for transmission */
125 sock
= lttcomm_accept_unix_sock(apps_socket
);
130 /* Basic recv here to handle the very simple data
131 * that the libust send to register (reg_msg).
133 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
139 /* Add application to the global traceable list */
140 if (reg_msg
.reg
== 1) {
142 lta
= malloc(sizeof(struct ltt_traceable_app
));
143 lta
->pid
= reg_msg
.pid
;
144 lta
->uid
= reg_msg
.uid
;
145 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
146 traceable_app_count
++;
150 cds_list_for_each_entry(lta
, <t_traceable_app_list
.head
, list
) {
151 if (lta
->pid
== reg_msg
.pid
&& lta
->uid
== reg_msg
.uid
) {
152 cds_list_del(<a
->list
);
153 /* Check to not overflow here */
154 if (traceable_app_count
!= 0) {
155 traceable_app_count
--;
161 /* If an item was found, free it from memory */
174 * thread_manage_clients
176 * This thread manage all clients request using the unix
177 * client socket for communication.
179 static void *thread_manage_clients(void *data
)
182 struct lttcomm_session_msg lsm
;
184 ret
= lttcomm_listen_unix_sock(client_socket
);
189 /* Notify parent pid that we are ready
190 * to accept command for client side.
192 if (opt_sig_parent
) {
197 /* Blocking call, waiting for transmission */
198 sock
= lttcomm_accept_unix_sock(client_socket
);
204 * Data is received from the lttng client. The struct
205 * lttcomm_session_msg (lsm) contains the command and data
206 * request of the client.
208 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
213 /* This function dispatch the work to the LTTng or UST libs
214 * and then sends back the response to the client. This is needed
215 * because there might be more then one lttcomm_lttng_msg to
216 * send out so process_client_msg do both jobs.
218 ret
= process_client_msg(sock
, &lsm
);
220 /* Error detected but still accept command */
232 * Send data on a unix socket using the liblttsessiondcomm API.
234 * Return lttcomm error code.
236 static int send_unix_sock(int sock
, void *buf
, size_t len
)
238 /* Check valid length */
243 return lttcomm_send_unix_sock(sock
, buf
, len
);
249 * Return a socket connected to the libust communication socket
250 * of the application identified by the pid.
252 static int connect_app(pid_t pid
)
256 sock
= ustctl_connect_pid(pid
);
258 fprintf(stderr
, "Fail connecting to the PID %d\n", pid
);
267 * Notify apps by writing 42 to a named pipe using name.
268 * Every applications waiting for a ltt-sessiond will be notified
269 * and re-register automatically to the session daemon.
271 * Return open or write error value.
273 static int notify_apps(const char *name
)
278 /* Try opening the global pipe */
279 fd
= open(name
, O_WRONLY
);
284 /* Notify by writing on the pipe */
285 ret
= write(fd
, "42", 2);
297 * Return a ltt_session structure ptr that matches the uuid.
299 static struct ltt_session
*find_session(uuid_t session_id
)
301 struct ltt_session
*iter
= NULL
;
303 /* Sanity check for NULL session_id */
304 if (uuid_is_null(session_id
)) {
308 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
309 if (uuid_compare(iter
->uuid
, session_id
)) {
321 * Delete session from the global session list
322 * and free the memory.
324 static void destroy_session(uuid_t session_id
)
326 struct ltt_session
*iter
= NULL
;
328 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
329 if (uuid_compare(iter
->uuid
, session_id
)) {
330 cds_list_del(&iter
->list
);
344 * Create a brand new session,
346 static int create_session(const char *name
, uuid_t
*session_id
)
348 struct ltt_session
*new_session
;
350 /* Allocate session data structure */
351 new_session
= malloc(sizeof(struct ltt_session
));
352 if (new_session
== NULL
) {
358 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
362 /* Generate session name based on the session count */
363 if (asprintf(&new_session
->name
, "%s%d", "auto", session_count
) < 0) {
368 /* UUID generation */
369 uuid_generate(new_session
->uuid
);
370 uuid_copy(*session_id
, new_session
->uuid
);
372 /* Set consumer (identifier) to 0. This means that there is
373 * NO consumer attach to that session yet.
375 new_session
->ust_consumer
= 0;
376 new_session
->lttng_consumer
= 0;
379 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
380 CDS_INIT_LIST_HEAD(&new_session
->lttng_traces
);
382 /* Add new session to the global session list */
383 cds_list_add(&new_session
->list
, <t_session_list
.head
);
396 * List traceable user-space application and fill an
399 * Return size of the array.
401 static size_t ust_list_apps(pid_t
**pids
)
404 struct ltt_traceable_app
*iter
= NULL
;
407 if (traceable_app_count
== 0) {
408 /* No dynamic allocation is done */
412 p
= malloc(sizeof(pid_t
) * traceable_app_count
);
414 /* TODO: Mutex needed to access this list */
415 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
429 * Copy common data between lttcomm_lttng_msg and lttcomm_session_msg
431 static void copy_common_data(struct lttcomm_lttng_msg
*llm
, struct lttcomm_session_msg
*lsm
)
433 llm
->cmd_type
= lsm
->cmd_type
;
435 if (!uuid_is_null(lsm
->session_id
)) {
436 uuid_copy(llm
->session_id
, lsm
->session_id
);
438 strncpy(llm
->trace_name
, lsm
->trace_name
, sizeof(llm
->trace_name
));
444 * This takes the lttcomm_session_msg struct and process the command requested
445 * by the client. It then creates response(s) and send it back to the
446 * given socket (sock).
448 * Return any error encountered or 0 for success.
450 static int process_client_msg(int sock
, struct lttcomm_session_msg
*lsm
)
453 struct lttcomm_lttng_msg llm
;
455 /* Copy common data to identify the response
456 * on the lttng client side.
458 copy_common_data(&llm
, lsm
);
460 /* Default return code.
461 * In our world, everything is OK... right? ;)
463 llm
.ret_code
= LTTCOMM_OK
;
465 /* Process by command type */
466 switch (lsm
->cmd_type
) {
470 llm
.num_pckt
= ust_list_apps(&pids
);
471 if (llm
.num_pckt
== 0) {
472 ret
= LTTCOMM_NO_APPS
;
476 /* Send all packets */
477 while (llm
.num_pckt
!= 0) {
478 llm
.u
.list_apps
.pid
= pids
[traceable_app_count
- llm
.num_pckt
];
479 ret
= send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
485 /* Allocated array by ust_list_apps() */
492 /* Undefined command */
504 /* Notify client of error */
506 send_unix_sock(sock
, (void*) &llm
, sizeof(llm
));
512 * usage function on stderr
514 static void usage(void)
516 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
517 "\t-h, --help\t\tDisplay this usage.\n"
518 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
519 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
520 "\t-d, --daemonize\t\tStart as a daemon.\n"
521 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
522 "\t-V, --version\t\tShow version number.\n"
523 "\t-S, --sig-parent\t\tSend SIGCHLD to parent pid to notify readiness.\n",
528 * daemon argument parsing
530 static int parse_args(int argc
, char **argv
)
534 static struct option long_options
[] = {
535 { "client-sock", 1, 0, 'c' },
536 { "apps-sock", 1, 0, 'a' },
537 { "daemonize", 0, 0, 'd' },
538 { "sig-parent", 0, 0, 'S' },
539 { "help", 0, 0, 'h' },
540 { "group", 1, 0, 'g' },
541 { "version", 0, 0, 'V' },
546 int option_index
= 0;
547 c
= getopt_long(argc
, argv
, "dhVS" "a:c:g:s:", long_options
, &option_index
);
554 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
556 fprintf(stderr
, " with arg %s\n", optarg
);
560 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
563 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
569 opt_tracing_group
= strdup(optarg
);
575 fprintf(stdout
, "%s\n", VERSION
);
581 /* Unknown option or other error.
582 * Error is printed by getopt, just return */
593 * Creates the two needed socket by the daemon.
594 * apps_socket - The communication socket for all UST apps.
595 * client_socket - The communication of the cli tool (lttng).
597 static int init_daemon_socket()
602 old_umask
= umask(0);
604 /* Create client tool unix socket */
605 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
606 if (client_socket
< 0) {
611 /* File permission MUST be 660 */
612 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
618 /* Create the application unix socket */
619 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
620 if (apps_socket
< 0) {
625 /* File permission MUST be 660 */
626 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
638 * check_existing_daemon
640 * Check if the global socket is available.
641 * If yes, error is returned.
643 static int check_existing_daemon()
647 ret
= access(client_unix_sock_path
, F_OK
);
649 ret
= access(apps_unix_sock_path
, F_OK
);
658 * Return pointer to home directory path using
659 * the env variable HOME.
663 static const char *get_home_dir(void)
665 const char *home_path
;
667 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
668 home_path
= default_home_dir
;
677 * Set the tracing group gid onto the client socket.
679 static int set_socket_perms(void)
684 /* Decide which group name to use */
685 (opt_tracing_group
!= NULL
) ?
686 (grp
= getgrnam(opt_tracing_group
)) :
687 (grp
= getgrnam(default_tracing_group
));
690 fprintf(stderr
, "Missing tracing group. Aborting execution.\n");
695 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
707 * Setup signal handler for :
708 * SIGINT, SIGTERM, SIGPIPE
710 static int set_signal_handler(void)
716 if ((ret
= sigemptyset(&sigset
)) < 0) {
717 perror("sigemptyset");
721 sa
.sa_handler
= sighandler
;
724 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
729 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
734 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
745 * Signal handler for the daemon
747 static void sighandler(int sig
)
765 * Cleanup the daemon on exit
767 static void cleanup()
770 fprintf(stdout
, "\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
771 fprintf(stdout
, "%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
774 unlink(client_unix_sock_path
);
775 unlink(apps_unix_sock_path
);
781 int main(int argc
, char **argv
)
786 pthread_t threads
[2];
788 /* Parse arguments */
790 if ((ret
= parse_args(argc
, argv
) < 0)) {
803 /* Check if daemon is UID = 0 */
806 /* Set all sockets path */
808 if (strlen(apps_unix_sock_path
) == 0) {
809 (snprintf(apps_unix_sock_path
, PATH_MAX
,
810 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
813 if (strlen(client_unix_sock_path
) == 0) {
814 (snprintf(client_unix_sock_path
, PATH_MAX
,
815 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
818 if (strlen(apps_unix_sock_path
) == 0) {
819 (snprintf(apps_unix_sock_path
, PATH_MAX
,
820 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
823 /* Set the cli tool unix socket path */
824 if (strlen(client_unix_sock_path
) == 0) {
825 (snprintf(client_unix_sock_path
, PATH_MAX
,
826 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
830 /* See if daemon already exist. If any of the two
831 * socket needed by the daemon are present, this test fails
833 if ((ret
= check_existing_daemon()) == 0) {
834 fprintf(stderr
, "Already running daemon.\n");
838 if (set_signal_handler() < 0) {
842 /* Setup the two needed unix socket */
843 if (init_daemon_socket() < 0) {
847 /* Set credentials to socket */
848 if (is_root
&& (set_socket_perms() < 0)) {
852 /* Get parent pid if -S, --sig-parent is specified. */
853 if (opt_sig_parent
) {
858 /* Create thread to manage the client socket */
859 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
861 perror("pthread_create");
865 /* Create thread to manage application socket */
866 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
868 perror("pthread_create");
872 for (i
= 0; i
< 2; i
++) {
873 ret
= pthread_join(threads
[i
], &status
);
875 perror("pthread_join");