From owner-svn-src-user@FreeBSD.ORG Sun Nov 30 22:40:15 2008 Return-Path: Delivered-To: svn-src-user@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 353101065673; Sun, 30 Nov 2008 22:40:15 +0000 (UTC) (envelope-from peter@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 2446D8FC16; Sun, 30 Nov 2008 22:40:15 +0000 (UTC) (envelope-from peter@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 mAUMeFD8004217; Sun, 30 Nov 2008 22:40:15 GMT (envelope-from peter@svn.freebsd.org) Received: (from peter@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id mAUMeEx9004213; Sun, 30 Nov 2008 22:40:14 GMT (envelope-from peter@svn.freebsd.org) Message-Id: <200811302240.mAUMeEx9004213@svn.freebsd.org> From: Peter Wemm Date: Sun, 30 Nov 2008 22:40:14 +0000 (UTC) To: src-committers@freebsd.org, svn-src-user@freebsd.org X-SVN-Group: user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r185493 - user/peter/kinfo/lib/libutil X-BeenThere: svn-src-user@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the experimental " user" src tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 30 Nov 2008 22:40:15 -0000 Author: peter Date: Sun Nov 30 22:40:14 2008 New Revision: 185493 URL: http://svn.freebsd.org/changeset/base/185493 Log: Add experimental front ends to the kinfo_vmentry and kinfo_filedesc sysctls. Added: user/peter/kinfo/lib/libutil/kinfo_getfile.c (contents, props changed) user/peter/kinfo/lib/libutil/kinfo_getvmmap.c (contents, props changed) Modified: user/peter/kinfo/lib/libutil/Makefile user/peter/kinfo/lib/libutil/libutil.h Modified: user/peter/kinfo/lib/libutil/Makefile ============================================================================== --- user/peter/kinfo/lib/libutil/Makefile Sun Nov 30 22:40:11 2008 (r185492) +++ user/peter/kinfo/lib/libutil/Makefile Sun Nov 30 22:40:14 2008 (r185493) @@ -9,7 +9,8 @@ LIB= util SHLIB_MAJOR= 7 SRCS= _secure_path.c auth.c expand_number.c flopen.c fparseln.c gr_util.c \ - hexdump.c humanize_number.c kld.c login.c login_auth.c login_cap.c \ + hexdump.c humanize_number.c kinfo_getfile.c kinfo_getvmmap.c kld.c \ + login.c login_auth.c login_cap.c \ login_class.c login_crypt.c login_ok.c login_times.c login_tty.c \ logout.c logwtmp.c pidfile.c property.c pty.c pw_util.c realhostname.c \ stub.c trimdomain.c uucplock.c Added: user/peter/kinfo/lib/libutil/kinfo_getfile.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/peter/kinfo/lib/libutil/kinfo_getfile.c Sun Nov 30 22:40:14 2008 (r185493) @@ -0,0 +1,72 @@ +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include + +#include "libutil.h" + +struct kinfo_file * +kinfo_getfile(pid_t pid, int *cntp) +{ + int mib[4]; + int error; + int cnt; + size_t len; + char *buf, *bp, *eb; + struct kinfo_file *kif, *kp, *kf; + + len = 0; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_FILEDESC; + mib[3] = pid; + + error = sysctl(mib, 4, NULL, &len, NULL, 0); + if (error) + return (0); + len = len * 4 / 3; + buf = malloc(len); + if (buf == NULL) + return (0); + error = sysctl(mib, 4, buf, &len, NULL, 0); + if (error) { + free(buf); + return (0); + } + /* Pass 1: count items */ + cnt = 0; + bp = buf; + eb = buf + len; + while (bp < eb) { + kf = (struct kinfo_file *)bp; + bp += kf->kf_structsize; + cnt++; + } + + kif = calloc(cnt, sizeof(*kif)); + if (kif == NULL) { + free(buf); + return (0); + } + bp = buf; + eb = buf + len; + kp = kif; + /* Pass 2: unpack */ + while (bp < eb) { + kf = (struct kinfo_file *)bp; + /* Copy/expand into pre-zeroed buffer */ + memcpy(kp, kf, kf->kf_structsize); + /* Advance to next packed record */ + bp += kf->kf_structsize; + /* Set field size to fixed length, advance */ + kp->kf_structsize = sizeof(*kp); + kp++; + } + free(buf); + *cntp = cnt; + return (kif); /* Caller must free() return value */ +} Added: user/peter/kinfo/lib/libutil/kinfo_getvmmap.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ user/peter/kinfo/lib/libutil/kinfo_getvmmap.c Sun Nov 30 22:40:14 2008 (r185493) @@ -0,0 +1,72 @@ +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include + +#include "libutil.h" + +struct kinfo_vmentry * +kinfo_getvmmap(pid_t pid, int *cntp) +{ + int mib[4]; + int error; + int cnt; + size_t len; + char *buf, *bp, *eb; + struct kinfo_vmentry *kiv, *kp, *kv; + + len = 0; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_VMMAP; + mib[3] = pid; + + error = sysctl(mib, 4, NULL, &len, NULL, 0); + if (error) + return (0); + len = len * 4 / 3; + buf = malloc(len); + if (buf == NULL) + return (0); + error = sysctl(mib, 4, buf, &len, NULL, 0); + if (error) { + free(buf); + return (0); + } + /* Pass 1: count items */ + cnt = 0; + bp = buf; + eb = buf + len; + while (bp < eb) { + kv = (struct kinfo_vmentry *)bp; + bp += kv->kve_structsize; + cnt++; + } + + kiv = calloc(cnt, sizeof(*kiv)); + if (kiv == NULL) { + free(buf); + return (0); + } + bp = buf; + eb = buf + len; + kp = kiv; + /* Pass 2: unpack */ + while (bp < eb) { + kv = (struct kinfo_vmentry *)bp; + /* Copy/expand into pre-zeroed buffer */ + memcpy(kp, kv, kv->kve_structsize); + /* Advance to next packed record */ + bp += kv->kve_structsize; + /* Set field size to fixed length, advance */ + kp->kve_structsize = sizeof(*kp); + kp++; + } + free(buf); + *cntp = cnt; + return (kiv); /* Caller must free() return value */ +} Modified: user/peter/kinfo/lib/libutil/libutil.h ============================================================================== --- user/peter/kinfo/lib/libutil/libutil.h Sun Nov 30 22:40:11 2008 (r185492) +++ user/peter/kinfo/lib/libutil/libutil.h Sun Nov 30 22:40:14 2008 (r185493) @@ -64,6 +64,8 @@ struct termios; struct winsize; struct utmp; struct in_addr; +struct kinfo_file; +struct kinfo_vmentry; __BEGIN_DECLS void clean_environment(const char * const *_white, @@ -100,6 +102,10 @@ int realhostname_sa(char *host, size_t h int kld_isloaded(const char *name); int kld_load(const char *name); +struct kinfo_file * + kinfo_getfile(pid_t _pid, int *_cntp); +struct kinfo_vmentry * + kinfo_getvmmap(pid_t _pid, int *_cntp); #ifdef _STDIO_H_ /* avoid adding new includes */ char *fparseln(FILE *, size_t *, size_t *, const char[3], int);