Mercurial > illumos > illumos-gate
changeset 9966:d4ba9662e3c8
PSARC/2009/182 prstat(1M) -r option
5073035 prstat could use a -r option which disables name lookups
6736091 u_name member of name_t struct not used in prstat(1M)
Contributed by Chad Mynhier <cmynhier@gmail.com>.
author | Menno Lageman <Menno.Lageman@Sun.COM> |
---|---|
date | Wed, 24 Jun 2009 22:52:54 -0700 |
parents | e5142f0d02b1 |
children | e0258b956de2 |
files | usr/src/cmd/prstat/prstat.c usr/src/cmd/prstat/prstat.h usr/src/cmd/prstat/prtable.c usr/src/cmd/prstat/prtable.h usr/src/cmd/prstat/prutil.c usr/src/cmd/prstat/prutil.h |
diffstat | 6 files changed, 52 insertions(+), 47 deletions(-) [+] |
line wrap: on
line diff
--- a/usr/src/cmd/prstat/prstat.c Wed Jun 24 18:18:47 2009 -0700 +++ b/usr/src/cmd/prstat/prstat.c Wed Jun 24 22:52:54 2009 -0700 @@ -22,6 +22,8 @@ /* * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. + * + * Portions Copyright 2009 Chad Mynhier */ #include <sys/types.h> @@ -141,8 +143,8 @@ static table_t tsk_tbl = {0, 0, NULL}; /* selected tasks */ static table_t lgr_tbl = {0, 0, NULL}; /* selected lgroups */ static zonetbl_t zone_tbl = {0, 0, NULL}; /* selected zones */ -static nametbl_t euid_tbl = {0, 0, NULL}; /* selected effective users */ -static nametbl_t ruid_tbl = {0, 0, NULL}; /* selected real users */ +static uidtbl_t euid_tbl = {0, 0, NULL}; /* selected effective users */ +static uidtbl_t ruid_tbl = {0, 0, NULL}; /* selected real users */ static uint_t total_procs; /* total number of procs */ static uint_t total_lwps; /* total number of lwps */ @@ -445,13 +447,15 @@ else mem = id->id_pctmem; if (list->l_type == LT_USERS) - pwd_getname(id->id_uid, pname, LOGNAME_MAX + 1); + pwd_getname(id->id_uid, pname, LOGNAME_MAX + 1, + opts.o_outpmode & OPT_NORESOLVE); else if (list->l_type == LT_ZONES) getzonename(id->id_zoneid, zonename, ZONENAME_MAX); else getprojname(id->id_projid, projname, - PROJNAME_MAX); + PROJNAME_MAX, + opts.o_outpmode & OPT_NORESOLVE); Format_size(psize, id->id_size, 6); Format_size(prssize, id->id_rssize, 6); Format_pct(pmem, mem, 4); @@ -484,8 +488,8 @@ else lwpid = lwp->li_info.pr_nlwp + lwp->li_info.pr_nzomb; - pwd_getname(lwp->li_info.pr_uid, pname, - LOGNAME_MAX + 1); + pwd_getname(lwp->li_info.pr_uid, pname, LOGNAME_MAX + 1, + opts.o_outpmode & OPT_NORESOLVE); if (opts.o_outpmode & OPT_PSINFO) { Format_size(psize, lwp->li_info.pr_size, 6); Format_size(prssize, lwp->li_info.pr_rssize, 6); @@ -1360,8 +1364,11 @@ pagesize = sysconf(_SC_PAGESIZE); while ((opt = getopt(argc, argv, - "vcd:HmaRLtu:U:n:p:C:P:h:s:S:j:k:TJz:Z")) != (int)EOF) { + "vcd:HmarRLtu:U:n:p:C:P:h:s:S:j:k:TJz:Z")) != (int)EOF) { switch (opt) { + case 'r': + opts.o_outpmode |= OPT_NORESOLVE; + break; case 'R': opts.o_outpmode |= OPT_REALTIME; break;
--- a/usr/src/cmd/prstat/prstat.h Wed Jun 24 18:18:47 2009 -0700 +++ b/usr/src/cmd/prstat/prstat.h Wed Jun 24 22:52:54 2009 -0700 @@ -22,6 +22,8 @@ /* * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. + * + * Portions Copyright 2009 Chad Mynhier */ #ifndef _PRSTAT_H @@ -70,6 +72,7 @@ #define OPT_LGRP 0x8000 /* report home lgroups */ #define OPT_UDATE 0x20000 /* print unix timestamp */ #define OPT_DDATE 0x40000 /* print timestamp in date(1) format */ +#define OPT_NORESOLVE 0x80000 /* no nsswitch lookups */ /* * Flags to keep track of process or lwp status
--- a/usr/src/cmd/prstat/prtable.c Wed Jun 24 18:18:47 2009 -0700 +++ b/usr/src/cmd/prstat/prtable.c Wed Jun 24 22:52:54 2009 -0700 @@ -19,12 +19,12 @@ * CDDL HEADER END */ /* - * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. + * + * Portions Copyright 2009 Chad Mynhier */ -#pragma ident "%Z%%M% %I% %E% SMI" - #include <procfs.h> #include <unistd.h> #include <stdlib.h> @@ -59,11 +59,11 @@ } void -pwd_getname(uid_t uid, char *name, int length) +pwd_getname(uid_t uid, char *name, int length, int noresolve) { struct passwd *pwd; - if ((pwd = getpwuid(uid)) == NULL) { + if (noresolve || (pwd = getpwuid(uid)) == NULL) { (void) snprintf(name, length, "%u", uid); } else { (void) snprintf(name, length, "%s", pwd->pw_name); @@ -71,35 +71,33 @@ } void -add_uid(nametbl_t *tbl, char *name) +add_uid(uidtbl_t *tbl, char *name) { - name_t *entp; + uid_t *uid; if (tbl->n_size == tbl->n_nent) { /* reallocation */ if ((tbl->n_size *= 2) == 0) tbl->n_size = 4; /* first time */ - tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (name_t)); + tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (uid_t)); } - entp = &tbl->n_list[tbl->n_nent++]; + uid = &tbl->n_list[tbl->n_nent++]; if (isdigit(name[0])) { - entp->u_id = Atoi(name); - pwd_getname(entp->u_id, entp->u_name, LOGNAME_MAX); + *uid = Atoi(name); } else { - entp->u_id = pwd_getid(name); - (void) snprintf(entp->u_name, LOGNAME_MAX, "%s", name); + *uid = pwd_getid(name); } } int -has_uid(nametbl_t *tbl, uid_t uid) +has_uid(uidtbl_t *tbl, uid_t uid) { size_t i; if (tbl->n_nent) { /* do linear search if table is not empty */ for (i = 0; i < tbl->n_nent; i++) - if (tbl->n_list[i].u_id == uid) + if (tbl->n_list[i] == uid) return (1); } else { return (1); /* if table is empty return true */
--- a/usr/src/cmd/prstat/prtable.h Wed Jun 24 18:18:47 2009 -0700 +++ b/usr/src/cmd/prstat/prtable.h Wed Jun 24 22:52:54 2009 -0700 @@ -19,15 +19,15 @@ * CDDL HEADER END */ /* - * Copyright 2007 Sun Microsystems, Inc. All rights reserved. + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. + * + * Portions Copyright 2009 Chad Mynhier */ #ifndef _PRTABLE_H #define _PRTABLE_H -#pragma ident "%Z%%M% %I% %E% SMI" - #ifdef __cplusplus extern "C" { #endif @@ -46,15 +46,10 @@ } table_t; typedef struct { - uid_t u_id; - char u_name[LOGNAME_MAX+1]; -} name_t; - -typedef struct { size_t n_size; size_t n_nent; - name_t *n_list; -} nametbl_t; + uid_t *n_list; +} uidtbl_t; typedef struct { zoneid_t z_id; @@ -75,9 +70,9 @@ struct plwp *l_next; } plwp_t; -extern void pwd_getname(uid_t, char *, int); -extern void add_uid(nametbl_t *, char *); -extern int has_uid(nametbl_t *, uid_t); +extern void pwd_getname(uid_t, char *, int, int); +extern void add_uid(uidtbl_t *, char *); +extern int has_uid(uidtbl_t *, uid_t); extern void add_element(table_t *, long); extern int has_element(table_t *, long); extern void add_zone(zonetbl_t *, char *);
--- a/usr/src/cmd/prstat/prutil.c Wed Jun 24 18:18:47 2009 -0700 +++ b/usr/src/cmd/prstat/prutil.c Wed Jun 24 22:52:54 2009 -0700 @@ -21,6 +21,8 @@ /* * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. + * + * Portions Copyright 2009 Chad Mynhier */ #include <sys/types.h> @@ -104,7 +106,7 @@ Usage() { (void) fprintf(stderr, gettext( - "Usage:\tprstat [-acHJLmRtTvZ] [-u euidlist] [-U uidlist]\n" + "Usage:\tprstat [-acHJLmrRtTvZ] [-u euidlist] [-U uidlist]\n" "\t[-p pidlist] [-P cpulist] [-C psrsetlist] [-h lgrouplist]\n" "\t[-j projidlist] [-k taskidlist] [-z zoneidlist]\n" "\t[-s key | -S key] [-n nprocs[,nusers]] [-d d|u]\n" @@ -276,14 +278,15 @@ } void -getprojname(projid_t projid, char *str, int len) +getprojname(projid_t projid, char *str, int len, int noresolve) { struct project proj; - if (getprojbyid(projid, &proj, projbuf, PROJECT_BUFSZ) != NULL) + if (noresolve || getprojbyid(projid, &proj, projbuf, PROJECT_BUFSZ) == + NULL) + (void) snprintf(str, len, "%-6d", (int)projid); + else (void) snprintf(str, len, "%-28s", proj.pj_name); - else - (void) snprintf(str, len, "%-6d", (int)projid); } void
--- a/usr/src/cmd/prstat/prutil.h Wed Jun 24 18:18:47 2009 -0700 +++ b/usr/src/cmd/prstat/prutil.h Wed Jun 24 22:52:54 2009 -0700 @@ -2,9 +2,8 @@ * CDDL HEADER START * * The contents of this file are subject to the terms of the - * Common Development and Distribution License, Version 1.0 only - * (the "License"). You may not use this file except in compliance - * with the License. + * Common Development and Distribution License (the "License"). + * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. @@ -20,15 +19,15 @@ * CDDL HEADER END */ /* - * Copyright 2003 Sun Microsystems, Inc. All rights reserved. + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. + * + * Portions Copyright 2009 Chad Mynhier */ #ifndef _PRUTIL_H #define _PRUTIL_H -#pragma ident "%Z%%M% %I% %E% SMI" - #include <sys/processor.h> #include <sys/types.h> @@ -51,7 +50,7 @@ extern void *Zalloc(size_t); extern int Setrlimit(); extern void Priocntl(char *); -extern void getprojname(projid_t, char *, int); +extern void getprojname(projid_t, char *, int, int); extern void getzonename(projid_t, char *, int); extern void stripfname(char *);