-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystemcl.c
293 lines (265 loc) · 7.5 KB
/
systemcl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
| Copyright (c) Seiden Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Rasmus Lerdorf <rasmus@php.net> |
| Ilia Alshanetsky <iliaa@php.net> |
| Calvin Buckley <calvin@cmpct.info> |
+----------------------------------------------------------------------+
*/
/*
* This file is based on ext/standard/exec.c from PHP, and retains its
* copyright and authorship notices.
*/
#include "php_ibmi_int.h"
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <as400_protos.h>
/**
* We prefer building the flags instead of passing them to raw to PHP because
* the default semantics are tricky for the average IBM i user.
*/
static int build_systemCL_flags(PhpSystemClFlags php_flags)
{
int new_flags = 0;
/* Capture spool to stdout, like "system" does. Maybe keep it later. */
/* XXX: make optional? */
new_flags |= SYSTEMCL_SPOOL_STDOUT;
/* Do the right thing by default (ASCII translation, out into EBCDIC) */
if (!(php_flags & IBMI_CL_EBCDIC_OUTPUT)) {
/* XXX: We don't filter stdin because we don't expect it */
new_flags |= (SYSTEMCL_FILTER_STDOUT | SYSTEMCL_FILTER_STDERR);
}
if (php_flags & IBMI_CL_SPAWN) {
new_flags |= SYSTEMCL_SPAWN;
}
if (!(php_flags & IBMI_CL_NO_MESSAGES)) {
/* It doesn't really matter for now, since it's all jumbled... */
new_flags |= SYSTEMCL_MSG_STDOUT;
}
/* XXX: keep spool, environment, remove msgid */
return new_flags;
}
/**
* Doesn't fork, but temporarily replaces stdout/stderr into a single stream.
* This allows PHP to intercept the output from systemCL, while executing in
* the context of the PHP process (so i.e chgjob works).
*/
static FILE* php_ibmi_popen(const char *cl, int flags)
{
int fds[2], tmpout = -1, tmperr = -1;
/* read from fds[0], dup fds[1] as XPF's stdout/stderr */
if (pipe(fds) == -1) {
return NULL;
}
/* XXX: Set CLOEXEC for new pipes, and if needed on out/err. Mutex? */
tmpout = dup(1);
if (tmpout == -1) {
goto fail;
}
tmperr = dup(2);
if (tmperr == -1) {
goto fail;
}
/* XXX: error checking here */
dup2(fds[1], 1);
dup2(fds[1], 2);
systemCL(cl, flags);
dup2(tmpout, 1);
dup2(tmperr, 2);
fsync(fds[1]);
close(fds[1]);
return fdopen(fds[0], "r");
fail:
if (tmpout != -1) {
close(tmpout);
}
if (tmperr != -1) {
close(tmperr);
}
close(fds[0]);
close(fds[1]);
return NULL;
}
static size_t strip_trailing_whitespace(char *buf, size_t bufl) {
size_t l = bufl;
while (l-- > 0 && isspace(((unsigned char *)buf)[l]));
if (l != (bufl - 1)) {
bufl = l + 1;
buf[bufl] = '\0';
}
return bufl;
}
static size_t handle_line(int type, zval *array, char *buf, size_t bufl) {
if (type == 1) {
PHPWRITE(buf, bufl);
if (php_output_get_level() < 1) {
sapi_flush();
}
} else if (type == 2) {
bufl = strip_trailing_whitespace(buf, bufl);
add_next_index_stringl(array, buf, bufl);
}
return bufl;
}
/* {{{ php_ibmi_exec
* If type==0, only last line of output is returned (exec)
* If type==1, all lines will be printed and last lined returned (system)
* If type==2, all lines will be saved to given array (exec with &$array)
* If type==3, output will be printed binary, no lines will be saved or returned (passthru)
*
*/
PHPAPI int php_ibmi_exec(int type, const char *cmd, PhpSystemClFlags flags, zval *array, zval *return_value)
{
FILE *fp;
char *buf;
int pclose_return;
char *b, *d=NULL;
php_stream *stream;
size_t buflen, bufl = 0;
#if PHP_SIGCHILD
void (*sig_handler)() = NULL;
#endif
#if PHP_SIGCHILD
sig_handler = signal (SIGCHLD, SIG_DFL);
#endif
fp = php_ibmi_popen(cmd, build_systemCL_flags(flags));
if (!fp) {
php_error_docref(NULL, E_WARNING, "Unable to fork [%s]", cmd);
goto err;
}
stream = php_stream_fopen_from_pipe(fp, "rb");
buf = (char *) emalloc(EXEC_INPUT_BUF);
buflen = EXEC_INPUT_BUF;
if (type != 3) {
b = buf;
while (php_stream_get_line(stream, b, EXEC_INPUT_BUF, &bufl)) {
/* no new line found, let's read some more */
if (b[bufl - 1] != '\n' && !php_stream_eof(stream)) {
if (buflen < (bufl + (b - buf) + EXEC_INPUT_BUF)) {
bufl += b - buf;
buflen = bufl + EXEC_INPUT_BUF;
buf = erealloc(buf, buflen);
b = buf + bufl;
} else {
b += bufl;
}
continue;
} else if (b != buf) {
bufl += b - buf;
}
bufl = handle_line(type, array, buf, bufl);
b = buf;
}
if (bufl) {
if (buf != b) {
/* Process remaining output */
bufl = handle_line(type, array, buf, bufl);
}
/* Return last line from the shell command */
bufl = strip_trailing_whitespace(buf, bufl);
RETVAL_STRINGL(buf, bufl);
} else { /* should return NULL, but for BC we return "" */
RETVAL_EMPTY_STRING();
}
} else {
ssize_t read;
while ((read = php_stream_read(stream, buf, EXEC_INPUT_BUF)) > 0) {
PHPWRITE(buf, read);
}
}
pclose_return = php_stream_close(stream);
efree(buf);
done:
#if PHP_SIGCHILD
if (sig_handler) {
signal(SIGCHLD, sig_handler);
}
#endif
if (d) {
efree(d);
}
return pclose_return;
err:
pclose_return = -1;
RETVAL_FALSE;
goto done;
}
/* }}} */
static void php_ibmi_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
{
char *cmd;
size_t cmd_len;
zval *ret_array=NULL;
int ret;
zend_long flags = 0;
ZEND_PARSE_PARAMETERS_START(1, (mode ? 2 : 3))
Z_PARAM_STRING(cmd, cmd_len)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(flags);
if (!mode) {
Z_PARAM_ZVAL(ret_array)
}
ZEND_PARSE_PARAMETERS_END();
if (!cmd_len) {
zend_argument_value_error(1, "cannot be empty");
RETURN_THROWS();
}
if (strlen(cmd) != cmd_len) {
zend_argument_value_error(1, "must not contain any null bytes");
RETURN_THROWS();
}
if (!ret_array) {
ret = php_ibmi_exec(mode, cmd, flags, NULL, return_value);
} else {
if (Z_TYPE_P(Z_REFVAL_P(ret_array)) == IS_ARRAY) {
ZVAL_DEREF(ret_array);
SEPARATE_ARRAY(ret_array);
} else {
#if PHP_VERSION_ID >= 70400
ret_array = zend_try_array_init(ret_array);
#else
zval_ptr_dtor(ret_array);
array_init(ret_array);
#endif
if (!ret_array) {
RETURN_THROWS();
}
}
ret = php_ibmi_exec(2, cmd, flags, ret_array, return_value);
}
}
/* }}} */
/* {{{ Execute an external program */
PHP_FUNCTION(ibmi_cl_exec)
{
php_ibmi_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ Execute an external program and display output */
PHP_FUNCTION(ibmi_cl_system)
{
php_ibmi_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ Execute an external program and display raw output */
PHP_FUNCTION(ibmi_cl_passthru)
{
/* TODO: This could just call systemCL directly for a fast path? */
php_ibmi_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
}
/* }}} */