-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphp_wincache.c
More file actions
3926 lines (3190 loc) · 102 KB
/
Copy pathphp_wincache.c
File metadata and controls
3926 lines (3190 loc) · 102 KB
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
/*
+----------------------------------------------------------------------------------------------+
| Windows Cache for PHP |
+----------------------------------------------------------------------------------------------+
| Copyright (c) 2009, Microsoft Corporation. All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without modification, are |
| permitted provided that the following conditions are met: |
| - Redistributions of source code must retain the above copyright notice, this list of |
| conditions and the following disclaimer. |
| - Redistributions in binary form must reproduce the above copyright notice, this list of |
| conditions and the following disclaimer in the documentation and/or other materials provided |
| with the distribution. |
| - Neither the name of the Microsoft Corporation nor the names of its contributors may be |
| used to endorse or promote products derived from this software without specific prior written|
| permission. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE|
| GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| OF THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------------------------+
| Module: php_wincache.c |
+----------------------------------------------------------------------------------------------+
| Author: Kanwaljeet Singla <ksingla@microsoft.com> |
| Updated: Eric Stenson <ericsten@microsoft.com> |
+----------------------------------------------------------------------------------------------+
*/
#include "precomp.h"
#define NAMESALT_LENGTH_MAXIMUM 8
#define LOCK_KEY_MAXLEN 150
#define RMDIR_WAIT_TIME 1000
#define MAX_ARRAY_INDEX_DIGITS 21
/* START OF PHP EXTENSION MACROS STUFF */
PHP_MINIT_FUNCTION(wincache);
PHP_MSHUTDOWN_FUNCTION(wincache);
PHP_RINIT_FUNCTION(wincache);
PHP_RSHUTDOWN_FUNCTION(wincache);
PHP_MINFO_FUNCTION(wincache);
/* Info functions exposed by this extension */
PHP_FUNCTION(wincache_rplist_fileinfo);
PHP_FUNCTION(wincache_rplist_meminfo);
PHP_FUNCTION(wincache_fcache_fileinfo);
PHP_FUNCTION(wincache_fcache_meminfo);
PHP_FUNCTION(wincache_ucache_meminfo);
PHP_FUNCTION(wincache_scache_meminfo);
/* Utility functions exposed by this extension */
PHP_FUNCTION(wincache_refresh_if_changed);
/* ZVAL cache functions matching other caches */
PHP_FUNCTION(wincache_ucache_get);
PHP_FUNCTION(wincache_ucache_set);
PHP_FUNCTION(wincache_ucache_add);
PHP_FUNCTION(wincache_ucache_delete);
PHP_FUNCTION(wincache_ucache_clear);
PHP_FUNCTION(wincache_ucache_exists);
PHP_FUNCTION(wincache_ucache_info);
PHP_FUNCTION(wincache_scache_info);
PHP_FUNCTION(wincache_ucache_inc);
PHP_FUNCTION(wincache_ucache_dec);
PHP_FUNCTION(wincache_ucache_cas);
PHP_FUNCTION(wincache_lock);
PHP_FUNCTION(wincache_unlock);
/* Wrapper functions for standard PHP functions */
static ZEND_NAMED_FUNCTION(wincache_file_exists);
static ZEND_NAMED_FUNCTION(wincache_file_get_contents);
static ZEND_NAMED_FUNCTION(wincache_filesize);
static ZEND_NAMED_FUNCTION(wincache_is_dir);
static ZEND_NAMED_FUNCTION(wincache_is_file);
static ZEND_NAMED_FUNCTION(wincache_is_readable);
static ZEND_NAMED_FUNCTION(wincache_is_writable);
static ZEND_NAMED_FUNCTION(wincache_readfile);
static ZEND_NAMED_FUNCTION(wincache_realpath);
static ZEND_NAMED_FUNCTION(wincache_unlink);
static ZEND_NAMED_FUNCTION(wincache_rename);
#ifdef WINCACHE_TEST
PHP_FUNCTION(wincache_ucache_lasterror);
PHP_FUNCTION(wincache_runtests);
PHP_FUNCTION(wincache_fcache_find);
PHP_FUNCTION(wincache_fcnotify_fileinfo);
PHP_FUNCTION(wincache_fcnotify_meminfo);
#endif
ZEND_DECLARE_MODULE_GLOBALS(wincache)
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_rplist_fileinfo, 0, 0, 0)
ZEND_ARG_INFO(0, summaryonly)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_rplist_meminfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_fcache_fileinfo, 0, 0, 0)
ZEND_ARG_INFO(0, summaryonly)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_fcache_meminfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_meminfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_scache_meminfo, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_refresh_if_changed, 0, 0, 0)
ZEND_ARG_INFO(0, file_list)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_get, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(1, success)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_set, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, ttl)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_add, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, ttl)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_delete, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_clear, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_exists, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_info, 0, 0, 0)
ZEND_ARG_INFO(0, summaryonly)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_scache_info, 0, 0, 0)
ZEND_ARG_INFO(0, summaryonly)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_inc, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, delta)
ZEND_ARG_INFO(1, success)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_dec, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, delta)
ZEND_ARG_INFO(1, success)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_cas, 0, 0, 3)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, cvalue)
ZEND_ARG_INFO(0, nvalue)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_lock, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, isglobal)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_unlock, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
#ifdef WINCACHE_TEST
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_ucache_lasterror, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_runtests, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_fcache_find, 0, 0, 1)
ZEND_ARG_INFO(0, filename)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_fcnotify_fileinfo, 0, 0, 0)
ZEND_ARG_INFO(0, summaryonly)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_wincache_fcnotify_meminfo, 0, 0, 0)
ZEND_END_ARG_INFO()
#endif
#define WINCACHE_FUNC(name) static PHP_NAMED_FUNCTION(name)
/* Put all user defined functions here */
zend_function_entry wincache_functions[] = {
PHP_FE(wincache_rplist_fileinfo, arginfo_wincache_rplist_fileinfo)
PHP_FE(wincache_rplist_meminfo, arginfo_wincache_rplist_meminfo)
PHP_FE(wincache_fcache_fileinfo, arginfo_wincache_fcache_fileinfo)
PHP_FE(wincache_fcache_meminfo, arginfo_wincache_fcache_meminfo)
PHP_FE(wincache_ucache_meminfo, arginfo_wincache_ucache_meminfo)
PHP_FE(wincache_scache_meminfo, arginfo_wincache_scache_meminfo)
PHP_FE(wincache_refresh_if_changed, arginfo_wincache_refresh_if_changed)
PHP_FE(wincache_ucache_get, arginfo_wincache_ucache_get)
PHP_FE(wincache_ucache_set, arginfo_wincache_ucache_set)
PHP_FE(wincache_ucache_add, arginfo_wincache_ucache_add)
PHP_FE(wincache_ucache_delete, arginfo_wincache_ucache_delete)
PHP_FE(wincache_ucache_clear, arginfo_wincache_ucache_clear)
PHP_FE(wincache_ucache_exists, arginfo_wincache_ucache_exists)
PHP_FE(wincache_ucache_info, arginfo_wincache_ucache_info)
PHP_FE(wincache_scache_info, arginfo_wincache_scache_info)
PHP_FE(wincache_ucache_inc, arginfo_wincache_ucache_inc)
PHP_FE(wincache_ucache_dec, arginfo_wincache_ucache_dec)
PHP_FE(wincache_ucache_cas, arginfo_wincache_ucache_cas)
PHP_FE(wincache_lock, arginfo_wincache_lock)
PHP_FE(wincache_unlock, arginfo_wincache_unlock)
#ifdef WINCACHE_TEST
PHP_FE(wincache_ucache_lasterror, arginfo_wincache_ucache_lasterror)
PHP_FE(wincache_runtests, arginfo_wincache_runtests)
PHP_FE(wincache_fcache_find, arginfo_wincache_fcache_find)
PHP_FE(wincache_fcnotify_fileinfo, arginfo_wincache_fcnotify_fileinfo)
PHP_FE(wincache_fcnotify_meminfo, arginfo_wincache_fcnotify_meminfo)
#endif
{NULL, NULL, NULL}
};
/* wincache_module_entry */
zend_module_entry wincache_module_entry = {
STANDARD_MODULE_HEADER,
PHP_WINCACHE_EXTNAME,
wincache_functions, /* Functions */
PHP_MINIT(wincache), /* MINIT */
PHP_MSHUTDOWN(wincache), /* MSHUTDOWN */
PHP_RINIT(wincache), /* RINIT */
PHP_RSHUTDOWN(wincache), /* RSHUTDOWN */
PHP_MINFO(wincache), /* MINFO */
PHP_WINCACHE_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_WINCACHE
ZEND_GET_MODULE(wincache)
#endif
PHP_INI_BEGIN()
/* index 0 */ STD_PHP_INI_BOOLEAN("wincache.fcenabled", "1", PHP_INI_ALL, OnUpdateBool, fcenabled, zend_wincache_globals, wincache_globals)
/* index 1 */ STD_PHP_INI_BOOLEAN("wincache.enablecli", "0", PHP_INI_SYSTEM, OnUpdateBool, enablecli, zend_wincache_globals, wincache_globals)
/* index 2 */ STD_PHP_INI_ENTRY("wincache.fcachesize", "24", PHP_INI_SYSTEM, OnUpdateLong, fcachesize, zend_wincache_globals, wincache_globals)
/* index 3 */ STD_PHP_INI_ENTRY("wincache.maxfilesize", "256", PHP_INI_SYSTEM, OnUpdateLong, maxfilesize, zend_wincache_globals, wincache_globals)
/* index 4 */ STD_PHP_INI_ENTRY("wincache.filecount", "4096", PHP_INI_SYSTEM, OnUpdateLong, numfiles, zend_wincache_globals, wincache_globals)
/* index 5 */ STD_PHP_INI_ENTRY("wincache.chkinterval", "30", PHP_INI_SYSTEM, OnUpdateLong, fcchkfreq, zend_wincache_globals, wincache_globals)
/* index 6 */ STD_PHP_INI_ENTRY("wincache.ttlmax", "1200", PHP_INI_SYSTEM, OnUpdateLong, ttlmax, zend_wincache_globals, wincache_globals)
/* index 7 */ STD_PHP_INI_ENTRY("wincache.debuglevel", "0", PHP_INI_ALL, wincache_modify_debuglevel, debuglevel, zend_wincache_globals, wincache_globals)
/* index 8 */ STD_PHP_INI_ENTRY("wincache.ignorelist", NULL, PHP_INI_ALL, OnUpdateString, ignorelist, zend_wincache_globals, wincache_globals)
/* index 9 */ STD_PHP_INI_ENTRY("wincache.fcenabledfilter", NULL, PHP_INI_SYSTEM, OnUpdateString, fcefilter, zend_wincache_globals, wincache_globals)
/* index 10 */ STD_PHP_INI_ENTRY("wincache.namesalt", NULL, PHP_INI_SYSTEM, OnUpdateString, namesalt, zend_wincache_globals, wincache_globals)
/* index 11 */ STD_PHP_INI_ENTRY("wincache.localheap", "0", PHP_INI_SYSTEM, OnUpdateBool, localheap, zend_wincache_globals, wincache_globals)
/* index 12 */ STD_PHP_INI_BOOLEAN("wincache.ucenabled", "1", PHP_INI_ALL, OnUpdateBool, ucenabled, zend_wincache_globals, wincache_globals)
/* index 13 */ STD_PHP_INI_ENTRY("wincache.ucachesize", "8", PHP_INI_SYSTEM, OnUpdateLong, ucachesize, zend_wincache_globals, wincache_globals)
/* index 14 */ STD_PHP_INI_ENTRY("wincache.scachesize", "8", PHP_INI_SYSTEM, OnUpdateLong, scachesize, zend_wincache_globals, wincache_globals)
/* index 15 */ STD_PHP_INI_BOOLEAN("wincache.fcndetect", "1", PHP_INI_SYSTEM, OnUpdateBool, fcndetect, zend_wincache_globals, wincache_globals)
/* index 16 */ STD_PHP_INI_ENTRY("wincache.apppoolid", NULL, PHP_INI_SYSTEM, OnUpdateString, apppoolid, zend_wincache_globals, wincache_globals)
/* index 17 */ STD_PHP_INI_BOOLEAN("wincache.reroute_enabled", "0", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdateBool, reroute_enabled, zend_wincache_globals, wincache_globals)
/* index 18 */ STD_PHP_INI_ENTRY("wincache.filemapdir", NULL, PHP_INI_SYSTEM, OnUpdateString, filemapdir, zend_wincache_globals, wincache_globals)
PHP_INI_END()
/* END OF PHP EXTENSION MACROS STUFF */
static void globals_initialize(zend_wincache_globals * globals);
static void globals_terminate(zend_wincache_globals * globals);
static unsigned char isin_ignorelist(const char * ignorelist, const char * filename);
static unsigned char isin_cseplist(const char * cseplist, const char * szinput);
/* Initialize globals */
static void globals_initialize(zend_wincache_globals * globals)
{
dprintverbose("start globals_initialize");
#if defined(COMPILE_DL_WINCACHE) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
/* Function pointers we will override */
original_resolve_path = zend_resolve_path;
original_stream_open_function = zend_stream_open_function;
/* Initalize the wincache_globals items, before parsing the INI file */
WCG(fcenabled) = 1; /* File cache enabled by default */
WCG(ucenabled) = 1; /* User cache enabled by default */
WCG(enablecli) = 0; /* WinCache not enabled by default for CLI */
WCG(fcachesize) = 24; /* File cache size is 24 MB by default */
WCG(ucachesize) = 8; /* User cache size is 8 MB by default */
WCG(scachesize) = 8; /* Session cache size is 8 MB by default */
WCG(maxfilesize) = 256; /* Maximum file size to cache is 256 KB */
WCG(numfiles) = 4096; /* 4096 hashtable keys */
WCG(fcchkfreq) = 30; /* File change check done every 30 secs */
WCG(ttlmax) = 1200; /* File removed if not used for 1200 secs */
WCG(debuglevel) = 0; /* Debug dump not enabled by default */
WCG(ignorelist) = NULL; /* List of files to ignore for caching */
WCG(fcefilter) = NULL; /* List of sites for which fcenabled is toggled */
WCG(namesalt) = NULL; /* Salt to use in names used by wincache */
WCG(fcndetect) = 1; /* File change notification enabled by default */
WCG(localheap) = 0; /* Local heap is disabled by default */
WCG(apppoolid) = NULL; /* Use this application id */
WCG(reroute_enabled) = 0;/* Enable wrappers around standard PHP functions */
WCG(filemapdir) = NULL; /* Directory where temp filemap files should be created */
WCG(lasterror) = 0; /* GetLastError() value set by wincache */
WCG(uclasterror) = 0; /* Last error returned by user cache */
WCG(lfcache) = NULL; /* Aplist to use for file/rpath cache */
WCG(zvucache) = NULL; /* zvcache_context for user zvals */
WCG(zvscache) = NULL; /* zvcache_context for session data */
WCG(phscache) = NULL; /* Hashtable containing zvcache_contexts */
WCG(wclocks) = NULL; /* HashTable for locks created by wincache_lock */
WCG(zvcopied) = NULL; /* HashTable which helps with refcounting */
WCG(parentpid) = 0; /* Parent process identifier to use */
WCG(fmapgdata) = NULL; /* Global filemap information data */
WCG(errmsglist) = NULL; /* Error messages list generated by PHP */
WCG(inifce) = NULL; /* Ini entry null until register_ini called */
WCG(inisavepath) = NULL; /* Fill when ps_open is called */
WCG(dofctoggle) = 0; /* If set to 1, toggle value of fcenabled */
dprintverbose("end globals_initialize");
return;
}
/* Terminate globals */
static void globals_terminate(zend_wincache_globals * globals)
{
dprintverbose("start globals_terminate");
dprintverbose("end globals_terminate");
return;
}
static unsigned char isin_ignorelist(const char * ignorelist, const char * filename)
{
unsigned char retvalue = 0;
char * searchstr = NULL;
char * fslash = NULL;
char filestr[ MAX_PATH];
char tempchar = 0;
size_t length = 0;
dprintverbose("start isin_ignorelist");
_ASSERT(filename != NULL);
if(ignorelist == NULL)
{
goto Finished;
}
/* Get the file name portion from filename */
searchstr = strrchr(filename, '\\');
if(searchstr != NULL)
{
fslash = strrchr(filename, '/');
if(fslash > searchstr)
{
filename = fslash + 1;
}
else
{
filename = searchstr + 1;
}
}
else
{
searchstr = strrchr(filename, '/');
if(searchstr != NULL)
{
filename = searchstr + 1;
}
}
length = strlen(filename);
if(length == 0 || length > MAX_PATH - 3)
{
goto Finished;
}
/* filestr is "|filename|" */
ZeroMemory(filestr, MAX_PATH);
filestr[0] = '|';
strncpy(filestr + 1, filename, length);
_strlwr(filestr);
/* Check if filename exactly matches ignorelist */
/* Both are lowercase and case sensitive lookup is fine */
if(strcmp(ignorelist, filestr + 1) == 0)
{
retvalue = 1;
goto Finished;
}
/* Check if filename is in end or middle of ignorelist */
searchstr = strstr(ignorelist, filestr);
if(searchstr != NULL)
{
tempchar = *(searchstr + length + 1);
if(tempchar == '|' || tempchar == 0)
{
retvalue = 1;
goto Finished;
}
}
/* Check if filename is in the start of ignorelist */
filestr[length + 1] = '|';
searchstr = strstr(ignorelist, filestr + 1);
if(searchstr != NULL && searchstr == ignorelist)
{
retvalue = 1;
goto Finished;
}
Finished:
dprintverbose("end isin_ignorelist");
return retvalue;
}
static unsigned char isin_cseplist(const char * cseplist, const char * szinput)
{
unsigned char retvalue = 0;
char * searchstr = NULL;
char inputstr[ MAX_PATH];
char tempchar = 0;
size_t length = 0;
dprintverbose("start isin_cseplist");
_ASSERT(szinput != NULL);
/* If list is NULL or input length is 0, return false */
if(cseplist == NULL)
{
goto Finished;
}
length = strlen(szinput);
if(length == 0)
{
goto Finished;
}
/* inputstr is ",szinput," */
ZeroMemory(inputstr, MAX_PATH);
inputstr[0] = ',';
strncpy(inputstr + 1, szinput, length);
_strlwr(inputstr);
/* Check if szinput exactly matches cseplist */
/* Both are lowercase and case sensitive lookup is fine */
if(strcmp(cseplist, inputstr + 1) == 0)
{
retvalue = 1;
goto Finished;
}
/* Check if szinput is in end or middle of cseplist */
searchstr = strstr(cseplist, inputstr);
if(searchstr != NULL)
{
tempchar = *(searchstr + length + 1);
if(tempchar == ',' || tempchar == 0)
{
retvalue = 1;
goto Finished;
}
}
/* Check if szinput is in the start of cseplist */
inputstr[length + 1] = ',';
searchstr = strstr(cseplist, inputstr + 1);
if(searchstr != NULL && searchstr == cseplist)
{
retvalue = 1;
goto Finished;
}
Finished:
dprintverbose("end isin_cseplist");
return retvalue;
}
PHP_MINIT_FUNCTION(wincache)
{
int result = NONFATAL;
aplist_context * plcache = NULL;
zvcache_context * pzcache = NULL;
int resnumber = -1;
zend_extension extension = {0};
zend_ini_entry * pinientry = NULL;
int rethash = 0;
ZEND_INIT_MODULE_GLOBALS(wincache, globals_initialize, globals_terminate);
REGISTER_INI_ENTRIES();
dprintverbose("start php_minit");
EventRegisterPHP_Wincache();
pinientry = zend_hash_str_find_ptr(EG(ini_directives), "wincache.fcenabled", sizeof("wincache.fcenabled")-1);
_ASSERT(pinientry != NULL);
WCG(inifce) = pinientry;
/* If enablecli is set to 0, don't initialize when run with cli sapi */
if(!WCG(enablecli) && !strcmp(sapi_module.name, "cli"))
{
goto Finished;
}
/* set up the app pool ID */
WCG(apppoolid) = utils_get_apppool_name();
/* Compare value of globals with minimum and maximum allowed */
WCG(numfiles) = (WCG(numfiles) < NUM_FILES_MINIMUM) ? NUM_FILES_MINIMUM : WCG(numfiles);
WCG(numfiles) = (WCG(numfiles) > NUM_FILES_MAXIMUM) ? NUM_FILES_MAXIMUM : WCG(numfiles);
WCG(fcachesize) = (WCG(fcachesize) < FCACHE_SIZE_MINIMUM) ? FCACHE_SIZE_MINIMUM : WCG(fcachesize);
WCG(fcachesize) = (WCG(fcachesize) > FCACHE_SIZE_MAXIMUM) ? FCACHE_SIZE_MAXIMUM : WCG(fcachesize);
WCG(ucachesize) = (WCG(ucachesize) < ZCACHE_SIZE_MINIMUM) ? ZCACHE_SIZE_MINIMUM : WCG(ucachesize);
WCG(ucachesize) = (WCG(ucachesize) > ZCACHE_SIZE_MAXIMUM) ? ZCACHE_SIZE_MAXIMUM : WCG(ucachesize);
WCG(scachesize) = (WCG(scachesize) < SCACHE_SIZE_MINIMUM) ? SCACHE_SIZE_MINIMUM : WCG(scachesize);
WCG(scachesize) = (WCG(scachesize) > SCACHE_SIZE_MAXIMUM) ? SCACHE_SIZE_MAXIMUM : WCG(scachesize);
WCG(maxfilesize) = (WCG(maxfilesize) < FILE_SIZE_MINIMUM) ? FILE_SIZE_MINIMUM : WCG(maxfilesize);
WCG(maxfilesize) = (WCG(maxfilesize) > FILE_SIZE_MAXIMUM) ? FILE_SIZE_MAXIMUM : WCG(maxfilesize);
/* ttlmax can be set to 0 which means scavenger is completely disabled */
if(WCG(ttlmax) != 0)
{
WCG(ttlmax) = (WCG(ttlmax) < TTL_VALUE_MINIMUM) ? TTL_VALUE_MINIMUM : WCG(ttlmax);
WCG(ttlmax) = (WCG(ttlmax) > TTL_VALUE_MAXIMUM) ? TTL_VALUE_MAXIMUM : WCG(ttlmax);
}
/* fcchkfreq can be set to 0 which will mean check is completely disabled */
if(WCG(fcchkfreq) != 0)
{
WCG(fcchkfreq) = (WCG(fcchkfreq) < FCHECK_FREQ_MINIMUM) ? FCHECK_FREQ_MINIMUM : WCG(fcchkfreq);
WCG(fcchkfreq) = (WCG(fcchkfreq) > FCHECK_FREQ_MAXIMUM) ? FCHECK_FREQ_MAXIMUM : WCG(fcchkfreq);
}
/* Truncate namesalt to 8 characters */
if(WCG(namesalt) != NULL && strlen(WCG(namesalt)) > NAMESALT_LENGTH_MAXIMUM)
{
// TODO: instead of stomping on memory we don't own, we should just
// TODO: allocate off a duplicate and truncate the value. Maybe even
// TODO: have a static array we use whenever this happens?
pinientry = zend_hash_str_find_ptr(EG(ini_directives), "wincache.namesalt", sizeof("wincache.namesalt")-1);
_ASSERT(pinientry != NULL);
*(ZSTR_VAL(pinientry->value) + NAMESALT_LENGTH_MAXIMUM) = 0;
ZSTR_LEN(pinientry->value) = NAMESALT_LENGTH_MAXIMUM;
/* since we touched it, we should be polite and clear the hash value. */
zend_string_forget_hash_val(pinientry->value);
/* WCG(namesalt) is already pointing to pinientry->value */
}
/* Convert ignorelist to lowercase soon enough */
if(WCG(ignorelist) != NULL)
{
_strlwr(WCG(ignorelist));
}
/* Even if enabled is set to false, create cache and set */
/* the hook because scripts can selectively enable it */
/* Call filemap global initialized. Terminate in MSHUTDOWN */
result = filemap_global_initialize();
if(FAILED(result))
{
goto Finished;
}
/*
* Create user cache
*/
/* Always create user cache as ucenabled can be set by script */
result = zvcache_create(&pzcache);
if(FAILED(result))
{
goto Finished;
}
/* issession = 0, islocal = 0, cachekey = 1, shmfilepath = NULL */
result = zvcache_initialize(pzcache, 0, 0, 1, WCG(ucachesize), NULL);
if(FAILED(result))
{
goto Finished;
}
/* Create hashtable zvcopied */
WCG(zvcopied) = (HashTable *)alloc_pemalloc(sizeof(HashTable));
if(WCG(zvcopied) == NULL)
{
result = FATAL_OUT_OF_LMEMORY;
goto Finished;
}
WCG(zvucache) = pzcache;
/* User cache is now operational, even if things below fail */
pzcache = NULL;
/*
* Register wincache session handler
*/
php_session_register_module(ps_wincache_ptr);
/*
* Create filelist cache
*/
result = aplist_create(&plcache);
if(FAILED(result))
{
goto Finished;
}
result = aplist_initialize(plcache, APLIST_TYPE_GLOBAL, WCG(numfiles), WCG(fcchkfreq), WCG(ttlmax));
if(FAILED(result))
{
goto Finished;
}
/* Initialize file cache */
result = aplist_fcache_initialize(plcache, WCG(fcachesize), WCG(maxfilesize));
if(FAILED(result))
{
goto Finished;
}
WCG(lfcache) = plcache;
wincache_intercept_functions_init();
zend_stream_open_function = wincache_stream_open_function;
zend_resolve_path = wincache_resolve_path;
dprintverbose("Installed function hooks for stream_open");
_ASSERT(SUCCEEDED(result));
Finished:
if(FAILED(result))
{
EventWriteModuleInitErrorEvent(result);
dprintimportant("failure %d in php_minit", result);
wincache_intercept_functions_shutdown();
if(plcache != NULL)
{
aplist_terminate(plcache);
aplist_destroy(plcache);
plcache = NULL;
WCG(lfcache) = NULL;
}
if(pzcache != NULL)
{
if(WCG(zvcopied) != NULL)
{
alloc_pefree(WCG(zvcopied));
WCG(zvcopied) = NULL;
}
zvcache_terminate(pzcache);
zvcache_destroy(pzcache);
pzcache = NULL;
WCG(zvucache) = NULL;
}
}
dprintverbose("end php_minit");
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(wincache)
{
dprintverbose("start php_mshutdown");
/* If enablecli is 0, short circuit this call in cli mode */
if(!WCG(enablecli) && !strcmp(sapi_module.name, "cli"))
{
goto Finished;
}
zend_resolve_path = original_resolve_path;
zend_stream_open_function = original_stream_open_function;
wincache_intercept_functions_shutdown();
/* wclocks_destructor will destroy the locks properly */
if(WCG(wclocks) != NULL)
{
zend_hash_destroy(WCG(wclocks));
alloc_pefree(WCG(wclocks));
WCG(wclocks) = NULL;
}
if(WCG(zvcopied) != NULL)
{
alloc_pefree(WCG(zvcopied));
WCG(zvcopied) = NULL;
}
if(WCG(lfcache) != NULL)
{
aplist_terminate(WCG(lfcache));
aplist_destroy(WCG(lfcache));
WCG(lfcache) = NULL;
}
if(WCG(zvucache) != NULL)
{
zvcache_terminate(WCG(zvucache));
zvcache_destroy(WCG(zvucache));
WCG(zvucache) = NULL;
}
/* Must shut down this table before shutting down WCG(zvscache) */
if(WCG(phscache) != NULL)
{
/* destructor in wincache_session.c will destroy zvcache_context */
zend_hash_destroy(WCG(phscache));
alloc_pefree(WCG(phscache));
WCG(phscache) = NULL;
}
if(WCG(zvscache) != NULL)
{
zvcache_terminate(WCG(zvscache));
zvcache_destroy(WCG(zvscache));
WCG(zvscache) = NULL;
}
#ifdef ZTS
ts_free_id(wincache_globals_id);
#else
globals_terminate(&wincache_globals);
#endif /* ZTS */
filemap_global_terminate();
Finished:
/* Unregister ini entries. globals_terminate will get */
/* called by zend engine after this */
UNREGISTER_INI_ENTRIES();
WCG(inifce) = NULL;
WCG(inisavepath) = NULL;
EventUnregisterPHP_Wincache();
dprintverbose("end php_mshutdown");
return SUCCESS;
}
PHP_RINIT_FUNCTION(wincache)
{
zval * siteid = NULL;
/* If enablecli is 0, short circuit this call in cli mode */
if(!WCG(enablecli) && !strcmp(sapi_module.name, "cli"))
{
goto Finished;
}
if ((WCG(fcefilter) != NULL))
{
/* Read site id from list of variables */
zend_is_auto_global_str("_SERVER", sizeof("_SERVER") - 1);
if (Z_ISUNDEF(PG(http_globals)[TRACK_VARS_SERVER]))
{
goto Finished;
}
if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) != IS_ARRAY)
{
goto Finished;
}
siteid = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_SERVER]), "INSTANCE_ID", sizeof("INSTANCE_ID")-1);
if (siteid == NULL)
{
goto Finished;
}
}
if(WCG(fcefilter) != NULL && isin_cseplist(WCG(fcefilter), Z_STRVAL_P(siteid)))
{
WCG(dofctoggle) = 1;
}
Finished:
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(wincache)
{
/* If enablecli is 0, short circuit this call in cli mode */
if(!WCG(enablecli) && !strcmp(sapi_module.name, "cli"))
{
goto Finished;
}
/* Destroy errmsglist now before zend_mm does it */
if(WCG(errmsglist) != NULL)
{
zend_llist_destroy(WCG(errmsglist));
alloc_efree(WCG(errmsglist));
WCG(errmsglist) = NULL;
}
WCG(dofctoggle) = 0;
Finished:
return SUCCESS;
}
PHP_MINFO_FUNCTION(wincache)
{
dprintverbose("start php_minfo");
php_info_print_table_start();
if(!WCG(enablecli) && !strcmp(sapi_module.name, "cli"))
{
php_info_print_table_row(2, "File cache", "disabled");
}
else
{
php_info_print_table_row(2, "File cache", WCG(fcenabled) ? "enabled" : "disabled");
}
php_info_print_table_row(2, "Version", PHP_WINCACHE_VERSION);
php_info_print_table_row(2, "Owner", "iisphp@microsoft.com");
php_info_print_table_row(2, "Build Date", __DATE__ " " __TIME__);
php_info_print_table_row(2, "Compatibility", PHP_WINCACHE_COMPATPHP);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
dprintverbose("end php_minfo");
return;
}
zend_string * wincache_resolve_path(const char * filename, int filename_len)
{
int result = NONFATAL;
char * res_path_str = NULL;
zend_string * resolve_path = NULL;
unsigned char cenabled = 0;
fcache_value * pfvalue = NULL;
cenabled = WCG(fcenabled);
/* If fcenabled is not modified in php code and toggle is set, change cenabled */
_ASSERT(WCG(inifce) != NULL);
if(WCG(inifce)->modified == 0 && WCG(dofctoggle) == 1)
{
/* toggle the current value of cenabled */
cenabled = !cenabled;
}
/* If wincache.fcenabled is set to 0 but some how */
/* this method is called, use original_resolve_path */
if(!cenabled || filename == NULL || WCG(lfcache) == NULL)
{
return original_resolve_path(filename, filename_len);
}
dprintverbose("zend_resolve_path called for %s", filename);
if(isin_ignorelist(WCG(ignorelist), filename))
{
dprintimportant("cache is disabled for the file because of ignore list");
return original_resolve_path(filename, filename_len);
}
/* Keep last argument as NULL to indicate that we only want fullpath of file */
result = aplist_fcache_get(WCG(lfcache), filename, SKIP_STREAM_OPEN_CHECK, &res_path_str, &pfvalue);
if(FAILED(result))
{
goto Finished;
}
_ASSERT(SUCCEEDED(result));
resolve_path = zend_string_init(res_path_str, strlen(res_path_str), 0);
Finished:
if(pfvalue != NULL)
{
aplist_fcache_close(WCG(lfcache), pfvalue);
pfvalue = NULL;
}
if (res_path_str != NULL)
{
alloc_efree(res_path_str);
res_path_str = NULL;
}
if(FAILED(result))
{
dprintverbose("wincache_resolve_path failed with error %u", result);
return original_resolve_path(filename, filename_len);
}
return resolve_path;
}
int wincache_stream_open_function(const char * filename, zend_file_handle * file_handle)
{
int result = NONFATAL;
fcache_value * pfvalue = NULL;
char * fullpath = NULL;
unsigned char cenabled = 0;
dprintverbose("start wincache_stream_open_function");
cenabled = WCG(fcenabled);
/* If fcenabled is not modified in php code and toggle is set, change cenabled */
_ASSERT(WCG(inifce) != NULL);
if(WCG(inifce)->modified == 0 && WCG(dofctoggle) == 1)
{
/* toggle the current value of cenabled */
cenabled = !cenabled;
}
/* If wincache.fcenabled is set to 0 but some how */
/* this method is called, use original_stream_open_function */
if(!cenabled || filename == NULL || WCG(lfcache) == NULL)
{
return original_stream_open_function(filename, file_handle);
}
dprintverbose("zend_stream_open_function called for %s", filename);
if(isin_ignorelist(WCG(ignorelist), filename))
{
dprintverbose("cache is disabled for the file because of ignore list");
return original_stream_open_function(filename, file_handle);
}
result = aplist_fcache_get(WCG(lfcache), filename, USE_STREAM_OPEN_CHECK, &fullpath, &pfvalue);
if(FAILED(result))
{
/* If original_stream_open failed, do not try again */
if(result == FATAL_FCACHE_ORIGINAL_OPEN)
{
return FAILURE;
}
goto Finished;
}
if(pfvalue != NULL)
{