PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - standard - proc_open.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 300
Code covered: 67.3 % Executed lines: 202
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2009 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: proc_open.c 286752 2009-08-03 19:05:56Z felipe $ */
      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 "safe_mode.h"
      32                 : #include "ext/standard/head.h"
      33                 : #include "ext/standard/basic_functions.h"
      34                 : #include "ext/standard/file.h"
      35                 : #include "exec.h"
      36                 : #include "php_globals.h"
      37                 : #include "SAPI.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                 :  * Otherplatforms 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 */
      76                 : static php_process_env_t _php_array_to_envp(zval *environment, int is_persistent TSRMLS_DC)
      77           13468 : {
      78                 :         zval **element;
      79                 :         php_process_env_t env;
      80                 :         char *string_key, *data;
      81                 : #ifndef PHP_WIN32
      82                 :         char **ep;
      83                 : #endif
      84                 :         char *p;
      85           13468 :         uint string_length, cnt, l, sizeenv=0, el_len;
      86                 :         ulong num_key;
      87                 :         HashTable *target_hash;
      88                 :         HashPosition pos;
      89                 : 
      90           13468 :         memset(&env, 0, sizeof(env));
      91                 :         
      92           13468 :         if (!environment) {
      93               0 :                 return env;
      94                 :         }
      95                 :         
      96           13468 :         cnt = zend_hash_num_elements(Z_ARRVAL_P(environment));
      97                 :         
      98           13468 :         if (cnt < 1) {
      99                 : #ifndef PHP_WIN32
     100               2 :                 env.envarray = (char **) pecalloc(1, sizeof(char *), is_persistent);
     101                 : #endif
     102               2 :                 env.envp = (char *) pecalloc(4, 1, is_persistent);
     103               2 :                 return env;
     104                 :         }
     105                 : 
     106           13466 :         target_hash = HASH_OF(environment);
     107           13466 :         if (!target_hash) {
     108               0 :                 return env;
     109                 :         }
     110                 : 
     111                 :         /* first, we have to get the size of all the elements in the hash */
     112           13466 :         for (zend_hash_internal_pointer_reset_ex(target_hash, &pos);
     113          737677 :                         zend_hash_get_current_data_ex(target_hash, (void **) &element, &pos) == SUCCESS;
     114          710745 :                         zend_hash_move_forward_ex(target_hash, &pos)) {
     115                 :                 
     116          710745 :                 convert_to_string_ex(element);
     117          710745 :                 el_len = Z_STRLEN_PP(element);
     118          710745 :                 if (el_len == 0) {
     119          107971 :                         continue;
     120                 :                 }
     121                 :                 
     122          602774 :                 sizeenv += el_len+1;
     123                 :                 
     124          602774 :                 switch (zend_hash_get_current_key_ex(target_hash, &string_key, &string_length, &num_key, 0, &pos)) {
     125                 :                         case HASH_KEY_IS_STRING:
     126          602774 :                                 if (string_length == 0) {
     127               0 :                                         continue;
     128                 :                                 }
     129          602774 :                                 sizeenv += string_length+1;
     130                 :                                 break;
     131                 :                 }
     132                 :         }
     133                 : 
     134                 : #ifndef PHP_WIN32
     135           13466 :         ep = env.envarray = (char **) pecalloc(cnt + 1, sizeof(char *), is_persistent);
     136                 : #endif
     137           13466 :         p = env.envp = (char *) pecalloc(sizeenv + 4, 1, is_persistent);
     138                 : 
     139           13466 :         for (zend_hash_internal_pointer_reset_ex(target_hash, &pos);
     140          737677 :                         zend_hash_get_current_data_ex(target_hash, (void **) &element, &pos) == SUCCESS;
     141          710745 :                         zend_hash_move_forward_ex(target_hash, &pos)) {
     142                 :                 
     143          710745 :                 convert_to_string_ex(element);
     144          710745 :                 el_len = Z_STRLEN_PP(element);
     145                 :                 
     146          710745 :                 if (el_len == 0) {
     147          107971 :                         continue;
     148                 :                 }
     149                 :                 
     150          602774 :                 data = Z_STRVAL_PP(element);
     151          602774 :                 switch (zend_hash_get_current_key_ex(target_hash, &string_key, &string_length, &num_key, 0, &pos)) {
     152                 :                         case HASH_KEY_IS_STRING:
     153          602774 :                                 if (string_length == 0) {
     154               0 :                                         continue;
     155                 :                                 }
     156          602774 :                                 if (PG(safe_mode)) {
     157                 :                                         /* Check the protected list */
     158               0 :                                         if (zend_hash_exists(&BG(sm_protected_env_vars), string_key, string_length - 1)) {
     159               0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Safe Mode warning: Cannot override protected environment variable '%s'", string_key);
     160               0 :                                                 return env;
     161                 :                                         }
     162                 :                                         /* Check the allowed list */
     163               0 :                                         if (BG(sm_allowed_env_vars) && *BG(sm_allowed_env_vars)) {
     164               0 :                                                 char *allowed_env_vars = estrdup(BG(sm_allowed_env_vars));
     165               0 :                                                 char *strtok_buf = NULL;
     166               0 :                                                 char *allowed_prefix = php_strtok_r(allowed_env_vars, ", ", &strtok_buf);
     167               0 :                                                 zend_bool allowed = 0;
     168                 : 
     169               0 :                                                 while (allowed_prefix) {
     170               0 :                                                         if (!strncmp(allowed_prefix, string_key, strlen(allowed_prefix))) {
     171               0 :                                                                 allowed = 1;
     172               0 :                                                                 break;
     173                 :                                                         }
     174               0 :                                                         allowed_prefix = php_strtok_r(NULL, ", ", &strtok_buf);
     175                 :                                                 }
     176               0 :                                                 efree(allowed_env_vars);
     177               0 :                                                 if (!allowed) {
     178               0 :                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Safe Mode warning: Cannot set environment variable '%s' - it's not in the allowed list", string_key);
     179               0 :                                                         return env;
     180                 :                                                 }
     181                 :                                         }
     182                 :                                 }
     183                 : 
     184          602774 :                                 l = string_length + el_len + 1;
     185          602774 :                                 memcpy(p, string_key, string_length);
     186          602774 :                                 strcat(p, "=");
     187          602774 :                                 strcat(p, data);
     188                 :                                 
     189                 : #ifndef PHP_WIN32
     190          602774 :                                 *ep = p;
     191          602774 :                                 ++ep;
     192                 : #endif
     193          602774 :                                 p += l;
     194          602774 :                                 break;
     195                 :                         case HASH_KEY_IS_LONG:
     196               0 :                                 memcpy(p,data,el_len);
     197                 : #ifndef PHP_WIN32
     198               0 :                                 *ep = p;
     199               0 :                                 ++ep;
     200                 : #endif
     201               0 :                                 p += el_len + 1;
     202                 :                                 break;
     203                 :                         case HASH_KEY_NON_EXISTANT:
     204                 :                                 break;
     205                 :                 }
     206                 :         }       
     207                 : 
     208                 :         assert(p - env.envp <= sizeenv);
     209                 :         
     210           13466 :         zend_hash_internal_pointer_reset_ex(target_hash, &pos);
     211                 : 
     212           13466 :         return env;
     213                 : }
     214                 : /* }}} */
     215                 : 
     216                 : /* {{{ _php_free_envp */
     217                 : static void _php_free_envp(php_process_env_t env, int is_persistent)
     218           13476 : {
     219                 : #ifndef PHP_WIN32
     220           13476 :         if (env.envarray) {
     221           13468 :                 pefree(env.envarray, is_persistent);
     222                 :         }
     223                 : #endif
     224           13476 :         if (env.envp) {
     225           13468 :                 pefree(env.envp, is_persistent);
     226                 :         }
     227           13476 : }
     228                 : /* }}} */
     229                 : 
     230                 : /* {{{ proc_open_rsrc_dtor */
     231                 : static void proc_open_rsrc_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     232           13476 : {
     233           13476 :         struct php_process_handle *proc = (struct php_process_handle*)rsrc->ptr;
     234                 :         int i;
     235                 : #ifdef PHP_WIN32
     236                 :         DWORD wstatus;
     237                 : #elif HAVE_SYS_WAIT_H
     238                 :         int wstatus;
     239                 :         pid_t wait_pid;
     240                 : #endif
     241                 : 
     242                 :         /* Close all handles to avoid a deadlock */
     243           53897 :         for (i = 0; i < proc->npipes; i++) {
     244           40421 :                 if (proc->pipes[i] != 0) {
     245           40418 :                         zend_list_delete(proc->pipes[i]);
     246           40418 :                         proc->pipes[i] = 0;
     247                 :                 }
     248                 :         }
     249                 :         
     250                 : #ifdef PHP_WIN32
     251                 :         
     252                 :         WaitForSingleObject(proc->childHandle, INFINITE);
     253                 :         GetExitCodeProcess(proc->childHandle, &wstatus);
     254                 :         FG(pclose_ret) = wstatus;
     255                 :         CloseHandle(proc->childHandle);
     256                 :         
     257                 : #elif HAVE_SYS_WAIT_H
     258                 :         
     259                 :         do {
     260           13476 :                 wait_pid = waitpid(proc->child, &wstatus, 0);
     261           13476 :         } while (wait_pid == -1 && errno == EINTR);
     262                 :         
     263           13476 :         if (wait_pid == -1)
     264           13457 :                 FG(pclose_ret) = -1;
     265                 :         else {
     266              19 :                 if (WIFEXITED(wstatus))
     267              16 :                         wstatus = WEXITSTATUS(wstatus);
     268              19 :                 FG(pclose_ret) = wstatus;
     269                 :         }
     270                 :         
     271                 : #else
     272                 :         FG(pclose_ret) = -1;
     273                 : #endif
     274           13476 :         _php_free_envp(proc->env, proc->is_persistent);
     275           13476 :         pefree(proc->command, proc->is_persistent);
     276           13476 :         pefree(proc, proc->is_persistent);
     277                 :         
     278           13476 : }
     279                 : /* }}} */
     280                 : 
     281                 : /* {{{ php_make_safe_mode_command */
     282                 : static int php_make_safe_mode_command(char *cmd, char **safecmd, int is_persistent TSRMLS_DC)
     283           13476 : {
     284                 :         int lcmd, larg0;
     285                 :         char *space, *sep, *arg0;
     286                 : 
     287           13476 :         if (!PG(safe_mode)) {
     288           13476 :                 *safecmd = pestrdup(cmd, is_persistent);
     289           13476 :                 return SUCCESS;
     290                 :         }
     291                 : 
     292               0 :         lcmd = strlen(cmd);
     293                 : 
     294               0 :         arg0 = estrndup(cmd, lcmd);
     295                 : 
     296               0 :         space = memchr(arg0, ' ', lcmd);
     297               0 :         if (space) {
     298               0 :                 *space = '\0';
     299               0 :                 larg0 = space - arg0;
     300                 :         } else {
     301               0 :                 larg0 = lcmd;
     302                 :         }
     303                 : 
     304               0 :         if (php_memnstr(arg0, "..", sizeof("..")-1, arg0 + larg0)) {
     305               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No '..' components allowed in path");
     306               0 :                 efree(arg0);
     307               0 :                 return FAILURE;
     308                 :         }
     309                 : 
     310               0 :         sep = zend_memrchr(arg0, PHP_DIR_SEPARATOR, larg0);
     311                 : 
     312               0 :         spprintf(safecmd, 0, "%s%s%s%s", PG(safe_mode_exec_dir), (sep ? sep : "/"), (sep ? "" : arg0), (space ? cmd + larg0 : ""));
     313                 : 
     314               0 :         efree(arg0);
     315               0 :         arg0 = php_escape_shell_cmd(*safecmd);
     316               0 :         efree(*safecmd);
     317               0 :         if (is_persistent) {
     318               0 :                 *safecmd = pestrdup(arg0, 1);
     319               0 :                 efree(arg0);
     320                 :         } else {
     321               0 :                 *safecmd = arg0;
     322                 :         }
     323                 : 
     324               0 :         return SUCCESS;
     325                 : }
     326                 : /* }}} */
     327                 : 
     328                 : /* {{{ PHP_MINIT_FUNCTION(proc_open) */
     329                 : PHP_MINIT_FUNCTION(proc_open)
     330           13565 : {
     331           13565 :         le_proc_open = zend_register_list_destructors_ex(proc_open_rsrc_dtor, NULL, "process", module_number);
     332           13565 :         return SUCCESS;
     333                 : }
     334                 : /* }}} */
     335                 : 
     336                 : /* {{{ proto bool proc_terminate(resource process [, long signal])
     337                 :    kill a process opened by proc_open */
     338                 : PHP_FUNCTION(proc_terminate)
     339               7 : {
     340                 :         zval *zproc;
     341                 :         struct php_process_handle *proc;
     342               7 :         long sig_no = SIGTERM;
     343                 :         
     344               7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zproc, &sig_no) == FAILURE) {
     345               0 :                 RETURN_FALSE;
     346                 :         }
     347                 : 
     348               7 :         ZEND_FETCH_RESOURCE(proc, struct php_process_handle *, &zproc, -1, "process", le_proc_open);
     349                 :         
     350                 : #ifdef PHP_WIN32
     351                 :         if (TerminateProcess(proc->childHandle, 255)) {
     352                 :                 RETURN_TRUE;
     353                 :         } else {
     354                 :                 RETURN_FALSE;
     355                 :         }
     356                 : #else
     357               7 :         if (kill(proc->child, sig_no) == 0) {
     358               6 :                 RETURN_TRUE;
     359                 :         } else {
     360               1 :                 RETURN_FALSE;
     361                 :         }
     362                 : #endif
     363                 : }
     364                 : /* }}} */
     365                 : 
     366                 : /* {{{ proto int proc_close(resource process)
     367                 :    close a process opened by proc_open */
     368                 : PHP_FUNCTION(proc_close)
     369           13474 : {
     370                 :         zval *zproc;
     371                 :         struct php_process_handle *proc;
     372                 :         
     373           13474 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zproc) == FAILURE) {
     374               0 :                 RETURN_FALSE;
     375                 :         }
     376                 : 
     377           13474 :         ZEND_FETCH_RESOURCE(proc, struct php_process_handle *, &zproc, -1, "process", le_proc_open);
     378                 :         
     379           13474 :         zend_list_delete(Z_LVAL_P(zproc));
     380           13474 :         RETURN_LONG(FG(pclose_ret));
     381                 : }
     382                 : /* }}} */
     383                 : 
     384                 : /* {{{ proto array proc_get_status(resource process)
     385                 :    get information about a process opened by proc_open */
     386                 : PHP_FUNCTION(proc_get_status)
     387           13469 : {
     388                 :         zval *zproc;
     389                 :         struct php_process_handle *proc;
     390                 : #ifdef PHP_WIN32
     391                 :         DWORD wstatus;
     392                 : #elif HAVE_SYS_WAIT_H
     393                 :         int wstatus;
     394                 :         pid_t wait_pid;
     395                 : #endif
     396           13469 :         int running = 1, signaled = 0, stopped = 0;
     397           13469 :         int exitcode = -1, termsig = 0, stopsig = 0;
     398                 :         
     399           13469 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zproc) == FAILURE) {
     400               0 :                 RETURN_FALSE;
     401                 :         }
     402                 : 
     403           13469 :         ZEND_FETCH_RESOURCE(proc, struct php_process_handle *, &zproc, -1, "process", le_proc_open);
     404                 : 
     405           13469 :         array_init(return_value);
     406                 : 
     407           13469 :         add_assoc_string(return_value, "command", proc->command, 1);
     408           13469 :         add_assoc_long(return_value, "pid", (long) proc->child);
     409                 :         
     410                 : #ifdef PHP_WIN32
     411                 :         
     412                 :         GetExitCodeProcess(proc->childHandle, &wstatus);
     413                 : 
     414                 :         running = wstatus == STILL_ACTIVE;
     415                 :         exitcode = running ? -1 : wstatus;
     416                 :         
     417                 : #elif HAVE_SYS_WAIT_H
     418                 :         
     419           13469 :         errno = 0;
     420           13469 :         wait_pid = waitpid(proc->child, &wstatus, WNOHANG|WUNTRACED);
     421                 :         
     422           13469 :         if (wait_pid == proc->child) {
     423           13457 :                 if (WIFEXITED(wstatus)) {
     424           13455 :                         running = 0;
     425           13455 :                         exitcode = WEXITSTATUS(wstatus);
     426                 :                 }
     427           13457 :                 if (WIFSIGNALED(wstatus)) {
     428               2 :                         running = 0;
     429               2 :                         signaled = 1;
     430                 : #ifdef NETWARE
     431                 :                         termsig = WIFTERMSIG(wstatus);
     432                 : #else
     433               2 :                         termsig = WTERMSIG(wstatus);
     434                 : #endif
     435                 :                 }
     436           13457 :                 if (WIFSTOPPED(wstatus)) {
     437               0 :                         stopped = 1;
     438               0 :                         stopsig = WSTOPSIG(wstatus);
     439                 :                 }
     440              12 :         } else if (wait_pid == -1) {
     441               1 :                 running = 0;
     442                 :         }
     443                 : #endif
     444                 : 
     445           13469 :         add_assoc_bool(return_value, "running", running);
     446           13469 :         add_assoc_bool(return_value, "signaled", signaled);
     447           13469 :         add_assoc_bool(return_value, "stopped", stopped);
     448           13469 :         add_assoc_long(return_value, "exitcode", exitcode);
     449           13469 :         add_assoc_long(return_value, "termsig", termsig);
     450           13469 :         add_assoc_long(return_value, "stopsig", stopsig);
     451                 : }
     452                 : /* }}} */
     453                 : 
     454                 : /* {{{ handy definitions for portability/readability */
     455                 : #ifdef PHP_WIN32
     456                 : # define pipe(pair)             (CreatePipe(&pair[0], &pair[1], &security, 2048L) ? 0 : -1)
     457                 : 
     458                 : # define COMSPEC_NT     "cmd.exe"
     459                 : # define COMSPEC_9X     "command.com"
     460                 : 
     461                 : static inline HANDLE dup_handle(HANDLE src, BOOL inherit, BOOL closeorig)
     462                 : {
     463                 :         HANDLE copy, self = GetCurrentProcess();
     464                 :         
     465                 :         if (!DuplicateHandle(self, src, self, &copy, 0, inherit, DUPLICATE_SAME_ACCESS |
     466                 :                                 (closeorig ? DUPLICATE_CLOSE_SOURCE : 0)))
     467                 :                 return NULL;
     468                 :         return copy;
     469                 : }
     470                 : 
     471                 : static inline HANDLE dup_fd_as_handle(int fd)
     472                 : {
     473                 :         return dup_handle((HANDLE)_get_osfhandle(fd), TRUE, FALSE);
     474                 : }
     475                 : 
     476                 : # define close_descriptor(fd)   CloseHandle(fd)
     477                 : #else
     478                 : # define close_descriptor(fd)   close(fd)
     479                 : #endif
     480                 : 
     481                 : #define DESC_PIPE               1
     482                 : #define DESC_FILE               2
     483                 : #define DESC_PARENT_MODE_WRITE  8
     484                 : 
     485                 : struct php_proc_open_descriptor_item {
     486                 :         int index;                                                      /* desired fd number in child process */
     487                 :         php_file_descriptor_t parentend, childend;      /* fds for pipes in parent/child */
     488                 :         int mode;                                                       /* mode for proc_open code */
     489                 :         int mode_flags;                                         /* mode flags for opening fds */
     490                 : };
     491                 : /* }}} */
     492                 : 
     493                 : /* {{{ proto resource proc_open(string command, array descriptorspec, array &pipes [, string cwd [, array env [, array other_options]]])
     494                 :    Run a process with more control over it's file descriptors */
     495                 : PHP_FUNCTION(proc_open)
     496           13476 : {
     497           13476 :         char *command, *cwd=NULL;
     498                 :         int command_len, cwd_len;
     499                 :         zval *descriptorspec;
     500                 :         zval *pipes;
     501           13476 :         zval *environment = NULL;
     502           13476 :         zval *other_options = NULL;
     503                 :         php_process_env_t env;
     504           13476 :         int ndesc = 0;
     505                 :         int i;
     506           13476 :         zval **descitem = NULL;
     507                 :         HashPosition pos;
     508                 :         struct php_proc_open_descriptor_item descriptors[PHP_PROC_OPEN_MAX_DESCRIPTORS];
     509                 : #ifdef PHP_WIN32
     510                 :         PROCESS_INFORMATION pi;
     511                 :         HANDLE childHandle;
     512                 :         STARTUPINFO si;
     513                 :         BOOL newprocok;
     514                 :         SECURITY_ATTRIBUTES security;
     515                 :         char *command_with_cmd;
     516                 :         UINT old_error_mode;
     517                 : #endif
     518                 : #ifdef NETWARE
     519                 :         char** child_argv = NULL;
     520                 :         char* command_dup = NULL;
     521                 :         char* orig_cwd = NULL;
     522                 :         int command_num_args = 0;
     523                 :         wiring_t channel;
     524                 : #endif
     525                 :         php_process_id_t child;
     526                 :         struct php_process_handle *proc;
     527           13476 :         int is_persistent = 0; /* TODO: ensure that persistent procs will work */
     528                 : #ifdef PHP_WIN32
     529                 :         int suppress_errors = 0;
     530                 :         int bypass_shell = 0;
     531                 : #endif
     532                 : #if PHP_CAN_DO_PTS
     533                 :         php_file_descriptor_t dev_ptmx = -1;    /* master */
     534                 :         php_file_descriptor_t slave_pty = -1;
     535                 : #endif
     536                 : 
     537           13476 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "saz|s!a!a!", &command,
     538                 :                                 &command_len, &descriptorspec, &pipes, &cwd, &cwd_len, &environment,
     539                 :                                 &other_options) == FAILURE) {
     540               0 :                 RETURN_FALSE;
     541                 :         }
     542                 : 
     543           13476 :         if (FAILURE == php_make_safe_mode_command(command, &command, is_persistent TSRMLS_CC)) {
     544               0 :                 RETURN_FALSE;
     545                 :         }
     546                 : 
     547                 : #ifdef PHP_WIN32
     548                 :         if (other_options) {
     549                 :                 zval **item;
     550                 :                 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(other_options), "suppress_errors", sizeof("suppress_errors"), (void**)&item)) {
     551                 :                         if ((Z_TYPE_PP(item) == IS_BOOL || Z_TYPE_PP(item) == IS_LONG) &&
     552                 :                             Z_LVAL_PP(item)) {
     553                 :                                 suppress_errors = 1;
     554                 :                         }
     555                 :                 }       
     556                 :                 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(other_options), "bypass_shell", sizeof("bypass_shell"), (void**)&item)) {
     557                 :                         if ((Z_TYPE_PP(item) == IS_BOOL || Z_TYPE_PP(item) == IS_LONG) &&
     558                 :                             Z_LVAL_PP(item)) {
     559                 :                                 bypass_shell = 1;
     560                 :                         }
     561                 :                 }       
     562                 :         }
     563                 : #endif
     564                 :         
     565           13476 :         command_len = strlen(command);
     566                 : 
     567           13476 :         if (environment) {
     568           13468 :                 env = _php_array_to_envp(environment, is_persistent TSRMLS_CC);
     569                 :         } else {
     570               8 :                 memset(&env, 0, sizeof(env));
     571                 :         }
     572                 : 
     573           13476 :         memset(descriptors, 0, sizeof(descriptors));
     574                 : 
     575                 : #ifdef PHP_WIN32
     576                 :         /* we use this to allow the child to inherit handles */
     577                 :         memset(&security, 0, sizeof(security));
     578                 :         security.nLength = sizeof(security);
     579                 :         security.bInheritHandle = TRUE;
     580                 :         security.lpSecurityDescriptor = NULL;
     581                 : #endif
     582                 :         
     583                 :         /* walk the descriptor spec and set up files/pipes */
     584           13476 :         zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(descriptorspec), &pos);
     585           67373 :         while (zend_hash_get_current_data_ex(Z_ARRVAL_P(descriptorspec), (void **)&descitem, &pos) == SUCCESS) {
     586                 :                 char *str_index;
     587                 :                 ulong nindex;
     588                 :                 zval **ztype;
     589                 : 
     590           40421 :                 str_index = NULL;
     591           40421 :                 zend_hash_get_current_key_ex(Z_ARRVAL_P(descriptorspec), &str_index, NULL, &nindex, 0, &pos);
     592                 : 
     593           40421 :                 if (str_index) {
     594               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "descriptor spec must be an integer indexed array");
     595               0 :                         goto exit_fail;
     596                 :                 }
     597                 : 
     598           40421 :                 descriptors[ndesc].index = nindex;
     599                 : 
     600           40421 :                 if (Z_TYPE_PP(descitem) == IS_RESOURCE) {
     601                 :                         /* should be a stream - try and dup the descriptor */
     602                 :                         php_stream *stream;
     603                 :                         int fd;
     604                 : 
     605               3 :                         php_stream_from_zval(stream, descitem);
     606                 : 
     607               3 :                         if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **)&fd, REPORT_ERRORS)) {
     608               0 :                                 goto exit_fail;
     609                 :                         }
     610                 : 
     611                 : #ifdef PHP_WIN32
     612                 :                         descriptors[ndesc].childend = dup_fd_as_handle(fd);
     613                 :                         if (descriptors[ndesc].childend == NULL) {
     614                 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to dup File-Handle for descriptor %d", nindex);
     615                 :                                 goto exit_fail;
     616                 :                         }
     617                 : #else
     618               3 :                         descriptors[ndesc].childend = dup(fd);
     619               3 :                         if (descriptors[ndesc].childend < 0) {
     620               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to dup File-Handle for descriptor %ld - %s", nindex, strerror(errno));
     621               0 :                                 goto exit_fail;
     622                 :                         }
     623                 : #endif
     624               3 :                         descriptors[ndesc].mode = DESC_FILE;
     625                 : 
     626           40418 :                 } else if (Z_TYPE_PP(descitem) != IS_ARRAY) {
     627               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Descriptor item must be either an array or a File-Handle");
     628               0 :                         goto exit_fail;
     629                 :                 } else {
     630                 : 
     631           40418 :                         if (zend_hash_index_find(Z_ARRVAL_PP(descitem), 0, (void **)&ztype) == SUCCESS) {
     632           40418 :                                 convert_to_string_ex(ztype);
     633                 :                         } else {
     634               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing handle qualifier in array");
     635               0 :                                 goto exit_fail;
     636                 :                         }
     637                 : 
     638           40418 :                         if (strcmp(Z_STRVAL_PP(ztype), "pipe") == 0) {
     639                 :                                 php_file_descriptor_t newpipe[2];
     640                 :                                 zval **zmode;
     641                 : 
     642           40418 :                                 if (zend_hash_index_find(Z_ARRVAL_PP(descitem), 1, (void **)&zmode) == SUCCESS) {
     643           40418 :                                         convert_to_string_ex(zmode);
     644                 :                                 } else {
     645               0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing mode parameter for 'pipe'");
     646               0 :                                         goto exit_fail;
     647                 :                                 }
     648                 : 
     649           40418 :                                 descriptors[ndesc].mode = DESC_PIPE;
     650                 : 
     651           40418 :                                 if (0 != pipe(newpipe)) {
     652               0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to create pipe %s", strerror(errno));
     653               0 :                                         goto exit_fail;
     654                 :                                 }
     655                 : 
     656           40418 :                                 if (strncmp(Z_STRVAL_PP(zmode), "w", 1) != 0) {
     657           13477 :                                         descriptors[ndesc].parentend = newpipe[1];
     658           13477 :                                         descriptors[ndesc].childend = newpipe[0];
     659           13477 :                                         descriptors[ndesc].mode |= DESC_PARENT_MODE_WRITE;
     660                 :                                 } else {
     661           26941 :                                         descriptors[ndesc].parentend = newpipe[0];
     662           26941 :                                         descriptors[ndesc].childend = newpipe[1];
     663                 :                                 }
     664                 : #ifdef PHP_WIN32
     665                 :                                 /* don't let the child inherit the parent side of the pipe */
     666                 :                                 descriptors[ndesc].parentend = dup_handle(descriptors[ndesc].parentend, FALSE, TRUE);
     667                 : #endif
     668           40418 :                                 descriptors[ndesc].mode_flags = descriptors[ndesc].mode & DESC_PARENT_MODE_WRITE ? O_WRONLY : O_RDONLY;
     669                 : #ifdef PHP_WIN32
     670                 :                                 if (Z_STRLEN_PP(zmode) >= 2 && Z_STRVAL_PP(zmode)[1] == 'b')
     671                 :                                         descriptors[ndesc].mode_flags |= O_BINARY;
     672                 : #endif
     673                 : 
     674               0 :                         } else if (strcmp(Z_STRVAL_PP(ztype), "file") == 0) {
     675                 :                                 zval **zfile, **zmode;
     676                 :                                 int fd;
     677                 :                                 php_stream *stream;
     678                 : 
     679               0 :                                 descriptors[ndesc].mode = DESC_FILE;
     680                 : 
     681               0 :                                 if (zend_hash_index_find(Z_ARRVAL_PP(descitem), 1, (void **)&zfile) == SUCCESS) {
     682               0 :                                         convert_to_string_ex(zfile);
     683                 :                                 } else {
     684               0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing file name parameter for 'file'");
     685               0 :                                         goto exit_fail;
     686                 :                                 }
     687                 : 
     688               0 :                                 if (zend_hash_index_find(Z_ARRVAL_PP(descitem), 2, (void **)&zmode) == SUCCESS) {
     689               0 :                                         convert_to_string_ex(zmode);
     690                 :                                 } else {
     691               0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing mode parameter for 'file'");
     692               0 :                                         goto exit_fail;
     693                 :                                 }
     694                 : 
     695                 :                                 /* try a wrapper */
     696               0 :                                 stream = php_stream_open_wrapper(Z_STRVAL_PP(zfile), Z_STRVAL_PP(zmode),
     697                 :                                                 ENFORCE_SAFE_MODE|REPORT_ERRORS|STREAM_WILL_CAST, NULL);
     698                 : 
     699                 :                                 /* force into an fd */
     700               0 :                                 if (stream == NULL || FAILURE == php_stream_cast(stream,
     701                 :                                                         PHP_STREAM_CAST_RELEASE|PHP_STREAM_AS_FD,
     702                 :                                                         (void **)&fd, REPORT_ERRORS)) {
     703                 :                                         goto exit_fail;
     704                 :                                 }
     705                 : 
     706                 : #ifdef PHP_WIN32
     707                 :                                 descriptors[ndesc].childend = dup_fd_as_handle(fd);
     708                 :                                 _close(fd);
     709                 : 
     710                 :                                 /* simulate the append mode by fseeking to the end of the file
     711                 :                                 this introduces a potential race-condition, but it is the best we can do, though */
     712                 :                                 if (strchr(Z_STRVAL_PP(zmode), 'a')) {
     713                 :                                         SetFilePointer(descriptors[ndesc].childend, 0, NULL, FILE_END);
     714                 :                                 }
     715                 : #else
     716               0 :                                 descriptors[ndesc].childend = fd;
     717                 : #endif
     718               0 :                         } else if (strcmp(Z_STRVAL_PP(ztype), "pty") == 0) {
     719                 : #if PHP_CAN_DO_PTS
     720                 :                                 if (dev_ptmx == -1) {
     721                 :                                         /* open things up */
     722                 :                                         dev_ptmx = open("/dev/ptmx", O_RDWR);
     723                 :                                         if (dev_ptmx == -1) {
     724                 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to open /dev/ptmx, errno %d", errno);
     725                 :                                                 goto exit_fail;
     726                 :                                         }
     727                 :                                         grantpt(dev_ptmx);
     728                 :                                         unlockpt(dev_ptmx);
     729                 :                                         slave_pty = open(ptsname(dev_ptmx), O_RDWR);
     730                 : 
     731                 :                                         if (slave_pty == -1) {
     732                 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to open slave pty, errno %d", errno);
     733                 :                                                 goto exit_fail;
     734                 :                                         }
     735                 :                                 }
     736                 :                                 descriptors[ndesc].mode = DESC_PIPE;
     737                 :                                 descriptors[ndesc].childend = dup(slave_pty);
     738                 :                                 descriptors[ndesc].parentend = dup(dev_ptmx);
     739                 :                                 descriptors[ndesc].mode_flags = O_RDWR;
     740                 : #else
     741               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pty pseudo terminal not supported on this system");
     742               0 :                                 goto exit_fail;
     743                 : #endif
     744                 :                         } else {
     745               0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s is not a valid descriptor spec/mode", Z_STRVAL_PP(ztype));
     746               0 :                                 goto exit_fail;
     747                 :                         }
     748                 :                 }
     749                 : 
     750           40421 :                 zend_hash_move_forward_ex(Z_ARRVAL_P(descriptorspec), &pos);
     751           40421 :                 if (++ndesc == PHP_PROC_OPEN_MAX_DESCRIPTORS)
     752               0 :                         break;
     753                 :         }
     754                 : 
     755                 : #ifdef PHP_WIN32
     756                 :         memset(&si, 0, sizeof(si));
     757                 :         si.cb = sizeof(si);
     758                 :         si.dwFlags = STARTF_USESTDHANDLES;
     759                 :         
     760                 :         si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
     761                 :         si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
     762                 :         si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
     763                 :         
     764                 :         /* redirect stdin/stdout/stderr if requested */
     765                 :         for (i = 0; i < ndesc; i++) {
     766                 :                 switch(descriptors[i].index) {
     767                 :                         case 0:
     768                 :                                 si.hStdInput = descriptors[i].childend;
     769                 :                                 break;
     770                 :                         case 1:
     771                 :                                 si.hStdOutput = descriptors[i].childend;
     772                 :                                 break;
     773                 :                         case 2:
     774                 :                                 si.hStdError = descriptors[i].childend;
     775                 :                                 break;
     776                 :                 }
     777                 :         }
     778                 : 
     779                 :         
     780                 :         memset(&pi, 0, sizeof(pi));
     781                 :         
     782                 :         if (suppress_errors) {
     783                 :                 old_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX);
     784                 :         }
     785                 :         
     786                 :         if (bypass_shell) {
     787                 :                 newprocok = CreateProcess(NULL, command, &security, &security, TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW, env.envp, cwd, &si, &pi);
     788                 :         } else {
     789                 :                 spprintf(&command_with_cmd, 0, "%s /c %s", GetVersion() < 0x80000000 ? COMSPEC_NT : COMSPEC_9X, command);
     790                 : 
     791                 :                 newprocok = CreateProcess(NULL, command_with_cmd, &security, &security, TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW, env.envp, cwd, &si, &pi);
     792                 :                 
     793                 :                 efree(command_with_cmd);
     794                 :         }
     795                 : 
     796                 :         if (suppress_errors) {
     797                 :                 SetErrorMode(old_error_mode);
     798                 :         }
     799                 :         
     800                 :         if (FALSE == newprocok) {
     801                 :                 DWORD dw = GetLastError();
     802                 : 
     803                 :                 /* clean up all the descriptors */
     804                 :                 for (i = 0; i < ndesc; i++) {
     805                 :                         CloseHandle(descriptors[i].childend);
     806                 :                         if (descriptors[i].parentend) {
     807                 :                                 CloseHandle(descriptors[i].parentend);
     808                 :                         }
     809                 :                 }
     810                 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "CreateProcess failed, error code - %u", dw);
     811                 :                 goto exit_fail;
     812                 :         }
     813                 : 
     814                 :         childHandle = pi.hProcess;
     815                 :         child       = pi.dwProcessId;
     816                 :         CloseHandle(pi.hThread);
     817                 : 
     818                 : #elif defined(NETWARE)
     819                 :         if (cwd) {
     820                 :                 orig_cwd = getcwd(NULL, PATH_MAX);
     821                 :                 chdir2(cwd);
     822                 :         }
     823                 :         channel.infd = descriptors[0].childend;
     824                 :         channel.outfd = descriptors[1].childend;
     825                 :         channel.errfd = -1;
     826                 :         /* Duplicate the command as processing downwards will modify it*/
     827                 :         command_dup = strdup(command);
     828                 :         if (!command_dup) {
     829                 :                 goto exit_fail;
     830                 :         }
     831                 :         /* get a number of args */
     832                 :         construct_argc_argv(command_dup, NULL, &command_num_args, NULL);
     833                 :         child_argv = (char**) malloc((command_num_args + 1) * sizeof(char*));
     834                 :         if(!child_argv) {
     835                 :                 free(command_dup);
     836                 :                 if (cwd && orig_cwd) {
     837                 :                         chdir2(orig_cwd);
     838                 :                         free(orig_cwd);
     839                 :                 }
     840                 :         }
     841                 :         /* fill the child arg vector */
     842                 :         construct_argc_argv(command_dup, NULL, &command_num_args, child_argv);
     843                 :         child_argv[command_num_args] = NULL;
     844                 :         child = procve(child_argv[0], PROC_DETACHED|PROC_INHERIT_CWD, NULL, &channel, NULL, NULL, 0, NULL, (const char**)child_argv);
     845                 :         free(child_argv);
     846                 :         free(command_dup);
     847                 :         if (cwd && orig_cwd) {
     848                 :                 chdir2(orig_cwd);
     849                 :                 free(orig_cwd);
     850                 :         }
     851                 :         if (child < 0) {
     852                 :                 /* failed to fork() */
     853                 :                 /* clean up all the descriptors */
     854                 :                 for (i = 0; i < ndesc; i++) {
     855                 :                         close(descriptors[i].childend);
     856                 :                         if (descriptors[i].parentend)
     857                 :                                 close(descriptors[i].parentend);
     858                 :                 }
     859                 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "procve failed - %s", strerror(errno));
     860                 :                 goto exit_fail;
     861                 :         }
     862                 : #elif HAVE_FORK
     863                 :         /* the unix way */
     864           13476 :         child = fork();
     865                 : 
     866           26951 :         if (child == 0) {
     867                 :                 /* this is the child process */
     868                 : 
     869                 : #if PHP_CAN_DO_PTS
     870                 :                 if (dev_ptmx >= 0) {
     871                 :                         int my_pid = getpid();
     872                 : 
     873                 : #ifdef TIOCNOTTY
     874                 :                         /* detach from original tty. Might only need this if isatty(0) is true */
     875                 :                         ioctl(0,TIOCNOTTY,NULL);
     876                 : #else
     877                 :                         setsid();
     878                 : #endif
     879                 :                         /* become process group leader */
     880                 :                         setpgid(my_pid, my_pid);
     881                 :                         tcsetpgrp(0, my_pid);
     882                 :                 }
     883                 : #endif
     884                 :                 
     885                 :                 /* close those descriptors that we just opened for the parent stuff,
     886                 :                  * dup new descriptors into required descriptors and close the original
     887                 :                  * cruft */
     888           53893 :                 for (i = 0; i < ndesc; i++) {
     889           40418 :                         switch (descriptors[i].mode & ~DESC_PARENT_MODE_WRITE) {
     890                 :                                 case DESC_PIPE:
     891           40415 :                                         close(descriptors[i].parentend);
     892                 :                                         break;
     893                 :                         }
     894           40418 :                         if (dup2(descriptors[i].childend, descriptors[i].index) < 0)
     895               0 :                                 perror("dup2");
     896           40418 :                         if (descriptors[i].childend != descriptors[i].index)
     897           40418 :                                 close(descriptors[i].childend);
     898                 :                 }
     899                 : 
     900                 : #if PHP_CAN_DO_PTS
     901                 :                 if (dev_ptmx >= 0) {
     902                 :                         close(dev_ptmx);
     903                 :                         close(slave_pty);
     904                 :                 }
     905                 : #endif
     906                 :                 
     907           13475 :                 if (cwd) {
     908           13468 :                         chdir(cwd);
     909                 :                 }
     910                 :                 
     911           13475 :                 if (env.envarray) {
     912           13468 :                         execle("/bin/sh", "sh", "-c", command, NULL, env.envarray);
     913                 :                 } else {
     914               7 :                         execl("/bin/sh", "sh", "-c", command, NULL);
     915                 :                 }
     916               0 :                 _exit(127);
     917                 : 
     918           13476 :         } else if (child < 0) {
     919                 :                 /* failed to fork() */
     920                 : 
     921                 :                 /* clean up all the descriptors */
     922               0 :                 for (i = 0; i < ndesc; i++) {
     923               0 :                         close(descriptors[i].childend);
     924               0 :                         if (descriptors[i].parentend)
     925               0 :                                 close(descriptors[i].parentend);
     926                 :                 }
     927                 : 
     928               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "fork failed - %s", strerror(errno));
     929                 : 
     930               0 :                 goto exit_fail;
     931                 : 
     932                 :         }
     933                 : #else
     934                 : # error You lose (configure should not have let you get here)
     935                 : #endif
     936                 :         /* we forked/spawned and this is the parent */
     937                 : 
     938           13476 :         proc = (struct php_process_handle*)pemalloc(sizeof(struct php_process_handle), is_persistent);
     939           13476 :         proc->is_persistent = is_persistent;
     940           13476 :         proc->command = command;
     941           13476 :         proc->npipes = ndesc;
     942           13476 :         proc->child = child;
     943                 : #ifdef PHP_WIN32
     944                 :         proc->childHandle = childHandle;
     945                 : #endif
     946           13476 :         proc->env = env;
     947                 : 
     948           13476 :         if (pipes != NULL) {
     949           13476 :                 zval_dtor(pipes);
     950                 :         }
     951           13476 :         array_init(pipes);
     952                 : 
     953                 : #if PHP_CAN_DO_PTS
     954                 :         if (dev_ptmx >= 0) {
     955                 :                 close(dev_ptmx);
     956                 :                 close(slave_pty);
     957                 :         }
     958                 : #endif
     959                 : 
     960                 :         /* clean up all the child ends and then open streams on the parent
     961                 :          * ends, where appropriate */
     962           53897 :         for (i = 0; i < ndesc; i++) {
     963           40421 :                 char *mode_string=NULL;
     964           40421 :                 php_stream *stream = NULL;
     965                 : 
     966           40421 :                 close_descriptor(descriptors[i].childend);
     967                 : 
     968           40421 :                 switch (descriptors[i].mode & ~DESC_PARENT_MODE_WRITE) {
     969                 :                         case DESC_PIPE:
     970           40418 :                                 switch(descriptors[i].mode_flags) {
     971                 : #ifdef PHP_WIN32
     972                 :                                         case O_WRONLY|O_BINARY:
     973                 :                                                 mode_string = "wb";
     974                 :                                                 break;
     975                 :                                         case O_RDONLY|O_BINARY:
     976                 :                                                 mode_string = "rb";
     977                 :                                                 break;
     978                 : #endif
     979                 :                                         case O_WRONLY:
     980           13477 :                                                 mode_string = "w";
     981           13477 :                                                 break;
     982                 :                                         case O_RDONLY:
     983           26941 :                                                 mode_string = "r";
     984           26941 :                                                 break;
     985                 :                                         case O_RDWR:
     986               0 :                                                 mode_string = "r+";
     987                 :                                                 break;
     988                 :                                 }
     989                 : #ifdef PHP_WIN32
     990                 :                                 stream = php_stream_fopen_from_fd(_open_osfhandle((zend_intptr_t)descriptors[i].parentend,
     991                 :                                                         descriptors[i].mode_flags), mode_string, NULL);
     992                 : #else
     993           40418 :                                 stream = php_stream_fopen_from_fd(descriptors[i].parentend, mode_string, NULL);
     994                 : # if defined(F_SETFD) && defined(FD_CLOEXEC)
     995                 :                                 /* mark the descriptor close-on-exec, so that it won't be inherited by potential other children */
     996           40418 :                                 fcntl(descriptors[i].parentend, F_SETFD, FD_CLOEXEC);
     997                 : # endif
     998                 : #endif
     999           40418 :                                 if (stream) {
    1000                 :                                         zval *retfp;
    1001                 : 
    1002                 :                                         /* nasty hack; don't copy it */
    1003           40418 :                                         stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
    1004                 :                                         
    1005           40418 :                                         MAKE_STD_ZVAL(retfp);
    1006           40418 :                                         php_stream_to_zval(stream, retfp);
    1007           40418 :                                         add_index_zval(pipes, descriptors[i].index, retfp);
    1008                 : 
    1009           40418 :                                         proc->pipes[i] = Z_LVAL_P(retfp);
    1010                 :                                 }
    1011           40418 :                                 break;
    1012                 :                         default:
    1013               3 :                                 proc->pipes[i] = 0;
    1014                 :                 }
    1015                 :         }
    1016                 : 
    1017           13476 :         ZEND_REGISTER_RESOURCE(return_value, proc, le_proc_open);
    1018           13476 :         return;
    1019                 : 
    1020               0 : exit_fail:
    1021               0 :         _php_free_envp(env, is_persistent);
    1022               0 :         pefree(command, is_persistent);
    1023                 : #if PHP_CAN_DO_PTS
    1024                 :         if (dev_ptmx >= 0) {
    1025                 :                 close(dev_ptmx);
    1026                 :         }
    1027                 :         if (slave_pty >= 0) {
    1028                 :                 close(slave_pty);
    1029                 :         }
    1030                 : #endif
    1031               0 :         RETURN_FALSE;
    1032                 : 
    1033                 : }
    1034                 : /* }}} */
    1035                 : 
    1036                 : #endif /* PHP_CAN_SUPPORT_PROC_OPEN */
    1037                 : 
    1038                 : /*
    1039                 :  * Local variables:
    1040                 :  * tab-width: 4
    1041                 :  * c-basic-offset: 4
    1042                 :  * End:
    1043                 :  * vim600: sw=4 ts=4 fdm=marker
    1044                 :  * vim<600: sw=4 ts=4
    1045                 :  */

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:23 +0000 (5 days ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.