kern_info_43.c revision 1.12.2.3 1 /* $NetBSD: kern_info_43.c,v 1.12.2.3 2002/05/29 21:32:13 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)subr_xxx.c 8.1 (Berkeley) 6/10/93
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: kern_info_43.c,v 1.12.2.3 2002/05/29 21:32:13 nathanw Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/filedesc.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/lwp.h>
47 #include <sys/proc.h>
48 #include <sys/file.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/stat.h>
52 #include <sys/ioctl.h>
53 #include <sys/fcntl.h>
54 #include <sys/malloc.h>
55 #include <sys/syslog.h>
56 #include <sys/unistd.h>
57 #include <sys/resourcevar.h>
58
59 #include <uvm/uvm_extern.h>
60 #include <sys/sysctl.h>
61
62 #include <sys/mount.h>
63 #include <sys/sa.h>
64 #include <sys/syscallargs.h>
65
66 int
67 compat_43_sys_getdtablesize(struct lwp *l, void *v, register_t *retval)
68 {
69 struct proc *p = l->l_proc;
70
71 *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
72 return (0);
73 }
74
75
76 /* ARGSUSED */
77 int
78 compat_43_sys_gethostid(struct lwp *l, void *v, register_t *retval)
79 {
80
81 *(int32_t *)retval = hostid;
82 return (0);
83 }
84
85
86 /*ARGSUSED*/
87 int
88 compat_43_sys_gethostname(struct lwp *l, void *v, register_t *retval)
89 {
90 struct compat_43_sys_gethostname_args /* {
91 syscallarg(char *) hostname;
92 syscallarg(u_int) len;
93 } */ *uap = v;
94 struct proc *p = l->l_proc;
95 int name;
96 size_t sz;
97
98 name = KERN_HOSTNAME;
99 sz = SCARG(uap, len);
100 return (kern_sysctl(&name, 1, SCARG(uap, hostname), &sz, 0, 0, p));
101 }
102
103 #define KINFO_PROC (0<<8)
104 #define KINFO_RT (1<<8)
105 #define KINFO_VNODE (2<<8)
106 #define KINFO_FILE (3<<8)
107 #define KINFO_METER (4<<8)
108 #define KINFO_LOADAVG (5<<8)
109 #define KINFO_CLOCKRATE (6<<8)
110 #define KINFO_BSDI_SYSINFO (101<<8)
111
112
113 /*
114 * The string data is appended to the end of the bsdi_si structure during
115 * copyout. The "char *" offsets in the bsdi_si struct are relative to the
116 * base of the bsdi_si struct.
117 */
118 struct bsdi_si {
119 char *machine;
120 char *cpu_model;
121 long ncpu;
122 long cpuspeed;
123 long hwflags;
124 u_long physmem;
125 u_long usermem;
126 u_long pagesize;
127
128 char *ostype;
129 char *osrelease;
130 long os_revision;
131 long posix1_version;
132 char *version;
133
134 long hz;
135 long profhz;
136 int ngroups_max;
137 long arg_max;
138 long open_max;
139 long child_max;
140
141 struct timeval boottime;
142 char *hostname;
143 };
144
145 int
146 compat_43_sys_getkerninfo(struct lwp *l, void *v, register_t *retval)
147 {
148 struct compat_43_sys_getkerninfo_args /* {
149 syscallarg(int) op;
150 syscallarg(char *) where;
151 syscallarg(int *) size;
152 syscallarg(int) arg;
153 } */ *uap = v;
154 struct proc *p = l->l_proc;
155 int error, name[5];
156 size_t size;
157
158 if (SCARG(uap, size) && (error = copyin((caddr_t)SCARG(uap, size),
159 (caddr_t)&size, sizeof(size))))
160 return (error);
161
162 switch (SCARG(uap, op) & 0xff00) {
163
164 case KINFO_RT:
165 name[0] = PF_ROUTE;
166 name[1] = 0;
167 name[2] = (SCARG(uap, op) & 0xff0000) >> 16;
168 name[3] = SCARG(uap, op) & 0xff;
169 name[4] = SCARG(uap, arg);
170 error =
171 net_sysctl(name, 5, SCARG(uap, where), &size, NULL, 0, p);
172 break;
173
174 case KINFO_VNODE:
175 name[0] = KERN_VNODE;
176 error =
177 kern_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, p);
178 break;
179
180 case KINFO_PROC:
181 name[0] = KERN_PROC;
182 name[1] = SCARG(uap, op) & 0xff;
183 name[2] = SCARG(uap, arg);
184 error =
185 kern_sysctl(name, 3, SCARG(uap, where), &size, NULL, 0, p);
186 break;
187
188 case KINFO_FILE:
189 name[0] = KERN_FILE;
190 error =
191 kern_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, p);
192 break;
193
194 case KINFO_METER:
195 name[0] = VM_METER;
196 error =
197 uvm_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, p);
198 break;
199
200 case KINFO_LOADAVG:
201 name[0] = VM_LOADAVG;
202 error =
203 uvm_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, p);
204 break;
205
206 case KINFO_CLOCKRATE:
207 name[0] = KERN_CLOCKRATE;
208 error =
209 kern_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, p);
210 break;
211
212
213 case KINFO_BSDI_SYSINFO:
214 {
215 size_t len;
216 struct bsdi_si *usi =
217 (struct bsdi_si *) SCARG(uap, where);
218 struct bsdi_si ksi;
219 char *us = (char *) &usi[1];
220
221 if (usi == NULL) {
222 size = sizeof(ksi) +
223 strlen(ostype) + strlen(cpu_model) +
224 strlen(osrelease) + strlen(machine) +
225 strlen(version) + strlen(hostname) + 6;
226 error = 0;
227 break;
228 }
229
230 #define COPY(fld) \
231 ksi.fld = us - (u_long) usi; \
232 if ((error = copyoutstr(fld, us, 1024, &len)) != 0)\
233 return error; \
234 us += len
235
236 COPY(machine);
237 COPY(cpu_model);
238 ksi.ncpu = 1; /* XXX */
239 ksi.cpuspeed = 40; /* XXX */
240 ksi.hwflags = 0; /* XXX */
241 ksi.physmem = ctob(physmem);
242 ksi.usermem = ctob(physmem); /* XXX */
243 ksi.pagesize = PAGE_SIZE;
244
245 COPY(ostype);
246 COPY(osrelease);
247 ksi.os_revision = NetBSD; /* XXX */
248 ksi.posix1_version = _POSIX_VERSION;
249 COPY(version); /* XXX */
250
251 ksi.hz = hz;
252 ksi.profhz = profhz;
253 ksi.ngroups_max = NGROUPS_MAX;
254 ksi.arg_max = ARG_MAX;
255 ksi.open_max = OPEN_MAX;
256 ksi.child_max = CHILD_MAX;
257
258 ksi.boottime = boottime;
259 COPY(hostname);
260
261 size = (us - (char *) &usi[1]) + sizeof(ksi);
262
263 if ((error = copyout(&ksi, usi, sizeof(ksi))) != 0)
264 return error;
265 }
266 break;
267
268 default:
269 return (EOPNOTSUPP);
270 }
271 if (error)
272 return (error);
273 *retval = size;
274 if (SCARG(uap, size))
275 error = copyout((caddr_t)&size, (caddr_t)SCARG(uap, size),
276 sizeof(size));
277 return (error);
278 }
279
280
281 /* ARGSUSED */
282 int
283 compat_43_sys_sethostid(struct lwp *l, void *v, register_t *retval)
284 {
285 struct compat_43_sys_sethostid_args /* {
286 syscallarg(int32_t) hostid;
287 } */ *uap = v;
288 struct proc *p = l->l_proc;
289 int error;
290
291 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
292 return (error);
293 hostid = SCARG(uap, hostid);
294 return (0);
295 }
296
297
298 /* ARGSUSED */
299 int
300 compat_43_sys_sethostname(struct lwp *l, void *v, register_t *retval)
301 {
302 struct compat_43_sys_sethostname_args *uap = v;
303 struct proc *p = l->l_proc;
304 int name;
305 int error;
306
307 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
308 return (error);
309 name = KERN_HOSTNAME;
310 return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, hostname),
311 SCARG(uap, len), p));
312 }
313