XCache is a fast, stable PHP opcode cacher that has been proven and is now running on production servers under high load. https://xcache.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3344 lines
97 KiB

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