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: 10 * 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. 20 * 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. 33 * 34 * $DragonFly: src/lib/libkcore/kcore_file.c,v 1.3 2005/06/22 01:51:40 dillon Exp $ 35 */ 36 37 #define _KERNEL_STRUCTURES 38 39 #include <sys/param.h> 40 #include <sys/file.h> 41 #include <sys/kcore.h> 42 #include <sys/user.h> 43 44 #include <err.h> 45 #include <errno.h> 46 #include <kcore.h> 47 #include <kvm.h> 48 #include <nlist.h> 49 #include <stdlib.h> 50 51 #include "kcore_private.h" 52 53 int 54 kcore_get_files(struct kcore_data *kc, struct kinfo_file **files, size_t *len) 55 { 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; 62 63 if (kc == NULL) 64 kc = &kcore_global; 65 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); 72 } 73 74 if ((retval = kcore_get_maxfiles(kc, &maxfiles)) != 0) { 75 free(procs); 76 return(retval); 77 } 78 79 *files = malloc(maxfiles * sizeof(struct kinfo_file)); 80 if (*files == NULL) { 81 free(procs); 82 return(ENOMEM); 83 } 84 *len = 0; 85 86 oprocs = procs; 87 for (; len_procs-- > 0; procs++) { 88 p = &procs->kp_proc; 89 if (p->p_fd == NULL || p->p_stat == SIDL) 90 continue; 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", 94 p->p_fd, p->p_pid); 95 continue; 96 } 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); 102 } 103 if (fpp == NULL) 104 continue; 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", 108 fpp, p->p_pid); 109 continue; 110 } 111 kcore_make_file(*files + *len, &fp, p->p_pid, 0, n); 112 (*len)++; 113 } 114 } 115 116 *files = reallocf(*files, *len * sizeof(struct kinfo_file)); 117 if (*files == NULL) 118 err(1, "realloc"); 119 free(oprocs); 120 return(0); 121 } 122 123 int 124 kcore_get_maxfiles(struct kcore_data *kc, int *maxfiles) 125 { 126 static struct nlist nl[] = { 127 { "_maxfiles", 0, 0, 0, 0}, 128 { NULL, 0, 0, 0, 0} 129 }; 130 131 return(kcore_get_generic(kc, nl, maxfiles, sizeof(*maxfiles))); 132 } 133 134 int 135 kcore_get_openfiles(struct kcore_data *kc, int *openfiles) 136 { 137 static struct nlist nl[] = { 138 { "_nfiles", 0, 0, 0, 0}, 139 { NULL, 0, 0, 0, 0} 140 }; 141 142 return(kcore_get_generic(kc, nl, openfiles, sizeof(*openfiles))); 143 } 144