Skip to content

Commit 5191581

Browse files
iamacarpetarnaud-lb
authored andcommitted
Fix merge of GH-16551
Some commits were mistakenly discarded during the rebase and squash of GH-16551.
1 parent 7b17313 commit 5191581

6 files changed

+76
-42
lines changed

ext/opcache/ZendAccelerator.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "zend_accelerator_util_funcs.h"
4747
#include "zend_accelerator_hash.h"
4848
#include "zend_file_cache.h"
49+
#include "zend_system_id.h"
4950
#include "ext/pcre/php_pcre.h"
5051
#include "ext/standard/basic_functions.h"
5152

@@ -1411,9 +1412,6 @@ zend_result zend_accel_invalidate(zend_string *filename, bool force)
14111412
}
14121413

14131414
if (ZCG(accel_directives).file_cache) {
1414-
if (ZCG(accel_directives).file_cache_read_only) {
1415-
return FAILURE;
1416-
}
14171415
zend_file_cache_invalidate(realpath);
14181416
}
14191417

@@ -3306,33 +3304,48 @@ static zend_result accel_post_startup(void)
33063304
}
33073305

33083306
/* opcache.file_cache_read_only should only be enabled when all script files are read-only */
3307+
int file_cache_access_mode = 0;
3308+
33093309
if (ZCG(accel_directives).file_cache_read_only) {
3310+
zend_accel_error(ACCEL_LOG_INFO, "opcache.file_cache is in read-only mode");
3311+
33103312
if (!ZCG(accel_directives).file_cache) {
33113313
accel_startup_ok = false;
33123314
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "opcache.file_cache_read_only is set without a proper setting of opcache.file_cache");
33133315
return SUCCESS;
33143316
}
3315-
if (ZCG(accel_directives).revalidate_freq != 0) {
3316-
accel_startup_ok = false;
3317-
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "opcache.file_cache_read_only cannot be enabled when opcache.revalidate_freq is not 0.");
3318-
return SUCCESS;
3319-
}
3320-
if (ZCG(accel_directives).validate_timestamps) {
3321-
accel_startup_ok = false;
3322-
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "opcache.file_cache_read_only cannot be enabled when opcache.validate_timestamps is enabled.");
3323-
return SUCCESS;
3324-
}
3317+
3318+
/* opcache.file_cache is read only, so ensure the directory is readable */
3319+
#ifndef ZEND_WIN32
3320+
file_cache_access_mode = R_OK | X_OK;
3321+
#else
3322+
file_cache_access_mode = 04; // Read access
3323+
#endif
33253324
} else {
33263325
/* opcache.file_cache isn't read only, so ensure the directory is writable */
3327-
if ( ZCG(accel_directives).file_cache &&
33283326
#ifndef ZEND_WIN32
3329-
access(ZCG(accel_directives).file_cache, R_OK | W_OK | X_OK) != 0
3327+
file_cache_access_mode = R_OK | W_OK | X_OK;
3328+
#else
3329+
file_cache_access_mode = 06; // Read and write access
3330+
#endif
3331+
}
3332+
3333+
if ( ZCG(accel_directives).file_cache ) {
3334+
zend_accel_error(ACCEL_LOG_INFO, "opcache.file_cache running with PHP build ID: %s", zend_system_id);
3335+
3336+
zend_stat_t buf = {0};
3337+
3338+
if (!IS_ABSOLUTE_PATH(ZCG(accel_directives).file_cache, strlen(ZCG(accel_directives).file_cache)) ||
3339+
zend_stat(ZCG(accel_directives).file_cache, &buf) != 0 ||
3340+
!S_ISDIR(buf.st_mode) ||
3341+
#ifndef ZEND_WIN32
3342+
access(ZCG(accel_directives).file_cache, file_cache_access_mode) != 0
33303343
#else
3331-
_access(ZCG(accel_directives).file_cache, 06) != 0
3344+
_access(ZCG(accel_directives).file_cache, file_cache_access_mode) != 0
33323345
#endif
3333-
) {
3346+
) {
33343347
accel_startup_ok = false;
3335-
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "opcache.file_cache must be a full path of an accessible, writable directory");
3348+
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "opcache.file_cache must be a full path of an accessible directory");
33363349
return SUCCESS;
33373350
}
33383351
}

ext/opcache/tests/zzz_basic_logging.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ outputs the correct logging at the highest log_verbosity_level
66
--INI--
77
opcache.enable=1
88
opcache.enable_cli=1
9+
opcache.file_cache=
910
opcache.file_cache_only=0
1011
opcache.error_log=
1112
opcache.log_verbosity_level=4

ext/opcache/zend_accelerator_module.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,6 @@ static ZEND_INI_MH(OnUpdateFileCache)
165165
if (new_value) {
166166
if (!ZSTR_LEN(new_value)) {
167167
new_value = NULL;
168-
} else {
169-
zend_stat_t buf = {0};
170-
171-
if (!IS_ABSOLUTE_PATH(ZSTR_VAL(new_value), ZSTR_LEN(new_value)) ||
172-
zend_stat(ZSTR_VAL(new_value), &buf) != 0 ||
173-
!S_ISDIR(buf.st_mode) ||
174-
#ifndef ZEND_WIN32
175-
access(ZSTR_VAL(new_value), R_OK | X_OK) != 0) {
176-
#else
177-
_access(ZSTR_VAL(new_value), 04) != 0) {
178-
#endif
179-
zend_accel_error(ACCEL_LOG_WARNING, "opcache.file_cache must be a full path of accessible directory.\n");
180-
new_value = NULL;
181-
}
182168
}
183169
}
184170
OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
@@ -311,10 +297,10 @@ ZEND_INI_BEGIN()
311297
STD_PHP_INI_ENTRY("opcache.mmap_base", NULL, PHP_INI_SYSTEM, OnUpdateString, accel_directives.mmap_base, zend_accel_globals, accel_globals)
312298
#endif
313299

314-
STD_PHP_INI_ENTRY("opcache.file_cache" , NULL , PHP_INI_SYSTEM, OnUpdateFileCache, accel_directives.file_cache, zend_accel_globals, accel_globals)
315-
STD_PHP_INI_BOOLEAN("opcache.file_cache_read_only" , "0" , PHP_INI_SYSTEM, OnUpdateBool, accel_directives.file_cache_read_only, zend_accel_globals, accel_globals)
316-
STD_PHP_INI_BOOLEAN("opcache.file_cache_only" , "0" , PHP_INI_SYSTEM, OnUpdateBool, accel_directives.file_cache_only, zend_accel_globals, accel_globals)
317-
STD_PHP_INI_BOOLEAN("opcache.file_cache_consistency_checks" , "1" , PHP_INI_SYSTEM, OnUpdateBool, accel_directives.file_cache_consistency_checks, zend_accel_globals, accel_globals)
300+
STD_PHP_INI_ENTRY("opcache.file_cache" , NULL , PHP_INI_SYSTEM, OnUpdateFileCache, accel_directives.file_cache, zend_accel_globals, accel_globals)
301+
STD_PHP_INI_BOOLEAN("opcache.file_cache_read_only" , "0" , PHP_INI_SYSTEM, OnUpdateBool, accel_directives.file_cache_read_only, zend_accel_globals, accel_globals)
302+
STD_PHP_INI_BOOLEAN("opcache.file_cache_only" , "0" , PHP_INI_SYSTEM, OnUpdateBool, accel_directives.file_cache_only, zend_accel_globals, accel_globals)
303+
STD_PHP_INI_BOOLEAN("opcache.file_cache_consistency_checks" , "1" , PHP_INI_SYSTEM, OnUpdateBool, accel_directives.file_cache_consistency_checks, zend_accel_globals, accel_globals)
318304
#if ENABLE_FILE_CACHE_FALLBACK
319305
STD_PHP_INI_BOOLEAN("opcache.file_cache_fallback" , "1" , PHP_INI_SYSTEM, OnUpdateBool, accel_directives.file_cache_fallback, zend_accel_globals, accel_globals)
320306
#endif

ext/opcache/zend_file_cache.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,9 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
18471847
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (info)\n", filename);
18481848
zend_file_cache_flock(fd, LOCK_UN);
18491849
close(fd);
1850-
zend_file_cache_unlink(filename);
1850+
if (!ZCG(accel_directives).file_cache_read_only) {
1851+
zend_file_cache_unlink(filename);
1852+
}
18511853
efree(filename);
18521854
return NULL;
18531855
}
@@ -1857,15 +1859,19 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
18571859
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (wrong header)\n", filename);
18581860
zend_file_cache_flock(fd, LOCK_UN);
18591861
close(fd);
1860-
zend_file_cache_unlink(filename);
1862+
if (!ZCG(accel_directives).file_cache_read_only) {
1863+
zend_file_cache_unlink(filename);
1864+
}
18611865
efree(filename);
18621866
return NULL;
18631867
}
18641868
if (memcmp(info.system_id, zend_system_id, 32) != 0) {
18651869
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (wrong \"system_id\")\n", filename);
18661870
zend_file_cache_flock(fd, LOCK_UN);
18671871
close(fd);
1868-
zend_file_cache_unlink(filename);
1872+
if (!ZCG(accel_directives).file_cache_read_only) {
1873+
zend_file_cache_unlink(filename);
1874+
}
18691875
efree(filename);
18701876
return NULL;
18711877
}
@@ -1877,7 +1883,9 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
18771883
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot unlock file '%s'\n", filename);
18781884
}
18791885
close(fd);
1880-
zend_file_cache_unlink(filename);
1886+
if (!ZCG(accel_directives).file_cache_read_only) {
1887+
zend_file_cache_unlink(filename);
1888+
}
18811889
efree(filename);
18821890
return NULL;
18831891
}
@@ -1895,7 +1903,9 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
18951903
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (mem)\n", filename);
18961904
zend_file_cache_flock(fd, LOCK_UN);
18971905
close(fd);
1898-
zend_file_cache_unlink(filename);
1906+
if (!ZCG(accel_directives).file_cache_read_only) {
1907+
zend_file_cache_unlink(filename);
1908+
}
18991909
zend_arena_release(&CG(arena), checkpoint);
19001910
efree(filename);
19011911
return NULL;
@@ -1909,7 +1919,9 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
19091919
if (ZCG(accel_directives).file_cache_consistency_checks &&
19101920
(actual_checksum = zend_adler32(ADLER32_INIT, mem, info.mem_size + info.str_size)) != info.checksum) {
19111921
zend_accel_error(ACCEL_LOG_WARNING, "corrupted file '%s' excepted checksum: 0x%08x actual checksum: 0x%08x\n", filename, info.checksum, actual_checksum);
1912-
zend_file_cache_unlink(filename);
1922+
if (!ZCG(accel_directives).file_cache_read_only) {
1923+
zend_file_cache_unlink(filename);
1924+
}
19131925
zend_arena_release(&CG(arena), checkpoint);
19141926
efree(filename);
19151927
return NULL;
@@ -2001,6 +2013,10 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
20012013

20022014
void zend_file_cache_invalidate(zend_string *full_path)
20032015
{
2016+
if (ZCG(accel_directives).file_cache_read_only) {
2017+
return;
2018+
}
2019+
20042020
char *filename;
20052021

20062022
filename = zend_file_cache_get_bin_file_path(full_path);

php.ini-development

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,15 @@ ldap.max_links = -1
17661766
; SHM reset. The default "" disables file based caching.
17671767
;opcache.file_cache=
17681768

1769+
; Enables or disables read-only mode for the second level cache directory.
1770+
; It should improve performance for read-only containers,
1771+
; when the cache is pre-warmed and packaged alongside the application.
1772+
; Best used with `opcache.validate_timestamps=0`, `opcache.enable_file_override=1`
1773+
; and `opcache.file_cache_consistency_checks=0`.
1774+
; Note: A cache generated with a different build of PHP, a different file path,
1775+
; or different settings (including which extensions are loaded), may be ignored.
1776+
;opcache.file_cache_read_only=0
1777+
17691778
; Enables or disables opcode caching in shared memory.
17701779
;opcache.file_cache_only=0
17711780

php.ini-production

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,15 @@ ldap.max_links = -1
17681768
; SHM reset. The default "" disables file based caching.
17691769
;opcache.file_cache=
17701770

1771+
; Enables or disables read-only mode for the second level cache directory.
1772+
; It should improve performance for read-only containers,
1773+
; when the cache is pre-warmed and packaged alongside the application.
1774+
; Best used with `opcache.validate_timestamps=0`, `opcache.enable_file_override=1`
1775+
; and `opcache.file_cache_consistency_checks=0`.
1776+
; Note: A cache generated with a different build of PHP, a different file path,
1777+
; or different settings (including which extensions are loaded), may be ignored.
1778+
;opcache.file_cache_read_only=0
1779+
17711780
; Enables or disables opcode caching in shared memory.
17721781
;opcache.file_cache_only=0
17731782

0 commit comments

Comments
 (0)