Home | History | Annotate | Line # | Download | only in common
kern_info_43.c revision 1.15
      1 /*	$NetBSD: kern_info_43.c,v 1.15 2003/06/28 14:21:16 darrenr 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.15 2003/06/28 14:21:16 darrenr 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 	int name;
     94 	size_t sz;
     95 
     96 	name = KERN_HOSTNAME;
     97 	sz = SCARG(uap, len);
     98 	return (kern_sysctl(&name, 1, 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  timeval boottime;
    140         char    *hostname;
    141 };
    142 
    143 int
    144 compat_43_sys_getkerninfo(struct lwp *l, void *v, register_t *retval)
    145 {
    146 	struct compat_43_sys_getkerninfo_args /* {
    147 		syscallarg(int) op;
    148 		syscallarg(char *) where;
    149 		syscallarg(int *) size;
    150 		syscallarg(int) arg;
    151 	} */ *uap = v;
    152 	int error, name[5];
    153 	size_t size;
    154 
    155 	if (SCARG(uap, size) && (error = copyin((caddr_t)SCARG(uap, size),
    156 	    (caddr_t)&size, sizeof(size))))
    157 		return (error);
    158 
    159 	switch (SCARG(uap, op) & 0xff00) {
    160 
    161 	case KINFO_RT:
    162 		name[0] = PF_ROUTE;
    163 		name[1] = 0;
    164 		name[2] = (SCARG(uap, op) & 0xff0000) >> 16;
    165 		name[3] = SCARG(uap, op) & 0xff;
    166 		name[4] = SCARG(uap, arg);
    167 		error =
    168 		    net_sysctl(name, 5, SCARG(uap, where), &size, NULL, 0, l);
    169 		break;
    170 
    171 	case KINFO_VNODE:
    172 		name[0] = KERN_VNODE;
    173 		error =
    174 		    kern_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, l);
    175 		break;
    176 
    177 	case KINFO_PROC:
    178 		name[0] = KERN_PROC;
    179 		name[1] = SCARG(uap, op) & 0xff;
    180 		name[2] = SCARG(uap, arg);
    181 		error =
    182 		    kern_sysctl(name, 3, SCARG(uap, where), &size, NULL, 0, l);
    183 		break;
    184 
    185 	case KINFO_FILE:
    186 		name[0] = KERN_FILE;
    187 		error =
    188 		    kern_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, l);
    189 		break;
    190 
    191 	case KINFO_METER:
    192 		name[0] = VM_METER;
    193 		error =
    194 		    uvm_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, l);
    195 		break;
    196 
    197 	case KINFO_LOADAVG:
    198 		name[0] = VM_LOADAVG;
    199 		error =
    200 		    uvm_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, l);
    201 		break;
    202 
    203 	case KINFO_CLOCKRATE:
    204 		name[0] = KERN_CLOCKRATE;
    205 		error =
    206 		    kern_sysctl(name, 1, SCARG(uap, where), &size, NULL, 0, l);
    207 		break;
    208 
    209 
    210 	case KINFO_BSDI_SYSINFO:
    211 		{
    212 			size_t len;
    213 			struct bsdi_si *usi =
    214 			    (struct bsdi_si *) SCARG(uap, where);
    215 			struct bsdi_si ksi;
    216 			char *us = (char *) &usi[1];
    217 
    218 			if (usi == NULL) {
    219 				size = sizeof(ksi) +
    220 				    strlen(ostype) + strlen(cpu_model) +
    221 				    strlen(osrelease) + strlen(machine) +
    222 				    strlen(version) + strlen(hostname) + 6;
    223 				error = 0;
    224 				break;
    225 			}
    226 
    227 #define COPY(fld)							\
    228 			ksi.fld = us - (u_long) usi;			\
    229 			if ((error = copyoutstr(fld, us, 1024, &len)) != 0)\
    230 				return error;				\
    231 			us += len
    232 
    233 			COPY(machine);
    234 			COPY(cpu_model);
    235 			ksi.ncpu = 1;			/* XXX */
    236 			ksi.cpuspeed = 40;		/* XXX */
    237 			ksi.hwflags = 0;		/* XXX */
    238 			ksi.physmem = ctob(physmem);
    239 			ksi.usermem = ctob(physmem);	/* XXX */
    240 			ksi.pagesize = PAGE_SIZE;
    241 
    242 			COPY(ostype);
    243 			COPY(osrelease);
    244 			ksi.os_revision = NetBSD;	/* XXX */
    245 			ksi.posix1_version = _POSIX_VERSION;
    246 			COPY(version);			/* XXX */
    247 
    248 			ksi.hz = hz;
    249 			ksi.profhz = profhz;
    250 			ksi.ngroups_max = NGROUPS_MAX;
    251 			ksi.arg_max = ARG_MAX;
    252 			ksi.open_max = OPEN_MAX;
    253 			ksi.child_max = CHILD_MAX;
    254 
    255 			ksi.boottime = boottime;
    256 			COPY(hostname);
    257 
    258 			size = (us - (char *) &usi[1]) + sizeof(ksi);
    259 
    260 			if ((error = copyout(&ksi, usi, sizeof(ksi))) != 0)
    261 				return error;
    262 		}
    263 		break;
    264 
    265 	default:
    266 		return (EOPNOTSUPP);
    267 	}
    268 	if (error)
    269 		return (error);
    270 	*retval = size;
    271 	if (SCARG(uap, size))
    272 		error = copyout((caddr_t)&size, (caddr_t)SCARG(uap, size),
    273 		    sizeof(size));
    274 	return (error);
    275 }
    276 
    277 
    278 /* ARGSUSED */
    279 int
    280 compat_43_sys_sethostid(struct lwp *l, void *v, register_t *retval)
    281 {
    282 	struct compat_43_sys_sethostid_args /* {
    283 		syscallarg(int32_t) hostid;
    284 	} */ *uap = v;
    285 	struct proc *p = l->l_proc;
    286 	int error;
    287 
    288 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    289 		return (error);
    290 	hostid = SCARG(uap, hostid);
    291 	return (0);
    292 }
    293 
    294 
    295 /* ARGSUSED */
    296 int
    297 compat_43_sys_sethostname(struct lwp *l, void *v, register_t *retval)
    298 {
    299 	struct compat_43_sys_sethostname_args *uap = v;
    300 	struct proc *p = l->l_proc;
    301 	int name;
    302 	int error;
    303 
    304 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    305 		return (error);
    306 	name = KERN_HOSTNAME;
    307 	return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, hostname),
    308 			    SCARG(uap, len), l));
    309 }
    310