I've moved most of the code out of the ext/standard directory and into the sapi/apache directory. In order to do that I had to make a few of the local functions in basic_functions.c non-local. See patch for details. The patch is a complete patch of all my changes and can be applied to a fresh checkout of PHP_4_3.
I still need someone to tell me how to #ifdef based on the presence of the --with-apxs configure flag. Also will I need to make modifications to the other SAPI modules because of the changes to SAPI.c and SAPI.h? There are a few more changes to be made so that the connection is guaranteed to be closed down cleanly. Joseph
? ext/mysql/.libs Index: ext/standard/basic_functions.c =================================================================== RCS file: /repository/php4/ext/standard/basic_functions.c,v retrieving revision 1.543.2.4 diff -u -b -r1.543.2.4 basic_functions.c --- ext/standard/basic_functions.c 20 Dec 2002 16:37:44 -0000 1.543.2.4 +++ ext/standard/basic_functions.c 27 Dec 2002 22:17:15 -0000 @@ -107,11 +107,6 @@ {3, BYREF_FORCE, BYREF_FORCE, BYREF_FORCE}; -typedef struct _php_shutdown_function_entry { - zval **arguments; - int arg_count; -} php_shutdown_function_entry; - typedef struct _user_tick_function_entry { zval **arguments; int arg_count; @@ -119,7 +114,6 @@ } user_tick_function_entry; /* some prototypes for local functions */ -static void user_shutdown_function_dtor(php_shutdown_function_entry *shutdown_function_entry); static void user_tick_function_dtor(user_tick_function_entry *tick_function_entry); /* Demo code. Enable only if you need this. */ @@ -1121,6 +1115,7 @@ } #endif BG(user_shutdown_function_names) = NULL; + BG(user_apache_shutdown_function_names) = NULL; #if HAVE_CRYPT PHP_RINIT(crypt) (INIT_FUNC_ARGS_PASSTHRU); @@ -1986,7 +1981,7 @@ /* }}} */ -void user_shutdown_function_dtor(php_shutdown_function_entry *shutdown_function_entry) +PHPAPI void user_shutdown_function_dtor(php_shutdown_function_entry +*shutdown_function_entry) { int i; @@ -2006,7 +2001,7 @@ efree(tick_function_entry->arguments); } -static int user_shutdown_function_call(php_shutdown_function_entry *shutdown_function_entry TSRMLS_DC) +PHPAPI int user_shutdown_function_call(php_shutdown_function_entry +*shutdown_function_entry TSRMLS_DC) { zval retval; @@ -2128,7 +2123,6 @@ zend_hash_next_index_insert(BG(user_shutdown_function_names), &shutdown_function_entry, sizeof(php_shutdown_function_entry), NULL); } /* }}} */ - ZEND_API void php_get_highlight_struct(zend_syntax_highlighter_ini *syntax_highlighter_ini) { Index: ext/standard/basic_functions.h =================================================================== RCS file: /repository/php4/ext/standard/basic_functions.h,v retrieving revision 1.109 diff -u -b -r1.109 basic_functions.h --- ext/standard/basic_functions.h 5 Nov 2002 06:05:48 -0000 1.109 +++ ext/standard/basic_functions.h 27 Dec 2002 22:17:15 -0000 @@ -74,6 +74,14 @@ PHP_FUNCTION(highlight_string); ZEND_API void php_get_highlight_struct(zend_syntax_highlighter_ini *syntax_highlighter_ini); +typedef struct _php_shutdown_function_entry { + zval **arguments; + int arg_count; +} php_shutdown_function_entry; + +PHPAPI void user_shutdown_function_dtor(php_shutdown_function_entry +*shutdown_function_entry); +PHPAPI int user_shutdown_function_call(php_shutdown_function_entry +*shutdown_function_entry TSRMLS_DC); + PHP_FUNCTION(ini_get); PHP_FUNCTION(ini_get_all); PHP_FUNCTION(ini_set); @@ -130,6 +138,7 @@ typedef struct { HashTable *user_shutdown_function_names; + HashTable *user_apache_shutdown_function_names; HashTable putenv_ht; zval *strtok_zval; char *strtok_string; Index: main/SAPI.c =================================================================== RCS file: /repository/php4/main/SAPI.c,v retrieving revision 1.155.2.2 diff -u -b -r1.155.2.2 SAPI.c --- main/SAPI.c 5 Dec 2002 22:15:00 -0000 1.155.2.2 +++ main/SAPI.c 27 Dec 2002 22:17:15 -0000 @@ -348,6 +348,14 @@ SG(sapi_headers).http_status_line = NULL; } } + +SAPI_API void sapi_close(TSRMLS_D) +{ + if(sapi_module.close) { + sapi_module.close(TSRMLS_C); + } + sapi_flush(TSRMLS_C); +} SAPI_API void sapi_deactivate(TSRMLS_D) { Index: main/SAPI.h =================================================================== RCS file: /repository/php4/main/SAPI.h,v retrieving revision 1.87 diff -u -b -r1.87 SAPI.h --- main/SAPI.h 12 Nov 2002 20:56:47 -0000 1.87 +++ main/SAPI.h 27 Dec 2002 22:17:15 -0000 @@ -130,6 +130,7 @@ SAPI_API void sapi_startup(sapi_module_struct *sf); SAPI_API void sapi_shutdown(void); +SAPI_API void sapi_close(TSRMLS_D); SAPI_API void sapi_activate(TSRMLS_D); SAPI_API void sapi_deactivate(TSRMLS_D); SAPI_API void sapi_initialize_empty_request(TSRMLS_D); @@ -190,6 +191,7 @@ int (*startup)(struct _sapi_module_struct *sapi_module); int (*shutdown)(struct _sapi_module_struct *sapi_module); + int (*close)(TSRMLS_D); int (*activate)(TSRMLS_D); int (*deactivate)(TSRMLS_D); Index: main/main.c =================================================================== RCS file: /repository/php4/main/main.c,v retrieving revision 1.512.2.5 diff -u -b -r1.512.2.5 main.c --- main/main.c 16 Dec 2002 15:44:06 -0000 1.512.2.5 +++ main/main.c 27 Dec 2002 22:17:15 -0000 @@ -923,6 +923,11 @@ if (PG(modules_activated)) zend_try { php_call_shutdown_functions(); } zend_end_try(); + + /*Close the connection before we cleanup*/ + zend_try { + sapi_close(TSRMLS_C); + } zend_end_try(); if (PG(modules_activated)) { zend_deactivate_modules(TSRMLS_C); Index: sapi/apache/mod_php4.c =================================================================== RCS file: /repository/php4/sapi/apache/mod_php4.c,v retrieving revision 1.146.2.1 diff -u -b -r1.146.2.1 mod_php4.c --- sapi/apache/mod_php4.c 21 Dec 2002 20:09:09 -0000 1.146.2.1 +++ sapi/apache/mod_php4.c 27 Dec 2002 22:17:15 -0000 @@ -300,6 +300,38 @@ } /* }}} */ +/* {{{ php_apache_sapi_close + */ +static int php_apache_sapi_close(TSRMLS_D) +{ + int rt; + request_rec *r = (request_rec *) SG(server_context); + +#if MODULE_MAGIC_NUMBER > 19970110 + rt = rflush(r); +#else + rt = bflush(r->connection->client); +#endif + if( rt = -1){ + ap_bclose(r->connection->client); + /* run the shutdown functions */ + php_call_apache_shutdown_functions(); + return SUCCESS; + } + ap_bsetflag(r->connection->client, B_EOUT, 1); + + if (r->connection->aborted) { + ap_bclose(r->connection->client); + /* run the shutdown functions */ + php_call_apache_shutdown_functions(); + return SUCCESS; + } + /* run the shutdown functions anyway */ + php_call_apache_shutdown_functions(); + return FAILURE; +} +/* }}} */ + /* {{{ php_apache_sapi_activate */ static int php_apache_sapi_activate(TSRMLS_D) @@ -351,6 +383,8 @@ php_apache_startup, /* startup */ php_module_shutdown_wrapper, /* shutdown */ + + php_apache_sapi_close, /* close */ php_apache_sapi_activate, /* activate */ NULL, /* deactivate */ Index: sapi/apache/mod_php4.h =================================================================== RCS file: /repository/php4/sapi/apache/mod_php4.h,v retrieving revision 1.17 diff -u -b -r1.17 mod_php4.h --- sapi/apache/mod_php4.h 28 Feb 2002 08:27:19 -0000 1.17 +++ sapi/apache/mod_php4.h 27 Dec 2002 22:17:15 -0000 @@ -35,6 +35,7 @@ } php_apache_info_struct; extern zend_module_entry apache_module_entry; +extern void php_call_apache_shutdown_functions(void); #ifdef ZTS extern int php_apache_info_id; Index: sapi/apache/php_apache.c =================================================================== RCS file: /repository/php4/sapi/apache/php_apache.c,v retrieving revision 1.69.2.1 diff -u -b -r1.69.2.1 php_apache.c --- sapi/apache/php_apache.c 5 Dec 2002 23:19:02 -0000 1.69.2.1 +++ sapi/apache/php_apache.c 27 Dec 2002 22:17:15 -0000 @@ -44,6 +44,7 @@ PHP_FUNCTION(apache_lookup_uri); PHP_FUNCTION(apache_child_terminate); PHP_FUNCTION(apache_setenv); +PHP_FUNCTION(apache_register_shutdown_function); PHP_MINFO_FUNCTION(apache); @@ -55,6 +56,7 @@ PHP_FE(apache_child_terminate, NULL) PHP_FE(apache_setenv, NULL) PHP_FE(apache_response_headers, NULL) + PHP_FE(apache_register_shutdown_function, NULL) PHP_FALIAS(getallheaders, apache_request_headers, NULL) {NULL, NULL, NULL} }; @@ -115,7 +117,7 @@ ap_child_terminate( ((request_rec *)SG(server_context)) ); RETURN_TRUE; } else { /* tell them to get lost! */ - php_error_docref(NULL TSRMLS_CC, E_WARNING, "apache.child_terminate is disabled"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "apache_child_terminate is +disabled"); RETURN_FALSE; } #else @@ -471,6 +473,59 @@ } destroy_sub_req(rr); +} +/* }}} */ + + +/* This function is called after the connection has closed so that the registered + * functions can be executed. + */ +void php_call_apache_shutdown_functions(void) +{ + TSRMLS_FETCH(); + + if (BG(user_apache_shutdown_function_names)) + zend_try { + zend_hash_apply(BG(user_apache_shutdown_function_names), +(apply_func_t) user_shutdown_function_call TSRMLS_CC); + memcpy(&EG(bailout), &orig_bailout, sizeof(jmp_buf)); + zend_hash_destroy(BG(user_apache_shutdown_function_names)); + efree(BG(user_apache_shutdown_function_names)); + } + zend_end_try(); +} + +/* {{{ proto void register_apache_shutdown_function(string function_name) + * Register a user-level function to be called on request termination: executed after +the + * connection has closed. This function is only available on Apache web servers +using the + * SAPI module as it uses Apache API calls to close the connection before continued + * execution. NOTE: Do not use "print()" or "echo()" calls within your registered + * functions as this will cause execution to fail. + */ +PHP_FUNCTION(apache_register_shutdown_function) +{ + php_shutdown_function_entry shutdown_function_entry; + int i; + + shutdown_function_entry.arg_count = ZEND_NUM_ARGS(); + + if (shutdown_function_entry.arg_count < 1) { + WRONG_PARAM_COUNT; + } + + shutdown_function_entry.arguments = (pval **) emalloc(sizeof(pval *) +*shutdown_function_entry.arg_count); + + if (zend_get_parameters_array(ht, shutdown_function_entry.arg_count, +shutdown_function_entry.arguments) == FAILURE) { + RETURN_FALSE; + } + if (!BG(user_apache_shutdown_function_names)) { + ALLOC_HASHTABLE(BG(user_apache_shutdown_function_names)); + zend_hash_init(BG(user_apache_shutdown_function_names), 0, NULL, (void +(*)(void *)) user_shutdown_function_dtor, 0); + } + + for (i = 0; i < shutdown_function_entry.arg_count; i++) { + shutdown_function_entry.arguments[i]->refcount++; + } + zend_hash_next_index_insert(BG(user_apache_shutdown_function_names), +&shutdown_function_entry, sizeof(php_shutdown_function_entry), NULL); } /* }}} */
<?php function shutdown() { sleep(3); print("Shutdown function.\n"); } function offline() { $i = 0; $out = fopen('/tmp/shutdown_test.out',"a+"); for($i = 0; $i < 6; $i++) { sleep(3); fputs($out, "sleeping\n"); } fclose($out); } apache_register_shutdown_function(offline); register_shutdown_function(shutdown); echo "This is the end.<br>\n"; ?>
-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php