sys.uc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2022 Jo-Philipp Wich <jo@mein.io>
  2. // Licensed to the public under the Apache License 2.0.
  3. import { basename, readlink, readfile, open, popen, stat, glob } from 'fs';
  4. export function process_list() {
  5. const top = popen('/bin/busybox top -bn1');
  6. let line, list = [];
  7. for (let line = top.read('line'); length(line); line = top.read('line')) {
  8. let m = match(trim(line), /^([0-9]+) +([0-9]+) +(.+) +([RSDZTWI][<NW ][<N ]) +([0-9]+m?) +([0-9]+%) +([0-9]+%) +(.+)$/);
  9. if (m && m[8] != '/bin/busybox top -bn1') {
  10. push(list, {
  11. PID: m[1],
  12. PPID: m[2],
  13. USER: trim(m[3]),
  14. STAT: m[4],
  15. VSZ: m[5],
  16. '%MEM': m[6],
  17. '%CPU': m[7],
  18. COMMAND: m[8]
  19. });
  20. }
  21. }
  22. top.close();
  23. return list;
  24. };
  25. export function conntrack_list(callback) {
  26. const etcpr = open('/etc/protocols');
  27. const protos = {};
  28. if (etcpr) {
  29. for (let line = etcpr.read('line'); length(line); line = etcpr.read('line')) {
  30. const m = match(line, /^([^# \t\n]+)\s+([0-9]+)\s+/);
  31. if (m)
  32. protos[m[2]] = m[1];
  33. }
  34. etcpr.close();
  35. }
  36. const nfct = open('/proc/net/nf_conntrack', 'r');
  37. let connt;
  38. if (nfct) {
  39. for (let line = nfct.read('line'); length(line); line = nfct.read('line')) {
  40. let m = match(line, /^(ipv[46]) +([0-9]+) +\S+ +([0-9]+)( +.+)\n$/);
  41. if (!m)
  42. continue;
  43. let fam = m[1];
  44. let l3 = m[2];
  45. let l4 = m[3];
  46. let tuples = m[4];
  47. let timeout = null;
  48. m = match(tuples, /^ +([0-9]+)( .+)$/);
  49. if (m) {
  50. timeout = m[1];
  51. tuples = m[2];
  52. }
  53. if (index(tuples, 'TIME_WAIT') !== -1)
  54. continue;
  55. let e = {
  56. bytes: 0,
  57. packets: 0,
  58. layer3: fam,
  59. layer4: protos[l4] ?? 'unknown',
  60. timeout: +timeout
  61. };
  62. for (let kv in match(tuples, / (\w+)=(\S+)/g)) {
  63. switch (kv[1]) {
  64. case 'bytes':
  65. case 'packets':
  66. e[kv[1]] += +kv[2];
  67. break;
  68. case 'src':
  69. case 'dst':
  70. e[kv[1]] ??= arrtoip(iptoarr(kv[2]));
  71. break;
  72. case 'sport':
  73. case 'dport':
  74. e[kv[1]] ??= +kv[2];
  75. break;
  76. default:
  77. e[kv[1]] = kv[2];
  78. }
  79. }
  80. if (callback)
  81. callback(e);
  82. else
  83. push(connt ??= [], e);
  84. }
  85. nfct.close();
  86. }
  87. return callback ? true : (connt ?? []);
  88. };
  89. export function init_list() {
  90. return map(filter(glob('/etc/init.d/*'), path => {
  91. const s = stat(path);
  92. return s?.type == 'file' && s?.perm?.user_exec;
  93. }), basename);
  94. };
  95. export function init_index(name) {
  96. const src = readfile(`/etc/init.d/${basename(name)}`, 2048);
  97. const idx = [];
  98. for (let m in match(src, /^[[:space:]]*(START|STOP)=('[0-9][0-9]'|"[0-9][0-9]"|[0-9][0-9])[[:space:]]*$/gs)) {
  99. switch (m[1]) {
  100. case 'START': idx[0] = +trim(m[2], '"\''); break;
  101. case 'STOP': idx[1] = +trim(m[2], '"\''); break;
  102. }
  103. }
  104. return length(idx) ? idx : null;
  105. };
  106. export function init_enabled(name) {
  107. for (let path in glob(`/etc/rc.d/[SK][0-9][0-9]${basename(name)}`)) {
  108. const ln = readlink(path);
  109. const s1 = stat(index(ln, '/') == 0 ? ln : `/etc/rc.d/${ln}`);
  110. const s2 = stat(`/etc/init.d/${basename(name)}`);
  111. if (s1?.inode == s2?.inode && s1?.type == 'file' && s1?.perm?.user_exec)
  112. return true;
  113. }
  114. return false;
  115. };
  116. export function init_action(name, action) {
  117. const s = stat(`/etc/init.d/${basename(name)}`);
  118. if (s?.type != 'file' || s?.user_exec == false)
  119. return false;
  120. return system(`env -i /etc/init.d/${basename(name)} ${action} >/dev/null`);
  121. };