Skip to content

Commit 32290b3

Browse files
committed
Add opcache_preloading() internal function
Add a C function, opcache_preloading(), that returns true during preloading. Extensions can use this to detect preloading, not only during compilation/execution, but also in RINIT()/RSHUTDOWN(). Since opcache currently doesn't install any header, I'm adding a new one: zend_accelerator_api.h. Header name is based on other files in ext/opcache. Closes GH-19288
1 parent 3088d64 commit 32290b3

12 files changed

+124
-1
lines changed

ext/opcache/ZendAccelerator.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4845,6 +4845,8 @@ static zend_result accel_finish_startup_preload(bool in_child)
48454845
bool old_reset_signals = SIGG(reset);
48464846
#endif
48474847

4848+
ZCG(preloading) = true;
4849+
48484850
sapi_module.activate = NULL;
48494851
sapi_module.deactivate = NULL;
48504852
sapi_module.register_server_variables = NULL;
@@ -4926,6 +4928,8 @@ static zend_result accel_finish_startup_preload(bool in_child)
49264928
sapi_module.ub_write = orig_ub_write;
49274929
sapi_module.flush = orig_flush;
49284930

4931+
ZCG(preloading) = false;
4932+
49294933
sapi_activate();
49304934

49314935
return ret;

ext/opcache/ZendAccelerator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ typedef struct _zend_accel_globals {
220220
#endif
221221
void *preloaded_internal_run_time_cache;
222222
size_t preloaded_internal_run_time_cache_size;
223+
bool preloading;
223224
/* preallocated shared-memory block to save current script */
224225
void *mem;
225226
zend_persistent_script *current_persistent_script;

ext/opcache/config.m4

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ PHP_NEW_EXTENSION([opcache], m4_normalize([
325325
shared_alloc_mmap.c
326326
shared_alloc_posix.c
327327
shared_alloc_shm.c
328+
zend_accelerator_api.c
328329
zend_accelerator_blacklist.c
329330
zend_accelerator_debug.c
330331
zend_accelerator_hash.c
@@ -361,3 +362,5 @@ AS_VAR_IF([PHP_OPCACHE_JIT], [yes], [
361362
])
362363
PHP_ADD_MAKEFILE_FRAGMENT([$ext_srcdir/jit/Makefile.frag])
363364
])
365+
366+
PHP_INSTALL_HEADERS([ext/opcache], [zend_accelerator_api.h])

ext/opcache/config.w32

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ PHP_OPCACHE="yes";
44

55
ZEND_EXTENSION('opcache', "\
66
ZendAccelerator.c \
7+
zend_accelerator_api.c \
78
zend_accelerator_blacklist.c \
89
zend_accelerator_debug.c \
910
zend_accelerator_hash.c \
@@ -68,3 +69,5 @@ if (PHP_OPCACHE_JIT == "yes") {
6869
}
6970

7071
ADD_FLAG('CFLAGS_OPCACHE', "/I " + configure_module_dirname);
72+
73+
PHP_INSTALL_HEADERS("ext/opcache", "zend_accelerator_api.h");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
printf("%s: %d\n", __FILE__, zend_test_opcache_preloading());
3+
?>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
opcache_preloading() api 001
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.preload={PWD}/opcache_preloading.inc
7+
--SKIPIF--
8+
<?php
9+
if (PHP_OS_FAMILY == 'Windows') die('skip Preloading is not supported on Windows');
10+
?>
11+
--FILE--
12+
<?php
13+
14+
printf("%s: %d\n", __FILE__, zend_test_opcache_preloading());
15+
16+
?>
17+
--EXPECTF--
18+
%sopcache_preloading.inc: 1
19+
%sopcache_preloading_001.php: 0
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
opcache_preloading() api 002
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.preload={PWD}/opcache_preloading.inc
7+
opcache.preload_user={ENV:TEST_NON_ROOT_USER}
8+
--EXTENSIONS--
9+
posix
10+
--SKIPIF--
11+
<?php
12+
if (PHP_OS_FAMILY == 'Windows') die('skip Preloading is not supported on Windows');
13+
if (posix_geteuid() !== 0) die('skip Test needs root user');
14+
?>
15+
--FILE--
16+
<?php
17+
18+
printf("%s: %d\n", __FILE__, zend_test_opcache_preloading());
19+
20+
?>
21+
--EXPECTF--
22+
%sopcache_preloading.inc: 1
23+
%sopcache_preloading_002.php: 0

ext/opcache/zend_accelerator_api.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Zend OPcache |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| https://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include "zend_accelerator_api.h"
18+
#include "ZendAccelerator.h"
19+
20+
ZEND_API bool opcache_preloading(void)
21+
{
22+
return ZCG(preloading);
23+
}

ext/opcache/zend_accelerator_api.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Zend OPcache |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| https://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef ZEND_ACCELERATOR_API_H
18+
#define ZEND_ACCELERATOR_API_H
19+
20+
#include "Zend/zend_portability.h"
21+
22+
BEGIN_EXTERN_C()
23+
24+
/* Returns true during preloading */
25+
ZEND_API bool opcache_preloading(void);
26+
27+
END_EXTERN_C()
28+
29+
#endif /* ZEND_ACCELERATOR_API_H */

ext/zend_test/test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
+----------------------------------------------------------------------+
1515
*/
1616

17+
#include "ext/opcache/zend_accelerator_api.h"
18+
#include "zend_API.h"
1719
#include "zend_modules.h"
1820
#include "zend_types.h"
1921
#ifdef HAVE_CONFIG_H
@@ -1645,3 +1647,10 @@ static PHP_FUNCTION(zend_test_gh18756)
16451647
zend_mm_gc(heap);
16461648
zend_mm_shutdown(heap, true, false);
16471649
}
1650+
1651+
static PHP_FUNCTION(zend_test_opcache_preloading)
1652+
{
1653+
ZEND_PARSE_PARAMETERS_NONE();
1654+
1655+
RETURN_BOOL(opcache_preloading());
1656+
}

0 commit comments

Comments
 (0)