lttd LFS ok
[ltt-control.git] / ltt / branches / poly / lttd / lttd.c
CommitLineData
617de8e1 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
1d483eea 13#define _GNU_SOURCE
469206ed 14#include <config.h>
617de8e1 15#include <stdio.h>
1d483eea 16#include <unistd.h>
617de8e1 17#include <errno.h>
18#include <sys/types.h>
19#include <sys/stat.h>
617de8e1 20#include <stdlib.h>
21#include <dirent.h>
22#include <string.h>
90ccaa9a 23#include <fcntl.h>
24#include <sys/poll.h>
1d483eea 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
617de8e1 42
43enum {
44 GET_SUBBUF,
45 PUT_SUBBUF,
46 GET_N_BUBBUFS,
47 GET_SUBBUF_SIZE
48};
49
50struct fd_pair {
51 int channel;
52 int trace;
1d483eea 53 unsigned int n_subbufs;
54 unsigned int subbuf_size;
55 void *mmap;
617de8e1 56};
57
58struct channel_trace_fd {
59 struct fd_pair *pair;
60 int num_pairs;
61};
62
63static char *trace_name = NULL;
64static char *channel_name = NULL;
65static int daemon_mode = 0;
90ccaa9a 66static int append_mode = 0;
469206ed 67static int sig_parent = 0;
1d483eea 68volatile static int quit_program = 0; /* For signal handler */
617de8e1 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).
90ccaa9a 75 * -a Trace append mode.
469206ed 76 * -s Send SIGIO to parent when ready for IO.
617de8e1 77 */
78void 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");
90ccaa9a 86 printf("-a Append to an possibly existing trace.\n");
469206ed 87 printf("-s Send SIGIO to parent when ready for IO.\n");
617de8e1 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 */
99int 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
90ccaa9a 110 while(argn < argc) {
617de8e1 111
112 switch(argv[argn][0]) {
113 case '-':
114 switch(argv[argn][1]) {
115 case 't':
90ccaa9a 116 if(argn+1 < argc) {
117 trace_name = argv[argn+1];
118 argn++;
119 }
617de8e1 120 break;
121 case 'c':
90ccaa9a 122 if(argn+1 < argc) {
123 channel_name = argv[argn+1];
124 argn++;
125 }
617de8e1 126 break;
127 case 'd':
128 daemon_mode = 1;
129 break;
90ccaa9a 130 case 'a':
131 append_mode = 1;
132 break;
469206ed 133 case 's':
134 sig_parent = 1;
135 break;
617de8e1 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
165void 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
1d483eea 175/* signal handling */
176
177static void handler(int signo)
178{
179 printf("Signal %d received : exiting cleanly\n", signo);
180 quit_program = 1;
181}
182
183
184
617de8e1 185int 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
617de8e1 204 printf("Creating trace subdirectory %s\n", subtrace_name);
205 ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
206 if(ret == -1) {
90ccaa9a 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 }
617de8e1 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;
90ccaa9a 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);
469206ed 258 fd_pairs->num_pairs--;
259 continue;
90ccaa9a 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) {
3357d360 265 printf("Appending to file %s as requested\n", path_trace);
90ccaa9a 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 }
617de8e1 288 }
617de8e1 289 }
290
291 closedir(channel_dir);
292
293 return 0;
294}
295
1d483eea 296
297int read_subbuffer(struct fd_pair *pair)
298{
299 unsigned int subbuf_index;
469206ed 300 int err, ret;
1d483eea 301
302
469206ed 303 err = ioctl(pair->channel, RELAYFS_GET_SUBBUF,
1d483eea 304 &subbuf_index);
469206ed 305 printf("index : %u\n", subbuf_index);
306 if(err != 0) {
1d483eea 307 perror("Error in reserving sub buffer");
469206ed 308 ret = -EPERM;
309 goto get_error;
1d483eea 310 }
469206ed 311
312 err = TEMP_FAILURE_RETRY(write(pair->trace,
1d483eea 313 pair->mmap + (subbuf_index * pair->subbuf_size),
314 pair->subbuf_size));
469206ed 315
316 if(err < 0) {
1d483eea 317 perror("Error in writing to file");
469206ed 318 ret = err;
319 goto write_error;
1d483eea 320 }
321
322
469206ed 323write_error:
324 err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF);
325 if(err != 0) {
1d483eea 326 perror("Error in unreserving sub buffer");
469206ed 327 ret = -EPERM;
328 goto get_error;
1d483eea 329 }
330
469206ed 331get_error:
332 return ret;
1d483eea 333}
334
335
90ccaa9a 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 */
617de8e1 351
352int read_channels(struct channel_trace_fd *fd_pairs)
353{
90ccaa9a 354 struct pollfd *pollfd;
1d483eea 355 int i,j;
356 int num_rdy, num_hup;
4f45ea55 357 int high_prio;
1d483eea 358 int ret;
359
469206ed 360 if(fd_pairs->num_pairs <= 0) {
361 printf("No channel to read\n");
362 goto end;
363 }
364
1d483eea 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];
90ccaa9a 369
1d483eea 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
90ccaa9a 399 pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd));
400
1d483eea 401 /* Note : index in pollfd is the same index as fd_pair->pair */
90ccaa9a 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
469206ed 407 /* Signal the parent that ready for IO */
408 if(sig_parent) kill(getppid(), SIGIO);
409
90ccaa9a 410 while(1) {
4f45ea55 411 high_prio = 0;
1d483eea 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);
469206ed 417 printf("Next poll (polling %d fd) :\n", fd_pairs->num_pairs);
1d483eea 418#endif //DEBUG
419
420 /* Have we received a signal ? */
421 if(quit_program) break;
422
90ccaa9a 423 num_rdy = poll(pollfd, fd_pairs->num_pairs, -1);
424 if(num_rdy == -1) {
425 perror("Poll error");
1d483eea 426 goto free_fd;
90ccaa9a 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);
1d483eea 435 num_hup++;
90ccaa9a 436 break;
437 case POLLHUP:
438 printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
1d483eea 439 num_hup++;
90ccaa9a 440 break;
441 case POLLNVAL:
442 printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
1d483eea 443 num_hup++;
90ccaa9a 444 break;
445 case POLLPRI:
1d483eea 446 printf("Urgent read on fd %d\n", pollfd[i].fd);
90ccaa9a 447 /* Take care of high priority channels first. */
4f45ea55 448 high_prio = 1;
1d483eea 449 ret |= read_subbuffer(&fd_pairs->pair[i]);
90ccaa9a 450 break;
4f45ea55 451 }
90ccaa9a 452 }
1d483eea 453 /* If every FD has hung up, we end the read loop here */
454 if(num_hup == fd_pairs->num_pairs) break;
90ccaa9a 455
4f45ea55 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. */
1d483eea 461 printf("Normal read on fd %d\n", pollfd[i].fd);
462 ret |= read_subbuffer(&fd_pairs->pair[i]);
4f45ea55 463 break;
464 }
465 }
90ccaa9a 466 }
467
468 }
469
1d483eea 470free_fd:
90ccaa9a 471 free(pollfd);
472
1d483eea 473 /* munmap only the successfully mmapped indexes */
474 i = fd_pairs->num_pairs;
475munmap:
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
488end:
489 return ret;
617de8e1 490}
491
492
493void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs)
494{
90ccaa9a 495 int i;
496 int ret;
617de8e1 497
90ccaa9a 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);
617de8e1 505}
506
507int main(int argc, char ** argv)
508{
509 int ret;
510 pid_t pid;
511 struct channel_trace_fd fd_pairs = { NULL, 0 };
1d483eea 512 struct sigaction act;
617de8e1 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
1d483eea 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;
617de8e1 548 if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs))
90ccaa9a 549 goto close_channel;
617de8e1 550
551 ret = read_channels(&fd_pairs);
552
90ccaa9a 553close_channel:
617de8e1 554 close_channel_trace_pairs(&fd_pairs);
555
617de8e1 556 return ret;
557}
This page took 0.056387 seconds and 4 git commands to generate.