Commit | Line | Data |
---|---|---|
d4a1283e JD |
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 | |
82a3637f DG |
7 | * as published by the Free Software Foundation; only version 2 |
8 | * of the License. | |
d4a1283e JD |
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> | |
03424a9b | 38 | #include <sys/mman.h> |
5348b470 | 39 | #include <assert.h> |
d4a1283e | 40 | |
1e307fab DG |
41 | #include <ltt-kconsumerd.h> |
42 | #include <lttng-kernel-ctl.h> | |
43 | #include <lttng-sessiond-comm.h> | |
6533b585 | 44 | #include <lttng/lttng-kconsumerd.h> |
1e307fab | 45 | #include <lttngerr.h> |
d4a1283e JD |
46 | |
47 | /* the two threads (receive fd and poll) */ | |
6533b585 | 48 | static pthread_t threads[2]; |
d4a1283e | 49 | |
13e44745 JD |
50 | /* to count the number of time the user pressed ctrl+c */ |
51 | static int sigintcount = 0; | |
52 | ||
d4a1283e JD |
53 | /* Argument variables */ |
54 | int opt_quiet; | |
55 | int opt_verbose; | |
56 | static int opt_daemon; | |
57 | static const char *progname; | |
6533b585 DG |
58 | static char command_sock_path[PATH_MAX]; /* Global command socket path */ |
59 | static char error_sock_path[PATH_MAX]; /* Global error path */ | |
d4a1283e | 60 | |
6533b585 DG |
61 | /* the liblttngkconsumerd context */ |
62 | static struct lttng_kconsumerd_local_data *ctx; | |
cb040cc1 | 63 | |
d4a1283e | 64 | /* |
6533b585 | 65 | * Signal handler for the daemon |
d4a1283e JD |
66 | */ |
67 | static void sighandler(int sig) | |
68 | { | |
13e44745 JD |
69 | if (sig == SIGINT && sigintcount++ == 0) { |
70 | DBG("ignoring first SIGINT"); | |
71 | return; | |
72 | } | |
73 | ||
6533b585 | 74 | lttng_kconsumerd_should_exit(ctx); |
d4a1283e JD |
75 | } |
76 | ||
77 | /* | |
6533b585 | 78 | * Setup signal handler for : |
d4a1283e JD |
79 | * SIGINT, SIGTERM, SIGPIPE |
80 | */ | |
81 | static int set_signal_handler(void) | |
82 | { | |
83 | int ret = 0; | |
84 | struct sigaction sa; | |
85 | sigset_t sigset; | |
86 | ||
87 | if ((ret = sigemptyset(&sigset)) < 0) { | |
88 | perror("sigemptyset"); | |
89 | return ret; | |
90 | } | |
91 | ||
92 | sa.sa_handler = sighandler; | |
93 | sa.sa_mask = sigset; | |
94 | sa.sa_flags = 0; | |
95 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
96 | perror("sigaction"); | |
97 | return ret; | |
98 | } | |
99 | ||
100 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
101 | perror("sigaction"); | |
102 | return ret; | |
103 | } | |
104 | ||
105 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
106 | perror("sigaction"); | |
107 | return ret; | |
108 | } | |
109 | ||
110 | return ret; | |
111 | } | |
112 | ||
d4a1283e JD |
113 | /* |
114 | * usage function on stderr | |
115 | */ | |
116 | static void usage(void) | |
117 | { | |
118 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); | |
119 | fprintf(stderr, " -h, --help " | |
120 | "Display this usage.\n"); | |
121 | fprintf(stderr, " -c, --kconsumerd-cmd-sock PATH " | |
122 | "Specify path for the command socket\n"); | |
123 | fprintf(stderr, " -e, --kconsumerd-err-sock PATH " | |
124 | "Specify path for the error socket\n"); | |
125 | fprintf(stderr, " -d, --daemonize " | |
126 | "Start as a daemon.\n"); | |
127 | fprintf(stderr, " -q, --quiet " | |
128 | "No output at all.\n"); | |
129 | fprintf(stderr, " -v, --verbose " | |
130 | "Verbose mode. Activate DBG() macro.\n"); | |
131 | fprintf(stderr, " -V, --version " | |
132 | "Show version number.\n"); | |
133 | } | |
134 | ||
135 | /* | |
136 | * daemon argument parsing | |
137 | */ | |
138 | static void parse_args(int argc, char **argv) | |
139 | { | |
140 | int c; | |
141 | ||
142 | static struct option long_options[] = { | |
143 | { "kconsumerd-cmd-sock", 1, 0, 'c' }, | |
144 | { "kconsumerd-err-sock", 1, 0, 'e' }, | |
145 | { "daemonize", 0, 0, 'd' }, | |
146 | { "help", 0, 0, 'h' }, | |
147 | { "quiet", 0, 0, 'q' }, | |
148 | { "verbose", 0, 0, 'v' }, | |
149 | { "version", 0, 0, 'V' }, | |
150 | { NULL, 0, 0, 0 } | |
151 | }; | |
152 | ||
153 | while (1) { | |
154 | int option_index = 0; | |
155 | c = getopt_long(argc, argv, "dhqvV" "c:e:", long_options, &option_index); | |
156 | if (c == -1) { | |
157 | break; | |
158 | } | |
159 | ||
160 | switch (c) { | |
914a571b JD |
161 | case 0: |
162 | fprintf(stderr, "option %s", long_options[option_index].name); | |
163 | if (optarg) { | |
164 | fprintf(stderr, " with arg %s\n", optarg); | |
165 | } | |
166 | break; | |
167 | case 'c': | |
168 | snprintf(command_sock_path, PATH_MAX, "%s", optarg); | |
169 | break; | |
170 | case 'e': | |
171 | snprintf(error_sock_path, PATH_MAX, "%s", optarg); | |
172 | break; | |
173 | case 'd': | |
174 | opt_daemon = 1; | |
175 | break; | |
176 | case 'h': | |
177 | usage(); | |
178 | exit(EXIT_FAILURE); | |
179 | case 'q': | |
180 | opt_quiet = 1; | |
181 | break; | |
182 | case 'v': | |
183 | opt_verbose = 1; | |
184 | break; | |
185 | case 'V': | |
186 | fprintf(stdout, "%s\n", VERSION); | |
187 | exit(EXIT_SUCCESS); | |
188 | default: | |
189 | usage(); | |
190 | exit(EXIT_FAILURE); | |
d4a1283e JD |
191 | } |
192 | } | |
193 | } | |
194 | ||
cb040cc1 | 195 | /* |
6533b585 | 196 | * Consume data on a file descriptor and write it on a trace file. |
cb040cc1 | 197 | */ |
6533b585 | 198 | static int read_subbuffer(struct lttng_kconsumerd_fd *kconsumerd_fd) |
cb040cc1 JD |
199 | { |
200 | unsigned long len; | |
201 | int err; | |
202 | long ret = 0; | |
203 | int infd = kconsumerd_fd->consumerd_fd; | |
204 | ||
205 | DBG("In kconsumerd_read_subbuffer (infd : %d)", infd); | |
206 | /* Get the next subbuffer */ | |
207 | err = kernctl_get_next_subbuf(infd); | |
208 | if (err != 0) { | |
209 | ret = errno; | |
81ffcd20 MD |
210 | /* |
211 | * This is a debug message even for single-threaded consumer, | |
212 | * because poll() have more relaxed criterions than get subbuf, | |
213 | * so get_subbuf may fail for short race windows where poll() | |
214 | * would issue wakeups. | |
215 | */ | |
216 | DBG("Reserving sub buffer failed (everything is normal, " | |
cb040cc1 JD |
217 | "it is due to concurrency)"); |
218 | goto end; | |
219 | } | |
220 | ||
57194bf2 | 221 | switch (kconsumerd_fd->output) { |
cb040cc1 JD |
222 | case LTTNG_EVENT_SPLICE: |
223 | /* read the whole subbuffer */ | |
224 | err = kernctl_get_padded_subbuf_size(infd, &len); | |
225 | if (err != 0) { | |
226 | ret = errno; | |
227 | perror("Getting sub-buffer len failed."); | |
228 | goto end; | |
229 | } | |
230 | ||
231 | /* splice the subbuffer to the tracefile */ | |
6533b585 | 232 | ret = lttng_kconsumerd_on_read_subbuffer_splice(ctx, kconsumerd_fd, len); |
cb040cc1 JD |
233 | if (ret < 0) { |
234 | /* | |
235 | * display the error but continue processing to try | |
236 | * to release the subbuffer | |
237 | */ | |
238 | ERR("Error splicing to tracefile"); | |
239 | } | |
240 | break; | |
241 | case LTTNG_EVENT_MMAP: | |
242 | /* read the used subbuffer size */ | |
8b270bdb | 243 | err = kernctl_get_padded_subbuf_size(infd, &len); |
cb040cc1 JD |
244 | if (err != 0) { |
245 | ret = errno; | |
246 | perror("Getting sub-buffer len failed."); | |
247 | goto end; | |
248 | } | |
249 | /* write the subbuffer to the tracefile */ | |
6533b585 | 250 | ret = lttng_kconsumerd_on_read_subbuffer_mmap(ctx, kconsumerd_fd, len); |
cb040cc1 JD |
251 | if (ret < 0) { |
252 | /* | |
253 | * display the error but continue processing to try | |
254 | * to release the subbuffer | |
255 | */ | |
256 | ERR("Error writing to tracefile"); | |
257 | } | |
258 | break; | |
259 | default: | |
260 | ERR("Unknown output method"); | |
261 | ret = -1; | |
262 | } | |
263 | ||
264 | err = kernctl_put_next_subbuf(infd); | |
265 | if (err != 0) { | |
266 | ret = errno; | |
267 | if (errno == EFAULT) { | |
268 | perror("Error in unreserving sub buffer\n"); | |
269 | } else if (errno == EIO) { | |
270 | /* Should never happen with newer LTTng versions */ | |
271 | perror("Reader has been pushed by the writer, last sub-buffer corrupted."); | |
272 | } | |
273 | goto end; | |
274 | } | |
275 | ||
276 | end: | |
277 | return ret; | |
278 | } | |
d4a1283e | 279 | |
5348b470 JD |
280 | static int on_recv_fd(struct lttng_kconsumerd_fd *kconsumerd_fd) |
281 | { | |
282 | int ret; | |
283 | ||
284 | /* Opening the tracefile in write mode */ | |
285 | if (kconsumerd_fd->path_name != NULL) { | |
286 | ret = open(kconsumerd_fd->path_name, | |
287 | O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO); | |
288 | if (ret < 0) { | |
289 | ERR("Opening %s", kconsumerd_fd->path_name); | |
290 | perror("open"); | |
291 | goto error; | |
292 | } | |
293 | kconsumerd_fd->out_fd = ret; | |
294 | } | |
295 | ||
296 | if (kconsumerd_fd->output == LTTNG_EVENT_MMAP) { | |
297 | /* get the len of the mmap region */ | |
298 | ret = kernctl_get_mmap_len(kconsumerd_fd->consumerd_fd, &kconsumerd_fd->mmap_len); | |
299 | if (ret != 0) { | |
300 | ret = errno; | |
301 | perror("kernctl_get_mmap_len"); | |
302 | goto error_close_fd; | |
303 | } | |
304 | ||
305 | kconsumerd_fd->mmap_base = mmap(NULL, kconsumerd_fd->mmap_len, | |
306 | PROT_READ, MAP_PRIVATE, kconsumerd_fd->consumerd_fd, 0); | |
307 | if (kconsumerd_fd->mmap_base == MAP_FAILED) { | |
308 | perror("Error mmaping"); | |
309 | ret = -1; | |
310 | goto error_close_fd; | |
311 | } | |
312 | } | |
313 | ||
314 | /* we return 0 to let the library handle the FD internally */ | |
315 | return 0; | |
316 | ||
317 | error_close_fd: | |
318 | { | |
319 | int err; | |
320 | ||
321 | err = close(kconsumerd_fd->out_fd); | |
322 | assert(!err); | |
323 | } | |
324 | error: | |
325 | return ret; | |
326 | } | |
327 | ||
d4a1283e JD |
328 | /* |
329 | * main | |
330 | */ | |
331 | int main(int argc, char **argv) | |
332 | { | |
333 | int i; | |
334 | int ret = 0; | |
335 | void *status; | |
336 | ||
337 | /* Parse arguments */ | |
338 | progname = argv[0]; | |
339 | parse_args(argc, argv); | |
340 | ||
341 | /* Daemonize */ | |
342 | if (opt_daemon) { | |
343 | ret = daemon(0, 0); | |
344 | if (ret < 0) { | |
345 | perror("daemon"); | |
346 | goto error; | |
347 | } | |
348 | } | |
349 | ||
350 | if (strlen(command_sock_path) == 0) { | |
351 | snprintf(command_sock_path, PATH_MAX, | |
352 | KCONSUMERD_CMD_SOCK_PATH); | |
353 | } | |
5348b470 JD |
354 | /* create the consumer instance with and assign the callbacks */ |
355 | ctx = lttng_kconsumerd_create(read_subbuffer, on_recv_fd, NULL); | |
cb040cc1 JD |
356 | if (ctx == NULL) { |
357 | goto error; | |
358 | } | |
359 | ||
6533b585 | 360 | lttng_kconsumerd_set_command_sock_path(ctx, command_sock_path); |
d4a1283e JD |
361 | if (strlen(error_sock_path) == 0) { |
362 | snprintf(error_sock_path, PATH_MAX, | |
363 | KCONSUMERD_ERR_SOCK_PATH); | |
364 | } | |
365 | ||
366 | if (set_signal_handler() < 0) { | |
367 | goto error; | |
368 | } | |
369 | ||
370 | /* Connect to the socket created by ltt-sessiond to report errors */ | |
371 | DBG("Connecting to error socket %s", error_sock_path); | |
1ce86c9a | 372 | ret = lttcomm_connect_unix_sock(error_sock_path); |
d4a1283e | 373 | /* not a fatal error, but all communication with ltt-sessiond will fail */ |
1ce86c9a | 374 | if (ret < 0) { |
d4a1283e JD |
375 | WARN("Cannot connect to error socket, is ltt-sessiond started ?"); |
376 | } | |
6533b585 | 377 | lttng_kconsumerd_set_error_sock(ctx, ret); |
d4a1283e JD |
378 | |
379 | /* Create the thread to manage the receive of fd */ | |
6533b585 | 380 | ret = pthread_create(&threads[0], NULL, lttng_kconsumerd_thread_receive_fds, |
cb040cc1 | 381 | (void *) ctx); |
d4a1283e JD |
382 | if (ret != 0) { |
383 | perror("pthread_create"); | |
384 | goto error; | |
385 | } | |
386 | ||
387 | /* Create thread to manage the polling/writing of traces */ | |
6533b585 | 388 | ret = pthread_create(&threads[1], NULL, lttng_kconsumerd_thread_poll_fds, |
cb040cc1 | 389 | (void *) ctx); |
d4a1283e JD |
390 | if (ret != 0) { |
391 | perror("pthread_create"); | |
392 | goto error; | |
393 | } | |
394 | ||
395 | for (i = 0; i < 2; i++) { | |
396 | ret = pthread_join(threads[i], &status); | |
397 | if (ret != 0) { | |
398 | perror("pthread_join"); | |
399 | goto error; | |
400 | } | |
401 | } | |
402 | ret = EXIT_SUCCESS; | |
6533b585 | 403 | lttng_kconsumerd_send_error(ctx, KCONSUMERD_EXIT_SUCCESS); |
d4a1283e JD |
404 | goto end; |
405 | ||
406 | error: | |
407 | ret = EXIT_FAILURE; | |
6533b585 | 408 | lttng_kconsumerd_send_error(ctx, KCONSUMERD_EXIT_FAILURE); |
d4a1283e JD |
409 | |
410 | end: | |
6533b585 DG |
411 | lttng_kconsumerd_destroy(ctx); |
412 | lttng_kconsumerd_cleanup(); | |
d4a1283e JD |
413 | |
414 | return ret; | |
415 | } |