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