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