Mount + proper init + sys/proc/tmp

This commit is contained in:
Marc
2021-12-01 21:26:11 +01:00
parent eedb9c067f
commit fbdabd66a5
16 changed files with 351 additions and 17 deletions
+4 -1
View File
@@ -1,3 +1,6 @@
{
"python.linting.enabled": false
"python.linting.enabled": false,
"files.associations": {
"*.inc": "cpp"
}
}
+14
View File
@@ -6,6 +6,20 @@
"sources": [
"clibs/chibrax/syscall.cc"
]
},
{
"target_name": "ioctl",
'cflags': [ '-Wall' ],
"sources": [
"clibs/chibrax/ioctl.cc"
]
},
{
"target_name": "mount",
'cflags': [ '-Wall' ],
"sources": [
"clibs/chibrax/mount.cc"
]
}
]
}
+5
View File
@@ -2,4 +2,9 @@
node node_modules/node-gyp/bin/node-gyp.js configure
sudo node node_modules/node-gyp/bin/node-gyp.js build
echo "syscall"
cp build/Release/syscall.node rootfs\ overrides/chibrax/syscall.node
echo "ioctl"
cp build/Release/ioctl.node rootfs\ overrides/chibrax/ioctl.node
echo "mount"
cp build/Release/mount.node rootfs\ overrides/chibrax/mount.node
+46
View File
@@ -0,0 +1,46 @@
#include <sys/ioctl.h>
#include <unistd.h>
#include <node.h>
#include <fcntl.h>
#include <string>
#define nodeparam v8::FunctionCallbackInfo<v8::Value>
namespace ioctlNode {
void nodeIoctl(const nodeparam& args) {
v8::Isolate* isolate = args.GetIsolate();
if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsUint32()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Argument 1& 2 must be a number").ToLocalChecked()));
return;
}
v8::String::Utf8Value str(isolate, args[0]);
std::string cppStr(*str);
int fd = open(cppStr.c_str(), 'r');
unsigned long request = args[1].As<v8::Number>()->Value();
int params[args.Length() - 2];
for(int i = 1 ; i < args.Length(); i++ ) {
if (!args[i]->IsNumber()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Argument must be a number").ToLocalChecked()));
return;
}
params[i - 1] = args[i].As<v8::Number>()->Value();
}
args.GetReturnValue().Set(ioctl(fd, request, &params));
close(fd);
}
void Initialize(v8::Local<v8::Object> exports) {
NODE_SET_METHOD(exports, "ioctl", nodeIoctl);
}
NODE_MODULE(ioctl_api, Initialize)
}
+64
View File
@@ -0,0 +1,64 @@
#include <sys/mount.h>
#include <unistd.h>
#include <node.h>
namespace mountNode {
void nodemount(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
if (!args[0]->IsString()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "First argument must be a string pointing to a device").ToLocalChecked()));
return;
}
if (!args[1]->IsString()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Argument must be a string pointing to a folder").ToLocalChecked()));
return;
}
if (!args[2]->IsString()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Argument must be a filesystem name").ToLocalChecked()));
return;
}
if (!args[3]->IsNumber()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Argument must be a number made from the mount flags").ToLocalChecked()));
return;
}
if (!args[4]->IsString()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Argument must be a string").ToLocalChecked()));
return;
}
v8::String::Utf8Value devpathV8(isolate, args[0]);
std::string devpath(*devpathV8);
v8::String::Utf8Value mountPointV8(isolate, args[1]);
std::string mountPoint(*mountPointV8);
v8::String::Utf8Value fsTypeV8(isolate, args[2]);
std::string fsType(*fsTypeV8);
unsigned long rwFlags = args[3].As<v8::Number>()->Value();
//comma separated parameters passed directly into the fs driver
v8::String::Utf8Value fsFlagsV8(isolate, args[4]);
std::string fsFlags(*fsFlagsV8);
const int returnValue = mount(devpath.c_str(), mountPoint.c_str(), fsType.c_str(), rwFlags, fsFlags.c_str());
args.GetReturnValue().Set(returnValue);
}
void Initialize(v8::Local<v8::Object> exports) {
NODE_SET_METHOD(exports, "mount", nodemount);
}
NODE_MODULE(mount_api, Initialize)
}
+2
View File
@@ -1 +1,3 @@
syscall.node
ioctl.node
mount.node
+4 -6
View File
@@ -1,3 +1,4 @@
//@ts-check
function panic() {
console.log("/!\\ INIT PANIK /!\\ LOSE CHIBRE")
@@ -6,15 +7,12 @@ function panic() {
async function main() {
try {
const audio = require('./audio.js')
const initd = require('./initd.js')
const reboot = require('./reboot.js')
const input = require('./input.js')
const { fHook } = require("./hook.js")
console.log("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
console.clear()
console.log(`
_______ _________ ______ _______ _______
( ____ \\|\\ /|\\__ __/( ___ \\ ( ____ )( ___ )|\\ /|
| ( \\/| ) ( | ) ( | ( ) )| ( )|| ( ) |( \\ / )
@@ -35,7 +33,7 @@ async function main() {
`)
audio('/chibrax/sheeebr.wav')
await initd()
//chainload into sheebr
await fHook("/usr/bin/sheebr.js")
+84
View File
@@ -0,0 +1,84 @@
//@ts-check
const fs = require('fs')
const path = require('path')
/**
* @typedef {{ name: string, before: string[], after: string[], init(): undefined | Promise<undefined> }} InitScript
*/
async function init() {
const initdFiles = fs.readdirSync('/etc/init.d/')
const initdScripts = initdFiles.filter( (v) => v.endsWith('.js'))
/**
* @type { Array<String> }
*/
const finishedScripts = []
/**
* @type { Array<InitScript> }
*/
const scripts = []
for(const script of initdScripts) {
const scriptCandidate = require(path.join('/etc/init.d/', script))
if(scriptCandidate?.before && scriptCandidate?.after && scriptCandidate?.init)
scripts.push({ ...scriptCandidate, name: script})
else
console.error(`Invalid(missing exports) script error :${script}`)
}
const befores = processBefores(scripts)
let i = 0
while(scripts.length) {
const s = scripts[i % scripts.length]
i += 1
if(isSubSet(s.after, finishedScripts) && ( !befores[s.name] || isSubSet(befores[s.name], finishedScripts) )) {
//on supprime ce service
scripts.splice(scripts.indexOf(s))
const result = s.init()
if(typeof result?.then === 'function') {
await result
}
finishedScripts.push(s.name)
}
}
}
/**
* @template T
* @param {Array<T>} arraya
* @param {Array<T>} arrayb
*/
function isSubSet(arraya, arrayb) {
for(const a of arraya) {
if(!arrayb.includes(a)) {
return false
}
}
return true
}
/**
*
* @param {Array<InitScript>} scripts
* @returns {{ [key: string]: string[] }}
*/
function processBefores(scripts) {
/**
* @type {{ [key: string]: string[] }}
*/
const result = {}
for(const script of scripts) {
for(const before of script.before){
if(! result[before]) result[before] = [script.name]
else result[before].push(script.name)
}
}
return result
}
module.exports = init
-7
View File
@@ -1,7 +0,0 @@
const fs = require('fs')
function setLayOut(layout) {
}
fs.readFileSync('/etc/vconsole.conf')
+68
View File
@@ -0,0 +1,68 @@
//@ts-check
const fs =require('fs')
/**
* @type {{ mount: (device_path: string, mount_point: string, fs: string, flags: number, fsparams: string ) => number }}
*///@ts-expect-error
const nodemount = require("./mount.node")
/**
* @deprecated
*/
const cmount = nodemount.mount
/* These are the fs-independent mount-flags: up to 16 flags are
supported */
const flags = {
MS_RDONLY: 1, /* Mount read-only. */
MS_NOSUID: 2, /* Ignore suid and sgid bits. */
MS_NODEV: 4, /* Disallow access to device special files. */
MS_NOEXEC: 8, /* Disallow program execution. */
MS_SYNCHRONOUS: 16, /* Writes are synced at once. */
MS_REMOUNT: 32, /* Alter flags of a mounted FS. */
MS_MANDLOCK: 64, /* Allow mandatory locks on an FS. */
MS_DIRSYNC: 128, /* Directory modifications are synchronous. */
MS_NOATIME: 1024, /* Do not update access times. */
MS_NODIRATIME: 2048, /* Do not update directory access times. */
MS_BIND: 4096, /* Bind directory at different place. */
MS_MOVE: 8192,
MS_REC: 16384,
MS_SILENT: 32768,
MS_POSIXACL: 1 << 16, /* VFS does not apply the umask. */
MS_UNBINDABLE: 1 << 17, /* Change to unbindable. */
MS_PRIVATE: 1 << 18, /* Change to private. */
MS_SLAVE: 1 << 19, /* Change to slave. */
MS_SHARED: 1 << 20, /* Change to shared. */
MS_RELATIME: 1 << 21, /* Update atime relative to mtime/ctime. */
MS_KERNMOUNT: 1 << 22, /* This is a kern_mount call. */
MS_I_VERSION: 1 << 23, /* Update inode I_version field. */
MS_STRICTATIME: 1 << 24, /* Always perform atime updates. */
MS_LAZYTIME: 1 << 25, /* Update the on-disk [acm]times lazily. */
MS_ACTIVE: 1 << 30,
MS_NOUSER: 1 << 31
};
/**
* @param {string} devicePath
* @param {string} mountPoint
* @param {string} fstype
* @param {number} flags
* @param {string} fsparams
* @returns {number}
*/
function mount(devicePath, mountPoint, fstype, flags, fsparams) {
const supportedFilesystems = require('./suportedFilesystems')
const filesystem = supportedFilesystems.find( v => v.name === fstype)
if(! filesystem) {
throw "Invalid filesystem"
}
if(! fs.existsSync(devicePath) && filesystem.hasdev)
throw "Device don't exist"
return cmount(devicePath, mountPoint, fstype, flags, fsparams)
}
module.exports = { mount, cmount, flags }
@@ -0,0 +1,9 @@
//@ts-check
const fs = require('fs')
const systems = fs.readFileSync('/proc/filesystems', { encoding: 'utf-8' }).split('\n')
const filesystems = systems.map(s => ({ hasdev: !s.includes('nodev'), name: s.includes('nodev') ? s.split('nodev')[1].trim() : s.trim() }))
module.exports = filesystems
Object.freeze(module.exports)
+20
View File
@@ -0,0 +1,20 @@
//@ts-check
const { cmount, mount, flags } = require('../../chibrax/mount.js')
async function init() {
cmount("dummy", "/proc", "proc",
flags.MS_NODEV | flags.MS_RDONLY | flags.MS_NOSUID | flags.MS_RELATIME
, "")
mount("dummy", "/sys", "sysfs", flags.MS_NODEV | flags.MS_NOSUID | flags.MS_RELATIME, "")
mount("dummy", "/tmp", "tmpfs", flags.MS_NODEV, "")
}
const after = [] // list of init script to load before
const before = []
module.exports = {
init,
after,
before
}
@@ -0,0 +1,13 @@
//async is optional but recomended for optimisation (optimisations are yet to be implemented)
async function init() {
//code to execute
}
const after = [] // list of init script to load before
const before = []
module.exports = {
init,
after,
before
}
@@ -0,0 +1,14 @@
const audio = require('/chibrax/audio.js')
async function init() {
audio('/chibrax/sheeebr.wav')
}
const after = [ "filesystem.js" ] // list of init script to load before
const before = []
module.exports = {
init,
after,
before
}
+1
View File
@@ -1,2 +1,3 @@
//@ts-check
const os = require('os');
console.log(os.networkInterfaces());