Skip to content

Commit 37995c0

Browse files
iamacarpetarnaud-lb
authored andcommitted
Add opcache.file_cache_read_only
Closes GH-16551
1 parent afa08b5 commit 37995c0

File tree

6 files changed

+60
-5
lines changed

6 files changed

+60
-5
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ PHP NEWS
2222

2323
- OPcache:
2424
. Fixed ZTS OPcache build on Cygwin. (cmb)
25+
. Added opcache.file_cache_read_only. (Samuel Melrose)
2526

2627
- Output:
2728
. Fixed calculation of aligned buffer size. (cmb)

UPGRADING

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ PHP 8.5 UPGRADE NOTES
143143
11. Changes to INI File Handling
144144
========================================
145145

146+
- Opcache:
147+
. Added opcache.file_cache_read_only to support a read-only
148+
opcache.file_cache directory, for use with read-only file systems
149+
(e.g. read-only Docker containers).
150+
Best used with opcache.validate_timestamps=0,
151+
opcache.enable_file_override=1,
152+
and opcache.file_cache_consistency_checks=0.
153+
Note: A cache generated with a different build of PHP, a different file
154+
path, or different settings (including which extensions are loaded), may be
155+
ignored.
156+
146157
========================================
147158
12. Windows Support
148159
========================================

ext/opcache/ZendAccelerator.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,9 @@ zend_result zend_accel_invalidate(zend_string *filename, bool force)
14111411
}
14121412

14131413
if (ZCG(accel_directives).file_cache) {
1414+
if (ZCG(accel_directives).file_cache_read_only) {
1415+
return FAILURE;
1416+
}
14141417
zend_file_cache_invalidate(realpath);
14151418
}
14161419

@@ -3301,6 +3304,39 @@ static zend_result accel_post_startup(void)
33013304
#endif
33023305
accel_shared_globals = calloc(1, sizeof(zend_accel_shared_globals));
33033306
}
3307+
3308+
/* opcache.file_cache_read_only should only be enabled when all script files are read-only */
3309+
if (ZCG(accel_directives).file_cache_read_only) {
3310+
if (!ZCG(accel_directives).file_cache) {
3311+
accel_startup_ok = false;
3312+
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "opcache.file_cache_read_only is set without a proper setting of opcache.file_cache");
3313+
return SUCCESS;
3314+
}
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+
}
3325+
} else {
3326+
/* opcache.file_cache isn't read only, so ensure the directory is writable */
3327+
if ( ZCG(accel_directives).file_cache &&
3328+
#ifndef ZEND_WIN32
3329+
access(ZCG(accel_directives).file_cache, R_OK | W_OK | X_OK) != 0
3330+
#else
3331+
_access(ZCG(accel_directives).file_cache, 06) != 0
3332+
#endif
3333+
) {
3334+
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");
3336+
return SUCCESS;
3337+
}
3338+
}
3339+
33043340
#if ENABLE_FILE_CACHE_FALLBACK
33053341
file_cache_fallback:
33063342
#endif

ext/opcache/ZendAccelerator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ typedef struct _zend_accel_directives {
177177
char *lockfile_path;
178178
#endif
179179
char *file_cache;
180+
bool file_cache_read_only;
180181
bool file_cache_only;
181182
bool file_cache_consistency_checks;
182183
#if ENABLE_FILE_CACHE_FALLBACK

ext/opcache/zend_accelerator_module.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ static ZEND_INI_MH(OnUpdateFileCache)
172172
zend_stat(ZSTR_VAL(new_value), &buf) != 0 ||
173173
!S_ISDIR(buf.st_mode) ||
174174
#ifndef ZEND_WIN32
175-
access(ZSTR_VAL(new_value), R_OK | W_OK | X_OK) != 0) {
175+
access(ZSTR_VAL(new_value), R_OK | X_OK) != 0) {
176176
#else
177-
_access(ZSTR_VAL(new_value), 06) != 0) {
177+
_access(ZSTR_VAL(new_value), 04) != 0) {
178178
#endif
179179
zend_accel_error(ACCEL_LOG_WARNING, "opcache.file_cache must be a full path of accessible directory.\n");
180180
new_value = NULL;
@@ -311,9 +311,10 @@ ZEND_INI_BEGIN()
311311
STD_PHP_INI_ENTRY("opcache.mmap_base", NULL, PHP_INI_SYSTEM, OnUpdateString, accel_directives.mmap_base, zend_accel_globals, accel_globals)
312312
#endif
313313

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_only" , "0" , PHP_INI_SYSTEM, OnUpdateBool, accel_directives.file_cache_only, zend_accel_globals, accel_globals)
316-
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)
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)
317318
#if ENABLE_FILE_CACHE_FALLBACK
318319
STD_PHP_INI_BOOLEAN("opcache.file_cache_fallback" , "1" , PHP_INI_SYSTEM, OnUpdateBool, accel_directives.file_cache_fallback, zend_accel_globals, accel_globals)
319320
#endif
@@ -823,6 +824,7 @@ ZEND_FUNCTION(opcache_get_configuration)
823824
#endif
824825

825826
add_assoc_string(&directives, "opcache.file_cache", ZCG(accel_directives).file_cache ? ZCG(accel_directives).file_cache : "");
827+
add_assoc_bool(&directives, "opcache.file_cache_read_only", ZCG(accel_directives).file_cache_read_only);
826828
add_assoc_bool(&directives, "opcache.file_cache_only", ZCG(accel_directives).file_cache_only);
827829
add_assoc_bool(&directives, "opcache.file_cache_consistency_checks", ZCG(accel_directives).file_cache_consistency_checks);
828830
#if ENABLE_FILE_CACHE_FALLBACK

ext/opcache/zend_file_cache.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,10 @@ int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm)
11001100
}
11011101
#endif
11021102

1103+
if (ZCG(accel_directives).file_cache_read_only) {
1104+
return FAILURE;
1105+
}
1106+
11031107
filename = zend_file_cache_get_bin_file_path(script->script.filename);
11041108

11051109
if (zend_file_cache_mkdir(filename, strlen(ZCG(accel_directives).file_cache)) != SUCCESS) {

0 commit comments

Comments
 (0)