From owner-svn-src-all@FreeBSD.ORG Thu Jun 11 17:03:29 2009 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 6766510657EE; Thu, 11 Jun 2009 17:03:29 +0000 (UTC) (envelope-from stas@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id B015D8FC12; Thu, 11 Jun 2009 17:03:28 +0000 (UTC) (envelope-from stas@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n5BH3SkX053810; Thu, 11 Jun 2009 17:03:28 GMT (envelope-from stas@svn.freebsd.org) Received: (from stas@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n5BH3Sb1053808; Thu, 11 Jun 2009 17:03:28 GMT (envelope-from stas@svn.freebsd.org) Message-Id: <200906111703.n5BH3Sb1053808@svn.freebsd.org> From: Stanislav Sedov Date: Thu, 11 Jun 2009 17:03:28 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r194014 - head/usr.sbin/pstat X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Jun 2009 17:03:30 -0000 Author: stas Date: Thu Jun 11 17:03:28 2009 New Revision: 194014 URL: http://svn.freebsd.org/changeset/base/194014 Log: - Make pstat(8) WARNS=6 safe. - While here, eliminate the check for len > 0 in ttymode_sysctl as the code is able to handle this case well. Reviewed by: ed (initial version) Modified: head/usr.sbin/pstat/Makefile head/usr.sbin/pstat/pstat.c Modified: head/usr.sbin/pstat/Makefile ============================================================================== --- head/usr.sbin/pstat/Makefile Thu Jun 11 17:03:15 2009 (r194013) +++ head/usr.sbin/pstat/Makefile Thu Jun 11 17:03:28 2009 (r194014) @@ -6,7 +6,7 @@ LINKS= ${BINDIR}/pstat ${BINDIR}/swapinf MAN= pstat.8 MLINKS= pstat.8 swapinfo.8 -WARNS?= 3 +WARNS?= 6 DPADD= ${LIBKVM} ${LIBUTIL} LDADD= -lkvm -lutil Modified: head/usr.sbin/pstat/pstat.c ============================================================================== --- head/usr.sbin/pstat/pstat.c Thu Jun 11 17:03:15 2009 (r194013) +++ head/usr.sbin/pstat/pstat.c Thu Jun 11 17:03:28 2009 (r194014) @@ -76,16 +76,22 @@ enum { NL_CONSTTY, NL_MAXFILES, NL_NFILES, - NL_TTY_LIST + NL_TTY_LIST, + NL_MARKER }; -static struct nlist nl[] = { - { .n_name = "_constty" }, - { .n_name = "_maxfiles" }, - { .n_name = "_openfiles" }, - { .n_name = "_tty_list" }, - { .n_name = "" } +static struct { + int order; + const char *name; +} namelist[] = { + { NL_CONSTTY, "_constty" }, + { NL_MAXFILES, "_maxfiles" }, + { NL_NFILES, "_openfiles" }, + { NL_TTY_LIST, "_tty_list" }, + { NL_MARKER, "" }, }; +#define NNAMES (sizeof(namelist) / sizeof(*namelist)) +static struct nlist nl[NNAMES]; static int humanflag; static int usenumflag; @@ -98,7 +104,7 @@ static kvm_t *kd; static const char *usagestr; static void filemode(void); -static int getfiles(char **, size_t *); +static int getfiles(struct xfile **, size_t *); static void swapmode(void); static void ttymode(void); static void ttyprt(struct xtty *); @@ -107,8 +113,9 @@ static void usage(void); int main(int argc, char *argv[]) { - int ch, i, quit, ret; + int ch, quit, ret; int fileflag, ttyflag; + unsigned int i; char buf[_POSIX2_LINE_MAX]; const char *opts; @@ -170,6 +177,12 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; + /* + * Initialize symbol names list. + */ + for (i = 0; i < NNAMES; i++) + nl[namelist[i].order].n_name = strdup(namelist[i].name); + if (memf != NULL) { kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); if (kd == NULL) @@ -253,12 +266,12 @@ ttymode_kvm(void) static void ttymode_sysctl(void) { - struct xtty *xt, *end; - void *xttys; + struct xtty *xttys; size_t len; + unsigned int i, n; (void)printf("%s", hdr); - if ((xttys = malloc(len = sizeof *xt)) == NULL) + if ((xttys = malloc(len = sizeof(*xttys))) == NULL) err(1, "malloc()"); while (sysctlbyname("kern.ttys", xttys, &len, 0, 0) == -1) { if (errno != ENOMEM) @@ -267,11 +280,9 @@ ttymode_sysctl(void) if ((xttys = realloc(xttys, len)) == NULL) err(1, "realloc()"); } - if (len > 0) { - end = (struct xtty *)((char *)xttys + len); - for (xt = xttys; xt < end; xt++) - ttyprt(xt); - } + n = len / sizeof(*xttys); + for (i = 0; i < n; i++) + ttyprt(&xttys[i]); } static void @@ -355,8 +366,8 @@ ttyprt(struct xtty *xt) static void filemode(void) { - struct xfile *fp; - char *buf, flagbuf[16], *fbp; + struct xfile *fp, *buf; + char flagbuf[16], *fbp; int maxf, openf; size_t len; static char const * const dtypes[] = { "???", "inode", "socket", @@ -413,11 +424,11 @@ filemode(void) } static int -getfiles(char **abuf, size_t *alen) +getfiles(struct xfile **abuf, size_t *alen) { + struct xfile *buf; size_t len; int mib[2]; - char *buf; /* * XXX