Cleanup mmap consuming
[lttng-tools.git] / ltt-kconsumerd / ltt-kconsumerd.c
CommitLineData
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>
d4a1283e
JD
39
40#include "lttngerr.h"
50ecdf72
MD
41#include "kernelctl.h"
42#include "lttkconsumerd.h"
d4a1283e
JD
43
44/* the two threads (receive fd and poll) */
1ce86c9a 45pthread_t threads[2];
d4a1283e 46
13e44745
JD
47/* to count the number of time the user pressed ctrl+c */
48static int sigintcount = 0;
49
d4a1283e
JD
50/* Argument variables */
51int opt_quiet;
52int opt_verbose;
53static int opt_daemon;
54static const char *progname;
1ce86c9a
JD
55char command_sock_path[PATH_MAX]; /* Global command socket path */
56char error_sock_path[PATH_MAX]; /* Global error path */
d4a1283e 57
cb040cc1
JD
58/* the liblttkconsumerd context */
59struct kconsumerd_local_data *ctx;
60
d4a1283e
JD
61/*
62 * sighandler
63 *
64 * Signal handler for the daemon
65 */
66static void sighandler(int sig)
67{
13e44745
JD
68 if (sig == SIGINT && sigintcount++ == 0) {
69 DBG("ignoring first SIGINT");
70 return;
71 }
72
cb040cc1 73 kconsumerd_should_exit(ctx);
d4a1283e
JD
74}
75
76/*
77 * set_signal_handler
78 *
79 * Setup signal handler for :
80 * SIGINT, SIGTERM, SIGPIPE
81 */
82static int set_signal_handler(void)
83{
84 int ret = 0;
85 struct sigaction sa;
86 sigset_t sigset;
87
88 if ((ret = sigemptyset(&sigset)) < 0) {
89 perror("sigemptyset");
90 return ret;
91 }
92
93 sa.sa_handler = sighandler;
94 sa.sa_mask = sigset;
95 sa.sa_flags = 0;
96 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
97 perror("sigaction");
98 return ret;
99 }
100
101 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
102 perror("sigaction");
103 return ret;
104 }
105
106 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
107 perror("sigaction");
108 return ret;
109 }
110
111 return ret;
112}
113
d4a1283e
JD
114/*
115 * usage function on stderr
116 */
117static void usage(void)
118{
119 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
120 fprintf(stderr, " -h, --help "
121 "Display this usage.\n");
122 fprintf(stderr, " -c, --kconsumerd-cmd-sock PATH "
123 "Specify path for the command socket\n");
124 fprintf(stderr, " -e, --kconsumerd-err-sock PATH "
125 "Specify path for the error socket\n");
126 fprintf(stderr, " -d, --daemonize "
127 "Start as a daemon.\n");
128 fprintf(stderr, " -q, --quiet "
129 "No output at all.\n");
130 fprintf(stderr, " -v, --verbose "
131 "Verbose mode. Activate DBG() macro.\n");
132 fprintf(stderr, " -V, --version "
133 "Show version number.\n");
134}
135
136/*
137 * daemon argument parsing
138 */
139static void parse_args(int argc, char **argv)
140{
141 int c;
142
143 static struct option long_options[] = {
144 { "kconsumerd-cmd-sock", 1, 0, 'c' },
145 { "kconsumerd-err-sock", 1, 0, 'e' },
146 { "daemonize", 0, 0, 'd' },
147 { "help", 0, 0, 'h' },
148 { "quiet", 0, 0, 'q' },
149 { "verbose", 0, 0, 'v' },
150 { "version", 0, 0, 'V' },
151 { NULL, 0, 0, 0 }
152 };
153
154 while (1) {
155 int option_index = 0;
156 c = getopt_long(argc, argv, "dhqvV" "c:e:", long_options, &option_index);
157 if (c == -1) {
158 break;
159 }
160
161 switch (c) {
914a571b
JD
162 case 0:
163 fprintf(stderr, "option %s", long_options[option_index].name);
164 if (optarg) {
165 fprintf(stderr, " with arg %s\n", optarg);
166 }
167 break;
168 case 'c':
169 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
170 break;
171 case 'e':
172 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
173 break;
174 case 'd':
175 opt_daemon = 1;
176 break;
177 case 'h':
178 usage();
179 exit(EXIT_FAILURE);
180 case 'q':
181 opt_quiet = 1;
182 break;
183 case 'v':
184 opt_verbose = 1;
185 break;
186 case 'V':
187 fprintf(stdout, "%s\n", VERSION);
188 exit(EXIT_SUCCESS);
189 default:
190 usage();
191 exit(EXIT_FAILURE);
d4a1283e
JD
192 }
193 }
194}
195
cb040cc1
JD
196/*
197 * read_subbuffer
198 *
199 * Consume data on a file descriptor and write it on a trace file
200 */
201static int read_subbuffer(struct kconsumerd_fd *kconsumerd_fd)
202{
203 unsigned long len;
204 int err;
205 long ret = 0;
206 int infd = kconsumerd_fd->consumerd_fd;
207
208 DBG("In kconsumerd_read_subbuffer (infd : %d)", infd);
209 /* Get the next subbuffer */
210 err = kernctl_get_next_subbuf(infd);
211 if (err != 0) {
212 ret = errno;
213 perror("Reserving sub buffer failed (everything is normal, "
214 "it is due to concurrency)");
215 goto end;
216 }
217
218 switch (DEFAULT_KERNEL_CHANNEL_OUTPUT) {
219 case LTTNG_EVENT_SPLICE:
220 /* read the whole subbuffer */
221 err = kernctl_get_padded_subbuf_size(infd, &len);
222 if (err != 0) {
223 ret = errno;
224 perror("Getting sub-buffer len failed.");
225 goto end;
226 }
227
228 /* splice the subbuffer to the tracefile */
229 ret = kconsumerd_on_read_subbuffer_splice(ctx, kconsumerd_fd, len);
230 if (ret < 0) {
231 /*
232 * display the error but continue processing to try
233 * to release the subbuffer
234 */
235 ERR("Error splicing to tracefile");
236 }
237 break;
238 case LTTNG_EVENT_MMAP:
239 /* read the used subbuffer size */
8b270bdb 240 err = kernctl_get_padded_subbuf_size(infd, &len);
cb040cc1
JD
241 if (err != 0) {
242 ret = errno;
243 perror("Getting sub-buffer len failed.");
244 goto end;
245 }
246 /* write the subbuffer to the tracefile */
247 ret = kconsumerd_on_read_subbuffer_mmap(ctx, kconsumerd_fd, len);
248 if (ret < 0) {
249 /*
250 * display the error but continue processing to try
251 * to release the subbuffer
252 */
253 ERR("Error writing to tracefile");
254 }
255 break;
256 default:
257 ERR("Unknown output method");
258 ret = -1;
259 }
260
261 err = kernctl_put_next_subbuf(infd);
262 if (err != 0) {
263 ret = errno;
264 if (errno == EFAULT) {
265 perror("Error in unreserving sub buffer\n");
266 } else if (errno == EIO) {
267 /* Should never happen with newer LTTng versions */
268 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
269 }
270 goto end;
271 }
272
273end:
274 return ret;
275}
d4a1283e
JD
276
277/*
278 * main
279 */
280int main(int argc, char **argv)
281{
282 int i;
283 int ret = 0;
284 void *status;
285
286 /* Parse arguments */
287 progname = argv[0];
288 parse_args(argc, argv);
289
290 /* Daemonize */
291 if (opt_daemon) {
292 ret = daemon(0, 0);
293 if (ret < 0) {
294 perror("daemon");
295 goto error;
296 }
297 }
298
299 if (strlen(command_sock_path) == 0) {
300 snprintf(command_sock_path, PATH_MAX,
301 KCONSUMERD_CMD_SOCK_PATH);
302 }
cb040cc1
JD
303 /* create the pipe to wake to receiving thread when needed */
304 ctx = kconsumerd_create(read_subbuffer);
305 if (ctx == NULL) {
306 goto error;
307 }
308
309 kconsumerd_set_command_socket_path(ctx, command_sock_path);
d4a1283e
JD
310 if (strlen(error_sock_path) == 0) {
311 snprintf(error_sock_path, PATH_MAX,
312 KCONSUMERD_ERR_SOCK_PATH);
313 }
314
315 if (set_signal_handler() < 0) {
316 goto error;
317 }
318
319 /* Connect to the socket created by ltt-sessiond to report errors */
320 DBG("Connecting to error socket %s", error_sock_path);
1ce86c9a 321 ret = lttcomm_connect_unix_sock(error_sock_path);
d4a1283e 322 /* not a fatal error, but all communication with ltt-sessiond will fail */
1ce86c9a 323 if (ret < 0) {
d4a1283e
JD
324 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
325 }
cb040cc1 326 kconsumerd_set_error_socket(ctx, ret);
d4a1283e
JD
327
328 /* Create the thread to manage the receive of fd */
1ce86c9a 329 ret = pthread_create(&threads[0], NULL, kconsumerd_thread_receive_fds,
cb040cc1 330 (void *) ctx);
d4a1283e
JD
331 if (ret != 0) {
332 perror("pthread_create");
333 goto error;
334 }
335
336 /* Create thread to manage the polling/writing of traces */
1ce86c9a 337 ret = pthread_create(&threads[1], NULL, kconsumerd_thread_poll_fds,
cb040cc1 338 (void *) ctx);
d4a1283e
JD
339 if (ret != 0) {
340 perror("pthread_create");
341 goto error;
342 }
343
344 for (i = 0; i < 2; i++) {
345 ret = pthread_join(threads[i], &status);
346 if (ret != 0) {
347 perror("pthread_join");
348 goto error;
349 }
350 }
351 ret = EXIT_SUCCESS;
cb040cc1 352 kconsumerd_send_error(ctx, KCONSUMERD_EXIT_SUCCESS);
d4a1283e
JD
353 goto end;
354
355error:
356 ret = EXIT_FAILURE;
cb040cc1 357 kconsumerd_send_error(ctx, KCONSUMERD_EXIT_FAILURE);
d4a1283e
JD
358
359end:
cb040cc1 360 kconsumerd_destroy(ctx);
1ce86c9a 361 kconsumerd_cleanup();
d4a1283e
JD
362
363 return ret;
364}
This page took 0.045303 seconds and 5 git commands to generate.