<?php
declare(strict_types=1);
$lane=$argv[1]??'';$mode=$argv[2]??'';if(!in_array($lane,['B','C'],true)||!in_array($mode,['preflight','readiness','auth','launch','fetch','test-gate','cleanup'],true))exit(78);
$pid=$lane==='B'?10001:112175;$root=$lane==='B'?'/tmp/zhct-http-store-mysql.dQTTBP':'/tmp/zhct-dm-poc-20260928.fcpiiN/store';if(strpos((string)file_get_contents('/proc/'.$pid.'/cmdline'),$root.'/public')===false)throw new RuntimeException('Original parent identity');
$allowed=['PATH','LANG','LC_ALL','LC_CTYPE','TZ','NLS_LANG','DM_CHARSET','DM_SVC_PATH','DM_HOME','LD_LIBRARY_PATH','POC_HOST','POC_PORT','POC_SCHEMA','POC_USER','POC_PASSWORD','POC_DIALECT','POC_PREFIX','POC_CHARSET','POC_EXTERNAL_STUBS','POC_REAL_QUEUE'];$env=[];foreach(explode("\0",(string)file_get_contents('/proc/'.$pid.'/environ'))as $pair){$p=explode('=',$pair,2);if(count($p)===2&&in_array($p[0],$allowed,true))$env[$p[0]]=$p[1];}
if(($env['POC_EXTERNAL_STUBS']??'')!=='1'||($env['POC_REAL_QUEUE']??'0')!=='0')throw new RuntimeException('External isolation');if($lane==='C'&&(($env['POC_HOST']??'')!=='127.0.0.1'||($env['POC_PORT']??'')!=='5237'||($env['POC_SCHEMA']??'')!=='ZHCT_APP'))throw new RuntimeException('Exact C nonprod');$env['NF_LANE']=$lane;$env['NF_ROOT']=$root;$env['POC_REAL_QUEUE']='0';
$args=[$lane==='B'?'/usr/local/php7.3/bin/php':'/www/server/php/73/bin/php'];if($lane==='C')array_push($args,'-c','/www/server/php/73/etc/php-cli.ini','-d','extension=/home/dmdba/dmdbms/drivers/php_pdo/libphp73_dm.so','-d','extension=/home/dmdba/dmdbms/drivers/php_pdo/php73_pdo_dm.so');array_push($args,'-d','display_errors=0','-d','log_errors=0');
if($mode==='launch'){$port=$lane==='B'?18921:18922;if(is_file(__DIR__.'/private/worker-pid.json'))throw new RuntimeException('Worker no replay');if($s=@fsockopen('127.0.0.1',$port,$n,$e,0.5)){fclose($s);throw new RuntimeException('Port collision');}array_push($args,'-S','127.0.0.1:'.$port,'-t',__DIR__,__DIR__.'/index.php');$cmd='nohup '.implode(' ',array_map('escapeshellarg',$args)).' </dev/null >/dev/null 2>&1 & echo $!';$descriptors=[1=>['pipe','w'],2=>['file',__DIR__.'/private/error.log','a']];}
else{array_push($args,__DIR__.'/'.$mode.'.php');$cmd=implode(' ',array_map('escapeshellarg',$args));$descriptors=[1=>STDOUT,2=>['file',__DIR__.'/private/error.log','a']];}
$p=proc_open($cmd,$descriptors,$pipes,null,$env);if($mode==='launch'){$worker=(int)trim(stream_get_contents($pipes[1]));fclose($pipes[1]);if($worker<2)throw new RuntimeException('Worker PID');file_put_contents(__DIR__.'/private/worker-pid.json',json_encode(['pid'=>$worker,'port'=>$port,'router'=>__DIR__.'/index.php']));chmod(__DIR__.'/private/worker-pid.json',0600);echo json_encode(['lane'=>$lane,'own_worker'=>$worker,'port'=>$port]),"\n";}$code=proc_close($p);if(is_file(__DIR__.'/private/error.log'))chmod(__DIR__.'/private/error.log',0600);exit($code);

