| [1] | 1 | |
|---|
| [205] | 2 | #if 0 |
|---|
| 3 | #define DEBUG |
|---|
| 4 | #endif |
|---|
| [1] | 5 | |
|---|
| 6 | /* {{{ macros */ |
|---|
| 7 | #include <stdlib.h> |
|---|
| 8 | #include <stdio.h> |
|---|
| 9 | #include <string.h> |
|---|
| 10 | |
|---|
| 11 | #include <signal.h> |
|---|
| 12 | |
|---|
| 13 | #include "php.h" |
|---|
| 14 | #include "ext/standard/info.h" |
|---|
| [34] | 15 | #include "ext/standard/md5.h" |
|---|
| [82] | 16 | #include "ext/standard/php_math.h" |
|---|
| [1] | 17 | #include "zend_extensions.h" |
|---|
| 18 | #include "SAPI.h" |
|---|
| 19 | |
|---|
| 20 | #include "xcache.h" |
|---|
| 21 | #include "optimizer.h" |
|---|
| [27] | 22 | #include "coverager.h" |
|---|
| [1] | 23 | #include "disassembler.h" |
|---|
| 24 | #include "align.h" |
|---|
| 25 | #include "stack.h" |
|---|
| 26 | #include "xcache_globals.h" |
|---|
| 27 | #include "processor.h" |
|---|
| 28 | #include "utils.h" |
|---|
| 29 | #include "const_string.h" |
|---|
| 30 | #include "opcode_spec.h" |
|---|
| 31 | |
|---|
| 32 | #ifdef DEBUG |
|---|
| [18] | 33 | # undef NDEBUG |
|---|
| [1] | 34 | # undef inline |
|---|
| 35 | # define inline |
|---|
| 36 | #else |
|---|
| [18] | 37 | # ifndef NDEBUG |
|---|
| 38 | # define NDEBUG |
|---|
| 39 | # endif |
|---|
| [1] | 40 | #endif |
|---|
| 41 | #include <assert.h> |
|---|
| 42 | |
|---|
| [114] | 43 | #define VAR_ENTRY_EXPIRED(pentry) ((pentry)->ttl && XG(request_time) > pentry->ctime + (pentry)->ttl) |
|---|
| [1] | 44 | #define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0) |
|---|
| 45 | #define LOCK(x) xc_lock(x->lck) |
|---|
| 46 | #define UNLOCK(x) xc_unlock(x->lck) |
|---|
| [130] | 47 | |
|---|
| 48 | #define ENTER_LOCK_EX(x) \ |
|---|
| [1] | 49 | xc_lock(x->lck); \ |
|---|
| 50 | zend_try { \ |
|---|
| 51 | do |
|---|
| [130] | 52 | #define LEAVE_LOCK_EX(x) \ |
|---|
| [1] | 53 | while (0); \ |
|---|
| 54 | } zend_catch { \ |
|---|
| 55 | catched = 1; \ |
|---|
| 56 | } zend_end_try(); \ |
|---|
| [130] | 57 | xc_unlock(x->lck) |
|---|
| 58 | |
|---|
| 59 | #define ENTER_LOCK(x) do { \ |
|---|
| 60 | int catched = 0; \ |
|---|
| 61 | ENTER_LOCK_EX(x) |
|---|
| 62 | #define LEAVE_LOCK(x) \ |
|---|
| 63 | LEAVE_LOCK_EX(x); \ |
|---|
| 64 | if (catched) { \ |
|---|
| 65 | zend_bailout(); \ |
|---|
| 66 | } \ |
|---|
| [1] | 67 | } while(0) |
|---|
| [130] | 68 | |
|---|
| [212] | 69 | #ifndef max |
|---|
| 70 | #define max(a, b) ((a) < (b) ? (b) : (a)) |
|---|
| 71 | #endif |
|---|
| 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) { |
|---|
| 573 | zend_uint class_idx = (/* class_num */ (int) ce->parent) - 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 | /* }}} */ |
|---|
| [130] | 1021 | ENTER_LOCK_EX(cache) { /* {{{ store/add entry */ |
|---|
| [1] | 1022 | stored_xce = xc_entry_store_dmz(&xce TSRMLS_CC); |
|---|
| [130] | 1023 | } LEAVE_LOCK_EX(cache); |
|---|
| [1] | 1024 | /* }}} */ |
|---|
| 1025 | #ifdef DEBUG |
|---|
| 1026 | fprintf(stderr, "stored\n"); |
|---|
| 1027 | #endif |
|---|
| 1028 | |
|---|
| [95] | 1029 | #define X_FREE(var) \ |
|---|
| 1030 | if (xce.data.php->var) { \ |
|---|
| 1031 | efree(xce.data.php->var); \ |
|---|
| 1032 | } \ |
|---|
| 1033 | err_##var: |
|---|
| 1034 | |
|---|
| 1035 | X_FREE(classinfos) |
|---|
| 1036 | X_FREE(funcinfos) |
|---|
| 1037 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 1038 | X_FREE(constinfos) |
|---|
| 1039 | #endif |
|---|
| 1040 | #undef X_FREE |
|---|
| 1041 | |
|---|
| [1] | 1042 | err_oparray: |
|---|
| 1043 | err_bailout: |
|---|
| 1044 | |
|---|
| 1045 | if (xc_test && stored_xce) { |
|---|
| [131] | 1046 | /* free it, no install. restore now */ |
|---|
| [1] | 1047 | xc_sandbox_free(&sandbox, 0 TSRMLS_CC); |
|---|
| 1048 | } |
|---|
| 1049 | else { |
|---|
| [212] | 1050 | CG(active_op_array) = op_array; |
|---|
| [1] | 1051 | xc_sandbox_free(&sandbox, 1 TSRMLS_CC); |
|---|
| 1052 | } |
|---|
| 1053 | |
|---|
| 1054 | ENTER_LOCK(cache) { |
|---|
| 1055 | cache->compiling = 0; |
|---|
| 1056 | } LEAVE_LOCK(cache); |
|---|
| 1057 | if (catched) { |
|---|
| 1058 | zend_bailout(); |
|---|
| 1059 | } |
|---|
| 1060 | if (xc_test && stored_xce) { |
|---|
| [132] | 1061 | #ifdef ZEND_ENGINE_2 |
|---|
| [119] | 1062 | destroy_op_array(op_array TSRMLS_CC); |
|---|
| [132] | 1063 | #else |
|---|
| 1064 | destroy_op_array(op_array); |
|---|
| 1065 | #endif |
|---|
| [119] | 1066 | efree(op_array); |
|---|
| [131] | 1067 | h = NULL; |
|---|
| [1] | 1068 | goto restore; |
|---|
| 1069 | } |
|---|
| 1070 | return op_array; |
|---|
| 1071 | |
|---|
| 1072 | restore: |
|---|
| [86] | 1073 | if (php_check_open_basedir(stored_xce->name.str.val TSRMLS_CC) != 0) { |
|---|
| 1074 | return NULL; |
|---|
| 1075 | } |
|---|
| 1076 | |
|---|
| [1] | 1077 | #ifdef DEBUG |
|---|
| 1078 | fprintf(stderr, "restoring\n"); |
|---|
| 1079 | #endif |
|---|
| 1080 | xc_processor_restore_xc_entry_t(&xce, stored_xce, xc_readonly_protection TSRMLS_CC); |
|---|
| 1081 | |
|---|
| [95] | 1082 | catched = 0; |
|---|
| 1083 | zend_try { |
|---|
| 1084 | op_array = xc_entry_install(&xce, h TSRMLS_CC); |
|---|
| 1085 | } zend_catch { |
|---|
| 1086 | catched = 1; |
|---|
| 1087 | } zend_end_try(); |
|---|
| 1088 | |
|---|
| 1089 | #define X_FREE(var) \ |
|---|
| 1090 | if (xce.data.php->var) { \ |
|---|
| 1091 | efree(xce.data.php->var); \ |
|---|
| 1092 | } |
|---|
| 1093 | X_FREE(classinfos) |
|---|
| 1094 | X_FREE(funcinfos) |
|---|
| 1095 | #ifdef HAVE_XCACHE_CONSTANT |
|---|
| 1096 | X_FREE(constinfos) |
|---|
| 1097 | #endif |
|---|
| 1098 | #undef X_FREE |
|---|
| [1] | 1099 | efree(xce.data.php); |
|---|
| [95] | 1100 | |
|---|
| 1101 | if (catched) { |
|---|
| 1102 | zend_bailout(); |
|---|
| 1103 | } |
|---|
| [1] | 1104 | #ifdef DEBUG |
|---|
| 1105 | fprintf(stderr, "restored\n"); |
|---|
| 1106 | #endif |
|---|
| 1107 | return op_array; |
|---|
| 1108 | } |
|---|
| 1109 | /* }}} */ |
|---|
| 1110 | |
|---|
| 1111 | /* gdb helper functions, but N/A for coredump */ |
|---|
| 1112 | int xc_is_rw(const void *p) /* {{{ */ |
|---|
| 1113 | { |
|---|
| [148] | 1114 | xc_shm_t *shm; |
|---|
| [1] | 1115 | int i; |
|---|
| 1116 | if (!xc_initized) { |
|---|
| 1117 | return 0; |
|---|
| 1118 | } |
|---|
| 1119 | for (i = 0; i < xc_php_hcache.size; i ++) { |
|---|
| [148] | 1120 | shm = xc_php_caches[i]->shm; |
|---|
| 1121 | if (shm->handlers->is_readwrite(shm, p)) { |
|---|
| [1] | 1122 | return 1; |
|---|
| 1123 | } |
|---|
| 1124 | } |
|---|
| 1125 | for (i = 0; i < xc_var_hcache.size; i ++) { |
|---|
| [148] | 1126 | shm = xc_var_caches[i]->shm; |
|---|
| 1127 | if (shm->handlers->is_readwrite(shm, p)) { |
|---|
| [1] | 1128 | return 1; |
|---|
| 1129 | } |
|---|
| 1130 | } |
|---|
| 1131 | return 0; |
|---|
| 1132 | } |
|---|
| 1133 | /* }}} */ |
|---|
| 1134 | int xc_is_ro(const void *p) /* {{{ */ |
|---|
| 1135 | { |
|---|
| [148] | 1136 | xc_shm_t *shm; |
|---|
| [1] | 1137 | int i; |
|---|
| 1138 | if (!xc_initized) { |
|---|
| 1139 | return 0; |
|---|
| 1140 | } |
|---|
| 1141 | for (i = 0; i < xc_php_hcache.size; i ++) { |
|---|
| [148] | 1142 | shm = xc_php_caches[i]->shm; |
|---|
| 1143 | if (shm->handlers->is_readonly(shm, p)) { |
|---|
| [1] | 1144 | return 1; |
|---|
| 1145 | } |
|---|
| 1146 | } |
|---|
| 1147 | for (i = 0; i < xc_var_hcache.size; i ++) { |
|---|
| [148] | 1148 | shm = xc_var_caches[i]->shm; |
|---|
| 1149 | if (shm->handlers->is_readonly(shm, p)) { |
|---|
| [1] | 1150 | return 1; |
|---|
| 1151 | } |
|---|
| 1152 | } |
|---|
| 1153 | return 0; |
|---|
| 1154 | } |
|---|
| 1155 | /* }}} */ |
|---|
| 1156 | int xc_is_shm(const void *p) /* {{{ */ |
|---|
| 1157 | { |
|---|
| 1158 | return xc_is_ro(p) || xc_is_rw(p); |
|---|
| 1159 | } |
|---|
| 1160 | /* }}} */ |
|---|
| 1161 | |
|---|
| 1162 | /* module helper function */ |
|---|
| 1163 | static int xc_init_constant(int module_number TSRMLS_DC) /* {{{ */ |
|---|
| 1164 | { |
|---|
| [11] | 1165 | typedef struct { |
|---|
| [1] | 1166 | const char *prefix; |
|---|
| [20] | 1167 | zend_uchar (*getsize)(); |
|---|
| [1] | 1168 | const char *(*get)(zend_uchar i); |
|---|
| [11] | 1169 | } xc_meminfo_t; |
|---|
| 1170 | xc_meminfo_t nameinfos[] = { |
|---|
| [1] | 1171 | { "", xc_get_op_type_count, xc_get_op_type }, |
|---|
| 1172 | { "", xc_get_data_type_count, xc_get_data_type }, |
|---|
| 1173 | { "", xc_get_opcode_count, xc_get_opcode }, |
|---|
| 1174 | { "OPSPEC_", xc_get_op_spec_count, xc_get_op_spec }, |
|---|
| 1175 | { NULL, NULL, NULL } |
|---|
| 1176 | }; |
|---|
| [11] | 1177 | xc_meminfo_t* p; |
|---|
| [20] | 1178 | zend_uchar i, count; |
|---|
| [1] | 1179 | char const_name[96]; |
|---|
| 1180 | int const_name_len; |
|---|
| 1181 | int undefdone = 0; |
|---|
| 1182 | |
|---|
| 1183 | for (p = nameinfos; p->getsize; p ++) { |
|---|
| [20] | 1184 | count = p->getsize(); |
|---|
| 1185 | for (i = 0; i < count; i ++) { |
|---|
| [1] | 1186 | const char *name = p->get(i); |
|---|
| 1187 | if (!name) continue; |
|---|
| 1188 | if (strcmp(name, "UNDEF") == 0) { |
|---|
| 1189 | if (undefdone) continue; |
|---|
| 1190 | undefdone = 1; |
|---|
| 1191 | } |
|---|
| 1192 | const_name_len = snprintf(const_name, sizeof(const_name), "XC_%s%s", p->prefix, name); |
|---|
| 1193 | zend_register_long_constant(const_name, const_name_len+1, i, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 1194 | } |
|---|
| 1195 | } |
|---|
| 1196 | |
|---|
| 1197 | zend_register_long_constant(ZEND_STRS("XC_SIZEOF_TEMP_VARIABLE"), sizeof(temp_variable), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 1198 | zend_register_long_constant(ZEND_STRS("XC_TYPE_PHP"), XC_TYPE_PHP, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 1199 | zend_register_long_constant(ZEND_STRS("XC_TYPE_VAR"), XC_TYPE_VAR, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| [119] | 1200 | zend_register_stringl_constant(ZEND_STRS("XCACHE_VERSION"), ZEND_STRL(XCACHE_VERSION), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| 1201 | zend_register_stringl_constant(ZEND_STRS("XCACHE_MODULES"), ZEND_STRL(XCACHE_MODULES), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
|---|
| [1] | 1202 | return 0; |
|---|
| 1203 | } |
|---|
| 1204 | /* }}} */ |
|---|
| [11] | 1205 | static xc_shm_t *xc_cache_destroy(xc_cache_t **caches, xc_hash_t *hcache) /* {{{ */ |
|---|
| [1] | 1206 | { |
|---|
| 1207 | int i; |
|---|
| 1208 | xc_cache_t *cache; |
|---|
| 1209 | xc_shm_t *shm; |
|---|
| 1210 | |
|---|
| 1211 | if (!caches) { |
|---|
| 1212 | return NULL; |
|---|
| 1213 | } |
|---|
| 1214 | shm = NULL; |
|---|
| 1215 | for (i = 0; i < hcache->size; i ++) { |
|---|
| 1216 | cache = caches[i]; |
|---|
| 1217 | if (cache) { |
|---|
| 1218 | if (cache->lck) { |
|---|
| 1219 | xc_lock_destroy(cache->lck); |
|---|
| 1220 | } |
|---|
| 1221 | /* do NOT free |
|---|
| 1222 | if (cache->entries) { |
|---|
| [148] | 1223 | cache->mem->handlers->free(cache->mem, cache->entries); |
|---|
| [1] | 1224 | } |
|---|
| [148] | 1225 | cache->mem->handlers->free(cache->mem, cache); |
|---|
| [1] | 1226 | */ |
|---|
| [154] | 1227 | shm = cache->shm; |
|---|
| [148] | 1228 | shm->handlers->memdestroy(cache->mem); |
|---|
| [1] | 1229 | } |
|---|
| 1230 | } |
|---|
| 1231 | free(caches); |
|---|
| 1232 | return shm; |
|---|
| 1233 | } |
|---|
| 1234 | /* }}} */ |
|---|
| [148] | 1235 | static xc_cache_t **xc_cache_init(xc_shm_t *shm, xc_hash_t *hcache, xc_hash_t *hentry, xc_shmsize_t shmsize) /* {{{ */ |
|---|
| [1] | 1236 | { |
|---|
| 1237 | xc_cache_t **caches = NULL, *cache; |
|---|
| 1238 | xc_mem_t *mem; |
|---|
| [114] | 1239 | time_t now = time(NULL); |
|---|
| [1] | 1240 | int i; |
|---|
| [49] | 1241 | xc_memsize_t memsize; |
|---|
| [11] | 1242 | |
|---|
| [49] | 1243 | memsize = shmsize / hcache->size; |
|---|
| [1] | 1244 | |
|---|
| [49] | 1245 | /* Don't let it break out of mem after ALIGNed |
|---|
| 1246 | * This is important for |
|---|
| 1247 | * Simply loop until it fit our need |
|---|
| 1248 | */ |
|---|
| 1249 | while (ALIGN(memsize) * hcache->size > shmsize && ALIGN(memsize) != memsize) { |
|---|
| 1250 | if (memsize < ALIGN(1)) { |
|---|
| 1251 | CHECK(NULL, "cache too small"); |
|---|
| 1252 | } |
|---|
| 1253 | memsize --; |
|---|
| 1254 | } |
|---|
| 1255 | |
|---|
| [1] | 1256 | CHECK(caches = calloc(hcache->size, sizeof(xc_cache_t *)), "caches OOM"); |
|---|
| 1257 | |
|---|
| 1258 | for (i = 0; i < hcache->size; i ++) { |
|---|
| [148] | 1259 | CHECK(mem = shm->handlers->meminit(shm, memsize), "Failed init memory allocator"); |
|---|
| 1260 | CHECK(cache = mem->handlers->calloc(mem, 1, sizeof(xc_cache_t)), "cache OOM"); |
|---|
| 1261 | CHECK(cache->entries = mem->handlers->calloc(mem, hentry->size, sizeof(xc_entry_t*)), "entries OOM"); |
|---|
| [1] | 1262 | CHECK(cache->lck = xc_lock_init(NULL), "can't create lock"); |
|---|
| 1263 | |
|---|
| 1264 | cache->hcache = hcache; |
|---|
| 1265 | cache->hentry = hentry; |
|---|
| 1266 | cache->shm = shm; |
|---|
| 1267 | cache->mem = mem; |
|---|
| 1268 | cache->cacheid = i; |
|---|
| [114] | 1269 | cache->last_gc_deletes = now; |
|---|
| 1270 | cache->last_gc_expires = now; |
|---|
| [1] | 1271 | caches[i] = cache; |
|---|
| 1272 | } |
|---|
| 1273 | return caches; |
|---|
| 1274 | |
|---|
| 1275 | err: |
|---|
| 1276 | if (caches) { |
|---|
| 1277 | xc_cache_destroy(caches, hcache); |
|---|
| 1278 | } |
|---|
| 1279 | return NULL; |
|---|
| 1280 | } |
|---|
| 1281 | /* }}} */ |
|---|
| 1282 | static void xc_destroy() /* {{{ */ |
|---|
| 1283 | { |
|---|
| 1284 | xc_shm_t *shm = NULL; |
|---|
| 1285 | |
|---|
| 1286 | if (origin_compile_file) { |
|---|
| 1287 | zend_compile_file = origin_compile_file; |
|---|
| 1288 | origin_compile_file = NULL; |
|---|
| 1289 | } |
|---|
| 1290 | |
|---|
| 1291 | if (xc_php_caches) { |
|---|
| 1292 | shm = xc_cache_destroy(xc_php_caches, &xc_php_hcache); |
|---|
| 1293 | xc_php_caches = NULL; |
|---|
| 1294 | } |
|---|
| 1295 | if (xc_var_caches) { |
|---|
| 1296 | shm = xc_cache_destroy(xc_var_caches, &xc_var_hcache); |
|---|
| 1297 | xc_var_caches = NULL; |
|---|
| 1298 | } |
|---|
| 1299 | if (shm) { |
|---|
| 1300 | xc_shm_destroy(shm); |
|---|
| 1301 | } |
|---|
| 1302 | } |
|---|
| 1303 | /* }}} */ |
|---|
| 1304 | static int xc_init(int module_number TSRMLS_DC) /* {{{ */ |
|---|
| 1305 | { |
|---|
| 1306 | xc_shm_t *shm; |
|---|
| 1307 | |
|---|
| [11] | 1308 | xc_php_caches = xc_var_caches = NULL; |
|---|
| 1309 | |
|---|
| [1] | 1310 | if (xc_php_size || xc_var_size) { |
|---|
| [148] | 1311 | 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"); |
|---|
| 1312 | if (!shm->handlers->can_readonly(shm)) { |
|---|
| [1] | 1313 | xc_readonly_protection = 0; |
|---|
| 1314 | } |
|---|
| 1315 | |
|---|
| 1316 | if (xc_php_size) { |
|---|
| 1317 | origin_compile_file = zend_compile_file; |
|---|
| 1318 | zend_compile_file = xc_compile_file; |
|---|
| 1319 | |
|---|
| [148] | 1320 | CHECK(xc_php_caches = xc_cache_init(shm, &xc_php_hcache, &xc_php_hentry, xc_php_size), "failed init opcode cache"); |
|---|
| [1] | 1321 | } |
|---|
| 1322 | |
|---|
| 1323 | if (xc_var_size) { |
|---|
| [148] | 1324 | CHECK(xc_var_caches = xc_cache_init(shm, &xc_var_hcache, &xc_var_hentry, xc_var_size), "failed init variable cache"); |
|---|
| [1] | 1325 | } |
|---|
| 1326 | } |
|---|
| 1327 | return 1; |
|---|
| 1328 | |
|---|
| 1329 | err: |
|---|
| 1330 | if (xc_php_caches || xc_var_caches) { |
|---|
| 1331 | xc_destroy(); |
|---|
| 1332 | /* shm destroied */ |
|---|
| 1333 | } |
|---|
| 1334 | else if (shm) { |
|---|
| 1335 | xc_shm_destroy(shm); |
|---|
| 1336 | } |
|---|
| 1337 | return 0; |
|---|
| 1338 | } |
|---|
| 1339 | /* }}} */ |
|---|
| 1340 | static void xc_request_init(TSRMLS_D) /* {{{ */ |
|---|
| 1341 | { |
|---|
| [17] | 1342 | int i; |
|---|
| 1343 | |
|---|
| 1344 | if (xc_php_hcache.size && !XG(php_holds)) { |
|---|
| 1345 | XG(php_holds) = calloc(xc_php_hcache.size, sizeof(xc_stack_t)); |
|---|
| 1346 | for (i = 0; i < xc_php_hcache.size; i ++) { |
|---|
| 1347 | xc_stack_init(&XG(php_holds[i])); |
|---|
| 1348 | } |
|---|
| 1349 | } |
|---|
| 1350 | |
|---|
| 1351 | if (xc_var_hcache.size && !XG(var_holds)) { |
|---|
| 1352 | XG(var_holds) = calloc(xc_var_hcache.size, sizeof(xc_stack_t)); |
|---|
| 1353 | for (i = 0; i < xc_var_hcache.size; i ++) { |
|---|
| 1354 | xc_stack_init(&XG(var_holds[i])); |
|---|
| 1355 | } |
|---|
| 1356 | } |
|---|
| 1357 | |
|---|
| [1] | 1358 | if (XG(cacher)) { |
|---|
| 1359 | #if PHP_API_VERSION <= 20041225 |
|---|
| 1360 | XG(request_time) = time(NULL); |
|---|
| 1361 | #else |
|---|
| 1362 | XG(request_time) = sapi_get_request_time(TSRMLS_C); |
|---|
| 1363 | #endif |
|---|
| 1364 | } |
|---|
| [27] | 1365 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 1366 | xc_coverager_request_init(TSRMLS_C); |
|---|
| [1] | 1367 | #endif |
|---|
| 1368 | } |
|---|
| 1369 | /* }}} */ |
|---|
| 1370 | static void xc_request_shutdown(TSRMLS_D) /* {{{ */ |
|---|
| 1371 | { |
|---|
| 1372 | xc_entry_unholds(TSRMLS_C); |
|---|
| [120] | 1373 | xc_gc_expires_php(TSRMLS_C); |
|---|
| 1374 | xc_gc_expires_var(TSRMLS_C); |
|---|
| [114] | 1375 | xc_gc_deletes(TSRMLS_C); |
|---|
| [27] | 1376 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 1377 | xc_coverager_request_shutdown(TSRMLS_C); |
|---|
| [1] | 1378 | #endif |
|---|
| 1379 | } |
|---|
| 1380 | /* }}} */ |
|---|
| [92] | 1381 | /* {{{ PHP_GINIT_FUNCTION(xcache) */ |
|---|
| 1382 | static |
|---|
| 1383 | #ifdef PHP_GINIT_FUNCTION |
|---|
| 1384 | PHP_GINIT_FUNCTION(xcache) |
|---|
| 1385 | #else |
|---|
| 1386 | void xc_init_globals(zend_xcache_globals* xcache_globals TSRMLS_DC) |
|---|
| 1387 | #endif |
|---|
| [1] | 1388 | { |
|---|
| [92] | 1389 | memset(xcache_globals, 0, sizeof(zend_xcache_globals)); |
|---|
| [1] | 1390 | } |
|---|
| 1391 | /* }}} */ |
|---|
| [92] | 1392 | /* {{{ PHP_GSHUTDOWN_FUNCTION(xcache) */ |
|---|
| 1393 | static |
|---|
| 1394 | #ifdef PHP_GSHUTDOWN_FUNCTION |
|---|
| 1395 | PHP_GSHUTDOWN_FUNCTION(xcache) |
|---|
| 1396 | #else |
|---|
| 1397 | void xc_shutdown_globals(zend_xcache_globals* xcache_globals TSRMLS_DC) |
|---|
| 1398 | #endif |
|---|
| [1] | 1399 | { |
|---|
| 1400 | int i; |
|---|
| 1401 | |
|---|
| [92] | 1402 | if (xcache_globals->php_holds != NULL) { |
|---|
| [1] | 1403 | for (i = 0; i < xc_php_hcache.size; i ++) { |
|---|
| [92] | 1404 | xc_stack_destroy(&xcache_globals->php_holds[i]); |
|---|
| [1] | 1405 | } |
|---|
| [92] | 1406 | free(xcache_globals->php_holds); |
|---|
| 1407 | xcache_globals->php_holds = NULL; |
|---|
| [1] | 1408 | } |
|---|
| 1409 | |
|---|
| [92] | 1410 | if (xcache_globals->var_holds != NULL) { |
|---|
| [1] | 1411 | for (i = 0; i < xc_var_hcache.size; i ++) { |
|---|
| [92] | 1412 | xc_stack_destroy(&xcache_globals->var_holds[i]); |
|---|
| [1] | 1413 | } |
|---|
| [92] | 1414 | free(xcache_globals->var_holds); |
|---|
| 1415 | xcache_globals->var_holds = NULL; |
|---|
| [1] | 1416 | } |
|---|
| 1417 | } |
|---|
| 1418 | /* }}} */ |
|---|
| 1419 | |
|---|
| 1420 | /* user functions */ |
|---|
| [55] | 1421 | static int xcache_admin_auth_check(TSRMLS_D) /* {{{ */ |
|---|
| [34] | 1422 | { |
|---|
| 1423 | zval **server = NULL; |
|---|
| 1424 | zval **user = NULL; |
|---|
| 1425 | zval **pass = NULL; |
|---|
| 1426 | char *admin_user = NULL; |
|---|
| 1427 | char *admin_pass = NULL; |
|---|
| 1428 | HashTable *ht; |
|---|
| 1429 | |
|---|
| 1430 | if (cfg_get_string("xcache.admin.user", &admin_user) == FAILURE || !admin_user[0]) { |
|---|
| 1431 | admin_user = NULL; |
|---|
| 1432 | } |
|---|
| 1433 | if (cfg_get_string("xcache.admin.pass", &admin_pass) == FAILURE || !admin_pass[0]) { |
|---|
| 1434 | admin_pass = NULL; |
|---|
| 1435 | } |
|---|
| 1436 | |
|---|
| 1437 | if (admin_user == NULL || admin_pass == NULL) { |
|---|
| 1438 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "xcache.admin.user and xcache.admin.pass is required"); |
|---|
| 1439 | zend_bailout(); |
|---|
| 1440 | } |
|---|
| 1441 | if (strlen(admin_pass) != 32) { |
|---|
| 1442 | 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)); |
|---|
| 1443 | zend_bailout(); |
|---|
| 1444 | } |
|---|
| 1445 | |
|---|
| [105] | 1446 | #ifdef ZEND_ENGINE_2_1 |
|---|
| [113] | 1447 | zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1 TSRMLS_CC); |
|---|
| [105] | 1448 | #endif |
|---|
| [34] | 1449 | if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &server) != SUCCESS || Z_TYPE_PP(server) != IS_ARRAY) { |
|---|
| 1450 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "_SERVER is corrupted"); |
|---|
| 1451 | zend_bailout(); |
|---|
| 1452 | } |
|---|
| 1453 | ht = HASH_OF((*server)); |
|---|
| 1454 | |
|---|
| 1455 | if (zend_hash_find(ht, "PHP_AUTH_USER", sizeof("PHP_AUTH_USER"), (void **) &user) == FAILURE) { |
|---|
| 1456 | user = NULL; |
|---|
| 1457 | } |
|---|
| 1458 | else if (Z_TYPE_PP(user) != IS_STRING) { |
|---|
| 1459 | user = NULL; |
|---|
| 1460 | } |
|---|
| 1461 | |
|---|
| 1462 | if (zend_hash_find(ht, "PHP_AUTH_PW", sizeof("PHP_AUTH_PW"), (void **) &pass) == FAILURE) { |
|---|
| 1463 | pass = NULL; |
|---|
| 1464 | } |
|---|
| 1465 | else if (Z_TYPE_PP(pass) != IS_STRING) { |
|---|
| 1466 | pass = NULL; |
|---|
| 1467 | } |
|---|
| 1468 | |
|---|
| 1469 | if (user != NULL && pass != NULL && strcmp(admin_user, Z_STRVAL_PP(user)) == 0) { |
|---|
| 1470 | PHP_MD5_CTX context; |
|---|
| 1471 | char md5str[33]; |
|---|
| 1472 | unsigned char digest[16]; |
|---|
| 1473 | |
|---|
| 1474 | PHP_MD5Init(&context); |
|---|
| [91] | 1475 | PHP_MD5Update(&context, (unsigned char *) Z_STRVAL_PP(pass), Z_STRLEN_PP(pass)); |
|---|
| [34] | 1476 | PHP_MD5Final(digest, &context); |
|---|
| 1477 | |
|---|
| 1478 | md5str[0] = '\0'; |
|---|
| 1479 | make_digest(md5str, digest); |
|---|
| 1480 | if (strcmp(admin_pass, md5str) == 0) { |
|---|
| 1481 | return 1; |
|---|
| 1482 | } |
|---|
| 1483 | } |
|---|
| 1484 | |
|---|
| 1485 | #define STR "WWW-authenticate: basic realm='XCache Administration'" |
|---|
| 1486 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
|---|
| 1487 | #undef STR |
|---|
| 1488 | #define STR "HTTP/1.0 401 Unauthorized" |
|---|
| 1489 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
|---|
| 1490 | #undef STR |
|---|
| 1491 | ZEND_PUTS("XCache Auth Failed. User and Password is case sense\n"); |
|---|
| 1492 | |
|---|
| 1493 | zend_bailout(); |
|---|
| 1494 | return 0; |
|---|
| 1495 | } |
|---|
| 1496 | /* }}} */ |
|---|
| 1497 | /* {{{ xcache_admin_operate */ |
|---|
| [1] | 1498 | typedef enum { XC_OP_COUNT, XC_OP_INFO, XC_OP_LIST, XC_OP_CLEAR } xcache_op_type; |
|---|
| [34] | 1499 | static void xcache_admin_operate(xcache_op_type optype, INTERNAL_FUNCTION_PARAMETERS) |
|---|
| [1] | 1500 | { |
|---|
| 1501 | long type; |
|---|
| 1502 | int size; |
|---|
| 1503 | xc_cache_t **caches, *cache; |
|---|
| 1504 | long id = 0; |
|---|
| 1505 | |
|---|
| [11] | 1506 | if (!xc_initized) { |
|---|
| 1507 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "XCache is not initized"); |
|---|
| 1508 | RETURN_FALSE; |
|---|
| 1509 | } |
|---|
| 1510 | |
|---|
| [34] | 1511 | xcache_admin_auth_check(TSRMLS_C); |
|---|
| 1512 | |
|---|
| [1] | 1513 | if (optype == XC_OP_COUNT) { |
|---|
| 1514 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type) == FAILURE) { |
|---|
| 1515 | return; |
|---|
| 1516 | } |
|---|
| 1517 | } |
|---|
| 1518 | else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &type, &id) == FAILURE) { |
|---|
| 1519 | return; |
|---|
| 1520 | } |
|---|
| 1521 | |
|---|
| 1522 | switch (type) { |
|---|
| 1523 | case XC_TYPE_PHP: |
|---|
| 1524 | size = xc_php_hcache.size; |
|---|
| 1525 | caches = xc_php_caches; |
|---|
| 1526 | break; |
|---|
| 1527 | |
|---|
| 1528 | case XC_TYPE_VAR: |
|---|
| 1529 | size = xc_var_hcache.size; |
|---|
| 1530 | caches = xc_var_caches; |
|---|
| 1531 | break; |
|---|
| 1532 | |
|---|
| 1533 | default: |
|---|
| 1534 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown type %ld", type); |
|---|
| 1535 | RETURN_FALSE; |
|---|
| 1536 | } |
|---|
| 1537 | |
|---|
| 1538 | switch (optype) { |
|---|
| 1539 | case XC_OP_COUNT: |
|---|
| 1540 | RETURN_LONG(size) |
|---|
| 1541 | break; |
|---|
| 1542 | |
|---|
| 1543 | case XC_OP_INFO: |
|---|
| 1544 | case XC_OP_LIST: |
|---|
| 1545 | if (id < 0 || id >= size) { |
|---|
| 1546 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists"); |
|---|
| 1547 | RETURN_FALSE; |
|---|
| 1548 | } |
|---|
| 1549 | |
|---|
| 1550 | array_init(return_value); |
|---|
| 1551 | |
|---|
| 1552 | cache = caches[id]; |
|---|
| 1553 | ENTER_LOCK(cache) { |
|---|
| 1554 | if (optype == XC_OP_INFO) { |
|---|
| [118] | 1555 | xc_fillinfo_dmz(type, cache, return_value TSRMLS_CC); |
|---|
| [1] | 1556 | } |
|---|
| 1557 | else { |
|---|
| 1558 | xc_filllist_dmz(cache, return_value TSRMLS_CC); |
|---|
| 1559 | } |
|---|
| 1560 | } LEAVE_LOCK(cache); |
|---|
| 1561 | break; |
|---|
| 1562 | case XC_OP_CLEAR: |
|---|
| 1563 | { |
|---|
| [141] | 1564 | xc_entry_t *e, *next; |
|---|
| [1] | 1565 | int i, c; |
|---|
| 1566 | |
|---|
| 1567 | if (id < 0 || id >= size) { |
|---|
| 1568 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists"); |
|---|
| 1569 | RETURN_FALSE; |
|---|
| 1570 | } |
|---|
| 1571 | |
|---|
| 1572 | cache = caches[id]; |
|---|
| 1573 | ENTER_LOCK(cache) { |
|---|
| 1574 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
|---|
| [141] | 1575 | for (e = cache->entries[i]; e; e = next) { |
|---|
| 1576 | next = e->next; |
|---|
| [1] | 1577 | xc_entry_remove_dmz(e TSRMLS_CC); |
|---|
| 1578 | } |
|---|
| 1579 | cache->entries[i] = NULL; |
|---|
| 1580 | } |
|---|
| 1581 | } LEAVE_LOCK(cache); |
|---|
| [114] | 1582 | xc_gc_deletes(TSRMLS_C); |
|---|
| [1] | 1583 | } |
|---|
| 1584 | break; |
|---|
| 1585 | |
|---|
| 1586 | default: |
|---|
| 1587 | assert(0); |
|---|
| 1588 | } |
|---|
| 1589 | } |
|---|
| 1590 | /* }}} */ |
|---|
| [9] | 1591 | /* {{{ proto int xcache_count(int type) |
|---|
| 1592 | Return count of cache on specified cache type */ |
|---|
| [1] | 1593 | PHP_FUNCTION(xcache_count) |
|---|
| 1594 | { |
|---|
| [34] | 1595 | xcache_admin_operate(XC_OP_COUNT, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| [1] | 1596 | } |
|---|
| 1597 | /* }}} */ |
|---|
| [9] | 1598 | /* {{{ proto array xcache_info(int type, int id) |
|---|
| 1599 | Get cache info by id on specified cache type */ |
|---|
| [1] | 1600 | PHP_FUNCTION(xcache_info) |
|---|
| 1601 | { |
|---|
| [34] | 1602 | xcache_admin_operate(XC_OP_INFO, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| [1] | 1603 | } |
|---|
| 1604 | /* }}} */ |
|---|
| [9] | 1605 | /* {{{ proto array xcache_list(int type, int id) |
|---|
| 1606 | Get cache entries list by id on specified cache type */ |
|---|
| [1] | 1607 | PHP_FUNCTION(xcache_list) |
|---|
| 1608 | { |
|---|
| [34] | 1609 | xcache_admin_operate(XC_OP_LIST, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| [1] | 1610 | } |
|---|
| 1611 | /* }}} */ |
|---|
| [9] | 1612 | /* {{{ proto array xcache_clear_cache(int type, int id) |
|---|
| 1613 | Clear cache by id on specified cache type */ |
|---|
| [1] | 1614 | PHP_FUNCTION(xcache_clear_cache) |
|---|
| 1615 | { |
|---|
| [34] | 1616 | xcache_admin_operate(XC_OP_CLEAR, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| [1] | 1617 | } |
|---|
| 1618 | /* }}} */ |
|---|
| 1619 | |
|---|
| 1620 | static int xc_entry_init_key_var(xc_entry_t *xce, zval *name TSRMLS_DC) /* {{{ */ |
|---|
| 1621 | { |
|---|
| 1622 | xc_hash_value_t hv; |
|---|
| 1623 | int cacheid; |
|---|
| 1624 | |
|---|
| 1625 | switch (Z_TYPE_P(name)) { |
|---|
| 1626 | #ifdef IS_UNICODE |
|---|
| 1627 | case IS_UNICODE: |
|---|
| 1628 | #endif |
|---|
| 1629 | case IS_STRING: |
|---|
| 1630 | break; |
|---|
| 1631 | default: |
|---|
| 1632 | #ifdef IS_UNICODE |
|---|
| 1633 | convert_to_text(name); |
|---|
| 1634 | #else |
|---|
| 1635 | convert_to_string(name); |
|---|
| 1636 | #endif |
|---|
| 1637 | } |
|---|
| 1638 | #ifdef IS_UNICODE |
|---|
| 1639 | xce->name_type = name->type; |
|---|
| 1640 | #endif |
|---|
| 1641 | xce->name = name->value; |
|---|
| 1642 | |
|---|
| [152] | 1643 | hv = xc_entry_hash_var(xce TSRMLS_CC); |
|---|
| [1] | 1644 | |
|---|
| 1645 | cacheid = (hv & xc_var_hcache.mask); |
|---|
| 1646 | xce->cache = xc_var_caches[cacheid]; |
|---|
| 1647 | hv >>= xc_var_hcache.bits; |
|---|
| 1648 | xce->hvalue = (hv & xc_var_hentry.mask); |
|---|
| 1649 | |
|---|
| 1650 | xce->type = XC_TYPE_VAR; |
|---|
| 1651 | return SUCCESS; |
|---|
| 1652 | } |
|---|
| 1653 | /* }}} */ |
|---|
| [9] | 1654 | /* {{{ proto mixed xcache_get(string name) |
|---|
| 1655 | Get cached data by specified name */ |
|---|
| [1] | 1656 | PHP_FUNCTION(xcache_get) |
|---|
| 1657 | { |
|---|
| 1658 | xc_entry_t xce, *stored_xce; |
|---|
| 1659 | xc_entry_data_var_t var; |
|---|
| 1660 | zval *name; |
|---|
| 1661 | |
|---|
| 1662 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
|---|
| 1663 | return; |
|---|
| 1664 | } |
|---|
| 1665 | xce.data.var = &var; |
|---|
| 1666 | xc_entry_init_key_var(&xce, name TSRMLS_CC); |
|---|
| 1667 | |
|---|
| 1668 | ENTER_LOCK(xce.cache) { |
|---|
| 1669 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 1670 | if (stored_xce) { |
|---|
| [114] | 1671 | if (XG(request_time) <= stored_xce->ctime + stored_xce->ttl) { |
|---|
| [1] | 1672 | xc_processor_restore_zval(return_value, stored_xce->data.var->value TSRMLS_CC); |
|---|
| 1673 | /* return */ |
|---|
| 1674 | break; |
|---|
| 1675 | } |
|---|
| 1676 | else { |
|---|
| [11] | 1677 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| [1] | 1678 | } |
|---|
| 1679 | } |
|---|
| 1680 | |
|---|
| 1681 | RETVAL_NULL(); |
|---|
| 1682 | } LEAVE_LOCK(xce.cache); |
|---|
| 1683 | } |
|---|
| 1684 | /* }}} */ |
|---|
| [9] | 1685 | /* {{{ proto bool xcache_set(string name, mixed value [, int ttl]) |
|---|
| 1686 | Store data to cache by specified name */ |
|---|
| [1] | 1687 | PHP_FUNCTION(xcache_set) |
|---|
| 1688 | { |
|---|
| 1689 | xc_entry_t xce, *stored_xce; |
|---|
| 1690 | xc_entry_data_var_t var; |
|---|
| 1691 | zval *name; |
|---|
| 1692 | zval *value; |
|---|
| 1693 | |
|---|
| [114] | 1694 | xce.ttl = XG(var_ttl); |
|---|
| 1695 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &name, &value, &xce.ttl) == FAILURE) { |
|---|
| [1] | 1696 | return; |
|---|
| 1697 | } |
|---|
| [114] | 1698 | |
|---|
| 1699 | /* max ttl */ |
|---|
| 1700 | if (xc_var_maxttl && (!xce.ttl || xce.ttl > xc_var_maxttl)) { |
|---|
| 1701 | xce.ttl = xc_var_maxttl; |
|---|
| 1702 | } |
|---|
| 1703 | |
|---|
| [1] | 1704 | xce.data.var = &var; |
|---|
| 1705 | xc_entry_init_key_var(&xce, name TSRMLS_CC); |
|---|
| 1706 | |
|---|
| 1707 | ENTER_LOCK(xce.cache) { |
|---|
| 1708 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 1709 | if (stored_xce) { |
|---|
| [11] | 1710 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| [1] | 1711 | } |
|---|
| 1712 | var.value = value; |
|---|
| [11] | 1713 | RETVAL_BOOL(xc_entry_store_dmz(&xce TSRMLS_CC) != NULL ? 1 : 0); |
|---|
| [1] | 1714 | } LEAVE_LOCK(xce.cache); |
|---|
| 1715 | } |
|---|
| 1716 | /* }}} */ |
|---|
| [9] | 1717 | /* {{{ proto bool xcache_isset(string name) |
|---|
| 1718 | Check if an entry exists in cache by specified name */ |
|---|
| [1] | 1719 | PHP_FUNCTION(xcache_isset) |
|---|
| 1720 | { |
|---|
| 1721 | xc_entry_t xce, *stored_xce; |
|---|
| 1722 | xc_entry_data_var_t var; |
|---|
| 1723 | zval *name; |
|---|
| 1724 | |
|---|
| 1725 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
|---|
| 1726 | return; |
|---|
| 1727 | } |
|---|
| 1728 | xce.data.var = &var; |
|---|
| 1729 | xc_entry_init_key_var(&xce, name TSRMLS_CC); |
|---|
| 1730 | |
|---|
| 1731 | ENTER_LOCK(xce.cache) { |
|---|
| 1732 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 1733 | if (stored_xce) { |
|---|
| [126] | 1734 | if (!VAR_ENTRY_EXPIRED(stored_xce)) { |
|---|
| [1] | 1735 | RETVAL_TRUE; |
|---|
| 1736 | /* return */ |
|---|
| 1737 | break; |
|---|
| 1738 | } |
|---|
| 1739 | else { |
|---|
| [11] | 1740 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| [1] | 1741 | } |
|---|
| 1742 | } |
|---|
| 1743 | |
|---|
| 1744 | RETVAL_FALSE; |
|---|
| 1745 | } LEAVE_LOCK(xce.cache); |
|---|
| 1746 | } |
|---|
| 1747 | /* }}} */ |
|---|
| [9] | 1748 | /* {{{ proto bool xcache_unset(string name) |
|---|
| 1749 | Unset existing data in cache by specified name */ |
|---|
| [1] | 1750 | PHP_FUNCTION(xcache_unset) |
|---|
| 1751 | { |
|---|
| 1752 | xc_entry_t xce, *stored_xce; |
|---|
| 1753 | xc_entry_data_var_t var; |
|---|
| 1754 | zval *name; |
|---|
| 1755 | |
|---|
| 1756 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
|---|
| 1757 | return; |
|---|
| 1758 | } |
|---|
| 1759 | xce.data.var = &var; |
|---|
| 1760 | xc_entry_init_key_var(&xce, name TSRMLS_CC); |
|---|
| 1761 | |
|---|
| 1762 | ENTER_LOCK(xce.cache) { |
|---|
| 1763 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 1764 | if (stored_xce) { |
|---|
| [11] | 1765 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| [1] | 1766 | RETVAL_TRUE; |
|---|
| 1767 | } |
|---|
| 1768 | else { |
|---|
| 1769 | RETVAL_FALSE; |
|---|
| 1770 | } |
|---|
| 1771 | } LEAVE_LOCK(xce.cache); |
|---|
| 1772 | } |
|---|
| 1773 | /* }}} */ |
|---|
| 1774 | static inline void xc_var_inc_dec(int inc, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */ |
|---|
| 1775 | { |
|---|
| 1776 | xc_entry_t xce, *stored_xce; |
|---|
| 1777 | xc_entry_data_var_t var, *stored_var; |
|---|
| 1778 | zval *name; |
|---|
| 1779 | long count = 1; |
|---|
| 1780 | long value = 0; |
|---|
| 1781 | zval oldzval; |
|---|
| 1782 | |
|---|
| [114] | 1783 | xce.ttl = XG(var_ttl); |
|---|
| 1784 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", &name, &count, &xce.ttl) == FAILURE) { |
|---|
| [1] | 1785 | return; |
|---|
| 1786 | } |
|---|
| [114] | 1787 | |
|---|
| 1788 | /* max ttl */ |
|---|
| 1789 | if (xc_var_maxttl && (!xce.ttl || xce.ttl > xc_var_maxttl)) { |
|---|
| 1790 | xce.ttl = xc_var_maxttl; |
|---|
| 1791 | } |
|---|
| 1792 | |
|---|
| [1] | 1793 | xce.data.var = &var; |
|---|
| 1794 | xc_entry_init_key_var(&xce, name TSRMLS_CC); |
|---|
| 1795 | |
|---|
| 1796 | ENTER_LOCK(xce.cache) { |
|---|
| 1797 | stored_xce = xc_entry_find_dmz(&xce TSRMLS_CC); |
|---|
| 1798 | if (stored_xce) { |
|---|
| 1799 | #ifdef DEBUG |
|---|
| 1800 | fprintf(stderr, "incdec: gotxce %s\n", xce.name.str.val); |
|---|
| 1801 | #endif |
|---|
| 1802 | /* timeout */ |
|---|
| [114] | 1803 | if (VAR_ENTRY_EXPIRED(stored_xce)) { |
|---|
| [1] | 1804 | #ifdef DEBUG |
|---|
| 1805 | fprintf(stderr, "incdec: expired\n"); |
|---|
| 1806 | #endif |
|---|
| [11] | 1807 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| [1] | 1808 | stored_xce = NULL; |
|---|
| 1809 | } |
|---|
| 1810 | else { |
|---|
| 1811 | /* do it in place */ |
|---|
| [114] | 1812 | stored_var = stored_xce->data.var; |
|---|
| [1] | 1813 | if (Z_TYPE_P(stored_var->value) == IS_LONG) { |
|---|
| [114] | 1814 | stored_xce->ctime = XG(request_time); |
|---|
| 1815 | stored_xce->ttl = xce.ttl; |
|---|
| [1] | 1816 | #ifdef DEBUG |
|---|
| 1817 | fprintf(stderr, "incdec: islong\n"); |
|---|
| 1818 | #endif |
|---|
| 1819 | value = Z_LVAL_P(stored_var->value); |
|---|
| 1820 | value += (inc == 1 ? count : - count); |
|---|
| 1821 | RETVAL_LONG(value); |
|---|
| 1822 | Z_LVAL_P(stored_var->value) = value; |
|---|
| [114] | 1823 | break; /* leave lock */ |
|---|
| [1] | 1824 | } |
|---|
| 1825 | else { |
|---|
| 1826 | #ifdef DEBUG |
|---|
| 1827 | fprintf(stderr, "incdec: notlong\n"); |
|---|
| 1828 | #endif |
|---|
| 1829 | xc_processor_restore_zval(&oldzval, stored_xce->data.var->value TSRMLS_CC); |
|---|
| 1830 | convert_to_long(&oldzval); |
|---|
| 1831 | value = Z_LVAL(oldzval); |
|---|
| 1832 | zval_dtor(&oldzval); |
|---|
| 1833 | } |
|---|
| 1834 | } |
|---|
| 1835 | } |
|---|
| 1836 | #ifdef DEBUG |
|---|
| 1837 | else { |
|---|
| 1838 | fprintf(stderr, "incdec: %s not found\n", xce.name.str.val); |
|---|
| 1839 | } |
|---|
| 1840 | #endif |
|---|
| 1841 | |
|---|
| 1842 | value += (inc == 1 ? count : - count); |
|---|
| 1843 | RETVAL_LONG(value); |
|---|
| 1844 | var.value = return_value; |
|---|
| [114] | 1845 | |
|---|
| [1] | 1846 | if (stored_xce) { |
|---|
| 1847 | xce.atime = stored_xce->atime; |
|---|
| 1848 | xce.ctime = stored_xce->ctime; |
|---|
| 1849 | xce.hits = stored_xce->hits; |
|---|
| [11] | 1850 | xc_entry_remove_dmz(stored_xce TSRMLS_CC); |
|---|
| [1] | 1851 | } |
|---|
| [11] | 1852 | xc_entry_store_dmz(&xce TSRMLS_CC); |
|---|
| [1] | 1853 | |
|---|
| 1854 | } LEAVE_LOCK(xce.cache); |
|---|
| 1855 | } |
|---|
| 1856 | /* }}} */ |
|---|
| [9] | 1857 | /* {{{ proto int xcache_inc(string name [, int value [, int ttl]]) |
|---|
| 1858 | Increase an int counter in cache by specified name, create it if not exists */ |
|---|
| [1] | 1859 | PHP_FUNCTION(xcache_inc) |
|---|
| 1860 | { |
|---|
| 1861 | xc_var_inc_dec(1, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1862 | } |
|---|
| 1863 | /* }}} */ |
|---|
| [9] | 1864 | /* {{{ proto int xcache_dec(string name [, int value [, int ttl]]) |
|---|
| 1865 | Decrease an int counter in cache by specified name, create it if not exists */ |
|---|
| [1] | 1866 | PHP_FUNCTION(xcache_dec) |
|---|
| 1867 | { |
|---|
| 1868 | xc_var_inc_dec(-1, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1869 | } |
|---|
| 1870 | /* }}} */ |
|---|
| [9] | 1871 | /* {{{ proto string xcache_asm(string filename) |
|---|
| 1872 | */ |
|---|
| [1] | 1873 | #ifdef HAVE_XCACHE_ASSEMBLER |
|---|
| 1874 | PHP_FUNCTION(xcache_asm) |
|---|
| 1875 | { |
|---|
| 1876 | } |
|---|
| 1877 | #endif |
|---|
| 1878 | /* }}} */ |
|---|
| 1879 | #ifdef HAVE_XCACHE_DISASSEMBLER |
|---|
| [9] | 1880 | /* {{{ proto array xcache_dasm_file(string filename) |
|---|
| 1881 | Disassemble file into opcode array by filename */ |
|---|
| [1] | 1882 | PHP_FUNCTION(xcache_dasm_file) |
|---|
| 1883 | { |
|---|
| 1884 | char *filename; |
|---|
| [143] | 1885 | int filename_len; |
|---|
| [1] | 1886 | |
|---|
| 1887 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) { |
|---|
| 1888 | return; |
|---|
| 1889 | } |
|---|
| 1890 | if (!filename_len) RETURN_FALSE; |
|---|
| 1891 | |
|---|
| 1892 | xc_dasm_file(return_value, filename TSRMLS_CC); |
|---|
| 1893 | } |
|---|
| 1894 | /* }}} */ |
|---|
| [9] | 1895 | /* {{{ proto array xcache_dasm_string(string code) |
|---|
| 1896 | Disassemble php code into opcode array */ |
|---|
| [1] | 1897 | PHP_FUNCTION(xcache_dasm_string) |
|---|
| 1898 | { |
|---|
| 1899 | zval *code; |
|---|
| 1900 | |
|---|
| 1901 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &code) == FAILURE) { |
|---|
| 1902 | return; |
|---|
| 1903 | } |
|---|
| 1904 | xc_dasm_string(return_value, code TSRMLS_CC); |
|---|
| 1905 | } |
|---|
| 1906 | /* }}} */ |
|---|
| 1907 | #endif |
|---|
| [9] | 1908 | /* {{{ proto string xcache_encode(string filename) |
|---|
| 1909 | Encode php file into XCache opcode encoded format */ |
|---|
| [1] | 1910 | #ifdef HAVE_XCACHE_ENCODER |
|---|
| 1911 | PHP_FUNCTION(xcache_encode) |
|---|
| 1912 | { |
|---|
| 1913 | } |
|---|
| 1914 | #endif |
|---|
| 1915 | /* }}} */ |
|---|
| [9] | 1916 | /* {{{ proto bool xcache_decode_file(string filename) |
|---|
| 1917 | Decode(load) opcode from XCache encoded format file */ |
|---|
| [1] | 1918 | #ifdef HAVE_XCACHE_DECODER |
|---|
| [9] | 1919 | PHP_FUNCTION(xcache_decode_file) |
|---|
| [1] | 1920 | { |
|---|
| 1921 | } |
|---|
| 1922 | #endif |
|---|
| 1923 | /* }}} */ |
|---|
| [9] | 1924 | /* {{{ proto bool xcache_decode_string(string data) |
|---|
| 1925 | Decode(load) opcode from XCache encoded format data */ |
|---|
| 1926 | #ifdef HAVE_XCACHE_DECODER |
|---|
| 1927 | PHP_FUNCTION(xcache_decode_string) |
|---|
| 1928 | { |
|---|
| 1929 | } |
|---|
| 1930 | #endif |
|---|
| 1931 | /* }}} */ |
|---|
| [1] | 1932 | /* {{{ xc_call_getter */ |
|---|
| 1933 | typedef const char *(xc_name_getter_t)(zend_uchar type); |
|---|
| 1934 | static void xc_call_getter(xc_name_getter_t getter, int count, INTERNAL_FUNCTION_PARAMETERS) |
|---|
| 1935 | { |
|---|
| 1936 | long spec; |
|---|
| 1937 | const char *name; |
|---|
| 1938 | |
|---|
| 1939 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &spec) == FAILURE) { |
|---|
| 1940 | return; |
|---|
| 1941 | } |
|---|
| 1942 | if (spec >= 0 && spec < count) { |
|---|
| [11] | 1943 | name = getter((zend_uchar) spec); |
|---|
| [1] | 1944 | if (name) { |
|---|
| 1945 | /* RETURN_STRING */ |
|---|
| 1946 | int len = strlen(name); |
|---|
| 1947 | return_value->value.str.len = len; |
|---|
| 1948 | return_value->value.str.val = estrndup(name, len); |
|---|
| 1949 | return_value->type = IS_STRING; |
|---|
| 1950 | return; |
|---|
| 1951 | } |
|---|
| 1952 | } |
|---|
| 1953 | RETURN_NULL(); |
|---|
| 1954 | } |
|---|
| 1955 | /* }}} */ |
|---|
| 1956 | /* {{{ proto string xcache_get_op_type(int op_type) */ |
|---|
| 1957 | PHP_FUNCTION(xcache_get_op_type) |
|---|
| 1958 | { |
|---|
| 1959 | xc_call_getter(xc_get_op_type, xc_get_op_type_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1960 | } |
|---|
| 1961 | /* }}} */ |
|---|
| 1962 | /* {{{ proto string xcache_get_data_type(int type) */ |
|---|
| 1963 | PHP_FUNCTION(xcache_get_data_type) |
|---|
| 1964 | { |
|---|
| 1965 | xc_call_getter(xc_get_data_type, xc_get_data_type_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1966 | } |
|---|
| 1967 | /* }}} */ |
|---|
| 1968 | /* {{{ proto string xcache_get_opcode(int opcode) */ |
|---|
| 1969 | PHP_FUNCTION(xcache_get_opcode) |
|---|
| 1970 | { |
|---|
| 1971 | xc_call_getter(xc_get_opcode, xc_get_opcode_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1972 | } |
|---|
| 1973 | /* }}} */ |
|---|
| 1974 | /* {{{ proto string xcache_get_op_spec(int op_type) */ |
|---|
| 1975 | PHP_FUNCTION(xcache_get_op_spec) |
|---|
| 1976 | { |
|---|
| 1977 | xc_call_getter(xc_get_op_spec, xc_get_op_spec_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
|---|
| 1978 | } |
|---|
| 1979 | /* }}} */ |
|---|
| [8] | 1980 | #ifdef HAVE_XCACHE_OPCODE_SPEC_DEF |
|---|
| [1] | 1981 | /* {{{ proto string xcache_get_opcode_spec(int opcode) */ |
|---|
| 1982 | PHP_FUNCTION(xcache_get_opcode_spec) |
|---|
| 1983 | { |
|---|
| 1984 | long spec; |
|---|
| 1985 | const xc_opcode_spec_t *opspec; |
|---|
| 1986 | |
|---|
| 1987 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &spec) == FAILURE) { |
|---|
| 1988 | return; |
|---|
| 1989 | } |
|---|
| [17] | 1990 | if ((zend_uchar) spec <= xc_get_opcode_spec_count()) { |
|---|
| [11] | 1991 | opspec = xc_get_opcode_spec((zend_uchar) spec); |
|---|
| [1] | 1992 | if (opspec) { |
|---|
| 1993 | array_init(return_value); |
|---|
| 1994 | add_assoc_long_ex(return_value, ZEND_STRS("ext"), opspec->ext); |
|---|
| 1995 | add_assoc_long_ex(return_value, ZEND_STRS("op1"), opspec->op1); |
|---|
| 1996 | add_assoc_long_ex(return_value, ZEND_STRS("op2"), opspec->op2); |
|---|
| 1997 | add_assoc_long_ex(return_value, ZEND_STRS("res"), opspec->res); |
|---|
| 1998 | return; |
|---|
| 1999 | } |
|---|
| 2000 | } |
|---|
| 2001 | RETURN_NULL(); |
|---|
| 2002 | } |
|---|
| 2003 | /* }}} */ |
|---|
| [8] | 2004 | #endif |
|---|
| [1] | 2005 | /* {{{ proto mixed xcache_get_special_value(zval value) */ |
|---|
| 2006 | PHP_FUNCTION(xcache_get_special_value) |
|---|
| 2007 | { |
|---|
| 2008 | zval *value; |
|---|
| 2009 | |
|---|
| 2010 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) { |
|---|
| 2011 | return; |
|---|
| 2012 | } |
|---|
| 2013 | |
|---|
| 2014 | if (value->type == IS_CONSTANT) { |
|---|
| 2015 | *return_value = *value; |
|---|
| 2016 | zval_copy_ctor(return_value); |
|---|
| 2017 | return_value->type = UNISW(IS_STRING, UG(unicode) ? IS_UNICODE : IS_STRING); |
|---|
| 2018 | return; |
|---|
| 2019 | } |
|---|
| 2020 | |
|---|
| 2021 | if (value->type == IS_CONSTANT_ARRAY) { |
|---|
| 2022 | *return_value = *value; |
|---|
| 2023 | zval_copy_ctor(return_value); |
|---|
| 2024 | return_value->type = IS_ARRAY; |
|---|
| 2025 | return; |
|---|
| 2026 | } |
|---|
| 2027 | |
|---|
| 2028 | RETURN_NULL(); |
|---|
| 2029 | } |
|---|
| 2030 | /* }}} */ |
|---|
| 2031 | /* {{{ proto string xcache_coredump(int op_type) */ |
|---|
| 2032 | PHP_FUNCTION(xcache_coredump) |
|---|
| 2033 | { |
|---|
| [9] | 2034 | if (xc_test) { |
|---|
| 2035 | raise(SIGSEGV); |
|---|
| 2036 | } |
|---|
| 2037 | else { |
|---|
| 2038 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "xcache.test must be enabled to test xcache_coredump()"); |
|---|
| 2039 | } |
|---|
| [1] | 2040 | } |
|---|
| 2041 | /* }}} */ |
|---|
| 2042 | /* {{{ proto string xcache_is_autoglobal(string name) */ |
|---|
| 2043 | PHP_FUNCTION(xcache_is_autoglobal) |
|---|
| 2044 | { |
|---|
| 2045 | char *name; |
|---|
| [143] | 2046 | int name_len; |
|---|
| [1] | 2047 | |
|---|
| 2048 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { |
|---|
| 2049 | return; |
|---|
| 2050 | } |
|---|
| 2051 | |
|---|
| 2052 | RETURN_BOOL(zend_hash_exists(CG(auto_globals), name, name_len + 1)); |
|---|
| 2053 | } |
|---|
| 2054 | /* }}} */ |
|---|
| 2055 | static function_entry xcache_functions[] = /* {{{ */ |
|---|
| 2056 | { |
|---|
| 2057 | PHP_FE(xcache_count, NULL) |
|---|
| 2058 | PHP_FE(xcache_info, NULL) |
|---|
| 2059 | PHP_FE(xcache_list, NULL) |
|---|
| 2060 | PHP_FE(xcache_clear_cache, NULL) |
|---|
| 2061 | PHP_FE(xcache_coredump, NULL) |
|---|
| 2062 | #ifdef HAVE_XCACHE_ASSEMBLER |
|---|
| 2063 | PHP_FE(xcache_asm, NULL) |
|---|
| 2064 | #endif |
|---|
| 2065 | #ifdef HAVE_XCACHE_DISASSEMBLER |
|---|
| 2066 | PHP_FE(xcache_dasm_file, NULL) |
|---|
| 2067 | PHP_FE(xcache_dasm_string, NULL) |
|---|
| 2068 | #endif |
|---|
| 2069 | #ifdef HAVE_XCACHE_ENCODER |
|---|
| 2070 | PHP_FE(xcache_encode, NULL) |
|---|
| 2071 | #endif |
|---|
| 2072 | #ifdef HAVE_XCACHE_DECODER |
|---|
| [9] | 2073 | PHP_FE(xcache_decode_file, NULL) |
|---|
| 2074 | PHP_FE(xcache_decode_string, NULL) |
|---|
| [1] | 2075 | #endif |
|---|
| [27] | 2076 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 2077 | PHP_FE(xcache_coverager_decode, NULL) |
|---|
| [204] | 2078 | PHP_FE(xcache_coverager_start, NULL) |
|---|
| 2079 | PHP_FE(xcache_coverager_stop, NULL) |
|---|
| 2080 | PHP_FE(xcache_coverager_get, NULL) |
|---|
| [1] | 2081 | #endif |
|---|
| 2082 | PHP_FE(xcache_get_special_value, NULL) |
|---|
| 2083 | PHP_FE(xcache_get_op_type, NULL) |
|---|
| 2084 | PHP_FE(xcache_get_data_type, NULL) |
|---|
| 2085 | PHP_FE(xcache_get_opcode, NULL) |
|---|
| [8] | 2086 | #ifdef HAVE_XCACHE_OPCODE_SPEC_DEF |
|---|
| [1] | 2087 | PHP_FE(xcache_get_opcode_spec, NULL) |
|---|
| [8] | 2088 | #endif |
|---|
| [1] | 2089 | PHP_FE(xcache_is_autoglobal, NULL) |
|---|
| 2090 | PHP_FE(xcache_inc, NULL) |
|---|
| 2091 | PHP_FE(xcache_dec, NULL) |
|---|
| 2092 | PHP_FE(xcache_get, NULL) |
|---|
| 2093 | PHP_FE(xcache_set, NULL) |
|---|
| 2094 | PHP_FE(xcache_isset, NULL) |
|---|
| 2095 | PHP_FE(xcache_unset, NULL) |
|---|
| 2096 | {NULL, NULL, NULL} |
|---|
| 2097 | }; |
|---|
| 2098 | /* }}} */ |
|---|
| 2099 | |
|---|
| [75] | 2100 | /* old signal handlers {{{ */ |
|---|
| 2101 | typedef void (*xc_sighandler_t)(int); |
|---|
| 2102 | #define FOREACH_SIG(sig) static xc_sighandler_t old_##sig##_handler = NULL |
|---|
| 2103 | #include "foreachcoresig.h" |
|---|
| 2104 | #undef FOREACH_SIG |
|---|
| 2105 | /* }}} */ |
|---|
| 2106 | static void xcache_signal_handler(int sig); |
|---|
| 2107 | static void xcache_restore_signal_handler() /* {{{ */ |
|---|
| [1] | 2108 | { |
|---|
| [75] | 2109 | #define FOREACH_SIG(sig) do { \ |
|---|
| 2110 | if (old_##sig##_handler != xcache_signal_handler) { \ |
|---|
| 2111 | signal(sig, old_##sig##_handler); \ |
|---|
| 2112 | } \ |
|---|
| 2113 | else { \ |
|---|
| 2114 | signal(sig, SIG_DFL); \ |
|---|
| 2115 | } \ |
|---|
| 2116 | } while (0) |
|---|
| 2117 | #include "foreachcoresig.h" |
|---|
| 2118 | #undef FOREACH_SIG |
|---|
| 2119 | } |
|---|
| 2120 | /* }}} */ |
|---|
| 2121 | static void xcache_init_signal_handler() /* {{{ */ |
|---|
| 2122 | { |
|---|
| 2123 | #define FOREACH_SIG(sig) \ |
|---|
| 2124 | old_##sig##_handler = signal(sig, xcache_signal_handler) |
|---|
| 2125 | #include "foreachcoresig.h" |
|---|
| 2126 | #undef FOREACH_SIG |
|---|
| 2127 | } |
|---|
| 2128 | /* }}} */ |
|---|
| 2129 | static void xcache_signal_handler(int sig) /* {{{ */ |
|---|
| 2130 | { |
|---|
| 2131 | xcache_restore_signal_handler(); |
|---|
| [1] | 2132 | if (xc_coredump_dir && xc_coredump_dir[0]) { |
|---|
| 2133 | chdir(xc_coredump_dir); |
|---|
| 2134 | } |
|---|
| [65] | 2135 | raise(sig); |
|---|
| [1] | 2136 | } |
|---|
| 2137 | /* }}} */ |
|---|
| 2138 | |
|---|
| 2139 | /* {{{ PHP_INI */ |
|---|
| 2140 | |
|---|
| [164] | 2141 | static PHP_INI_MH(xc_OnUpdateDummy) |
|---|
| 2142 | { |
|---|
| 2143 | return SUCCESS; |
|---|
| 2144 | } |
|---|
| 2145 | |
|---|
| 2146 | static PHP_INI_MH(xc_OnUpdateULong) |
|---|
| 2147 | { |
|---|
| 2148 | zend_ulong *p = (zend_ulong *) mh_arg1; |
|---|
| 2149 | |
|---|
| 2150 | *p = (zend_ulong) atoi(new_value); |
|---|
| 2151 | return SUCCESS; |
|---|
| 2152 | } |
|---|
| 2153 | |
|---|
| [1] | 2154 | static PHP_INI_MH(xc_OnUpdateBool) |
|---|
| 2155 | { |
|---|
| 2156 | zend_bool *p = (zend_bool *)mh_arg1; |
|---|
| 2157 | |
|---|
| 2158 | if (strncasecmp("on", new_value, sizeof("on"))) { |
|---|
| 2159 | *p = (zend_bool) atoi(new_value); |
|---|
| 2160 | } |
|---|
| 2161 | else { |
|---|
| 2162 | *p = (zend_bool) 1; |
|---|
| 2163 | } |
|---|
| 2164 | return SUCCESS; |
|---|
| 2165 | } |
|---|
| 2166 | |
|---|
| 2167 | static PHP_INI_MH(xc_OnUpdateString) |
|---|
| 2168 | { |
|---|
| 2169 | char **p = (char**)mh_arg1; |
|---|
| 2170 | if (*p) { |
|---|
| 2171 | pefree(*p, 1); |
|---|
| 2172 | } |
|---|
| 2173 | *p = pemalloc(strlen(new_value) + 1, 1); |
|---|
| 2174 | strcpy(*p, new_value); |
|---|
| 2175 | return SUCCESS; |
|---|
| 2176 | } |
|---|
| [82] | 2177 | |
|---|
| [114] | 2178 | #ifndef ZEND_ENGINE_2 |
|---|
| 2179 | #define OnUpdateLong OnUpdateInt |
|---|
| [1] | 2180 | #endif |
|---|
| 2181 | |
|---|
| [21] | 2182 | #ifdef ZEND_WIN32 |
|---|
| 2183 | # define DEFAULT_PATH "xcache" |
|---|
| 2184 | #else |
|---|
| 2185 | # define DEFAULT_PATH "/dev/zero" |
|---|
| 2186 | #endif |
|---|
| [1] | 2187 | PHP_INI_BEGIN() |
|---|
| [21] | 2188 | PHP_INI_ENTRY1 ("xcache.mmap_path", DEFAULT_PATH, PHP_INI_SYSTEM, xc_OnUpdateString, &xc_mmap_path) |
|---|
| [1] | 2189 | PHP_INI_ENTRY1 ("xcache.coredump_directory", "", PHP_INI_SYSTEM, xc_OnUpdateString, &xc_coredump_dir) |
|---|
| 2190 | PHP_INI_ENTRY1 ("xcache.test", "0", PHP_INI_SYSTEM, xc_OnUpdateBool, &xc_test) |
|---|
| 2191 | PHP_INI_ENTRY1 ("xcache.readonly_protection", "0", PHP_INI_SYSTEM, xc_OnUpdateBool, &xc_readonly_protection) |
|---|
| [164] | 2192 | /* opcode cache */ |
|---|
| 2193 | PHP_INI_ENTRY1 ("xcache.size", "0", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2194 | PHP_INI_ENTRY1 ("xcache.count", "1", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2195 | PHP_INI_ENTRY1 ("xcache.slots", "8K", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2196 | PHP_INI_ENTRY1 ("xcache.shm_scheme", "mmap", PHP_INI_SYSTEM, xc_OnUpdateString, &xc_shm_scheme) |
|---|
| 2197 | PHP_INI_ENTRY1 ("xcache.ttl", "0", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_php_ttl) |
|---|
| 2198 | PHP_INI_ENTRY1 ("xcache.gc_interval", "0", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_php_gc_interval) |
|---|
| 2199 | /* var cache */ |
|---|
| 2200 | PHP_INI_ENTRY1 ("xcache.var_size", "0", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2201 | PHP_INI_ENTRY1 ("xcache.var_count", "1", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2202 | PHP_INI_ENTRY1 ("xcache.var_slots", "8K", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| 2203 | PHP_INI_ENTRY1 ("xcache.var_maxttl", "0", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_var_maxttl) |
|---|
| 2204 | PHP_INI_ENTRY1 ("xcache.var_gc_interval", "120", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_var_gc_interval) |
|---|
| [1] | 2205 | |
|---|
| 2206 | STD_PHP_INI_BOOLEAN("xcache.cacher", "1", PHP_INI_ALL, OnUpdateBool, cacher, zend_xcache_globals, xcache_globals) |
|---|
| [165] | 2207 | STD_PHP_INI_BOOLEAN("xcache.stat", "1", PHP_INI_ALL, OnUpdateBool, stat, zend_xcache_globals, xcache_globals) |
|---|
| [1] | 2208 | #ifdef HAVE_XCACHE_OPTIMIZER |
|---|
| 2209 | STD_PHP_INI_BOOLEAN("xcache.optimizer", "0", PHP_INI_ALL, OnUpdateBool, optimizer, zend_xcache_globals, xcache_globals) |
|---|
| 2210 | #endif |
|---|
| [164] | 2211 | STD_PHP_INI_BOOLEAN("xcache.var_ttl", "0", PHP_INI_ALL, OnUpdateLong, var_ttl, zend_xcache_globals, xcache_globals) |
|---|
| [27] | 2212 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| [204] | 2213 | STD_PHP_INI_BOOLEAN("xcache.coverager" , "0", PHP_INI_ALL, OnUpdateBool, coverager, zend_xcache_globals, xcache_globals) |
|---|
| 2214 | PHP_INI_ENTRY1 ("xcache.coveragedump_directory", "", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
|---|
| [1] | 2215 | #endif |
|---|
| 2216 | PHP_INI_END() |
|---|
| 2217 | /* }}} */ |
|---|
| 2218 | /* {{{ PHP_MINFO_FUNCTION(xcache) */ |
|---|
| 2219 | static PHP_MINFO_FUNCTION(xcache) |
|---|
| 2220 | { |
|---|
| [82] | 2221 | char buf[100]; |
|---|
| 2222 | char *ptr; |
|---|
| [163] | 2223 | int left, len; |
|---|
| 2224 | xc_shm_scheme_t *scheme; |
|---|
| [204] | 2225 | char *covdumpdir; |
|---|
| [82] | 2226 | |
|---|
| [1] | 2227 | php_info_print_table_start(); |
|---|
| [148] | 2228 | php_info_print_table_header(2, "XCache Support", "enabled"); |
|---|
| [1] | 2229 | php_info_print_table_row(2, "Version", XCACHE_VERSION); |
|---|
| [26] | 2230 | php_info_print_table_row(2, "Modules Built", XCACHE_MODULES); |
|---|
| [1] | 2231 | php_info_print_table_row(2, "Readonly Protection", xc_readonly_protection ? "enabled" : "N/A"); |
|---|
| [82] | 2232 | |
|---|
| 2233 | if (xc_php_size) { |
|---|
| 2234 | ptr = _php_math_number_format(xc_php_size, 0, '.', ','); |
|---|
| [163] | 2235 | snprintf(buf, sizeof(buf), "enabled, %s bytes, %d split(s), with %d slots each", ptr, xc_php_hcache.size, xc_php_hentry.size); |
|---|
| [82] | 2236 | php_info_print_table_row(2, "Opcode Cache", buf); |
|---|
| 2237 | efree(ptr); |
|---|
| 2238 | } |
|---|
| 2239 | else { |
|---|
| 2240 | php_info_print_table_row(2, "Opcode Cache", "disabled"); |
|---|
| 2241 | } |
|---|
| 2242 | if (xc_var_size) { |
|---|
| 2243 | ptr = _php_math_number_format(xc_var_size, 0, '.', ','); |
|---|
| [163] | 2244 | snprintf(buf, sizeof(buf), "enabled, %s bytes, %d split(s), with %d slots each", ptr, xc_var_hcache.size, xc_var_hentry.size); |
|---|
| [82] | 2245 | php_info_print_table_row(2, "Variable Cache", buf); |
|---|
| 2246 | efree(ptr); |
|---|
| 2247 | } |
|---|
| 2248 | else { |
|---|
| 2249 | php_info_print_table_row(2, "Variable Cache", "disabled"); |
|---|
| 2250 | } |
|---|
| [163] | 2251 | |
|---|
| 2252 | left = sizeof(buf); |
|---|
| 2253 | ptr = buf; |
|---|
| 2254 | buf[0] = '\0'; |
|---|
| 2255 | for (scheme = xc_shm_scheme_first(); scheme; scheme = xc_shm_scheme_next(scheme)) { |
|---|
| 2256 | len = snprintf(ptr, left, ptr == buf ? "%s" : ", %s", xc_shm_scheme_name(scheme)); |
|---|
| 2257 | left -= len; |
|---|
| 2258 | ptr += len; |
|---|
| 2259 | } |
|---|
| 2260 | php_info_print_table_row(2, "Shared Memory Schemes", buf); |
|---|
| 2261 | |
|---|
| [26] | 2262 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| [204] | 2263 | if (cfg_get_string("xcache.coveragedump_directory", &covdumpdir) != SUCCESS || !covdumpdir[0]) { |
|---|
| 2264 | covdumpdir = NULL; |
|---|
| 2265 | } |
|---|
| 2266 | php_info_print_table_row(2, "Coverage Auto Dumper", XG(coverager) && covdumpdir ? "enabled" : "disabled"); |
|---|
| [26] | 2267 | #endif |
|---|
| [1] | 2268 | php_info_print_table_end(); |
|---|
| [82] | 2269 | |
|---|
| [1] | 2270 | DISPLAY_INI_ENTRIES(); |
|---|
| 2271 | } |
|---|
| 2272 | /* }}} */ |
|---|
| 2273 | /* {{{ extension startup */ |
|---|
| 2274 | static void xc_zend_extension_register(zend_extension *new_extension, DL_HANDLE handle) |
|---|
| 2275 | { |
|---|
| 2276 | zend_extension extension; |
|---|
| 2277 | |
|---|
| 2278 | extension = *new_extension; |
|---|
| 2279 | extension.handle = handle; |
|---|
| 2280 | |
|---|
| 2281 | zend_extension_dispatch_message(ZEND_EXTMSG_NEW_EXTENSION, &extension); |
|---|
| 2282 | |
|---|
| 2283 | zend_llist_add_element(&zend_extensions, &extension); |
|---|
| 2284 | #ifdef DEBUG |
|---|
| 2285 | fprintf(stderr, "registered\n"); |
|---|
| 2286 | #endif |
|---|
| 2287 | } |
|---|
| 2288 | |
|---|
| 2289 | /* dirty check */ |
|---|
| 2290 | #if defined(COMPILE_DL_XCACHE) && (defined(ZEND_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)) |
|---|
| 2291 | # define zend_append_version_info(x) do { } while (0) |
|---|
| 2292 | #else |
|---|
| 2293 | extern void zend_append_version_info(zend_extension *extension); |
|---|
| 2294 | #endif |
|---|
| 2295 | static int xc_zend_extension_startup(zend_extension *extension) |
|---|
| 2296 | { |
|---|
| 2297 | if (extension->startup) { |
|---|
| 2298 | if (extension->startup(extension) != SUCCESS) { |
|---|
| 2299 | return FAILURE; |
|---|
| 2300 | } |
|---|
| 2301 | zend_append_version_info(extension); |
|---|
| 2302 | } |
|---|
| 2303 | return SUCCESS; |
|---|
| 2304 | } |
|---|
| 2305 | /* }}} */ |
|---|
| [82] | 2306 | static int xc_config_hash(xc_hash_t *p, char *name, char *default_value) /* {{{ */ |
|---|
| 2307 | { |
|---|
| 2308 | int bits, size; |
|---|
| 2309 | char *value; |
|---|
| 2310 | |
|---|
| 2311 | if (cfg_get_string(name, &value) != SUCCESS) { |
|---|
| 2312 | value = default_value; |
|---|
| 2313 | } |
|---|
| 2314 | |
|---|
| 2315 | p->size = zend_atoi(value, strlen(value)); |
|---|
| 2316 | for (size = 1, bits = 1; size < p->size; bits ++, size <<= 1) { |
|---|
| 2317 | /* empty body */ |
|---|
| 2318 | } |
|---|
| 2319 | p->size = size; |
|---|
| 2320 | p->bits = bits; |
|---|
| 2321 | p->mask = size - 1; |
|---|
| 2322 | |
|---|
| 2323 | return SUCCESS; |
|---|
| 2324 | } |
|---|
| 2325 | /* }}} */ |
|---|
| [90] | 2326 | static int xc_config_long(zend_ulong *p, char *name, char *default_value) /* {{{ */ |
|---|
| [82] | 2327 | { |
|---|
| 2328 | char *value; |
|---|
| 2329 | |
|---|
| 2330 | if (cfg_get_string(name, &value) != SUCCESS) { |
|---|
| 2331 | value = default_value; |
|---|
| 2332 | } |
|---|
| 2333 | |
|---|
| 2334 | *p = zend_atoi(value, strlen(value)); |
|---|
| 2335 | return SUCCESS; |
|---|
| 2336 | } |
|---|
| 2337 | /* }}} */ |
|---|
| [1] | 2338 | /* {{{ PHP_MINIT_FUNCTION(xcache) */ |
|---|
| 2339 | static PHP_MINIT_FUNCTION(xcache) |
|---|
| 2340 | { |
|---|
| 2341 | char *env; |
|---|
| [189] | 2342 | zend_extension *ext; |
|---|
| 2343 | zend_llist_position lpos; |
|---|
| [1] | 2344 | |
|---|
| 2345 | xc_module_gotup = 1; |
|---|
| 2346 | if (!xc_zend_extension_gotup) { |
|---|
| 2347 | if (zend_get_extension(XCACHE_NAME) == NULL) { |
|---|
| 2348 | xc_zend_extension_register(&zend_extension_entry, 0); |
|---|
| 2349 | xc_zend_extension_startup(&zend_extension_entry); |
|---|
| 2350 | } |
|---|
| 2351 | } |
|---|
| 2352 | |
|---|
| [190] | 2353 | ext = zend_get_extension("Zend Optimizer"); |
|---|
| 2354 | if (ext) { |
|---|
| 2355 | /* zend_optimizer.optimization_level>0 is not compatible with other cacher, disabling */ |
|---|
| 2356 | ext->op_array_handler = NULL; |
|---|
| 2357 | } |
|---|
| [189] | 2358 | /* cache if there's an op_array_ctor */ |
|---|
| 2359 | for (ext = zend_llist_get_first_ex(&zend_extensions, &lpos); |
|---|
| 2360 | ext; |
|---|
| 2361 | ext = zend_llist_get_next_ex(&zend_extensions, &lpos)) { |
|---|
| 2362 | if (ext->op_array_ctor) { |
|---|
| 2363 | xc_have_op_array_ctor = 1; |
|---|
| 2364 | break; |
|---|
| 2365 | } |
|---|
| 2366 | } |
|---|
| 2367 | |
|---|
| 2368 | |
|---|
| [92] | 2369 | #ifndef PHP_GINIT |
|---|
| [17] | 2370 | ZEND_INIT_MODULE_GLOBALS(xcache, xc_init_globals, xc_shutdown_globals); |
|---|
| [92] | 2371 | #endif |
|---|
| [17] | 2372 | REGISTER_INI_ENTRIES(); |
|---|
| 2373 | |
|---|
| [1] | 2374 | if (strcmp(sapi_module.name, "cli") == 0) { |
|---|
| 2375 | if ((env = getenv("XCACHE_TEST")) != NULL) { |
|---|
| 2376 | zend_alter_ini_entry("xcache.test", sizeof("xcache.test"), env, strlen(env) + 1, PHP_INI_SYSTEM, PHP_INI_STAGE_STARTUP); |
|---|
| 2377 | } |
|---|
| 2378 | if (!xc_test) { |
|---|
| 2379 | /* disable cache for cli except for test */ |
|---|
| 2380 | xc_php_size = xc_var_size = 0; |
|---|
| 2381 | } |
|---|
| 2382 | } |
|---|
| 2383 | |
|---|
| [114] | 2384 | xc_config_long(&xc_php_size, "xcache.size", "0"); |
|---|
| 2385 | xc_config_hash(&xc_php_hcache, "xcache.count", "1"); |
|---|
| 2386 | xc_config_hash(&xc_php_hentry, "xcache.slots", "8K"); |
|---|
| [82] | 2387 | |
|---|
| [164] | 2388 | xc_config_long(&xc_var_size, "xcache.var_size", "0"); |
|---|
| 2389 | xc_config_hash(&xc_var_hcache, "xcache.var_count", "1"); |
|---|
| 2390 | xc_config_hash(&xc_var_hentry, "xcache.var_slots", "8K"); |
|---|
| [82] | 2391 | |
|---|
| [1] | 2392 | if (xc_php_size <= 0) { |
|---|
| 2393 | xc_php_size = xc_php_hcache.size = 0; |
|---|
| 2394 | } |
|---|
| 2395 | if (xc_var_size <= 0) { |
|---|
| 2396 | xc_var_size = xc_var_hcache.size = 0; |
|---|
| 2397 | } |
|---|
| 2398 | |
|---|
| [65] | 2399 | if (xc_coredump_dir && xc_coredump_dir[0]) { |
|---|
| [75] | 2400 | xcache_init_signal_handler(); |
|---|
| [65] | 2401 | } |
|---|
| [1] | 2402 | |
|---|
| 2403 | xc_init_constant(module_number TSRMLS_CC); |
|---|
| [148] | 2404 | xc_shm_init_modules(); |
|---|
| [1] | 2405 | |
|---|
| 2406 | if ((xc_php_size || xc_var_size) && xc_mmap_path && xc_mmap_path[0]) { |
|---|
| 2407 | if (!xc_init(module_number TSRMLS_CC)) { |
|---|
| [21] | 2408 | zend_error(E_ERROR, "XCache: Cannot init"); |
|---|
| [1] | 2409 | goto err_init; |
|---|
| 2410 | } |
|---|
| 2411 | xc_initized = 1; |
|---|
| 2412 | } |
|---|
| 2413 | |
|---|
| [27] | 2414 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 2415 | xc_coverager_init(module_number TSRMLS_CC); |
|---|
| [1] | 2416 | #endif |
|---|
| 2417 | |
|---|
| 2418 | return SUCCESS; |
|---|
| 2419 | |
|---|
| 2420 | err_init: |
|---|
| 2421 | return FAILURE; |
|---|
| 2422 | } |
|---|
| 2423 | /* }}} */ |
|---|
| 2424 | /* {{{ PHP_MSHUTDOWN_FUNCTION(xcache) */ |
|---|
| 2425 | static PHP_MSHUTDOWN_FUNCTION(xcache) |
|---|
| 2426 | { |
|---|
| 2427 | if (xc_initized) { |
|---|
| 2428 | xc_destroy(); |
|---|
| 2429 | xc_initized = 0; |
|---|
| 2430 | } |
|---|
| 2431 | if (xc_mmap_path) { |
|---|
| 2432 | pefree(xc_mmap_path, 1); |
|---|
| 2433 | xc_mmap_path = NULL; |
|---|
| 2434 | } |
|---|
| [148] | 2435 | if (xc_shm_scheme) { |
|---|
| 2436 | pefree(xc_shm_scheme, 1); |
|---|
| 2437 | xc_shm_scheme = NULL; |
|---|
| 2438 | } |
|---|
| [1] | 2439 | |
|---|
| [27] | 2440 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 2441 | xc_coverager_destroy(); |
|---|
| [1] | 2442 | #endif |
|---|
| 2443 | |
|---|
| [65] | 2444 | if (xc_coredump_dir && xc_coredump_dir[0]) { |
|---|
| [75] | 2445 | xcache_restore_signal_handler(); |
|---|
| [65] | 2446 | } |
|---|
| [1] | 2447 | if (xc_coredump_dir) { |
|---|
| 2448 | pefree(xc_coredump_dir, 1); |
|---|
| 2449 | xc_coredump_dir = NULL; |
|---|
| 2450 | } |
|---|
| [92] | 2451 | #ifndef PHP_GINIT |
|---|
| 2452 | # ifdef ZTS |
|---|
| [25] | 2453 | ts_free_id(xcache_globals_id); |
|---|
| [92] | 2454 | # else |
|---|
| [1] | 2455 | xc_shutdown_globals(&xcache_globals TSRMLS_CC); |
|---|
| [92] | 2456 | # endif |
|---|
| [1] | 2457 | #endif |
|---|
| 2458 | |
|---|
| 2459 | UNREGISTER_INI_ENTRIES(); |
|---|
| 2460 | return SUCCESS; |
|---|
| 2461 | } |
|---|
| 2462 | /* }}} */ |
|---|
| 2463 | /* {{{ PHP_RINIT_FUNCTION(xcache) */ |
|---|
| 2464 | static PHP_RINIT_FUNCTION(xcache) |
|---|
| 2465 | { |
|---|
| 2466 | xc_request_init(TSRMLS_C); |
|---|
| 2467 | return SUCCESS; |
|---|
| 2468 | } |
|---|
| 2469 | /* }}} */ |
|---|
| 2470 | /* {{{ PHP_RSHUTDOWN_FUNCTION(xcache) */ |
|---|
| 2471 | #ifndef ZEND_ENGINE_2 |
|---|
| 2472 | static PHP_RSHUTDOWN_FUNCTION(xcache) |
|---|
| 2473 | #else |
|---|
| 2474 | static ZEND_MODULE_POST_ZEND_DEACTIVATE_D(xcache) |
|---|
| 2475 | #endif |
|---|
| 2476 | { |
|---|
| [18] | 2477 | #ifdef ZEND_ENGINE_2 |
|---|
| [11] | 2478 | TSRMLS_FETCH(); |
|---|
| [18] | 2479 | #endif |
|---|
| [11] | 2480 | |
|---|
| [1] | 2481 | xc_request_shutdown(TSRMLS_C); |
|---|
| 2482 | return SUCCESS; |
|---|
| 2483 | } |
|---|
| 2484 | /* }}} */ |
|---|
| 2485 | /* {{{ module definition structure */ |
|---|
| 2486 | |
|---|
| 2487 | zend_module_entry xcache_module_entry = { |
|---|
| 2488 | STANDARD_MODULE_HEADER, |
|---|
| [21] | 2489 | "XCache", |
|---|
| [1] | 2490 | xcache_functions, |
|---|
| 2491 | PHP_MINIT(xcache), |
|---|
| 2492 | PHP_MSHUTDOWN(xcache), |
|---|
| 2493 | PHP_RINIT(xcache), |
|---|
| 2494 | #ifndef ZEND_ENGINE_2 |
|---|
| 2495 | PHP_RSHUTDOWN(xcache), |
|---|
| 2496 | #else |
|---|
| 2497 | NULL, |
|---|
| 2498 | #endif |
|---|
| 2499 | PHP_MINFO(xcache), |
|---|
| 2500 | XCACHE_VERSION, |
|---|
| [92] | 2501 | #ifdef PHP_GINIT |
|---|
| 2502 | PHP_MODULE_GLOBALS(xcache), |
|---|
| 2503 | PHP_GINIT(xcache), |
|---|
| 2504 | PHP_GSHUTDOWN(xcache), |
|---|
| 2505 | #endif |
|---|
| [1] | 2506 | #ifdef ZEND_ENGINE_2 |
|---|
| 2507 | ZEND_MODULE_POST_ZEND_DEACTIVATE_N(xcache), |
|---|
| 2508 | #else |
|---|
| 2509 | NULL, |
|---|
| 2510 | NULL, |
|---|
| 2511 | #endif |
|---|
| 2512 | STANDARD_MODULE_PROPERTIES_EX |
|---|
| 2513 | }; |
|---|
| 2514 | |
|---|
| 2515 | #ifdef COMPILE_DL_XCACHE |
|---|
| 2516 | ZEND_GET_MODULE(xcache) |
|---|
| 2517 | #endif |
|---|
| 2518 | /* }}} */ |
|---|
| [190] | 2519 | static startup_func_t xc_last_ext_startup; |
|---|
| 2520 | static zend_llist_element *xc_llist_element; |
|---|
| [196] | 2521 | static int xc_ptr_compare_func(void *p1, void *p2) /* {{{ */ |
|---|
| [190] | 2522 | { |
|---|
| 2523 | return p1 == p2; |
|---|
| 2524 | } |
|---|
| 2525 | /* }}} */ |
|---|
| 2526 | static int xc_zend_startup_last(zend_extension *extension) /* {{{ */ |
|---|
| 2527 | { |
|---|
| 2528 | /* restore */ |
|---|
| 2529 | extension->startup = xc_last_ext_startup; |
|---|
| 2530 | if (extension->startup) { |
|---|
| 2531 | if (extension->startup(extension) != SUCCESS) { |
|---|
| 2532 | return FAILURE; |
|---|
| 2533 | } |
|---|
| 2534 | } |
|---|
| 2535 | xc_zend_extension_register(&zend_extension_entry, 0); |
|---|
| 2536 | if (!xc_module_gotup) { |
|---|
| 2537 | return zend_startup_module(&xcache_module_entry); |
|---|
| 2538 | } |
|---|
| 2539 | return SUCCESS; |
|---|
| 2540 | } |
|---|
| 2541 | /* }}} */ |
|---|
| [1] | 2542 | ZEND_DLEXPORT int xcache_zend_startup(zend_extension *extension) /* {{{ */ |
|---|
| 2543 | { |
|---|
| 2544 | if (xc_zend_extension_gotup) { |
|---|
| 2545 | return FAILURE; |
|---|
| 2546 | } |
|---|
| 2547 | xc_zend_extension_gotup = 1; |
|---|
| [190] | 2548 | xc_llist_element = NULL; |
|---|
| 2549 | if (zend_llist_count(&zend_extensions) > 1) { |
|---|
| 2550 | zend_llist_position lpos; |
|---|
| 2551 | zend_extension *ext; |
|---|
| 2552 | llist_dtor_func_t dtor; |
|---|
| 2553 | |
|---|
| 2554 | ext = zend_get_extension(XCACHE_NAME); |
|---|
| 2555 | assert(ext); |
|---|
| 2556 | dtor = zend_extensions.dtor; /* avoid dtor */ |
|---|
| 2557 | zend_extensions.dtor = NULL; |
|---|
| 2558 | zend_llist_del_element(&zend_extensions, ext, xc_ptr_compare_func); |
|---|
| 2559 | zend_extensions.dtor = dtor; |
|---|
| 2560 | |
|---|
| 2561 | ext = (zend_extension *) zend_llist_get_last_ex(&zend_extensions, &lpos); |
|---|
| 2562 | assert(ext); |
|---|
| 2563 | xc_last_ext_startup = ext->startup; |
|---|
| 2564 | ext->startup = xc_zend_startup_last; |
|---|
| 2565 | } |
|---|
| 2566 | else if (!xc_module_gotup) { |
|---|
| [1] | 2567 | return zend_startup_module(&xcache_module_entry); |
|---|
| 2568 | } |
|---|
| 2569 | return SUCCESS; |
|---|
| 2570 | } |
|---|
| 2571 | /* }}} */ |
|---|
| 2572 | ZEND_DLEXPORT void xcache_zend_shutdown(zend_extension *extension) /* {{{ */ |
|---|
| 2573 | { |
|---|
| 2574 | /* empty */ |
|---|
| 2575 | } |
|---|
| 2576 | /* }}} */ |
|---|
| 2577 | ZEND_DLEXPORT void xcache_statement_handler(zend_op_array *op_array) /* {{{ */ |
|---|
| 2578 | { |
|---|
| [27] | 2579 | #ifdef HAVE_XCACHE_COVERAGER |
|---|
| 2580 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_STMT); |
|---|
| [1] | 2581 | #endif |
|---|
| 2582 | } |
|---|
| 2583 | /* }}} */ |
|---|
| 2584 | ZEND_DLEXPORT void xcache_fcall_begin_handler(zend_op_array *op_array) /* {{{ */ |
|---|
| 2585 | { |
|---|
| 2586 | #if 0 |
|---|
| [27] | 2587 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_FCALL_BEGIN); |
|---|
| [1] | 2588 | #endif |
|---|
| 2589 | } |
|---|
| 2590 | /* }}} */ |
|---|
| 2591 | ZEND_DLEXPORT void xcache_fcall_end_handler(zend_op_array *op_array) /* {{{ */ |
|---|
| 2592 | { |
|---|
| 2593 | #if 0 |
|---|
| [27] | 2594 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_FCALL_END); |
|---|
| [1] | 2595 | #endif |
|---|
| 2596 | } |
|---|
| 2597 | /* }}} */ |
|---|
| 2598 | /* {{{ zend extension definition structure */ |
|---|
| 2599 | ZEND_DLEXPORT zend_extension zend_extension_entry = { |
|---|
| 2600 | XCACHE_NAME, |
|---|
| 2601 | XCACHE_VERSION, |
|---|
| 2602 | XCACHE_AUTHOR, |
|---|
| 2603 | XCACHE_URL, |
|---|
| 2604 | XCACHE_COPYRIGHT, |
|---|
| 2605 | xcache_zend_startup, |
|---|
| 2606 | xcache_zend_shutdown, |
|---|
| 2607 | NULL, /* activate_func_t */ |
|---|
| 2608 | NULL, /* deactivate_func_t */ |
|---|
| 2609 | NULL, /* message_handler_func_t */ |
|---|
| 2610 | NULL, /* op_array_handler_func_t */ |
|---|
| 2611 | xcache_statement_handler, |
|---|
| 2612 | xcache_fcall_begin_handler, |
|---|
| 2613 | xcache_fcall_end_handler, |
|---|
| 2614 | NULL, /* op_array_ctor_func_t */ |
|---|
| 2615 | NULL, /* op_array_dtor_func_t */ |
|---|
| 2616 | STANDARD_ZEND_EXTENSION_PROPERTIES |
|---|
| 2617 | }; |
|---|
| 2618 | |
|---|
| 2619 | #ifndef ZEND_EXT_API |
|---|
| 2620 | # define ZEND_EXT_API ZEND_DLEXPORT |
|---|
| 2621 | #endif |
|---|
| 2622 | #if COMPILE_DL_XCACHE |
|---|
| 2623 | ZEND_EXTENSION(); |
|---|
| 2624 | #endif |
|---|
| 2625 | /* }}} */ |
|---|