Index: /trunk/ChangeLog
===================================================================
--- /trunk/ChangeLog	(revision 978)
+++ /trunk/ChangeLog	(revision 979)
@@ -3,6 +3,12 @@
 ========
  * chg: proto array xcache_clear_cache(int type, [ int id = -1 ]). -1 means all cache splits
+
+Ini Settings Changes
+========
+ * new: xcache.disable_on_crash = Off
+
 ChangeLog
 ========
+ * closes #2: auto disable caching on crash
  * closes #972: warning/error when XCache is loaded incorrectly
  * closes #73: warn for improper PHP_FCGI_CHILDREN setting fcgi mode (>=PHP_5_3)
Index: /trunk/NEWS
===================================================================
--- /trunk/NEWS	(revision 978)
+++ /trunk/NEWS	(revision 979)
@@ -3,4 +3,5 @@
  * improvements
  * adds warning for misconfiguration
+ * auto disable caching on crash
 
 2.0.1 2012-07-14
Index: /trunk/mmap.c
===================================================================
--- /trunk/mmap.c	(revision 978)
+++ /trunk/mmap.c	(revision 979)
@@ -41,4 +41,5 @@
 struct _xc_mmap_shm_t {
 	xc_shm_handlers_t *handlers;
+	zend_bool disabled;
 	void *ptr;
 	void *ptr_ro;
Index: /trunk/xc_shm.h
===================================================================
--- /trunk/xc_shm.h	(revision 978)
+++ /trunk/xc_shm.h	(revision 979)
@@ -6,4 +6,5 @@
 struct _xc_shm_t {
 	const xc_shm_handlers_t *handlers;
+	zend_bool disabled;
 };
 #define XC_SHM_IMPL _xc_shm_t
Index: /trunk/xcache.c
===================================================================
--- /trunk/xcache.c	(revision 978)
+++ /trunk/xcache.c	(revision 979)
@@ -70,4 +70,5 @@
 static char *xc_mmap_path = NULL;
 static char *xc_coredump_dir = NULL;
+static zend_bool xc_disable_on_crash = 0;
 
 static xc_hash_t xc_php_hcache = { 0 };
@@ -87,4 +88,5 @@
 static zend_ulong xc_var_size  = 0;
 
+static xc_shm_t *xc_shm = NULL;
 static xc_cache_t **xc_php_caches = NULL;
 static xc_cache_t **xc_var_caches = NULL;
@@ -2155,4 +2157,5 @@
 	 || strstr(PG(include_path), "://") != NULL
 #endif
+	 || xc_shm || xc_shm->disabled
 	 ) {
 		TRACE("%s", "cacher not enabled");
@@ -2304,14 +2307,13 @@
 }
 /* }}} */
-static xc_shm_t *xc_cache_destroy(xc_cache_t **caches, xc_hash_t *hcache) /* {{{ */
+static void xc_cache_destroy(xc_cache_t **caches, xc_hash_t *hcache) /* {{{ */
 {
 	size_t i;
 	xc_cache_t *cache;
-	xc_shm_t *shm;
 
 	if (!caches) {
-		return NULL;
-	}
-	shm = NULL;
+		return;
+	}
+
 	for (i = 0; i < hcache->size; i ++) {
 		cache = caches[i];
@@ -2326,10 +2328,8 @@
 			cache->mem->handlers->free(cache->mem, cache);
 			*/
-			shm = cache->shm;
-			shm->handlers->memdestroy(cache->mem);
+			cache->shm->handlers->memdestroy(cache->mem);
 		}
 	}
 	free(caches);
-	return shm;
 }
 /* }}} */
@@ -2387,6 +2387,4 @@
 static void xc_destroy() /* {{{ */
 {
-	xc_shm_t *shm = NULL;
-
 	if (old_compile_file) {
 		zend_compile_file = old_compile_file;
@@ -2400,15 +2398,16 @@
 
 	if (xc_php_caches) {
-		shm = xc_cache_destroy(xc_php_caches, &xc_php_hcache);
+		xc_cache_destroy(xc_php_caches, &xc_php_hcache);
 		xc_php_caches = NULL;
 	}
 
 	if (xc_var_caches) {
-		shm = xc_cache_destroy(xc_var_caches, &xc_var_hcache);
+		xc_cache_destroy(xc_var_caches, &xc_var_hcache);
 		xc_var_caches = NULL;
 	}
 
-	if (shm) {
-		xc_shm_destroy(shm);
+	if (xc_shm) {
+		xc_shm_destroy(xc_shm);
+		xc_shm = NULL;
 	}
 
@@ -2418,9 +2417,8 @@
 static int xc_init(int module_number TSRMLS_DC) /* {{{ */
 {
-	xc_shm_t *shm;
 	xc_shmsize_t shmsize = ALIGN(xc_php_size) + ALIGN(xc_var_size);
 
 	xc_php_caches = xc_var_caches = NULL;
-	shm = NULL;
+	xc_shm = NULL;
 
 	if (shmsize < (size_t) xc_php_size || shmsize < (size_t) xc_var_size) {
@@ -2430,6 +2428,6 @@
 
 	if (xc_php_size || xc_var_size) {
-		CHECK(shm = xc_shm_init(xc_shm_scheme, shmsize, xc_readonly_protection, xc_mmap_path, NULL), "Cannot create shm");
-		if (!shm->handlers->can_readonly(shm)) {
+		CHECK(xc_shm = xc_shm_init(xc_shm_scheme, shmsize, xc_readonly_protection, xc_mmap_path, NULL), "Cannot create shm");
+		if (!xc_shm->handlers->can_readonly(xc_shm)) {
 			xc_readonly_protection = 0;
 		}
@@ -2439,9 +2437,9 @@
 			zend_compile_file = xc_compile_file;
 
-			CHECK(xc_php_caches = xc_cache_init(shm, &xc_php_hcache, &xc_php_hentry, &xc_php_hentry, xc_php_size), "failed init opcode cache");
+			CHECK(xc_php_caches = xc_cache_init(xc_shm, &xc_php_hcache, &xc_php_hentry, &xc_php_hentry, xc_php_size), "failed init opcode cache");
 		}
 
 		if (xc_var_size) {
-			CHECK(xc_var_caches = xc_cache_init(shm, &xc_var_hcache, &xc_var_hentry, NULL, xc_var_size), "failed init variable cache");
+			CHECK(xc_var_caches = xc_cache_init(xc_shm, &xc_var_hcache, &xc_var_hentry, NULL, xc_var_size), "failed init variable cache");
 		}
 	}
@@ -2453,7 +2451,8 @@
 		/* shm destroied in xc_destroy() */
 	}
-	else if (shm) {
+	else if (xc_shm) {
 		xc_destroy();
-		xc_shm_destroy(shm);
+		xc_shm_destroy(xc_shm);
+		xc_shm = NULL;
 	}
 	return 0;
@@ -2519,11 +2518,13 @@
 static void xc_request_shutdown(TSRMLS_D) /* {{{ */
 {
-	xc_entry_unholds(TSRMLS_C);
+	if (xc_shm && !xc_shm->disabled) {
+		xc_entry_unholds(TSRMLS_C);
+		xc_gc_expires_php(TSRMLS_C);
+		xc_gc_expires_var(TSRMLS_C);
+		xc_gc_deletes(TSRMLS_C);
+	}
 #ifdef ZEND_ENGINE_2
 	zend_llist_destroy(&XG(gc_op_arrays));
 #endif
-	xc_gc_expires_php(TSRMLS_C);
-	xc_gc_expires_var(TSRMLS_C);
-	xc_gc_deletes(TSRMLS_C);
 #ifdef HAVE_XCACHE_COVERAGER
 	xc_coverager_request_shutdown(TSRMLS_C);
@@ -2709,5 +2710,5 @@
 	xcache_admin_auth_check(TSRMLS_C);
 
-	if (!xc_initized) {
+	if (!xc_initized || !xc_shm || xc_shm->disabled) {
 		RETURN_NULL();
 	}
@@ -2867,5 +2868,5 @@
 	zval *name;
 
-	if (!xc_var_caches) {
+	if (!xc_var_caches || !xc_shm || xc_shm->disabled) {
 		VAR_DISABLED_WARNING();
 		RETURN_NULL();
@@ -2901,5 +2902,5 @@
 	zval *value;
 
-	if (!xc_var_caches) {
+	if (!xc_var_caches || !xc_shm || xc_shm->disabled) {
 		VAR_DISABLED_WARNING();
 		RETURN_NULL();
@@ -2943,5 +2944,5 @@
 	zval *name;
 
-	if (!xc_var_caches) {
+	if (!xc_var_caches || !xc_shm || xc_shm->disabled) {
 		VAR_DISABLED_WARNING();
 		RETURN_FALSE;
@@ -2977,5 +2978,5 @@
 	zval *name;
 
-	if (!xc_var_caches) {
+	if (!xc_var_caches || !xc_shm || xc_shm->disabled) {
 		VAR_DISABLED_WARNING();
 		RETURN_FALSE;
@@ -3007,5 +3008,5 @@
 	int i, iend;
 
-	if (!xc_var_caches) {
+	if (!xc_var_caches || !xc_shm || xc_shm->disabled) {
 		VAR_DISABLED_WARNING();
 		RETURN_FALSE;
@@ -3043,5 +3044,5 @@
 	zval oldzval;
 
-	if (!xc_var_caches) {
+	if (!xc_var_caches || !xc_shm || xc_shm->disabled) {
 		VAR_DISABLED_WARNING();
 		RETURN_NULL();
@@ -3554,4 +3555,10 @@
 		}
 	}
+	if (xc_disable_on_crash) {
+		xc_disable_on_crash = 0;
+		if (xc_shm) {
+			xc_shm->disabled = 1;
+		}
+	}
 	raise(sig);
 }
@@ -3610,4 +3617,5 @@
 	PHP_INI_ENTRY1     ("xcache.mmap_path",     DEFAULT_PATH, PHP_INI_SYSTEM, xc_OnUpdateString,   &xc_mmap_path)
 	PHP_INI_ENTRY1     ("xcache.coredump_directory",      "", PHP_INI_SYSTEM, xc_OnUpdateString,   &xc_coredump_dir)
+	PHP_INI_ENTRY1     ("xcache.disable_on_crash",       "0", PHP_INI_SYSTEM, xc_OnUpdateBool,     &xc_disable_on_crash)
 	PHP_INI_ENTRY1     ("xcache.test",                   "0", PHP_INI_SYSTEM, xc_OnUpdateBool,     &xc_test)
 	PHP_INI_ENTRY1     ("xcache.readonly_protection",    "0", PHP_INI_SYSTEM, xc_OnUpdateBool,     &xc_readonly_protection)
Index: /trunk/xcache.ini
===================================================================
--- /trunk/xcache.ini	(revision 978)
+++ /trunk/xcache.ini	(revision 979)
@@ -58,4 +58,6 @@
 ; make sure it's writable by php (open_basedir is not checked)
 xcache.coredump_directory =   ""
+; disable cache after crash until restart. (non-Win32 only)
+xcache.disable_on_crash =    Off
 
 ; enable experimental documented features for each release if available
