#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 124 of file Pipe.cpp. References m_child_pid, m_fp, ASSA::PIPE, and trace_with_mask. Referenced by kill(), open(), and ~Pipe(). 00125 { 00126 trace_with_mask("Pipe::close", PIPE); 00127 00128 int ret = 0; 00129 if (m_child_pid == 0) { 00130 ret = EOF; 00131 } 00132 00133 if (m_fp) { 00134 ret = fclose (m_fp); 00135 } 00136 m_fp = NULL; 00137 m_child_pid = 0; 00138 return ret == EOF ? -1 : 0; 00139 }
|
|
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 111 of file Pipe.cpp. References close(), m_child_pid, ASSA::PIPE, and trace_with_mask. 00112 { 00113 trace_with_mask("Pipe::kill", PIPE); 00114 00115 if (m_child_pid == 0) return -1; 00116 00117 int ret = ::kill (m_child_pid, SIGTERM); 00118 close (); 00119 return ret; 00120 }
|
|
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 close(), DL, EL, ASSA::ERROR, 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 (type_ != "r" && type_ != "w") { 00056 EL((ERROR,"Wrong type \"%s\"\n", type_.c_str ())); 00057 errno = EINVAL; 00058 return NULL; 00059 } 00060 00061 int fd [2]; 00062 if (pipe (fd) < 0) { 00063 EL((ERROR,"failed: pipe(2)\n")); 00064 return NULL; 00065 } 00066 Fork f (Fork::KILL_ON_EXIT, Fork::IGNORE_STATUS); 00067 00068 if (f.isChild ()) { 00069 if (type_ == "r") { 00070 ::close (fd [0]); 00071 if (fd [1] != STDOUT_FILENO) { 00072 dup2 (fd [1], STDOUT_FILENO); 00073 ::close (fd [1]); 00074 } 00075 } 00076 else { // 'w' 00077 ::close (fd [1]); 00078 if (fd [0] != STDIN_FILENO) { 00079 dup2 (fd [0], STDIN_FILENO); 00080 ::close (fd [0]); 00081 } 00082 } 00083 00084 DL((PIPE,"Executing cmd: \"%s\"\n", cmd_.c_str ())); 00085 execl ("/bin/sh", "sh", "-c", cmd_.c_str (), (char* ) 0); 00086 EL((ERROR,"failed: execl(2)\n")); 00087 _exit (127); 00088 } 00089 /* parent */ 00090 if (type_ == "r") { 00091 ::close (fd [1]); 00092 if ((m_fp = fdopen (fd [0], type_.c_str ())) == NULL) { 00093 EL((ERROR,"failed: fdopen ()\n")); 00094 return NULL; 00095 } 00096 } 00097 else { // 'w' 00098 ::close (fd [0]); 00099 if ((m_fp = fdopen (fd [1], type_.c_str ())) == NULL) { 00100 EL((ERROR,"failed: fdopen ()\n")); 00101 return NULL; 00102 } 00103 } 00104 m_child_pid = f.getChildPID (); 00105 DL((PIPE,"m_child_pid = %d\n",m_child_pid)); 00106 return m_fp; 00107 }
|
|
|
|
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.
|