| 1 | |
|---|
| 2 | #if 0 |
|---|
| 3 | #define DEBUG |
|---|
| 4 | #endif |
|---|
| 5 | |
|---|
| 6 | /* {{{ macros */ |
|---|
| 7 | #include <stdlib.h> |
|---|
| 8 | #include <stdio.h> |
|---|
| 9 | #include <string.h> |
|---|
| 10 | |
|---|
| 11 | #include <signal.h> |
|---|
| 12 | |
|---|
| 13 | #include "php.h" |
|---|
| 14 | #include "ext/standard/info.h" |
|---|
| 15 | #include "ext/standard/md5.h" |
|---|
| 16 | #include "ext/standard/php_math.h" |
|---|
| 17 | #include "zend_extensions.h" |
|---|
| 18 | #include "SAPI.h" |
|---|
| 19 | |
|---|
| 20 | #include "xcache.h" |
|---|
| 21 | #include "optimizer.h" |
|---|
| 22 | #include "coverager.h" |
|---|
| 23 | #include "disassembler.h" |
|---|
| 24 | #include "align.h" |
|---|
| 25 | #include "stack.h" |
|---|
| 26 | #include "xcache_globals.h" |
|---|
| 27 | #include "processor.h" |
|---|
| 28 | #include "utils.h" |
|---|
| 29 | #include "const_string.h" |
|---|
| 30 | #include "opcode_spec.h" |
|---|
| 31 | |
|---|
| 32 | #ifdef DEBUG |
|---|
| 33 | # undef NDEBUG |
|---|
| 34 | # undef inline |
|---|
| 35 | # define inline |
|---|
| 36 | #else |
|---|
| 37 | # ifndef NDEBUG |
|---|
| 38 | # define NDEBUG |
|---|
| 39 | # endif |
|---|
| 40 | #endif |
|---|
| 41 | #include <assert.h> |
|---|
| 42 | |
|---|
| 43 | #define VAR_ENTRY_EXPIRED(pentry) ((pentry)->ttl && XG(request_time) > pentry->ctime + (pentry)->ttl) |
|---|
| 44 | #define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0) |
|---|
| 45 | #define LOCK(x) xc_lock(x->lck) |
|---|
| 46 | #define UNLOCK(x) xc_unlock(x->lck) |
|---|
| 47 | |
|---|
| 48 | #define ENTER_LOCK_EX(x) \ |
|---|
| 49 | xc_lock(x->lck); \ |
|---|
| 50 | zend_try { \ |
|---|
| 51 | do |
|---|
| 52 | #define LEAVE_LOCK_EX(x) \ |
|---|
| 53 | while (0); \ |
|---|
| 54 | } zend_catch { \ |
|---|
| 55 | catched = 1; \ |
|---|
| 56 | } zend_end_try(); \ |
|---|
| 57 | xc_unlock(x->lck) |
|---|
| 58 | |
|---|
| 59 | #define ENTER_LOCK(x) do { \ |
|---|
| 60 | int catched = 0; \ |
|---|
| 61 | ENTER_LOCK_EX(x) |
|---|
| 62 | #define LEAVE_LOCK(x) \ |
|---|
| 63 | LEAVE_LOCK_EX(x); \ |
|---|
| 64 | if (catched) { \ |
|---|
| 65 | zend_bailout(); \ |
|---|
| 66 | } \ |
|---|
| 67 | } while(0) |
|---|
| 68 | |
|---|
| 69 | /* }}} */ |
|---|
| 70 | |
|---|
| 71 | /* {{{ globals */ |
|---|
| 72 | static char *xc_shm_scheme = NULL; |
|---|
| 73 | static char *xc_mmap_path = NULL; |
|---|
| 74 | static char *xc_coredump_dir = NULL; |
|---|
| 75 | |
|---|
| 76 | static xc_hash_t xc_php_hcache = {0}; |
|---|
| 77 | static xc_hash_t xc_php_hentry = {0}; |
|---|
| 78 | static xc_hash_t xc_var_hcache = {0}; |
|---|
| 79 | static xc_hash_t xc_var_hentry = {0}; |
|---|
| 80 | |
|---|
| 81 | static zend_ulong xc_php_ttl = 0; |
|---|
| 82 | static zend_ulong xc_var_maxttl = 0; |
|---|
| 83 | |
|---|
| 84 | enum { xc_deletes_gc_interval = 120 }; |
|---|
| 85 | static zend_ulong xc_php_gc_interval = 0; |
|---|
| 86 | static zend_ulong xc_var_gc_interval = 0; |
|---|
| 87 | |
|---|
| 88 | /* total size */ |
|---|
| 89 | static zend_ulong xc_php_size = 0; |
|---|
| 90 | static zend_ulong xc_var_size = 0; |
|---|
| 91 | |
|---|
| 92 | static xc_cache_t **xc_php_caches = NULL; |
|---|
| 93 | static xc_cache_t **xc_var_caches = NULL; |
|---|
| 94 | |
|---|
| 95 | static zend_bool xc_initized = 0; |
|---|
| 96 | static zend_compile_file_t *origin_compile_file; |
|---|
| 97 | |
|---|
| 98 | static zend_bool xc_test = 0; |
|---|
| 99 | static zend_bool xc_readonly_protection = 0; |
|---|
| 100 | |
|---|
| 101 | zend_bool xc_have_op_array_ctor = 0; |
|---|
| 102 | |
|---|
| 103 | static zend_bool xc_module_gotup = 0; |
|---|
| 104 | static zend_bool xc_zend_extension_gotup = 0; |
|---|
| 105 | #if !COMPILE_DL_XCACHE |
|---|
| 106 | # define zend_extension_entry xcache_zend_extension_entry |
|---|
| 107 | #endif |
|---|
| 108 | ZEND_DLEXPORT zend_extension zend_extension_entry; |
|---|
| 109 | ZEND_DECLARE_MODULE_GLOBALS(xcache); |
|---|
| 110 | /* }}} */ |
|---|
| 111 | |
|---|
| 112 | /* any function in *_dmz is only safe be called within locked(single thread) area */ |
|---|
| 113 | |
|---|
| 114 | static inline int xc_entry_equal_dmz(xc_entry_t *a, xc_entry_t *b) /* {{{ */ |
|---|
| 115 | { |
|---|
| 116 | /* this function isn't required but can be in dmz */ |
|---|
| 117 | |
|---|
| 118 | if (a->type != b->type) { |
|---|
| 119 | return 0; |
|---|
| 120 | } |
|---|
| 121 | switch (a->type) { |
|---|
| 122 | case XC_TYPE_PHP: |
|---|
| 123 | #ifdef HAVE_INODE |
|---|
| 124 | do { |
|---|
| 125 | xc_entry_data_php_t *ap = a->data.php; |
|---|
| 126 | xc_entry_data_php_t *bp = b->data.php; |
|---|
| 127 | if (ap->inode) { |
|---|
| 128 | return ap->inode == bp->inode |
|---|
| 129 | && ap->device == bp->device; |
|---|
| 130 | } |
|---|
| 131 | } while(0); |
|---|
| 132 | #endif |
|---|
| 133 | /* fall */ |
|---|
| 134 | |
|---|
| 135 | case XC_TYPE_VAR: |
|---|
| 136 | do { |
|---|
| 137 | #ifdef IS_UNICODE |
|---|
| 138 | if (a->name_type == IS_UNICODE) { |
|---|
| 139 | if (a->name.ustr.len != b->name.ustr.len) { |
|---|
| 140 | return 0; |
|---|
| 141 | } |
|---|
| 142 | return memcmp(a->name.ustr.val, b->name.ustr.val, (a->name.ustr.len + 1) * sizeof(UChar)) == 0; |
|---|
| 143 | } |
|---|
| 144 | else { |
|---|
| 145 | return memcmp(a->name.str.val, b->name.str.val, a->name.str.len + 1) == 0; |
|---|
| 146 | } |
|---|
| 147 | #else |
|---|
| 148 | return memcmp(a->name.str.val, b->name.str.val, a->name.str.len + 1) == 0; |
|---|
| 149 | #endif |
|---|
| 150 | |
|---|
| 151 | } while(0); |
|---|
| 152 | default: |
|---|
| 153 | assert(0); |
|---|
| 154 | } |
|---|
| 155 | return 0; |
|---|
| 156 | } |
|---|
| 157 | /* }}} */ |
|---|
| 158 | static void xc_entry_free_real_dmz(volatile xc_entry_t *xce) /* {{{ */ |
|---|
| 159 | { |
|---|
| 160 | xce->cache->mem->handlers->free(xce->cache->mem, (xc_entry_t *)xce); |
|---|
| 161 | } |
|---|
| 162 | /* }}} */ |
|---|
| 163 | static void xc_entry_add_dmz(xc_entry_t *xce) /* {{{ */ |
|---|
| 164 | { |
|---|
| 165 | xc_entry_t **head = &(xce->cache->entries[xce->hvalue]); |
|---|
| 166 | xce->next = *head; |
|---|
| 167 | *head = xce; |
|---|
| 168 | xce->cache->entries_count ++; |
|---|
| 169 | } |
|---|
| 170 | /* }}} */ |
|---|
| 171 | static xc_entry_t *xc_entry_store_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 172 | { |
|---|
| 173 | xc_entry_t *stored_xce; |
|---|
| 174 | |
|---|
| 175 | xce->hits = 0; |
|---|
| 176 | xce->ctime = XG(request_time); |
|---|
| 177 | xce->atime = XG(request_time); |
|---|
| 178 | stored_xce = xc_processor_store_xc_entry_t(xce TSRMLS_CC); |
|---|
| 179 | if (stored_xce) { |
|---|
| 180 | xc_entry_add_dmz(stored_xce); |
|---|
| 181 | return stored_xce; |
|---|
| 182 | } |
|---|
| 183 | else { |
|---|
| 184 | xce->cache->ooms ++; |
|---|
| 185 | return NULL; |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | /* }}} */ |
|---|
| 189 | static void xc_entry_free_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 190 | { |
|---|
| 191 | xce->cache->entries_count --; |
|---|
| 192 | if (xce->refcount == 0) { |
|---|
| 193 | xc_entry_free_real_dmz(xce); |
|---|
| 194 | } |
|---|
| 195 | else { |
|---|
| 196 | xce->next = xce->cache->deletes; |
|---|
| 197 | xce->cache->deletes = xce; |
|---|
| 198 | xce->dtime = XG(request_time); |
|---|
| 199 | xce->cache->deletes_count ++; |
|---|
| 200 | } |
|---|
| 201 | return; |
|---|
| 202 | } |
|---|
| 203 | /* }}} */ |
|---|
| 204 | static void xc_entry_remove_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 205 | { |
|---|
| 206 | xc_entry_t **pp = &(xce->cache->entries[xce->hvalue]); |
|---|
| 207 | xc_entry_t *p; |
|---|
| 208 | for (p = *pp; p; pp = &(p->next), p = p->next) { |
|---|
| 209 | if (xc_entry_equal_dmz(xce, p)) { |
|---|
| 210 | /* unlink */ |
|---|
| 211 | *pp = p->next; |
|---|
| 212 | xc_entry_free_dmz(xce TSRMLS_CC); |
|---|
| 213 | return; |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | assert(0); |
|---|
| 217 | } |
|---|
| 218 | /* }}} */ |
|---|
| 219 | static xc_entry_t *xc_entry_find_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 220 | { |
|---|
| 221 | xc_entry_t *p; |
|---|
| 222 | for (p = xce->cache->entries[xce->hvalue]; p; p = p->next) { |
|---|
| 223 | if (xc_entry_equal_dmz(xce, p)) { |
|---|
| 224 | if (p->type == XC_TYPE_VAR || /* PHP */ p->data.php->mtime == xce->data.php->mtime) { |
|---|
| 225 | p->hits ++; |
|---|
| 226 | p->atime = XG(request_time); |
|---|
| 227 | return p; |
|---|
| 228 | } |
|---|
| 229 | else { |
|---|
| 230 | xc_entry_remove_dmz(p TSRMLS_CC); |
|---|
| 231 | return NULL; |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | return NULL; |
|---|
| 236 | } |
|---|
| 237 | /* }}} */ |
|---|
| 238 | static void xc_entry_hold_php_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 239 | { |
|---|
| 240 | #ifdef DEBUG |
|---|
| 241 | fprintf(stderr, "hold %s\n", ZSTR_S(xce->name)); |
|---|
| 242 | #endif |
|---|
| 243 | xce->refcount ++; |
|---|
| 244 | xc_stack_push(&XG(php_holds)[xce->cache->cacheid], (void *)xce); |
|---|
| 245 | } |
|---|
| 246 | /* }}} */ |
|---|
| 247 | #if 0 |
|---|
| 248 | static void xc_entry_hold_var_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 249 | { |
|---|
| 250 | xce->refcount ++; |
|---|
| 251 | xc_stack_push(&XG(var_holds)[xce->cache->cacheid], (void *)xce); |
|---|
| 252 | } |
|---|
| 253 | /* }}} */ |
|---|
| 254 | #endif |
|---|
| 255 | |
|---|
| 256 | /* helper function that loop through each entry */ |
|---|
| 257 | #define XC_ENTRY_APPLY_FUNC(name) int name(xc_entry_t *entry TSRMLS_DC) |
|---|
| 258 | typedef XC_ENTRY_APPLY_FUNC((*cache_apply_dmz_func_t)); |
|---|
| 259 | static void xc_entry_apply_dmz(xc_cache_t *cache, cache_apply_dmz_func_t apply_func TSRMLS_DC) /* {{{ */ |
|---|
| 260 | { |
|---|
| 261 | xc_entry_t *p, **pp; |
|---|
| 262 | int i, c; |
|---|
| 263 | |
|---|
| 264 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
|---|
| 265 | pp = &(cache->entries[i]); |
|---|
| 266 | for (p = *pp; p; p = *pp) { |
|---|
| 267 | if (apply_func(p TSRMLS_CC)) { |
|---|
| 268 | /* unlink */ |
|---|
| 269 | *pp = p->next; |
|---|
| 270 | xc_entry_free_dmz(p TSRMLS_CC); |
|---|
| 271 | } |
|---|
| 272 | else { |
|---|
| 273 | pp = &(p->next); |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | } |
|---|
| 277 | } |
|---|
| 278 | /* }}} */ |
|---|
| 279 | |
|---|
| 280 | #define XC_CACHE_APPLY_FUNC(name) void name(xc_cache_t *cache TSRMLS_DC) |
|---|
| 281 | /* call graph: |
|---|
| 282 | * xc_gc_expires_php -> xc_gc_expires_one -> xc_entry_apply_dmz -> xc_gc_expires_php_entry_dmz |
|---|
| 283 | * xc_gc_expires_var -> xc_gc_expires_one -> xc_entry_apply_dmz -> xc_gc_expires_var_entry_dmz |
|---|
| 284 | */ |
|---|
| 285 | static XC_ENTRY_APPLY_FUNC(xc_gc_expires_php_entry_dmz) /* {{{ */ |
|---|
| 286 | { |
|---|
| 287 | #ifdef DEBUG |
|---|
| 288 | fprintf(stderr, "ttl %d, %d %d\n", XG(request_time), entry->atime, xc_php_ttl); |
|---|
| 289 | #endif |
|---|
| 290 | if (XG(request_time) > entry->atime + xc_php_ttl) { |
|---|
| 291 | return 1; |
|---|
| 292 | } |
|---|
| 293 | return 0; |
|---|
| 294 | } |
|---|
| 295 | /* }}} */ |
|---|
| 296 | static XC_ENTRY_APPLY_FUNC(xc_gc_expires_var_entry_dmz) /* {{{ */ |
|---|
| 297 | { |
|---|
| 298 | if (VAR_ENTRY_EXPIRED(entry)) { |
|---|
| 299 | return 1; |
|---|
| 300 | } |
|---|
| 301 | return 0; |
|---|
| 302 | } |
|---|
| 303 | /* }}} */ |
|---|
| 304 | static void xc_gc_expires_one(xc_cache_t *cache, zend_ulong gc_interval, cache_apply_dmz_func_t apply_func TSRMLS_DC) /* {{{ */ |
|---|
| 305 | { |
|---|
| 306 | #ifdef DEBUG |
|---|
| 307 | fprintf(stderr, "interval %d, %d %d\n", XG(request_time), cache->last_gc_expires, gc_interval); |
|---|
| 308 | #endif |
|---|
| 309 | if (XG(request_time) - cache->last_gc_expires >= gc_interval) { |
|---|
| 310 | ENTER_LOCK(cache) { |
|---|
| 311 | if (XG(request_time) - cache->last_gc_expires >= gc_interval) { |
|---|
| 312 | cache->last_gc_expires = XG(request_time); |
|---|
| 313 | xc_entry_apply_dmz(cache, apply_func TSRMLS_CC); |
|---|
| 314 | } |
|---|
| 315 | } LEAVE_LOCK(cache); |
|---|
| 316 | } |
|---|
| 317 | } |
|---|
| 318 | /* }}} */ |
|---|
| 319 | static void xc_gc_expires_php(TSRMLS_D) /* {{{ */ |
|---|
| 320 | { |
|---|
| 321 | int i, c; |
|---|
| 322 | |
|---|
| 323 | if (!xc_php_ttl || !xc_php_gc_interval) { |
|---|
| 324 | return; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | for (i = 0, c = xc_php_hcache.size; i < c; i ++) { |
|---|
| 328 | xc_gc_expires_one(xc_php_caches[i], xc_php_gc_interval, xc_gc_expires_php_entry_dmz TSRMLS_CC); |
|---|
| 329 | } |
|---|
| 330 | } |
|---|
| 331 | /* }}} */ |
|---|
| 332 | static void xc_gc_expires_var(TSRMLS_D) /* {{{ */ |
|---|
| 333 | { |
|---|
| 334 | int i, c; |
|---|
| 335 | |
|---|
| 336 | if (!xc_var_gc_interval) { |
|---|
| 337 | return; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | for (i = 0, c = xc_var_hcache.size; i < c; i ++) { |
|---|
| 341 | xc_gc_expires_one(xc_var_caches[i], xc_var_gc_interval, xc_gc_expires_var_entry_dmz TSRMLS_CC); |
|---|
| 342 | } |
|---|
| 343 | } |
|---|
| 344 | /* }}} */ |
|---|
| 345 | |
|---|
| 346 | static XC_CACHE_APPLY_FUNC(xc_gc_delete_dmz) /* {{{ */ |
|---|
| 347 | { |
|---|
| 348 | xc_entry_t *p, **pp; |
|---|
| 349 | |
|---|
| 350 | pp = &cache->deletes; |
|---|
| 351 | for (p = *pp; p; p = *pp) { |
|---|
| 352 | if (XG(request_time) - p->dtime > 3600) { |
|---|
| 353 | p->refcount = 0; |
|---|
| 354 | /* issue warning here */ |
|---|
| 355 | } |
|---|
| 356 | if (p->refcount == 0) { |
|---|
| 357 | /* unlink */ |
|---|
| 358 | *pp = p->next; |
|---|
| 359 | cache->deletes_count --; |
|---|
| 360 | xc_entry_free_real_dmz(p); |
|---|
| 361 | } |
|---|
| 362 | else { |
|---|
| 363 | pp = &(p->next); |
|---|
| 364 | } |
|---|
| 365 | } |
|---|
| 366 | } |
|---|
| 367 | /* }}} */ |
|---|
| 368 | static XC_CACHE_APPLY_FUNC(xc_gc_deletes_one) /* {{{ */ |
|---|
| 369 | { |
|---|
| 370 | if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) { |
|---|
| 371 | ENTER_LOCK(cache) { |
|---|
| 372 | if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) { |
|---|
| 373 | cache->last_gc_deletes = XG(request_time); |
|---|
| 374 | xc_gc_delete_dmz(cache TSRMLS_CC); |
|---|
| 375 | } |
|---|
| 376 | } LEAVE_LOCK(cache); |
|---|
| 377 | } |
|---|
| 378 | } |
|---|
| 379 | /* }}} */ |
|---|
| 380 | static void xc_gc_deletes(TSRMLS_D) /* {{{ */ |
|---|
| 381 | { |
|---|
| 382 | int i, c; |
|---|
| 383 | |
|---|
| 384 | for (i = 0, c = xc_php_hcache.size; i < c; i ++) { |
|---|
| 385 | xc_gc_deletes_one(xc_php_caches[i] TSRMLS_CC); |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | for (i = 0, c = xc_var_hcache.size; i < c; i ++) { |
|---|
| 389 | xc_gc_deletes_one(xc_var_caches[i] TSRMLS_CC); |
|---|
| 390 | } |
|---|
| 391 | } |
|---|
| 392 | /* }}} */ |
|---|
| 393 | |
|---|
| 394 | /* helper functions for user functions */ |
|---|
| 395 | static void xc_fillinfo_dmz(int cachetype, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 396 | { |
|---|
| 397 | zval *blocks; |
|---|
| 398 | const xc_block_t *b; |
|---|
| 399 | #ifndef NDEBUG |
|---|
| 400 | xc_memsize_t avail = 0; |
|---|
| 401 | #endif |
|---|
| 402 | xc_mem_t *mem = cache->mem; |
|---|
| 403 | const xc_mem_handlers_t *handlers = mem->handlers; |
|---|
| 404 | zend_ulong interval = (cachetype == XC_TYPE_PHP) ? xc_php_gc_interval : xc_var_gc_interval; |
|---|
| 405 | |
|---|
| 406 | add_assoc_long_ex(return_value, ZEND_STRS("slots"), cache->hentry->size); |
|---|
| 407 | add_assoc_long_ex(return_value, ZEND_STRS("compiling"), cache->compiling); |
|---|
| 408 | add_assoc_long_ex(return_value, ZEND_STRS("misses"), cache->misses); |
|---|
| 409 | add_assoc_long_ex(return_value, ZEND_STRS("hits"), cache->hits); |
|---|
| 410 | add_assoc_long_ex(return_value, ZEND_STRS("clogs"), cache->clogs); |
|---|
| 411 | add_assoc_long_ex(return_value, ZEND_STRS("ooms"), cache->ooms); |
|---|
| 412 | |
|---|
| 413 | add_assoc_long_ex(return_value, ZEND_STRS("cached"), cache->entries_count); |
|---|
| 414 | add_assoc_long_ex(return_value, ZEND_STRS("deleted"), cache->deletes_count); |
|---|
| 415 | if (interval) { |
|---|
| 416 | add_assoc_long_ex(return_value, ZEND_STRS("gc"), (cache->last_gc_expires + interval) - XG(request_time)); |
|---|
| 417 | } |
|---|
| 418 | else { |
|---|
| 419 | add_assoc_null_ex(return_value, ZEND_STRS("gc")); |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | MAKE_STD_ZVAL(blocks); |
|---|
| 423 | array_init(blocks); |
|---|
| 424 | |
|---|
| 425 | add_assoc_long_ex(return_value, ZEND_STRS("size"), handlers->size(mem)); |
|---|
| 426 | add_assoc_long_ex(return_value, ZEND_STRS("avail"), handlers->avail(mem)); |
|---|
| 427 | add_assoc_bool_ex(return_value, ZEND_STRS("can_readonly"), xc_readonly_protection); |
|---|
| 428 | |
|---|
| 429 | for (b = handlers->freeblock_first(mem); b; b = handlers->freeblock_next(b)) { |
|---|
| 430 | zval *bi; |
|---|
| 431 | |
|---|
| 432 | MAKE_STD_ZVAL(bi); |
|---|
| 433 | array_init(bi); |
|---|
| 434 | |
|---|
| 435 | add_assoc_long_ex(bi, ZEND_STRS("size"), handlers->block_size(b)); |
|---|
| 436 | add_assoc_long_ex(bi, ZEND_STRS("offset"), handlers->block_offset(mem, b)); |
|---|
| 437 | add_next_index_zval(blocks, bi); |
|---|
| 438 | #ifndef NDEBUG |
|---|
| 439 | avail += handlers->block_size(b); |
|---|
| 440 | #endif |
|---|
| 441 | } |
|---|
| 442 | add_assoc_zval_ex(return_value, ZEND_STRS("free_blocks"), blocks); |
|---|
| 443 | assert(avail == handlers->avail(mem)); |
|---|
| 444 | } |
|---|
| 445 | /* }}} */ |
|---|
| 446 | static void xc_fillentry_dmz(xc_entry_t *entry, int del, zval *list TSRMLS_DC) /* {{{ */ |
|---|
| 447 | { |
|---|
| 448 | zval* ei; |
|---|
| 449 | xc_entry_data_php_t *php; |
|---|
| 450 | xc_entry_data_var_t *var; |
|---|
| 451 | |
|---|
| 452 | ALLOC_INIT_ZVAL(ei); |
|---|
| 453 | array_init(ei); |
|---|
| 454 | |
|---|
| 455 | add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size); |
|---|
| 456 | add_assoc_long_ex(ei, ZEND_STRS("refcount"), entry->refcount); |
|---|
| 457 | add_assoc_long_ex(ei, ZEND_STRS("hits"), entry->hits); |
|---|
| 458 | add_assoc_long_ex(ei, ZEND_STRS("ctime"), entry->ctime); |
|---|
| 459 | add_assoc_long_ex(ei, ZEND_STRS("atime"), entry->atime); |
|---|
| 460 | if (del) { |
|---|
| 461 | add_assoc_long_ex(ei, ZEND_STRS("dtime"), entry->dtime); |
|---|
| 462 | } |
|---|
| 463 | #ifdef IS_UNICODE |
|---|
| 464 | do { |
|---|
| 465 | zval *zv; |
|---|
| 466 | ALLOC_INIT_ZVAL(zv); |
|---|
| 467 | switch (entry->name_type) { |
|---|
| 468 | case IS_UNICODE: |
|---|
| 469 | ZVAL_UNICODEL(zv, entry->name.ustr.val, entry->name.ustr.len, 1); |
|---|
| 470 | break; |
|---|
| 471 | case IS_STRING: |
|---|
| 472 | ZVAL_STRINGL(zv, entry->name.str.val, entry->name.str.len, 1); |
|---|
| 473 | break; |
|---|
| 474 | default: |
|---|
| 475 | assert(0); |
|---|
| 476 | } |
|---|
| 477 | zv->type = entry->name_type; |
|---|
| 478 | add_assoc_zval_ex(ei, ZEND_STRS("name"), zv); |
|---|
| 479 | } while (0); |
|---|
| 480 | #else |
|---|
| 481 | add_assoc_stringl_ex(ei, ZEND_STRS("name"), entry->name.str.val, entry->name.str.len, 1); |
|---|
| 482 | #endif |
|---|
| 483 | switch (entry->type) { |
|---|
| 484 | case XC_TYPE_PHP: |
|---|
| 485 | php = entry->data.php; |
|---|
| 486 | add_assoc_long_ex(ei, ZEND_STRS("sourcesize"), php->sourcesize); |
|---|
| 487 | #ifdef HAVE_INODE |
|---|
| 488 | add_assoc_long_ex(ei, ZEND_STRS("device"), php->device); |
|---|
| 489 | add_assoc_long_ex(ei, ZEND_STRS("inode"), php->inode); |
|---|
| 490 | #endif |
|---|
| 491 | add_assoc_long_ex(ei, ZEND_STRS("mtime"), php->mtime); |
|---|
| 492 | |
|---|
| 493 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 494 | add_assoc_long_ex(ei, ZEND_STRS("constinfo_cnt"), php->constinfo_cnt); |
|---|
| 495 | #endif |
|---|
| 496 | add_assoc_long_ex(ei, ZEND_STRS("function_cnt"), php->funcinfo_cnt); |
|---|
| 497 | add_assoc_long_ex(ei, ZEND_STRS("class_cnt"), php->classinfo_cnt); |
|---|
| 498 | break; |
|---|
| 499 | case XC_TYPE_VAR: |
|---|
| 500 | var = entry->data.var; |
|---|
| 501 | break; |
|---|
| 502 | |
|---|
| 503 | default: |
|---|
| 504 | assert(0); |
|---|
| 505 | } |
|---|
| 506 | |
|---|
| 507 | add_next_index_zval(list, ei); |
|---|
| 508 | } |
|---|
| 509 | /* }}} */ |
|---|
| 510 | static void xc_filllist_dmz(xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
|---|
| 511 | { |
|---|
| 512 | zval* list; |
|---|
| 513 | int i, c; |
|---|
| 514 | xc_entry_t *e; |
|---|
| 515 | |
|---|
| 516 | ALLOC_INIT_ZVAL(list); |
|---|
| 517 | array_init(list); |
|---|
| 518 | |
|---|
| 519 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
|---|
| 520 | for (e = cache->entries[i]; e; e = e->next) { |
|---|
| 521 | xc_fillentry_dmz(e, 0, list TSRMLS_CC); |
|---|
| 522 | } |
|---|
| 523 | } |
|---|
| 524 | add_assoc_zval(return_value, "cache_list", list); |
|---|
| 525 | |
|---|
| 526 | ALLOC_INIT_ZVAL(list); |
|---|
| 527 | array_init(list); |
|---|
| 528 | for (e = cache->deletes; e; e = e->next) { |
|---|
| 529 | xc_fillentry_dmz(e, 1, list TSRMLS_CC); |
|---|
| 530 | } |
|---|
| 531 | add_assoc_zval(return_value, "deleted_list", list); |
|---|
| 532 | } |
|---|
| 533 | /* }}} */ |
|---|
| 534 | |
|---|
| 535 | static zend_op_array *xc_entry_install(xc_entry_t *xce, zend_file_handle *h TSRMLS_DC) /* {{{ */ |
|---|
| 536 | { |
|---|
| 537 | zend_uint i; |
|---|
| 538 | xc_entry_data_php_t *p = xce->data.php; |
|---|
| 539 | #ifndef ZEND_ENGINE_2 |
|---|
| 540 | /* new ptr which is stored inside CG(class_table) */ |
|---|
| 541 | xc_cest_t **new_cest_ptrs = (xc_cest_t **)do_alloca(sizeof(xc_cest_t*) * p->classinfo_cnt); |
|---|
| 542 | #endif |
|---|
| 543 | |
|---|
| 544 | CG(active_op_array) = p->op_array; |
|---|
| 545 | |
|---|
| 546 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 547 | /* install constant */ |
|---|
| 548 | for (i = 0; i < p->constinfo_cnt; i ++) { |
|---|
| 549 | xc_constinfo_t *ci = &p->constinfos[i]; |
|---|
| 550 | xc_install_constant(xce->name.str.val, &ci->constant, |
|---|
| 551 | UNISW(0, ci->type), ci->key, ci->key_size TSRMLS_CC); |
|---|
| 552 | } |
|---|
| 553 | #endif |
|---|
| 554 | |
|---|
| 555 | /* install function */ |
|---|
| 556 | for (i = 0; i < p->funcinfo_cnt; i ++) { |
|---|
| 557 | xc_funcinfo_t *fi = &p->funcinfos[i]; |
|---|
| 558 | xc_install_function(xce->name.str.val, &fi->func, |
|---|
| 559 | UNISW(0, fi->type), fi->key, fi->key_size TSRMLS_CC); |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | /* install class */ |
|---|
| 563 | for (i = 0; i < p->classinfo_cnt; i ++) { |
|---|
| 564 | xc_classinfo_t *ci = &p->classinfos[i]; |
|---|
| 565 | #ifndef ZEND_ENGINE_2 |
|---|
| 566 | zend_class_entry *ce = CestToCePtr(ci->cest); |
|---|
| 567 | /* fix pointer to the be which inside class_table */ |
|---|
| 568 | if (ce->parent) { |
|---|
| 569 | zend_uint class_idx = (/* class_num */ (int) ce->parent) - 1; |
|---|
| 570 | assert(class_idx < i); |
|---|
| 571 | ci->cest.parent = new_cest_ptrs[class_idx]; |
|---|
| 572 | } |
|---|
| 573 | new_cest_ptrs[i] = |
|---|
| 574 | #endif |
|---|
| 575 | xc_install_class(xce->name.str.val, &ci->cest, ci->oplineno, |
|---|
| 576 | UNISW(0, ci->type), ci->key, ci->key_size TSRMLS_CC); |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | i = 1; |
|---|
| 580 | zend_hash_add(&EG(included_files), xce->name.str.val, xce->name.str.len+1, (void *)&i, sizeof(int), NULL); |
|---|
| 581 | if (h) { |
|---|
| 582 | zend_llist_add_element(&CG(open_files), h); |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | #ifndef ZEND_ENGINE_2 |
|---|
| 586 | free_alloca(new_cest_ptrs); |
|---|
| 587 | #endif |
|---|
| 588 | return p->op_array; |
|---|
| 589 | } |
|---|
| 590 | /* }}} */ |
|---|
| 591 | |
|---|
| 592 | static inline void xc_entry_unholds_real(xc_stack_t *holds, xc_cache_t **caches, int cachecount TSRMLS_DC) /* {{{ */ |
|---|
| 593 | { |
|---|
| 594 | int i; |
|---|
| 595 | xc_stack_t *s; |
|---|
| 596 | xc_cache_t *cache; |
|---|
| 597 | xc_entry_t *xce; |
|---|
| 598 | |
|---|
| 599 | for (i = 0; i < cachecount; i ++) { |
|---|
| 600 | s = &holds[i]; |
|---|
| 601 | #ifdef DEBUG |
|---|
| 602 | fprintf(stderr, "holded %d\n", xc_stack_size(s)); |
|---|
| 603 | #endif |
|---|
| 604 | if (xc_stack_size(s)) { |
|---|
| 605 | cache = ((xc_entry_t *)xc_stack_top(s))->cache; |
|---|
| 606 | ENTER_LOCK(cache) { |
|---|
| 607 | while (xc_stack_size(s)) { |
|---|
| 608 | xce = (xc_entry_t*) xc_stack_pop(s); |
|---|
| 609 | #ifdef DEBUG |
|---|
| 610 | fprintf(stderr, "unhold %s\n", ZSTR_S(xce->name)); |
|---|
| 611 | #endif |
|---|
| 612 | xce->refcount --; |
|---|
| 613 | assert(xce->refcount >= 0); |
|---|
| 614 | } |
|---|
| 615 | } LEAVE_LOCK(cache); |
|---|
| 616 | } |
|---|
| 617 | } |
|---|
| 618 | } |
|---|
| 619 | /* }}} */ |
|---|
| 620 | static void xc_entry_unholds(TSRMLS_D) /* {{{ */ |
|---|
| 621 | { |
|---|
| 622 | xc_entry_unholds_real(XG(php_holds), xc_php_caches, xc_php_hcache.size TSRMLS_CC); |
|---|
| 623 | xc_entry_unholds_real(XG(var_holds), xc_var_caches, xc_var_hcache.size TSRMLS_CC); |
|---|
| 624 | } |
|---|
| 625 | /* }}} */ |
|---|
| 626 | static int xc_stat(const char *filename, const char *include_path, struct stat *pbuf TSRMLS_DC) /* {{{ */ |
|---|
| 627 | { |
|---|
| 628 | char filepath[MAXPATHLEN]; |
|---|
| 629 | char *paths, *path; |
|---|
| 630 | char *tokbuf; |
|---|
| 631 | int size = strlen(include_path) + 1; |
|---|
| 632 | char tokens[] = { DEFAULT_DIR_SEPARATOR, '\0' }; |
|---|
| 633 | |
|---|
| 634 | paths = (char *)do_alloca(size); |
|---|
| 635 | memcpy(paths, include_path, size); |
|---|
| 636 | |
|---|
| 637 | for (path = php_strtok_r(paths, tokens, &tokbuf); path; path = php_strtok_r(NULL, tokens, &tokbuf)) { |
|---|
| 638 | if (snprintf(filepath, sizeof(filepath), "%s/%s", path, filename) >= MAXPATHLEN - 1) { |
|---|
| 639 | continue; |
|---|
| 640 | } |
|---|
| 641 | if (VCWD_STAT(filepath, pbuf) == 0) { |
|---|
| 642 | free_alloca(paths); |
|---|
| 643 | return 0; |
|---|
| 644 | } |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | free_alloca(paths); |
|---|
| 648 | |
|---|
| 649 | return 1; |
|---|
| 650 | } |
|---|
| 651 | /* }}} */ |
|---|
| 652 | |
|---|
| 653 | #define HASH(i) (i) |
|---|
| 654 | #define HASH_USTR_L(t, s, l) HASH(zend_u_inline_hash_func(t, s, (l + 1) * sizeof(UChar))) |
|---|
| 655 | #define HASH_STR_L(s, l) HASH(zend_inline_hash_func(s, l + 1)) |
|---|
| 656 | #define HASH_STR(s) HASH_STR_L(s, strlen(s) + 1) |
|---|
| 657 | #define HASH_NUM(n) HASH(n) |
|---|
| 658 | static inline xc_hash_value_t xc_entry_hash_name(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 659 | { |
|---|
| 660 | return UNISW(NOTHING, UG(unicode) ? HASH_USTR_L(xce->name_type, xce->name.uni.val, xce->name.uni.len) :) |
|---|
| 661 | HASH_STR_L(xce->name.str.val, xce->name.str.len); |
|---|
| 662 | } |
|---|
| 663 | /* }}} */ |
|---|
| 664 | #define xc_entry_hash_var xc_entry_hash_name |
|---|
| 665 | static inline xc_hash_value_t xc_entry_hash_php(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
|---|
| 666 | { |
|---|
| 667 | #ifdef HAVE_INODE |
|---|
| 668 | if (xce->data.php->inode) { |
|---|
| 669 | return HASH(xce->data.php->device + xce->data.php->inode); |
|---|
| 670 | } |
|---|
| 671 | #endif |
|---|
| 672 | return xc_entry_hash_name(xce TSRMLS_CC); |
|---|
| 673 | } |
|---|
| 674 | /* }}} */ |
|---|
| 675 | static int xc_entry_init_key_php(xc_entry_t *xce, char *filename, char *opened_path_buffer TSRMLS_DC) /* {{{ */ |
|---|
| 676 | { |
|---|
| 677 | struct stat buf, *pbuf; |
|---|
| 678 | xc_hash_value_t hv; |
|---|
| 679 | int cacheid; |
|---|
| 680 | xc_entry_data_php_t *php; |
|---|
| 681 | char *ptr; |
|---|
| 682 | |
|---|
| 683 | if (!filename || !SG(request_info).path_translated) { |
|---|
| 684 | return 0; |
|---|
| 685 | } |
|---|
| 686 | |
|---|
| 687 | php = xce->data.php; |
|---|
| 688 | |
|---|
| 689 | if (XG(stat)) { |
|---|
| 690 | if (strcmp(SG(request_info).path_translated, filename) == 0) { |
|---|
| 691 | /* sapi has already done this stat() for us */ |
|---|
| 692 | pbuf = sapi_get_stat(TSRMLS_C); |
|---|
| 693 | if (pbuf) { |
|---|
| 694 | goto stat_done; |
|---|
| 695 | } |
|---|
| 696 | } |
|---|
| 697 | |
|---|
| 698 | /* absolute path */ |
|---|
| 699 | pbuf = &buf; |
|---|
| 700 | if (IS_ABSOLUTE_PATH(filename, strlen(filename))) { |
|---|
| 701 | if (VCWD_STAT(filename, pbuf) != 0) { |
|---|
| 702 | return 0; |
|---|
| 703 | } |
|---|
| 704 | goto stat_done; |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | /* relative path */ |
|---|
| 708 | if (*filename == '.' && (IS_SLASH(filename[1]) || filename[1] == '.')) { |
|---|
| 709 | ptr = filename + 1; |
|---|
| 710 | if (*ptr == '.') { |
|---|
| 711 | while (*(++ptr) == '.'); |
|---|
| 712 | if (!IS_SLASH(*ptr)) { |
|---|
| 713 | goto not_relative_path; |
|---|
| 714 | } |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | if (VCWD_STAT(filename, pbuf) != 0) { |
|---|
| 718 | return 0; |
|---|
| 719 | } |
|---|
| 720 | goto stat_done; |
|---|
| 721 | } |
|---|
| 722 | not_relative_path: |
|---|
| 723 | |
|---|
| 724 | /* use include_path */ |
|---|
| 725 | if (xc_stat(filename, PG(include_path), pbuf TSRMLS_CC) != 0) { |
|---|
| 726 | return 0; |
|---|
| 727 | } |
|---|
| 728 | |
|---|
| 729 | /* fall */ |
|---|
| 730 | |
|---|
| 731 | stat_done: |
|---|
| 732 | if (XG(request_time) - pbuf->st_mtime < 2 && !xc_test) { |
|---|
| 733 | return 0; |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | php->mtime = pbuf->st_mtime; |
|---|
| 737 | #ifdef HAVE_INODE |
|---|
| 738 | php->device = pbuf->st_dev; |
|---|
| 739 | php->inode = pbuf->st_ino; |
|---|
| 740 | #endif |
|---|
| 741 | php->sourcesize = pbuf->st_size; |
|---|
| 742 | } |
|---|
| 743 | else { /* XG(inode) */ |
|---|
| 744 | php->mtime = 0; |
|---|
| 745 | #ifdef HAVE_INODE |
|---|
| 746 | php->device = 0; |
|---|
| 747 | php->inode = 0; |
|---|
| 748 | #endif |
|---|
| 749 | php->sourcesize = 0; |
|---|
| 750 | } |
|---|
| 751 | |
|---|
| 752 | #ifdef HAVE_INODE |
|---|
| 753 | if (!php->inode) |
|---|
| 754 | #endif |
|---|
| 755 | { |
|---|
| 756 | /* hash on filename, let's expand it to real path */ |
|---|
| 757 | filename = expand_filepath(filename, opened_path_buffer TSRMLS_CC); |
|---|
| 758 | if (filename == NULL) { |
|---|
| 759 | return 0; |
|---|
| 760 | } |
|---|
| 761 | } |
|---|
| 762 | |
|---|
| 763 | UNISW(NOTHING, xce->name_type = IS_STRING;) |
|---|
| 764 | xce->name.str.val = filename; |
|---|
| 765 | xce->name.str.len = strlen(filename); |
|---|
| 766 | |
|---|
| 767 | hv = xc_entry_hash_php(xce TSRMLS_CC); |
|---|
| 768 | cacheid = (hv & xc_php_hcache.mask); |
|---|
| 769 | xce->cache = xc_php_caches[cacheid]; |
|---|
| 770 | hv >>= xc_php_hcache.bits; |
|---|
| 771 | xce->hvalue = (hv & xc_php_hentry.mask); |
|---|
| 772 | |
|---|
| 773 | xce->type = XC_TYPE_PHP; |
|---|
| 774 | return 1; |
|---|
| 775 | } |
|---|
| 776 | /* }}} */ |
|---|
| 777 | static void xc_cache_early_binding_class_cb(zend_op *opline, int oplineno, void *data TSRMLS_DC) /* {{{ */ |
|---|
| 778 | { |
|---|
| 779 | char *class_name; |
|---|
| 780 | int i, class_len; |
|---|
| 781 | xc_cest_t cest; |
|---|
| 782 | xc_entry_data_php_t *php = (xc_entry_data_php_t *) data; |
|---|
| 783 | |
|---|
| 784 | class_name = opline->op1.u.constant.value.str.val; |
|---|
| 785 | class_len = opline->op1.u.constant.value.str.len; |
|---|
| 786 | if (zend_hash_find(CG(class_table), class_name, class_len, (void **) &cest) == FAILURE) { |
|---|
| 787 | assert(0); |
|---|
| 788 | } |
|---|
| 789 | #ifdef DEBUG |
|---|
| 790 | fprintf(stderr, "got ZEND_DECLARE_INHERITED_CLASS: %s\n", class_name + 1); |
|---|
| 791 | #endif |
|---|
| 792 | /* let's see which class */ |
|---|
| 793 | for (i = 0; i < php->classinfo_cnt; i ++) { |
|---|
| 794 | if (memcmp(ZSTR_S(php->classinfos[i].key), class_name, class_len) == 0) { |
|---|
| 795 | php->classinfos[i].oplineno = oplineno; |
|---|
| 796 | php->have_early_binding = 1; |
|---|
| 797 | break; |
|---|
| 798 | } |
|---|
| 799 | } |
|---|
| 800 | |
|---|
| 801 | if (i == php->classinfo_cnt) { |
|---|
| 802 | assert(0); |
|---|
| 803 | } |
|---|
| 804 | } |
|---|
| 805 | /* }}} */ |
|---|
| 806 | static zend_op_array *xc_compile_file(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
|---|
| 807 | { |
|---|
| 808 | xc_sandbox_t sandbox; |
|---|
| 809 | zend_op_array *op_array; |
|---|
| 810 | xc_entry_t xce, *stored_xce; |
|---|
| 811 | xc_entry_data_php_t php; |
|---|
| 812 | xc_cache_t *cache; |
|---|
| 813 | zend_bool clogged = 0; |
|---|
| 814 | zend_bool catched = 0; |
|---|
| 815 | char *filename; |
|---|
| 816 | char opened_path_buffer[MAXPATHLEN]; |
|---|
| 817 | int old_constinfo_cnt, old_funcinfo_cnt, old_classinfo_cnt; |
|---|
| 818 | int i; |
|---|
| 819 | |
|---|
| 820 | if (!xc_initized) { |
|---|
| 821 | assert(0); |
|---|
| 822 | } |
|---|
| 823 | |
|---|
| 824 | if (!XG(cacher)) { |
|---|
| 825 | op_array = origin_compile_file(h, type TSRMLS_CC); |
|---|
| 826 | #ifdef HAVE_XCACHE_OPTIMIZER |
|---|
| 827 | if (XG(optimizer)) { |
|---|
| 828 | xc_optimize(op_array TSRMLS_CC); |
|---|
| 829 | } |
|---|
| 830 | #endif |
|---|
| 831 | return op_array; |
|---|
| 832 | } |
|---|
| 833 | |
|---|
| 834 | /* {{{ prepare key |
|---|
| 835 | * include_once() and require_once() gives us opened_path |
|---|
| 836 | * however, include() and require() non-absolute path which break |
|---|
| 837 | * included_files, and may confuse with (include|require)_once |
|---|
| 838 | * -- Xuefer |
|---|
| 839 | */ |
|---|
| 840 | |
|---|
| 841 | filename = h->opened_path ? h->opened_path : h->filename; |
|---|
| 842 | xce.data.php = &php; |
|---|
| 843 | if (!xc_entry_init_key_php(&xce, filename, opened_path_buffer TSRMLS_CC)) { |
|---|
| 844 | return origin_compile_file(h, type TSRMLS_CC); |
|---|
| 845 | } |
|---|
| 846 | cache = xce.cache; |
|---|
| 847 | /* }}} */ |
|---|
| 848 | /* {{{ restore */ |
|---|
| 849 | /* stale precheck */ |
|---|
| 850 | if (cache->compiling) { |
|---|
| 851 | cache->clogs ++; /* is it safe here? */ |
|---|
| 852 | return origin_compile_file(h, type TSRMLS_CC); |
|---|
| 853 | } |
|---|
| 854 | |
|---|
| 855 | stored_xce = NULL; |
|---|
| 856 | op_array = NULL; |
|---|
| 857 | ENTER_LOCK_EX(cache) { |
|---|
| 858 | /* clogged */ |
|---|
| 859 | if (cache->compiling) { |
|---|
| 860 | cache->clogs ++; |
|---|
| 861 | op_array = NULL; |
|---|
| 862 | clogged = 1; |
|---|
| 863 | break; |
|---|
| 864 | } |
|---|
| 865 | |
|---|
| 866 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 867 | /* found */ |
|---|
| 868 | if (stored_xce) { |
|---|
| 869 | #ifdef DEBUG |
|---|
| 870 | fprintf(stderr, "found %s, catch it\n", stored_xce->name.str.val); |
|---|
| 871 | #endif |
|---|
| 872 | xc_entry_hold_php_dmz(stored_xce TSRMLS_CC); |
|---|
| 873 | cache->hits ++; |
|---|
| 874 | break; |
|---|
| 875 | } |
|---|
| 876 | |
|---|
| 877 | cache->compiling = XG(request_time); |
|---|
| 878 | cache->misses ++; |
|---|
| 879 | } LEAVE_LOCK_EX(cache); |
|---|
| 880 | |
|---|
| 881 | if (catched) { |
|---|
| 882 | cache->compiling = 0; |
|---|
| 883 | zend_bailout(); |
|---|
| 884 | } |
|---|
| 885 | |
|---|
| 886 | /* found */ |
|---|
| 887 | if (stored_xce) { |
|---|
| 888 | goto restore; |
|---|
| 889 | } |
|---|
| 890 | |
|---|
| 891 | /* clogged */ |
|---|
| 892 | if (clogged) { |
|---|
| 893 | return origin_compile_file(h, type TSRMLS_CC); |
|---|
| 894 | } |
|---|
| 895 | /* }}} */ |
|---|
| 896 | |
|---|
| 897 | /* {{{ compile */ |
|---|
| 898 | #ifdef DEBUG |
|---|
| 899 | fprintf(stderr, "compiling %s\n", filename); |
|---|
| 900 | #endif |
|---|
| 901 | |
|---|
| 902 | /* make compile inside sandbox */ |
|---|
| 903 | xc_sandbox_init(&sandbox, filename TSRMLS_CC); |
|---|
| 904 | |
|---|
| 905 | old_classinfo_cnt = zend_hash_num_elements(CG(class_table)); |
|---|
| 906 | old_funcinfo_cnt = zend_hash_num_elements(CG(function_table)); |
|---|
| 907 | old_constinfo_cnt = zend_hash_num_elements(EG(zend_constants)); |
|---|
| 908 | |
|---|
| 909 | zend_try { |
|---|
| 910 | op_array = origin_compile_file(h, type TSRMLS_CC); |
|---|
| 911 | } zend_catch { |
|---|
| 912 | catched = 1; |
|---|
| 913 | } zend_end_try(); |
|---|
| 914 | |
|---|
| 915 | if (catched) { |
|---|
| 916 | goto err_bailout; |
|---|
| 917 | } |
|---|
| 918 | |
|---|
| 919 | if (op_array == NULL) { |
|---|
| 920 | goto err_oparray; |
|---|
| 921 | } |
|---|
| 922 | |
|---|
| 923 | filename = h->opened_path ? h->opened_path : h->filename; |
|---|
| 924 | /* none-inode enabled entry hash/compare on name |
|---|
| 925 | * do not update to its name to real pathname |
|---|
| 926 | */ |
|---|
| 927 | #ifdef HAVE_INODE |
|---|
| 928 | if (xce.data.php->inode) |
|---|
| 929 | { |
|---|
| 930 | if (xce.name.str.val != filename) { |
|---|
| 931 | xce.name.str.val = filename; |
|---|
| 932 | xce.name.str.len = strlen(filename); |
|---|
| 933 | } |
|---|
| 934 | } |
|---|
| 935 | #endif |
|---|
| 936 | |
|---|
| 937 | #ifdef HAVE_XCACHE_OPTIMIZER |
|---|
| 938 | if (XG(optimizer)) { |
|---|
| 939 | xc_optimize(op_array TSRMLS_CC); |
|---|
| 940 | } |
|---|
| 941 | #endif |
|---|
| 942 | /* }}} */ |
|---|
| 943 | /* {{{ prepare */ |
|---|
| 944 | php.op_array = op_array; |
|---|
| 945 | |
|---|
| 946 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 947 | php.constinfo_cnt = zend_hash_num_elements(EG(zend_constants)) - old_constinfo_cnt; |
|---|
| 948 | #endif |
|---|
| 949 | php.funcinfo_cnt = zend_hash_num_elements(CG(function_table)) - old_funcinfo_cnt; |
|---|
| 950 | php.classinfo_cnt = zend_hash_num_elements(CG(class_table)) - old_classinfo_cnt; |
|---|
| 951 | |
|---|
| 952 | #define X_ALLOC_N(var, cnt) do { \ |
|---|
| 953 | if (php.cnt) { \ |
|---|
| 954 | ECALLOC_N(php.var, php.cnt); \ |
|---|
| 955 | if (!php.var) { \ |
|---|
| 956 | goto err_##var; \ |
|---|
| 957 | } \ |
|---|
| 958 | } \ |
|---|
| 959 | else { \ |
|---|
| 960 | php.var = NULL; \ |
|---|
| 961 | } \ |
|---|
| 962 | } while (0) |
|---|
| 963 | |
|---|
| 964 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 965 | X_ALLOC_N(constinfos, constinfo_cnt); |
|---|
| 966 | #endif |
|---|
| 967 | X_ALLOC_N(funcinfos, funcinfo_cnt); |
|---|
| 968 | X_ALLOC_N(classinfos, classinfo_cnt); |
|---|
| 969 | #undef X_ALLOC |
|---|
| 970 | /* }}} */ |
|---|
| 971 | /* {{{ shallow copy, pointers only */ { |
|---|
| 972 | Bucket *b; |
|---|
| 973 | unsigned int i; |
|---|
| 974 | |
|---|
| 975 | #define COPY_H(vartype, var, cnt, name, datatype) do { \ |
|---|
| 976 | for (i = 0; b; i ++, b = b->pListNext) { \ |
|---|
| 977 | vartype *data = &php.var[i]; \ |
|---|
| 978 | \ |
|---|
| 979 | if (i < old_##cnt) { \ |
|---|
| 980 | continue; \ |
|---|
| 981 | } \ |
|---|
| 982 | \ |
|---|
| 983 | assert(i < old_##cnt + php.cnt); \ |
|---|
| 984 | assert(b->pData); \ |
|---|
| 985 | memcpy(&data->name, b->pData, sizeof(datatype)); \ |
|---|
| 986 | UNISW(NOTHING, data->type = b->key.type;) \ |
|---|
| 987 | if (UNISW(1, b->key.type == IS_STRING)) { \ |
|---|
| 988 | ZSTR_S(data->key) = BUCKET_KEY_S(b); \ |
|---|
| 989 | } \ |
|---|
| 990 | else { \ |
|---|
| 991 | ZSTR_U(data->key) = BUCKET_KEY_U(b); \ |
|---|
| 992 | } \ |
|---|
| 993 | data->key_size = b->nKeyLength; \ |
|---|
| 994 | } \ |
|---|
| 995 | } while(0) |
|---|
| 996 | |
|---|
| 997 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 998 | b = EG(zend_constants)->pListHead; COPY_H(xc_constinfo_t, constinfos, constinfo_cnt, constant, zend_constant); |
|---|
| 999 | #endif |
|---|
| 1000 | b = CG(function_table)->pListHead; COPY_H(xc_funcinfo_t, funcinfos, funcinfo_cnt, func, zend_function); |
|---|
| 1001 | b = CG(class_table)->pListHead; COPY_H(xc_classinfo_t, classinfos, classinfo_cnt, cest, xc_cest_t); |
|---|
| 1002 | |
|---|
| 1003 | #undef COPY_H |
|---|
| 1004 | /* for ZE1, cest need to fix inside store */ |
|---|
| 1005 | } |
|---|
| 1006 | /* }}} */ |
|---|
| 1007 | /* {{{ find inherited classes that should be early-binding */ |
|---|
| 1008 | php.have_early_binding = 0; |
|---|
| 1009 | for (i = 0; i < php.classinfo_cnt; i ++) { |
|---|
| 1010 | php.classinfos[i].oplineno = -1; |
|---|
| 1011 | } |
|---|
| 1012 | |
|---|
| 1013 | xc_undo_pass_two(php.op_array TSRMLS_CC); |
|---|
| 1014 | xc_foreach_early_binding_class(php.op_array, xc_cache_early_binding_class_cb, (void *) &php TSRMLS_CC); |
|---|
| 1015 | xc_redo_pass_two(php.op_array TSRMLS_CC); |
|---|
| 1016 | /* }}} */ |
|---|
| 1017 | ENTER_LOCK_EX(cache) { /* {{{ store/add entry */ |
|---|
| 1018 | stored_xce = xc_entry_store_dmz(&xce TSRMLS_CC); |
|---|
| 1019 | } LEAVE_LOCK_EX(cache); |
|---|
| 1020 | /* }}} */ |
|---|
| 1021 | #ifdef DEBUG |
|---|
| 1022 | fprintf(stderr, "stored\n"); |
|---|
| 1023 | #endif |
|---|
| 1024 | |
|---|
| 1025 | #define X_FREE(var) \ |
|---|
| 1026 | if (xce.data.php->var) { \ |
|---|
| 1027 | efree(xce.data.php->var); \ |
|---|
| 1028 | } \ |
|---|
| 1029 | err_##var: |
|---|
| 1030 | |
|---|
| 1031 | X_FREE(classinfos) |
|---|
| 1032 | X_FREE(funcinfos) |
|---|
| 1033 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 1034 | X_FREE(constinfos) |
|---|
| 1035 | #endif |
|---|
| 1036 | #undef X_FREE |
|---|
| 1037 | |
|---|
| 1038 | err_oparray: |
|---|
| 1039 | err_bailout: |
|---|
| 1040 | |
|---|
| 1041 | if (xc_test && stored_xce) { |
|---|
| 1042 | /* free it, no install. restore now */ |
|---|
| 1043 | xc_sandbox_free(&sandbox, 0 TSRMLS_CC); |
|---|
| 1044 | } |
|---|
| 1045 | else { |
|---|
| 1046 | CG(active_op_array) = op_array; |
|---|
| 1047 | xc_sandbox_free(&sandbox, 1 TSRMLS_CC); |
|---|
| 1048 | } |
|---|
| 1049 | |
|---|
| 1050 | ENTER_LOCK(cache) { |
|---|
| 1051 | cache->compiling = 0; |
|---|
| 1052 | } LEAVE_LOCK(cache); |
|---|
| 1053 | if (catched) { |
|---|
| 1054 | zend_bailout(); |
|---|
| 1055 | } |
|---|
| 1056 | if (xc_test && stored_xce) { |
|---|
| 1057 | #ifdef ZEND_ENGINE_2 |
|---|
| 1058 | destroy_op_array(op_array TSRMLS_CC); |
|---|
| 1059 | #else |
|---|
| 1060 | destroy_op_array(op_array); |
|---|
| 1061 | #endif |
|---|
| 1062 | efree(op_array); |
|---|
| 1063 | h = NULL; |
|---|
| 1064 | goto restore; |
|---|
| 1065 | } |
|---|
| 1066 | return op_array; |
|---|
| 1067 | |
|---|
| 1068 | restore: |
|---|
| 1069 | if (php_check_open_basedir(stored_xce->name.str.val TSRMLS_CC) != 0) { |
|---|
| 1070 | return NULL; |
|---|
| 1071 | } |
|---|
| 1072 | |
|---|
| 1073 | #ifdef DEBUG |
|---|
| 1074 | fprintf(stderr, "restoring\n"); |
|---|
| 1075 | #endif |
|---|
| 1076 | xc_processor_restore_xc_entry_t(&xce, stored_xce, xc_readonly_protection TSRMLS_CC); |
|---|
| 1077 | |
|---|
| 1078 | catched = 0; |
|---|
| 1079 | zend_try { |
|---|
| 1080 | op_array = xc_entry_install(&xce, h TSRMLS_CC); |
|---|
| 1081 | } zend_catch { |
|---|
| 1082 | catched = 1; |
|---|
| 1083 | } zend_end_try(); |
|---|
| 1084 | |
|---|
| 1085 | #define X_FREE(var) \ |
|---|
| 1086 | if (xce.data.php->var) { \ |
|---|
| 1087 | efree(xce.data.php->var); \ |
|---|
| 1088 | } |
|---|
| 1089 | X_FREE(classinfos) |
|---|
| 1090 | X_FREE(funcinfos) |
|---|
| 1091 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 1092 | X_FREE(constinfos) |
|---|
| 1093 | #endif |
|---|
| 1094 | #undef X_FREE |
|---|
| 1095 | efree(xce.data.php); |
|---|
| 1096 | |
|---|
| 1097 | if (catched) { |
|---|
| 1098 | zend_bailout(); |
|---|
| 1099 | } |
|---|
| 1100 | #ifdef DEBUG |
|---|
| 1101 | fprintf(stderr, "restored\n"); |
|---|
| 1102 | #endif |
|---|
| 1103 | return op_array; |
|---|
| 1104 | } |
|---|
| 1105 | /* }}} */ |
|---|
| 1106 | |
|---|
| 1107 | /* gdb helper functions, but N/A for coredump */ |
|---|
| 1108 | int xc_is_rw(const void *p) /* {{{ */ |
|---|
| 1109 | { |
|---|
| 1110 | xc_shm_t *shm; |
|---|
| 1111 | int i; |
|---|
| 1112 | if (!xc_initized) { |
|---|
| 1113 | return 0; |
|---|
| 1114 | } |
|---|
| 1115 | for (i = 0; i < xc_php_hcache.size; i ++) { |
|---|
| 1116 | shm = xc_php_caches[i]->shm; |
|---|
| 1117 | if (shm->handlers->is_readwrite(shm, p)) { |
|---|
| 1118 | return 1; |
|---|
| 1119 | } |
|---|
| 1120 | } |
|---|
| 1121 | for (i = 0; i < xc_var_hcache.size; i ++) { |
|---|
| 1122 | shm = xc_var_caches[i]->shm; |
|---|
| 1123 | if (shm->handlers->is_readwrite(shm, p)) { |
|---|
| 1124 | return 1; |
|---|
| 1125 | } |
|---|
| 1126 | } |
|---|
| 1127 | return 0; |
|---|
| 1128 | } |
|---|
| 1129 | /* }}} */ |
|---|
| 1130 | int xc_is_ro(const void *p) /* {{{ */ |
|---|
| 1131 | { |
|---|
| 1132 | xc_shm_t *shm; |
|---|
| 1133 | int i; |
|---|
| 1134 | if (!xc_initized) { |
|---|
| 1135 | return 0; |
|---|
| 1136 | } |
|---|
| 1137 | for (i = 0; i < xc_php_hcache.size; i ++) { |
|---|
| 1138 | shm = xc_php_caches[i]->shm; |
|---|
| 1139 | if (shm->handlers->is_readonly(shm, p)) { |
|---|
| 1140 | return 1; |
|---|
| 1141 | } |
|---|
| 1142 | } |
|---|
| 1143 | for (i = 0; i < xc_var_hcache.size; i ++) { |
|---|
| 1144 | shm = xc_var_caches[i]->shm; |
|---|
| 1145 | if (shm->handlers->is_readonly(shm, p)) { |
|---|
| 1146 | return 1; |
|---|
| 1147 | } |
|---|
| 1148 | } |
|---|
| 1149 | return 0; |
|---|
| 1150 | } |
|---|
| 1151 | /* }}} */ |
|---|
| 1152 | int xc_is_shm(const void *p) /* {{{ */ |
|---|
| 1153 | { |
|---|
| 1154 | return xc_is_ro(p) || xc_is_rw(p); |
|---|
| 1155 | } |
|---|
| 1156 | /* }}} */ |
|---|
| 1157 | |
|---|
| 1158 | /* module helper function */ |
|---|
| 1159 | static int xc_init_constant(int module_number TSRMLS_DC) /* {{{ */ |
|---|
| 1160 | { |
|---|
| 1161 | typedef struct { |
|---|
| 1162 | const char *prefix; |
|---|
| 1163 | zend_uchar (*getsize)(); |
|---|
| 1164 | const char *(*get)(zend_uchar i); |
|---|
| 1165 | } xc_meminfo_t; |
|---|
| 1166 | xc_meminfo_t nameinfos[] = { |
|---|
| 1167 | { "", xc_get_op_type_count, xc_get_op_type }, |
|---|
| 1168 | { "", xc_get_data_type_count, xc_get_data_type }, |
|---|
| 1169 | { "", xc_get_opcode_count, xc_get_opcode }, |
|---|
| 1170 | { "OPSPEC_", xc_get_op_spec_count, xc_get_op_spec }, |
|---|
| 1171 | { NULL, NULL, NULL } |
|---|
| 1172 | }; |
|---|
| 1173 | xc_meminfo_t* p; |
|---|
| 1174 | zend_uchar i, count; |
|---|
| 1175 | char const_name[96]; |
|---|
| 1176 | int const_name_len; |
|---|
| 1177 | int undefdone = 0; |
|---|
| 1178 | |
|---|
| 1179 | for (p = nameinfos; p->getsize; p ++) { |
|---|
| 1180 | count = p->getsize(); |
|---|
| 1181 | for (i = 0; i < count; i ++) { |
|---|
| 1182 | const char *name = p->get(i); |
|---|
| 1183 | if (!name) continue; |
|---|
| 1184 | if (strcmp(name, "UNDEF") == 0) { |
|---|
| 1185 | if (undefdone) continue; |
|---|
| 1186 | undefdone = 1; |
|---|
| 1187 | } |
|---|
| 1188 | const_name_len = snprintf(const_name, sizeof(const_name), "XC_%s%s", p->prefix, name); |
|---|
| 1189 | zend_register_long_constant(const_name, const_name_len+1, i, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 1190 | } |
|---|
| 1191 | } |
|---|
| 1192 | |
|---|
| 1193 | zend_register_long_constant(ZEND_STRS("XC_SIZEOF_TEMP_VARIABLE"), sizeof(temp_variable), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 1194 | zend_register_long_constant(ZEND_STRS("XC_TYPE_PHP"), XC_TYPE_PHP, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 1195 | zend_register_long_constant(ZEND_STRS("XC_TYPE_VAR"), XC_TYPE_VAR, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 1196 | zend_register_stringl_constant(ZEND_STRS("XCACHE_VERSION"), ZEND_STRL(XCACHE_VERSION), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 1197 | zend_register_stringl_constant(ZEND_STRS("XCACHE_MODULES"), ZEND_STRL(XCACHE_MODULES), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 1198 | return 0; |
|---|
| 1199 | } |
|---|
| 1200 | /* }}} */ |
|---|
| 1201 | static xc_shm_t *xc_cache_destroy(xc_cache_t **caches, xc_hash_t *hcache) /* {{{ */ |
|---|
| 1202 | { |
|---|
| 1203 | int i; |
|---|
| 1204 | xc_cache_t *cache; |
|---|
| 1205 | xc_shm_t *shm; |
|---|
| 1206 | |
|---|
| 1207 | if (!caches) { |
|---|
| 1208 | return NULL; |
|---|
| 1209 | } |
|---|
| 1210 | shm = NULL; |
|---|
| 1211 | for (i = 0; i < hcache->size; i ++) { |
|---|
| 1212 | cache = caches[i]; |
|---|
| 1213 | if (cache) { |
|---|
| 1214 | if (cache->lck) { |
|---|
| 1215 | xc_lock_destroy(cache->lck); |
|---|
| 1216 | } |
|---|
| 1217 | /* do NOT free |
|---|
| 1218 | if (cache->entries) { |
|---|
| 1219 | cache->mem->handlers->free(cache->mem, cache->entries); |
|---|
| 1220 | } |
|---|
| 1221 | cache->mem->handlers->free(cache->mem, cache); |
|---|
| 1222 | */ |
|---|
| 1223 | shm = cache->shm; |
|---|
| 1224 | shm->handlers->memdestroy(cache->mem); |
|---|
| 1225 | } |
|---|
| 1226 | } |
|---|
| 1227 | free(caches); |
|---|
| 1228 | return shm; |
|---|
| 1229 | } |
|---|
| 1230 | /* }}} */ |
|---|
| 1231 | static xc_cache_t **xc_cache_init(xc_shm_t *shm, xc_hash_t *hcache, xc_hash_t *hentry, xc_shmsize_t shmsize) /* {{{ */ |
|---|
| 1232 | { |
|---|
| 1233 | xc_cache_t **caches = NULL, *cache; |
|---|
| 1234 | xc_mem_t *mem; |
|---|
| 1235 | time_t now = time(NULL); |
|---|
| 1236 | int i; |
|---|
| 1237 | xc_memsize_t memsize; |
|---|
| 1238 | |
|---|
| 1239 | memsize = shmsize / hcache->size; |
|---|
| 1240 | |
|---|
| 1241 | /* Don't let it break out of mem after ALIGNed |
|---|
| 1242 | * This is important for |
|---|
| 1243 | * Simply loop until it fit our need |
|---|
| 1244 | */ |
|---|
| 1245 | while (ALIGN(memsize) * hcache->size > shmsize && ALIGN(memsize) != memsize) { |
|---|
| 1246 | if (memsize < ALIGN(1)) { |
|---|
| 1247 | CHECK(NULL, "cache too small"); |
|---|
| 1248 | } |
|---|
| 1249 | memsize --; |
|---|
| 1250 | } |
|---|
| 1251 | |
|---|
| 1252 | CHECK(caches = calloc(hcache->size, sizeof(xc_cache_t *)), "caches OOM"); |
|---|
| 1253 | |
|---|
| 1254 | for (i = 0; i < hcache->size; i ++) { |
|---|
| 1255 | CHECK(mem = shm->handlers->meminit(shm, memsize), "Failed init memory allocator"); |
|---|
| 1256 | CHECK(cache = mem->handlers->calloc(mem, 1, sizeof(xc_cache_t)), "cache OOM"); |
|---|
| 1257 | CHECK(cache->entries = mem->handlers->calloc(mem, hentry->size, sizeof(xc_entry_t*)), "entries OOM"); |
|---|
| 1258 | CHECK(cache->lck = xc_lock_init(NULL), "can't create lock"); |
|---|
| 1259 | |
|---|
| 1260 | cache->hcache = hcache; |
|---|
| 1261 | cache->hentry = hentry; |
|---|
| 1262 | cache->shm = shm; |
|---|
| 1263 | cache->mem = mem; |
|---|
| 1264 | cache->cacheid = i; |
|---|
| 1265 | cache->last_gc_deletes = now; |
|---|
| 1266 | cache->last_gc_expires = now; |
|---|
| 1267 | caches[i] = cache; |
|---|
| 1268 | } |
|---|
| 1269 | return caches; |
|---|
| 1270 | |
|---|
| 1271 | err: |
|---|
| 1272 | if (caches) { |
|---|
| 1273 | xc_cache_destroy(caches, hcache); |
|---|
| 1274 | } |
|---|
| 1275 | return NULL; |
|---|
| 1276 | } |
|---|
| 1277 | /* }}} */ |
|---|
| 1278 | static void xc_destroy() /* {{{ */ |
|---|
| 1279 | { |
|---|
| 1280 | xc_shm_t *shm = NULL; |
|---|
| 1281 | |
|---|
| 1282 | if (origin_compile_file) { |
|---|
| 1283 | zend_compile_file = origin_compile_file; |
|---|
| 1284 | origin_compile_file = NULL; |
|---|
| 1285 | } |
|---|
| 1286 | |
|---|
| 1287 | if (xc_php_caches) { |
|---|
| 1288 | shm = xc_cache_destroy(xc_php_caches, &xc_php_hcache); |
|---|
| 1289 | xc_php_caches = NULL; |
|---|
| 1290 | } |
|---|
| 1291 | if (xc_var_caches) { |
|---|
| 1292 | shm = xc_cache_destroy(xc_var_caches, &xc_var_hcache); |
|---|
| 1293 | xc_var_caches = NULL; |
|---|
| 1294 | } |
|---|
| 1295 | if (shm) { |
|---|
| 1296 | xc_shm_destroy(shm); |
|---|
| 1297 | } |
|---|
| 1298 | } |
|---|
| 1299 | /* }}} */ |
|---|
| 1300 | static int xc_init(int module_number TSRMLS_DC) /* {{{ */ |
|---|
| 1301 | { |
|---|
| 1302 | xc_shm_t *shm; |
|---|
| 1303 | |
|---|
| 1304 | xc_php_caches = xc_var_caches = NULL; |
|---|
| 1305 | |
|---|
| 1306 | if (xc_php_size || xc_var_size) { |
|---|
| 1307 | 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"); |
|---|
| 1308 | if (!shm->handlers->can_readonly(shm)) { |
|---|
| 1309 | xc_readonly_protection = 0; |
|---|
| 1310 | } |
|---|
| 1311 | |
|---|
| 1312 | if (xc_php_size) { |
|---|
| 1313 | origin_compile_file = zend_compile_file; |
|---|
| 1314 | zend_compile_file = xc_compile_file; |
|---|
| 1315 | |
|---|
| 1316 | CHECK(xc_php_caches = xc_cache_init(shm, &xc_php_hcache, &xc_php_hentry, xc_php_size), "failed init opcode cache"); |
|---|
| 1317 | } |
|---|
| 1318 | |
|---|
| 1319 | if (xc_var_size) { |
|---|
| 1320 | CHECK(xc_var_caches = xc_cache_init(shm, &xc_var_hcache, &xc_var_hentry, xc_var_size), "failed init variable cache"); |
|---|
| 1321 | } |
|---|
| 1322 | } |
|---|
| 1323 | return 1; |
|---|
| 1324 | |
|---|
| 1325 | err: |
|---|
| 1326 | if (xc_php_caches || xc_var_caches) { |
|---|
| 1327 | xc_destroy(); |
|---|
| 1328 | /* shm destroied */ |
|---|
| 1329 | } |
|---|
| 1330 | else if (shm) { |
|---|
| 1331 | xc_shm_destroy(shm); |
|---|
| 1332 | } |
|---|
| 1333 | return 0; |
|---|
| 1334 | } |
|---|
| 1335 | /* }}} */ |
|---|
| 1336 | static void xc_request_init(TSRMLS_D) /* {{{ */ |
|---|
| 1337 | { |
|---|
| 1338 | int i; |
|---|
| 1339 | |
|---|
| 1340 | if (xc_php_hcache.size && !XG(php_holds)) { |
|---|
| 1341 | XG(php_holds) = calloc(xc_php_hcache.size, sizeof(xc_stack_t)); |
|---|
| 1342 | for (i = 0; i < xc_php_hcache.size; i ++) { |
|---|
| 1343 | xc_stack_init(&XG(php_holds[i])); |
|---|
| 1344 | } |
|---|
| 1345 | } |
|---|
| 1346 | |
|---|
| 1347 | if (xc_var_hcache.size && !XG(var_holds)) { |
|---|
| 1348 | XG(var_holds) = calloc(xc_var_hcache.size, sizeof(xc_stack_t)); |
|---|
| 1349 | for (i = 0; i < xc_var_hcache.size; i ++) { |
|---|
| 1350 | xc_stack_init(&XG(var_holds[i])); |
|---|
| 1351 | } |
|---|
| 1352 | } |
|---|
| 1353 | |
|---|
| 1354 | if (XG(cacher)) { |
|---|
| 1355 | #if PHP_API_VERSION <= 20041225 |
|---|
| 1356 | XG(request_time) = time(NULL); |
|---|
| 1357 | #else |
|---|
| 1358 | XG(request_time) = sapi_get_request_time(TSRMLS_C); |
|---|
| 1359 | #endif |
|---|
| 1360 | } |
|---|
| 1361 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 1362 | xc_coverager_request_init(TSRMLS_C); |
|---|
| 1363 | #endif |
|---|
| 1364 | } |
|---|
| 1365 | /* }}} */ |
|---|
| 1366 | static void xc_request_shutdown(TSRMLS_D) /* {{{ */ |
|---|
| 1367 | { |
|---|
| 1368 | xc_entry_unholds(TSRMLS_C); |
|---|
| 1369 | xc_gc_expires_php(TSRMLS_C); |
|---|
| 1370 | xc_gc_expires_var(TSRMLS_C); |
|---|
| 1371 | xc_gc_deletes(TSRMLS_C); |
|---|
| 1372 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 1373 | xc_coverager_request_shutdown(TSRMLS_C); |
|---|
| 1374 | #endif |
|---|
| 1375 | } |
|---|
| 1376 | /* }}} */ |
|---|
| 1377 | /* {{{ PHP_GINIT_FUNCTION(xcache) */ |
|---|
| 1378 | static |
|---|
| 1379 | #ifdef PHP_GINIT_FUNCTION |
|---|
| 1380 | PHP_GINIT_FUNCTION(xcache) |
|---|
| 1381 | #else |
|---|
| 1382 | void xc_init_globals(zend_xcache_globals* xcache_globals TSRMLS_DC) |
|---|
| 1383 | #endif |
|---|
| 1384 | { |
|---|
| 1385 | memset(xcache_globals, 0, sizeof(zend_xcache_globals)); |
|---|
| 1386 | } |
|---|
| 1387 | /* }}} */ |
|---|
| 1388 | /* {{{ PHP_GSHUTDOWN_FUNCTION(xcache) */ |
|---|
| 1389 | static |
|---|
| 1390 | #ifdef PHP_GSHUTDOWN_FUNCTION |
|---|
| 1391 | PHP_GSHUTDOWN_FUNCTION(xcache) |
|---|
| 1392 | #else |
|---|
| 1393 | void xc_shutdown_globals(zend_xcache_globals* xcache_globals TSRMLS_DC) |
|---|
| 1394 | #endif |
|---|
| 1395 | { |
|---|
| 1396 | int i; |
|---|
| 1397 | |
|---|
| 1398 | if (xcache_globals->php_holds != NULL) { |
|---|
| 1399 | for (i = 0; i < xc_php_hcache.size; i ++) { |
|---|
| 1400 | xc_stack_destroy(&xcache_globals->php_holds[i]); |
|---|
| 1401 | } |
|---|
| 1402 | free(xcache_globals->php_holds); |
|---|
| 1403 | xcache_globals->php_holds = NULL; |
|---|
| 1404 | } |
|---|
| 1405 | |
|---|
| 1406 | if (xcache_globals->var_holds != NULL) { |
|---|
| 1407 | for (i = 0; i < xc_var_hcache.size; i ++) { |
|---|
| 1408 | xc_stack_destroy(&xcache_globals->var_holds[i]); |
|---|
| 1409 | } |
|---|
| 1410 | free(xcache_globals->var_holds); |
|---|
| 1411 | xcache_globals->var_holds = NULL; |
|---|
| 1412 | } |
|---|
| 1413 | } |
|---|
| 1414 | /* }}} */ |
|---|
| 1415 | |
|---|
| 1416 | /* user functions */ |
|---|
| 1417 | static int xcache_admin_auth_check(TSRMLS_D) /* {{{ */ |
|---|
| 1418 | { |
|---|
| 1419 | zval **server = NULL; |
|---|
| 1420 | zval **user = NULL; |
|---|
| 1421 | zval **pass = NULL; |
|---|
| 1422 | char *admin_user = NULL; |
|---|
| 1423 | char *admin_pass = NULL; |
|---|
| 1424 | HashTable *ht; |
|---|
| 1425 | |
|---|
| 1426 | if (cfg_get_string("xcache.admin.user", &admin_user) == FAILURE || !admin_user[0]) { |
|---|
| 1427 | admin_user = NULL; |
|---|
| 1428 | } |
|---|
| 1429 | if (cfg_get_string("xcache.admin.pass", &admin_pass) == FAILURE || !admin_pass[0]) { |
|---|
| 1430 | admin_pass = NULL; |
|---|
| 1431 | } |
|---|
| 1432 | |
|---|
| 1433 | if (admin_user == NULL || admin_pass == NULL) { |
|---|
| 1434 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "xcache.admin.user and xcache.admin.pass is required"); |
|---|
| 1435 | zend_bailout(); |
|---|
| 1436 | } |
|---|
| 1437 | if (strlen(admin_pass) != 32) { |
|---|
| 1438 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "unexpect %d bytes of xcache.admin.pass, expected 32 bytes, the password after md5()", strlen(admin_pass)); |
|---|
| 1439 | zend_bailout(); |
|---|
| 1440 | } |
|---|
| 1441 | |
|---|
| 1442 | #ifdef ZEND_ENGINE_2_1 |
|---|
| 1443 | zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1 TSRMLS_CC); |
|---|
| 1444 | #endif |
|---|
| 1445 | if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &server) != SUCCESS || Z_TYPE_PP(server) != IS_ARRAY) { |
|---|
| 1446 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "_SERVER is corrupted"); |
|---|
| 1447 | zend_bailout(); |
|---|
| 1448 | } |
|---|
| 1449 | ht = HASH_OF((*server)); |
|---|
| 1450 | |
|---|
| 1451 | if (zend_hash_find(ht, "PHP_AUTH_USER", sizeof("PHP_AUTH_USER"), (void **) &user) == FAILURE) { |
|---|
| 1452 | user = NULL; |
|---|
| 1453 | } |
|---|
| 1454 | else if (Z_TYPE_PP(user) != IS_STRING) { |
|---|
| 1455 | user = NULL; |
|---|
| 1456 | } |
|---|
| 1457 | |
|---|
| 1458 | if (zend_hash_find(ht, "PHP_AUTH_PW", sizeof("PHP_AUTH_PW"), (void **) &pass) == FAILURE) { |
|---|
| 1459 | pass = NULL; |
|---|
| 1460 | } |
|---|
| 1461 | else if (Z_TYPE_PP(pass) != IS_STRING) { |
|---|
| 1462 | pass = NULL; |
|---|
| 1463 | } |
|---|
| 1464 | |
|---|
| 1465 | if (user != NULL && pass != NULL && strcmp(admin_user, Z_STRVAL_PP(user)) == 0) { |
|---|
| 1466 | PHP_MD5_CTX context; |
|---|
| 1467 | char md5str[33]; |
|---|
| 1468 | unsigned char digest[16]; |
|---|
| 1469 | |
|---|
| 1470 | PHP_MD5Init(&context); |
|---|
| 1471 | PHP_MD5Update(&context, (unsigned char *) Z_STRVAL_PP(pass), Z_STRLEN_PP(pass)); |
|---|
| 1472 | PHP_MD5Final(digest, &context); |
|---|
| 1473 | |
|---|
| 1474 | md5str[0] = '\0'; |
|---|
| 1475 | make_digest(md5str, digest); |
|---|
| 1476 | if (strcmp(admin_pass, md5str) == 0) { |
|---|
| 1477 | return 1; |
|---|
| 1478 | } |
|---|
| 1479 | } |
|---|
| 1480 | |
|---|
| 1481 | #define STR "WWW-authenticate: basic realm='XCache Administration'" |
|---|
| 1482 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
|---|
| 1483 | #undef STR |
|---|
| 1484 | #define STR "HTTP/1.0 401 Unauthorized" |
|---|
| 1485 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
|---|
| 1486 | #undef STR |
|---|
| 1487 | ZEND_PUTS("XCache Auth Failed. User and Password is case sense\n"); |
|---|
| 1488 | |
|---|
| 1489 | zend_bailout(); |
|---|
| 1490 | return 0; |
|---|
| 1491 | } |
|---|
| 1492 | /* }}} */ |
|---|
| 1493 | /* {{{ xcache_admin_operate */ |
|---|
| 1494 | typedef enum { XC_OP_COUNT, XC_OP_INFO, XC_OP_LIST, XC_OP_CLEAR } xcache_op_type; |
|---|
| 1495 | static void xcache_admin_operate(xcache_op_type optype, INTERNAL_FUNCTION_PARAMETERS) |
|---|
| 1496 | { |
|---|
| 1497 | long type; |
|---|
| 1498 | int size; |
|---|
| 1499 | xc_cache_t **caches, *cache; |
|---|
| 1500 | long id = 0; |
|---|
| 1501 | |
|---|
| 1502 | if (!xc_initized) { |
|---|
| 1503 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "XCache is not initized"); |
|---|
| 1504 | RETURN_FALSE; |
|---|
| 1505 | } |
|---|
| 1506 | |
|---|
| 1507 | xcache_admin_auth_check(TSRMLS_C); |
|---|
| 1508 | |
|---|
| 1509 | if (optype == XC_OP_COUNT) { |
|---|
| 1510 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type) == FAILURE) { |
|---|
| 1511 | return; |
|---|
| 1512 | } |
|---|
| 1513 | } |
|---|
| 1514 | else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &type, &id) == FAILURE) { |
|---|
| 1515 | return; |
|---|
| 1516 | } |
|---|
| 1517 | |
|---|
| 1518 | switch (type) { |
|---|
| 1519 | case XC_TYPE_PHP: |
|---|
| 1520 | size = xc_php_hcache.size; |
|---|
| 1521 | caches = xc_php_caches; |
|---|
| 1522 | break; |
|---|
| 1523 | |
|---|
| 1524 | case XC_TYPE_VAR: |
|---|
| 1525 | size = xc_var_hcache.size; |
|---|
| 1526 | caches = xc_var_caches; |
|---|
| 1527 | break; |
|---|
| 1528 | |
|---|
| 1529 | default: |
|---|
| 1530 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown type %ld", type); |
|---|
| 1531 | RETURN_FALSE; |
|---|
| 1532 | } |
|---|
| 1533 | |
|---|
| 1534 | switch (optype) { |
|---|
| 1535 | case XC_OP_COUNT: |
|---|
| 1536 | RETURN_LONG(size) |
|---|
| 1537 | break; |
|---|
| 1538 | |
|---|
| 1539 | case XC_OP_INFO: |
|---|
| 1540 | case XC_OP_LIST: |
|---|
| 1541 | if (id < 0 || id >= size) { |
|---|
| 1542 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists"); |
|---|
| 1543 | RETURN_FALSE; |
|---|
| 1544 | } |
|---|
| 1545 | |
|---|
| 1546 | array_init(return_value); |
|---|
| 1547 | |
|---|
| 1548 | cache = caches[id]; |
|---|
| 1549 | ENTER_LOCK(cache) { |
|---|
| 1550 | if (optype == XC_OP_INFO) { |
|---|
| 1551 | xc_fillinfo_dmz(type, cache, return_value TSRMLS_CC); |
|---|
| 1552 | } |
|---|
| 1553 | else { |
|---|
| 1554 | xc_filllist_dmz(cache, return_value TSRMLS_CC); |
|---|
| 1555 | } |
|---|
| 1556 | } LEAVE_LOCK(cache); |
|---|
| 1557 | break; |
|---|
| 1558 | case XC_OP_CLEAR: |
|---|
| 1559 | { |
|---|
| 1560 | xc_entry_t *e, *next; |
|---|
| 1561 | int i, c; |
|---|
| 1562 | |
|---|
| 1563 | if (id < 0 || id >= size) { |
|---|
| 1564 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists"); |
|---|
| 1565 | RETURN_FALSE; |
|---|
| 1566 | } |
|---|
| 1567 | |
|---|
| 1568 | cache = caches[id]; |
|---|
| 1569 | ENTER_LOCK(cache) { |
|---|
| 1570 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
|---|
| 1571 | for (e = cache->entries[i]; e; e = next) { |
|---|
| 1572 | next = e->next; |
|---|
| 1573 | xc_entry_remove_dmz(e TSRMLS_CC); |
|---|
| 1574 | } |
|---|
| 1575 | cache->entries[i] = NULL; |
|---|
| 1576 | } |
|---|
| 1577 | } LEAVE_LOCK(cache); |
|---|
| 1578 | xc_gc_deletes(TSRMLS_C); |
|---|
| 1579 | } |
|---|
| 1580 | break; |
|---|
| 1581 | |
|---|
| 1582 | default: |
|---|
| 1583 | assert(0); |
|---|
| 1584 | } |
|---|
| 1585 | } |
|---|
| 1586 | /* }}} */ |
|---|
| 1587 | /* {{{ proto int xcache_count(int type) |
|---|
| 1588 | Return count of cache on specified cache type */ |
|---|
| 1589 | PHP_FUNCTION(xcache_count) |
|---|
| 1590 | { |
|---|
| 1591 | xcache_admin_operate(XC_OP_COUNT, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1592 | } |
|---|
| 1593 | /* }}} */ |
|---|
| 1594 | /* {{{ proto array xcache_info(int type, int id) |
|---|
| 1595 | Get cache info by id on specified cache type */ |
|---|
| 1596 | PHP_FUNCTION(xcache_info) |
|---|
| 1597 | { |
|---|
| 1598 | xcache_admin_operate(XC_OP_INFO, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1599 | } |
|---|
| 1600 | /* }}} */ |
|---|
| 1601 | /* {{{ proto array xcache_list(int type, int id) |
|---|
| 1602 | Get cache entries list by id on specified cache type */ |
|---|
| 1603 | PHP_FUNCTION(xcache_list) |
|---|
| 1604 | { |
|---|
| 1605 | xcache_admin_operate(XC_OP_LIST, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1606 | } |
|---|
| 1607 | /* }}} */ |
|---|
| 1608 | /* {{{ proto array xcache_clear_cache(int type, int id) |
|---|
| 1609 | Clear cache by id on specified cache type */ |
|---|
| 1610 | PHP_FUNCTION(xcache_clear_cache) |
|---|
| 1611 | { |
|---|
| 1612 | xcache_admin_operate(XC_OP_CLEAR, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1613 | } |
|---|
| 1614 | /* }}} */ |
|---|
| 1615 | |
|---|
| 1616 | static int xc_entry_init_key_var(xc_entry_t *xce, zval *name TSRMLS_DC) /* {{{ */ |
|---|
| 1617 | { |
|---|
| 1618 | xc_hash_value_t hv; |
|---|
| 1619 | int cacheid; |
|---|
| 1620 | |
|---|
| 1621 | switch (Z_TYPE_P(name)) { |
|---|
| 1622 | #ifdef IS_UNICODE |
|---|
| 1623 | case IS_UNICODE: |
|---|
| 1624 | #endif |
|---|
| 1625 | case IS_STRING: |
|---|
| 1626 | break; |
|---|
| 1627 | default: |
|---|
| 1628 | #ifdef IS_UNICODE |
|---|
| 1629 | convert_to_text(name); |
|---|
| 1630 | #else |
|---|
| 1631 | convert_to_string(name); |
|---|
| 1632 | #endif |
|---|
| 1633 | } |
|---|
| 1634 | #ifdef IS_UNICODE |
|---|
| 1635 | xce->name_type = name->type; |
|---|
| 1636 | #endif |
|---|
| 1637 | xce->name = name->value; |
|---|
| 1638 | |
|---|
| 1639 | hv = xc_entry_hash_var(xce TSRMLS_CC); |
|---|
| 1640 | |
|---|
| 1641 | cacheid = (hv & xc_var_hcache.mask); |
|---|
| 1642 | xce->cache = xc_var_caches[cacheid]; |
|---|
| 1643 | hv >>= xc_var_hcache.bits; |
|---|
| 1644 | xce->hvalue = (hv & xc_var_hentry.mask); |
|---|
| 1645 | |
|---|
| 1646 | xce->type = XC_TYPE_VAR; |
|---|
| 1647 | return SUCCESS; |
|---|
| 1648 | } |
|---|
| 1649 | /* }}} */ |
|---|
| 1650 | /* {{{ proto mixed xcache_get(string name) |
|---|
| 1651 | Get cached data by specified name */ |
|---|
| 1652 | PHP_FUNCTION(xcache_get) |
|---|
| 1653 | { |
|---|
| 1654 | xc_entry_t xce, *stored_xce; |
|---|
| 1655 | xc_entry_data_var_t var; |
|---|
| 1656 | zval *name; |
|---|
| 1657 | |
|---|
| 1658 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
|---|
| 1659 | return; |
|---|
| 1660 | } |
|---|
| 1661 | xce.data.var = &var; |
|---|
| 1662 | xc_entry_init_key_var(&xce, name TSRMLS_CC); |
|---|
| 1663 | |
|---|
| 1664 | ENTER_LOCK(xce.cache) { |
|---|
| 1665 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 1666 | if (stored_xce) { |
|---|
| 1667 | if (XG(request_time) <= stored_xce->ctime + stored_xce->ttl) { |
|---|
| 1668 | xc_processor_restore_zval(return_value, stored_xce->data.var->value TSRMLS_CC); |
|---|
| 1669 | /* return */ |
|---|
| 1670 | break; |
|---|
| 1671 | } |
|---|
| 1672 | else { |
|---|
| 1673 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| 1674 | } |
|---|
| 1675 | } |
|---|
| 1676 | |
|---|
| 1677 | RETVAL_NULL(); |
|---|
| 1678 | } LEAVE_LOCK(xce.cache); |
|---|
| 1679 | } |
|---|
| 1680 | /* }}} */ |
|---|
| 1681 | /* {{{ proto bool xcache_set(string name, mixed value [, int ttl]) |
|---|
| 1682 | Store data to cache by specified name */ |
|---|
| 1683 | PHP_FUNCTION(xcache_set) |
|---|
| 1684 | { |
|---|
| 1685 | xc_entry_t xce, *stored_xce; |
|---|
| 1686 | xc_entry_data_var_t var; |
|---|
| 1687 | zval *name; |
|---|
| 1688 | zval *value; |
|---|
| 1689 | |
|---|
| 1690 | xce.ttl = XG(var_ttl); |
|---|
| 1691 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &name, &value, &xce.ttl) == FAILURE) { |
|---|
| 1692 | return; |
|---|
| 1693 | } |
|---|
| 1694 | |
|---|
| 1695 | /* max ttl */ |
|---|
| 1696 | if (xc_var_maxttl && (!xce.ttl || xce.ttl > xc_var_maxttl)) { |
|---|
| 1697 | xce.ttl = xc_var_maxttl; |
|---|
| 1698 | } |
|---|
| 1699 | |
|---|
| 1700 | xce.data.var = &var; |
|---|
| 1701 | xc_entry_init_key_var(&xce, name TSRMLS_CC); |
|---|
| 1702 | |
|---|
| 1703 | ENTER_LOCK(xce.cache) { |
|---|
| 1704 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 1705 | if (stored_xce) { |
|---|
| 1706 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| 1707 | } |
|---|
| 1708 | var.value = value; |
|---|
| 1709 | RETVAL_BOOL(xc_entry_store_dmz(&xce TSRMLS_CC) != NULL ? 1 : 0); |
|---|
| 1710 | } LEAVE_LOCK(xce.cache); |
|---|
| 1711 | } |
|---|
| 1712 | /* }}} */ |
|---|
| 1713 | /* {{{ proto bool xcache_isset(string name) |
|---|
| 1714 | Check if an entry exists in cache by specified name */ |
|---|
| 1715 | PHP_FUNCTION(xcache_isset) |
|---|
| 1716 | { |
|---|
| 1717 | xc_entry_t xce, *stored_xce; |
|---|
| 1718 | xc_entry_data_var_t var; |
|---|
| 1719 | zval *name; |
|---|
| 1720 | |
|---|
| 1721 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
|---|
| 1722 | return; |
|---|
| 1723 | } |
|---|
| 1724 | xce.data.var = &var; |
|---|
| 1725 | xc_entry_init_key_var(&xce, name TSRMLS_CC); |
|---|
| 1726 | |
|---|
| 1727 | ENTER_LOCK(xce.cache) { |
|---|
| 1728 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 1729 | if (stored_xce) { |
|---|
| 1730 | if (!VAR_ENTRY_EXPIRED(stored_xce)) { |
|---|
| 1731 | RETVAL_TRUE; |
|---|
| 1732 | /* return */ |
|---|
| 1733 | break; |
|---|
| 1734 | } |
|---|
| 1735 | else { |
|---|
| 1736 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| 1737 | } |
|---|
| 1738 | } |
|---|
| 1739 | |
|---|
| 1740 | RETVAL_FALSE; |
|---|
| 1741 | } LEAVE_LOCK(xce.cache); |
|---|
| 1742 | } |
|---|
| 1743 | /* }}} */ |
|---|
| 1744 | /* {{{ proto bool xcache_unset(string name) |
|---|
| 1745 | Unset existing data in cache by specified name */ |
|---|
| 1746 | PHP_FUNCTION(xcache_unset) |
|---|
| 1747 | { |
|---|
| 1748 | xc_entry_t xce, *stored_xce; |
|---|
| 1749 | xc_entry_data_var_t var; |
|---|
| 1750 | zval *name; |
|---|
| 1751 | |
|---|
| 1752 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
|---|
| 1753 | return; |
|---|
| 1754 | } |
|---|
| 1755 | xce.data.var = &var; |
|---|
| 1756 | xc_entry_init_key_var(&xce, name TSRMLS_CC); |
|---|
| 1757 | |
|---|
| 1758 | ENTER_LOCK(xce.cache) { |
|---|
| 1759 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 1760 | if (stored_xce) { |
|---|
| 1761 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| 1762 | RETVAL_TRUE; |
|---|
| 1763 | } |
|---|
| 1764 | else { |
|---|
| 1765 | RETVAL_FALSE; |
|---|
| 1766 | } |
|---|
| 1767 | } LEAVE_LOCK(xce.cache); |
|---|
| 1768 | } |
|---|
| 1769 | /* }}} */ |
|---|
| 1770 | static inline void xc_var_inc_dec(int inc, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */ |
|---|
| 1771 | { |
|---|
| 1772 | xc_entry_t xce, *stored_xce; |
|---|
| 1773 | xc_entry_data_var_t var, *stored_var; |
|---|
| 1774 | zval *name; |
|---|
| 1775 | long count = 1; |
|---|
| 1776 | long value = 0; |
|---|
| 1777 | zval oldzval; |
|---|
| 1778 | |
|---|
| 1779 | xce.ttl = XG(var_ttl); |
|---|
| 1780 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", &name, &count, &xce.ttl) == FAILURE) { |
|---|
| 1781 | return; |
|---|
| 1782 | } |
|---|
| 1783 | |
|---|
| 1784 | /* max ttl */ |
|---|
| 1785 | if (xc_var_maxttl && (!xce.ttl || xce.ttl > xc_var_maxttl)) { |
|---|
| 1786 | xce.ttl = xc_var_maxttl; |
|---|
| 1787 | } |
|---|
| 1788 | |
|---|
| 1789 | xce.data.var = &var; |
|---|
| 1790 | xc_entry_init_key_var(&xce, name TSRMLS_CC); |
|---|
| 1791 | |
|---|
| 1792 | ENTER_LOCK(xce.cache) { |
|---|
| 1793 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 1794 | if (stored_xce) { |
|---|
| 1795 | #ifdef DEBUG |
|---|
| 1796 | fprintf(stderr, "incdec: gotxce %s\n", xce.name.str.val); |
|---|
| 1797 | #endif |
|---|
| 1798 | /* timeout */ |
|---|
| 1799 | if (VAR_ENTRY_EXPIRED(stored_xce)) { |
|---|
| 1800 | #ifdef DEBUG |
|---|
| 1801 | fprintf(stderr, "incdec: expired\n"); |
|---|
| 1802 | #endif |
|---|
| 1803 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| 1804 | stored_xce = NULL; |
|---|
| 1805 | } |
|---|
| 1806 | else { |
|---|
| 1807 | /* do it in place */ |
|---|
| 1808 | stored_var = stored_xce->data.var; |
|---|
| 1809 | if (Z_TYPE_P(stored_var->value) == IS_LONG) { |
|---|
| 1810 | stored_xce->ctime = XG(request_time); |
|---|
| 1811 | stored_xce->ttl = xce.ttl; |
|---|
| 1812 | #ifdef DEBUG |
|---|
| 1813 | fprintf(stderr, "incdec: islong\n"); |
|---|
| 1814 | #endif |
|---|
| 1815 | value = Z_LVAL_P(stored_var->value); |
|---|
| 1816 | value += (inc == 1 ? count : - count); |
|---|
| 1817 | RETVAL_LONG(value); |
|---|
| 1818 | Z_LVAL_P(stored_var->value) = value; |
|---|
| 1819 | break; /* leave lock */ |
|---|
| 1820 | } |
|---|
| 1821 | else { |
|---|
| 1822 | #ifdef DEBUG |
|---|
| 1823 | fprintf(stderr, "incdec: notlong\n"); |
|---|
| 1824 | #endif |
|---|
| 1825 | xc_processor_restore_zval(&oldzval, stored_xce->data.var->value TSRMLS_CC); |
|---|
| 1826 | convert_to_long(&oldzval); |
|---|
| 1827 | value = Z_LVAL(oldzval); |
|---|
| 1828 | zval_dtor(&oldzval); |
|---|
| 1829 | } |
|---|
| 1830 | } |
|---|
| 1831 | } |
|---|
| 1832 | #ifdef DEBUG |
|---|
| 1833 | else { |
|---|
| 1834 | fprintf(stderr, "incdec: %s not found\n", xce.name.str.val); |
|---|
| 1835 | } |
|---|
| 1836 | #endif |
|---|
| 1837 | |
|---|
| 1838 | value += (inc == 1 ? count : - count); |
|---|
| 1839 | RETVAL_LONG(value); |
|---|
| 1840 | var.value = return_value; |
|---|
| 1841 | |
|---|
| 1842 | if (stored_xce) { |
|---|
| 1843 | xce.atime = stored_xce->atime; |
|---|
| 1844 | xce.ctime = stored_xce->ctime; |
|---|
| 1845 | xce.hits = stored_xce->hits; |
|---|
| 1846 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| 1847 | } |
|---|
| 1848 | xc_entry_store_dmz(&xce TSRMLS_CC); |
|---|
| 1849 | |
|---|
| 1850 | } LEAVE_LOCK(xce.cache); |
|---|
| 1851 | } |
|---|
| 1852 | /* }}} */ |
|---|
| 1853 | /* {{{ proto int xcache_inc(string name [, int value [, int ttl]]) |
|---|
| 1854 | Increase an int counter in cache by specified name, create it if not exists */ |
|---|
| 1855 | PHP_FUNCTION(xcache_inc) |
|---|
| 1856 | { |
|---|
| 1857 | xc_var_inc_dec(1, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1858 | } |
|---|
| 1859 | /* }}} */ |
|---|
| 1860 | /* {{{ proto int xcache_dec(string name [, int value [, int ttl]]) |
|---|
| 1861 | Decrease an int counter in cache by specified name, create it if not exists */ |
|---|
| 1862 | PHP_FUNCTION(xcache_dec) |
|---|
| 1863 | { |
|---|
| 1864 | xc_var_inc_dec(-1, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1865 | } |
|---|
| 1866 | /* }}} */ |
|---|
| 1867 | /* {{{ proto string xcache_asm(string filename) |
|---|
| 1868 | */ |
|---|
| 1869 | #ifdef HAVE_XCACHE_ASSEMBLER |
|---|
| 1870 | PHP_FUNCTION(xcache_asm) |
|---|
| 1871 | { |
|---|
| 1872 | } |
|---|
| 1873 | #endif |
|---|
| 1874 | /* }}} */ |
|---|
| 1875 | #ifdef HAVE_XCACHE_DISASSEMBLER |
|---|
| 1876 | /* {{{ proto array xcache_dasm_file(string filename) |
|---|
| 1877 | Disassemble file into opcode array by filename */ |
|---|
| 1878 | PHP_FUNCTION(xcache_dasm_file) |
|---|
| 1879 | { |
|---|
| 1880 | char *filename; |
|---|
| 1881 | int filename_len; |
|---|
| 1882 | |
|---|
| 1883 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) { |
|---|
| 1884 | return; |
|---|
| 1885 | } |
|---|
| 1886 | if (!filename_len) RETURN_FALSE; |
|---|
| 1887 | |
|---|
| 1888 | xc_dasm_file(return_value, filename TSRMLS_CC); |
|---|
| 1889 | } |
|---|
| 1890 | /* }}} */ |
|---|
| 1891 | /* {{{ proto array xcache_dasm_string(string code) |
|---|
| 1892 | Disassemble php code into opcode array */ |
|---|
| 1893 | PHP_FUNCTION(xcache_dasm_string) |
|---|
| 1894 | { |
|---|
| 1895 | zval *code; |
|---|
| 1896 | |
|---|
| 1897 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &code) == FAILURE) { |
|---|
| 1898 | return; |
|---|
| 1899 | } |
|---|
| 1900 | xc_dasm_string(return_value, code TSRMLS_CC); |
|---|
| 1901 | } |
|---|
| 1902 | /* }}} */ |
|---|
| 1903 | #endif |
|---|
| 1904 | /* {{{ proto string xcache_encode(string filename) |
|---|
| 1905 | Encode php file into XCache opcode encoded format */ |
|---|
| 1906 | #ifdef HAVE_XCACHE_ENCODER |
|---|
| 1907 | PHP_FUNCTION(xcache_encode) |
|---|
| 1908 | { |
|---|
| 1909 | } |
|---|
| 1910 | #endif |
|---|
| 1911 | /* }}} */ |
|---|
| 1912 | /* {{{ proto bool xcache_decode_file(string filename) |
|---|
| 1913 | Decode(load) opcode from XCache encoded format file */ |
|---|
| 1914 | #ifdef HAVE_XCACHE_DECODER |
|---|
| 1915 | PHP_FUNCTION(xcache_decode_file) |
|---|
| 1916 | { |
|---|
| 1917 | } |
|---|
| 1918 | #endif |
|---|
| 1919 | /* }}} */ |
|---|
| 1920 | /* {{{ proto bool xcache_decode_string(string data) |
|---|
| 1921 | Decode(load) opcode from XCache encoded format data */ |
|---|
| 1922 | #ifdef HAVE_XCACHE_DECODER |
|---|
| 1923 | PHP_FUNCTION(xcache_decode_string) |
|---|
| 1924 | { |
|---|
| 1925 | } |
|---|
| 1926 | #endif |
|---|
| 1927 | /* }}} */ |
|---|
| 1928 | /* {{{ xc_call_getter */ |
|---|
| 1929 | typedef const char *(xc_name_getter_t)(zend_uchar type); |
|---|
| 1930 | static void xc_call_getter(xc_name_getter_t getter, int count, INTERNAL_FUNCTION_PARAMETERS) |
|---|
| 1931 | { |
|---|
| 1932 | long spec; |
|---|
| 1933 | const char *name; |
|---|
| 1934 | |
|---|
| 1935 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &spec) == FAILURE) { |
|---|
| 1936 | return; |
|---|
| 1937 | } |
|---|
| 1938 | if (spec >= 0 && spec < count) { |
|---|
| 1939 | name = getter((zend_uchar) spec); |
|---|
| 1940 | if (name) { |
|---|
| 1941 | /* RETURN_STRING */ |
|---|
| 1942 | int len = strlen(name); |
|---|
| 1943 | return_value->value.str.len = len; |
|---|
| 1944 | return_value->value.str.val = estrndup(name, len); |
|---|
| 1945 | return_value->type = IS_STRING; |
|---|
| 1946 | return; |
|---|
| 1947 | } |
|---|
| 1948 | } |
|---|
| 1949 | RETURN_NULL(); |
|---|
| 1950 | } |
|---|
| 1951 | /* }}} */ |
|---|
| 1952 | /* {{{ proto string xcache_get_op_type(int op_type) */ |
|---|
| 1953 | PHP_FUNCTION(xcache_get_op_type) |
|---|
| 1954 | { |
|---|
| 1955 | xc_call_getter(xc_get_op_type, xc_get_op_type_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1956 | } |
|---|
| 1957 | /* }}} */ |
|---|
| 1958 | /* {{{ proto string xcache_get_data_type(int type) */ |
|---|
| 1959 | PHP_FUNCTION(xcache_get_data_type) |
|---|
| 1960 | { |
|---|
| 1961 | xc_call_getter(xc_get_data_type, xc_get_data_type_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1962 | } |
|---|
| 1963 | /* }}} */ |
|---|
| 1964 | /* {{{ proto string xcache_get_opcode(int opcode) */ |
|---|
| 1965 | PHP_FUNCTION(xcache_get_opcode) |
|---|
| 1966 | { |
|---|
| 1967 | xc_call_getter(xc_get_opcode, xc_get_opcode_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1968 | } |
|---|
| 1969 | /* }}} */ |
|---|
| 1970 | /* {{{ proto string xcache_get_op_spec(int op_type) */ |
|---|
| 1971 | PHP_FUNCTION(xcache_get_op_spec) |
|---|
| 1972 | { |
|---|
| 1973 | xc_call_getter(xc_get_op_spec, xc_get_op_spec_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1974 | } |
|---|
| 1975 | /* }}} */ |
|---|
| 1976 | #ifdef HAVE_XCACHE_OPCODE_SPEC_DEF |
|---|
| 1977 | /* {{{ proto string xcache_get_opcode_spec(int opcode) */ |
|---|
| 1978 | PHP_FUNCTION(xcache_get_opcode_spec) |
|---|
| 1979 | { |
|---|
| 1980 | long spec; |
|---|
| 1981 | const xc_opcode_spec_t *opspec; |
|---|
| 1982 | |
|---|
| 1983 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &spec) == FAILURE) { |
|---|
| 1984 | return; |
|---|
| 1985 | } |
|---|
| 1986 | if ((zend_uchar) spec <= xc_get_opcode_spec_count()) { |
|---|
| 1987 | opspec = xc_get_opcode_spec((zend_uchar) spec); |
|---|
| 1988 | if (opspec) { |
|---|
| 1989 | array_init(return_value); |
|---|
| 1990 | add_assoc_long_ex(return_value, ZEND_STRS("ext"), opspec->ext); |
|---|
| 1991 | add_assoc_long_ex(return_value, ZEND_STRS("op1"), opspec->op1); |
|---|
| 1992 | add_assoc_long_ex(return_value, ZEND_STRS("op2"), opspec->op2); |
|---|
| 1993 | add_assoc_long_ex(return_value, ZEND_STRS("res"), opspec->res); |
|---|
| 1994 | return; |
|---|
| 1995 | } |
|---|
| 1996 | } |
|---|
| 1997 | RETURN_NULL(); |
|---|
| 1998 | } |
|---|
| 1999 | /* }}} */ |
|---|
| 2000 | #endif |
|---|
| 2001 | /* {{{ proto mixed xcache_get_special_value(zval value) */ |
|---|
| 2002 | PHP_FUNCTION(xcache_get_special_value) |
|---|
| 2003 | { |
|---|
| 2004 | zval *value; |
|---|
| 2005 | |
|---|
| 2006 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) { |
|---|
| 2007 | return; |
|---|
| 2008 | } |
|---|
| 2009 | |
|---|
| 2010 | if (value->type == IS_CONSTANT) { |
|---|
| 2011 | *return_value = *value; |
|---|
| 2012 | zval_copy_ctor(return_value); |
|---|
| 2013 | return_value->type = UNISW(IS_STRING, UG(unicode) ? IS_UNICODE : IS_STRING); |
|---|
| 2014 | return; |
|---|
| 2015 | } |
|---|
| 2016 | |
|---|
| 2017 | if (value->type == IS_CONSTANT_ARRAY) { |
|---|
| 2018 | *return_value = *value; |
|---|
| 2019 | zval_copy_ctor(return_value); |
|---|
| 2020 | return_value->type = IS_ARRAY; |
|---|
| 2021 | return; |
|---|
| 2022 | } |
|---|
| 2023 | |
|---|
| 2024 | RETURN_NULL(); |
|---|
| 2025 | } |
|---|
| 2026 | /* }}} */ |
|---|
| 2027 | /* {{{ proto string xcache_coredump(int op_type) */ |
|---|
| 2028 | PHP_FUNCTION(xcache_coredump) |
|---|
| 2029 | { |
|---|
| 2030 | if (xc_test) { |
|---|
| 2031 | raise(SIGSEGV); |
|---|
| 2032 | } |
|---|
| 2033 | else { |
|---|
| 2034 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "xcache.test must be enabled to test xcache_coredump()"); |
|---|
| 2035 | } |
|---|
| 2036 | } |
|---|
| 2037 | /* }}} */ |
|---|
| 2038 | /* {{{ proto string xcache_is_autoglobal(string name) */ |
|---|
| 2039 | PHP_FUNCTION(xcache_is_autoglobal) |
|---|
| 2040 | { |
|---|
| 2041 | char *name; |
|---|
| 2042 | int name_len; |
|---|
| 2043 | |
|---|
| 2044 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { |
|---|
| 2045 | return; |
|---|
| 2046 | } |
|---|
| 2047 | |
|---|
| 2048 | RETURN_BOOL(zend_hash_exists(CG(auto_globals), name, name_len + 1)); |
|---|
| 2049 | } |
|---|
| 2050 | /* }}} */ |
|---|
| 2051 | static function_entry xcache_functions[] = /* {{{ */ |
|---|
| 2052 | { |
|---|
| 2053 | PHP_FE(xcache_count, NULL) |
|---|
| 2054 | PHP_FE(xcache_info, NULL) |
|---|
| 2055 | PHP_FE(xcache_list, NULL) |
|---|
| 2056 | PHP_FE(xcache_clear_cache, NULL) |
|---|
| 2057 | PHP_FE(xcache_coredump, NULL) |
|---|
| 2058 | #ifdef HAVE_XCACHE_ASSEMBLER |
|---|
| 2059 | PHP_FE(xcache_asm, NULL) |
|---|
| 2060 | #endif |
|---|
| 2061 | #ifdef HAVE_XCACHE_DISASSEMBLER |
|---|
| 2062 | PHP_FE(xcache_dasm_file, NULL) |
|---|
| 2063 | PHP_FE(xcache_dasm_string, NULL) |
|---|
| 2064 | #endif |
|---|
| 2065 | #ifdef HAVE_XCACHE_ENCODER |
|---|
| 2066 | PHP_FE(xcache_encode, NULL) |
|---|
| 2067 | #endif |
|---|
| 2068 | #ifdef HAVE_XCACHE_DECODER |
|---|
| 2069 | PHP_FE(xcache_decode_file, NULL) |
|---|
| 2070 | PHP_FE(xcache_decode_string, NULL) |
|---|
| 2071 | #endif |
|---|
| 2072 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 2073 | PHP_FE(xcache_coverager_decode, NULL) |
|---|
| 2074 | PHP_FE(xcache_coverager_start, NULL) |
|---|
| 2075 | PHP_FE(xcache_coverager_stop, NULL) |
|---|
| 2076 | PHP_FE(xcache_coverager_get, NULL) |
|---|
| 2077 | #endif |
|---|
| 2078 | PHP_FE(xcache_get_special_value, NULL) |
|---|
| 2079 | PHP_FE(xcache_get_op_type, NULL) |
|---|
| 2080 | PHP_FE(xcache_get_data_type, NULL) |
|---|
| 2081 | PHP_FE(xcache_get_opcode, NULL) |
|---|
| 2082 | #ifdef HAVE_XCACHE_OPCODE_SPEC_DEF |
|---|
| 2083 | PHP_FE(xcache_get_opcode_spec, NULL) |
|---|
| 2084 | #endif |
|---|
| 2085 | PHP_FE(xcache_is_autoglobal, NULL) |
|---|
| 2086 | PHP_FE(xcache_inc, NULL) |
|---|
| 2087 | PHP_FE(xcache_dec, NULL) |
|---|
| 2088 | PHP_FE(xcache_get, NULL) |
|---|
| 2089 | PHP_FE(xcache_set, NULL) |
|---|
| 2090 | PHP_FE(xcache_isset, NULL) |
|---|
| 2091 | PHP_FE(xcache_unset, NULL) |
|---|
| 2092 | {NULL, NULL, NULL} |
|---|
| 2093 | }; |
|---|
| 2094 | /* }}} */ |
|---|
| 2095 | |
|---|
| 2096 | /* old signal handlers {{{ */ |
|---|
| 2097 | typedef void (*xc_sighandler_t)(int); |
|---|
| 2098 | #define FOREACH_SIG(sig) static xc_sighandler_t old_##sig##_handler = NULL |
|---|
| 2099 | #include "foreachcoresig.h" |
|---|
| 2100 | #undef FOREACH_SIG |
|---|
| 2101 | /* }}} */ |
|---|
| 2102 | static void xcache_signal_handler(int sig); |
|---|
| 2103 | static void xcache_restore_signal_handler() /* {{{ */ |
|---|
| 2104 | { |
|---|
| 2105 | #define FOREACH_SIG(sig) do { \ |
|---|
| 2106 | if (old_##sig##_handler != xcache_signal_handler) { \ |
|---|
| 2107 | signal(sig, old_##sig##_handler); \ |
|---|
| 2108 | } \ |
|---|
| 2109 | else { \ |
|---|
| 2110 | signal(sig, SIG_DFL); \ |
|---|
| 2111 | } \ |
|---|
| 2112 | } while (0) |
|---|
| 2113 | #include "foreachcoresig.h" |
|---|
| 2114 | #undef FOREACH_SIG |
|---|
| 2115 | } |
|---|
| 2116 | /* }}} */ |
|---|
| 2117 | static void xcache_init_signal_handler() /* {{{ */ |
|---|
| 2118 | { |
|---|
| 2119 | #define FOREACH_SIG(sig) \ |
|---|
| 2120 | old_##sig##_handler = signal(sig, xcache_signal_handler) |
|---|
| 2121 | #include "foreachcoresig.h" |
|---|
| 2122 | #undef FOREACH_SIG |
|---|
| 2123 | } |
|---|
| 2124 | /* }}} */ |
|---|
| 2125 | static void xcache_signal_handler(int sig) /* {{{ */ |
|---|
| 2126 | { |
|---|
| 2127 | xcache_restore_signal_handler(); |
|---|
| 2128 | if (xc_coredump_dir && xc_coredump_dir[0]) { |
|---|
| 2129 | chdir(xc_coredump_dir); |
|---|
| 2130 | } |
|---|
| 2131 | raise(sig); |
|---|
| 2132 | } |
|---|
| 2133 | /* }}} */ |
|---|
| 2134 | |
|---|
| 2135 | /* {{{ PHP_INI */ |
|---|
| 2136 | |
|---|
| 2137 | static PHP_INI_MH(xc_OnUpdateDummy) |
|---|
| 2138 | { |
|---|
| 2139 | return SUCCESS; |
|---|
| 2140 | } |
|---|
| 2141 | |
|---|
| 2142 | static PHP_INI_MH(xc_OnUpdateULong) |
|---|
| 2143 | { |
|---|
| 2144 | zend_ulong *p = (zend_ulong *) mh_arg1; |
|---|
| 2145 | |
|---|
| 2146 | *p = (zend_ulong) atoi(new_value); |
|---|
| 2147 | return SUCCESS; |
|---|
| 2148 | } |
|---|
| 2149 | |
|---|
| 2150 | static PHP_INI_MH(xc_OnUpdateBool) |
|---|
| 2151 | { |
|---|
| 2152 | zend_bool *p = (zend_bool *)mh_arg1; |
|---|
| 2153 | |
|---|
| 2154 | if (strncasecmp("on", new_value, sizeof("on"))) { |
|---|
| 2155 | *p = (zend_bool) atoi(new_value); |
|---|
| 2156 | } |
|---|
| 2157 | else { |
|---|
| 2158 | *p = (zend_bool) 1; |
|---|
| 2159 | } |
|---|
| 2160 | return SUCCESS; |
|---|
| 2161 | } |
|---|
| 2162 | |
|---|
| 2163 | static PHP_INI_MH(xc_OnUpdateString) |
|---|
| 2164 | { |
|---|
| 2165 | char **p = (char**)mh_arg1; |
|---|
| 2166 | if (*p) { |
|---|
| 2167 | pefree(*p, 1); |
|---|
| 2168 | } |
|---|
| 2169 | *p = pemalloc(strlen(new_value) + 1, 1); |
|---|
| 2170 | strcpy(*p, new_value); |
|---|
| 2171 | return SUCCESS; |
|---|
| 2172 | } |
|---|
| 2173 | |
|---|
| 2174 | #ifndef ZEND_ENGINE_2 |
|---|
| 2175 | #define OnUpdateLong OnUpdateInt |
|---|
| 2176 | #endif |
|---|
| 2177 | |
|---|
| 2178 | #ifdef ZEND_WIN32 |
|---|
| 2179 | # define DEFAULT_PATH "xcache" |
|---|
| 2180 | #else |
|---|
| 2181 | # define DEFAULT_PATH "/dev/zero" |
|---|
| 2182 | #endif |
|---|
| 2183 | PHP_INI_BEGIN() |
|---|
| 2184 | PHP_INI_ENTRY1 ("xcache.mmap_path", DEFAULT_PATH, PHP_INI_SYSTEM, xc_OnUpdateString, &xc_mmap_path) |
|---|
| 2185 | PHP_INI_ENTRY1 ("xcache.coredump_directory", "", PHP_INI_SYSTEM, xc_OnUpdateString, &xc_coredump_dir) |
|---|
| 2186 | PHP_INI_ENTRY1 ("xcache.test", "0", PHP_INI_SYSTEM, xc_OnUpdateBool, &xc_test) |
|---|
| 2187 | PHP_INI_ENTRY1 ("xcache.readonly_protection", "0", PHP_INI_SYSTEM, xc_OnUpdateBool, &xc_readonly_protection) |
|---|
| 2188 | /* opcode cache */ |
|---|
| 2189 | PHP_INI_ENTRY1 ("xcache.size", "0", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2190 | PHP_INI_ENTRY1 ("xcache.count", "1", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2191 | PHP_INI_ENTRY1 ("xcache.slots", "8K", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2192 | PHP_INI_ENTRY1 ("xcache.shm_scheme", "mmap", PHP_INI_SYSTEM, xc_OnUpdateString, &xc_shm_scheme) |
|---|
| 2193 | PHP_INI_ENTRY1 ("xcache.ttl", "0", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_php_ttl) |
|---|
| 2194 | PHP_INI_ENTRY1 ("xcache.gc_interval", "0", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_php_gc_interval) |
|---|
| 2195 | /* var cache */ |
|---|
| 2196 | PHP_INI_ENTRY1 ("xcache.var_size", "0", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2197 | PHP_INI_ENTRY1 ("xcache.var_count", "1", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2198 | PHP_INI_ENTRY1 ("xcache.var_slots", "8K", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2199 | PHP_INI_ENTRY1 ("xcache.var_maxttl", "0", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_var_maxttl) |
|---|
| 2200 | PHP_INI_ENTRY1 ("xcache.var_gc_interval", "120", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_var_gc_interval) |
|---|
| 2201 | |
|---|
| 2202 | STD_PHP_INI_BOOLEAN("xcache.cacher", "1", PHP_INI_ALL, OnUpdateBool, cacher, zend_xcache_globals, xcache_globals) |
|---|
| 2203 | STD_PHP_INI_BOOLEAN("xcache.stat", "1", PHP_INI_ALL, OnUpdateBool, stat, zend_xcache_globals, xcache_globals) |
|---|
| 2204 | #ifdef HAVE_XCACHE_OPTIMIZER |
|---|
| 2205 | STD_PHP_INI_BOOLEAN("xcache.optimizer", "0", PHP_INI_ALL, OnUpdateBool, optimizer, zend_xcache_globals, xcache_globals) |
|---|
| 2206 | #endif |
|---|
| 2207 | STD_PHP_INI_BOOLEAN("xcache.var_ttl", "0", PHP_INI_ALL, OnUpdateLong, var_ttl, zend_xcache_globals, xcache_globals) |
|---|
| 2208 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 2209 | STD_PHP_INI_BOOLEAN("xcache.coverager" , "0", PHP_INI_ALL, OnUpdateBool, coverager, zend_xcache_globals, xcache_globals) |
|---|
| 2210 | PHP_INI_ENTRY1 ("xcache.coveragedump_directory", "", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2211 | #endif |
|---|
| 2212 | PHP_INI_END() |
|---|
| 2213 | /* }}} */ |
|---|
| 2214 | /* {{{ PHP_MINFO_FUNCTION(xcache) */ |
|---|
| 2215 | static PHP_MINFO_FUNCTION(xcache) |
|---|
| 2216 | { |
|---|
| 2217 | char buf[100]; |
|---|
| 2218 | char *ptr; |
|---|
| 2219 | int left, len; |
|---|
| 2220 | xc_shm_scheme_t *scheme; |
|---|
| 2221 | char *covdumpdir; |
|---|
| 2222 | |
|---|
| 2223 | php_info_print_table_start(); |
|---|
| 2224 | php_info_print_table_header(2, "XCache Support", "enabled"); |
|---|
| 2225 | php_info_print_table_row(2, "Version", XCACHE_VERSION); |
|---|
| 2226 | php_info_print_table_row(2, "Modules Built", XCACHE_MODULES); |
|---|
| 2227 | php_info_print_table_row(2, "Readonly Protection", xc_readonly_protection ? "enabled" : "N/A"); |
|---|
| 2228 | |
|---|
| 2229 | if (xc_php_size) { |
|---|
| 2230 | ptr = _php_math_number_format(xc_php_size, 0, '.', ','); |
|---|
| 2231 | snprintf(buf, sizeof(buf), "enabled, %s bytes, %d split(s), with %d slots each", ptr, xc_php_hcache.size, xc_php_hentry.size); |
|---|
| 2232 | php_info_print_table_row(2, "Opcode Cache", buf); |
|---|
| 2233 | efree(ptr); |
|---|
| 2234 | } |
|---|
| 2235 | else { |
|---|
| 2236 | php_info_print_table_row(2, "Opcode Cache", "disabled"); |
|---|
| 2237 | } |
|---|
| 2238 | if (xc_var_size) { |
|---|
| 2239 | ptr = _php_math_number_format(xc_var_size, 0, '.', ','); |
|---|
| 2240 | snprintf(buf, sizeof(buf), "enabled, %s bytes, %d split(s), with %d slots each", ptr, xc_var_hcache.size, xc_var_hentry.size); |
|---|
| 2241 | php_info_print_table_row(2, "Variable Cache", buf); |
|---|
| 2242 | efree(ptr); |
|---|
| 2243 | } |
|---|
| 2244 | else { |
|---|
| 2245 | php_info_print_table_row(2, "Variable Cache", "disabled"); |
|---|
| 2246 | } |
|---|
| 2247 | |
|---|
| 2248 | left = sizeof(buf); |
|---|
| 2249 | ptr = buf; |
|---|
| 2250 | buf[0] = '\0'; |
|---|
| 2251 | for (scheme = xc_shm_scheme_first(); scheme; scheme = xc_shm_scheme_next(scheme)) { |
|---|
| 2252 | len = snprintf(ptr, left, ptr == buf ? "%s" : ", %s", xc_shm_scheme_name(scheme)); |
|---|
| 2253 | left -= len; |
|---|
| 2254 | ptr += len; |
|---|
| 2255 | } |
|---|
| 2256 | php_info_print_table_row(2, "Shared Memory Schemes", buf); |
|---|
| 2257 | |
|---|
| 2258 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 2259 | if (cfg_get_string("xcache.coveragedump_directory", &covdumpdir) != SUCCESS || !covdumpdir[0]) { |
|---|
| 2260 | covdumpdir = NULL; |
|---|
| 2261 | } |
|---|
| 2262 | php_info_print_table_row(2, "Coverage Auto Dumper", XG(coverager) && covdumpdir ? "enabled" : "disabled"); |
|---|
| 2263 | #endif |
|---|
| 2264 | php_info_print_table_end(); |
|---|
| 2265 | |
|---|
| 2266 | DISPLAY_INI_ENTRIES(); |
|---|
| 2267 | } |
|---|
| 2268 | /* }}} */ |
|---|
| 2269 | /* {{{ extension startup */ |
|---|
| 2270 | static void xc_zend_extension_register(zend_extension *new_extension, DL_HANDLE handle) |
|---|
| 2271 | { |
|---|
| 2272 | zend_extension extension; |
|---|
| 2273 | |
|---|
| 2274 | extension = *new_extension; |
|---|
| 2275 | extension.handle = handle; |
|---|
| 2276 | |
|---|
| 2277 | zend_extension_dispatch_message(ZEND_EXTMSG_NEW_EXTENSION, &extension); |
|---|
| 2278 | |
|---|
| 2279 | zend_llist_add_element(&zend_extensions, &extension); |
|---|
| 2280 | #ifdef DEBUG |
|---|
| 2281 | fprintf(stderr, "registered\n"); |
|---|
| 2282 | #endif |
|---|
| 2283 | } |
|---|
| 2284 | |
|---|
| 2285 | /* dirty check */ |
|---|
| 2286 | #if defined(COMPILE_DL_XCACHE) && (defined(ZEND_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)) |
|---|
| 2287 | # define zend_append_version_info(x) do { } while (0) |
|---|
| 2288 | #else |
|---|
| 2289 | extern void zend_append_version_info(zend_extension *extension); |
|---|
| 2290 | #endif |
|---|
| 2291 | static int xc_zend_extension_startup(zend_extension *extension) |
|---|
| 2292 | { |
|---|
| 2293 | if (extension->startup) { |
|---|
| 2294 | if (extension->startup(extension) != SUCCESS) { |
|---|
| 2295 | return FAILURE; |
|---|
| 2296 | } |
|---|
| 2297 | zend_append_version_info(extension); |
|---|
| 2298 | } |
|---|
| 2299 | return SUCCESS; |
|---|
| 2300 | } |
|---|
| 2301 | /* }}} */ |
|---|
| 2302 | static int xc_config_hash(xc_hash_t *p, char *name, char *default_value) /* {{{ */ |
|---|
| 2303 | { |
|---|
| 2304 | int bits, size; |
|---|
| 2305 | char *value; |
|---|
| 2306 | |
|---|
| 2307 | if (cfg_get_string(name, &value) != SUCCESS) { |
|---|
| 2308 | value = default_value; |
|---|
| 2309 | } |
|---|
| 2310 | |
|---|
| 2311 | p->size = zend_atoi(value, strlen(value)); |
|---|
| 2312 | for (size = 1, bits = 1; size < p->size; bits ++, size <<= 1) { |
|---|
| 2313 | /* empty body */ |
|---|
| 2314 | } |
|---|
| 2315 | p->size = size; |
|---|
| 2316 | p->bits = bits; |
|---|
| 2317 | p->mask = size - 1; |
|---|
| 2318 | |
|---|
| 2319 | return SUCCESS; |
|---|
| 2320 | } |
|---|
| 2321 | /* }}} */ |
|---|
| 2322 | static int xc_config_long(zend_ulong *p, char *name, char *default_value) /* {{{ */ |
|---|
| 2323 | { |
|---|
| 2324 | char *value; |
|---|
| 2325 | |
|---|
| 2326 | if (cfg_get_string(name, &value) != SUCCESS) { |
|---|
| 2327 | value = default_value; |
|---|
| 2328 | } |
|---|
| 2329 | |
|---|
| 2330 | *p = zend_atoi(value, strlen(value)); |
|---|
| 2331 | return SUCCESS; |
|---|
| 2332 | } |
|---|
| 2333 | /* }}} */ |
|---|
| 2334 | /* {{{ PHP_MINIT_FUNCTION(xcache) */ |
|---|
| 2335 | static PHP_MINIT_FUNCTION(xcache) |
|---|
| 2336 | { |
|---|
| 2337 | char *env; |
|---|
| 2338 | zend_extension *ext; |
|---|
| 2339 | zend_llist_position lpos; |
|---|
| 2340 | |
|---|
| 2341 | xc_module_gotup = 1; |
|---|
| 2342 | if (!xc_zend_extension_gotup) { |
|---|
| 2343 | if (zend_get_extension(XCACHE_NAME) == NULL) { |
|---|
| 2344 | xc_zend_extension_register(&zend_extension_entry, 0); |
|---|
| 2345 | xc_zend_extension_startup(&zend_extension_entry); |
|---|
| 2346 | } |
|---|
| 2347 | } |
|---|
| 2348 | |
|---|
| 2349 | ext = zend_get_extension("Zend Optimizer"); |
|---|
| 2350 | if (ext) { |
|---|
| 2351 | /* zend_optimizer.optimization_level>0 is not compatible with other cacher, disabling */ |
|---|
| 2352 | ext->op_array_handler = NULL; |
|---|
| 2353 | } |
|---|
| 2354 | /* cache if there's an op_array_ctor */ |
|---|
| 2355 | for (ext = zend_llist_get_first_ex(&zend_extensions, &lpos); |
|---|
| 2356 | ext; |
|---|
| 2357 | ext = zend_llist_get_next_ex(&zend_extensions, &lpos)) { |
|---|
| 2358 | if (ext->op_array_ctor) { |
|---|
| 2359 | xc_have_op_array_ctor = 1; |
|---|
| 2360 | break; |
|---|
| 2361 | } |
|---|
| 2362 | } |
|---|
| 2363 | |
|---|
| 2364 | |
|---|
| 2365 | #ifndef PHP_GINIT |
|---|
| 2366 | ZEND_INIT_MODULE_GLOBALS(xcache, xc_init_globals, xc_shutdown_globals); |
|---|
| 2367 | #endif |
|---|
| 2368 | REGISTER_INI_ENTRIES(); |
|---|
| 2369 | |
|---|
| 2370 | if (strcmp(sapi_module.name, "cli") == 0) { |
|---|
| 2371 | if ((env = getenv("XCACHE_TEST")) != NULL) { |
|---|
| 2372 | zend_alter_ini_entry("xcache.test", sizeof("xcache.test"), env, strlen(env) + 1, PHP_INI_SYSTEM, PHP_INI_STAGE_STARTUP); |
|---|
| 2373 | } |
|---|
| 2374 | if (!xc_test) { |
|---|
| 2375 | /* disable cache for cli except for test */ |
|---|
| 2376 | xc_php_size = xc_var_size = 0; |
|---|
| 2377 | } |
|---|
| 2378 | } |
|---|
| 2379 | |
|---|
| 2380 | xc_config_long(&xc_php_size, "xcache.size", "0"); |
|---|
| 2381 | xc_config_hash(&xc_php_hcache, "xcache.count", "1"); |
|---|
| 2382 | xc_config_hash(&xc_php_hentry, "xcache.slots", "8K"); |
|---|
| 2383 | |
|---|
| 2384 | xc_config_long(&xc_var_size, "xcache.var_size", "0"); |
|---|
| 2385 | xc_config_hash(&xc_var_hcache, "xcache.var_count", "1"); |
|---|
| 2386 | xc_config_hash(&xc_var_hentry, "xcache.var_slots", "8K"); |
|---|
| 2387 | |
|---|
| 2388 | if (xc_php_size <= 0) { |
|---|
| 2389 | xc_php_size = xc_php_hcache.size = 0; |
|---|
| 2390 | } |
|---|
| 2391 | if (xc_var_size <= 0) { |
|---|
| 2392 | xc_var_size = xc_var_hcache.size = 0; |
|---|
| 2393 | } |
|---|
| 2394 | |
|---|
| 2395 | if (xc_coredump_dir && xc_coredump_dir[0]) { |
|---|
| 2396 | xcache_init_signal_handler(); |
|---|
| 2397 | } |
|---|
| 2398 | |
|---|
| 2399 | xc_init_constant(module_number TSRMLS_CC); |
|---|
| 2400 | xc_shm_init_modules(); |
|---|
| 2401 | |
|---|
| 2402 | if ((xc_php_size || xc_var_size) && xc_mmap_path && xc_mmap_path[0]) { |
|---|
| 2403 | if (!xc_init(module_number TSRMLS_CC)) { |
|---|
| 2404 | zend_error(E_ERROR, "XCache: Cannot init"); |
|---|
| 2405 | goto err_init; |
|---|
| 2406 | } |
|---|
| 2407 | xc_initized = 1; |
|---|
| 2408 | } |
|---|
| 2409 | |
|---|
| 2410 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 2411 | xc_coverager_init(module_number TSRMLS_CC); |
|---|
| 2412 | #endif |
|---|
| 2413 | |
|---|
| 2414 | return SUCCESS; |
|---|
| 2415 | |
|---|
| 2416 | err_init: |
|---|
| 2417 | return FAILURE; |
|---|
| 2418 | } |
|---|
| 2419 | /* }}} */ |
|---|
| 2420 | /* {{{ PHP_MSHUTDOWN_FUNCTION(xcache) */ |
|---|
| 2421 | static PHP_MSHUTDOWN_FUNCTION(xcache) |
|---|
| 2422 | { |
|---|
| 2423 | if (xc_initized) { |
|---|
| 2424 | xc_destroy(); |
|---|
| 2425 | xc_initized = 0; |
|---|
| 2426 | } |
|---|
| 2427 | if (xc_mmap_path) { |
|---|
| 2428 | pefree(xc_mmap_path, 1); |
|---|
| 2429 | xc_mmap_path = NULL; |
|---|
| 2430 | } |
|---|
| 2431 | if (xc_shm_scheme) { |
|---|
| 2432 | pefree(xc_shm_scheme, 1); |
|---|
| 2433 | xc_shm_scheme = NULL; |
|---|
| 2434 | } |
|---|
| 2435 | |
|---|
| 2436 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 2437 | xc_coverager_destroy(); |
|---|
| 2438 | #endif |
|---|
| 2439 | |
|---|
| 2440 | if (xc_coredump_dir && xc_coredump_dir[0]) { |
|---|
| 2441 | xcache_restore_signal_handler(); |
|---|
| 2442 | } |
|---|
| 2443 | if (xc_coredump_dir) { |
|---|
| 2444 | pefree(xc_coredump_dir, 1); |
|---|
| 2445 | xc_coredump_dir = NULL; |
|---|
| 2446 | } |
|---|
| 2447 | #ifndef PHP_GINIT |
|---|
| 2448 | # ifdef ZTS |
|---|
| 2449 | ts_free_id(xcache_globals_id); |
|---|
| 2450 | # else |
|---|
| 2451 | xc_shutdown_globals(&xcache_globals TSRMLS_CC); |
|---|
| 2452 | # endif |
|---|
| 2453 | #endif |
|---|
| 2454 | |
|---|
| 2455 | UNREGISTER_INI_ENTRIES(); |
|---|
| 2456 | return SUCCESS; |
|---|
| 2457 | } |
|---|
| 2458 | /* }}} */ |
|---|
| 2459 | /* {{{ PHP_RINIT_FUNCTION(xcache) */ |
|---|
| 2460 | static PHP_RINIT_FUNCTION(xcache) |
|---|
| 2461 | { |
|---|
| 2462 | xc_request_init(TSRMLS_C); |
|---|
| 2463 | return SUCCESS; |
|---|
| 2464 | } |
|---|
| 2465 | /* }}} */ |
|---|
| 2466 | /* {{{ PHP_RSHUTDOWN_FUNCTION(xcache) */ |
|---|
| 2467 | #ifndef ZEND_ENGINE_2 |
|---|
| 2468 | static PHP_RSHUTDOWN_FUNCTION(xcache) |
|---|
| 2469 | #else |
|---|
| 2470 | static ZEND_MODULE_POST_ZEND_DEACTIVATE_D(xcache) |
|---|
| 2471 | #endif |
|---|
| 2472 | { |
|---|
| 2473 | #ifdef ZEND_ENGINE_2 |
|---|
| 2474 | TSRMLS_FETCH(); |
|---|
| 2475 | #endif |
|---|
| 2476 | |
|---|
| 2477 | xc_request_shutdown(TSRMLS_C); |
|---|
| 2478 | return SUCCESS; |
|---|
| 2479 | } |
|---|
| 2480 | /* }}} */ |
|---|
| 2481 | /* {{{ module definition structure */ |
|---|
| 2482 | |
|---|
| 2483 | zend_module_entry xcache_module_entry = { |
|---|
| 2484 | STANDARD_MODULE_HEADER, |
|---|
| 2485 | "XCache", |
|---|
| 2486 | xcache_functions, |
|---|
| 2487 | PHP_MINIT(xcache), |
|---|
| 2488 | PHP_MSHUTDOWN(xcache), |
|---|
| 2489 | PHP_RINIT(xcache), |
|---|
| 2490 | #ifndef ZEND_ENGINE_2 |
|---|
| 2491 | PHP_RSHUTDOWN(xcache), |
|---|
| 2492 | #else |
|---|
| 2493 | NULL, |
|---|
| 2494 | #endif |
|---|
| 2495 | PHP_MINFO(xcache), |
|---|
| 2496 | XCACHE_VERSION, |
|---|
| 2497 | #ifdef PHP_GINIT |
|---|
| 2498 | PHP_MODULE_GLOBALS(xcache), |
|---|
| 2499 | PHP_GINIT(xcache), |
|---|
| 2500 | PHP_GSHUTDOWN(xcache), |
|---|
| 2501 | #endif |
|---|
| 2502 | #ifdef ZEND_ENGINE_2 |
|---|
| 2503 | ZEND_MODULE_POST_ZEND_DEACTIVATE_N(xcache), |
|---|
| 2504 | #else |
|---|
| 2505 | NULL, |
|---|
| 2506 | NULL, |
|---|
| 2507 | #endif |
|---|
| 2508 | STANDARD_MODULE_PROPERTIES_EX |
|---|
| 2509 | }; |
|---|
| 2510 | |
|---|
| 2511 | #ifdef COMPILE_DL_XCACHE |
|---|
| 2512 | ZEND_GET_MODULE(xcache) |
|---|
| 2513 | #endif |
|---|
| 2514 | /* }}} */ |
|---|
| 2515 | static startup_func_t xc_last_ext_startup; |
|---|
| 2516 | static zend_llist_element *xc_llist_element; |
|---|
| 2517 | static int xc_ptr_compare_func(void *p1, void *p2) /* {{{ */ |
|---|
| 2518 | { |
|---|
| 2519 | return p1 == p2; |
|---|
| 2520 | } |
|---|
| 2521 | /* }}} */ |
|---|
| 2522 | static int xc_zend_startup_last(zend_extension *extension) /* {{{ */ |
|---|
| 2523 | { |
|---|
| 2524 | /* restore */ |
|---|
| 2525 | extension->startup = xc_last_ext_startup; |
|---|
| 2526 | if (extension->startup) { |
|---|
| 2527 | if (extension->startup(extension) != SUCCESS) { |
|---|
| 2528 | return FAILURE; |
|---|
| 2529 | } |
|---|
| 2530 | } |
|---|
| 2531 | xc_zend_extension_register(&zend_extension_entry, 0); |
|---|
| 2532 | if (!xc_module_gotup) { |
|---|
| 2533 | return zend_startup_module(&xcache_module_entry); |
|---|
| 2534 | } |
|---|
| 2535 | return SUCCESS; |
|---|
| 2536 | } |
|---|
| 2537 | /* }}} */ |
|---|
| 2538 | ZEND_DLEXPORT int xcache_zend_startup(zend_extension *extension) /* {{{ */ |
|---|
| 2539 | { |
|---|
| 2540 | if (xc_zend_extension_gotup) { |
|---|
| 2541 | return FAILURE; |
|---|
| 2542 | } |
|---|
| 2543 | xc_zend_extension_gotup = 1; |
|---|
| 2544 | xc_llist_element = NULL; |
|---|
| 2545 | if (zend_llist_count(&zend_extensions) > 1) { |
|---|
| 2546 | zend_llist_position lpos; |
|---|
| 2547 | zend_extension *ext; |
|---|
| 2548 | llist_dtor_func_t dtor; |
|---|
| 2549 | |
|---|
| 2550 | ext = zend_get_extension(XCACHE_NAME); |
|---|
| 2551 | assert(ext); |
|---|
| 2552 | dtor = zend_extensions.dtor; /* avoid dtor */ |
|---|
| 2553 | zend_extensions.dtor = NULL; |
|---|
| 2554 | zend_llist_del_element(&zend_extensions, ext, xc_ptr_compare_func); |
|---|
| 2555 | zend_extensions.dtor = dtor; |
|---|
| 2556 | |
|---|
| 2557 | ext = (zend_extension *) zend_llist_get_last_ex(&zend_extensions, &lpos); |
|---|
| 2558 | assert(ext); |
|---|
| 2559 | xc_last_ext_startup = ext->startup; |
|---|
| 2560 | ext->startup = xc_zend_startup_last; |
|---|
| 2561 | } |
|---|
| 2562 | else if (!xc_module_gotup) { |
|---|
| 2563 | return zend_startup_module(&xcache_module_entry); |
|---|
| 2564 | } |
|---|
| 2565 | return SUCCESS; |
|---|
| 2566 | } |
|---|
| 2567 | /* }}} */ |
|---|
| 2568 | ZEND_DLEXPORT void xcache_zend_shutdown(zend_extension *extension) /* {{{ */ |
|---|
| 2569 | { |
|---|
| 2570 | /* empty */ |
|---|
| 2571 | } |
|---|
| 2572 | /* }}} */ |
|---|
| 2573 | ZEND_DLEXPORT void xcache_statement_handler(zend_op_array *op_array) /* {{{ */ |
|---|
| 2574 | { |
|---|
| 2575 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 2576 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_STMT); |
|---|
| 2577 | #endif |
|---|
| 2578 | } |
|---|
| 2579 | /* }}} */ |
|---|
| 2580 | ZEND_DLEXPORT void xcache_fcall_begin_handler(zend_op_array *op_array) /* {{{ */ |
|---|
| 2581 | { |
|---|
| 2582 | #if 0 |
|---|
| 2583 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_FCALL_BEGIN); |
|---|
| 2584 | #endif |
|---|
| 2585 | } |
|---|
| 2586 | /* }}} */ |
|---|
| 2587 | ZEND_DLEXPORT void xcache_fcall_end_handler(zend_op_array *op_array) /* {{{ */ |
|---|
| 2588 | { |
|---|
| 2589 | #if 0 |
|---|
| 2590 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_FCALL_END); |
|---|
| 2591 | #endif |
|---|
| 2592 | } |
|---|
| 2593 | /* }}} */ |
|---|
| 2594 | /* {{{ zend extension definition structure */ |
|---|
| 2595 | ZEND_DLEXPORT zend_extension zend_extension_entry = { |
|---|
| 2596 | XCACHE_NAME, |
|---|
| 2597 | XCACHE_VERSION, |
|---|
| 2598 | XCACHE_AUTHOR, |
|---|
| 2599 | XCACHE_URL, |
|---|
| 2600 | XCACHE_COPYRIGHT, |
|---|
| 2601 | xcache_zend_startup, |
|---|
| 2602 | xcache_zend_shutdown, |
|---|
| 2603 | NULL, /* activate_func_t */ |
|---|
| 2604 | NULL, /* deactivate_func_t */ |
|---|
| 2605 | NULL, /* message_handler_func_t */ |
|---|
| 2606 | NULL, /* op_array_handler_func_t */ |
|---|
| 2607 | xcache_statement_handler, |
|---|
| 2608 | xcache_fcall_begin_handler, |
|---|
| 2609 | xcache_fcall_end_handler, |
|---|
| 2610 | NULL, /* op_array_ctor_func_t */ |
|---|
| 2611 | NULL, /* op_array_dtor_func_t */ |
|---|
| 2612 | STANDARD_ZEND_EXTENSION_PROPERTIES |
|---|
| 2613 | }; |
|---|
| 2614 | |
|---|
| 2615 | #ifndef ZEND_EXT_API |
|---|
| 2616 | # define ZEND_EXT_API ZEND_DLEXPORT |
|---|
| 2617 | #endif |
|---|
| 2618 | #if COMPILE_DL_XCACHE |
|---|
| 2619 | ZEND_EXTENSION(); |
|---|
| 2620 | #endif |
|---|
| 2621 | /* }}} */ |
|---|