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