Logo Search packages:      
Sourcecode: fence-agents version File versions  Download package

int StartProcess ( const char *  cmd,
int *  readfd,
int *  writefd,
int  flags 
)

Start a process with its stdin and stdout redirected to pipes so the parent process can talk to it.

Parameters:
cmdCommand line to run.
readfdFilled with a pipe to the the output of the new child.
writefdFilled with a pipe to the input of the new child.
redir_err0, 1 = stderr, 2 = setsid, 3 = both
Returns:
-1 on failure (with errno set appropriately) or or the PID of the new child process.

Definition at line 220 of file expect.c.

References close_all_files().

{
      pid_t pid;
      int   wrpipe[2];  /* The pipe the parent process writes to */
                        /* (which the child process reads from) */
      int   rdpipe[2];  /* The pipe the parent process reads from */
                        /* (which the child process writes to) */

      if (pipe(wrpipe) < 0) {
            perror("cannot create pipe\n");
            return(-1);
      }
      if (pipe(rdpipe) < 0) {
            perror("cannot create pipe\n");
            close(wrpipe[0]);
            close(wrpipe[1]);
            return(-1);
      }
      switch(pid=fork()) {
      case -1:    
            perror("cannot StartProcess cmd");
            close(rdpipe[0]);
            close(rdpipe[1]);
            close(wrpipe[0]);
            close(wrpipe[1]);
            return(-1);

      case 0:
            /* We are the child */
            /* Redirect stdin */
            if (wrpipe[0] != 0) {
                  close(0);
                  if(dup2(wrpipe[0], 0) < 0) {
                      syslog(LOG_CRIT,
                           "StartProcess: dup2(%d,0) failed: %s\n",
                           wrpipe[0],
                           strerror(errno));
                      exit(1);
                  }
                  close(wrpipe[0]);
            }
            close(wrpipe[1]);

            /* Redirect stdout */
            if (rdpipe[1] != 1) {
                  close(1);
                  if(dup2(rdpipe[1], 1) < 0) {
                      syslog(LOG_CRIT,
                           "StartProcess: dup2(%d,1) failed: %s\n",
                           rdpipe[1],
                           strerror(errno));
                      exit(1);
                  }
                  close(rdpipe[1]);
            }
            close(rdpipe[0]);
            
            if (flags & EXP_STDERR) {
                  /* Redirect stderr */
                  close(2);
                  if(dup2(1, 2) < 0) {
                        syslog(LOG_CRIT,
                               "StartProcess: dup2(1,2) failed: %s\n",
                               strerror(errno));
                        exit(1);
                  }
            }
            
            if (flags & EXP_NOCTTY)
                  setsid();
            close_all_files(); /* Workaround telnet bugs */
#if defined(SCHED_OTHER)
            {
                  /*
                   * Try and (re)set our scheduling to "normal"
                   * Sometimes our callers run in soft
                   * real-time mode.  The program we exec might
                   * not be very well behaved - this is bad for
                   * operation in high-priority (soft real-time)
                   * mode.  In particular, telnet is prone to
                   * going into infinite loops when killed.
                   */
                  struct sched_param      sp;
                  memset(&sp, 0, sizeof(sp));
                  sp.sched_priority = 0;
                  sched_setscheduler(0, SCHED_OTHER, &sp);
            }
#endif
            execlp("/bin/bash", "bash", "-c", cmd, NULL);
            perror("cannot exec shell!");
            exit(1);

      default:    /* We are the parent */
            *readfd = rdpipe[0];
            close(rdpipe[1]);
            *writefd = wrpipe[1];
            close(wrpipe[0]);
            return(pid);
      }
      /*NOTREACHED*/
      return(-1);
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index