[flow-tools] flow-capture post rotate exec patch - correction

Mark Fullmer maf@eng.oar.net
Mon, 22 Oct 2001 09:58:58 -0400


I reworked the patch a little bit to get rid of the
double fork so flow-capture could report a non 0 exit
status of the child.

Index: flow-capture.c
===================================================================
RCS file: /usr/home/djnz-cvsroot/flow-tools/src/flow-capture.c,v
retrieving revision 1.49
retrieving revision 1.48
diff -c -r1.49 -r1.48
*** flow-capture.c	2001/10/21 16:54:34	1.49
--- flow-capture.c	2001/10/20 19:33:01	1.48
***************
*** 23,29 ****
   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   *
!  *      $Id: flow-capture.c,v 1.49 2001/10/21 16:54:34 maf Exp $
   */
  
  #if HAVE_CONFIG_H
--- 23,29 ----
   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   *
!  *      $Id: flow-capture.c,v 1.48 2001/10/20 19:33:01 maf Exp $
   */
  
  #if HAVE_CONFIG_H
***************
*** 37,43 ****
  #include <sys/socket.h>
  #include <sys/param.h>
  #include <sys/stat.h>
- #include <sys/wait.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  #include <unistd.h>
--- 37,42 ----
***************
*** 64,69 ****
--- 63,70 ----
  
  /* TODO
   *
+  *  call external program on file rotation
+  *
   *  The TCP client write() needs to have userland buffering
   *
   *  fix the client
***************
*** 120,130 ****
  };
  
  int debug;
! int sig_pipe_flag, sig_quit_flag, sig_hup_flag, sig_chld_counter;
  void sig_pipe();
  void sig_quit();
  void sig_hup();
- void sig_chld();
  char *progname = "flow-capture\0";
  pid_t pid;
  struct ftnet ftnet;
--- 121,130 ----
  };
  
  int debug;
! int sig_pipe_flag, sig_quit_flag, sig_hup_flag;
  void sig_pipe();
  void sig_quit();
  void sig_hup();
  char *progname = "flow-capture\0";
  pid_t pid;
  struct ftnet ftnet;
***************
*** 160,181 ****
    struct rotate rot;
    struct file cap_file;
    struct ftipmask ftipmask;
-   pid_t child_pid;
    time_t now;
!   char work_dir[MAXPATHLEN+1], post_rotate_exec[MAXPATHLEN+1];
    int i, n, len, invalid, enable_unlink, offset, detach, nest, one;
    unsigned int v1, v2;
    u_int32 privacy_mask, hash;
    char fmt_src_ip[32], fmt_dst_ip[32], fmt_dst_port[32];
    char out_rec[FT_IO_MAXREC];
!   int stat_interval, stat_next, child_status;
  
    bzero (&rot, sizeof rot);
    bzero (&cap_file, sizeof cap_file);
    bzero (&ftnet, sizeof ftnet);
    bzero (&tv, sizeof tv);
    bzero (&work_dir, sizeof work_dir);
-   bzero (&post_rotate_exec, sizeof post_rotate_exec);
    bzero (&fte, sizeof fte);
    bzero (&client, sizeof client);
    bzero (&ftpdu, sizeof ftpdu);
--- 160,179 ----
    struct rotate rot;
    struct file cap_file;
    struct ftipmask ftipmask;
    time_t now;
!   char work_dir[MAXPATHLEN+1];
    int i, n, len, invalid, enable_unlink, offset, detach, nest, one;
    unsigned int v1, v2;
    u_int32 privacy_mask, hash;
    char fmt_src_ip[32], fmt_dst_ip[32], fmt_dst_port[32];
    char out_rec[FT_IO_MAXREC];
!   int stat_interval, stat_next;
  
    bzero (&rot, sizeof rot);
    bzero (&cap_file, sizeof cap_file);
    bzero (&ftnet, sizeof ftnet);
    bzero (&tv, sizeof tv);
    bzero (&work_dir, sizeof work_dir);
    bzero (&fte, sizeof fte);
    bzero (&client, sizeof client);
    bzero (&ftpdu, sizeof ftpdu);
***************
*** 218,224 ****
    /* year/month/day nesting */
    nest = 3;
  
!   while ((i = getopt(argc, argv, "A:b:c:C:d:De:E:hm:n:N:S:V:w:z:R:")) != -1)
    
      switch (i) {
  
--- 216,222 ----
    /* year/month/day nesting */
    nest = 3;
  
!   while ((i = getopt(argc, argv, "A:b:c:C:d:De:E:hm:n:N:S:V:w:z:")) != -1)
    
      switch (i) {
  
***************
*** 284,295 ****
          fterr_errx(1, "-3 <= nesting level <= 3\n");
        break;
  
-     case 'R': /* Post rotate exec */
-       if (strlen(optarg) > MAXPATHLEN)
-         fterr_errx(1, "Post rotate argument too long");
-       strcpy(post_rotate_exec,optarg);
-       break;
- 
      case 'S': /* stat interval */
        stat_interval = atoi(optarg);
        if ((stat_interval < 0) || (stat_interval > 60))
--- 282,287 ----
***************
*** 407,422 ****
     */
  
    if (signal(SIGPIPE, sig_pipe) < 0)
!     fterr_err(1, "signal(SIGPIPE)");
  
    if (signal(SIGHUP, sig_hup) < 0)
!     fterr_err(1, "signal(SIGHUP)");
  
    if (signal(SIGQUIT, sig_quit) < 0)
!     fterr_err(1, "signal(SIGQUIT)");
! 
!   if (signal(SIGCHLD, sig_chld) < 0)
!     fterr_err(1, "signal(SIGCHLD)");
  
    /* rotations needs to divide the day evenly */
    if (rot.n && (86400 % (rot.n+1)))
--- 399,411 ----
     */
  
    if (signal(SIGPIPE, sig_pipe) < 0)
!     fterr_err(1, "signal()");
  
    if (signal(SIGHUP, sig_hup) < 0)
!     fterr_err(1, "signal()");
  
    if (signal(SIGQUIT, sig_quit) < 0)
!     fterr_err(1, "signal()");
  
    /* rotations needs to divide the day evenly */
    if (rot.n && (86400 % (rot.n+1)))
***************
*** 581,617 ****
  
      bzero (&tv, sizeof tv);
      tv.tv_sec = SELECT_TIMEOUT;
- 
-     now = time((time_t)0L);
- 
-     /* stake the zombies */
-     while (sig_chld_counter) {
- 
-       /* one less zombie */
-       sig_chld_counter --;
- 
-       child_pid = wait3(&child_status, 0, 0);
- 
-       if (WIFEXITED(child_status)) {
- 
-         if (WEXITSTATUS(child_status))
-           fterr_warnx("Child %d exit_status=%d", (int)child_pid,
-             WEXITSTATUS(child_status));
- 
-       } else if (WIFSIGNALED(child_status)) {
- 
-         fterr_warnx("Child %d signal=%d", (int)child_pid,
-           WTERMSIG(child_status));
- 
-       } else {
  
!         fterr_warnx("PID %d exited status=%d", (int)child_pid,
!           child_status);
  
-       }
- 
-     } /* buffy */
- 
      if (stat_interval) {
  
        tm = localtime (&now);
--- 570,578 ----
  
      bzero (&tv, sizeof tv);
      tv.tv_sec = SELECT_TIMEOUT;
  
!     now = time((time_t)0L); 
  
      if (stat_interval) {
  
        tm = localtime (&now);
***************
*** 985,1011 ****
          /* debugging gets a dump of the ager */
          if (debug)
            ftfile_dump(&fte);
- 
- 	/* Do the post rotate exec */
-         if (post_rotate_exec[0]) {
- 
-           if((n = fork()) == -1) {
- 
-             fterr_err(1, "fork()");
- 
-           } else if (!n) { /* child */
  
!             n = execl(post_rotate_exec, post_rotate_exec, cap_file.nname,
!                 NULL);
! 
!             if (n == -1) 
!               fterr_err(1, "exec(%s)", post_rotate_exec);
! 
!             _exit(0);
!           } /* child */
!         } /* post rotate exec */
!         
! 	/* reset */
          bzero(&cap_file, sizeof cap_file);
  
          /* invalidate file descriptor */
--- 946,953 ----
          /* debugging gets a dump of the ager */
          if (debug)
            ftfile_dump(&fte);
  
!         /* reset */
          bzero(&cap_file, sizeof cap_file);
  
          /* invalidate file descriptor */
***************
*** 1059,1065 ****
    fprintf(stderr, " -m m  privacy mask, ie 255.255.255.0\n");
    fprintf(stderr, " -n #  file rotations per day.\n");
    fprintf(stderr, " -N #  directory nesting level.\n");
-   fprintf(stderr, " -R s  post rotate exec pathname.\n");
    fprintf(stderr, " -s #  size of files to keep before expire. (0=disable)\n"
  );
    fprintf(stderr, "       use b,K,M,G as multipliers, ie 16M for 16*1024*1024\n");
--- 1001,1006 ----
***************
*** 1095,1105 ****
  void sig_quit()
  {  
    sig_quit_flag = 1;
- }
- 
- void sig_chld()
- {  
-   sig_chld_counter ++;
  }
  
  int calc_rotate (int next, u_int32 *trotate, int *cur)
--- 1036,1041 ----

On Mon, Oct 22, 2001 at 03:05:50PM +0600, Pavel B Dudkin wrote:
> Hi,
> 
> I took the patch for post rotate execution by Stefan Stefanov and it
> didn't work for me. After a little investigation, I noticed that execl()
> call takes wrong argument list which is not NULL-terminated:
> 
> execl(post_rotate_exec,cap_file.nname);
> 
> It should be as follows:
> 
> execl(post_rotate_exec,post_rotate_exec,cap_file.nname,NULL);
> 
> Hope the right one makes it into the 0.56 :)
> 
> Pavel Dudkin, Uraltelecom
> 
> 
> 
> _______________________________________________
> flow-tools@splintered.net
> http://www.splintered.net/sw/flow-tools