code wiki / (root) / nx_proc_list.nx

nx_proc_list.nx source

↩ module page · 60 lines · 3027 B

1// nx_proc_list.nx -- SOVEREIGN process lister (no ps/shell). Scans /proc, prints "pid<TAB>comm" for 2// every process whose comm contains "torrent" or "nx_" (to find what holds the torrent port). Run: nx_proc_list 3import "nx_syscalls.nx" 4 5const PBUF: i64 = 65536 6 7func pl_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 8func pl_n(v: i64) -> i64 { let t: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m} var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let b: *u8=sys_mmap(28); var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 9func pl_pid(name: *u8) -> i64 { 10 var v: i64=0; var i: i64=0 11 if name[0]==(0 as u8) { return 0-1 } 12 while name[i]!=(0 as u8) { let c: i64=name[i] as i64; if c<48 {return 0-1} if c>57 {return 0-1} v=v*10+(c-48); i=i+1 } 13 return v 14} 15// does buf[0..n] contain the substring s? 16func pl_has(buf: *u8, n: i64, s: *u8) -> i64 { 17 var sl: i64=0; while s[sl]!=(0 as u8){sl=sl+1} 18 if sl==0 { return 1 } 19 var i: i64=0 20 while i+sl<=n { var j: i64=0; var ok: i64=1; while j<sl { if buf[i+j]!=s[j] {ok=0;j=sl} else {j=j+1} } if ok==1 {return 1} i=i+1 } 21 return 0 22} 23func main() -> i64 { 24 let fd: i64 = sys_openat_rd("/proc" as *u8) 25 if fd < 0 { pl_p("no /proc\n" as *u8); sys_exit(1); return 1 } 26 let gbuf: *u8 = sys_mmap(PBUF); let path: *u8 = sys_mmap(256); let comm: *u8 = sys_mmap(256) 27 pl_p("pid\tcomm (filtered: torrent / nx_)\n" as *u8) 28 var go: i64 = 1 29 while go == 1 { 30 let n: i64 = sys_getdents64(fd, gbuf, PBUF) 31 if n <= 0 { go = 0 } else { 32 var off: i64 = 0 33 while off < n { 34 let rec: *u8 = ((gbuf as i64) + off) as *u8 35 let reclen: i64 = dirent_reclen(rec) 36 let name: *u8 = dirent_name(rec) 37 let pid: i64 = pl_pid(name) 38 if pid > 0 { 39 var po: i64=0; let pf: *u8="/proc/" as *u8; while pf[po]!=(0 as u8){path[po]=pf[po];po=po+1} 40 var ni: i64=0; while name[ni]!=(0 as u8){path[po]=name[ni];po=po+1;ni=ni+1} 41 let sf: *u8="/comm" as *u8; var si: i64=0; while sf[si]!=(0 as u8){path[po]=sf[si];po=po+1;si=si+1} 42 path[po]=0 as u8 43 let cfd: i64 = sys_openat_rd(path) 44 if cfd >= 0 { 45 let r: i64 = sys_read(cfd, comm, 255); sys_close(cfd) 46 if r > 0 { 47 comm[r]=0 as u8 48 if pl_has(comm, r, "torrent" as *u8)==1 { pl_n(pid); pl_p("\t" as *u8); sys_write(1, comm, r) } 49 else { if pl_has(comm, r, "nx_" as *u8)==1 { pl_n(pid); pl_p("\t" as *u8); sys_write(1, comm, r) } } 50 } 51 } 52 } 53 if reclen <= 0 { off = n } else { off = off + reclen } 54 } 55 } 56 } 57 sys_close(fd) 58 pl_p("--- end ---\n" as *u8) 59 sys_exit(0); return 0 60}