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