Skip to content

Commit 5979b4d

Browse files
henderkesdunglas
andauthored
fix php startup errors when ini files contain environment variables (#2252)
closes #2250 --------- Signed-off-by: Marc <m@pyc.ac> Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
1 parent a85faae commit 5979b4d

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

frankenphp.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,13 @@ static void *php_main(void *arg) {
11351135

11361136
frankenphp_init_interned_strings();
11371137

1138+
/* take a snapshot of the environment for sandboxing */
1139+
if (main_thread_env == NULL) {
1140+
main_thread_env = pemalloc(sizeof(HashTable), 1);
1141+
zend_hash_init(main_thread_env, 8, NULL, NULL, 1);
1142+
go_init_os_env(main_thread_env);
1143+
}
1144+
11381145
frankenphp_sapi_module.startup(&frankenphp_sapi_module);
11391146

11401147
/* check if a default filter is set in php.ini and only filter if
@@ -1144,13 +1151,6 @@ static void *php_main(void *arg) {
11441151
should_filter_var = default_filter != NULL;
11451152
original_user_abort_setting = PG(ignore_user_abort);
11461153

1147-
/* take a snapshot of the environment for sandboxing */
1148-
if (main_thread_env == NULL) {
1149-
main_thread_env = pemalloc(sizeof(HashTable), 1);
1150-
zend_hash_init(main_thread_env, 8, NULL, NULL, 1);
1151-
go_init_os_env(main_thread_env);
1152-
}
1153-
11541154
go_frankenphp_main_thread_is_ready();
11551155

11561156
/* channel closed, shutdown gracefully */

frankenphp_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ func testHelloWorld(t *testing.T, opts *testOptions) {
160160
}, opts)
161161
}
162162

163+
func TestEnvVarsInPhpIni(t *testing.T) {
164+
t.Setenv("OPCACHE_ENABLE", "0")
165+
166+
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) {
167+
body, _ := testGet("http://example.com/ini.php?key=opcache.enable", handler, t)
168+
assert.Equal(t, "opcache.enable:0", body)
169+
}, &testOptions{
170+
phpIni: map[string]string{
171+
"opcache.enable": "${OPCACHE_ENABLE}",
172+
},
173+
})
174+
}
175+
163176
func TestFinishRequest_module(t *testing.T) { testFinishRequest(t, nil) }
164177
func TestFinishRequest_worker(t *testing.T) {
165178
testFinishRequest(t, &testOptions{workerScript: "finish-request.php"})

phpmainthread_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,11 @@ func TestReturnAnErrorIf2WorkersHaveTheSameFileName(t *testing.T) {
188188
workersByName = map[string]*worker{}
189189
workersByPath = map[string]*worker{}
190190
w, err1 := newWorker(workerOpt{fileName: testDataPath + "/index.php"})
191+
assert.NoError(t, err1)
191192
workers = append(workers, w)
192193
workersByName[w.name] = w
193194
workersByPath[w.fileName] = w
194195
_, err2 := newWorker(workerOpt{fileName: testDataPath + "/index.php"})
195-
196-
assert.NoError(t, err1)
197196
assert.Error(t, err2, "two workers cannot have the same filename")
198197
}
199198

@@ -202,12 +201,11 @@ func TestReturnAnErrorIf2ModuleWorkersHaveTheSameName(t *testing.T) {
202201
workersByName = map[string]*worker{}
203202
workersByPath = map[string]*worker{}
204203
w, err1 := newWorker(workerOpt{fileName: testDataPath + "/index.php", name: "workername"})
204+
assert.NoError(t, err1)
205205
workers = append(workers, w)
206206
workersByName[w.name] = w
207207
workersByPath[w.fileName] = w
208208
_, err2 := newWorker(workerOpt{fileName: testDataPath + "/hello.php", name: "workername"})
209-
210-
assert.NoError(t, err1)
211209
assert.Error(t, err2, "two workers cannot have the same name")
212210
}
213211

0 commit comments

Comments
 (0)