daemon works
[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
197 //FIXME : check if the directory already exist, and ask the user if he wants
198 //to append to the traces.
199 printf("Creating trace subdirectory %s\n", subtrace_name);
200 ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
201 if(ret == -1) {
90ccaa9a 202 if(errno == EEXIST && append_mode) {
203 printf("Appending to directory %s as resquested\n", subtrace_name);
204 } else {
205 perror(subtrace_name);
206 return -1;
207 }
617de8e1 208 }
209
210 strncpy(path_channel, subchannel_name, PATH_MAX-1);
211 path_channel_len = strlen(path_channel);
212 path_channel[path_channel_len] = '/';
213 path_channel_len++;
214 path_channel_ptr = path_channel + path_channel_len;
215
216 strncpy(path_trace, subtrace_name, PATH_MAX-1);
217 path_trace_len = strlen(path_trace);
218 path_trace[path_trace_len] = '/';
219 path_trace_len++;
220 path_trace_ptr = path_trace + path_trace_len;
221
222 while((entry = readdir(channel_dir)) != NULL) {
223
224 if(entry->d_name[0] == '.') continue;
225
226 strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len);
227 strncpy(path_trace_ptr, entry->d_name, PATH_MAX - path_trace_len);
228
229 ret = stat(path_channel, &stat_buf);
230 if(ret == -1) {
231 perror(path_channel);
232 continue;
233 }
234
235 printf("Channel file : %s\n", path_channel);
236
237 if(S_ISDIR(stat_buf.st_mode)) {
238
239 printf("Entering channel subdirectory...\n");
240 ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs);
241 if(ret < 0) continue;
90ccaa9a 242 } else if(S_ISREG(stat_buf.st_mode)) {
243 printf("Opening file.\n");
244
245 fd_pairs->pair = realloc(fd_pairs->pair,
246 ++fd_pairs->num_pairs * sizeof(struct fd_pair));
247
248 /* Open the channel in read mode */
249 fd_pairs->pair[fd_pairs->num_pairs-1].channel =
250 open(path_channel, O_RDONLY | O_NONBLOCK);
251 if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) {
252 perror(path_channel);
253 }
254 /* Open the trace in write mode, only append if append_mode */
255 ret = stat(path_trace, &stat_buf);
256 if(ret == 0) {
257 if(append_mode) {
258 printf("Appending to file %s as resquested\n", path_trace);
259
260 fd_pairs->pair[fd_pairs->num_pairs-1].trace =
261 open(path_trace, O_WRONLY|O_APPEND,
262 S_IRWXU|S_IRWXG|S_IRWXO);
263
264 if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
265 perror(path_trace);
266 }
267 } else {
268 printf("File %s exists, cannot open. Try append mode.\n", path_trace);
269 return -1;
270 }
271 } else {
272 if(errno == ENOENT) {
273 fd_pairs->pair[fd_pairs->num_pairs-1].trace =
274 open(path_trace, O_WRONLY|O_CREAT|O_EXCL,
275 S_IRWXU|S_IRWXG|S_IRWXO);
276 if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
277 perror(path_trace);
278 }
279 }
280 }
617de8e1 281 }
617de8e1 282 }
283
284 closedir(channel_dir);
285
286 return 0;
287}
288
1d483eea 289
290int read_subbuffer(struct fd_pair *pair)
291{
292 unsigned int subbuf_index;
293 int ret;
294
295
296 ret = ioctl(pair->channel, RELAYFS_GET_SUBBUF,
297 &subbuf_index);
298 if(ret != 0) {
299 perror("Error in reserving sub buffer");
300 goto error;
301 }
302
303 ret = TEMP_FAILURE_RETRY(write(pair->trace,
304 pair->mmap + (subbuf_index * pair->subbuf_size),
305 pair->subbuf_size));
306
307 if(ret != 0) {
308 perror("Error in writing to file");
309 goto error;
310 }
311
312
313 ret = ioctl(pair->channel, RELAYFS_PUT_SUBBUF);
314 if(ret != 0) {
315 perror("Error in unreserving sub buffer");
316 goto error;
317 }
318
319 return 0;
320error:
321 return -1;
322}
323
324
90ccaa9a 325/* read_channels
326 *
327 * Read the realyfs channels and write them in the paired tracefiles.
328 *
329 * @fd_pairs : paired channels and trace files.
330 *
331 * returns 0 on success, -1 on error.
332 *
333 * Note that the high priority polled channels are consumed first. We then poll
334 * again to see if these channels are still in priority. Only when no
335 * high priority channel is left, we start reading low priority channels.
336 *
337 * Note that a channel is considered high priority when the buffer is almost
338 * full.
339 */
617de8e1 340
341int read_channels(struct channel_trace_fd *fd_pairs)
342{
90ccaa9a 343 struct pollfd *pollfd;
1d483eea 344 int i,j;
345 int num_rdy, num_hup;
4f45ea55 346 int high_prio;
1d483eea 347 int ret;
348
349 /* Get the subbuf sizes and number */
350
351 for(i=0;i<fd_pairs->num_pairs;i++) {
352 struct fd_pair *pair = &fd_pairs->pair[i];
90ccaa9a 353
1d483eea 354 ret = ioctl(pair->channel, RELAYFS_GET_N_SUBBUFS,
355 &pair->n_subbufs);
356 if(ret != 0) {
357 perror("Error in getting the number of subbuffers");
358 goto end;
359 }
360 ret = ioctl(pair->channel, RELAYFS_GET_SUBBUF_SIZE,
361 &pair->subbuf_size);
362 if(ret != 0) {
363 perror("Error in getting the size of the subbuffers");
364 goto end;
365 }
366 }
367
368 /* Mmap each FD */
369 for(i=0;i<fd_pairs->num_pairs;i++) {
370 struct fd_pair *pair = &fd_pairs->pair[i];
371
372 pair->mmap = mmap(0, pair->subbuf_size * pair->n_subbufs, PROT_READ,
373 MAP_SHARED, pair->channel, 0);
374 if(pair->mmap == MAP_FAILED) {
375 perror("Mmap error");
376 goto munmap;
377 }
378 }
379
380
381 /* Start polling the FD */
382
90ccaa9a 383 pollfd = malloc(fd_pairs->num_pairs * sizeof(struct pollfd));
384
1d483eea 385 /* Note : index in pollfd is the same index as fd_pair->pair */
90ccaa9a 386 for(i=0;i<fd_pairs->num_pairs;i++) {
387 pollfd[i].fd = fd_pairs->pair[i].channel;
388 pollfd[i].events = POLLIN|POLLPRI;
389 }
390
391 while(1) {
4f45ea55 392 high_prio = 0;
1d483eea 393 num_hup = 0;
394#ifdef DEBUG
395 printf("Press a key for next poll...\n");
396 char buf[1];
397 read(STDIN_FILENO, &buf, 1);
398 printf("Next poll :\n");
399#endif //DEBUG
400
401 /* Have we received a signal ? */
402 if(quit_program) break;
403
90ccaa9a 404 num_rdy = poll(pollfd, fd_pairs->num_pairs, -1);
405 if(num_rdy == -1) {
406 perror("Poll error");
1d483eea 407 goto free_fd;
90ccaa9a 408 }
409
410 printf("Data received\n");
411
412 for(i=0;i<fd_pairs->num_pairs;i++) {
413 switch(pollfd[i].revents) {
414 case POLLERR:
415 printf("Error returned in polling fd %d.\n", pollfd[i].fd);
1d483eea 416 num_hup++;
90ccaa9a 417 break;
418 case POLLHUP:
419 printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
1d483eea 420 num_hup++;
90ccaa9a 421 break;
422 case POLLNVAL:
423 printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
1d483eea 424 num_hup++;
90ccaa9a 425 break;
426 case POLLPRI:
1d483eea 427 printf("Urgent read on fd %d\n", pollfd[i].fd);
90ccaa9a 428 /* Take care of high priority channels first. */
4f45ea55 429 high_prio = 1;
1d483eea 430 ret |= read_subbuffer(&fd_pairs->pair[i]);
90ccaa9a 431 break;
4f45ea55 432 }
90ccaa9a 433 }
1d483eea 434 /* If every FD has hung up, we end the read loop here */
435 if(num_hup == fd_pairs->num_pairs) break;
90ccaa9a 436
4f45ea55 437 if(!high_prio) {
438 for(i=0;i<fd_pairs->num_pairs;i++) {
439 switch(pollfd[i].revents) {
440 case POLLIN:
441 /* Take care of low priority channels. */
1d483eea 442 printf("Normal read on fd %d\n", pollfd[i].fd);
443 ret |= read_subbuffer(&fd_pairs->pair[i]);
4f45ea55 444 break;
445 }
446 }
90ccaa9a 447 }
448
449 }
450
1d483eea 451free_fd:
90ccaa9a 452 free(pollfd);
453
1d483eea 454 /* munmap only the successfully mmapped indexes */
455 i = fd_pairs->num_pairs;
456munmap:
457 /* Munmap each FD */
458 for(j=0;j<i;j++) {
459 struct fd_pair *pair = &fd_pairs->pair[j];
460 int err_ret;
461
462 err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs);
463 if(err_ret != 0) {
464 perror("Error in munmap");
465 }
466 ret |= err_ret;
467 }
468
469end:
470 return ret;
617de8e1 471}
472
473
474void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs)
475{
90ccaa9a 476 int i;
477 int ret;
617de8e1 478
90ccaa9a 479 for(i=0;i<fd_pairs->num_pairs;i++) {
480 ret = close(fd_pairs->pair[i].channel);
481 if(ret == -1) perror("Close error on channel");
482 ret = close(fd_pairs->pair[i].trace);
483 if(ret == -1) perror("Close error on trace");
484 }
485 free(fd_pairs->pair);
617de8e1 486}
487
488int main(int argc, char ** argv)
489{
490 int ret;
491 pid_t pid;
492 struct channel_trace_fd fd_pairs = { NULL, 0 };
1d483eea 493 struct sigaction act;
617de8e1 494
495 ret = parse_arguments(argc, argv);
496
497 if(ret != 0) show_arguments();
498 if(ret < 0) return EINVAL;
499 if(ret > 0) return 0;
500
501 show_info();
502
503 if(daemon_mode) {
504 pid = fork();
505
506 if(pid > 0) {
507 /* parent */
508 return 0;
509 } else if(pid < 0) {
510 /* error */
511 printf("An error occured while forking.\n");
512 return -1;
513 }
514 /* else, we are the child, continue... */
515 }
516
1d483eea 517 /* Connect the signal handlers */
518 act.sa_handler = handler;
519 act.sa_flags = 0;
520 sigemptyset(&(act.sa_mask));
521 sigaddset(&(act.sa_mask), SIGTERM);
522 sigaddset(&(act.sa_mask), SIGQUIT);
523 sigaddset(&(act.sa_mask), SIGINT);
524 sigaction(SIGTERM, &act, NULL);
525 sigaction(SIGQUIT, &act, NULL);
526 sigaction(SIGINT, &act, NULL);
527
528 //return 0;
617de8e1 529 if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs))
90ccaa9a 530 goto close_channel;
617de8e1 531
532 ret = read_channels(&fd_pairs);
533
90ccaa9a 534close_channel:
617de8e1 535 close_channel_trace_pairs(&fd_pairs);
536
617de8e1 537 return ret;
538}
This page took 0.053694 seconds and 4 git commands to generate.