#include <Pipe.h>
Public Member Functions | |
Pipe () | |
A no-op constructor. | |
~Pipe () | |
Destructor calls close () first in an attempt to close opened pipe. | |
FILE * | open (const string &cmd_, const string &type_) |
Starts a subshell and feed it the string cmd_ to be executed. | |
int | close () |
Close the pipe. | |
int | kill () |
Kill subprocess with SIGTERM. | |
pid_t | pid () const |
Get subprocess' PID. | |
FILE * | fp () const |
Get pipe's standard I/O file pointer. | |
int | fd () const |
Get pipe's file descriptor. | |
Private Member Functions | |
Pipe (const Pipe &) | |
Pipe & | operator= (const Pipe &) |
Private Attributes | |
FILE * | m_fp |
A standard I/O stream descriptor. | |
pid_t | m_child_pid |
Supbrocess' PID. |
Definition at line 28 of file Pipe.h.
|
A no-op constructor.
Definition at line 34 of file Pipe.cpp. References ASSA::PIPE, and trace_with_mask. 00035 : m_fp (NULL), 00036 m_child_pid (0) 00037 { 00038 trace_with_mask("Pipe::Pipe", PIPE); 00039 /* no-op */ 00040 }
|
|
Destructor calls close () first in an attempt to close opened pipe.
Definition at line 43 of file Pipe.cpp. References close(), ASSA::PIPE, and trace_with_mask. 00044 { 00045 trace_with_mask("Pipe::~Pipe", PIPE); 00046 close (); 00047 }
|
|
|
|
Close the pipe. The subprocess' status is collected to ensure that the child process have finished.
Definition at line 136 of file Pipe.cpp. References m_child_pid, m_fp, ASSA::PIPE, and trace_with_mask. Referenced by kill(), open(), and ~Pipe(). 00137 { 00138 trace_with_mask("Pipe::close", PIPE); 00139 00140 int ret = 0; 00141 if (m_child_pid == 0) { 00142 ret = EOF; 00143 } 00144 00145 if (m_fp) { 00146 ret = fclose (m_fp); 00147 } 00148 m_fp = NULL; 00149 m_child_pid = 0; 00150 return ret == EOF ? -1 : 0; 00151 }
|
|
Get pipe's file descriptor.
Definition at line 105 of file Pipe.h. References m_fp. Referenced by open(). 00105 { return fileno (m_fp); }
|
|
Get pipe's standard I/O file pointer.
Definition at line 108 of file Pipe.h. References m_fp. 00108 { return m_fp; }
|
|
Kill subprocess with SIGTERM. You should most probably call close() afterwards to collect child process' status.
Definition at line 118 of file Pipe.cpp. References ASSA::ASSAERR, close(), DL, m_child_pid, ASSA::PIPE, and trace_with_mask. 00119 { 00120 trace_with_mask("Pipe::kill", PIPE); 00121 00122 #if !defined(WIN32) 00123 if (m_child_pid == 0) return -1; 00124 00125 int ret = ::kill (m_child_pid, SIGTERM); 00126 close (); 00127 return ret; 00128 #else 00129 DL((ASSAERR|PIPE,"Not implemented for win32!\n")); 00130 return -1; 00131 #endif 00132 }
|
|
Starts a subshell and feed it the string cmd_ to be executed. The pipe is created and attached to the standard input or standard output of the subprocess, according to whether type_ is either "r" (read) or "w" (write). The other end of the pipe is returned to the calling code as a standard I/O stream, FILE, ready for buffered use with fprintf(), fscanf(), fgets, etc.
Definition at line 51 of file Pipe.cpp. References ASSA::ASSAERR, close(), DL, EL, fd(), ASSA::Fork::IGNORE_STATUS, ASSA::Fork::isChild(), ASSA::Fork::KILL_ON_EXIT, ASSA::PIPE, and trace_with_mask. 00052 { 00053 trace_with_mask("Pipe::open", PIPE); 00054 00055 #if !defined(WIN32) // not yet implemented 00056 00057 if (type_ != "r" && type_ != "w") { 00058 EL((ASSAERR,"Wrong type \"%s\"\n", type_.c_str ())); 00059 errno = EINVAL; 00060 return NULL; 00061 } 00062 00063 int fd [2]; 00064 if (pipe (fd) < 0) { 00065 EL((ASSAERR,"failed: pipe(2)\n")); 00066 return NULL; 00067 } 00068 Fork f (Fork::KILL_ON_EXIT, Fork::IGNORE_STATUS); 00069 00070 if (f.isChild ()) { 00071 if (type_ == "r") { 00072 ::close (fd [0]); 00073 if (fd [1] != STDOUT_FILENO) { 00074 dup2 (fd [1], STDOUT_FILENO); 00075 ::close (fd [1]); 00076 } 00077 } 00078 else { // 'w' 00079 ::close (fd [1]); 00080 if (fd [0] != STDIN_FILENO) { 00081 dup2 (fd [0], STDIN_FILENO); 00082 ::close (fd [0]); 00083 } 00084 } 00085 00086 DL((PIPE,"Executing cmd: \"%s\"\n", cmd_.c_str ())); 00087 execl ("/bin/sh", "sh", "-c", cmd_.c_str (), (char* ) 0); 00088 EL((ASSAERR,"failed: execl(2)\n")); 00089 _exit (127); 00090 } 00091 /* parent */ 00092 if (type_ == "r") { 00093 ::close (fd [1]); 00094 if ((m_fp = fdopen (fd [0], type_.c_str ())) == NULL) { 00095 EL((ASSAERR,"failed: fdopen ()\n")); 00096 return NULL; 00097 } 00098 } 00099 else { // 'w' 00100 ::close (fd [0]); 00101 if ((m_fp = fdopen (fd [1], type_.c_str ())) == NULL) { 00102 EL((ASSAERR,"failed: fdopen ()\n")); 00103 return NULL; 00104 } 00105 } 00106 m_child_pid = f.getChildPID (); 00107 DL((PIPE,"m_child_pid = %d\n",m_child_pid)); 00108 return m_fp; 00109 00110 #else 00111 DL((ASSAERR|PIPE,"Not implemented for win32!\n")); 00112 return NULL; 00113 #endif 00114 }
|
|
|
|
Get subprocess' PID.
Definition at line 102 of file Pipe.h. References m_child_pid. 00102 { return m_child_pid; }
|
|
Supbrocess' PID.
|
|
A standard I/O stream descriptor.
|