<?php
declare(strict_types=1);

// Recover only the approved isolated runtime's connection environment in memory.
// Never print or persist environment values.
$pid = (string)getenv('POC_RUNTIME_PID');
$script = $argv[1] ?? '';
if (!ctype_digit($pid) || !is_file('/proc/' . $pid . '/environ')
    || !is_file($script) || strpos(realpath($script), '/tmp/zhct-') !== 0) {
    fwrite(STDERR, "Invalid isolated runtime or script\n");
    exit(2);
}
$allowed = ['DM_SVC_PATH', 'LD_LIBRARY_PATH'];
foreach (explode("\0", (string)file_get_contents('/proc/' . $pid . '/environ')) as $entry) {
    $pair = explode('=', $entry, 2);
    if (count($pair) !== 2) continue;
    if (strpos($pair[0], 'POC_') !== 0 && !in_array($pair[0], $allowed, true)) continue;
    if (getenv($pair[0]) === false) putenv($pair[0] . '=' . $pair[1]);
}
require $script;
