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