//@ts-check import { dlopen, FFIType } from 'bun:ffi'; import errno from './errno.js' import * as fs from 'fs' import * as path from 'path' const { symbols: { syscall, }, } = dlopen('/usr/lib/libc.so.6', { syscall: { args: [ FFIType.int, FFIType.int, FFIType.cstring, FFIType.int ], returns: FFIType.int, }, }); // TBD const SYSCALL_INIT_MODULE = 105 const SYSCALL_FINIT_MODULE = 273 export const doProcExists = () => fs.existsSync('/proc') /** * * @param {string} path * @param {string} params * @param {number} flags * @deprecated untested very dangerous */ export function finit_module(path, params = "", flags = 0) { const fileDescriptor = fs.openSync(path, 'r') const result = syscall(SYSCALL_FINIT_MODULE, fileDescriptor, params, flags) if(result > 0) { throw new Error(errno[result]) } else { if(result == -1) { throw new Error('non number or string argument recived') }else if(result == -2) { throw new Error('unsuported argument length (1-5)') } } } export function uname() { if(!doProcExists()) { throw "Proc don't exists exception" } const linuxVersionString = fs.readFileSync('/proc/version').toString() return linuxVersionString.split(' ')[2] } /** * @param {string} modulename * @deprecated untested very dangerous */ export function modprobe(modulename) { const name = uname() if(fs.existsSync(path.join('/lib/modules', name, 'kernel', modulename))) { finit_module(path.join('/lib/modules', name, 'kernel', modulename)) } else { throw new Error("file not found " + path.join('/lib/modules', name, 'kernel', modulename)) } } export function lsmod() { if(!doProcExists()) { throw "Proc don't exists exception" } return fs.readFileSync('/proc/modules').toString('utf-8').split('\n') }