Fix DBG message grammatical error
[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 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
82a3637f
DG
7 * as published by the Free Software Foundation; only version 2
8 * of the License.
d4a1283e
JD
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#define _GNU_SOURCE
21#include <fcntl.h>
22#include <getopt.h>
23#include <grp.h>
24#include <limits.h>
25#include <pthread.h>
26#include <signal.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/ipc.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>
03424a9b 38#include <sys/mman.h>
5348b470 39#include <assert.h>
3bd1e081 40#include <config.h>
7753dea8 41#include <urcu/compiler.h>
d4a1283e 42
10a8a223
DG
43#include <common/lttngerr.h>
44#include <common/kernel-ctl/kernel-ctl.h>
45#include <common/sessiond-comm/sessiond-comm.h>
46#include <common/kernel-consumer/kernel-consumer.h>
47#include <common/ust-consumer/ust-consumer.h>
48
49#include "lttng-consumerd.h"
d4a1283e 50
3bd1e081
MD
51/* TODO : support UST (all direct kernctl accesses). */
52
d4a1283e 53/* the two threads (receive fd and poll) */
6533b585 54static pthread_t threads[2];
d4a1283e 55
13e44745
JD
56/* to count the number of time the user pressed ctrl+c */
57static int sigintcount = 0;
58
d4a1283e
JD
59/* Argument variables */
60int opt_quiet;
61int opt_verbose;
62static int opt_daemon;
63static const char *progname;
6533b585
DG
64static char command_sock_path[PATH_MAX]; /* Global command socket path */
65static char error_sock_path[PATH_MAX]; /* Global error path */
3bd1e081 66static enum lttng_consumer_type opt_type = LTTNG_CONSUMER_KERNEL;
d4a1283e 67
7753dea8 68/* the liblttngconsumerd context */
3bd1e081 69static struct lttng_consumer_local_data *ctx;
cb040cc1 70
d4a1283e 71/*
6533b585 72 * Signal handler for the daemon
d4a1283e
JD
73 */
74static 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 */
88static 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
JD
120/*
121 * usage function on stderr
122 */
123static void usage(void)
124{
125 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
126 fprintf(stderr, " -h, --help "
127 "Display this usage.\n");
7753dea8 128 fprintf(stderr, " -c, --consumerd-cmd-sock PATH "
d4a1283e 129 "Specify path for the command socket\n");
7753dea8 130 fprintf(stderr, " -e, --consumerd-err-sock PATH "
d4a1283e
JD
131 "Specify path for the error socket\n");
132 fprintf(stderr, " -d, --daemonize "
133 "Start as a daemon.\n");
134 fprintf(stderr, " -q, --quiet "
135 "No output at all.\n");
136 fprintf(stderr, " -v, --verbose "
137 "Verbose mode. Activate DBG() macro.\n");
138 fprintf(stderr, " -V, --version "
139 "Show version number.\n");
3bd1e081
MD
140 fprintf(stderr, " -k, --kernel "
141 "Consumer kernel buffers (default).\n");
142 fprintf(stderr, " -u, --ust "
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 */
155static 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':
198 usage();
199 exit(EXIT_FAILURE);
200 case 'q':
201 opt_quiet = 1;
202 break;
203 case 'v':
204 opt_verbose = 1;
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
JD
223 default:
224 usage();
225 exit(EXIT_FAILURE);
d4a1283e
JD
226 }
227 }
228}
229
d4a1283e
JD
230/*
231 * main
232 */
233int main(int argc, char **argv)
234{
235 int i;
236 int ret = 0;
237 void *status;
238
239 /* Parse arguments */
240 progname = argv[0];
241 parse_args(argc, argv);
242
243 /* Daemonize */
244 if (opt_daemon) {
245 ret = daemon(0, 0);
246 if (ret < 0) {
247 perror("daemon");
248 goto error;
249 }
250 }
251
252 if (strlen(command_sock_path) == 0) {
7753dea8
MD
253 switch (opt_type) {
254 case LTTNG_CONSUMER_KERNEL:
67e40797
DG
255 snprintf(command_sock_path, PATH_MAX, KCONSUMERD_CMD_SOCK_PATH,
256 LTTNG_RUNDIR);
7753dea8
MD
257 break;
258 case LTTNG_CONSUMER64_UST:
67e40797
DG
259 snprintf(command_sock_path, PATH_MAX,
260 USTCONSUMERD64_CMD_SOCK_PATH, LTTNG_RUNDIR);
7753dea8
MD
261 break;
262 case LTTNG_CONSUMER32_UST:
67e40797
DG
263 snprintf(command_sock_path, PATH_MAX,
264 USTCONSUMERD32_CMD_SOCK_PATH, LTTNG_RUNDIR);
7753dea8
MD
265 break;
266 default:
267 WARN("Unknown consumerd type");
268 goto error;
269 }
d4a1283e 270 }
e4421fec
DG
271
272 /* Init */
273 lttng_consumer_init();
274
5348b470 275 /* create the consumer instance with and assign the callbacks */
d41f73b7
MD
276 ctx = lttng_consumer_create(opt_type, lttng_consumer_read_subbuffer,
277 NULL, lttng_consumer_on_recv_stream, NULL);
cb040cc1
JD
278 if (ctx == NULL) {
279 goto error;
280 }
281
3bd1e081 282 lttng_consumer_set_command_sock_path(ctx, command_sock_path);
d4a1283e 283 if (strlen(error_sock_path) == 0) {
7753dea8
MD
284 switch (opt_type) {
285 case LTTNG_CONSUMER_KERNEL:
67e40797
DG
286 snprintf(error_sock_path, PATH_MAX, KCONSUMERD_ERR_SOCK_PATH,
287 LTTNG_RUNDIR);
7753dea8
MD
288 break;
289 case LTTNG_CONSUMER64_UST:
67e40797
DG
290 snprintf(error_sock_path, PATH_MAX,
291 USTCONSUMERD64_ERR_SOCK_PATH, LTTNG_RUNDIR);
7753dea8
MD
292 break;
293 case LTTNG_CONSUMER32_UST:
67e40797
DG
294 snprintf(error_sock_path, PATH_MAX,
295 USTCONSUMERD32_ERR_SOCK_PATH, LTTNG_RUNDIR);
7753dea8
MD
296 break;
297 default:
298 WARN("Unknown consumerd type");
299 goto error;
300 }
d4a1283e
JD
301 }
302
303 if (set_signal_handler() < 0) {
304 goto error;
305 }
306
32258573 307 /* Connect to the socket created by lttng-sessiond to report errors */
d4a1283e 308 DBG("Connecting to error socket %s", error_sock_path);
1ce86c9a 309 ret = lttcomm_connect_unix_sock(error_sock_path);
32258573 310 /* not a fatal error, but all communication with lttng-sessiond will fail */
1ce86c9a 311 if (ret < 0) {
32258573 312 WARN("Cannot connect to error socket, is lttng-sessiond started ?");
d4a1283e 313 }
3bd1e081 314 lttng_consumer_set_error_sock(ctx, ret);
d4a1283e
JD
315
316 /* Create the thread to manage the receive of fd */
3bd1e081 317 ret = pthread_create(&threads[0], NULL, lttng_consumer_thread_receive_fds,
cb040cc1 318 (void *) ctx);
d4a1283e
JD
319 if (ret != 0) {
320 perror("pthread_create");
321 goto error;
322 }
323
324 /* Create thread to manage the polling/writing of traces */
3bd1e081 325 ret = pthread_create(&threads[1], NULL, lttng_consumer_thread_poll_fds,
cb040cc1 326 (void *) ctx);
d4a1283e
JD
327 if (ret != 0) {
328 perror("pthread_create");
329 goto error;
330 }
331
332 for (i = 0; i < 2; i++) {
333 ret = pthread_join(threads[i], &status);
334 if (ret != 0) {
335 perror("pthread_join");
336 goto error;
337 }
338 }
339 ret = EXIT_SUCCESS;
3bd1e081 340 lttng_consumer_send_error(ctx, CONSUMERD_EXIT_SUCCESS);
d4a1283e
JD
341 goto end;
342
343error:
344 ret = EXIT_FAILURE;
3bd1e081 345 lttng_consumer_send_error(ctx, CONSUMERD_EXIT_FAILURE);
d4a1283e
JD
346
347end:
3bd1e081
MD
348 lttng_consumer_destroy(ctx);
349 lttng_consumer_cleanup();
d4a1283e
JD
350
351 return ret;
352}
This page took 0.048802 seconds and 5 git commands to generate.