<?php
require __DIR__.'/common.php';
$pid=$lane==='B'?7620:2294827;$port=$lane==='B'?18891:18892;$parent=$lane==='B'?10001:112175;$cmd=(string)@file_get_contents('/proc/'.$pid.'/cmdline');
if(strpos($cmd,__DIR__.'/index.php')===false||strpos($cmd,'127.0.0.1:'.$port)===false)throw new RuntimeException('Exact own worker mismatch');
$lock=json_decode(file_get_contents(__DIR__.'/source-lock.json'),true);$verified=0;foreach($lock as $f=>$sha){if(hash_file('sha256',__DIR__.'/source/application/'.$f)!==$sha)throw new RuntimeException('Final source SHA drift');$verified++;}
$deps=[];foreach(['thinkphp/library/think/db/connector/Mysql.php','thinkphp/library/think/db/builder/Mysql.php','thinkphp/library/think/db/Connection.php','thinkphp/library/think/db/Query.php','external_stubs.php']as $f)$deps[$f]=hash_file('sha256',$root.'/'.$f);
$extension=[];$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root.'/extend',FilesystemIterator::SKIP_DOTS));foreach($it as $f){if($f->isLink())throw new RuntimeException('Original extend link');if($f->isFile())$extension[substr($f->getPathname(),strlen($root.'/extend/'))]=hash_file('sha256',$f->getPathname());}ksort($extension);
$save('source-provenance.json',['source_php_files'=>$verified,'source_lock_sha256'=>hash_file('sha256',__DIR__.'/source-lock.json'),'dependencies'=>$deps,'readonly_extend'=>$extension,'own_worker_pid'=>$pid,'own_worker_cmdline'=>$cmd,'original_parent_present'=>is_file('/proc/'.$parent.'/cmdline'),'original_parent_cmdline_sha256'=>hash('sha256',(string)file_get_contents('/proc/'.$parent.'/cmdline'))]);
if(!posix_kill($pid,SIGTERM))throw new RuntimeException('Own stop failed');
usleep(200000);$socket=@fsockopen('127.0.0.1',$port,$n,$e,1);$closed=$socket===false;if($socket)fclose($socket);
$apple=[];foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__,FilesystemIterator::SKIP_DOTS))as $f)if(strpos($f->getFilename(),'._')===0)$apple[]=$f->getPathname();
$out=['own_worker_absent'=>!is_file('/proc/'.$pid.'/cmdline')||(string)@file_get_contents('/proc/'.$pid.'/cmdline')==='','own_port_closed'=>$closed,'original_parent_present'=>is_file('/proc/'.$parent.'/cmdline'),'request_absent'=>!is_file(__DIR__.'/private/request.json'),'cache_absent'=>!is_dir(__DIR__.'/cache'),'AppleDouble'=>$apple];$save('resource-check.json',$out);echo json_encode($out),"\n";
