summaryrefslogtreecommitdiff
path: root/src/stable/process.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable/process.h')
-rw-r--r--src/stable/process.h153
1 files changed, 153 insertions, 0 deletions
diff --git a/src/stable/process.h b/src/stable/process.h
new file mode 100644
index 0000000..d6282e0
--- /dev/null
+++ b/src/stable/process.h
@@ -0,0 +1,153 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_PROCESS_H
9#define BU_PROCESS_H
10
11#include <stdint.h>
12#include <sys/types.h>
13
14#include "bu/stream.h"
15#include "bu/string.h"
16
17namespace Bu
18{
19 /**
20 * Runs a program and attaches streams to it's stdin, stdout, and stderr.
21 * Reading from a Bu::Process will read from the program's standard output,
22 * writing to a Bu::Process will write to the program's standard input.
23 */
24 class Process : public Bu::Stream
25 {
26 public:
27 enum Flags
28 {
29 None = 0x00,
30 StdOut = 0x01,
31 StdErr = 0x02,
32 Both = 0x03
33 };
34
35 public:
36 class Options
37 {
38 public:
39 enum OptFlags
40 {
41 None = 0x00,
42 SetUid = 0x01,
43 SetGid = 0x02,
44 };
45
46 Options() : eFlags( None ) {}
47
48 int eFlags;
49 int iUid;
50 int iGid;
51 };
52
53 Process( Flags eFlags, const char *sName, char *const argv[] );
54 Process( Flags eFlags, const char *sName, const char *argv, ...);
55 Process( Flags eFlags, const Options &opt, const char *sName, char *const argv[] );
56 Process( Flags eFlags, const Options &opt, const char *sName, const char *argv, ...);
57 virtual ~Process();
58
59 /**
60 * Waits until the process exits. This blocks the caller until the
61 * child process terminates.
62 */
63 void wait();
64
65 virtual void close();
66 virtual void closeStdIn();
67 virtual void closeStdOut();
68 virtual Bu::size read( void *pBuf, Bu::size nBytes );
69 virtual Bu::size readErr( void *pBuf, Bu::size nBytes );
70 virtual Bu::size write( const void *pBuf, Bu::size nBytes );
71 using Stream::write;
72
73 virtual Bu::size tell();
74 virtual void seek( Bu::size offset );
75 virtual void setPos( Bu::size pos );
76 virtual void setPosEnd( Bu::size pos );
77 virtual bool isEos();
78 virtual bool isOpen();
79
80 virtual void flush();
81
82 virtual bool canRead();
83 virtual bool canWrite();
84
85 virtual bool isReadable();
86 virtual bool isWritable();
87 virtual bool isSeekable();
88
89 virtual bool isBlocking();
90 virtual void setBlocking( bool bBlocking=true );
91
92 virtual void setSize( Bu::size iSize );
93
94 virtual size getBlockSize() const;
95 virtual size getSize() const;
96 virtual Bu::String getLocation() const;
97
98 void select( bool &bStdOut, bool &bStdErr );
99
100 bool isRunning();
101 void ignoreStdErr();
102
103 /**
104 * Returns the pid of the child process, or zero if there is no
105 * currently running child. Note that a read operation must be
106 * performed in order to discover that the child has ended.
107 */
108 pid_t getPid();
109
110 /**
111 * Returns true if the child exited normally (by calling exit or
112 * returning from main).
113 */
114 bool childExited();
115
116 /**
117 * Returns the 8 bit integer value returned from the child program if
118 * childExited returned true.
119 */
120 int childExitStatus();
121
122 /**
123 * Returns true if the child exited because of a signal.
124 */
125 bool childSignaled();
126
127 /**
128 * Returns the signal ID if the childSignaled return true.
129 */
130 int childSignal();
131
132 /**
133 * Returns true if the child left a core dump behind when it exited.
134 */
135 bool childCoreDumped();
136
137 private:
138 int iStdIn;
139 int iStdOut;
140 int iStdErr;
141 pid_t iPid;
142 int iProcStatus;
143 bool bBlocking;
144 bool bStdOutEos;
145 bool bStdErrEos;
146
147 void gexec( Flags eFlags, const char *sName, char *const argv[] );
148 void checkClose();
149 Options opt;
150 };
151}
152
153#endif