openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.75 kB
JavaScript
import{spawn as e}from"node:child_process";import{constants as t}from"node:os";import n from"node:path";import r from"node:fs";var __esmMin=(e,t,n)=>()=>{if(n)throw n[0];try{return e&&(t=e(e=0)),t}catch(e){throw n=[e],e}};function signalExitCode(e){let t=signalNumberFor(e);return t?128+t:1}function signalNumberFor(e){switch(e){case`SIGHUP`:return 1;case`SIGINT`:return 2;case`SIGTERM`:return 15;default:return t.signals?.[e]??0}}__esmMin((()=>{}))();const i=/\\([0-7]{3})/g;function decodeMountInfoPath(e){return e.replace(i,(e,t)=>String.fromCharCode(Number.parseInt(t,8)))}const a=[`memory.max`,`memory.high`],o=[`memory.limit_in_bytes`];function parseCgroupMemoryLimitBytes(e){let t=e.trim();if(t===``||t===`max`||!/^\d+$/u.test(t))return null;let n=BigInt(t);return n<0n||n>BigInt(2**53-1)?null:Number(n)}function resolveCgroupMountPoints(e={}){let t=e.fs??r,n=``;try{n=t.readFileSync(`/proc/self/mountinfo`,`utf8`)}catch{}let i=[],a=[];for(let e of n.split(`
`)){let[t,n]=e.split(` - `),r=(t??``).split(` `),o=r[3],s=r[4],[c,,l]=(n??``).split(` `);if(!s||!o)continue;let u=decodeMountInfoPath(o),d=decodeMountInfoPath(s);c===`cgroup2`?i.push({mountPoint:d,observed:!0,root:u}):c===`cgroup`&&(l??``).split(`,`).includes(`memory`)&&a.push({mountPoint:d,observed:!0,root:u})}return{unified:i.length>0?i:[{mountPoint:`/sys/fs/cgroup`,observed:!1,root:`/`}],v1Memory:a.length>0?a:[{mountPoint:`/sys/fs/cgroup/memory`,observed:!1,root:`/`}]}}function relativeCgroupPath(e,t){if(t.split(`/`).includes(`..`))return null;if(e===`/`)return t;let n=e.split(`/`).filter(Boolean);return n.length>0&&n.every(e=>e===`..`)||n.includes(`..`)||t===`/`?null:t===e?`/`:t.startsWith(`${e}/`)?t.slice(e.length):null}function resolveCgroupMemoryLimitPaths(e={}){let t=e.fs??r,i=``,s=!1;try{i=t.readFileSync(`/proc/self/cgroup`,`utf8`)}catch{s=(e.platform??process.platform)===`linux`}let c=[],addHierarchy=(e,r,i,a)=>{let o=c.length,s=!1,l=!1;for(let o of e){let e=c.length,u=relativeCgroupPath(o.root,i);if(u===null)continue;let d=u.split(`/`).filter(Boolean);for(let e=d.length;e>=0;--e){if(a&&e<d.length)try{let r=n.join(o.mountPoint,...d.slice(0,e),a),i=t.readFileSync(r,`utf8`).trim();if(i===`0`)break;if(i!==`1`){l=!0;break}}catch{l=!0;break}for(let t of r)c.push(n.join(o.mountPoint,...d.slice(0,e),t))}s||=o.observed&&c.length>e}return{added:c.length>o,addedObservedPath:s,hierarchyMetadataUnreadable:l}},l=resolveCgroupMountPoints(e),u=!1,d=!1,f=!1,p=!1,m=!1,h=!1;for(let e of i.split(`
`)){let t=/^\d+:([^:]*):(.*)$/u.exec(e);if(!t)continue;let n=t[1]??``;if(n===``){u=!0;let e=t[2]??``;f||=e===`/`&&l.unified.some(e=>e.observed&&e.root===`/`);let n=addHierarchy(l.unified,a,e);d||=n.addedObservedPath,h||=!n.added}else if(n.split(`,`).includes(`memory`)){u=!0,m=!0;let e=addHierarchy(l.v1Memory,o,t[2]??``,`memory.use_hierarchy`);p||=e.hierarchyMetadataUnreadable,h||=!e.added}}if(!u){for(let e of l.unified)addHierarchy([e],a,e.root);for(let e of l.v1Memory)addHierarchy([e],o,e.root)}return{paths:c,cgroupRecordReadFailed:s,sawMemoryRecord:u,sawObservedV2Mapping:d,sawObservedUnconstrainedV2Root:f&&!m,sawUnresolvedCgroupLimit:h,sawUnreadableV1HierarchyMetadata:p,sawV1MemoryRecord:m}}function hasLinuxMemoryContainment(e,t={},i){if((t.platform??process.platform)!==`linux`)return!1;let a=resolveCgroupMemoryLimitPaths(t);if(!a.sawObservedV2Mapping||a.cgroupRecordReadFailed||a.sawUnresolvedCgroupLimit)return!1;let o=t.fs??r;return a.paths.some(t=>{if(n.basename(t)!==`memory.max`||i&&n.basename(n.dirname(t))!==i)return!1;try{let r=parseCgroupMemoryLimitBytes(o.readFileSync(t,`utf8`)),i=n.dirname(t);return r!==null&&r>0&&r<=e&&o.readFileSync(n.join(i,`memory.swap.max`),`utf8`).trim()===`0`&&o.readFileSync(n.join(i,`memory.oom.group`),`utf8`).trim()===`1`}catch{return!1}})}const[s,c,l,u,...d]=process.argv.slice(2),f=Number(s);if(!c||!u||l!==`true`&&l!==`false`||!Number.isSafeInteger(f)||f<=0||!hasLinuxMemoryContainment(f,{},c))console.error(`[memory] The owned cgroup has no verified memory/swap limit; semantic command was not started. Use a cgroup-v2 host with a user systemd manager or a bounded Crabbox.`),process.exitCode=75;else{let t={...process.env,NODE_OPTIONS:process.env.OPENCLAW_MANAGED_NODE_OPTIONS??``};delete t.OPENCLAW_MANAGED_NODE_OPTIONS;let n=e(u,d,{env:t,stdio:`inherit`,shell:l===`true`}),r,i,remember=e=>{r??=e},a=[`SIGINT`,`SIGTERM`,`SIGHUP`].map(e=>[e,()=>remember(e)]);for(let[e,t]of a)process.on(e,t);process.exitCode=await new Promise(e=>{n.once(`error`,t=>{console.error(t),e(75)}),n.once(`exit`,(t,n)=>{i=r??n??void 0,e(i?signalExitCode(i):t??75)})});for(let[e,t]of a)process.off(e,t);if(i){if(i!==`SIGKILL`&&i!==`SIGSTOP`){let reset=()=>{};process.on(i,reset),process.off(i,reset)}process.kill(process.pid,i)}}export{};