diff options
-rw-r--r-- | src/initlog.c | 24 | ||||
-rw-r--r-- | src/process.c | 48 |
2 files changed, 62 insertions, 10 deletions
diff --git a/src/initlog.c b/src/initlog.c index 23f5c508..e9b8e265 100644 --- a/src/initlog.c +++ b/src/initlog.c @@ -40,7 +40,7 @@ struct logInfo *logData = NULL; void readConfiguration(char *fname) { int fd,num=0; struct stat sbuf; - char *data,*line; + char *data,*line, *d; regex_t *regexp; int lfac=-1,lpri=-1; @@ -49,7 +49,7 @@ void readConfiguration(char *fname) { close(fd); return; } - data=malloc(sbuf.st_size+1); + d = data=malloc(sbuf.st_size+1); if (read(fd,data,sbuf.st_size)!=sbuf.st_size) { close(fd); return; @@ -98,6 +98,7 @@ void readConfiguration(char *fname) { } if (lfac!=-1) logfacility=lfac; if (lpri!=-1) logpriority=lpri; + free(d); } char *getLine(char **data) { @@ -267,7 +268,7 @@ int logEvent(char *cmd, int eventtype,char *string) { /* insert more here */ NULL }; - int x=0,len; + int x=0,len, rc; struct logInfo logentry; if (cmd) { @@ -278,8 +279,10 @@ int logEvent(char *cmd, int eventtype,char *string) { logentry.cmd+=3; } else logentry.cmd = strdup(_("(none)")); - if (!string) - string = strdup(cmd); + if (!string) { + string = alloca(strlen(cmd)+1); + strcpy(string,cmd); + } while (eventtable[x] && x<eventtype) x++; if (!(eventtable[x])) x=0; @@ -291,11 +294,15 @@ int logEvent(char *cmd, int eventtype,char *string) { logentry.pri = logpriority; logentry.fac = logfacility; - return logLine(&logentry); + rc = logLine(&logentry); + free(logentry.line); + free(logentry.cmd); + return rc; } int logString(char *cmd, char *string) { struct logInfo logentry; + int rc; if (cmd) { logentry.cmd = strdup(basename(cmd)); @@ -309,7 +316,10 @@ int logString(char *cmd, char *string) { logentry.pri = logpriority; logentry.fac = logfacility; - return logLine(&logentry); + rc = logLine(&logentry); + free(logentry.line); + free(logentry.cmd); + return rc; } int processArgs(int argc, char **argv, int silent) { diff --git a/src/process.c b/src/process.c index 6948b8a4..0eec34ac 100644 --- a/src/process.c +++ b/src/process.c @@ -1,3 +1,14 @@ +/* + * Copyright (c) 1999-2003 Red Hat, Inc. All rights reserved. + * + * This software may be freely redistributed under the terms of the GNU + * public license. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + */ #include <errno.h> #include <fcntl.h> @@ -22,7 +33,7 @@ extern regex_t **regList; int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) { /* Fork command 'cmd', returning pid, and optionally pointer * to open file descriptor fd */ - int fdout, fderr, fdcmd, pid; + int fdout=-1, fderr=-1, fdcmd=-1, pid; int outpipe[2], errpipe[2], fdpipe[2]; int ourpid; @@ -52,6 +63,8 @@ int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) { } else { fdcmd = open("/dev/null",O_WRONLY); } + if (fdout==-1 || fderr==-1 || fdcmd==-1) + return -1; ourpid = getpid(); if ((pid = fork())==-1) { perror("fork"); @@ -128,7 +141,6 @@ 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 debug) { struct pollfd *pfds; - char *buf=malloc(8192*sizeof(char)); char *outbuf=NULL; char *tmpstr=NULL; int x,y,rc=-1; @@ -154,6 +166,9 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, usleep(500); if (((x=poll(pfds,numfds,500))==-1)&&errno!=EINTR) { perror("poll"); + free(pfds); + if (procpath) + free(procpath); return -1; } if (!reexec) { @@ -165,16 +180,23 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, if (stat(procpath,&sbuf)&&!stat("/proc/cpuinfo",&sbuf)) done=1; } + if (x<0) + continue; y=0; while (y<numfds) { if ( x && ((pfds[y].revents & (POLLIN | POLLPRI)) )) { int bytesread = 0; do { - buf=calloc(8192,sizeof(char)); + char *b, *buf=calloc(8192,sizeof(char)); + b = buf; bytesread = read(pfds[y].fd,buf,8192); if (bytesread==-1) { perror("read"); + free(pfds); + if (procpath) + free(procpath); + free(buf); return -1; } if (bytesread) { @@ -230,10 +252,16 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, } cmdargs[cmdargc+1]=NULL; processArgs(cmdargc+1,cmdargs,1); + for (z=0;z<(cmdargc);z++) { + free(cmdargs[z]); + } + free(cmdargs); } } + if (tmpstr) free(tmpstr); } } + free(b); } while ( bytesread==8192 ); } y++; @@ -245,8 +273,18 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, if (quiet && output) { write(1,outbuf,strlen(outbuf)); } + free(pfds); + if (procpath) + free(procpath); + if(outbuf) + free(outbuf); return (rc); } + free(pfds); + if (procpath) + free(procpath); + if(outbuf) + free(outbuf); return 0; } @@ -272,10 +310,14 @@ int runCommand(char *cmd, int reexec, int quiet, int debug) { cmdname+=3; if (!reexec) { pid=forkCommand(args,&fds[0],&fds[1],NULL,quiet); + if (pid == -1) + return -1; x=monitor(cmdname,pid,2,fds,reexec,quiet,debug); } else { setenv("IN_INITLOG","yes",1); pid=forkCommand(args,NULL,NULL,&fds[0],quiet); + if (pid == -1) + return -1; unsetenv("IN_INITLOG"); x=monitor(cmdname,pid,1,&fds[0],reexec,quiet,debug); } |