Fix: close all file descriptors when executed as daemon
[lttng-tools.git] / src / bin / lttng-consumerd / lttng-consumerd.c
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 modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
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 *
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.
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>
30 #include <sys/resource.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>
38 #include <sys/mman.h>
39 #include <assert.h>
40 #include <config.h>
41 #include <urcu/compiler.h>
42 #include <ulimit.h>
43
44 #include <common/defaults.h>
45 #include <common/common.h>
46 #include <common/kernel-consumer/kernel-consumer.h>
47 #include <common/kernel-ctl/kernel-ctl.h>
48 #include <common/sessiond-comm/sessiond-comm.h>
49 #include <common/ust-consumer/ust-consumer.h>
50
51 #include "lttng-consumerd.h"
52
53 /* TODO : support UST (all direct kernel-ctl accesses). */
54
55 /* the two threads (receive fd and poll) */
56 static pthread_t threads[2];
57
58 /* to count the number of times the user pressed ctrl+c */
59 static int sigintcount = 0;
60
61 /* Argument variables */
62 int lttng_opt_quiet; /* not static in error.h */
63 int lttng_opt_verbose; /* not static in error.h */
64 static int opt_daemon;
65 static const char *progname;
66 static char command_sock_path[PATH_MAX]; /* Global command socket path */
67 static char error_sock_path[PATH_MAX]; /* Global error path */
68 static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL;
69
70 /* the liblttngconsumerd context */
71 static struct lttng_consumer_local_data *ctx;
72
73 /*
74 * Signal handler for the daemon
75 */
76 static void sighandler(int sig)
77 {
78 if (sig == SIGINT && sigintcount++ == 0) {
79 DBG("ignoring first SIGINT");
80 return;
81 }
82
83 lttng_consumer_should_exit(ctx);
84 }
85
86 /*
87 * Setup signal handler for :
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
122 /*
123 * Usage function on stream file.
124 */
125 static void usage(FILE *fp)
126 {
127 fprintf(fp, "Usage: %s OPTIONS\n\nOptions:\n", progname);
128 fprintf(fp, " -h, --help "
129 "Display this usage.\n");
130 fprintf(fp, " -c, --consumerd-cmd-sock PATH "
131 "Specify path for the command socket\n");
132 fprintf(fp, " -e, --consumerd-err-sock PATH "
133 "Specify path for the error socket\n");
134 fprintf(fp, " -d, --daemonize "
135 "Start as a daemon.\n");
136 fprintf(fp, " -q, --quiet "
137 "No output at all.\n");
138 fprintf(fp, " -v, --verbose "
139 "Verbose mode. Activate DBG() macro.\n");
140 fprintf(fp, " -V, --version "
141 "Show version number.\n");
142 fprintf(fp, " -k, --kernel "
143 "Consumer kernel buffers (default).\n");
144 fprintf(fp, " -u, --ust "
145 "Consumer UST buffers.%s\n",
146 #ifdef HAVE_LIBLTTNG_UST_CTL
147 ""
148 #else
149 " (support not compiled in)"
150 #endif
151 );
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[] = {
162 { "consumerd-cmd-sock", 1, 0, 'c' },
163 { "consumerd-err-sock", 1, 0, 'e' },
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' },
169 { "kernel", 0, 0, 'k' },
170 #ifdef HAVE_LIBLTTNG_UST_CTL
171 { "ust", 0, 0, 'u' },
172 #endif
173 { NULL, 0, 0, 0 }
174 };
175
176 while (1) {
177 int option_index = 0;
178 c = getopt_long(argc, argv, "dhqvVku" "c:e:", long_options, &option_index);
179 if (c == -1) {
180 break;
181 }
182
183 switch (c) {
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':
200 usage(stdout);
201 exit(EXIT_SUCCESS);
202 case 'q':
203 lttng_opt_quiet = 1;
204 break;
205 case 'v':
206 lttng_opt_verbose = 1;
207 break;
208 case 'V':
209 fprintf(stdout, "%s\n", VERSION);
210 exit(EXIT_SUCCESS);
211 case 'k':
212 opt_type = LTTNG_CONSUMER_KERNEL;
213 break;
214 #ifdef HAVE_LIBLTTNG_UST_CTL
215 case 'u':
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
223 break;
224 #endif
225 default:
226 usage(stderr);
227 exit(EXIT_FAILURE);
228 }
229 }
230 }
231
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
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) {
266 int i;
267
268 /*
269 * fork
270 * child: setsid, close FD 0, 1, 2, chdir /
271 * parent: exit (if fork is successful)
272 */
273 ret = daemon(0, 0);
274 if (ret < 0) {
275 PERROR("daemon");
276 goto error;
277 }
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 }
286 }
287
288 if (strlen(command_sock_path) == 0) {
289 switch (opt_type) {
290 case LTTNG_CONSUMER_KERNEL:
291 snprintf(command_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
292 DEFAULT_LTTNG_RUNDIR);
293 break;
294 case LTTNG_CONSUMER64_UST:
295 snprintf(command_sock_path, PATH_MAX,
296 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
297 break;
298 case LTTNG_CONSUMER32_UST:
299 snprintf(command_sock_path, PATH_MAX,
300 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
301 break;
302 default:
303 WARN("Unknown consumerd type");
304 goto error;
305 }
306 }
307
308 /* Init */
309 lttng_consumer_init();
310
311 if (!getuid()) {
312 /* Set limit for open files */
313 set_ulimit();
314 }
315
316 /* create the consumer instance with and assign the callbacks */
317 ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer,
318 NULL, lttng_consumer_on_recv_stream, NULL);
319 if (ctx == NULL) {
320 goto error;
321 }
322
323 lttng_consumer_set_command_sock_path(ctx, command_sock_path);
324 if (strlen(error_sock_path) == 0) {
325 switch (opt_type) {
326 case LTTNG_CONSUMER_KERNEL:
327 snprintf(error_sock_path, PATH_MAX, DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
328 DEFAULT_LTTNG_RUNDIR);
329 break;
330 case LTTNG_CONSUMER64_UST:
331 snprintf(error_sock_path, PATH_MAX,
332 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
333 break;
334 case LTTNG_CONSUMER32_UST:
335 snprintf(error_sock_path, PATH_MAX,
336 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, DEFAULT_LTTNG_RUNDIR);
337 break;
338 default:
339 WARN("Unknown consumerd type");
340 goto error;
341 }
342 }
343
344 if (set_signal_handler() < 0) {
345 goto error;
346 }
347
348 /* Connect to the socket created by lttng-sessiond to report errors */
349 DBG("Connecting to error socket %s", error_sock_path);
350 ret = lttcomm_connect_unix_sock(error_sock_path);
351 /* not a fatal error, but all communication with lttng-sessiond will fail */
352 if (ret < 0) {
353 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
354 }
355 lttng_consumer_set_error_sock(ctx, ret);
356
357 /* Create the thread to manage the receive of fd */
358 ret = pthread_create(&threads[0], NULL, lttng_consumer_thread_receive_fds,
359 (void *) ctx);
360 if (ret != 0) {
361 perror("pthread_create");
362 goto error;
363 }
364
365 /* Create thread to manage the polling/writing of traces */
366 ret = pthread_create(&threads[1], NULL, lttng_consumer_thread_poll_fds,
367 (void *) ctx);
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;
381 lttng_consumer_send_error(ctx, CONSUMERD_EXIT_SUCCESS);
382 goto end;
383
384 error:
385 ret = EXIT_FAILURE;
386 lttng_consumer_send_error(ctx, CONSUMERD_EXIT_FAILURE);
387
388 end:
389 lttng_consumer_destroy(ctx);
390 lttng_consumer_cleanup();
391
392 return ret;
393 }
This page took 0.058066 seconds and 4 git commands to generate.