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; either version 2
8 * of the License, or (at your option) any later version.
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 "libkernelctl.h"
42 #include "liblttsessiondcomm.h"
43 #include "liblttkconsumerd.h"
45 /* the two threads (receive fd and poll) */
48 /* to count the number of time the user pressed ctrl+c */
49 static int sigintcount
= 0;
51 /* Argument variables */
54 static int opt_daemon
;
55 static const char *progname
;
56 char command_sock_path
[PATH_MAX
]; /* Global command socket path */
57 char error_sock_path
[PATH_MAX
]; /* Global error path */
62 * Signal handler for the daemon
64 static void sighandler(int sig
)
66 if (sig
== SIGINT
&& sigintcount
++ == 0) {
67 DBG("ignoring first SIGINT");
77 * Setup signal handler for :
78 * SIGINT, SIGTERM, SIGPIPE
80 static int set_signal_handler(void)
86 if ((ret
= sigemptyset(&sigset
)) < 0) {
87 perror("sigemptyset");
91 sa
.sa_handler
= sighandler
;
94 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
99 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
104 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
113 * usage function on stderr
115 static void usage(void)
117 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
118 fprintf(stderr
, " -h, --help "
119 "Display this usage.\n");
120 fprintf(stderr
, " -c, --kconsumerd-cmd-sock PATH "
121 "Specify path for the command socket\n");
122 fprintf(stderr
, " -e, --kconsumerd-err-sock PATH "
123 "Specify path for the error socket\n");
124 fprintf(stderr
, " -d, --daemonize "
125 "Start as a daemon.\n");
126 fprintf(stderr
, " -q, --quiet "
127 "No output at all.\n");
128 fprintf(stderr
, " -v, --verbose "
129 "Verbose mode. Activate DBG() macro.\n");
130 fprintf(stderr
, " -V, --version "
131 "Show version number.\n");
135 * daemon argument parsing
137 static void parse_args(int argc
, char **argv
)
141 static struct option long_options
[] = {
142 { "kconsumerd-cmd-sock", 1, 0, 'c' },
143 { "kconsumerd-err-sock", 1, 0, 'e' },
144 { "daemonize", 0, 0, 'd' },
145 { "help", 0, 0, 'h' },
146 { "quiet", 0, 0, 'q' },
147 { "verbose", 0, 0, 'v' },
148 { "version", 0, 0, 'V' },
153 int option_index
= 0;
154 c
= getopt_long(argc
, argv
, "dhqvV" "c:e:", long_options
, &option_index
);
161 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
163 fprintf(stderr
, " with arg %s\n", optarg
);
167 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
170 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
185 fprintf(stdout
, "%s\n", VERSION
);
198 int main(int argc
, char **argv
)
204 /* Parse arguments */
206 parse_args(argc
, argv
);
217 if (strlen(command_sock_path
) == 0) {
218 snprintf(command_sock_path
, PATH_MAX
,
219 KCONSUMERD_CMD_SOCK_PATH
);
221 kconsumerd_set_command_socket_path(command_sock_path
);
222 if (strlen(error_sock_path
) == 0) {
223 snprintf(error_sock_path
, PATH_MAX
,
224 KCONSUMERD_ERR_SOCK_PATH
);
227 if (set_signal_handler() < 0) {
231 /* create the pipe to wake to polling thread when needed */
232 ret
= kconsumerd_create_poll_pipe();
234 perror("Error creating poll pipe");
238 /* Connect to the socket created by ltt-sessiond to report errors */
239 DBG("Connecting to error socket %s", error_sock_path
);
240 ret
= lttcomm_connect_unix_sock(error_sock_path
);
241 /* not a fatal error, but all communication with ltt-sessiond will fail */
243 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
245 kconsumerd_set_error_socket(ret
);
247 /* Create the thread to manage the receive of fd */
248 ret
= pthread_create(&threads
[0], NULL
, kconsumerd_thread_receive_fds
,
251 perror("pthread_create");
255 /* Create thread to manage the polling/writing of traces */
256 ret
= pthread_create(&threads
[1], NULL
, kconsumerd_thread_poll_fds
,
259 perror("pthread_create");
263 for (i
= 0; i
< 2; i
++) {
264 ret
= pthread_join(threads
[i
], &status
);
266 perror("pthread_join");
271 kconsumerd_send_error(KCONSUMERD_EXIT_SUCCESS
);
276 kconsumerd_send_error(KCONSUMERD_EXIT_FAILURE
);
279 kconsumerd_cleanup();