libassa 3.5.1
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
ASSA::Pipe Class Reference

#include <Pipe.h>

Public Member Functions

 Pipe ()
 A no-op constructor.
 
 ~Pipe ()
 Destructor calls close () first in an attempt to close opened pipe.
 
FILEopen (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.
 
FILEfp () const
 Get pipe's standard I/O file pointer.
 
int fd () const
 Get pipe's file descriptor.
 

Private Member Functions

 Pipe (const Pipe &)
 
Pipeoperator= (const Pipe &)
 

Private Attributes

FILEm_fp
 A standard I/O stream descriptor.
 
pid_t m_child_pid
 Supbrocess' PID.
 

Detailed Description

Definition at line 28 of file Pipe.h.

Constructor & Destructor Documentation

◆ Pipe() [1/2]

Pipe::Pipe ( )

A no-op constructor.

Definition at line 33 of file Pipe.cpp.

35 : m_fp (NULL),
36 m_child_pid (0)
37 {
38 trace_with_mask("Pipe::Pipe", PIPE);
39 /* no-op */
40}
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
pid_t m_child_pid
Supbrocess' PID.
Definition Pipe.h:98
FILE * m_fp
A standard I/O stream descriptor.
Definition Pipe.h:93
@ PIPE
Class Pipe messages
Definition LogMask.h:49

References ASSA::PIPE, and trace_with_mask.

◆ ~Pipe()

Pipe::~Pipe ( )

Destructor calls close () first in an attempt to close opened pipe.

Definition at line 42 of file Pipe.cpp.

44{
45 trace_with_mask("Pipe::~Pipe", PIPE);
46 close ();
47}
int close()
Close the pipe.
Definition Pipe.cpp:136

References close(), ASSA::PIPE, and trace_with_mask.

◆ Pipe() [2/2]

ASSA::Pipe::Pipe ( const Pipe )
private

Member Function Documentation

◆ close()

int Pipe::close ( )

Close the pipe.

The subprocess' status is collected to ensure that the child process have finished.

Returns
0 on success; -1 on error.

Definition at line 135 of file Pipe.cpp.

137{
138 trace_with_mask("Pipe::close", PIPE);
139
140 int ret = 0;
141 if (m_child_pid == 0) {
142 ret = EOF;
143 }
144
145 if (m_fp) {
146 ret = fclose (m_fp);
147 }
148 m_fp = NULL;
149 m_child_pid = 0;
150 return ret == EOF ? -1 : 0;
151}

References m_child_pid, m_fp, ASSA::PIPE, and trace_with_mask.

Referenced by kill(), open(), and ~Pipe().

◆ fd()

int ASSA::Pipe::fd ( ) const
inline

Get pipe's file descriptor.

Definition at line 105 of file Pipe.h.

105{ return fileno (m_fp); }

References m_fp.

Referenced by open().

◆ fp()

FILE * ASSA::Pipe::fp ( ) const
inline

Get pipe's standard I/O file pointer.

Definition at line 108 of file Pipe.h.

108{ return m_fp; }

References m_fp.

◆ kill()

int Pipe::kill ( )

Kill subprocess with SIGTERM.

You should most probably call close() afterwards to collect child process' status.

See also
close()
Returns
0 on success, -1 if kill(2) failed.

Definition at line 117 of file Pipe.cpp.

119{
120 trace_with_mask("Pipe::kill", PIPE);
121
122#if !defined(WIN32)
123 if (m_child_pid == 0) return -1;
124
125 int ret = ::kill (m_child_pid, SIGTERM);
126 close ();
127 return ret;
128#else
129 DL((ASSAERR|PIPE,"Not implemented for win32!\n"));
130 return -1;
131#endif
132}
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
int kill()
Kill subprocess with SIGTERM.
Definition Pipe.cpp:118
@ ASSAERR
ASSA and system errors
Definition LogMask.h:34

References ASSA::ASSAERR, close(), DL, kill(), m_child_pid, ASSA::PIPE, and trace_with_mask.

Referenced by kill().

◆ open()

FILE * Pipe::open ( const string &  cmd_,
const string &  type_ 
)

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.

See also
Fork
Parameters
cmd_command to execute
type_"w" for write pipe and "r" for read pipe
Returns
pointer to a standard I/O stream. In case of error, NULL is returned with errno set to indicate the type of error encountered.

Definition at line 50 of file Pipe.cpp.

52{
53 trace_with_mask("Pipe::open", PIPE);
54
55#if !defined(WIN32) // not yet implemented
56
57 if (type_ != "r" && type_ != "w") {
58 EL((ASSAERR,"Wrong type \"%s\"\n", type_.c_str ()));
59 errno = EINVAL;
60 return NULL;
61 }
62
63 int fd [2];
64 if (pipe (fd) < 0) {
65 EL((ASSAERR,"failed: pipe(2)\n"));
66 return NULL;
67 }
69
70 if (f.isChild ()) {
71 if (type_ == "r") {
72 ::close (fd [0]);
73 if (fd [1] != STDOUT_FILENO) {
74 dup2 (fd [1], STDOUT_FILENO);
75 ::close (fd [1]);
76 }
77 }
78 else { // 'w'
79 ::close (fd [1]);
80 if (fd [0] != STDIN_FILENO) {
81 dup2 (fd [0], STDIN_FILENO);
82 ::close (fd [0]);
83 }
84 }
85
86 DL((PIPE,"Executing cmd: \"%s\"\n", cmd_.c_str ()));
87 execl ("/bin/sh", "sh", "-c", cmd_.c_str (), (char* ) 0);
88 EL((ASSAERR,"failed: execl(2)\n"));
89 _exit (127);
90 }
91 /* parent */
92 if (type_ == "r") {
93 ::close (fd [1]);
94 if ((m_fp = fdopen (fd [0], type_.c_str ())) == NULL) {
95 EL((ASSAERR,"failed: fdopen ()\n"));
96 return NULL;
97 }
98 }
99 else { // 'w'
100 ::close (fd [0]);
101 if ((m_fp = fdopen (fd [1], type_.c_str ())) == NULL) {
102 EL((ASSAERR,"failed: fdopen ()\n"));
103 return NULL;
104 }
105 }
106 m_child_pid = f.getChildPID ();
107 DL((PIPE,"m_child_pid = %d\n",m_child_pid));
108 return m_fp;
109
110#else
111 DL((ASSAERR|PIPE,"Not implemented for win32!\n"));
112 return NULL;
113#endif
114}
#define EL(X)
A macro for writing error message to the Logger.
Definition Logger.h:285
Fork class is a simple wrapper around C library function fork().
Definition Fork.h:86
@ KILL_ON_EXIT
Kill all childer on exit.
Definition Fork.h:92
@ IGNORE_STATUS
Don't wait for child to complete.
Definition Fork.h:100
int fd() const
Get pipe's file descriptor.
Definition Pipe.h:105

References ASSA::ASSAERR, close(), DL, EL, fd(), ASSA::Fork::IGNORE_STATUS, ASSA::Fork::KILL_ON_EXIT, m_child_pid, m_fp, ASSA::PIPE, and trace_with_mask.

◆ operator=()

Pipe & ASSA::Pipe::operator= ( const Pipe )
private

◆ pid()

pid_t ASSA::Pipe::pid ( ) const
inline

Get subprocess' PID.

Definition at line 102 of file Pipe.h.

102{ return m_child_pid; }

References m_child_pid.

Member Data Documentation

◆ m_child_pid

pid_t ASSA::Pipe::m_child_pid
private

Supbrocess' PID.

Definition at line 98 of file Pipe.h.

Referenced by close(), kill(), open(), and pid().

◆ m_fp

FILE* ASSA::Pipe::m_fp
private

A standard I/O stream descriptor.

Definition at line 93 of file Pipe.h.

Referenced by close(), fd(), fp(), and open().


The documentation for this class was generated from the following files: