Create a library for the kernel consumer
[lttng-tools.git] / kconsumerd / kconsumerd.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #define _GNU_SOURCE
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <grp.h>
24 #include <limits.h>
25 #include <pthread.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/ipc.h>
31 #include <sys/shm.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <urcu/list.h>
36 #include <poll.h>
37 #include <unistd.h>
38 #include <sys/mman.h>
39
40 #include "lttngerr.h"
41 #include "libkernelctl.h"
42 #include "liblttsessiondcomm.h"
43 #include "liblttkconsumerd.h"
44
45 /* the two threads (receive fd and poll) */
46 pthread_t threads[2];
47
48 /* to count the number of time the user pressed ctrl+c */
49 static int sigintcount = 0;
50
51 /* Argument variables */
52 int opt_quiet;
53 int opt_verbose;
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 */
58
59 /*
60 * sighandler
61 *
62 * Signal handler for the daemon
63 */
64 static void sighandler(int sig)
65 {
66 if (sig == SIGINT && sigintcount++ == 0) {
67 DBG("ignoring first SIGINT");
68 return;
69 }
70
71 kconsumerd_cleanup();
72 }
73
74 /*
75 * set_signal_handler
76 *
77 * Setup signal handler for :
78 * SIGINT, SIGTERM, SIGPIPE
79 */
80 static int set_signal_handler(void)
81 {
82 int ret = 0;
83 struct sigaction sa;
84 sigset_t sigset;
85
86 if ((ret = sigemptyset(&sigset)) < 0) {
87 perror("sigemptyset");
88 return ret;
89 }
90
91 sa.sa_handler = sighandler;
92 sa.sa_mask = sigset;
93 sa.sa_flags = 0;
94 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
95 perror("sigaction");
96 return ret;
97 }
98
99 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
100 perror("sigaction");
101 return ret;
102 }
103
104 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
105 perror("sigaction");
106 return ret;
107 }
108
109 return ret;
110 }
111
112 /*
113 * usage function on stderr
114 */
115 static void usage(void)
116 {
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");
132 }
133
134 /*
135 * daemon argument parsing
136 */
137 static void parse_args(int argc, char **argv)
138 {
139 int c;
140
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' },
149 { NULL, 0, 0, 0 }
150 };
151
152 while (1) {
153 int option_index = 0;
154 c = getopt_long(argc, argv, "dhqvV" "c:e:", long_options, &option_index);
155 if (c == -1) {
156 break;
157 }
158
159 switch (c) {
160 case 0:
161 fprintf(stderr, "option %s", long_options[option_index].name);
162 if (optarg) {
163 fprintf(stderr, " with arg %s\n", optarg);
164 }
165 break;
166 case 'c':
167 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
168 break;
169 case 'e':
170 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
171 break;
172 case 'd':
173 opt_daemon = 1;
174 break;
175 case 'h':
176 usage();
177 exit(EXIT_FAILURE);
178 case 'q':
179 opt_quiet = 1;
180 break;
181 case 'v':
182 opt_verbose = 1;
183 break;
184 case 'V':
185 fprintf(stdout, "%s\n", VERSION);
186 exit(EXIT_SUCCESS);
187 default:
188 usage();
189 exit(EXIT_FAILURE);
190 }
191 }
192 }
193
194
195 /*
196 * main
197 */
198 int main(int argc, char **argv)
199 {
200 int i;
201 int ret = 0;
202 void *status;
203
204 /* Parse arguments */
205 progname = argv[0];
206 parse_args(argc, argv);
207
208 /* Daemonize */
209 if (opt_daemon) {
210 ret = daemon(0, 0);
211 if (ret < 0) {
212 perror("daemon");
213 goto error;
214 }
215 }
216
217 if (strlen(command_sock_path) == 0) {
218 snprintf(command_sock_path, PATH_MAX,
219 KCONSUMERD_CMD_SOCK_PATH);
220 }
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);
225 }
226
227 if (set_signal_handler() < 0) {
228 goto error;
229 }
230
231 /* create the pipe to wake to polling thread when needed */
232 ret = kconsumerd_create_poll_pipe();
233 if (ret < 0) {
234 perror("Error creating poll pipe");
235 goto end;
236 }
237
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 */
242 if (ret < 0) {
243 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
244 }
245 kconsumerd_set_error_socket(ret);
246
247 /* Create the thread to manage the receive of fd */
248 ret = pthread_create(&threads[0], NULL, kconsumerd_thread_receive_fds,
249 (void *) NULL);
250 if (ret != 0) {
251 perror("pthread_create");
252 goto error;
253 }
254
255 /* Create thread to manage the polling/writing of traces */
256 ret = pthread_create(&threads[1], NULL, kconsumerd_thread_poll_fds,
257 (void *) NULL);
258 if (ret != 0) {
259 perror("pthread_create");
260 goto error;
261 }
262
263 for (i = 0; i < 2; i++) {
264 ret = pthread_join(threads[i], &status);
265 if (ret != 0) {
266 perror("pthread_join");
267 goto error;
268 }
269 }
270 ret = EXIT_SUCCESS;
271 kconsumerd_send_error(KCONSUMERD_EXIT_SUCCESS);
272 goto end;
273
274 error:
275 ret = EXIT_FAILURE;
276 kconsumerd_send_error(KCONSUMERD_EXIT_FAILURE);
277
278 end:
279 kconsumerd_cleanup();
280
281 return ret;
282 }
This page took 0.057661 seconds and 4 git commands to generate.