2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Joerg Sonnenberger <joerg@bec.de>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/lib/libkcore/kcore_file.c,v 1.3 2005/06/22 01:51:40 dillon Exp $
37 #define _KERNEL_STRUCTURES
39 #include <sys/param.h>
41 #include <sys/kcore.h>
51 #include "kcore_private.h"
54 kcore_get_files(struct kcore_data *kc, struct kinfo_file **files, size_t *len)
56 struct kinfo_proc *procs, *oprocs;
61 int maxfiles, n, retval;
66 if ((retval = kcore_get_procs(kc, &procs, &len_procs)) != 0)
68 if (len_procs == 0) { /* no procs, no files */
74 if ((retval = kcore_get_maxfiles(kc, &maxfiles)) != 0) {
79 *files = malloc(maxfiles * sizeof(struct kinfo_file));
87 for (; len_procs-- > 0; procs++) {
89 if (p->p_fd == NULL || p->p_stat == SIDL)
91 if (kvm_read(kc->kd, (long)p->p_fd, &fdp,
92 sizeof (fdp)) != sizeof(fdp)) {
93 warnx("cannot read filedesc at %p for pid %d\n",
97 for (n = 0; n < fdp.fd_nfiles; n++) {
98 if (kvm_read(kc->kd, (long)(&fdp.fd_files[n].fp), &fpp,
99 sizeof(fpp)) != sizeof(fpp)) {
100 warnx("cannot read filep at %p for pid %d\n",
101 &fdp.fd_files[n].fp, p->p_pid);
105 if (kvm_read(kc->kd, (long)fpp, &fp,
106 sizeof(fp)) != sizeof(fp)) {
107 warnx("cannot read file at %p for pid %d\n",
111 kcore_make_file(*files + *len, &fp, p->p_pid, 0, n);
116 *files = reallocf(*files, *len * sizeof(struct kinfo_file));
124 kcore_get_maxfiles(struct kcore_data *kc, int *maxfiles)
126 static struct nlist nl[] = {
127 { "_maxfiles", 0, 0, 0, 0},
131 return(kcore_get_generic(kc, nl, maxfiles, sizeof(*maxfiles)));
135 kcore_get_openfiles(struct kcore_data *kc, int *openfiles)
137 static struct nlist nl[] = {
138 { "_nfiles", 0, 0, 0, 0},
142 return(kcore_get_generic(kc, nl, openfiles, sizeof(*openfiles)));