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.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>
42 #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
++) {
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
);
94 if (p
.p_fd
== NULL
|| procs
->kp_stat
== SIDL
)
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
);
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
);
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",
116 kcore_make_file(*files
+ *len
, &fp
, procs
->kp_pid
, 0, n
);
121 *files
= reallocf(*files
, *len
* sizeof(struct kinfo_file
));
129 kcore_get_maxfiles(struct kcore_data
*kc
, int *maxfiles
)
131 static struct nlist nl
[] = {
132 { "_maxfiles", 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},
147 return(kcore_get_generic(kc
, nl
, openfiles
, sizeof(*openfiles
)));