1*86d7f5d3SJohn Marino /*
2*86d7f5d3SJohn Marino * Copyright (c) 2004 The DragonFly Project. All rights reserved.
3*86d7f5d3SJohn Marino *
4*86d7f5d3SJohn Marino * This code is derived from software contributed to The DragonFly Project
5*86d7f5d3SJohn Marino * by Joerg Sonnenberger <joerg@bec.de>.
6*86d7f5d3SJohn Marino *
7*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
8*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
9*86d7f5d3SJohn Marino * are met:
10*86d7f5d3SJohn Marino *
11*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
12*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
13*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
14*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in
15*86d7f5d3SJohn Marino * the documentation and/or other materials provided with the
16*86d7f5d3SJohn Marino * distribution.
17*86d7f5d3SJohn Marino * 3. Neither the name of The DragonFly Project nor the names of its
18*86d7f5d3SJohn Marino * contributors may be used to endorse or promote products derived
19*86d7f5d3SJohn Marino * from this software without specific, prior written permission.
20*86d7f5d3SJohn Marino *
21*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*86d7f5d3SJohn Marino * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*86d7f5d3SJohn Marino * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*86d7f5d3SJohn Marino * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*86d7f5d3SJohn Marino * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*86d7f5d3SJohn Marino * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*86d7f5d3SJohn Marino * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28*86d7f5d3SJohn Marino * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29*86d7f5d3SJohn Marino * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30*86d7f5d3SJohn Marino * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31*86d7f5d3SJohn Marino * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*86d7f5d3SJohn Marino * SUCH DAMAGE.
33*86d7f5d3SJohn Marino *
34*86d7f5d3SJohn Marino * $DragonFly: src/lib/libkinfo/kinfo_file.c,v 1.4 2004/12/21 15:07:42 joerg Exp $
35*86d7f5d3SJohn Marino */
36*86d7f5d3SJohn Marino
37*86d7f5d3SJohn Marino #include <sys/kinfo.h>
38*86d7f5d3SJohn Marino #include <sys/param.h>
39*86d7f5d3SJohn Marino #include <sys/sysctl.h>
40*86d7f5d3SJohn Marino
41*86d7f5d3SJohn Marino #include <err.h>
42*86d7f5d3SJohn Marino #include <errno.h>
43*86d7f5d3SJohn Marino #include <kinfo.h>
44*86d7f5d3SJohn Marino #include <stdlib.h>
45*86d7f5d3SJohn Marino
46*86d7f5d3SJohn Marino #define KERN_FILE_SYSCTL "kern.file"
47*86d7f5d3SJohn Marino
48*86d7f5d3SJohn Marino int
kinfo_get_files(struct kinfo_file ** file_buf,size_t * len)49*86d7f5d3SJohn Marino kinfo_get_files(struct kinfo_file **file_buf, size_t *len)
50*86d7f5d3SJohn Marino {
51*86d7f5d3SJohn Marino int retval;
52*86d7f5d3SJohn Marino void *buf;
53*86d7f5d3SJohn Marino size_t new_len;
54*86d7f5d3SJohn Marino
55*86d7f5d3SJohn Marino retval = sysctlbyname(KERN_FILE_SYSCTL, NULL, &new_len, NULL, 0);
56*86d7f5d3SJohn Marino if (retval)
57*86d7f5d3SJohn Marino return(retval);
58*86d7f5d3SJohn Marino if ((buf = malloc(new_len)) == NULL)
59*86d7f5d3SJohn Marino return(ENOMEM);
60*86d7f5d3SJohn Marino retval = sysctlbyname(KERN_FILE_SYSCTL, buf, &new_len, NULL, 0);
61*86d7f5d3SJohn Marino if (retval) {
62*86d7f5d3SJohn Marino free(buf);
63*86d7f5d3SJohn Marino return(retval);
64*86d7f5d3SJohn Marino }
65*86d7f5d3SJohn Marino /*
66*86d7f5d3SJohn Marino * Shrink the buffer to the minimum size, this is not supposed
67*86d7f5d3SJohn Marino * to fail.
68*86d7f5d3SJohn Marino */
69*86d7f5d3SJohn Marino if ((buf = reallocf(buf, new_len)) == NULL)
70*86d7f5d3SJohn Marino err(1, "realloc");
71*86d7f5d3SJohn Marino if (new_len != 0 &&
72*86d7f5d3SJohn Marino ((struct kinfo_file *)buf)->f_size != sizeof(struct kinfo_file)) {
73*86d7f5d3SJohn Marino warnx("kernel size of struct kinfo_file changed");
74*86d7f5d3SJohn Marino free(buf);
75*86d7f5d3SJohn Marino return(EOPNOTSUPP);
76*86d7f5d3SJohn Marino }
77*86d7f5d3SJohn Marino *len = new_len / sizeof(struct kinfo_file);
78*86d7f5d3SJohn Marino *file_buf = buf;
79*86d7f5d3SJohn Marino return(0);
80*86d7f5d3SJohn Marino }
81*86d7f5d3SJohn Marino
82*86d7f5d3SJohn Marino /* XXX convert kern.maxfiles to size_t */
83*86d7f5d3SJohn Marino int
kinfo_get_maxfiles(int * maxfiles)84*86d7f5d3SJohn Marino kinfo_get_maxfiles(int *maxfiles)
85*86d7f5d3SJohn Marino {
86*86d7f5d3SJohn Marino size_t len = sizeof(*maxfiles);
87*86d7f5d3SJohn Marino
88*86d7f5d3SJohn Marino return(sysctlbyname("kern.maxfiles", maxfiles, &len, NULL, 0));
89*86d7f5d3SJohn Marino }
90*86d7f5d3SJohn Marino
91*86d7f5d3SJohn Marino /* XXX convert kern.openfiles to size_t */
92*86d7f5d3SJohn Marino int
kinfo_get_openfiles(int * openfiles)93*86d7f5d3SJohn Marino kinfo_get_openfiles(int *openfiles)
94*86d7f5d3SJohn Marino {
95*86d7f5d3SJohn Marino size_t len = sizeof(*openfiles);
96*86d7f5d3SJohn Marino
97*86d7f5d3SJohn Marino return(sysctlbyname("kern.openfiles", openfiles, &len, NULL, 0));
98*86d7f5d3SJohn Marino }
99