1 | |
---|
2 | #if 0 |
---|
3 | #define XCACHE_DEBUG |
---|
4 | #endif |
---|
5 | |
---|
6 | #if 0 |
---|
7 | #define SHOW_DPRINT |
---|
8 | #endif |
---|
9 | |
---|
10 | /* {{{ macros */ |
---|
11 | #include <stdlib.h> |
---|
12 | #include <stdio.h> |
---|
13 | #include <string.h> |
---|
14 | |
---|
15 | #include <signal.h> |
---|
16 | |
---|
17 | #include "php.h" |
---|
18 | #include "ext/standard/info.h" |
---|
19 | #include "ext/standard/md5.h" |
---|
20 | #include "ext/standard/php_math.h" |
---|
21 | #include "ext/standard/php_string.h" |
---|
22 | #include "zend_extensions.h" |
---|
23 | #include "SAPI.h" |
---|
24 | |
---|
25 | #include "xcache.h" |
---|
26 | #ifdef ZEND_ENGINE_2_1 |
---|
27 | #include "ext/date/php_date.h" |
---|
28 | #endif |
---|
29 | #include "optimizer.h" |
---|
30 | #include "coverager.h" |
---|
31 | #include "disassembler.h" |
---|
32 | #include "align.h" |
---|
33 | #include "stack.h" |
---|
34 | #include "xcache_globals.h" |
---|
35 | #include "processor.h" |
---|
36 | #include "const_string.h" |
---|
37 | #include "opcode_spec.h" |
---|
38 | #include "utils.h" |
---|
39 | |
---|
40 | #define VAR_ENTRY_EXPIRED(pentry) ((pentry)->ttl && XG(request_time) > (pentry)->ctime + (pentry)->ttl) |
---|
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 | |
---|
45 | #define ENTER_LOCK_EX(x) \ |
---|
46 | xc_lock((x)->lck); \ |
---|
47 | zend_try { \ |
---|
48 | do |
---|
49 | #define LEAVE_LOCK_EX(x) \ |
---|
50 | while (0); \ |
---|
51 | } zend_catch { \ |
---|
52 | catched = 1; \ |
---|
53 | } zend_end_try(); \ |
---|
54 | xc_unlock((x)->lck) |
---|
55 | |
---|
56 | #define ENTER_LOCK(x) do { \ |
---|
57 | int catched = 0; \ |
---|
58 | ENTER_LOCK_EX(x) |
---|
59 | #define LEAVE_LOCK(x) \ |
---|
60 | LEAVE_LOCK_EX(x); \ |
---|
61 | if (catched) { \ |
---|
62 | zend_bailout(); \ |
---|
63 | } \ |
---|
64 | } while(0) |
---|
65 | |
---|
66 | /* }}} */ |
---|
67 | |
---|
68 | /* {{{ globals */ |
---|
69 | static char *xc_shm_scheme = NULL; |
---|
70 | static char *xc_mmap_path = NULL; |
---|
71 | static char *xc_coredump_dir = NULL; |
---|
72 | |
---|
73 | static xc_hash_t xc_php_hcache = {0}; |
---|
74 | static xc_hash_t xc_php_hentry = {0}; |
---|
75 | static xc_hash_t xc_var_hcache = {0}; |
---|
76 | static xc_hash_t xc_var_hentry = {0}; |
---|
77 | |
---|
78 | static zend_ulong xc_php_ttl = 0; |
---|
79 | static zend_ulong xc_var_maxttl = 0; |
---|
80 | |
---|
81 | enum { xc_deletes_gc_interval = 120 }; |
---|
82 | static zend_ulong xc_php_gc_interval = 0; |
---|
83 | static zend_ulong xc_var_gc_interval = 0; |
---|
84 | |
---|
85 | /* total size */ |
---|
86 | static zend_ulong xc_php_size = 0; |
---|
87 | static zend_ulong xc_var_size = 0; |
---|
88 | |
---|
89 | static xc_cache_t **xc_php_caches = NULL; |
---|
90 | static xc_cache_t **xc_var_caches = NULL; |
---|
91 | |
---|
92 | static zend_bool xc_initized = 0; |
---|
93 | static time_t xc_init_time = 0; |
---|
94 | static long unsigned xc_init_instance_id = 0; |
---|
95 | #ifdef ZTS |
---|
96 | static long unsigned xc_init_instance_subid = 0; |
---|
97 | #endif |
---|
98 | static zend_compile_file_t *origin_compile_file = NULL; |
---|
99 | static zend_compile_file_t *old_compile_file = NULL; |
---|
100 | static zend_llist_element *xc_llist_zend_extension = NULL; |
---|
101 | |
---|
102 | static zend_bool xc_test = 0; |
---|
103 | static zend_bool xc_readonly_protection = 0; |
---|
104 | |
---|
105 | zend_bool xc_have_op_array_ctor = 0; |
---|
106 | |
---|
107 | static zend_bool xc_module_gotup = 0; |
---|
108 | static zend_bool xc_zend_extension_gotup = 0; |
---|
109 | static zend_bool xc_zend_extension_faked = 0; |
---|
110 | #if !COMPILE_DL_XCACHE |
---|
111 | # define zend_extension_entry xcache_zend_extension_entry |
---|
112 | #endif |
---|
113 | ZEND_DLEXPORT zend_extension zend_extension_entry; |
---|
114 | ZEND_DECLARE_MODULE_GLOBALS(xcache); |
---|
115 | |
---|
116 | typedef enum { XC_TYPE_PHP, XC_TYPE_VAR } xc_entry_type_t; |
---|
117 | /* }}} */ |
---|
118 | |
---|
119 | /* any function in *_dmz is only safe be called within locked(single thread) area */ |
---|
120 | |
---|
121 | static void xc_php_add_dmz(xc_cache_t *cache, xc_entry_data_php_t *php) /* {{{ */ |
---|
122 | { |
---|
123 | xc_entry_data_php_t **head = &(cache->phps[php->hvalue]); |
---|
124 | php->next = *head; |
---|
125 | *head = php; |
---|
126 | cache->phps_count ++; |
---|
127 | } |
---|
128 | /* }}} */ |
---|
129 | static xc_entry_data_php_t *xc_php_store_dmz(xc_cache_t *cache, xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
---|
130 | { |
---|
131 | xc_entry_data_php_t *stored_php; |
---|
132 | |
---|
133 | php->hits = 0; |
---|
134 | php->refcount = 0; |
---|
135 | stored_php = xc_processor_store_xc_entry_data_php_t(cache, php TSRMLS_CC); |
---|
136 | if (stored_php) { |
---|
137 | xc_php_add_dmz(cache, stored_php); |
---|
138 | return stored_php; |
---|
139 | } |
---|
140 | else { |
---|
141 | cache->ooms ++; |
---|
142 | return NULL; |
---|
143 | } |
---|
144 | } |
---|
145 | /* }}} */ |
---|
146 | static xc_entry_data_php_t *xc_php_find_dmz(xc_cache_t *cache, xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
---|
147 | { |
---|
148 | xc_entry_data_php_t *p; |
---|
149 | for (p = cache->phps[php->hvalue]; p; p = (xc_entry_data_php_t *) p->next) { |
---|
150 | if (memcmp(&php->md5.digest, &p->md5.digest, sizeof(php->md5.digest)) == 0) { |
---|
151 | p->hits ++; |
---|
152 | return p; |
---|
153 | } |
---|
154 | } |
---|
155 | return NULL; |
---|
156 | } |
---|
157 | /* }}} */ |
---|
158 | static void xc_php_free_dmz(xc_cache_t *cache, xc_entry_data_php_t *php) /* {{{ */ |
---|
159 | { |
---|
160 | cache->mem->handlers->free(cache->mem, (xc_entry_data_php_t *)php); |
---|
161 | } |
---|
162 | /* }}} */ |
---|
163 | static void xc_php_addref_dmz(xc_entry_data_php_t *php) /* {{{ */ |
---|
164 | { |
---|
165 | php->refcount ++; |
---|
166 | } |
---|
167 | /* }}} */ |
---|
168 | static void xc_php_release_dmz(xc_cache_t *cache, xc_entry_data_php_t *php) /* {{{ */ |
---|
169 | { |
---|
170 | if (-- php->refcount == 0) { |
---|
171 | xc_entry_data_php_t **pp = &(cache->phps[php->hvalue]); |
---|
172 | xc_entry_data_php_t *p; |
---|
173 | for (p = *pp; p; pp = &(p->next), p = p->next) { |
---|
174 | if (memcmp(&php->md5.digest, &p->md5.digest, sizeof(php->md5.digest)) == 0) { |
---|
175 | /* unlink */ |
---|
176 | *pp = p->next; |
---|
177 | xc_php_free_dmz(cache, php); |
---|
178 | return; |
---|
179 | } |
---|
180 | } |
---|
181 | assert(0); |
---|
182 | } |
---|
183 | } |
---|
184 | /* }}} */ |
---|
185 | |
---|
186 | static inline int xc_entry_equal_dmz(xc_entry_type_t type, const xc_entry_t *entry1, const xc_entry_t *entry2) /* {{{ */ |
---|
187 | { |
---|
188 | /* this function isn't required but can be in dmz */ |
---|
189 | switch (type) { |
---|
190 | case XC_TYPE_PHP: |
---|
191 | #ifdef HAVE_INODE |
---|
192 | { |
---|
193 | const xc_entry_php_t *php_entry1 = (const xc_entry_php_t *) entry1; |
---|
194 | const xc_entry_php_t *php_entry2 = (const xc_entry_php_t *) entry2; |
---|
195 | if (php_entry1->file_inode) { |
---|
196 | return php_entry1->file_inode == php_entry2->file_inode |
---|
197 | && php_entry1->file_device == php_entry2->file_device; |
---|
198 | } |
---|
199 | } |
---|
200 | #endif |
---|
201 | /* fall */ |
---|
202 | |
---|
203 | case XC_TYPE_VAR: |
---|
204 | do { |
---|
205 | #ifdef IS_UNICODE |
---|
206 | if (entry1->name_type == IS_UNICODE) { |
---|
207 | if (entry1->name.ustr.len != entry2->name.ustr.len) { |
---|
208 | return 0; |
---|
209 | } |
---|
210 | return memcmp(entry1->name.ustr.val, entry2->name.ustr.val, (entry1->name.ustr.len + 1) * sizeof(UChar)) == 0; |
---|
211 | } |
---|
212 | #endif |
---|
213 | if (entry1->name.str.len != entry2->name.str.len) { |
---|
214 | return 0; |
---|
215 | } |
---|
216 | return memcmp(entry1->name.str.val, entry2->name.str.val, entry1->name.str.len + 1) == 0; |
---|
217 | |
---|
218 | } while(0); |
---|
219 | default: |
---|
220 | assert(0); |
---|
221 | } |
---|
222 | return 0; |
---|
223 | } |
---|
224 | /* }}} */ |
---|
225 | static inline int xc_entry_has_prefix_dmz(xc_entry_type_t type, xc_entry_t *entry, zval *prefix) /* {{{ */ |
---|
226 | { |
---|
227 | /* this function isn't required but can be in dmz */ |
---|
228 | |
---|
229 | #ifdef IS_UNICODE |
---|
230 | if (entry->name_type != prefix->type) { |
---|
231 | return 0; |
---|
232 | } |
---|
233 | |
---|
234 | if (entry->name_type == IS_UNICODE) { |
---|
235 | if (entry->name.ustr.len < Z_USTRLEN_P(prefix)) { |
---|
236 | return 0; |
---|
237 | } |
---|
238 | return memcmp(entry->name.ustr.val, Z_USTRVAL_P(prefix), Z_USTRLEN_P(prefix) * sizeof(UChar)) == 0; |
---|
239 | } |
---|
240 | #endif |
---|
241 | if (prefix->type != IS_STRING) { |
---|
242 | return 0; |
---|
243 | } |
---|
244 | |
---|
245 | if (entry->name.str.len < Z_STRLEN_P(prefix)) { |
---|
246 | return 0; |
---|
247 | } |
---|
248 | |
---|
249 | return memcmp(entry->name.str.val, Z_STRVAL_P(prefix), Z_STRLEN_P(prefix)) == 0; |
---|
250 | } |
---|
251 | /* }}} */ |
---|
252 | static void xc_entry_add_dmz(xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *xce) /* {{{ */ |
---|
253 | { |
---|
254 | xc_entry_t **head = &(cache->entries[entryslotid]); |
---|
255 | xce->next = *head; |
---|
256 | *head = xce; |
---|
257 | cache->entries_count ++; |
---|
258 | } |
---|
259 | /* }}} */ |
---|
260 | static xc_entry_t *xc_entry_store_dmz(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
---|
261 | { |
---|
262 | xc_entry_t *stored_xce; |
---|
263 | |
---|
264 | xce->hits = 0; |
---|
265 | xce->ctime = XG(request_time); |
---|
266 | xce->atime = XG(request_time); |
---|
267 | stored_xce = type == XC_TYPE_PHP |
---|
268 | ? (xc_entry_t *) xc_processor_store_xc_entry_php_t(cache, (xc_entry_php_t *) xce TSRMLS_CC) |
---|
269 | : (xc_entry_t *) xc_processor_store_xc_entry_var_t(cache, (xc_entry_var_t *) xce TSRMLS_CC); |
---|
270 | if (stored_xce) { |
---|
271 | xc_entry_add_dmz(cache, entryslotid, stored_xce); |
---|
272 | return stored_xce; |
---|
273 | } |
---|
274 | else { |
---|
275 | cache->ooms ++; |
---|
276 | return NULL; |
---|
277 | } |
---|
278 | } |
---|
279 | /* }}} */ |
---|
280 | static xc_entry_php_t *xc_entry_php_store_dmz(xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_php_t *xce TSRMLS_DC) /* {{{ */ |
---|
281 | { |
---|
282 | return (xc_entry_php_t *) xc_entry_store_dmz(XC_TYPE_PHP, cache, entryslotid, (xc_entry_t *) xce TSRMLS_CC); |
---|
283 | } |
---|
284 | /* }}} */ |
---|
285 | static xc_entry_var_t *xc_entry_var_store_dmz(xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_var_t *xce TSRMLS_DC) /* {{{ */ |
---|
286 | { |
---|
287 | return (xc_entry_var_t *) xc_entry_store_dmz(XC_TYPE_VAR, cache, entryslotid, (xc_entry_t *) xce TSRMLS_CC); |
---|
288 | } |
---|
289 | /* }}} */ |
---|
290 | static void xc_entry_free_real_dmz(xc_entry_type_t type, xc_cache_t *cache, volatile xc_entry_t *xce) /* {{{ */ |
---|
291 | { |
---|
292 | if (type == XC_TYPE_PHP) { |
---|
293 | xc_php_release_dmz(cache, ((xc_entry_php_t *) xce)->php); |
---|
294 | } |
---|
295 | cache->mem->handlers->free(cache->mem, (xc_entry_t *)xce); |
---|
296 | } |
---|
297 | /* }}} */ |
---|
298 | static void xc_entry_free_dmz(xc_entry_type_t type, xc_cache_t *cache, xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
---|
299 | { |
---|
300 | cache->entries_count --; |
---|
301 | if ((type == XC_TYPE_PHP ? ((xc_entry_php_t *) xce)->refcount : 0) == 0) { |
---|
302 | xc_entry_free_real_dmz(type, cache, xce); |
---|
303 | } |
---|
304 | else { |
---|
305 | xce->next = cache->deletes; |
---|
306 | cache->deletes = xce; |
---|
307 | xce->dtime = XG(request_time); |
---|
308 | cache->deletes_count ++; |
---|
309 | } |
---|
310 | return; |
---|
311 | } |
---|
312 | /* }}} */ |
---|
313 | static void xc_entry_remove_dmz(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
---|
314 | { |
---|
315 | xc_entry_t **pp = &(cache->entries[entryslotid]); |
---|
316 | xc_entry_t *p; |
---|
317 | for (p = *pp; p; pp = &(p->next), p = p->next) { |
---|
318 | if (xc_entry_equal_dmz(type, xce, p)) { |
---|
319 | /* unlink */ |
---|
320 | *pp = p->next; |
---|
321 | xc_entry_free_dmz(type, cache, xce TSRMLS_CC); |
---|
322 | return; |
---|
323 | } |
---|
324 | } |
---|
325 | assert(0); |
---|
326 | } |
---|
327 | /* }}} */ |
---|
328 | static xc_entry_t *xc_entry_find_dmz(xc_entry_type_t type, xc_cache_t *cache, xc_hash_value_t entryslotid, xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
---|
329 | { |
---|
330 | xc_entry_t *p; |
---|
331 | for (p = cache->entries[entryslotid]; p; p = p->next) { |
---|
332 | if (xc_entry_equal_dmz(type, xce, p)) { |
---|
333 | zend_bool fresh; |
---|
334 | switch (type) { |
---|
335 | case XC_TYPE_PHP: |
---|
336 | { |
---|
337 | xc_entry_php_t *p_php = (xc_entry_php_t *) p; |
---|
338 | xc_entry_php_t *xce_php = (xc_entry_php_t *) xce; |
---|
339 | fresh = p_php->file_mtime == xce_php->file_mtime && p_php->php->file_size == xce_php->php->file_size; |
---|
340 | } |
---|
341 | break; |
---|
342 | |
---|
343 | case XC_TYPE_VAR: |
---|
344 | { |
---|
345 | fresh = !VAR_ENTRY_EXPIRED(p); |
---|
346 | } |
---|
347 | break; |
---|
348 | |
---|
349 | default: |
---|
350 | assert(0); |
---|
351 | } |
---|
352 | |
---|
353 | if (fresh) { |
---|
354 | p->hits ++; |
---|
355 | p->atime = XG(request_time); |
---|
356 | return p; |
---|
357 | } |
---|
358 | |
---|
359 | xc_entry_remove_dmz(type, cache, entryslotid, p TSRMLS_CC); |
---|
360 | return NULL; |
---|
361 | } |
---|
362 | } |
---|
363 | return NULL; |
---|
364 | } |
---|
365 | /* }}} */ |
---|
366 | static void xc_entry_hold_php_dmz(xc_cache_t *cache, xc_entry_php_t *xce TSRMLS_DC) /* {{{ */ |
---|
367 | { |
---|
368 | TRACE("hold %s", xce->entry.name.str.val); |
---|
369 | xce->refcount ++; |
---|
370 | xc_stack_push(&XG(php_holds)[cache->cacheid], (void *)xce); |
---|
371 | } |
---|
372 | /* }}} */ |
---|
373 | #if 0 |
---|
374 | static void xc_entry_hold_var_dmz(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
---|
375 | { |
---|
376 | xce->refcount ++; |
---|
377 | xc_stack_push(&XG(var_holds)[xce->cache->cacheid], (void *)xce); |
---|
378 | } |
---|
379 | /* }}} */ |
---|
380 | #endif |
---|
381 | static inline zend_uint advance_wrapped(zend_uint val, zend_uint count) /* {{{ */ |
---|
382 | { |
---|
383 | if (val + 1 >= count) { |
---|
384 | return 0; |
---|
385 | } |
---|
386 | return val + 1; |
---|
387 | } |
---|
388 | /* }}} */ |
---|
389 | static void xc_counters_inc(time_t *curtime, zend_uint *curslot, time_t period, zend_ulong *counters, zend_uint count TSRMLS_DC) /* {{{ */ |
---|
390 | { |
---|
391 | time_t n = XG(request_time) / period; |
---|
392 | if (*curtime != n) { |
---|
393 | zend_uint target_slot = n % count; |
---|
394 | if (n - *curtime > period) { |
---|
395 | memset(counters, 0, sizeof(counters[0]) * count); |
---|
396 | } |
---|
397 | else { |
---|
398 | zend_uint slot; |
---|
399 | for (slot = advance_wrapped(*curslot, count); |
---|
400 | slot != target_slot; |
---|
401 | slot = advance_wrapped(slot, count)) { |
---|
402 | counters[slot] = 0; |
---|
403 | } |
---|
404 | counters[target_slot] = 0; |
---|
405 | } |
---|
406 | *curtime = n; |
---|
407 | *curslot = target_slot; |
---|
408 | } |
---|
409 | counters[*curslot] ++; |
---|
410 | } |
---|
411 | /* }}} */ |
---|
412 | static void xc_cache_hit_dmz(xc_cache_t *cache TSRMLS_DC) /* {{{ */ |
---|
413 | { |
---|
414 | cache->hits ++; |
---|
415 | |
---|
416 | xc_counters_inc(&cache->hits_by_hour_cur_time |
---|
417 | , &cache->hits_by_hour_cur_slot, 60 * 60 |
---|
418 | , cache->hits_by_hour |
---|
419 | , sizeof(cache->hits_by_hour) / sizeof(cache->hits_by_hour[0]) |
---|
420 | TSRMLS_CC); |
---|
421 | |
---|
422 | xc_counters_inc(&cache->hits_by_second_cur_time |
---|
423 | , &cache->hits_by_second_cur_slot |
---|
424 | , 1 |
---|
425 | , cache->hits_by_second |
---|
426 | , sizeof(cache->hits_by_second) / sizeof(cache->hits_by_second[0]) |
---|
427 | TSRMLS_CC); |
---|
428 | } |
---|
429 | /* }}} */ |
---|
430 | |
---|
431 | /* helper function that loop through each entry */ |
---|
432 | #define XC_ENTRY_APPLY_FUNC(name) int name(xc_entry_t *entry TSRMLS_DC) |
---|
433 | typedef XC_ENTRY_APPLY_FUNC((*cache_apply_dmz_func_t)); |
---|
434 | static void xc_entry_apply_dmz(xc_entry_type_t type, xc_cache_t *cache, cache_apply_dmz_func_t apply_func TSRMLS_DC) /* {{{ */ |
---|
435 | { |
---|
436 | xc_entry_t *p, **pp; |
---|
437 | int i, c; |
---|
438 | |
---|
439 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
---|
440 | pp = &(cache->entries[i]); |
---|
441 | for (p = *pp; p; p = *pp) { |
---|
442 | if (apply_func(p TSRMLS_CC)) { |
---|
443 | /* unlink */ |
---|
444 | *pp = p->next; |
---|
445 | xc_entry_free_dmz(type, cache, p TSRMLS_CC); |
---|
446 | } |
---|
447 | else { |
---|
448 | pp = &(p->next); |
---|
449 | } |
---|
450 | } |
---|
451 | } |
---|
452 | } |
---|
453 | /* }}} */ |
---|
454 | |
---|
455 | #define XC_CACHE_APPLY_FUNC(name) void name(xc_cache_t *cache TSRMLS_DC) |
---|
456 | /* call graph: |
---|
457 | * xc_gc_expires_php -> xc_gc_expires_one -> xc_entry_apply_dmz -> xc_gc_expires_php_entry_dmz |
---|
458 | * xc_gc_expires_var -> xc_gc_expires_one -> xc_entry_apply_dmz -> xc_gc_expires_var_entry_dmz |
---|
459 | */ |
---|
460 | static XC_ENTRY_APPLY_FUNC(xc_gc_expires_php_entry_dmz) /* {{{ */ |
---|
461 | { |
---|
462 | TRACE("ttl %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) entry->atime, xc_php_ttl); |
---|
463 | if (XG(request_time) > entry->atime + xc_php_ttl) { |
---|
464 | return 1; |
---|
465 | } |
---|
466 | return 0; |
---|
467 | } |
---|
468 | /* }}} */ |
---|
469 | static XC_ENTRY_APPLY_FUNC(xc_gc_expires_var_entry_dmz) /* {{{ */ |
---|
470 | { |
---|
471 | if (VAR_ENTRY_EXPIRED(entry)) { |
---|
472 | return 1; |
---|
473 | } |
---|
474 | return 0; |
---|
475 | } |
---|
476 | /* }}} */ |
---|
477 | static void xc_gc_expires_one(xc_entry_type_t type, xc_cache_t *cache, zend_ulong gc_interval, cache_apply_dmz_func_t apply_func TSRMLS_DC) /* {{{ */ |
---|
478 | { |
---|
479 | TRACE("interval %lu, %lu %lu", (zend_ulong) XG(request_time), (zend_ulong) cache->last_gc_expires, gc_interval); |
---|
480 | if (XG(request_time) - cache->last_gc_expires >= gc_interval) { |
---|
481 | ENTER_LOCK(cache) { |
---|
482 | if (XG(request_time) - cache->last_gc_expires >= gc_interval) { |
---|
483 | cache->last_gc_expires = XG(request_time); |
---|
484 | xc_entry_apply_dmz(type, cache, apply_func TSRMLS_CC); |
---|
485 | } |
---|
486 | } LEAVE_LOCK(cache); |
---|
487 | } |
---|
488 | } |
---|
489 | /* }}} */ |
---|
490 | static void xc_gc_expires_php(TSRMLS_D) /* {{{ */ |
---|
491 | { |
---|
492 | int i, c; |
---|
493 | |
---|
494 | if (!xc_php_ttl || !xc_php_gc_interval || !xc_php_caches) { |
---|
495 | return; |
---|
496 | } |
---|
497 | |
---|
498 | for (i = 0, c = xc_php_hcache.size; i < c; i ++) { |
---|
499 | xc_gc_expires_one(XC_TYPE_PHP, xc_php_caches[i], xc_php_gc_interval, xc_gc_expires_php_entry_dmz TSRMLS_CC); |
---|
500 | } |
---|
501 | } |
---|
502 | /* }}} */ |
---|
503 | static void xc_gc_expires_var(TSRMLS_D) /* {{{ */ |
---|
504 | { |
---|
505 | int i, c; |
---|
506 | |
---|
507 | if (!xc_var_gc_interval || !xc_var_caches) { |
---|
508 | return; |
---|
509 | } |
---|
510 | |
---|
511 | for (i = 0, c = xc_var_hcache.size; i < c; i ++) { |
---|
512 | xc_gc_expires_one(XC_TYPE_VAR, xc_var_caches[i], xc_var_gc_interval, xc_gc_expires_var_entry_dmz TSRMLS_CC); |
---|
513 | } |
---|
514 | } |
---|
515 | /* }}} */ |
---|
516 | |
---|
517 | static XC_CACHE_APPLY_FUNC(xc_gc_delete_dmz) /* {{{ */ |
---|
518 | { |
---|
519 | xc_entry_t *p, **pp; |
---|
520 | |
---|
521 | pp = &cache->deletes; |
---|
522 | for (p = *pp; p; p = *pp) { |
---|
523 | xc_entry_php_t *entry = (xc_entry_php_t *) p; |
---|
524 | if (XG(request_time) - p->dtime > 3600) { |
---|
525 | entry->refcount = 0; |
---|
526 | /* issue warning here */ |
---|
527 | } |
---|
528 | if (entry->refcount == 0) { |
---|
529 | /* unlink */ |
---|
530 | *pp = p->next; |
---|
531 | cache->deletes_count --; |
---|
532 | xc_entry_free_real_dmz(XC_TYPE_PHP, cache, p); |
---|
533 | } |
---|
534 | else { |
---|
535 | pp = &(p->next); |
---|
536 | } |
---|
537 | } |
---|
538 | } |
---|
539 | /* }}} */ |
---|
540 | static XC_CACHE_APPLY_FUNC(xc_gc_deletes_one) /* {{{ */ |
---|
541 | { |
---|
542 | if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) { |
---|
543 | ENTER_LOCK(cache) { |
---|
544 | if (cache->deletes && XG(request_time) - cache->last_gc_deletes > xc_deletes_gc_interval) { |
---|
545 | cache->last_gc_deletes = XG(request_time); |
---|
546 | xc_gc_delete_dmz(cache TSRMLS_CC); |
---|
547 | } |
---|
548 | } LEAVE_LOCK(cache); |
---|
549 | } |
---|
550 | } |
---|
551 | /* }}} */ |
---|
552 | static void xc_gc_deletes(TSRMLS_D) /* {{{ */ |
---|
553 | { |
---|
554 | int i, c; |
---|
555 | |
---|
556 | if (xc_php_caches) { |
---|
557 | for (i = 0, c = xc_php_hcache.size; i < c; i ++) { |
---|
558 | xc_gc_deletes_one(xc_php_caches[i] TSRMLS_CC); |
---|
559 | } |
---|
560 | } |
---|
561 | |
---|
562 | if (xc_var_caches) { |
---|
563 | for (i = 0, c = xc_var_hcache.size; i < c; i ++) { |
---|
564 | xc_gc_deletes_one(xc_var_caches[i] TSRMLS_CC); |
---|
565 | } |
---|
566 | } |
---|
567 | } |
---|
568 | /* }}} */ |
---|
569 | |
---|
570 | /* helper functions for user functions */ |
---|
571 | static void xc_fillinfo_dmz(int cachetype, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
---|
572 | { |
---|
573 | zval *blocks, *hits; |
---|
574 | int i; |
---|
575 | const xc_block_t *b; |
---|
576 | #ifndef NDEBUG |
---|
577 | xc_memsize_t avail = 0; |
---|
578 | #endif |
---|
579 | xc_mem_t *mem = cache->mem; |
---|
580 | const xc_mem_handlers_t *handlers = mem->handlers; |
---|
581 | zend_ulong interval; |
---|
582 | if (cachetype == XC_TYPE_PHP) { |
---|
583 | interval = xc_php_ttl ? xc_php_gc_interval : 0; |
---|
584 | } |
---|
585 | else { |
---|
586 | interval = xc_var_gc_interval; |
---|
587 | } |
---|
588 | |
---|
589 | add_assoc_long_ex(return_value, ZEND_STRS("slots"), cache->hentry->size); |
---|
590 | add_assoc_long_ex(return_value, ZEND_STRS("compiling"), cache->compiling); |
---|
591 | add_assoc_long_ex(return_value, ZEND_STRS("misses"), cache->misses); |
---|
592 | add_assoc_long_ex(return_value, ZEND_STRS("hits"), cache->hits); |
---|
593 | add_assoc_long_ex(return_value, ZEND_STRS("clogs"), cache->clogs); |
---|
594 | add_assoc_long_ex(return_value, ZEND_STRS("ooms"), cache->ooms); |
---|
595 | add_assoc_long_ex(return_value, ZEND_STRS("errors"), cache->errors); |
---|
596 | |
---|
597 | add_assoc_long_ex(return_value, ZEND_STRS("cached"), cache->entries_count); |
---|
598 | add_assoc_long_ex(return_value, ZEND_STRS("deleted"), cache->deletes_count); |
---|
599 | if (interval) { |
---|
600 | time_t gc = (cache->last_gc_expires + interval) - XG(request_time); |
---|
601 | add_assoc_long_ex(return_value, ZEND_STRS("gc"), gc > 0 ? gc : 0); |
---|
602 | } |
---|
603 | else { |
---|
604 | add_assoc_null_ex(return_value, ZEND_STRS("gc")); |
---|
605 | } |
---|
606 | MAKE_STD_ZVAL(hits); |
---|
607 | array_init(hits); |
---|
608 | for (i = 0; i < sizeof(cache->hits_by_hour) / sizeof(cache->hits_by_hour[0]); i ++) { |
---|
609 | add_next_index_long(hits, (long) cache->hits_by_hour[i]); |
---|
610 | } |
---|
611 | add_assoc_zval_ex(return_value, ZEND_STRS("hits_by_hour"), hits); |
---|
612 | |
---|
613 | MAKE_STD_ZVAL(hits); |
---|
614 | array_init(hits); |
---|
615 | for (i = 0; i < sizeof(cache->hits_by_second) / sizeof(cache->hits_by_second[0]); i ++) { |
---|
616 | add_next_index_long(hits, (long) cache->hits_by_second[i]); |
---|
617 | } |
---|
618 | add_assoc_zval_ex(return_value, ZEND_STRS("hits_by_second"), hits); |
---|
619 | |
---|
620 | MAKE_STD_ZVAL(blocks); |
---|
621 | array_init(blocks); |
---|
622 | |
---|
623 | add_assoc_long_ex(return_value, ZEND_STRS("size"), handlers->size(mem)); |
---|
624 | add_assoc_long_ex(return_value, ZEND_STRS("avail"), handlers->avail(mem)); |
---|
625 | add_assoc_bool_ex(return_value, ZEND_STRS("can_readonly"), xc_readonly_protection); |
---|
626 | |
---|
627 | for (b = handlers->freeblock_first(mem); b; b = handlers->freeblock_next(b)) { |
---|
628 | zval *bi; |
---|
629 | |
---|
630 | MAKE_STD_ZVAL(bi); |
---|
631 | array_init(bi); |
---|
632 | |
---|
633 | add_assoc_long_ex(bi, ZEND_STRS("size"), handlers->block_size(b)); |
---|
634 | add_assoc_long_ex(bi, ZEND_STRS("offset"), handlers->block_offset(mem, b)); |
---|
635 | add_next_index_zval(blocks, bi); |
---|
636 | #ifndef NDEBUG |
---|
637 | avail += handlers->block_size(b); |
---|
638 | #endif |
---|
639 | } |
---|
640 | add_assoc_zval_ex(return_value, ZEND_STRS("free_blocks"), blocks); |
---|
641 | #ifndef NDEBUG |
---|
642 | assert(avail == handlers->avail(mem)); |
---|
643 | #endif |
---|
644 | } |
---|
645 | /* }}} */ |
---|
646 | static void xc_fillentry_dmz(xc_entry_type_t type, const xc_entry_t *entry, xc_hash_value_t entryslotid, int del, zval *list TSRMLS_DC) /* {{{ */ |
---|
647 | { |
---|
648 | zval* ei; |
---|
649 | const xc_entry_data_php_t *php; |
---|
650 | xc_entry_php_t *entry_php = (xc_entry_php_t *) entry; |
---|
651 | |
---|
652 | ALLOC_INIT_ZVAL(ei); |
---|
653 | array_init(ei); |
---|
654 | |
---|
655 | add_assoc_long_ex(ei, ZEND_STRS("refcount"), entry_php->refcount); |
---|
656 | add_assoc_long_ex(ei, ZEND_STRS("hits"), entry->hits); |
---|
657 | add_assoc_long_ex(ei, ZEND_STRS("ctime"), entry->ctime); |
---|
658 | add_assoc_long_ex(ei, ZEND_STRS("atime"), entry->atime); |
---|
659 | add_assoc_long_ex(ei, ZEND_STRS("hvalue"), entryslotid); |
---|
660 | if (del) { |
---|
661 | add_assoc_long_ex(ei, ZEND_STRS("dtime"), entry->dtime); |
---|
662 | } |
---|
663 | #ifdef IS_UNICODE |
---|
664 | do { |
---|
665 | zval *zv; |
---|
666 | ALLOC_INIT_ZVAL(zv); |
---|
667 | switch (entry->name_type) { |
---|
668 | case IS_UNICODE: |
---|
669 | ZVAL_UNICODEL(zv, entry->name.ustr.val, entry->name.ustr.len, 1); |
---|
670 | break; |
---|
671 | case IS_STRING: |
---|
672 | ZVAL_STRINGL(zv, entry->name.str.val, entry->name.str.len, 1); |
---|
673 | break; |
---|
674 | default: |
---|
675 | assert(0); |
---|
676 | } |
---|
677 | zv->type = entry->name_type; |
---|
678 | add_assoc_zval_ex(ei, ZEND_STRS("name"), zv); |
---|
679 | } while (0); |
---|
680 | #else |
---|
681 | add_assoc_stringl_ex(ei, ZEND_STRS("name"), entry->name.str.val, entry->name.str.len, 1); |
---|
682 | #endif |
---|
683 | switch (type) { |
---|
684 | case XC_TYPE_PHP: |
---|
685 | php = entry_php->php; |
---|
686 | add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size + php->size); |
---|
687 | add_assoc_long_ex(ei, ZEND_STRS("phprefcount"), php->refcount); |
---|
688 | add_assoc_long_ex(ei, ZEND_STRS("file_size"), php->file_size); |
---|
689 | #ifdef HAVE_INODE |
---|
690 | add_assoc_long_ex(ei, ZEND_STRS("file_device"), entry_php->file_device); |
---|
691 | add_assoc_long_ex(ei, ZEND_STRS("file_inode"), entry_php->file_inode); |
---|
692 | #endif |
---|
693 | add_assoc_long_ex(ei, ZEND_STRS("file_mtime"), entry_php->file_mtime); |
---|
694 | |
---|
695 | #ifdef HAVE_XCACHE_CONSTANT |
---|
696 | add_assoc_long_ex(ei, ZEND_STRS("constinfo_cnt"), php->constinfo_cnt); |
---|
697 | #endif |
---|
698 | add_assoc_long_ex(ei, ZEND_STRS("function_cnt"), php->funcinfo_cnt); |
---|
699 | add_assoc_long_ex(ei, ZEND_STRS("class_cnt"), php->classinfo_cnt); |
---|
700 | #ifdef ZEND_ENGINE_2_1 |
---|
701 | add_assoc_long_ex(ei, ZEND_STRS("autoglobal_cnt"),php->autoglobal_cnt); |
---|
702 | #endif |
---|
703 | break; |
---|
704 | |
---|
705 | case XC_TYPE_VAR: |
---|
706 | add_assoc_long_ex(ei, ZEND_STRS("size"), entry->size); |
---|
707 | break; |
---|
708 | |
---|
709 | default: |
---|
710 | assert(0); |
---|
711 | } |
---|
712 | |
---|
713 | add_next_index_zval(list, ei); |
---|
714 | } |
---|
715 | /* }}} */ |
---|
716 | static void xc_filllist_dmz(xc_entry_type_t type, xc_cache_t *cache, zval *return_value TSRMLS_DC) /* {{{ */ |
---|
717 | { |
---|
718 | zval* list; |
---|
719 | int i, c; |
---|
720 | xc_entry_t *e; |
---|
721 | |
---|
722 | ALLOC_INIT_ZVAL(list); |
---|
723 | array_init(list); |
---|
724 | |
---|
725 | for (i = 0, c = cache->hentry->size; i < c; i ++) { |
---|
726 | for (e = cache->entries[i]; e; e = e->next) { |
---|
727 | xc_fillentry_dmz(type, e, i, 0, list TSRMLS_CC); |
---|
728 | } |
---|
729 | } |
---|
730 | add_assoc_zval(return_value, "cache_list", list); |
---|
731 | |
---|
732 | ALLOC_INIT_ZVAL(list); |
---|
733 | array_init(list); |
---|
734 | for (e = cache->deletes; e; e = e->next) { |
---|
735 | xc_fillentry_dmz(XC_TYPE_PHP, e, 0, 1, list TSRMLS_CC); |
---|
736 | } |
---|
737 | add_assoc_zval(return_value, "deleted_list", list); |
---|
738 | } |
---|
739 | /* }}} */ |
---|
740 | |
---|
741 | static zend_op_array *xc_entry_install(xc_entry_php_t *xce, zend_file_handle *h TSRMLS_DC) /* {{{ */ |
---|
742 | { |
---|
743 | zend_uint i; |
---|
744 | xc_entry_data_php_t *p = xce->php; |
---|
745 | zend_op_array *old_active_op_array = CG(active_op_array); |
---|
746 | #ifndef ZEND_ENGINE_2 |
---|
747 | ALLOCA_FLAG(use_heap) |
---|
748 | /* new ptr which is stored inside CG(class_table) */ |
---|
749 | xc_cest_t **new_cest_ptrs = (xc_cest_t **)my_do_alloca(sizeof(xc_cest_t*) * p->classinfo_cnt, use_heap); |
---|
750 | #endif |
---|
751 | |
---|
752 | CG(active_op_array) = p->op_array; |
---|
753 | |
---|
754 | #ifdef HAVE_XCACHE_CONSTANT |
---|
755 | /* install constant */ |
---|
756 | for (i = 0; i < p->constinfo_cnt; i ++) { |
---|
757 | xc_constinfo_t *ci = &p->constinfos[i]; |
---|
758 | xc_install_constant(xce->entry.name.str.val, &ci->constant, |
---|
759 | UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC); |
---|
760 | } |
---|
761 | #endif |
---|
762 | |
---|
763 | /* install function */ |
---|
764 | for (i = 0; i < p->funcinfo_cnt; i ++) { |
---|
765 | xc_funcinfo_t *fi = &p->funcinfos[i]; |
---|
766 | xc_install_function(xce->entry.name.str.val, &fi->func, |
---|
767 | UNISW(0, fi->type), fi->key, fi->key_size, fi->h TSRMLS_CC); |
---|
768 | } |
---|
769 | |
---|
770 | /* install class */ |
---|
771 | for (i = 0; i < p->classinfo_cnt; i ++) { |
---|
772 | xc_classinfo_t *ci = &p->classinfos[i]; |
---|
773 | #ifndef ZEND_ENGINE_2 |
---|
774 | zend_class_entry *ce = CestToCePtr(ci->cest); |
---|
775 | /* fix pointer to the be which inside class_table */ |
---|
776 | if (ce->parent) { |
---|
777 | zend_uint class_idx = (/* class_num */ (int) (long) ce->parent) - 1; |
---|
778 | assert(class_idx < i); |
---|
779 | ci->cest.parent = new_cest_ptrs[class_idx]; |
---|
780 | } |
---|
781 | new_cest_ptrs[i] = |
---|
782 | #endif |
---|
783 | #ifdef ZEND_COMPILE_DELAYED_BINDING |
---|
784 | xc_install_class(xce->entry.name.str.val, &ci->cest, -1, |
---|
785 | UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC); |
---|
786 | #else |
---|
787 | xc_install_class(xce->entry.name.str.val, &ci->cest, ci->oplineno, |
---|
788 | UNISW(0, ci->type), ci->key, ci->key_size, ci->h TSRMLS_CC); |
---|
789 | #endif |
---|
790 | } |
---|
791 | |
---|
792 | #ifdef ZEND_ENGINE_2_1 |
---|
793 | /* trigger auto_globals jit */ |
---|
794 | for (i = 0; i < p->autoglobal_cnt; i ++) { |
---|
795 | xc_autoglobal_t *aginfo = &p->autoglobals[i]; |
---|
796 | zend_u_is_auto_global(aginfo->type, aginfo->key, aginfo->key_len TSRMLS_CC); |
---|
797 | } |
---|
798 | #endif |
---|
799 | #ifdef XCACHE_ERROR_CACHING |
---|
800 | /* restore trigger errors */ |
---|
801 | for (i = 0; i < p->compilererror_cnt; i ++) { |
---|
802 | xc_compilererror_t *error = &p->compilererrors[i]; |
---|
803 | CG(zend_lineno) = error->lineno; |
---|
804 | zend_error(error->type, "%s", error->error); |
---|
805 | } |
---|
806 | CG(zend_lineno) = 0; |
---|
807 | #endif |
---|
808 | |
---|
809 | i = 1; |
---|
810 | zend_hash_add(&EG(included_files), xce->entry.name.str.val, xce->entry.name.str.len+1, (void *)&i, sizeof(int), NULL); |
---|
811 | if (h) { |
---|
812 | zend_llist_add_element(&CG(open_files), h); |
---|
813 | } |
---|
814 | |
---|
815 | #ifndef ZEND_ENGINE_2 |
---|
816 | my_free_alloca(new_cest_ptrs, use_heap); |
---|
817 | #endif |
---|
818 | CG(active_op_array) = old_active_op_array; |
---|
819 | return p->op_array; |
---|
820 | } |
---|
821 | /* }}} */ |
---|
822 | |
---|
823 | static inline void xc_entry_unholds_real(xc_stack_t *holds, xc_cache_t **caches, int cachecount TSRMLS_DC) /* {{{ */ |
---|
824 | { |
---|
825 | int i; |
---|
826 | xc_stack_t *s; |
---|
827 | xc_cache_t *cache; |
---|
828 | xc_entry_t *xce; |
---|
829 | |
---|
830 | for (i = 0; i < cachecount; i ++) { |
---|
831 | s = &holds[i]; |
---|
832 | TRACE("holded %d", xc_stack_count(s)); |
---|
833 | if (xc_stack_count(s)) { |
---|
834 | cache = caches[i]; |
---|
835 | ENTER_LOCK(cache) { |
---|
836 | while (xc_stack_count(s)) { |
---|
837 | xce = (xc_entry_t*) xc_stack_pop(s); |
---|
838 | TRACE("unhold %s", xce->name.str.val); |
---|
839 | ((xc_entry_php_t *) xce)->refcount ++; |
---|
840 | assert(((xc_entry_php_t *) xce)->refcount >= 0); |
---|
841 | } |
---|
842 | } LEAVE_LOCK(cache); |
---|
843 | } |
---|
844 | } |
---|
845 | } |
---|
846 | /* }}} */ |
---|
847 | static void xc_entry_unholds(TSRMLS_D) /* {{{ */ |
---|
848 | { |
---|
849 | if (xc_php_caches) { |
---|
850 | xc_entry_unholds_real(XG(php_holds), xc_php_caches, xc_php_hcache.size TSRMLS_CC); |
---|
851 | } |
---|
852 | |
---|
853 | if (xc_var_caches) { |
---|
854 | xc_entry_unholds_real(XG(var_holds), xc_var_caches, xc_var_hcache.size TSRMLS_CC); |
---|
855 | } |
---|
856 | } |
---|
857 | /* }}} */ |
---|
858 | static int xc_stat(const char *filename, const char *include_path, struct stat *pbuf TSRMLS_DC) /* {{{ */ |
---|
859 | { |
---|
860 | char filepath[MAXPATHLEN]; |
---|
861 | char *paths, *path; |
---|
862 | char *tokbuf; |
---|
863 | int size = strlen(include_path) + 1; |
---|
864 | char tokens[] = { DEFAULT_DIR_SEPARATOR, '\0' }; |
---|
865 | int ret; |
---|
866 | ALLOCA_FLAG(use_heap) |
---|
867 | |
---|
868 | paths = (char *)my_do_alloca(size, use_heap); |
---|
869 | memcpy(paths, include_path, size); |
---|
870 | |
---|
871 | for (path = php_strtok_r(paths, tokens, &tokbuf); path; path = php_strtok_r(NULL, tokens, &tokbuf)) { |
---|
872 | if (snprintf(filepath, sizeof(filepath), "%s/%s", path, filename) < MAXPATHLEN - 1) { |
---|
873 | if (VCWD_STAT(filepath, pbuf) == 0) { |
---|
874 | ret = SUCCESS; |
---|
875 | goto finish; |
---|
876 | } |
---|
877 | } |
---|
878 | } |
---|
879 | |
---|
880 | /* fall back to current directory */ |
---|
881 | if (zend_is_executing(TSRMLS_C)) { |
---|
882 | const char *executed_filename = zend_get_executed_filename(TSRMLS_C); |
---|
883 | if (executed_filename && executed_filename[0] != '[') { |
---|
884 | int len = strlen(executed_filename); |
---|
885 | while ((--len >= 0) && !IS_SLASH(executed_filename[len])) { |
---|
886 | /* skipped */ |
---|
887 | } |
---|
888 | if (len > 0 && len + strlen(filename) + 1 < MAXPATHLEN - 1) { |
---|
889 | strcpy(filepath, executed_filename); |
---|
890 | strcpy(filepath + len + 1, filename); |
---|
891 | if (VCWD_STAT(filepath, pbuf) == 0) { |
---|
892 | ret = SUCCESS; |
---|
893 | goto finish; |
---|
894 | } |
---|
895 | } |
---|
896 | } |
---|
897 | } |
---|
898 | |
---|
899 | ret = FAILURE; |
---|
900 | |
---|
901 | finish: |
---|
902 | my_free_alloca(paths, use_heap); |
---|
903 | |
---|
904 | return ret; |
---|
905 | } |
---|
906 | /* }}} */ |
---|
907 | |
---|
908 | #if 0 /* {{{ note about php hashing */ |
---|
909 | the folling note is written in the form of "got = from" |
---|
910 | |
---|
911 | TODO: open_basedir |
---|
912 | |
---|
913 | opened_path = stat || zend_compile_file |
---|
914 | |
---|
915 | phphashid = inode ? inode : hash(basename) |
---|
916 | md5key = md5(relativepath) |
---|
917 | md5hashid = hash(md5key) |
---|
918 | cachehashid = multislot ? hash(basename) : hash(phphashid) |
---|
919 | |
---|
920 | cached = phphashid -> md5key -> cachedmd5info -> cachedphp |
---|
921 | cachedphp = [phphashid, fullpath] |
---|
922 | restoredphp = [fullpath, phphashid] |
---|
923 | |
---|
924 | #endif /* }}} */ |
---|
925 | |
---|
926 | #define HASH(i) (i) |
---|
927 | #define HASH_ZSTR_L(t, s, l) HASH(zend_u_inline_hash_func((t), (s), ((l) + 1) * sizeof(UChar))) |
---|
928 | #define HASH_STR_S(s, l) HASH(zend_inline_hash_func((s), (l))) |
---|
929 | #define HASH_STR_L(s, l) HASH_STR_S((s), (l) + 1) |
---|
930 | #define HASH_STR(s) HASH_STR_L((s), strlen((s)) + 1) |
---|
931 | #define HASH_NUM(n) HASH(n) |
---|
932 | static inline xc_hash_value_t xc_hash_fold(xc_hash_value_t hvalue, const xc_hash_t *hasher) /* {{{ fold hash bits as needed */ |
---|
933 | { |
---|
934 | xc_hash_value_t folded = 0; |
---|
935 | while (hvalue) { |
---|
936 | folded ^= (hvalue & hasher->mask); |
---|
937 | hvalue >>= hasher->bits; |
---|
938 | } |
---|
939 | return folded; |
---|
940 | } |
---|
941 | /* }}} */ |
---|
942 | static inline xc_hash_value_t xc_entry_hash_name(xc_entry_t *xce TSRMLS_DC) /* {{{ */ |
---|
943 | { |
---|
944 | return UNISW(NOTHING, UG(unicode) ? HASH_ZSTR_L(xce->name_type, xce->name.uni.val, xce->name.uni.len) :) |
---|
945 | HASH_STR_L(xce->name.str.val, xce->name.str.len); |
---|
946 | } |
---|
947 | /* }}} */ |
---|
948 | static inline xc_hash_value_t xc_entry_hash_php_basename(xc_entry_php_t *xce TSRMLS_DC) /* {{{ */ |
---|
949 | { |
---|
950 | #ifdef IS_UNICODE |
---|
951 | if (UG(unicode) && xce->entry.name_type == IS_UNICODE) { |
---|
952 | zstr basename; |
---|
953 | size_t basename_len; |
---|
954 | php_u_basename(xce->entry.name.ustr.val, xce->entry.name.ustr.len, NULL, 0, &basename.u, &basename_len TSRMLS_CC); |
---|
955 | return HASH_ZSTR_L(IS_UNICODE, basename, basename_len); |
---|
956 | } |
---|
957 | else |
---|
958 | #endif |
---|
959 | { |
---|
960 | char *basename; |
---|
961 | xc_hash_value_t h; |
---|
962 | size_t basename_len; |
---|
963 | #ifdef ZEND_ENGINE_2 |
---|
964 | php_basename(xce->entry.name.str.val, xce->entry.name.str.len, "", 0, &basename, &basename_len TSRMLS_CC); |
---|
965 | #else |
---|
966 | basename = php_basename(xce->entry.name.str.val, xce->entry.name.str.len, "", 0); |
---|
967 | basename_len = strlen(basename); |
---|
968 | #endif |
---|
969 | h = HASH_STR_L(basename, basename_len); |
---|
970 | efree(basename); |
---|
971 | return h; |
---|
972 | } |
---|
973 | } |
---|
974 | /* }}} */ |
---|
975 | #define xc_entry_hash_var xc_entry_hash_name |
---|
976 | static inline xc_hash_value_t xc_entry_hash_php(xc_entry_php_t *xce TSRMLS_DC) /* {{{ */ |
---|
977 | { |
---|
978 | return |
---|
979 | #ifdef HAVE_INODE |
---|
980 | xce->file_inode ? HASH(xce->file_device + xce->file_inode) : |
---|
981 | #endif |
---|
982 | xc_entry_hash_php_basename(xce TSRMLS_CC); |
---|
983 | } |
---|
984 | /* }}} */ |
---|
985 | static void xc_entry_free_key_php(xc_entry_php_t *xce TSRMLS_DC) /* {{{ */ |
---|
986 | { |
---|
987 | #define X_FREE(var) do {\ |
---|
988 | if (xce->var) { \ |
---|
989 | efree(xce->var); \ |
---|
990 | } \ |
---|
991 | } while (0) |
---|
992 | X_FREE(dirpath); |
---|
993 | #ifdef IS_UNICODE |
---|
994 | X_FREE(ufilepath); |
---|
995 | X_FREE(udirpath); |
---|
996 | #endif |
---|
997 | |
---|
998 | #undef X_FREE |
---|
999 | } |
---|
1000 | /* }}} */ |
---|
1001 | |
---|
1002 | static int xc_entry_init_key_php(xc_entry_hash_t *entry_hash, xc_entry_php_t *xce, const char *filename TSRMLS_DC) /* {{{ */ |
---|
1003 | { |
---|
1004 | char opened_path_buffer[MAXPATHLEN]; |
---|
1005 | |
---|
1006 | if (!filename || !SG(request_info).path_translated) { |
---|
1007 | return FAILURE; |
---|
1008 | } |
---|
1009 | |
---|
1010 | if (strstr(filename, "://") != NULL) { |
---|
1011 | return FAILURE; |
---|
1012 | } |
---|
1013 | |
---|
1014 | if (XG(stat)) { |
---|
1015 | struct stat buf, *pbuf; |
---|
1016 | |
---|
1017 | if (strcmp(SG(request_info).path_translated, filename) == 0) { |
---|
1018 | /* sapi has already done this stat() for us */ |
---|
1019 | pbuf = sapi_get_stat(TSRMLS_C); |
---|
1020 | if (pbuf) { |
---|
1021 | goto stat_done; |
---|
1022 | } |
---|
1023 | } |
---|
1024 | |
---|
1025 | /* absolute path */ |
---|
1026 | pbuf = &buf; |
---|
1027 | if (IS_ABSOLUTE_PATH(filename, strlen(filename))) { |
---|
1028 | if (VCWD_STAT(filename, pbuf) != 0) { |
---|
1029 | return FAILURE; |
---|
1030 | } |
---|
1031 | goto stat_done; |
---|
1032 | } |
---|
1033 | |
---|
1034 | /* relative path */ |
---|
1035 | if (*filename == '.' && (IS_SLASH(filename[1]) || filename[1] == '.')) { |
---|
1036 | const char *ptr = filename + 1; |
---|
1037 | if (*ptr == '.') { |
---|
1038 | while (*(++ptr) == '.'); |
---|
1039 | if (!IS_SLASH(*ptr)) { |
---|
1040 | goto not_relative_path; |
---|
1041 | } |
---|
1042 | } |
---|
1043 | |
---|
1044 | if (VCWD_STAT(filename, pbuf) != 0) { |
---|
1045 | return FAILURE; |
---|
1046 | } |
---|
1047 | goto stat_done; |
---|
1048 | } |
---|
1049 | not_relative_path: |
---|
1050 | |
---|
1051 | /* use include_path */ |
---|
1052 | if (xc_stat(filename, PG(include_path), pbuf TSRMLS_CC) != SUCCESS) { |
---|
1053 | return FAILURE; |
---|
1054 | } |
---|
1055 | |
---|
1056 | /* fall */ |
---|
1057 | |
---|
1058 | stat_done: |
---|
1059 | { |
---|
1060 | time_t delta = XG(request_time) - pbuf->st_mtime; |
---|
1061 | if (abs(delta) < 2 && !xc_test) { |
---|
1062 | return FAILURE; |
---|
1063 | } |
---|
1064 | } |
---|
1065 | |
---|
1066 | xce->file_mtime = pbuf->st_mtime; |
---|
1067 | #ifdef HAVE_INODE |
---|
1068 | xce->file_device = pbuf->st_dev; |
---|
1069 | xce->file_inode = pbuf->st_ino; |
---|
1070 | #endif |
---|
1071 | xce->php->file_size = pbuf->st_size; |
---|
1072 | } |
---|
1073 | else { /* XG(inode) */ |
---|
1074 | xce->file_mtime = 0; |
---|
1075 | #ifdef HAVE_INODE |
---|
1076 | xce->file_device = 0; |
---|
1077 | xce->file_inode = 0; |
---|
1078 | #endif |
---|
1079 | xce->php->file_size = 0; |
---|
1080 | } |
---|
1081 | |
---|
1082 | #ifdef HAVE_INODE |
---|
1083 | if (!xce->file_inode) |
---|
1084 | #endif |
---|
1085 | { |
---|
1086 | /* hash on filename, let's expand it to real path */ |
---|
1087 | /* FIXME */ |
---|
1088 | filename = expand_filepath(filename, opened_path_buffer TSRMLS_CC); |
---|
1089 | if (filename == NULL) { |
---|
1090 | return FAILURE; |
---|
1091 | } |
---|
1092 | } |
---|
1093 | |
---|
1094 | UNISW(NOTHING, xce->entry.name_type = IS_STRING;) |
---|
1095 | xce->entry.name.str.val = (char *) filename; |
---|
1096 | xce->entry.name.str.len = strlen(filename); |
---|
1097 | |
---|
1098 | entry_hash->cacheslotid = xc_php_hcache.size > 1 ? xc_hash_fold(xc_entry_hash_php_basename(xce TSRMLS_CC), &xc_php_hcache) : 0; |
---|
1099 | entry_hash->entryslotid = xc_hash_fold(xc_entry_hash_php(xce TSRMLS_CC), &xc_php_hentry); |
---|
1100 | xce->filepath = NULL; |
---|
1101 | xce->dirpath = NULL; |
---|
1102 | #ifdef IS_UNICODE |
---|
1103 | xce->ufilepath = NULL; |
---|
1104 | xce->udirpath = NULL; |
---|
1105 | #endif |
---|
1106 | |
---|
1107 | return SUCCESS; |
---|
1108 | } |
---|
1109 | /* }}} */ |
---|
1110 | static inline xc_hash_value_t xc_php_hash_md5(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
---|
1111 | { |
---|
1112 | return HASH_STR_S(php->md5.digest, sizeof(php->md5.digest)); |
---|
1113 | } |
---|
1114 | /* }}} */ |
---|
1115 | static int xc_entry_init_key_php_md5(xc_cache_t *cache, xc_entry_data_php_t *php, xc_entry_php_t *xce TSRMLS_DC) /* {{{ */ |
---|
1116 | { |
---|
1117 | unsigned char buf[1024]; |
---|
1118 | PHP_MD5_CTX context; |
---|
1119 | int n; |
---|
1120 | php_stream *stream; |
---|
1121 | ulong old_rsid = EG(regular_list).nNextFreeElement; |
---|
1122 | |
---|
1123 | stream = php_stream_open_wrapper(xce->entry.name.str.val, "rb", USE_PATH | REPORT_ERRORS | ENFORCE_SAFE_MODE | STREAM_DISABLE_OPEN_BASEDIR, NULL); |
---|
1124 | if (!stream) { |
---|
1125 | return FAILURE; |
---|
1126 | } |
---|
1127 | |
---|
1128 | PHP_MD5Init(&context); |
---|
1129 | while ((n = php_stream_read(stream, (char *) buf, sizeof(buf))) > 0) { |
---|
1130 | PHP_MD5Update(&context, buf, n); |
---|
1131 | } |
---|
1132 | PHP_MD5Final((unsigned char *) php->md5.digest, &context); |
---|
1133 | |
---|
1134 | php_stream_close(stream); |
---|
1135 | if (EG(regular_list).nNextFreeElement == old_rsid + 1) { |
---|
1136 | EG(regular_list).nNextFreeElement = old_rsid; |
---|
1137 | } |
---|
1138 | |
---|
1139 | if (n < 0) { |
---|
1140 | return FAILURE; |
---|
1141 | } |
---|
1142 | |
---|
1143 | php->hvalue = (xc_php_hash_md5(php TSRMLS_CC) & cache->hphp->mask); |
---|
1144 | #ifdef XCACHE_DEBUG |
---|
1145 | { |
---|
1146 | char md5str[33]; |
---|
1147 | make_digest(md5str, (unsigned char *) php->md5.digest); |
---|
1148 | TRACE("md5 %s", md5str); |
---|
1149 | } |
---|
1150 | #endif |
---|
1151 | |
---|
1152 | return SUCCESS; |
---|
1153 | } |
---|
1154 | /* }}} */ |
---|
1155 | static void xc_entry_init_key_php_entry(xc_entry_php_t *xce, ZEND_24(const) char *filepath TSRMLS_DC) /* {{{*/ |
---|
1156 | { |
---|
1157 | xce->filepath = filepath; |
---|
1158 | xce->filepath_len = strlen(xce->filepath); |
---|
1159 | xce->dirpath = estrndup(xce->filepath, xce->filepath_len); |
---|
1160 | xce->dirpath_len = zend_dirname(xce->dirpath, xce->filepath_len); |
---|
1161 | #ifdef IS_UNICODE |
---|
1162 | zend_string_to_unicode(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &xce->ufilepath, &xce->ufilepath_len, xce->filepath, xce->filepath_len TSRMLS_CC); |
---|
1163 | zend_string_to_unicode(ZEND_U_CONVERTER(UG(runtime_encoding_conv)), &xce->udirpath, &xce->udirpath_len, xce->dirpath, xce->dirpath_len TSRMLS_CC); |
---|
1164 | #endif |
---|
1165 | } |
---|
1166 | /* }}} */ |
---|
1167 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
---|
1168 | static void xc_cache_early_binding_class_cb(zend_op *opline, int oplineno, void *data TSRMLS_DC) /* {{{ */ |
---|
1169 | { |
---|
1170 | char *class_name; |
---|
1171 | int i, class_len; |
---|
1172 | xc_cest_t cest; |
---|
1173 | xc_entry_data_php_t *php = (xc_entry_data_php_t *) data; |
---|
1174 | |
---|
1175 | class_name = Z_OP_CONSTANT(opline->op1).value.str.val; |
---|
1176 | class_len = Z_OP_CONSTANT(opline->op1).value.str.len; |
---|
1177 | if (zend_hash_find(CG(class_table), class_name, class_len, (void **) &cest) == FAILURE) { |
---|
1178 | assert(0); |
---|
1179 | } |
---|
1180 | TRACE("got ZEND_DECLARE_INHERITED_CLASS: %s", class_name + 1); |
---|
1181 | /* let's see which class */ |
---|
1182 | for (i = 0; i < php->classinfo_cnt; i ++) { |
---|
1183 | if (memcmp(ZSTR_S(php->classinfos[i].key), class_name, class_len) == 0) { |
---|
1184 | php->classinfos[i].oplineno = oplineno; |
---|
1185 | php->have_early_binding = 1; |
---|
1186 | break; |
---|
1187 | } |
---|
1188 | } |
---|
1189 | |
---|
1190 | if (i == php->classinfo_cnt) { |
---|
1191 | assert(0); |
---|
1192 | } |
---|
1193 | } |
---|
1194 | /* }}} */ |
---|
1195 | #endif |
---|
1196 | |
---|
1197 | /* {{{ Constant Usage */ |
---|
1198 | #ifdef ZEND_ENGINE_2_4 |
---|
1199 | # define xcache_literal_is_dir 1 |
---|
1200 | # define xcache_literal_is_file 2 |
---|
1201 | #else |
---|
1202 | # define xcache_op1_is_file 1 |
---|
1203 | # define xcache_op1_is_dir 2 |
---|
1204 | # define xcache_op2_is_file 4 |
---|
1205 | # define xcache_op2_is_dir 8 |
---|
1206 | #endif |
---|
1207 | typedef struct { |
---|
1208 | zend_bool filepath_used; |
---|
1209 | zend_bool dirpath_used; |
---|
1210 | zend_bool ufilepath_used; |
---|
1211 | zend_bool udirpath_used; |
---|
1212 | } xc_const_usage_t; |
---|
1213 | /* }}} */ |
---|
1214 | static void xc_collect_op_array_info(xc_entry_php_t *xce, xc_entry_data_php_t *php, xc_const_usage_t *usage, xc_op_array_info_t *op_array_info, zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
1215 | { |
---|
1216 | int i; |
---|
1217 | xc_vector_t details; |
---|
1218 | |
---|
1219 | xc_vector_init(xc_op_array_info_detail_t, &details); |
---|
1220 | |
---|
1221 | #define XCACHE_ANALYZE_LITERAL(type) \ |
---|
1222 | if (zend_binary_strcmp(Z_STRVAL(literal->constant), Z_STRLEN(literal->constant), xce->type##path, xce->type##path_len) == 0) { \ |
---|
1223 | usage->type##path_used = 1; \ |
---|
1224 | literalinfo |= xcache_##literal##_is_##type; \ |
---|
1225 | } |
---|
1226 | |
---|
1227 | #define XCACHE_U_ANALYZE_LITERAL(type) \ |
---|
1228 | if (zend_u_##binary_strcmp(Z_USTRVAL(literal->constant), Z_USTRLEN(literal->constant), xce->u##type##path, xce->u##type##path_len) == 0) { \ |
---|
1229 | usage->u##type##path_used = 1; \ |
---|
1230 | literalinfo |= xcache_##literal##_is_##type; \ |
---|
1231 | } |
---|
1232 | |
---|
1233 | #define XCACHE_ANALYZE_OP(type, op) \ |
---|
1234 | if (zend_binary_strcmp(Z_STRVAL(Z_OP_CONSTANT(opline->op)), Z_STRLEN(Z_OP_CONSTANT(opline->op)), xce->type##path, xce->type##path_len) == 0) { \ |
---|
1235 | usage->type##path_used = 1; \ |
---|
1236 | oplineinfo |= xcache_##op##_is_##type; \ |
---|
1237 | } |
---|
1238 | |
---|
1239 | #define XCACHE_U_ANALYZE_OP(type, op) \ |
---|
1240 | if (zend_u_##binary_strcmp(Z_USTRVAL(Z_OP_CONSTANT(opline->op)), Z_USTRLEN(Z_OP_CONSTANT(opline->op)), xce->u##type##path, xce->u##type##path_len) == 0) { \ |
---|
1241 | usage->u##type##path_used = 1; \ |
---|
1242 | oplineinfo |= xcache_##op##_is_##type; \ |
---|
1243 | } |
---|
1244 | |
---|
1245 | #ifdef ZEND_ENGINE_2_4 |
---|
1246 | for (i = 0; i < op_array->last_literal; i++) { |
---|
1247 | zend_literal *literal = &op_array->literals[i]; |
---|
1248 | zend_uint literalinfo = 0; |
---|
1249 | if (Z_TYPE(literal->constant) == IS_STRING) { |
---|
1250 | XCACHE_ANALYZE_LITERAL(file) |
---|
1251 | else XCACHE_ANALYZE_LITERAL(dir) |
---|
1252 | } |
---|
1253 | #ifdef IS_UNICODE |
---|
1254 | else if (Z_TYPE(literal->constant) == IS_UNICODE) { |
---|
1255 | XCACHE_U_ANALYZE_LITERAL(file) |
---|
1256 | else XCACHE_U_ANALYZE_LITERAL(dir) |
---|
1257 | } |
---|
1258 | #endif |
---|
1259 | if (literalinfo) { |
---|
1260 | xc_op_array_info_detail_t detail; |
---|
1261 | detail.index = i; |
---|
1262 | detail.info = literalinfo; |
---|
1263 | xc_vector_add(xc_op_array_info_detail_t, &details, detail); |
---|
1264 | } |
---|
1265 | } |
---|
1266 | |
---|
1267 | op_array_info->literalinfo_cnt = details.cnt; |
---|
1268 | op_array_info->literalinfos = xc_vector_detach(xc_op_array_info_detail_t, &details); |
---|
1269 | #else /* ZEND_ENGINE_2_4 */ |
---|
1270 | for (i = 0; i < op_array->last; i++) { |
---|
1271 | zend_op *opline = &op_array->opcodes[i]; |
---|
1272 | zend_uint oplineinfo = 0; |
---|
1273 | if (Z_OP_TYPE(opline->op1) == IS_CONST) { |
---|
1274 | if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) { |
---|
1275 | XCACHE_ANALYZE_OP(file, op1) |
---|
1276 | else XCACHE_ANALYZE_OP(dir, op1) |
---|
1277 | } |
---|
1278 | #ifdef IS_UNICODE |
---|
1279 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) { |
---|
1280 | XCACHE_U_ANALYZE_OP(file, op1) |
---|
1281 | else XCACHE_U_ANALYZE_OP(dir, op1) |
---|
1282 | } |
---|
1283 | #endif |
---|
1284 | } |
---|
1285 | |
---|
1286 | if (Z_OP_TYPE(opline->op2) == IS_CONST) { |
---|
1287 | if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) { |
---|
1288 | XCACHE_ANALYZE_OP(file, op2) |
---|
1289 | else XCACHE_ANALYZE_OP(dir, op2) |
---|
1290 | } |
---|
1291 | #ifdef IS_UNICODE |
---|
1292 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) { |
---|
1293 | XCACHE_U_ANALYZE_OP(file, op2) |
---|
1294 | else XCACHE_U_ANALYZE_OP(dir, op2) |
---|
1295 | } |
---|
1296 | #endif |
---|
1297 | } |
---|
1298 | |
---|
1299 | if (oplineinfo) { |
---|
1300 | xc_op_array_info_detail_t detail; |
---|
1301 | detail.index = i; |
---|
1302 | detail.info = oplineinfo; |
---|
1303 | xc_vector_add(xc_op_array_info_detail_t, &details, detail); |
---|
1304 | } |
---|
1305 | } |
---|
1306 | |
---|
1307 | op_array_info->oplineinfo_cnt = details.cnt; |
---|
1308 | op_array_info->oplineinfos = xc_vector_detach(xc_op_array_info_detail_t, &details); |
---|
1309 | #endif /* ZEND_ENGINE_2_4 */ |
---|
1310 | xc_vector_free(xc_op_array_info_detail_t, &details); |
---|
1311 | } |
---|
1312 | /* }}} */ |
---|
1313 | void xc_fix_op_array_info(const xc_entry_php_t *xce, const xc_entry_data_php_t *php, zend_op_array *op_array, int shallow_copy, const xc_op_array_info_t *op_array_info TSRMLS_DC) /* {{{ */ |
---|
1314 | { |
---|
1315 | int i; |
---|
1316 | |
---|
1317 | #ifdef ZEND_ENGINE_2_4 |
---|
1318 | for (i = 0; i < op_array_info->literalinfo_cnt; ++i) { |
---|
1319 | int index = op_array_info->literalinfos[i].index; |
---|
1320 | int literalinfo = op_array_info->literalinfos[i].info; |
---|
1321 | zend_literal *literal = &op_array->literals[index]; |
---|
1322 | if ((literalinfo & xcache_literal_is_file)) { |
---|
1323 | if (!shallow_copy) { |
---|
1324 | efree(Z_STRVAL(literal->constant)); |
---|
1325 | } |
---|
1326 | if (Z_TYPE(literal->constant) == IS_STRING) { |
---|
1327 | assert(xce->filepath); |
---|
1328 | ZVAL_STRINGL(&literal->constant, xce->filepath, xce->filepath_len, !shallow_copy); |
---|
1329 | TRACE("restored literal constant: %s", xce->filepath); |
---|
1330 | } |
---|
1331 | #ifdef IS_UNICODE |
---|
1332 | else if (Z_TYPE(literal->constant) == IS_UNICODE) { |
---|
1333 | assert(xce->ufilepath); |
---|
1334 | ZVAL_UNICODEL(&literal->constant, xce->ufilepath, xce->ufilepath_len, !shallow_copy); |
---|
1335 | } |
---|
1336 | #endif |
---|
1337 | else { |
---|
1338 | assert(0); |
---|
1339 | } |
---|
1340 | } |
---|
1341 | else if ((literalinfo & xcache_literal_is_dir)) { |
---|
1342 | if (!shallow_copy) { |
---|
1343 | efree(Z_STRVAL(literal->constant)); |
---|
1344 | } |
---|
1345 | if (Z_TYPE(literal->constant) == IS_STRING) { |
---|
1346 | assert(xce->dirpath); |
---|
1347 | TRACE("restored literal constant: %s", xce->dirpath); |
---|
1348 | ZVAL_STRINGL(&literal->constant, xce->dirpath, xce->dirpath_len, !shallow_copy); |
---|
1349 | } |
---|
1350 | #ifdef IS_UNICODE |
---|
1351 | else if (Z_TYPE(literal->constant) == IS_UNICODE) { |
---|
1352 | assert(!xce->udirpath); |
---|
1353 | ZVAL_UNICODEL(&literal->constant, xce->udirpath, xce->udirpath_len, !shallow_copy); |
---|
1354 | } |
---|
1355 | #endif |
---|
1356 | else { |
---|
1357 | assert(0); |
---|
1358 | } |
---|
1359 | } |
---|
1360 | } |
---|
1361 | #else |
---|
1362 | for (i = 0; i < op_array_info->oplineinfo_cnt; ++i) { |
---|
1363 | int oplineno = op_array_info->oplineinfos[i].index; |
---|
1364 | int oplineinfo = op_array_info->oplineinfos[i].info; |
---|
1365 | zend_op *opline = &op_array->opcodes[oplineno]; |
---|
1366 | if ((oplineinfo & xcache_op1_is_file)) { |
---|
1367 | assert(Z_OP_TYPE(opline->op1) == IS_CONST); |
---|
1368 | if (!shallow_copy) { |
---|
1369 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op1))); |
---|
1370 | } |
---|
1371 | if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) { |
---|
1372 | assert(xce->filepath); |
---|
1373 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op1), xce->filepath, xce->filepath_len, !shallow_copy); |
---|
1374 | TRACE("restored op1 constant: %s", xce->filepath); |
---|
1375 | } |
---|
1376 | #ifdef IS_UNICODE |
---|
1377 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) { |
---|
1378 | assert(xce->ufilepath); |
---|
1379 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op1), xce->ufilepath, xce->ufilepath_len, !shallow_copy); |
---|
1380 | } |
---|
1381 | #endif |
---|
1382 | else { |
---|
1383 | assert(0); |
---|
1384 | } |
---|
1385 | } |
---|
1386 | else if ((oplineinfo & xcache_op1_is_dir)) { |
---|
1387 | assert(Z_OP_TYPE(opline->op1) == IS_CONST); |
---|
1388 | if (!shallow_copy) { |
---|
1389 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op1))); |
---|
1390 | } |
---|
1391 | if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_STRING) { |
---|
1392 | assert(xce->dirpath); |
---|
1393 | TRACE("restored op1 constant: %s", xce->dirpath); |
---|
1394 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op1), xce->dirpath, xce->dirpath_len, !shallow_copy); |
---|
1395 | } |
---|
1396 | #ifdef IS_UNICODE |
---|
1397 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op1)) == IS_UNICODE) { |
---|
1398 | assert(!xce->udirpath); |
---|
1399 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op1), xce->udirpath, xce->udirpath_len, !shallow_copy); |
---|
1400 | } |
---|
1401 | #endif |
---|
1402 | else { |
---|
1403 | assert(0); |
---|
1404 | } |
---|
1405 | } |
---|
1406 | |
---|
1407 | if ((oplineinfo & xcache_op2_is_file)) { |
---|
1408 | assert(Z_OP_TYPE(opline->op2) == IS_CONST); |
---|
1409 | if (!shallow_copy) { |
---|
1410 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op2))); |
---|
1411 | } |
---|
1412 | if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) { |
---|
1413 | assert(xce->filepath); |
---|
1414 | TRACE("restored op2 constant: %s", xce->filepath); |
---|
1415 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op2), xce->filepath, xce->filepath_len, !shallow_copy); |
---|
1416 | } |
---|
1417 | #ifdef IS_UNICODE |
---|
1418 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) { |
---|
1419 | assert(xce->ufilepath); |
---|
1420 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op2), xce->ufilepath, xce->ufilepath_len, !shallow_copy); |
---|
1421 | } |
---|
1422 | #endif |
---|
1423 | else { |
---|
1424 | assert(0); |
---|
1425 | } |
---|
1426 | } |
---|
1427 | else if ((oplineinfo & xcache_op2_is_dir)) { |
---|
1428 | assert(Z_OP_TYPE(opline->op2) == IS_CONST); |
---|
1429 | if (!shallow_copy) { |
---|
1430 | efree(Z_STRVAL(Z_OP_CONSTANT(opline->op2))); |
---|
1431 | } |
---|
1432 | if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_STRING) { |
---|
1433 | assert(!xce->dirpath); |
---|
1434 | TRACE("restored op2 constant: %s", xce->dirpath); |
---|
1435 | ZVAL_STRINGL(&Z_OP_CONSTANT(opline->op2), xce->dirpath, xce->dirpath_len, !shallow_copy); |
---|
1436 | } |
---|
1437 | #ifdef IS_UNICODE |
---|
1438 | else if (Z_TYPE(Z_OP_CONSTANT(opline->op2)) == IS_UNICODE) { |
---|
1439 | assert(!xce->udirpath); |
---|
1440 | ZVAL_UNICODEL(&Z_OP_CONSTANT(opline->op2), xce->udirpath, xce->udirpath_len, !shallow_copy); |
---|
1441 | } |
---|
1442 | #endif |
---|
1443 | else { |
---|
1444 | assert(0); |
---|
1445 | } |
---|
1446 | } |
---|
1447 | } |
---|
1448 | #endif |
---|
1449 | } |
---|
1450 | /* }}} */ |
---|
1451 | static void xc_free_op_array_info(xc_op_array_info_t *op_array_info TSRMLS_DC) /* {{{ */ |
---|
1452 | { |
---|
1453 | #ifdef ZEND_ENGINE_2_4 |
---|
1454 | if (op_array_info->literalinfos) { |
---|
1455 | efree(op_array_info->literalinfos); |
---|
1456 | } |
---|
1457 | #else |
---|
1458 | if (op_array_info->oplineinfos) { |
---|
1459 | efree(op_array_info->oplineinfos); |
---|
1460 | } |
---|
1461 | #endif |
---|
1462 | } |
---|
1463 | /* }}} */ |
---|
1464 | static void xc_free_php(xc_entry_data_php_t *php TSRMLS_DC) /* {{{ */ |
---|
1465 | { |
---|
1466 | int i; |
---|
1467 | if (php->classinfos) { |
---|
1468 | for (i = 0; i < php->classinfo_cnt; i ++) { |
---|
1469 | xc_classinfo_t *classinfo = &php->classinfos[i]; |
---|
1470 | int j; |
---|
1471 | for (j = 0; j < classinfo->methodinfo_cnt; j ++) { |
---|
1472 | xc_free_op_array_info(&classinfo->methodinfos[j] TSRMLS_CC); |
---|
1473 | } |
---|
1474 | |
---|
1475 | if (classinfo->methodinfos) { |
---|
1476 | efree(classinfo->methodinfos); |
---|
1477 | } |
---|
1478 | } |
---|
1479 | } |
---|
1480 | if (php->funcinfos) { |
---|
1481 | for (i = 0; i < php->funcinfo_cnt; i ++) { |
---|
1482 | xc_free_op_array_info(&php->funcinfos[i].op_array_info TSRMLS_CC); |
---|
1483 | } |
---|
1484 | } |
---|
1485 | xc_free_op_array_info(&php->op_array_info TSRMLS_CC); |
---|
1486 | |
---|
1487 | #define X_FREE(var) do {\ |
---|
1488 | if (php->var) { \ |
---|
1489 | efree(php->var); \ |
---|
1490 | } \ |
---|
1491 | } while (0) |
---|
1492 | |
---|
1493 | #ifdef ZEND_ENGINE_2_1 |
---|
1494 | X_FREE(autoglobals); |
---|
1495 | #endif |
---|
1496 | X_FREE(classinfos); |
---|
1497 | X_FREE(funcinfos); |
---|
1498 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1499 | X_FREE(constinfos); |
---|
1500 | #endif |
---|
1501 | #undef X_FREE |
---|
1502 | } |
---|
1503 | /* }}} */ |
---|
1504 | static zend_op_array *xc_compile_php(xc_entry_php_t *xce, xc_entry_data_php_t *php, zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
---|
1505 | { |
---|
1506 | zend_op_array *op_array; |
---|
1507 | int old_constinfo_cnt, old_funcinfo_cnt, old_classinfo_cnt; |
---|
1508 | zend_bool catched = 0; |
---|
1509 | |
---|
1510 | /* {{{ compile */ |
---|
1511 | TRACE("compiling %s", h->opened_path ? h->opened_path : h->filename); |
---|
1512 | |
---|
1513 | old_classinfo_cnt = zend_hash_num_elements(CG(class_table)); |
---|
1514 | old_funcinfo_cnt = zend_hash_num_elements(CG(function_table)); |
---|
1515 | old_constinfo_cnt = zend_hash_num_elements(EG(zend_constants)); |
---|
1516 | |
---|
1517 | php->op_array = NULL; |
---|
1518 | XG(initial_compile_file_called) = 0; |
---|
1519 | zend_try { |
---|
1520 | op_array = old_compile_file(h, type TSRMLS_CC); |
---|
1521 | } zend_catch { |
---|
1522 | catched = 1; |
---|
1523 | } zend_end_try(); |
---|
1524 | |
---|
1525 | if (catched) { |
---|
1526 | goto err_bailout; |
---|
1527 | } |
---|
1528 | |
---|
1529 | if (op_array == NULL) { |
---|
1530 | goto err_op_array; |
---|
1531 | } |
---|
1532 | |
---|
1533 | if (!XG(initial_compile_file_called)) { |
---|
1534 | return op_array; |
---|
1535 | } |
---|
1536 | |
---|
1537 | /* }}} */ |
---|
1538 | /* {{{ prepare */ |
---|
1539 | zend_restore_compiled_filename(h->opened_path ? h->opened_path : (char *) h->filename TSRMLS_CC); |
---|
1540 | php->op_array = op_array; |
---|
1541 | |
---|
1542 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1543 | php->constinfo_cnt = zend_hash_num_elements(EG(zend_constants)) - old_constinfo_cnt; |
---|
1544 | #endif |
---|
1545 | php->funcinfo_cnt = zend_hash_num_elements(CG(function_table)) - old_funcinfo_cnt; |
---|
1546 | php->classinfo_cnt = zend_hash_num_elements(CG(class_table)) - old_classinfo_cnt; |
---|
1547 | #ifdef ZEND_ENGINE_2_1 |
---|
1548 | /* {{{ count php->autoglobal_cnt */ { |
---|
1549 | Bucket *b; |
---|
1550 | |
---|
1551 | php->autoglobal_cnt = 0; |
---|
1552 | for (b = CG(auto_globals)->pListHead; b != NULL; b = b->pListNext) { |
---|
1553 | zend_auto_global *auto_global = (zend_auto_global *) b->pData; |
---|
1554 | /* check if actived */ |
---|
1555 | if (auto_global->auto_global_callback && !auto_global->armed) { |
---|
1556 | php->autoglobal_cnt ++; |
---|
1557 | } |
---|
1558 | } |
---|
1559 | } |
---|
1560 | /* }}} */ |
---|
1561 | #endif |
---|
1562 | |
---|
1563 | #define X_ALLOC_N(var, cnt) do { \ |
---|
1564 | if (php->cnt) { \ |
---|
1565 | ECALLOC_N(php->var, php->cnt); \ |
---|
1566 | if (!php->var) { \ |
---|
1567 | goto err_alloc; \ |
---|
1568 | } \ |
---|
1569 | } \ |
---|
1570 | else { \ |
---|
1571 | php->var = NULL; \ |
---|
1572 | } \ |
---|
1573 | } while (0) |
---|
1574 | |
---|
1575 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1576 | X_ALLOC_N(constinfos, constinfo_cnt); |
---|
1577 | #endif |
---|
1578 | X_ALLOC_N(funcinfos, funcinfo_cnt); |
---|
1579 | X_ALLOC_N(classinfos, classinfo_cnt); |
---|
1580 | #ifdef ZEND_ENGINE_2_1 |
---|
1581 | X_ALLOC_N(autoglobals, autoglobal_cnt); |
---|
1582 | #endif |
---|
1583 | #undef X_ALLOC |
---|
1584 | /* }}} */ |
---|
1585 | |
---|
1586 | /* {{{ shallow copy, pointers only */ { |
---|
1587 | Bucket *b; |
---|
1588 | unsigned int i; |
---|
1589 | unsigned int j; |
---|
1590 | |
---|
1591 | #define COPY_H(vartype, var, cnt, name, datatype) do { \ |
---|
1592 | for (i = 0, j = 0; b; i ++, b = b->pListNext) { \ |
---|
1593 | vartype *data = &php->var[j]; \ |
---|
1594 | \ |
---|
1595 | if (i < old_##cnt) { \ |
---|
1596 | continue; \ |
---|
1597 | } \ |
---|
1598 | j ++; \ |
---|
1599 | \ |
---|
1600 | assert(i < old_##cnt + php->cnt); \ |
---|
1601 | assert(b->pData); \ |
---|
1602 | memcpy(&data->name, b->pData, sizeof(datatype)); \ |
---|
1603 | UNISW(NOTHING, data->type = b->key.type;) \ |
---|
1604 | if (UNISW(1, b->key.type == IS_STRING)) { \ |
---|
1605 | ZSTR_S(data->key) = BUCKET_KEY_S(b); \ |
---|
1606 | } \ |
---|
1607 | else { \ |
---|
1608 | ZSTR_U(data->key) = BUCKET_KEY_U(b); \ |
---|
1609 | } \ |
---|
1610 | data->key_size = b->nKeyLength; \ |
---|
1611 | data->h = b->h; \ |
---|
1612 | } \ |
---|
1613 | } while(0) |
---|
1614 | |
---|
1615 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1616 | b = EG(zend_constants)->pListHead; COPY_H(xc_constinfo_t, constinfos, constinfo_cnt, constant, zend_constant); |
---|
1617 | #endif |
---|
1618 | b = CG(function_table)->pListHead; COPY_H(xc_funcinfo_t, funcinfos, funcinfo_cnt, func, zend_function); |
---|
1619 | b = CG(class_table)->pListHead; COPY_H(xc_classinfo_t, classinfos, classinfo_cnt, cest, xc_cest_t); |
---|
1620 | |
---|
1621 | #undef COPY_H |
---|
1622 | |
---|
1623 | /* for ZE1, cest need to be fixed inside store */ |
---|
1624 | |
---|
1625 | #ifdef ZEND_ENGINE_2_1 |
---|
1626 | /* scan for acatived auto globals */ |
---|
1627 | i = 0; |
---|
1628 | for (b = CG(auto_globals)->pListHead; b != NULL; b = b->pListNext) { |
---|
1629 | zend_auto_global *auto_global = (zend_auto_global *) b->pData; |
---|
1630 | /* check if actived */ |
---|
1631 | if (auto_global->auto_global_callback && !auto_global->armed) { |
---|
1632 | xc_autoglobal_t *data = &php->autoglobals[i]; |
---|
1633 | |
---|
1634 | assert(i < php->autoglobal_cnt); |
---|
1635 | i ++; |
---|
1636 | UNISW(NOTHING, data->type = b->key.type;) |
---|
1637 | if (UNISW(1, b->key.type == IS_STRING)) { |
---|
1638 | ZSTR_S(data->key) = BUCKET_KEY_S(b); |
---|
1639 | } |
---|
1640 | else { |
---|
1641 | ZSTR_U(data->key) = BUCKET_KEY_U(b); |
---|
1642 | } |
---|
1643 | data->key_len = b->nKeyLength - 1; |
---|
1644 | data->h = b->h; |
---|
1645 | } |
---|
1646 | } |
---|
1647 | #endif |
---|
1648 | } |
---|
1649 | /* }}} */ |
---|
1650 | |
---|
1651 | /* {{{ collect info for file/dir path */ { |
---|
1652 | Bucket *b; |
---|
1653 | xc_const_usage_t const_usage; |
---|
1654 | unsigned int i; |
---|
1655 | |
---|
1656 | xc_entry_init_key_php_entry(xce, zend_get_compiled_filename(TSRMLS_C) TSRMLS_CC); |
---|
1657 | memset(&const_usage, 0, sizeof(const_usage)); |
---|
1658 | |
---|
1659 | for (i = 0; i < php->classinfo_cnt; i ++) { |
---|
1660 | xc_classinfo_t *classinfo = &php->classinfos[i]; |
---|
1661 | zend_class_entry *ce = CestToCePtr(classinfo->cest); |
---|
1662 | classinfo->methodinfo_cnt = ce->function_table.nTableSize; |
---|
1663 | if (classinfo->methodinfo_cnt) { |
---|
1664 | int j; |
---|
1665 | |
---|
1666 | ECALLOC_N(classinfo->methodinfos, classinfo->methodinfo_cnt); |
---|
1667 | if (!classinfo->methodinfos) { |
---|
1668 | goto err_alloc; |
---|
1669 | } |
---|
1670 | |
---|
1671 | for (j = 0, b = ce->function_table.pListHead; b; j ++, b = b->pListNext) { |
---|
1672 | xc_collect_op_array_info(xce, php, &const_usage, &classinfo->methodinfos[j], (zend_op_array *) b->pData TSRMLS_CC); |
---|
1673 | } |
---|
1674 | } |
---|
1675 | else { |
---|
1676 | classinfo->methodinfos = NULL; |
---|
1677 | } |
---|
1678 | } |
---|
1679 | |
---|
1680 | for (i = 0; i < php->funcinfo_cnt; i ++) { |
---|
1681 | xc_collect_op_array_info(xce, php, &const_usage, &php->funcinfos[i].op_array_info, (zend_op_array *) &php->funcinfos[i].func TSRMLS_CC); |
---|
1682 | } |
---|
1683 | |
---|
1684 | xc_collect_op_array_info(xce, php, &const_usage, &php->op_array_info, php->op_array TSRMLS_CC); |
---|
1685 | |
---|
1686 | /* file/dir path free unused */ |
---|
1687 | #define X_FREE_UNUSED(var) \ |
---|
1688 | if (!const_usage.var##path_used) { \ |
---|
1689 | efree(xce->var##path); \ |
---|
1690 | xce->var##path = NULL; \ |
---|
1691 | xce->var##path_len = 0; \ |
---|
1692 | } |
---|
1693 | /* filepath is required to restore op_array->filename, so no free filepath here */ |
---|
1694 | X_FREE_UNUSED(dir) |
---|
1695 | #ifdef IS_UNICODE |
---|
1696 | X_FREE_UNUSED(ufile) |
---|
1697 | X_FREE_UNUSED(udir) |
---|
1698 | #endif |
---|
1699 | #undef X_FREE_UNUSED |
---|
1700 | } |
---|
1701 | /* }}} */ |
---|
1702 | #ifdef XCACHE_ERROR_CACHING |
---|
1703 | php->compilererrors = ((xc_sandbox_t *) XG(sandbox))->compilererrors; |
---|
1704 | php->compilererror_cnt = ((xc_sandbox_t *) XG(sandbox))->compilererror_cnt; |
---|
1705 | #endif |
---|
1706 | #ifndef ZEND_COMPILE_DELAYED_BINDING |
---|
1707 | /* {{{ find inherited classes that should be early-binding */ |
---|
1708 | php->have_early_binding = 0; |
---|
1709 | { |
---|
1710 | int i; |
---|
1711 | for (i = 0; i < php->classinfo_cnt; i ++) { |
---|
1712 | php->classinfos[i].oplineno = -1; |
---|
1713 | } |
---|
1714 | } |
---|
1715 | |
---|
1716 | xc_undo_pass_two(php->op_array TSRMLS_CC); |
---|
1717 | xc_foreach_early_binding_class(php->op_array, xc_cache_early_binding_class_cb, (void *) php TSRMLS_CC); |
---|
1718 | xc_redo_pass_two(php->op_array TSRMLS_CC); |
---|
1719 | /* }}} */ |
---|
1720 | #endif |
---|
1721 | |
---|
1722 | return op_array; |
---|
1723 | |
---|
1724 | err_alloc: |
---|
1725 | xc_free_php(php TSRMLS_CC); |
---|
1726 | |
---|
1727 | err_bailout: |
---|
1728 | err_op_array: |
---|
1729 | |
---|
1730 | if (catched) { |
---|
1731 | zend_bailout(); |
---|
1732 | } |
---|
1733 | |
---|
1734 | return op_array; |
---|
1735 | } |
---|
1736 | /* }}} */ |
---|
1737 | static zend_op_array *xc_compile_restore(xc_entry_php_t *stored_xce, zend_file_handle *h TSRMLS_DC) /* {{{ */ |
---|
1738 | { |
---|
1739 | zend_op_array *op_array; |
---|
1740 | xc_entry_php_t xce; |
---|
1741 | xc_entry_data_php_t php; |
---|
1742 | zend_bool catched; |
---|
1743 | |
---|
1744 | CG(in_compilation) = 1; |
---|
1745 | CG(compiled_filename) = stored_xce->entry.name.str.val; |
---|
1746 | CG(zend_lineno) = 0; |
---|
1747 | TRACE("restoring %s", stored_xce->entry.name.str.val); |
---|
1748 | xc_processor_restore_xc_entry_php_t(&xce, stored_xce TSRMLS_CC); |
---|
1749 | xc_processor_restore_xc_entry_data_php_t(stored_xce, &php, xce.php, xc_readonly_protection TSRMLS_CC); |
---|
1750 | xce.php = &php; |
---|
1751 | #ifdef SHOW_DPRINT |
---|
1752 | xc_dprint(&xce, 0 TSRMLS_CC); |
---|
1753 | #endif |
---|
1754 | |
---|
1755 | catched = 0; |
---|
1756 | zend_try { |
---|
1757 | op_array = xc_entry_install(&xce, h TSRMLS_CC); |
---|
1758 | } zend_catch { |
---|
1759 | catched = 1; |
---|
1760 | } zend_end_try(); |
---|
1761 | |
---|
1762 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1763 | if (php.constinfos) { |
---|
1764 | efree(php.constinfos); |
---|
1765 | } |
---|
1766 | #endif |
---|
1767 | if (php.funcinfos) { |
---|
1768 | efree(php.funcinfos); |
---|
1769 | } |
---|
1770 | if (php.classinfos) { |
---|
1771 | efree(php.classinfos); |
---|
1772 | } |
---|
1773 | |
---|
1774 | if (catched) { |
---|
1775 | zend_bailout(); |
---|
1776 | } |
---|
1777 | CG(in_compilation) = 0; |
---|
1778 | CG(compiled_filename) = NULL; |
---|
1779 | TRACE("restored %s", stored_xce->entry.name.str.val); |
---|
1780 | return op_array; |
---|
1781 | } |
---|
1782 | /* }}} */ |
---|
1783 | static zend_op_array *xc_check_initial_compile_file(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
---|
1784 | { |
---|
1785 | XG(initial_compile_file_called) = 1; |
---|
1786 | return origin_compile_file(h, type TSRMLS_CC); |
---|
1787 | } |
---|
1788 | /* }}} */ |
---|
1789 | static zend_op_array *xc_compile_file_ex(xc_entry_hash_t *entry_hash, xc_entry_php_t *xce, zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
---|
1790 | { |
---|
1791 | zend_op_array *op_array; |
---|
1792 | xc_entry_php_t *stored_xce; |
---|
1793 | xc_entry_data_php_t *stored_php; |
---|
1794 | zend_bool gaveup = 0; |
---|
1795 | zend_bool catched = 0; |
---|
1796 | zend_bool newlycompiled; |
---|
1797 | xc_sandbox_t sandbox; |
---|
1798 | xc_cache_t *cache = xc_php_caches[entry_hash->cacheslotid]; |
---|
1799 | |
---|
1800 | /* stale clogs precheck */ |
---|
1801 | if (XG(request_time) - cache->compiling < 30) { |
---|
1802 | cache->clogs ++; |
---|
1803 | return old_compile_file(h, type TSRMLS_CC); |
---|
1804 | } |
---|
1805 | /* {{{ entry_lookup/hit/md5_init/php_lookup */ |
---|
1806 | stored_xce = NULL; |
---|
1807 | stored_php = NULL; |
---|
1808 | ENTER_LOCK_EX(cache) { |
---|
1809 | stored_xce = (xc_entry_php_t *) xc_entry_find_dmz(XC_TYPE_PHP, cache, entry_hash->entryslotid, (xc_entry_t *) xce TSRMLS_CC); |
---|
1810 | if (stored_xce) { |
---|
1811 | xc_cache_hit_dmz(cache TSRMLS_CC); |
---|
1812 | |
---|
1813 | TRACE("hit %s, holding", stored_xce->entry.name.str.val); |
---|
1814 | xc_entry_hold_php_dmz(cache, stored_xce TSRMLS_CC); |
---|
1815 | } |
---|
1816 | else { |
---|
1817 | cache->misses ++; |
---|
1818 | TRACE("miss %s", xce->entry.name.str.val); |
---|
1819 | |
---|
1820 | if (xc_entry_init_key_php_md5(cache, xce->php, xce TSRMLS_CC) != SUCCESS) { |
---|
1821 | gaveup = 1; |
---|
1822 | break; |
---|
1823 | } |
---|
1824 | |
---|
1825 | stored_php = xc_php_find_dmz(cache, xce->php TSRMLS_CC); |
---|
1826 | |
---|
1827 | /* miss but compiling */ |
---|
1828 | if (!stored_php) { |
---|
1829 | if (XG(request_time) - cache->compiling < 30) { |
---|
1830 | TRACE("%s", "miss but compiling"); |
---|
1831 | cache->clogs ++; |
---|
1832 | gaveup = 1; |
---|
1833 | break; |
---|
1834 | } |
---|
1835 | TRACE("%s", "php_lookup miss"); |
---|
1836 | } |
---|
1837 | else { |
---|
1838 | TRACE("%s", "php_lookup hit"); |
---|
1839 | } |
---|
1840 | |
---|
1841 | cache->compiling = XG(request_time); |
---|
1842 | } |
---|
1843 | } LEAVE_LOCK_EX(cache); |
---|
1844 | |
---|
1845 | if (catched) { |
---|
1846 | cache->compiling = 0; |
---|
1847 | zend_bailout(); |
---|
1848 | } |
---|
1849 | |
---|
1850 | /* hit */ |
---|
1851 | if (stored_xce) { |
---|
1852 | return xc_compile_restore(stored_xce, h TSRMLS_CC); |
---|
1853 | } |
---|
1854 | |
---|
1855 | /* gaveup */ |
---|
1856 | if (gaveup) { |
---|
1857 | return old_compile_file(h, type TSRMLS_CC); |
---|
1858 | } |
---|
1859 | /* }}} */ |
---|
1860 | op_array = NULL; |
---|
1861 | /* {{{ compile */ |
---|
1862 | if (stored_php) { |
---|
1863 | newlycompiled = 0; |
---|
1864 | xc_entry_init_key_php_entry(xce, h->opened_path ? h->opened_path : h->filename TSRMLS_CC); |
---|
1865 | xce->php = stored_php; |
---|
1866 | } |
---|
1867 | else { |
---|
1868 | newlycompiled = 1; |
---|
1869 | |
---|
1870 | /* make compile inside sandbox */ |
---|
1871 | xc_sandbox_init(&sandbox, h->opened_path ? h->opened_path : h->filename TSRMLS_CC); |
---|
1872 | |
---|
1873 | #ifdef HAVE_XCACHE_CONSTANT |
---|
1874 | xce->php->constinfos = NULL; |
---|
1875 | #endif |
---|
1876 | xce->php->funcinfos = NULL; |
---|
1877 | xce->php->classinfos = NULL; |
---|
1878 | #ifdef ZEND_ENGINE_2_1 |
---|
1879 | xce->php->autoglobals = NULL; |
---|
1880 | #endif |
---|
1881 | |
---|
1882 | memset(&xce->php->op_array_info, 0, sizeof(xce->php->op_array_info)); |
---|
1883 | |
---|
1884 | zend_try { |
---|
1885 | op_array = xc_compile_php(xce, xce->php, h, type TSRMLS_CC); |
---|
1886 | } zend_catch { |
---|
1887 | catched = 1; |
---|
1888 | } zend_end_try(); |
---|
1889 | |
---|
1890 | if (catched || !op_array) { |
---|
1891 | goto err_aftersandbox; |
---|
1892 | } |
---|
1893 | |
---|
1894 | /* not cachable */ |
---|
1895 | if (!xce->php->op_array) { |
---|
1896 | cache->compiling = 0; |
---|
1897 | /* it's not cachable, but don't scare the users with high misses */ |
---|
1898 | cache->misses --; |
---|
1899 | xc_sandbox_free(&sandbox, XC_InstallNoBinding TSRMLS_CC); |
---|
1900 | return op_array; |
---|
1901 | } |
---|
1902 | } |
---|
1903 | /* }}} */ |
---|
1904 | #ifdef HAVE_INODE |
---|
1905 | /* {{{ path name fix |
---|
1906 | * inode enabled entry hash/compare on name |
---|
1907 | * do not update to its name to real pathname |
---|
1908 | * WARNING: this code is required to be after compile |
---|
1909 | */ |
---|
1910 | if (xce->file_inode) { |
---|
1911 | const char *filename = h->opened_path ? h->opened_path : h->filename; |
---|
1912 | if (xce->entry.name.str.val != filename) { |
---|
1913 | xce->entry.name.str.val = (char *) filename; |
---|
1914 | xce->entry.name.str.len = strlen(filename); |
---|
1915 | } |
---|
1916 | } |
---|
1917 | /* }}} */ |
---|
1918 | #endif |
---|
1919 | #ifdef SHOW_DPRINT |
---|
1920 | xc_dprint(xce, 0 TSRMLS_CC); |
---|
1921 | #endif |
---|
1922 | stored_xce = NULL; |
---|
1923 | ENTER_LOCK_EX(cache) { /* {{{ php_store/entry_store */ |
---|
1924 | /* php_store */ |
---|
1925 | if (newlycompiled) { |
---|
1926 | stored_php = xc_php_store_dmz(cache, xce->php TSRMLS_CC); |
---|
1927 | if (!stored_php) { |
---|
1928 | /* error */ |
---|
1929 | break; |
---|
1930 | } |
---|
1931 | } |
---|
1932 | /* entry_store */ |
---|
1933 | xc_php_addref_dmz(stored_php); |
---|
1934 | stored_xce = xc_entry_php_store_dmz(cache, entry_hash->entryslotid, xce TSRMLS_CC); |
---|
1935 | if (stored_xce) { |
---|
1936 | stored_xce->php = stored_php; |
---|
1937 | } |
---|
1938 | else { |
---|
1939 | /* error */ |
---|
1940 | xc_php_release_dmz(cache, stored_php); |
---|
1941 | } |
---|
1942 | } LEAVE_LOCK_EX(cache); |
---|
1943 | /* }}} */ |
---|
1944 | TRACE("%s", stored_xce ? "stored" : "store failed"); |
---|
1945 | |
---|
1946 | cache->compiling = 0; |
---|
1947 | if (catched) { |
---|
1948 | goto err_aftersandbox; |
---|
1949 | } |
---|
1950 | |
---|
1951 | if (newlycompiled) { |
---|
1952 | xc_free_php(xce->php TSRMLS_CC); |
---|
1953 | } |
---|
1954 | |
---|
1955 | if (stored_xce) { |
---|
1956 | if (op_array) { |
---|
1957 | #ifdef ZEND_ENGINE_2 |
---|
1958 | destroy_op_array(op_array TSRMLS_CC); |
---|
1959 | #else |
---|
1960 | destroy_op_array(op_array); |
---|
1961 | #endif |
---|
1962 | efree(op_array); |
---|
1963 | h = NULL; |
---|
1964 | } |
---|
1965 | if (newlycompiled) { |
---|
1966 | xc_sandbox_free(&sandbox, XC_NoInstall TSRMLS_CC); |
---|
1967 | } |
---|
1968 | return xc_compile_restore(stored_xce, h TSRMLS_CC); |
---|
1969 | } |
---|
1970 | else { |
---|
1971 | if (newlycompiled) { |
---|
1972 | zend_op_array *old_active_op_array = CG(active_op_array); |
---|
1973 | /* install it */ |
---|
1974 | CG(active_op_array) = op_array; |
---|
1975 | xc_sandbox_free(&sandbox, XC_Install TSRMLS_CC); |
---|
1976 | CG(active_op_array) = old_active_op_array; |
---|
1977 | } |
---|
1978 | } |
---|
1979 | return op_array; |
---|
1980 | |
---|
1981 | err_aftersandbox: |
---|
1982 | if (newlycompiled) { |
---|
1983 | xc_free_php(xce->php TSRMLS_CC); |
---|
1984 | xc_sandbox_free(&sandbox, XC_NoInstall TSRMLS_CC); |
---|
1985 | } |
---|
1986 | |
---|
1987 | if (catched) { |
---|
1988 | cache->compiling = 0; |
---|
1989 | cache->errors ++; |
---|
1990 | zend_bailout(); |
---|
1991 | } |
---|
1992 | return op_array; |
---|
1993 | } |
---|
1994 | /* }}} */ |
---|
1995 | static zend_op_array *xc_compile_file(zend_file_handle *h, int type TSRMLS_DC) /* {{{ */ |
---|
1996 | { |
---|
1997 | zend_op_array *op_array; |
---|
1998 | xc_entry_php_t xce; |
---|
1999 | xc_entry_hash_t entry_hash; |
---|
2000 | xc_entry_data_php_t php; |
---|
2001 | const char *filename; |
---|
2002 | |
---|
2003 | assert(xc_initized); |
---|
2004 | |
---|
2005 | TRACE("type = %d\n", h->type); |
---|
2006 | if (!XG(cacher)) { |
---|
2007 | op_array = old_compile_file(h, type TSRMLS_CC); |
---|
2008 | return op_array; |
---|
2009 | } |
---|
2010 | |
---|
2011 | /* {{{ entry_init_key */ |
---|
2012 | filename = h->opened_path ? h->opened_path : h->filename; |
---|
2013 | xce.php = &php; |
---|
2014 | if (xc_entry_init_key_php(&entry_hash, &xce, filename TSRMLS_CC) != SUCCESS) { |
---|
2015 | TRACE("failed to init key for %s", filename); |
---|
2016 | return old_compile_file(h, type TSRMLS_CC); |
---|
2017 | } |
---|
2018 | /* }}} */ |
---|
2019 | |
---|
2020 | op_array = xc_compile_file_ex(&entry_hash, &xce, h, type TSRMLS_CC); |
---|
2021 | |
---|
2022 | xc_entry_free_key_php(&xce TSRMLS_CC); |
---|
2023 | |
---|
2024 | return op_array; |
---|
2025 | } |
---|
2026 | /* }}} */ |
---|
2027 | |
---|
2028 | /* gdb helper functions, but N/A for coredump */ |
---|
2029 | int xc_is_rw(const void *p) /* {{{ */ |
---|
2030 | { |
---|
2031 | xc_shm_t *shm; |
---|
2032 | int i; |
---|
2033 | |
---|
2034 | if (xc_php_caches) { |
---|
2035 | for (i = 0; i < xc_php_hcache.size; i ++) { |
---|
2036 | shm = xc_php_caches[i]->shm; |
---|
2037 | if (shm->handlers->is_readwrite(shm, p)) { |
---|
2038 | return 1; |
---|
2039 | } |
---|
2040 | } |
---|
2041 | } |
---|
2042 | |
---|
2043 | if (xc_var_caches) { |
---|
2044 | for (i = 0; i < xc_var_hcache.size; i ++) { |
---|
2045 | shm = xc_var_caches[i]->shm; |
---|
2046 | if (shm->handlers->is_readwrite(shm, p)) { |
---|
2047 | return 1; |
---|
2048 | } |
---|
2049 | } |
---|
2050 | } |
---|
2051 | return 0; |
---|
2052 | } |
---|
2053 | /* }}} */ |
---|
2054 | int xc_is_ro(const void *p) /* {{{ */ |
---|
2055 | { |
---|
2056 | xc_shm_t *shm; |
---|
2057 | int i; |
---|
2058 | |
---|
2059 | if (xc_php_caches) { |
---|
2060 | for (i = 0; i < xc_php_hcache.size; i ++) { |
---|
2061 | shm = xc_php_caches[i]->shm; |
---|
2062 | if (shm->handlers->is_readonly(shm, p)) { |
---|
2063 | return 1; |
---|
2064 | } |
---|
2065 | } |
---|
2066 | } |
---|
2067 | |
---|
2068 | if (xc_var_caches) { |
---|
2069 | for (i = 0; i < xc_var_hcache.size; i ++) { |
---|
2070 | shm = xc_var_caches[i]->shm; |
---|
2071 | if (shm->handlers->is_readonly(shm, p)) { |
---|
2072 | return 1; |
---|
2073 | } |
---|
2074 | } |
---|
2075 | } |
---|
2076 | return 0; |
---|
2077 | } |
---|
2078 | /* }}} */ |
---|
2079 | int xc_is_shm(const void *p) /* {{{ */ |
---|
2080 | { |
---|
2081 | return xc_is_ro(p) || xc_is_rw(p); |
---|
2082 | } |
---|
2083 | /* }}} */ |
---|
2084 | |
---|
2085 | void xc_gc_add_op_array(xc_gc_op_array_t *gc_op_array TSRMLS_DC) /* {{{ */ |
---|
2086 | { |
---|
2087 | zend_llist_add_element(&XG(gc_op_arrays), (void *) gc_op_array); |
---|
2088 | } |
---|
2089 | /* }}} */ |
---|
2090 | static void xc_gc_op_array(void *pDest) /* {{{ */ |
---|
2091 | { |
---|
2092 | xc_gc_op_array_t *op_array = (xc_gc_op_array_t *) pDest; |
---|
2093 | zend_uint i; |
---|
2094 | #ifdef ZEND_ENGINE_2 |
---|
2095 | if (op_array->arg_info) { |
---|
2096 | for (i = 0; i < op_array->num_args; i++) { |
---|
2097 | efree((char *) ZSTR_V(op_array->arg_info[i].name)); |
---|
2098 | if (ZSTR_V(op_array->arg_info[i].class_name)) { |
---|
2099 | efree((char *) ZSTR_V(op_array->arg_info[i].class_name)); |
---|
2100 | } |
---|
2101 | } |
---|
2102 | efree(op_array->arg_info); |
---|
2103 | } |
---|
2104 | #endif |
---|
2105 | if (op_array->opcodes) { |
---|
2106 | efree(op_array->opcodes); |
---|
2107 | } |
---|
2108 | } |
---|
2109 | /* }}} */ |
---|
2110 | |
---|
2111 | /* module helper function */ |
---|
2112 | static int xc_init_constant(int module_number TSRMLS_DC) /* {{{ */ |
---|
2113 | { |
---|
2114 | typedef struct { |
---|
2115 | const char *prefix; |
---|
2116 | zend_uchar (*getsize)(); |
---|
2117 | const char *(*get)(zend_uchar i); |
---|
2118 | } xc_meminfo_t; |
---|
2119 | xc_meminfo_t nameinfos[] = { |
---|
2120 | { "", xc_get_op_type_count, xc_get_op_type }, |
---|
2121 | { "", xc_get_data_type_count, xc_get_data_type }, |
---|
2122 | { "", xc_get_opcode_count, xc_get_opcode }, |
---|
2123 | { "OPSPEC_", xc_get_op_spec_count, xc_get_op_spec }, |
---|
2124 | { NULL, NULL, NULL } |
---|
2125 | }; |
---|
2126 | xc_meminfo_t* p; |
---|
2127 | zend_uchar i, count; |
---|
2128 | char const_name[96]; |
---|
2129 | int const_name_len; |
---|
2130 | int undefdone = 0; |
---|
2131 | |
---|
2132 | for (p = nameinfos; p->getsize; p ++) { |
---|
2133 | count = p->getsize(); |
---|
2134 | for (i = 0; i < count; i ++) { |
---|
2135 | const char *name = p->get(i); |
---|
2136 | if (!name) continue; |
---|
2137 | if (strcmp(name, "UNDEF") == 0) { |
---|
2138 | if (undefdone) continue; |
---|
2139 | undefdone = 1; |
---|
2140 | } |
---|
2141 | const_name_len = snprintf(const_name, sizeof(const_name), "XC_%s%s", p->prefix, name); |
---|
2142 | zend_register_long_constant(const_name, const_name_len+1, i, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
---|
2143 | } |
---|
2144 | } |
---|
2145 | |
---|
2146 | zend_register_long_constant(ZEND_STRS("XC_SIZEOF_TEMP_VARIABLE"), sizeof(temp_variable), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
---|
2147 | zend_register_long_constant(ZEND_STRS("XC_TYPE_PHP"), XC_TYPE_PHP, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
---|
2148 | zend_register_long_constant(ZEND_STRS("XC_TYPE_VAR"), XC_TYPE_VAR, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
---|
2149 | zend_register_stringl_constant(ZEND_STRS("XCACHE_VERSION"), ZEND_STRL(XCACHE_VERSION), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
---|
2150 | zend_register_stringl_constant(ZEND_STRS("XCACHE_MODULES"), ZEND_STRL(XCACHE_MODULES), CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC); |
---|
2151 | return 0; |
---|
2152 | } |
---|
2153 | /* }}} */ |
---|
2154 | static xc_shm_t *xc_cache_destroy(xc_cache_t **caches, xc_hash_t *hcache) /* {{{ */ |
---|
2155 | { |
---|
2156 | int i; |
---|
2157 | xc_cache_t *cache; |
---|
2158 | xc_shm_t *shm; |
---|
2159 | |
---|
2160 | if (!caches) { |
---|
2161 | return NULL; |
---|
2162 | } |
---|
2163 | shm = NULL; |
---|
2164 | for (i = 0; i < hcache->size; i ++) { |
---|
2165 | cache = caches[i]; |
---|
2166 | if (cache) { |
---|
2167 | if (cache->lck) { |
---|
2168 | xc_lock_destroy(cache->lck); |
---|
2169 | } |
---|
2170 | /* do NOT free |
---|
2171 | if (cache->entries) { |
---|
2172 | cache->mem->handlers->free(cache->mem, cache->entries); |
---|
2173 | } |
---|
2174 | cache->mem->handlers->free(cache->mem, cache); |
---|
2175 | */ |
---|
2176 | shm = cache->shm; |
---|
2177 | shm->handlers->memdestroy(cache->mem); |
---|
2178 | } |
---|
2179 | } |
---|
2180 | free(caches); |
---|
2181 | return shm; |
---|
2182 | } |
---|
2183 | /* }}} */ |
---|
2184 | static xc_cache_t **xc_cache_init(xc_shm_t *shm, xc_hash_t *hcache, xc_hash_t *hentry, xc_hash_t *hphp, xc_shmsize_t shmsize) /* {{{ */ |
---|
2185 | { |
---|
2186 | xc_cache_t **caches = NULL, *cache; |
---|
2187 | xc_mem_t *mem; |
---|
2188 | time_t now = time(NULL); |
---|
2189 | int i; |
---|
2190 | xc_memsize_t memsize; |
---|
2191 | |
---|
2192 | memsize = shmsize / hcache->size; |
---|
2193 | |
---|
2194 | /* Don't let it break out of mem after ALIGNed |
---|
2195 | * This is important for |
---|
2196 | * Simply loop until it fit our need |
---|
2197 | */ |
---|
2198 | while (ALIGN(memsize) * hcache->size > shmsize && ALIGN(memsize) != memsize) { |
---|
2199 | if (memsize < ALIGN(1)) { |
---|
2200 | CHECK(NULL, "cache too small"); |
---|
2201 | } |
---|
2202 | memsize --; |
---|
2203 | } |
---|
2204 | |
---|
2205 | CHECK(caches = calloc(hcache->size, sizeof(xc_cache_t *)), "caches OOM"); |
---|
2206 | |
---|
2207 | for (i = 0; i < hcache->size; i ++) { |
---|
2208 | CHECK(mem = shm->handlers->meminit(shm, memsize), "Failed init memory allocator"); |
---|
2209 | CHECK(cache = mem->handlers->calloc(mem, 1, sizeof(xc_cache_t)), "cache OOM"); |
---|
2210 | CHECK(cache->entries = mem->handlers->calloc(mem, hentry->size, sizeof(xc_entry_t*)), "entries OOM"); |
---|
2211 | if (hphp) { |
---|
2212 | CHECK(cache->phps= mem->handlers->calloc(mem, hphp->size, sizeof(xc_entry_data_php_t*)), "phps OOM"); |
---|
2213 | } |
---|
2214 | CHECK(cache->lck = xc_lock_init(NULL), "can't create lock"); |
---|
2215 | |
---|
2216 | cache->hcache = hcache; |
---|
2217 | cache->hentry = hentry; |
---|
2218 | cache->hphp = hphp; |
---|
2219 | cache->shm = shm; |
---|
2220 | cache->mem = mem; |
---|
2221 | cache->cacheid = i; |
---|
2222 | cache->last_gc_deletes = now; |
---|
2223 | cache->last_gc_expires = now; |
---|
2224 | caches[i] = cache; |
---|
2225 | } |
---|
2226 | return caches; |
---|
2227 | |
---|
2228 | err: |
---|
2229 | if (caches) { |
---|
2230 | xc_cache_destroy(caches, hcache); |
---|
2231 | } |
---|
2232 | return NULL; |
---|
2233 | } |
---|
2234 | /* }}} */ |
---|
2235 | static void xc_destroy() /* {{{ */ |
---|
2236 | { |
---|
2237 | xc_shm_t *shm = NULL; |
---|
2238 | |
---|
2239 | if (old_compile_file) { |
---|
2240 | zend_compile_file = old_compile_file; |
---|
2241 | old_compile_file = NULL; |
---|
2242 | } |
---|
2243 | |
---|
2244 | if (origin_compile_file) { |
---|
2245 | zend_compile_file = origin_compile_file; |
---|
2246 | origin_compile_file = NULL; |
---|
2247 | } |
---|
2248 | |
---|
2249 | if (xc_php_caches) { |
---|
2250 | shm = xc_cache_destroy(xc_php_caches, &xc_php_hcache); |
---|
2251 | xc_php_caches = NULL; |
---|
2252 | } |
---|
2253 | |
---|
2254 | if (xc_var_caches) { |
---|
2255 | shm = xc_cache_destroy(xc_var_caches, &xc_var_hcache); |
---|
2256 | xc_var_caches = NULL; |
---|
2257 | } |
---|
2258 | |
---|
2259 | if (shm) { |
---|
2260 | xc_shm_destroy(shm); |
---|
2261 | } |
---|
2262 | |
---|
2263 | xc_initized = 0; |
---|
2264 | } |
---|
2265 | /* }}} */ |
---|
2266 | static int xc_init(int module_number TSRMLS_DC) /* {{{ */ |
---|
2267 | { |
---|
2268 | xc_shm_t *shm; |
---|
2269 | xc_shmsize_t shmsize = ALIGN(xc_php_size) + ALIGN(xc_var_size); |
---|
2270 | |
---|
2271 | xc_php_caches = xc_var_caches = NULL; |
---|
2272 | shm = NULL; |
---|
2273 | |
---|
2274 | if (shmsize < (size_t) xc_php_size || shmsize < (size_t) xc_var_size) { |
---|
2275 | zend_error(E_ERROR, "XCache: neither xcache.size nor xcache.var_size can be negative"); |
---|
2276 | goto err; |
---|
2277 | } |
---|
2278 | |
---|
2279 | if (xc_php_size || xc_var_size) { |
---|
2280 | CHECK(shm = xc_shm_init(xc_shm_scheme, shmsize, xc_readonly_protection, xc_mmap_path, NULL), "Cannot create shm"); |
---|
2281 | if (!shm->handlers->can_readonly(shm)) { |
---|
2282 | xc_readonly_protection = 0; |
---|
2283 | } |
---|
2284 | |
---|
2285 | if (xc_php_size) { |
---|
2286 | old_compile_file = zend_compile_file; |
---|
2287 | zend_compile_file = xc_compile_file; |
---|
2288 | |
---|
2289 | CHECK(xc_php_caches = xc_cache_init(shm, &xc_php_hcache, &xc_php_hentry, &xc_php_hentry, xc_php_size), "failed init opcode cache"); |
---|
2290 | } |
---|
2291 | |
---|
2292 | if (xc_var_size) { |
---|
2293 | CHECK(xc_var_caches = xc_cache_init(shm, &xc_var_hcache, &xc_var_hentry, NULL, xc_var_size), "failed init variable cache"); |
---|
2294 | } |
---|
2295 | } |
---|
2296 | return SUCCESS; |
---|
2297 | |
---|
2298 | err: |
---|
2299 | if (xc_php_caches || xc_var_caches) { |
---|
2300 | xc_destroy(); |
---|
2301 | /* shm destroied in xc_destroy() */ |
---|
2302 | } |
---|
2303 | else if (shm) { |
---|
2304 | xc_destroy(); |
---|
2305 | xc_shm_destroy(shm); |
---|
2306 | } |
---|
2307 | return 0; |
---|
2308 | } |
---|
2309 | /* }}} */ |
---|
2310 | static void xc_request_init(TSRMLS_D) /* {{{ */ |
---|
2311 | { |
---|
2312 | int i; |
---|
2313 | |
---|
2314 | if (!XG(internal_table_copied)) { |
---|
2315 | zend_function tmp_func; |
---|
2316 | xc_cest_t tmp_cest; |
---|
2317 | |
---|
2318 | #ifdef HAVE_XCACHE_CONSTANT |
---|
2319 | zend_hash_destroy(&XG(internal_constant_table)); |
---|
2320 | #endif |
---|
2321 | zend_hash_destroy(&XG(internal_function_table)); |
---|
2322 | zend_hash_destroy(&XG(internal_class_table)); |
---|
2323 | |
---|
2324 | #ifdef HAVE_XCACHE_CONSTANT |
---|
2325 | zend_hash_init_ex(&XG(internal_constant_table), 20, NULL, (dtor_func_t) xc_zend_constant_dtor, 1, 0); |
---|
2326 | #endif |
---|
2327 | zend_hash_init_ex(&XG(internal_function_table), 100, NULL, NULL, 1, 0); |
---|
2328 | zend_hash_init_ex(&XG(internal_class_table), 10, NULL, NULL, 1, 0); |
---|
2329 | |
---|
2330 | #ifdef HAVE_XCACHE_CONSTANT |
---|
2331 | xc_copy_internal_zend_constants(&XG(internal_constant_table), EG(zend_constants)); |
---|
2332 | #endif |
---|
2333 | zend_hash_copy(&XG(internal_function_table), CG(function_table), NULL, &tmp_func, sizeof(tmp_func)); |
---|
2334 | zend_hash_copy(&XG(internal_class_table), CG(class_table), NULL, &tmp_cest, sizeof(tmp_cest)); |
---|
2335 | |
---|
2336 | XG(internal_table_copied) = 1; |
---|
2337 | } |
---|
2338 | if (xc_php_caches && !XG(php_holds)) { |
---|
2339 | XG(php_holds) = calloc(xc_php_hcache.size, sizeof(xc_stack_t)); |
---|
2340 | for (i = 0; i < xc_php_hcache.size; i ++) { |
---|
2341 | xc_stack_init(&XG(php_holds[i])); |
---|
2342 | } |
---|
2343 | } |
---|
2344 | |
---|
2345 | if (xc_var_caches && !XG(var_holds)) { |
---|
2346 | XG(var_holds) = calloc(xc_var_hcache.size, sizeof(xc_stack_t)); |
---|
2347 | for (i = 0; i < xc_var_hcache.size; i ++) { |
---|
2348 | xc_stack_init(&XG(var_holds[i])); |
---|
2349 | } |
---|
2350 | } |
---|
2351 | |
---|
2352 | #ifdef ZEND_ENGINE_2 |
---|
2353 | zend_llist_init(&XG(gc_op_arrays), sizeof(xc_gc_op_array_t), xc_gc_op_array, 0); |
---|
2354 | #endif |
---|
2355 | |
---|
2356 | #if PHP_API_VERSION <= 20041225 |
---|
2357 | XG(request_time) = time(NULL); |
---|
2358 | #else |
---|
2359 | XG(request_time) = sapi_get_request_time(TSRMLS_C); |
---|
2360 | #endif |
---|
2361 | |
---|
2362 | #ifdef HAVE_XCACHE_COVERAGER |
---|
2363 | xc_coverager_request_init(TSRMLS_C); |
---|
2364 | #endif |
---|
2365 | } |
---|
2366 | /* }}} */ |
---|
2367 | static void xc_request_shutdown(TSRMLS_D) /* {{{ */ |
---|
2368 | { |
---|
2369 | xc_entry_unholds(TSRMLS_C); |
---|
2370 | #ifdef ZEND_ENGINE_2 |
---|
2371 | zend_llist_destroy(&XG(gc_op_arrays)); |
---|
2372 | #endif |
---|
2373 | xc_gc_expires_php(TSRMLS_C); |
---|
2374 | xc_gc_expires_var(TSRMLS_C); |
---|
2375 | xc_gc_deletes(TSRMLS_C); |
---|
2376 | #ifdef HAVE_XCACHE_COVERAGER |
---|
2377 | xc_coverager_request_shutdown(TSRMLS_C); |
---|
2378 | #endif |
---|
2379 | } |
---|
2380 | /* }}} */ |
---|
2381 | /* {{{ PHP_GINIT_FUNCTION(xcache) */ |
---|
2382 | static |
---|
2383 | #ifdef PHP_GINIT_FUNCTION |
---|
2384 | PHP_GINIT_FUNCTION(xcache) |
---|
2385 | #else |
---|
2386 | void xc_init_globals(zend_xcache_globals* xcache_globals TSRMLS_DC) |
---|
2387 | #endif |
---|
2388 | { |
---|
2389 | memset(xcache_globals, 0, sizeof(zend_xcache_globals)); |
---|
2390 | |
---|
2391 | #ifdef HAVE_XCACHE_CONSTANT |
---|
2392 | zend_hash_init_ex(&xcache_globals->internal_constant_table, 1, NULL, (dtor_func_t) xc_zend_constant_dtor, 1, 0); |
---|
2393 | #endif |
---|
2394 | zend_hash_init_ex(&xcache_globals->internal_function_table, 1, NULL, NULL, 1, 0); |
---|
2395 | zend_hash_init_ex(&xcache_globals->internal_class_table, 1, NULL, NULL, 1, 0); |
---|
2396 | } |
---|
2397 | /* }}} */ |
---|
2398 | /* {{{ PHP_GSHUTDOWN_FUNCTION(xcache) */ |
---|
2399 | static |
---|
2400 | #ifdef PHP_GSHUTDOWN_FUNCTION |
---|
2401 | PHP_GSHUTDOWN_FUNCTION(xcache) |
---|
2402 | #else |
---|
2403 | void xc_shutdown_globals(zend_xcache_globals* xcache_globals TSRMLS_DC) |
---|
2404 | #endif |
---|
2405 | { |
---|
2406 | int i; |
---|
2407 | |
---|
2408 | if (xcache_globals->php_holds != NULL) { |
---|
2409 | for (i = 0; i < xc_php_hcache.size; i ++) { |
---|
2410 | xc_stack_destroy(&xcache_globals->php_holds[i]); |
---|
2411 | } |
---|
2412 | free(xcache_globals->php_holds); |
---|
2413 | xcache_globals->php_holds = NULL; |
---|
2414 | } |
---|
2415 | |
---|
2416 | if (xcache_globals->var_holds != NULL) { |
---|
2417 | for (i = 0; i < xc_var_hcache.size; i ++) { |
---|
2418 | xc_stack_destroy(&xcache_globals->var_holds[i]); |
---|
2419 | } |
---|
2420 | free(xcache_globals->var_holds); |
---|
2421 | xcache_globals->var_holds = NULL; |
---|
2422 | } |
---|
2423 | |
---|
2424 | if (xcache_globals->internal_table_copied) { |
---|
2425 | #ifdef HAVE_XCACHE_CONSTANT |
---|
2426 | zend_hash_destroy(&xcache_globals->internal_constant_table); |
---|
2427 | #endif |
---|
2428 | zend_hash_destroy(&xcache_globals->internal_function_table); |
---|
2429 | zend_hash_destroy(&xcache_globals->internal_class_table); |
---|
2430 | } |
---|
2431 | } |
---|
2432 | /* }}} */ |
---|
2433 | |
---|
2434 | /* user functions */ |
---|
2435 | static int xcache_admin_auth_check(TSRMLS_D) /* {{{ */ |
---|
2436 | { |
---|
2437 | zval **server = NULL; |
---|
2438 | zval **user = NULL; |
---|
2439 | zval **pass = NULL; |
---|
2440 | char *admin_user = NULL; |
---|
2441 | char *admin_pass = NULL; |
---|
2442 | HashTable *ht; |
---|
2443 | |
---|
2444 | if (cfg_get_string("xcache.admin.user", &admin_user) == FAILURE || !admin_user[0]) { |
---|
2445 | admin_user = NULL; |
---|
2446 | } |
---|
2447 | if (cfg_get_string("xcache.admin.pass", &admin_pass) == FAILURE || !admin_pass[0]) { |
---|
2448 | admin_pass = NULL; |
---|
2449 | } |
---|
2450 | |
---|
2451 | if (admin_user == NULL || admin_pass == NULL) { |
---|
2452 | php_error_docref(XCACHE_WIKI_URL "/InstallAdministration" TSRMLS_CC, E_ERROR, |
---|
2453 | "xcache.admin.user and/or xcache.admin.pass settings is not configured." |
---|
2454 | " Make sure you've modified the correct php ini file for your php used in webserver."); |
---|
2455 | zend_bailout(); |
---|
2456 | } |
---|
2457 | if (strlen(admin_pass) != 32) { |
---|
2458 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "xcache.admin.pass is %lu chars unexpectedly, it is supposed to be the password after md5() which should be 32 chars", (unsigned long) strlen(admin_pass)); |
---|
2459 | zend_bailout(); |
---|
2460 | } |
---|
2461 | |
---|
2462 | #ifdef ZEND_ENGINE_2_1 |
---|
2463 | zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1 TSRMLS_CC); |
---|
2464 | #endif |
---|
2465 | if (zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &server) != SUCCESS || Z_TYPE_PP(server) != IS_ARRAY) { |
---|
2466 | php_error_docref(NULL TSRMLS_CC, E_ERROR, "_SERVER is corrupted"); |
---|
2467 | zend_bailout(); |
---|
2468 | } |
---|
2469 | ht = HASH_OF((*server)); |
---|
2470 | |
---|
2471 | if (zend_hash_find(ht, "PHP_AUTH_USER", sizeof("PHP_AUTH_USER"), (void **) &user) == FAILURE) { |
---|
2472 | user = NULL; |
---|
2473 | } |
---|
2474 | else if (Z_TYPE_PP(user) != IS_STRING) { |
---|
2475 | user = NULL; |
---|
2476 | } |
---|
2477 | |
---|
2478 | if (zend_hash_find(ht, "PHP_AUTH_PW", sizeof("PHP_AUTH_PW"), (void **) &pass) == FAILURE) { |
---|
2479 | pass = NULL; |
---|
2480 | } |
---|
2481 | else if (Z_TYPE_PP(pass) != IS_STRING) { |
---|
2482 | pass = NULL; |
---|
2483 | } |
---|
2484 | |
---|
2485 | if (user != NULL && pass != NULL && strcmp(admin_user, Z_STRVAL_PP(user)) == 0) { |
---|
2486 | PHP_MD5_CTX context; |
---|
2487 | char md5str[33]; |
---|
2488 | unsigned char digest[16]; |
---|
2489 | |
---|
2490 | PHP_MD5Init(&context); |
---|
2491 | PHP_MD5Update(&context, (unsigned char *) Z_STRVAL_PP(pass), Z_STRLEN_PP(pass)); |
---|
2492 | PHP_MD5Final(digest, &context); |
---|
2493 | |
---|
2494 | md5str[0] = '\0'; |
---|
2495 | make_digest(md5str, digest); |
---|
2496 | if (strcmp(admin_pass, md5str) == 0) { |
---|
2497 | return 1; |
---|
2498 | } |
---|
2499 | } |
---|
2500 | |
---|
2501 | #define STR "HTTP/1.0 401 Unauthorized" |
---|
2502 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
---|
2503 | #undef STR |
---|
2504 | #define STR "WWW-authenticate: Basic Realm=\"XCache Administration\"" |
---|
2505 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
---|
2506 | #undef STR |
---|
2507 | #define STR "Content-type: text/html; charset=UTF-8" |
---|
2508 | sapi_add_header_ex(STR, sizeof(STR) - 1, 1, 1 TSRMLS_CC); |
---|
2509 | #undef STR |
---|
2510 | ZEND_PUTS("<html>\n"); |
---|
2511 | ZEND_PUTS("<head><title>XCache Authentication Failed</title></head>\n"); |
---|
2512 | ZEND_PUTS("<body>\n"); |
---|
2513 | ZEND_PUTS("<h1>XCache Authentication Failed</h1>\n"); |
---|
2514 | ZEND_PUTS("<p>You're not authorized to access this page due to wrong username and/or password you typed.<br />The following check points is suggested:</p>\n"); |
---|
2515 | ZEND_PUTS("<ul>\n"); |
---|
2516 | ZEND_PUTS("<li>Be aware that `Username' and `Password' is case sense. Check capslock status led on your keyboard, and punch left/right Shift keys once for each</li>\n"); |
---|
2517 | ZEND_PUTS("<li>Make sure the md5 password is generated correctly. You may use <a href=\"mkpassword.php\">mkpassword.php</a></li>\n"); |
---|
2518 | ZEND_PUTS("<li>Reload browser cache by pressing F5 and/or Ctrl+F5, or simply clear browser cache after you've updated username/password in php ini.</li>\n"); |
---|
2519 | ZEND_PUTS("</ul>\n"); |
---|
2520 | ZEND_PUTS("Check <a href=\"" XCACHE_WIKI_URL "/InstallAdministration\">XCache wiki page</a> for more information.\n"); |
---|
2521 | ZEND_PUTS("</body>\n"); |
---|
2522 | ZEND_PUTS("</html>\n"); |
---|
2523 | |
---|
2524 | zend_bailout(); |
---|
2525 | return 0; |
---|
2526 | } |
---|
2527 | /* }}} */ |
---|
2528 | /* {{{ xcache_admin_operate */ |
---|
2529 | typedef enum { XC_OP_COUNT, XC_OP_INFO, XC_OP_LIST, XC_OP_CLEAR } xcache_op_type; |
---|
2530 | static void xcache_admin_operate(xcache_op_type optype, INTERNAL_FUNCTION_PARAMETERS) |
---|
2531 | { |
---|
2532 | long type; |
---|
2533 | int size; |
---|
2534 | xc_cache_t **caches, *cache; |
---|
2535 | long id = 0; |
---|
2536 | |
---|
2537 | xcache_admin_auth_check(TSRMLS_C); |
---|
2538 | |
---|
2539 | if (!xc_initized) { |
---|
2540 | RETURN_NULL(); |
---|
2541 | } |
---|
2542 | |
---|
2543 | if (optype == XC_OP_COUNT) { |
---|
2544 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type) == FAILURE) { |
---|
2545 | return; |
---|
2546 | } |
---|
2547 | } |
---|
2548 | else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &type, &id) == FAILURE) { |
---|
2549 | return; |
---|
2550 | } |
---|
2551 | |
---|
2552 | switch (type) { |
---|
2553 | case XC_TYPE_PHP: |
---|
2554 | size = xc_php_hcache.size; |
---|
2555 | caches = xc_php_caches; |
---|
2556 | break; |
---|
2557 | |
---|
2558 | case XC_TYPE_VAR: |
---|
2559 | size = xc_var_hcache.size; |
---|
2560 | caches = xc_var_caches; |
---|
2561 | break; |
---|
2562 | |
---|
2563 | default: |
---|
2564 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown type %ld", type); |
---|
2565 | RETURN_FALSE; |
---|
2566 | } |
---|
2567 | |
---|
2568 | switch (optype) { |
---|
2569 | case XC_OP_COUNT: |
---|
2570 | RETURN_LONG(size) |
---|
2571 | break; |
---|
2572 | |
---|
2573 | case XC_OP_INFO: |
---|
2574 | case XC_OP_LIST: |
---|
2575 | if (id < 0 || id >= size) { |
---|
2576 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists"); |
---|
2577 | RETURN_FALSE; |
---|
2578 | } |
---|
2579 | |
---|
2580 | array_init(return_value); |
---|
2581 | |
---|
2582 | cache = caches[id]; |
---|
2583 | ENTER_LOCK(cache) { |
---|
2584 | if (optype == XC_OP_INFO) { |
---|
2585 | xc_fillinfo_dmz(type, cache, return_value TSRMLS_CC); |
---|
2586 | } |
---|
2587 | else { |
---|
2588 | xc_filllist_dmz(type, cache, return_value TSRMLS_CC); |
---|
2589 | } |
---|
2590 | } LEAVE_LOCK(cache); |
---|
2591 | break; |
---|
2592 | case XC_OP_CLEAR: |
---|
2593 | { |
---|
2594 | xc_entry_t *e, *next; |
---|
2595 | int entryslotid, c; |
---|
2596 | |
---|
2597 | if (id < 0 || id >= size) { |
---|
2598 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache not exists"); |
---|
2599 | RETURN_FALSE; |
---|
2600 | } |
---|
2601 | |
---|
2602 | cache = caches[id]; |
---|
2603 | ENTER_LOCK(cache) { |
---|
2604 | for (entryslotid = 0, c = cache->hentry->size; entryslotid < c; entryslotid ++) { |
---|
2605 | for (e = cache->entries[entryslotid]; e; e = next) { |
---|
2606 | next = e->next; |
---|
2607 | xc_entry_remove_dmz(type, cache, entryslotid, e TSRMLS_CC); |
---|
2608 | } |
---|
2609 | cache->entries[entryslotid] = NULL; |
---|
2610 | } |
---|
2611 | } LEAVE_LOCK(cache); |
---|
2612 | xc_gc_deletes(TSRMLS_C); |
---|
2613 | } |
---|
2614 | break; |
---|
2615 | |
---|
2616 | default: |
---|
2617 | assert(0); |
---|
2618 | } |
---|
2619 | } |
---|
2620 | /* }}} */ |
---|
2621 | /* {{{ proto int xcache_count(int type) |
---|
2622 | Return count of cache on specified cache type */ |
---|
2623 | PHP_FUNCTION(xcache_count) |
---|
2624 | { |
---|
2625 | xcache_admin_operate(XC_OP_COUNT, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
2626 | } |
---|
2627 | /* }}} */ |
---|
2628 | /* {{{ proto array xcache_info(int type, int id) |
---|
2629 | Get cache info by id on specified cache type */ |
---|
2630 | PHP_FUNCTION(xcache_info) |
---|
2631 | { |
---|
2632 | xcache_admin_operate(XC_OP_INFO, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
2633 | } |
---|
2634 | /* }}} */ |
---|
2635 | /* {{{ proto array xcache_list(int type, int id) |
---|
2636 | Get cache entries list by id on specified cache type */ |
---|
2637 | PHP_FUNCTION(xcache_list) |
---|
2638 | { |
---|
2639 | xcache_admin_operate(XC_OP_LIST, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
2640 | } |
---|
2641 | /* }}} */ |
---|
2642 | /* {{{ proto array xcache_clear_cache(int type, int id) |
---|
2643 | Clear cache by id on specified cache type */ |
---|
2644 | PHP_FUNCTION(xcache_clear_cache) |
---|
2645 | { |
---|
2646 | xcache_admin_operate(XC_OP_CLEAR, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
2647 | } |
---|
2648 | /* }}} */ |
---|
2649 | |
---|
2650 | #define VAR_DISABLED_WARNING() do { \ |
---|
2651 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "xcache.var_size is either 0 or too small to enable var data caching"); \ |
---|
2652 | } while (0) |
---|
2653 | |
---|
2654 | static int xc_entry_init_key_var(xc_entry_hash_t *entry_hash, xc_entry_var_t *xce, zval *name TSRMLS_DC) /* {{{ */ |
---|
2655 | { |
---|
2656 | xc_hash_value_t hv; |
---|
2657 | |
---|
2658 | switch (Z_TYPE_P(name)) { |
---|
2659 | #ifdef IS_UNICODE |
---|
2660 | case IS_UNICODE: |
---|
2661 | #endif |
---|
2662 | case IS_STRING: |
---|
2663 | break; |
---|
2664 | default: |
---|
2665 | #ifdef IS_UNICODE |
---|
2666 | convert_to_unicode(name); |
---|
2667 | #else |
---|
2668 | convert_to_string(name); |
---|
2669 | #endif |
---|
2670 | } |
---|
2671 | #ifdef IS_UNICODE |
---|
2672 | xce->entry.name_type = name->type; |
---|
2673 | #endif |
---|
2674 | xce->entry.name = name->value; |
---|
2675 | |
---|
2676 | hv = xc_entry_hash_var((xc_entry_t *) xce TSRMLS_CC); |
---|
2677 | |
---|
2678 | entry_hash->cacheslotid = (hv & xc_var_hcache.mask); |
---|
2679 | hv >>= xc_var_hcache.bits; |
---|
2680 | entry_hash->entryslotid = (hv & xc_var_hentry.mask); |
---|
2681 | return SUCCESS; |
---|
2682 | } |
---|
2683 | /* }}} */ |
---|
2684 | /* {{{ proto mixed xcache_get(string name) |
---|
2685 | Get cached data by specified name */ |
---|
2686 | PHP_FUNCTION(xcache_get) |
---|
2687 | { |
---|
2688 | xc_entry_hash_t entry_hash; |
---|
2689 | xc_cache_t *cache; |
---|
2690 | xc_entry_var_t xce, *stored_xce; |
---|
2691 | zval *name; |
---|
2692 | |
---|
2693 | if (!xc_var_caches) { |
---|
2694 | VAR_DISABLED_WARNING(); |
---|
2695 | RETURN_NULL(); |
---|
2696 | } |
---|
2697 | |
---|
2698 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
---|
2699 | return; |
---|
2700 | } |
---|
2701 | xc_entry_init_key_var(&entry_hash, &xce, name TSRMLS_CC); |
---|
2702 | cache = xc_var_caches[entry_hash.cacheslotid]; |
---|
2703 | |
---|
2704 | ENTER_LOCK(cache) { |
---|
2705 | stored_xce = (xc_entry_var_t *) xc_entry_find_dmz(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &xce TSRMLS_CC); |
---|
2706 | if (stored_xce) { |
---|
2707 | /* return */ |
---|
2708 | xc_processor_restore_zval(return_value, stored_xce->value, stored_xce->have_references TSRMLS_CC); |
---|
2709 | xc_cache_hit_dmz(cache TSRMLS_CC); |
---|
2710 | } |
---|
2711 | else { |
---|
2712 | RETVAL_NULL(); |
---|
2713 | cache->misses ++; |
---|
2714 | } |
---|
2715 | } LEAVE_LOCK(cache); |
---|
2716 | } |
---|
2717 | /* }}} */ |
---|
2718 | /* {{{ proto bool xcache_set(string name, mixed value [, int ttl]) |
---|
2719 | Store data to cache by specified name */ |
---|
2720 | PHP_FUNCTION(xcache_set) |
---|
2721 | { |
---|
2722 | xc_entry_hash_t entry_hash; |
---|
2723 | xc_cache_t *cache; |
---|
2724 | xc_entry_var_t xce, *stored_xce; |
---|
2725 | zval *name; |
---|
2726 | zval *value; |
---|
2727 | |
---|
2728 | if (!xc_var_caches) { |
---|
2729 | VAR_DISABLED_WARNING(); |
---|
2730 | RETURN_NULL(); |
---|
2731 | } |
---|
2732 | |
---|
2733 | xce.entry.ttl = XG(var_ttl); |
---|
2734 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|l", &name, &value, &xce.entry.ttl) == FAILURE) { |
---|
2735 | return; |
---|
2736 | } |
---|
2737 | |
---|
2738 | /* max ttl */ |
---|
2739 | if (xc_var_maxttl && (!xce.entry.ttl || xce.entry.ttl > xc_var_maxttl)) { |
---|
2740 | xce.entry.ttl = xc_var_maxttl; |
---|
2741 | } |
---|
2742 | |
---|
2743 | xc_entry_init_key_var(&entry_hash, &xce, name TSRMLS_CC); |
---|
2744 | cache = xc_var_caches[entry_hash.cacheslotid]; |
---|
2745 | |
---|
2746 | ENTER_LOCK(cache) { |
---|
2747 | stored_xce = (xc_entry_var_t *) xc_entry_find_dmz(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &xce TSRMLS_CC); |
---|
2748 | if (stored_xce) { |
---|
2749 | xc_entry_remove_dmz(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) stored_xce TSRMLS_CC); |
---|
2750 | } |
---|
2751 | xce.value = value; |
---|
2752 | RETVAL_BOOL(xc_entry_var_store_dmz(cache, entry_hash.entryslotid, &xce TSRMLS_CC) != NULL ? 1 : 0); |
---|
2753 | } LEAVE_LOCK(cache); |
---|
2754 | } |
---|
2755 | /* }}} */ |
---|
2756 | /* {{{ proto bool xcache_isset(string name) |
---|
2757 | Check if an entry exists in cache by specified name */ |
---|
2758 | PHP_FUNCTION(xcache_isset) |
---|
2759 | { |
---|
2760 | xc_entry_hash_t entry_hash; |
---|
2761 | xc_cache_t *cache; |
---|
2762 | xc_entry_var_t xce, *stored_xce; |
---|
2763 | zval *name; |
---|
2764 | |
---|
2765 | if (!xc_var_caches) { |
---|
2766 | VAR_DISABLED_WARNING(); |
---|
2767 | RETURN_FALSE; |
---|
2768 | } |
---|
2769 | |
---|
2770 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
---|
2771 | return; |
---|
2772 | } |
---|
2773 | xc_entry_init_key_var(&entry_hash, &xce, name TSRMLS_CC); |
---|
2774 | cache = xc_var_caches[entry_hash.cacheslotid]; |
---|
2775 | |
---|
2776 | ENTER_LOCK(cache) { |
---|
2777 | stored_xce = (xc_entry_var_t *) xc_entry_find_dmz(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &xce TSRMLS_CC); |
---|
2778 | if (stored_xce) { |
---|
2779 | xc_cache_hit_dmz(cache TSRMLS_CC); |
---|
2780 | RETVAL_TRUE; |
---|
2781 | /* return */ |
---|
2782 | } |
---|
2783 | else { |
---|
2784 | RETVAL_FALSE; |
---|
2785 | } |
---|
2786 | |
---|
2787 | } LEAVE_LOCK(cache); |
---|
2788 | } |
---|
2789 | /* }}} */ |
---|
2790 | /* {{{ proto bool xcache_unset(string name) |
---|
2791 | Unset existing data in cache by specified name */ |
---|
2792 | PHP_FUNCTION(xcache_unset) |
---|
2793 | { |
---|
2794 | xc_entry_hash_t entry_hash; |
---|
2795 | xc_cache_t *cache; |
---|
2796 | xc_entry_var_t xce, *stored_xce; |
---|
2797 | zval *name; |
---|
2798 | |
---|
2799 | if (!xc_var_caches) { |
---|
2800 | VAR_DISABLED_WARNING(); |
---|
2801 | RETURN_FALSE; |
---|
2802 | } |
---|
2803 | |
---|
2804 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &name) == FAILURE) { |
---|
2805 | return; |
---|
2806 | } |
---|
2807 | xc_entry_init_key_var(&entry_hash, &xce, name TSRMLS_CC); |
---|
2808 | cache = xc_var_caches[entry_hash.cacheslotid]; |
---|
2809 | |
---|
2810 | ENTER_LOCK(cache) { |
---|
2811 | stored_xce = (xc_entry_var_t *) xc_entry_find_dmz(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &xce TSRMLS_CC); |
---|
2812 | if (stored_xce) { |
---|
2813 | xc_entry_remove_dmz(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) stored_xce TSRMLS_CC); |
---|
2814 | RETVAL_TRUE; |
---|
2815 | } |
---|
2816 | else { |
---|
2817 | RETVAL_FALSE; |
---|
2818 | } |
---|
2819 | } LEAVE_LOCK(cache); |
---|
2820 | } |
---|
2821 | /* }}} */ |
---|
2822 | /* {{{ proto bool xcache_unset_by_prefix(string prefix) |
---|
2823 | Unset existing data in cache by specified prefix */ |
---|
2824 | PHP_FUNCTION(xcache_unset_by_prefix) |
---|
2825 | { |
---|
2826 | zval *prefix; |
---|
2827 | int i, iend; |
---|
2828 | |
---|
2829 | if (!xc_var_caches) { |
---|
2830 | VAR_DISABLED_WARNING(); |
---|
2831 | RETURN_FALSE; |
---|
2832 | } |
---|
2833 | |
---|
2834 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &prefix) == FAILURE) { |
---|
2835 | return; |
---|
2836 | } |
---|
2837 | |
---|
2838 | for (i = 0, iend = xc_var_hcache.size; i < iend; i ++) { |
---|
2839 | xc_cache_t *cache = xc_var_caches[i]; |
---|
2840 | ENTER_LOCK(cache) { |
---|
2841 | int entryslotid, jend; |
---|
2842 | for (entryslotid = 0, jend = cache->hentry->size; entryslotid < jend; entryslotid ++) { |
---|
2843 | xc_entry_t *entry, *next; |
---|
2844 | for (entry = cache->entries[entryslotid]; entry; entry = next) { |
---|
2845 | next = entry->next; |
---|
2846 | if (xc_entry_has_prefix_dmz(XC_TYPE_VAR, entry, prefix)) { |
---|
2847 | xc_entry_remove_dmz(XC_TYPE_VAR, cache, entryslotid, entry TSRMLS_CC); |
---|
2848 | } |
---|
2849 | } |
---|
2850 | } |
---|
2851 | } LEAVE_LOCK(cache); |
---|
2852 | } |
---|
2853 | } |
---|
2854 | /* }}} */ |
---|
2855 | static inline void xc_var_inc_dec(int inc, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */ |
---|
2856 | { |
---|
2857 | xc_entry_hash_t entry_hash; |
---|
2858 | xc_cache_t *cache; |
---|
2859 | xc_entry_var_t xce, *stored_xce; |
---|
2860 | zval *name; |
---|
2861 | long count = 1; |
---|
2862 | long value = 0; |
---|
2863 | zval oldzval; |
---|
2864 | |
---|
2865 | if (!xc_var_caches) { |
---|
2866 | VAR_DISABLED_WARNING(); |
---|
2867 | RETURN_NULL(); |
---|
2868 | } |
---|
2869 | |
---|
2870 | xce.entry.ttl = XG(var_ttl); |
---|
2871 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", &name, &count, &xce.entry.ttl) == FAILURE) { |
---|
2872 | return; |
---|
2873 | } |
---|
2874 | |
---|
2875 | /* max ttl */ |
---|
2876 | if (xc_var_maxttl && (!xce.entry.ttl || xce.entry.ttl > xc_var_maxttl)) { |
---|
2877 | xce.entry.ttl = xc_var_maxttl; |
---|
2878 | } |
---|
2879 | |
---|
2880 | xc_entry_init_key_var(&entry_hash, &xce, name TSRMLS_CC); |
---|
2881 | cache = xc_var_caches[entry_hash.cacheslotid]; |
---|
2882 | |
---|
2883 | ENTER_LOCK(cache) { |
---|
2884 | stored_xce = (xc_entry_var_t *) xc_entry_find_dmz(XC_TYPE_VAR, cache, entry_hash.entryslotid, (xc_entry_t *) &xce TSRMLS_CC); |
---|
2885 | if (stored_xce) { |
---|
2886 | TRACE("incdec: gotxce %s", xce.entry.name.str.val); |
---|
2887 | /* do it in place */ |
---|
2888 | if (Z_TYPE_P(stored_xce->value) == IS_LONG) { |
---|
2889 | zval *zv; |
---|
2890 | stored_xce->entry.ctime = XG(request_time); |
---|
2891 | stored_xce->entry.ttl = xce.entry.ttl; |
---|
2892 | TRACE("%s", "incdec: islong"); |
---|
2893 | value = Z_LVAL_P(stored_xce->value); |
---|
2894 | value += (inc == 1 ? count : - count); |
---|
2895 | RETVAL_LONG(value); |
---|
2896 | |
---|
2897 | zv = (zval *) cache->shm->handlers->to_readwrite(cache->shm, (char *) stored_xce->value); |
---|
2898 | Z_LVAL_P(zv) = value; |
---|
2899 | break; /* leave lock */ |
---|
2900 | } |
---|
2901 | |
---|
2902 | TRACE("%s", "incdec: notlong"); |
---|
2903 | xc_processor_restore_zval(&oldzval, stored_xce->value, stored_xce->have_references TSRMLS_CC); |
---|
2904 | convert_to_long(&oldzval); |
---|
2905 | value = Z_LVAL(oldzval); |
---|
2906 | zval_dtor(&oldzval); |
---|
2907 | } |
---|
2908 | else { |
---|
2909 | TRACE("incdec: %s not found", xce.entry.name.str.val); |
---|
2910 | } |
---|
2911 | |
---|
2912 | value += (inc == 1 ? count : - count); |
---|
2913 | RETVAL_LONG(value); |
---|
2914 | xce.value = return_value; |
---|
2915 | |
---|
2916 | if (stored_xce) { |
---|
2917 | xce.entry.atime = stored_xce->entry.atime; |
---|
2918 | xce.entry.ctime = stored_xce->entry.ctime; |
---|
2919 | xce.entry.hits = stored_xce->entry.hits; |
---|
2920 | xc_entry_remove_dmz(XC_TYPE_VAR, cache, entry_hash.cacheslotid, (xc_entry_t *) stored_xce TSRMLS_CC); |
---|
2921 | } |
---|
2922 | xc_entry_var_store_dmz(cache, entry_hash.cacheslotid, &xce TSRMLS_CC); |
---|
2923 | |
---|
2924 | } LEAVE_LOCK(cache); |
---|
2925 | } |
---|
2926 | /* }}} */ |
---|
2927 | /* {{{ proto int xcache_inc(string name [, int value [, int ttl]]) |
---|
2928 | Increase an int counter in cache by specified name, create it if not exists */ |
---|
2929 | PHP_FUNCTION(xcache_inc) |
---|
2930 | { |
---|
2931 | xc_var_inc_dec(1, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
2932 | } |
---|
2933 | /* }}} */ |
---|
2934 | /* {{{ proto int xcache_dec(string name [, int value [, int ttl]]) |
---|
2935 | Decrease an int counter in cache by specified name, create it if not exists */ |
---|
2936 | PHP_FUNCTION(xcache_dec) |
---|
2937 | { |
---|
2938 | xc_var_inc_dec(-1, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
2939 | } |
---|
2940 | /* }}} */ |
---|
2941 | /* {{{ proto int xcache_get_refcount(mixed variable) |
---|
2942 | XCache internal uses only: Get reference count of variable */ |
---|
2943 | PHP_FUNCTION(xcache_get_refcount) |
---|
2944 | { |
---|
2945 | zval *variable; |
---|
2946 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &variable) == FAILURE) { |
---|
2947 | RETURN_NULL(); |
---|
2948 | } |
---|
2949 | |
---|
2950 | RETURN_LONG(Z_REFCOUNT(*variable)); |
---|
2951 | } |
---|
2952 | /* }}} */ |
---|
2953 | /* {{{ proto bool xcache_get_isref(mixed variable) |
---|
2954 | XCache internal uses only: Check if variable data is marked referenced */ |
---|
2955 | ZEND_BEGIN_ARG_INFO_EX(arginfo_xcache_get_isref, 0, 0, 1) |
---|
2956 | ZEND_ARG_INFO(1, variable) |
---|
2957 | ZEND_END_ARG_INFO() |
---|
2958 | |
---|
2959 | PHP_FUNCTION(xcache_get_isref) |
---|
2960 | { |
---|
2961 | zval *variable; |
---|
2962 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &variable) == FAILURE) { |
---|
2963 | RETURN_NULL(); |
---|
2964 | } |
---|
2965 | |
---|
2966 | RETURN_BOOL(Z_ISREF(*variable) && Z_REFCOUNT(*variable) >= 3); |
---|
2967 | } |
---|
2968 | /* }}} */ |
---|
2969 | #ifdef HAVE_XCACHE_DPRINT |
---|
2970 | /* {{{ proto bool xcache_dprint(mixed value) |
---|
2971 | Prints variable (or value) internal struct (debug only) */ |
---|
2972 | PHP_FUNCTION(xcache_dprint) |
---|
2973 | { |
---|
2974 | zval *value; |
---|
2975 | |
---|
2976 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) { |
---|
2977 | return; |
---|
2978 | } |
---|
2979 | xc_dprint_zval(value, 0 TSRMLS_CC); |
---|
2980 | } |
---|
2981 | /* }}} */ |
---|
2982 | #endif |
---|
2983 | /* {{{ proto string xcache_asm(string filename) |
---|
2984 | */ |
---|
2985 | #ifdef HAVE_XCACHE_ASSEMBLER |
---|
2986 | PHP_FUNCTION(xcache_asm) |
---|
2987 | { |
---|
2988 | } |
---|
2989 | #endif |
---|
2990 | /* }}} */ |
---|
2991 | #ifdef HAVE_XCACHE_DISASSEMBLER |
---|
2992 | /* {{{ proto array xcache_dasm_file(string filename) |
---|
2993 | Disassemble file into opcode array by filename */ |
---|
2994 | PHP_FUNCTION(xcache_dasm_file) |
---|
2995 | { |
---|
2996 | char *filename; |
---|
2997 | int filename_len; |
---|
2998 | |
---|
2999 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) { |
---|
3000 | return; |
---|
3001 | } |
---|
3002 | if (!filename_len) RETURN_FALSE; |
---|
3003 | |
---|
3004 | xc_dasm_file(return_value, filename TSRMLS_CC); |
---|
3005 | } |
---|
3006 | /* }}} */ |
---|
3007 | /* {{{ proto array xcache_dasm_string(string code) |
---|
3008 | Disassemble php code into opcode array */ |
---|
3009 | PHP_FUNCTION(xcache_dasm_string) |
---|
3010 | { |
---|
3011 | zval *code; |
---|
3012 | |
---|
3013 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &code) == FAILURE) { |
---|
3014 | return; |
---|
3015 | } |
---|
3016 | xc_dasm_string(return_value, code TSRMLS_CC); |
---|
3017 | } |
---|
3018 | /* }}} */ |
---|
3019 | #endif |
---|
3020 | /* {{{ proto string xcache_encode(string filename) |
---|
3021 | Encode php file into XCache opcode encoded format */ |
---|
3022 | #ifdef HAVE_XCACHE_ENCODER |
---|
3023 | PHP_FUNCTION(xcache_encode) |
---|
3024 | { |
---|
3025 | } |
---|
3026 | #endif |
---|
3027 | /* }}} */ |
---|
3028 | /* {{{ proto bool xcache_decode_file(string filename) |
---|
3029 | Decode(load) opcode from XCache encoded format file */ |
---|
3030 | #ifdef HAVE_XCACHE_DECODER |
---|
3031 | PHP_FUNCTION(xcache_decode_file) |
---|
3032 | { |
---|
3033 | } |
---|
3034 | #endif |
---|
3035 | /* }}} */ |
---|
3036 | /* {{{ proto bool xcache_decode_string(string data) |
---|
3037 | Decode(load) opcode from XCache encoded format data */ |
---|
3038 | #ifdef HAVE_XCACHE_DECODER |
---|
3039 | PHP_FUNCTION(xcache_decode_string) |
---|
3040 | { |
---|
3041 | } |
---|
3042 | #endif |
---|
3043 | /* }}} */ |
---|
3044 | /* {{{ xc_call_getter */ |
---|
3045 | typedef const char *(xc_name_getter_t)(zend_uchar type); |
---|
3046 | static void xc_call_getter(xc_name_getter_t getter, int count, INTERNAL_FUNCTION_PARAMETERS) |
---|
3047 | { |
---|
3048 | long spec; |
---|
3049 | const char *name; |
---|
3050 | |
---|
3051 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &spec) == FAILURE) { |
---|
3052 | return; |
---|
3053 | } |
---|
3054 | if (spec >= 0 && spec < count) { |
---|
3055 | name = getter((zend_uchar) spec); |
---|
3056 | if (name) { |
---|
3057 | /* RETURN_STRING */ |
---|
3058 | int len = strlen(name); |
---|
3059 | return_value->value.str.len = len; |
---|
3060 | return_value->value.str.val = estrndup(name, len); |
---|
3061 | return_value->type = IS_STRING; |
---|
3062 | return; |
---|
3063 | } |
---|
3064 | } |
---|
3065 | RETURN_NULL(); |
---|
3066 | } |
---|
3067 | /* }}} */ |
---|
3068 | /* {{{ proto string xcache_get_op_type(int op_type) */ |
---|
3069 | PHP_FUNCTION(xcache_get_op_type) |
---|
3070 | { |
---|
3071 | xc_call_getter(xc_get_op_type, xc_get_op_type_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
3072 | } |
---|
3073 | /* }}} */ |
---|
3074 | /* {{{ proto string xcache_get_data_type(int type) */ |
---|
3075 | PHP_FUNCTION(xcache_get_data_type) |
---|
3076 | { |
---|
3077 | xc_call_getter(xc_get_data_type, xc_get_data_type_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
3078 | } |
---|
3079 | /* }}} */ |
---|
3080 | /* {{{ proto string xcache_get_opcode(int opcode) */ |
---|
3081 | PHP_FUNCTION(xcache_get_opcode) |
---|
3082 | { |
---|
3083 | xc_call_getter(xc_get_opcode, xc_get_opcode_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
3084 | } |
---|
3085 | /* }}} */ |
---|
3086 | /* {{{ proto string xcache_get_op_spec(int op_type) */ |
---|
3087 | PHP_FUNCTION(xcache_get_op_spec) |
---|
3088 | { |
---|
3089 | xc_call_getter(xc_get_op_spec, xc_get_op_spec_count(), INTERNAL_FUNCTION_PARAM_PASSTHRU); |
---|
3090 | } |
---|
3091 | /* }}} */ |
---|
3092 | #ifdef HAVE_XCACHE_OPCODE_SPEC_DEF |
---|
3093 | /* {{{ proto string xcache_get_opcode_spec(int opcode) */ |
---|
3094 | PHP_FUNCTION(xcache_get_opcode_spec) |
---|
3095 | { |
---|
3096 | long spec; |
---|
3097 | const xc_opcode_spec_t *opspec; |
---|
3098 | |
---|
3099 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &spec) == FAILURE) { |
---|
3100 | return; |
---|
3101 | } |
---|
3102 | if ((zend_uchar) spec <= xc_get_opcode_spec_count()) { |
---|
3103 | opspec = xc_get_opcode_spec((zend_uchar) spec); |
---|
3104 | if (opspec) { |
---|
3105 | array_init(return_value); |
---|
3106 | add_assoc_long_ex(return_value, ZEND_STRS("ext"), opspec->ext); |
---|
3107 | add_assoc_long_ex(return_value, ZEND_STRS("op1"), opspec->op1); |
---|
3108 | add_assoc_long_ex(return_value, ZEND_STRS("op2"), opspec->op2); |
---|
3109 | add_assoc_long_ex(return_value, ZEND_STRS("res"), opspec->res); |
---|
3110 | return; |
---|
3111 | } |
---|
3112 | } |
---|
3113 | RETURN_NULL(); |
---|
3114 | } |
---|
3115 | /* }}} */ |
---|
3116 | #endif |
---|
3117 | /* {{{ proto mixed xcache_get_special_value(zval value) |
---|
3118 | XCache internal use only: For decompiler to get static value with type fixed */ |
---|
3119 | PHP_FUNCTION(xcache_get_special_value) |
---|
3120 | { |
---|
3121 | zval *value; |
---|
3122 | |
---|
3123 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) { |
---|
3124 | return; |
---|
3125 | } |
---|
3126 | |
---|
3127 | switch ((Z_TYPE_P(value) & IS_CONSTANT_TYPE_MASK)) { |
---|
3128 | case IS_CONSTANT: |
---|
3129 | *return_value = *value; |
---|
3130 | zval_copy_ctor(return_value); |
---|
3131 | return_value->type = UNISW(IS_STRING, UG(unicode) ? IS_UNICODE : IS_STRING); |
---|
3132 | break; |
---|
3133 | |
---|
3134 | case IS_CONSTANT_ARRAY: |
---|
3135 | *return_value = *value; |
---|
3136 | zval_copy_ctor(return_value); |
---|
3137 | return_value->type = IS_ARRAY; |
---|
3138 | break; |
---|
3139 | |
---|
3140 | default: |
---|
3141 | RETURN_NULL(); |
---|
3142 | } |
---|
3143 | } |
---|
3144 | /* }}} */ |
---|
3145 | /* {{{ proto int xcache_get_type(zval value) |
---|
3146 | XCache internal use only for disassembler to get variable type in engine level */ |
---|
3147 | PHP_FUNCTION(xcache_get_type) |
---|
3148 | { |
---|
3149 | zval *value; |
---|
3150 | |
---|
3151 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) { |
---|
3152 | return; |
---|
3153 | } |
---|
3154 | |
---|
3155 | RETURN_LONG(Z_TYPE_P(value)); |
---|
3156 | } |
---|
3157 | /* }}} */ |
---|
3158 | /* {{{ proto string xcache_coredump(int op_type) */ |
---|
3159 | PHP_FUNCTION(xcache_coredump) |
---|
3160 | { |
---|
3161 | if (xc_test) { |
---|
3162 | raise(SIGSEGV); |
---|
3163 | } |
---|
3164 | else { |
---|
3165 | php_error_docref(NULL TSRMLS_CC, E_WARNING, "xcache.test must be enabled to test xcache_coredump()"); |
---|
3166 | } |
---|
3167 | } |
---|
3168 | /* }}} */ |
---|
3169 | /* {{{ proto string xcache_is_autoglobal(string name) */ |
---|
3170 | PHP_FUNCTION(xcache_is_autoglobal) |
---|
3171 | { |
---|
3172 | char *name; |
---|
3173 | int name_len; |
---|
3174 | |
---|
3175 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { |
---|
3176 | return; |
---|
3177 | } |
---|
3178 | |
---|
3179 | RETURN_BOOL(zend_hash_exists(CG(auto_globals), name, name_len + 1)); |
---|
3180 | } |
---|
3181 | /* }}} */ |
---|
3182 | static zend_function_entry xcache_functions[] = /* {{{ */ |
---|
3183 | { |
---|
3184 | PHP_FE(xcache_count, NULL) |
---|
3185 | PHP_FE(xcache_info, NULL) |
---|
3186 | PHP_FE(xcache_list, NULL) |
---|
3187 | PHP_FE(xcache_clear_cache, NULL) |
---|
3188 | PHP_FE(xcache_coredump, NULL) |
---|
3189 | #ifdef HAVE_XCACHE_ASSEMBLER |
---|
3190 | PHP_FE(xcache_asm, NULL) |
---|
3191 | #endif |
---|
3192 | #ifdef HAVE_XCACHE_DISASSEMBLER |
---|
3193 | PHP_FE(xcache_dasm_file, NULL) |
---|
3194 | PHP_FE(xcache_dasm_string, NULL) |
---|
3195 | #endif |
---|
3196 | #ifdef HAVE_XCACHE_ENCODER |
---|
3197 | PHP_FE(xcache_encode, NULL) |
---|
3198 | #endif |
---|
3199 | #ifdef HAVE_XCACHE_DECODER |
---|
3200 | PHP_FE(xcache_decode_file, NULL) |
---|
3201 | PHP_FE(xcache_decode_string, NULL) |
---|
3202 | #endif |
---|
3203 | #ifdef HAVE_XCACHE_COVERAGER |
---|
3204 | PHP_FE(xcache_coverager_decode, NULL) |
---|
3205 | PHP_FE(xcache_coverager_start, NULL) |
---|
3206 | PHP_FE(xcache_coverager_stop, NULL) |
---|
3207 | PHP_FE(xcache_coverager_get, NULL) |
---|
3208 | #endif |
---|
3209 | PHP_FE(xcache_get_special_value, NULL) |
---|
3210 | PHP_FE(xcache_get_type, NULL) |
---|
3211 | PHP_FE(xcache_get_op_type, NULL) |
---|
3212 | PHP_FE(xcache_get_data_type, NULL) |
---|
3213 | PHP_FE(xcache_get_opcode, NULL) |
---|
3214 | #ifdef HAVE_XCACHE_OPCODE_SPEC_DEF |
---|
3215 | PHP_FE(xcache_get_opcode_spec, NULL) |
---|
3216 | #endif |
---|
3217 | PHP_FE(xcache_is_autoglobal, NULL) |
---|
3218 | PHP_FE(xcache_inc, NULL) |
---|
3219 | PHP_FE(xcache_dec, NULL) |
---|
3220 | PHP_FE(xcache_get, NULL) |
---|
3221 | PHP_FE(xcache_set, NULL) |
---|
3222 | PHP_FE(xcache_isset, NULL) |
---|
3223 | PHP_FE(xcache_unset, NULL) |
---|
3224 | PHP_FE(xcache_unset_by_prefix, NULL) |
---|
3225 | PHP_FE(xcache_get_refcount, NULL) |
---|
3226 | PHP_FE(xcache_get_isref, arginfo_xcache_get_isref) |
---|
3227 | #ifdef HAVE_XCACHE_DPRINT |
---|
3228 | PHP_FE(xcache_dprint, NULL) |
---|
3229 | #endif |
---|
3230 | {NULL, NULL, NULL} |
---|
3231 | }; |
---|
3232 | /* }}} */ |
---|
3233 | |
---|
3234 | /* old signal handlers {{{ */ |
---|
3235 | typedef void (*xc_sighandler_t)(int); |
---|
3236 | #define FOREACH_SIG(sig) static xc_sighandler_t old_##sig##_handler = NULL |
---|
3237 | #include "foreachcoresig.h" |
---|
3238 | #undef FOREACH_SIG |
---|
3239 | /* }}} */ |
---|
3240 | static void xcache_signal_handler(int sig); |
---|
3241 | static void xcache_restore_signal_handler() /* {{{ */ |
---|
3242 | { |
---|
3243 | #define FOREACH_SIG(sig) do { \ |
---|
3244 | if (old_##sig##_handler != xcache_signal_handler) { \ |
---|
3245 | signal(sig, old_##sig##_handler); \ |
---|
3246 | } \ |
---|
3247 | else { \ |
---|
3248 | signal(sig, SIG_DFL); \ |
---|
3249 | } \ |
---|
3250 | } while (0) |
---|
3251 | #include "foreachcoresig.h" |
---|
3252 | #undef FOREACH_SIG |
---|
3253 | } |
---|
3254 | /* }}} */ |
---|
3255 | static void xcache_init_signal_handler() /* {{{ */ |
---|
3256 | { |
---|
3257 | #define FOREACH_SIG(sig) \ |
---|
3258 | old_##sig##_handler = signal(sig, xcache_signal_handler) |
---|
3259 | #include "foreachcoresig.h" |
---|
3260 | #undef FOREACH_SIG |
---|
3261 | } |
---|
3262 | /* }}} */ |
---|
3263 | static void xcache_signal_handler(int sig) /* {{{ */ |
---|
3264 | { |
---|
3265 | xcache_restore_signal_handler(); |
---|
3266 | if (xc_coredump_dir && xc_coredump_dir[0]) { |
---|
3267 | if (chdir(xc_coredump_dir) != 0) { |
---|
3268 | /* error, but nothing can do about it |
---|
3269 | * and should'nt print anything which might SEGV again */ |
---|
3270 | } |
---|
3271 | } |
---|
3272 | raise(sig); |
---|
3273 | } |
---|
3274 | /* }}} */ |
---|
3275 | |
---|
3276 | /* {{{ PHP_INI */ |
---|
3277 | |
---|
3278 | static PHP_INI_MH(xc_OnUpdateDummy) |
---|
3279 | { |
---|
3280 | return SUCCESS; |
---|
3281 | } |
---|
3282 | |
---|
3283 | static PHP_INI_MH(xc_OnUpdateULong) |
---|
3284 | { |
---|
3285 | zend_ulong *p = (zend_ulong *) mh_arg1; |
---|
3286 | |
---|
3287 | *p = (zend_ulong) atoi(new_value); |
---|
3288 | return SUCCESS; |
---|
3289 | } |
---|
3290 | |
---|
3291 | static PHP_INI_MH(xc_OnUpdateBool) |
---|
3292 | { |
---|
3293 | zend_bool *p = (zend_bool *)mh_arg1; |
---|
3294 | |
---|
3295 | if (strncasecmp("on", new_value, sizeof("on"))) { |
---|
3296 | *p = (zend_bool) atoi(new_value); |
---|
3297 | } |
---|
3298 | else { |
---|
3299 | *p = (zend_bool) 1; |
---|
3300 | } |
---|
3301 | return SUCCESS; |
---|
3302 | } |
---|
3303 | |
---|
3304 | static PHP_INI_MH(xc_OnUpdateString) |
---|
3305 | { |
---|
3306 | char **p = (char**)mh_arg1; |
---|
3307 | if (*p) { |
---|
3308 | pefree(*p, 1); |
---|
3309 | } |
---|
3310 | *p = pemalloc(strlen(new_value) + 1, 1); |
---|
3311 | strcpy(*p, new_value); |
---|
3312 | return SUCCESS; |
---|
3313 | } |
---|
3314 | |
---|
3315 | #ifndef ZEND_ENGINE_2 |
---|
3316 | #define OnUpdateLong OnUpdateInt |
---|
3317 | #endif |
---|
3318 | |
---|
3319 | #ifdef ZEND_WIN32 |
---|
3320 | # define DEFAULT_PATH "xcache" |
---|
3321 | #else |
---|
3322 | # define DEFAULT_PATH "/dev/zero" |
---|
3323 | #endif |
---|
3324 | PHP_INI_BEGIN() |
---|
3325 | PHP_INI_ENTRY1 ("xcache.mmap_path", DEFAULT_PATH, PHP_INI_SYSTEM, xc_OnUpdateString, &xc_mmap_path) |
---|
3326 | PHP_INI_ENTRY1 ("xcache.coredump_directory", "", PHP_INI_SYSTEM, xc_OnUpdateString, &xc_coredump_dir) |
---|
3327 | PHP_INI_ENTRY1 ("xcache.test", "0", PHP_INI_SYSTEM, xc_OnUpdateBool, &xc_test) |
---|
3328 | PHP_INI_ENTRY1 ("xcache.readonly_protection", "0", PHP_INI_SYSTEM, xc_OnUpdateBool, &xc_readonly_protection) |
---|
3329 | /* opcode cache */ |
---|
3330 | PHP_INI_ENTRY1 ("xcache.size", "0", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
---|
3331 | PHP_INI_ENTRY1 ("xcache.count", "1", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
---|
3332 | PHP_INI_ENTRY1 ("xcache.slots", "8K", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
---|
3333 | PHP_INI_ENTRY1 ("xcache.shm_scheme", "mmap", PHP_INI_SYSTEM, xc_OnUpdateString, &xc_shm_scheme) |
---|
3334 | PHP_INI_ENTRY1 ("xcache.ttl", "0", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_php_ttl) |
---|
3335 | PHP_INI_ENTRY1 ("xcache.gc_interval", "0", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_php_gc_interval) |
---|
3336 | /* var cache */ |
---|
3337 | PHP_INI_ENTRY1 ("xcache.var_size", "0", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
---|
3338 | PHP_INI_ENTRY1 ("xcache.var_count", "1", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
---|
3339 | PHP_INI_ENTRY1 ("xcache.var_slots", "8K", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
---|
3340 | PHP_INI_ENTRY1 ("xcache.var_maxttl", "0", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_var_maxttl) |
---|
3341 | PHP_INI_ENTRY1 ("xcache.var_gc_interval", "120", PHP_INI_SYSTEM, xc_OnUpdateULong, &xc_var_gc_interval) |
---|
3342 | |
---|
3343 | STD_PHP_INI_BOOLEAN("xcache.cacher", "1", PHP_INI_ALL, OnUpdateBool, cacher, zend_xcache_globals, xcache_globals) |
---|
3344 | STD_PHP_INI_BOOLEAN("xcache.stat", "1", PHP_INI_ALL, OnUpdateBool, stat, zend_xcache_globals, xcache_globals) |
---|
3345 | STD_PHP_INI_BOOLEAN("xcache.experimental", "0", PHP_INI_ALL, OnUpdateBool, experimental, zend_xcache_globals, xcache_globals) |
---|
3346 | #ifdef HAVE_XCACHE_OPTIMIZER |
---|
3347 | STD_PHP_INI_BOOLEAN("xcache.optimizer", "0", PHP_INI_ALL, OnUpdateBool, optimizer, zend_xcache_globals, xcache_globals) |
---|
3348 | #endif |
---|
3349 | STD_PHP_INI_ENTRY ("xcache.var_ttl", "0", PHP_INI_ALL, OnUpdateLong, var_ttl, zend_xcache_globals, xcache_globals) |
---|
3350 | #ifdef HAVE_XCACHE_COVERAGER |
---|
3351 | STD_PHP_INI_BOOLEAN("xcache.coverager" , "0", PHP_INI_ALL, OnUpdateBool, coverager, zend_xcache_globals, xcache_globals) |
---|
3352 | PHP_INI_ENTRY1 ("xcache.coveragedump_directory", "", PHP_INI_SYSTEM, xc_OnUpdateDummy, NULL) |
---|
3353 | #endif |
---|
3354 | PHP_INI_END() |
---|
3355 | /* }}} */ |
---|
3356 | /* {{{ PHP_MINFO_FUNCTION(xcache) */ |
---|
3357 | static PHP_MINFO_FUNCTION(xcache) |
---|
3358 | { |
---|
3359 | char buf[100]; |
---|
3360 | char *ptr; |
---|
3361 | int left, len; |
---|
3362 | xc_shm_scheme_t *scheme; |
---|
3363 | #ifdef HAVE_XCACHE_COVERAGER |
---|
3364 | char *covdumpdir; |
---|
3365 | #endif |
---|
3366 | |
---|
3367 | php_info_print_table_start(); |
---|
3368 | php_info_print_table_header(2, "XCache Support", "enabled"); |
---|
3369 | php_info_print_table_row(2, "Version", XCACHE_VERSION); |
---|
3370 | php_info_print_table_row(2, "Modules Built", XCACHE_MODULES); |
---|
3371 | php_info_print_table_row(2, "Readonly Protection", xc_readonly_protection ? "enabled" : "N/A"); |
---|
3372 | #ifdef ZEND_ENGINE_2_1 |
---|
3373 | ptr = php_format_date("Y-m-d H:i:s", sizeof("Y-m-d H:i:s") - 1, xc_init_time, 1 TSRMLS_CC); |
---|
3374 | php_info_print_table_row(2, "Cache Init Time", ptr); |
---|
3375 | efree(ptr); |
---|
3376 | #else |
---|
3377 | snprintf(buf, sizeof(buf), "%lu", (long unsigned) xc_init_time); |
---|
3378 | php_info_print_table_row(2, "Cache Init Time", buf); |
---|
3379 | #endif |
---|
3380 | |
---|
3381 | #ifdef ZTS |
---|
3382 | snprintf(buf, sizeof(buf), "%lu.%lu", xc_init_instance_id, xc_init_instance_subid); |
---|
3383 | #else |
---|
3384 | snprintf(buf, sizeof(buf), "%lu", xc_init_instance_id); |
---|
3385 | #endif |
---|
3386 | php_info_print_table_row(2, "Cache Instance Id", buf); |
---|
3387 | |
---|
3388 | if (xc_php_size) { |
---|
3389 | ptr = _php_math_number_format(xc_php_size, 0, '.', ','); |
---|
3390 | snprintf(buf, sizeof(buf), "enabled, %s bytes, %d split(s), with %d slots each", ptr, xc_php_hcache.size, xc_php_hentry.size); |
---|
3391 | php_info_print_table_row(2, "Opcode Cache", buf); |
---|
3392 | efree(ptr); |
---|
3393 | } |
---|
3394 | else { |
---|
3395 | php_info_print_table_row(2, "Opcode Cache", "disabled"); |
---|
3396 | } |
---|
3397 | if (xc_var_size) { |
---|
3398 | ptr = _php_math_number_format(xc_var_size, 0, '.', ','); |
---|
3399 | snprintf(buf, sizeof(buf), "enabled, %s bytes, %d split(s), with %d slots each", ptr, xc_var_hcache.size, xc_var_hentry.size); |
---|
3400 | php_info_print_table_row(2, "Variable Cache", buf); |
---|
3401 | efree(ptr); |
---|
3402 | } |
---|
3403 | else { |
---|
3404 | php_info_print_table_row(2, "Variable Cache", "disabled"); |
---|
3405 | } |
---|
3406 | |
---|
3407 | left = sizeof(buf); |
---|
3408 | ptr = buf; |
---|
3409 | buf[0] = '\0'; |
---|
3410 | for (scheme = xc_shm_scheme_first(); scheme; scheme = xc_shm_scheme_next(scheme)) { |
---|
3411 | len = snprintf(ptr, left, ptr == buf ? "%s" : ", %s", xc_shm_scheme_name(scheme)); |
---|
3412 | left -= len; |
---|
3413 | ptr += len; |
---|
3414 | } |
---|
3415 | php_info_print_table_row(2, "Shared Memory Schemes", buf); |
---|
3416 | |
---|
3417 | #ifdef HAVE_XCACHE_COVERAGER |
---|
3418 | if (cfg_get_string("xcache.coveragedump_directory", &covdumpdir) != SUCCESS || !covdumpdir[0]) { |
---|
3419 | covdumpdir = NULL; |
---|
3420 | } |
---|
3421 | php_info_print_table_row(2, "Coverage Auto Dumper", XG(coverager) && covdumpdir ? "enabled" : "disabled"); |
---|
3422 | #endif |
---|
3423 | php_info_print_table_end(); |
---|
3424 | |
---|
3425 | DISPLAY_INI_ENTRIES(); |
---|
3426 | } |
---|
3427 | /* }}} */ |
---|
3428 | /* {{{ extension startup */ |
---|
3429 | static void xc_zend_extension_register(zend_extension *new_extension, DL_HANDLE handle) |
---|
3430 | { |
---|
3431 | zend_extension extension; |
---|
3432 | |
---|
3433 | extension = *new_extension; |
---|
3434 | extension.handle = handle; |
---|
3435 | |
---|
3436 | zend_extension_dispatch_message(ZEND_EXTMSG_NEW_EXTENSION, &extension); |
---|
3437 | |
---|
3438 | zend_llist_prepend_element(&zend_extensions, &extension); |
---|
3439 | TRACE("%s", "registered"); |
---|
3440 | } |
---|
3441 | |
---|
3442 | static zend_llist_element *xc_llist_get_element_by_zend_extension(zend_llist *l, const char *extension_name) |
---|
3443 | { |
---|
3444 | zend_llist_element *element; |
---|
3445 | |
---|
3446 | for (element = zend_extensions.head; element; element = element->next) { |
---|
3447 | zend_extension *extension = (zend_extension *) element->data; |
---|
3448 | |
---|
3449 | if (!strcmp(extension->name, extension_name)) { |
---|
3450 | return element; |
---|
3451 | } |
---|
3452 | } |
---|
3453 | return NULL; |
---|
3454 | } |
---|
3455 | |
---|
3456 | static void xc_llist_prepend(zend_llist *l, zend_llist_element *element) |
---|
3457 | { |
---|
3458 | element->next = l->head; |
---|
3459 | element->prev = NULL; |
---|
3460 | if (l->head) { |
---|
3461 | l->head->prev = element; |
---|
3462 | } |
---|
3463 | else { |
---|
3464 | l->tail = element; |
---|
3465 | } |
---|
3466 | l->head = element; |
---|
3467 | ++l->count; |
---|
3468 | } |
---|
3469 | |
---|
3470 | static void xc_llist_unlink(zend_llist *l, zend_llist_element *element) |
---|
3471 | { |
---|
3472 | if ((element)->prev) { |
---|
3473 | (element)->prev->next = (element)->next; |
---|
3474 | } |
---|
3475 | else { |
---|
3476 | (l)->head = (element)->next; |
---|
3477 | } |
---|
3478 | |
---|
3479 | if ((element)->next) { |
---|
3480 | (element)->next->prev = (element)->prev; |
---|
3481 | } |
---|
3482 | else { |
---|
3483 | (l)->tail = (element)->prev; |
---|
3484 | } |
---|
3485 | |
---|
3486 | --l->count; |
---|
3487 | } |
---|
3488 | |
---|
3489 | static int xc_zend_extension_startup(zend_extension *extension) |
---|
3490 | { |
---|
3491 | if (extension->startup) { |
---|
3492 | if (extension->startup(extension) != SUCCESS) { |
---|
3493 | return FAILURE; |
---|
3494 | } |
---|
3495 | } |
---|
3496 | return SUCCESS; |
---|
3497 | } |
---|
3498 | /* }}} */ |
---|
3499 | static int xc_ptr_compare_func(void *p1, void *p2) /* {{{ */ |
---|
3500 | { |
---|
3501 | return p1 == p2; |
---|
3502 | } |
---|
3503 | /* }}} */ |
---|
3504 | static int xc_zend_remove_extension(zend_extension *extension) /* {{{ */ |
---|
3505 | { |
---|
3506 | llist_dtor_func_t dtor; |
---|
3507 | |
---|
3508 | assert(extension); |
---|
3509 | dtor = zend_extensions.dtor; /* avoid dtor */ |
---|
3510 | zend_extensions.dtor = NULL; |
---|
3511 | zend_llist_del_element(&zend_extensions, extension, xc_ptr_compare_func); |
---|
3512 | zend_extensions.dtor = dtor; |
---|
3513 | return SUCCESS; |
---|
3514 | } |
---|
3515 | /* }}} */ |
---|
3516 | static int xc_config_hash(xc_hash_t *p, char *name, char *default_value) /* {{{ */ |
---|
3517 | { |
---|
3518 | int bits, size; |
---|
3519 | char *value; |
---|
3520 | |
---|
3521 | if (cfg_get_string(name, &value) != SUCCESS) { |
---|
3522 | value = default_value; |
---|
3523 | } |
---|
3524 | |
---|
3525 | p->size = zend_atoi(value, strlen(value)); |
---|
3526 | for (size = 1, bits = 1; size < p->size; bits ++, size <<= 1) { |
---|
3527 | /* empty body */ |
---|
3528 | } |
---|
3529 | p->size = size; |
---|
3530 | p->bits = bits; |
---|
3531 | p->mask = size - 1; |
---|
3532 | |
---|
3533 | return SUCCESS; |
---|
3534 | } |
---|
3535 | /* }}} */ |
---|
3536 | static int xc_config_long(zend_ulong *p, char *name, char *default_value) /* {{{ */ |
---|
3537 | { |
---|
3538 | char *value; |
---|
3539 | |
---|
3540 | if (cfg_get_string(name, &value) != SUCCESS) { |
---|
3541 | value = default_value; |
---|
3542 | } |
---|
3543 | |
---|
3544 | *p = zend_atol(value, strlen(value)); |
---|
3545 | return SUCCESS; |
---|
3546 | } |
---|
3547 | /* }}} */ |
---|
3548 | /* {{{ PHP_MINIT_FUNCTION(xcache) */ |
---|
3549 | static PHP_MINIT_FUNCTION(xcache) |
---|
3550 | { |
---|
3551 | char *env; |
---|
3552 | zend_extension *ext; |
---|
3553 | zend_llist_position lpos; |
---|
3554 | |
---|
3555 | xc_module_gotup = 1; |
---|
3556 | if (!xc_zend_extension_gotup) { |
---|
3557 | xc_zend_extension_register(&zend_extension_entry, 0); |
---|
3558 | xc_zend_extension_startup(&zend_extension_entry); |
---|
3559 | xc_zend_extension_faked = 1; |
---|
3560 | } |
---|
3561 | |
---|
3562 | ext = zend_get_extension("Zend Optimizer"); |
---|
3563 | if (ext) { |
---|
3564 | /* zend_optimizer.optimization_level>0 is not compatible with other cacher, disabling */ |
---|
3565 | ext->op_array_handler = NULL; |
---|
3566 | } |
---|
3567 | /* cache if there's an op_array_ctor */ |
---|
3568 | for (ext = zend_llist_get_first_ex(&zend_extensions, &lpos); |
---|
3569 | ext; |
---|
3570 | ext = zend_llist_get_next_ex(&zend_extensions, &lpos)) { |
---|
3571 | if (ext->op_array_ctor) { |
---|
3572 | xc_have_op_array_ctor = 1; |
---|
3573 | break; |
---|
3574 | } |
---|
3575 | } |
---|
3576 | |
---|
3577 | |
---|
3578 | #ifndef PHP_GINIT |
---|
3579 | ZEND_INIT_MODULE_GLOBALS(xcache, xc_init_globals, xc_shutdown_globals); |
---|
3580 | #endif |
---|
3581 | REGISTER_INI_ENTRIES(); |
---|
3582 | |
---|
3583 | xc_config_long(&xc_php_size, "xcache.size", "0"); |
---|
3584 | xc_config_hash(&xc_php_hcache, "xcache.count", "1"); |
---|
3585 | xc_config_hash(&xc_php_hentry, "xcache.slots", "8K"); |
---|
3586 | |
---|
3587 | xc_config_long(&xc_var_size, "xcache.var_size", "0"); |
---|
3588 | xc_config_hash(&xc_var_hcache, "xcache.var_count", "1"); |
---|
3589 | xc_config_hash(&xc_var_hentry, "xcache.var_slots", "8K"); |
---|
3590 | |
---|
3591 | if (strcmp(sapi_module.name, "cli") == 0) { |
---|
3592 | if ((env = getenv("XCACHE_TEST")) != NULL) { |
---|
3593 | xc_test = atoi(env); |
---|
3594 | } |
---|
3595 | if (!xc_test) { |
---|
3596 | /* disable cache for cli except for testing */ |
---|
3597 | xc_php_size = xc_var_size = 0; |
---|
3598 | } |
---|
3599 | } |
---|
3600 | |
---|
3601 | if (xc_php_size <= 0) { |
---|
3602 | xc_php_size = xc_php_hcache.size = 0; |
---|
3603 | } |
---|
3604 | if (xc_var_size <= 0) { |
---|
3605 | xc_var_size = xc_var_hcache.size = 0; |
---|
3606 | } |
---|
3607 | |
---|
3608 | if (xc_coredump_dir && xc_coredump_dir[0]) { |
---|
3609 | xcache_init_signal_handler(); |
---|
3610 | } |
---|
3611 | |
---|
3612 | xc_init_constant(module_number TSRMLS_CC); |
---|
3613 | xc_shm_init_modules(); |
---|
3614 | |
---|
3615 | if ((xc_php_size || xc_var_size) && xc_mmap_path && xc_mmap_path[0]) { |
---|
3616 | if (xc_init(module_number TSRMLS_CC) != SUCCESS) { |
---|
3617 | zend_error(E_ERROR, "XCache: Cannot init"); |
---|
3618 | goto err_init; |
---|
3619 | } |
---|
3620 | xc_initized = 1; |
---|
3621 | xc_init_time = time(NULL); |
---|
3622 | #ifdef PHP_WIN32 |
---|
3623 | xc_init_instance_id = GetCurrentProcessId(); |
---|
3624 | #else |
---|
3625 | xc_init_instance_id = getpid(); |
---|
3626 | #endif |
---|
3627 | #ifdef ZTS |
---|
3628 | xc_init_instance_subid = tsrm_thread_id(); |
---|
3629 | #endif |
---|
3630 | } |
---|
3631 | |
---|
3632 | #ifdef HAVE_XCACHE_COVERAGER |
---|
3633 | xc_coverager_init(module_number TSRMLS_CC); |
---|
3634 | #endif |
---|
3635 | |
---|
3636 | return SUCCESS; |
---|
3637 | |
---|
3638 | err_init: |
---|
3639 | return FAILURE; |
---|
3640 | } |
---|
3641 | /* }}} */ |
---|
3642 | /* {{{ PHP_MSHUTDOWN_FUNCTION(xcache) */ |
---|
3643 | static PHP_MSHUTDOWN_FUNCTION(xcache) |
---|
3644 | { |
---|
3645 | if (xc_initized) { |
---|
3646 | xc_destroy(); |
---|
3647 | } |
---|
3648 | if (xc_mmap_path) { |
---|
3649 | pefree(xc_mmap_path, 1); |
---|
3650 | xc_mmap_path = NULL; |
---|
3651 | } |
---|
3652 | if (xc_shm_scheme) { |
---|
3653 | pefree(xc_shm_scheme, 1); |
---|
3654 | xc_shm_scheme = NULL; |
---|
3655 | } |
---|
3656 | |
---|
3657 | #ifdef HAVE_XCACHE_COVERAGER |
---|
3658 | xc_coverager_destroy(); |
---|
3659 | #endif |
---|
3660 | |
---|
3661 | if (xc_coredump_dir && xc_coredump_dir[0]) { |
---|
3662 | xcache_restore_signal_handler(); |
---|
3663 | } |
---|
3664 | if (xc_coredump_dir) { |
---|
3665 | pefree(xc_coredump_dir, 1); |
---|
3666 | xc_coredump_dir = NULL; |
---|
3667 | } |
---|
3668 | #ifndef PHP_GINIT |
---|
3669 | # ifdef ZTS |
---|
3670 | ts_free_id(xcache_globals_id); |
---|
3671 | # else |
---|
3672 | xc_shutdown_globals(&xcache_globals TSRMLS_CC); |
---|
3673 | # endif |
---|
3674 | #endif |
---|
3675 | |
---|
3676 | if (xc_zend_extension_faked) { |
---|
3677 | zend_extension *ext = zend_get_extension(XCACHE_NAME); |
---|
3678 | if (ext) { |
---|
3679 | if (ext->shutdown) { |
---|
3680 | ext->shutdown(ext); |
---|
3681 | } |
---|
3682 | xc_zend_remove_extension(ext); |
---|
3683 | } |
---|
3684 | } |
---|
3685 | UNREGISTER_INI_ENTRIES(); |
---|
3686 | |
---|
3687 | xc_module_gotup = 0; |
---|
3688 | xc_zend_extension_gotup = 0; |
---|
3689 | xc_zend_extension_faked = 0; |
---|
3690 | |
---|
3691 | return SUCCESS; |
---|
3692 | } |
---|
3693 | /* }}} */ |
---|
3694 | /* {{{ PHP_RINIT_FUNCTION(xcache) */ |
---|
3695 | static PHP_RINIT_FUNCTION(xcache) |
---|
3696 | { |
---|
3697 | xc_request_init(TSRMLS_C); |
---|
3698 | return SUCCESS; |
---|
3699 | } |
---|
3700 | /* }}} */ |
---|
3701 | /* {{{ PHP_RSHUTDOWN_FUNCTION(xcache) */ |
---|
3702 | #ifndef ZEND_ENGINE_2 |
---|
3703 | static PHP_RSHUTDOWN_FUNCTION(xcache) |
---|
3704 | #else |
---|
3705 | static ZEND_MODULE_POST_ZEND_DEACTIVATE_D(xcache) |
---|
3706 | #endif |
---|
3707 | { |
---|
3708 | #ifdef ZEND_ENGINE_2 |
---|
3709 | TSRMLS_FETCH(); |
---|
3710 | #endif |
---|
3711 | |
---|
3712 | xc_request_shutdown(TSRMLS_C); |
---|
3713 | return SUCCESS; |
---|
3714 | } |
---|
3715 | /* }}} */ |
---|
3716 | /* {{{ module dependencies */ |
---|
3717 | #if ZEND_MODULE_API_NO >= 20050922 |
---|
3718 | static zend_module_dep xcache_module_deps[] = { |
---|
3719 | ZEND_MOD_REQUIRED("standard") |
---|
3720 | ZEND_MOD_CONFLICTS("apc") |
---|
3721 | ZEND_MOD_CONFLICTS("eAccelerator") |
---|
3722 | ZEND_MOD_CONFLICTS("Turck MMCache") |
---|
3723 | {NULL, NULL, NULL} |
---|
3724 | }; |
---|
3725 | #endif |
---|
3726 | /* }}} */ |
---|
3727 | /* {{{ module definition structure */ |
---|
3728 | |
---|
3729 | zend_module_entry xcache_module_entry = { |
---|
3730 | #if ZEND_MODULE_API_NO >= 20050922 |
---|
3731 | STANDARD_MODULE_HEADER_EX, |
---|
3732 | NULL, |
---|
3733 | xcache_module_deps, |
---|
3734 | #else |
---|
3735 | STANDARD_MODULE_HEADER, |
---|
3736 | #endif |
---|
3737 | XCACHE_NAME, |
---|
3738 | xcache_functions, |
---|
3739 | PHP_MINIT(xcache), |
---|
3740 | PHP_MSHUTDOWN(xcache), |
---|
3741 | PHP_RINIT(xcache), |
---|
3742 | #ifndef ZEND_ENGINE_2 |
---|
3743 | PHP_RSHUTDOWN(xcache), |
---|
3744 | #else |
---|
3745 | NULL, |
---|
3746 | #endif |
---|
3747 | PHP_MINFO(xcache), |
---|
3748 | XCACHE_VERSION, |
---|
3749 | #ifdef PHP_GINIT |
---|
3750 | PHP_MODULE_GLOBALS(xcache), |
---|
3751 | PHP_GINIT(xcache), |
---|
3752 | PHP_GSHUTDOWN(xcache), |
---|
3753 | #endif |
---|
3754 | #ifdef ZEND_ENGINE_2 |
---|
3755 | ZEND_MODULE_POST_ZEND_DEACTIVATE_N(xcache), |
---|
3756 | #else |
---|
3757 | NULL, |
---|
3758 | NULL, |
---|
3759 | #endif |
---|
3760 | STANDARD_MODULE_PROPERTIES_EX |
---|
3761 | }; |
---|
3762 | |
---|
3763 | #ifdef COMPILE_DL_XCACHE |
---|
3764 | ZEND_GET_MODULE(xcache) |
---|
3765 | #endif |
---|
3766 | /* }}} */ |
---|
3767 | static startup_func_t xc_last_ext_startup; |
---|
3768 | static int xc_zend_startup_last(zend_extension *extension) /* {{{ */ |
---|
3769 | { |
---|
3770 | /* restore */ |
---|
3771 | extension->startup = xc_last_ext_startup; |
---|
3772 | if (extension->startup) { |
---|
3773 | if (extension->startup(extension) != SUCCESS) { |
---|
3774 | return FAILURE; |
---|
3775 | } |
---|
3776 | } |
---|
3777 | assert(xc_llist_zend_extension); |
---|
3778 | xc_llist_prepend(&zend_extensions, xc_llist_zend_extension); |
---|
3779 | if (!xc_module_gotup) { |
---|
3780 | return zend_startup_module(&xcache_module_entry); |
---|
3781 | } |
---|
3782 | return SUCCESS; |
---|
3783 | } |
---|
3784 | /* }}} */ |
---|
3785 | ZEND_DLEXPORT int xcache_zend_startup(zend_extension *extension) /* {{{ */ |
---|
3786 | { |
---|
3787 | xc_zend_extension_gotup = 1; |
---|
3788 | |
---|
3789 | if (!origin_compile_file) { |
---|
3790 | origin_compile_file = zend_compile_file; |
---|
3791 | zend_compile_file = xc_check_initial_compile_file; |
---|
3792 | } |
---|
3793 | |
---|
3794 | if (zend_llist_count(&zend_extensions) > 1) { |
---|
3795 | zend_llist_position lpos; |
---|
3796 | zend_extension *ext; |
---|
3797 | |
---|
3798 | xc_llist_zend_extension = xc_llist_get_element_by_zend_extension(&zend_extensions, XCACHE_NAME); |
---|
3799 | xc_llist_unlink(&zend_extensions, xc_llist_zend_extension); |
---|
3800 | |
---|
3801 | ext = (zend_extension *) zend_llist_get_last_ex(&zend_extensions, &lpos); |
---|
3802 | assert(ext && ext != (zend_extension *) xc_llist_zend_extension->data); |
---|
3803 | xc_last_ext_startup = ext->startup; |
---|
3804 | ext->startup = xc_zend_startup_last; |
---|
3805 | } |
---|
3806 | else if (!xc_module_gotup) { |
---|
3807 | return zend_startup_module(&xcache_module_entry); |
---|
3808 | } |
---|
3809 | return SUCCESS; |
---|
3810 | } |
---|
3811 | /* }}} */ |
---|
3812 | ZEND_DLEXPORT void xcache_zend_shutdown(zend_extension *extension) /* {{{ */ |
---|
3813 | { |
---|
3814 | /* empty */ |
---|
3815 | } |
---|
3816 | /* }}} */ |
---|
3817 | ZEND_DLEXPORT void xcache_statement_handler(zend_op_array *op_array) /* {{{ */ |
---|
3818 | { |
---|
3819 | #ifdef HAVE_XCACHE_COVERAGER |
---|
3820 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_STMT); |
---|
3821 | #endif |
---|
3822 | } |
---|
3823 | /* }}} */ |
---|
3824 | ZEND_DLEXPORT void xcache_fcall_begin_handler(zend_op_array *op_array) /* {{{ */ |
---|
3825 | { |
---|
3826 | #if 0 |
---|
3827 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_FCALL_BEGIN); |
---|
3828 | #endif |
---|
3829 | } |
---|
3830 | /* }}} */ |
---|
3831 | ZEND_DLEXPORT void xcache_fcall_end_handler(zend_op_array *op_array) /* {{{ */ |
---|
3832 | { |
---|
3833 | #if 0 |
---|
3834 | xc_coverager_handle_ext_stmt(op_array, ZEND_EXT_FCALL_END); |
---|
3835 | #endif |
---|
3836 | } |
---|
3837 | /* }}} */ |
---|
3838 | /* {{{ zend extension definition structure */ |
---|
3839 | ZEND_DLEXPORT zend_extension zend_extension_entry = { |
---|
3840 | XCACHE_NAME, |
---|
3841 | XCACHE_VERSION, |
---|
3842 | XCACHE_AUTHOR, |
---|
3843 | XCACHE_URL, |
---|
3844 | XCACHE_COPYRIGHT, |
---|
3845 | xcache_zend_startup, |
---|
3846 | xcache_zend_shutdown, |
---|
3847 | NULL, /* activate_func_t */ |
---|
3848 | NULL, /* deactivate_func_t */ |
---|
3849 | NULL, /* message_handler_func_t */ |
---|
3850 | #ifdef HAVE_XCACHE_OPTIMIZER |
---|
3851 | xc_optimizer_op_array_handler, |
---|
3852 | #else |
---|
3853 | NULL, /* op_array_handler_func_t */ |
---|
3854 | #endif |
---|
3855 | xcache_statement_handler, |
---|
3856 | xcache_fcall_begin_handler, |
---|
3857 | xcache_fcall_end_handler, |
---|
3858 | NULL, /* op_array_ctor_func_t */ |
---|
3859 | NULL, /* op_array_dtor_func_t */ |
---|
3860 | STANDARD_ZEND_EXTENSION_PROPERTIES |
---|
3861 | }; |
---|
3862 | |
---|
3863 | #ifndef ZEND_EXT_API |
---|
3864 | # define ZEND_EXT_API ZEND_DLEXPORT |
---|
3865 | #endif |
---|
3866 | #if COMPILE_DL_XCACHE |
---|
3867 | ZEND_EXTENSION(); |
---|
3868 | #endif |
---|
3869 | /* }}} */ |
---|