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