ltt-kconsumerd: Fix incorrect typing
[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>
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 48static pthread_t threads[2];
d4a1283e 49
13e44745
JD
50/* to count the number of time the user pressed ctrl+c */
51static int sigintcount = 0;
52
d4a1283e
JD
53/* Argument variables */
54int opt_quiet;
55int opt_verbose;
56static int opt_daemon;
57static const char *progname;
6533b585
DG
58static char command_sock_path[PATH_MAX]; /* Global command socket path */
59static char error_sock_path[PATH_MAX]; /* Global error path */
d4a1283e 60
6533b585
DG
61/* the liblttngkconsumerd context */
62static struct lttng_kconsumerd_local_data *ctx;
cb040cc1 63
d4a1283e 64/*
6533b585 65 * Signal handler for the daemon
d4a1283e
JD
66 */
67static 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 */
81static 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 */
116static 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 */
138static 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 198static 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
276end:
277 return ret;
278}
d4a1283e 279
5348b470
JD
280static 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 */
558e70e9
MD
298 unsigned long mmap_len;
299
300 ret = kernctl_get_mmap_len(kconsumerd_fd->consumerd_fd, &mmap_len);
5348b470
JD
301 if (ret != 0) {
302 ret = errno;
303 perror("kernctl_get_mmap_len");
304 goto error_close_fd;
305 }
558e70e9 306 kconsumerd_fd->mmap_len = (size_t) mmap_len;
5348b470
JD
307
308 kconsumerd_fd->mmap_base = mmap(NULL, kconsumerd_fd->mmap_len,
309 PROT_READ, MAP_PRIVATE, kconsumerd_fd->consumerd_fd, 0);
310 if (kconsumerd_fd->mmap_base == MAP_FAILED) {
311 perror("Error mmaping");
312 ret = -1;
313 goto error_close_fd;
314 }
315 }
316
317 /* we return 0 to let the library handle the FD internally */
318 return 0;
319
320error_close_fd:
321 {
322 int err;
323
324 err = close(kconsumerd_fd->out_fd);
325 assert(!err);
326 }
327error:
328 return ret;
329}
330
d4a1283e
JD
331/*
332 * main
333 */
334int main(int argc, char **argv)
335{
336 int i;
337 int ret = 0;
338 void *status;
339
340 /* Parse arguments */
341 progname = argv[0];
342 parse_args(argc, argv);
343
344 /* Daemonize */
345 if (opt_daemon) {
346 ret = daemon(0, 0);
347 if (ret < 0) {
348 perror("daemon");
349 goto error;
350 }
351 }
352
353 if (strlen(command_sock_path) == 0) {
354 snprintf(command_sock_path, PATH_MAX,
355 KCONSUMERD_CMD_SOCK_PATH);
356 }
5348b470
JD
357 /* create the consumer instance with and assign the callbacks */
358 ctx = lttng_kconsumerd_create(read_subbuffer, on_recv_fd, NULL);
cb040cc1
JD
359 if (ctx == NULL) {
360 goto error;
361 }
362
6533b585 363 lttng_kconsumerd_set_command_sock_path(ctx, command_sock_path);
d4a1283e
JD
364 if (strlen(error_sock_path) == 0) {
365 snprintf(error_sock_path, PATH_MAX,
366 KCONSUMERD_ERR_SOCK_PATH);
367 }
368
369 if (set_signal_handler() < 0) {
370 goto error;
371 }
372
373 /* Connect to the socket created by ltt-sessiond to report errors */
374 DBG("Connecting to error socket %s", error_sock_path);
1ce86c9a 375 ret = lttcomm_connect_unix_sock(error_sock_path);
d4a1283e 376 /* not a fatal error, but all communication with ltt-sessiond will fail */
1ce86c9a 377 if (ret < 0) {
d4a1283e
JD
378 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
379 }
6533b585 380 lttng_kconsumerd_set_error_sock(ctx, ret);
d4a1283e
JD
381
382 /* Create the thread to manage the receive of fd */
6533b585 383 ret = pthread_create(&threads[0], NULL, lttng_kconsumerd_thread_receive_fds,
cb040cc1 384 (void *) ctx);
d4a1283e
JD
385 if (ret != 0) {
386 perror("pthread_create");
387 goto error;
388 }
389
390 /* Create thread to manage the polling/writing of traces */
6533b585 391 ret = pthread_create(&threads[1], NULL, lttng_kconsumerd_thread_poll_fds,
cb040cc1 392 (void *) ctx);
d4a1283e
JD
393 if (ret != 0) {
394 perror("pthread_create");
395 goto error;
396 }
397
398 for (i = 0; i < 2; i++) {
399 ret = pthread_join(threads[i], &status);
400 if (ret != 0) {
401 perror("pthread_join");
402 goto error;
403 }
404 }
405 ret = EXIT_SUCCESS;
6533b585 406 lttng_kconsumerd_send_error(ctx, KCONSUMERD_EXIT_SUCCESS);
d4a1283e
JD
407 goto end;
408
409error:
410 ret = EXIT_FAILURE;
6533b585 411 lttng_kconsumerd_send_error(ctx, KCONSUMERD_EXIT_FAILURE);
d4a1283e
JD
412
413end:
6533b585
DG
414 lttng_kconsumerd_destroy(ctx);
415 lttng_kconsumerd_cleanup();
d4a1283e
JD
416
417 return ret;
418}
This page took 0.064328 seconds and 4 git commands to generate.