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