PageRenderTime 35ms CodeModel.GetById 10ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/release/picobsd/tinyware/sps/sps.c

https://bitbucket.org/freebsd/freebsd-head/
C | 122 lines | 86 code | 5 blank | 31 comment | 17 complexity | 3b7c2b6e753361f712cd22fd7b6db2cb MD5 | raw file
  1/*-
  2 * Copyright (c) 1998 Andrzej Bialecki
  3 * All rights reserved.
  4 *
  5 * Redistribution and use in source and binary forms, with or without
  6 * modification, are permitted provided that the following conditions
  7 * are met:
  8 * 1. Redistributions of source code must retain the above copyright
  9 *    notice, this list of conditions and the following disclaimer.
 10 * 2. Redistributions in binary form must reproduce the above copyright
 11 *    notice, this list of conditions and the following disclaimer in the
 12 *    documentation and/or other materials provided with the distribution.
 13 *
 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 24 * SUCH DAMAGE.
 25 *
 26 * $FreeBSD$
 27 */
 28
 29/*
 30 * Small replacement for ps(1) - uses only sysctl(3) to retrieve info
 31 */
 32
 33#include <stdio.h>
 34#include <stdlib.h>
 35#include <string.h>
 36#include <sys/param.h>
 37#include <sys/sysctl.h>
 38#include <sys/stat.h>
 39#include <sys/user.h>
 40
 41char p_stat[] = "?iRSTZWM";
 42
 43int
 44main(int argc, char *argv[])
 45{
 46	int mib[4], i, num, len, j, plen;
 47	char buf[MAXPATHLEN], vty[5], pst[5], wmesg[10];
 48	struct kinfo_proc *ki;
 49	char *t;
 50	int ma, mi;
 51
 52	mib[0] = CTL_KERN;
 53	mib[1] = KERN_PROC;
 54	mib[2] = KERN_PROC_ALL;
 55	if (sysctl(mib, 3, NULL, &len, NULL, 0) != 0) {
 56		perror("sysctl sizing");
 57		exit(1);
 58	}
 59	t = (char *)malloc(len);
 60	if (sysctl(mib, 3, t, &len, NULL, 0) != 0) {
 61		perror("sysctl info");
 62		exit(1);
 63	}
 64	mib[2] = KERN_PROC_ARGS;
 65	num = len / KINFO_PROC_SIZE;
 66	i = 0;
 67	printf("USERNAME   PID  PPID PRI NICE TTY STAT WCHAN   COMMAND\n");
 68	while(i < num) {
 69		ki = (struct kinfo_proc *)(t + (num - i - 1) * KINFO_PROC_SIZE);
 70		mib[3] = ki->ki_pid;
 71		plen = MAXPATHLEN;
 72		if (sysctl(mib, 4, buf, &plen, NULL, 0) != 0) {
 73			perror("sysctl cmd info");
 74			exit(1);
 75		}
 76		if (plen == 0) {
 77			sprintf(buf, "(%s)", ki->ki_comm);
 78		} else {
 79			for (j = 0; j < plen - 1; j++) {
 80				if (buf[j] == '\0') buf[j] = ' ';
 81			}
 82		}
 83		if (strcmp(ki->ki_wmesg, "") == 0) {
 84			sprintf(wmesg, "-");
 85		} else {
 86			strcpy(wmesg, ki->ki_wmesg);
 87		}
 88		ma = major(ki->ki_tdev);
 89		mi = minor(ki->ki_tdev);
 90		switch(ma) {
 91		case 255:
 92			strcpy(vty, "??");
 93			break;
 94		case 12:
 95			if(mi != 255) {
 96				sprintf(vty, "v%d", mi);
 97				break;
 98			}
 99			/* FALLTHROUGH */
100		case 0:
101			strcpy(vty, "con");
102			break;
103		case 5:
104			sprintf(vty, "p%d", mi);
105			break;
106		}
107		sprintf(pst, "%c", p_stat[ki->ki_stat]);
108		printf("%8s %5u %5u %3d %4d %3s %-4s %-7s %s\n",
109			ki->ki_login,
110			ki->ki_pid,
111			ki->ki_ppid,
112			ki->ki_pri.pri_level, /* XXX check this */
113			ki->ki_nice,
114			vty,
115			pst,
116			wmesg,
117			buf);
118		i++;
119	}
120	free((void *)t);
121	exit(0);
122}