pipe.h

Go to the documentation of this file.
00001 /** @file rutz/pipe.h wrap posix inter-process pipes in a c++
00002     iostreams interface */
00003 
00004 ///////////////////////////////////////////////////////////////////////
00005 //
00006 // Copyright (c) 2000-2004 California Institute of Technology
00007 // Copyright (c) 2004-2007 University of Southern California
00008 // Rob Peters <rjpeters at usc dot edu>
00009 //
00010 // created: Fri Jan 14 17:33:24 2000
00011 // commit: $Id: pipe.h 11238 2009-05-28 01:05:46Z jshen $
00012 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/pipe.h $
00013 //
00014 // --------------------------------------------------------------------
00015 //
00016 // This file is part of GroovX.
00017 //   [http://ilab.usc.edu/rjpeters/groovx/]
00018 //
00019 // GroovX is free software; you can redistribute it and/or modify it
00020 // under the terms of the GNU General Public License as published by
00021 // the Free Software Foundation; either version 2 of the License, or
00022 // (at your option) any later version.
00023 //
00024 // GroovX is distributed in the hope that it will be useful, but
00025 // WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027 // General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU General Public License
00030 // along with GroovX; if not, write to the Free Software Foundation,
00031 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00032 //
00033 ///////////////////////////////////////////////////////////////////////
00034 
00035 #ifndef GROOVX_RUTZ_PIPE_H_UTC20050626084019_DEFINED
00036 #define GROOVX_RUTZ_PIPE_H_UTC20050626084019_DEFINED
00037 
00038 #include "rutz/stdiobuf.h"
00039 #include "nub/object.h"
00040 #include <cstdio>
00041 #include <istream>
00042 #include <sys/types.h>
00043 #include <sys/wait.h>
00044 #include <unistd.h>
00045 
00046 namespace rutz
00047 {
00048   class shell_pipe;
00049   class pipe_fds;
00050   class child_process;
00051   class exec_pipe;
00052   class bidir_pipe;
00053 }
00054 
00055 /// Adapts UNIX-style process pipes to a std::iostream interface.
00056 class rutz::shell_pipe
00057 {
00058 public:
00059   shell_pipe(const char* command, const char* mode);
00060 
00061   ~shell_pipe();
00062 
00063   std::iostream& stream() { return m_stream; }
00064 
00065   int close();
00066 
00067   bool is_closed() const { return (m_file == 0) || !m_stream.is_open(); }
00068 
00069   int exit_status() const { return m_exit_status; }
00070 
00071 private:
00072   shell_pipe(const shell_pipe&);
00073   shell_pipe& operator=(const shell_pipe&);
00074 
00075   FILE*             m_file;
00076   rutz::stdiostream m_stream;
00077   int               m_exit_status;
00078 };
00079 
00080 /// An exception-safe wrapper around a pair of file descriptors from pipe().
00081 class rutz::pipe_fds
00082 {
00083 public:
00084   /// Create a pipe pair of file descriptors.
00085   /** Throws an exception if the pipe() call fails. */
00086   pipe_fds();
00087 
00088   /// Destructor closes both file descriptors.
00089   ~pipe_fds() throw();
00090 
00091   int reader() const throw() { return m_fds[0]; }
00092   int writer() const throw() { return m_fds[1]; }
00093 
00094   void close_reader() throw() { if (m_fds[0] >= 0) close(m_fds[0]); m_fds[0] = -1; }
00095   void close_writer() throw() { if (m_fds[1] >= 0) close(m_fds[1]); m_fds[1] = -1; }
00096 
00097 private:
00098   pipe_fds(const pipe_fds&);
00099   pipe_fds& operator=(const pipe_fds&);
00100 
00101   int m_fds[2]; // reading == m_fds[0], writing == m_fds[1]
00102 };
00103 
00104 /// An exception-safe wrapper around a child process from fork().
00105 class rutz::child_process
00106 {
00107 public:
00108   /// Fork a child process.
00109   /** Throws an exception if the fork() call fails. */
00110   child_process();
00111 
00112   /// Destructor waits for child process to complete.
00113   ~child_process() throw();
00114 
00115   /// Check if we're in the parent or child process after the fork().
00116   bool in_parent() const throw() { return m_pid != 0; }
00117 
00118   /// Wait for child process to complete, and return its status code.
00119   int wait() throw();
00120 
00121 private:
00122   child_process(const child_process&);
00123   child_process& operator=(const child_process&);
00124 
00125   int m_child_status;
00126   pid_t m_pid;
00127 };
00128 
00129 
00130 /// An exception-safe wrapper around a pipe-fork-exec sequence.
00131 class rutz::exec_pipe : public nub::object
00132 {
00133 private:
00134   void init(char* const* argv);
00135 
00136 public:
00137   /// Set up a pipe to a child process with the given argv array.
00138   /** The mode should be "r" if the parent is reading, and "w" if the
00139       parent is writing. NOTE that the argv array MUST be
00140       NULL-terminated! */
00141   exec_pipe(const char* m, char* const* argv);
00142 
00143   /// Set up a pipe to a child process with the given list of args.
00144   /** The mode should be "r" if the parent is reading, and "w" if the
00145       parent is writing. NOTE that the variable length argument list
00146       MUST NULL-terminated! */
00147   exec_pipe(const char* m, const char* argv0, ...);
00148 
00149   /// Destructor cleans up child process and the pipe's file descriptors.
00150   ~exec_pipe() throw();
00151 
00152   /// Get the stream that is linked to the child process.
00153   std::iostream& stream() throw();
00154 
00155   /// Close the underlying stream AND the underlying file descriptor.
00156   void close();
00157 
00158   /// Wait for child process to complete, return 0 if all is OK, -1 if error.
00159   int exit_status() throw();
00160 
00161 private:
00162   bool               m_parent_is_reader;
00163   pipe_fds           m_fds;
00164   child_process      m_child;
00165   rutz::stdiostream* m_stream;
00166 };
00167 
00168 
00169 /// An exception-safe wrapper around a pipe-fork-exec sequence.
00170 class rutz::bidir_pipe
00171 {
00172 public:
00173   /// Default construct; you MUST call init() before using any of the streams!
00174   bidir_pipe();
00175 
00176   /// Set up a pipe to a child process with the given argv array.
00177   /** BE SURE the argv array is NULL-terminated! */
00178   bidir_pipe(char* const* argv);
00179 
00180   /// Set up a pipe to a child process with the given list of args.
00181   /** BE SURE the variable-length argument list is NULL-terminated!
00182 
00183       A private copy will be made of all the arguments, so it is safe
00184       to pass const strings here (such as the result of str.c_str()
00185       for a std::string object).
00186   */
00187   bidir_pipe(const char* argv0, ...);
00188 
00189   /// Destructor cleans up child process and the pipe's file descriptors.
00190   ~bidir_pipe() throw();
00191 
00192   /// Request that SIGINT be ignored in the child process.
00193   /** You must request this BEFORE calling init(); so the proper
00194       sequence is:
00195 
00196       \code
00197       rutz::bidir_pipe prog;
00198       prog.block_child_signals();
00199       prog.init("progname", "arg1", "arg2", NULL);
00200       \endcode
00201 
00202       This functionality is useful if the parent process is already
00203       handling SIGINT specially; if you don't block SIGINT in the
00204       child process, then when the user presses Ctrl-C, the following
00205       happens: (1) the main parent handles the SIGINT, as desired, but
00206       (2) the child process also receives a SIGINT, thus it dies, and
00207       (3) the parent process then gets a SIGPIPE and dies itself
00208       without getting a chance for a clean exit.
00209   */
00210   void block_child_sigint();
00211 
00212   /// Core code for starting the child process.
00213   /** NOTE the argv array must be NULL-terminated! */
00214   void init(char* const* argv);
00215 
00216   /// Core code for starting the child process.
00217   /** BE SURE the variable-length argument list is NULL-terminated! */
00218   void init(const char* argv0, ...);
00219 
00220   /// Get the stream that is receiving input from the child process.
00221   std::iostream& in_stream() throw();
00222 
00223   /// Get the stream that is sending output to the child process.
00224   std::iostream& out_stream() throw();
00225 
00226   /// Close the underlying input stream file descriptor.
00227   void close_in();
00228 
00229   /// Close the underlying output stream file descriptor.
00230   void close_out();
00231 
00232   /// Wait for child process to complete, return 0 if all is OK, -1 if error.
00233   int exit_status() throw();
00234 
00235 private:
00236   bidir_pipe(const bidir_pipe&); // not implemented
00237   bidir_pipe& operator=(const bidir_pipe&); // not implemented
00238 
00239   pipe_fds           m_in_pipe;
00240   pipe_fds           m_out_pipe;
00241   child_process      m_child;
00242   rutz::stdiostream* m_in_stream;
00243   rutz::stdiostream* m_out_stream;
00244   bool               m_block_child_sigint;
00245 };
00246 
00247 static const char __attribute__((used)) vcid_groovx_rutz_pipe_h_utc20050626084019[] = "$Id: pipe.h 11238 2009-05-28 01:05:46Z jshen $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/pipe.h $";
00248 #endif // !GROOVX_RUTZ_PIPE_H_UTC20050626084019_DEFINED
Generated on Sun May 8 08:42:12 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3