From owner-svn-src-projects@FreeBSD.ORG Thu Jul 9 12:48:44 2009 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 303921065676; Thu, 9 Jul 2009 12:48:44 +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 13C868FC17; Thu, 9 Jul 2009 12:48:44 +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 n69Cmhf6085404; Thu, 9 Jul 2009 12:48:43 GMT (envelope-from stas@svn.freebsd.org) Received: (from stas@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n69CmhX3085403; Thu, 9 Jul 2009 12:48:43 GMT (envelope-from stas@svn.freebsd.org) Message-Id: <200907091248.n69CmhX3085403@svn.freebsd.org> From: Stanislav Sedov Date: Thu, 9 Jul 2009 12:48:43 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r195490 - projects/libprocstat/usr.bin/fstat X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 09 Jul 2009 12:48:44 -0000 Author: stas Date: Thu Jul 9 12:48:43 2009 New Revision: 195490 URL: http://svn.freebsd.org/changeset/base/195490 Log: - Delete old ifdefs. - Allocate memory dynamically whenever needed instead of using global buffer. Modified: projects/libprocstat/usr.bin/fstat/fstat.c Modified: projects/libprocstat/usr.bin/fstat/fstat.c ============================================================================== --- projects/libprocstat/usr.bin/fstat/fstat.c Thu Jul 9 11:48:48 2009 (r195489) +++ projects/libprocstat/usr.bin/fstat/fstat.c Thu Jul 9 12:48:43 2009 (r195490) @@ -93,7 +93,6 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include #include @@ -113,12 +112,6 @@ __FBSDID("$FreeBSD$"); #define MMAP -5 #define JDIR -6 -#ifdef notdef -struct nlist nl[] = { - { "" }, -}; -#endif - int fsflg, /* show files on same filesystem as file(s) argument */ pflg, /* show files open by a particular pid */ uflg; /* show files open by a particular (effective) user */ @@ -126,19 +119,6 @@ int checkfile; /* true if restricting t int nflg; /* (numerical) display f.s. and rdev as dev_t */ int mflg; /* include memory-mapped files */ - -struct file **ofiles; /* buffer of pointers to file structures */ -int maxfiles; -#define ALLOC_OFILES(d) \ - if ((d) > maxfiles) { \ - free(ofiles); \ - ofiles = malloc((d) * sizeof(struct file *)); \ - if (ofiles == NULL) { \ - err(1, NULL); \ - } \ - maxfiles = (d); \ - } - typedef struct devs { struct devs *next; long fsid; @@ -268,8 +248,6 @@ fstat_kvm(int what, int arg) char buf[_POSIX2_LINE_MAX]; int cnt; - ALLOC_OFILES(256); /* reserve space for file pointers */ - /* * Discard setgid privileges if not the running kernel so that bad * guys can't print interesting stuff from kernel memory. @@ -280,10 +258,6 @@ fstat_kvm(int what, int arg) if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) errx(1, "%s", buf); setgid(getgid()); -#ifdef notdef - if (kvm_nlist(kd, nl) != 0) - errx(1, "no namelist: %s", kvm_geterr(kd)); -#endif if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) errx(1, "%s", kvm_geterr(kd)); print_header(); @@ -395,6 +369,8 @@ dofiles(struct kinfo_proc *kp) int i; struct file file; struct filedesc filed; + unsigned int nfiles; + struct file **ofiles; Uname = user_from_uid(kp->ki_uid, 0); Pid = kp->ki_pid; @@ -443,12 +419,18 @@ dofiles(struct kinfo_proc *kp) if (filed.fd_lastfile <= -1 || filed.fd_lastfile > MAX_LASTFILE) return; - ALLOC_OFILES(filed.fd_lastfile+1); + nfiles = filed.fd_lastfile + 1; + ofiles = malloc(nfiles * sizeof(struct file *)); + if (ofiles == NULL) { + warn("malloc(%zd)", nfiles * sizeof(struct file *)); + return; + } if (!kvm_read_all(kd, (unsigned long)filed.fd_ofiles, ofiles, (filed.fd_lastfile+1) * FPSIZE)) { dprintf(stderr, "can't read file structures at %p for pid %d\n", (void *)filed.fd_ofiles, Pid); + free(ofiles); return; } for (i = 0; i <= filed.fd_lastfile; i++) { @@ -490,6 +472,7 @@ dofiles(struct kinfo_proc *kp) file.f_type, i, Pid); } } + free(ofiles); } /*