err val handling
[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. */
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
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;
469206ed 70static int sig_parent = 0;
1d483eea 71volatile static int quit_program = 0; /* For signal handler */
617de8e1 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).
90ccaa9a 78 * -a Trace append mode.
469206ed 79 * -s Send SIGIO to parent when ready for IO.
617de8e1 80 */
81void 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");
90ccaa9a 89 printf("-a Append to an possibly existing trace.\n");
469206ed 90 printf("-s Send SIGIO to parent when ready for IO.\n");
617de8e1 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 */
102int 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
90ccaa9a 113 while(argn < argc) {
617de8e1 114
115 switch(argv[argn][0]) {
116 case '-':
117 switch(argv[argn][1]) {
118 case 't':
90ccaa9a 119 if(argn+1 < argc) {
120 trace_name = argv[argn+1];
121 argn++;
122 }
617de8e1 123 break;
124 case 'c':
90ccaa9a 125 if(argn+1 < argc) {
126 channel_name = argv[argn+1];
127 argn++;
128 }
617de8e1 129 break;
130 case 'd':
131 daemon_mode = 1;
132 break;
90ccaa9a 133 case 'a':
134 append_mode = 1;
135 break;
469206ed 136 case 's':
137 sig_parent = 1;
138 break;
617de8e1 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
168void 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
1d483eea 178/* signal handling */
179
180static void handler(int signo)
181{
182 printf("Signal %d received : exiting cleanly\n", signo);
183 quit_program = 1;
184}
185
186
187
617de8e1 188int 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;
d304b1dd 201 int open_ret = 0;
617de8e1 202
203 if(channel_dir == NULL) {
204 perror(subchannel_name);
d304b1dd 205 open_ret = ENOENT;
206 goto end;
617de8e1 207 }
208
617de8e1 209 printf("Creating trace subdirectory %s\n", subtrace_name);
210 ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
211 if(ret == -1) {
90ccaa9a 212 if(errno == EEXIST && append_mode) {
213 printf("Appending to directory %s as resquested\n", subtrace_name);
214 } else {
215 perror(subtrace_name);
d304b1dd 216 open_ret = -1;
217 goto end;
90ccaa9a 218 }
617de8e1 219 }
220
221 strncpy(path_channel, subchannel_name, PATH_MAX-1);
222 path_channel_len = strlen(path_channel);
223 path_channel[path_channel_len] = '/';
224 path_channel_len++;
225 path_channel_ptr = path_channel + path_channel_len;
226
227 strncpy(path_trace, subtrace_name, PATH_MAX-1);
228 path_trace_len = strlen(path_trace);
229 path_trace[path_trace_len] = '/';
230 path_trace_len++;
231 path_trace_ptr = path_trace + path_trace_len;
232
233 while((entry = readdir(channel_dir)) != NULL) {
234
235 if(entry->d_name[0] == '.') continue;
236
237 strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len);
238 strncpy(path_trace_ptr, entry->d_name, PATH_MAX - path_trace_len);
239
240 ret = stat(path_channel, &stat_buf);
241 if(ret == -1) {
242 perror(path_channel);
243 continue;
244 }
245
246 printf("Channel file : %s\n", path_channel);
247
248 if(S_ISDIR(stat_buf.st_mode)) {
249
250 printf("Entering channel subdirectory...\n");
251 ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs);
252 if(ret < 0) continue;
90ccaa9a 253 } else if(S_ISREG(stat_buf.st_mode)) {
254 printf("Opening file.\n");
255
256 fd_pairs->pair = realloc(fd_pairs->pair,
257 ++fd_pairs->num_pairs * sizeof(struct fd_pair));
258
259 /* Open the channel in read mode */
260 fd_pairs->pair[fd_pairs->num_pairs-1].channel =
261 open(path_channel, O_RDONLY | O_NONBLOCK);
262 if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) {
263 perror(path_channel);
469206ed 264 fd_pairs->num_pairs--;
265 continue;
90ccaa9a 266 }
267 /* Open the trace in write mode, only append if append_mode */
268 ret = stat(path_trace, &stat_buf);
269 if(ret == 0) {
270 if(append_mode) {
3357d360 271 printf("Appending to file %s as requested\n", path_trace);
90ccaa9a 272
273 fd_pairs->pair[fd_pairs->num_pairs-1].trace =
274 open(path_trace, O_WRONLY|O_APPEND,
275 S_IRWXU|S_IRWXG|S_IRWXO);
276
277 if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
278 perror(path_trace);
279 }
280 } else {
281 printf("File %s exists, cannot open. Try append mode.\n", path_trace);
d304b1dd 282 open_ret = -1;
283 goto end;
90ccaa9a 284 }
285 } else {
286 if(errno == ENOENT) {
287 fd_pairs->pair[fd_pairs->num_pairs-1].trace =
288 open(path_trace, O_WRONLY|O_CREAT|O_EXCL,
289 S_IRWXU|S_IRWXG|S_IRWXO);
290 if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
291 perror(path_trace);
292 }
293 }
294 }
617de8e1 295 }
617de8e1 296 }
297
d304b1dd 298end:
617de8e1 299 closedir(channel_dir);
300
d304b1dd 301 return open_ret;
617de8e1 302}
303
1d483eea 304
305int read_subbuffer(struct fd_pair *pair)
306{
307 unsigned int subbuf_index;
469206ed 308 int err, ret;
1d483eea 309
310
469206ed 311 err = ioctl(pair->channel, RELAYFS_GET_SUBBUF,
1d483eea 312 &subbuf_index);
469206ed 313 printf("index : %u\n", subbuf_index);
314 if(err != 0) {
1d483eea 315 perror("Error in reserving sub buffer");
469206ed 316 ret = -EPERM;
317 goto get_error;
1d483eea 318 }
469206ed 319
320 err = TEMP_FAILURE_RETRY(write(pair->trace,
1d483eea 321 pair->mmap + (subbuf_index * pair->subbuf_size),
322 pair->subbuf_size));
469206ed 323
324 if(err < 0) {
1d483eea 325 perror("Error in writing to file");
469206ed 326 ret = err;
327 goto write_error;
1d483eea 328 }
329
330
469206ed 331write_error:
332 err = ioctl(pair->channel, RELAYFS_PUT_SUBBUF);
333 if(err != 0) {
1d483eea 334 perror("Error in unreserving sub buffer");
469206ed 335 ret = -EPERM;
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
469206ed 415 /* Signal the parent that ready for IO */
416 if(sig_parent) kill(getppid(), SIGIO);
417
90ccaa9a 418 while(1) {
4f45ea55 419 high_prio = 0;
1d483eea 420 num_hup = 0;
421#ifdef DEBUG
422 printf("Press a key for next poll...\n");
423 char buf[1];
424 read(STDIN_FILENO, &buf, 1);
469206ed 425 printf("Next poll (polling %d fd) :\n", fd_pairs->num_pairs);
1d483eea 426#endif //DEBUG
427
428 /* Have we received a signal ? */
429 if(quit_program) break;
430
90ccaa9a 431 num_rdy = poll(pollfd, fd_pairs->num_pairs, -1);
432 if(num_rdy == -1) {
433 perror("Poll error");
1d483eea 434 goto free_fd;
90ccaa9a 435 }
436
437 printf("Data received\n");
438
439 for(i=0;i<fd_pairs->num_pairs;i++) {
440 switch(pollfd[i].revents) {
441 case POLLERR:
442 printf("Error returned in polling fd %d.\n", pollfd[i].fd);
1d483eea 443 num_hup++;
90ccaa9a 444 break;
445 case POLLHUP:
446 printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
1d483eea 447 num_hup++;
90ccaa9a 448 break;
449 case POLLNVAL:
450 printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
1d483eea 451 num_hup++;
90ccaa9a 452 break;
453 case POLLPRI:
1d483eea 454 printf("Urgent read on fd %d\n", pollfd[i].fd);
90ccaa9a 455 /* Take care of high priority channels first. */
4f45ea55 456 high_prio = 1;
1d483eea 457 ret |= read_subbuffer(&fd_pairs->pair[i]);
90ccaa9a 458 break;
4f45ea55 459 }
90ccaa9a 460 }
1d483eea 461 /* If every FD has hung up, we end the read loop here */
462 if(num_hup == fd_pairs->num_pairs) break;
90ccaa9a 463
4f45ea55 464 if(!high_prio) {
465 for(i=0;i<fd_pairs->num_pairs;i++) {
466 switch(pollfd[i].revents) {
467 case POLLIN:
468 /* Take care of low priority channels. */
1d483eea 469 printf("Normal read on fd %d\n", pollfd[i].fd);
470 ret |= read_subbuffer(&fd_pairs->pair[i]);
4f45ea55 471 break;
472 }
473 }
90ccaa9a 474 }
475
476 }
477
1d483eea 478free_fd:
90ccaa9a 479 free(pollfd);
480
1d483eea 481 /* munmap only the successfully mmapped indexes */
482 i = fd_pairs->num_pairs;
483munmap:
484 /* Munmap each FD */
485 for(j=0;j<i;j++) {
486 struct fd_pair *pair = &fd_pairs->pair[j];
487 int err_ret;
488
489 err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs);
490 if(err_ret != 0) {
491 perror("Error in munmap");
492 }
493 ret |= err_ret;
494 }
495
496end:
497 return ret;
617de8e1 498}
499
500
501void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs)
502{
90ccaa9a 503 int i;
504 int ret;
617de8e1 505
90ccaa9a 506 for(i=0;i<fd_pairs->num_pairs;i++) {
507 ret = close(fd_pairs->pair[i].channel);
508 if(ret == -1) perror("Close error on channel");
509 ret = close(fd_pairs->pair[i].trace);
510 if(ret == -1) perror("Close error on trace");
511 }
512 free(fd_pairs->pair);
617de8e1 513}
514
515int main(int argc, char ** argv)
516{
517 int ret;
518 pid_t pid;
519 struct channel_trace_fd fd_pairs = { NULL, 0 };
1d483eea 520 struct sigaction act;
617de8e1 521
522 ret = parse_arguments(argc, argv);
523
524 if(ret != 0) show_arguments();
525 if(ret < 0) return EINVAL;
526 if(ret > 0) return 0;
527
528 show_info();
529
530 if(daemon_mode) {
531 pid = fork();
532
533 if(pid > 0) {
534 /* parent */
535 return 0;
536 } else if(pid < 0) {
537 /* error */
538 printf("An error occured while forking.\n");
539 return -1;
540 }
541 /* else, we are the child, continue... */
542 }
543
1d483eea 544 /* Connect the signal handlers */
545 act.sa_handler = handler;
546 act.sa_flags = 0;
547 sigemptyset(&(act.sa_mask));
548 sigaddset(&(act.sa_mask), SIGTERM);
549 sigaddset(&(act.sa_mask), SIGQUIT);
550 sigaddset(&(act.sa_mask), SIGINT);
551 sigaction(SIGTERM, &act, NULL);
552 sigaction(SIGQUIT, &act, NULL);
553 sigaction(SIGINT, &act, NULL);
554
555 //return 0;
617de8e1 556 if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs))
90ccaa9a 557 goto close_channel;
617de8e1 558
559 ret = read_channels(&fd_pairs);
560
90ccaa9a 561close_channel:
617de8e1 562 close_channel_trace_pairs(&fd_pairs);
563
617de8e1 564 return ret;
565}
This page took 0.045664 seconds and 4 git commands to generate.