HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_WritePipe.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_WritePipe.h ( UT Library, C++)
7  *
8  * COMMENTS: This is a write pipe which allows you to poll the pipe to see
9  * if the child process has completed. It works almost exactly
10  * like popen/pclose except for a few things:
11  * 1: You can easily get the pid of the child
12  * 2: The close() method has an option to not block
13  * and return even if the child is running.
14  *
15  * CAVEATS: This class does not work for read pipes, but then again, read
16  * pipes don't have the problem of blocking on the child
17  * process...
18  */
19 
20 #ifndef __UT_WritePipe__
21 #define __UT_WritePipe__
22 
23 #include "UT_API.h"
24 #include <sys/types.h>
25 #include <stdio.h>
26 #include "UT_Lock.h"
27 #include "UT_SysClone.h"
28 
30 public:
31  // change_process_group will put the child process in a new process group
32  // to isolate the parent from any crashes coming from the child.
33  explicit UT_WritePipe(const char *cmd = 0,
34  bool change_process_group = false);
35 
36  // If the pipe is open and destructed, the process will be blocked until
37  // the child is terminated.
38  ~UT_WritePipe();
39 
40  /// Open a command as a write pipe. The file descriptor returned will be
41  /// the command's stdin.
42  FILE *open(const char *cmd)
43  { return open(cmd, NULL, NULL); }
44 
45  /// Open a command as a write pipe. The file descriptor returned will be
46  /// the command's stdin. If the @c kid_stdout file descriptor is given,
47  /// this will become stdout for the child. If @c kid_stderr is specified,
48  /// it will be come stderr for the child.
49  FILE *open(const char *cmd,
50  FILE *kid_stdout,
51  FILE *kid_stderr);
52 
53  // Close: Close will return
54  // -1 - Error closing the pipe
55  // 0 - Child is still running (only returned if the arg is 0)
56  // >0 - Child is terminated
57  // After close is called, it is not safe to write to the pipe anymore
58  // (since the file descriptor is closed). Please use isComplete() to see
59  // if the child process has terminated.
60  int close(int wait_for_child_to_terminate = 0);
61 
62  // Call this to find out whether it's safe to close (i.e. the child process
63  // has terminated). This can be called before, or after the pipe has been
64  // closed.
65  int isComplete(int block=0);
66 
67  void suspend();
68  void restart();
69  void terminate();
70  int isSuspended() const;
71 
72  FILE *getFilePtr() { return myFilePtr; }
73 
74  // If close() fails, you can get the errno of the fail call by calling
75  // this method. If the child is still running, the error will be
76  // set to EWOULDBLOCK
77  int getErrno() const { return myErrno; }
78 
79  // If close() succeeds, you can get the exit status of the child process by
80  // calling the following method.
81  int getStatus() const { return myExitStatus; }
82 
83  // This method will return the child process id.
84  pid_t getPid() const { return myPid; }
85 
86 #ifdef WIN32
87  FILE *doPOpen( char *cmd, FILE *kstdout, FILE *kstderr );
88 #endif
89 
90 private:
91  FILE *myFilePtr;
92  int myErrno;
93  int myExitStatus;
94  unsigned myFlag;
95  pid_t myPid;
96  bool myChangeProcessGroup;
97 #ifdef WIN32
98  void *myHThread;
99  void *myHProcess;
100  void *myPipe;
101 #else
102  UT_Lock myCompleteLock;
103 #endif
104 };
105 
106 #endif
107 
pid_t getPid() const
Definition: UT_WritePipe.h:84
FILE * open(const char *cmd)
Definition: UT_WritePipe.h:42
int getStatus() const
Definition: UT_WritePipe.h:81
#define UT_API
Definition: UT_API.h:12
FILE * getFilePtr()
Definition: UT_WritePipe.h:72
virtual int open(float queuesize)
virtual void close()
int getErrno() const
Definition: UT_WritePipe.h:77