Commit | Line | Data |
---|---|---|
0e23f538 TJ |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | */ | |
e93545dd GE |
20 | /*************************************************************************** |
21 | inpipestream.hxx - C++ streambuffer wrapper | |
22 | ------------------- | |
23 | begin : Thu Dec 27 2001 | |
24 | copyright : (C) 2001 by Intra2net AG | |
e93545dd GE |
25 | ***************************************************************************/ |
26 | ||
27 | #ifndef _PIPESTREAM | |
28 | #define _PIPESTREAM | |
29 | ||
55a22930 | 30 | #include <sys/prctl.h> |
be3dbe32 TJ |
31 | #include <stdio.h> |
32 | ||
ad4490f1 | 33 | #include <cstring> |
e93545dd GE |
34 | #include <string> |
35 | #include <streambuf> | |
4a56f9ac GE |
36 | #include <istream> |
37 | #include <ostream> | |
c2c29997 | 38 | #include <vector> |
e93545dd | 39 | |
8621037f PG |
40 | #include <stringfunc.hxx> |
41 | ||
f8f119bf GE |
42 | struct ExecResult |
43 | { | |
44 | /** if the program exited normally and returned a return code */ | |
45 | bool normal_exit; | |
46 | ||
47 | /** the real return code of the program, only set when normal_exit true */ | |
48 | char return_code; | |
49 | ||
50 | /** if the program was terminated by a signal */ | |
51 | bool terminated_by_signal; | |
52 | ||
53 | /** number of the signal that terminated the program, only valid when terminated_by_signal true */ | |
54 | int signal; | |
55 | ||
56 | /** errormessage if we have one */ | |
57 | std::string error_message; | |
8621037f PG |
58 | |
59 | inline std::string format (void) const | |
60 | { | |
61 | return std::string ("(") | |
62 | + "(normal_exit " + (this->normal_exit ? "T" : "F") + ") " | |
63 | "(return_code '" + I2n::to_string ((int)this->return_code) + "') " | |
64 | "(signal " + (this->terminated_by_signal | |
65 | ? strsignal (this->signal) | |
66 | : "<nil>") + "))" | |
67 | ; | |
68 | }; | |
f8f119bf GE |
69 | }; |
70 | typedef struct ExecResult ExecResult; | |
71 | ||
825c519f PG |
72 | namespace capture_flag { |
73 | ||
74 | static const int none = 0; | |
75 | static const int collect_out = 1 << 0; | |
76 | static const int collect_err = 1 << 1; | |
77 | static const int search_path = 1 << 2; | |
78 | static const int env_passthru = 1 << 3; | |
55a22930 | 79 | static const int no_new_privs = 1 << 4; |
825c519f PG |
80 | |
81 | static const int dflt = collect_out; | |
82 | static const int collect_any = collect_out | collect_err; | |
83 | ||
84 | } /* [namespace capture_flag] */ | |
85 | ||
f8f119bf | 86 | std::string capture_exec(const std::string& command, ExecResult &rescode); |
cc917897 | 87 | std::string capture_exec(const char *const *command, ExecResult &rescode, |
825c519f | 88 | const int flags=capture_flag::dflt); |
cc917897 | 89 | std::string capture_exec(const std::vector<std::string>& command, ExecResult &rescode, |
825c519f | 90 | const int flags=capture_flag::dflt); |
f8f119bf | 91 | |
9e322fb7 PG |
92 | inline std::string capture_exec (const std::string &command) |
93 | { | |
94 | ExecResult r; | |
95 | return capture_exec(command,r); | |
96 | } | |
97 | ||
98 | inline std::string capture_exec(const char *const *command) | |
f8f119bf GE |
99 | { |
100 | ExecResult r; | |
101 | return capture_exec(command,r); | |
102 | } | |
103 | ||
c2c29997 PG |
104 | inline std::string capture_exec(const std::vector<std::string>& command) |
105 | { | |
106 | ExecResult r; | |
107 | return capture_exec(command,r); | |
108 | } | |
109 | ||
bf4c487c CH |
110 | /** @brief runs command and provides buffered input for it through pipe |
111 | * | |
112 | * opens pipe to command using popen; exit status available after destruction | |
113 | * (use WEXITSTATUS to get the "regular" return code (lowest byte)) | |
114 | * | |
9050e9f7 | 115 | * ATTENTION: A lot of mysterious STL bugs occurred |
bf4c487c CH |
116 | * with a "real" buffer (buffer larger than 1 byte and up to 100 bytes) |
117 | * -> Keep it slow and working! | |
118 | */ | |
e93545dd GE |
119 | class inpipebuf : public std::streambuf |
120 | { | |
20a79471 TJ |
121 | protected: |
122 | char buffer; | |
9e322fb7 | 123 | |
20a79471 | 124 | FILE *pipe; |
17b459b3 | 125 | pid_t pid; |
20a79471 TJ |
126 | |
127 | // "callback" variables for destructor to store exit status | |
128 | bool *status_set; | |
129 | int *exit_status; | |
130 | ||
131 | public: | |
825c519f PG |
132 | inpipebuf(const std::string& command, const int flags); |
133 | inpipebuf(const char *const *command, const int flags); | |
134 | inpipebuf(const std::vector<std::string> &command, const int flags); | |
20a79471 | 135 | |
7e606af5 | 136 | ~inpipebuf(); |
20a79471 | 137 | |
7e606af5 | 138 | void store_exit_status(bool *_status_set, int *_exit_status); |
20a79471 TJ |
139 | |
140 | protected: | |
7e606af5 | 141 | virtual int_type underflow(); |
9e322fb7 PG |
142 | |
143 | private: | |
17b459b3 | 144 | std::pair <pid_t, FILE *> |
825c519f | 145 | init_without_shell (const char *const *argv, const int flags) const; |
20a79471 | 146 | }; |
e93545dd | 147 | |
bf4c487c | 148 | /** @brief stream around inpipebuf -- see comment there */ |
e93545dd GE |
149 | class inpipestream : public std::istream |
150 | { | |
20a79471 TJ |
151 | protected: |
152 | inpipebuf buf; | |
153 | ||
154 | public: | |
cc917897 | 155 | inpipestream(const std::string& command, |
825c519f PG |
156 | const int flags=capture_flag::dflt) |
157 | : std::istream(&buf), buf(command, flags) | |
20a79471 | 158 | {} |
cc917897 PG |
159 | |
160 | inpipestream(const char *const command[], | |
825c519f PG |
161 | const int flags=capture_flag::dflt) |
162 | : std::istream(&buf), buf(command, flags) | |
9e322fb7 | 163 | {} |
20a79471 | 164 | |
cc917897 | 165 | inpipestream(const std::vector<std::string> &command, |
825c519f PG |
166 | const int flags=capture_flag::dflt) |
167 | : std::istream(&buf), buf(command, flags) | |
c2c29997 PG |
168 | {} |
169 | ||
20a79471 TJ |
170 | void store_exit_status(bool *_status_set, int *_exit_status) |
171 | { buf.store_exit_status(_status_set, _exit_status); } | |
e93545dd GE |
172 | }; |
173 | ||
bf4c487c CH |
174 | /** @brief runs command and provides buffered ouptput from it through pipe |
175 | * | |
176 | * opens pipe to command using popen; exit status available after destruction | |
177 | * (use WEXITSTATUS to get the "regular" return code (lowest byte)) | |
178 | */ | |
e93545dd GE |
179 | class outpipebuf : public std::streambuf |
180 | { | |
20a79471 TJ |
181 | protected: |
182 | FILE *pipe; | |
183 | ||
184 | // "callback" variables for destructor to store exit status | |
185 | bool *status_set; | |
186 | int *exit_status; | |
187 | ||
188 | public: | |
7e606af5 GE |
189 | outpipebuf(const std::string& command); |
190 | ||
191 | ~outpipebuf(); | |
20a79471 | 192 | |
bf4c487c | 193 | /** note: exit status only available after destruction */ |
7e606af5 | 194 | void store_exit_status(bool *_status_set, int *_exit_status); |
20a79471 TJ |
195 | |
196 | protected: | |
7e606af5 GE |
197 | virtual int_type overflow(int_type c); |
198 | ||
199 | virtual std::streamsize xsputn(const char* s, std::streamsize num); | |
e93545dd GE |
200 | }; |
201 | ||
bf4c487c CH |
202 | |
203 | /** @brief stream around outpipebuf -- see comment there */ | |
e93545dd GE |
204 | class outpipestream : public std::ostream |
205 | { | |
20a79471 TJ |
206 | protected: |
207 | outpipebuf buf; | |
208 | public: | |
209 | outpipestream(const std::string& command) | |
d8ffcdb0 | 210 | : std::ostream(&buf), buf(command) |
20a79471 TJ |
211 | {} |
212 | ||
213 | void store_exit_status(bool *_status_set, int *_exit_status) | |
214 | { buf.store_exit_status(_status_set, _exit_status); } | |
e93545dd GE |
215 | }; |
216 | ||
217 | #endif |