1/*	$NetBSD: kern_info_43.c,v 1.40 2021/09/07 11:43:02 riastradh 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.40 2021/09/07 11:43:02 riastradh 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
70static 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
84int
85compat_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 */
97int
98compat_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*/
107int
108compat_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 */
138struct 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
165int
166compat_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	int isize;
176	size_t size;
177
178	if (!SCARG(uap, size))
179		return EINVAL;
180
181	if ((error = copyin(SCARG(uap, size), &isize, sizeof(isize))) != 0)
182		return error;
183
184	if (isize < 0 || isize > 4096)
185		return EINVAL;
186
187	size = isize;
188
189	switch (SCARG(uap, op) & 0xff00) {
190
191	case KINFO_RT:
192		name[0] = CTL_NET;
193		name[1] = PF_ROUTE;
194		name[2] = 0;
195		name[3] = (SCARG(uap, op) & 0xff0000) >> 16;
196		name[4] = SCARG(uap, op) & 0xff;
197		name[5] = SCARG(uap, arg);
198		error = old_sysctl(&name[0], 6, SCARG(uap, where), &size,
199				   NULL, 0, l);
200		break;
201
202	case KINFO_VNODE:
203		name[0] = CTL_KERN;
204		name[1] = KERN_VNODE;
205		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
206				   NULL, 0, l);
207		break;
208
209	case KINFO_PROC:
210		name[0] = CTL_KERN;
211		name[1] = KERN_PROC;
212		name[2] = SCARG(uap, op) & 0xff;
213		name[3] = SCARG(uap, arg);
214		error = old_sysctl(&name[0], 4, SCARG(uap, where), &size,
215				   NULL, 0, l);
216		break;
217
218	case KINFO_FILE:
219		name[0] = CTL_KERN;
220		name[1] = KERN_FILE;
221		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
222				   NULL, 0, l);
223		break;
224
225	case KINFO_METER:
226		name[0] = CTL_VM;
227		name[1] = VM_METER;
228		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
229				   NULL, 0, l);
230		break;
231
232	case KINFO_LOADAVG:
233		name[0] = CTL_VM;
234		name[1] = VM_LOADAVG;
235		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
236				   NULL, 0, l);
237		break;
238
239	case KINFO_CLOCKRATE:
240		name[0] = CTL_KERN;
241		name[1] = KERN_CLOCKRATE;
242		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
243				   NULL, 0, l);
244		break;
245
246
247	case KINFO_BSDI_SYSINFO:
248		{
249			size_t len;
250			struct bsdi_si *usi =
251			    (struct bsdi_si *) SCARG(uap, where);
252			struct bsdi_si ksi;
253			struct timeval tv;
254			const char *cpu_model = cpu_getmodel();
255			char *us = (char *) &usi[1];
256
257			if (usi == NULL) {
258				size = sizeof(ksi) +
259				    strlen(ostype) + strlen(cpu_model) +
260				    strlen(osrelease) + strlen(machine) +
261				    strlen(version) + strlen(hostname) + 6;
262				error = 0;
263				break;
264			}
265
266			memset(&ksi, 0, sizeof(ksi));
267
268#define COPY(fld)							\
269			ksi.fld = us - (u_long) usi;			\
270			if ((error = copyoutstr(fld, us, 1024, &len)) != 0)\
271				return error;				\
272			us += len
273
274			COPY(machine);
275			COPY(cpu_model);
276			ksi.ncpu = ncpu;		/* XXX */
277			ksi.cpuspeed = 40;		/* XXX */
278			ksi.hwflags = 0;		/* XXX */
279			ksi.physmem = ctob(physmem);
280			ksi.usermem = ctob(physmem);	/* XXX */
281			ksi.pagesize = PAGE_SIZE;
282
283			COPY(ostype);
284			COPY(osrelease);
285			ksi.os_revision = NetBSD;	/* XXX */
286			ksi.posix1_version = _POSIX_VERSION;
287			COPY(version);			/* XXX */
288
289			ksi.hz = hz;
290			ksi.profhz = profhz;
291			ksi.ngroups_max = NGROUPS_MAX;
292			ksi.arg_max = ARG_MAX;
293			ksi.open_max = OPEN_MAX;
294			ksi.child_max = CHILD_MAX;
295
296			getmicroboottime(&tv);
297			timeval_to_timeval50(&tv, &ksi.boottime);
298			COPY(hostname);
299
300			size = (us - (char *) &usi[1]) + sizeof(ksi);
301
302			if ((error = copyout(&ksi, usi, sizeof(ksi))) != 0)
303				return error;
304		}
305		break;
306
307	default:
308		return (EOPNOTSUPP);
309	}
310	if (error)
311		return (error);
312	*retval = size;
313	if (SCARG(uap, size))
314		error = copyout((void *)&size, (void *)SCARG(uap, size),
315		    sizeof(size));
316	return (error);
317}
318
319
320/* ARGSUSED */
321int
322compat_43_sys_sethostid(struct lwp *l, const struct compat_43_sys_sethostid_args *uap, register_t *retval)
323{
324	long uhostid;
325	int name[2];
326
327	uhostid = SCARG(uap, hostid);
328	name[0] = CTL_KERN;
329	name[1] = KERN_HOSTID;
330
331	return (old_sysctl(&name[0], 2, 0, 0, &uhostid, sizeof(long), l));
332}
333
334
335/* ARGSUSED */
336int
337compat_43_sys_sethostname(struct lwp *l, const struct compat_43_sys_sethostname_args *uap, register_t *retval)
338{
339	int name[2];
340
341	name[0] = CTL_KERN;
342	name[1] = KERN_HOSTNAME;
343	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, hostname),
344			   SCARG(uap, len), l));
345}
346
347int
348kern_info_43_init(void)
349{
350
351	return syscall_establish(NULL, kern_info_43_syscalls);
352}
353
354int
355kern_info_43_fini(void)
356{
357
358	return syscall_disestablish(NULL, kern_info_43_syscalls);
359}
360