sps.c revision 38589
156043Syokota/*-
256043Syokota * Copyright (c) 1998 Andrzej Bialecki
356043Syokota * All rights reserved.
456043Syokota *
556043Syokota * Redistribution and use in source and binary forms, with or without
656043Syokota * modification, are permitted provided that the following conditions
756043Syokota * are met:
856043Syokota * 1. Redistributions of source code must retain the above copyright
956043Syokota *    notice, this list of conditions and the following disclaimer.
1056043Syokota * 2. Redistributions in binary form must reproduce the above copyright
1156043Syokota *    notice, this list of conditions and the following disclaimer in the
1256043Syokota *    documentation and/or other materials provided with the distribution.
1356043Syokota *
1456043Syokota * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1556043Syokota * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1656043Syokota * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1756043Syokota * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1856043Syokota * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1956043Syokota * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2056043Syokota * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2156043Syokota * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2256043Syokota * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2356043Syokota * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2456043Syokota * SUCH DAMAGE.
2556043Syokota *
2656043Syokota *	$Id: sps.c,v 1.2 1998/07/17 07:55:47 abial Exp $
2756043Syokota */
28119420Sobrien
29119420Sobrien/*
30119420Sobrien * Small replacement for ps(1) - uses only sysctl(3) to retrieve info
3156043Syokota */
3256043Syokota
3356043Syokota#include <stdio.h>
3456043Syokota#include <stdlib.h>
35164033Srwatson#include <string.h>
36181905Sed#include <sys/param.h>
3756043Syokota#include <sys/sysctl.h>
3856043Syokota#include <sys/stat.h>
3966834Sphk#include <sys/proc.h>
4066860Sphk#include <sys/user.h>
4156043Syokota
4256043Syokotachar p_stat[]="?iRSTZ";
4356043Syokota
4456043Syokotaint
4556043Syokotamain(int argc, char *argv[])
4656043Syokota{
4756043Syokota	int mib[3],i=0,num,len;
4856043Syokota	struct kinfo_proc kp,*t,*u;
4956043Syokota	char buf[20],vty[5],pst[5];
5056043Syokota	int ma,mi;
51181905Sed
52181905Sed	mib[0]=CTL_KERN;
5356043Syokota	mib[1]=KERN_PROC;
5456043Syokota	mib[2]=KERN_PROC_ALL;
5556043Syokota	if(sysctl(mib,3,NULL,&len,NULL,0)) {
5656043Syokota		perror("sysctl sizing");
5756043Syokota		exit(1);
58181905Sed	}
5956043Syokota	t=(struct kinfo_proc *)malloc(len);
6056043Syokota	if(sysctl(mib,3,t,&len,NULL,0)) {
6156043Syokota		perror("sysctl info");
6256043Syokota		exit(1);
6356043Syokota	}
6456043Syokota	num=len / sizeof(struct kinfo_proc);
6556043Syokota	i=0;
6656043Syokota	printf("USERNAME  PID PPID PRI NICE TTY STAT WCHAN   COMMAND\n");
6756043Syokota	while(i<num) {
6856043Syokota		u=(t+num-i-1);
6956043Syokota		ma=major(u->kp_eproc.e_tdev);
7056043Syokota		mi=minor(u->kp_eproc.e_tdev);
7156043Syokota		switch(ma) {
7256043Syokota		case 255:
7356043Syokota			strcpy(vty,"??");
7456043Syokota			break;
7556043Syokota		case 12:
7656043Syokota			if(mi!=255) {
7756043Syokota				sprintf(vty,"v%d",mi);
7856043Syokota				break;
7956043Syokota			}
8056043Syokota			/* FALLTHROUGH */
8156043Syokota		case 0:
8256043Syokota			strcpy(vty,"con");
8356043Syokota			break;
8456043Syokota		case 5:
8556043Syokota			sprintf(vty,"p%d",mi);
8656043Syokota			break;
8756043Syokota		}
8856043Syokota		sprintf(pst,"%c",p_stat[u->kp_proc.p_stat]);
8956043Syokota		printf("%8s%5d%5d %3d %4d %3s %-4s %-7s (%s)\n",
9056043Syokota			u->kp_eproc.e_login,
9156043Syokota			u->kp_proc.p_pid,
9256043Syokota			u->kp_eproc.e_ppid,
9356043Syokota			u->kp_proc.p_priority,
9456043Syokota			u->kp_proc.p_nice,
9556043Syokota			vty,
9656043Syokota			pst,
9756043Syokota			u->kp_eproc.e_wmesg,
9856043Syokota			u->kp_proc.p_comm);
9956043Syokota		i++;
10056043Syokota	}
10156043Syokota	free(t);
10265129Syokota	exit(0);
10365129Syokota
10465129Syokota}
10556043Syokota