Files
Linux.js---javascript-OS/rootfs overrides/chibrax/kernel.js
T
2022-02-15 12:28:44 +01:00

65 lines
1.7 KiB
JavaScript

//@ts-check
//@ts-expect-error
const { syscall } = require('./syscall.node')
const errno = require('./errno')
const fs = require('fs')
const path = require('path')
// TBD
const SYSCALL_INIT_MODULE = 105
const SYSCALL_FINIT_MODULE = 273
const doProcExists = () => fs.existsSync('/proc')
/**
*
* @param {string} path
* @param {string} params
* @param {number} flags
* @deprecated un tested dangerous
*/
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)')
}
}
}
function uname() {
if(!doProcExists) {
throw "Proc don't exists exception"
}
const linuxVersionString = fs.readFileSync('/proc/version').toString()
return linuxVersionString.split(' ')[2]
}
/**
* @param {string} modulename
*/
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))
}
}
function lsmod() {
if(!doProcExists) {
throw "Proc don't exists exception"
}
return fs.readFileSync('/proc/modules').toString('utf-8').split('\n')
}
module.exports = { modprobe, uname, finit_module, doProcExists, lsmod, syscall }