Skip to content

Commit 6bb080a

Browse files
committed
made server persist, support disconnect and reconnect
1 parent 50da1ff commit 6bb080a

File tree

3 files changed

+83
-24
lines changed

3 files changed

+83
-24
lines changed

phpdbg.c

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -589,22 +589,23 @@ int phpdbg_open_socket(short port) /* {{{ */
589589
return fd;
590590
} /* }}} */
591591

592-
int phpdbg_open_sockets(int listen[2], FILE* streams[2]) /* {{{ */
592+
/* don't inline this, want to debug it easily, will inline when done */
593+
594+
int phpdbg_open_sockets(int port[2], int (*listen)[2], int (*socket)[2], FILE* streams[2]) /* {{{ */
593595
{
594-
int sockets[2] = {
595-
phpdbg_open_socket((short)listen[0]),
596-
phpdbg_open_socket((short)listen[1])
597-
};
598-
int accepted[2] = {-1, -1};
596+
if (((*listen)[0]) < 0 && ((*listen)[1]) < 0) {
597+
((*listen)[0]) = phpdbg_open_socket((short)port[0]);
598+
((*listen)[1]) = phpdbg_open_socket((short)port[1]);
599+
}
599600

600601
streams[0] = NULL;
601602
streams[1] = NULL;
602603

603-
if (sockets[0] < 0 || sockets[1] < 0) {
604-
if (sockets[0] >= 0)
605-
close(sockets[0]);
606-
if (sockets[1] >= 0)
607-
close(sockets[1]);
604+
if ((*listen)[0] < 0 || (*listen)[1] < 0) {
605+
if ((*listen)[0] >= 0)
606+
close((*listen)[0]);
607+
if ((*listen)[1] >= 0)
608+
close((*listen)[1]);
608609
return FAILURE;
609610
}
610611

@@ -613,20 +614,40 @@ int phpdbg_open_sockets(int listen[2], FILE* streams[2]) /* {{{ */
613614
socklen_t size = sizeof(address);
614615

615616
memset(&address, 0, size);
616-
accepted[0] = accept(
617-
sockets[0], (struct sockaddr *) &address, &size);
617+
(*socket)[0] = accept(
618+
(*listen)[0], (struct sockaddr *) &address, &size);
618619

619620
memset(&address, 0, size);
620-
accepted[1] = accept(
621-
sockets[1], (struct sockaddr *) &address, &size);
621+
(*socket)[1] = accept(
622+
(*listen)[1], (struct sockaddr *) &address, &size);
622623
}
623624

624-
streams[0] = fdopen(accepted[0], "r");
625-
streams[1] = fdopen(accepted[1], "w");
625+
streams[0] = fdopen((*socket)[0], "r");
626+
streams[1] = fdopen((*socket)[1], "w");
626627

627628
return SUCCESS;
628629
} /* }}} */
629630

631+
static inline void phpdbg_close_sockets(int (*socket)[2], FILE *streams[2]) /* {{{ */
632+
{
633+
if ((*socket)[0]) {
634+
close((*socket)[0]);
635+
}
636+
637+
if (streams[0]) {
638+
fclose(streams[0]);
639+
}
640+
641+
if (streams[1]) {
642+
fflush(streams[1]);
643+
fclose(streams[1]);
644+
}
645+
646+
if ((*socket)[1]) {
647+
close((*socket)[1]);
648+
}
649+
} /* }}} */
650+
630651
int main(int argc, char **argv) /* {{{ */
631652
{
632653
sapi_module_struct *phpdbg = &phpdbg_sapi_module;
@@ -649,11 +670,22 @@ int main(int argc, char **argv) /* {{{ */
649670
int step = 0;
650671
char *bp_tmp_file;
651672
int listen[2];
673+
int server[2];
674+
int socket[2];
652675
FILE* streams[2] = {NULL, NULL};
653676

654677
#ifdef ZTS
655678
void ***tsrm_ls;
656679
#endif
680+
681+
socket[0] = -1;
682+
socket[1] = -1;
683+
listen[0] = -1;
684+
listen[1] = -1;
685+
server[0] = -1;
686+
server[1] = -1;
687+
streams[0] = NULL;
688+
streams[1] = NULL;
657689

658690
#ifdef PHP_WIN32
659691
_fmode = _O_BINARY; /* sets default for file streams to binary */
@@ -693,8 +725,6 @@ int main(int argc, char **argv) /* {{{ */
693725
opt = 0;
694726
run = 0;
695727
step = 0;
696-
listen[0] = 0;
697-
listen[1] = 0;
698728

699729
while ((opt = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
700730
switch (opt) {
@@ -800,9 +830,10 @@ int main(int argc, char **argv) /* {{{ */
800830
}
801831
}
802832

833+
/* setup remote server if necessary */
803834
if (!cleaning &&
804-
(listen[0] && listen[1])) {
805-
phpdbg_open_sockets(listen, streams);
835+
(listen[0] > 0 && listen[1] > 0)) {
836+
phpdbg_open_sockets(listen, &server, &socket, streams);
806837
}
807838

808839
phpdbg->ini_defaults = phpdbg_ini_defaults;
@@ -852,6 +883,7 @@ int main(int argc, char **argv) /* {{{ */
852883
PHPDBG_G(io)[PHPDBG_STDIN] = streams[0];
853884
PHPDBG_G(io)[PHPDBG_STDOUT] = streams[1];
854885
PHPDBG_G(io)[PHPDBG_STDERR] = stderr;
886+
855887
signal(SIGPIPE, SIG_IGN);
856888
} else {
857889
/* local console */
@@ -926,6 +958,7 @@ int main(int argc, char **argv) /* {{{ */
926958
}
927959
}
928960

961+
phpdbg_interact:
929962
/* phpdbg main() */
930963
do {
931964
zend_try {
@@ -941,13 +974,38 @@ int main(int argc, char **argv) /* {{{ */
941974
cleaning = 0;
942975
}
943976

977+
/* remote client disconnected */
978+
if ((PHPDBG_G(flags) & PHPDBG_IS_DISCONNECTED)) {
979+
980+
/* close old streams/sockets */
981+
phpdbg_close_sockets(&socket, streams);
982+
983+
/* renegociate connections */
984+
phpdbg_open_sockets(
985+
listen, &server, &socket, streams);
986+
987+
/* set streams */
988+
if (streams[0] && streams[1]) {
989+
PHPDBG_G(flags) &= ~PHPDBG_IS_QUITTING;
990+
PHPDBG_G(io)[PHPDBG_STDIN] = streams[0];
991+
PHPDBG_G(io)[PHPDBG_STDOUT] = streams[1];
992+
PHPDBG_G(io)[PHPDBG_STDERR] = stderr;
993+
CG(unclean_shutdown) = 0;
994+
}
995+
}
996+
944997
if (PHPDBG_G(flags) & PHPDBG_IS_QUITTING) {
945998
goto phpdbg_out;
946999
}
9471000
} zend_end_try();
9481001
} while(!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING));
949-
1002+
9501003
phpdbg_out:
1004+
if (PHPDBG_G(flags) & PHPDBG_IS_DISCONNECTED) {
1005+
PHPDBG_G(flags) &= ~PHPDBG_IS_DISCONNECTED;
1006+
goto phpdbg_interact;
1007+
}
1008+
9511009
if (ini_entries) {
9521010
free(ini_entries);
9531011
}

phpdbg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
#define PHPDBG_IS_INTERACTIVE (1<<21)
128128
#define PHPDBG_IS_BP_ENABLED (1<<22)
129129
#define PHPDBG_IS_REMOTE (1<<23)
130+
#define PHPDBG_IS_DISCONNECTED (1<<24)
130131

131132
#ifndef _WIN32
132133
# define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET|PHPDBG_IS_COLOURED|PHPDBG_IS_BP_ENABLED)

phpdbg_cmd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ PHPDBG_API phpdbg_input_t *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
230230
!fgets(buf, PHPDBG_MAX_CMD, PHPDBG_G(io)[PHPDBG_STDIN])) {
231231
/* the user has gone away */
232232
phpdbg_error("Failed to read console !");
233-
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
233+
PHPDBG_G(flags) |= (PHPDBG_IS_QUITTING|PHPDBG_IS_DISCONNECTED);
234234
zend_bailout();
235235
return NULL;
236236
}
@@ -247,7 +247,7 @@ PHPDBG_API phpdbg_input_t *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
247247
if (!cmd) {
248248
/* the user has gone away */
249249
phpdbg_error("Failed to read console !");
250-
PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
250+
PHPDBG_G(flags) |= (PHPDBG_IS_QUITTING|PHPDBG_IS_DISCONNECTED);
251251
zend_bailout();
252252
return NULL;
253253
}

0 commit comments

Comments
 (0)