xref: /netbsd-src/sys/compat/common/kern_info_43.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: kern_info_43.c,v 1.37 2019/01/27 02:08:39 pgoyette 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 2019/01/27 02:08:39 pgoyette 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 #define COPY(fld)							\
259 			ksi.fld = us - (u_long) usi;			\
260 			if ((error = copyoutstr(fld, us, 1024, &len)) != 0)\
261 				return error;				\
262 			us += len
263 
264 			COPY(machine);
265 			COPY(cpu_model);
266 			ksi.ncpu = ncpu;		/* XXX */
267 			ksi.cpuspeed = 40;		/* XXX */
268 			ksi.hwflags = 0;		/* XXX */
269 			ksi.physmem = ctob(physmem);
270 			ksi.usermem = ctob(physmem);	/* XXX */
271 			ksi.pagesize = PAGE_SIZE;
272 
273 			COPY(ostype);
274 			COPY(osrelease);
275 			ksi.os_revision = NetBSD;	/* XXX */
276 			ksi.posix1_version = _POSIX_VERSION;
277 			COPY(version);			/* XXX */
278 
279 			ksi.hz = hz;
280 			ksi.profhz = profhz;
281 			ksi.ngroups_max = NGROUPS_MAX;
282 			ksi.arg_max = ARG_MAX;
283 			ksi.open_max = OPEN_MAX;
284 			ksi.child_max = CHILD_MAX;
285 
286 			TIMESPEC_TO_TIMEVAL(&tv, &boottime);
287 			timeval_to_timeval50(&tv, &ksi.boottime);
288 			COPY(hostname);
289 
290 			size = (us - (char *) &usi[1]) + sizeof(ksi);
291 
292 			if ((error = copyout(&ksi, usi, sizeof(ksi))) != 0)
293 				return error;
294 		}
295 		break;
296 
297 	default:
298 		return (EOPNOTSUPP);
299 	}
300 	if (error)
301 		return (error);
302 	*retval = size;
303 	if (SCARG(uap, size))
304 		error = copyout((void *)&size, (void *)SCARG(uap, size),
305 		    sizeof(size));
306 	return (error);
307 }
308 
309 
310 /* ARGSUSED */
311 int
312 compat_43_sys_sethostid(struct lwp *l, const struct compat_43_sys_sethostid_args *uap, register_t *retval)
313 {
314 	long uhostid;
315 	int name[2];
316 
317 	uhostid = SCARG(uap, hostid);
318 	name[0] = CTL_KERN;
319 	name[1] = KERN_HOSTID;
320 
321 	return (old_sysctl(&name[0], 2, 0, 0, &uhostid, sizeof(long), l));
322 }
323 
324 
325 /* ARGSUSED */
326 int
327 compat_43_sys_sethostname(struct lwp *l, const struct compat_43_sys_sethostname_args *uap, register_t *retval)
328 {
329 	int name[2];
330 
331 	name[0] = CTL_KERN;
332 	name[1] = KERN_HOSTNAME;
333 	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, hostname),
334 			   SCARG(uap, len), l));
335 }
336 
337 int
338 kern_info_43_init(void)
339 {
340 
341 	return syscall_establish(NULL, kern_info_43_syscalls);
342 }
343 
344 int
345 kern_info_43_fini(void)
346 {
347 
348 	return syscall_disestablish(NULL, kern_info_43_syscalls);
349 }
350