2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <urcu/list.h>
41 #include "kernelctl.h"
42 #include "lttkconsumerd.h"
44 /* the two threads (receive fd and poll) */
47 /* to count the number of time the user pressed ctrl+c */
48 static int sigintcount
= 0;
50 /* Argument variables */
53 static int opt_daemon
;
54 static const char *progname
;
55 char command_sock_path
[PATH_MAX
]; /* Global command socket path */
56 char error_sock_path
[PATH_MAX
]; /* Global error path */
61 * Signal handler for the daemon
63 static void sighandler(int sig
)
65 if (sig
== SIGINT
&& sigintcount
++ == 0) {
66 DBG("ignoring first SIGINT");
70 kconsumerd_should_exit();
76 * Setup signal handler for :
77 * SIGINT, SIGTERM, SIGPIPE
79 static int set_signal_handler(void)
85 if ((ret
= sigemptyset(&sigset
)) < 0) {
86 perror("sigemptyset");
90 sa
.sa_handler
= sighandler
;
93 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
98 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
103 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
112 * usage function on stderr
114 static void usage(void)
116 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
117 fprintf(stderr
, " -h, --help "
118 "Display this usage.\n");
119 fprintf(stderr
, " -c, --kconsumerd-cmd-sock PATH "
120 "Specify path for the command socket\n");
121 fprintf(stderr
, " -e, --kconsumerd-err-sock PATH "
122 "Specify path for the error socket\n");
123 fprintf(stderr
, " -d, --daemonize "
124 "Start as a daemon.\n");
125 fprintf(stderr
, " -q, --quiet "
126 "No output at all.\n");
127 fprintf(stderr
, " -v, --verbose "
128 "Verbose mode. Activate DBG() macro.\n");
129 fprintf(stderr
, " -V, --version "
130 "Show version number.\n");
134 * daemon argument parsing
136 static void parse_args(int argc
, char **argv
)
140 static struct option long_options
[] = {
141 { "kconsumerd-cmd-sock", 1, 0, 'c' },
142 { "kconsumerd-err-sock", 1, 0, 'e' },
143 { "daemonize", 0, 0, 'd' },
144 { "help", 0, 0, 'h' },
145 { "quiet", 0, 0, 'q' },
146 { "verbose", 0, 0, 'v' },
147 { "version", 0, 0, 'V' },
152 int option_index
= 0;
153 c
= getopt_long(argc
, argv
, "dhqvV" "c:e:", long_options
, &option_index
);
160 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
162 fprintf(stderr
, " with arg %s\n", optarg
);
166 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
169 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
184 fprintf(stdout
, "%s\n", VERSION
);
197 int main(int argc
, char **argv
)
203 /* Parse arguments */
205 parse_args(argc
, argv
);
216 if (strlen(command_sock_path
) == 0) {
217 snprintf(command_sock_path
, PATH_MAX
,
218 KCONSUMERD_CMD_SOCK_PATH
);
220 kconsumerd_set_command_socket_path(command_sock_path
);
221 if (strlen(error_sock_path
) == 0) {
222 snprintf(error_sock_path
, PATH_MAX
,
223 KCONSUMERD_ERR_SOCK_PATH
);
226 if (set_signal_handler() < 0) {
230 /* create the pipe to wake to receiving thread when needed */
231 ret
= kconsumerd_init();
236 /* Connect to the socket created by ltt-sessiond to report errors */
237 DBG("Connecting to error socket %s", error_sock_path
);
238 ret
= lttcomm_connect_unix_sock(error_sock_path
);
239 /* not a fatal error, but all communication with ltt-sessiond will fail */
241 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
243 kconsumerd_set_error_socket(ret
);
245 /* Create the thread to manage the receive of fd */
246 ret
= pthread_create(&threads
[0], NULL
, kconsumerd_thread_receive_fds
,
249 perror("pthread_create");
253 /* Create thread to manage the polling/writing of traces */
254 ret
= pthread_create(&threads
[1], NULL
, kconsumerd_thread_poll_fds
,
257 perror("pthread_create");
261 for (i
= 0; i
< 2; i
++) {
262 ret
= pthread_join(threads
[i
], &status
);
264 perror("pthread_join");
269 kconsumerd_send_error(KCONSUMERD_EXIT_SUCCESS
);
274 kconsumerd_send_error(KCONSUMERD_EXIT_FAILURE
);
277 kconsumerd_cleanup();