]> granicus.if.org Git - php/commitdiff
made server persist, support disconnect and reconnect
authorkrakjoe <joe.watkins@live.co.uk>
Thu, 28 Nov 2013 09:31:06 +0000 (09:31 +0000)
committerkrakjoe <joe.watkins@live.co.uk>
Thu, 28 Nov 2013 09:31:06 +0000 (09:31 +0000)
phpdbg.c
phpdbg.h
phpdbg_cmd.c

index d536f9d801e7f285e46c3cf9af7719188d9ff9db..2e42d5993f2c00bf7cf30c54419038daa7a6db2f 100644 (file)
--- a/phpdbg.c
+++ b/phpdbg.c
@@ -589,22 +589,23 @@ int phpdbg_open_socket(short port) /* {{{ */
        return fd;
 } /* }}} */
 
-int phpdbg_open_sockets(int listen[2], FILE* streams[2]) /* {{{ */
+/* don't inline this, want to debug it easily, will inline when done */
+
+int phpdbg_open_sockets(int port[2], int (*listen)[2], int (*socket)[2], FILE* streams[2]) /* {{{ */
 {
-       int sockets[2] = {
-               phpdbg_open_socket((short)listen[0]),
-               phpdbg_open_socket((short)listen[1])
-       };
-       int accepted[2] = {-1, -1};
+       if (((*listen)[0]) < 0 && ((*listen)[1]) < 0) {
+               ((*listen)[0]) = phpdbg_open_socket((short)port[0]);
+               ((*listen)[1]) = phpdbg_open_socket((short)port[1]);
+       }
        
        streams[0] = NULL;
        streams[1] = NULL;
        
-       if (sockets[0] < 0 || sockets[1] < 0) {
-               if (sockets[0] >= 0)
-                       close(sockets[0]);
-               if (sockets[1] >= 0)
-                       close(sockets[1]);
+       if ((*listen)[0] < 0 || (*listen)[1] < 0) {
+               if ((*listen)[0] >= 0)
+                       close((*listen)[0]);
+               if ((*listen)[1] >= 0)
+                       close((*listen)[1]);
                return FAILURE;
        }
        
@@ -613,20 +614,40 @@ int phpdbg_open_sockets(int listen[2], FILE* streams[2]) /* {{{ */
         socklen_t size = sizeof(address);
 
         memset(&address, 0, size);
-        accepted[0] = accept(
-               sockets[0], (struct sockaddr *) &address, &size);
+        (*socket)[0] = accept(
+               (*listen)[0], (struct sockaddr *) &address, &size);
         
         memset(&address, 0, size);
-        accepted[1] = accept(
-               sockets[1], (struct sockaddr *) &address, &size);
+        (*socket)[1] = accept(
+               (*listen)[1], (struct sockaddr *) &address, &size);
        }
        
-       streams[0] = fdopen(accepted[0], "r");
-       streams[1] = fdopen(accepted[1], "w");
+       streams[0] = fdopen((*socket)[0], "r");
+       streams[1] = fdopen((*socket)[1], "w");
 
        return SUCCESS;
 } /* }}} */
 
+static inline void phpdbg_close_sockets(int (*socket)[2], FILE *streams[2]) /* {{{ */
+{
+       if ((*socket)[0]) {
+               close((*socket)[0]);
+       }
+       
+       if (streams[0]) {
+               fclose(streams[0]);
+       }
+       
+       if (streams[1]) {
+               fflush(streams[1]);
+               fclose(streams[1]);
+       }
+       
+       if ((*socket)[1]) {
+               close((*socket)[1]);
+       }
+} /* }}} */
+
 int main(int argc, char **argv) /* {{{ */
 {
        sapi_module_struct *phpdbg = &phpdbg_sapi_module;
@@ -649,11 +670,22 @@ int main(int argc, char **argv) /* {{{ */
        int step = 0;
        char *bp_tmp_file;
        int listen[2];
+       int server[2];
+       int socket[2];
        FILE* streams[2] = {NULL, NULL};
 
 #ifdef ZTS
        void ***tsrm_ls;
 #endif
+       
+       socket[0] = -1;
+       socket[1] = -1;
+       listen[0] = -1;
+       listen[1] = -1;
+       server[0] = -1;
+       server[1] = -1;
+       streams[0] = NULL;
+       streams[1] = NULL;
 
 #ifdef PHP_WIN32
        _fmode = _O_BINARY;                 /* sets default for file streams to binary */
@@ -693,8 +725,6 @@ phpdbg_main:
        opt = 0;
        run = 0;
        step = 0;
-       listen[0] = 0;
-       listen[1] = 0;
        
        while ((opt = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
                switch (opt) {
@@ -800,9 +830,10 @@ phpdbg_main:
                }
        }
 
+       /* setup remote server if necessary */
        if (!cleaning && 
-               (listen[0] && listen[1])) {
-               phpdbg_open_sockets(listen, streams);
+               (listen[0] > 0 && listen[1] > 0)) {
+               phpdbg_open_sockets(listen, &server, &socket, streams);
        }
        
        phpdbg->ini_defaults = phpdbg_ini_defaults;
@@ -852,6 +883,7 @@ phpdbg_main:
                        PHPDBG_G(io)[PHPDBG_STDIN] = streams[0];
                        PHPDBG_G(io)[PHPDBG_STDOUT] = streams[1];
                        PHPDBG_G(io)[PHPDBG_STDERR] = stderr;
+                       
                        signal(SIGPIPE, SIG_IGN);
                } else {
                        /* local console */
@@ -926,6 +958,7 @@ phpdbg_main:
                        }
                }
 
+phpdbg_interact:
                /* phpdbg main() */
                do {
                        zend_try {
@@ -941,13 +974,38 @@ phpdbg_main:
                                        cleaning = 0;
                                }
 
+                               /* remote client disconnected */
+                               if ((PHPDBG_G(flags) & PHPDBG_IS_DISCONNECTED)) {
+
+                                       /* close old streams/sockets */
+                                       phpdbg_close_sockets(&socket, streams);
+                                       
+                                       /* renegociate connections */
+                                       phpdbg_open_sockets(
+                                               listen, &server, &socket, streams);
+                                       
+                                       /* set streams */
+                                       if (streams[0] && streams[1]) {
+                                               PHPDBG_G(flags) &= ~PHPDBG_IS_QUITTING;
+                                               PHPDBG_G(io)[PHPDBG_STDIN] = streams[0];
+                                               PHPDBG_G(io)[PHPDBG_STDOUT] = streams[1];
+                                               PHPDBG_G(io)[PHPDBG_STDERR] =  stderr;
+                                               CG(unclean_shutdown) = 0;
+                                       }
+                               }
+
                                if (PHPDBG_G(flags) & PHPDBG_IS_QUITTING) {
                                        goto phpdbg_out;
                                }
                        } zend_end_try();
                } while(!(PHPDBG_G(flags) & PHPDBG_IS_QUITTING));
-
+               
 phpdbg_out:
+               if (PHPDBG_G(flags) & PHPDBG_IS_DISCONNECTED) {
+                       PHPDBG_G(flags) &= ~PHPDBG_IS_DISCONNECTED;
+                       goto phpdbg_interact;
+               }
+               
                if (ini_entries) {
                        free(ini_entries);
                }
index 2cb11b5de76297e9df82ac3079d0e43759cc8c06..dd3242cffd54352f1bd69106999854fcab46431e 100644 (file)
--- a/phpdbg.h
+++ b/phpdbg.h
 #define PHPDBG_IS_INTERACTIVE   (1<<21)
 #define PHPDBG_IS_BP_ENABLED    (1<<22)
 #define PHPDBG_IS_REMOTE               (1<<23)
+#define PHPDBG_IS_DISCONNECTED (1<<24)
 
 #ifndef _WIN32
 #      define PHPDBG_DEFAULT_FLAGS (PHPDBG_IS_QUIET|PHPDBG_IS_COLOURED|PHPDBG_IS_BP_ENABLED)
index c4ba06e5db40e3c32290060ee4aea1d551132800..916c9d7f62eae0e4be8174cbad4aad6bfc858873 100644 (file)
@@ -230,7 +230,7 @@ PHPDBG_API phpdbg_input_t *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
                                !fgets(buf, PHPDBG_MAX_CMD, PHPDBG_G(io)[PHPDBG_STDIN])) {
                                /* the user has gone away */
                                phpdbg_error("Failed to read console !");
-                               PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
+                               PHPDBG_G(flags) |= (PHPDBG_IS_QUITTING|PHPDBG_IS_DISCONNECTED);
                                zend_bailout();
                                return NULL;
                        }
@@ -247,7 +247,7 @@ PHPDBG_API phpdbg_input_t *phpdbg_read_input(char *buffered TSRMLS_DC) /* {{{ */
                        if (!cmd) {
                                /* the user has gone away */
                                phpdbg_error("Failed to read console !");
-                               PHPDBG_G(flags) |= PHPDBG_IS_QUITTING;
+                               PHPDBG_G(flags) |= (PHPDBG_IS_QUITTING|PHPDBG_IS_DISCONNECTED);
                                zend_bailout();
                                return NULL;
                        }