Skip to content

Commit 8007ecd

Browse files
committed
opcache-file-permission-setting
1 parent eaa6325 commit 8007ecd

6 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎ext/opcache/ZendAccelerator.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ typedef struct _zend_accel_directives {
176176
char *lockfile_path;
177177
#endif
178178
char *file_cache;
179+
mode_t file_cache_permissions;
179180
bool file_cache_read_only;
180181
bool file_cache_only;
181182
bool file_cache_consistency_checks;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
opcache.file_cache_permissions
3+
--EXTENSIONS--
4+
opcache
5+
--SKIPIF--
6+
<?php
7+
$fileCache = sys_get_temp_dir() . '/9664b02e-6f64-42dd-b088-99f3dccc3422';
8+
@mkdir($fileCache, 0777, true);
9+
?>
10+
--INI--
11+
opcache.enable="1"
12+
opcache.enable_cli="1"
13+
opcache.file_cache="{TMP}/9664b02e-6f64-42dd-b088-99f3dccc3422"
14+
opcache.file_cache_only="1"
15+
opcache.file_update_protection="0"
16+
opcache.file_cache_permissions="0660"
17+
--FILE--
18+
<?php
19+
$fileCache = sys_get_temp_dir() . '/9664b02e-6f64-42dd-b088-99f3dccc3422';
20+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fileCache, FilesystemIterator::SKIP_DOTS));
21+
foreach ($iterator as $splFileInfo) {
22+
if (!$splFileInfo->isFile()) {
23+
continue;
24+
}
25+
if (str_ends_with($splFileInfo->getPathname(), '.bin')) {
26+
var_dump(sprintf('%o', fileperms($splFileInfo->getPathname()) & 0o777));
27+
break;
28+
}
29+
}
30+
?>
31+
--EXPECT--
32+
string(3) "660"

‎ext/opcache/zend_accelerator_module.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ ZEND_INI_BEGIN()
320320
#endif
321321

322322
STD_PHP_INI_ENTRY("opcache.file_cache" , NULL , PHP_INI_SYSTEM, OnUpdateFileCache, accel_directives.file_cache, zend_accel_globals, accel_globals)
323+
STD_PHP_INI_ENTRY("opcache.file_cache_permissions", "0600", PHP_INI_SYSTEM, OnUpdateLong, accel_directives.file_cache_permissions, zend_accel_globals, accel_globals)
323324
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)
324325
STD_PHP_INI_BOOLEAN("opcache.file_cache_only" , "0" , PHP_INI_SYSTEM, OnUpdateBool, accel_directives.file_cache_only, zend_accel_globals, accel_globals)
325326
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)
@@ -845,6 +846,7 @@ ZEND_FUNCTION(opcache_get_configuration)
845846
#endif
846847

847848
add_assoc_string(&directives, "opcache.file_cache", ZCG(accel_directives).file_cache ? ZCG(accel_directives).file_cache : "");
849+
add_assoc_long(&directives, "opcache.file_cache_permissions", ZCG(accel_directives).file_cache_permissions);
848850
add_assoc_bool(&directives, "opcache.file_cache_read_only", ZCG(accel_directives).file_cache_read_only);
849851
add_assoc_bool(&directives, "opcache.file_cache_only", ZCG(accel_directives).file_cache_only);
850852
add_assoc_bool(&directives, "opcache.file_cache_consistency_checks", ZCG(accel_directives).file_cache_consistency_checks);

‎ext/opcache/zend_file_cache.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,14 +1171,19 @@ int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm)
11711171
return FAILURE;
11721172
}
11731173

1174-
fd = zend_file_cache_open(filename, O_CREAT | O_EXCL | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR);
1174+
fd = zend_file_cache_open(filename, O_CREAT | O_EXCL | O_RDWR | O_BINARY, ZCG(accel_directives).file_cache_permissions);
11751175
if (fd < 0) {
11761176
if (errno != EEXIST) {
11771177
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot create file '%s', %s\n", filename, strerror(errno));
11781178
}
11791179
efree(filename);
11801180
return FAILURE;
11811181
}
1182+
#ifndef ZEND_WIN32
1183+
if (fchmod(fd, ZCG(accel_directives).file_cache_permissions) == -1) {
1184+
zend_accel_error(ACCEL_LOG_WARNING, "Unable to change opcache file permissions in %s: %s (%d)", filename, strerror(errno), errno);
1185+
}
1186+
#endif
11821187

11831188
if (zend_file_cache_flock(fd, LOCK_EX) != 0) {
11841189
close(fd);

‎php.ini-development‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,9 @@ ldap.max_links = -1
17771777
; SHM reset. The default "" disables file based caching.
17781778
;opcache.file_cache=
17791779

1780+
; The permissions of the files cached.
1781+
;opcache.file_cache_permissions=0600
1782+
17801783
; Enables or disables read-only mode for the second level cache directory.
17811784
; It should improve performance for read-only containers,
17821785
; when the cache is pre-warmed and packaged alongside the application.

‎php.ini-production‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,9 @@ ldap.max_links = -1
17791779
; SHM reset. The default "" disables file based caching.
17801780
;opcache.file_cache=
17811781

1782+
; The permissions of the files cached.
1783+
;opcache.file_cache_permissions=0600
1784+
17821785
; Enables or disables read-only mode for the second level cache directory.
17831786
; It should improve performance for read-only containers,
17841787
; when the cache is pre-warmed and packaged alongside the application.

0 commit comments

Comments
 (0)