Fix: Delete stream on write error in consumer
[lttng-tools.git] / src / bin / lttng-consumerd / lttng-consumerd.c
CommitLineData
d4a1283e
JD
1/*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
d4a1283e
JD
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
d4a1283e
JD
17 */
18
19#define _GNU_SOURCE
20#include <fcntl.h>
21#include <getopt.h>
22#include <grp.h>
23#include <limits.h>
24#include <pthread.h>
25#include <signal.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <sys/ipc.h>
1ccfc0e3 30#include <sys/resource.h>
d4a1283e
JD
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>
3bd1e081 40#include <config.h>
7753dea8 41#include <urcu/compiler.h>
1ccfc0e3 42#include <ulimit.h>
d4a1283e 43
db758600
DG
44#include <common/defaults.h>
45#include <common/common.h>
f02e1e8a 46#include <common/consumer.h>
fb3a43a9 47#include <common/compat/poll.h>
10a8a223 48#include <common/sessiond-comm/sessiond-comm.h>
10a8a223
DG
49
50#include "lttng-consumerd.h"
d4a1283e 51
99bab54f 52/* TODO : support UST (all direct kernel-ctl accesses). */
3bd1e081 53
fb3a43a9 54/* the two threads (receive fd, poll and metadata) */
df27ef7b 55static pthread_t data_thread, metadata_thread, sessiond_thread;
d4a1283e 56
3183dbb0 57/* to count the number of times the user pressed ctrl+c */
13e44745
JD
58static int sigintcount = 0;
59
d4a1283e 60/* Argument variables */
97e19046
DG
61int lttng_opt_quiet; /* not static in error.h */
62int lttng_opt_verbose; /* not static in error.h */
d4a1283e
JD
63static int opt_daemon;
64static const char *progname;
6533b585
DG
65static char command_sock_path[PATH_MAX]; /* Global command socket path */
66static char error_sock_path[PATH_MAX]; /* Global error path */
3bd1e081 67static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL;
d4a1283e 68
7753dea8 69/* the liblttngconsumerd context */
3bd1e081 70static struct lttng_consumer_local_data *ctx;
cb040cc1 71
d4a1283e 72/*
6533b585 73 * Signal handler for the daemon
d4a1283e
JD
74 */
75static void sighandler(int sig)
76{
13e44745
JD
77 if (sig == SIGINT && sigintcount++ == 0) {
78 DBG("ignoring first SIGINT");
79 return;
80 }
81
ab1027f4
DG
82 /*
83 * Ignore SIGPIPE because it should not stop the consumer whenever a
84 * SIGPIPE is catched through a FD operation.
85 */
86 if (sig == SIGPIPE) {
87 return;
88 }
89
3bd1e081 90 lttng_consumer_should_exit(ctx);
d4a1283e
JD
91}
92
93/*
6533b585 94 * Setup signal handler for :
d4a1283e
JD
95 * SIGINT, SIGTERM, SIGPIPE
96 */
97static int set_signal_handler(void)
98{
99 int ret = 0;
100 struct sigaction sa;
101 sigset_t sigset;
102
103 if ((ret = sigemptyset(&sigset)) < 0) {
104 perror("sigemptyset");
105 return ret;
106 }
107
108 sa.sa_handler = sighandler;
109 sa.sa_mask = sigset;
110 sa.sa_flags = 0;
111 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
112 perror("sigaction");
113 return ret;
114 }
115
116 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
117 perror("sigaction");
118 return ret;
119 }
120
121 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
122 perror("sigaction");
123 return ret;
124 }
125
126 return ret;
127}
128
d4a1283e 129/*
3183dbb0 130 * Usage function on stream file.
d4a1283e 131 */
3183dbb0 132static void usage(FILE *fp)
d4a1283e 133{
3183dbb0
DG
134 fprintf(fp, "Usage: %s OPTIONS\n\nOptions:\n", progname);
135 fprintf(fp, " -h, --help "
d4a1283e 136 "Display this usage.\n");
3183dbb0 137 fprintf(fp, " -c, --consumerd-cmd-sock PATH "
d4a1283e 138 "Specify path for the command socket\n");
3183dbb0 139 fprintf(fp, " -e, --consumerd-err-sock PATH "
d4a1283e 140 "Specify path for the error socket\n");
3183dbb0 141 fprintf(fp, " -d, --daemonize "
d4a1283e 142 "Start as a daemon.\n");
3183dbb0 143 fprintf(fp, " -q, --quiet "
d4a1283e 144 "No output at all.\n");
3183dbb0 145 fprintf(fp, " -v, --verbose "
d4a1283e 146 "Verbose mode. Activate DBG() macro.\n");
3183dbb0 147 fprintf(fp, " -V, --version "
d4a1283e 148 "Show version number.\n");
3183dbb0 149 fprintf(fp, " -k, --kernel "
3bd1e081 150 "Consumer kernel buffers (default).\n");
3183dbb0 151 fprintf(fp, " -u, --ust "
3bd1e081 152 "Consumer UST buffers.%s\n",
74d0b642 153#ifdef HAVE_LIBLTTNG_UST_CTL
3bd1e081
MD
154 ""
155#else
156 " (support not compiled in)"
157#endif
158 );
d4a1283e
JD
159}
160
161/*
162 * daemon argument parsing
163 */
164static void parse_args(int argc, char **argv)
165{
166 int c;
167
168 static struct option long_options[] = {
7753dea8
MD
169 { "consumerd-cmd-sock", 1, 0, 'c' },
170 { "consumerd-err-sock", 1, 0, 'e' },
d4a1283e
JD
171 { "daemonize", 0, 0, 'd' },
172 { "help", 0, 0, 'h' },
173 { "quiet", 0, 0, 'q' },
174 { "verbose", 0, 0, 'v' },
175 { "version", 0, 0, 'V' },
3bd1e081 176 { "kernel", 0, 0, 'k' },
74d0b642 177#ifdef HAVE_LIBLTTNG_UST_CTL
3bd1e081
MD
178 { "ust", 0, 0, 'u' },
179#endif
d4a1283e
JD
180 { NULL, 0, 0, 0 }
181 };
182
183 while (1) {
184 int option_index = 0;
3bd1e081 185 c = getopt_long(argc, argv, "dhqvVku" "c:e:", long_options, &option_index);
d4a1283e
JD
186 if (c == -1) {
187 break;
188 }
189
190 switch (c) {
914a571b
JD
191 case 0:
192 fprintf(stderr, "option %s", long_options[option_index].name);
193 if (optarg) {
194 fprintf(stderr, " with arg %s\n", optarg);
195 }
196 break;
197 case 'c':
198 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
199 break;
200 case 'e':
201 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
202 break;
203 case 'd':
204 opt_daemon = 1;
205 break;
206 case 'h':
3183dbb0
DG
207 usage(stdout);
208 exit(EXIT_SUCCESS);
914a571b 209 case 'q':
97e19046 210 lttng_opt_quiet = 1;
914a571b
JD
211 break;
212 case 'v':
97e19046 213 lttng_opt_verbose = 1;
914a571b
JD
214 break;
215 case 'V':
216 fprintf(stdout, "%s\n", VERSION);
217 exit(EXIT_SUCCESS);
3bd1e081
MD
218 case 'k':
219 opt_type = LTTNG_CONSUMER_KERNEL;
220 break;
74d0b642 221#ifdef HAVE_LIBLTTNG_UST_CTL
3bd1e081 222 case 'u':
7753dea8
MD
223# if (CAA_BITS_PER_LONG == 64)
224 opt_type = LTTNG_CONSUMER64_UST;
225# elif (CAA_BITS_PER_LONG == 32)
226 opt_type = LTTNG_CONSUMER32_UST;
227# else
228# error "Unknown bitness"
229# endif
3bd1e081
MD
230 break;
231#endif
914a571b 232 default:
3183dbb0 233 usage(stderr);
914a571b 234 exit(EXIT_FAILURE);
d4a1283e
JD
235 }
236 }
237}
238
1ccfc0e3
DG
239/*
240 * Set open files limit to unlimited. This daemon can open a large number of
241 * file descriptors in order to consumer multiple kernel traces.
242 */
243static void set_ulimit(void)
244{
245 int ret;
246 struct rlimit lim;
247
248 /* The kernel does not allowed an infinite limit for open files */
249 lim.rlim_cur = 65535;
250 lim.rlim_max = 65535;
251
252 ret = setrlimit(RLIMIT_NOFILE, &lim);
253 if (ret < 0) {
254 PERROR("failed to set open files limit");
255 }
256}
257
d4a1283e
JD
258/*
259 * main
260 */
261int main(int argc, char **argv)
262{
d4a1283e
JD
263 int ret = 0;
264 void *status;
265
266 /* Parse arguments */
267 progname = argv[0];
268 parse_args(argc, argv);
269
270 /* Daemonize */
271 if (opt_daemon) {
ceed52b5
MD
272 int i;
273
274 /*
275 * fork
276 * child: setsid, close FD 0, 1, 2, chdir /
277 * parent: exit (if fork is successful)
278 */
d4a1283e
JD
279 ret = daemon(0, 0);
280 if (ret < 0) {
ceed52b5 281 PERROR("daemon");
d4a1283e
JD
282 goto error;
283 }
ceed52b5
MD
284 /*
285 * We are in the child. Make sure all other file
286 * descriptors are closed, in case we are called with
287 * more opened file descriptors than the standard ones.
288 */
289 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
290 (void) close(i);
291 }
d4a1283e
JD
292 }
293
fb3a43a9
DG
294 /* Set up max poll set size */
295 lttng_poll_set_max_size();
296
d4a1283e 297 if (strlen(command_sock_path) == 0) {
7753dea8
MD
298 switch (opt_type) {
299 case LTTNG_CONSUMER_KERNEL:
60922cb0 300 snprintf(command_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
990570ed 301 DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
302 break;
303 case LTTNG_CONSUMER64_UST:
67e40797 304 snprintf(command_sock_path, PATH_MAX,
60922cb0 305 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
306 break;
307 case LTTNG_CONSUMER32_UST:
67e40797 308 snprintf(command_sock_path, PATH_MAX,
60922cb0 309 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
310 break;
311 default:
312 WARN("Unknown consumerd type");
313 goto error;
314 }
d4a1283e 315 }
e4421fec
DG
316
317 /* Init */
318 lttng_consumer_init();
319
1ccfc0e3
DG
320 if (!getuid()) {
321 /* Set limit for open files */
322 set_ulimit();
323 }
324
5348b470 325 /* create the consumer instance with and assign the callbacks */
d41f73b7
MD
326 ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer,
327 NULL, lttng_consumer_on_recv_stream, NULL);
cb040cc1
JD
328 if (ctx == NULL) {
329 goto error;
330 }
331
3bd1e081 332 lttng_consumer_set_command_sock_path(ctx, command_sock_path);
d4a1283e 333 if (strlen(error_sock_path) == 0) {
7753dea8
MD
334 switch (opt_type) {
335 case LTTNG_CONSUMER_KERNEL:
60922cb0 336 snprintf(error_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
990570ed 337 DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
338 break;
339 case LTTNG_CONSUMER64_UST:
67e40797 340 snprintf(error_sock_path, PATH_MAX,
60922cb0 341 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
342 break;
343 case LTTNG_CONSUMER32_UST:
67e40797 344 snprintf(error_sock_path, PATH_MAX,
60922cb0 345 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
346 break;
347 default:
348 WARN("Unknown consumerd type");
349 goto error;
350 }
d4a1283e
JD
351 }
352
353 if (set_signal_handler() < 0) {
354 goto error;
355 }
356
32258573 357 /* Connect to the socket created by lttng-sessiond to report errors */
d4a1283e 358 DBG("Connecting to error socket %s", error_sock_path);
1ce86c9a 359 ret = lttcomm_connect_unix_sock(error_sock_path);
32258573 360 /* not a fatal error, but all communication with lttng-sessiond will fail */
1ce86c9a 361 if (ret < 0) {
99bab54f 362 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
d4a1283e 363 }
3bd1e081 364 lttng_consumer_set_error_sock(ctx, ret);
d4a1283e 365
7d980def 366 /* Create thread to manage the polling/writing of trace metadata */
df27ef7b 367 ret = pthread_create(&metadata_thread, NULL, consumer_thread_metadata_poll,
7d980def
DG
368 (void *) ctx);
369 if (ret != 0) {
370 perror("pthread_create");
371 goto error;
372 }
373
374 /* Create thread to manage the polling/writing of trace data */
df27ef7b 375 ret = pthread_create(&data_thread, NULL, consumer_thread_data_poll,
cb040cc1 376 (void *) ctx);
d4a1283e
JD
377 if (ret != 0) {
378 perror("pthread_create");
df27ef7b 379 goto data_error;
d4a1283e
JD
380 }
381
7d980def 382 /* Create the thread to manage the receive of fd */
df27ef7b 383 ret = pthread_create(&sessiond_thread, NULL, consumer_thread_sessiond_poll,
cb040cc1 384 (void *) ctx);
d4a1283e
JD
385 if (ret != 0) {
386 perror("pthread_create");
df27ef7b
DG
387 goto sessiond_error;
388 }
389
390 ret = pthread_join(sessiond_thread, &status);
391 if (ret != 0) {
392 perror("pthread_join");
d4a1283e
JD
393 goto error;
394 }
395
df27ef7b
DG
396sessiond_error:
397 ret = pthread_join(data_thread, &status);
398 if (ret != 0) {
399 perror("pthread_join");
400 goto error;
401 }
402
403data_error:
404 ret = pthread_join(metadata_thread, &status);
405 if (ret != 0) {
406 perror("pthread_join");
407 goto error;
408 }
409
410 if (!ret) {
411 ret = EXIT_SUCCESS;
412 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_EXIT_SUCCESS);
413 goto end;
d4a1283e 414 }
d4a1283e
JD
415
416error:
417 ret = EXIT_FAILURE;
f73fabfd 418 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_EXIT_FAILURE);
d4a1283e
JD
419
420end:
3bd1e081
MD
421 lttng_consumer_destroy(ctx);
422 lttng_consumer_cleanup();
d4a1283e
JD
423
424 return ret;
425}
This page took 0.049765 seconds and 4 git commands to generate.