1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2017 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Wez Furlong <wez@thebrainroom.com> |
16 +----------------------------------------------------------------------+
17 */
18 /* $Id$ */
19
20 #if 0 && (defined(__linux__) || defined(sun) || defined(__IRIX__))
21 # define _BSD_SOURCE /* linux wants this when XOPEN mode is on */
22 # define _BSD_COMPAT /* irix: uint */
23 # define _XOPEN_SOURCE 500 /* turn on Unix98 */
24 # define __EXTENSIONS__ 1 /* Solaris: uint */
25 #endif
26
27 #include "php.h"
28 #include <stdio.h>
29 #include <ctype.h>
30 #include "php_string.h"
31 #include "ext/standard/head.h"
32 #include "ext/standard/basic_functions.h"
33 #include "ext/standard/file.h"
34 #include "exec.h"
35 #include "php_globals.h"
36 #include "SAPI.h"
37 #include "main/php_network.h"
38
39 #ifdef NETWARE
40 #include <proc.h>
41 #include <library.h>
42 #endif
43
44 #if HAVE_SYS_WAIT_H
45 #include <sys/wait.h>
46 #endif
47 #if HAVE_SIGNAL_H
48 #include <signal.h>
49 #endif
50
51 #if HAVE_SYS_STAT_H
52 #include <sys/stat.h>
53 #endif
54 #if HAVE_FCNTL_H
55 #include <fcntl.h>
56 #endif
57
58 /* This symbol is defined in ext/standard/config.m4.
59 * Essentially, it is set if you HAVE_FORK || PHP_WIN32
60 * Other platforms may modify that configure check and add suitable #ifdefs
61 * around the alternate code.
62 * */
63 #ifdef PHP_CAN_SUPPORT_PROC_OPEN
64
65 #if 0 && HAVE_PTSNAME && HAVE_GRANTPT && HAVE_UNLOCKPT && HAVE_SYS_IOCTL_H && HAVE_TERMIOS_H
66 # include <sys/ioctl.h>
67 # include <termios.h>
68 # define PHP_CAN_DO_PTS 1
69 #endif
70
71 #include "proc_open.h"
72
73 static int le_proc_open;
74
75 /* {{{ _php_array_to_envp */
_php_array_to_envp(zval * environment,int is_persistent)76 static php_process_env_t _php_array_to_envp(zval *environment, int is_persistent)
77 {
78 zval *element;
79 php_process_env_t env;
80 zend_string *key, *str;
81 #ifndef PHP_WIN32
82 char **ep;
83 #endif
84 char *p;
85 size_t cnt, l, sizeenv = 0;
86 HashTable *env_hash;
87
88 memset(&env, 0, sizeof(env));
89
90 if (!environment) {
91 return env;
92 }
93
94 cnt = zend_hash_num_elements(Z_ARRVAL_P(environment));
95
96 if (cnt < 1) {
97 #ifndef PHP_WIN32
98 env.envarray = (char **) pecalloc(1, sizeof(char *), is_persistent);
99 #endif
100 env.envp = (char *) pecalloc(4, 1, is_persistent);
101 return env;
102 }
103
104 ALLOC_HASHTABLE(env_hash);
105 zend_hash_init(env_hash, cnt, NULL, NULL, 0);
106
107 /* first, we have to get the size of all the elements in the hash */
108 ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(environment), key, element) {
109 str = zval_get_string(element);
110
111 if (ZSTR_LEN(str) == 0) {
112 zend_string_release(str);
113 continue;
114 }
115
116 sizeenv += ZSTR_LEN(str) + 1;
117
118 if (key && ZSTR_LEN(key)) {
119 sizeenv += ZSTR_LEN(key) + 1;
120 zend_hash_add_ptr(env_hash, key, str);
121 } else {
122 zend_hash_next_index_insert_ptr(env_hash, str);
123 }
124 } ZEND_HASH_FOREACH_END();
125
126 #ifndef PHP_WIN32
127 ep = env.envarray = (char **) pecalloc(cnt + 1, sizeof(char *), is_persistent);
128 #endif
129 p = env.envp = (char *) pecalloc(sizeenv + 4, 1, is_persistent);
130
131 ZEND_HASH_FOREACH_STR_KEY_PTR(env_hash, key, str) {
132 if (key) {
133 l = ZSTR_LEN(key) + ZSTR_LEN(str) + 2;
134 memcpy(p, ZSTR_VAL(key), ZSTR_LEN(key));
135 strncat(p, "=", 1);
136 strncat(p, ZSTR_VAL(str), ZSTR_LEN(str));
137
138 #ifndef PHP_WIN32
139 *ep = p;
140 ++ep;
141 #endif
142 p += l;
143 } else {
144 memcpy(p, ZSTR_VAL(str), ZSTR_LEN(str));
145 #ifndef PHP_WIN32
146 *ep = p;
147 ++ep;
148 #endif
149 p += ZSTR_LEN(str) + 1;
150 }
151 zend_string_release(str);
152 } ZEND_HASH_FOREACH_END();
153
154 assert((uint)(p - env.envp) <= sizeenv);
155
156 zend_hash_destroy(env_hash);
157 FREE_HASHTABLE(env_hash);
158
159 return env;
160 }
161 /* }}} */
162
163 /* {{{ _php_free_envp */
_php_free_envp(php_process_env_t env,int is_persistent)164 static void _php_free_envp(php_process_env_t env, int is_persistent)
165 {
166 #ifndef PHP_WIN32
167 if (env.envarray) {
168 pefree(env.envarray, is_persistent);
169 }
170 #endif
171 if (env.envp) {
172 pefree(env.envp, is_persistent);
173 }
174 }
175 /* }}} */
176
177 /* {{{ proc_open_rsrc_dtor */
proc_open_rsrc_dtor(zend_resource * rsrc)178 static void proc_open_rsrc_dtor(zend_resource *rsrc)
179 {
180 struct php_process_handle *proc = (struct php_process_handle*)rsrc->ptr;
181 int i;
182 #ifdef PHP_WIN32
183 DWORD wstatus;
184 #elif HAVE_SYS_WAIT_H
185 int wstatus;
186 int waitpid_options = 0;
187 pid_t wait_pid;
188 #endif
189
190 /* Close all handles to avoid a deadlock */
191 for (i = 0; i < proc->npipes; i++) {
192 if (proc->pipes[i] != 0) {
193 GC_REFCOUNT(proc->pipes[i])--;
194 zend_list_close(proc->pipes[i]);
195 proc->pipes[i] = 0;
196 }
197 }
198
199 #ifdef PHP_WIN32
200 if (FG(pclose_wait)) {
201 WaitForSingleObject(proc->childHandle, INFINITE);
202 }
203 GetExitCodeProcess(proc->childHandle, &wstatus);
204 if (wstatus == STILL_ACTIVE) {
205 FG(pclose_ret) = -1;
206 } else {
207 FG(pclose_ret) = wstatus;
208 }
209 CloseHandle(proc->childHandle);
210
211 #elif HAVE_SYS_WAIT_H
212
213 if (!FG(pclose_wait)) {
214 waitpid_options = WNOHANG;
215 }
216 do {
217 wait_pid = waitpid(proc->child, &wstatus, waitpid_options);
218 } while (wait_pid == -1 && errno == EINTR);
219
220 if (wait_pid <= 0) {
221 FG(pclose_ret) = -1;
222 } else {
223 if (WIFEXITED(wstatus))
224 wstatus = WEXITSTATUS(wstatus);
225 FG(pclose_ret) = wstatus;
226 }
227
228 #else
229 FG(pclose_ret) = -1;
230 #endif
231 _php_free_envp(proc->env, proc->is_persistent);
232 pefree(proc->pipes, proc->is_persistent);
233 pefree(proc->command, proc->is_persistent);
234 pefree(proc, proc->is_persistent);
235
236 }
237 /* }}} */
238
239 /* {{{ PHP_MINIT_FUNCTION(proc_open) */
PHP_MINIT_FUNCTION(proc_open)240 PHP_MINIT_FUNCTION(proc_open)
241 {
242 le_proc_open = zend_register_list_destructors_ex(proc_open_rsrc_dtor, NULL, "process", module_number);
243 return SUCCESS;
244 }
245 /* }}} */
246
247 /* {{{ proto bool proc_terminate(resource process [, long signal])
248 kill a process opened by proc_open */
PHP_FUNCTION(proc_terminate)249 PHP_FUNCTION(proc_terminate)
250 {
251 zval *zproc;
252 struct php_process_handle *proc;
253 zend_long sig_no = SIGTERM;
254
255 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &zproc, &sig_no) == FAILURE) {
256 RETURN_FALSE;
257 }
258
259 if ((proc = (struct php_process_handle *)zend_fetch_resource(Z_RES_P(zproc), "process", le_proc_open)) == NULL) {
260 RETURN_FALSE;
261 }
262
263 #ifdef PHP_WIN32
264 if (TerminateProcess(proc->childHandle, 255)) {
265 RETURN_TRUE;
266 } else {
267 RETURN_FALSE;
268 }
269 #else
270 if (kill(proc->child, sig_no) == 0) {
271 RETURN_TRUE;
272 } else {
273 RETURN_FALSE;
274 }
275 #endif
276 }
277 /* }}} */
278
279 /* {{{ proto int proc_close(resource process)
280 close a process opened by proc_open */
PHP_FUNCTION(proc_close)281 PHP_FUNCTION(proc_close)
282 {
283 zval *zproc;
284 struct php_process_handle *proc;
285
286 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zproc) == FAILURE) {
287 RETURN_FALSE;
288 }
289
290 if ((proc = (struct php_process_handle *)zend_fetch_resource(Z_RES_P(zproc), "process", le_proc_open)) == NULL) {
291 RETURN_FALSE;
292 }
293
294 FG(pclose_wait) = 1;
295 zend_list_close(Z_RES_P(zproc));
296 FG(pclose_wait) = 0;
297 RETURN_LONG(FG(pclose_ret));
298 }
299 /* }}} */
300
301 /* {{{ proto array proc_get_status(resource process)
302 get information about a process opened by proc_open */
PHP_FUNCTION(proc_get_status)303 PHP_FUNCTION(proc_get_status)
304 {
305 zval *zproc;
306 struct php_process_handle *proc;
307 #ifdef PHP_WIN32
308 DWORD wstatus;
309 #elif HAVE_SYS_WAIT_H
310 int wstatus;
311 pid_t wait_pid;
312 #endif
313 int running = 1, signaled = 0, stopped = 0;
314 int exitcode = -1, termsig = 0, stopsig = 0;
315
316 if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zproc) == FAILURE) {
317 RETURN_FALSE;
318 }
319
320 if ((proc = (struct php_process_handle *)zend_fetch_resource(Z_RES_P(zproc), "process", le_proc_open)) == NULL) {
321 RETURN_FALSE;
322 }
323
324 array_init(return_value);
325
326 add_assoc_string(return_value, "command", proc->command);
327 add_assoc_long(return_value, "pid", (zend_long) proc->child);
328
329 #ifdef PHP_WIN32
330
331 GetExitCodeProcess(proc->childHandle, &wstatus);
332
333 running = wstatus == STILL_ACTIVE;
334 exitcode = running ? -1 : wstatus;
335
336 #elif HAVE_SYS_WAIT_H
337
338 errno = 0;
339 wait_pid = waitpid(proc->child, &wstatus, WNOHANG|WUNTRACED);
340
341 if (wait_pid == proc->child) {
342 if (WIFEXITED(wstatus)) {
343 running = 0;
344 exitcode = WEXITSTATUS(wstatus);
345 }
346 if (WIFSIGNALED(wstatus)) {
347 running = 0;
348 signaled = 1;
349 #ifdef NETWARE
350 termsig = WIFTERMSIG(wstatus);
351 #else
352 termsig = WTERMSIG(wstatus);
353 #endif
354 }
355 if (WIFSTOPPED(wstatus)) {
356 stopped = 1;
357 stopsig = WSTOPSIG(wstatus);
358 }
359 } else if (wait_pid == -1) {
360 running = 0;
361 }
362 #endif
363
364 add_assoc_bool(return_value, "running", running);
365 add_assoc_bool(return_value, "signaled", signaled);
366 add_assoc_bool(return_value, "stopped", stopped);
367 add_assoc_long(return_value, "exitcode", exitcode);
368 add_assoc_long(return_value, "termsig", termsig);
369 add_assoc_long(return_value, "stopsig", stopsig);
370 }
371 /* }}} */
372
373 /* {{{ handy definitions for portability/readability */
374 #ifdef PHP_WIN32
375 # define pipe(pair) (CreatePipe(&pair[0], &pair[1], &security, 0) ? 0 : -1)
376
377 # define COMSPEC_NT "cmd.exe"
378
dup_handle(HANDLE src,BOOL inherit,BOOL closeorig)379 static inline HANDLE dup_handle(HANDLE src, BOOL inherit, BOOL closeorig)
380 {
381 HANDLE copy, self = GetCurrentProcess();
382
383 if (!DuplicateHandle(self, src, self, ©, 0, inherit, DUPLICATE_SAME_ACCESS |
384 (closeorig ? DUPLICATE_CLOSE_SOURCE : 0)))
385 return NULL;
386 return copy;
387 }
388
dup_fd_as_handle(int fd)389 static inline HANDLE dup_fd_as_handle(int fd)
390 {
391 return dup_handle((HANDLE)_get_osfhandle(fd), TRUE, FALSE);
392 }
393
394 # define close_descriptor(fd) CloseHandle(fd)
395 #else
396 # define close_descriptor(fd) close(fd)
397 #endif
398
399 #define DESC_PIPE 1
400 #define DESC_FILE 2
401 #define DESC_PARENT_MODE_WRITE 8
402
403 struct php_proc_open_descriptor_item {
404 int index; /* desired fd number in child process */
405 php_file_descriptor_t parentend, childend; /* fds for pipes in parent/child */
406 int mode; /* mode for proc_open code */
407 int mode_flags; /* mode flags for opening fds */
408 };
409 /* }}} */
410
411 /* {{{ proto resource proc_open(string command, array descriptorspec, array &pipes [, string cwd [, array env [, array other_options]]])
412 Run a process with more control over it's file descriptors */
PHP_FUNCTION(proc_open)413 PHP_FUNCTION(proc_open)
414 {
415 char *command, *cwd=NULL;
416 size_t command_len, cwd_len = 0;
417 zval *descriptorspec;
418 zval *pipes;
419 zval *environment = NULL;
420 zval *other_options = NULL;
421 php_process_env_t env;
422 int ndesc = 0;
423 int i;
424 zval *descitem = NULL;
425 zend_string *str_index;
426 zend_ulong nindex;
427 struct php_proc_open_descriptor_item *descriptors = NULL;
428 int ndescriptors_array;
429 #ifdef PHP_WIN32
430 PROCESS_INFORMATION pi;
431 HANDLE childHandle;
432 STARTUPINFO si;
433 BOOL newprocok;
434 SECURITY_ATTRIBUTES security;
435 DWORD dwCreateFlags = 0;
436 char *command_with_cmd;
437 UINT old_error_mode;
438 char cur_cwd[MAXPATHLEN];
439 #endif
440 #ifdef NETWARE
441 char** child_argv = NULL;
442 char* command_dup = NULL;
443 char* orig_cwd = NULL;
444 int command_num_args = 0;
445 wiring_t channel;
446 #endif
447 php_process_id_t child;
448 struct php_process_handle *proc;
449 int is_persistent = 0; /* TODO: ensure that persistent procs will work */
450 #ifdef PHP_WIN32
451 int suppress_errors = 0;
452 int bypass_shell = 0;
453 int blocking_pipes = 0;
454 #endif
455 #if PHP_CAN_DO_PTS
456 php_file_descriptor_t dev_ptmx = -1; /* master */
457 php_file_descriptor_t slave_pty = -1;
458 #endif
459
460 if (zend_parse_parameters(ZEND_NUM_ARGS(), "saz/|s!a!a!", &command,
461 &command_len, &descriptorspec, &pipes, &cwd, &cwd_len, &environment,
462 &other_options) == FAILURE) {
463 RETURN_FALSE;
464 }
465
466 command = pestrdup(command, is_persistent);
467
468 #ifdef PHP_WIN32
469 if (other_options) {
470 zval *item = zend_hash_str_find(Z_ARRVAL_P(other_options), "suppress_errors", sizeof("suppress_errors") - 1);
471 if (item != NULL) {
472 if (Z_TYPE_P(item) == IS_TRUE || ((Z_TYPE_P(item) == IS_LONG) && Z_LVAL_P(item))) {
473 suppress_errors = 1;
474 }
475 }
476
477 item = zend_hash_str_find(Z_ARRVAL_P(other_options), "bypass_shell", sizeof("bypass_shell") - 1);
478 if (item != NULL) {
479 if (Z_TYPE_P(item) == IS_TRUE || ((Z_TYPE_P(item) == IS_LONG) && Z_LVAL_P(item))) {
480 bypass_shell = 1;
481 }
482 }
483
484 item = zend_hash_str_find(Z_ARRVAL_P(other_options), "blocking_pipes", sizeof("blocking_pipes") - 1);
485 if (item != NULL) {
486 if (Z_TYPE_P(item) == IS_TRUE || ((Z_TYPE_P(item) == IS_LONG) && Z_LVAL_P(item))) {
487 blocking_pipes = 1;
488 }
489 }
490 }
491 #endif
492
493 command_len = strlen(command);
494
495 if (environment) {
496 env = _php_array_to_envp(environment, is_persistent);
497 } else {
498 memset(&env, 0, sizeof(env));
499 }
500
501 ndescriptors_array = zend_hash_num_elements(Z_ARRVAL_P(descriptorspec));
502
503 descriptors = safe_emalloc(sizeof(struct php_proc_open_descriptor_item), ndescriptors_array, 0);
504
505 memset(descriptors, 0, sizeof(struct php_proc_open_descriptor_item) * ndescriptors_array);
506
507 #ifdef PHP_WIN32
508 /* we use this to allow the child to inherit handles */
509 memset(&security, 0, sizeof(security));
510 security.nLength = sizeof(security);
511 security.bInheritHandle = TRUE;
512 security.lpSecurityDescriptor = NULL;
513 #endif
514
515 /* walk the descriptor spec and set up files/pipes */
516 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(descriptorspec), nindex, str_index, descitem) {
517 zval *ztype;
518
519 if (str_index) {
520 php_error_docref(NULL, E_WARNING, "descriptor spec must be an integer indexed array");
521 goto exit_fail;
522 }
523
524 descriptors[ndesc].index = (int)nindex;
525
526 if (Z_TYPE_P(descitem) == IS_RESOURCE) {
527 /* should be a stream - try and dup the descriptor */
528 php_stream *stream;
529 php_socket_t fd;
530
531 php_stream_from_zval(stream, descitem);
532
533 if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&fd, REPORT_ERRORS)) {
534 goto exit_fail;
535 }
536
537 #ifdef PHP_WIN32
538 descriptors[ndesc].childend = dup_fd_as_handle((int)fd);
539 if (descriptors[ndesc].childend == NULL) {
540 php_error_docref(NULL, E_WARNING, "unable to dup File-Handle for descriptor %d", nindex);
541 goto exit_fail;
542 }
543 #else
544 descriptors[ndesc].childend = dup(fd);
545 if (descriptors[ndesc].childend < 0) {
546 php_error_docref(NULL, E_WARNING, "unable to dup File-Handle for descriptor %pd - %s", nindex, strerror(errno));
547 goto exit_fail;
548 }
549 #endif
550 descriptors[ndesc].mode = DESC_FILE;
551
552 } else if (Z_TYPE_P(descitem) != IS_ARRAY) {
553 php_error_docref(NULL, E_WARNING, "Descriptor item must be either an array or a File-Handle");
554 goto exit_fail;
555 } else {
556
557 if ((ztype = zend_hash_index_find(Z_ARRVAL_P(descitem), 0)) != NULL) {
558 convert_to_string_ex(ztype);
559 } else {
560 php_error_docref(NULL, E_WARNING, "Missing handle qualifier in array");
561 goto exit_fail;
562 }
563
564 if (strcmp(Z_STRVAL_P(ztype), "pipe") == 0) {
565 php_file_descriptor_t newpipe[2];
566 zval *zmode;
567
568 if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
569 convert_to_string_ex(zmode);
570 } else {
571 php_error_docref(NULL, E_WARNING, "Missing mode parameter for 'pipe'");
572 goto exit_fail;
573 }
574
575 descriptors[ndesc].mode = DESC_PIPE;
576
577 if (0 != pipe(newpipe)) {
578 php_error_docref(NULL, E_WARNING, "unable to create pipe %s", strerror(errno));
579 goto exit_fail;
580 }
581
582 if (strncmp(Z_STRVAL_P(zmode), "w", 1) != 0) {
583 descriptors[ndesc].parentend = newpipe[1];
584 descriptors[ndesc].childend = newpipe[0];
585 descriptors[ndesc].mode |= DESC_PARENT_MODE_WRITE;
586 } else {
587 descriptors[ndesc].parentend = newpipe[0];
588 descriptors[ndesc].childend = newpipe[1];
589 }
590 #ifdef PHP_WIN32
591 /* don't let the child inherit the parent side of the pipe */
592 descriptors[ndesc].parentend = dup_handle(descriptors[ndesc].parentend, FALSE, TRUE);
593 #endif
594 descriptors[ndesc].mode_flags = descriptors[ndesc].mode & DESC_PARENT_MODE_WRITE ? O_WRONLY : O_RDONLY;
595 #ifdef PHP_WIN32
596 if (Z_STRLEN_P(zmode) >= 2 && Z_STRVAL_P(zmode)[1] == 'b')
597 descriptors[ndesc].mode_flags |= O_BINARY;
598 #endif
599
600 } else if (strcmp(Z_STRVAL_P(ztype), "file") == 0) {
601 zval *zfile, *zmode;
602 php_socket_t fd;
603 php_stream *stream;
604
605 descriptors[ndesc].mode = DESC_FILE;
606
607 if ((zfile = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
608 convert_to_string_ex(zfile);
609 } else {
610 php_error_docref(NULL, E_WARNING, "Missing file name parameter for 'file'");
611 goto exit_fail;
612 }
613
614 if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 2)) != NULL) {
615 convert_to_string_ex(zmode);
616 } else {
617 php_error_docref(NULL, E_WARNING, "Missing mode parameter for 'file'");
618 goto exit_fail;
619 }
620
621 /* try a wrapper */
622 stream = php_stream_open_wrapper(Z_STRVAL_P(zfile), Z_STRVAL_P(zmode),
623 REPORT_ERRORS|STREAM_WILL_CAST, NULL);
624
625 /* force into an fd */
626 if (stream == NULL || FAILURE == php_stream_cast(stream,
627 PHP_STREAM_CAST_RELEASE|PHP_STREAM_AS_FD,
628 (void **)&fd, REPORT_ERRORS)) {
629 goto exit_fail;
630 }
631
632 #ifdef PHP_WIN32
633 descriptors[ndesc].childend = dup_fd_as_handle((int)fd);
634 _close((int)fd);
635
636 /* simulate the append mode by fseeking to the end of the file
637 this introduces a potential race-condition, but it is the best we can do, though */
638 if (strchr(Z_STRVAL_P(zmode), 'a')) {
639 SetFilePointer(descriptors[ndesc].childend, 0, NULL, FILE_END);
640 }
641 #else
642 descriptors[ndesc].childend = fd;
643 #endif
644 } else if (strcmp(Z_STRVAL_P(ztype), "pty") == 0) {
645 #if PHP_CAN_DO_PTS
646 if (dev_ptmx == -1) {
647 /* open things up */
648 dev_ptmx = open("/dev/ptmx", O_RDWR);
649 if (dev_ptmx == -1) {
650 php_error_docref(NULL, E_WARNING, "failed to open /dev/ptmx, errno %d", errno);
651 goto exit_fail;
652 }
653 grantpt(dev_ptmx);
654 unlockpt(dev_ptmx);
655 slave_pty = open(ptsname(dev_ptmx), O_RDWR);
656
657 if (slave_pty == -1) {
658 php_error_docref(NULL, E_WARNING, "failed to open slave pty, errno %d", errno);
659 goto exit_fail;
660 }
661 }
662 descriptors[ndesc].mode = DESC_PIPE;
663 descriptors[ndesc].childend = dup(slave_pty);
664 descriptors[ndesc].parentend = dup(dev_ptmx);
665 descriptors[ndesc].mode_flags = O_RDWR;
666 #else
667 php_error_docref(NULL, E_WARNING, "pty pseudo terminal not supported on this system");
668 goto exit_fail;
669 #endif
670 } else {
671 php_error_docref(NULL, E_WARNING, "%s is not a valid descriptor spec/mode", Z_STRVAL_P(ztype));
672 goto exit_fail;
673 }
674 }
675 ndesc++;
676 } ZEND_HASH_FOREACH_END();
677
678 #ifdef PHP_WIN32
679 if (cwd == NULL) {
680 char *getcwd_result;
681 getcwd_result = VCWD_GETCWD(cur_cwd, MAXPATHLEN);
682 if (!getcwd_result) {
683 php_error_docref(NULL, E_WARNING, "Cannot get current directory");
684 goto exit_fail;
685 }
686 cwd = cur_cwd;
687 }
688
689 memset(&si, 0, sizeof(si));
690 si.cb = sizeof(si);
691 si.dwFlags = STARTF_USESTDHANDLES;
692
693 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
694 si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
695 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
696
697 /* redirect stdin/stdout/stderr if requested */
698 for (i = 0; i < ndesc; i++) {
699 switch(descriptors[i].index) {
700 case 0:
701 si.hStdInput = descriptors[i].childend;
702 break;
703 case 1:
704 si.hStdOutput = descriptors[i].childend;
705 break;
706 case 2:
707 si.hStdError = descriptors[i].childend;
708 break;
709 }
710 }
711
712
713 memset(&pi, 0, sizeof(pi));
714
715 if (suppress_errors) {
716 old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX);
717 }
718
719 dwCreateFlags = NORMAL_PRIORITY_CLASS;
720 if(strcmp(sapi_module.name, "cli") != 0) {
721 dwCreateFlags |= CREATE_NO_WINDOW;
722 }
723
724 if (bypass_shell) {
725 newprocok = CreateProcess(NULL, command, &security, &security, TRUE, dwCreateFlags, env.envp, cwd, &si, &pi);
726 } else {
727 spprintf(&command_with_cmd, 0, "%s /c %s", COMSPEC_NT, command);
728
729 newprocok = CreateProcess(NULL, command_with_cmd, &security, &security, TRUE, dwCreateFlags, env.envp, cwd, &si, &pi);
730
731 efree(command_with_cmd);
732 }
733
734 if (suppress_errors) {
735 SetErrorMode(old_error_mode);
736 }
737
738 if (FALSE == newprocok) {
739 DWORD dw = GetLastError();
740
741 /* clean up all the descriptors */
742 for (i = 0; i < ndesc; i++) {
743 CloseHandle(descriptors[i].childend);
744 if (descriptors[i].parentend) {
745 CloseHandle(descriptors[i].parentend);
746 }
747 }
748 php_error_docref(NULL, E_WARNING, "CreateProcess failed, error code - %u", dw);
749 goto exit_fail;
750 }
751
752 childHandle = pi.hProcess;
753 child = pi.dwProcessId;
754 CloseHandle(pi.hThread);
755
756 #elif defined(NETWARE)
757 if (cwd) {
758 orig_cwd = getcwd(NULL, PATH_MAX);
759 chdir2(cwd);
760 }
761 channel.infd = descriptors[0].childend;
762 channel.outfd = descriptors[1].childend;
763 channel.errfd = -1;
764 /* Duplicate the command as processing downwards will modify it*/
765 command_dup = strdup(command);
766 if (!command_dup) {
767 goto exit_fail;
768 }
769 /* get a number of args */
770 construct_argc_argv(command_dup, NULL, &command_num_args, NULL);
771 child_argv = (char**) malloc((command_num_args + 1) * sizeof(char*));
772 if(!child_argv) {
773 free(command_dup);
774 if (cwd && orig_cwd) {
775 chdir2(orig_cwd);
776 free(orig_cwd);
777 }
778 }
779 /* fill the child arg vector */
780 construct_argc_argv(command_dup, NULL, &command_num_args, child_argv);
781 child_argv[command_num_args] = NULL;
782 child = procve(child_argv[0], PROC_DETACHED|PROC_INHERIT_CWD, NULL, &channel, NULL, NULL, 0, NULL, (const char**)child_argv);
783 free(child_argv);
784 free(command_dup);
785 if (cwd && orig_cwd) {
786 chdir2(orig_cwd);
787 free(orig_cwd);
788 }
789 if (child < 0) {
790 /* failed to fork() */
791 /* clean up all the descriptors */
792 for (i = 0; i < ndesc; i++) {
793 close(descriptors[i].childend);
794 if (descriptors[i].parentend)
795 close(descriptors[i].parentend);
796 }
797 php_error_docref(NULL, E_WARNING, "procve failed - %s", strerror(errno));
798 goto exit_fail;
799 }
800 #elif HAVE_FORK
801 /* the unix way */
802 child = fork();
803
804 if (child == 0) {
805 /* this is the child process */
806
807 #if PHP_CAN_DO_PTS
808 if (dev_ptmx >= 0) {
809 int my_pid = getpid();
810
811 #ifdef TIOCNOTTY
812 /* detach from original tty. Might only need this if isatty(0) is true */
813 ioctl(0,TIOCNOTTY,NULL);
814 #else
815 setsid();
816 #endif
817 /* become process group leader */
818 setpgid(my_pid, my_pid);
819 tcsetpgrp(0, my_pid);
820 }
821 #endif
822
823 #if PHP_CAN_DO_PTS
824 if (dev_ptmx >= 0) {
825 close(dev_ptmx);
826 close(slave_pty);
827 }
828 #endif
829 /* close those descriptors that we just opened for the parent stuff,
830 * dup new descriptors into required descriptors and close the original
831 * cruft */
832 for (i = 0; i < ndesc; i++) {
833 switch (descriptors[i].mode & ~DESC_PARENT_MODE_WRITE) {
834 case DESC_PIPE:
835 close(descriptors[i].parentend);
836 break;
837 }
838 if (dup2(descriptors[i].childend, descriptors[i].index) < 0)
839 perror("dup2");
840 if (descriptors[i].childend != descriptors[i].index)
841 close(descriptors[i].childend);
842 }
843
844 if (cwd) {
845 php_ignore_value(chdir(cwd));
846 }
847
848 if (env.envarray) {
849 execle("/bin/sh", "sh", "-c", command, NULL, env.envarray);
850 } else {
851 execl("/bin/sh", "sh", "-c", command, NULL);
852 }
853 _exit(127);
854
855 } else if (child < 0) {
856 /* failed to fork() */
857
858 /* clean up all the descriptors */
859 for (i = 0; i < ndesc; i++) {
860 close(descriptors[i].childend);
861 if (descriptors[i].parentend)
862 close(descriptors[i].parentend);
863 }
864
865 php_error_docref(NULL, E_WARNING, "fork failed - %s", strerror(errno));
866
867 goto exit_fail;
868
869 }
870 #else
871 # error You lose (configure should not have let you get here)
872 #endif
873 /* we forked/spawned and this is the parent */
874
875 proc = (struct php_process_handle*)pemalloc(sizeof(struct php_process_handle), is_persistent);
876 proc->is_persistent = is_persistent;
877 proc->command = command;
878 proc->pipes = pemalloc(sizeof(zend_resource *) * ndesc, is_persistent);
879 proc->npipes = ndesc;
880 proc->child = child;
881 #ifdef PHP_WIN32
882 proc->childHandle = childHandle;
883 #endif
884 proc->env = env;
885
886 if (pipes != NULL) {
887 zval_dtor(pipes);
888 }
889
890 array_init(pipes);
891
892 #if PHP_CAN_DO_PTS
893 if (dev_ptmx >= 0) {
894 close(dev_ptmx);
895 close(slave_pty);
896 }
897 #endif
898
899 /* clean up all the child ends and then open streams on the parent
900 * ends, where appropriate */
901 for (i = 0; i < ndesc; i++) {
902 char *mode_string=NULL;
903 php_stream *stream = NULL;
904
905 close_descriptor(descriptors[i].childend);
906
907 switch (descriptors[i].mode & ~DESC_PARENT_MODE_WRITE) {
908 case DESC_PIPE:
909 switch(descriptors[i].mode_flags) {
910 #ifdef PHP_WIN32
911 case O_WRONLY|O_BINARY:
912 mode_string = "wb";
913 break;
914 case O_RDONLY|O_BINARY:
915 mode_string = "rb";
916 break;
917 #endif
918 case O_WRONLY:
919 mode_string = "w";
920 break;
921 case O_RDONLY:
922 mode_string = "r";
923 break;
924 case O_RDWR:
925 mode_string = "r+";
926 break;
927 }
928 #ifdef PHP_WIN32
929 stream = php_stream_fopen_from_fd(_open_osfhandle((zend_intptr_t)descriptors[i].parentend,
930 descriptors[i].mode_flags), mode_string, NULL);
931 php_stream_set_option(stream, PHP_STREAM_OPTION_PIPE_BLOCKING, blocking_pipes, NULL);
932 #else
933 stream = php_stream_fopen_from_fd(descriptors[i].parentend, mode_string, NULL);
934 # if defined(F_SETFD) && defined(FD_CLOEXEC)
935 /* mark the descriptor close-on-exec, so that it won't be inherited by potential other children */
936 fcntl(descriptors[i].parentend, F_SETFD, FD_CLOEXEC);
937 # endif
938 #endif
939 if (stream) {
940 zval retfp;
941
942 /* nasty hack; don't copy it */
943 stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
944
945 php_stream_to_zval(stream, &retfp);
946 add_index_zval(pipes, descriptors[i].index, &retfp);
947
948 proc->pipes[i] = Z_RES(retfp);
949 Z_ADDREF(retfp);
950 }
951 break;
952 default:
953 proc->pipes[i] = NULL;
954 }
955 }
956
957 efree(descriptors);
958 ZVAL_RES(return_value, zend_register_resource(proc, le_proc_open));
959 return;
960
961 exit_fail:
962 efree(descriptors);
963 _php_free_envp(env, is_persistent);
964 pefree(command, is_persistent);
965 #if PHP_CAN_DO_PTS
966 if (dev_ptmx >= 0) {
967 close(dev_ptmx);
968 }
969 if (slave_pty >= 0) {
970 close(slave_pty);
971 }
972 #endif
973 RETURN_FALSE;
974
975 }
976 /* }}} */
977
978 #endif /* PHP_CAN_SUPPORT_PROC_OPEN */
979
980 /*
981 * Local variables:
982 * tab-width: 4
983 * c-basic-offset: 4
984 * End:
985 * vim600: sw=4 ts=4 fdm=marker
986 * vim<600: sw=4 ts=4
987 */
988