Back to index

radiance  4R0+20100331
Classes | Defines | Typedefs | Functions
rtprocess.h File Reference
#include <errno.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/types.h>
#include "paths.h"

Go to the source code of this file.

Classes

struct  SUBPROC

Defines

#define PIPE_BUF   512 /* hyperconservative */
#define SP_INACTIVE   {-1,-1,0,0} /* for static initializations */

Typedefs

typedef pid_t RT_PID

Functions

int open_process (SUBPROC *pd, char *av[])
int close_process (SUBPROC *pd)
int process (SUBPROC *pd, char *recvbuf, char *sendbuf, int nbr, int nbs)
int readbuf (int fd, char *bpos, int siz)
int writebuf (int fd, char *bpos, int siz)

Class Documentation

struct SUBPROC

Definition at line 67 of file rtprocess.h.

Class Members
RT_PID pid
int r
int running
int w

Define Documentation

#define PIPE_BUF   512 /* hyperconservative */

Definition at line 62 of file rtprocess.h.

#define SP_INACTIVE   {-1,-1,0,0} /* for static initializations */

Definition at line 74 of file rtprocess.h.


Typedef Documentation

typedef pid_t RT_PID

Definition at line 36 of file rtprocess.h.


Function Documentation

int close_process ( SUBPROC pd)

Definition at line 73 of file unix_process.c.

{
       int    status;

       if (!pd->running)
              return(0);
       close(pd->w);
       pd->running = 0;
       if (waitpid(pd->pid, &status, 0) == pd->pid) {
              close(pd->r);
              return(status>>8 & 0xff);
       }
       return(-1);          /* ? unknown status */
}

Here is the call graph for this function:

Here is the caller graph for this function:

int open_process ( SUBPROC pd,
char *  av[] 
)

Definition at line 23 of file unix_process.c.

{
       char   *compath;
       int    p0[2], p1[2];

       pd->running = 0; /* not yet */
                                   /* find executable */
       compath = getpath(av[0], getenv("PATH"), 1);
       if (compath == 0)
              return(0);
       if (pipe(p0) < 0 || pipe(p1) < 0)
              return(-1);
       if ((pd->pid = fork()) == 0) {            /* if child */
              close(p0[1]);
              close(p1[0]);
              if (p0[0] != 0) {    /* connect p0 to stdin */
                     dup2(p0[0], 0);
                     close(p0[0]);
              }
              if (p1[1] != 1) {    /* connect p1 to stdout */
                     dup2(p1[1], 1);
                     close(p1[1]);
              }
              execv(compath, av);  /* exec command */
              perror(compath);
              _exit(127);
       }
       if (pd->pid == -1)
              return(-1);
       close(p0[0]);
       close(p1[1]);
       pd->r = p1[0];
       pd->w = p0[1];
       /*
        * Close write stream on exec to avoid multiprocessing deadlock.
        * No use in read stream without it, so set flag there as well.
        * GW: This bug took me two days to figure out!!
        */
       fcntl(pd->r, F_SETFD, FD_CLOEXEC);
       fcntl(pd->w, F_SETFD, FD_CLOEXEC);
       pd->running = 1;
       return(PIPE_BUF);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int process ( SUBPROC pd,
char *  recvbuf,
char *  sendbuf,
int  nbr,
int  nbs 
)

Definition at line 25 of file process.c.

{
       if (nbs > PIPE_BUF)
              return(-1);
       if (!pd->running)
              return(-1);
       if (writebuf(pd->w, sendbuf, nbs) < nbs)
              return(-1);
       return(readbuf(pd->r, recvbuf, nbr));
}

Here is the call graph for this function:

Here is the caller graph for this function:

int readbuf ( int  fd,
char *  bpos,
int  siz 
)

Definition at line 43 of file process.c.

{
       register int  cc = 0, nrem = siz;
retry:
       while (nrem > 0 && (cc = read(fd, bpos, nrem)) > 0) {
              bpos += cc;
              nrem -= cc;
       }
       if (cc < 0) {
#ifndef BSD
              if (errno == EINTR)  /* we were interrupted! */
                     goto retry;
#endif
              return(cc);
       }
       return(siz-nrem);
}

Here is the caller graph for this function:

int writebuf ( int  fd,
char *  bpos,
int  siz 
)

Definition at line 67 of file process.c.

{
       register int  cc = 0, nrem = siz;
retry:
       while (nrem > 0 && (cc = write(fd, bpos, nrem)) > 0) {
              bpos += cc;
              nrem -= cc;
       }
       if (cc < 0) {
#ifndef BSD
              if (errno == EINTR)  /* we were interrupted! */
                     goto retry;
#endif
              return(cc);
       }
       return(siz-nrem);
}

Here is the caller graph for this function: