home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / rotatelo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-13  |  1.8 KB  |  83 lines

  1. /*
  2.  
  3. Simple program to rotate Apache logs without having to kill the server.
  4.  
  5. Contributed by Ben Laurie <ben@algroup.co.uk>
  6.  
  7. 12 Mar 1996
  8.  
  9. */
  10.  
  11. #define BUFSIZE        65536
  12. #define MAX_PATH    1024
  13.  
  14. #include <stdio.h>
  15. #include <time.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18.  
  19. void main(int argc,char **argv)
  20.     {
  21.     char buf[BUFSIZE],buf2[MAX_PATH];
  22.     time_t tLogEnd;
  23.     time_t tRotation;
  24.     int nLogFD=-1;
  25.     int nRead;
  26.     char *szLogRoot;
  27.  
  28.     if(argc != 3)
  29.     {
  30.     fprintf(stderr,"%s <logfile> <rotation time in seconds>\n\n",argv[0]);
  31. #ifdef __EMX__
  32.     fprintf(stderr,"Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",argv[0]);
  33. #else
  34.            fprintf(stderr,"Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",argv[0]);
  35. #endif
  36.     fprintf(stderr,"to httpd.conf. The generated name will be /some/where.nnnn where nnnn is the\n");
  37.     fprintf(stderr,"system time at which the log nominally starts (N.B. this time will always be a\n");
  38.     fprintf(stderr,"multiple of the rotation time, so you can synchronize cron scripts with it).\n");
  39.     fprintf(stderr,"At the end of each rotation time a new log is started.\n");
  40.     exit(1);
  41.     }
  42.  
  43.     szLogRoot=argv[1];
  44.     tRotation=atoi(argv[2]);
  45.     if(tRotation <= 0)
  46.     {
  47.     fprintf(stderr,"Rotation time must be > 0\n");
  48.     exit(6);
  49.     }
  50.  
  51.     for( ; ; )
  52.     {
  53.     nRead=read(0,buf,sizeof buf);
  54.     if(nRead == 0)
  55.         exit(3);
  56.     if(nRead < 0)
  57.         if(errno != EINTR)
  58.         exit(4);
  59.     if(nLogFD >= 0 && (time(NULL) >= tLogEnd || nRead < 0))
  60.         {
  61.         close(nLogFD);
  62.         nLogFD=-1;
  63.         }
  64.     if(nLogFD < 0)
  65.         {
  66.         time_t tLogStart=(time(NULL)/tRotation)*tRotation;
  67.         sprintf(buf2,"%s.%d",szLogRoot,tLogStart);
  68.         tLogEnd=tLogStart+tRotation;
  69.         nLogFD=open(buf2,O_WRONLY|O_CREAT|O_APPEND,0666);
  70.         if(nLogFD < 0)
  71.         {
  72.         perror(buf2);
  73.         exit(2);
  74.         }
  75.         }
  76.     if(write(nLogFD,buf,nRead) != nRead)
  77.         {
  78.         perror(buf2);
  79.         exit(5);
  80.         }
  81.     }
  82.     }
  83.