-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathtest_no_zombie.php
More file actions
73 lines (57 loc) · 2.15 KB
/
test_no_zombie.php
File metadata and controls
73 lines (57 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
require_once __DIR__."/includes/autoload.php";
skip_if_php5();
$telemetryLogPath = tempnam(sys_get_temp_dir(), 'test_loader_');
// Build the command to run PHP with the loader
$cmd = sprintf(
'FAKE_FORWARDER_LOG_PATH=%s DD_TELEMETRY_FORWARDER_PATH=%s php -n -dzend_extension=%s -r "sleep(1);"',
escapeshellarg($telemetryLogPath),
escapeshellarg(__DIR__.'/../../bin/fake_forwarder.sh'),
escapeshellarg(getLoaderAbsolutePath())
);
try {
// Start the PHP process in background and get its PID
$descriptors = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
];
$process = proc_open($cmd . ' & echo $!', $descriptors, $pipes);
if (!is_resource($process)) {
throw new \Exception("Failed to start PHP process");
}
// Get the real PHP PID from the output
$firstLine = fgets($pipes[1]);
$phpPid = (int)trim($firstLine);
if ($phpPid <= 0) {
throw new \Exception("Failed to get PHP PID");
}
if (debug()) {
echo "[debug] PHP PID: $phpPid\n";
}
// Wait for the telemetry fork to happen and complete
usleep(300000); // 300ms
// Check for zombie processes that are children of the PHP process
$zombieCheckCmd = sprintf('ps --ppid %d -o pid,state,comm --no-headers 2>/dev/null || echo "NO_CHILDREN"', $phpPid);
$zombieOutput = shell_exec($zombieCheckCmd);
if (debug()) {
echo "[debug] Children processes:\n" . $zombieOutput . "\n";
}
$zombieCount = substr_count($zombieOutput, ' Z ');
// Wait for the PHP process to finish
$waitCmd = sprintf('wait %d 2>/dev/null; echo $?', $phpPid);
$phpExitCode = (int)trim(shell_exec($waitCmd));
// Read the remaining output and close pipes
$output = stream_get_contents($pipes[1]);
$errors = stream_get_contents($pipes[2]);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
if ($zombieCount > 0) {
throw new \Exception("FAILED: Zombie process detected after telemetry fork! Found $zombieCount zombie(s)");
}
echo "OK: No zombie processes detected\n";
} finally {
@unlink($telemetryLogPath);
}