Home | History | Annotate | Line # | Download | only in common
kern_info_43.c revision 1.9
      1 /*	$NetBSD: kern_info_43.c,v 1.9 1999/03/24 05:51:19 mrg 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 	register 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 			extern struct timeval boottime;
    227 			extern char ostype[], osrelease[], machine[],
    228 			    hostname[], cpu_model[], version[];
    229 
    230 			if (usi == NULL) {
    231 				size = sizeof(ksi) +
    232 				    strlen(ostype) + strlen(cpu_model) +
    233 				    strlen(osrelease) + strlen(machine) +
    234 				    strlen(version) + strlen(hostname) + 6;
    235 				error = 0;
    236 				break;
    237 			}
    238 
    239 #define COPY(fld)							\
    240 			ksi.fld = us - (u_long) usi;			\
    241 			if ((error = copyoutstr(fld, us, 1024, &len)) != 0)\
    242 				return error;				\
    243 			us += len
    244 
    245 			COPY(machine);
    246 			COPY(cpu_model);
    247 			ksi.ncpu = 1;			/* XXX */
    248 			ksi.cpuspeed = 40;		/* XXX */
    249 			ksi.hwflags = 0;		/* XXX */
    250 			ksi.physmem = ctob(physmem);
    251 			ksi.usermem = ctob(physmem);	/* XXX */
    252 			ksi.pagesize = PAGE_SIZE;
    253 
    254 			COPY(ostype);
    255 			COPY(osrelease);
    256 			ksi.os_revision = NetBSD;	/* XXX */
    257 			ksi.posix1_version = _POSIX_VERSION;
    258 			COPY(version);			/* XXX */
    259 
    260 			ksi.hz = hz;
    261 			ksi.profhz = profhz;
    262 			ksi.ngroups_max = NGROUPS_MAX;
    263 			ksi.arg_max = ARG_MAX;
    264 			ksi.open_max = OPEN_MAX;
    265 			ksi.child_max = CHILD_MAX;
    266 
    267 			ksi.boottime = boottime;
    268 			COPY(hostname);
    269 
    270 			size = (us - (char *) &usi[1]) + sizeof(ksi);
    271 
    272 			if ((error = copyout(&ksi, usi, sizeof(ksi))) != 0)
    273 				return error;
    274 		}
    275 		break;
    276 
    277 	default:
    278 		return (EOPNOTSUPP);
    279 	}
    280 	if (error)
    281 		return (error);
    282 	*retval = size;
    283 	if (SCARG(uap, size))
    284 		error = copyout((caddr_t)&size, (caddr_t)SCARG(uap, size),
    285 		    sizeof(size));
    286 	return (error);
    287 }
    288 
    289 
    290 /* ARGSUSED */
    291 int
    292 compat_43_sys_sethostid(p, v, retval)
    293 	struct proc *p;
    294 	void *v;
    295 	register_t *retval;
    296 {
    297 	struct compat_43_sys_sethostid_args /* {
    298 		syscallarg(int32_t) hostid;
    299 	} */ *uap = v;
    300 	int error;
    301 
    302 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    303 		return (error);
    304 	hostid = SCARG(uap, hostid);
    305 	return (0);
    306 }
    307 
    308 
    309 /* ARGSUSED */
    310 int
    311 compat_43_sys_sethostname(p, v, retval)
    312 	struct proc *p;
    313 	void *v;
    314 	register_t *retval;
    315 {
    316 	struct compat_43_sys_sethostname_args *uap = v;
    317 	int name;
    318 	int error;
    319 
    320 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    321 		return (error);
    322 	name = KERN_HOSTNAME;
    323 	return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, hostname),
    324 			    SCARG(uap, len), p));
    325 }
    326