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