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 daemonize(void);
52 static void cleanup(void);
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 struct lttcomm_lttng_msg
*process_client_msg(struct lttcomm_session_msg
*);
59 static void *thread_manage_clients(void *);
60 static void *thread_manage_apps(void *);
62 static int create_session(const char*, uuid_t
*);
63 static void destroy_session(uuid_t
);
65 static struct ltt_session
*find_session(uuid_t
);
69 const char *opt_tracing_group
;
70 static int opt_daemon
;
71 static int is_root
; /* Set to 1 if the daemon is running as root */
73 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
74 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
76 static int client_socket
;
77 static int apps_socket
;
79 static struct ltt_session
*current_session
;
80 static int session_count
;
82 /* Init session's list */
83 static struct ltt_session_list ltt_session_list
= {
84 .head
= CDS_LIST_HEAD_INIT(ltt_session_list
.head
),
87 static struct ltt_traceable_app_list ltt_traceable_app_list
= {
88 .head
= CDS_LIST_HEAD_INIT(ltt_traceable_app_list
.head
),
94 * This thread manage the application socket communication
96 static void *thread_manage_apps(void *data
)
99 struct ltt_traceable_app
*lta
;
101 /* TODO: Something more elegant is needed but fine for now */
103 int reg
; /* 1:register, 0:unregister */
108 /* Notify all applications to register */
109 notify_apps(default_global_apps_pipe
);
111 ret
= lttcomm_listen_unix_sock(apps_socket
);
117 /* Blocking call, waiting for transmission */
118 sock
= lttcomm_accept_unix_sock(apps_socket
);
123 /* Basic recv here to handle the very simple data
124 * that the libust send to register (reg_msg).
126 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
132 /* Add application to the global traceable list */
133 if (reg_msg
.reg
== 1) {
135 lta
= malloc(sizeof(struct ltt_traceable_app
));
136 lta
->pid
= reg_msg
.pid
;
137 lta
->uid
= reg_msg
.uid
;
138 cds_list_add(<a
->list
, <t_traceable_app_list
.head
);
142 cds_list_for_each_entry(lta
, <t_traceable_app_list
.head
, list
) {
143 if (lta
->pid
== reg_msg
.pid
&& lta
->uid
== reg_msg
.uid
) {
144 cds_list_del(<a
->list
);
149 /* If an item was found, free it from memory */
162 * thread_manage_clients
164 * This thread manage all clients request using the unix
165 * client socket for communication.
167 static void *thread_manage_clients(void *data
)
170 struct lttcomm_session_msg lsm
;
171 struct lttcomm_lttng_msg
*llm
;
173 ret
= lttcomm_listen_unix_sock(client_socket
);
179 /* Blocking call, waiting for transmission */
180 sock
= lttcomm_accept_unix_sock(client_socket
);
186 * Data is received from the lttng client. The struct
187 * lttcomm_session_msg (lsm) contains the command and data
188 * request of the client.
190 ret
= lttcomm_recv_unix_sock(sock
, &lsm
, sizeof(lsm
));
195 /* This function dispatch the work to the LTTng or UST libs
196 * and make sure that the reply structure (llm) is filled.
198 llm
= process_client_msg(&lsm
);
200 /* Having a valid lttcomm_lttng_msg struct, reply is sent back
201 * to the client directly.
204 ret
= lttcomm_send_unix_sock(sock
, llm
,
205 sizeof(struct lttcomm_lttng_msg
));
211 /* The lttcomm_lttng_msg struct was not allocated
212 * correctly. Fatal error since the daemon is not able
213 * to respond. However, we still permit client connection.
215 * TODO: We should have a default llm that tells the client
216 * that the sessiond had a fatal error and thus the client could
217 * take action to restart ltt-sessiond or inform someone.
229 * Return a socket connected to the libust communication socket
230 * of the application identified by the pid.
232 static int connect_app(pid_t pid
)
236 sock
= ustctl_connect_pid(pid
);
238 fprintf(stderr
, "Fail connecting to the PID %d\n", pid
);
247 * Notify apps by writing 42 to a named pipe using name.
248 * Every applications waiting for a ltt-sessiond will be notified
249 * and re-register automatically to the session daemon.
251 * Return open or write error value.
253 static int notify_apps(const char *name
)
258 /* Try opening the global pipe */
259 fd
= open(name
, O_WRONLY
);
264 /* Notify by writing on the pipe */
265 ret
= write(fd
, "42", 2);
277 * Return a ltt_session structure ptr that matches the uuid.
279 static struct ltt_session
*find_session(uuid_t session_id
)
281 struct ltt_session
*iter
= NULL
;
283 /* Sanity check for NULL session_id */
284 if (uuid_is_null(session_id
)) {
288 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
289 if (uuid_compare(iter
->uuid
, session_id
)) {
301 * Delete session from the global session list
302 * and free the memory.
304 static void destroy_session(uuid_t session_id
)
306 struct ltt_session
*iter
= NULL
;
308 cds_list_for_each_entry(iter
, <t_session_list
.head
, list
) {
309 if (uuid_compare(iter
->uuid
, session_id
)) {
310 cds_list_del(&iter
->list
);
324 * Create a brand new session,
326 static int create_session(const char *name
, uuid_t
*session_id
)
328 struct ltt_session
*new_session
;
330 /* Allocate session data structure */
331 new_session
= malloc(sizeof(struct ltt_session
));
332 if (new_session
== NULL
) {
338 if (asprintf(&new_session
->name
, "%s", name
) < 0) {
342 /* Generate session name based on the session count */
343 if (asprintf(&new_session
->name
, "%s%d", "auto", session_count
) < 0) {
348 /* UUID generation */
349 uuid_generate(new_session
->uuid
);
350 uuid_copy(*session_id
, new_session
->uuid
);
352 /* Set consumer (identifier) to 0. This means that there is
353 * NO consumer attach to that session yet.
355 new_session
->ust_consumer
= 0;
356 new_session
->lttng_consumer
= 0;
359 CDS_INIT_LIST_HEAD(&new_session
->ust_traces
);
360 CDS_INIT_LIST_HEAD(&new_session
->lttng_traces
);
362 /* Add new session to the global session list */
363 cds_list_add(&new_session
->list
, <t_session_list
.head
);
376 * List traceable user-space application and fill an
379 * Return size of the array.
381 static size_t ust_list_apps(pid_t
*pids
)
384 struct ltt_traceable_app
*iter
= NULL
;
386 cds_list_for_each_entry(iter
, <t_traceable_app_list
.head
, list
) {
387 if (size
>= MAX_APPS_PID
) {
391 pids
[size
] = iter
->pid
;
401 * This takes the lttcomm_session_msg struct and process the command requested
402 * by the client. It then creates the reply by allocating a lttcomm_lttng_msg
403 * and fill it with the necessary information.
405 * It's the caller responsability to free that structure when done with it.
407 * Return pointer to lttcomm_lttng_msg allocated struct.
409 static struct lttcomm_lttng_msg
*process_client_msg(struct lttcomm_session_msg
*lsm
)
411 struct lttcomm_lttng_msg
*llm
;
413 /* Allocate the reply message structure */
414 llm
= malloc(sizeof(struct lttcomm_lttng_msg
));
420 /* Copy common data to identify the response
421 * on the lttng client side.
423 llm
->cmd_type
= lsm
->cmd_type
;
425 if (!uuid_is_null(lsm
->session_id
)) {
426 uuid_copy(llm
->session_id
, lsm
->session_id
);
428 strncpy(llm
->trace_name
, lsm
->trace_name
, sizeof(llm
->trace_name
));
430 /* Default return code.
431 * In a our world, everything is OK... right?
433 llm
->ret_code
= LTTCOMM_OK
;
435 /* Process by command type */
436 switch (lsm
->cmd_type
) {
439 llm
->u
.list_apps
.size
= ust_list_apps(llm
->u
.list_apps
.pids
);
443 /* Undefined command */
444 llm
->ret_code
= LTTCOMM_UND
;
453 * usage function on stderr
455 static void usage(void)
457 fprintf(stderr
, "Usage:\n%s OPTIONS\n\nOptions:\n"
458 "\t-h, --help\t\tDisplay this usage.\n"
459 "\t-c, --client-sock PATH\t\tSpecify path for the client unix socket\n"
460 "\t-a, --apps-sock PATH\t\tSpecify path for apps unix socket.\n"
461 "\t-d, --daemonize\t\tStart as a daemon.\n"
462 "\t-g, --group NAME\t\tSpecify the tracing group name. (default: tracing)\n"
463 "\t-V, --version\t\tShow version number.\n",
468 * daemon argument parsing
470 static int parse_args(int argc
, char **argv
)
474 static struct option long_options
[] = {
475 { "client-sock", 1, 0, 'c' },
476 { "apps-sock", 1, 0, 'a' },
477 { "daemonize", 0, 0, 'd' },
478 { "help", 0, 0, 'h' },
479 { "group", 1, 0, 'g' },
480 { "version", 0, 0, 'V' },
485 int option_index
= 0;
486 c
= getopt_long(argc
, argv
, "dhV" "a:c:g:s:", long_options
, &option_index
);
493 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
495 fprintf(stderr
, " with arg %s\n", optarg
);
499 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
502 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
508 opt_tracing_group
= strdup(optarg
);
514 fprintf(stdout
, "%s\n", VERSION
);
517 /* Unknown option or other error.
518 * Error is printed by getopt, just return */
529 * Creates the two needed socket by the daemon.
530 * apps_socket - The communication socket for all UST apps.
531 * client_socket - The communication of the cli tool (lttng).
533 static int init_daemon_socket()
538 old_umask
= umask(0);
540 /* Create client tool unix socket */
541 client_socket
= lttcomm_create_unix_sock(client_unix_sock_path
);
542 if (client_socket
< 0) {
547 /* File permission MUST be 660 */
548 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
554 /* Create the application unix socket */
555 apps_socket
= lttcomm_create_unix_sock(apps_unix_sock_path
);
556 if (apps_socket
< 0) {
561 /* File permission MUST be 660 */
562 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
574 * check_existing_daemon
576 * Check if the global socket is available.
577 * If yes, error is returned.
579 static int check_existing_daemon()
583 ret
= access(client_unix_sock_path
, F_OK
);
585 ret
= access(apps_unix_sock_path
, F_OK
);
594 * Return pointer to home directory path using
595 * the env variable HOME.
599 static const char *get_home_dir(void)
601 const char *home_path
;
603 if ((home_path
= (const char *) getenv("HOME")) == NULL
) {
604 home_path
= default_home_dir
;
613 * Set the tracing group gid onto the client socket.
615 static int set_socket_perms(void)
620 /* Decide which group name to use */
621 (opt_tracing_group
!= NULL
) ?
622 (grp
= getgrnam(opt_tracing_group
)) :
623 (grp
= getgrnam(default_tracing_group
));
626 fprintf(stderr
, "Missing tracing group. Aborting execution.\n");
631 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
643 * Daemonize ltt-sessiond.
645 static void daemonize(void)
648 const char *home_dir
= get_home_dir();
650 /* Fork off the parent process */
651 if ((pid
= fork()) < 0) {
656 /* Parent can now exit */
661 /* Change the file mode mask */
664 /* Create a new SID for the child process */
665 if ((sid
= setsid()) < 0) {
670 /* Change the current working directory */
671 if ((chdir(home_dir
)) < 0) {
676 /* Close out the standard file descriptors */
678 close(STDOUT_FILENO
);
679 close(STDERR_FILENO
);
685 * Setup signal handler for :
686 * SIGINT, SIGTERM, SIGPIPE
688 static int set_signal_handler(void)
694 if ((ret
= sigemptyset(&sigset
)) < 0) {
695 perror("sigemptyset");
699 sa
.sa_handler
= sighandler
;
702 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
707 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
712 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
723 * Signal handler for the daemon
725 static void sighandler(int sig
)
743 * Cleanup the daemon on exit
745 static void cleanup()
748 fprintf(stdout
, "\n\n%c[%d;%dm*** assert failed *** ==> %c[%dm", 27,1,31,27,0);
749 fprintf(stdout
, "%c[%d;%dm Matthew, BEET driven development works!%c[%dm\n",27,1,33,27,0);
752 unlink(client_unix_sock_path
);
753 unlink(apps_unix_sock_path
);
759 int main(int argc
, char **argv
)
764 pthread_t threads
[2];
766 /* Parse arguments */
768 if ((ret
= parse_args(argc
, argv
) < 0)) {
777 /* Check if daemon is UID = 0 */
780 /* Set all sockets path */
782 if (strlen(apps_unix_sock_path
) == 0) {
783 (snprintf(apps_unix_sock_path
, PATH_MAX
,
784 DEFAULT_GLOBAL_APPS_UNIX_SOCK
));
787 if (strlen(client_unix_sock_path
) == 0) {
788 (snprintf(client_unix_sock_path
, PATH_MAX
,
789 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
));
792 if (strlen(apps_unix_sock_path
) == 0) {
793 (snprintf(apps_unix_sock_path
, PATH_MAX
,
794 DEFAULT_HOME_APPS_UNIX_SOCK
, get_home_dir()));
797 /* Set the cli tool unix socket path */
798 if (strlen(client_unix_sock_path
) == 0) {
799 (snprintf(client_unix_sock_path
, PATH_MAX
,
800 DEFAULT_HOME_CLIENT_UNIX_SOCK
, get_home_dir()));
804 /* See if daemon already exist. If any of the two
805 * socket needed by the daemon are present, this test fails
807 if ((ret
= check_existing_daemon()) == 0) {
808 fprintf(stderr
, "Already running daemon.\n");
812 if (set_signal_handler() < 0) {
816 /* Setup the two needed unix socket */
817 if (init_daemon_socket() < 0) {
821 /* Set credentials to socket */
822 if (is_root
&& (set_socket_perms() < 0)) {
827 /* Create thread to manage the client socket */
828 ret
= pthread_create(&threads
[0], NULL
, thread_manage_clients
, (void *) NULL
);
830 perror("pthread_create");
834 /* Create thread to manage application socket */
835 ret
= pthread_create(&threads
[1], NULL
, thread_manage_apps
, (void *) NULL
);
837 perror("pthread_create");
841 for (i
= 0; i
< 2; i
++) {
842 ret
= pthread_join(threads
[i
], &status
);
844 perror("pthread_join");