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