pgrep cleanup

This commit is contained in:
albert 2006-06-21 04:18:02 +00:00
parent 634c9f6b43
commit 492acac001
3 changed files with 40 additions and 51 deletions

2
NEWS
View File

@ -3,12 +3,14 @@ procps-3.2.6 --> procps-3.2.7
top: document H option -- thanks Tony Ernst top: document H option -- thanks Tony Ernst
top: terabytes -- thanks Tony Ernst top: terabytes -- thanks Tony Ernst
top: CPU usage column width fixes -- thanks Tony Ernst top: CPU usage column width fixes -- thanks Tony Ernst
top: *roff change #326517
ps: SCHED_BATCH is B ps: SCHED_BATCH is B
ps: fix s format (signals) output with thread display ps: fix s format (signals) output with thread display
watch: avoid integer overflow for the time delay watch: avoid integer overflow for the time delay
pwdx: buffer overflow fixed -- thanks Ulf Harnhammar pwdx: buffer overflow fixed -- thanks Ulf Harnhammar
procps.spec needed a slash -- thanks Jesse Brandeburg procps.spec needed a slash -- thanks Jesse Brandeburg
w: stale utmp entries snuck in via uninitialized var -- thanks Robert A Basch w: stale utmp entries snuck in via uninitialized var -- thanks Robert A Basch
pgrep/pkill: fix some realloc-related crashes #353894
procps-3.2.5 --> procps-3.2.6 procps-3.2.5 --> procps-3.2.6

81
pgrep.c
View File

@ -31,6 +31,11 @@
#include "proc/sysinfo.h" #include "proc/sysinfo.h"
#include "proc/version.h" /* procps_version */ #include "proc/version.h" /* procps_version */
// EXIT_SUCCESS is 0
// EXIT_FAILURE is 1
#define EXIT_USAGE 2
#define EXIT_FATAL 3
static int i_am_pkill = 0; static int i_am_pkill = 0;
static const char *progname = "pgrep"; static const char *progname = "pgrep";
@ -71,12 +76,11 @@ static int usage (int opt)
fprintf (stderr, "[-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]\n" fprintf (stderr, "[-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]\n"
"\t[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] " "\t[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] "
"[PATTERN]\n"); "[PATTERN]\n");
exit (opt == '?' ? 0 : 2); exit (opt == '?' ? EXIT_SUCCESS : EXIT_USAGE);
} }
static union el * static union el *split_list (const char *restrict str, int (*convert)(const char *, union el *))
split_list (const char *restrict str, int (*convert)(const char *, union el *))
{ {
char *copy = strdup (str); char *copy = strdup (str);
char *ptr = copy; char *ptr = copy;
@ -91,14 +95,14 @@ split_list (const char *restrict str, int (*convert)(const char *, union el *))
// add 1 because slot zero is a count // add 1 because slot zero is a count
list = realloc (list, 1 + size * sizeof *list); list = realloc (list, 1 + size * sizeof *list);
if (list == NULL) if (list == NULL)
exit (3); exit (EXIT_FATAL);
} }
sep_pos = strchr (ptr, ','); sep_pos = strchr (ptr, ',');
if (sep_pos) if (sep_pos)
*sep_pos = 0; *sep_pos = 0;
// Use ++i instead of i++ because slot zero is a count // Use ++i instead of i++ because slot zero is a count
if (!convert (ptr, &list[++i])) if (!convert (ptr, &list[++i]))
exit (2); exit (EXIT_USAGE);
if (sep_pos) if (sep_pos)
ptr = sep_pos + 1; ptr = sep_pos + 1;
} while (sep_pos); } while (sep_pos);
@ -113,11 +117,9 @@ split_list (const char *restrict str, int (*convert)(const char *, union el *))
return list; return list;
} }
/* strict_atol returns a Boolean: TRUE if the input string contains a // strict_atol returns a Boolean: TRUE if the input string
plain number, FALSE if there are any non-digits. */ // contains a plain number, FALSE if there are any non-digits.
static int strict_atol (const char *restrict str, long *restrict value)
static int
strict_atol (const char *restrict str, long *restrict value)
{ {
int res = 0; int res = 0;
int sign = 1; int sign = 1;
@ -139,8 +141,7 @@ strict_atol (const char *restrict str, long *restrict value)
return 1; return 1;
} }
static int static int conv_uid (const char *restrict name, union el *restrict e)
conv_uid (const char *restrict name, union el *restrict e)
{ {
struct passwd *pwd; struct passwd *pwd;
@ -158,8 +159,7 @@ conv_uid (const char *restrict name, union el *restrict e)
} }
static int static int conv_gid (const char *restrict name, union el *restrict e)
conv_gid (const char *restrict name, union el *restrict e)
{ {
struct group *grp; struct group *grp;
@ -177,8 +177,7 @@ conv_gid (const char *restrict name, union el *restrict e)
} }
static int static int conv_pgrp (const char *restrict name, union el *restrict e)
conv_pgrp (const char *restrict name, union el *restrict e)
{ {
if (! strict_atol (name, &e->num)) { if (! strict_atol (name, &e->num)) {
fprintf (stderr, "%s: invalid process group: %s\n", fprintf (stderr, "%s: invalid process group: %s\n",
@ -191,8 +190,7 @@ conv_pgrp (const char *restrict name, union el *restrict e)
} }
static int static int conv_sid (const char *restrict name, union el *restrict e)
conv_sid (const char *restrict name, union el *restrict e)
{ {
if (! strict_atol (name, &e->num)) { if (! strict_atol (name, &e->num)) {
fprintf (stderr, "%s: invalid session id: %s\n", fprintf (stderr, "%s: invalid session id: %s\n",
@ -205,8 +203,7 @@ conv_sid (const char *restrict name, union el *restrict e)
} }
static int static int conv_num (const char *restrict name, union el *restrict e)
conv_num (const char *restrict name, union el *restrict e)
{ {
if (! strict_atol (name, &e->num)) { if (! strict_atol (name, &e->num)) {
fprintf (stderr, "%s: not a number: %s\n", fprintf (stderr, "%s: not a number: %s\n",
@ -217,16 +214,14 @@ conv_num (const char *restrict name, union el *restrict e)
} }
static int static int conv_str (const char *restrict name, union el *restrict e)
conv_str (const char *restrict name, union el *restrict e)
{ {
e->str = strdup (name); e->str = strdup (name);
return 1; return 1;
} }
static int static int match_numlist (long value, const union el *restrict list)
match_numlist (long value, const union el *restrict list)
{ {
int found = 0; int found = 0;
if (list == NULL) if (list == NULL)
@ -241,8 +236,7 @@ match_numlist (long value, const union el *restrict list)
return found; return found;
} }
static int static int match_strlist (const char *restrict value, const union el *restrict list)
match_strlist (const char *restrict value, const union el *restrict list)
{ {
int found = 0; int found = 0;
if (list == NULL) if (list == NULL)
@ -257,8 +251,7 @@ match_strlist (const char *restrict value, const union el *restrict list)
return found; return found;
} }
static void static void output_numlist (const union el *restrict list, int num)
output_numlist (const union el *restrict list, int num)
{ {
int i; int i;
const char *delim = opt_delim; const char *delim = opt_delim;
@ -269,8 +262,7 @@ output_numlist (const union el *restrict list, int num)
} }
} }
static void static void output_strlist (const union el *restrict list, int num)
output_strlist (const union el *restrict list, int num)
{ {
// FIXME: escape codes // FIXME: escape codes
int i; int i;
@ -282,8 +274,7 @@ output_strlist (const union el *restrict list, int num)
} }
} }
static PROCTAB * static PROCTAB *do_openproc (void)
do_openproc (void)
{ {
PROCTAB *ptp; PROCTAB *ptp;
int flags = 0; int flags = 0;
@ -301,7 +292,7 @@ do_openproc (void)
int i = num; int i = num;
uid_t *uids = malloc (num * sizeof (uid_t)); uid_t *uids = malloc (num * sizeof (uid_t));
if (uids == NULL) if (uids == NULL)
exit (3); exit (EXIT_FATAL);
while (i-- > 0) { while (i-- > 0) {
uids[i] = opt_euid[i+1].num; uids[i] = opt_euid[i+1].num;
} }
@ -313,8 +304,7 @@ do_openproc (void)
return ptp; return ptp;
} }
static regex_t * static regex_t * do_regcomp (void)
do_regcomp (void)
{ {
regex_t *preg = NULL; regex_t *preg = NULL;
@ -325,11 +315,11 @@ do_regcomp (void)
preg = malloc (sizeof (regex_t)); preg = malloc (sizeof (regex_t));
if (preg == NULL) if (preg == NULL)
exit (3); exit (EXIT_FATAL);
if (opt_exact) { if (opt_exact) {
re = malloc (strlen (opt_pattern) + 5); re = malloc (strlen (opt_pattern) + 5);
if (re == NULL) if (re == NULL)
exit (3); exit (EXIT_FATAL);
sprintf (re, "^(%s)$", opt_pattern); sprintf (re, "^(%s)$", opt_pattern);
} else { } else {
re = opt_pattern; re = opt_pattern;
@ -339,14 +329,13 @@ do_regcomp (void)
if (re_err) { if (re_err) {
regerror (re_err, preg, errbuf, sizeof(errbuf)); regerror (re_err, preg, errbuf, sizeof(errbuf));
fputs(errbuf,stderr); fputs(errbuf,stderr);
exit (2); exit (EXIT_USAGE);
} }
} }
return preg; return preg;
} }
static union el * static union el * select_procs (int *num)
select_procs (int *num)
{ {
PROCTAB *ptp; PROCTAB *ptp;
proc_t task; proc_t task;
@ -447,7 +436,7 @@ select_procs (int *num)
size = size * 5 / 4 + 4; size = size * 5 / 4 + 4;
list = realloc(list, size * sizeof *list); list = realloc(list, size * sizeof *list);
if (list == NULL) if (list == NULL)
exit (3); exit (EXIT_FATAL);
} }
if (opt_long) { if (opt_long) {
char buff[5096]; // FIXME char buff[5096]; // FIXME
@ -467,8 +456,7 @@ select_procs (int *num)
} }
static void static void parse_opts (int argc, char **argv)
parse_opts (int argc, char **argv)
{ {
char opts[32] = ""; char opts[32] = "";
int opt; int opt;
@ -539,7 +527,7 @@ parse_opts (int argc, char **argv)
break; break;
case 'V': case 'V':
fprintf(stdout, "%s (%s)\n", progname, procps_version); fprintf(stdout, "%s (%s)\n", progname, procps_version);
exit(0); exit(EXIT_SUCCESS);
// case 'c': // Solaris: match by contract ID // case 'c': // Solaris: match by contract ID
// break; // break;
case 'd': // Solaris: change the delimiter case 'd': // Solaris: change the delimiter
@ -621,8 +609,7 @@ parse_opts (int argc, char **argv)
} }
int int main (int argc, char *argv[])
main (int argc, char **argv)
{ {
union el *procs; union el *procs;
int num; int num;
@ -644,5 +631,5 @@ main (int argc, char **argv)
else else
output_numlist(procs,num); output_numlist(procs,num);
} }
return !num; return !num; // exit(EXIT_SUCCESS) if match, otherwise exit(EXIT_FAILURE)
} }

8
top.1
View File

@ -373,10 +373,10 @@ processes to change \*(PUs more often (because of the extra demand for
.TP 3 .TP 3
k:\fB %CPU\fR \*(EM \*(PU usage k:\fB %CPU\fR \*(EM \*(PU usage
The task's share of the elapsed \*(PU time since the last screen update, expressed The task's share of the elapsed \*(PU time since the last screen update,
as a percentage of total \*(PU time. expressed as a percentage of total \*(PU time.
In a true SMP environment, if 'Irix mode' is \*F, \*(Me will operate in In a true SMP environment, if 'Irix mode' is \*F, \*(Me will operate
\'Solaris mode' where a task's \*(Pu usage will be divided by the total in 'Solaris mode' where a task's \*(Pu usage will be divided by the total
number of \*(PUs. number of \*(PUs.
You toggle 'Irix/Solaris' modes with the 'I' \*(CI. You toggle 'Irix/Solaris' modes with the 'I' \*(CI.