6cecb9cf |
1 | /* lttd |
2 | * |
3 | * Linux Trace Toolkit Daemon |
4 | * |
5 | * This is a simple daemon that reads a few relayfs channels and save them in a |
6 | * trace. |
7 | * |
8 | * |
9 | * Copyright 2005 - |
10 | * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
11 | */ |
12 | |
4e4d11b3 |
13 | #ifdef HAVE_CONFIG_H |
13474e75 |
14 | #include <config.h> |
4e4d11b3 |
15 | #endif |
16 | |
c3e8c24f |
17 | #define _REENTRANT |
4e4d11b3 |
18 | #define _GNU_SOURCE |
c3e8c24f |
19 | #include <features.h> |
6cecb9cf |
20 | #include <stdio.h> |
af4061b2 |
21 | #include <unistd.h> |
6cecb9cf |
22 | #include <errno.h> |
23 | #include <sys/types.h> |
24 | #include <sys/stat.h> |
6cecb9cf |
25 | #include <stdlib.h> |
26 | #include <dirent.h> |
27 | #include <string.h> |
9e7dbe44 |
28 | #include <fcntl.h> |
29 | #include <sys/poll.h> |
af4061b2 |
30 | #include <sys/mman.h> |
31 | #include <signal.h> |
c3e8c24f |
32 | #include <pthread.h> |
af4061b2 |
33 | |
34 | /* Relayfs IOCTL */ |
35 | #include <asm/ioctl.h> |
36 | #include <asm/types.h> |
37 | |
38 | /* Get the next sub buffer that can be read. */ |
39 | #define RELAYFS_GET_SUBBUF _IOR(0xF4, 0x00,__u32) |
40 | /* Release the oldest reserved (by "get") sub buffer. */ |
b2c7cb27 |
41 | #define RELAYFS_PUT_SUBBUF _IOW(0xF4, 0x01,__u32) |
af4061b2 |
42 | /* returns the number of sub buffers in the per cpu channel. */ |
43 | #define RELAYFS_GET_N_SUBBUFS _IOR(0xF4, 0x02,__u32) |
44 | /* returns the size of the sub buffers. */ |
45 | #define RELAYFS_GET_SUBBUF_SIZE _IOR(0xF4, 0x03,__u32) |
46 | |
47 | |
6cecb9cf |
48 | |
49 | enum { |
50 | GET_SUBBUF, |
51 | PUT_SUBBUF, |
52 | GET_N_BUBBUFS, |
53 | GET_SUBBUF_SIZE |
54 | }; |
55 | |
56 | struct fd_pair { |
57 | int channel; |
58 | int trace; |
af4061b2 |
59 | unsigned int n_subbufs; |
60 | unsigned int subbuf_size; |
61 | void *mmap; |
22acb617 |
62 | pthread_mutex_t mutex; |
6cecb9cf |
63 | }; |
64 | |
65 | struct channel_trace_fd { |
66 | struct fd_pair *pair; |
67 | int num_pairs; |
68 | }; |
69 | |
70 | static char *trace_name = NULL; |
71 | static char *channel_name = NULL; |
72 | static int daemon_mode = 0; |
9e7dbe44 |
73 | static int append_mode = 0; |
c3e8c24f |
74 | static unsigned long num_threads = 1; |
af4061b2 |
75 | volatile static int quit_program = 0; /* For signal handler */ |
6cecb9cf |
76 | |
77 | /* Args : |
78 | * |
79 | * -t directory Directory name of the trace to write to. Will be created. |
80 | * -c directory Root directory of the relayfs trace channels. |
81 | * -d Run in background (daemon). |
9e7dbe44 |
82 | * -a Trace append mode. |
86a65fdb |
83 | * -s Send SIGUSR1 to parent when ready for IO. |
6cecb9cf |
84 | */ |
85 | void show_arguments(void) |
86 | { |
87 | printf("Please use the following arguments :\n"); |
88 | printf("\n"); |
89 | printf("-t directory Directory name of the trace to write to.\n" |
90 | " It will be created.\n"); |
91 | printf("-c directory Root directory of the relayfs trace channels.\n"); |
92 | printf("-d Run in background (daemon).\n"); |
9e7dbe44 |
93 | printf("-a Append to an possibly existing trace.\n"); |
22acb617 |
94 | printf("-N Number of threads to start.\n"); |
6cecb9cf |
95 | printf("\n"); |
96 | } |
97 | |
98 | |
99 | /* parse_arguments |
100 | * |
101 | * Parses the command line arguments. |
102 | * |
103 | * Returns 1 if the arguments were correct, but doesn't ask for program |
104 | * continuation. Returns -1 if the arguments are incorrect, or 0 if OK. |
105 | */ |
106 | int parse_arguments(int argc, char **argv) |
107 | { |
108 | int ret = 0; |
109 | int argn = 1; |
110 | |
111 | if(argc == 2) { |
112 | if(strcmp(argv[1], "-h") == 0) { |
113 | return 1; |
114 | } |
115 | } |
116 | |
9e7dbe44 |
117 | while(argn < argc) { |
6cecb9cf |
118 | |
119 | switch(argv[argn][0]) { |
120 | case '-': |
121 | switch(argv[argn][1]) { |
122 | case 't': |
9e7dbe44 |
123 | if(argn+1 < argc) { |
124 | trace_name = argv[argn+1]; |
125 | argn++; |
126 | } |
6cecb9cf |
127 | break; |
128 | case 'c': |
9e7dbe44 |
129 | if(argn+1 < argc) { |
130 | channel_name = argv[argn+1]; |
131 | argn++; |
132 | } |
6cecb9cf |
133 | break; |
134 | case 'd': |
135 | daemon_mode = 1; |
136 | break; |
9e7dbe44 |
137 | case 'a': |
138 | append_mode = 1; |
139 | break; |
22acb617 |
140 | case 'N': |
c3e8c24f |
141 | if(argn+1 < argc) { |
142 | num_threads = strtoul(argv[argn+1], NULL, 0); |
143 | argn++; |
144 | } |
145 | break; |
6cecb9cf |
146 | default: |
147 | printf("Invalid argument '%s'.\n", argv[argn]); |
148 | printf("\n"); |
149 | ret = -1; |
150 | } |
151 | break; |
152 | default: |
153 | printf("Invalid argument '%s'.\n", argv[argn]); |
154 | printf("\n"); |
155 | ret = -1; |
156 | } |
157 | argn++; |
158 | } |
159 | |
160 | if(trace_name == NULL) { |
161 | printf("Please specify a trace name.\n"); |
162 | printf("\n"); |
163 | ret = -1; |
164 | } |
165 | |
166 | if(channel_name == NULL) { |
167 | printf("Please specify a channel name.\n"); |
168 | printf("\n"); |
169 | ret = -1; |
170 | } |
171 | |
172 | return ret; |
173 | } |
174 | |
175 | void show_info(void) |
176 | { |
177 | printf("Linux Trace Toolkit Trace Daemon\n"); |
178 | printf("\n"); |
179 | printf("Reading from relayfs directory : %s\n", channel_name); |
180 | printf("Writing to trace directory : %s\n", trace_name); |
181 | printf("\n"); |
182 | } |
183 | |
184 | |
af4061b2 |
185 | /* signal handling */ |
186 | |
187 | static void handler(int signo) |
188 | { |
189 | printf("Signal %d received : exiting cleanly\n", signo); |
190 | quit_program = 1; |
191 | } |
192 | |
193 | |
194 | |
6cecb9cf |
195 | int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name, |
196 | struct channel_trace_fd *fd_pairs) |
197 | { |
198 | DIR *channel_dir = opendir(subchannel_name); |
199 | struct dirent *entry; |
200 | struct stat stat_buf; |
201 | int ret; |
202 | char path_channel[PATH_MAX]; |
203 | int path_channel_len; |
204 | char *path_channel_ptr; |
205 | char path_trace[PATH_MAX]; |
206 | int path_trace_len; |
207 | char *path_trace_ptr; |
02ba971e |
208 | int open_ret = 0; |
6cecb9cf |
209 | |
210 | if(channel_dir == NULL) { |
211 | perror(subchannel_name); |
02ba971e |
212 | open_ret = ENOENT; |
213 | goto end; |
6cecb9cf |
214 | } |
215 | |
6cecb9cf |
216 | printf("Creating trace subdirectory %s\n", subtrace_name); |
217 | ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO); |
218 | if(ret == -1) { |
3986d00b |
219 | if(errno != EEXIST) { |
9e7dbe44 |
220 | perror(subtrace_name); |
02ba971e |
221 | open_ret = -1; |
222 | goto end; |
9e7dbe44 |
223 | } |
6cecb9cf |
224 | } |
225 | |
226 | strncpy(path_channel, subchannel_name, PATH_MAX-1); |
227 | path_channel_len = strlen(path_channel); |
228 | path_channel[path_channel_len] = '/'; |
229 | path_channel_len++; |
230 | path_channel_ptr = path_channel + path_channel_len; |
231 | |
232 | strncpy(path_trace, subtrace_name, PATH_MAX-1); |
233 | path_trace_len = strlen(path_trace); |
234 | path_trace[path_trace_len] = '/'; |
235 | path_trace_len++; |
236 | path_trace_ptr = path_trace + path_trace_len; |
237 | |
238 | while((entry = readdir(channel_dir)) != NULL) { |
239 | |
240 | if(entry->d_name[0] == '.') continue; |
241 | |
242 | strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len); |
243 | strncpy(path_trace_ptr, entry->d_name, PATH_MAX - path_trace_len); |
244 | |
245 | ret = stat(path_channel, &stat_buf); |
246 | if(ret == -1) { |
247 | perror(path_channel); |
248 | continue; |
249 | } |
250 | |
251 | printf("Channel file : %s\n", path_channel); |
252 | |
253 | if(S_ISDIR(stat_buf.st_mode)) { |
254 | |
255 | printf("Entering channel subdirectory...\n"); |
256 | ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs); |
257 | if(ret < 0) continue; |
9e7dbe44 |
258 | } else if(S_ISREG(stat_buf.st_mode)) { |
259 | printf("Opening file.\n"); |
260 | |
261 | fd_pairs->pair = realloc(fd_pairs->pair, |
262 | ++fd_pairs->num_pairs * sizeof(struct fd_pair)); |
263 | |
264 | /* Open the channel in read mode */ |
265 | fd_pairs->pair[fd_pairs->num_pairs-1].channel = |
266 | open(path_channel, O_RDONLY | O_NONBLOCK); |
267 | if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) { |
268 | perror(path_channel); |
13474e75 |
269 | fd_pairs->num_pairs--; |
270 | continue; |
9e7dbe44 |
271 | } |
272 | /* Open the trace in write mode, only append if append_mode */ |
273 | ret = stat(path_trace, &stat_buf); |
274 | if(ret == 0) { |
275 | if(append_mode) { |
251b5c5e |
276 | printf("Appending to file %s as requested\n", path_trace); |
9e7dbe44 |
277 | |
278 | fd_pairs->pair[fd_pairs->num_pairs-1].trace = |
279 | open(path_trace, O_WRONLY|O_APPEND, |
280 | S_IRWXU|S_IRWXG|S_IRWXO); |
281 | |
282 | if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { |
283 | perror(path_trace); |
284 | } |
285 | } else { |
286 | printf("File %s exists, cannot open. Try append mode.\n", path_trace); |
02ba971e |
287 | open_ret = -1; |
288 | goto end; |
9e7dbe44 |
289 | } |
290 | } else { |
291 | if(errno == ENOENT) { |
292 | fd_pairs->pair[fd_pairs->num_pairs-1].trace = |
293 | open(path_trace, O_WRONLY|O_CREAT|O_EXCL, |
294 | S_IRWXU|S_IRWXG|S_IRWXO); |
295 | if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) { |
296 | perror(path_trace); |
297 | } |
298 | } |
299 | } |
6cecb9cf |
300 | } |
6cecb9cf |
301 | } |
302 | |
02ba971e |
303 | end: |
6cecb9cf |
304 | closedir(channel_dir); |
305 | |
02ba971e |
306 | return open_ret; |
6cecb9cf |
307 | } |
308 | |
af4061b2 |
309 | |
310 | int read_subbuffer(struct fd_pair *pair) |
311 | { |
12bcec97 |
312 | unsigned int consumed_old; |
22acb617 |
313 | int err, ret=0; |
af4061b2 |
314 | |
315 | |
13474e75 |
316 | err = ioctl(pair->channel, RELAYFS_GET_SUBBUF, |
12bcec97 |
317 | &consumed_old); |
318 | printf("cookie : %u\n", consumed_old); |
13474e75 |
319 | if(err != 0) { |
22acb617 |
320 | ret = errno; |
b8d28629 |
321 | perror("Reserving sub buffer failed (everything is normal)"); |
13474e75 |
322 | goto get_error; |
af4061b2 |
323 | } |
13474e75 |
324 | |
325 | err = TEMP_FAILURE_RETRY(write(pair->trace, |
2312de30 |
326 | pair->mmap |
327 | + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)), |
af4061b2 |
328 | pair->subbuf_size)); |
13474e75 |
329 | |
330 | if(err < 0) { |
22acb617 |
331 | ret = errno; |
af4061b2 |
332 | perror("Error in writing to file"); |
13474e75 |
333 | goto write_error; |
af4061b2 |
334 | } |
335 | |
336 | |
13474e75 |
337 | write_error: |
12bcec97 |
338 | err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF, &consumed_old); |
13474e75 |
339 | if(err != 0) { |
22acb617 |
340 | ret = errno; |
12bcec97 |
341 | if(errno == -EFAULT) { |
22acb617 |
342 | perror("Error in unreserving sub buffer\n"); |
12bcec97 |
343 | } else if(errno == -EIO) { |
344 | perror("Reader has been pushed by the writer, last subbuffer corrupted."); |
db9d75c0 |
345 | /* FIXME : we may delete the last written buffer if we wish. */ |
12bcec97 |
346 | } |
13474e75 |
347 | goto get_error; |
af4061b2 |
348 | } |
349 | |
13474e75 |
350 | get_error: |
351 | return ret; |
af4061b2 |
352 | } |
353 | |
354 | |
6cecb9cf |
355 | |
c3e8c24f |
356 | int map_channels(struct channel_trace_fd *fd_pairs) |
6cecb9cf |
357 | { |
af4061b2 |
358 | int i,j; |
c3e8c24f |
359 | int ret=0; |
af4061b2 |
360 | |
13474e75 |
361 | if(fd_pairs->num_pairs <= 0) { |
362 | printf("No channel to read\n"); |
363 | goto end; |
364 | } |
365 | |
af4061b2 |
366 | /* Get the subbuf sizes and number */ |
367 | |
368 | for(i=0;i<fd_pairs->num_pairs;i++) { |
369 | struct fd_pair *pair = &fd_pairs->pair[i]; |
9e7dbe44 |
370 | |
af4061b2 |
371 | ret = ioctl(pair->channel, RELAYFS_GET_N_SUBBUFS, |
372 | &pair->n_subbufs); |
373 | if(ret != 0) { |
374 | perror("Error in getting the number of subbuffers"); |
375 | goto end; |
376 | } |
377 | ret = ioctl(pair->channel, RELAYFS_GET_SUBBUF_SIZE, |
378 | &pair->subbuf_size); |
379 | if(ret != 0) { |
380 | perror("Error in getting the size of the subbuffers"); |
381 | goto end; |
382 | } |
22acb617 |
383 | ret = pthread_mutex_init(&pair->mutex, NULL); /* Fast mutex */ |
384 | if(ret != 0) { |
385 | perror("Error in mutex init"); |
386 | goto end; |
387 | } |
af4061b2 |
388 | } |
389 | |
390 | /* Mmap each FD */ |
391 | for(i=0;i<fd_pairs->num_pairs;i++) { |
392 | struct fd_pair *pair = &fd_pairs->pair[i]; |
393 | |
394 | pair->mmap = mmap(0, pair->subbuf_size * pair->n_subbufs, PROT_READ, |
395 | MAP_SHARED, pair->channel, 0); |
396 | if(pair->mmap == MAP_FAILED) { |
397 | perror("Mmap error"); |
398 | goto munmap; |
399 | } |
400 | } |
401 | |
22acb617 |
402 | goto end; /* success */ |
af4061b2 |
403 | |
c3e8c24f |
404 | /* Error handling */ |
405 | /* munmap only the successfully mmapped indexes */ |
406 | munmap: |
407 | /* Munmap each FD */ |
408 | for(j=0;j<i;j++) { |
409 | struct fd_pair *pair = &fd_pairs->pair[j]; |
410 | int err_ret; |
411 | |
412 | err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); |
413 | if(err_ret != 0) { |
414 | perror("Error in munmap"); |
415 | } |
416 | ret |= err_ret; |
417 | } |
418 | |
419 | end: |
420 | return ret; |
421 | |
422 | |
423 | } |
424 | |
425 | |
426 | int unmap_channels(struct channel_trace_fd *fd_pairs) |
427 | { |
428 | int j; |
429 | int ret=0; |
430 | |
431 | /* Munmap each FD */ |
432 | for(j=0;j<fd_pairs->num_pairs;j++) { |
433 | struct fd_pair *pair = &fd_pairs->pair[j]; |
434 | int err_ret; |
435 | |
436 | err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); |
437 | if(err_ret != 0) { |
438 | perror("Error in munmap"); |
439 | } |
440 | ret |= err_ret; |
22acb617 |
441 | err_ret = pthread_mutex_destroy(&pair->mutex); |
442 | if(err_ret != 0) { |
443 | perror("Error in mutex destroy"); |
444 | } |
445 | ret |= err_ret; |
c3e8c24f |
446 | } |
447 | |
448 | return ret; |
449 | } |
450 | |
451 | |
452 | /* read_channels |
22acb617 |
453 | * |
454 | * Thread worker. |
c3e8c24f |
455 | * |
456 | * Read the relayfs channels and write them in the paired tracefiles. |
457 | * |
458 | * @fd_pairs : paired channels and trace files. |
459 | * |
22acb617 |
460 | * returns (void*)0 on success, (void*)-1 on error. |
c3e8c24f |
461 | * |
462 | * Note that the high priority polled channels are consumed first. We then poll |
463 | * again to see if these channels are still in priority. Only when no |
464 | * high priority channel is left, we start reading low priority channels. |
465 | * |
466 | * Note that a channel is considered high priority when the buffer is almost |
467 | * full. |
468 | */ |
469 | |
22acb617 |
470 | void * read_channels(void *arg) |
c3e8c24f |
471 | { |
472 | struct pollfd *pollfd; |
473 | int i,j; |
474 | int num_rdy, num_hup; |
475 | int high_prio; |
22acb617 |
476 | int ret = 0; |
477 | struct channel_trace_fd *fd_pairs = (struct channel_trace_fd *)arg; |
c3e8c24f |
478 | |
af4061b2 |
479 | /* Start polling the FD */ |
480 | |
9e7dbe44 |
481 | pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd)); |
482 | |
af4061b2 |
483 | /* Note : index in pollfd is the same index as fd_pair->pair */ |
9e7dbe44 |
484 | for(i=0;i<fd_pairs->num_pairs;i++) { |
485 | pollfd[i].fd = fd_pairs->pair[i].channel; |
486 | pollfd[i].events = POLLIN|POLLPRI; |
487 | } |
488 | |
489 | while(1) { |
68047576 |
490 | high_prio = 0; |
af4061b2 |
491 | num_hup = 0; |
492 | #ifdef DEBUG |
493 | printf("Press a key for next poll...\n"); |
494 | char buf[1]; |
495 | read(STDIN_FILENO, &buf, 1); |
13474e75 |
496 | printf("Next poll (polling %d fd) :\n", fd_pairs->num_pairs); |
af4061b2 |
497 | #endif //DEBUG |
498 | |
499 | /* Have we received a signal ? */ |
500 | if(quit_program) break; |
501 | |
9e7dbe44 |
502 | num_rdy = poll(pollfd, fd_pairs->num_pairs, -1); |
503 | if(num_rdy == -1) { |
504 | perror("Poll error"); |
af4061b2 |
505 | goto free_fd; |
9e7dbe44 |
506 | } |
507 | |
508 | printf("Data received\n"); |
509 | |
510 | for(i=0;i<fd_pairs->num_pairs;i++) { |
511 | switch(pollfd[i].revents) { |
512 | case POLLERR: |
513 | printf("Error returned in polling fd %d.\n", pollfd[i].fd); |
af4061b2 |
514 | num_hup++; |
9e7dbe44 |
515 | break; |
516 | case POLLHUP: |
517 | printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd); |
af4061b2 |
518 | num_hup++; |
9e7dbe44 |
519 | break; |
520 | case POLLNVAL: |
521 | printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd); |
af4061b2 |
522 | num_hup++; |
9e7dbe44 |
523 | break; |
524 | case POLLPRI: |
22acb617 |
525 | if(pthread_mutex_trylock(&fd_pairs->pair[i].mutex) == 0) { |
526 | printf("Urgent read on fd %d\n", pollfd[i].fd); |
527 | /* Take care of high priority channels first. */ |
528 | high_prio = 1; |
529 | /* it's ok to have an unavailable subbuffer */ |
530 | ret = read_subbuffer(&fd_pairs->pair[i]); |
531 | if(ret == -EAGAIN) ret = 0; |
b8d28629 |
532 | |
22acb617 |
533 | ret = pthread_mutex_unlock(&fd_pairs->pair[i].mutex); |
534 | if(ret) |
535 | printf("Error in mutex unlock : %s\n", strerror(ret)); |
536 | } |
9e7dbe44 |
537 | break; |
68047576 |
538 | } |
9e7dbe44 |
539 | } |
af4061b2 |
540 | /* If every FD has hung up, we end the read loop here */ |
541 | if(num_hup == fd_pairs->num_pairs) break; |
9e7dbe44 |
542 | |
68047576 |
543 | if(!high_prio) { |
544 | for(i=0;i<fd_pairs->num_pairs;i++) { |
545 | switch(pollfd[i].revents) { |
546 | case POLLIN: |
22acb617 |
547 | if(pthread_mutex_trylock(&fd_pairs->pair[i].mutex) == 0) { |
548 | /* Take care of low priority channels. */ |
549 | printf("Normal read on fd %d\n", pollfd[i].fd); |
550 | /* it's ok to have an unavailable subbuffer */ |
551 | ret = read_subbuffer(&fd_pairs->pair[i]); |
552 | if(ret == -EAGAIN) ret = 0; |
b8d28629 |
553 | |
22acb617 |
554 | ret = pthread_mutex_unlock(&fd_pairs->pair[i].mutex); |
555 | if(ret) |
556 | printf("Error in mutex unlock : %s\n", strerror(ret)); |
557 | } |
68047576 |
558 | break; |
559 | } |
560 | } |
9e7dbe44 |
561 | } |
562 | |
563 | } |
564 | |
af4061b2 |
565 | free_fd: |
9e7dbe44 |
566 | free(pollfd); |
567 | |
af4061b2 |
568 | end: |
22acb617 |
569 | return (void*)ret; |
6cecb9cf |
570 | } |
571 | |
572 | |
573 | void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs) |
574 | { |
9e7dbe44 |
575 | int i; |
576 | int ret; |
6cecb9cf |
577 | |
9e7dbe44 |
578 | for(i=0;i<fd_pairs->num_pairs;i++) { |
579 | ret = close(fd_pairs->pair[i].channel); |
580 | if(ret == -1) perror("Close error on channel"); |
581 | ret = close(fd_pairs->pair[i].trace); |
582 | if(ret == -1) perror("Close error on trace"); |
583 | } |
584 | free(fd_pairs->pair); |
6cecb9cf |
585 | } |
586 | |
587 | int main(int argc, char ** argv) |
588 | { |
c3e8c24f |
589 | int ret = 0; |
6cecb9cf |
590 | struct channel_trace_fd fd_pairs = { NULL, 0 }; |
af4061b2 |
591 | struct sigaction act; |
22acb617 |
592 | pthread_t *tids; |
593 | unsigned int i; |
594 | void *tret; |
6cecb9cf |
595 | |
596 | ret = parse_arguments(argc, argv); |
597 | |
598 | if(ret != 0) show_arguments(); |
599 | if(ret < 0) return EINVAL; |
600 | if(ret > 0) return 0; |
601 | |
602 | show_info(); |
603 | |
604 | if(daemon_mode) { |
4d20e474 |
605 | ret = daemon(0, 0); |
606 | |
607 | if(ret == -1) { |
608 | perror("An error occured while daemonizing."); |
609 | exit(-1); |
610 | } |
611 | } |
6cecb9cf |
612 | |
af4061b2 |
613 | /* Connect the signal handlers */ |
614 | act.sa_handler = handler; |
615 | act.sa_flags = 0; |
616 | sigemptyset(&(act.sa_mask)); |
617 | sigaddset(&(act.sa_mask), SIGTERM); |
618 | sigaddset(&(act.sa_mask), SIGQUIT); |
619 | sigaddset(&(act.sa_mask), SIGINT); |
620 | sigaction(SIGTERM, &act, NULL); |
621 | sigaction(SIGQUIT, &act, NULL); |
622 | sigaction(SIGINT, &act, NULL); |
623 | |
c3e8c24f |
624 | |
6cecb9cf |
625 | if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs)) |
9e7dbe44 |
626 | goto close_channel; |
6cecb9cf |
627 | |
c3e8c24f |
628 | if(ret = map_channels(&fd_pairs)) |
629 | goto close_channel; |
630 | |
22acb617 |
631 | tids = malloc(sizeof(pthread_t) * num_threads); |
632 | for(i=0; i<num_threads; i++) { |
633 | ret = pthread_create(&tids[i], NULL, read_channels, &fd_pairs); |
634 | if(ret) { |
635 | perror("Error creating thread"); |
636 | break; |
637 | } |
638 | } |
6cecb9cf |
639 | |
22acb617 |
640 | for(i=0; i<num_threads; i++) { |
641 | ret = pthread_join(tids[i], &tret); |
642 | if(ret) { |
643 | perror("Error joining thread"); |
644 | break; |
645 | } |
646 | if((int)tret != 0) { |
647 | printf("Error %s occured in thread %u\n", strerror(-(int)tret), i); |
648 | } |
649 | } |
650 | |
651 | free(tids); |
652 | |
c3e8c24f |
653 | ret |= unmap_channels(&fd_pairs); |
654 | |
9e7dbe44 |
655 | close_channel: |
6cecb9cf |
656 | close_channel_trace_pairs(&fd_pairs); |
657 | |
6cecb9cf |
658 | return ret; |
659 | } |