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 /* Static functions */
43 static int set_signal_handler(void);
44 static int set_socket_perms(void);
45 static void sighandler(int);
46 static void daemonize(void);
47 static void cleanup(void);
48 static int check_existing_daemon(void);
49 static int notify_apps(const char*);
50 static int connect_app(pid_t
);
51 static int init_daemon_socket(void);
52 static struct lttcomm_lttng_msg
*process_client_msg(struct lttcomm_session_msg
*);
54 static void *thread_manage_clients(void *);
55 static void *thread_manage_apps(void *);
57 static int create_session(const char*, uuid_t
*);
58 static void destroy_session(uuid_t
);
60 static struct ltt_session
*find_session(uuid_t
);
64 const char *opt_tracing_group
;
65 static int opt_daemon
;
66 static int is_root
; /* Set to 1 if the daemon is running as root */
68 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
69 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
71 static int client_socket
;
72 static int apps_socket
;
74 static struct ltt_session
*current_session
;
75 static int session_count
;
77 /* Init session's list */
78 static struct ltt_session_list ltt_session_list
= {
79 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
82 static struct ltt_traceable_app_list ltt_traceable_app_list
= {
83 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
89 * This thread manage the application socket communication
91 static void *thread_manage_apps(void *data
)
94 struct ltt_traceable_app
*lta
;
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 lta
= malloc(sizeof(struct ltt_traceable_app
));
131 lta
->pid
= reg_msg
.pid
;
132 lta
->uid
= reg_msg
.uid
;
133 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
137 cds_list_for_each_entry(lta
, <t_traceable_app_list
.head
, list
) {
138 if (lta
->pid
== reg_msg
.pid
&& lta
->uid
== reg_msg
.uid
) {
139 cds_list_del(<a
->list
);
144 /* If an item was found, free it from memory */
157 * thread_manage_clients
159 * This thread manage all clients request using the unix
160 * client socket for communication.
162 static void *thread_manage_clients(void *data
)
165 struct lttcomm_session_msg lsm
;
166 struct lttcomm_lttng_msg
*llm
;
168 ret
= lttcomm_listen_unix_sock(client_socket
);
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 make sure that the reply structure (llm) is filled.
193 llm
= process_client_msg(&lsm
);
195 /* Having a valid lttcomm_lttng_msg struct, reply is sent back
196 * to the client directly.
199 ret
= lttcomm_send_unix_sock(sock
, llm
,
200 sizeof(struct lttcomm_lttng_msg
));
206 /* The lttcomm_lttng_msg struct was not allocated
207 * correctly. Fatal error since the daemon is not able
208 * to respond. However, we still permit client connection.
210 * TODO: We should have a default llm that tells the client
211 * that the sessiond had a fatal error and thus the client could
212 * take action to restart ltt-sessiond or inform someone.
224 * Return a socket connected to the libust communication socket
225 * of the application identified by the pid.
227 static int connect_app(pid_t pid
)
231 sock
= ustctl_connect_pid(pid
);
233 fprintf(stderr
, "Fail connecting to the PID %d\n", pid
);
242 * Notify apps by writing 42 to a named pipe using name.
243 * Every applications waiting for a ltt-sessiond will be notified
244 * and re-register automatically to the session daemon.
246 * Return open or write error value.
248 static int notify_apps(const char *name
)
253 /* Try opening the global pipe */
254 fd
= open(name
, O_WRONLY
);
259 /* Notify by writing on the pipe */
260 ret
= write(fd
, "42", 2);
272 * Return a ltt_session structure ptr that matches the uuid.
274 static struct ltt_session
*find_session(uuid_t session_id
)
276 struct ltt_session
*iter
= NULL
;
278 /* Sanity check for NULL session_id */
279 if (uuid_is_null(session_id
)) {
283 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
284 if (uuid_compare(iter
->uuid
, session_id
)) {
296 * Delete session from the global session list
297 * and free the memory.
299 static void destroy_session(uuid_t session_id
)
301 struct ltt_session
*iter
= NULL
;
303 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
304 if (uuid_compare(iter
->uuid
, session_id
)) {
305 cds_list_del(&iter
->list
);
319 * Create a brand new session,
321 static int create_session(const char *name
, uuid_t
*session_id
)
323 struct ltt_session
*new_session
;
325 /* Allocate session data structure */
326 new_session
= malloc(sizeof(struct ltt_session
));
327 if (new_session
== NULL
) {
333 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
337 /* Generate session name based on the session count */
338 if (asprintf(&new_session
->name
, "%s%d", "auto", session_count
) < 0) {
343 /* UUID generation */
344 uuid_generate(new_session
->uuid
);
345 uuid_copy(*session_id
, new_session
->uuid
);
347 /* Set consumer (identifier) to 0. This means that there is
348 * NO consumer attach to that session yet.
350 new_session
->ust_consumer
= 0;
351 new_session
->lttng_consumer
= 0;
354 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
355 CDS_INIT_LIST_HEAD(&new_session
->lttng_traces
);
357 /* Add new session to the global session list */
358 cds_list_add(&new_session
->list
, <t_session_list
.head
);
371 * List traceable user-space application and fill an
374 * Return size of the array.
376 static size_t ust_list_apps(pid_t
*pids
)
379 struct ltt_traceable_app
*iter
= NULL
;
381 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
382 if (size
>= MAX_APPS_PID
) {
386 pids
[size
] = iter
->pid
;
396 * This takes the lttcomm_session_msg struct and process the command requested
397 * by the client. It then creates the reply by allocating a lttcomm_lttng_msg
398 * and fill it with the necessary information.
400 * It's the caller responsability to free that structure when done with it.
402 * Return pointer to lttcomm_lttng_msg allocated struct.
404 static struct lttcomm_lttng_msg
*process_client_msg(struct lttcomm_session_msg
*lsm
)
406 struct lttcomm_lttng_msg
*llm
;
408 /* Allocate the reply message structure */
409 llm
= malloc(sizeof(struct lttcomm_lttng_msg
));
415 /* Copy common data to identify the response
416 * on the lttng client side.
418 llm
->cmd_type
= lsm
->cmd_type
;
420 if (!uuid_is_null(lsm
->session_id
)) {
421 uuid_copy(llm
->session_id
, lsm
->session_id
);
423 strncpy(llm
->trace_name
, lsm
->trace_name
, sizeof(llm
->trace_name
));
425 /* Default return code.
426 * In a our world, everything is OK... right?
428 llm
->ret_code
= LTTCOMM_OK
;
430 /* Process by command type */
431 switch (lsm
->cmd_type
) {
434 llm
->u
.list_apps
.size
= ust_list_apps(llm
->u
.list_apps
.pids
);
438 /* Undefined command */
439 llm
->ret_code
= LTTCOMM_UND
;
448 * usage function on stderr
450 static void usage(void)
452 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
453 "\t-h, --help\t\tDisplay this usage.\n"
454 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
455 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
456 "\t-d, --daemonize\t\tStart as a daemon.\n"
457 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
458 "\t-V, --version\t\tShow version number.\n",
463 * daemon argument parsing
465 static int parse_args(int argc
, char **argv
)
469 static struct option long_options
[] = {
470 { "client-sock", 1, 0, 'c' },
471 { "apps-sock", 1, 0, 'a' },
472 { "daemonize", 0, 0, 'd' },
473 { "help", 0, 0, 'h' },
474 { "group", 1, 0, 'g' },
475 { "version", 0, 0, 'V' },
480 int option_index
= 0;
481 c
= getopt_long(argc
, argv
, "dhV" "a:c:g:s:", long_options
, &option_index
);
488 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
490 fprintf(stderr
, " with arg %s\n", optarg
);
494 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
497 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
503 opt_tracing_group
= strdup(optarg
);
509 fprintf(stdout
, "%s\n", VERSION
);
512 /* Unknown option or other error.
513 * Error is printed by getopt, just return */
524 * Creates the two needed socket by the daemon.
525 * apps_socket - The communication socket for all UST apps.
526 * client_socket - The communication of the cli tool (lttng).
528 static int init_daemon_socket()
533 old_umask
= umask(0);
535 /* Create client tool unix socket */
536 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
537 if (client_socket
< 0) {
542 /* File permission MUST be 660 */
543 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
549 /* Create the application unix socket */
550 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
551 if (apps_socket
< 0) {
556 /* File permission MUST be 660 */
557 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
569 * check_existing_daemon
571 * Check if the global socket is available.
572 * If yes, error is returned.
574 static int check_existing_daemon()
578 ret
= access(client_unix_sock_path
, F_OK
);
580 ret
= access(apps_unix_sock_path
, F_OK
);
589 * Return pointer to home directory path using
590 * the env variable HOME.
594 static const char *get_home_dir(void)
596 const char *home_path
;
598 if ((home_path
= (const char*) getenv("HOME")) == NULL
) {
599 home_path
= default_home_dir
;
608 * Set the tracing group gid onto the client socket.
610 static int set_socket_perms(void)
615 /* Decide which group name to use */
616 (opt_tracing_group
!= NULL
) ?
617 (grp
= getgrnam(opt_tracing_group
)) :
618 (grp
= getgrnam(default_tracing_group
));
621 fprintf(stderr
, "Missing tracing group. Aborting execution.\n");
626 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
638 * Daemonize ltt-sessiond.
640 static void daemonize(void)
643 const char *home_dir
= get_home_dir();
645 /* Fork off the parent process */
646 if ((pid
= fork()) < 0) {
651 /* Parent can now exit */
656 /* Change the file mode mask */
659 /* Create a new SID for the child process */
660 if ((sid
= setsid()) < 0) {
665 /* Change the current working directory */
666 if ((chdir(home_dir
)) < 0) {
671 /* Close out the standard file descriptors */
673 close(STDOUT_FILENO
);
674 close(STDERR_FILENO
);
680 * Setup signal handler for :
681 * SIGINT, SIGTERM, SIGPIPE
683 static int set_signal_handler(void)
689 if ((ret
= sigemptyset(&sigset
)) < 0) {
690 perror("sigemptyset");
694 sa
.sa_handler
= sighandler
;
697 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
702 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
707 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
718 * Signal handler for the daemon
720 static void sighandler(int sig
)
737 * Cleanup the daemon on exit
739 static void cleanup()
742 fprintf(stdout
, "\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
743 fprintf(stdout
, "%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
746 unlink(client_unix_sock_path
);
747 unlink(apps_unix_sock_path
);
753 int main(int argc
, char **argv
)
758 pthread_t threads
[2];
760 /* Parse arguments */
762 if ((ret
= parse_args(argc
, argv
) < 0)) {
771 /* Check if daemon is UID = 0 */
774 /* Set all sockets path */
776 if (strlen(apps_unix_sock_path
) == 0) {
777 (snprintf(apps_unix_sock_path
, PATH_MAX
,
778 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
781 if (strlen(client_unix_sock_path
) == 0) {
782 (snprintf(client_unix_sock_path
, PATH_MAX
,
783 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
786 if (strlen(apps_unix_sock_path
) == 0) {
787 (snprintf(apps_unix_sock_path
, PATH_MAX
,
788 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
791 /* Set the cli tool unix socket path */
792 if (strlen(client_unix_sock_path
) == 0) {
793 (snprintf(client_unix_sock_path
, PATH_MAX
,
794 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
798 /* See if daemon already exist. If any of the two
799 * socket needed by the daemon are present, this test fails
801 if ((ret
= check_existing_daemon()) == 0) {
802 fprintf(stderr
, "Already running daemon.\n");
806 if (set_signal_handler() < 0) {
810 /* Setup the two needed unix socket */
811 if (init_daemon_socket() < 0) {
815 /* Set credentials to socket */
816 if (is_root
&& (set_socket_perms() < 0)) {
821 /* Create thread to manage the client socket */
822 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
824 perror("pthread_create");
828 /* Create thread to manage application socket */
829 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
831 perror("pthread_create");
835 for (i
= 0; i
< 2; i
++) {
836 ret
= pthread_join(threads
[i
], &status
);
838 perror("pthread_join");