Home | History | Annotate | Line # | Download | only in common
kern_info_43.c revision 1.35.28.3
      1  1.35.28.3  pgoyette /*	$NetBSD: kern_info_43.c,v 1.35.28.3 2018/09/22 04:56:28 pgoyette Exp $	*/
      2        1.1  christos 
      3        1.1  christos /*
      4        1.1  christos  * Copyright (c) 1982, 1986, 1991, 1993
      5        1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6        1.1  christos  *
      7        1.1  christos  * Redistribution and use in source and binary forms, with or without
      8        1.1  christos  * modification, are permitted provided that the following conditions
      9        1.1  christos  * are met:
     10        1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11        1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12        1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14        1.1  christos  *    documentation and/or other materials provided with the distribution.
     15       1.17       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1  christos  *    may be used to endorse or promote products derived from this software
     17        1.1  christos  *    without specific prior written permission.
     18        1.1  christos  *
     19        1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1  christos  * SUCH DAMAGE.
     30        1.1  christos  *
     31        1.1  christos  *	@(#)subr_xxx.c	8.1 (Berkeley) 6/10/93
     32        1.1  christos  */
     33       1.13     lukem 
     34       1.13     lukem #include <sys/cdefs.h>
     35  1.35.28.3  pgoyette __KERNEL_RCSID(0, "$NetBSD: kern_info_43.c,v 1.35.28.3 2018/09/22 04:56:28 pgoyette Exp $");
     36  1.35.28.3  pgoyette 
     37  1.35.28.3  pgoyette #if defined(_KERNEL_OPT)
     38  1.35.28.3  pgoyette #include "opt_compat_netbsd.h"
     39  1.35.28.3  pgoyette #endif
     40        1.8       mrg 
     41        1.1  christos #include <sys/param.h>
     42        1.1  christos #include <sys/systm.h>
     43        1.1  christos #include <sys/filedesc.h>
     44        1.1  christos #include <sys/kernel.h>
     45        1.1  christos #include <sys/vnode.h>
     46        1.1  christos #include <sys/proc.h>
     47        1.1  christos #include <sys/file.h>
     48        1.1  christos #include <sys/socket.h>
     49        1.1  christos #include <sys/socketvar.h>
     50        1.1  christos #include <sys/stat.h>
     51        1.1  christos #include <sys/ioctl.h>
     52        1.1  christos #include <sys/fcntl.h>
     53        1.1  christos #include <sys/syslog.h>
     54        1.1  christos #include <sys/unistd.h>
     55        1.1  christos #include <sys/resourcevar.h>
     56       1.22      elad #include <sys/kauth.h>
     57       1.35  christos #include <sys/cpu.h>
     58        1.9       mrg 
     59       1.12       mrg #include <uvm/uvm_extern.h>
     60        1.1  christos #include <sys/sysctl.h>
     61        1.7       mrg 
     62        1.1  christos #include <sys/mount.h>
     63  1.35.28.1  pgoyette #include <sys/syscall.h>
     64  1.35.28.1  pgoyette #include <sys/syscallvar.h>
     65        1.1  christos #include <sys/syscallargs.h>
     66       1.34  christos #include <compat/sys/time.h>
     67        1.1  christos 
     68  1.35.28.1  pgoyette #include <compat/common/compat_mod.h>
     69  1.35.28.1  pgoyette 
     70  1.35.28.1  pgoyette static struct syscall_package kern_info_43_syscalls[] = {
     71  1.35.28.1  pgoyette 	{ SYS_compat_43_ogetdtablesize, 0,
     72  1.35.28.1  pgoyette 	    (sy_call_t *)compat_43_sys_getdtablesize },
     73  1.35.28.1  pgoyette 	{ SYS_compat_43_ogethostid, 0, (sy_call_t *)compat_43_sys_gethostid },
     74  1.35.28.1  pgoyette         { SYS_compat_43_ogethostname, 0,
     75  1.35.28.1  pgoyette 	    (sy_call_t *)compat_43_sys_gethostname },
     76  1.35.28.1  pgoyette 	{ SYS_compat_43_ogetkerninfo, 0,
     77  1.35.28.1  pgoyette 	    (sy_call_t *)compat_43_sys_getkerninfo },
     78  1.35.28.1  pgoyette 	{ SYS_compat_43_osethostid, 0, (sy_call_t *)compat_43_sys_sethostid },
     79  1.35.28.1  pgoyette 	{ SYS_compat_43_osethostname, 0,
     80  1.35.28.1  pgoyette 	    (sy_call_t *)compat_43_sys_sethostname },
     81  1.35.28.1  pgoyette 	{ 0, 0, NULL }
     82  1.35.28.1  pgoyette };
     83  1.35.28.1  pgoyette 
     84        1.1  christos int
     85       1.30       dsl compat_43_sys_getdtablesize(struct lwp *l, const void *v, register_t *retval)
     86        1.1  christos {
     87       1.14   thorpej 	struct proc *p = l->l_proc;
     88        1.1  christos 
     89       1.33        ad 	mutex_enter(p->p_lock);
     90  1.35.28.2  pgoyette 	*retval = uimin((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
     91       1.33        ad 	mutex_exit(p->p_lock);
     92        1.1  christos 	return (0);
     93        1.1  christos }
     94        1.1  christos 
     95        1.1  christos 
     96        1.1  christos /* ARGSUSED */
     97        1.1  christos int
     98       1.30       dsl compat_43_sys_gethostid(struct lwp *l, const void *v, register_t *retval)
     99        1.1  christos {
    100        1.1  christos 
    101        1.1  christos 	*(int32_t *)retval = hostid;
    102        1.1  christos 	return (0);
    103        1.1  christos }
    104        1.1  christos 
    105        1.1  christos 
    106        1.1  christos /*ARGSUSED*/
    107        1.1  christos int
    108       1.30       dsl compat_43_sys_gethostname(struct lwp *l, const struct compat_43_sys_gethostname_args *uap, register_t *retval)
    109        1.2   thorpej {
    110       1.30       dsl 	/* {
    111        1.1  christos 		syscallarg(char *) hostname;
    112        1.1  christos 		syscallarg(u_int) len;
    113       1.30       dsl 	} */
    114       1.19    atatat 	int name[2];
    115        1.5       cgd 	size_t sz;
    116        1.1  christos 
    117       1.19    atatat 	name[0] = CTL_KERN;
    118       1.19    atatat 	name[1] = KERN_HOSTNAME;
    119        1.5       cgd 	sz = SCARG(uap, len);
    120       1.19    atatat 	return (old_sysctl(&name[0], 2, SCARG(uap, hostname), &sz, 0, 0, l));
    121        1.1  christos }
    122        1.1  christos 
    123        1.1  christos #define	KINFO_PROC		(0<<8)
    124        1.1  christos #define	KINFO_RT		(1<<8)
    125        1.1  christos #define	KINFO_VNODE		(2<<8)
    126        1.1  christos #define	KINFO_FILE		(3<<8)
    127        1.1  christos #define	KINFO_METER		(4<<8)
    128        1.1  christos #define	KINFO_LOADAVG		(5<<8)
    129        1.1  christos #define	KINFO_CLOCKRATE		(6<<8)
    130        1.6  christos #define	KINFO_BSDI_SYSINFO	(101<<8)
    131        1.6  christos 
    132        1.6  christos 
    133        1.6  christos /*
    134        1.6  christos  * The string data is appended to the end of the bsdi_si structure during
    135        1.6  christos  * copyout. The "char *" offsets in the bsdi_si struct are relative to the
    136       1.20     perry  * base of the bsdi_si struct.
    137        1.6  christos  */
    138        1.6  christos struct bsdi_si {
    139        1.6  christos         char    *machine;
    140        1.6  christos         char    *cpu_model;
    141        1.6  christos         long    ncpu;
    142        1.6  christos         long    cpuspeed;
    143        1.6  christos         long    hwflags;
    144        1.6  christos         u_long  physmem;
    145        1.6  christos         u_long  usermem;
    146        1.6  christos         u_long  pagesize;
    147        1.6  christos 
    148        1.6  christos         char    *ostype;
    149        1.6  christos         char    *osrelease;
    150        1.6  christos         long    os_revision;
    151        1.6  christos         long    posix1_version;
    152        1.6  christos         char    *version;
    153        1.6  christos 
    154        1.6  christos         long    hz;
    155        1.6  christos         long    profhz;
    156        1.6  christos         int     ngroups_max;
    157        1.6  christos         long    arg_max;
    158        1.6  christos         long    open_max;
    159        1.6  christos         long    child_max;
    160        1.6  christos 
    161       1.34  christos         struct  timeval50 boottime;
    162        1.6  christos         char    *hostname;
    163        1.6  christos };
    164        1.1  christos 
    165        1.1  christos int
    166       1.30       dsl compat_43_sys_getkerninfo(struct lwp *l, const struct compat_43_sys_getkerninfo_args *uap, register_t *retval)
    167        1.2   thorpej {
    168       1.30       dsl 	/* {
    169        1.1  christos 		syscallarg(int) op;
    170        1.1  christos 		syscallarg(char *) where;
    171        1.1  christos 		syscallarg(int *) size;
    172        1.1  christos 		syscallarg(int) arg;
    173       1.30       dsl 	} */
    174       1.19    atatat 	int error, name[6];
    175        1.1  christos 	size_t size;
    176        1.1  christos 
    177       1.29  christos 	if (SCARG(uap, size) && (error = copyin((void *)SCARG(uap, size),
    178       1.29  christos 	    (void *)&size, sizeof(size))))
    179        1.1  christos 		return (error);
    180        1.1  christos 
    181        1.1  christos 	switch (SCARG(uap, op) & 0xff00) {
    182        1.1  christos 
    183        1.1  christos 	case KINFO_RT:
    184       1.19    atatat 		name[0] = CTL_NET;
    185       1.19    atatat 		name[1] = PF_ROUTE;
    186       1.19    atatat 		name[2] = 0;
    187       1.19    atatat 		name[3] = (SCARG(uap, op) & 0xff0000) >> 16;
    188       1.19    atatat 		name[4] = SCARG(uap, op) & 0xff;
    189       1.19    atatat 		name[5] = SCARG(uap, arg);
    190       1.19    atatat 		error = old_sysctl(&name[0], 6, SCARG(uap, where), &size,
    191       1.19    atatat 				   NULL, 0, l);
    192        1.1  christos 		break;
    193        1.1  christos 
    194        1.1  christos 	case KINFO_VNODE:
    195       1.19    atatat 		name[0] = CTL_KERN;
    196       1.19    atatat 		name[1] = KERN_VNODE;
    197       1.19    atatat 		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
    198       1.19    atatat 				   NULL, 0, l);
    199        1.1  christos 		break;
    200        1.1  christos 
    201        1.1  christos 	case KINFO_PROC:
    202       1.19    atatat 		name[0] = CTL_KERN;
    203       1.19    atatat 		name[1] = KERN_PROC;
    204       1.19    atatat 		name[2] = SCARG(uap, op) & 0xff;
    205       1.19    atatat 		name[3] = SCARG(uap, arg);
    206       1.19    atatat 		error = old_sysctl(&name[0], 4, SCARG(uap, where), &size,
    207       1.19    atatat 				   NULL, 0, l);
    208        1.1  christos 		break;
    209        1.1  christos 
    210        1.1  christos 	case KINFO_FILE:
    211       1.19    atatat 		name[0] = CTL_KERN;
    212       1.19    atatat 		name[1] = KERN_FILE;
    213       1.19    atatat 		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
    214       1.19    atatat 				   NULL, 0, l);
    215        1.1  christos 		break;
    216        1.1  christos 
    217        1.1  christos 	case KINFO_METER:
    218       1.19    atatat 		name[0] = CTL_VM;
    219       1.19    atatat 		name[1] = VM_METER;
    220       1.19    atatat 		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
    221       1.19    atatat 				   NULL, 0, l);
    222        1.1  christos 		break;
    223        1.1  christos 
    224        1.1  christos 	case KINFO_LOADAVG:
    225       1.19    atatat 		name[0] = CTL_VM;
    226       1.19    atatat 		name[1] = VM_LOADAVG;
    227       1.19    atatat 		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
    228       1.19    atatat 				   NULL, 0, l);
    229        1.1  christos 		break;
    230        1.1  christos 
    231        1.1  christos 	case KINFO_CLOCKRATE:
    232       1.19    atatat 		name[0] = CTL_KERN;
    233       1.19    atatat 		name[1] = KERN_CLOCKRATE;
    234       1.19    atatat 		error = old_sysctl(&name[0], 2, SCARG(uap, where), &size,
    235       1.19    atatat 				   NULL, 0, l);
    236        1.6  christos 		break;
    237        1.6  christos 
    238        1.6  christos 
    239        1.6  christos 	case KINFO_BSDI_SYSINFO:
    240        1.6  christos 		{
    241        1.6  christos 			size_t len;
    242        1.6  christos 			struct bsdi_si *usi =
    243        1.6  christos 			    (struct bsdi_si *) SCARG(uap, where);
    244        1.6  christos 			struct bsdi_si ksi;
    245       1.34  christos 			struct timeval tv;
    246       1.35  christos 			const char *cpu_model = cpu_getmodel();
    247        1.6  christos 			char *us = (char *) &usi[1];
    248        1.6  christos 
    249        1.6  christos 			if (usi == NULL) {
    250        1.6  christos 				size = sizeof(ksi) +
    251        1.6  christos 				    strlen(ostype) + strlen(cpu_model) +
    252        1.6  christos 				    strlen(osrelease) + strlen(machine) +
    253        1.6  christos 				    strlen(version) + strlen(hostname) + 6;
    254        1.6  christos 				error = 0;
    255        1.6  christos 				break;
    256        1.6  christos 			}
    257        1.6  christos 
    258        1.6  christos #define COPY(fld)							\
    259        1.6  christos 			ksi.fld = us - (u_long) usi;			\
    260        1.6  christos 			if ((error = copyoutstr(fld, us, 1024, &len)) != 0)\
    261        1.6  christos 				return error;				\
    262        1.6  christos 			us += len
    263        1.6  christos 
    264        1.6  christos 			COPY(machine);
    265        1.6  christos 			COPY(cpu_model);
    266       1.28        ad 			ksi.ncpu = ncpu;		/* XXX */
    267        1.6  christos 			ksi.cpuspeed = 40;		/* XXX */
    268        1.6  christos 			ksi.hwflags = 0;		/* XXX */
    269        1.6  christos 			ksi.physmem = ctob(physmem);
    270        1.6  christos 			ksi.usermem = ctob(physmem);	/* XXX */
    271        1.6  christos 			ksi.pagesize = PAGE_SIZE;
    272        1.6  christos 
    273        1.6  christos 			COPY(ostype);
    274        1.6  christos 			COPY(osrelease);
    275        1.6  christos 			ksi.os_revision = NetBSD;	/* XXX */
    276        1.6  christos 			ksi.posix1_version = _POSIX_VERSION;
    277        1.6  christos 			COPY(version);			/* XXX */
    278        1.6  christos 
    279        1.6  christos 			ksi.hz = hz;
    280        1.6  christos 			ksi.profhz = profhz;
    281        1.6  christos 			ksi.ngroups_max = NGROUPS_MAX;
    282        1.6  christos 			ksi.arg_max = ARG_MAX;
    283        1.6  christos 			ksi.open_max = OPEN_MAX;
    284        1.6  christos 			ksi.child_max = CHILD_MAX;
    285        1.6  christos 
    286       1.34  christos 			TIMESPEC_TO_TIMEVAL(&tv, &boottime);
    287       1.34  christos 			timeval_to_timeval50(&tv, &ksi.boottime);
    288        1.6  christos 			COPY(hostname);
    289        1.6  christos 
    290        1.6  christos 			size = (us - (char *) &usi[1]) + sizeof(ksi);
    291        1.6  christos 
    292        1.6  christos 			if ((error = copyout(&ksi, usi, sizeof(ksi))) != 0)
    293        1.6  christos 				return error;
    294        1.6  christos 		}
    295        1.1  christos 		break;
    296        1.1  christos 
    297        1.1  christos 	default:
    298        1.1  christos 		return (EOPNOTSUPP);
    299        1.1  christos 	}
    300        1.1  christos 	if (error)
    301        1.1  christos 		return (error);
    302        1.1  christos 	*retval = size;
    303        1.1  christos 	if (SCARG(uap, size))
    304       1.29  christos 		error = copyout((void *)&size, (void *)SCARG(uap, size),
    305        1.1  christos 		    sizeof(size));
    306        1.1  christos 	return (error);
    307        1.1  christos }
    308        1.1  christos 
    309        1.1  christos 
    310        1.1  christos /* ARGSUSED */
    311        1.1  christos int
    312       1.30       dsl compat_43_sys_sethostid(struct lwp *l, const struct compat_43_sys_sethostid_args *uap, register_t *retval)
    313        1.2   thorpej {
    314       1.32      elad 	long uhostid;
    315       1.32      elad 	int name[2];
    316       1.32      elad 
    317       1.32      elad 	uhostid = SCARG(uap, hostid);
    318       1.32      elad 	name[0] = CTL_KERN;
    319       1.32      elad 	name[1] = KERN_HOSTID;
    320        1.1  christos 
    321       1.32      elad 	return (old_sysctl(&name[0], 2, 0, 0, &uhostid, sizeof(long), l));
    322        1.1  christos }
    323        1.1  christos 
    324        1.1  christos 
    325        1.1  christos /* ARGSUSED */
    326        1.1  christos int
    327       1.30       dsl compat_43_sys_sethostname(struct lwp *l, const struct compat_43_sys_sethostname_args *uap, register_t *retval)
    328        1.1  christos {
    329       1.19    atatat 	int name[2];
    330        1.1  christos 
    331       1.19    atatat 	name[0] = CTL_KERN;
    332       1.19    atatat 	name[1] = KERN_HOSTNAME;
    333       1.19    atatat 	return (old_sysctl(&name[0], 2, 0, 0, SCARG(uap, hostname),
    334       1.19    atatat 			   SCARG(uap, len), l));
    335        1.1  christos }
    336  1.35.28.1  pgoyette 
    337  1.35.28.1  pgoyette int
    338  1.35.28.1  pgoyette kern_info_43_init(void)
    339  1.35.28.1  pgoyette {
    340  1.35.28.1  pgoyette 
    341  1.35.28.1  pgoyette 	return syscall_establish(NULL, kern_info_43_syscalls);
    342  1.35.28.1  pgoyette }
    343  1.35.28.1  pgoyette 
    344  1.35.28.1  pgoyette int
    345  1.35.28.1  pgoyette kern_info_43_fini(void)
    346  1.35.28.1  pgoyette {
    347  1.35.28.1  pgoyette 
    348  1.35.28.1  pgoyette 	return syscall_disestablish(NULL, kern_info_43_syscalls);
    349  1.35.28.1  pgoyette }
    350