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