aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/initlog.c22
-rw-r--r--src/initlog.h2
-rw-r--r--src/minilogd.c13
-rw-r--r--src/process.c12
-rw-r--r--src/process.h2
5 files changed, 33 insertions, 18 deletions
diff --git a/src/initlog.c b/src/initlog.c
index 6e6e4385..1a77bd1f 100644
--- a/src/initlog.c
+++ b/src/initlog.c
@@ -25,6 +25,7 @@ static int logfacility=LOG_DAEMON;
static int logpriority=LOG_NOTICE;
static int reexec=0;
static int quiet=0;
+int debug=0;
static int logEntries = 0;
struct logInfo *logData = NULL;
@@ -87,8 +88,10 @@ int startDaemon() {
if ( pid ) {
/* parent */
waitpid(pid,&rc,0);
- if (WIFEXITED(rc))
+ if (WIFEXITED(rc)) {
+ DDEBUG("minilogd returned %d!\n",WEXITSTATUS(rc));
return WEXITSTATUS(rc);
+ }
else
return -1;
} else {
@@ -101,27 +104,30 @@ int startDaemon() {
/* kid */
execlp("minilogd","minilogd",NULL);
perror("exec");
+ abort();
exit(-1);
}
}
int logLine(struct logInfo *logEnt) {
/* Logs a line... somewhere. */
- int x,y,z;
+ int x=0,y=0,z=0;
struct stat statbuf;
/* Don't log empty or null lines */
if (!logEnt->line || !strcmp(logEnt->line,"\n")) return 0;
-
- if ( ((stat(_PATH_LOG,&statbuf)==-1)||(access(_PATH_LOG,W_OK)==-1))
- && (x=startDaemon())
+
+ if ( ((stat(_PATH_LOG,&statbuf)==-1) ||(access("/",W_OK)==-1))
+ && startDaemon()
) {
+ DDEBUG("starting daemon failed, pooling entry %d\n",logEntries);
logData=realloc(logData,(logEntries+1)*sizeof(struct logInfo));
logData[logEntries]= (*logEnt);
logEntries++;
} else {
if (logEntries>0) {
for (x=0;x<logEntries;x++) {
+ DDEBUG("flushing log entry %d =%s=\n",x,logData[x].line);
openlog(logData[x].cmd,0,logData[x].fac);
syslog(logData[x].pri,"%s",logData[x].line);
closelog();
@@ -129,6 +135,7 @@ int logLine(struct logInfo *logEnt) {
free(logData);
logEntries = 0;
}
+ DDEBUG("logging =%s= via syslog\n",logEnt->line);
openlog(logEnt->cmd,0,logEnt->fac);
syslog(logEnt->pri,"%s",logEnt->line);
closelog();
@@ -208,6 +215,9 @@ int processArgs(int argc, char **argv, int silent) {
{ "cmd", 'c', POPT_ARG_STRING, &cmd, 0,
"command to run, logging output", NULL
},
+ { "debug", 'd', POPT_ARG_NONE, &debug, 0,
+ "print lots of verbose debugging info", NULL
+ },
{ "run", 'r', POPT_ARG_STRING, &cmd, 3,
"command to run, accepting input on open fd", NULL
},
@@ -289,7 +299,7 @@ int processArgs(int argc, char **argv, int silent) {
} else if (logstring) {
logString(cmdname,logstring);
} else if ( cmd ) {
- return(runCommand(cmd,reexec,quiet));
+ return(runCommand(cmd,reexec,quiet,debug));
} else {
if (!silent)
fprintf(stderr,"nothing to do!\n");
diff --git a/src/initlog.h b/src/initlog.h
index 20f64d1d..94f467bf 100644
--- a/src/initlog.h
+++ b/src/initlog.h
@@ -15,4 +15,6 @@ char *getLine(char **data);
int logString(char *cmd, char *string);
int processArgs(int argc, char **argv, int silent);
+#define DDEBUG if (debug) printf
+
#endif
diff --git a/src/minilogd.c b/src/minilogd.c
index 345ec3c4..b0432408 100644
--- a/src/minilogd.c
+++ b/src/minilogd.c
@@ -23,6 +23,8 @@ static int we_own_log=0;
static char **buffer=NULL;
static int buflines=0;
+int debug;
+
void freeBuffer() {
struct sockaddr_un addr;
int sock;
@@ -50,7 +52,6 @@ void cleanup(int exitcode) {
unlink(_PATH_LOG);
}
/* Don't try to free buffer if we were called from a signal handler */
- perror("foo");
if (exitcode<=0) {
if (buffer) freeBuffer();
exit(exitcode);
@@ -121,11 +122,13 @@ void runDaemon(int sock) {
cleanup(0);
}
-int main() {
+int main(int argc, char **argv) {
struct sockaddr_un addr;
int sock;
int pid;
+ /* option processing made simple... */
+ if (argc>1) debug=1;
/* just in case */
sock = open("/dev/null",O_RDWR);
dup2(sock,0);
@@ -143,16 +146,16 @@ int main() {
listen(sock,5);
if ((pid=fork())==-1) {
perror("fork");
- exit(-1);
+ exit(3);
}
if (pid) {
exit(0);
} else {
runDaemon(sock);
/* shouldn't get back here... */
- exit(-1);
+ exit(4);
}
} else {
- exit(-1);
+ exit(5);
}
}
diff --git a/src/process.c b/src/process.c
index f462a1de..0681b8f4 100644
--- a/src/process.c
+++ b/src/process.c
@@ -97,7 +97,7 @@ int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) {
}
}
-int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet) {
+int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, int debug) {
struct pollfd *pfds;
char *buf=malloc(2048*sizeof(char));
int outpipe[2];
@@ -144,6 +144,7 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet)
if (!reexec) {
if (getenv("IN_INITLOG")) {
char *buffer=calloc(2048,sizeof(char));
+ DDEBUG("sending =%s= to initlog parent\n",tmpstr);
snprintf(buffer,2048,"-n %s -s \"%s\"\n",
cmdname,tmpstr);
write(CMD_FD,buffer,strlen(buffer));
@@ -151,8 +152,7 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet)
} else {
logString(cmdname,tmpstr);
}
- }
- else {
+ } else {
char **cmdargs=NULL;
char **tmpargs=NULL;
int cmdargc,x;
@@ -191,7 +191,7 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet)
return 0;
}
-int runCommand(char *cmd, int reexec, int quiet) {
+int runCommand(char *cmd, int reexec, int quiet, int debug) {
int fds[2];
int pid,x;
char **args, **tmpargs;
@@ -212,12 +212,12 @@ int runCommand(char *cmd, int reexec, int quiet) {
cmdname+=3;
if (!reexec) {
pid=forkCommand(args,&fds[0],&fds[1],NULL,quiet);
- x=monitor(cmdname,pid,2,fds,reexec,quiet);
+ x=monitor(cmdname,pid,2,fds,reexec,quiet,debug);
} else {
setenv("IN_INITLOG","yes",1);
pid=forkCommand(args,NULL,NULL,&fds[0],quiet);
unsetenv("IN_INITLOG");
- x=monitor(cmdname,pid,1,&fds[0],reexec,quiet);
+ x=monitor(cmdname,pid,1,&fds[0],reexec,quiet,debug);
}
return x;
}
diff --git a/src/process.h b/src/process.h
index 6c796cb2..ef98f787 100644
--- a/src/process.h
+++ b/src/process.h
@@ -4,6 +4,6 @@
#define CMD_FD 21
-int runCommand(char *cmd, int reexec, int quiet);
+int runCommand(char *cmd, int reexec, int quiet, int debug);
#endif