// ********** settings ********************************************************
$allowed_to_kill = array('127.0.0.1', '192.168.16.124', '217.10.34.24'); // hosts that are allowed to kill processes
$go_exe = 'D:\OS2Apps\go.exe'; // path to go.exe
$zuko_uptime = 'D:\OS2Apps\uptime.exe'; // uptime.exe by zuko - http://zuko.mitm.ru/
$zuko_mem = 'D:\OS2Apps\mem.exe'; // mem.exe by zuko
$ths_getram = 'D:\OS2Apps\Theseus\getram.exe'; // Theseus4 getram utility
$pstat = 'C:\OS2\PSTAT.EXE'; // PSTAT utility location
$psfiles = 'C:\OS2\PSFILES.EXE'; // PSFILES utility location
$expose_zuko = true; // zuko rocks
$show_getram = true; // show getram output
$parse_pstat = true; // parse PSTAT output
$parse_psfiles = true; // parse PSFILES output
// ********** global vars *****************************************************
$proc = array();
$mem = array();
preg_match('/(^[^?]+)(\?|$)/', $_SERVER['REQUEST_URI'], $m); // CGI workaround
$self = $m[1];
// DosOpen modes
$modes1 = array(
0x8000 => 'OPEN_FLAGS_DASD',
0x4000 => 'OPEN_FLAGS_WRITE_THROUGH',
0x2000 => 'OPEN_FLAGS_FAIL_ON_ERROR',
0x1000 => 'OPEN_FLAGS_NO_CACHE',
);
$modes_10_8 = array(
0x0300 => 'OPEN_FLAGS_RANDOMSEQUENTIAL',
0x0200 => 'OPEN_FLAGS_RANDOM',
0x0100 => 'OPEN_FLAGS_SEQUENTIAL',
0x0000 => 'OPEN_FLAGS_NO_LOCALITY',
);
$mask_10_8 = 0x0700; // 0000'0111'0000'0000
$modes2 = array(
0x0080 => 'OPEN_FLAGS_NOINHERIT',
0x0040 => 'OPEN_SHARE_DENYNONE',
0x0030 => 'OPEN_SHARE_DENYREAD',
0x0020 => 'OPEN_SHARE_DENYWRITE',
0x0010 => 'OPEN_SHARE_DENYREADWRITE',
);
$modes_2_0 = array(
0x0002 => 'OPEN_ACCESS_READWRITE',
0x0001 => 'OPEN_ACCESS_WRITEONLY',
0x0000 => 'OPEN_ACCESS_READONLY',
);
$mask_2_0 = 0x0003; // 0000'0000'0000'0011
// file attributes
$attrs = array(
0x0020 => 'FILE_ARCHIVED',
0x0010 => 'FILE_DIRECTORY',
0x0004 => 'FILE_SYSTEM',
0x0002 => 'FILE_HIDDEN',
0x0001 => 'FILE_READONLY',
// 0x0000 => 'FILE_NORMAL'
);
// priorities
$priorities = array(1 => 'idle', 'regular', 'server', 'timecritical');
// ********** HTML blocks *****************************************************
$header = '
Process viewer
%s
';
$styles = '';
$footer = '
';
$table_spacer = '==== | ==== | ======= | === | ==== | %s============ | ============ |
'."\n";
if ($show_getram) {
$table_spacer = sprintf($table_spacer, '======== | ======= | ');
} else {
$table_spacer = sprintf($table_spacer, '');
}
$table_header = "
\n".
''.
''.
'PID | '.
'PPID | '.
'Sess | '.
'Thr | '.
'Prio | '.
'%s'.
'CPU time | '.
'Name |
'.
"\n".$table_spacer;
if ($show_getram) {
$table_header = sprintf($table_header, 'Private | Shared | ');
} else {
$table_header = sprintf($table_header, '');
}
$table_footer = $table_spacer."
\n";
$back = "\n
<< back\n";
$zuko = 'zuko';
$theseus4 = 'theseus4';
// ========== start ===========================================================
if ((strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla/4') !== false) and !strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
$header = sprintf($header, ''); // do not show styles to Netscape
} else {
$header = sprintf($header, $styles);
}
// ********** if request to kill **********************************************
$k = intval($_GET['k']);
$ka = $_GET['ka'];
if ($k or strlen($ka)) {
if ($_GET['sure'] == 'y') {
if (!in_array($_SERVER['REMOTE_ADDR'], $allowed_to_kill)) {
echo $header;
echo "Go kill yourself, bro!\n
You are not allowed to kill processes on this box.";
echo $back;
echo $footer;
die();
}
$cmd = '';
if ($k) { $cmd = "$go_exe -k $k"; }
if (strlen($ka)) { $cmd = "$go_exe -ka $ka"; }
if (strlen($cmd)) { exec($cmd); }
my_redirect($self);
die();
}
echo $header;
$go = `$go_exe`;
parse_go_procs($go, $proc);
if ($k) {
$name = '';
foreach ($proc as $a) {
if (intval($a['pid']) == $k) {
$name = trim($a['name']);
break;
}
}
if (strlen($name)) {
echo "Are you sure you want to kill process with PID $k ($name)?\n";
echo '
yes '."\n";
echo ' no '."\n";
} else {
echo "Process with PID $k not found!";
echo $back;
}
} else {
$count= 0;
foreach ($proc as $a) {
if (trim($a['name']) == $ka) { $count++; }
}
if ($count) {
echo "Are you sure you want to kill all $ka processes (total: $count)?\n";
echo '
yes '."\n";
echo ' no '."\n";
} else {
echo "No $ka process(es) found!";
echo $back;
}
}
echo $footer;
die();
}
// ********** if show more... *************************************************
if ($pid = intval($_GET['show'])) {
echo $header;
$cmd = sprintf('%s /P:%X', $pstat, $pid);
echo "PSTAT information:\n";
$out = `$cmd`;
if ($parse_pstat) {
echo "
\n".parse_pstat_output($out);
} else {
echo "\n$out
";
}
echo "\n
";
$cmd = sprintf('%s %X', $psfiles, $pid);
echo "\nPSFILES information:\n";
$out = `$cmd`;
if ($parse_psfiles) {
echo "
\n".parse_psfiles_output($out);
} else {
echo "\n$out
";
}
echo $back;
echo $footer;
die();
}
// ********** if show `go` output *********************************************
$sort = $_GET['sort'];
$sortby = array('PID' => 'sort_by_pid', 'PPID' => 'sort_by_ppid', 'Sess' => 'sort_by_sess',
'Thr' => 'sort_by_thr', 'Prio' => 'sort_by_prio', 'PrivMem' => 'sort_by_privmem',
'ShMem' => 'sort_by_shmem', 'CPU' => 'sort_by_cpu', 'Name' => 'sort_by_name');
echo $header;
$go = `$go_exe`;
go_proc_report($go);
go_uptime_report($go);
if ($expose_zuko) { zuko_uptime_report($zuko_uptime); }
parse_go_procs($go, $proc);
if ($show_getram) { $ths_mem_report = parse_getram($ths_getram, $mem); }
$n = sizeof($proc);
if (strlen($sortby[$sort])) {
usort($proc, $sortby[$sort]);
if (($sort == 'CPU') or ($sort == 'Name')) {
for ($i=0;$i<$n;$i++) { $proc[$i]['name'] = trim($proc[$i]['name']); }
}
}
?>
Process list
if (strlen($sortby[$sort])) {
echo ' (sorted by '.$sort.') (view unsorted)';
}
?>:
echo $table_header;
$tmpl = ''.
'%d | '. // pid
'%d | '. // ppid
'%s | '. // sess1
'%s | '. // sess2
' %s | '. // thr
'%s | '. // prio
'%s'. // possible mem report
'%s | '. // cpu
'%s | '. // name
'
'."\n";
$tmpl2 = '%s | %s | '; // priv mem, sh mem
for ($i=0;$i<$n;$i++) {
$curr_pid = $proc[$i]['pid'];
$out = sprintf($tmpl,
$self, $curr_pid, $curr_pid, $curr_pid,
$proc[$i]['ppid'],
$proc[$i]['sess1'],
$proc[$i]['sess2'],
$self, urlencode(trim($proc[$i]['name'])), trim($proc[$i]['name']), $proc[$i]['thr'],
say_prio($proc[$i]['prio']), $proc[$i]['prio'],
'%s',
$proc[$i]['cpu'],
$self, $curr_pid, str_replace(' ', ' ', $proc[$i]['name']));
if ($show_getram) {
if (!strlen($privmem = $mem[$curr_pid]['privmem'])) { $privmem = '- - -'; }
if (!strlen($shmem = $mem[$curr_pid]['shmem'])) { $shmem = '- - -'; }
printf($out, sprintf($tmpl2, $privmem, $shmem));
} else {
printf($out, '');
}
}
echo $table_footer;
if ($expose_zuko) { zuko_mem_report($zuko_mem); }
if ($show_getram) { echo $ths_mem_report; }
echo $footer;
// ========== end =============================================================
// ********** funcs *************************************************************
// show number of processes & threads from `go` output
function go_proc_report(&$go) {
preg_match('/ (\d+) Processes with (\d+)/', $go, $m);
list($match, $numproc, $numthr) = $m;
echo "\nThere are $numproc processes with $numthr threads.\n";
}
// show uptime from `go` output
function go_uptime_report(&$go) {
preg_match('/uptime is (\d+)d (\d+)h (\d+)m (\d+)s/', $go, $m);
list($match, $day, $hour, $min, $sec) = $m;
if (intval($day)) {
printf("
System uptime is %d days %02d:%02d:%02d\n", $day, $hour, $min, $sec);
} else {
printf("
System uptime is %02d:%02d:%02d\n", $hour, $min, $sec);
}
}
// parse `go.exe` output to $proc array
function parse_go_procs(&$go, &$proc) {
preg_match_all('/(\d+)\s+(\d+)\s([0-9A-F]+) (\w+)\s+(\d+)\s+([0-9A-F]+)\s+([.:0-9]+) (\s*[\S]+)/m', $go, $matches);
// 0: match
// 1: P-ID
// 2: PPID
// 3: Sess 1
// 4: Sess 2
// 5: Thr
// 6: Prio
// 7: CPU Time
// 8: Name
list($match, $pid, $ppid, $sess1, $sess2, $thr, $prio, $cpu, $name) = $matches;
$n = sizeof($match);
for ($i=0;$i<$n;$i++) {
$proc[$i]['pid'] = intval($pid[$i]);
$proc[$i]['ppid'] = intval($ppid[$i]);
$proc[$i]['sess1'] = $sess1[$i];
$proc[$i]['sess2'] = $sess2[$i];
$proc[$i]['thr'] = $thr[$i];
$proc[$i]['prio'] = $prio[$i];
$proc[$i]['cpu'] = $cpu[$i];
$proc[$i]['name'] = $name[$i];
if (intval($pid[$i]) == 1) { $proc[$i]['name'] = 'SYSINIT'; } // go.exe bug
}
}
function parse_getram($ths_getram, &$mem) {
GLOBAL $theseus4;
$cmd = "$ths_getram 1 0";
$ths = `$cmd`;
preg_match_all('/^(\d+)\s+([.\d]+)M\s+([.\d]+)M\s+([.\d]+)\s+([.\d]+)\s+/m', $ths, $matches);
// 0: match
// 1: pid
// 2: priv mem (Mb)
// 3: sh mem (Mb)
// 4: priv mem (pages)
// 5: sh mem (pages)
list($match, $pid, $privmem, $shmem, $privpages, $shpages) = $matches;
$n = sizeof($pid);
for ($i=0;$i<$n;$i++) {
$key = intval($pid[$i]);
$mem[$key] = array('privmem' => $privmem[$i], 'shmem' => $shmem[$i],
'privpages' => $privpages[$i], 'shpages' => $shpages[$i]);
}
// find summary
preg_match_all('/^\s+([.\d]+)M\s+(|[.0-9]+M)\s+(\d+)\s+(|[.0-9]+)\s+(.*)/m', $ths, $matches);
// 251.355M 68.570M 64347 17554 Total RAM used
// 191.570M 49042 Free RAM available
// -------- -------
// 511.496M 130943 Total RAM (Prvt+Shr+Free)
list($match, $privmem, $shmem, $privpages, $shpages, $name) = $matches;
$out = "
\n\n";
$out .= ''.$theseus4.' memory report: | Count in Megabytes | Count in Pages |
'."\n";
$out .= ' | Private | Shared | Private | Shared |
'."\n";
$n = sizeof($match);
for ($i=0;$i<$n;$i++) {
$pm = substr($privmem[$i], 0, -1).' Mb';
if (strlen($sm = $shmem[$i])) { $sm = substr($sm, 0, -1).' Mb'; } else { $sm = ' '; }
$out .= sprintf("%s | %s | %s | %s | %s |
\n",
trim($name[$i]), $pm, $sm, $privpages[$i], $shpages[$i]);
}
$out .= "
\n";
return $out;
}
// show zuko uptime report
function zuko_uptime_report($zuko_uptime) {
GLOBAL $zuko;
preg_match('/(\d+)d (\d+)h (\d+)m (\d+)s/', `$zuko_uptime`, $m);
list($match, $day, $hour, $min, $sec) = $m;
if (intval($day)) {
printf("
$zuko says system uptime is %d days %02d:%02d:%02d\n", $day, $hour, $min, $sec);
} else {
printf("
$zuko says system uptime is %02d:%02d:%02d\n", $hour, $min, $sec);
}
}
// show zuko mem report
function zuko_mem_report($zuko_mem) {
GLOBAL $zuko;
$out = "
$zuko memory report:\n\n";
preg_match_all('/^(.+) :\s+([.\d]+) Mb\s+\((\d+)/m', `$zuko_mem`, $matches);
// match
// type of memory
// Mb
// kb
list($match, $name, $mb, $kb) = $matches;
$n = sizeof($match);
for ($i=0;$i<$n;$i++) {
$out .= sprintf("%s | %s Mb | (%s kb) |
\n", trim($name[$i]), $mb[$i], $kb[$i]);
}
$out .= "
\n";
echo $out;
}
// parse PSFILES output
function parse_pstat_output($in) {
preg_match('/([0-9a-f]{4})\s+([0-9a-f]{4})\s+([0-9a-f]{2})\s+(\S+)(.*)Thread.*Semaphores(.*)/ism', $in, $matches);
list($match, $pid, $ppid, $sid, $process, $dlls, $threads) = $matches;
$tmpl = "\n".
"Process: %s\n".
"PID: %d\n".
"PPID: %d\n".
"Session: 0x%s\n".
"
\n";
$out = sprintf($tmpl, $process, hexdec($pid), hexdec($ppid), $sid);
preg_match_all('/\s+(\S.*)/m', $dlls, $matches);
list($match, $dlls) = $matches;
$out .= "\n";
$out .= 'Run-time link libraries | Shared memory names |
'."\n";
foreach ($dlls as $dll) {
list($dll, $memname) = preg_split('/\s+/', $dll);
$out .= "$dll | $memname |
";
}
$out .= "\n
\n";
$out .= "
Threads\n";
preg_match_all('/\s+(\S.*)/m', $threads, $matches);
list($match, $threads) = $matches;
$out .= "\n";
$out .= 'ID | Priority | State | Block ID | Owned Semaphores |
'."\n";
$tmpl = '%d | '. // thread id
'%s | '. // prio
'%s | '. // state
'%s | '. // block
"%s |
\n"; // sem
foreach ($threads as $thread) {
list($tid, $prio, $state, $block, $sem) = preg_split('/\s+/', $thread);
$out .= sprintf($tmpl, $tid, say_prio($prio), $prio, $state, $block, $sem);
}
$out .= "\n
";
return $out;
}
// parse PSFILES output
function parse_psfiles_output($in) {
preg_match_all('/([0-9a-f]{4}) ([0-9a-f]{4}) ([0-9a-f]{4})([0-9a-f]{4}| no )\s*([0-9a-f]{8}|)\s*([0-9a-f]{8}|)\s*([0-9a-f]{8}|)\s*([0-9a-f]{4}|)\s*([0-9a-f]{4}|)\s*(.*)/m', $in, $matches);
// pid Hdl SFN refs flags mode size hvpb attr name
list($match, $pid, $Hdl, $SFN, $refs, $flags, $mode, $size, $hvpb, $attr, $name) = $matches;
$out = "\n";
$out .= 'Hdl | SFN | refs | flags | mode | size | hvpb | attr | name |
'."\n";
$tmpl = ''.
' %d | '. // Hdl
' %s | '. // SFN
' %d | '. // refs
' %s | '. // flags
'%s | '. // mode
' %s | '. // size
' %s | '. // hvpb
'%s | '. // attr
'%s | '. // name
"
\n";
$n = sizeof($match);
for ($i=0;$i<$n;$i++) {
$out .= sprintf($tmpl,
$Hdl[$i], hexdec($Hdl[$i]),
$SFN[$i],
$refs[$i], hexdec($refs[$i]),
$flags[$i],
$mode[$i], say_mode($mode[$i]),
$size[$i], nice_decimal(hexdec($size[$i])),
$hvpb[$i],
$attr[$i], say_attr($attr[$i]),
trim($name[$i]));
}
$out .= '
';
return $out;
}
// sorting functions
function sort_by_pid($a, $b) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
function sort_by_ppid($a, $b) {
if (intval($a['ppid']) == intval($b['ppid'])) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return (intval($a['ppid']) < intval($b['ppid'])) ? -1 : 1;
}
function sort_by_sess($a, $b) {
if ($a['sess1'] == $b['sess1']) {
if ($a['sess2'] == $b['sess2']) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return ($a['sess2'] < $b['sess2']) ? -1 : 1;
}
return ($a['sess1'] < $b['sess1']) ? -1 : 1;
}
function sort_by_thr($a, $b) {
if (intval($a['thr']) == intval($b['thr'])) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return (intval($a['thr']) > intval($b['thr'])) ? -1 : 1;
}
function sort_by_prio($a, $b) {
if ($a['prio'] == $b['prio']) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return ($a['prio'] > $b['prio']) ? -1 : 1;
}
function sort_by_privmem($a, $b) {
GLOBAL $mem;
$mema = (float)$mem[$a['pid']]['privmem'];
$memb = (float)$mem[$b['pid']]['privmem'];
if ($mema == $memb) {
return ($a['pid'] < $b['pid']) ? -1 : 1;
}
return ($mema > $memb) ? -1 : 1;
}
function sort_by_shmem($a, $b) {
GLOBAL $mem;
$mema = (float)$mem[$a['pid']]['shmem'];
$memb = (float)$mem[$b['pid']]['shmem'];
if ($mema == $memb) {
return ($a['pid'] < $b['pid']) ? -1 : 1;
}
return ($mema > $memb) ? -1 : 1;
}
function sort_by_cpu($a, $b) {
preg_match('/(\d+):(\d{2}):(\d{2})\.(\d{2})/', $a['cpu'], $m);
list($match, $h, $m, $s, $th) = $m; $a1 = 60*60*$h + 60*$m + $s + $th/100;
preg_match('/(\d+):(\d+):(\d+)\.(\d+)/', $b['cpu'], $m);
list($match, $h, $m, $s, $th) = $m; $b1 = 60*60*$h + 60*$m + $s + $th/100;
if ($a1 == $b1) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return ($a1 > $b1) ? -1 : 1;
}
function sort_by_name($a, $b) {
if (trim($a['name']) == trim($b['name'])) {
return (intval($a['pid']) < intval($b['pid'])) ? -1 : 1;
}
return (trim($a['name']) < trim($b['name'])) ? -1 : 1;
}
// redirection
function my_redirect($url, $code = 302) {
if ($code == 303) {
preg_match('/^HTTP\/(\d)\.(\d)/', $_SERVER['SERVER_PROTOCOL'], $m);
if (intval($m[1])*10+intval($m[2]) < 11) { $code = 302; }
}
if ($code == 303) {
Header($_SERVER['SERVER_PROTOCOL'].' 303 See Other');
} else {
Header($_SERVER['SERVER_PROTOCOL'].' 302 Found');
}
if (substr($url, 0, 1) == '/') {
$url = 'http://'.$_SERVER['HTTP_HOST'].$url;
} elseif (!strpos($url, '://')) {
preg_match('/(.+?)[^\/]+$/', $_SERVER['REQUEST_URI'], $m);
$url = 'http://'.$_SERVER['HTTP_HOST'].$m[1].$url;
}
Header('Location: '.$url);
?>
Oops!
Ваш браузер не понимает HTTP redirect (status code: =$code?>).
Следуйте по этой ссылке.
Your browser does not understand HTTP redirect (status code: =$code?>).
Follow this link.
}
// return fsOpenMode
function say_mode($str) {
GLOBAL $modes1, $modes_10_8, $mask_10_8, $modes2, $modes_2_0, $mask_2_0;
if (!strlen($str)) { return ' - - -'; }
$mode = hexdec($str);
$out = '';
foreach ($modes1 as $bit => $name) {
if (($mode & $bit) == $bit) {
$out .= ' '.$name.'
';
}
}
foreach ($modes_10_8 as $bit => $name) {
if ((($mode & $mask_10_8) & $bit) == $bit) {
$out .= ' '.$name.'
';
break;
}
}
foreach ($modes2 as $bit => $name) {
if (($mode & $bit) == $bit) {
$out .= ' '.$name.'
';
}
}
foreach ($modes_2_0 as $bit => $name) {
if ((($mode & $mask_2_0) & $bit) == $bit) {
$out .= ' '.$name.' ';
break;
}
}
return $out;
}
// return ulAttribute
function say_attr($str) {
GLOBAL $attrs;
if (!strlen($str)) { return ' - - -'; }
$attr = hexdec($str);
if (!$attr) {
$out = ' FILE_NORMAL ';
} else {
$out = '';
foreach ($attrs as $bit => $name) {
if (($attr & $bit) == $bit) {
$out .= ' '.$name.'
';
}
}
$out = substr($out, 0, -4);
}
return $out;
}
// 02xx -> regular+xx
function say_prio($str) {
GLOBAL $priorities;
return $priorities[(intval($str[1]))].'+'.substr($str, -2);
}
// 1234567 -> 1,234,567
function nice_decimal($a, $separator = ',') {
$a = strval($a);
$out = '';
for($i=strlen($a);$i>3;$i-=3) {
$out = $separator.substr($a, $i-3, 3).$out;
}
return substr($a, 0, $i).$out;
}
// Dmitry Ban, ban at ram.ru
?>