Update lttng-ust header copy for disabled UST compilation
[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>
46#include <common/kernel-consumer/kernel-consumer.h>
10a8a223
DG
47#include <common/kernel-ctl/kernel-ctl.h>
48#include <common/sessiond-comm/sessiond-comm.h>
10a8a223
DG
49#include <common/ust-consumer/ust-consumer.h>
50
51#include "lttng-consumerd.h"
d4a1283e 52
99bab54f 53/* TODO : support UST (all direct kernel-ctl accesses). */
3bd1e081 54
d4a1283e 55/* the two threads (receive fd and poll) */
6533b585 56static pthread_t threads[2];
d4a1283e 57
3183dbb0 58/* to count the number of times the user pressed ctrl+c */
13e44745
JD
59static int sigintcount = 0;
60
d4a1283e 61/* Argument variables */
97e19046
DG
62int lttng_opt_quiet; /* not static in error.h */
63int lttng_opt_verbose; /* not static in error.h */
d4a1283e
JD
64static int opt_daemon;
65static const char *progname;
6533b585
DG
66static char command_sock_path[PATH_MAX]; /* Global command socket path */
67static char error_sock_path[PATH_MAX]; /* Global error path */
3bd1e081 68static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL;
d4a1283e 69
7753dea8 70/* the liblttngconsumerd context */
3bd1e081 71static struct lttng_consumer_local_data *ctx;
cb040cc1 72
d4a1283e 73/*
6533b585 74 * Signal handler for the daemon
d4a1283e
JD
75 */
76static void sighandler(int sig)
77{
13e44745
JD
78 if (sig == SIGINT && sigintcount++ == 0) {
79 DBG("ignoring first SIGINT");
80 return;
81 }
82
3bd1e081 83 lttng_consumer_should_exit(ctx);
d4a1283e
JD
84}
85
86/*
6533b585 87 * Setup signal handler for :
d4a1283e
JD
88 * SIGINT, SIGTERM, SIGPIPE
89 */
90static int set_signal_handler(void)
91{
92 int ret = 0;
93 struct sigaction sa;
94 sigset_t sigset;
95
96 if ((ret = sigemptyset(&sigset)) < 0) {
97 perror("sigemptyset");
98 return ret;
99 }
100
101 sa.sa_handler = sighandler;
102 sa.sa_mask = sigset;
103 sa.sa_flags = 0;
104 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
105 perror("sigaction");
106 return ret;
107 }
108
109 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
110 perror("sigaction");
111 return ret;
112 }
113
114 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
115 perror("sigaction");
116 return ret;
117 }
118
119 return ret;
120}
121
d4a1283e 122/*
3183dbb0 123 * Usage function on stream file.
d4a1283e 124 */
3183dbb0 125static void usage(FILE *fp)
d4a1283e 126{
3183dbb0
DG
127 fprintf(fp, "Usage: %s OPTIONS\n\nOptions:\n", progname);
128 fprintf(fp, " -h, --help "
d4a1283e 129 "Display this usage.\n");
3183dbb0 130 fprintf(fp, " -c, --consumerd-cmd-sock PATH "
d4a1283e 131 "Specify path for the command socket\n");
3183dbb0 132 fprintf(fp, " -e, --consumerd-err-sock PATH "
d4a1283e 133 "Specify path for the error socket\n");
3183dbb0 134 fprintf(fp, " -d, --daemonize "
d4a1283e 135 "Start as a daemon.\n");
3183dbb0 136 fprintf(fp, " -q, --quiet "
d4a1283e 137 "No output at all.\n");
3183dbb0 138 fprintf(fp, " -v, --verbose "
d4a1283e 139 "Verbose mode. Activate DBG() macro.\n");
3183dbb0 140 fprintf(fp, " -V, --version "
d4a1283e 141 "Show version number.\n");
3183dbb0 142 fprintf(fp, " -k, --kernel "
3bd1e081 143 "Consumer kernel buffers (default).\n");
3183dbb0 144 fprintf(fp, " -u, --ust "
3bd1e081 145 "Consumer UST buffers.%s\n",
74d0b642 146#ifdef HAVE_LIBLTTNG_UST_CTL
3bd1e081
MD
147 ""
148#else
149 " (support not compiled in)"
150#endif
151 );
d4a1283e
JD
152}
153
154/*
155 * daemon argument parsing
156 */
157static void parse_args(int argc, char **argv)
158{
159 int c;
160
161 static struct option long_options[] = {
7753dea8
MD
162 { "consumerd-cmd-sock", 1, 0, 'c' },
163 { "consumerd-err-sock", 1, 0, 'e' },
d4a1283e
JD
164 { "daemonize", 0, 0, 'd' },
165 { "help", 0, 0, 'h' },
166 { "quiet", 0, 0, 'q' },
167 { "verbose", 0, 0, 'v' },
168 { "version", 0, 0, 'V' },
3bd1e081 169 { "kernel", 0, 0, 'k' },
74d0b642 170#ifdef HAVE_LIBLTTNG_UST_CTL
3bd1e081
MD
171 { "ust", 0, 0, 'u' },
172#endif
d4a1283e
JD
173 { NULL, 0, 0, 0 }
174 };
175
176 while (1) {
177 int option_index = 0;
3bd1e081 178 c = getopt_long(argc, argv, "dhqvVku" "c:e:", long_options, &option_index);
d4a1283e
JD
179 if (c == -1) {
180 break;
181 }
182
183 switch (c) {
914a571b
JD
184 case 0:
185 fprintf(stderr, "option %s", long_options[option_index].name);
186 if (optarg) {
187 fprintf(stderr, " with arg %s\n", optarg);
188 }
189 break;
190 case 'c':
191 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
192 break;
193 case 'e':
194 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
195 break;
196 case 'd':
197 opt_daemon = 1;
198 break;
199 case 'h':
3183dbb0
DG
200 usage(stdout);
201 exit(EXIT_SUCCESS);
914a571b 202 case 'q':
97e19046 203 lttng_opt_quiet = 1;
914a571b
JD
204 break;
205 case 'v':
97e19046 206 lttng_opt_verbose = 1;
914a571b
JD
207 break;
208 case 'V':
209 fprintf(stdout, "%s\n", VERSION);
210 exit(EXIT_SUCCESS);
3bd1e081
MD
211 case 'k':
212 opt_type = LTTNG_CONSUMER_KERNEL;
213 break;
74d0b642 214#ifdef HAVE_LIBLTTNG_UST_CTL
3bd1e081 215 case 'u':
7753dea8
MD
216# if (CAA_BITS_PER_LONG == 64)
217 opt_type = LTTNG_CONSUMER64_UST;
218# elif (CAA_BITS_PER_LONG == 32)
219 opt_type = LTTNG_CONSUMER32_UST;
220# else
221# error "Unknown bitness"
222# endif
3bd1e081
MD
223 break;
224#endif
914a571b 225 default:
3183dbb0 226 usage(stderr);
914a571b 227 exit(EXIT_FAILURE);
d4a1283e
JD
228 }
229 }
230}
231
1ccfc0e3
DG
232/*
233 * Set open files limit to unlimited. This daemon can open a large number of
234 * file descriptors in order to consumer multiple kernel traces.
235 */
236static void set_ulimit(void)
237{
238 int ret;
239 struct rlimit lim;
240
241 /* The kernel does not allowed an infinite limit for open files */
242 lim.rlim_cur = 65535;
243 lim.rlim_max = 65535;
244
245 ret = setrlimit(RLIMIT_NOFILE, &lim);
246 if (ret < 0) {
247 PERROR("failed to set open files limit");
248 }
249}
250
d4a1283e
JD
251/*
252 * main
253 */
254int main(int argc, char **argv)
255{
256 int i;
257 int ret = 0;
258 void *status;
259
260 /* Parse arguments */
261 progname = argv[0];
262 parse_args(argc, argv);
263
264 /* Daemonize */
265 if (opt_daemon) {
266 ret = daemon(0, 0);
267 if (ret < 0) {
268 perror("daemon");
269 goto error;
270 }
271 }
272
273 if (strlen(command_sock_path) == 0) {
7753dea8
MD
274 switch (opt_type) {
275 case LTTNG_CONSUMER_KERNEL:
60922cb0 276 snprintf(command_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
990570ed 277 DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
278 break;
279 case LTTNG_CONSUMER64_UST:
67e40797 280 snprintf(command_sock_path, PATH_MAX,
60922cb0 281 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
282 break;
283 case LTTNG_CONSUMER32_UST:
67e40797 284 snprintf(command_sock_path, PATH_MAX,
60922cb0 285 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
286 break;
287 default:
288 WARN("Unknown consumerd type");
289 goto error;
290 }
d4a1283e 291 }
e4421fec
DG
292
293 /* Init */
294 lttng_consumer_init();
295
1ccfc0e3
DG
296 if (!getuid()) {
297 /* Set limit for open files */
298 set_ulimit();
299 }
300
5348b470 301 /* create the consumer instance with and assign the callbacks */
d41f73b7
MD
302 ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer,
303 NULL, lttng_consumer_on_recv_stream, NULL);
cb040cc1
JD
304 if (ctx == NULL) {
305 goto error;
306 }
307
3bd1e081 308 lttng_consumer_set_command_sock_path(ctx, command_sock_path);
d4a1283e 309 if (strlen(error_sock_path) == 0) {
7753dea8
MD
310 switch (opt_type) {
311 case LTTNG_CONSUMER_KERNEL:
60922cb0 312 snprintf(error_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
990570ed 313 DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
314 break;
315 case LTTNG_CONSUMER64_UST:
67e40797 316 snprintf(error_sock_path, PATH_MAX,
60922cb0 317 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
318 break;
319 case LTTNG_CONSUMER32_UST:
67e40797 320 snprintf(error_sock_path, PATH_MAX,
60922cb0 321 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
7753dea8
MD
322 break;
323 default:
324 WARN("Unknown consumerd type");
325 goto error;
326 }
d4a1283e
JD
327 }
328
329 if (set_signal_handler() < 0) {
330 goto error;
331 }
332
32258573 333 /* Connect to the socket created by lttng-sessiond to report errors */
d4a1283e 334 DBG("Connecting to error socket %s", error_sock_path);
1ce86c9a 335 ret = lttcomm_connect_unix_sock(error_sock_path);
32258573 336 /* not a fatal error, but all communication with lttng-sessiond will fail */
1ce86c9a 337 if (ret < 0) {
99bab54f 338 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
d4a1283e 339 }
3bd1e081 340 lttng_consumer_set_error_sock(ctx, ret);
d4a1283e
JD
341
342 /* Create the thread to manage the receive of fd */
3bd1e081 343 ret = pthread_create(&threads[0], NULL, lttng_consumer_thread_receive_fds,
cb040cc1 344 (void *) ctx);
d4a1283e
JD
345 if (ret != 0) {
346 perror("pthread_create");
347 goto error;
348 }
349
350 /* Create thread to manage the polling/writing of traces */
3bd1e081 351 ret = pthread_create(&threads[1], NULL, lttng_consumer_thread_poll_fds,
cb040cc1 352 (void *) ctx);
d4a1283e
JD
353 if (ret != 0) {
354 perror("pthread_create");
355 goto error;
356 }
357
358 for (i = 0; i < 2; i++) {
359 ret = pthread_join(threads[i], &status);
360 if (ret != 0) {
361 perror("pthread_join");
362 goto error;
363 }
364 }
365 ret = EXIT_SUCCESS;
3bd1e081 366 lttng_consumer_send_error(ctx, CONSUMERD_EXIT_SUCCESS);
d4a1283e
JD
367 goto end;
368
369error:
370 ret = EXIT_FAILURE;
3bd1e081 371 lttng_consumer_send_error(ctx, CONSUMERD_EXIT_FAILURE);
d4a1283e
JD
372
373end:
3bd1e081
MD
374 lttng_consumer_destroy(ctx);
375 lttng_consumer_cleanup();
d4a1283e
JD
376
377 return ret;
378}
This page took 0.05127 seconds and 5 git commands to generate.