/*- * Copyright (c) 1990 The Regents of the University of California. * All rights reserved. * * %sccs.include.redist.c% */ #ifndef lint char copyright[] = "@(#) Copyright (c) 1990 The Regents of the University of California.\n\ All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)ps.c 5.31 (Berkeley) 02/08/91"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ps.h" KINFO *kinfo; VAR *vhead, *vtail; int eval; /* exit value */ int rawcpu; /* -C */ int sumrusage; /* -S */ int termwidth; /* width of screen (0 == infinity) */ int totwidth; /* calculated width of requested variables */ static int needuser, needcomm; enum sort { SORTMEM, SORTCPU } sortby; uid_t getuid(); char *ttyname(); #define DFMT "pid tname state cputime command" #define JFMT "user pid ppid pgid sess jobc state tname cputime command" #define LFMT \ "uid pid ppid cpu pri nice vsize rss wchan state tname cputime command" #define SFMT "uid pid sig sigmask sigignore sigcatch state tname command" #define UFMT \ "user pid pcpu pmem vsize rss tname state start cputime command" #define VFMT \ "pid tname state cputime sl re pagein vsize rss lim tsiz trs pcpu pmem command" main(argc, argv) int argc; char **argv; { extern char *optarg; extern int optind; register struct proc *p; register size_t nentries; register VAR *v; register int i; struct winsize ws; dev_t ttydev; int all, ch, flag, fmt, lineno, pid, prtheader, uid, what, xflg; int pscomp(); char *kludge_oldps_options(); if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) || ws.ws_col == 0) termwidth = 79; else termwidth = ws.ws_col - 1; if (argc > 1) argv[1] = kludge_oldps_options(argv[1]); fmt = 0; all = xflg = 0; pid = uid = -1; ttydev = NODEV; while ((ch = getopt(argc, argv, "aCghjLlmO:o:p:rSsTt:uvwx")) != EOF) switch((char)ch) { case 'a': all = 1; break; case 'C': rawcpu = 1; break; case 'g': break; /* no-op */ case 'h': prtheader = ws.ws_row > 5 ? ws.ws_row : 22; break; case 'j': parsefmt(JFMT); fmt = 1; break; case 'L': showkey(); exit(0); case 'l': parsefmt(LFMT); fmt = 1; break; case 'm': sortby = SORTMEM; break; case 'O': parsefmt("pid"); parsefmt(optarg); parsefmt("state tname cputime command"); fmt = 1; break; case 'o': parsefmt(optarg); fmt = 1; break; case 'p': pid = atoi(optarg); xflg = 1; break; case 'r': sortby = SORTCPU; break; case 'S': sumrusage = 1; break; case 's': parsefmt(SFMT); fmt = 1; break; case 'T': if ((optarg = ttyname(STDIN_FILENO)) == NULL) error("stdin: not a terminal"); /* FALLTHROUGH */ case 't': { char *ttypath; struct stat stbuf; char pathbuf[MAXPATHLEN]; if (strcmp(optarg, "co") == 0) ttypath = _PATH_CONSOLE; else if (*optarg != '/') (void) sprintf(ttypath = pathbuf, "%s%s", _PATH_TTY, optarg); else ttypath = optarg; if (stat(ttypath, &stbuf) == -1) { (void)fprintf(stderr, "ps: %s: %s\n", strerror(ttypath)); exit(1); } if (!S_ISCHR(stbuf.st_mode)) error("%s: not a terminal", ttypath); ttydev = stbuf.st_rdev; break; } case 'u': parsefmt(UFMT); sortby = SORTCPU; fmt = 1; break; case 'v': parsefmt(VFMT); sortby = SORTMEM; fmt = 1; break; case 'w': if (termwidth < 131) termwidth = 131; else termwidth = UNLIMITED; break; case 'x': xflg = 1; break; case '?': default: usage(); } argc -= optind; argv += optind; if (*argv) { char *nlistf, *memf = NULL, *swapf = NULL; nlistf = *argv++; if (*argv) { memf = *argv++; if (*argv) swapf = *argv++; } if (kvm_openfiles(nlistf, memf, swapf) == -1) error("kvm_openfiles: %s", kvm_geterr()); } if (!fmt) parsefmt(DFMT); if (!all && ttydev == NODEV && pid == -1) /* XXX - should be cleaner */ uid = getuid(); /* * scan requested variables, noting what structures are needed, * and adjusting header widths as appropiate. */ scanvars(); /* * get proc list */ if (uid != -1) { what = KINFO_PROC_UID; flag = uid; } else if (ttydev != NODEV) { what = KINFO_PROC_TTY; flag = ttydev; } else if (pid != -1) { what = KINFO_PROC_PID; flag = pid; } else what = KINFO_PROC_ALL; /* * select procs */ if ((nentries = kvm_getprocs(what, flag)) == -1) { (void) fprintf(stderr, "ps: %s\n", kvm_geterr()); exit(1); } kinfo = (KINFO *)malloc(nentries * sizeof(KINFO)); if (kinfo == NULL) { (void)fprintf(stderr, "ps: %s\n", strerror(ENOMEM)); exit(1); } for (nentries = 0; p = kvm_nextproc(); ++nentries) { kinfo[nentries].ki_p = p; kinfo[nentries].ki_e = kvm_geteproc(p); if (needuser) saveuser(&kinfo[nentries]); } /* * print header */ printheader(); if (nentries == 0) exit(0); /* * sort proc list */ qsort((void *)kinfo, nentries, sizeof(KINFO), pscomp); /* * for each proc, call each variable output function. */ for (i = lineno = 0; i < nentries; i++) { if (xflg == 0 && (kinfo[i].ki_e->e_tdev == NODEV || (kinfo[i].ki_p->p_flag & SCTTY ) == 0)) continue; for (v = vhead; v; v = v->next) { (*v->oproc)(&kinfo[i], v); if (v->next != NULL) (void) putchar(' '); } (void) putchar('\n'); if (prtheader && lineno++ == prtheader-4) { (void) putchar('\n'); printheader(); lineno = 0; } } exit(eval); } scanvars() { register VAR *v; register int i; for (v = vhead; v; v = v->next) { i = strlen(v->header); if (v->width < i) v->width = i; totwidth += v->width + 1; /* +1 for space */ if (v->flag & USER) needuser = 1; if (v->flag & COMM) needcomm = 1; } totwidth--; } /* XXX - redo */ saveuser(ki) KINFO *ki; { register struct usave *usp; register struct user *up; if ((usp = (struct usave *)calloc(1, sizeof(struct usave))) == NULL) { (void)fprintf(stderr, "ps: %s\n", strerror(errno)); exit(1); } ki->ki_u = usp; up = kvm_getu(ki->ki_p); /* * save arguments if needed */ ki->ki_args = needcomm ? strdup(kvm_getargs(ki->ki_p, up)) : NULL; if (up != NULL) { /* * save important fields */ usp->u_procp = up->u_procp; usp->u_start = up->u_start; usp->u_ru = up->u_ru; usp->u_cru = up->u_cru; usp->u_acflag = up->u_acflag; } } pscomp(k1, k2) KINFO *k1, *k2; { int i; #define VSIZE(k) ((k)->ki_p->p_dsize + (k)->ki_p->p_ssize + (k)->ki_e->e_xsize) if (sortby == SORTCPU) return (getpcpu(k2) - getpcpu(k1)); if (sortby == SORTMEM) return (VSIZE(k2) - VSIZE(k1)); i = k1->ki_e->e_tdev - k2->ki_e->e_tdev; if (i == 0) i = k1->ki_p->p_pid - k2->ki_p->p_pid; return (i); } /* * ICK (all for getopt), would rather hide the ugliness * here than taint the main code. * * ps foo -> ps -foo * ps 34 -> ps -p34 * * The old convention that 't' with no trailing tty arg means the users * tty, is only supported if argv[1] doesn't begin with a '-'. This same * feature is available with the option 'T', which takes no argument. */ char * kludge_oldps_options(s) char *s; { size_t len; char *newopts, *ns, *cp; len = strlen(s); if ((newopts = ns = malloc(len + 2)) == NULL) { (void)fprintf(stderr, "ps: %s\n", strerror(errno)); exit(1); } /* * options begin with '-' */ if (*s != '-') *ns++ = '-'; /* add option flag */ /* * gaze to end of argv[1] */ cp = s + len - 1; /* * if last letter is a 't' flag with no argument (in the context * of the oldps options -- option string NOT starting with a '-' -- * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). */ if (*cp == 't' && *s != '-') *cp = 'T'; else { /* * otherwise check for trailing number, which *may* be a * pid. */ while (cp >= s && isdigit(*cp)) --cp; } cp++; bcopy(s, ns, (size_t)(cp - s)); /* copy up to trailing number */ ns += cp - s; /* * if there's a trailing number, and not a preceding 'p' (pid) or * 't' (tty) flag, then assume it's a pid and insert a 'p' flag. */ if (isdigit(*cp) && (cp == s || cp[-1] != 't' && cp[-1] != 'p' && (cp - 1 == s || cp[-2] != 't'))) *ns++ = 'p'; (void) strcpy(ns, cp); /* and append the number */ return (newopts); } #ifdef lint /* VARARGS1 */ error(fmt) char *fmt; { (void) fputs(fmt, stderr); exit(1); /* NOTREACHED */ } #else error(fmt) char *fmt; { va_list ap; va_start(ap, fmt); (void) fprintf(stderr, "ps: "); (void) vfprintf(stderr, fmt, ap); (void) fprintf(stderr, "\n"); va_end(ap); exit(1); } #endif usage() { (void) fprintf(stderr, "usage:\tps [ -aChjlmrSsTuvwx ] [ -O|o fmt ] [ -p pid ] [ -t tty ] [ system ] [ core ] [ swap ]\n\t ps [ -L ]\n"); exit(1); }