kern_mib.c revision 87275
115103Sphk/*-
215103Sphk * Copyright (c) 1982, 1986, 1989, 1993
315103Sphk *	The Regents of the University of California.  All rights reserved.
415103Sphk *
515103Sphk * This code is derived from software contributed to Berkeley by
615103Sphk * Mike Karels at Berkeley Software Design, Inc.
715103Sphk *
815103Sphk * Quite extensively rewritten by Poul-Henning Kamp of the FreeBSD
915103Sphk * project, to make these variables more userfriendly.
1015103Sphk *
1115103Sphk * Redistribution and use in source and binary forms, with or without
1215103Sphk * modification, are permitted provided that the following conditions
1315103Sphk * are met:
1415103Sphk * 1. Redistributions of source code must retain the above copyright
1515103Sphk *    notice, this list of conditions and the following disclaimer.
1615103Sphk * 2. Redistributions in binary form must reproduce the above copyright
1715103Sphk *    notice, this list of conditions and the following disclaimer in the
1815103Sphk *    documentation and/or other materials provided with the distribution.
1915103Sphk * 3. All advertising materials mentioning features or use of this software
2015103Sphk *    must display the following acknowledgement:
2115103Sphk *	This product includes software developed by the University of
2215103Sphk *	California, Berkeley and its contributors.
2315103Sphk * 4. Neither the name of the University nor the names of its contributors
2415103Sphk *    may be used to endorse or promote products derived from this software
2515103Sphk *    without specific prior written permission.
2615103Sphk *
2715103Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2815103Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2915103Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3015103Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3115103Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3215103Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3315103Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3415103Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3515103Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3615103Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3715103Sphk * SUCH DAMAGE.
3815103Sphk *
3915103Sphk *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
4050477Speter * $FreeBSD: head/sys/kern/kern_mib.c 87275 2001-12-03 16:12:27Z rwatson $
4115103Sphk */
4215103Sphk
4384611Srwatson#include "opt_global.h"
4486190Srwatson#include "opt_posix.h"
4584611Srwatson
4615103Sphk#include <sys/param.h>
4715103Sphk#include <sys/kernel.h>
4815103Sphk#include <sys/systm.h>
4915103Sphk#include <sys/sysctl.h>
5015103Sphk#include <sys/proc.h>
5187275Srwatson#include <sys/lock.h>
5287275Srwatson#include <sys/mutex.h>
5346155Sphk#include <sys/jail.h>
5476078Sjhb#include <sys/smp.h>
5528918Skato
5615103SphkSYSCTL_NODE(, 0,	  sysctl, CTLFLAG_RW, 0,
5715103Sphk	"Sysctl internal magic");
5815103SphkSYSCTL_NODE(, CTL_KERN,	  kern,   CTLFLAG_RW, 0,
5915103Sphk	"High kernel, proc, limits &c");
6015103SphkSYSCTL_NODE(, CTL_VM,	  vm,     CTLFLAG_RW, 0,
6115103Sphk	"Virtual memory");
6223382SbdeSYSCTL_NODE(, CTL_VFS,	  vfs,     CTLFLAG_RW, 0,
6315103Sphk	"File system");
6415103SphkSYSCTL_NODE(, CTL_NET,	  net,    CTLFLAG_RW, 0,
6515103Sphk	"Network, (see socket.h)");
6615103SphkSYSCTL_NODE(, CTL_DEBUG,  debug,  CTLFLAG_RW, 0,
6715103Sphk	"Debugging");
6848891SphkSYSCTL_NODE(_debug, OID_AUTO,  sizeof,  CTLFLAG_RW, 0,
6948891Sphk	"Sizeof various things");
7015103SphkSYSCTL_NODE(, CTL_HW,	  hw,     CTLFLAG_RW, 0,
7115103Sphk	"hardware");
7215103SphkSYSCTL_NODE(, CTL_MACHDEP, machdep, CTLFLAG_RW, 0,
7315103Sphk	"machine dependent");
7415103SphkSYSCTL_NODE(, CTL_USER,	  user,   CTLFLAG_RW, 0,
7515103Sphk	"user-level");
7634925SdufaultSYSCTL_NODE(, CTL_P1003_1B,  p1003_1b,   CTLFLAG_RW, 0,
7734925Sdufault	"p1003_1b, (see p1003_1b.h)");
7834029Sdufault
7950465SmarcelSYSCTL_NODE(, OID_AUTO,  compat, CTLFLAG_RW, 0,
8050465Smarcel	"Compatibility code");
8186190Srwatson#ifdef REGRESSION
8286190SrwatsonSYSCTL_NODE(, OID_AUTO, regression, CTLFLAG_RW, 0,
8386190Srwatson     "Regression test MIB");
8486190Srwatson#endif
8550465Smarcel
8686189SrwatsonSYSCTL_STRING(_kern, KERN_OSRELEASE, osrelease, CTLFLAG_RD,
8746381Sbillf    osrelease, 0, "Operating system type");
8815103Sphk
8986189SrwatsonSYSCTL_INT(_kern, KERN_OSREV, osrevision, CTLFLAG_RD,
9046381Sbillf    0, BSD, "Operating system revision");
9115103Sphk
9286189SrwatsonSYSCTL_STRING(_kern, KERN_VERSION, version, CTLFLAG_RD,
9346381Sbillf    version, 0, "Kernel version");
9415103Sphk
9586189SrwatsonSYSCTL_STRING(_kern, KERN_OSTYPE, ostype, CTLFLAG_RD,
9646381Sbillf    ostype, 0, "Operating system type");
9715103Sphk
9815103Sphkextern int osreldate;
9986189SrwatsonSYSCTL_INT(_kern, KERN_OSRELDATE, osreldate, CTLFLAG_RD,
10046381Sbillf    &osreldate, 0, "Operating system release date");
10115103Sphk
10286189SrwatsonSYSCTL_INT(_kern, KERN_MAXPROC, maxproc, CTLFLAG_RD,
10346381Sbillf    &maxproc, 0, "Maximum number of processes");
10415103Sphk
10586189SrwatsonSYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid, CTLFLAG_RW,
10646381Sbillf    &maxprocperuid, 0, "Maximum processes allowed per userid");
10715103Sphk
10886189SrwatsonSYSCTL_INT(_kern, OID_AUTO, maxusers, CTLFLAG_RD,
10980418Speter    &maxusers, 0, "Hint for kernel tuning");
11080418Speter
11186189SrwatsonSYSCTL_INT(_kern, KERN_ARGMAX, argmax, CTLFLAG_RD,
11246381Sbillf    0, ARG_MAX, "Maximum bytes of argument to execve(2)");
11315103Sphk
11486189SrwatsonSYSCTL_INT(_kern, KERN_POSIX1, posix1version, CTLFLAG_RD,
11546381Sbillf    0, _KPOSIX_VERSION, "Version of POSIX attempting to comply to");
11615103Sphk
11786189SrwatsonSYSCTL_INT(_kern, KERN_NGROUPS, ngroups, CTLFLAG_RD,
11846381Sbillf    0, NGROUPS_MAX, "Maximum number of groups a user can belong to");
11915103Sphk
12086189SrwatsonSYSCTL_INT(_kern, KERN_JOB_CONTROL, job_control, CTLFLAG_RD,
12146381Sbillf    0, 1, "Whether job control is available");
12215103Sphk
12315103Sphk#ifdef _POSIX_SAVED_IDS
12486189SrwatsonSYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD,
12546381Sbillf    0, 1, "Whether saved set-group/user ID is available");
12615103Sphk#else
12786189SrwatsonSYSCTL_INT(_kern, KERN_SAVED_IDS, saved_ids, CTLFLAG_RD,
12846381Sbillf    0, 0, "Whether saved set-group/user ID is available");
12915103Sphk#endif
13015103Sphk
13115103Sphkchar kernelname[MAXPATHLEN] = "/kernel";	/* XXX bloat */
13215103Sphk
13386189SrwatsonSYSCTL_STRING(_kern, KERN_BOOTFILE, bootfile, CTLFLAG_RW,
13446381Sbillf    kernelname, sizeof kernelname, "Name of kernel file booted");
13515103Sphk
13631990Sgpalmer#ifdef SMP
13786189SrwatsonSYSCTL_INT(_hw, HW_NCPU, ncpu, CTLFLAG_RD,
13846381Sbillf    &mp_ncpus, 0, "Number of active CPUs");
13931990Sgpalmer#else
14086189SrwatsonSYSCTL_INT(_hw, HW_NCPU, ncpu, CTLFLAG_RD,
14146381Sbillf    0, 1, "Number of active CPUs");
14231990Sgpalmer#endif
14315103Sphk
14486189SrwatsonSYSCTL_INT(_hw, HW_BYTEORDER, byteorder, CTLFLAG_RD,
14546381Sbillf    0, BYTE_ORDER, "System byte order");
14615103Sphk
14786189SrwatsonSYSCTL_INT(_hw, HW_PAGESIZE, pagesize, CTLFLAG_RD,
14846381Sbillf    0, PAGE_SIZE, "System memory page size");
14915103Sphk
15028885Skatostatic char	machine_arch[] = MACHINE_ARCH;
15128885SkatoSYSCTL_STRING(_hw, HW_MACHINE_ARCH, machine_arch, CTLFLAG_RD,
15246381Sbillf    machine_arch, 0, "System architecture");
15328885Skato
15415103Sphkchar hostname[MAXHOSTNAMELEN];
15515103Sphk
15646155Sphkstatic int
15762573Sphksysctl_hostname(SYSCTL_HANDLER_ARGS)
15846155Sphk{
15987072Srwatson	struct prison *pr;
16087275Srwatson	char tmphostname[MAXHOSTNAMELEN];
16146155Sphk	int error;
16215103Sphk
16387072Srwatson	pr = req->td->td_proc->p_ucred->cr_prison;
16487072Srwatson	if (pr != NULL) {
16557163Srwatson		if (!jail_set_hostname_allowed && req->newptr)
16686190Srwatson			return (EPERM);
16787275Srwatson		/*
16887275Srwatson		 * Process is in jail, so make a local copy of jail
16987275Srwatson		 * hostname to get/set so we don't have to hold the jail
17087275Srwatson		 * mutex during the sysctl copyin/copyout activities.
17187275Srwatson		 */
17287275Srwatson		mtx_lock(&pr->pr_mtx);
17387275Srwatson		bcopy(pr->pr_host, tmphostname, MAXHOSTNAMELEN);
17487275Srwatson		mtx_unlock(&pr->pr_mtx);
17587275Srwatson
17687275Srwatson		error = sysctl_handle_string(oidp, tmphostname,
17787072Srwatson		    sizeof pr->pr_host, req);
17887275Srwatson
17987275Srwatson		if (req->newptr != NULL && error == 0) {
18087275Srwatson			/*
18187275Srwatson			 * Copy the locally set hostname to the jail, if
18287275Srwatson			 * appropriate.
18387275Srwatson			 */
18487275Srwatson			mtx_lock(&pr->pr_mtx);
18587275Srwatson			bcopy(tmphostname, pr->pr_host, MAXHOSTNAMELEN);
18687275Srwatson			mtx_unlock(&pr->pr_mtx);
18787275Srwatson		}
18857111Srwatson	} else
18986189Srwatson		error = sysctl_handle_string(oidp,
19046155Sphk		    hostname, sizeof hostname, req);
19146155Sphk	return (error);
19246155Sphk}
19346155Sphk
19486189SrwatsonSYSCTL_PROC(_kern, KERN_HOSTNAME, hostname,
19546155Sphk       CTLTYPE_STRING|CTLFLAG_RW|CTLFLAG_PRISON,
19646381Sbillf       0, 0, sysctl_hostname, "A", "Hostname");
19746155Sphk
19886190Srwatsonstatic int	regression_securelevel_nonmonotonic = 0;
19984611Srwatson
20087072Srwatson#ifdef REGRESSION
20184611SrwatsonSYSCTL_INT(_regression, OID_AUTO, securelevel_nonmonotonic, CTLFLAG_RW,
20284611Srwatson    &regression_securelevel_nonmonotonic, 0, "securelevel may be lowered");
20386190Srwatson#endif
20484611Srwatson
20515103Sphkint securelevel = -1;
20615103Sphk
20715103Sphkstatic int
20862573Sphksysctl_kern_securelvl(SYSCTL_HANDLER_ARGS)
20915103Sphk{
21086145Srwatson	struct prison *pr;
21186140Srwatson	int error, level;
21215103Sphk
21386183Srwatson	pr = req->td->td_proc->p_ucred->cr_prison;
21486145Srwatson
21586140Srwatson	/*
21686140Srwatson	 * If the process is in jail, return the maximum of the global and
21786140Srwatson	 * local levels; otherwise, return the global level.
21886140Srwatson	 */
21987275Srwatson	if (pr != NULL) {
22087275Srwatson		mtx_lock(&pr->pr_mtx);
22186145Srwatson		level = imax(securelevel, pr->pr_securelevel);
22287275Srwatson		mtx_unlock(&pr->pr_mtx);
22387275Srwatson	} else
22486140Srwatson		level = securelevel;
22586140Srwatson	error = sysctl_handle_int(oidp, &level, 0, req);
22686140Srwatson	if (error || !req->newptr)
22786140Srwatson		return (error);
22886140Srwatson	/*
22986140Srwatson	 * Permit update only if the new securelevel exceeds the
23086140Srwatson	 * global level, and local level if any.
23186140Srwatson	 */
23286145Srwatson	if (pr != NULL) {
23387275Srwatson		mtx_lock(&pr->pr_mtx);
23487072Srwatson		if (!regression_securelevel_nonmonotonic &&
23587275Srwatson		    (level < imax(securelevel, pr->pr_securelevel))) {
23687275Srwatson			mtx_unlock(&pr->pr_mtx);
23786140Srwatson			return (EPERM);
23887275Srwatson		}
23986145Srwatson		pr->pr_securelevel = level;
24087275Srwatson		mtx_unlock(&pr->pr_mtx);
24186140Srwatson	} else {
24287072Srwatson		if (!regression_securelevel_nonmonotonic &&
24387072Srwatson		    (level < securelevel))
24486140Srwatson			return (EPERM);
24586140Srwatson		securelevel = level;
24686140Srwatson	}
24786140Srwatson	return (error);
24815103Sphk}
24915103Sphk
25083990SrwatsonSYSCTL_PROC(_kern, KERN_SECURELVL, securelevel,
25183990Srwatson    CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, sysctl_kern_securelvl,
25283990Srwatson    "I", "Current secure level");
25361370Srwatson
25415103Sphkchar domainname[MAXHOSTNAMELEN];
25517281SwollmanSYSCTL_STRING(_kern, KERN_NISDOMAINNAME, domainname, CTLFLAG_RW,
25646381Sbillf    &domainname, sizeof(domainname), "Name of the current YP/NIS domain");
25715103Sphk
25886190Srwatsonu_long hostid;
25978609SpirzykSYSCTL_ULONG(_kern, KERN_HOSTID, hostid, CTLFLAG_RW, &hostid, 0, "Host ID");
26015103Sphk
26115103Sphk/*
26215103Sphk * This is really cheating.  These actually live in the libc, something
26386189Srwatson * which I'm not quite sure is a good idea anyway, but in order for
26415103Sphk * getnext and friends to actually work, we define dummies here.
26515103Sphk */
26686189SrwatsonSYSCTL_STRING(_user, USER_CS_PATH, cs_path, CTLFLAG_RD,
26746381Sbillf    "", 0, "PATH that finds all the standard utilities");
26886189SrwatsonSYSCTL_INT(_user, USER_BC_BASE_MAX, bc_base_max, CTLFLAG_RD,
26946381Sbillf    0, 0, "Max ibase/obase values in bc(1)");
27086189SrwatsonSYSCTL_INT(_user, USER_BC_DIM_MAX, bc_dim_max, CTLFLAG_RD,
27146381Sbillf    0, 0, "Max array size in bc(1)");
27286189SrwatsonSYSCTL_INT(_user, USER_BC_SCALE_MAX, bc_scale_max, CTLFLAG_RD,
27346381Sbillf    0, 0, "Max scale value in bc(1)");
27486189SrwatsonSYSCTL_INT(_user, USER_BC_STRING_MAX, bc_string_max, CTLFLAG_RD,
27546381Sbillf    0, 0, "Max string length in bc(1)");
27686189SrwatsonSYSCTL_INT(_user, USER_COLL_WEIGHTS_MAX, coll_weights_max, CTLFLAG_RD,
27746381Sbillf    0, 0, "Maximum number of weights assigned to an LC_COLLATE locale entry");
27818540SbdeSYSCTL_INT(_user, USER_EXPR_NEST_MAX, expr_nest_max, CTLFLAG_RD, 0, 0, "");
27986189SrwatsonSYSCTL_INT(_user, USER_LINE_MAX, line_max, CTLFLAG_RD,
28046381Sbillf    0, 0, "Max length (bytes) of a text-processing utility's input line");
28186189SrwatsonSYSCTL_INT(_user, USER_RE_DUP_MAX, re_dup_max, CTLFLAG_RD,
28246381Sbillf    0, 0, "Maximum number of repeats of a regexp permitted");
28386189SrwatsonSYSCTL_INT(_user, USER_POSIX2_VERSION, posix2_version, CTLFLAG_RD,
28486189Srwatson    0, 0,
28546381Sbillf    "The version of POSIX 1003.2 with which the system attempts to comply");
28686189SrwatsonSYSCTL_INT(_user, USER_POSIX2_C_BIND, posix2_c_bind, CTLFLAG_RD,
28746381Sbillf    0, 0, "Whether C development supports the C bindings option");
28886189SrwatsonSYSCTL_INT(_user, USER_POSIX2_C_DEV, posix2_c_dev, CTLFLAG_RD,
28946381Sbillf    0, 0, "Whether system supports the C development utilities option");
29086189SrwatsonSYSCTL_INT(_user, USER_POSIX2_CHAR_TERM, posix2_char_term, CTLFLAG_RD,
29146381Sbillf    0, 0, "");
29286189SrwatsonSYSCTL_INT(_user, USER_POSIX2_FORT_DEV, posix2_fort_dev, CTLFLAG_RD,
29346381Sbillf    0, 0, "Whether system supports FORTRAN development utilities");
29486189SrwatsonSYSCTL_INT(_user, USER_POSIX2_FORT_RUN, posix2_fort_run, CTLFLAG_RD,
29546381Sbillf    0, 0, "Whether system supports FORTRAN runtime utilities");
29686189SrwatsonSYSCTL_INT(_user, USER_POSIX2_LOCALEDEF, posix2_localedef, CTLFLAG_RD,
29746381Sbillf    0, 0, "Whether system supports creation of locales");
29886189SrwatsonSYSCTL_INT(_user, USER_POSIX2_SW_DEV, posix2_sw_dev, CTLFLAG_RD,
29946381Sbillf    0, 0, "Whether system supports software development utilities");
30086189SrwatsonSYSCTL_INT(_user, USER_POSIX2_UPE, posix2_upe, CTLFLAG_RD,
30146381Sbillf    0, 0, "Whether system supports the user portability utilities");
30286189SrwatsonSYSCTL_INT(_user, USER_STREAM_MAX, stream_max, CTLFLAG_RD,
30346381Sbillf    0, 0, "Min Maximum number of streams a process may have open at one time");
30486189SrwatsonSYSCTL_INT(_user, USER_TZNAME_MAX, tzname_max, CTLFLAG_RD,
30546381Sbillf    0, 0, "Min Maximum number of types supported for timezone names");
30648891Sphk
30748891Sphk#include <sys/vnode.h>
30886189SrwatsonSYSCTL_INT(_debug_sizeof, OID_AUTO, vnode, CTLFLAG_RD,
30948891Sphk    0, sizeof(struct vnode), "sizeof(struct vnode)");
31048891Sphk
31186189SrwatsonSYSCTL_INT(_debug_sizeof, OID_AUTO, proc, CTLFLAG_RD,
31248891Sphk    0, sizeof(struct proc), "sizeof(struct proc)");
31348927Sphk
31449535Sphk#include <sys/conf.h>
31548927SphkSYSCTL_INT(_debug_sizeof, OID_AUTO, specinfo, CTLFLAG_RD,
31648927Sphk    0, sizeof(struct specinfo), "sizeof(struct specinfo)");
31758926Sphk
31860041Sphk#include <sys/bio.h>
31958926Sphk#include <sys/buf.h>
32058926SphkSYSCTL_INT(_debug_sizeof, OID_AUTO, bio, CTLFLAG_RD,
32158926Sphk    0, sizeof(struct bio), "sizeof(struct bio)");
32258926SphkSYSCTL_INT(_debug_sizeof, OID_AUTO, buf, CTLFLAG_RD,
32358926Sphk    0, sizeof(struct buf), "sizeof(struct buf)");
32472376Sjake
32572376Sjake#include <sys/user.h>
32672376SjakeSYSCTL_INT(_debug_sizeof, OID_AUTO, kinfo_proc, CTLFLAG_RD,
32772376Sjake    0, sizeof(struct kinfo_proc), "sizeof(struct kinfo_proc)");
328