[pkg-java] r2954 - trunk/commons-daemon/src/native/unix/native

Marcus Better marcusb-guest at alioth.debian.org
Wed Jan 24 09:42:38 CET 2007


Author: marcusb-guest
Date: 2007-01-24 09:42:37 +0100 (Wed, 24 Jan 2007)
New Revision: 2954

Modified:
   trunk/commons-daemon/src/native/unix/native/jsvc-unix.c
Log:
Preliminary support for redirecting stdout to syslog, not working quite yet.

Modified: trunk/commons-daemon/src/native/unix/native/jsvc-unix.c
===================================================================
--- trunk/commons-daemon/src/native/unix/native/jsvc-unix.c	2007-01-24 08:42:04 UTC (rev 2953)
+++ trunk/commons-daemon/src/native/unix/native/jsvc-unix.c	2007-01-24 08:42:37 UTC (rev 2954)
@@ -25,6 +25,8 @@
 #include <stdio.h>
 #include <pwd.h>
 #include <grp.h>
+#include <syslog.h>
+#include <errno.h>
 #ifdef OS_LINUX
 #include <sys/prctl.h>
 #include <sys/syscall.h>
@@ -44,6 +46,7 @@
 static mode_t envmask; /* mask to create the files */
 
 pid_t controlled=0; /* the son process pid */
+pid_t logger=0;     /* the logger process pid */
 static bool stopping=false;
 static bool doreload=false;
 static void (*handler_int)(int)=NULL;
@@ -615,35 +618,121 @@
     return(freopen(outfile,mode,stream));
 }
 
+#define LOGBUF_SIZE 1024
+
+/* Read from file descriptors. Log to syslog. */
+static int logger_child (int outpipe, int errpipe, char *procname) {
+    fd_set rfds;
+    struct timeval tv;
+    int retval, n;
+    char buf[LOGBUF_SIZE];
+
+    buf[LOGBUF_SIZE-1] = 0;
+
+    if (outpipe > errpipe) {
+        n = outpipe + 1;
+    } else {
+        n = errpipe + 1;
+    }
+
+    openlog(procname, LOG_PID, LOG_DAEMON);
+
+    while (1) {
+        FD_ZERO(&rfds);
+        FD_SET(outpipe, &rfds);
+        FD_SET(errpipe, &rfds);
+        tv.tv_sec = 60;
+        tv.tv_usec = 0;
+        retval = select(n, &rfds, NULL, NULL, &tv);
+        if (retval == -1)
+            perror("select()");
+        else if (retval) {
+            if (FD_ISSET(outpipe, &rfds)) {
+                read(outpipe, buf, LOGBUF_SIZE-1);
+                syslog(LOG_INFO, "%s", buf);
+            }
+            if (FD_ISSET(errpipe, &rfds)) {
+                read(errpipe, buf, LOGBUF_SIZE-1);
+                syslog(LOG_ERR, "%s", buf);
+            }
+        }
+    }
+}
+
 /**
  *  Redirect stdin, stdout, stderr.
  */
-static void set_output(char *outfile, char *errfile) {
+static void set_output(char *outfile, char *errfile, char *procname) {
+    int out_pipe[2] = {0, 0}, err_pipe[2] = {0, 0}, fork_needed = 0;
+
     freopen("/dev/null", "r", stdin); 
     log_debug("redirecting stdout to %s and stderr to %s",outfile,errfile);
 
     /* make sure the debug goes out */
     if (log_debug_flag==true && strcmp(errfile,"/dev/null") == 0)
-      return;
+        return;
 
     /* Handle malicious case here */
     if(strcmp(outfile, "&2") == 0 && strcmp(errfile,"&1") == 0) {
-      outfile="/dev/null";
+        outfile="/dev/null";
     }
-    if(strcmp(outfile, "&2") != 0) {
-      loc_freopen(outfile, "a", stdout);
+
+    if (strcmp(outfile, "SYSLOG") == 0) {
+        /* Send stdout to syslog through a logger process */
+        if (pipe(out_pipe) == -1) {
+            log_error("cannot create stdout pipe: %s",
+                      strerror(errno));
+        } else {
+            fork_needed = 1;
+        }
+    } else if (strcmp(outfile, "&2") != 0) {
+        loc_freopen(outfile, "a", stdout);
     }
 
-    if(strcmp(errfile,"&1") != 0) {
-      loc_freopen(errfile, "a", stderr);
+    if (strcmp(errfile, "SYSLOG") == 0) {
+        /* Send stderr to syslog through a logger process */
+        if (pipe(err_pipe) == -1) {
+            log_error("cannot create stderr pipe: %s",
+                      strerror(errno));
+        } else {
+            fork_needed = 1;
+        }
+    } else if (strcmp(errfile, "&1") != 0) {
+        loc_freopen(errfile, "a", stderr);
     } else {
-      close(2);
-      dup(1);
+        close(2);
+        dup(1);
     }
     if(strcmp(outfile, "&2") == 0) {
-      close(1);
-      dup(2);
+        close(1);
+        dup(2);
     }
+
+    if (fork_needed) {
+        pid_t pid = fork();
+        if (pid == -1) {
+            log_error("cannot create logger process: %s", strerror(errno));
+        } else {
+            if (pid) {
+                if (out_pipe[0] != 0) {
+                    close(out_pipe[0]);
+                    if (dup2(out_pipe[1], 1) == -1) {
+                        log_error("cannot redirect stdout to pipe for syslog: %s",
+                                  strerror(errno));
+                    }
+                }
+                if (err_pipe[0] != 0) {
+                    close(err_pipe[0]);
+                    if (dup2(err_pipe[1], 2) == -1) {
+                        log_error("cannot redirect stderr to pipe for syslog: %s",
+                                  strerror(errno));
+                    }
+                }
+            } else {
+                exit(logger_child(out_pipe[1], err_pipe[1], procname));
+            }
+        }
+    }
 }
 
 int main(int argc, char *argv[]) {
@@ -676,7 +765,7 @@
         return(0);
     }
 
-#ifdef OS_LINUX
+#if 0
     /* On some UNIX operating systems, we need to REPLACE this current
        process image with another one (thru execve) to allow the correct
        loading of VMs (notably this is for Linux). Set, replace, and go. */
@@ -750,7 +839,7 @@
     }
 
     envmask = umask(0077);
-    set_output(args->outfile, args->errfile);
+    set_output(args->outfile, args->errfile, args->procname);
 
     /* We have to fork: this process will become the controller and the other
        will be the child */
@@ -823,10 +912,15 @@
 void main_reload(void) {
     log_debug("Killing self with HUP signal");
     kill(controlled,SIGHUP);
+    if (logger != 0) {
+        kill(logger,SIGHUP);
+    }
 }
 
 void main_shutdown(void) {
     log_debug("Killing self with TERM signal");
     kill(controlled,SIGTERM);
+    if (logger != 0) {
+        kill(logger,SIGTERM);
+    }
 }
-




More information about the pkg-java-commits mailing list