Guest User

flog.c

a guest
Jan 31st, 2012
221
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. flog - "follow log"
  3.  
  4. Copyright (C) Marcus Ranum, 1992 All rights reserved.
  5. You may freely use this software provided you do not alter
  6. this copyright statement.
  7.  
  8. This little utility does the equivalent of "tail -f" but is smart
  9. enough to handle changing files if a log rotater moves the file out
  10. from under the user while we're running. Also, unlike "tail -f" this
  11. program is smart enough to exit when the user logs out(!) and will
  12. not spew logs to the pty forever. No effort is made at elegance or
  13. performance.
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <errno.h>
  20. extern int errno;
  21. #include <fcntl.h>
  22. #include <sys/file.h>
  23. #include <sys/time.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26.  
  27. #define DEFAULT_FILE "/usr/spool/mqueue/syslog"
  28.  
  29. #ifdef hpux
  30. #include <unistd.h>
  31. int getdtablesize()
  32. {
  33. return(sysconf(_SC_OPEN_MAX));
  34. }
  35. #endif
  36.  
  37. main(ac,av)
  38. int ac;
  39. char **av;
  40. {
  41. char rbuf[BUFSIZ];
  42. struct stat sbuf;
  43. int red;
  44. int *tb;
  45. int *sb;
  46. char **nb;
  47. off_t *ob;
  48. int max = 0;
  49. int xx;
  50. int siz;
  51.  
  52. /* file descriptor table */
  53. siz = getdtablesize();
  54. tb = (int *)malloc(sizeof(int) * siz);
  55. sb = (int *)malloc(sizeof(int) * siz);
  56. ob = (off_t *)malloc(sizeof(off_t) * siz);
  57. nb = (char **)malloc(sizeof(char *) * siz);
  58. if(nb == (char **)0 || ob == (off_t *)0 || sb == (int *)0 || tb == (int *)0) {
  59. perror("cannot malloc file descriptor table");
  60. exit(1);
  61. } else {
  62. for(xx = 0; xx < siz; xx++) {
  63. tb[xx] = -1;
  64. sb[xx] = -1;
  65. ob[xx] = 0;
  66. nb[xx] = (char *)0;
  67. }
  68. }
  69.  
  70.  
  71. /* open all the files */
  72. if(ac > 1) {
  73. for(xx = 1; xx < ac; xx++)
  74. if(openfile(av[xx],tb,sb,ob,nb,max) == 0)
  75. max++;
  76. } else {
  77. if(openfile(DEFAULT_FILE,tb,sb,ob,nb,max) == 0)
  78. max++;
  79. }
  80.  
  81. looptop:
  82. if(getppid() == 1 || max == 0)
  83. exit(0);
  84.  
  85. for(xx = 0; xx < max; xx++) {
  86. if(stat(nb[xx],&sbuf)) {
  87. if(tb[xx] != -1)
  88. (void)close(tb[xx]);
  89. tb[xx] = -1;
  90. (void)openfile(nb[xx],tb,sb,ob,nb,xx);
  91. continue;
  92. }
  93. if(sb[xx] != sbuf.st_ino) {
  94. (void)close(tb[xx]);
  95. tb[xx] = -1;
  96. (void)openfile(nb[xx],tb,sb,ob,nb,xx);
  97. }
  98. if(tb[xx] == -1)
  99. continue;
  100. while(ob[xx] < sbuf.st_size) {
  101. int t;
  102.  
  103. t = sbuf.st_size - ob[xx] > sizeof(rbuf) ? sizeof(rbuf) : sbuf.st_size - ob[xx];
  104. red = read(tb[xx],rbuf,t);
  105. if(red < 0) {
  106. (void)close(tb[xx]);
  107. tb[xx] = -1;
  108. continue;
  109. }
  110. if(red > 0) {
  111. if(write(1,rbuf,red) != red)
  112. exit(1);
  113. }
  114. ob[xx] += red;
  115. }
  116. }
  117.  
  118. sleep(3);
  119. goto looptop;
  120. }
  121.  
  122.  
  123. openfile(path,tb,sb,ob,nb,max)
  124. char *path;
  125. int *tb;
  126. int *sb;
  127. off_t *ob;
  128. char **nb;
  129. int max;
  130. {
  131. struct stat sbuf;
  132.  
  133. if(stat(path,&sbuf)) {
  134. perror(path);
  135. return(1);
  136. }
  137. if((tb[max] = open(path,O_RDONLY,0)) < 0) {
  138. perror(path);
  139. return(1);
  140. }
  141. (void)lseek(tb[max],0L,L_XTND);
  142. sb[max] = sbuf.st_ino;
  143. ob[max] = sbuf.st_size;
  144. nb[max] = path;
  145. fprintf(stderr,"flog: opened %s\n",nb[max]);
  146. return(0);
  147. }
RAW Paste Data