libassa 3.5.0
Public Member Functions | Private Member Functions | Private Attributes

ASSA::Pipe Class Reference

#include <Pipe.h>

List of all members.

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 &)
Pipeoperator= (const Pipe &)

Private Attributes

FILE * m_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::Pipe ( )

A no-op constructor.

Definition at line 34 of file Pipe.cpp.

References ASSA::PIPE, and trace_with_mask.

    : m_fp (NULL), 
      m_child_pid (0)
 {
    trace_with_mask("Pipe::Pipe", PIPE);
    /* no-op */
}
Pipe::~Pipe ( )

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.

{
    trace_with_mask("Pipe::~Pipe", PIPE);
    close ();
}
ASSA::Pipe::Pipe ( const Pipe ) [private]

Member Function Documentation

int Pipe::close ( void  )

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 136 of file Pipe.cpp.

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

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

{
    trace_with_mask("Pipe::close", PIPE);

    int ret = 0;
    if (m_child_pid == 0) {
        ret = EOF;
    }

    if (m_fp) {
        ret = fclose (m_fp);
    }
    m_fp = NULL;
    m_child_pid = 0;
    return ret == EOF ? -1 : 0;
}
int ASSA::Pipe::fd ( ) const [inline]

Get pipe's file descriptor.

Definition at line 105 of file Pipe.h.

References m_fp.

Referenced by open().

{ return fileno (m_fp); }
FILE * ASSA::Pipe::fp ( ) const [inline]

Get pipe's standard I/O file pointer.

Definition at line 108 of file Pipe.h.

References m_fp.

{ return m_fp; }
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 118 of file Pipe.cpp.

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

{
    trace_with_mask("Pipe::kill", PIPE);

#if !defined(WIN32)
    if (m_child_pid == 0) return -1;

    int ret = ::kill (m_child_pid, SIGTERM);
    close ();
    return ret;
#else
     DL((ASSAERR|PIPE,"Not implemented for win32!\n"));
     return -1;
#endif
}
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 51 of file Pipe.cpp.

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

{
    trace_with_mask("Pipe::open", PIPE);

#if !defined(WIN32)       // not yet implemented

    if (type_ != "r" && type_ != "w") {
        EL((ASSAERR,"Wrong type \"%s\"\n", type_.c_str ()));
        errno = EINVAL;
        return NULL;
    }
    
    int fd [2]; 
    if (pipe (fd) < 0) {
        EL((ASSAERR,"failed: pipe(2)\n"));
        return NULL;
    }
    Fork f (Fork::KILL_ON_EXIT, Fork::IGNORE_STATUS);

    if (f.isChild ()) {
        if (type_ == "r") {
			::close (fd [0]);
            if (fd [1] != STDOUT_FILENO) {
                dup2 (fd [1], STDOUT_FILENO);
				::close (fd [1]);
            }
        }
        else {                  // 'w'
			::close (fd [1]);
            if (fd [0] != STDIN_FILENO) {
                dup2 (fd [0], STDIN_FILENO);
				::close (fd [0]);
            }
        }

        DL((PIPE,"Executing cmd: \"%s\"\n", cmd_.c_str ()));
        execl ("/bin/sh", "sh", "-c", cmd_.c_str (), (char* ) 0);
        EL((ASSAERR,"failed: execl(2)\n"));
        _exit (127);
    }
    /* parent */
    if (type_ == "r") {
		::close (fd [1]);
        if ((m_fp = fdopen (fd [0], type_.c_str ())) == NULL) {
            EL((ASSAERR,"failed: fdopen ()\n"));
            return NULL;
        }
    }
    else {                  // 'w'
		::close (fd [0]);
        if ((m_fp = fdopen (fd [1], type_.c_str ())) == NULL) {
            EL((ASSAERR,"failed: fdopen ()\n"));
            return NULL;
        }
    }
    m_child_pid = f.getChildPID ();
    DL((PIPE,"m_child_pid = %d\n",m_child_pid));
    return m_fp;
    
#else
     DL((ASSAERR|PIPE,"Not implemented for win32!\n"));
     return NULL;
#endif
}
Pipe& ASSA::Pipe::operator= ( const Pipe ) [private]
pid_t ASSA::Pipe::pid ( ) const [inline]

Get subprocess' PID.

Definition at line 102 of file Pipe.h.

References m_child_pid.

{ return m_child_pid; }

Member Data Documentation

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().

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:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines