MFC r1.8:
[dragonfly.git] / lib / libkcore / kcore_file.c
blob95eb9ca08320fb41296ffbcd164fc2ab80b7b157
1 /*
2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Joerg Sonnenberger <joerg@bec.de>.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 * $DragonFly: src/lib/libkcore/kcore_file.c,v 1.5 2007/04/29 01:36:03 dillon Exp $
37 #define _KERNEL_STRUCTURES
39 #include <sys/user.h> /* MUST BE FIRST */
40 #include <sys/param.h>
41 #include <sys/file.h>
42 #include <sys/kcore.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <kcore.h>
47 #include <kvm.h>
48 #include <nlist.h>
49 #include <stdlib.h>
51 #include "kcore_private.h"
53 int
54 kcore_get_files(struct kcore_data *kc, struct kinfo_file **files, size_t *len)
56 struct kinfo_proc *procs, *oprocs;
57 struct proc p;
58 struct filedesc fdp;
59 struct file fp, *fpp;
60 size_t len_procs;
61 int maxfiles, n, retval;
63 if (kc == NULL)
64 kc = &kcore_global;
66 if ((retval = kcore_get_procs(kc, &procs, &len_procs)) != 0)
67 return(retval);
68 if (len_procs == 0) { /* no procs, no files */
69 *files = NULL;
70 *len = 0;
71 return(0);
74 if ((retval = kcore_get_maxfiles(kc, &maxfiles)) != 0) {
75 free(procs);
76 return(retval);
79 *files = malloc(maxfiles * sizeof(struct kinfo_file));
80 if (*files == NULL) {
81 free(procs);
82 return(ENOMEM);
84 *len = 0;
86 oprocs = procs;
87 for (; len_procs-- > 0; procs++) {
88 if (kvm_read(kc->kd, procs->kp_paddr, &p,
89 sizeof (p)) != sizeof(p)) {
90 warnx("cannot read proc at %p for pid %d\n",
91 (void *)procs->kp_paddr, procs->kp_pid);
92 continue;
94 if (p.p_fd == NULL || procs->kp_stat == SIDL)
95 continue;
96 if (kvm_read(kc->kd, (long)p.p_fd, &fdp,
97 sizeof (fdp)) != sizeof(fdp)) {
98 warnx("cannot read filedesc at %p for pid %d\n",
99 p.p_fd, procs->kp_pid);
100 continue;
102 for (n = 0; n < fdp.fd_nfiles; n++) {
103 if (kvm_read(kc->kd, (long)(&fdp.fd_files[n].fp), &fpp,
104 sizeof(fpp)) != sizeof(fpp)) {
105 warnx("cannot read filep at %p for pid %d\n",
106 &fdp.fd_files[n].fp, procs->kp_pid);
108 if (fpp == NULL)
109 continue;
110 if (kvm_read(kc->kd, (long)fpp, &fp,
111 sizeof(fp)) != sizeof(fp)) {
112 warnx("cannot read file at %p for pid %d\n",
113 fpp, procs->kp_pid);
114 continue;
116 kcore_make_file(*files + *len, &fp, procs->kp_pid, 0, n);
117 (*len)++;
121 *files = reallocf(*files, *len * sizeof(struct kinfo_file));
122 if (*files == NULL)
123 err(1, "realloc");
124 free(oprocs);
125 return(0);
129 kcore_get_maxfiles(struct kcore_data *kc, int *maxfiles)
131 static struct nlist nl[] = {
132 { "_maxfiles", 0, 0, 0, 0},
133 { NULL, 0, 0, 0, 0}
136 return(kcore_get_generic(kc, nl, maxfiles, sizeof(*maxfiles)));
140 kcore_get_openfiles(struct kcore_data *kc, int *openfiles)
142 static struct nlist nl[] = {
143 { "_nfiles", 0, 0, 0, 0},
144 { NULL, 0, 0, 0, 0}
147 return(kcore_get_generic(kc, nl, openfiles, sizeof(*openfiles)));