From 1c55bd1c118641b2294a4252a425a3bd50b98615 Mon Sep 17 00:00:00 2001 From: Joerg Sonnenberger Date: Wed, 24 Nov 2004 22:51:01 +0000 Subject: [PATCH] Add the basic of libkcore. Switch pstat to use kcore/kinfo backing, defaulting to kcore for now. --- lib/Makefile | 6 +- lib/libkcore/Makefile | 16 ++ .../kinfo_vfs.c => libkcore/kcore.c} | 43 +++-- lib/{libkinfo/kinfo.h => libkcore/kcore.h} | 38 +++- lib/libkcore/kcore_file.c | 169 ++++++++++++++++++ .../kinfo.h => libkcore/kcore_private.h} | 19 +- .../kinfo_vfs.c => libkcore/kcore_proc.c} | 35 +++- .../kinfo_vfs.c => libkcore/kcore_vfs.c} | 33 +++- lib/libkinfo/kinfo.h | 7 +- lib/libkinfo/kinfo_file.c | 3 +- lib/libkinfo/kinfo_vfs.c | 9 +- share/mk/bsd.libnames.mk | 3 +- sys/conf/files | 3 +- sys/kern/kern_descrip.c | 17 +- .../kinfo_vfs.c => sys/kern/subr_kcore.c | 42 +++-- lib/libkinfo/kinfo.h => sys/sys/kcore.h | 17 +- usr.sbin/pstat/Makefile | 7 +- usr.sbin/pstat/pstat.c | 91 ++++------ 18 files changed, 406 insertions(+), 152 deletions(-) create mode 100644 lib/libkcore/Makefile copy lib/{libkinfo/kinfo_vfs.c => libkcore/kcore.c} (71%) copy lib/{libkinfo/kinfo.h => libkcore/kcore.h} (64%) create mode 100644 lib/libkcore/kcore_file.c copy lib/{libkinfo/kinfo.h => libkcore/kcore_private.h} (84%) copy lib/{libkinfo/kinfo_vfs.c => libkcore/kcore_proc.c} (71%) copy lib/{libkinfo/kinfo_vfs.c => libkcore/kcore_vfs.c} (73%) copy lib/libkinfo/kinfo_vfs.c => sys/kern/subr_kcore.c (68%) copy lib/libkinfo/kinfo.h => sys/sys/kcore.h (84%) diff --git a/lib/Makefile b/lib/Makefile index 09576c02f6..0d7583ff18 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,6 +1,6 @@ # @(#)Makefile 8.1 (Berkeley) 6/4/93 # $FreeBSD: src/lib/Makefile,v 1.107.2.16 2002/10/10 19:24:35 kbyanc Exp $ -# $DragonFly: src/lib/Makefile,v 1.9 2004/11/18 13:56:56 joerg Exp $ +# $DragonFly: src/lib/Makefile,v 1.10 2004/11/24 22:51:01 joerg Exp $ # To satisfy shared library or ELF linkage when only the libraries being # built are visible: @@ -24,8 +24,8 @@ SUBDIR= ${_csu} csu/common libarchive libcom_err libcrypt msun libmd \ libncurses libradius libskey libtacplus libutil libsbuf \ ${_compat} libalias libatm ${_libbind} libbz2 libc ${_libc_r} libcalendar \ libcam libcompat libdevstat libdisk libedit libfetch libform \ - libftpio ${_libio} libipsec libipx libisc libkinfo libkvm libmenu \ - ${_libmilter} ${_libncp} \ + libftpio ${_libio} libipsec libipx libisc libkcore libkinfo libkvm \ + libmenu ${_libmilter} ${_libncp} \ libnetgraph libopie libpam libpanel libpcap \ libposix1e librpcsvc ${_libsm} ${_libsmb} ${_libsmdb} \ ${_libsmutil} \ diff --git a/lib/libkcore/Makefile b/lib/libkcore/Makefile new file mode 100644 index 0000000000..08180558ec --- /dev/null +++ b/lib/libkcore/Makefile @@ -0,0 +1,16 @@ +# $DragonFly: src/lib/libkcore/Makefile,v 1.1 2004/11/24 22:51:01 joerg Exp $ + +.PATH: ${.CURDIR}/../../sys/kern + +LIB= kcore +SRCS= kcore.c kcore_file.c kcore_proc.c kcore_vfs.c +SRCS+= subr_kcore.c +INCS= kcore.h +NOMAN= + +CFLAGS+= -I${.CURDIR} -g +WARNS?= 6 + +SHLIB_MAJOR?= 1 + +.include diff --git a/lib/libkinfo/kinfo_vfs.c b/lib/libkcore/kcore.c similarity index 71% copy from lib/libkinfo/kinfo_vfs.c copy to lib/libkcore/kcore.c index 84fcd2219f..853590d1ed 100644 --- a/lib/libkinfo/kinfo_vfs.c +++ b/lib/libkcore/kcore.c @@ -31,22 +31,45 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/lib/libkinfo/kinfo_vfs.c,v 1.1 2004/11/18 14:33:01 joerg Exp $ + * $DragonFly: src/lib/libkcore/kcore.c,v 1.1 2004/11/24 22:51:01 joerg Exp $ */ -#include -#include -#include +#include +#include -#include -#include -#include +#include +#include #include +#include "kcore_private.h" + +struct kcore_data kcore_global; + +struct kcore_data * +kcore_open(const char *execfile, const char *corefile, char *errbuf) +{ + struct kcore_data *kc; + + kc = malloc(sizeof(*kc)); + kc->kd = kvm_openfiles(execfile, corefile, NULL, O_RDONLY, errbuf); + if (kc->kd == NULL) { + free(kc); + return(NULL); + } + return(kc); +} + int -kinfo_get_vfs_bufspace(int *bufspace) +kcore_close(struct kcore_data *kc) { - int len = sizeof(*bufspace); + int retval; + + if (kc == NULL) { + retval = kvm_close(kcore_global.kd); + return(retval); + } - return(sysctlbyname("vfs.bufspace", bufspace, &len, NULL, 0)); + retval = kvm_close(kc->kd); + free(kc); + return(retval); } diff --git a/lib/libkinfo/kinfo.h b/lib/libkcore/kcore.h similarity index 64% copy from lib/libkinfo/kinfo.h copy to lib/libkcore/kcore.h index d740c2b74b..ef41955d99 100644 --- a/lib/libkinfo/kinfo.h +++ b/lib/libkcore/kcore.h @@ -31,19 +31,43 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/lib/libkinfo/kinfo.h,v 1.2 2004/11/18 14:33:01 joerg Exp $ + * $DragonFly: src/lib/libkcore/kcore.h,v 1.1 2004/11/24 22:51:01 joerg Exp $ */ -#ifndef _KDATA_H -#define _KDATA_H +#ifndef _KCORE_H +#define _KCORE_H #include +#include +#include + +#ifdef KCORE_KINFO_WRAPPER + +#define kinfo_get_files(files, len) \ + kcore_get_files(NULL, files, len) +#define kinfo_get_maxfiles(maxfiles) \ + kcore_get_maxfiles(NULL, maxfiles) +#define kinfo_get_openfiles(openfiles) \ + kcore_get_openfiles(NULL, openfiles) +#define kinfo_get_vfs_bufspace(bufspace) \ + kcore_get_vfs_bufspace(NULL, bufspace) + +#endif /* KCORE_KINFO_WRAPPER */ + +struct kcore_data; +struct kinfo_proc; + __BEGIN_DECLS; -int kinfo_get_files(struct kinfo_file **, size_t *); -int kinfo_get_maxfiles(int *); -int kinfo_get_openfiles(int *); -int kinfo_get_vfs_bufspace(int *); +struct kcore_data + *kcore_open(const char *, const char *, char *); +int kcore_close(struct kcore_data *); +int kcore_get_files(struct kcore_data *, struct kinfo_file **, size_t *); +int kcore_get_maxfiles(struct kcore_data *, int *); +int kcore_get_openfiles(struct kcore_data *, int *); +int kcore_get_procs(struct kcore_data *kc, struct kinfo_proc **procs, + size_t *len); +int kcore_get_vfs_bufspace(struct kcore_data *, int *); __END_DECLS; #endif diff --git a/lib/libkcore/kcore_file.c b/lib/libkcore/kcore_file.c new file mode 100644 index 0000000000..8fb0e37a62 --- /dev/null +++ b/lib/libkcore/kcore_file.c @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2004 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Joerg Sonnenberger . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $DragonFly: src/lib/libkcore/kcore_file.c,v 1.1 2004/11/24 22:51:01 joerg Exp $ + */ + +#define _KERNEL_STRUCTURES + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "kcore_private.h" + +int +kcore_get_files(struct kcore_data *kc, struct kinfo_file **files, size_t *len) +{ + struct kinfo_proc *procs, *oprocs; + struct proc *p; + struct filedesc fdp; + struct file fp, *fpp; + size_t len_procs; + int maxfiles, n, retval; + + if (kc == NULL) + kc = &kcore_global; + + if ((retval = kcore_get_procs(kc, &procs, &len_procs)) != 0) + return(retval); + if (len_procs == 0) { /* no procs, no files */ + *files = NULL; + *len = 0; + return(0); + } + + if ((retval = kcore_get_maxfiles(kc, &maxfiles)) != 0) { + free(procs); + return(retval); + } + + *files = malloc(maxfiles * sizeof(struct kinfo_file)); + if (*files == NULL) { + free(procs); + return(ENOMEM); + } + *len = 0; + + oprocs = procs; + for (; len_procs-- > 0; procs++) { + p = &procs->kp_proc; + if (p->p_fd == NULL || p->p_stat == SIDL) + continue; + if (kvm_read(kc->kd, (long)p->p_fd, &fdp, + sizeof (fdp)) != sizeof(fdp)) { + warnx("cannot read filedesc at %p for pid %d\n", + p->p_fd, p->p_pid); + continue; + } + for (n = 0; n < fdp.fd_nfiles; n++) { + if (kvm_read(kc->kd, (long)(&fdp.fd_ofiles[n]), &fpp, + sizeof(fpp)) != sizeof(fpp)) { + warnx("cannot read filep at %p for pid %d\n", + &fdp.fd_ofiles[n], p->p_pid); + } + if (fpp == NULL) + continue; + if (kvm_read(kc->kd, (long)fpp, &fp, + sizeof(fp)) != sizeof(fp)) { + warnx("cannot read file at %p for pid %d\n", + fpp, p->p_pid); + continue; + } + kcore_make_file(*files + *len, &fp, p->p_pid, 0, n); + (*len)++; + } + } + + *files = reallocf(*files, *len * sizeof(struct kinfo_file)); + if (*files == NULL) + err(1, "realloc"); + free(oprocs); + return(0); +} + +int +kcore_get_maxfiles(struct kcore_data *kc, int *maxfiles) +{ + static struct nlist nl[] = { + { "_maxfiles", 0, 0, 0, 0}, + { NULL, 0, 0, 0, 0} + }; + + if (kc == NULL) + kc = &kcore_global; + + if (nl[0].n_value == 0) { + if ((kvm_nlist(kc->kd, nl) < 0) || (nl[0].n_value == 0)) { + errno = EOPNOTSUPP; + return(-1); + } + } + if (kvm_read(kc->kd, nl[0].n_value, maxfiles, + sizeof(*maxfiles)) != sizeof(*maxfiles)) { + warnx("cannot read _maxfiles: %s", kvm_geterr(kc->kd)); + } + return(0); +} + +int +kcore_get_openfiles(struct kcore_data *kc, int *openfiles) +{ + static struct nlist nl[] = { + { "_nfiles", 0, 0, 0, 0}, + { NULL, 0, 0, 0, 0} + }; + + if (kc == NULL) + kc = &kcore_global; + + if (nl[0].n_value == 0) { + if ((kvm_nlist(kc->kd, nl) < 0) || (nl[0].n_value == 0)) { + errno = EOPNOTSUPP; + return(-1); + } + } + if (kvm_read(kc->kd, nl[0].n_value, openfiles, + sizeof(*openfiles)) != sizeof(*openfiles)) { + warnx("cannot read _nfiles: %s", kvm_geterr(kc->kd)); + } + return(0); +} diff --git a/lib/libkinfo/kinfo.h b/lib/libkcore/kcore_private.h similarity index 84% copy from lib/libkinfo/kinfo.h copy to lib/libkcore/kcore_private.h index d740c2b74b..6d30ebeec1 100644 --- a/lib/libkinfo/kinfo.h +++ b/lib/libkcore/kcore_private.h @@ -31,19 +31,18 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/lib/libkinfo/kinfo.h,v 1.2 2004/11/18 14:33:01 joerg Exp $ + * $DragonFly: src/lib/libkcore/kcore_private.h,v 1.1 2004/11/24 22:51:01 joerg Exp $ */ -#ifndef _KDATA_H -#define _KDATA_H +#ifndef KCORE_PRIVATE_H +#define KCORE_PRIVATE_H -#include +#include -__BEGIN_DECLS; -int kinfo_get_files(struct kinfo_file **, size_t *); -int kinfo_get_maxfiles(int *); -int kinfo_get_openfiles(int *); -int kinfo_get_vfs_bufspace(int *); -__END_DECLS; +struct kcore_data { + kvm_t *kd; +}; + +extern struct kcore_data kcore_global; #endif diff --git a/lib/libkinfo/kinfo_vfs.c b/lib/libkcore/kcore_proc.c similarity index 71% copy from lib/libkinfo/kinfo_vfs.c copy to lib/libkcore/kcore_proc.c index 84fcd2219f..2b5ceec512 100644 --- a/lib/libkinfo/kinfo_vfs.c +++ b/lib/libkcore/kcore_proc.c @@ -31,22 +31,45 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/lib/libkinfo/kinfo_vfs.c,v 1.1 2004/11/18 14:33:01 joerg Exp $ + * $DragonFly: src/lib/libkcore/kcore_proc.c,v 1.1 2004/11/24 22:51:01 joerg Exp $ */ #include -#include #include +#include #include #include -#include +#include +#include +#include #include +#include + +#include "kcore_private.h" int -kinfo_get_vfs_bufspace(int *bufspace) +kcore_get_procs(struct kcore_data *kc, struct kinfo_proc **procs, size_t *len) { - int len = sizeof(*bufspace); + struct kinfo_proc *p; + int nlen; + + if (kc == NULL) + kc = &kcore_global; - return(sysctlbyname("vfs.bufspace", bufspace, &len, NULL, 0)); + p = kvm_getprocs(kc->kd, KERN_PROC_ALL, 0, &nlen); + if (p == NULL) { + warnx("cannot read process table: %s", kvm_geterr(kc->kd)); + return(-1); + } + if (nlen == 0) { + procs = NULL; + len = 0; + } + *procs = malloc(sizeof(struct kinfo_proc) * nlen); + if (*procs == NULL) + return(ENOMEM); + memcpy(*procs, p, sizeof(struct kinfo_proc) * nlen); + *len = nlen; + return(0); } diff --git a/lib/libkinfo/kinfo_vfs.c b/lib/libkcore/kcore_vfs.c similarity index 73% copy from lib/libkinfo/kinfo_vfs.c copy to lib/libkcore/kcore_vfs.c index 84fcd2219f..bae33510a6 100644 --- a/lib/libkinfo/kinfo_vfs.c +++ b/lib/libkcore/kcore_vfs.c @@ -31,22 +31,39 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/lib/libkinfo/kinfo_vfs.c,v 1.1 2004/11/18 14:33:01 joerg Exp $ + * $DragonFly: src/lib/libkcore/kcore_vfs.c,v 1.1 2004/11/24 22:51:01 joerg Exp $ */ #include -#include -#include #include #include -#include -#include +#include +#include +#include + +#include "kcore_private.h" int -kinfo_get_vfs_bufspace(int *bufspace) +kcore_get_vfs_bufspace(struct kcore_data *kc, int *bufspace) { - int len = sizeof(*bufspace); + static struct nlist nl[] = { + { "_bufspace", 0, 0, 0, 0}, + { NULL, 0, 0, 0, 0} + }; + + if (kc == NULL) + kc = &kcore_global; - return(sysctlbyname("vfs.bufspace", bufspace, &len, NULL, 0)); + if (nl[0].n_value == 0) { + if ((kvm_nlist(kc->kd, nl) < 0) || (nl[0].n_value == 0)) { + errno = EOPNOTSUPP; + return(-1); + } + } + if (kvm_read(kc->kd, nl[0].n_value, bufspace, + sizeof(*bufspace)) != sizeof(*bufspace)) { + warnx("cannot read _bufspace: %s", kvm_geterr(kc->kd)); + } + return(0); } diff --git a/lib/libkinfo/kinfo.h b/lib/libkinfo/kinfo.h index d740c2b74b..b1315e2770 100644 --- a/lib/libkinfo/kinfo.h +++ b/lib/libkinfo/kinfo.h @@ -31,13 +31,14 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/lib/libkinfo/kinfo.h,v 1.2 2004/11/18 14:33:01 joerg Exp $ + * $DragonFly: src/lib/libkinfo/kinfo.h,v 1.3 2004/11/24 22:51:01 joerg Exp $ */ -#ifndef _KDATA_H -#define _KDATA_H +#ifndef _KINFO_H +#define _KINFO_H #include +#include __BEGIN_DECLS; int kinfo_get_files(struct kinfo_file **, size_t *); diff --git a/lib/libkinfo/kinfo_file.c b/lib/libkinfo/kinfo_file.c index ce6639055a..54c26a4a75 100644 --- a/lib/libkinfo/kinfo_file.c +++ b/lib/libkinfo/kinfo_file.c @@ -31,11 +31,10 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/lib/libkinfo/kinfo_file.c,v 1.1 2004/11/18 13:56:56 joerg Exp $ + * $DragonFly: src/lib/libkinfo/kinfo_file.c,v 1.2 2004/11/24 22:51:01 joerg Exp $ */ #include -#include #include #include diff --git a/lib/libkinfo/kinfo_vfs.c b/lib/libkinfo/kinfo_vfs.c index 84fcd2219f..aadf7f1f10 100644 --- a/lib/libkinfo/kinfo_vfs.c +++ b/lib/libkinfo/kinfo_vfs.c @@ -31,17 +31,14 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/lib/libkinfo/kinfo_vfs.c,v 1.1 2004/11/18 14:33:01 joerg Exp $ + * $DragonFly: src/lib/libkinfo/kinfo_vfs.c,v 1.2 2004/11/24 22:51:01 joerg Exp $ */ -#include -#include +#include #include -#include -#include #include -#include +#include int kinfo_get_vfs_bufspace(int *bufspace) diff --git a/share/mk/bsd.libnames.mk b/share/mk/bsd.libnames.mk index 89426aaf53..b962386f39 100644 --- a/share/mk/bsd.libnames.mk +++ b/share/mk/bsd.libnames.mk @@ -1,5 +1,5 @@ # $FreeBSD: src/share/mk/bsd.libnames.mk,v 1.28.2.10 2002/08/08 09:33:28 ru Exp $ -# $DragonFly: src/share/mk/bsd.libnames.mk,v 1.6 2004/11/18 13:56:56 joerg Exp $ +# $DragonFly: src/share/mk/bsd.libnames.mk,v 1.7 2004/11/24 22:51:01 joerg Exp $ # # The include file define library names. # Other include files (e.g. bsd.prog.mk, bsd.lib.mk) include this @@ -50,6 +50,7 @@ LIBHISTORY?= ${DESTDIR}${LIBDIR}/libhistory.a LIBIPSEC?= ${DESTDIR}${LIBDIR}/libipsec.a LIBIPX?= ${DESTDIR}${LIBDIR}/libipx.a LIBISC?= ${DESTDIR}${LIBDIR}/libisc.a +LIBKCORE?= ${DESTDIR}${LIBDIR}/libkcore.a LIBKDB?= ${DESTDIR}${LIBDIR}/libkdb.a # XXX in secure dist, not base LIBKRB?= ${DESTDIR}${LIBDIR}/libkrb.a # XXX in secure dist, not base LIBKRB5?= ${DESTDIR}${LIBDIR}/libkrb5.a # XXX in secure dist, not base diff --git a/sys/conf/files b/sys/conf/files index 3f6012bc19..397a1c67d9 100644 --- a/sys/conf/files +++ b/sys/conf/files @@ -1,5 +1,5 @@ # $FreeBSD: src/sys/conf/files,v 1.340.2.137 2003/06/04 17:10:30 sam Exp $ -# $DragonFly: src/sys/conf/files,v 1.80 2004/11/23 06:31:59 dillon Exp $ +# $DragonFly: src/sys/conf/files,v 1.81 2004/11/24 22:51:01 joerg Exp $ # # The long compile-with and dependency lines are required because of # limitations in config: backslash-newline doesn't work in strings, and @@ -678,6 +678,7 @@ kern/subr_devstat.c standard kern/subr_disk.c standard kern/subr_diskslice.c standard kern/subr_eventhandler.c standard +kern/subr_kcore.c standard kern/subr_kobj.c standard kern/subr_log.c standard kern/libmchain/subr_mchain.c optional libmchain diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index e18be7faf7..7e5b55547d 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -37,7 +37,7 @@ * * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94 * $FreeBSD: src/sys/kern/kern_descrip.c,v 1.81.2.19 2004/02/28 00:43:31 tegge Exp $ - * $DragonFly: src/sys/kern/kern_descrip.c,v 1.33 2004/11/18 13:56:56 joerg Exp $ + * $DragonFly: src/sys/kern/kern_descrip.c,v 1.34 2004/11/24 22:51:01 joerg Exp $ */ #include "opt_compat.h" @@ -60,6 +60,7 @@ #include #include #include +#include #include #include @@ -1715,30 +1716,20 @@ sysctl_kern_file(SYSCTL_HANDLER_ARGS) return (SYSCTL_OUT(req, 0, n * sizeof(kf))); } error = 0; - bzero(&kf, sizeof(kf)); - kf.f_size = sizeof(kf); LIST_FOREACH(p, &allproc, p_list) { if (p->p_stat == SIDL) continue; if (!PRISON_CHECK(req->td->td_proc->p_ucred, p->p_ucred) != 0) { continue; } - kf.f_pid = p->p_pid; - kf.f_uid = p->p_ucred->cr_uid; if ((fdp = p->p_fd) == NULL) { continue; } for (n = 0; n < fdp->fd_nfiles; ++n) { if ((fp = fdp->fd_ofiles[n]) == NULL) continue; - kf.f_fd = n; - kf.f_file = fp; - kf.f_data = fp->f_data; - kf.f_type = fp->f_type; - kf.f_count = fp->f_count; - kf.f_msgcount = fp->f_msgcount; - kf.f_offset = fp->f_offset; - kf.f_flag = fp->f_flag; + kcore_make_file(&kf, fp, p->p_pid, + p->p_ucred->cr_uid, n); error = SYSCTL_OUT(req, &kf, sizeof(kf)); if (error) break; diff --git a/lib/libkinfo/kinfo_vfs.c b/sys/kern/subr_kcore.c similarity index 68% copy from lib/libkinfo/kinfo_vfs.c copy to sys/kern/subr_kcore.c index 84fcd2219f..5996cc2b95 100644 --- a/lib/libkinfo/kinfo_vfs.c +++ b/sys/kern/subr_kcore.c @@ -31,22 +31,44 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/lib/libkinfo/kinfo_vfs.c,v 1.1 2004/11/18 14:33:01 joerg Exp $ + * $DragonFly: src/sys/kern/subr_kcore.c,v 1.1 2004/11/24 22:51:01 joerg Exp $ */ +/* + * This file is shared between the kernel and libkcore and they have + * to be kept synchronized. + */ + +#define _KERNEL_STRUCTURES + #include +#include +#include #include -#include -#include -#include -#include -#include +#ifdef _KERNEL +# include +# include +#else +# include +# include +#endif -int -kinfo_get_vfs_bufspace(int *bufspace) +void +kcore_make_file(struct kinfo_file *ufile, struct file *kfile, + pid_t pid, uid_t owner, int n) { - int len = sizeof(*bufspace); + bzero(ufile, sizeof(*ufile)); + ufile->f_size = sizeof(*ufile); + ufile->f_pid = pid; + ufile->f_uid = owner; - return(sysctlbyname("vfs.bufspace", bufspace, &len, NULL, 0)); + ufile->f_fd = n; + ufile->f_file = kfile; + ufile->f_data = kfile->f_data; + ufile->f_type = kfile->f_type; + ufile->f_count = kfile->f_count; + ufile->f_msgcount = kfile->f_msgcount; + ufile->f_offset = kfile->f_offset; + ufile->f_flag = kfile->f_flag; } diff --git a/lib/libkinfo/kinfo.h b/sys/sys/kcore.h similarity index 84% copy from lib/libkinfo/kinfo.h copy to sys/sys/kcore.h index d740c2b74b..3b22b3aea2 100644 --- a/lib/libkinfo/kinfo.h +++ b/sys/sys/kcore.h @@ -31,19 +31,16 @@ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $DragonFly: src/lib/libkinfo/kinfo.h,v 1.2 2004/11/18 14:33:01 joerg Exp $ + * $DragonFly: src/sys/sys/kcore.h,v 1.1 2004/11/24 22:51:01 joerg Exp $ */ -#ifndef _KDATA_H -#define _KDATA_H +#ifndef _SYS_KCORE_H +#define _SYS_KCORE_H -#include +struct kinfo_file; +struct file; +struct proc; -__BEGIN_DECLS; -int kinfo_get_files(struct kinfo_file **, size_t *); -int kinfo_get_maxfiles(int *); -int kinfo_get_openfiles(int *); -int kinfo_get_vfs_bufspace(int *); -__END_DECLS; +void kcore_make_file(struct kinfo_file *, struct file *, pid_t, uid_t, int); #endif diff --git a/usr.sbin/pstat/Makefile b/usr.sbin/pstat/Makefile index a512600027..2d095b63b8 100644 --- a/usr.sbin/pstat/Makefile +++ b/usr.sbin/pstat/Makefile @@ -1,13 +1,14 @@ # @(#)Makefile 8.1 (Berkeley) 6/6/93 # $FreeBSD: src/usr.sbin/pstat/Makefile,v 1.5.6.1 2001/04/25 12:10:38 ru Exp $ -# $DragonFly: src/usr.sbin/pstat/Makefile,v 1.4 2004/11/18 13:35:52 joerg Exp $ +# $DragonFly: src/usr.sbin/pstat/Makefile,v 1.5 2004/11/24 22:51:01 joerg Exp $ PROG= pstat CFLAGS+=-I${.CURDIR}/../../sys +CFLAGS+=-DUSE_KCORE BINGRP= kmem BINMODE=2555 -DPADD= ${LIBKINFO} ${LIBKVM} -LDADD= -lkinfo -lkvm +DPADD= ${LIBKCORE} ${LIBKINFO} ${LIBKVM} +LDADD= -lkcore -lkinfo -lkvm MAN= pstat.8 LINKS= ${BINDIR}/pstat ${BINDIR}/swapinfo MLINKS= pstat.8 swapinfo.8 diff --git a/usr.sbin/pstat/pstat.c b/usr.sbin/pstat/pstat.c index 5843610c09..9f5d2f734c 100644 --- a/usr.sbin/pstat/pstat.c +++ b/usr.sbin/pstat/pstat.c @@ -33,7 +33,7 @@ * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved. * @(#)pstat.c 8.16 (Berkeley) 5/9/95 * $FreeBSD: src/usr.sbin/pstat/pstat.c,v 1.49.2.5 2002/07/12 09:12:49 des Exp $ - * $DragonFly: src/usr.sbin/pstat/pstat.c,v 1.10 2004/11/18 13:35:52 joerg Exp $ + * $DragonFly: src/usr.sbin/pstat/pstat.c,v 1.11 2004/11/24 22:51:01 joerg Exp $ */ #define _KERNEL_STRUCTURES @@ -66,7 +66,6 @@ #include #include -#include #include #include #include @@ -75,17 +74,23 @@ #include #include +#ifdef USE_KCORE +# define KCORE_KINFO_WRAPPER +# include + +struct kcore_data *KCORE_KVM_GLOBAL; + +#else +# include +#endif + struct nlist nl[] = { #define NLMANDATORYBEG 0 #define V_MOUNTLIST 0 { "_mountlist", 0, 0, 0, 0 }, /* address of head of mount list. */ #define V_NUMV 1 { "_numvnodes", 0, 0, 0, 0 }, -#define FNL_NFILE 2 - {"_nfiles", 0, 0, 0, 0}, -#define FNL_MAXFILE 3 - {"_maxfiles", 0, 0, 0, 0}, -#define NLMANDATORYEND FNL_MAXFILE /* names up to here are mandatory */ +#define NLMANDATORYEND V_NUMV /* names up to here are mandatory */ #define SCONS NLMANDATORYEND + 1 { "_cons", 0, 0, 0, 0 }, #define SPTY NLMANDATORYEND + 2 @@ -282,6 +287,10 @@ main(int argc, char **argv) if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0) errx(1, "kvm_openfiles: %s", buf); +#ifdef USE_KCORE + if ((KCORE_KVM_GLOBAL = kcore_open(nlistf, memf, buf)) == NULL) + errx(1, "kcore_open: %s", buf); +#endif if ((ret = kvm_nlist(kd, nl)) != 0) { if (ret == -1) errx(1, "kvm_nlist: %s", kvm_geterr(kd)); @@ -891,35 +900,30 @@ ttyprt(struct tty *tp, int line) void filemode(void) { - struct file *fp; - struct file *addr; - char *buf, flagbuf[16], *fbp; - int len, maxfile, nfile; + struct kinfo_file *fp, *ofp; + size_t len; + char flagbuf[16], *fbp; + int maxfile, nfile; static const char *dtypes[] = { "???", "inode", "socket" }; - KGET(FNL_MAXFILE, maxfile); + if (kinfo_get_maxfiles(&maxfile)) + err(1, "kinfo_get_maxfiles"); if (totalflag) { - KGET(FNL_NFILE, nfile); + if (kinfo_get_openfiles(&nfile)) + err(1, "kinfo_get_openfiles"); (void)printf("%3d/%3d files\n", nfile, maxfile); return; } - if (getfiles(&buf, &len) == -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). - */ - addr = ((struct filelist *)buf)->lh_first; - fp = (struct file *)(buf + sizeof(struct filelist)); - nfile = (len - sizeof(struct filelist)) / sizeof(struct file); - - (void)printf("%d/%d open files\n", nfile, maxfile); + if (kinfo_get_files(&fp, &len)) + err(1, "kinfo_get_files"); + ofp = fp; + + (void)printf("%d/%d open files\n", len, maxfile); (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); - for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) { + for (; len-- > 0; fp++) { if ((unsigned)fp->f_type > DTYPE_SOCKET) continue; - (void)printf("%8lx ", (u_long)(void *)addr); + (void)printf("%p ", fp->f_file); (void)printf("%-8.8s", dtypes[fp->f_type]); fbp = flagbuf; if (fp->f_flag & FREAD) @@ -945,38 +949,7 @@ filemode(void) else (void)printf(" %qd\n", fp->f_offset); } - free(buf); -} - -int -getfiles(char **abuf, int *alen) -{ - size_t len; - int mib[2]; - char *buf; - - /* - * XXX - * Add emulation of KINFO_FILE here. - */ - if (memf != NULL) - 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"); - return (-1); - } - if ((buf = malloc(len)) == NULL) - errx(1, "malloc"); - if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { - warn("sysctl: KERN_FILE"); - return (-1); - } - *abuf = buf; - *alen = len; - return (0); + free(ofp); } /* -- 2.41.0