proc_open.c 25 KB

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