| 1 | greg | 3.18 | /* RCSid $Id: rtprocess.h,v 3.17 2018/03/20 17:45:07 greg Exp $ */ | 
| 2 | schorsch | 3.1 | /* | 
| 3 |  |  | *   rtprocess.h | 
| 4 |  |  | *   Routines to communicate with separate process via dual pipes | 
| 5 |  |  | * | 
| 6 |  |  | *   WARNING: On Windows, there's a system header named <process.h>. | 
| 7 |  |  | */ | 
| 8 |  |  | #ifndef _RAD_PROCESS_H_ | 
| 9 |  |  | #define _RAD_PROCESS_H_ | 
| 10 |  |  |  | 
| 11 | schorsch | 3.3 | #include  <errno.h> | 
| 12 | greg | 3.13 | #include <stdio.h> | 
| 13 | schorsch | 3.16 | #if defined(_WIN32) || defined(_WIN64) | 
| 14 | schorsch | 3.3 | #include <windows.h> /* DWORD etc. */ | 
| 15 | schorsch | 3.10 | typedef DWORD RT_PID; | 
| 16 | schorsch | 3.3 | #include <process.h> /* getpid() and others */ | 
| 17 | schorsch | 3.16 | #define getpid _getpid | 
| 18 |  |  | #define execv _execv | 
| 19 | greg | 3.17 | #define execvp _execvp | 
| 20 | schorsch | 3.1 | #else | 
| 21 |  |  | #include <sys/param.h> | 
| 22 | schorsch | 3.10 | #include <sys/types.h> | 
| 23 |  |  | typedef pid_t RT_PID; | 
| 24 | schorsch | 3.1 | #endif | 
| 25 |  |  |  | 
| 26 |  |  | #include "paths.h" | 
| 27 |  |  |  | 
| 28 | schorsch | 3.4 | #ifdef __cplusplus | 
| 29 |  |  | extern "C" { | 
| 30 |  |  | #endif | 
| 31 | schorsch | 3.1 |  | 
| 32 |  |  | /* On Windows, a process ID is a DWORD. That might actually be the | 
| 33 |  |  | same thing as an int, but it's better not to assume anything. | 
| 34 |  |  |  | 
| 35 |  |  | This means that we shouldn't rely on PIDs and file descriptors | 
| 36 |  |  | being the same type, so we have to describe processes with a struct, | 
| 37 | schorsch | 3.10 | instead of the original int[3]. For that purpose, we typedef a | 
| 38 |  |  | platform independent RT_PID. | 
| 39 | schorsch | 3.1 | */ | 
| 40 |  |  |  | 
| 41 | greg | 3.18 | /* On Unix, we can set flags and assign descriptors before opening a | 
| 42 |  |  | process, coupling an existing input or output to the new process rather | 
| 43 |  |  | than opening both pipes.  If PF_FILT_INP is passed in the flags member of | 
| 44 |  |  | SUBPROC, then the given r stream will be attached to the standard input | 
| 45 |  |  | of the child process, and subsequent reads from that descriptor in the | 
| 46 |  |  | parent get data from the standard output of the child, instead.  The | 
| 47 |  |  | returned w descriptor is set to -1, since there is no longer any way | 
| 48 |  |  | to write to the input of the child.  The default r descriptor of 0 will | 
| 49 |  |  | compel the child to act as a filter on the standard input of the parent. | 
| 50 |  |  | Whatever r handle you specify, the child will filter its read operations. | 
| 51 |  |  | Note that this should be called before anything has been buffered using r. | 
| 52 |  |  | If PF_FILT_OUT is set in flags, then the given w stream will be | 
| 53 |  |  | attached to the standard output of the child, and subsequent writes | 
| 54 |  |  | to that descriptor in the parent send data to the standard input | 
| 55 |  |  | of the child. The returned r descriptor is set to -1, since | 
| 56 |  |  | there is no output to read from any longer in the child.  The | 
| 57 |  |  | default w descriptor of 1 will cause the child to act as a filter | 
| 58 |  |  | on the output of the parent.  Make sure to call fflush(stdout) first | 
| 59 |  |  | if any data was buffered.  It is illegal to set both PF_FILT_INP and | 
| 60 |  |  | PF_FILT_OUT, as a circular process is guaranteed to hang. | 
| 61 |  |  | */ | 
| 62 |  |  |  | 
| 63 | schorsch | 3.1 |  | 
| 64 |  |  | #ifndef PIPE_BUF | 
| 65 |  |  | #ifdef PIPSIZ | 
| 66 |  |  | #define PIPE_BUF    PIPSIZ | 
| 67 |  |  | #else | 
| 68 |  |  | #ifdef PIPE_MAX | 
| 69 |  |  | #define PIPE_BUF  PIPE_MAX | 
| 70 |  |  | #else | 
| 71 |  |  | #define PIPE_BUF  512             /* hyperconservative */ | 
| 72 |  |  | #endif | 
| 73 |  |  | #endif | 
| 74 |  |  | #endif | 
| 75 | greg | 3.18 | /* process flags */ | 
| 76 |  |  | #define PF_RUNNING      1               /* process is running */ | 
| 77 |  |  | #define PF_FILT_INP     2               /* use assigned read descriptor */ | 
| 78 |  |  | #define PF_FILT_OUT     4               /* use assigned write descriptor */ | 
| 79 | schorsch | 3.1 |  | 
| 80 |  |  | typedef struct { | 
| 81 | greg | 3.18 | int     flags;          /* what is being done */ | 
| 82 |  |  | int     r;              /* read handle */ | 
| 83 |  |  | int     w;              /* write handle */ | 
| 84 |  |  | RT_PID  pid;            /* process ID */ | 
| 85 | schorsch | 3.1 | } SUBPROC; | 
| 86 |  |  |  | 
| 87 | greg | 3.18 | #define SP_INACTIVE {0,0,1,-1}  /* for static initializations */ | 
| 88 | schorsch | 3.1 |  | 
| 89 | greg | 3.15 | #define close_process(pd)       close_processes(pd,1) | 
| 90 |  |  |  | 
| 91 | schorsch | 3.1 | extern int open_process(SUBPROC *pd, char *av[]); | 
| 92 | greg | 3.15 | extern int close_processes(SUBPROC pd[], int nproc); | 
| 93 | schorsch | 3.1 | extern int process(SUBPROC *pd, char *recvbuf, char *sendbuf, int nbr, int nbs); | 
| 94 |  |  | extern int readbuf(int fd, char *bpos, int siz); | 
| 95 |  |  | extern int writebuf(int fd, char *bpos, int siz); | 
| 96 | schorsch | 3.5 |  | 
| 97 | schorsch | 3.16 | #if defined(_WIN32) || defined(_WIN64) | 
| 98 | schorsch | 3.5 | /* any non-negative increment will send the process to IDLE_PRIORITY_CLASS. */ | 
| 99 | greg | 3.14 | extern int win_kill(RT_PID pid, int sig /* ignored */); | 
| 100 | schorsch | 3.5 | extern int win_nice(int inc); | 
| 101 |  |  | #endif | 
| 102 | schorsch | 3.1 |  | 
| 103 | greg | 3.18 | extern SUBPROC  sp_inactive; | 
| 104 | schorsch | 3.1 |  | 
| 105 |  |  | #ifdef __cplusplus | 
| 106 |  |  | } | 
| 107 |  |  | #endif | 
| 108 |  |  | #endif /* _RAD_PROCESS_H_ */ | 
| 109 |  |  |  |