xref: /netbsd-src/sys/compat/common/kern_info_43.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: kern_info_43.c,v 1.11 2000/03/30 11:27:14 augustss 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/param.h>
39 #include <sys/systm.h>
40 #include <sys/filedesc.h>
41 #include <sys/kernel.h>
42 #include <sys/vnode.h>
43 #include <sys/proc.h>
44 #include <sys/file.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/stat.h>
48 #include <sys/ioctl.h>
49 #include <sys/fcntl.h>
50 #include <sys/malloc.h>
51 #include <sys/syslog.h>
52 #include <sys/unistd.h>
53 #include <sys/resourcevar.h>
54 
55 #include <vm/vm.h>
56 #include <sys/sysctl.h>
57 
58 #include <uvm/uvm_extern.h>
59 
60 #include <sys/mount.h>
61 #include <sys/syscallargs.h>
62 
63 int
64 compat_43_sys_getdtablesize(p, v, retval)
65 	struct proc *p;
66 	void *v;
67 	register_t *retval;
68 {
69 
70 	*retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
71 	return (0);
72 }
73 
74 
75 /* ARGSUSED */
76 int
77 compat_43_sys_gethostid(p, v, retval)
78 	struct proc *p;
79 	void *v;
80 	register_t *retval;
81 {
82 
83 	*(int32_t *)retval = hostid;
84 	return (0);
85 }
86 
87 
88 /*ARGSUSED*/
89 int
90 compat_43_sys_gethostname(p, v, retval)
91 	struct proc *p;
92 	void *v;
93 	register_t *retval;
94 {
95 	struct compat_43_sys_gethostname_args /* {
96 		syscallarg(char *) hostname;
97 		syscallarg(u_int) len;
98 	} */ *uap = v;
99 	int name;
100 	size_t sz;
101 
102 	name = KERN_HOSTNAME;
103 	sz = SCARG(uap, len);
104 	return (kern_sysctl(&name, 1, SCARG(uap, hostname), &sz, 0, 0, p));
105 }
106 
107 #define	KINFO_PROC		(0<<8)
108 #define	KINFO_RT		(1<<8)
109 #define	KINFO_VNODE		(2<<8)
110 #define	KINFO_FILE		(3<<8)
111 #define	KINFO_METER		(4<<8)
112 #define	KINFO_LOADAVG		(5<<8)
113 #define	KINFO_CLOCKRATE		(6<<8)
114 #define	KINFO_BSDI_SYSINFO	(101<<8)
115 
116 
117 /*
118  * The string data is appended to the end of the bsdi_si structure during
119  * copyout. The "char *" offsets in the bsdi_si struct are relative to the
120  * base of the bsdi_si struct.
121  */
122 struct bsdi_si {
123         char    *machine;
124         char    *cpu_model;
125         long    ncpu;
126         long    cpuspeed;
127         long    hwflags;
128         u_long  physmem;
129         u_long  usermem;
130         u_long  pagesize;
131 
132         char    *ostype;
133         char    *osrelease;
134         long    os_revision;
135         long    posix1_version;
136         char    *version;
137 
138         long    hz;
139         long    profhz;
140         int     ngroups_max;
141         long    arg_max;
142         long    open_max;
143         long    child_max;
144 
145         struct  timeval boottime;
146         char    *hostname;
147 };
148 
149 int
150 compat_43_sys_getkerninfo(p, v, retval)
151 	struct proc *p;
152 	void *v;
153 	register_t *retval;
154 {
155 	struct compat_43_sys_getkerninfo_args /* {
156 		syscallarg(int) op;
157 		syscallarg(char *) where;
158 		syscallarg(int *) size;
159 		syscallarg(int) arg;
160 	} */ *uap = v;
161 	int error, name[5];
162 	size_t size;
163 
164 	if (SCARG(uap, size) && (error = copyin((caddr_t)SCARG(uap, size),
165 	    (caddr_t)&size, sizeof(size))))
166 		return (error);
167 
168 	switch (SCARG(uap, op) & 0xff00) {
169 
170 	case KINFO_RT:
171 		name[0] = PF_ROUTE;
172 		name[1] = 0;
173 		name[2] = (SCARG(uap, op) & 0xff0000) >> 16;
174 		name[3] = SCARG(uap, op) & 0xff;
175 		name[4] = SCARG(uap, arg);
176 		error =
177 		    net_sysctl(name, 5, SCARG(uap, where), &size, NULL, 0, p);
178 		break;
179 
180 	case KINFO_VNODE:
181 		name[0] = KERN_VNODE;
182 		error =
183 		    kern_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, p);
184 		break;
185 
186 	case KINFO_PROC:
187 		name[0] = KERN_PROC;
188 		name[1] = SCARG(uap, op) & 0xff;
189 		name[2] = SCARG(uap, arg);
190 		error =
191 		    kern_sysctl(name, 3, SCARG(uap, where), &size, NULL, 0, p);
192 		break;
193 
194 	case KINFO_FILE:
195 		name[0] = KERN_FILE;
196 		error =
197 		    kern_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, p);
198 		break;
199 
200 	case KINFO_METER:
201 		name[0] = VM_METER;
202 		error =
203 		    uvm_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, p);
204 		break;
205 
206 	case KINFO_LOADAVG:
207 		name[0] = VM_LOADAVG;
208 		error =
209 		    uvm_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, p);
210 		break;
211 
212 	case KINFO_CLOCKRATE:
213 		name[0] = KERN_CLOCKRATE;
214 		error =
215 		    kern_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, p);
216 		break;
217 
218 
219 	case KINFO_BSDI_SYSINFO:
220 		{
221 			size_t len;
222 			struct bsdi_si *usi =
223 			    (struct bsdi_si *) SCARG(uap, where);
224 			struct bsdi_si ksi;
225 			char *us = (char *) &usi[1];
226 
227 			if (usi == NULL) {
228 				size = sizeof(ksi) +
229 				    strlen(ostype) + strlen(cpu_model) +
230 				    strlen(osrelease) + strlen(machine) +
231 				    strlen(version) + strlen(hostname) + 6;
232 				error = 0;
233 				break;
234 			}
235 
236 #define COPY(fld)							\
237 			ksi.fld = us - (u_long) usi;			\
238 			if ((error = copyoutstr(fld, us, 1024, &len)) != 0)\
239 				return error;				\
240 			us += len
241 
242 			COPY(machine);
243 			COPY(cpu_model);
244 			ksi.ncpu = 1;			/* XXX */
245 			ksi.cpuspeed = 40;		/* XXX */
246 			ksi.hwflags = 0;		/* XXX */
247 			ksi.physmem = ctob(physmem);
248 			ksi.usermem = ctob(physmem);	/* XXX */
249 			ksi.pagesize = PAGE_SIZE;
250 
251 			COPY(ostype);
252 			COPY(osrelease);
253 			ksi.os_revision = NetBSD;	/* XXX */
254 			ksi.posix1_version = _POSIX_VERSION;
255 			COPY(version);			/* XXX */
256 
257 			ksi.hz = hz;
258 			ksi.profhz = profhz;
259 			ksi.ngroups_max = NGROUPS_MAX;
260 			ksi.arg_max = ARG_MAX;
261 			ksi.open_max = OPEN_MAX;
262 			ksi.child_max = CHILD_MAX;
263 
264 			ksi.boottime = boottime;
265 			COPY(hostname);
266 
267 			size = (us - (char *) &usi[1]) + sizeof(ksi);
268 
269 			if ((error = copyout(&ksi, usi, sizeof(ksi))) != 0)
270 				return error;
271 		}
272 		break;
273 
274 	default:
275 		return (EOPNOTSUPP);
276 	}
277 	if (error)
278 		return (error);
279 	*retval = size;
280 	if (SCARG(uap, size))
281 		error = copyout((caddr_t)&size, (caddr_t)SCARG(uap, size),
282 		    sizeof(size));
283 	return (error);
284 }
285 
286 
287 /* ARGSUSED */
288 int
289 compat_43_sys_sethostid(p, v, retval)
290 	struct proc *p;
291 	void *v;
292 	register_t *retval;
293 {
294 	struct compat_43_sys_sethostid_args /* {
295 		syscallarg(int32_t) hostid;
296 	} */ *uap = v;
297 	int error;
298 
299 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
300 		return (error);
301 	hostid = SCARG(uap, hostid);
302 	return (0);
303 }
304 
305 
306 /* ARGSUSED */
307 int
308 compat_43_sys_sethostname(p, v, retval)
309 	struct proc *p;
310 	void *v;
311 	register_t *retval;
312 {
313 	struct compat_43_sys_sethostname_args *uap = v;
314 	int name;
315 	int error;
316 
317 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
318 		return (error);
319 	name = KERN_HOSTNAME;
320 	return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, hostname),
321 			    SCARG(uap, len), p));
322 }
323