Commit | Line | Data |
---|---|---|
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> |
d4a1283e | 39 | |
6533b585 DG |
40 | #include <lttng/lttng-kconsumerd.h> |
41 | ||
d4a1283e | 42 | #include "lttngerr.h" |
50ecdf72 | 43 | #include "kernelctl.h" |
6533b585 DG |
44 | #include "ltt-kconsumerd.h" |
45 | #include "lttng-sessiond-comm.h" | |
d4a1283e JD |
46 | |
47 | /* the two threads (receive fd and poll) */ | |
6533b585 | 48 | static pthread_t threads[2]; |
d4a1283e | 49 | |
13e44745 JD |
50 | /* to count the number of time the user pressed ctrl+c */ |
51 | static int sigintcount = 0; | |
52 | ||
d4a1283e JD |
53 | /* Argument variables */ |
54 | int opt_quiet; | |
55 | int opt_verbose; | |
56 | static int opt_daemon; | |
57 | static const char *progname; | |
6533b585 DG |
58 | static char command_sock_path[PATH_MAX]; /* Global command socket path */ |
59 | static char error_sock_path[PATH_MAX]; /* Global error path */ | |
d4a1283e | 60 | |
6533b585 DG |
61 | /* the liblttngkconsumerd context */ |
62 | static struct lttng_kconsumerd_local_data *ctx; | |
cb040cc1 | 63 | |
d4a1283e | 64 | /* |
6533b585 | 65 | * Signal handler for the daemon |
d4a1283e JD |
66 | */ |
67 | static void sighandler(int sig) | |
68 | { | |
13e44745 JD |
69 | if (sig == SIGINT && sigintcount++ == 0) { |
70 | DBG("ignoring first SIGINT"); | |
71 | return; | |
72 | } | |
73 | ||
6533b585 | 74 | lttng_kconsumerd_should_exit(ctx); |
d4a1283e JD |
75 | } |
76 | ||
77 | /* | |
6533b585 | 78 | * Setup signal handler for : |
d4a1283e JD |
79 | * SIGINT, SIGTERM, SIGPIPE |
80 | */ | |
81 | static int set_signal_handler(void) | |
82 | { | |
83 | int ret = 0; | |
84 | struct sigaction sa; | |
85 | sigset_t sigset; | |
86 | ||
87 | if ((ret = sigemptyset(&sigset)) < 0) { | |
88 | perror("sigemptyset"); | |
89 | return ret; | |
90 | } | |
91 | ||
92 | sa.sa_handler = sighandler; | |
93 | sa.sa_mask = sigset; | |
94 | sa.sa_flags = 0; | |
95 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { | |
96 | perror("sigaction"); | |
97 | return ret; | |
98 | } | |
99 | ||
100 | if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { | |
101 | perror("sigaction"); | |
102 | return ret; | |
103 | } | |
104 | ||
105 | if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) { | |
106 | perror("sigaction"); | |
107 | return ret; | |
108 | } | |
109 | ||
110 | return ret; | |
111 | } | |
112 | ||
d4a1283e JD |
113 | /* |
114 | * usage function on stderr | |
115 | */ | |
116 | static void usage(void) | |
117 | { | |
118 | fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname); | |
119 | fprintf(stderr, " -h, --help " | |
120 | "Display this usage.\n"); | |
121 | fprintf(stderr, " -c, --kconsumerd-cmd-sock PATH " | |
122 | "Specify path for the command socket\n"); | |
123 | fprintf(stderr, " -e, --kconsumerd-err-sock PATH " | |
124 | "Specify path for the error socket\n"); | |
125 | fprintf(stderr, " -d, --daemonize " | |
126 | "Start as a daemon.\n"); | |
127 | fprintf(stderr, " -q, --quiet " | |
128 | "No output at all.\n"); | |
129 | fprintf(stderr, " -v, --verbose " | |
130 | "Verbose mode. Activate DBG() macro.\n"); | |
131 | fprintf(stderr, " -V, --version " | |
132 | "Show version number.\n"); | |
133 | } | |
134 | ||
135 | /* | |
136 | * daemon argument parsing | |
137 | */ | |
138 | static void parse_args(int argc, char **argv) | |
139 | { | |
140 | int c; | |
141 | ||
142 | static struct option long_options[] = { | |
143 | { "kconsumerd-cmd-sock", 1, 0, 'c' }, | |
144 | { "kconsumerd-err-sock", 1, 0, 'e' }, | |
145 | { "daemonize", 0, 0, 'd' }, | |
146 | { "help", 0, 0, 'h' }, | |
147 | { "quiet", 0, 0, 'q' }, | |
148 | { "verbose", 0, 0, 'v' }, | |
149 | { "version", 0, 0, 'V' }, | |
150 | { NULL, 0, 0, 0 } | |
151 | }; | |
152 | ||
153 | while (1) { | |
154 | int option_index = 0; | |
155 | c = getopt_long(argc, argv, "dhqvV" "c:e:", long_options, &option_index); | |
156 | if (c == -1) { | |
157 | break; | |
158 | } | |
159 | ||
160 | switch (c) { | |
914a571b JD |
161 | case 0: |
162 | fprintf(stderr, "option %s", long_options[option_index].name); | |
163 | if (optarg) { | |
164 | fprintf(stderr, " with arg %s\n", optarg); | |
165 | } | |
166 | break; | |
167 | case 'c': | |
168 | snprintf(command_sock_path, PATH_MAX, "%s", optarg); | |
169 | break; | |
170 | case 'e': | |
171 | snprintf(error_sock_path, PATH_MAX, "%s", optarg); | |
172 | break; | |
173 | case 'd': | |
174 | opt_daemon = 1; | |
175 | break; | |
176 | case 'h': | |
177 | usage(); | |
178 | exit(EXIT_FAILURE); | |
179 | case 'q': | |
180 | opt_quiet = 1; | |
181 | break; | |
182 | case 'v': | |
183 | opt_verbose = 1; | |
184 | break; | |
185 | case 'V': | |
186 | fprintf(stdout, "%s\n", VERSION); | |
187 | exit(EXIT_SUCCESS); | |
188 | default: | |
189 | usage(); | |
190 | exit(EXIT_FAILURE); | |
d4a1283e JD |
191 | } |
192 | } | |
193 | } | |
194 | ||
cb040cc1 | 195 | /* |
6533b585 | 196 | * Consume data on a file descriptor and write it on a trace file. |
cb040cc1 | 197 | */ |
6533b585 | 198 | static int read_subbuffer(struct lttng_kconsumerd_fd *kconsumerd_fd) |
cb040cc1 JD |
199 | { |
200 | unsigned long len; | |
201 | int err; | |
202 | long ret = 0; | |
203 | int infd = kconsumerd_fd->consumerd_fd; | |
204 | ||
205 | DBG("In kconsumerd_read_subbuffer (infd : %d)", infd); | |
206 | /* Get the next subbuffer */ | |
207 | err = kernctl_get_next_subbuf(infd); | |
208 | if (err != 0) { | |
209 | ret = errno; | |
210 | perror("Reserving sub buffer failed (everything is normal, " | |
211 | "it is due to concurrency)"); | |
212 | goto end; | |
213 | } | |
214 | ||
57194bf2 | 215 | switch (kconsumerd_fd->output) { |
cb040cc1 JD |
216 | case LTTNG_EVENT_SPLICE: |
217 | /* read the whole subbuffer */ | |
218 | err = kernctl_get_padded_subbuf_size(infd, &len); | |
219 | if (err != 0) { | |
220 | ret = errno; | |
221 | perror("Getting sub-buffer len failed."); | |
222 | goto end; | |
223 | } | |
224 | ||
225 | /* splice the subbuffer to the tracefile */ | |
6533b585 | 226 | ret = lttng_kconsumerd_on_read_subbuffer_splice(ctx, kconsumerd_fd, len); |
cb040cc1 JD |
227 | if (ret < 0) { |
228 | /* | |
229 | * display the error but continue processing to try | |
230 | * to release the subbuffer | |
231 | */ | |
232 | ERR("Error splicing to tracefile"); | |
233 | } | |
234 | break; | |
235 | case LTTNG_EVENT_MMAP: | |
236 | /* read the used subbuffer size */ | |
8b270bdb | 237 | err = kernctl_get_padded_subbuf_size(infd, &len); |
cb040cc1 JD |
238 | if (err != 0) { |
239 | ret = errno; | |
240 | perror("Getting sub-buffer len failed."); | |
241 | goto end; | |
242 | } | |
243 | /* write the subbuffer to the tracefile */ | |
6533b585 | 244 | ret = lttng_kconsumerd_on_read_subbuffer_mmap(ctx, kconsumerd_fd, len); |
cb040cc1 JD |
245 | if (ret < 0) { |
246 | /* | |
247 | * display the error but continue processing to try | |
248 | * to release the subbuffer | |
249 | */ | |
250 | ERR("Error writing to tracefile"); | |
251 | } | |
252 | break; | |
253 | default: | |
254 | ERR("Unknown output method"); | |
255 | ret = -1; | |
256 | } | |
257 | ||
258 | err = kernctl_put_next_subbuf(infd); | |
259 | if (err != 0) { | |
260 | ret = errno; | |
261 | if (errno == EFAULT) { | |
262 | perror("Error in unreserving sub buffer\n"); | |
263 | } else if (errno == EIO) { | |
264 | /* Should never happen with newer LTTng versions */ | |
265 | perror("Reader has been pushed by the writer, last sub-buffer corrupted."); | |
266 | } | |
267 | goto end; | |
268 | } | |
269 | ||
270 | end: | |
271 | return ret; | |
272 | } | |
d4a1283e JD |
273 | |
274 | /* | |
275 | * main | |
276 | */ | |
277 | int main(int argc, char **argv) | |
278 | { | |
279 | int i; | |
280 | int ret = 0; | |
281 | void *status; | |
282 | ||
283 | /* Parse arguments */ | |
284 | progname = argv[0]; | |
285 | parse_args(argc, argv); | |
286 | ||
287 | /* Daemonize */ | |
288 | if (opt_daemon) { | |
289 | ret = daemon(0, 0); | |
290 | if (ret < 0) { | |
291 | perror("daemon"); | |
292 | goto error; | |
293 | } | |
294 | } | |
295 | ||
296 | if (strlen(command_sock_path) == 0) { | |
297 | snprintf(command_sock_path, PATH_MAX, | |
298 | KCONSUMERD_CMD_SOCK_PATH); | |
299 | } | |
cb040cc1 | 300 | /* create the pipe to wake to receiving thread when needed */ |
6533b585 | 301 | ctx = lttng_kconsumerd_create(read_subbuffer); |
cb040cc1 JD |
302 | if (ctx == NULL) { |
303 | goto error; | |
304 | } | |
305 | ||
6533b585 | 306 | lttng_kconsumerd_set_command_sock_path(ctx, command_sock_path); |
d4a1283e JD |
307 | if (strlen(error_sock_path) == 0) { |
308 | snprintf(error_sock_path, PATH_MAX, | |
309 | KCONSUMERD_ERR_SOCK_PATH); | |
310 | } | |
311 | ||
312 | if (set_signal_handler() < 0) { | |
313 | goto error; | |
314 | } | |
315 | ||
316 | /* Connect to the socket created by ltt-sessiond to report errors */ | |
317 | DBG("Connecting to error socket %s", error_sock_path); | |
1ce86c9a | 318 | ret = lttcomm_connect_unix_sock(error_sock_path); |
d4a1283e | 319 | /* not a fatal error, but all communication with ltt-sessiond will fail */ |
1ce86c9a | 320 | if (ret < 0) { |
d4a1283e JD |
321 | WARN("Cannot connect to error socket, is ltt-sessiond started ?"); |
322 | } | |
6533b585 | 323 | lttng_kconsumerd_set_error_sock(ctx, ret); |
d4a1283e JD |
324 | |
325 | /* Create the thread to manage the receive of fd */ | |
6533b585 | 326 | ret = pthread_create(&threads[0], NULL, lttng_kconsumerd_thread_receive_fds, |
cb040cc1 | 327 | (void *) ctx); |
d4a1283e JD |
328 | if (ret != 0) { |
329 | perror("pthread_create"); | |
330 | goto error; | |
331 | } | |
332 | ||
333 | /* Create thread to manage the polling/writing of traces */ | |
6533b585 | 334 | ret = pthread_create(&threads[1], NULL, lttng_kconsumerd_thread_poll_fds, |
cb040cc1 | 335 | (void *) ctx); |
d4a1283e JD |
336 | if (ret != 0) { |
337 | perror("pthread_create"); | |
338 | goto error; | |
339 | } | |
340 | ||
341 | for (i = 0; i < 2; i++) { | |
342 | ret = pthread_join(threads[i], &status); | |
343 | if (ret != 0) { | |
344 | perror("pthread_join"); | |
345 | goto error; | |
346 | } | |
347 | } | |
348 | ret = EXIT_SUCCESS; | |
6533b585 | 349 | lttng_kconsumerd_send_error(ctx, KCONSUMERD_EXIT_SUCCESS); |
d4a1283e JD |
350 | goto end; |
351 | ||
352 | error: | |
353 | ret = EXIT_FAILURE; | |
6533b585 | 354 | lttng_kconsumerd_send_error(ctx, KCONSUMERD_EXIT_FAILURE); |
d4a1283e JD |
355 | |
356 | end: | |
6533b585 DG |
357 | lttng_kconsumerd_destroy(ctx); |
358 | lttng_kconsumerd_cleanup(); | |
d4a1283e JD |
359 | |
360 | return ret; | |
361 | } |