Skip to content

Commit daeb5e2

Browse files
committed
resolve #30 #31
1 parent 68776e3 commit daeb5e2

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

phpdbg.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -590,23 +590,24 @@ int phpdbg_open_socket(short port) /* {{{ */
590590
} /* }}} */
591591

592592
static inline void phpdbg_close_sockets(int (*socket)[2], FILE *streams[2]) /* {{{ */
593-
{
594-
if (streams[0]) {
595-
fclose(streams[0]);
596-
}
597-
593+
{
598594
if ((*socket)[0]) {
595+
shutdown((*socket)[0], SHUT_RDWR);
599596
close((*socket)[0]);
600597
}
601598

602-
if (streams[1]) {
603-
fflush(streams[1]);
604-
fclose(streams[1]);
599+
if (streams[0]) {
600+
fclose(streams[0]);
605601
}
606602

607603
if ((*socket)[1]) {
604+
shutdown((*socket)[1], SHUT_RDWR);
608605
close((*socket)[1]);
609606
}
607+
608+
if (streams[1]) {
609+
fclose(streams[1]);
610+
}
610611
} /* }}} */
611612

612613
/* don't inline this, want to debug it easily, will inline when done */
@@ -646,6 +647,8 @@ int phpdbg_open_sockets(int port[2], int (*listen)[2], int (*socket)[2], FILE* s
646647
dup2((*socket)[0], fileno(stdin));
647648
dup2((*socket)[1], fileno(stdout));
648649

650+
setbuf(stdout, NULL);
651+
649652
streams[0] = fdopen((*socket)[0], "r");
650653
streams[1] = fdopen((*socket)[1], "w");
651654

@@ -837,7 +840,10 @@ int main(int argc, char **argv) /* {{{ */
837840
/* setup remote server if necessary */
838841
if (!cleaning &&
839842
(listen[0] > 0 && listen[1] > 0)) {
840-
phpdbg_open_sockets(listen, &server, &socket, streams);
843+
if (phpdbg_open_sockets(listen, &server, &socket, streams) == FAILURE) {
844+
fprintf(stderr, "Failed to open remote console on ports %d/%d", listen[0], listen[1]);
845+
goto phpdbg_out;
846+
}
841847
}
842848

843849
phpdbg->ini_defaults = phpdbg_ini_defaults;
@@ -866,13 +872,19 @@ int main(int argc, char **argv) /* {{{ */
866872
if (phpdbg->startup(phpdbg) == SUCCESS) {
867873
zend_activate(TSRMLS_C);
868874

875+
/* do not install sigint handlers for remote consoles */
876+
/* sending SIGINT then provides a decent way of shutting down the server */
869877
#ifdef ZEND_SIGNALS
870-
zend_try {
871-
zend_signal_activate(TSRMLS_C);
872-
zend_signal(SIGINT, phpdbg_sigint_handler TSRMLS_CC);
873-
} zend_end_try();
878+
if (listen[0] < 0) {
879+
zend_try {
880+
zend_signal_activate(TSRMLS_C);
881+
zend_signal(SIGINT, phpdbg_sigint_handler TSRMLS_CC);
882+
} zend_end_try();
883+
}
874884
#else
875-
signal(SIGINT, phpdbg_sigint_handler);
885+
if (listen[0] < 0) {
886+
signal(SIGINT, phpdbg_sigint_handler);
887+
}
876888
#endif
877889

878890
PG(modules_activated) = 0;
@@ -983,9 +995,7 @@ int main(int argc, char **argv) /* {{{ */
983995
/* set streams */
984996
if (streams[0] && streams[1]) {
985997
PHPDBG_G(flags) &= ~PHPDBG_IS_QUITTING;
986-
PHPDBG_G(io)[PHPDBG_STDIN] = streams[0];
987-
PHPDBG_G(io)[PHPDBG_STDOUT] = streams[1];
988-
PHPDBG_G(io)[PHPDBG_STDERR] = stderr;
998+
989999
CG(unclean_shutdown) = 0;
9901000
}
9911001
}

0 commit comments

Comments
 (0)