Skip to content

Commit a3783d6

Browse files
committed
opcache: support file_cache_read_only (#16484)
1 parent 7f6ad37 commit a3783d6

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

ext/opcache/ZendAccelerator.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,6 +3300,39 @@ static zend_result accel_post_startup(void)
33003300
#endif
33013301
accel_shared_globals = calloc(1, sizeof(zend_accel_shared_globals));
33023302
}
3303+
3304+
/* opcache.file_cache_read_only should only be enabled when all script files are read-only */
3305+
if (ZCG(accel_directives).file_cache_read_only) {
3306+
if (!ZCG(accel_directives).file_cache) {
3307+
accel_startup_ok = false;
3308+
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "opcache.file_cache_read_only is set without a proper setting of opcache.file_cache");
3309+
return SUCCESS;
3310+
}
3311+
if (ZCG(accel_directives).revalidate_freq != 0) {
3312+
accel_startup_ok = false;
3313+
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "opcache.file_cache_read_only cannot be enabled when opcache.revalidate_freq is not 0.");
3314+
return SUCCESS;
3315+
}
3316+
if (ZCG(accel_directives).validate_timestamps) {
3317+
accel_startup_ok = false;
3318+
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "opcache.file_cache_read_only cannot be enabled when opcache.validate_timestamps is enabled.");
3319+
return SUCCESS;
3320+
}
3321+
} else {
3322+
/* opcache.file_cache isn't read only, so ensure the directory is writable */
3323+
if (
3324+
#ifndef ZEND_WIN32
3325+
access(ZCG(accel_directives).file_cache, R_OK | W_OK | X_OK) != 0
3326+
#else
3327+
_access(ZCG(accel_directives).file_cache, 06) != 0
3328+
#endif
3329+
) {
3330+
accel_startup_ok = false;
3331+
zend_accel_error_noreturn(ACCEL_LOG_FATAL, "opcache.file_cache must be a full path of accessible, writable directory");
3332+
return SUCCESS;
3333+
}
3334+
}
3335+
33033336
#if ENABLE_FILE_CACHE_FALLBACK
33043337
file_cache_fallback:
33053338
#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)