openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 6.55 kB
JavaScript
import{n as e,r as t}from"./openclaw-state-read-process-env-DgVwfN3Y.mjs";import{createRequire as n}from"node:module";import r from"node:fs";import i from"node:path";import a,{spawnSync as o}from"node:child_process";import{ProcSafeError as s}from"@openclaw/proc-safe/errors";import{readProcessIdentity as c}from"@openclaw/proc-safe/identity";const l=`C:\\Windows`;function readNativeProcessStartTime(e){if(!(process.platform!==`win32`||typeof SEALED_RUNTIME_BUILD==`boolean`&&SEALED_RUNTIME_BUILD))try{let t=c(e);return t&&!t.exited?Math.floor(t.startTimeMicros/1e3):null}catch{return}}function windowsSystemRoot(e){let n=t(e,`SystemRoot`,`win32`)??t(e,`WINDIR`,`win32`);if(!n)return l;let r=i.win32.normalize(n);return/^[A-Za-z]:\\/.test(r)&&!r.startsWith(`\\\\`)?r:l}function windowsPowerShellPath(e){return i.win32.join(windowsSystemRoot(e),`System32`,`WindowsPowerShell`,`v1.0`,`powershell.exe`)}function windowsWmicPath(e){return i.win32.join(windowsSystemRoot(e),`System32`,`wbem`,`wmic.exe`)}function decodeWindowsProcessOutput(e){return Buffer.isBuffer(e)?e.length>=2&&e[0]===255&&e[1]===254?e.toString(`utf16le`):e.toString(`utf8`):e}function parseWindowsProcessStartTime(e){let t=decodeWindowsProcessOutput(e).split(/\r?\n/).map(e=>e.trim()).filter(Boolean),n=t.find(e=>e.toLowerCase().startsWith(`creationdate=`))?.slice(13).trim()??t.find(e=>e.toLowerCase()!==`creationdate`)??``,r=Date.parse(n);if(Number.isFinite(r))return r;let i=n.match(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})\.(\d{6})([+-])(\d{3})$/);if(!i)return null;let[,a,o,s,c,l,u,d,f,p]=i;return Date.UTC(Number(a),Number(o)-1,Number(s),Number(c),Number(l),Number(u),Math.floor(Number(d)/1e3))-Number(p)*6e4*(f===`+`?1:-1)}function readWindowsProcessStartTimeSync(t,n=1e4,r=process.env){if(!Number.isInteger(t)||t<=0||t>4294967295)return null;let i=Date.now()+n,a=readNativeProcessStartTime(t);if(a!==void 0)return a;let s=i-Date.now();if(s<=0)return null;let c=o(windowsPowerShellPath(r),[`-NoProfile`,`-NonInteractive`,`-Command`,`$process = [System.Diagnostics.Process]::GetProcessById(${t}); try { [Console]::Out.Write($process.StartTime.ToUniversalTime().ToString("o")) } finally { $process.Dispose() }`],{encoding:`utf8`,env:e(r,`win32`),timeout:Math.min(s,5e3),windowsHide:!0});if(!c.error&&c.status===0){let e=parseWindowsProcessStartTime(c.stdout);if(e!==null)return e}let l=i-Date.now();if(l<=0)return null;let u=o(windowsWmicPath(r),[`process`,`where`,`ProcessId=${t}`,`get`,`CreationDate`,`/value`],{env:e(r,`win32`),timeout:l,windowsHide:!0,stdio:[`ignore`,`pipe`,`ignore`]});return!u.error&&u.status===0?parseWindowsProcessStartTime(u.stdout):null}let u;function loadFreeBsdProcessIdentityNative(){if(typeof SEALED_RUNTIME_BUILD==`boolean`&&SEALED_RUNTIME_BUILD)throw Error(`FreeBSD process identity is unavailable in this sealed runtime`);return u||=n(import.meta.url)(`@openclaw/proc-safe/identity`),u}function readFreeBsdProcessStartTime(e){if(process.platform!==`freebsd`||!Number.isInteger(e)||e<=0||e>2147483647)return null;try{return loadFreeBsdProcessIdentityNative().readProcessIdentity(e)?.startTimeSinceBootMicros??null}catch{return null}}function readDarwinNativeIdentity(e){if(!(process.platform!==`darwin`||typeof SEALED_RUNTIME_BUILD==`boolean`&&SEALED_RUNTIME_BUILD))try{let t=c(e);return t&&!t.exited?{startedAt:Math.floor(t.startTimeMicros/1e6),startTimeMicros:t.startTimeMicros}:null}catch{return}}function readNativeLiveness(e){if(!(![`darwin`,`win32`,`freebsd`].includes(process.platform)||typeof SEALED_RUNTIME_BUILD==`boolean`&&SEALED_RUNTIME_BUILD))try{let t=c(e);return t!==null&&!t.exited}catch(e){return e instanceof s&&(e.code===`helper-unavailable`||e.code===`unsupported-platform`)?void 0:!0}}let d=null;function isValidPid(e){return Number.isInteger(e)&&e>0}function hasLinuxProcfs(){return process.platform===`linux`||process.platform===`android`}function isExitedLinuxProcess(e){if(!hasLinuxProcfs())return!1;try{let t=r.readFileSync(`/proc/${e}/status`,`utf8`),n=t.match(/^State:\s+(\S)/m)?.[1],i=t.match(/^Threads:[ \t]+(\d+)[ \t]*$/m)?.[1];if(i!==`0`){let r=t.match(/^Tgid:[ \t]+(\d+)[ \t]*$/m)?.[1];return r!==void 0&&Number(r)!==e||(n===`Z`||n===`X`)&&i===`1`}}catch{}try{process.kill(e,0)}catch(e){return e.code===`ESRCH`}return!1}function isPidAlive(e){if(!isValidPid(e))return!1;try{process.kill(e,0)}catch(t){let n=t.code;if(n!==`EPERM`)return n===`ESRCH`?readNativeLiveness(e)??!1:!1}return readNativeLiveness(e)??!isExitedLinuxProcess(e)}function isPidDefinitelyDead(e){if(!isValidPid(e))return!0;let t=!1;try{process.kill(e,0)}catch(e){let n=e.code;if(n!==`ESRCH`&&(process.platform!==`win32`||n!==`EPERM`))return!1;t=n===`ESRCH`}let n=readNativeLiveness(e);return n===void 0?t&&process.platform!==`freebsd`||isExitedLinuxProcess(e):!n}function getDarwinProcessStartTime(t,n,r){let i=performance.now(),o=readDarwinNativeIdentity(t);if(o!==void 0)return o?.startedAt??null;let s=r===void 0?1e3:Math.ceil(r-(performance.now()-i));if(s<=0)return null;try{let r=a.execFileSync(`/bin/ps`,[`-o`,`lstart=`,`-p`,String(t)],{encoding:`utf8`,env:{...e(n),LC_ALL:`C`,TZ:`UTC`},stdio:[`ignore`,`pipe`,`ignore`],timeout:s,killSignal:`SIGKILL`}).trim(),i=Date.parse(`${r} UTC`);return Number.isFinite(i)?Math.floor(i/1e3):null}catch{return null}}function readLinuxProcessStat(e){if(!isValidPid(e)||!hasLinuxProcfs())return null;try{let t=r.readFileSync(`/proc/${e}/stat`,`utf8`),n=t.lastIndexOf(`)`);if(n<0||!t.startsWith(`${e} (`))return null;let i=t.slice(n+1).trimStart().split(/\s+/),a=Number(i[19]),o=Number(i[1]);return Number.isSafeInteger(a)&&a>=0&&Number.isSafeInteger(o)&&o>=0?{parentPid:o,startedAt:a}:null}catch{return null}}function getProcessStartTime(e){return readLinuxProcessStat(e)?.startedAt??null}function getProcessInstanceStartTime(e){if(!isValidPid(e))return null;if(hasLinuxProcfs()){let t=getProcessStartTime(e);return Number.isSafeInteger(t)?t:null}let t=readDarwinNativeIdentity(e);if(!t)return null;let n=t.startTimeMicros;return Number.isSafeInteger(n)?n:null}function getFileLockProcessStartTime(e,t=process.env,n){if(!isValidPid(e))return null;let r=e===process.pid;if(r&&d!==null)return d;let i=process.platform===`darwin`?getDarwinProcessStartTime(e,t,n):process.platform===`win32`?readWindowsProcessStartTimeSync(e,n,t):process.platform===`freebsd`?readFreeBsdProcessStartTime(e):getProcessStartTime(e);return r&&i!==null&&(d=i),i}export{isPidDefinitelyDead as a,isPidAlive as i,getProcessInstanceStartTime as n,readLinuxProcessStat as o,getProcessStartTime as r,getFileLockProcessStartTime as t};