kern_info_43.c revision 1.35.28.1 1 /* $NetBSD: kern_info_43.c,v 1.35.28.1 2018/04/17 00:02:58 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.35.28.1 2018/04/17 00:02:58 pgoyette 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/syscall.h>
60 #include <sys/syscallvar.h>
61 #include <sys/syscallargs.h>
62 #include <compat/sys/time.h>
63
64 #include <compat/common/compat_mod.h>
65
66 static struct syscall_package kern_info_43_syscalls[] = {
67 { SYS_compat_43_ogetdtablesize, 0,
68 (sy_call_t *)compat_43_sys_getdtablesize },
69 { SYS_compat_43_ogethostid, 0, (sy_call_t *)compat_43_sys_gethostid },
70 { SYS_compat_43_ogethostname, 0,
71 (sy_call_t *)compat_43_sys_gethostname },
72 { SYS_compat_43_ogetkerninfo, 0,
73 (sy_call_t *)compat_43_sys_getkerninfo },
74 { SYS_compat_43_osethostid, 0, (sy_call_t *)compat_43_sys_sethostid },
75 { SYS_compat_43_osethostname, 0,
76 (sy_call_t *)compat_43_sys_sethostname },
77 { 0, 0, NULL }
78 };
79
80 int
81 compat_43_sys_getdtablesize(struct lwp *l, const void *v, register_t *retval)
82 {
83 struct proc *p = l->l_proc;
84
85 mutex_enter(p->p_lock);
86 *retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
87 mutex_exit(p->p_lock);
88 return (0);
89 }
90
91
92 /* ARGSUSED */
93 int
94 compat_43_sys_gethostid(struct lwp *l, const void *v, register_t *retval)
95 {
96
97 *(int32_t *)retval = hostid;
98 return (0);
99 }
100
101
102 /*ARGSUSED*/
103 int
104 compat_43_sys_gethostname(struct lwp *l, const struct compat_43_sys_gethostname_args *uap, register_t *retval)
105 {
106 /* {
107 syscallarg(char *) hostname;
108 syscallarg(u_int) len;
109 } */
110 int name[2];
111 size_t sz;
112
113 name[0] = CTL_KERN;
114 name[1] = KERN_HOSTNAME;
115 sz = SCARG(uap, len);
116 return (old_sysctl(&name[0], 2, SCARG(uap, hostname), &sz, 0, 0, l));
117 }
118
119 #define KINFO_PROC (0<<8)
120 #define KINFO_RT (1<<8)
121 #define KINFO_VNODE (2<<8)
122 #define KINFO_FILE (3<<8)
123 #define KINFO_METER (4<<8)
124 #define KINFO_LOADAVG (5<<8)
125 #define KINFO_CLOCKRATE (6<<8)
126 #define KINFO_BSDI_SYSINFO (101<<8)
127
128
129 /*
130 * The string data is appended to the end of the bsdi_si structure during
131 * copyout. The "char *" offsets in the bsdi_si struct are relative to the
132 * base of the bsdi_si struct.
133 */
134 struct bsdi_si {
135 char *machine;
136 char *cpu_model;
137 long ncpu;
138 long cpuspeed;
139 long hwflags;
140 u_long physmem;
141 u_long usermem;
142 u_long pagesize;
143
144 char *ostype;
145 char *osrelease;
146 long os_revision;
147 long posix1_version;
148 char *version;
149
150 long hz;
151 long profhz;
152 int ngroups_max;
153 long arg_max;
154 long open_max;
155 long child_max;
156
157 struct timeval50 boottime;
158 char *hostname;
159 };
160
161 int
162 compat_43_sys_getkerninfo(struct lwp *l, const struct compat_43_sys_getkerninfo_args *uap, register_t *retval)
163 {
164 /* {
165 syscallarg(int) op;
166 syscallarg(char *) where;
167 syscallarg(int *) size;
168 syscallarg(int) arg;
169 } */
170 int error, name[6];
171 size_t size;
172
173 if (SCARG(uap, size) && (error = copyin((void *)SCARG(uap, size),
174 (void *)&size, sizeof(size))))
175 return (error);
176
177 switch (SCARG(uap, op) & 0xff00) {
178
179 case KINFO_RT:
180 name[0] = CTL_NET;
181 name[1] = PF_ROUTE;
182 name[2] = 0;
183 name[3] = (SCARG(uap, op) & 0xff0000) >> 16;
184 name[4] = SCARG(uap, op) & 0xff;
185 name[5] = SCARG(uap, arg);
186 error = old_sysctl(&name[0], 6, SCARG(uap, where), &size,
187 NULL, 0, l);
188 break;
189
190 case KINFO_VNODE:
191 name[0] = CTL_KERN;
192 name[1] = KERN_VNODE;
193 error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
194 NULL, 0, l);
195 break;
196
197 case KINFO_PROC:
198 name[0] = CTL_KERN;
199 name[1] = KERN_PROC;
200 name[2] = SCARG(uap, op) & 0xff;
201 name[3] = SCARG(uap, arg);
202 error = old_sysctl(&name[0], 4, SCARG(uap, where), &size,
203 NULL, 0, l);
204 break;
205
206 case KINFO_FILE:
207 name[0] = CTL_KERN;
208 name[1] = KERN_FILE;
209 error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
210 NULL, 0, l);
211 break;
212
213 case KINFO_METER:
214 name[0] = CTL_VM;
215 name[1] = VM_METER;
216 error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
217 NULL, 0, l);
218 break;
219
220 case KINFO_LOADAVG:
221 name[0] = CTL_VM;
222 name[1] = VM_LOADAVG;
223 error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
224 NULL, 0, l);
225 break;
226
227 case KINFO_CLOCKRATE:
228 name[0] = CTL_KERN;
229 name[1] = KERN_CLOCKRATE;
230 error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
231 NULL, 0, l);
232 break;
233
234
235 case KINFO_BSDI_SYSINFO:
236 {
237 size_t len;
238 struct bsdi_si *usi =
239 (struct bsdi_si *) SCARG(uap, where);
240 struct bsdi_si ksi;
241 struct timeval tv;
242 const char *cpu_model = cpu_getmodel();
243 char *us = (char *) &usi[1];
244
245 if (usi == NULL) {
246 size = sizeof(ksi) +
247 strlen(ostype) + strlen(cpu_model) +
248 strlen(osrelease) + strlen(machine) +
249 strlen(version) + strlen(hostname) + 6;
250 error = 0;
251 break;
252 }
253
254 #define COPY(fld) \
255 ksi.fld = us - (u_long) usi; \
256 if ((error = copyoutstr(fld, us, 1024, &len)) != 0)\
257 return error; \
258 us += len
259
260 COPY(machine);
261 COPY(cpu_model);
262 ksi.ncpu = ncpu; /* XXX */
263 ksi.cpuspeed = 40; /* XXX */
264 ksi.hwflags = 0; /* XXX */
265 ksi.physmem = ctob(physmem);
266 ksi.usermem = ctob(physmem); /* XXX */
267 ksi.pagesize = PAGE_SIZE;
268
269 COPY(ostype);
270 COPY(osrelease);
271 ksi.os_revision = NetBSD; /* XXX */
272 ksi.posix1_version = _POSIX_VERSION;
273 COPY(version); /* XXX */
274
275 ksi.hz = hz;
276 ksi.profhz = profhz;
277 ksi.ngroups_max = NGROUPS_MAX;
278 ksi.arg_max = ARG_MAX;
279 ksi.open_max = OPEN_MAX;
280 ksi.child_max = CHILD_MAX;
281
282 TIMESPEC_TO_TIMEVAL(&tv, &boottime);
283 timeval_to_timeval50(&tv, &ksi.boottime);
284 COPY(hostname);
285
286 size = (us - (char *) &usi[1]) + sizeof(ksi);
287
288 if ((error = copyout(&ksi, usi, sizeof(ksi))) != 0)
289 return error;
290 }
291 break;
292
293 default:
294 return (EOPNOTSUPP);
295 }
296 if (error)
297 return (error);
298 *retval = size;
299 if (SCARG(uap, size))
300 error = copyout((void *)&size, (void *)SCARG(uap, size),
301 sizeof(size));
302 return (error);
303 }
304
305
306 /* ARGSUSED */
307 int
308 compat_43_sys_sethostid(struct lwp *l, const struct compat_43_sys_sethostid_args *uap, register_t *retval)
309 {
310 long uhostid;
311 int name[2];
312
313 uhostid = SCARG(uap, hostid);
314 name[0] = CTL_KERN;
315 name[1] = KERN_HOSTID;
316
317 return (old_sysctl(&name[0], 2, 0, 0, &uhostid, sizeof(long), l));
318 }
319
320
321 /* ARGSUSED */
322 int
323 compat_43_sys_sethostname(struct lwp *l, const struct compat_43_sys_sethostname_args *uap, register_t *retval)
324 {
325 int name[2];
326
327 name[0] = CTL_KERN;
328 name[1] = KERN_HOSTNAME;
329 return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, hostname),
330 SCARG(uap, len), l));
331 }
332
333 int
334 kern_info_43_init(void)
335 {
336
337 return syscall_establish(NULL, kern_info_43_syscalls);
338 }
339
340 int
341 kern_info_43_fini(void)
342 {
343
344 return syscall_disestablish(NULL, kern_info_43_syscalls);
345 }
346