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