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; |
4d508021 |
208 | int open_ret = 0; |
6cecb9cf |
209 | |
210 | if(channel_dir == NULL) { |
211 | perror(subchannel_name); |
02ba971e |
212 | open_ret = ENOENT; |
4d508021 |
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); |
4d508021 |
221 | open_ret = -1; |
02ba971e |
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; |
4d508021 |
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; |
63bf9ed1 |
321 | perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)"); |
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 | } |
c5fdfca5 |
335 | #if 0 |
336 | err = fsync(pair->trace); |
337 | if(err < 0) { |
338 | ret = errno; |
339 | perror("Error in writing to file"); |
340 | goto write_error; |
341 | } |
342 | #endif //0 |
13474e75 |
343 | write_error: |
12bcec97 |
344 | err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF, &consumed_old); |
13474e75 |
345 | if(err != 0) { |
22acb617 |
346 | ret = errno; |
63bf9ed1 |
347 | if(errno == EFAULT) { |
22acb617 |
348 | perror("Error in unreserving sub buffer\n"); |
63bf9ed1 |
349 | } else if(errno == EIO) { |
12bcec97 |
350 | perror("Reader has been pushed by the writer, last subbuffer corrupted."); |
db9d75c0 |
351 | /* FIXME : we may delete the last written buffer if we wish. */ |
12bcec97 |
352 | } |
13474e75 |
353 | goto get_error; |
af4061b2 |
354 | } |
355 | |
13474e75 |
356 | get_error: |
357 | return ret; |
af4061b2 |
358 | } |
359 | |
360 | |
6cecb9cf |
361 | |
c3e8c24f |
362 | int map_channels(struct channel_trace_fd *fd_pairs) |
6cecb9cf |
363 | { |
af4061b2 |
364 | int i,j; |
c3e8c24f |
365 | int ret=0; |
af4061b2 |
366 | |
13474e75 |
367 | if(fd_pairs->num_pairs <= 0) { |
368 | printf("No channel to read\n"); |
369 | goto end; |
370 | } |
371 | |
af4061b2 |
372 | /* Get the subbuf sizes and number */ |
373 | |
374 | for(i=0;i<fd_pairs->num_pairs;i++) { |
375 | struct fd_pair *pair = &fd_pairs->pair[i]; |
9e7dbe44 |
376 | |
af4061b2 |
377 | ret = ioctl(pair->channel, RELAYFS_GET_N_SUBBUFS, |
378 | &pair->n_subbufs); |
379 | if(ret != 0) { |
380 | perror("Error in getting the number of subbuffers"); |
381 | goto end; |
382 | } |
383 | ret = ioctl(pair->channel, RELAYFS_GET_SUBBUF_SIZE, |
384 | &pair->subbuf_size); |
385 | if(ret != 0) { |
386 | perror("Error in getting the size of the subbuffers"); |
387 | goto end; |
388 | } |
22acb617 |
389 | ret = pthread_mutex_init(&pair->mutex, NULL); /* Fast mutex */ |
390 | if(ret != 0) { |
391 | perror("Error in mutex init"); |
392 | goto end; |
393 | } |
af4061b2 |
394 | } |
395 | |
396 | /* Mmap each FD */ |
397 | for(i=0;i<fd_pairs->num_pairs;i++) { |
398 | struct fd_pair *pair = &fd_pairs->pair[i]; |
399 | |
400 | pair->mmap = mmap(0, pair->subbuf_size * pair->n_subbufs, PROT_READ, |
401 | MAP_SHARED, pair->channel, 0); |
402 | if(pair->mmap == MAP_FAILED) { |
403 | perror("Mmap error"); |
404 | goto munmap; |
405 | } |
406 | } |
407 | |
22acb617 |
408 | goto end; /* success */ |
af4061b2 |
409 | |
c3e8c24f |
410 | /* Error handling */ |
411 | /* munmap only the successfully mmapped indexes */ |
412 | munmap: |
413 | /* Munmap each FD */ |
414 | for(j=0;j<i;j++) { |
415 | struct fd_pair *pair = &fd_pairs->pair[j]; |
416 | int err_ret; |
417 | |
418 | err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); |
419 | if(err_ret != 0) { |
420 | perror("Error in munmap"); |
421 | } |
422 | ret |= err_ret; |
423 | } |
424 | |
425 | end: |
426 | return ret; |
427 | |
428 | |
429 | } |
430 | |
431 | |
432 | int unmap_channels(struct channel_trace_fd *fd_pairs) |
433 | { |
434 | int j; |
435 | int ret=0; |
436 | |
437 | /* Munmap each FD */ |
438 | for(j=0;j<fd_pairs->num_pairs;j++) { |
439 | struct fd_pair *pair = &fd_pairs->pair[j]; |
440 | int err_ret; |
441 | |
442 | err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs); |
443 | if(err_ret != 0) { |
444 | perror("Error in munmap"); |
445 | } |
446 | ret |= err_ret; |
22acb617 |
447 | err_ret = pthread_mutex_destroy(&pair->mutex); |
448 | if(err_ret != 0) { |
449 | perror("Error in mutex destroy"); |
450 | } |
451 | ret |= err_ret; |
c3e8c24f |
452 | } |
453 | |
454 | return ret; |
455 | } |
456 | |
457 | |
458 | /* read_channels |
22acb617 |
459 | * |
460 | * Thread worker. |
c3e8c24f |
461 | * |
462 | * Read the relayfs channels and write them in the paired tracefiles. |
463 | * |
464 | * @fd_pairs : paired channels and trace files. |
465 | * |
22acb617 |
466 | * returns (void*)0 on success, (void*)-1 on error. |
c3e8c24f |
467 | * |
468 | * Note that the high priority polled channels are consumed first. We then poll |
469 | * again to see if these channels are still in priority. Only when no |
470 | * high priority channel is left, we start reading low priority channels. |
471 | * |
472 | * Note that a channel is considered high priority when the buffer is almost |
473 | * full. |
474 | */ |
475 | |
22acb617 |
476 | void * read_channels(void *arg) |
c3e8c24f |
477 | { |
478 | struct pollfd *pollfd; |
479 | int i,j; |
480 | int num_rdy, num_hup; |
481 | int high_prio; |
22acb617 |
482 | int ret = 0; |
483 | struct channel_trace_fd *fd_pairs = (struct channel_trace_fd *)arg; |
c3e8c24f |
484 | |
af4061b2 |
485 | /* Start polling the FD */ |
486 | |
9e7dbe44 |
487 | pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd)); |
488 | |
af4061b2 |
489 | /* Note : index in pollfd is the same index as fd_pair->pair */ |
9e7dbe44 |
490 | for(i=0;i<fd_pairs->num_pairs;i++) { |
491 | pollfd[i].fd = fd_pairs->pair[i].channel; |
492 | pollfd[i].events = POLLIN|POLLPRI; |
493 | } |
494 | |
495 | while(1) { |
68047576 |
496 | high_prio = 0; |
af4061b2 |
497 | num_hup = 0; |
498 | #ifdef DEBUG |
499 | printf("Press a key for next poll...\n"); |
500 | char buf[1]; |
501 | read(STDIN_FILENO, &buf, 1); |
13474e75 |
502 | printf("Next poll (polling %d fd) :\n", fd_pairs->num_pairs); |
af4061b2 |
503 | #endif //DEBUG |
504 | |
505 | /* Have we received a signal ? */ |
506 | if(quit_program) break; |
507 | |
9e7dbe44 |
508 | num_rdy = poll(pollfd, fd_pairs->num_pairs, -1); |
509 | if(num_rdy == -1) { |
510 | perror("Poll error"); |
af4061b2 |
511 | goto free_fd; |
9e7dbe44 |
512 | } |
513 | |
514 | printf("Data received\n"); |
515 | |
516 | for(i=0;i<fd_pairs->num_pairs;i++) { |
517 | switch(pollfd[i].revents) { |
518 | case POLLERR: |
519 | printf("Error returned in polling fd %d.\n", pollfd[i].fd); |
af4061b2 |
520 | num_hup++; |
9e7dbe44 |
521 | break; |
522 | case POLLHUP: |
523 | printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd); |
af4061b2 |
524 | num_hup++; |
9e7dbe44 |
525 | break; |
526 | case POLLNVAL: |
527 | printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd); |
af4061b2 |
528 | num_hup++; |
9e7dbe44 |
529 | break; |
530 | case POLLPRI: |
22acb617 |
531 | if(pthread_mutex_trylock(&fd_pairs->pair[i].mutex) == 0) { |
532 | printf("Urgent read on fd %d\n", pollfd[i].fd); |
533 | /* Take care of high priority channels first. */ |
534 | high_prio = 1; |
535 | /* it's ok to have an unavailable subbuffer */ |
536 | ret = read_subbuffer(&fd_pairs->pair[i]); |
63bf9ed1 |
537 | if(ret == EAGAIN) ret = 0; |
b8d28629 |
538 | |
22acb617 |
539 | ret = pthread_mutex_unlock(&fd_pairs->pair[i].mutex); |
540 | if(ret) |
541 | printf("Error in mutex unlock : %s\n", strerror(ret)); |
542 | } |
9e7dbe44 |
543 | break; |
68047576 |
544 | } |
9e7dbe44 |
545 | } |
af4061b2 |
546 | /* If every FD has hung up, we end the read loop here */ |
547 | if(num_hup == fd_pairs->num_pairs) break; |
9e7dbe44 |
548 | |
68047576 |
549 | if(!high_prio) { |
550 | for(i=0;i<fd_pairs->num_pairs;i++) { |
551 | switch(pollfd[i].revents) { |
552 | case POLLIN: |
22acb617 |
553 | if(pthread_mutex_trylock(&fd_pairs->pair[i].mutex) == 0) { |
554 | /* Take care of low priority channels. */ |
555 | printf("Normal read on fd %d\n", pollfd[i].fd); |
556 | /* it's ok to have an unavailable subbuffer */ |
557 | ret = read_subbuffer(&fd_pairs->pair[i]); |
63bf9ed1 |
558 | if(ret == EAGAIN) ret = 0; |
b8d28629 |
559 | |
22acb617 |
560 | ret = pthread_mutex_unlock(&fd_pairs->pair[i].mutex); |
561 | if(ret) |
562 | printf("Error in mutex unlock : %s\n", strerror(ret)); |
563 | } |
68047576 |
564 | break; |
565 | } |
566 | } |
9e7dbe44 |
567 | } |
568 | |
569 | } |
570 | |
af4061b2 |
571 | free_fd: |
9e7dbe44 |
572 | free(pollfd); |
573 | |
af4061b2 |
574 | end: |
22acb617 |
575 | return (void*)ret; |
6cecb9cf |
576 | } |
577 | |
578 | |
579 | void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs) |
580 | { |
9e7dbe44 |
581 | int i; |
582 | int ret; |
6cecb9cf |
583 | |
9e7dbe44 |
584 | for(i=0;i<fd_pairs->num_pairs;i++) { |
585 | ret = close(fd_pairs->pair[i].channel); |
586 | if(ret == -1) perror("Close error on channel"); |
587 | ret = close(fd_pairs->pair[i].trace); |
588 | if(ret == -1) perror("Close error on trace"); |
589 | } |
590 | free(fd_pairs->pair); |
6cecb9cf |
591 | } |
592 | |
593 | int main(int argc, char ** argv) |
594 | { |
c3e8c24f |
595 | int ret = 0; |
6cecb9cf |
596 | struct channel_trace_fd fd_pairs = { NULL, 0 }; |
af4061b2 |
597 | struct sigaction act; |
22acb617 |
598 | pthread_t *tids; |
599 | unsigned int i; |
600 | void *tret; |
6cecb9cf |
601 | |
602 | ret = parse_arguments(argc, argv); |
603 | |
604 | if(ret != 0) show_arguments(); |
605 | if(ret < 0) return EINVAL; |
606 | if(ret > 0) return 0; |
607 | |
608 | show_info(); |
609 | |
610 | if(daemon_mode) { |
4d20e474 |
611 | ret = daemon(0, 0); |
4d508021 |
612 | |
613 | if(ret == -1) { |
614 | perror("An error occured while daemonizing."); |
615 | exit(-1); |
616 | } |
617 | } |
6cecb9cf |
618 | |
af4061b2 |
619 | /* Connect the signal handlers */ |
620 | act.sa_handler = handler; |
621 | act.sa_flags = 0; |
622 | sigemptyset(&(act.sa_mask)); |
623 | sigaddset(&(act.sa_mask), SIGTERM); |
624 | sigaddset(&(act.sa_mask), SIGQUIT); |
625 | sigaddset(&(act.sa_mask), SIGINT); |
626 | sigaction(SIGTERM, &act, NULL); |
627 | sigaction(SIGQUIT, &act, NULL); |
628 | sigaction(SIGINT, &act, NULL); |
629 | |
c3e8c24f |
630 | |
6cecb9cf |
631 | if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs)) |
9e7dbe44 |
632 | goto close_channel; |
6cecb9cf |
633 | |
c3e8c24f |
634 | if(ret = map_channels(&fd_pairs)) |
635 | goto close_channel; |
636 | |
22acb617 |
637 | tids = malloc(sizeof(pthread_t) * num_threads); |
638 | for(i=0; i<num_threads; i++) { |
639 | ret = pthread_create(&tids[i], NULL, read_channels, &fd_pairs); |
640 | if(ret) { |
641 | perror("Error creating thread"); |
642 | break; |
643 | } |
644 | } |
6cecb9cf |
645 | |
22acb617 |
646 | for(i=0; i<num_threads; i++) { |
647 | ret = pthread_join(tids[i], &tret); |
648 | if(ret) { |
649 | perror("Error joining thread"); |
650 | break; |
651 | } |
652 | if((int)tret != 0) { |
c5fdfca5 |
653 | printf("Error %s occured in thread %u\n", strerror((int)tret), i); |
22acb617 |
654 | } |
655 | } |
656 | |
657 | free(tids); |
658 | |
c3e8c24f |
659 | ret |= unmap_channels(&fd_pairs); |
660 | |
9e7dbe44 |
661 | close_channel: |
6cecb9cf |
662 | close_channel_trace_pairs(&fd_pairs); |
663 | |
6cecb9cf |
664 | return ret; |
665 | } |