34 lines
1015 B
TypeScript
34 lines
1015 B
TypeScript
import { chmodSync, rmSync } from 'node:fs';
|
|
import { loadConfig } from './config.ts';
|
|
import { SandboxManager } from './sandbox.ts';
|
|
import { createSpawnerServer } from './server.ts';
|
|
|
|
const log = (message: string) => console.log(`[spawner] ${message}`);
|
|
|
|
const config = loadConfig(process.env);
|
|
const manager = new SandboxManager(config);
|
|
|
|
await manager.assertRuntime();
|
|
const reaped = await manager.reapOrphans();
|
|
if (reaped > 0) log(`removed ${reaped} orphaned sandbox(es)`);
|
|
|
|
// The socket must never be reachable by anyone outside the shared group,
|
|
// not even for the moment between creating it and changing its mode.
|
|
process.umask(0o117);
|
|
rmSync(config.listenSocket, { force: true });
|
|
|
|
const server = createSpawnerServer(manager, log);
|
|
server.listen(config.listenSocket, () =>
|
|
{
|
|
chmodSync(config.listenSocket, 0o660);
|
|
log(`listening on ${config.listenSocket}`);
|
|
});
|
|
|
|
for (const signal of ['SIGTERM', 'SIGINT'] as const)
|
|
{
|
|
process.once(signal, () => {
|
|
server.close();
|
|
process.exit(0);
|
|
});
|
|
}
|