ucbps.c revision fba40a479337f3eb612fb77d692257a45d282023
0N/A * The contents of this file are subject to the terms of the 0N/A * Common Development and Distribution License (the "License"). 0N/A * You may not use this file except in compliance with the License. 0N/A * See the License for the specific language governing permissions 0N/A * and limitations under the License. 0N/A * When distributing Covered Code, include this CDDL HEADER in each 0N/A * If applicable, add the following below this CDDL HEADER, with the 0N/A * fields enclosed by brackets "[]" replaced with your own identifying 0N/A * information: Portions Copyright [yyyy] [name of copyright owner] 0N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 0N/A * Use is subject to license terms. 0N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 0N/A/* All Rights Reserved */ 0N/A * University Copyright- Copyright (c) 1982, 1986, 1988 0N/A * The Regents of the University of California 0N/A * All Rights Reserved 0N/A * University Acknowledgment- Portions of this document are derived from 0N/A * software developed by the University of California, Berkeley, and its 0N/A * ps -- print things about processes. 0N/A#
define NTTYS 2 /* max ttys that can be specified with the -t option */ 0N/A /* only one tty can be specified with SunOS ps */ 0N/A#
define SIZ 30 /* max processes that can be specified with -p and -g */ 0N/A#
define ARGSIZ 30 /* size of buffer holding args for -t, -p, -u options */ 0N/Astatic int uflg;
/* user-oriented output */ 0N/Astatic int aflg;
/* Display all processes */ 0N/Astatic int eflg;
/* Display environment as well as arguments */ 0N/Astatic int gflg;
/* Display process group leaders */ 0N/Astatic int tflg;
/* Processes running on specific terminals */ 0N/Astatic int rflg;
/* Running processes only flag */ 0N/Astatic int Sflg;
/* Accumulated time plus all reaped children */ 0N/Astatic int xflg;
/* Include processes with no controlling tty */ 0N/Astatic int cflg;
/* Display command name */ 0N/Astatic int vflg;
/* Virtual memory-oriented output */ 0N/Astatic int nflg;
/* Numerical output */ 0N/Astatic int pflg;
/* Specific process id passed as argument */ 0N/Astatic int Uflg;
/* Update private database, ups_data */ 0N/Astatic char *
p1;
/* points to successive option arguments */ 0N/Astatic int ndev;
/* number of devices */ 0N/Astatic int maxdev;
/* number of devl structures allocated */ 0N/Astatic struct devl {
/* device list */ 0N/Astatic char *
procdir =
"/proc";
/* standard /proc directory */ 0N/Astatic void usage();
/* print usage message and quit */ 0N/A char *
psargs =
NULL;
/* pointer to buffer for -w and -ww options */ 0N/A * This program needs the proc_owner privilege 0N/A * calculate width of pid fields based on configured MAXPID 0N/A * (must be at least 5 to retain output format compatibility) 0N/A /* add the '-' for BSD compatibility */ 0N/A gflg++;
/* include process group leaders */ 0N/A case 'c':
/* display internal command name */ 0N/A case 'r':
/* restrict output to running processes */ 0N/A case 'S':
/* display time by process and all reaped children */ 0N/A case 'x':
/* process w/o controlling tty */ 0N/A case 'l':
/* long listing */ 0N/A case 'u':
/* user-oriented output */ 0N/A case 'U':
/* update private database ups_data */ 0N/A case 'w':
/* increase display width */ 0N/A else /* second w option */ 0N/A case 'v':
/* display virtual memory format */ 0N/A * display all processes except process group 0N/A * leaders and processes w/o controlling tty 0N/A /* Display environment along with aguments. */ 0N/A case 'n':
/* Display numerical output */ 0N/A case 't':
/* restrict output to named terminal */ 0N/A do {
/* only loop through once (NTTYS = 2) */ 0N/A default:
/* error on ? */ 0N/A * The -U option is obsolete. Attempts to use it cause ps to exit 0N/A * without printing anything. 0N/A "ps: %s is an invalid non-numeric argument for a process id\n",
parg);
0N/A /* allocate an initial guess for the number of processes */ 0N/A nent = 0;
/* no active entries yet */ 0N/A " F UID%*s%*s %%C PRI NI SZ RSS " 0N/A " UID%*s %%CPU %%MEM SZ RSS " 0N/A "TT S START TIME COMMAND",
0N/A "USER %*s %%CPU %%MEM SZ RSS " 0N/A "TT S START TIME COMMAND",
0N/A "%*s TT S TIME SIZE RSS %%CPU %%MEM " 0N/A * Determine which processes to print info about by searching 0N/A * the /proc directory and looking at each process. 0N/A /* for each active process --- */ 0N/A /* now we need the proc_owner privilege */ 0N/A /* drop proc_owner privilege after open */ 0N/A * Get the info structure for the process 0N/A /* Display only process from command line */ 0N/A * Omit "uninteresting" processes unless 'g' option. 0N/A * Omit non-running processes for 'r' option 0N/A * Read the args for the -w and -ww cases 0N/A "ps: read() on %s: %s\n",
0N/A /* revert to non-privileged user */ 0N/Ausage()
/* print usage message and quit */ 0N/A static char usage1[] =
"ps [ -aceglnrSuUvwx ] [ -t term ] [ num ]";
0N/A * Read the process arguments from the process. 0N/A * This allows >PRARGSZ characters of arguments to be displayed but, 0N/A * unlike pr_psargs[], the process may have changed them. for (i = 0; i <
NARG; i++)
* Read environment variables from the process. * Append them to psargs if there is room. for (i = 0; i <
NARG; i++)
* getarg() finds the next argument in list and copies arg into argbuf. * p1 first pts to arg passed back from getopt routine. p1 is then * bumped to next character that is not a comma or blank -- p1 NULL while ((c = *
p1) !=
'\0' && (c ==
',' ||
isspace(c)))
while ((c = *
p1) !=
'\0' && c !=
',' && !
isspace(c)) {
while ((c = *
p1) !=
'\0' && (c ==
',' ||
isspace(c)))
"ps: not enough memory for %d devices\n",
maxdev);
* gettty returns the user's tty number or ? if none. * Print percent from 16-bit binary fraction [0 .. 1] * Round up .01 to .1 to indicate some small percentage (the 0x7000 below). value = ((
value *
1000) +
0x7000) >>
15;
/* [0 .. 1000] */ * Print info about the process. * If process is zombie, call print routine and return. * Get current terminal. If none ("?") and 'a' is set, don't print * info. If 't' is set, check if term is in list of desired terminals * Look for same device under different names. (
void)
printf(
" %-8.14s",
tp);
/* TTY */ (
void)
printf(
" %-8.14s",
tp);
/* TTY */ /* time just for process */ if (
Sflg) {
/* calculate time for process and all reaped children */ (
void)
printf(
" %2ld:%.2ld",
tm /
60,
tm %
60);
/* TIME */ * PRARGSZ == length of cmd arg string. /* dumps core on really long strings */ * Print starting time of process unless process started more than 24 hours * ago, in which case the date is printed. (
void)
printf(
" ");
/* NICE */ (
void)
printf(
" 0");
/* RSS */ (
void)
printf(
" ");
/* WCHAN */ (
void)
printf(
" ");
/* START */ /* time just for process */ if (
Sflg) {
/* calculate time for process and all reaped children */ (
void)
printf(
" %2ld:%.2ld",
tm /
60,
tm %
60);
/* TIME */ (
void)
printf(
" 0");
/* RSS */ * Returns true iff string is all numeric. }
while ((c = *++s) !=
'\0');
* Function to compute the number of printable bytes in a multibyte * command string ("internationalization"). return (
8);
/* default to use for illegal chars */