[comp.unix.microport] dynamic process status display

series@well.UUCP (Frank Korzeniewski) (09/28/89)

hi:
    i have wanted a program like the following for two years. the Venix
program by Alan Wendt was the catalyst for me to just do it. this version
does dynamic displays of process info using curses. you can specify
the process PID's or a UID or a tty device. thanks Alan.

Frank Korzeniewski.

--------------------------- cut here ----------------------------------
/*
 * UDPY.C - dynamic display of processes on System V/386 Rel. 3
 *
 * udpy -u 100		-- display all processes for UID 100 (no shells)
 * udpy -t 5 1		-- display all processes for tty device 5 1
 * udpy 9 2 34		-- display specified processes
 *
 * NOTE: this program requires access to /dev/mem,
 *	/dev/kmem and /dev/swap in order to run.
 *
 * build with: cc -o udpy udpy.c -lcurses
 *
 * This was adapted from the Sys V Venix program by Alan Wendt.
 * Adaptation by Frank Korzeniewski.
 * There was no copyright on the original so theres none on this one.
 * Hope you find it usefull.
 */

#include <nlist.h>
#include <sys/types.h>
#include <sys/immu.h>
#include <sys/region.h>
#include <sys/proc.h>
#include <sys/var.h>
#include <sys/fs/s5dir.h>
#include <sys/tss.h>
#include <sys/signal.h>
#include <sys/user.h>
#include <stdio.h>
#include <curses.h>

#define	eq	==
#define	ne	!=

struct nlist nl [] =
{
	{ "proc" },
	{ "v" },
	{ "" }
};

struct
{
	char	*signame;
	int	sigbit;
} sigprt [] =
{
	{	" SIGHUP",	0x00000001	},
	{	" SIGINT",	0x00000002	},
	{	" SIGQUIT",	0x00000004	},
	{	" SIGILL",	0x00000008	},
	{	" SIGTRAP",	0x00000010	},
	{	" SIGABRT",	0x00000020	},
	{	" SIGEMT",	0x00000040	},
	{	" SIGFPE",	0x00000080	},
	{	" SIGKILL",	0x00000100	},
	{	" SIGBUS",	0x00000200	},
	{	" SIGSEGV",	0x00000400	},
	{	" SIGSYS",	0x00000800	},
	{	" SIGPIPE",	0x00001000	},
	{	" SIGALRM",	0x00002000	},
	{	" SIGTERM",	0x00004000	},
	{	" SIGUSR1",	0x00008000	},
	{	" SIGUSR2",	0x00010000	},
	{	" SIGCLD",	0x00020000	},
	{	" SIGPWR",	0x00040000	},
	{	" SIGPOLL",	0x00200000	},
	{	0,		0		}
};

struct proc	aproc;
struct var	v;
pde_t		uptbl [2];
struct user	user;
int		uvalid;
WINDOW		*win;
char		string [1024];
int		pid [20], pid_p [20];
int		npid = 0;
int		f;
int		m;
int		s;
int		uidf = 0;
int		uid;
int		ttyf = 0;
int		tty;

	void
main (argc, argv)
	int		argc;
	int		argv [];
{
	long		p;
	int		i, j, k;

	if (argc < 2)
	{
		printf ("Usage:\tudpy PID-LIST\n");
		printf ("\tudpy -u UID\n");
		printf ("\tudpy -t MAJOR MINOR\n");
		exit (1);
	}

	if (argc eq 3 && strcmp ("-u", argv [1]) == 0)
	{
		uidf = 1;
		uid = atoi (argv [2]);
	}
	else  if (argc eq 4 && strcmp ("-t", argv [1]) == 0)
	{
		ttyf = 1;
		tty = atoi (argv [2]) * 256 + atoi (argv [3]);
	}
	else
	{
		for (npid = 0, i = 1; i < argc; ++i, ++npid)
		{
			pid [npid] = atoi (argv [i]);
		}
	}

	if ((f = open ("/dev/kmem", 0)) < 0)
	{
		printf ("FATAL: can't open /dev/kmem\n");
		exit (1);
	}
	if ((m = open ("/dev/mem", 0)) < 0)
	{
		printf ("FATAL: can't open /dev/mem\n");
		exit (1);
	}
	if ((s = open ("/dev/swap", 0)) < 0)
	{
		printf ("FATAL: can't open /dev/swap\n");
		exit (1);
	}

	nlist("/unix", nl);			/* get some symbol values */
					/* from the kernel's symbol table */

	lseek (f, nl [1] . n_value, 0);
	read (f, &v, sizeof (v));	/* read the "v" structure	*/

	if (!uidf && !ttyf)
	{
		for (p = nl [0] . n_value, i = 0, k = npid;
			i < v . v_proc && k;
			p += sizeof (struct proc), i++)
		{
			/* read a process table entry	*/
			lseek (f, p, 0);
			read (f, &aproc, sizeof (aproc));

			for (j = 0; j < npid; ++j)
			{
				if (pid_p [j] == 0 && aproc . p_pid eq pid [j])
				{
					pid_p [j] = p;
					--k;
					break;
				}
			}
		}

		if (k)
		{
			for (j = 0; j < npid; ++j)
				if (pid_p [j] == 0)
					printf ("process %d not found\n",
						pid [j]);
			exit (1);
		}
	}

	win = initscr ();	/* setup curses */
	cbreak ();		/* no echo/erase/kill in driver */
	noecho ();		/* no echo in getch () */

	halfdelay (10);		/* 1 second terminal read delay */

	for (;;)
	{
		wmove (win, 0, 0);

		if (uidf || ttyf)
		{
			for (p = nl [0] . n_value, i = 0;
				i < v . v_proc;
				p += sizeof (struct proc), i++)
			{
				/* read a process table entry	*/
				lseek (f, p, 0);
				read (f, &aproc, sizeof (aproc));

				if (aproc . p_stat == 0)
					continue;
				if (ttyf || (uidf && aproc . p_uid eq uid))
					doit ();
			}
		}
		else
		{
			for (j = 0; j < npid; ++j)
			{
				/* read the process table entry	*/
				lseek (f, pid_p [j], 0);
				read (f, &aproc, sizeof (aproc));

				if (aproc . p_stat eq 0)
				{
					sprintf (string,
						"process %d terminated\n",
						pid [j]);
					waddstr (win, string);
					if (npid == 1)
					{
						endwin ();
						exit (0);
					}
					if (--npid ne j)
					{
						pid [j] = pid [npid];
						pid_p [j] = pid_p [npid];
						--j;
					}
					continue;
				}
				doit ();
			}
		}
		wclrtobot (win);
		wrefresh (win);

		if (tolower (getch ()) eq 'x')
		{
			endwin (win);
			exit (0);
		}
	}
}

doit ()
{
	int		blkno;

	set_uvalid ();

	if ((aproc . p_flag & SLOAD) && uvalid)
	{
		/* get page table for user area */
		lseek (f, aproc . p_ubptbl, 0);
		read (f, uptbl, sizeof (uptbl));

		/* get 1st half of user area */
		lseek (m, uptbl [0] . pgm . pg_pfn << PNUMSHFT, 0);
		read (m, &user, NBPP);

		/* get 2nd half of user area */
		lseek (m, uptbl [1] . pgm . pg_pfn << PNUMSHFT, 0);
		read (m, &user . u_tss, sizeof (user) - NBPP);
	}
	else  if (uvalid)
	{
		/* get block number of user area in swap */
		blkno = ((dbd_t *) &aproc . p_ubptbl) -> dbd_blkno;

		/* get the whole user area */
		lseek (s, blkno << (PNUMSHFT - DPPSHFT), 0);
		read (s, &user, sizeof (user));
	}

	if (uidf)
	{
		if (user . u_psargs [0] eq '-')
			return;		/* ignore login shell */
		if (strncmp ("sh ", user . u_psargs, 3) == 0)
			return;		/* ignore shells */
		if (strncmp ("csh ", user . u_psargs, 4) == 0)
			return;		/* ignore shells */
		if (strncmp ("ksh ", user . u_psargs, 4) == 0)
			return;		/* ignore shells */
		if (aproc . p_stat eq SONPROC)
			return;		/* ignore us */
	}

	if (ttyf && tty ne user . u_ttyd)
		return;

	sprintf (string, "PID: %d Pgrp: %d", aproc . p_pid, aproc . p_pgrp);
	waddstr (win, string);

	print_stat ();

	print_flags ();

	sprintf (string,
	"    Pri: %d Cpu: %d Nice: %d Ppid: %d Pgsz: %d Alarm: %d",
		aproc . p_pri, aproc . p_cpu, aproc . p_nice, aproc . p_ppid,
		aproc . p_size, aproc . p_clktim);
	waddstr (win, string);
	wclrtoeol (win);
	waddch (win, '\n');

	if (uvalid)
	{
		print_syscall ();

		sprintf (string,
    "    Ctty: 0x%x Euid: %d Egid: %d Ruid: %d Rgid: %d Utime: %d Stime: %d",
			user . u_ttyd, user . u_uid, user . u_gid,
			user . u_ruid, user . u_rgid, user . u_utime,
			user . u_stime);
		waddstr (win, string);
		wclrtoeol (win);
		waddch (win, '\n');

		sprintf (string,
		"    Mem: %d Ior: %d Iow: %d Iosw: %d Ioch: %d",
			user . u_mem, user . u_ior, user . u_iow,
			user . u_iosw, user . u_ioch);
		waddstr (win, string);
		wclrtoeol (win);
		waddch (win, '\n');

		if (aproc . p_sig)
			print_psig ("Sigpend:", aproc . p_sig);

		if (aproc . p_hold)
			print_psig ("Sighold:", aproc . p_hold);

		if (aproc . p_chold)
			print_psig ("Sigset:", aproc . p_chold);

		if (aproc . p_stat ne SZOMB)
		{
			sprintf (string, "    Command: %s",
				user . u_psargs);
			waddstr (win, string);
			wclrtoeol (win);
			waddch (win, '\n');
		}
	}
}

set_uvalid ()
{
	uvalid = TRUE;

	switch (aproc . p_stat)
	{
	case SSLEEP :
	case SRUN :
	case SSTOP :
	case SIDL :
	case SONPROC :		/* current process */
	case SXBRK :
		break;

	case SZOMB :
	default :
		uvalid = FALSE;
		break;
	}
}

print_stat ()
{
	char		*s;

	switch (aproc . p_stat)
	{
	case SSLEEP :
		sprintf (string, " State: SLEEP on %x", aproc . p_wchan);
		waddstr (win, string);
		return;

	case SRUN :
		s = "RUN";
		break;

	case SZOMB :
		s = "ZOMB";
		break;

	case SSTOP :
		s = "STOP";
		break;

	case SIDL :
		s = "IDLE";
		break;

	case SONPROC :		/* current process */
		s = "CUR";
		break;

	case SXBRK :
		s = "XBRK";
		break;

	default :
		s = "UNKNOWN";
		break;
	}

	sprintf (string, " State: %s", s);
	waddstr (win, string);
}

print_flags ()
{
	int		did = 0;

	strcpy (string, " Flags: ");
	if (aproc . p_flag & 0x0001)
	{
		strcat (string, "SYS");		/* system process */
		did = 1;
	}
	if (aproc . p_flag & 0x0002)
	{
		if (did)
			strcat (string, " + ");
		did = 1;
		strcat (string, "PTRC");	/* ptrace active */
	}
	if (aproc . p_flag & 0x0004)
	{
		if (did)
			strcat (string, " + ");
		did = 1;
		strcat (string, "SWTED");
	}
	if (aproc . p_flag & 0x0008)
	{
		if (did)
			strcat (string, " + ");
		did = 1;
		strcat (string, "NOWAKE");
	}
	if (aproc . p_flag & 0x0010)
	{
		if (did)
			strcat (string, " + ");
		did = 1;
		strcat (string, "INCORE");	/* in core */
	}
	if (aproc . p_flag & 0x0020)
	{
		if (did)
			strcat (string, " + ");
		did = 1;
		strcat (string, "LOCKD");	/* locked in core */
	}
	if (aproc . p_flag & 0x0800)
	{
		if (did)
			strcat (string, " + ");
		did = 1;
		strcat (string, "STOPEXEC");
	}
	waddstr (win, string);
	wclrtoeol (win);
	waddch (win, '\n');
}

print_psig (intro, val)
	char		*intro;
	int		val;
{
	int		i, did;

	did = 0;
	sprintf (string, "    %s:", intro);
	for (i = 0; sigprt [i] . signame; ++i)
	{
		if (val & sigprt [i] . sigbit)
		{
			strcat (string, sigprt [i] . signame);
			did = 1;
		}
	}
	if (did)
	{
		waddstr (win, string);
		wclrtoeol (win);
		waddch (win, '\n');
	}
}

print_syscall ()
{
	char		*s;

	switch (user . u_syscall)
	{
	case 0x01 :
		s = "exit";
		break;
	case 0x02 :
		s = "fork";
		break;
	case 0x03 :
		s = "read";
		break;
	case 0x04 :
		s = "write";
		break;
	case 0x05 :
		s = "open";
		break;
	case 0x06 :
		s = "close";
		break;
	case 0x07 :
		s = "wait";
		break;
	case 0x08 :
		s = "creat";
		break;
	case 0x09 :
		s = "link";
		break;
	case 0x0a :
		s = "unlink";
		break;
	case 0x0b :
		s = "exec";
		break;
	case 0x0c :
		s = "chdir";
		break;
	case 0x0d :
		s = "time";
		break;
	case 0x0e :
		s = "mknod";
		break;
	case 0x0f :
		s = "chmod";
		break;
	case 0x10 :
		s = "chown";
		break;
	case 0x11 :
		s = "brk";
		break;
	case 0x12 :
		s = "stat";
		break;
	case 0x13 :
		s = "lseek";
		break;
	case 0x14 :
		s = "getpid/getppid";
		break;
	case 0x15 :
		s = "mount";
		break;
	case 0x16 :
		s = "umount";
		break;
	case 0x17 :
		s = "setuid";
		break;
	case 0x18 :
		s = "getuid/geteuid";
		break;
	case 0x19 :
		s = "stime";
		break;
	case 0x1a :
		s = "ptrace";
		break;
	case 0x1b :
		s = "alarm";
		break;
	case 0x1c :
		s = "fstat";
		break;
	case 0x1d :
		s = "pause";
		break;
	case 0x1e :
		s = "utime";
		break;
	case 0x20 :
		s = "stty";
		break;
	case 0x21 :
		s = "access";
		break;
	case 0x22 :
		s = "nice";
		break;
	case 0x23 :
		s = "statfs";
		break;
	case 0x24 :
		s = "sync";
		break;
	case 0x25 :
		s = "kill";
		break;
	case 0x26 :
		s = "fstatfs";
		break;
	case 0x27 :
		s = "getpgrp/setpgrp";
		break;
	/* nosys	/* 0x28 */
	case 0x29 :
		s = "dup";
		break;
	case 0x2a :
		s = "pipe";
		break;
	case 0x2b :
		s = "times";
		break;
	case 0x2c :
		s = "profil";
		break;
	case 0x2d :
		s = "plock";
		break;
	case 0x2e :
		s = "setgid";
		break;
	case 0x2f :
		s = "getgid/getegid";
		break;
	case 0x30 :
		s = "signal";
		break;
	case 0x31 :
		s = "msgget/ctl/rcv/snd";
		break;
	case 0x32 :
		s = "sysi86";
		break;
	case 0x33 :
		s = "acct";
		break;
	case 0x34 :
		s = "shmat/ctl/dt";
		break;
	case 0x35 :
		s = "semctl/get/op/get";
		break;
	case 0x36 :
		s = "ioctl";
		break;
	case 0x37 :
		s = "uadmin";
		break;
	/* nosys	/* 0x38 */
	case 0x39 :
		s = "ustat";
		break;
	/* nosys	/* 0x3a */
	case 0x3b :
		s = "execve/lp/vp/l/v/le";
		break;
	case 0x3c :
		s = "umask";
		break;
	case 0x3d :
		s = "chroot";
		break;
	case 0x3e :
		s = "fcntl";
		break;
	case 0x3f :
		s = "ulimit";
		break;
	default :
		s = "UNKNOWN";
		break;
	}

	sprintf (string,
		"    Syscall: %s 0x%x Ret1: 0x%x Ret2: 0x%x Error: %d",
		s, user . u_syscall, user . u_r . r_reg . r_val1,
		user . u_r . r_reg . r_val2, user . u_error);
	waddstr (win, string);
	wclrtoeol (win);
	waddch (win, '\n');
}