UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 7.25 kB
import{v as e,y as t}from"./openclaw-state-read-read-open-flags-CtNL1rJs.mjs";import{n,r}from"./openclaw-state-read-process-env-DgVwfN3Y.mjs";import{createRequire as i}from"node:module";import a from"node:fs";import o from"node:path";import s,{spawnSync as c}from"node:child_process";import{ProcSafeError as l}from"@openclaw/proc-safe/errors";import{readProcessIdentity as u}from"@openclaw/proc-safe/identity";const d=`C:\\Windows`;function readNativeProcessStartTime(e){if(!(process.platform!==`win32`||typeof SEALED_RUNTIME_BUILD==`boolean`&&SEALED_RUNTIME_BUILD))try{let t=u(e);return t&&!t.exited?Math.floor(t.startTimeMicros/1e3):null}catch{return}}function windowsSystemRoot(e){let t=r(e,`SystemRoot`,`win32`)??r(e,`WINDIR`,`win32`);if(!t)return d;let n=o.win32.normalize(t);return/^[A-Za-z]:\\/.test(n)&&!n.startsWith(`\\\\`)?n:d}function windowsPowerShellPath(e){return o.win32.join(windowsSystemRoot(e),`System32`,`WindowsPowerShell`,`v1.0`,`powershell.exe`)}function windowsWmicPath(e){return o.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(e,t=1e4,r=process.env){if(!Number.isInteger(e)||e<=0||e>4294967295)return null;let i=performance.now()+t,a=readNativeProcessStartTime(e);if(a!==void 0)return a;let o=Math.ceil(i-performance.now());if(o<=0)return null;let s=c(windowsPowerShellPath(r),[`-NoProfile`,`-NonInteractive`,`-Command`,`$process = [System.Diagnostics.Process]::GetProcessById(${e}); try { [Console]::Out.Write($process.StartTime.ToUniversalTime().ToString("o")) } finally { $process.Dispose() }`],{encoding:`utf8`,env:n(r,`win32`),timeout:Math.min(o,5e3),windowsHide:!0});if(!s.error&&s.status===0){let e=parseWindowsProcessStartTime(s.stdout);if(e!==null)return e}let l=Math.ceil(i-performance.now());if(l<=0)return null;let u=c(windowsWmicPath(r),[`process`,`where`,`ProcessId=${e}`,`get`,`CreationDate`,`/value`],{env:n(r,`win32`),timeout:l,windowsHide:!0,stdio:[`ignore`,`pipe`,`ignore`]});return!u.error&&u.status===0?parseWindowsProcessStartTime(u.stdout):null}let f;function loadFreeBsdProcessIdentityNative(){if(typeof SEALED_RUNTIME_BUILD==`boolean`&&SEALED_RUNTIME_BUILD)throw Error(`FreeBSD process identity is unavailable in this sealed runtime`);return f||=i(import.meta.url)(`@openclaw/proc-safe/identity`),f}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=u(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=u(e);return t!==null&&!t.exited}catch(e){return e instanceof l&&(e.code===`helper-unavailable`||e.code===`unsupported-platform`)?void 0:!0}}let p=null;function isValidPid(e){return Number.isInteger(e)&&e>0}function hasLinuxProcfs(){return process.platform===`linux`||process.platform===`android`}function exitedLinuxStatus(e,t){let n=t.match(/^State:\s+(\S)/m)?.[1],r=t.match(/^Threads:[ \t]+(\d+)[ \t]*$/m)?.[1];if(r===`0`)return;let i=t.match(/^Tgid:[ \t]+(\d+)[ \t]*$/m)?.[1];return i!==void 0&&Number(i)!==e||(n===`Z`||n===`X`)&&r===`1`}function isMissingPid(t){try{process.kill(t,0)}catch(t){return e(t,`ESRCH`)}return!1}function isExitedLinuxProcess(e){if(!hasLinuxProcfs())return!1;try{let t=exitedLinuxStatus(e,a.readFileSync(`/proc/${e}/status`,`utf8`));if(t!==void 0)return t}catch{}return isMissingPid(e)}async function isPidDefinitelyDeadAsync(t){if(!hasLinuxProcfs())return isPidDefinitelyDead(t);if(!isValidPid(t))return!0;try{process.kill(t,0)}catch(t){return e(t,`ESRCH`)}try{let e=exitedLinuxStatus(t,await a.promises.readFile(`/proc/${t}/status`,`utf8`));if(e!==void 0)return e}catch{}return isMissingPid(t)}function isPidAlive(e){if(!isValidPid(e))return!1;try{process.kill(e,0)}catch(n){let r=t(n)?n.code:void 0;if(r!==`EPERM`)return r===`ESRCH`?readNativeLiveness(e)??!1:!1}return readNativeLiveness(e)??!isExitedLinuxProcess(e)}function isPidDefinitelyDead(e){if(!isValidPid(e))return!0;let n=!1;try{process.kill(e,0)}catch(e){let r=t(e)?e.code:void 0;if(r!==`ESRCH`&&(process.platform!==`win32`||r!==`EPERM`))return!1;n=r===`ESRCH`}let r=readNativeLiveness(e);return r===void 0?n&&process.platform!==`freebsd`||isExitedLinuxProcess(e):!r}function getDarwinProcessStartTime(e,t,r){let i=performance.now(),a=readDarwinNativeIdentity(e);if(a!==void 0)return a?.startedAt??null;let o=r===void 0?1e3:Math.ceil(r-(performance.now()-i));if(o<=0)return null;try{let r=s.execFileSync(`/bin/ps`,[`-o`,`lstart=`,`-p`,String(e)],{encoding:`utf8`,env:{...n(t),LC_ALL:`C`,TZ:`UTC`},stdio:[`ignore`,`pipe`,`ignore`],timeout:o,killSignal:`SIGKILL`}).trim(),i=Date.parse(`${r} UTC`);return Number.isFinite(i)?Math.floor(i/1e3):null}catch{return null}}function parseLinuxProcessStat(e,t){let n=t.lastIndexOf(`)`);if(n<0||!t.startsWith(`${e} (`))return null;let r=t.slice(n+1).trimStart().split(/\s+/),i=Number(r[19]),a=Number(r[1]);return Number.isSafeInteger(i)&&i>=0&&Number.isSafeInteger(a)&&a>=0?{parentPid:a,startedAt:i}:null}function readLinuxProcessStat(e){if(!isValidPid(e)||!hasLinuxProcfs())return null;try{return parseLinuxProcessStat(e,a.readFileSync(`/proc/${e}/stat`,`utf8`))}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&&p!==null)return p;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&&(p=i),i}export{isPidDefinitelyDead as a,readLinuxProcessStat as c,isPidAlive as i,getProcessInstanceStartTime as n,isPidDefinitelyDeadAsync as o,getProcessStartTime as r,parseLinuxProcessStat as s,getFileLockProcessStartTime as t};