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