Files
orca/src/shared/linux-proc-socket-owner-scanner.ts
T
NeilandOrca 879aad7dd6 oom(foundation): bound shared readers/limits + add BoundedMap primitive (#10299)
* oom(01): A1-shared-readers — reintroduce #10179 subset

Files: 18 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(02): A2-shared-image-media — reintroduce #10179 subset

Files: 7 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(03): A3-shared-fs-listing — reintroduce #10179 subset

Files: 21 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(04): A4-shared-remote-relay — reintroduce #10179 subset

Files: 8 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(05): A5-shared-misc — reintroduce #10179 subset

Files: 28 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

* oom(06): B-shared-wiring — reintroduce #10179 subset

Files: 81 applied, 0 deleted (from 6eb70d8370)

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-07-24 21:36:57 -07:00

65 lines
1.8 KiB
TypeScript

import { opendir, readlink } from 'node:fs/promises'
import { posix } from 'node:path'
type LinuxProcSocketOwnerScannerDependencies = {
readDirectoryNames: (directoryPath: string) => AsyncIterable<string>
readLink: (filePath: string) => Promise<string>
}
async function* readNodeDirectoryNames(directoryPath: string): AsyncGenerator<string> {
try {
const directory = await opendir(directoryPath)
for await (const entry of directory) {
yield entry.name
}
} catch {}
}
const defaultDependencies: LinuxProcSocketOwnerScannerDependencies = {
readDirectoryNames: readNodeDirectoryNames,
readLink: readlink
}
export async function mapLinuxSocketInodesToPids(
inodes: ReadonlySet<number>,
dependencies: LinuxProcSocketOwnerScannerDependencies = defaultDependencies
): Promise<Map<number, number>> {
const result = new Map<number, number>()
if (inodes.size === 0) {
return result
}
try {
for await (const pidText of dependencies.readDirectoryNames('/proc')) {
if (!/^\d+$/.test(pidText)) {
continue
}
const pid = Number.parseInt(pidText, 10)
const fdDirectory = posix.join('/proc', pidText, 'fd')
try {
for await (const fd of dependencies.readDirectoryNames(fdDirectory)) {
let link: string
try {
link = await dependencies.readLink(posix.join(fdDirectory, fd))
} catch {
continue
}
const match = /^socket:\[(\d+)\]$/.exec(link)
if (!match) {
continue
}
const inode = Number.parseInt(match[1], 10)
if (inodes.has(inode)) {
result.set(inode, pid)
}
}
} catch {
continue
}
}
} catch {
return result
}
return result
}