-
Notifications
You must be signed in to change notification settings - Fork 142
/
ZendAccelerator.c
2805 lines (2439 loc) · 88.6 KB
/
ZendAccelerator.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
/*
+----------------------------------------------------------------------+
| Zend OPcache |
+----------------------------------------------------------------------+
| Copyright (c) 1998-2015 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: Andi Gutmans <[email protected]> |
| Zeev Suraski <[email protected]> |
| Stanislav Malyshev <[email protected]> |
| Dmitry Stogov <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "main/php.h"
#include "main/php_globals.h"
#include "zend.h"
#include "zend_extensions.h"
#include "zend_compile.h"
#include "ZendAccelerator.h"
#include "zend_persist.h"
#include "zend_shared_alloc.h"
#include "zend_accelerator_module.h"
#include "zend_accelerator_blacklist.h"
#include "zend_list.h"
#include "zend_execute.h"
#include "main/SAPI.h"
#include "main/php_streams.h"
#include "main/php_open_temporary_file.h"
#include "zend_API.h"
#include "zend_ini.h"
#include "TSRM/tsrm_virtual_cwd.h"
#include "zend_accelerator_util_funcs.h"
#include "zend_accelerator_hash.h"
#ifndef ZEND_WIN32
#include <netdb.h>
#endif
#ifdef ZEND_WIN32
typedef int uid_t;
typedef int gid_t;
#include <io.h>
#endif
#ifndef ZEND_WIN32
# include <sys/time.h>
#else
# include <process.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#ifndef ZEND_WIN32
# include <sys/types.h>
# include <sys/ipc.h>
#endif
#include <sys/stat.h>
#include <errno.h>
#define SHM_PROTECT() \
do { \
if (ZCG(accel_directives).protect_memory) { \
zend_accel_shared_protect(1 TSRMLS_CC); \
} \
} while (0)
#define SHM_UNPROTECT() \
do { \
if (ZCG(accel_directives).protect_memory) { \
zend_accel_shared_protect(0 TSRMLS_CC); \
} \
} while (0)
ZEND_EXTENSION();
#ifndef ZTS
zend_accel_globals accel_globals;
#else
int accel_globals_id;
#endif
/* Points to the structure shared across all PHP processes */
zend_accel_shared_globals *accel_shared_globals = NULL;
/* true globals, no need for thread safety */
zend_bool accel_startup_ok = 0;
static char *zps_failure_reason = NULL;
char *zps_api_failure_reason = NULL;
static zend_op_array *(*accelerator_orig_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);
static int (*accelerator_orig_zend_stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC);
#if ZEND_EXTENSION_API_NO >= PHP_5_3_X_API_NO
static char *(*accelerator_orig_zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
#endif
static void (*orig_chdir)(INTERNAL_FUNCTION_PARAMETERS) = NULL;
static ZEND_INI_MH((*orig_include_path_on_modify)) = NULL;
#if ZEND_EXTENSION_API_NO < PHP_5_3_X_API_NO
static char *accel_php_resolve_path(const char *filename, int filename_length, const char *path TSRMLS_DC);
#endif
#ifdef ZEND_WIN32
# define INCREMENT(v) InterlockedIncrement(&ZCSG(v))
# define DECREMENT(v) InterlockedDecrement(&ZCSG(v))
# define LOCKVAL(v) (ZCSG(v))
#endif
#ifdef ZEND_WIN32
static time_t zend_accel_get_time(void)
{
FILETIME now;
GetSystemTimeAsFileTime(&now);
return (time_t) ((((((__int64)now.dwHighDateTime) << 32)|now.dwLowDateTime) - 116444736000000000L)/10000000);
}
#else
# define zend_accel_get_time() time(NULL)
#endif
static inline int is_stream_path(const char *filename)
{
const char *p;
for (p = filename; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++);
return ((*p == ':') && (p - filename > 1) && (p[1] == '/') && (p[2] == '/'));
}
static inline int is_cacheable_stream_path(const char *filename)
{
return memcmp(filename, "file://", sizeof("file://") - 1) == 0 ||
memcmp(filename, "phar://", sizeof("phar://") - 1) == 0;
}
/* O+ overrides PHP chdir() function and remembers the current working directory
* in ZCG(cwd) and ZCG(cwd_len). Later accel_getcwd() can use stored value and
* avoid getcwd() call.
*/
static ZEND_FUNCTION(accel_chdir)
{
char cwd[MAXPATHLEN];
orig_chdir(INTERNAL_FUNCTION_PARAM_PASSTHRU);
if (VCWD_GETCWD(cwd, MAXPATHLEN)) {
if (ZCG(cwd)) {
efree(ZCG(cwd));
}
ZCG(cwd_len) = strlen(cwd);
ZCG(cwd) = estrndup(cwd, ZCG(cwd_len));
} else {
if (ZCG(cwd)) {
efree(ZCG(cwd));
ZCG(cwd) = NULL;
}
}
}
static inline char* accel_getcwd(int *cwd_len TSRMLS_DC)
{
if (ZCG(cwd)) {
*cwd_len = ZCG(cwd_len);
return ZCG(cwd);
} else {
char cwd[MAXPATHLEN + 1];
if (!VCWD_GETCWD(cwd, MAXPATHLEN)) {
return NULL;
}
*cwd_len = ZCG(cwd_len) = strlen(cwd);
ZCG(cwd) = estrndup(cwd, ZCG(cwd_len));
return ZCG(cwd);
}
}
void zend_accel_schedule_restart_if_necessary(zend_accel_restart_reason reason TSRMLS_DC)
{
if ((((double) ZSMMG(wasted_shared_memory)) / ZCG(accel_directives).memory_consumption) >= ZCG(accel_directives).max_wasted_percentage) {
zend_accel_schedule_restart(reason TSRMLS_CC);
}
}
/* O+ tracks changes of "include_path" directive. It stores all the requested
* values in ZCG(include_paths) shared hash table, current value in
* ZCG(include_path)/ZCG(include_path_len) and one letter "path key" in
* ZCG(include_path_key).
*/
static ZEND_INI_MH(accel_include_path_on_modify)
{
int ret = orig_include_path_on_modify(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
ZCG(include_path_key) = NULL;
if (ret == SUCCESS) {
ZCG(include_path) = new_value;
if (ZCG(include_path) && *ZCG(include_path)) {
ZCG(include_path_len) = new_value_length;
if (ZCG(enabled) && accel_startup_ok &&
(ZCG(counted) || ZCSG(accelerator_enabled))) {
ZCG(include_path_key) = zend_accel_hash_find(&ZCSG(include_paths), ZCG(include_path), ZCG(include_path_len) + 1);
if (!ZCG(include_path_key) &&
!zend_accel_hash_is_full(&ZCSG(include_paths))) {
SHM_UNPROTECT();
zend_shared_alloc_lock(TSRMLS_C);
ZCG(include_path_key) = zend_accel_hash_find(&ZCSG(include_paths), ZCG(include_path), ZCG(include_path_len) + 1);
if (!ZCG(include_path_key) &&
!zend_accel_hash_is_full(&ZCSG(include_paths))) {
char *key;
key = zend_shared_alloc(ZCG(include_path_len) + 2);
if (key) {
memcpy(key, ZCG(include_path), ZCG(include_path_len) + 1);
key[ZCG(include_path_len) + 1] = 'A' + ZCSG(include_paths).num_entries;
ZCG(include_path_key) = key + ZCG(include_path_len) + 1;
zend_accel_hash_update(&ZCSG(include_paths), key, ZCG(include_path_len) + 1, 0, ZCG(include_path_key));
} else {
zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_OOM TSRMLS_CC);
}
}
zend_shared_alloc_unlock(TSRMLS_C);
SHM_PROTECT();
}
} else {
ZCG(include_path_check) = 1;
}
} else {
ZCG(include_path) = "";
ZCG(include_path_len) = 0;
}
}
return ret;
}
#if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO
/* Interned strings support */
static char *orig_interned_strings_start;
static char *orig_interned_strings_end;
static const char *(*orig_new_interned_string)(const char *str, int len, int free_src TSRMLS_DC);
static void (*orig_interned_strings_snapshot)(TSRMLS_D);
static void (*orig_interned_strings_restore)(TSRMLS_D);
/* O+ disables creation of interned strings by regular PHP compiler, instead,
* it creates interned strings in shared memory when saves a script.
* Such interned strings are shared across all PHP processes
*/
static const char *accel_new_interned_string_for_php(const char *str, int len, int free_src TSRMLS_DC)
{
return str;
}
static void accel_interned_strings_snapshot_for_php(TSRMLS_D)
{
}
static void accel_interned_strings_restore_for_php(TSRMLS_D)
{
}
#ifndef ZTS
static void accel_interned_strings_restore_state(TSRMLS_D)
{
unsigned int i;
for (i = 0; i < ZCSG(interned_strings).nTableSize; i++) {
ZCSG(interned_strings).arBuckets[i] = ZCSG(interned_strings_saved_state).arBuckets[i];
if (ZCSG(interned_strings).arBuckets[i]) {
ZCSG(interned_strings).arBuckets[i]->pLast = NULL;
}
}
ZCSG(interned_strings).pListHead = ZCSG(interned_strings_saved_state).pListHead;
ZCSG(interned_strings).pListTail = ZCSG(interned_strings_saved_state).pListTail;
if (ZCSG(interned_strings).pListHead) {
ZCSG(interned_strings).pListHead->pListLast = NULL;
}
if (ZCSG(interned_strings).pListTail) {
ZCSG(interned_strings).pListTail->pListNext = NULL;
}
ZCSG(interned_strings_top) = ZCSG(interned_strings_saved_state).top;
}
static void accel_interned_strings_save_state(TSRMLS_D)
{
ZCSG(interned_strings_saved_state).arBuckets = (Bucket**)zend_shared_alloc(ZCSG(interned_strings).nTableSize * sizeof(Bucket *));
if (!ZCSG(interned_strings_saved_state).arBuckets) {
zend_accel_error(ACCEL_LOG_FATAL, "Insufficient shared memory!");
}
memcpy(ZCSG(interned_strings_saved_state).arBuckets, ZCSG(interned_strings).arBuckets, ZCSG(interned_strings).nTableSize * sizeof(Bucket *));
ZCSG(interned_strings_saved_state).pListHead = ZCSG(interned_strings).pListHead;
ZCSG(interned_strings_saved_state).pListTail = ZCSG(interned_strings).pListTail;
ZCSG(interned_strings_saved_state).top = ZCSG(interned_strings_top);
}
#endif
const char *accel_new_interned_string(const char *arKey, int nKeyLength, int free_src TSRMLS_DC)
{
/* for now interned strings are supported only for non-ZTS build */
#ifndef ZTS
ulong h;
uint nIndex;
Bucket *p;
if (arKey >= ZCSG(interned_strings_start) && arKey < ZCSG(interned_strings_end)) {
/* this is already an interned string */
return arKey;
}
h = zend_inline_hash_func(arKey, nKeyLength);
nIndex = h & ZCSG(interned_strings).nTableMask;
/* check for existing interned string */
p = ZCSG(interned_strings).arBuckets[nIndex];
while (p != NULL) {
if ((p->h == h) && (p->nKeyLength == (uint)nKeyLength)) {
if (!memcmp(p->arKey, arKey, nKeyLength)) {
if (free_src) {
efree((char*)arKey);
}
return p->arKey;
}
}
p = p->pNext;
}
if (ZCSG(interned_strings_top) + ZEND_MM_ALIGNED_SIZE(sizeof(Bucket) + nKeyLength) >=
ZCSG(interned_strings_end)) {
/* no memory, return the same non-interned string */
zend_accel_error(ACCEL_LOG_WARNING, "Interned string buffer overflow");
return arKey;
}
/* create new interning string in shared interned strings buffer */
p = (Bucket *) ZCSG(interned_strings_top);
ZCSG(interned_strings_top) += ZEND_MM_ALIGNED_SIZE(sizeof(Bucket) + nKeyLength);
p->arKey = (char*)(p + 1);
memcpy((char*)p->arKey, arKey, nKeyLength);
p->nKeyLength = nKeyLength;
p->h = h;
p->pData = &p->pDataPtr;
p->pDataPtr = p;
p->pNext = ZCSG(interned_strings).arBuckets[nIndex];
p->pLast = NULL;
if (p->pNext) {
p->pNext->pLast = p;
}
ZCSG(interned_strings).arBuckets[nIndex] = p;
p->pListLast = ZCSG(interned_strings).pListTail;
ZCSG(interned_strings).pListTail = p;
p->pListNext = NULL;
if (p->pListLast != NULL) {
p->pListLast->pListNext = p;
}
if (!ZCSG(interned_strings).pListHead) {
ZCSG(interned_strings).pListHead = p;
}
ZCSG(interned_strings).nNumOfElements++;
if (free_src) {
efree((char*)arKey);
}
return p->arKey;
#else
return arKey;
#endif
}
#ifndef ZTS
/* Copy PHP interned strings from PHP process memory into the shared memory */
static void accel_use_shm_interned_strings(TSRMLS_D)
{
Bucket *p, *q;
/* function table hash keys */
p = CG(function_table)->pListHead;
while (p) {
if (p->nKeyLength) {
p->arKey = accel_new_interned_string(p->arKey, p->nKeyLength, 0 TSRMLS_CC);
}
p = p->pListNext;
}
/* class table hash keys, class names, properties, methods, constants, etc */
p = CG(class_table)->pListHead;
while (p) {
zend_class_entry *ce = (zend_class_entry*)(p->pDataPtr);
if (p->nKeyLength) {
p->arKey = accel_new_interned_string(p->arKey, p->nKeyLength, 0 TSRMLS_CC);
}
if (ce->name) {
ce->name = accel_new_interned_string(ce->name, ce->name_length + 1, 0 TSRMLS_CC);
}
q = ce->properties_info.pListHead;
while (q) {
zend_property_info *info = (zend_property_info*)(q->pData);
if (q->nKeyLength) {
q->arKey = accel_new_interned_string(q->arKey, q->nKeyLength, 0 TSRMLS_CC);
}
if (info->name) {
info->name = accel_new_interned_string(info->name, info->name_length + 1, 0 TSRMLS_CC);
}
q = q->pListNext;
}
q = ce->function_table.pListHead;
while (q) {
if (q->nKeyLength) {
q->arKey = accel_new_interned_string(q->arKey, q->nKeyLength, 0 TSRMLS_CC);
}
q = q->pListNext;
}
q = ce->constants_table.pListHead;
while (q) {
if (q->nKeyLength) {
q->arKey = accel_new_interned_string(q->arKey, q->nKeyLength, 0 TSRMLS_CC);
}
q = q->pListNext;
}
p = p->pListNext;
}
/* constant hash keys */
p = EG(zend_constants)->pListHead;
while (p) {
if (p->nKeyLength) {
p->arKey = accel_new_interned_string(p->arKey, p->nKeyLength, 0 TSRMLS_CC);
}
p = p->pListNext;
}
/* auto globals hash keys and names */
p = CG(auto_globals)->pListHead;
while (p) {
zend_auto_global *auto_global = (zend_auto_global*)p->pData;
auto_global->name = accel_new_interned_string(auto_global->name, auto_global->name_len + 1, 0 TSRMLS_CC);
if (p->nKeyLength) {
p->arKey = accel_new_interned_string(p->arKey, p->nKeyLength, 0 TSRMLS_CC);
}
p = p->pListNext;
}
}
#endif
#endif
static inline void accel_restart_enter(TSRMLS_D)
{
#ifdef ZEND_WIN32
INCREMENT(restart_in);
#else
static const FLOCK_STRUCTURE(restart_in_progress, F_WRLCK, SEEK_SET, 2, 1);
if (fcntl(lock_file, F_SETLK, &restart_in_progress) == -1) {
zend_accel_error(ACCEL_LOG_DEBUG, "RestartC(+1): %s (%d)", strerror(errno), errno);
}
#endif
ZCSG(restart_in_progress) = 1;
}
static inline void accel_restart_leave(TSRMLS_D)
{
#ifdef ZEND_WIN32
ZCSG(restart_in_progress) = 0;
DECREMENT(restart_in);
#else
static const FLOCK_STRUCTURE(restart_finished, F_UNLCK, SEEK_SET, 2, 1);
ZCSG(restart_in_progress) = 0;
if (fcntl(lock_file, F_SETLK, &restart_finished) == -1) {
zend_accel_error(ACCEL_LOG_DEBUG, "RestartC(-1): %s (%d)", strerror(errno), errno);
}
#endif
}
static inline int accel_restart_is_active(TSRMLS_D)
{
if (ZCSG(restart_in_progress)) {
#ifndef ZEND_WIN32
FLOCK_STRUCTURE(restart_check, F_WRLCK, SEEK_SET, 2, 1);
if (fcntl(lock_file, F_GETLK, &restart_check) == -1) {
zend_accel_error(ACCEL_LOG_DEBUG, "RestartC: %s (%d)", strerror(errno), errno);
return FAILURE;
}
if (restart_check.l_type == F_UNLCK) {
ZCSG(restart_in_progress) = 0;
return 0;
} else {
return 1;
}
#else
return LOCKVAL(restart_in) != 0;
#endif
}
return 0;
}
/* Creates a read lock for SHM access */
static inline void accel_activate_add(TSRMLS_D)
{
#ifdef ZEND_WIN32
INCREMENT(mem_usage);
#else
static const FLOCK_STRUCTURE(mem_usage_lock, F_RDLCK, SEEK_SET, 1, 1);
if (fcntl(lock_file, F_SETLK, &mem_usage_lock) == -1) {
zend_accel_error(ACCEL_LOG_DEBUG, "UpdateC(+1): %s (%d)", strerror(errno), errno);
}
#endif
}
/* Releases a lock for SHM access */
static inline void accel_deactivate_sub(TSRMLS_D)
{
#ifdef ZEND_WIN32
if (ZCG(counted)) {
DECREMENT(mem_usage);
ZCG(counted) = 0;
}
#else
static const FLOCK_STRUCTURE(mem_usage_unlock, F_UNLCK, SEEK_SET, 1, 1);
if (fcntl(lock_file, F_SETLK, &mem_usage_unlock) == -1) {
zend_accel_error(ACCEL_LOG_DEBUG, "UpdateC(-1): %s (%d)", strerror(errno), errno);
}
#endif
}
static inline void accel_unlock_all(TSRMLS_D)
{
#ifdef ZEND_WIN32
accel_deactivate_sub(TSRMLS_C);
#else
static const FLOCK_STRUCTURE(mem_usage_unlock_all, F_UNLCK, SEEK_SET, 0, 0);
if (fcntl(lock_file, F_SETLK, &mem_usage_unlock_all) == -1) {
zend_accel_error(ACCEL_LOG_DEBUG, "UnlockAll: %s (%d)", strerror(errno), errno);
}
#endif
}
#ifndef ZEND_WIN32
static inline void kill_all_lockers(struct flock *mem_usage_check)
{
int tries = 10;
/* so that other process won't try to force while we are busy cleaning up */
ZCSG(force_restart_time) = 0;
while (mem_usage_check->l_pid > 0) {
while (tries--) {
zend_accel_error(ACCEL_LOG_ERROR, "Killed locker %d", mem_usage_check->l_pid);
if (kill(mem_usage_check->l_pid, SIGKILL)) {
break;
}
/* give it a chance to die */
usleep(20000);
if (kill(mem_usage_check->l_pid, 0)) {
/* can't kill it */
break;
}
usleep(10000);
}
if (!tries) {
zend_accel_error(ACCEL_LOG_ERROR, "Can't kill %d after 20 tries!", mem_usage_check->l_pid);
ZCSG(force_restart_time) = time(NULL); /* restore forced restart request */
}
mem_usage_check->l_type = F_WRLCK;
mem_usage_check->l_whence = SEEK_SET;
mem_usage_check->l_start = 1;
mem_usage_check->l_len = 1;
mem_usage_check->l_pid = -1;
if (fcntl(lock_file, F_GETLK, mem_usage_check) == -1) {
zend_accel_error(ACCEL_LOG_DEBUG, "KLockers: %s (%d)", strerror(errno), errno);
break;
}
if (mem_usage_check->l_type == F_UNLCK || mem_usage_check->l_pid <= 0) {
break;
}
}
}
#endif
static inline int accel_is_inactive(TSRMLS_D)
{
#ifdef ZEND_WIN32
if (LOCKVAL(mem_usage) == 0) {
return SUCCESS;
}
#else
FLOCK_STRUCTURE(mem_usage_check, F_WRLCK, SEEK_SET, 1, 1);
mem_usage_check.l_pid = -1;
if (fcntl(lock_file, F_GETLK, &mem_usage_check) == -1) {
zend_accel_error(ACCEL_LOG_DEBUG, "UpdateC: %s (%d)", strerror(errno), errno);
return FAILURE;
}
if (mem_usage_check.l_type == F_UNLCK) {
return SUCCESS;
}
if (ZCG(accel_directives).force_restart_timeout
&& ZCSG(force_restart_time)
&& time(NULL) >= ZCSG(force_restart_time)) {
zend_accel_error(ACCEL_LOG_WARNING, "Forced restart at %d (after %d seconds), locked by %d", time(NULL), ZCG(accel_directives).force_restart_timeout, mem_usage_check.l_pid);
kill_all_lockers(&mem_usage_check);
return FAILURE; /* next request should be able to restart it */
}
#endif
return FAILURE;
}
static int zend_get_stream_timestamp(const char *filename, struct stat *statbuf TSRMLS_DC)
{
php_stream_wrapper *wrapper;
php_stream_statbuf stream_statbuf;
int ret, er;
if (!filename) {
return FAILURE;
}
wrapper = php_stream_locate_url_wrapper(filename, NULL, STREAM_LOCATE_WRAPPERS_ONLY TSRMLS_CC);
if (!wrapper) {
return FAILURE;
}
if (!wrapper->wops || !wrapper->wops->url_stat) {
statbuf->st_mtime = 1;
return SUCCESS; /* anything other than 0 is considered to be a valid timestamp */
}
er = EG(error_reporting);
EG(error_reporting) = 0;
zend_try {
ret = wrapper->wops->url_stat(wrapper, (char*)filename, PHP_STREAM_URL_STAT_QUIET, &stream_statbuf, NULL TSRMLS_CC);
} zend_catch {
ret = -1;
} zend_end_try();
EG(error_reporting) = er;
if (ret != 0) {
return FAILURE;
}
*statbuf = stream_statbuf.sb;
return SUCCESS;
}
#if ZEND_WIN32
static accel_time_t zend_get_file_handle_timestamp_win(zend_file_handle *file_handle, size_t *size)
{
static unsigned __int64 utc_base = 0;
static FILETIME utc_base_ft;
WIN32_FILE_ATTRIBUTE_DATA fdata;
if (!file_handle->opened_path) {
return 0;
}
if (!utc_base) {
SYSTEMTIME st;
st.wYear = 1970;
st.wMonth = 1;
st.wDay = 1;
st.wHour = 0;
st.wMinute = 0;
st.wSecond = 0;
st.wMilliseconds = 0;
SystemTimeToFileTime (&st, &utc_base_ft);
utc_base = (((unsigned __int64)utc_base_ft.dwHighDateTime) << 32) + utc_base_ft.dwLowDateTime;
}
if (GetFileAttributesEx(file_handle->opened_path, GetFileExInfoStandard, &fdata) != 0) {
unsigned __int64 ftime;
if (CompareFileTime (&fdata.ftLastWriteTime, &utc_base_ft) < 0) {
return 0;
}
ftime = (((unsigned __int64)fdata.ftLastWriteTime.dwHighDateTime) << 32) + fdata.ftLastWriteTime.dwLowDateTime - utc_base;
ftime /= 10000000L;
if (size) {
*size = (size_t)(((unsigned __int64)fdata.nFileSizeHigh) << 32 + (unsigned __int64)fdata.nFileSizeLow);
}
return (accel_time_t)ftime;
}
return 0;
}
#endif
static accel_time_t zend_get_file_handle_timestamp(zend_file_handle *file_handle, size_t *size TSRMLS_DC)
{
struct stat statbuf;
#ifdef ZEND_WIN32
accel_time_t res;
#endif
if (sapi_module.get_stat &&
!EG(opline_ptr) &&
file_handle->filename == SG(request_info).path_translated) {
struct stat *tmpbuf = sapi_module.get_stat(TSRMLS_C);
if (tmpbuf) {
if (size) {
*size = tmpbuf->st_size;
}
return tmpbuf->st_mtime;
}
}
#ifdef ZEND_WIN32
res = zend_get_file_handle_timestamp_win(file_handle, size);
if (res) {
return res;
}
#endif
switch (file_handle->type) {
case ZEND_HANDLE_FD:
if (fstat(file_handle->handle.fd, &statbuf) == -1) {
return 0;
}
break;
case ZEND_HANDLE_FP:
if (fstat(fileno(file_handle->handle.fp), &statbuf) == -1) {
if (zend_get_stream_timestamp(file_handle->filename, &statbuf TSRMLS_CC) != SUCCESS) {
return 0;
}
}
break;
case ZEND_HANDLE_FILENAME:
#if ZEND_EXTENSION_API_NO >= PHP_5_3_X_API_NO
case ZEND_HANDLE_MAPPED:
#endif
{
char *file_path = file_handle->opened_path;
if (file_path) {
if (is_stream_path(file_path)) {
if (zend_get_stream_timestamp(file_path, &statbuf TSRMLS_CC) == SUCCESS) {
break;
}
}
if (VCWD_STAT(file_path, &statbuf) != -1) {
break;
}
}
if (zend_get_stream_timestamp(file_handle->filename, &statbuf TSRMLS_CC) != SUCCESS) {
return 0;
}
break;
}
case ZEND_HANDLE_STREAM:
{
php_stream *stream = (php_stream *)file_handle->handle.stream.handle;
php_stream_statbuf sb;
int ret, er;
if (!stream ||
!stream->ops ||
!stream->ops->stat) {
return 0;
}
er = EG(error_reporting);
EG(error_reporting) = 0;
zend_try {
ret = stream->ops->stat(stream, &sb TSRMLS_CC);
} zend_catch {
ret = -1;
} zend_end_try();
EG(error_reporting) = er;
if (ret != 0) {
return 0;
}
statbuf = sb.sb;
}
break;
default:
return 0;
}
if (size) {
*size = statbuf.st_size;
}
return statbuf.st_mtime;
}
static inline int do_validate_timestamps(zend_persistent_script *persistent_script, zend_file_handle *file_handle TSRMLS_DC)
{
zend_file_handle ps_handle;
char *full_path_ptr = NULL;
/** check that the persistant script is indeed the same file we cached
* (if part of the path is a symlink than it possible that the user will change it)
* See bug #15140
*/
if (file_handle->opened_path) {
if (strcmp(persistent_script->full_path, file_handle->opened_path) != 0) {
return FAILURE;
}
} else {
#if ZEND_EXTENSION_API_NO < PHP_5_3_X_API_NO
full_path_ptr = accel_php_resolve_path(file_handle->filename, strlen(file_handle->filename), ZCG(include_path) TSRMLS_CC);
#else
full_path_ptr = accelerator_orig_zend_resolve_path(file_handle->filename, strlen(file_handle->filename) TSRMLS_CC);
#endif
if (full_path_ptr && strcmp(persistent_script->full_path, full_path_ptr) != 0) {
efree(full_path_ptr);
return FAILURE;
}
file_handle->opened_path = full_path_ptr;
}
if (persistent_script->timestamp == 0) {
if (full_path_ptr) {
efree(full_path_ptr);
file_handle->opened_path = NULL;
}
return FAILURE;
}
if (zend_get_file_handle_timestamp(file_handle, NULL TSRMLS_CC) == persistent_script->timestamp) {
if (full_path_ptr) {
efree(full_path_ptr);
file_handle->opened_path = NULL;
}
return SUCCESS;
}
if (full_path_ptr) {
efree(full_path_ptr);
file_handle->opened_path = NULL;
}
ps_handle.type = ZEND_HANDLE_FILENAME;
ps_handle.filename = persistent_script->full_path;
ps_handle.opened_path = persistent_script->full_path;
if (zend_get_file_handle_timestamp(&ps_handle, NULL TSRMLS_CC) == persistent_script->timestamp) {
return SUCCESS;
}
return FAILURE;
}
int validate_timestamp_and_record(zend_persistent_script *persistent_script, zend_file_handle *file_handle TSRMLS_DC)
{
if (ZCG(accel_directives).revalidate_freq &&
persistent_script->dynamic_members.revalidate >= ZCG(request_time)) {
return SUCCESS;
} else if (do_validate_timestamps(persistent_script, file_handle TSRMLS_CC) == FAILURE) {
return FAILURE;
} else {
persistent_script->dynamic_members.revalidate = ZCG(request_time) + ZCG(accel_directives).revalidate_freq;
return SUCCESS;
}
}
static unsigned int zend_accel_script_checksum(zend_persistent_script *persistent_script)
{
signed char *mem = (signed char*)persistent_script->mem;
size_t size = persistent_script->size;
size_t persistent_script_check_block_size = ((char *)&(persistent_script->dynamic_members)) - (char *)persistent_script;
unsigned int checksum = ADLER32_INIT;
if (mem < (signed char*)persistent_script) {
checksum = zend_adler32(checksum, mem, (signed char*)persistent_script - mem);
size -= (signed char*)persistent_script - mem;
mem += (signed char*)persistent_script - mem;
}
zend_adler32(checksum, mem, persistent_script_check_block_size);
mem += sizeof(*persistent_script);
size -= sizeof(*persistent_script);
if (size > 0) {
checksum = zend_adler32(checksum, mem, size);
}
return checksum;
}
/* Instead of resolving full real path name each time we need to identify file,
* we create a key that consist from requested file name, current working
* directory, current include_path, etc */
char *accel_make_persistent_key_ex(zend_file_handle *file_handle, int path_length, int *key_len TSRMLS_DC)
{
int key_length;
/* CWD and include_path don't matter for absolute file names and streams */
if (ZCG(accel_directives).use_cwd &&
!IS_ABSOLUTE_PATH(file_handle->filename, path_length) &&
!is_stream_path(file_handle->filename)) {
char *include_path = NULL;
int include_path_len = 0;
const char *parent_script = NULL;
int parent_script_len = 0;
int cur_len = 0;
int cwd_len;
char *cwd;
if ((cwd = accel_getcwd(&cwd_len TSRMLS_CC)) == NULL) {
/* we don't handle this well for now. */
zend_accel_error(ACCEL_LOG_INFO, "getcwd() failed for '%s' (%d), please try to set opcache.use_cwd to 0 in ini file", file_handle->filename, errno);
if (file_handle->opened_path) {
cwd = file_handle->opened_path;
cwd_len = strlen(cwd);
} else {
ZCG(key_len) = 0;
return NULL;
}
}
if (ZCG(include_path_key)) {
include_path = ZCG(include_path_key);
include_path_len = 1;
} else {
include_path = ZCG(include_path);
include_path_len = ZCG(include_path_len);
if (ZCG(include_path_check) &&
ZCG(enabled) && accel_startup_ok &&
(ZCG(counted) || ZCSG(accelerator_enabled)) &&
!zend_accel_hash_find(&ZCSG(include_paths), ZCG(include_path), ZCG(include_path_len) + 1) &&
!zend_accel_hash_is_full(&ZCSG(include_paths))) {
SHM_UNPROTECT();
zend_shared_alloc_lock(TSRMLS_C);
ZCG(include_path_key) = zend_accel_hash_find(&ZCSG(include_paths), ZCG(include_path), ZCG(include_path_len) + 1);
if (ZCG(include_path_key)) {
include_path = ZCG(include_path_key);
include_path_len = 1;
} else if (!zend_accel_hash_is_full(&ZCSG(include_paths))) {
char *key;
key = zend_shared_alloc(ZCG(include_path_len) + 2);
if (key) {
memcpy(key, ZCG(include_path), ZCG(include_path_len) + 1);
key[ZCG(include_path_len) + 1] = 'A' + ZCSG(include_paths).num_entries;
ZCG(include_path_key) = key + ZCG(include_path_len) + 1;
zend_accel_hash_update(&ZCSG(include_paths), key, ZCG(include_path_len) + 1, 0, ZCG(include_path_key));
include_path = ZCG(include_path_key);
include_path_len = 1;
} else {
zend_accel_schedule_restart_if_necessary(ACCEL_RESTART_OOM TSRMLS_CC);
}
}
zend_shared_alloc_unlock(TSRMLS_C);
SHM_PROTECT();
}
}
/* Here we add to the key the parent script directory,
since fopen_wrappers from version 4.0.7 use current script's path
in include path too.
*/
if (EG(in_execution) &&
(parent_script = zend_get_executed_filename(TSRMLS_C)) != NULL &&
parent_script[0] != '[') {
parent_script_len = strlen(parent_script);
while ((--parent_script_len > 0) && !IS_SLASH(parent_script[parent_script_len]));
}
/* Calculate key length */