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