// Copyright 2022 Jo-Philipp Wich // Licensed to the public under the Apache License 2.0. import { basename, readlink, readfile, open, popen, stat, glob } from 'fs'; export function process_list() { const top = popen('/bin/busybox top -bn1'); let line, list = []; for (let line = top.read('line'); length(line); line = top.read('line')) { let m = match(trim(line), /^([0-9]+) +([0-9]+) +(.+) +([RSDZTWI][ { const s = stat(path); return s?.type == 'file' && s?.perm?.user_exec; }), basename); }; export function init_index(name) { const src = readfile(`/etc/init.d/${basename(name)}`, 2048); const idx = []; for (let m in match(src, /^[[:space:]]*(START|STOP)=('[0-9][0-9]'|"[0-9][0-9]"|[0-9][0-9])[[:space:]]*$/gs)) { switch (m[1]) { case 'START': idx[0] = +trim(m[2], '"\''); break; case 'STOP': idx[1] = +trim(m[2], '"\''); break; } } return length(idx) ? idx : null; }; export function init_enabled(name) { for (let path in glob(`/etc/rc.d/[SK][0-9][0-9]${basename(name)}`)) { const ln = readlink(path); const s1 = stat(index(ln, '/') == 0 ? ln : `/etc/rc.d/${ln}`); const s2 = stat(`/etc/init.d/${basename(name)}`); if (s1?.inode == s2?.inode && s1?.type == 'file' && s1?.perm?.user_exec) return true; } return false; }; export function init_action(name, action) { const s = stat(`/etc/init.d/${basename(name)}`); if (s?.type != 'file' || s?.user_exec == false) return false; return system(`env -i /etc/init.d/${basename(name)} ${action} >/dev/null`); };