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> | |
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 | 56 | static pthread_t threads[2]; |
d4a1283e | 57 | |
3183dbb0 | 58 | /* to count the number of times the user pressed ctrl+c */ |
13e44745 JD |
59 | static int sigintcount = 0; |
60 | ||
d4a1283e | 61 | /* Argument variables */ |
97e19046 DG |
62 | int lttng_opt_quiet; /* not static in error.h */ |
63 | int lttng_opt_verbose; /* not static in error.h */ | |
d4a1283e JD |
64 | static int opt_daemon; |
65 | static const char *progname; | |
6533b585 DG |
66 | static char command_sock_path[PATH_MAX]; /* Global command socket path */ |
67 | static char error_sock_path[PATH_MAX]; /* Global error path */ | |
3bd1e081 | 68 | static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL; |
d4a1283e | 69 | |
7753dea8 | 70 | /* the liblttngconsumerd context */ |
3bd1e081 | 71 | static struct lttng_consumer_local_data *ctx; |
cb040cc1 | 72 | |
d4a1283e | 73 | /* |
6533b585 | 74 | * Signal handler for the daemon |
d4a1283e JD |
75 | */ |
76 | static 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 | */ | |
90 | static 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 | 125 | static 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 | */ | |
157 | static 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 | */ | |
236 | static 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 | */ | |
254 | int 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) { | |
ceed52b5 MD |
266 | int i; |
267 | ||
268 | /* | |
269 | * fork | |
270 | * child: setsid, close FD 0, 1, 2, chdir / | |
271 | * parent: exit (if fork is successful) | |
272 | */ | |
d4a1283e JD |
273 | ret = daemon(0, 0); |
274 | if (ret < 0) { | |
ceed52b5 | 275 | PERROR("daemon"); |
d4a1283e JD |
276 | goto error; |
277 | } | |
ceed52b5 MD |
278 | /* |
279 | * We are in the child. Make sure all other file | |
280 | * descriptors are closed, in case we are called with | |
281 | * more opened file descriptors than the standard ones. | |
282 | */ | |
283 | for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { | |
284 | (void) close(i); | |
285 | } | |
d4a1283e JD |
286 | } |
287 | ||
288 | if (strlen(command_sock_path) == 0) { | |
7753dea8 MD |
289 | switch (opt_type) { |
290 | case LTTNG_CONSUMER_KERNEL: | |
60922cb0 | 291 | snprintf(command_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_CMD_SOCK_PATH, |
990570ed | 292 | DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
293 | break; |
294 | case LTTNG_CONSUMER64_UST: | |
67e40797 | 295 | snprintf(command_sock_path, PATH_MAX, |
60922cb0 | 296 | DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
297 | break; |
298 | case LTTNG_CONSUMER32_UST: | |
67e40797 | 299 | snprintf(command_sock_path, PATH_MAX, |
60922cb0 | 300 | DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
301 | break; |
302 | default: | |
303 | WARN("Unknown consumerd type"); | |
304 | goto error; | |
305 | } | |
d4a1283e | 306 | } |
e4421fec DG |
307 | |
308 | /* Init */ | |
309 | lttng_consumer_init(); | |
310 | ||
1ccfc0e3 DG |
311 | if (!getuid()) { |
312 | /* Set limit for open files */ | |
313 | set_ulimit(); | |
314 | } | |
315 | ||
5348b470 | 316 | /* create the consumer instance with and assign the callbacks */ |
d41f73b7 MD |
317 | ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer, |
318 | NULL, lttng_consumer_on_recv_stream, NULL); | |
cb040cc1 JD |
319 | if (ctx == NULL) { |
320 | goto error; | |
321 | } | |
322 | ||
3bd1e081 | 323 | lttng_consumer_set_command_sock_path(ctx, command_sock_path); |
d4a1283e | 324 | if (strlen(error_sock_path) == 0) { |
7753dea8 MD |
325 | switch (opt_type) { |
326 | case LTTNG_CONSUMER_KERNEL: | |
60922cb0 | 327 | snprintf(error_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_ERR_SOCK_PATH, |
990570ed | 328 | DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
329 | break; |
330 | case LTTNG_CONSUMER64_UST: | |
67e40797 | 331 | snprintf(error_sock_path, PATH_MAX, |
60922cb0 | 332 | DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
333 | break; |
334 | case LTTNG_CONSUMER32_UST: | |
67e40797 | 335 | snprintf(error_sock_path, PATH_MAX, |
60922cb0 | 336 | DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR); |
7753dea8 MD |
337 | break; |
338 | default: | |
339 | WARN("Unknown consumerd type"); | |
340 | goto error; | |
341 | } | |
d4a1283e JD |
342 | } |
343 | ||
344 | if (set_signal_handler() < 0) { | |
345 | goto error; | |
346 | } | |
347 | ||
32258573 | 348 | /* Connect to the socket created by lttng-sessiond to report errors */ |
d4a1283e | 349 | DBG("Connecting to error socket %s", error_sock_path); |
1ce86c9a | 350 | ret = lttcomm_connect_unix_sock(error_sock_path); |
32258573 | 351 | /* not a fatal error, but all communication with lttng-sessiond will fail */ |
1ce86c9a | 352 | if (ret < 0) { |
99bab54f | 353 | WARN("Cannot connect to error socket (is lttng-sessiond started?)"); |
d4a1283e | 354 | } |
3bd1e081 | 355 | lttng_consumer_set_error_sock(ctx, ret); |
d4a1283e JD |
356 | |
357 | /* Create the thread to manage the receive of fd */ | |
3bd1e081 | 358 | ret = pthread_create(&threads[0], NULL, lttng_consumer_thread_receive_fds, |
cb040cc1 | 359 | (void *) ctx); |
d4a1283e JD |
360 | if (ret != 0) { |
361 | perror("pthread_create"); | |
362 | goto error; | |
363 | } | |
364 | ||
365 | /* Create thread to manage the polling/writing of traces */ | |
3bd1e081 | 366 | ret = pthread_create(&threads[1], NULL, lttng_consumer_thread_poll_fds, |
cb040cc1 | 367 | (void *) ctx); |
d4a1283e JD |
368 | if (ret != 0) { |
369 | perror("pthread_create"); | |
370 | goto error; | |
371 | } | |
372 | ||
373 | for (i = 0; i < 2; i++) { | |
374 | ret = pthread_join(threads[i], &status); | |
375 | if (ret != 0) { | |
376 | perror("pthread_join"); | |
377 | goto error; | |
378 | } | |
379 | } | |
380 | ret = EXIT_SUCCESS; | |
3bd1e081 | 381 | lttng_consumer_send_error(ctx, CONSUMERD_EXIT_SUCCESS); |
d4a1283e JD |
382 | goto end; |
383 | ||
384 | error: | |
385 | ret = EXIT_FAILURE; | |
3bd1e081 | 386 | lttng_consumer_send_error(ctx, CONSUMERD_EXIT_FAILURE); |
d4a1283e JD |
387 | |
388 | end: | |
3bd1e081 MD |
389 | lttng_consumer_destroy(ctx); |
390 | lttng_consumer_cleanup(); | |
d4a1283e JD |
391 | |
392 | return ret; | |
393 | } |