[pkg-java] r3308 - trunk/commons-daemon/src/native/unix/native
Marcus Better
marcusb-guest at alioth.debian.org
Mon Apr 23 13:18:23 UTC 2007
Author: marcusb-guest
Date: 2007-04-23 13:18:23 +0000 (Mon, 23 Apr 2007)
New Revision: 3308
Modified:
trunk/commons-daemon/src/native/unix/native/jsvc-unix.c
Log:
Working implementation of syslogging.
Modified: trunk/commons-daemon/src/native/unix/native/jsvc-unix.c
===================================================================
--- trunk/commons-daemon/src/native/unix/native/jsvc-unix.c 2007-04-23 12:08:28 UTC (rev 3307)
+++ trunk/commons-daemon/src/native/unix/native/jsvc-unix.c 2007-04-23 13:18:23 UTC (rev 3308)
@@ -23,6 +23,7 @@
#include <sys/wait.h>
#include <fcntl.h>
#include <stdio.h>
+#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <syslog.h>
@@ -46,13 +47,16 @@
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 */
+pid_t logger_pid=0; /* the logger process pid */
static bool stopping=false;
static bool doreload=false;
static void (*handler_int)(int)=NULL;
static void (*handler_hup)(int)=NULL;
static void (*handler_trm)(int)=NULL;
+static int run_controller(arg_data *args, home_data *data, uid_t uid,
+ gid_t gid);
+
#ifdef OS_CYGWIN
/*
* File locking routine
@@ -621,39 +625,48 @@
#define LOGBUF_SIZE 1024
/* Read from file descriptors. Log to syslog. */
-static int logger_child (int outpipe, int errpipe, char *procname) {
+static int logger_child(int out_fd, int err_fd, 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;
+ if (out_fd > err_fd) {
+ n = out_fd + 1;
} else {
- n = errpipe + 1;
+ n = err_fd + 1;
}
openlog(procname, LOG_PID, LOG_DAEMON);
while (1) {
FD_ZERO(&rfds);
- FD_SET(outpipe, &rfds);
- FD_SET(errpipe, &rfds);
+ FD_SET(out_fd, &rfds);
+ FD_SET(err_fd, &rfds);
tv.tv_sec = 60;
tv.tv_usec = 0;
retval = select(n, &rfds, NULL, NULL, &tv);
if (retval == -1)
- perror("select()");
+ syslog(LOG_ERR, "select: %s", strerror(errno));
else if (retval) {
- if (FD_ISSET(outpipe, &rfds)) {
- read(outpipe, buf, LOGBUF_SIZE-1);
- syslog(LOG_INFO, "%s", buf);
+ if (FD_ISSET(out_fd, &rfds)) {
+ ssize_t n = read(out_fd, buf, LOGBUF_SIZE-1);
+ if (n < 0) {
+ syslog(LOG_ERR, "read: %s", strerror(errno));
+ } else if (n > 0) {
+ buf[n] = 0;
+ syslog(LOG_INFO, "%s", buf);
+ }
}
- if (FD_ISSET(errpipe, &rfds)) {
- read(errpipe, buf, LOGBUF_SIZE-1);
- syslog(LOG_ERR, "%s", buf);
+ if (FD_ISSET(err_fd, &rfds)) {
+ ssize_t n = read(err_fd, buf, LOGBUF_SIZE-1);
+ if (n < 0) {
+ syslog(LOG_ERR, "read: %s", strerror(errno));
+ } else if (n > 0) {
+ buf[n] = 0;
+ syslog(LOG_ERR, "%s", buf);
+ }
}
}
}
@@ -714,6 +727,7 @@
log_error("cannot create logger process: %s", strerror(errno));
} else {
if (pid) {
+ logger_pid = pid;
if (out_pipe[0] != 0) {
close(out_pipe[0]);
if (dup2(out_pipe[1], 1) == -1) {
@@ -729,7 +743,7 @@
}
}
} else {
- exit(logger_child(out_pipe[1], err_pipe[1], procname));
+ exit(logger_child(out_pipe[0], err_pipe[0], procname));
}
}
}
@@ -738,11 +752,10 @@
int main(int argc, char *argv[]) {
arg_data *args=NULL;
home_data *data=NULL;
- int status=0;
pid_t pid=0;
uid_t uid=0;
gid_t gid=0;
- time_t laststart;
+ int res;
/* Parse command line arguments */
args=arguments(argc,argv);
@@ -841,22 +854,38 @@
envmask = umask(0077);
set_output(args->outfile, args->errfile, args->procname);
+ res = run_controller(args, data, uid, gid);
+ if (logger_pid != 0) {
+ kill(logger_pid, SIGTERM);
+ }
+
+ return res;
+}
+
+static int run_controller(arg_data *args, home_data *data, uid_t uid,
+ gid_t gid)
+{
+ pid_t pid=0;
+
/* We have to fork: this process will become the controller and the other
will be the child */
while ((pid=fork())!=-1) {
+ time_t laststart;
+ int status=0;
+
/* We forked (again), if this is the child, we go on normally */
if (pid==0) exit(child(args,data,uid,gid));
laststart = time(NULL);
/* We are in the controller, we have to forward all interesting signals
to the child, and wait for it to die */
- controlled=pid;
+ controlled = pid;
#ifdef OS_CYGWIN
SetTerm(cygwincontroller);
#endif
- signal(SIGHUP,controller);
- signal(SIGTERM,controller);
- signal(SIGINT,controller);
+ signal(SIGHUP, controller);
+ signal(SIGTERM, controller);
+ signal(SIGINT, controller);
while (waitpid(pid,&status,0)!=pid);
@@ -906,21 +935,14 @@
/* Got out of the loop? A fork() failed then. */
log_error("Cannot decouple controller/child processes");
return(1);
-
}
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