diff options
Diffstat (limited to 'src/process.c')
-rw-r--r-- | src/process.c | 48 |
1 files changed, 45 insertions, 3 deletions
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); } |