Skip to content

Commit 151a915

Browse files
committed
Introduce set_proc_descriptor_to_pipe helper in proc_open.c
1 parent 472cdac commit 151a915

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

ext/standard/proc_open.c

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,38 @@ static int set_proc_descriptor_to_blackhole(struct php_proc_open_descriptor_item
512512
return SUCCESS;
513513
}
514514

515+
static int set_proc_descriptor_to_pipe(struct php_proc_open_descriptor_item *desc, zval *zmode)
516+
{
517+
php_file_descriptor_t newpipe[2];
518+
519+
if (pipe(newpipe)) {
520+
php_error_docref(NULL, E_WARNING, "Unable to create pipe %s", strerror(errno));
521+
return FAILURE;
522+
}
523+
524+
desc->mode = DESC_PIPE;
525+
526+
if (strncmp(Z_STRVAL_P(zmode), "w", 1) != 0) {
527+
desc->parentend = newpipe[1];
528+
desc->childend = newpipe[0];
529+
desc->mode_flags = O_WRONLY;
530+
} else {
531+
desc->parentend = newpipe[0];
532+
desc->childend = newpipe[1];
533+
desc->mode_flags = O_RDONLY;
534+
}
535+
536+
#ifdef PHP_WIN32
537+
/* don't let the child inherit the parent side of the pipe */
538+
desc->parentend = dup_handle(desc->parentend, FALSE, TRUE);
539+
540+
if (Z_STRLEN_P(zmode) >= 2 && Z_STRVAL_P(zmode)[1] == 'b')
541+
desc->mode_flags |= O_BINARY;
542+
#endif
543+
544+
return SUCCESS;
545+
}
546+
515547
static void efree_argv(char **argv)
516548
{
517549
if (argv) {
@@ -690,7 +722,6 @@ PHP_FUNCTION(proc_open)
690722
}
691723

692724
if (strcmp(Z_STRVAL_P(ztype), "pipe") == 0) {
693-
php_file_descriptor_t newpipe[2];
694725
zval *zmode;
695726

696727
if ((zmode = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
@@ -702,30 +733,9 @@ PHP_FUNCTION(proc_open)
702733
goto exit_fail;
703734
}
704735

705-
descriptors[ndesc].mode = DESC_PIPE;
706-
707-
if (0 != pipe(newpipe)) {
708-
php_error_docref(NULL, E_WARNING, "Unable to create pipe %s", strerror(errno));
736+
if (set_proc_descriptor_to_pipe(&descriptors[ndesc], zmode) == FAILURE) {
709737
goto exit_fail;
710738
}
711-
712-
if (strncmp(Z_STRVAL_P(zmode), "w", 1) != 0) {
713-
descriptors[ndesc].parentend = newpipe[1];
714-
descriptors[ndesc].childend = newpipe[0];
715-
descriptors[ndesc].mode_flags = O_WRONLY;
716-
} else {
717-
descriptors[ndesc].parentend = newpipe[0];
718-
descriptors[ndesc].childend = newpipe[1];
719-
descriptors[ndesc].mode_flags = O_RDONLY;
720-
}
721-
#ifdef PHP_WIN32
722-
/* don't let the child inherit the parent side of the pipe */
723-
descriptors[ndesc].parentend = dup_handle(descriptors[ndesc].parentend, FALSE, TRUE);
724-
725-
if (Z_STRLEN_P(zmode) >= 2 && Z_STRVAL_P(zmode)[1] == 'b')
726-
descriptors[ndesc].mode_flags |= O_BINARY;
727-
#endif
728-
729739
} else if (strcmp(Z_STRVAL_P(ztype), "file") == 0) {
730740
zval *zfile, *zmode;
731741
php_socket_t fd;

0 commit comments

Comments
 (0)