kern_info_43.c revision 1.37.4.1 1 /* $NetBSD: kern_info_43.c,v 1.37.4.1 2022/08/03 11:11:31 martin 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)subr_xxx.c 8.1 (Berkeley) 6/10/93
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: kern_info_43.c,v 1.37.4.1 2022/08/03 11:11:31 martin Exp $");
36
37 #if defined(_KERNEL_OPT)
38 #include "opt_compat_netbsd.h"
39 #endif
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/proc.h>
47 #include <sys/file.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/stat.h>
51 #include <sys/ioctl.h>
52 #include <sys/fcntl.h>
53 #include <sys/syslog.h>
54 #include <sys/unistd.h>
55 #include <sys/resourcevar.h>
56 #include <sys/kauth.h>
57 #include <sys/cpu.h>
58
59 #include <uvm/uvm_extern.h>
60 #include <sys/sysctl.h>
61
62 #include <sys/mount.h>
63 #include <sys/syscall.h>
64 #include <sys/syscallvar.h>
65 #include <sys/syscallargs.h>
66 #include <compat/sys/time.h>
67
68 #include <compat/common/compat_mod.h>
69
70 static struct syscall_package kern_info_43_syscalls[] = {
71 { SYS_compat_43_ogetdtablesize, 0,
72 (sy_call_t *)compat_43_sys_getdtablesize },
73 { SYS_compat_43_ogethostid, 0, (sy_call_t *)compat_43_sys_gethostid },
74 { SYS_compat_43_ogethostname, 0,
75 (sy_call_t *)compat_43_sys_gethostname },
76 { SYS_compat_43_ogetkerninfo, 0,
77 (sy_call_t *)compat_43_sys_getkerninfo },
78 { SYS_compat_43_osethostid, 0, (sy_call_t *)compat_43_sys_sethostid },
79 { SYS_compat_43_osethostname, 0,
80 (sy_call_t *)compat_43_sys_sethostname },
81 { 0, 0, NULL }
82 };
83
84 int
85 compat_43_sys_getdtablesize(struct lwp *l, const void *v, register_t *retval)
86 {
87 struct proc *p = l->l_proc;
88
89 mutex_enter(p->p_lock);
90 *retval = uimin((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
91 mutex_exit(p->p_lock);
92 return (0);
93 }
94
95
96 /* ARGSUSED */
97 int
98 compat_43_sys_gethostid(struct lwp *l, const void *v, register_t *retval)
99 {
100
101 *(int32_t *)retval = hostid;
102 return (0);
103 }
104
105
106 /*ARGSUSED*/
107 int
108 compat_43_sys_gethostname(struct lwp *l, const struct compat_43_sys_gethostname_args *uap, register_t *retval)
109 {
110 /* {
111 syscallarg(char *) hostname;
112 syscallarg(u_int) len;
113 } */
114 int name[2];
115 size_t sz;
116
117 name[0] = CTL_KERN;
118 name[1] = KERN_HOSTNAME;
119 sz = SCARG(uap, len);
120 return (old_sysctl(&name[0], 2, SCARG(uap, hostname), &sz, 0, 0, l));
121 }
122
123 #define KINFO_PROC (0<<8)
124 #define KINFO_RT (1<<8)
125 #define KINFO_VNODE (2<<8)
126 #define KINFO_FILE (3<<8)
127 #define KINFO_METER (4<<8)
128 #define KINFO_LOADAVG (5<<8)
129 #define KINFO_CLOCKRATE (6<<8)
130 #define KINFO_BSDI_SYSINFO (101<<8)
131
132
133 /*
134 * The string data is appended to the end of the bsdi_si structure during
135 * copyout. The "char *" offsets in the bsdi_si struct are relative to the
136 * base of the bsdi_si struct.
137 */
138 struct bsdi_si {
139 char *machine;
140 char *cpu_model;
141 long ncpu;
142 long cpuspeed;
143 long hwflags;
144 u_long physmem;
145 u_long usermem;
146 u_long pagesize;
147
148 char *ostype;
149 char *osrelease;
150 long os_revision;
151 long posix1_version;
152 char *version;
153
154 long hz;
155 long profhz;
156 int ngroups_max;
157 long arg_max;
158 long open_max;
159 long child_max;
160
161 struct timeval50 boottime;
162 char *hostname;
163 };
164
165 int
166 compat_43_sys_getkerninfo(struct lwp *l, const struct compat_43_sys_getkerninfo_args *uap, register_t *retval)
167 {
168 /* {
169 syscallarg(int) op;
170 syscallarg(char *) where;
171 syscallarg(int *) size;
172 syscallarg(int) arg;
173 } */
174 int error, name[6];
175 size_t size;
176
177 if (SCARG(uap, size) && (error = copyin((void *)SCARG(uap, size),
178 (void *)&size, sizeof(size))))
179 return (error);
180
181 switch (SCARG(uap, op) & 0xff00) {
182
183 case KINFO_RT:
184 name[0] = CTL_NET;
185 name[1] = PF_ROUTE;
186 name[2] = 0;
187 name[3] = (SCARG(uap, op) & 0xff0000) >> 16;
188 name[4] = SCARG(uap, op) & 0xff;
189 name[5] = SCARG(uap, arg);
190 error = old_sysctl(&name[0], 6, SCARG(uap, where), &size,
191 NULL, 0, l);
192 break;
193
194 case KINFO_VNODE:
195 name[0] = CTL_KERN;
196 name[1] = KERN_VNODE;
197 error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
198 NULL, 0, l);
199 break;
200
201 case KINFO_PROC:
202 name[0] = CTL_KERN;
203 name[1] = KERN_PROC;
204 name[2] = SCARG(uap, op) & 0xff;
205 name[3] = SCARG(uap, arg);
206 error = old_sysctl(&name[0], 4, SCARG(uap, where), &size,
207 NULL, 0, l);
208 break;
209
210 case KINFO_FILE:
211 name[0] = CTL_KERN;
212 name[1] = KERN_FILE;
213 error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
214 NULL, 0, l);
215 break;
216
217 case KINFO_METER:
218 name[0] = CTL_VM;
219 name[1] = VM_METER;
220 error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
221 NULL, 0, l);
222 break;
223
224 case KINFO_LOADAVG:
225 name[0] = CTL_VM;
226 name[1] = VM_LOADAVG;
227 error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
228 NULL, 0, l);
229 break;
230
231 case KINFO_CLOCKRATE:
232 name[0] = CTL_KERN;
233 name[1] = KERN_CLOCKRATE;
234 error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
235 NULL, 0, l);
236 break;
237
238
239 case KINFO_BSDI_SYSINFO:
240 {
241 size_t len;
242 struct bsdi_si *usi =
243 (struct bsdi_si *) SCARG(uap, where);
244 struct bsdi_si ksi;
245 struct timeval tv;
246 const char *cpu_model = cpu_getmodel();
247 char *us = (char *) &usi[1];
248
249 if (usi == NULL) {
250 size = sizeof(ksi) +
251 strlen(ostype) + strlen(cpu_model) +
252 strlen(osrelease) + strlen(machine) +
253 strlen(version) + strlen(hostname) + 6;
254 error = 0;
255 break;
256 }
257
258 memset(&ksi, 0, sizeof(ksi));
259
260 #define COPY(fld) \
261 ksi.fld = us - (u_long) usi; \
262 if ((error = copyoutstr(fld, us, 1024, &len)) != 0)\
263 return error; \
264 us += len
265
266 COPY(machine);
267 COPY(cpu_model);
268 ksi.ncpu = ncpu; /* XXX */
269 ksi.cpuspeed = 40; /* XXX */
270 ksi.hwflags = 0; /* XXX */
271 ksi.physmem = ctob(physmem);
272 ksi.usermem = ctob(physmem); /* XXX */
273 ksi.pagesize = PAGE_SIZE;
274
275 COPY(ostype);
276 COPY(osrelease);
277 ksi.os_revision = NetBSD; /* XXX */
278 ksi.posix1_version = _POSIX_VERSION;
279 COPY(version); /* XXX */
280
281 ksi.hz = hz;
282 ksi.profhz = profhz;
283 ksi.ngroups_max = NGROUPS_MAX;
284 ksi.arg_max = ARG_MAX;
285 ksi.open_max = OPEN_MAX;
286 ksi.child_max = CHILD_MAX;
287
288 TIMESPEC_TO_TIMEVAL(&tv, &boottime);
289 timeval_to_timeval50(&tv, &ksi.boottime);
290 COPY(hostname);
291
292 size = (us - (char *) &usi[1]) + sizeof(ksi);
293
294 if ((error = copyout(&ksi, usi, sizeof(ksi))) != 0)
295 return error;
296 }
297 break;
298
299 default:
300 return (EOPNOTSUPP);
301 }
302 if (error)
303 return (error);
304 *retval = size;
305 if (SCARG(uap, size))
306 error = copyout((void *)&size, (void *)SCARG(uap, size),
307 sizeof(size));
308 return (error);
309 }
310
311
312 /* ARGSUSED */
313 int
314 compat_43_sys_sethostid(struct lwp *l, const struct compat_43_sys_sethostid_args *uap, register_t *retval)
315 {
316 long uhostid;
317 int name[2];
318
319 uhostid = SCARG(uap, hostid);
320 name[0] = CTL_KERN;
321 name[1] = KERN_HOSTID;
322
323 return (old_sysctl(&name[0], 2, 0, 0, &uhostid, sizeof(long), l));
324 }
325
326
327 /* ARGSUSED */
328 int
329 compat_43_sys_sethostname(struct lwp *l, const struct compat_43_sys_sethostname_args *uap, register_t *retval)
330 {
331 int name[2];
332
333 name[0] = CTL_KERN;
334 name[1] = KERN_HOSTNAME;
335 return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, hostname),
336 SCARG(uap, len), l));
337 }
338
339 int
340 kern_info_43_init(void)
341 {
342
343 return syscall_establish(NULL, kern_info_43_syscalls);
344 }
345
346 int
347 kern_info_43_fini(void)
348 {
349
350 return syscall_disestablish(NULL, kern_info_43_syscalls);
351 }
352