1 /*	$NetBSD: kern_info_43.c,v 1.35 2014/03/24 20:08:08 christos 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.35 2014/03/24 20:08:08 christos 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/syslog.h>
50 #include <sys/unistd.h>
51 #include <sys/resourcevar.h>
52 #include <sys/kauth.h>
53 #include <sys/cpu.h>
54 
55 #include <uvm/uvm_extern.h>
56 #include <sys/sysctl.h>
57 
58 #include <sys/mount.h>
59 #include <sys/syscallargs.h>
60 #include <compat/sys/time.h>
61 
62 int
compat_43_sys_getdtablesize(struct lwp * l,const void * v,register_t * retval)63 compat_43_sys_getdtablesize(struct lwp *l, const void *v, register_t *retval)
64 {
65 	struct proc *p = l->l_proc;
66 
67 	mutex_enter(p->p_lock);
68 	*retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
69 	mutex_exit(p->p_lock);
70 	return (0);
71 }
72 
73 
74 /* ARGSUSED */
75 int
compat_43_sys_gethostid(struct lwp * l,const void * v,register_t * retval)76 compat_43_sys_gethostid(struct lwp *l, const void *v, register_t *retval)
77 {
78 
79 	*(int32_t *)retval = hostid;
80 	return (0);
81 }
82 
83 
84 /*ARGSUSED*/
85 int
compat_43_sys_gethostname(struct lwp * l,const struct compat_43_sys_gethostname_args * uap,register_t * retval)86 compat_43_sys_gethostname(struct lwp *l, const struct compat_43_sys_gethostname_args *uap, register_t *retval)
87 {
88 	/* {
89 		syscallarg(char *) hostname;
90 		syscallarg(u_int) len;
91 	} */
92 	int name[2];
93 	size_t sz;
94 
95 	name[0] = CTL_KERN;
96 	name[1] = KERN_HOSTNAME;
97 	sz = SCARG(uap, len);
98 	return (old_sysctl(&name[0], 2, SCARG(uap, hostname), &sz, 0, 0, l));
99 }
100 
101 #define	KINFO_PROC		(0<<8)
102 #define	KINFO_RT		(1<<8)
103 #define	KINFO_VNODE		(2<<8)
104 #define	KINFO_FILE		(3<<8)
105 #define	KINFO_METER		(4<<8)
106 #define	KINFO_LOADAVG		(5<<8)
107 #define	KINFO_CLOCKRATE		(6<<8)
108 #define	KINFO_BSDI_SYSINFO	(101<<8)
109 
110 
111 /*
112  * The string data is appended to the end of the bsdi_si structure during
113  * copyout. The "char *" offsets in the bsdi_si struct are relative to the
114  * base of the bsdi_si struct.
115  */
116 struct bsdi_si {
117         char    *machine;
118         char    *cpu_model;
119         long    ncpu;
120         long    cpuspeed;
121         long    hwflags;
122         u_long  physmem;
123         u_long  usermem;
124         u_long  pagesize;
125 
126         char    *ostype;
127         char    *osrelease;
128         long    os_revision;
129         long    posix1_version;
130         char    *version;
131 
132         long    hz;
133         long    profhz;
134         int     ngroups_max;
135         long    arg_max;
136         long    open_max;
137         long    child_max;
138 
139         struct  timeval50 boottime;
140         char    *hostname;
141 };
142 
143 int
compat_43_sys_getkerninfo(struct lwp * l,const struct compat_43_sys_getkerninfo_args * uap,register_t * retval)144 compat_43_sys_getkerninfo(struct lwp *l, const struct compat_43_sys_getkerninfo_args *uap, register_t *retval)
145 {
146 	/* {
147 		syscallarg(int) op;
148 		syscallarg(char *) where;
149 		syscallarg(int *) size;
150 		syscallarg(int) arg;
151 	} */
152 	int error, name[6];
153 	size_t size;
154 
155 	if (SCARG(uap, size) && (error = copyin((void *)SCARG(uap, size),
156 	    (void *)&size, sizeof(size))))
157 		return (error);
158 
159 	switch (SCARG(uap, op) & 0xff00) {
160 
161 	case KINFO_RT:
162 		name[0] = CTL_NET;
163 		name[1] = PF_ROUTE;
164 		name[2] = 0;
165 		name[3] = (SCARG(uap, op) & 0xff0000) >> 16;
166 		name[4] = SCARG(uap, op) & 0xff;
167 		name[5] = SCARG(uap, arg);
168 		error = old_sysctl(&name[0], 6, SCARG(uap, where), &size,
169 				   NULL, 0, l);
170 		break;
171 
172 	case KINFO_VNODE:
173 		name[0] = CTL_KERN;
174 		name[1] = KERN_VNODE;
175 		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
176 				   NULL, 0, l);
177 		break;
178 
179 	case KINFO_PROC:
180 		name[0] = CTL_KERN;
181 		name[1] = KERN_PROC;
182 		name[2] = SCARG(uap, op) & 0xff;
183 		name[3] = SCARG(uap, arg);
184 		error = old_sysctl(&name[0], 4, SCARG(uap, where), &size,
185 				   NULL, 0, l);
186 		break;
187 
188 	case KINFO_FILE:
189 		name[0] = CTL_KERN;
190 		name[1] = KERN_FILE;
191 		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
192 				   NULL, 0, l);
193 		break;
194 
195 	case KINFO_METER:
196 		name[0] = CTL_VM;
197 		name[1] = VM_METER;
198 		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
199 				   NULL, 0, l);
200 		break;
201 
202 	case KINFO_LOADAVG:
203 		name[0] = CTL_VM;
204 		name[1] = VM_LOADAVG;
205 		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
206 				   NULL, 0, l);
207 		break;
208 
209 	case KINFO_CLOCKRATE:
210 		name[0] = CTL_KERN;
211 		name[1] = KERN_CLOCKRATE;
212 		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
213 				   NULL, 0, l);
214 		break;
215 
216 
217 	case KINFO_BSDI_SYSINFO:
218 		{
219 			size_t len;
220 			struct bsdi_si *usi =
221 			    (struct bsdi_si *) SCARG(uap, where);
222 			struct bsdi_si ksi;
223 			struct timeval tv;
224 			const char *cpu_model = cpu_getmodel();
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 = ncpu;		/* 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 			TIMESPEC_TO_TIMEVAL(&tv, &boottime);
265 			timeval_to_timeval50(&tv, &ksi.boottime);
266 			COPY(hostname);
267 
268 			size = (us - (char *) &usi[1]) + sizeof(ksi);
269 
270 			if ((error = copyout(&ksi, usi, sizeof(ksi))) != 0)
271 				return error;
272 		}
273 		break;
274 
275 	default:
276 		return (EOPNOTSUPP);
277 	}
278 	if (error)
279 		return (error);
280 	*retval = size;
281 	if (SCARG(uap, size))
282 		error = copyout((void *)&size, (void *)SCARG(uap, size),
283 		    sizeof(size));
284 	return (error);
285 }
286 
287 
288 /* ARGSUSED */
289 int
compat_43_sys_sethostid(struct lwp * l,const struct compat_43_sys_sethostid_args * uap,register_t * retval)290 compat_43_sys_sethostid(struct lwp *l, const struct compat_43_sys_sethostid_args *uap, register_t *retval)
291 {
292 	long uhostid;
293 	int name[2];
294 
295 	uhostid = SCARG(uap, hostid);
296 	name[0] = CTL_KERN;
297 	name[1] = KERN_HOSTID;
298 
299 	return (old_sysctl(&name[0], 2, 0, 0, &uhostid, sizeof(long), l));
300 }
301 
302 
303 /* ARGSUSED */
304 int
compat_43_sys_sethostname(struct lwp * l,const struct compat_43_sys_sethostname_args * uap,register_t * retval)305 compat_43_sys_sethostname(struct lwp *l, const struct compat_43_sys_sethostname_args *uap, register_t *retval)
306 {
307 	int name[2];
308 
309 	name[0] = CTL_KERN;
310 	name[1] = KERN_HOSTNAME;
311 	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, hostname),
312 			   SCARG(uap, len), l));
313 }
314