1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 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 STARTUPINFOW si;
433 BOOL newprocok;
434 SECURITY_ATTRIBUTES security;
435 DWORD dwCreateFlags = 0;
436 UINT old_error_mode;
437 char cur_cwd[MAXPATHLEN];
438 wchar_t *cmdw = NULL, *cwdw = NULL, *envpw = NULL;
439 size_t tmp_len;
440 #endif
441 #ifdef NETWARE
442 char** child_argv = NULL;
443 char* command_dup = NULL;
444 char* orig_cwd = NULL;
445 int command_num_args = 0;
446 wiring_t channel;
447 #endif
448 php_process_id_t child;
449 struct php_process_handle *proc;
450 int is_persistent = 0; /* TODO: ensure that persistent procs will work */
451 #ifdef PHP_WIN32
452 int suppress_errors = 0;
453 int bypass_shell = 0;
454 int blocking_pipes = 0;
455 #endif
456 #if PHP_CAN_DO_PTS
457 php_file_descriptor_t dev_ptmx = -1; /* master */
458 php_file_descriptor_t slave_pty = -1;
459 #endif
460
461 if (zend_parse_parameters(ZEND_NUM_ARGS(), "saz/|s!a!a!", &command,
462 &command_len, &descriptorspec, &pipes, &cwd, &cwd_len, &environment,
463 &other_options) == FAILURE) {
464 RETURN_FALSE;
465 }
466
467 command = pestrdup(command, is_persistent);
468
469 #ifdef PHP_WIN32
470 if (other_options) {
471 zval *item = zend_hash_str_find(Z_ARRVAL_P(other_options), "suppress_errors", sizeof("suppress_errors") - 1);
472 if (item != NULL) {
473 if (Z_TYPE_P(item) == IS_TRUE || ((Z_TYPE_P(item) == IS_LONG) && Z_LVAL_P(item))) {
474 suppress_errors = 1;
475 }
476 }
477
478 item = zend_hash_str_find(Z_ARRVAL_P(other_options), "bypass_shell", sizeof("bypass_shell") - 1);
479 if (item != NULL) {
480 if (Z_TYPE_P(item) == IS_TRUE || ((Z_TYPE_P(item) == IS_LONG) && Z_LVAL_P(item))) {
481 bypass_shell = 1;
482 }
483 }
484
485 item = zend_hash_str_find(Z_ARRVAL_P(other_options), "blocking_pipes", sizeof("blocking_pipes") - 1);
486 if (item != NULL) {
487 if (Z_TYPE_P(item) == IS_TRUE || ((Z_TYPE_P(item) == IS_LONG) && Z_LVAL_P(item))) {
488 blocking_pipes = 1;
489 }
490 }
491 }
492 #endif
493
494 command_len = strlen(command);
495
496 if (environment) {
497 env = _php_array_to_envp(environment, is_persistent);
498 } else {
499 memset(&env, 0, sizeof(env));
500 }
501
502 ndescriptors_array = zend_hash_num_elements(Z_ARRVAL_P(descriptorspec));
503
504 descriptors = safe_emalloc(sizeof(struct php_proc_open_descriptor_item), ndescriptors_array, 0);
505
506 memset(descriptors, 0, sizeof(struct php_proc_open_descriptor_item) * ndescriptors_array);
507
508 #ifdef PHP_WIN32
509 /* we use this to allow the child to inherit handles */
510 memset(&security, 0, sizeof(security));
511 security.nLength = sizeof(security);
512 security.bInheritHandle = TRUE;
513 security.lpSecurityDescriptor = NULL;
514 #endif
515
516 /* walk the descriptor spec and set up files/pipes */
517 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(descriptorspec), nindex, str_index, descitem) {
518 zval *ztype;
519
520 if (str_index) {
521 php_error_docref(NULL, E_WARNING, "descriptor spec must be an integer indexed array");
522 goto exit_fail;
523 }
524
525 descriptors[ndesc].index = (int)nindex;
526
527 if (Z_TYPE_P(descitem) == IS_RESOURCE) {
528 /* should be a stream - try and dup the descriptor */
529 php_stream *stream;
530 php_socket_t fd;
531
532 php_stream_from_zval(stream, descitem);
533
534 if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&fd, REPORT_ERRORS)) {
535 goto exit_fail;
536 }
537
538 #ifdef PHP_WIN32
539 descriptors[ndesc].childend = dup_fd_as_handle((int)fd);
540 if (descriptors[ndesc].childend == NULL) {
541 php_error_docref(NULL, E_WARNING, "unable to dup File-Handle for descriptor %d", nindex);
542 goto exit_fail;
543 }
544 #else
545 descriptors[ndesc].childend = dup(fd);
546 if (descriptors[ndesc].childend < 0) {
547 php_error_docref(NULL, E_WARNING, "unable to dup File-Handle for descriptor " ZEND_ULONG_FMT " - %s", nindex, strerror(errno));
548 goto exit_fail;
549 }
550 #endif
551 descriptors[ndesc].mode = DESC_FILE;
552
553 } else if (Z_TYPE_P(descitem) != IS_ARRAY) {
554 php_error_docref(NULL, E_WARNING, "Descriptor item must be either an array or a File-Handle");
555 goto exit_fail;
556 } else {
557
558 if ((ztype = zend_hash_index_find(Z_ARRVAL_P(descitem), 0)) != NULL) {
559 convert_to_string_ex(ztype);
560 } else {
561 php_error_docref(NULL, E_WARNING, "Missing handle qualifier in array");
562 goto exit_fail;
563 }
564
565 if (strcmp(Z_STRVAL_P(ztype), "pipe") == 0) {
566 php_file_descriptor_t newpipe[2];
567 zval *zmode;
568
569 if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
570 convert_to_string_ex(zmode);
571 } else {
572 php_error_docref(NULL, E_WARNING, "Missing mode parameter for 'pipe'");
573 goto exit_fail;
574 }
575
576 descriptors[ndesc].mode = DESC_PIPE;
577
578 if (0 != pipe(newpipe)) {
579 php_error_docref(NULL, E_WARNING, "unable to create pipe %s", strerror(errno));
580 goto exit_fail;
581 }
582
583 if (strncmp(Z_STRVAL_P(zmode), "w", 1) != 0) {
584 descriptors[ndesc].parentend = newpipe[1];
585 descriptors[ndesc].childend = newpipe[0];
586 descriptors[ndesc].mode |= DESC_PARENT_MODE_WRITE;
587 } else {
588 descriptors[ndesc].parentend = newpipe[0];
589 descriptors[ndesc].childend = newpipe[1];
590 }
591 #ifdef PHP_WIN32
592 /* don't let the child inherit the parent side of the pipe */
593 descriptors[ndesc].parentend = dup_handle(descriptors[ndesc].parentend, FALSE, TRUE);
594 #endif
595 descriptors[ndesc].mode_flags = descriptors[ndesc].mode & DESC_PARENT_MODE_WRITE ? O_WRONLY : O_RDONLY;
596 #ifdef PHP_WIN32
597 if (Z_STRLEN_P(zmode) >= 2 && Z_STRVAL_P(zmode)[1] == 'b')
598 descriptors[ndesc].mode_flags |= O_BINARY;
599 #endif
600
601 } else if (strcmp(Z_STRVAL_P(ztype), "file") == 0) {
602 zval *zfile, *zmode;
603 php_socket_t fd;
604 php_stream *stream;
605
606 descriptors[ndesc].mode = DESC_FILE;
607
608 if ((zfile = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
609 convert_to_string_ex(zfile);
610 } else {
611 php_error_docref(NULL, E_WARNING, "Missing file name parameter for 'file'");
612 goto exit_fail;
613 }
614
615 if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 2)) != NULL) {
616 convert_to_string_ex(zmode);
617 } else {
618 php_error_docref(NULL, E_WARNING, "Missing mode parameter for 'file'");
619 goto exit_fail;
620 }
621
622 /* try a wrapper */
623 stream = php_stream_open_wrapper(Z_STRVAL_P(zfile), Z_STRVAL_P(zmode),
624 REPORT_ERRORS|STREAM_WILL_CAST, NULL);
625
626 /* force into an fd */
627 if (stream == NULL || FAILURE == php_stream_cast(stream,
628 PHP_STREAM_CAST_RELEASE|PHP_STREAM_AS_FD,
629 (void **)&fd, REPORT_ERRORS)) {
630 goto exit_fail;
631 }
632
633 #ifdef PHP_WIN32
634 descriptors[ndesc].childend = dup_fd_as_handle((int)fd);
635 _close((int)fd);
636
637 /* simulate the append mode by fseeking to the end of the file
638 this introduces a potential race-condition, but it is the best we can do, though */
639 if (strchr(Z_STRVAL_P(zmode), 'a')) {
640 SetFilePointer(descriptors[ndesc].childend, 0, NULL, FILE_END);
641 }
642 #else
643 descriptors[ndesc].childend = fd;
644 #endif
645 } else if (strcmp(Z_STRVAL_P(ztype), "pty") == 0) {
646 #if PHP_CAN_DO_PTS
647 if (dev_ptmx == -1) {
648 /* open things up */
649 dev_ptmx = open("/dev/ptmx", O_RDWR);
650 if (dev_ptmx == -1) {
651 php_error_docref(NULL, E_WARNING, "failed to open /dev/ptmx, errno %d", errno);
652 goto exit_fail;
653 }
654 grantpt(dev_ptmx);
655 unlockpt(dev_ptmx);
656 slave_pty = open(ptsname(dev_ptmx), O_RDWR);
657
658 if (slave_pty == -1) {
659 php_error_docref(NULL, E_WARNING, "failed to open slave pty, errno %d", errno);
660 goto exit_fail;
661 }
662 }
663 descriptors[ndesc].mode = DESC_PIPE;
664 descriptors[ndesc].childend = dup(slave_pty);
665 descriptors[ndesc].parentend = dup(dev_ptmx);
666 descriptors[ndesc].mode_flags = O_RDWR;
667 #else
668 php_error_docref(NULL, E_WARNING, "pty pseudo terminal not supported on this system");
669 goto exit_fail;
670 #endif
671 } else {
672 php_error_docref(NULL, E_WARNING, "%s is not a valid descriptor spec/mode", Z_STRVAL_P(ztype));
673 goto exit_fail;
674 }
675 }
676 ndesc++;
677 } ZEND_HASH_FOREACH_END();
678
679 #ifdef PHP_WIN32
680 if (cwd == NULL) {
681 char *getcwd_result;
682 getcwd_result = VCWD_GETCWD(cur_cwd, MAXPATHLEN);
683 if (!getcwd_result) {
684 php_error_docref(NULL, E_WARNING, "Cannot get current directory");
685 goto exit_fail;
686 }
687 cwd = cur_cwd;
688 }
689 cwdw = php_win32_cp_any_to_w(cwd);
690 if (!cwdw) {
691 php_error_docref(NULL, E_WARNING, "CWD conversion failed");
692 goto exit_fail;
693 }
694
695 memset(&si, 0, sizeof(si));
696 si.cb = sizeof(si);
697 si.dwFlags = STARTF_USESTDHANDLES;
698
699 si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
700 si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
701 si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
702
703 /* redirect stdin/stdout/stderr if requested */
704 for (i = 0; i < ndesc; i++) {
705 switch(descriptors[i].index) {
706 case 0:
707 si.hStdInput = descriptors[i].childend;
708 break;
709 case 1:
710 si.hStdOutput = descriptors[i].childend;
711 break;
712 case 2:
713 si.hStdError = descriptors[i].childend;
714 break;
715 }
716 }
717
718
719 memset(&pi, 0, sizeof(pi));
720
721 if (suppress_errors) {
722 old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX);
723 }
724
725 dwCreateFlags = NORMAL_PRIORITY_CLASS;
726 if(strcmp(sapi_module.name, "cli") != 0) {
727 dwCreateFlags |= CREATE_NO_WINDOW;
728 }
729
730 envpw = php_win32_cp_env_any_to_w(env.envp);
731 if (envpw) {
732 dwCreateFlags |= CREATE_UNICODE_ENVIRONMENT;
733 } else {
734 if (env.envp) {
735 php_error_docref(NULL, E_WARNING, "ENV conversion failed");
736 goto exit_fail;
737 }
738 }
739
740 cmdw = php_win32_cp_conv_any_to_w(command, command_len, &tmp_len);
741 if (!cmdw) {
742 php_error_docref(NULL, E_WARNING, "Command conversion failed");
743 goto exit_fail;
744 }
745
746 if (bypass_shell) {
747 newprocok = CreateProcessW(NULL, cmdw, &security, &security, TRUE, dwCreateFlags, envpw, cwdw, &si, &pi);
748 } else {
749 int ret;
750 size_t len;
751 wchar_t *cmdw2;
752
753
754 len = (sizeof(COMSPEC_NT) + sizeof(" /c ") + tmp_len + 1);
755 cmdw2 = (wchar_t *)malloc(len * sizeof(wchar_t));
756 if (!cmdw2) {
757 php_error_docref(NULL, E_WARNING, "Command conversion failed");
758 goto exit_fail;
759 }
760 ret = _snwprintf(cmdw2, len, L"%hs /c %s", COMSPEC_NT, cmdw);
761
762 if (-1 == ret) {
763 free(cmdw2);
764 php_error_docref(NULL, E_WARNING, "Command conversion failed");
765 goto exit_fail;
766 }
767
768 newprocok = CreateProcessW(NULL, cmdw2, &security, &security, TRUE, dwCreateFlags, envpw, cwdw, &si, &pi);
769 free(cmdw2);
770 }
771
772 free(cwdw);
773 cwdw = NULL;
774 free(cmdw);
775 cmdw = NULL;
776 free(envpw);
777 envpw = NULL;
778
779 if (suppress_errors) {
780 SetErrorMode(old_error_mode);
781 }
782
783 if (FALSE == newprocok) {
784 DWORD dw = GetLastError();
785
786 /* clean up all the descriptors */
787 for (i = 0; i < ndesc; i++) {
788 CloseHandle(descriptors[i].childend);
789 if (descriptors[i].parentend) {
790 CloseHandle(descriptors[i].parentend);
791 }
792 }
793 php_error_docref(NULL, E_WARNING, "CreateProcess failed, error code - %u", dw);
794 goto exit_fail;
795 }
796
797 childHandle = pi.hProcess;
798 child = pi.dwProcessId;
799 CloseHandle(pi.hThread);
800
801 #elif defined(NETWARE)
802 if (cwd) {
803 orig_cwd = getcwd(NULL, PATH_MAX);
804 chdir2(cwd);
805 }
806 channel.infd = descriptors[0].childend;
807 channel.outfd = descriptors[1].childend;
808 channel.errfd = -1;
809 /* Duplicate the command as processing downwards will modify it*/
810 command_dup = strdup(command);
811 if (!command_dup) {
812 goto exit_fail;
813 }
814 /* get a number of args */
815 construct_argc_argv(command_dup, NULL, &command_num_args, NULL);
816 child_argv = (char**) malloc((command_num_args + 1) * sizeof(char*));
817 if(!child_argv) {
818 free(command_dup);
819 if (cwd && orig_cwd) {
820 chdir2(orig_cwd);
821 free(orig_cwd);
822 }
823 }
824 /* fill the child arg vector */
825 construct_argc_argv(command_dup, NULL, &command_num_args, child_argv);
826 child_argv[command_num_args] = NULL;
827 child = procve(child_argv[0], PROC_DETACHED|PROC_INHERIT_CWD, NULL, &channel, NULL, NULL, 0, NULL, (const char**)child_argv);
828 free(child_argv);
829 free(command_dup);
830 if (cwd && orig_cwd) {
831 chdir2(orig_cwd);
832 free(orig_cwd);
833 }
834 if (child < 0) {
835 /* failed to fork() */
836 /* clean up all the descriptors */
837 for (i = 0; i < ndesc; i++) {
838 close(descriptors[i].childend);
839 if (descriptors[i].parentend)
840 close(descriptors[i].parentend);
841 }
842 php_error_docref(NULL, E_WARNING, "procve failed - %s", strerror(errno));
843 goto exit_fail;
844 }
845 #elif HAVE_FORK
846 /* the unix way */
847 child = fork();
848
849 if (child == 0) {
850 /* this is the child process */
851
852 #if PHP_CAN_DO_PTS
853 if (dev_ptmx >= 0) {
854 int my_pid = getpid();
855
856 #ifdef TIOCNOTTY
857 /* detach from original tty. Might only need this if isatty(0) is true */
858 ioctl(0,TIOCNOTTY,NULL);
859 #else
860 setsid();
861 #endif
862 /* become process group leader */
863 setpgid(my_pid, my_pid);
864 tcsetpgrp(0, my_pid);
865 }
866 #endif
867
868 #if PHP_CAN_DO_PTS
869 if (dev_ptmx >= 0) {
870 close(dev_ptmx);
871 close(slave_pty);
872 }
873 #endif
874 /* close those descriptors that we just opened for the parent stuff,
875 * dup new descriptors into required descriptors and close the original
876 * cruft */
877 for (i = 0; i < ndesc; i++) {
878 switch (descriptors[i].mode & ~DESC_PARENT_MODE_WRITE) {
879 case DESC_PIPE:
880 close(descriptors[i].parentend);
881 break;
882 }
883 if (dup2(descriptors[i].childend, descriptors[i].index) < 0)
884 perror("dup2");
885 if (descriptors[i].childend != descriptors[i].index)
886 close(descriptors[i].childend);
887 }
888
889 if (cwd) {
890 php_ignore_value(chdir(cwd));
891 }
892
893 if (env.envarray) {
894 execle("/bin/sh", "sh", "-c", command, NULL, env.envarray);
895 } else {
896 execl("/bin/sh", "sh", "-c", command, NULL);
897 }
898 _exit(127);
899
900 } else if (child < 0) {
901 /* failed to fork() */
902
903 /* clean up all the descriptors */
904 for (i = 0; i < ndesc; i++) {
905 close(descriptors[i].childend);
906 if (descriptors[i].parentend)
907 close(descriptors[i].parentend);
908 }
909
910 php_error_docref(NULL, E_WARNING, "fork failed - %s", strerror(errno));
911
912 goto exit_fail;
913
914 }
915 #else
916 # error You lose (configure should not have let you get here)
917 #endif
918 /* we forked/spawned and this is the parent */
919
920 proc = (struct php_process_handle*)pemalloc(sizeof(struct php_process_handle), is_persistent);
921 proc->is_persistent = is_persistent;
922 proc->command = command;
923 proc->pipes = pemalloc(sizeof(zend_resource *) * ndesc, is_persistent);
924 proc->npipes = ndesc;
925 proc->child = child;
926 #ifdef PHP_WIN32
927 proc->childHandle = childHandle;
928 #endif
929 proc->env = env;
930
931 if (pipes != NULL) {
932 zval_dtor(pipes);
933 }
934
935 array_init(pipes);
936
937 #if PHP_CAN_DO_PTS
938 if (dev_ptmx >= 0) {
939 close(dev_ptmx);
940 close(slave_pty);
941 }
942 #endif
943
944 /* clean up all the child ends and then open streams on the parent
945 * ends, where appropriate */
946 for (i = 0; i < ndesc; i++) {
947 char *mode_string=NULL;
948 php_stream *stream = NULL;
949
950 close_descriptor(descriptors[i].childend);
951
952 switch (descriptors[i].mode & ~DESC_PARENT_MODE_WRITE) {
953 case DESC_PIPE:
954 switch(descriptors[i].mode_flags) {
955 #ifdef PHP_WIN32
956 case O_WRONLY|O_BINARY:
957 mode_string = "wb";
958 break;
959 case O_RDONLY|O_BINARY:
960 mode_string = "rb";
961 break;
962 #endif
963 case O_WRONLY:
964 mode_string = "w";
965 break;
966 case O_RDONLY:
967 mode_string = "r";
968 break;
969 case O_RDWR:
970 mode_string = "r+";
971 break;
972 }
973 #ifdef PHP_WIN32
974 stream = php_stream_fopen_from_fd(_open_osfhandle((zend_intptr_t)descriptors[i].parentend,
975 descriptors[i].mode_flags), mode_string, NULL);
976 php_stream_set_option(stream, PHP_STREAM_OPTION_PIPE_BLOCKING, blocking_pipes, NULL);
977 #else
978 stream = php_stream_fopen_from_fd(descriptors[i].parentend, mode_string, NULL);
979 # if defined(F_SETFD) && defined(FD_CLOEXEC)
980 /* mark the descriptor close-on-exec, so that it won't be inherited by potential other children */
981 fcntl(descriptors[i].parentend, F_SETFD, FD_CLOEXEC);
982 # endif
983 #endif
984 if (stream) {
985 zval retfp;
986
987 /* nasty hack; don't copy it */
988 stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
989
990 php_stream_to_zval(stream, &retfp);
991 add_index_zval(pipes, descriptors[i].index, &retfp);
992
993 proc->pipes[i] = Z_RES(retfp);
994 Z_ADDREF(retfp);
995 }
996 break;
997 default:
998 proc->pipes[i] = NULL;
999 }
1000 }
1001
1002 efree(descriptors);
1003 ZVAL_RES(return_value, zend_register_resource(proc, le_proc_open));
1004 return;
1005
1006 exit_fail:
1007 efree(descriptors);
1008 _php_free_envp(env, is_persistent);
1009 pefree(command, is_persistent);
1010 #ifdef PHP_WIN32
1011 free(cwdw);
1012 free(cmdw);
1013 free(envpw);
1014 #endif
1015 #if PHP_CAN_DO_PTS
1016 if (dev_ptmx >= 0) {
1017 close(dev_ptmx);
1018 }
1019 if (slave_pty >= 0) {
1020 close(slave_pty);
1021 }
1022 #endif
1023 RETURN_FALSE;
1024
1025 }
1026 /* }}} */
1027
1028 #endif /* PHP_CAN_SUPPORT_PROC_OPEN */
1029
1030 /*
1031 * Local variables:
1032 * tab-width: 4
1033 * c-basic-offset: 4
1034 * End:
1035 * vim600: sw=4 ts=4 fdm=marker
1036 * vim<600: sw=4 ts=4
1037 */
1038