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