lttd daemon complete
[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
617de8e1 14#include <stdio.h>
1d483eea 15#include <unistd.h>
617de8e1 16#include <errno.h>
17#include <sys/types.h>
18#include <sys/stat.h>
617de8e1 19#include <stdlib.h>
20#include <dirent.h>
21#include <string.h>
90ccaa9a 22#include <fcntl.h>
23#include <sys/poll.h>
1d483eea 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
617de8e1 41
42enum {
43 GET_SUBBUF,
44 PUT_SUBBUF,
45 GET_N_BUBBUFS,
46 GET_SUBBUF_SIZE
47};
48
49struct fd_pair {
50 int channel;
51 int trace;
1d483eea 52 unsigned int n_subbufs;
53 unsigned int subbuf_size;
54 void *mmap;
617de8e1 55};
56
57struct channel_trace_fd {
58 struct fd_pair *pair;
59 int num_pairs;
60};
61
62static char *trace_name = NULL;
63static char *channel_name = NULL;
64static int daemon_mode = 0;
90ccaa9a 65static int append_mode = 0;
1d483eea 66volatile static int quit_program = 0; /* For signal handler */
617de8e1 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).
90ccaa9a 73 * -a Trace append mode.
617de8e1 74 */
75void 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");
90ccaa9a 83 printf("-a Append to an possibly existing trace.\n");
617de8e1 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 */
95int 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
90ccaa9a 106 while(argn < argc) {
617de8e1 107
108 switch(argv[argn][0]) {
109 case '-':
110 switch(argv[argn][1]) {
111 case 't':
90ccaa9a 112 if(argn+1 < argc) {
113 trace_name = argv[argn+1];
114 argn++;
115 }
617de8e1 116 break;
117 case 'c':
90ccaa9a 118 if(argn+1 < argc) {
119 channel_name = argv[argn+1];
120 argn++;
121 }
617de8e1 122 break;
123 case 'd':
124 daemon_mode = 1;
125 break;
90ccaa9a 126 case 'a':
127 append_mode = 1;
128 break;
617de8e1 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
158void 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
1d483eea 168/* signal handling */
169
170static void handler(int signo)
171{
172 printf("Signal %d received : exiting cleanly\n", signo);
173 quit_program = 1;
174}
175
176
177
617de8e1 178int 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
617de8e1 197 printf("Creating trace subdirectory %s\n", subtrace_name);
198 ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
199 if(ret == -1) {
90ccaa9a 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 }
617de8e1 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;
90ccaa9a 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) {
3357d360 256 printf("Appending to file %s as requested\n", path_trace);
90ccaa9a 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 }
617de8e1 279 }
617de8e1 280 }
281
282 closedir(channel_dir);
283
284 return 0;
285}
286
1d483eea 287
288int 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
3357d360 305 if(ret < 0) {
1d483eea 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;
318error:
319 return -1;
320}
321
322
90ccaa9a 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 */
617de8e1 338
339int read_channels(struct channel_trace_fd *fd_pairs)
340{
90ccaa9a 341 struct pollfd *pollfd;
1d483eea 342 int i,j;
343 int num_rdy, num_hup;
4f45ea55 344 int high_prio;
1d483eea 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];
90ccaa9a 351
1d483eea 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
90ccaa9a 381 pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd));
382
1d483eea 383 /* Note : index in pollfd is the same index as fd_pair->pair */
90ccaa9a 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) {
4f45ea55 390 high_prio = 0;
1d483eea 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
90ccaa9a 402 num_rdy = poll(pollfd, fd_pairs->num_pairs, -1);
403 if(num_rdy == -1) {
404 perror("Poll error");
1d483eea 405 goto free_fd;
90ccaa9a 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);
1d483eea 414 num_hup++;
90ccaa9a 415 break;
416 case POLLHUP:
417 printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
1d483eea 418 num_hup++;
90ccaa9a 419 break;
420 case POLLNVAL:
421 printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
1d483eea 422 num_hup++;
90ccaa9a 423 break;
424 case POLLPRI:
1d483eea 425 printf("Urgent read on fd %d\n", pollfd[i].fd);
90ccaa9a 426 /* Take care of high priority channels first. */
4f45ea55 427 high_prio = 1;
1d483eea 428 ret |= read_subbuffer(&fd_pairs->pair[i]);
90ccaa9a 429 break;
4f45ea55 430 }
90ccaa9a 431 }
1d483eea 432 /* If every FD has hung up, we end the read loop here */
433 if(num_hup == fd_pairs->num_pairs) break;
90ccaa9a 434
4f45ea55 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. */
1d483eea 440 printf("Normal read on fd %d\n", pollfd[i].fd);
441 ret |= read_subbuffer(&fd_pairs->pair[i]);
4f45ea55 442 break;
443 }
444 }
90ccaa9a 445 }
446
447 }
448
1d483eea 449free_fd:
90ccaa9a 450 free(pollfd);
451
1d483eea 452 /* munmap only the successfully mmapped indexes */
453 i = fd_pairs->num_pairs;
454munmap:
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
467end:
468 return ret;
617de8e1 469}
470
471
472void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs)
473{
90ccaa9a 474 int i;
475 int ret;
617de8e1 476
90ccaa9a 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);
617de8e1 484}
485
486int main(int argc, char ** argv)
487{
488 int ret;
489 pid_t pid;
490 struct channel_trace_fd fd_pairs = { NULL, 0 };
1d483eea 491 struct sigaction act;
617de8e1 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
1d483eea 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;
617de8e1 527 if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs))
90ccaa9a 528 goto close_channel;
617de8e1 529
530 ret = read_channels(&fd_pairs);
531
90ccaa9a 532close_channel:
617de8e1 533 close_channel_trace_pairs(&fd_pairs);
534
617de8e1 535 return ret;
536}
This page took 0.043932 seconds and 4 git commands to generate.