From 12363ef4770a6127ad330d5ba47a6afd58557d6b Mon Sep 17 00:00:00 2001 From: Xuefer Date: Sun, 17 Dec 2006 03:20:43 +0000 Subject: [PATCH] 1.2->trunk: [334] robust error handling on shm init git-svn-id: svn://svn.lighttpd.net/xcache/trunk@337 c26eb9a1-5813-0410-bd6c-c2e55f420ca7 --- mmap.c | 4 ++-- xcache.c | 25 +++++++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/mmap.c b/mmap.c index c76560f..c0211c7 100644 --- a/mmap.c +++ b/mmap.c @@ -175,14 +175,14 @@ static XC_SHM_INIT(xc_mmap_init) /* {{{ */ /* do not create file in /dev */ if (strncmp(shm->name, "/dev", 4) == 0) { perror(shm->name); - errstr = "Cannot open file set by xcache.mmap_path"; + errstr = "Cannot open file set by xcache.mmap_path, check the xcache.size/var_size against system limitation"; goto err; } fd = open(shm->name, O_CREAT | O_RDWR, XCACHE_MMAP_PERMISSION); shm->newfile = 1; if (fd == -1) { perror(shm->name); - errstr = "Cannot open or create file set by xcache.mmap_path"; + errstr = "Cannot open or create file set by xcache.mmap_path, check the path permission or check xcache.size/var_size against system limitation"; goto err; } } diff --git a/xcache.c b/xcache.c index a578876..e958d67 100644 --- a/xcache.c +++ b/xcache.c @@ -1540,23 +1540,37 @@ static void xc_destroy() /* {{{ */ shm = xc_cache_destroy(xc_php_caches, &xc_php_hcache); xc_php_caches = NULL; } + xc_php_hcache.size = 0; + if (xc_var_caches) { shm = xc_cache_destroy(xc_var_caches, &xc_var_hcache); xc_var_caches = NULL; } + xc_var_hcache.size = 0; + fprintf(stderr, "set 0\n"); + if (shm) { xc_shm_destroy(shm); } + + xc_initized = 0; } /* }}} */ 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; + + if (shmsize < (size_t) xc_php_size || shmsize < (size_t) xc_var_size) { + zend_error(E_ERROR, "XCache: neither xcache.size nor xcache.var_size can be negative"); + goto err; + } if (xc_php_size || xc_var_size) { - CHECK(shm = xc_shm_init(xc_shm_scheme, ALIGN(xc_php_size) + ALIGN(xc_var_size), xc_readonly_protection, xc_mmap_path, NULL), "Cannot create shm"); + 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)) { xc_readonly_protection = 0; } @@ -1572,12 +1586,12 @@ static int xc_init(int module_number TSRMLS_DC) /* {{{ */ CHECK(xc_var_caches = xc_cache_init(shm, &xc_var_hcache, &xc_var_hentry, NULL, xc_var_size), "failed init variable cache"); } } - return 1; + return SUCCESS; err: + xc_destroy(); if (xc_php_caches || xc_var_caches) { - xc_destroy(); - /* shm destroied */ + /* shm destroied in xc_destroy() */ } else if (shm) { xc_shm_destroy(shm); @@ -2677,7 +2691,7 @@ static PHP_MINIT_FUNCTION(xcache) xc_shm_init_modules(); if ((xc_php_size || xc_var_size) && xc_mmap_path && xc_mmap_path[0]) { - if (!xc_init(module_number TSRMLS_CC)) { + if (xc_init(module_number TSRMLS_CC) != SUCCESS) { zend_error(E_ERROR, "XCache: Cannot init"); goto err_init; } @@ -2699,7 +2713,6 @@ static PHP_MSHUTDOWN_FUNCTION(xcache) { if (xc_initized) { xc_destroy(); - xc_initized = 0; } if (xc_mmap_path) { pefree(xc_mmap_path, 1);