Skip to content

Commit 0148d3b

Browse files
committed
ext/opcache/zend_shared_alloc: use memfd for locking if available
A memfd is a virtual file that has no reachable path, therefore does not clobber any filesystem. It is deleted automatically as soon as the last handle gets closed. The feature is available since Linux kernel 3.17.
1 parent 9f5c391 commit 0148d3b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

ext/opcache/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ if test "$PHP_OPCACHE" != "no"; then
105105

106106
fi
107107

108-
AC_CHECK_FUNCS([mprotect])
108+
AC_CHECK_FUNCS([mprotect memfd_create])
109109

110110
AC_MSG_CHECKING(for sysvipc shared memory support)
111111
AC_RUN_IFELSE([AC_LANG_SOURCE([[

ext/opcache/zend_shared_alloc.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
+----------------------------------------------------------------------+
2020
*/
2121

22+
#ifdef HAVE_MEMFD_CREATE
23+
# ifndef _GNU_SOURCE
24+
# define _GNU_SOURCE
25+
# endif
26+
# include <sys/mman.h>
27+
#endif
28+
2229
#include <errno.h>
2330
#include "ZendAccelerator.h"
2431
#include "zend_shared_alloc.h"
@@ -81,6 +88,12 @@ void zend_shared_alloc_create_lock(char *lockfile_path)
8188
zts_lock = tsrm_mutex_alloc();
8289
#endif
8390

91+
#ifdef HAVE_MEMFD_CREATE
92+
lock_file = memfd_create("opcache_lock", MFD_CLOEXEC);
93+
if (lock_file >= 0)
94+
return;
95+
#endif
96+
8497
snprintf(lockfile_name, sizeof(lockfile_name), "%s/%sXXXXXX", lockfile_path, SEM_FILENAME_PREFIX);
8598
lock_file = mkstemp(lockfile_name);
8699
if (lock_file == -1) {

0 commit comments

Comments
 (0)