-
Notifications
You must be signed in to change notification settings - Fork 0
/
apc_main.c
1066 lines (887 loc) · 33.9 KB
/
apc_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| APC |
+----------------------------------------------------------------------+
| Copyright (c) 2006-2011 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Daniel Cowgill <[email protected]> |
| Rasmus Lerdorf <[email protected]> |
| Arun C. Murthy <[email protected]> |
| Gopal Vijayaraghavan <[email protected]> |
+----------------------------------------------------------------------+
This software was contributed to PHP by Community Connect Inc. in 2002
and revised in 2005 by Yahoo! Inc. to add support for PHP 5.1.
Future revisions and derivatives of this source code must acknowledge
Community Connect Inc. as the original contributor of this module by
leaving this note intact in the source code.
All other licensing and usage conditions are those of the PHP Group.
*/
/* $Id: apc_main.c 327102 2012-08-13 13:04:46Z ab $ */
#include "apc_php.h"
#include "apc_main.h"
#include "apc.h"
#include "apc_lock.h"
#include "apc_cache.h"
#include "apc_compile.h"
#include "apc_globals.h"
#include "apc_sma.h"
#include "apc_stack.h"
#include "apc_zend.h"
#include "apc_pool.h"
#include "apc_string.h"
#include "SAPI.h"
#include "php_scandir.h"
#include "ext/standard/php_var.h"
#include "ext/standard/md5.h"
#define APC_MAX_SERIALIZERS 16
/* {{{ module variables */
/* pointer to the original Zend engine compile_file function */
typedef zend_op_array* (zend_compile_t)(zend_file_handle*, int TSRMLS_DC);
static zend_compile_t *old_compile_file;
static apc_serializer_t apc_serializers[APC_MAX_SERIALIZERS] = {{0,}};
/* }}} */
/* {{{ get/set old_compile_file (to interact with other extensions that need the compile hook) */
static zend_compile_t* set_compile_hook(zend_compile_t *ptr)
{
zend_compile_t *retval = old_compile_file;
if (ptr != NULL) old_compile_file = ptr;
return retval;
}
/* }}} */
/* {{{ install_function */
static int install_function(apc_function_t fn, apc_context_t* ctxt, int lazy TSRMLS_DC)
{
int status;
#if APC_HAVE_LOOKUP_HOOKS
if(lazy && fn.name[0] != '\0' && strncmp(fn.name, "__autoload", fn.name_len) != 0) {
status = zend_hash_add(APCG(lazy_function_table),
fn.name,
fn.name_len+1,
&fn,
sizeof(apc_function_t),
NULL);
#else
if(0) {
#endif
} else {
zend_function *func = apc_copy_function_for_execution(fn.function, ctxt TSRMLS_CC);
status = zend_hash_add(EG(function_table),
fn.name,
fn.name_len+1,
func,
sizeof(zend_function),
NULL);
efree(func);
}
if (status == FAILURE) {
/* apc_error("Cannot redeclare %s()" TSRMLS_CC, fn.name); */
}
return status;
}
/* }}} */
/* {{{ apc_lookup_function_hook */
int apc_lookup_function_hook(char *name, int len, ulong hash, zend_function **fe) {
apc_function_t *fn;
int status = FAILURE;
apc_context_t ctxt = {0,};
TSRMLS_FETCH();
ctxt.pool = apc_pool_create(APC_UNPOOL, apc_php_malloc, apc_php_free, apc_sma_protect, apc_sma_unprotect TSRMLS_CC);
ctxt.copy = APC_COPY_OUT_OPCODE;
if(zend_hash_quick_find(APCG(lazy_function_table), name, len, hash, (void**)&fn) == SUCCESS) {
*fe = apc_copy_function_for_execution(fn->function, &ctxt TSRMLS_CC);
status = zend_hash_add(EG(function_table),
fn->name,
fn->name_len+1,
*fe,
sizeof(zend_function),
NULL);
}
return status;
}
/* }}} */
/* {{{ install_class */
static int install_class(apc_class_t cl, apc_context_t* ctxt, int lazy TSRMLS_DC)
{
zend_class_entry* class_entry = cl.class_entry;
zend_class_entry* parent = NULL;
int status;
/* Special case for mangled names. Mangled names are unique to a file.
* There is no way two classes with the same mangled name will occur,
* unless a file is included twice. And if in case, a file is included
* twice, all mangled name conflicts can be ignored and the class redeclaration
* error may be deferred till runtime of the corresponding DECLARE_CLASS
* calls.
*/
if(cl.name_len != 0 && cl.name[0] == '\0') {
if(zend_hash_exists(CG(class_table), cl.name, cl.name_len+1)) {
return SUCCESS;
}
}
if(lazy && cl.name_len != 0 && cl.name[0] != '\0') {
status = zend_hash_add(APCG(lazy_class_table),
cl.name,
cl.name_len+1,
&cl,
sizeof(apc_class_t),
NULL);
if(status == FAILURE) {
zend_error(E_ERROR, "Cannot redeclare class %s", cl.name);
}
return status;
}
class_entry =
apc_copy_class_entry_for_execution(cl.class_entry, ctxt TSRMLS_CC);
/* restore parent class pointer for compile-time inheritance */
if (cl.parent_name != NULL) {
zend_class_entry** parent_ptr = NULL;
/*
* __autoload brings in the old issues with mixed inheritance.
* When a statically inherited class triggers autoload, it runs
* afoul of a potential require_once "parent.php" in the previous
* line, which when executed provides the parent class, but right
* now goes and hits __autoload which could fail.
*
* missing parent == re-compile.
*
* whether __autoload is enabled or not, because __autoload errors
* cause php to die.
*
* Aside: Do NOT pass *strlen(cl.parent_name)+1* because
* zend_lookup_class_ex does it internally anyway!
*/
status = zend_lookup_class_ex(cl.parent_name,
strlen(cl.parent_name),
#ifdef ZEND_ENGINE_2_4
NULL,
#endif
0,
&parent_ptr TSRMLS_CC);
if (status == FAILURE) {
if(APCG(report_autofilter)) {
apc_warning("Dynamic inheritance detected for class %s" TSRMLS_CC, cl.name);
}
class_entry->parent = NULL;
return status;
}
else {
parent = *parent_ptr;
class_entry->parent = parent;
zend_do_inheritance(class_entry, parent TSRMLS_CC);
}
}
status = zend_hash_add(EG(class_table),
cl.name,
cl.name_len+1,
&class_entry,
sizeof(zend_class_entry*),
NULL);
if (status == FAILURE) {
apc_error("Cannot redeclare class %s" TSRMLS_CC, cl.name);
}
return status;
}
/* }}} */
/* {{{ apc_lookup_class_hook */
int apc_lookup_class_hook(char *name, int len, ulong hash, zend_class_entry ***ce) {
apc_class_t *cl;
apc_context_t ctxt = {0,};
TSRMLS_FETCH();
if(zend_is_compiling(TSRMLS_C)) { return FAILURE; }
if(zend_hash_quick_find(APCG(lazy_class_table), name, len, hash, (void**)&cl) == FAILURE) {
return FAILURE;
}
ctxt.pool = apc_pool_create(APC_UNPOOL, apc_php_malloc, apc_php_free, apc_sma_protect, apc_sma_unprotect TSRMLS_CC);
ctxt.copy = APC_COPY_OUT_OPCODE;
if(install_class(*cl, &ctxt, 0 TSRMLS_CC) == FAILURE) {
apc_warning("apc_lookup_class_hook: could not install %s" TSRMLS_CC, name);
return FAILURE;
}
if(zend_hash_quick_find(EG(class_table), name, len, hash, (void**)ce) == FAILURE) {
apc_warning("apc_lookup_class_hook: known error trying to fetch class %s" TSRMLS_CC, name);
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ uninstall_class */
static int uninstall_class(apc_class_t cl TSRMLS_DC)
{
int status;
status = zend_hash_del(EG(class_table),
cl.name,
cl.name_len+1);
if (status == FAILURE) {
apc_error("Cannot delete class %s" TSRMLS_CC, cl.name);
}
return status;
}
/* }}} */
/* {{{ copy_function_name (taken from zend_builtin_functions.c to ensure future compatibility with APC) */
static int copy_function_name(apc_function_t *pf TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
{
zval *internal_ar = va_arg(args, zval *),
*user_ar = va_arg(args, zval *);
zend_function *func = pf->function;
if (hash_key->nKeyLength == 0 || hash_key->arKey[0] == 0) {
return 0;
}
if (func->type == ZEND_INTERNAL_FUNCTION) {
add_next_index_stringl(internal_ar, hash_key->arKey, hash_key->nKeyLength-1, 1);
} else if (func->type == ZEND_USER_FUNCTION) {
add_next_index_stringl(user_ar, hash_key->arKey, hash_key->nKeyLength-1, 1);
}
return 0;
}
/* {{{ copy_class_or_interface_name (taken from zend_builtin_functions.c to ensure future compatibility with APC) */
static int copy_class_or_interface_name(apc_class_t *cl TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
{
zval *array = va_arg(args, zval *);
zend_uint mask = va_arg(args, zend_uint);
zend_uint comply = va_arg(args, zend_uint);
zend_uint comply_mask = (comply)? mask:0;
zend_class_entry *ce = cl->class_entry;
if ((hash_key->nKeyLength==0 || hash_key->arKey[0]!=0)
&& (comply_mask == (ce->ce_flags & mask))) {
add_next_index_stringl(array, ce->name, ce->name_length, 1);
}
return ZEND_HASH_APPLY_KEEP;
}
/* }}} */
/* }}} */
/* {{{ apc_defined_function_hook */
int apc_defined_function_hook(zval *internal, zval *user) {
TSRMLS_FETCH();
zend_hash_apply_with_arguments(APCG(lazy_function_table)
#ifdef ZEND_ENGINE_2_3
TSRMLS_CC
#endif
,(apply_func_args_t) copy_function_name, 2, internal, user);
return 1;
}
/* }}} */
/* {{{ apc_declared_class_hook */
int apc_declared_class_hook(zval *classes, zend_uint mask, zend_uint comply) {
TSRMLS_FETCH();
zend_hash_apply_with_arguments(APCG(lazy_class_table)
#ifdef ZEND_ENGINE_2_3
TSRMLS_CC
#endif
, (apply_func_args_t) copy_class_or_interface_name, 3, classes, mask, comply);
return 1;
}
/* }}} */
/* {{{ cached_compile */
static zend_op_array* cached_compile(zend_file_handle* h,
int type,
apc_context_t* ctxt TSRMLS_DC)
{
apc_cache_entry_t* cache_entry;
int i, ii;
cache_entry = (apc_cache_entry_t*) apc_stack_top(APCG(cache_stack));
assert(cache_entry != NULL);
if (cache_entry->data.file.classes) {
int lazy_classes = APCG(lazy_classes);
for (i = 0; cache_entry->data.file.classes[i].class_entry != NULL; i++) {
if(install_class(cache_entry->data.file.classes[i], ctxt, lazy_classes TSRMLS_CC) == FAILURE) {
goto default_compile;
}
}
}
if (cache_entry->data.file.functions) {
int lazy_functions = APCG(lazy_functions);
for (i = 0; cache_entry->data.file.functions[i].function != NULL; i++) {
install_function(cache_entry->data.file.functions[i], ctxt, lazy_functions TSRMLS_CC);
}
}
apc_do_halt_compiler_register(cache_entry->data.file.filename, cache_entry->data.file.halt_offset TSRMLS_CC);
return apc_copy_op_array_for_execution(NULL, cache_entry->data.file.op_array, ctxt TSRMLS_CC);
default_compile:
/* XXX more cleanup in uninstall class, and uninstall_function() should be here too */
if(cache_entry->data.file.classes) {
for(ii = 0; ii < i ; ii++) {
uninstall_class(cache_entry->data.file.classes[ii] TSRMLS_CC);
}
}
apc_stack_pop(APCG(cache_stack)); /* pop out cache_entry */
apc_cache_release(apc_cache, cache_entry TSRMLS_CC);
/* cannot free up cache data yet, it maybe in use */
return NULL;
}
/* }}} */
/* {{{ apc_compile_cache_entry */
zend_bool apc_compile_cache_entry(apc_cache_key_t *key, zend_file_handle* h, int type, time_t t, zend_op_array** op_array, apc_cache_entry_t** cache_entry TSRMLS_DC) {
int num_functions, num_classes;
apc_function_t* alloc_functions;
zend_op_array* alloc_op_array;
apc_class_t* alloc_classes;
char *path;
apc_context_t ctxt;
/* remember how many functions and classes existed before compilation */
num_functions = zend_hash_num_elements(CG(function_table));
num_classes = zend_hash_num_elements(CG(class_table));
/* compile the file using the default compile function, *
* we set *op_array here so we return opcodes during *
* a failure. We should not return prior to this line. */
*op_array = old_compile_file(h, type TSRMLS_CC);
if (*op_array == NULL) {
return FAILURE;
}
ctxt.pool = apc_pool_create(APC_MEDIUM_POOL, apc_sma_malloc, apc_sma_free,
apc_sma_protect, apc_sma_unprotect TSRMLS_CC);
if (!ctxt.pool) {
apc_warning("Unable to allocate memory for pool." TSRMLS_CC);
return FAILURE;
}
ctxt.copy = APC_COPY_IN_OPCODE;
if(APCG(file_md5)) {
int n;
unsigned char buf[1024];
PHP_MD5_CTX context;
php_stream *stream;
char *filename;
if(h->opened_path) {
filename = h->opened_path;
} else {
filename = h->filename;
}
stream = php_stream_open_wrapper(filename, "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL);
if(stream) {
PHP_MD5Init(&context);
while((n = php_stream_read(stream, (char*)buf, sizeof(buf))) > 0) {
PHP_MD5Update(&context, buf, n);
}
PHP_MD5Final(key->md5, &context);
php_stream_close(stream);
if(n<0) {
apc_warning("Error while reading '%s' for md5 generation." TSRMLS_CC, filename);
}
} else {
apc_warning("Unable to open '%s' for md5 generation." TSRMLS_CC, filename);
}
}
if(!(alloc_op_array = apc_copy_op_array(NULL, *op_array, &ctxt TSRMLS_CC))) {
goto freepool;
}
if(!(alloc_functions = apc_copy_new_functions(num_functions, &ctxt TSRMLS_CC))) {
goto freepool;
}
if(!(alloc_classes = apc_copy_new_classes(*op_array, num_classes, &ctxt TSRMLS_CC))) {
goto freepool;
}
path = h->opened_path;
if(!path && key->type == APC_CACHE_KEY_FPFILE) path = (char*)key->data.fpfile.fullpath;
if(!path) path=h->filename;
apc_debug("2. h->opened_path=[%s] h->filename=[%s]\n" TSRMLS_CC, h->opened_path?h->opened_path:"null",h->filename);
if(!(*cache_entry = apc_cache_make_file_entry(path, alloc_op_array, alloc_functions, alloc_classes, &ctxt TSRMLS_CC))) {
goto freepool;
}
return SUCCESS;
freepool:
apc_pool_destroy(ctxt.pool TSRMLS_CC);
ctxt.pool = NULL;
return FAILURE;
}
/* }}} */
/* {{{ my_compile_file
Overrides zend_compile_file */
static zend_op_array* my_compile_file(zend_file_handle* h,
int type TSRMLS_DC)
{
apc_cache_key_t key;
apc_cache_entry_t* cache_entry;
zend_op_array* op_array = NULL;
time_t t;
apc_context_t ctxt = {0,};
int bailout=0;
const char* filename = NULL;
if (!APCG(enabled) || apc_cache_busy(apc_cache)) {
return old_compile_file(h, type TSRMLS_CC);
}
if(h->opened_path) {
filename = h->opened_path;
} else {
filename = h->filename;
}
/* check our regular expression filters */
if (APCG(filters) && APCG(compiled_filters) && filename) {
int ret = apc_regex_match_array(APCG(compiled_filters), filename);
if(ret == APC_NEGATIVE_MATCH || (ret != APC_POSITIVE_MATCH && !APCG(cache_by_default))) {
return old_compile_file(h, type TSRMLS_CC);
}
} else if(!APCG(cache_by_default)) {
return old_compile_file(h, type TSRMLS_CC);
}
APCG(current_cache) = apc_cache;
t = apc_time();
apc_debug("1. h->opened_path=[%s] h->filename=[%s]\n" TSRMLS_CC, h->opened_path?h->opened_path:"null",h->filename);
/* try to create a cache key; if we fail, give up on caching */
if (!apc_cache_make_file_key(&key, h->filename, PG(include_path), t TSRMLS_CC)) {
return old_compile_file(h, type TSRMLS_CC);
}
if(!APCG(force_file_update)) {
/* search for the file in the cache */
cache_entry = apc_cache_find(apc_cache, key, t TSRMLS_CC);
ctxt.force_update = 0;
} else {
cache_entry = NULL;
ctxt.force_update = 1;
}
if (cache_entry != NULL) {
int dummy = 1;
ctxt.pool = apc_pool_create(APC_UNPOOL, apc_php_malloc, apc_php_free,
apc_sma_protect, apc_sma_unprotect TSRMLS_CC);
if (!ctxt.pool) {
apc_warning("Unable to allocate memory for pool." TSRMLS_CC);
return old_compile_file(h, type TSRMLS_CC);
}
ctxt.copy = APC_COPY_OUT_OPCODE;
zend_hash_add(&EG(included_files), cache_entry->data.file.filename,
strlen(cache_entry->data.file.filename)+1,
(void *)&dummy, sizeof(int), NULL);
apc_stack_push(APCG(cache_stack), cache_entry TSRMLS_CC);
op_array = cached_compile(h, type, &ctxt TSRMLS_CC);
if(op_array) {
#ifdef APC_FILEHITS
/* If the file comes from the cache, add it to the global request file list */
add_next_index_string(APCG(filehits), h->filename, 1);
#endif
/* this is an unpool, which has no cleanup - this only free's the pool header */
apc_pool_destroy(ctxt.pool TSRMLS_CC);
/* We might leak fds without this hack */
if (h->type != ZEND_HANDLE_FILENAME) {
zend_llist_add_element(&CG(open_files), h);
}
/* save this to free on rshutdown */
cache_entry->data.file.exec_refcount = op_array->refcount;
return op_array;
}
if(APCG(report_autofilter)) {
apc_warning("Autofiltering %s" TSRMLS_CC,
(h->opened_path ? h->opened_path : h->filename));
apc_warning("Recompiling %s" TSRMLS_CC, cache_entry->data.file.filename);
}
/* TODO: check what happens with EG(included_files) */
}
/* Make sure the mtime reflects the files last known mtime, and we respect max_file_size in the case of fpstat==0 */
if(key.type == APC_CACHE_KEY_FPFILE) {
apc_fileinfo_t fileinfo;
struct stat *tmp_buf = NULL;
if(!strcmp(SG(request_info).path_translated, h->filename)) {
tmp_buf = sapi_get_stat(TSRMLS_C); /* Apache has already done this stat() for us */
}
if(tmp_buf) {
fileinfo.st_buf.sb = *tmp_buf;
} else {
if (apc_search_paths(h->filename, PG(include_path), &fileinfo TSRMLS_CC) != 0) {
apc_debug("Stat failed %s - bailing (%s) (%d)\n" TSRMLS_CC,h->filename,SG(request_info).path_translated);
return old_compile_file(h, type TSRMLS_CC);
}
}
if (APCG(max_file_size) < fileinfo.st_buf.sb.st_size) {
apc_debug("File is too big %s (%ld) - bailing\n" TSRMLS_CC, h->filename, fileinfo.st_buf.sb.st_size);
return old_compile_file(h, type TSRMLS_CC);
}
key.mtime = fileinfo.st_buf.sb.st_mtime;
}
HANDLE_BLOCK_INTERRUPTIONS();
#if NONBLOCKING_LOCK_AVAILABLE
if(APCG(write_lock)) {
if(!apc_cache_write_lock(apc_cache TSRMLS_CC)) {
HANDLE_UNBLOCK_INTERRUPTIONS();
return old_compile_file(h, type TSRMLS_CC);
}
}
#endif
zend_try {
if (apc_compile_cache_entry(&key, h, type, t, &op_array, &cache_entry TSRMLS_CC) == SUCCESS) {
ctxt.pool = cache_entry->pool;
ctxt.copy = APC_COPY_IN_OPCODE;
if (apc_cache_insert(apc_cache, key, cache_entry, &ctxt, t TSRMLS_CC) != 1) {
apc_pool_destroy(ctxt.pool TSRMLS_CC);
ctxt.pool = NULL;
}
}
} zend_catch {
bailout=1; /* in the event of a bailout, ensure we don't create a dead-lock */
} zend_end_try();
APCG(current_cache) = NULL;
#if NONBLOCKING_LOCK_AVAILABLE
if(APCG(write_lock)) {
apc_cache_write_unlock(apc_cache TSRMLS_CC);
}
#endif
HANDLE_UNBLOCK_INTERRUPTIONS();
if (bailout) zend_bailout();
return op_array;
}
/* }}} */
/* {{{ data preload */
extern int _apc_store(char *strkey, int strkey_len, const zval *val, const unsigned int ttl, const int exclusive TSRMLS_DC);
static zval* data_unserialize(const char *filename TSRMLS_DC)
{
zval* retval;
long len = 0;
struct stat sb;
char *contents, *tmp;
FILE *fp;
php_unserialize_data_t var_hash;
if(VCWD_STAT(filename, &sb) == -1) {
return NULL;
}
fp = fopen(filename, "rb");
len = sizeof(char)*sb.st_size;
tmp = contents = malloc(len);
if(!contents) {
return NULL;
}
if(fread(contents, 1, len, fp) < 1) {
free(contents);
return NULL;
}
MAKE_STD_ZVAL(retval);
PHP_VAR_UNSERIALIZE_INIT(var_hash);
/* I wish I could use json */
if(!php_var_unserialize(&retval, (const unsigned char**)&tmp, (const unsigned char*)(contents+len), &var_hash TSRMLS_CC)) {
zval_ptr_dtor(&retval);
return NULL;
}
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
free(contents);
fclose(fp);
return retval;
}
static int apc_load_data(const char *data_file TSRMLS_DC)
{
char *p;
char key[MAXPATHLEN] = {0,};
unsigned int key_len;
zval *data;
p = strrchr(data_file, DEFAULT_SLASH);
if(p && p[1]) {
strlcpy(key, p+1, sizeof(key));
p = strrchr(key, '.');
if(p) {
p[0] = '\0';
key_len = strlen(key)+1;
data = data_unserialize(data_file TSRMLS_CC);
if(data) {
_apc_store(key, key_len, data, 0, 1 TSRMLS_CC);
}
return 1;
}
}
return 0;
}
static int apc_walk_dir(const char *path TSRMLS_DC)
{
char file[MAXPATHLEN]={0,};
int ndir, i;
char *p = NULL;
struct dirent **namelist = NULL;
if ((ndir = php_scandir(path, &namelist, 0, php_alphasort)) > 0)
{
for (i = 0; i < ndir; i++)
{
/* check for extension */
if (!(p = strrchr(namelist[i]->d_name, '.'))
|| (p && strcmp(p, ".data")))
{
free(namelist[i]);
continue;
}
snprintf(file, MAXPATHLEN, "%s%c%s",
path, DEFAULT_SLASH, namelist[i]->d_name);
if(!apc_load_data(file TSRMLS_CC))
{
/* print error */
}
free(namelist[i]);
}
free(namelist);
}
return 1;
}
void apc_data_preload(TSRMLS_D)
{
if(!APCG(preload_path)) return;
apc_walk_dir(APCG(preload_path) TSRMLS_CC);
}
/* }}} */
/* {{{ apc_serializer hooks */
static int _apc_register_serializer(const char* name, apc_serialize_t serialize,
apc_unserialize_t unserialize,
void *config TSRMLS_DC)
{
int i;
apc_serializer_t *serializer;
for(i = 0; i < APC_MAX_SERIALIZERS; i++) {
serializer = &apc_serializers[i];
if(!serializer->name) {
/* empty entry */
serializer->name = name; /* assumed to be const */
serializer->serialize = serialize;
serializer->unserialize = unserialize;
serializer->config = config;
apc_serializers[i+1].name = NULL;
return 1;
}
}
return 0;
}
apc_serializer_t* apc_find_serializer(const char* name TSRMLS_DC)
{
int i;
apc_serializer_t *serializer;
for(i = 0; i < APC_MAX_SERIALIZERS; i++) {
serializer = &apc_serializers[i];
if(serializer->name && (strcmp(serializer->name, name) == 0)) {
return serializer;
}
}
return NULL;
}
apc_serializer_t* apc_get_serializers(TSRMLS_D)
{
return &(apc_serializers[0]);
}
/* }}} */
/* {{{ module init and shutdown */
int apc_module_init(int module_number TSRMLS_DC)
{
/* apc initialization */
#if APC_MMAP
apc_sma_init(APCG(shm_segments), APCG(shm_size), APCG(mmap_file_mask) TSRMLS_CC);
#else
apc_sma_init(APCG(shm_segments), APCG(shm_size), NULL TSRMLS_CC);
#endif
apc_cache = apc_cache_create(APCG(num_files_hint), APCG(gc_ttl), APCG(ttl) TSRMLS_CC);
apc_user_cache = apc_cache_create(APCG(user_entries_hint), APCG(gc_ttl), APCG(user_ttl) TSRMLS_CC);
/* override compilation */
old_compile_file = zend_compile_file;
zend_compile_file = my_compile_file;
REGISTER_LONG_CONSTANT("\000apc_magic", (long)&set_compile_hook, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT("\000apc_compile_file", (long)&my_compile_file, CONST_PERSISTENT | CONST_CS);
REGISTER_LONG_CONSTANT(APC_SERIALIZER_CONSTANT, (long)&_apc_register_serializer, CONST_PERSISTENT | CONST_CS);
/* test out the constant function pointer */
apc_register_serializer("php", APC_SERIALIZER_NAME(php), APC_UNSERIALIZER_NAME(php), NULL TSRMLS_CC);
assert(apc_serializers[0].name != NULL);
apc_pool_init();
#ifdef ZEND_ENGINE_2_4
#ifndef ZTS
apc_interned_strings_init(TSRMLS_C);
#endif
#endif
apc_data_preload(TSRMLS_C);
#if APC_HAVE_LOOKUP_HOOKS
if(APCG(lazy_functions)) {
zend_set_lookup_function_hook(apc_lookup_function_hook TSRMLS_CC);
zend_set_defined_function_hook(apc_defined_function_hook TSRMLS_CC);
}
if(APCG(lazy_classes)) {
zend_set_lookup_class_hook(apc_lookup_class_hook TSRMLS_CC);
zend_set_declared_class_hook(apc_declared_class_hook TSRMLS_CC);
}
#else
if(APCG(lazy_functions) || APCG(lazy_classes)) {
apc_warning("Lazy function/class loading not available with this version of PHP, please disable APC lazy loading." TSRMLS_CC);
APCG(lazy_functions) = APCG(lazy_classes) = 0;
}
#endif
APCG(initialized) = 1;
return 0;
}
int apc_module_shutdown(TSRMLS_D)
{
if (!APCG(initialized))
return 0;
/* restore compilation */
zend_compile_file = old_compile_file;
/*
* In case we got interrupted by a SIGTERM or something else during execution
* we may have cache entries left on the stack that we need to check to make
* sure that any functions or classes these may have added to the global function
* and class tables are removed before we blow away the memory that hold them.
*
* This is merely to remove memory leak warnings - as the process is terminated
* immediately after shutdown. The following while loop can be removed without
* affecting anything else.
*/
while (apc_stack_size(APCG(cache_stack)) > 0) {
int i;
apc_cache_entry_t* cache_entry = (apc_cache_entry_t*) apc_stack_pop(APCG(cache_stack));
if (cache_entry->data.file.functions) {
for (i = 0; cache_entry->data.file.functions[i].function != NULL; i++) {
zend_hash_del(EG(function_table),
cache_entry->data.file.functions[i].name,
cache_entry->data.file.functions[i].name_len+1);
}
}
if (cache_entry->data.file.classes) {
for (i = 0; cache_entry->data.file.classes[i].class_entry != NULL; i++) {
zend_hash_del(EG(class_table),
cache_entry->data.file.classes[i].name,
cache_entry->data.file.classes[i].name_len+1);
}
}
apc_cache_release(apc_cache, cache_entry TSRMLS_CC);
}
#ifdef ZEND_ENGINE_2_4
#ifndef ZTS
apc_interned_strings_shutdown(TSRMLS_C);
#endif
#endif
apc_cache_destroy(apc_cache TSRMLS_CC);
apc_cache_destroy(apc_user_cache TSRMLS_CC);
apc_sma_cleanup(TSRMLS_C);
APCG(initialized) = 0;
return 0;
}
/* }}} */
/* {{{ process init and shutdown */
int apc_process_init(int module_number TSRMLS_DC)
{
return 0;
}
int apc_process_shutdown(TSRMLS_D)
{
return 0;
}
/* }}} */
/* {{{ apc_deactivate */
static void apc_deactivate(TSRMLS_D)
{
/* The execution stack was unwound, which prevented us from decrementing
* the reference counts on active cache entries in `my_execute`.
*/
while (apc_stack_size(APCG(cache_stack)) > 0) {
int i;
apc_cache_entry_t* cache_entry =
(apc_cache_entry_t*) apc_stack_pop(APCG(cache_stack));
if (cache_entry->data.file.classes) {
zend_class_entry* zce = NULL;
void ** centry = (void*)(&zce);
zend_class_entry** pzce = NULL;
for (i = 0; cache_entry->data.file.classes[i].class_entry != NULL; i++) {
centry = (void**)&pzce; /* a triple indirection to get zend_class_entry*** */
if(zend_hash_find(EG(class_table),
cache_entry->data.file.classes[i].name,
cache_entry->data.file.classes[i].name_len+1,
(void**)centry) == FAILURE)
{
/* double inclusion of conditional classes ends up failing
* this lookup the second time around.
*/
continue;
}
zce = *pzce;
zend_hash_del(EG(class_table),
cache_entry->data.file.classes[i].name,
cache_entry->data.file.classes[i].name_len+1);
apc_free_class_entry_after_execution(zce TSRMLS_CC);
zce = NULL;
}
}
#if 0
if (cache_entry->data.file.functions) {
zend_function fn, *pfn = NULL;
for (i = 0; cache_entry->data.file.functions[i].function != NULL; i++) {
if (zend_hash_find(EG(function_table),
cache_entry->data.file.functions[i].name,
cache_entry->data.file.functions[i].name_len+1,
(void**)&pfn) == FAILURE) {
continue;
}
fn = *pfn;
zend_hash_del(EG(function_table),
cache_entry->data.file.functions[i].name,
cache_entry->data.file.functions[i].name_len+1);
#if 0
apc_free_function_after_execution(&fn TSRMLS_CC);
efree(fn);
#endif
pfn = NULL;
}
}
/* This is a very special case of apc_free_op_array_after_execution.
File related op_array->refcount is allocated on unpool for execution
and would never be freed in zend_execute_scripts() */
#ifdef ZEND_ENGINE_2_4
if (cache_entry->data.file.exec_refcount) {
apc_php_free(cache_entry->data.file.exec_refcount TSRMLS_CC);
cache_entry->data.file.exec_refcount = NULL;
}
#endif
#endif
apc_cache_release(apc_cache, cache_entry TSRMLS_CC);
}
}
/* }}} */
/* {{{ request init and shutdown */
int apc_request_init(TSRMLS_D)
{
apc_stack_clear(APCG(cache_stack));
if (!APCG(compiled_filters) && APCG(filters)) {
/* compile regex filters here to avoid race condition between MINIT of PCRE and APC.
* This should be moved to apc_cache_create() if this race condition between modules is resolved */