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

LCOV - code coverage report
Current view: top level - ext/standard - proc_open.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 208 258 80.6 %
Date: 2015-07-31 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 31 Jul 2015 08:58:59 +0000 (43 hours ago)

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