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