Skip to content

Commit a19267d

Browse files
committed
Fix GH-11086: FPM: config test runs twice in daemonised mode
The previous check for STDERR did not work so this fixes it. Closes GH-13357
1 parent 72197e3 commit a19267d

File tree

6 files changed

+66
-10
lines changed

6 files changed

+66
-10
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ PHP NEWS
88
. Fixed bug GH-13612 (Corrupted memory in destructor with weak references).
99
(nielsdos)
1010

11+
- FPM:
12+
. Fixed GH-11086 (FPM: config test runs twice in daemonised mode).
13+
(Jakub Zelenka)
14+
1115
- Gettext:
1216
. Fixed sigabrt raised with dcgettext/dcngettext calls with gettext 0.22.5
1317
with category set to LC_ALL. (David Carlier)

sapi/fpm/fpm/fpm_stdio.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ int fpm_stdio_init_child(struct fpm_worker_pool_s *wp) /* {{{ */
153153
close(fpm_globals.error_log_fd);
154154
}
155155
fpm_globals.error_log_fd = -1;
156-
zlog_set_fd(-1);
156+
zlog_set_fd(-1, 0);
157157

158158
return 0;
159159
}
@@ -374,13 +374,14 @@ int fpm_stdio_open_error_log(int reopen) /* {{{ */
374374
php_openlog(fpm_global_config.syslog_ident, LOG_PID | LOG_CONS, fpm_global_config.syslog_facility);
375375
fpm_globals.error_log_fd = ZLOG_SYSLOG;
376376
if (fpm_use_error_log()) {
377-
zlog_set_fd(fpm_globals.error_log_fd);
377+
zlog_set_fd(fpm_globals.error_log_fd, 0);
378378
}
379379
return 0;
380380
}
381381
#endif
382382

383383
fd = open(fpm_global_config.error_log, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
384+
384385
if (0 > fd) {
385386
zlog(ZLOG_SYSERROR, "failed to open error_log (%s)", fpm_global_config.error_log);
386387
return -1;
@@ -393,7 +394,11 @@ int fpm_stdio_open_error_log(int reopen) /* {{{ */
393394
} else {
394395
fpm_globals.error_log_fd = fd;
395396
if (fpm_use_error_log()) {
396-
zlog_set_fd(fpm_globals.error_log_fd);
397+
bool is_stderr = (
398+
strcmp(fpm_global_config.error_log, "/dev/stderr") == 0 ||
399+
strcmp(fpm_global_config.error_log, "/proc/self/fd/2") == 0
400+
);
401+
zlog_set_fd(fpm_globals.error_log_fd, is_stderr);
397402
}
398403
}
399404
if (0 > fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC)) {

sapi/fpm/fpm/zlog.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define EXTRA_SPACE_FOR_PREFIX 128
2626

2727
static int zlog_fd = -1;
28+
static bool zlog_fd_is_stderr = false;
2829
static int zlog_level = ZLOG_NOTICE;
2930
static int zlog_limit = ZLOG_DEFAULT_LIMIT;
3031
static zlog_bool zlog_buffering = ZLOG_DEFAULT_BUFFERING;
@@ -88,11 +89,13 @@ size_t zlog_print_time(struct timeval *tv, char *timebuf, size_t timebuf_len) /*
8889
}
8990
/* }}} */
9091

91-
int zlog_set_fd(int new_fd) /* {{{ */
92+
int zlog_set_fd(int new_fd, zlog_bool is_stderr) /* {{{ */
9293
{
9394
int old_fd = zlog_fd;
9495

9596
zlog_fd = new_fd;
97+
zlog_fd_is_stderr = is_stderr;
98+
9699
return old_fd;
97100
}
98101
/* }}} */
@@ -244,7 +247,7 @@ void vzlog(const char *function, int line, int flags, const char *fmt, va_list a
244247
zend_quiet_write(zlog_fd > -1 ? zlog_fd : STDERR_FILENO, buf, len);
245248
}
246249

247-
if (zlog_fd != STDERR_FILENO && zlog_fd != -1 &&
250+
if (!zlog_fd_is_stderr && zlog_fd != -1 &&
248251
!launched && (flags & ZLOG_LEVEL_MASK) >= ZLOG_NOTICE) {
249252
zend_quiet_write(STDERR_FILENO, buf, len);
250253
}

sapi/fpm/fpm/zlog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ typedef unsigned char zlog_bool;
1717
#define ZLOG_FALSE 0
1818

1919
void zlog_set_external_logger(void (*logger)(int, char *, size_t));
20-
int zlog_set_fd(int new_fd);
20+
int zlog_set_fd(int new_fd, zlog_bool is_stderr);
2121
int zlog_set_level(int new_value);
2222
int zlog_set_limit(int new_value);
2323
int zlog_set_buffering(zlog_bool buffering);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
FPM: gh68591 - daemonized mode duplicated logs
3+
--SKIPIF--
4+
<?php
5+
include "skipif.inc";
6+
?>
7+
--FILE--
8+
<?php
9+
10+
require_once "tester.inc";
11+
12+
$cfg = <<<EOT
13+
[global]
14+
error_log = /dev/stderr
15+
daemonize = true
16+
[unconfined]
17+
listen = {{ADDR}}
18+
pm = static
19+
pm.max_children = 1
20+
EOT;
21+
22+
$tester = new FPM\Tester($cfg);
23+
$tester->testConfig(dumpConfig: false, printOutput: true);
24+
25+
?>
26+
Done
27+
--EXPECTF--
28+
%sNOTICE: configuration file %s test is successful
29+
Done
30+
--CLEAN--
31+
<?php
32+
require_once "tester.inc";
33+
FPM\Tester::clean();
34+
?>

sapi/fpm/tests/tester.inc

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,22 @@ class Tester
440440
* @return null|array
441441
* @throws \Exception
442442
*/
443-
public function testConfig($silent = false, array|string|null $expectedPattern = null): ?array
444-
{
445-
$configFile = $this->createConfig();
446-
$cmd = self::findExecutable() . ' -n -tt -y ' . $configFile . ' 2>&1';
443+
public function testConfig(
444+
$silent = false,
445+
array|string|null $expectedPattern = null,
446+
$dumpConfig = true,
447+
$printOutput = false
448+
): ?array {
449+
$configFile = $this->createConfig();
450+
$configTestArg = $dumpConfig ? '-tt' : '-t';
451+
$cmd = self::findExecutable() . " -n $configTestArg -y $configFile 2>&1";
447452
$this->trace('Testing config using command', $cmd, true);
448453
exec($cmd, $output, $code);
454+
if ($printOutput) {
455+
foreach ($output as $outputLine) {
456+
echo $outputLine . "\n";
457+
}
458+
}
449459
$found = 0;
450460
if ($expectedPattern !== null) {
451461
$expectedPatterns = is_array($expectedPattern) ? $expectedPattern : [$expectedPattern];

0 commit comments

Comments
 (0)