Module Name: src Committed By: msaitoh Date: Fri Mar 27 10:44:03 UTC 2009
Modified Files: src/usr.sbin/pstat [netbsd-3-0]: pstat.c Log Message: Pull up following revision(s) (requested by mrg in ticket #2000): usr.sbin/pstat/pstat.c: revision 1.113 convert getfiles() to use KERN_FILE2 sysctl. now it can survive "struct file" changing, as is upcoming. To generate a diff of this commit: cvs rdiff -u -r1.88 -r1.88.4.1 src/usr.sbin/pstat/pstat.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.sbin/pstat/pstat.c diff -u src/usr.sbin/pstat/pstat.c:1.88 src/usr.sbin/pstat/pstat.c:1.88.4.1 --- src/usr.sbin/pstat/pstat.c:1.88 Sat Dec 18 08:43:35 2004 +++ src/usr.sbin/pstat/pstat.c Fri Mar 27 10:44:03 2009 @@ -1,4 +1,4 @@ -/* $NetBSD: pstat.c,v 1.88 2004/12/18 08:43:35 christos Exp $ */ +/* $NetBSD: pstat.c,v 1.88.4.1 2009/03/27 10:44:03 msaitoh Exp $ */ /*- * Copyright (c) 1980, 1991, 1993, 1994 @@ -39,7 +39,7 @@ #if 0 static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95"; #else -__RCSID("$NetBSD: pstat.c,v 1.88 2004/12/18 08:43:35 christos Exp $"); +__RCSID("$NetBSD: pstat.c,v 1.88.4.1 2009/03/27 10:44:03 msaitoh Exp $"); #endif #endif /* not lint */ @@ -878,8 +878,7 @@ void filemode() { - struct file *fp; - struct file *addr; + struct kinfo_file *ki; char flags[sizeof(filemode_flags) / sizeof(filemode_flags[0])]; char *buf, *offset; int len, maxfile, nfile, ovflw; @@ -893,37 +892,34 @@ if (getfiles(&buf, &len, &offset) == -1) return; /* - * Getfiles returns in malloc'd memory a pointer to the first file - * structure, and then an array of file structs (whose addresses are - * derivable from the previous entry). + * Getfiles returns in malloc'd memory to an array of kinfo_file2 + * structures. */ - addr = ((struct filelist *)offset)->lh_first; - fp = (struct file *)(offset + sizeof(struct filelist)); - nfile = (len - sizeof(struct filelist)) / sizeof(struct file); + nfile = len / sizeof(struct kinfo_file); (void)printf("%d/%d open files\n", nfile, maxfile); (void)printf("%*s%s%*s TYPE FLG CNT MSG %*s%s%*s USE IFLG OFFSET\n", (PTRSTRWIDTH - 4) / 2, "", " LOC", (PTRSTRWIDTH - 4) / 2, "", (PTRSTRWIDTH - 4) / 2, "", "DATA", (PTRSTRWIDTH - 4) / 2, ""); - for (; (char *)fp < offset + len; addr = fp->f_list.le_next, fp++) { - if ((unsigned)fp->f_type > sizeof(dtypes) / sizeof(dtypes[0])) + for (ki = (struct kinfo_file *)offset; nfile--; ki++) { + if ((unsigned)ki->ki_ftype >= sizeof(dtypes) / sizeof(dtypes[0])) continue; ovflw = 0; - (void)getflags(filemode_flags, flags, fp->f_flag); - PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)addr); - PRWORD(ovflw, " %-*s", 9, 1, dtypes[fp->f_type]); + (void)getflags(filemode_flags, flags, ki->ki_flag); + PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)ki->ki_fileaddr); + PRWORD(ovflw, " %-*s", 9, 1, dtypes[ki->ki_ftype]); PRWORD(ovflw, " %*s", 6, 1, flags); - PRWORD(ovflw, " %*d", 5, 1, fp->f_count); - PRWORD(ovflw, " %*d", 5, 1, fp->f_msgcount); - PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 2, (long)fp->f_data); - PRWORD(ovflw, " %*d", 5, 1, fp->f_usecount); - PRWORD(ovflw, " %*x", 5, 1, fp->f_iflags); - if (fp->f_offset < 0) + PRWORD(ovflw, " %*d", 5, 1, ki->ki_count); + PRWORD(ovflw, " %*d", 5, 1, ki->ki_msgcount); + PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 2, (long)ki->ki_fdata); + PRWORD(ovflw, " %*d", 5, 1, ki->ki_usecount); + PRWORD(ovflw, " %*x", 5, 1, ki->ki_iflags); + if (ki->ki_foffset < 0) PRWORD(ovflw, " %-*lld\n", PTRSTRWIDTH + 1, 2, - (long long)fp->f_offset); + (long long)ki->ki_foffset); else PRWORD(ovflw, " %-*lld\n", PTRSTRWIDTH + 1, 2, - (long long)fp->f_offset); + (long long)ki->ki_foffset); } free(buf); } @@ -935,7 +931,7 @@ char **aoffset; { size_t len; - int mib[2]; + int mib[6]; char *buf; size_t offset; @@ -947,17 +943,22 @@ errx(1, "files on dead kernel, not implemented"); mib[0] = CTL_KERN; - mib[1] = KERN_FILE; - if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { - warn("sysctl: KERN_FILE"); + mib[1] = KERN_FILE2; + mib[2] = KERN_FILE_BYFILE; + mib[3] = 0; + mib[4] = sizeof(struct kinfo_file); + mib[5] = 0; + if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) { + warn("sysctl: KERN_FILE2"); return (-1); } - /* We need to align (struct file *) in the buffer. */ + /* We need to align (struct kinfo_file *) in the buffer. */ offset = len % sizeof(off_t); + mib[5] = len / sizeof(struct kinfo_file); if ((buf = malloc(len + offset)) == NULL) err(1, "malloc"); - if (sysctl(mib, 2, buf + offset, &len, NULL, 0) == -1) { - warn("sysctl: KERN_FILE"); + if (sysctl(mib, 6, buf + offset, &len, NULL, 0) == -1) { + warn("sysctl: 2nd KERN_FILE2"); return (-1); } *abuf = buf;