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