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