16 int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) {
17 /* Fork command 'cmd', returning pid, and optionally pointer
18 * to open file descriptor fd */
19 int fdin, fdout, fderr, fdcmd, pid;
20 int outpipe[2], errpipe[2], fdpipe[2];
22 if ( (pipe(outpipe)==-1) || (pipe(errpipe)==-1) || (pipe(fdpipe)==-1) ) {
45 if ((pid = fork())==-1) {
59 if ( (dup2(fdout,1)==-1) ) {
64 if ((dup2(open("/dev/null",O_WRONLY),1))==-1) {
70 if ((dup2(fderr,2)==-1)) {
75 if ((dup2(open("/dev/null",O_WRONLY),2))==-1) {
81 if ((dup2(fdcmd,CMD_FD)==-1)) {
100 int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, int debug) {
102 char *buf=malloc(2048*sizeof(char));
111 pfds = malloc(numfds*sizeof(struct pollfd));
112 for (x=0;x<numfds;x++) {
114 pfds[x].events = POLLIN | POLLPRI;
118 if (((x=poll(pfds,numfds,500))==-1)&&errno!=EINTR) {
122 if (waitpid(pid,&rc,WNOHANG))
126 if ( x && ((pfds[y].revents & (POLLIN | POLLPRI)) )) {
130 buf=calloc(2048,sizeof(char));
131 bytesread = read(pfds[y].fd,buf,2048);
137 if (!quiet && !reexec)
138 write(1,buf,bytesread);
141 write(outpipe[1],buf,bytesread);
143 while ((tmpstr=getLine(&buf))) {
145 if (getenv("IN_INITLOG")) {
146 char *buffer=calloc(2048,sizeof(char));
147 DDEBUG("sending =%s= to initlog parent\n",tmpstr);
148 snprintf(buffer,2048,"-n %s -s \"%s\"\n",
150 write(CMD_FD,buffer,strlen(buffer));
153 logString(cmdname,tmpstr);
160 poptParseArgvString(tmpstr,&cmdargc,&tmpargs);
161 cmdargs=malloc( (cmdargc++) * sizeof(char *) );
162 cmdargs[0]=strdup("initlog");
163 for (x=0;x<(cmdargc-1);x++) {
164 cmdargs[x+1]=tmpargs[x];
166 processArgs(cmdargc,cmdargs,1);
171 } while ( bytesread==2048 );
176 if ((!WIFEXITED(rc)) || (rc=WEXITSTATUS(rc))) {
177 /* If there was an error and we're quiet, be loud */
180 if (quiet && output) {
181 buf=calloc(2048,sizeof(char));
183 x=read(outpipe[0],buf,2048);
186 buf=calloc(2048,sizeof(char));
194 int runCommand(char *cmd, int reexec, int quiet, int debug) {
197 char **args, **tmpargs;
200 poptParseArgvString(cmd,&x,&tmpargs);
201 args = malloc((x+1)*sizeof(char *));
202 for ( pid = 0; pid < x ; pid++) {
203 args[pid] = strdup(tmpargs[pid]);
206 if (strcmp(args[0],"sh") && strcmp(args[0],"/bin/sh"))
207 cmdname = basename(args[0]);
209 cmdname = basename(args[1]);
210 if ((cmdname[0] =='K' || cmdname[0] == 'S') && ( 30 <= cmdname[1] <= 39 )
211 && ( 30 <= cmdname[2] <= 39 ) )
214 pid=forkCommand(args,&fds[0],&fds[1],NULL,quiet);
215 x=monitor(cmdname,pid,2,fds,reexec,quiet,debug);
217 setenv("IN_INITLOG","yes",1);
218 pid=forkCommand(args,NULL,NULL,&fds[0],quiet);
219 unsetenv("IN_INITLOG");
220 x=monitor(cmdname,pid,1,&fds[0],reexec,quiet,debug);