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