xref: /freebsd/sys/kern/kern_xxx.c (revision 27aef04699fda115d1899d414e1f485886a4f335)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)kern_xxx.c	8.2 (Berkeley) 11/14/93
3427aef046SPoul-Henning Kamp  * $Id: kern_xxx.c,v 1.18 1995/11/12 07:04:30 bde Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39d2d3e875SBruce Evans #include <sys/sysproto.h>
40df8bae1dSRodney W. Grimes #include <sys/kernel.h>
41df8bae1dSRodney W. Grimes #include <sys/proc.h>
42df8bae1dSRodney W. Grimes #include <sys/reboot.h>
43df8bae1dSRodney W. Grimes #include <vm/vm.h>
44df8bae1dSRodney W. Grimes #include <sys/sysctl.h>
4526f9a767SRodney W. Grimes #include <sys/utsname.h>
46797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
4726f9a767SRodney W. Grimes 
4854213fd8SPoul-Henning Kamp /* This implements a "TEXT_SET" for cleanup functions */
4954213fd8SPoul-Henning Kamp 
5054213fd8SPoul-Henning Kamp static void
5154213fd8SPoul-Henning Kamp dummy_cleanup() {}
5254213fd8SPoul-Henning Kamp TEXT_SET(cleanup_set, dummy_cleanup);
5354213fd8SPoul-Henning Kamp 
5454213fd8SPoul-Henning Kamp typedef void (*cleanup_func_t)(void);
5554213fd8SPoul-Henning Kamp extern const struct linker_set cleanup_set;
5654213fd8SPoul-Henning Kamp static const cleanup_func_t *cleanups =
5754213fd8SPoul-Henning Kamp         (const cleanup_func_t *)&cleanup_set.ls_items[0];
5854213fd8SPoul-Henning Kamp 
59d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
60df8bae1dSRodney W. Grimes struct reboot_args {
61df8bae1dSRodney W. Grimes 	int	opt;
62df8bae1dSRodney W. Grimes };
63d2d3e875SBruce Evans #endif
64df8bae1dSRodney W. Grimes /* ARGSUSED */
6526f9a767SRodney W. Grimes int
66df8bae1dSRodney W. Grimes reboot(p, uap, retval)
67df8bae1dSRodney W. Grimes 	struct proc *p;
68df8bae1dSRodney W. Grimes 	struct reboot_args *uap;
69df8bae1dSRodney W. Grimes 	int *retval;
70df8bae1dSRodney W. Grimes {
71df8bae1dSRodney W. Grimes 	int error;
72df8bae1dSRodney W. Grimes 
73bb56ec4aSPoul-Henning Kamp 	if ((error = suser(p->p_ucred, &p->p_acflag)))
74df8bae1dSRodney W. Grimes 		return (error);
7554213fd8SPoul-Henning Kamp 
7654213fd8SPoul-Henning Kamp 	if (!uap->opt & RB_NOSYNC) {
7754213fd8SPoul-Henning Kamp 		printf("\ncleaning up... ");
7854213fd8SPoul-Henning Kamp                 while(*cleanups) {
7954213fd8SPoul-Henning Kamp                         (**cleanups++)();
8054213fd8SPoul-Henning Kamp                 }
8154213fd8SPoul-Henning Kamp 	}
8254213fd8SPoul-Henning Kamp 
83df8bae1dSRodney W. Grimes 	boot(uap->opt);
84df8bae1dSRodney W. Grimes 	return (0);
85df8bae1dSRodney W. Grimes }
86df8bae1dSRodney W. Grimes 
87df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
88df8bae1dSRodney W. Grimes 
89d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
90df8bae1dSRodney W. Grimes struct gethostname_args {
91df8bae1dSRodney W. Grimes 	char	*hostname;
92df8bae1dSRodney W. Grimes 	u_int	len;
93df8bae1dSRodney W. Grimes };
94d2d3e875SBruce Evans #endif
95df8bae1dSRodney W. Grimes /* ARGSUSED */
9626f9a767SRodney W. Grimes int
97df8bae1dSRodney W. Grimes ogethostname(p, uap, retval)
98df8bae1dSRodney W. Grimes 	struct proc *p;
99df8bae1dSRodney W. Grimes 	struct gethostname_args *uap;
100df8bae1dSRodney W. Grimes 	int *retval;
101df8bae1dSRodney W. Grimes {
102b8da2396SPoul-Henning Kamp 	int name[2];
103df8bae1dSRodney W. Grimes 
104b8da2396SPoul-Henning Kamp 	name[0] = CTL_KERN;
105b8da2396SPoul-Henning Kamp 	name[1] = KERN_HOSTNAME;
106b8da2396SPoul-Henning Kamp 	return (userland_sysctl(p, name, 2, uap->hostname, &uap->len,
107b8da2396SPoul-Henning Kamp 		1, 0, 0, 0));
108df8bae1dSRodney W. Grimes }
109df8bae1dSRodney W. Grimes 
110d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
111df8bae1dSRodney W. Grimes struct sethostname_args {
112df8bae1dSRodney W. Grimes 	char	*hostname;
113df8bae1dSRodney W. Grimes 	u_int	len;
114df8bae1dSRodney W. Grimes };
115d2d3e875SBruce Evans #endif
116df8bae1dSRodney W. Grimes /* ARGSUSED */
11726f9a767SRodney W. Grimes int
118df8bae1dSRodney W. Grimes osethostname(p, uap, retval)
119df8bae1dSRodney W. Grimes 	struct proc *p;
120df8bae1dSRodney W. Grimes 	register struct sethostname_args *uap;
121df8bae1dSRodney W. Grimes 	int *retval;
122df8bae1dSRodney W. Grimes {
123b8da2396SPoul-Henning Kamp 	int name[2];
124df8bae1dSRodney W. Grimes 	int error;
125df8bae1dSRodney W. Grimes 
126b8da2396SPoul-Henning Kamp 	name[0] = CTL_KERN;
127b8da2396SPoul-Henning Kamp 	name[1] = KERN_HOSTNAME;
128bb56ec4aSPoul-Henning Kamp 	if ((error = suser(p->p_ucred, &p->p_acflag)))
129df8bae1dSRodney W. Grimes 		return (error);
130b8da2396SPoul-Henning Kamp 	return (userland_sysctl(p, name, 2, 0, 0, 0,
131b8da2396SPoul-Henning Kamp 		uap->hostname, uap->len, 0));
132df8bae1dSRodney W. Grimes }
133df8bae1dSRodney W. Grimes 
134d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1352635f86cSBruce Evans struct ogethostid_args {
136df8bae1dSRodney W. Grimes 	int	dummy;
137df8bae1dSRodney W. Grimes };
138d2d3e875SBruce Evans #endif
139df8bae1dSRodney W. Grimes /* ARGSUSED */
14026f9a767SRodney W. Grimes int
141df8bae1dSRodney W. Grimes ogethostid(p, uap, retval)
142df8bae1dSRodney W. Grimes 	struct proc *p;
1432635f86cSBruce Evans 	struct ogethostid_args *uap;
144df8bae1dSRodney W. Grimes 	int *retval;
145df8bae1dSRodney W. Grimes {
146df8bae1dSRodney W. Grimes 
147df8bae1dSRodney W. Grimes 	*(long *)retval = hostid;
148df8bae1dSRodney W. Grimes 	return (0);
149df8bae1dSRodney W. Grimes }
150df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
151df8bae1dSRodney W. Grimes 
152df8bae1dSRodney W. Grimes #ifdef COMPAT_43
153d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1542635f86cSBruce Evans struct osethostid_args {
155df8bae1dSRodney W. Grimes 	long	hostid;
156df8bae1dSRodney W. Grimes };
157d2d3e875SBruce Evans #endif
158df8bae1dSRodney W. Grimes /* ARGSUSED */
15926f9a767SRodney W. Grimes int
160df8bae1dSRodney W. Grimes osethostid(p, uap, retval)
161df8bae1dSRodney W. Grimes 	struct proc *p;
1622635f86cSBruce Evans 	struct osethostid_args *uap;
163df8bae1dSRodney W. Grimes 	int *retval;
164df8bae1dSRodney W. Grimes {
165df8bae1dSRodney W. Grimes 	int error;
166df8bae1dSRodney W. Grimes 
167bb56ec4aSPoul-Henning Kamp 	if ((error = suser(p->p_ucred, &p->p_acflag)))
168df8bae1dSRodney W. Grimes 		return (error);
169df8bae1dSRodney W. Grimes 	hostid = uap->hostid;
170df8bae1dSRodney W. Grimes 	return (0);
171df8bae1dSRodney W. Grimes }
172df8bae1dSRodney W. Grimes 
17326f9a767SRodney W. Grimes int
1742635f86cSBruce Evans oquota(p, uap, retval)
1752635f86cSBruce Evans 	struct proc *p;
1762635f86cSBruce Evans 	struct oquota_args *uap;
1772635f86cSBruce Evans 	int *retval;
178df8bae1dSRodney W. Grimes {
179df8bae1dSRodney W. Grimes 
180df8bae1dSRodney W. Grimes 	return (ENOSYS);
181df8bae1dSRodney W. Grimes }
182df8bae1dSRodney W. Grimes #endif /* COMPAT_43 */
18326f9a767SRodney W. Grimes 
18426f9a767SRodney W. Grimes void
18526f9a767SRodney W. Grimes shutdown_nice(void)
18626f9a767SRodney W. Grimes {
18726f9a767SRodney W. Grimes 	/* Send a signal to init(8) and have it shutdown the world */
188cd92a618SDavid Greenman 	if (initproc != NULL) {
189887f4118SDavid Greenman 		psignal(initproc, SIGINT);
190cd92a618SDavid Greenman 	} else {
191cd92a618SDavid Greenman 		/* No init(8) running, so simply reboot */
192cd92a618SDavid Greenman 		boot(RB_NOSYNC);
193cd92a618SDavid Greenman 	}
19426f9a767SRodney W. Grimes 	return;
19526f9a767SRodney W. Grimes }
19626f9a767SRodney W. Grimes 
19726f9a767SRodney W. Grimes 
198d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
19926f9a767SRodney W. Grimes struct uname_args {
20026f9a767SRodney W. Grimes         struct utsname  *name;
20126f9a767SRodney W. Grimes };
202d2d3e875SBruce Evans #endif
20326f9a767SRodney W. Grimes 
20426f9a767SRodney W. Grimes /* ARGSUSED */
20526f9a767SRodney W. Grimes int
20626f9a767SRodney W. Grimes uname(p, uap, retval)
20726f9a767SRodney W. Grimes 	struct proc *p;
20826f9a767SRodney W. Grimes 	struct uname_args *uap;
20926f9a767SRodney W. Grimes 	int *retval;
21026f9a767SRodney W. Grimes {
211b8da2396SPoul-Henning Kamp 	int name[2], len, rtval, junk;
21226f9a767SRodney W. Grimes 	char *s, *us;
21326f9a767SRodney W. Grimes 
214b8da2396SPoul-Henning Kamp 	name[0] = CTL_KERN;
215b8da2396SPoul-Henning Kamp 	name[1] = KERN_OSTYPE;
21626f9a767SRodney W. Grimes 	len = sizeof uap->name->sysname;
217b8da2396SPoul-Henning Kamp 	rtval = userland_sysctl(p, name, 2, uap->name->sysname, &len,
218b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
21926f9a767SRodney W. Grimes 	if( rtval) return rtval;
22026f9a767SRodney W. Grimes 	subyte( uap->name->sysname + sizeof(uap->name->sysname) - 1, 0);
22126f9a767SRodney W. Grimes 
222b8da2396SPoul-Henning Kamp 	name[1] = KERN_HOSTNAME;
22326f9a767SRodney W. Grimes 	len = sizeof uap->name->nodename;
224b8da2396SPoul-Henning Kamp 	rtval = userland_sysctl(p, name, 2, uap->name->nodename, &len,
225b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
22626f9a767SRodney W. Grimes 	if( rtval) return rtval;
22726f9a767SRodney W. Grimes 	subyte( uap->name->nodename + sizeof(uap->name->nodename) - 1, 0);
22826f9a767SRodney W. Grimes 
229b8da2396SPoul-Henning Kamp 	name[1] = KERN_OSRELEASE;
23026f9a767SRodney W. Grimes 	len = sizeof uap->name->release;
231b8da2396SPoul-Henning Kamp 	rtval = userland_sysctl(p, name, 2, uap->name->release, &len,
232b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
23326f9a767SRodney W. Grimes 	if( rtval) return rtval;
23426f9a767SRodney W. Grimes 	subyte( uap->name->release + sizeof(uap->name->release) - 1, 0);
23526f9a767SRodney W. Grimes 
23626f9a767SRodney W. Grimes /*
23726f9a767SRodney W. Grimes 	name = KERN_VERSION;
23826f9a767SRodney W. Grimes 	len = sizeof uap->name->version;
239b8da2396SPoul-Henning Kamp 	rtval = userland_sysctl(p, name, 2, uap->name->version, &len,
240b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
24126f9a767SRodney W. Grimes 	if( rtval) return rtval;
24226f9a767SRodney W. Grimes 	subyte( uap->name->version + sizeof(uap->name->version) - 1, 0);
24326f9a767SRodney W. Grimes */
24426f9a767SRodney W. Grimes 
24526f9a767SRodney W. Grimes /*
24626f9a767SRodney W. Grimes  * this stupid hackery to make the version field look like FreeBSD 1.1
24726f9a767SRodney W. Grimes  */
24826f9a767SRodney W. Grimes 	for(s = version; *s && *s != '#'; s++);
24926f9a767SRodney W. Grimes 
25026f9a767SRodney W. Grimes 	for(us = uap->name->version; *s && *s != ':'; s++) {
25126f9a767SRodney W. Grimes 		rtval = subyte( us++, *s);
25226f9a767SRodney W. Grimes 		if( rtval)
25326f9a767SRodney W. Grimes 			return rtval;
25426f9a767SRodney W. Grimes 	}
25526f9a767SRodney W. Grimes 	rtval = subyte( us++, 0);
25626f9a767SRodney W. Grimes 	if( rtval)
25726f9a767SRodney W. Grimes 		return rtval;
25826f9a767SRodney W. Grimes 
259b8da2396SPoul-Henning Kamp 	name[1] = HW_MACHINE;
26026f9a767SRodney W. Grimes 	len = sizeof uap->name->machine;
261b8da2396SPoul-Henning Kamp 	rtval = userland_sysctl(p, name, 2, uap->name->machine, &len,
262b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
26326f9a767SRodney W. Grimes 	if( rtval) return rtval;
26426f9a767SRodney W. Grimes 	subyte( uap->name->machine + sizeof(uap->name->machine) - 1, 0);
26526f9a767SRodney W. Grimes 
26626f9a767SRodney W. Grimes 	return 0;
26726f9a767SRodney W. Grimes }
26826f9a767SRodney W. Grimes 
269d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
27026f9a767SRodney W. Grimes struct getdomainname_args {
27126f9a767SRodney W. Grimes         char    *domainname;
272f35a53bfSBruce Evans         int     len;
27326f9a767SRodney W. Grimes };
274d2d3e875SBruce Evans #endif
275f35a53bfSBruce Evans 
27626f9a767SRodney W. Grimes /* ARGSUSED */
27726f9a767SRodney W. Grimes int
27826f9a767SRodney W. Grimes getdomainname(p, uap, retval)
27926f9a767SRodney W. Grimes         struct proc *p;
28026f9a767SRodney W. Grimes         struct getdomainname_args *uap;
28126f9a767SRodney W. Grimes         int *retval;
28226f9a767SRodney W. Grimes {
28327aef046SPoul-Henning Kamp 	int domainnamelen = strlen(domainname) + 1;
284f35a53bfSBruce Evans 	if ((u_int)uap->len > domainnamelen + 1)
28526f9a767SRodney W. Grimes 		uap->len = domainnamelen + 1;
28626f9a767SRodney W. Grimes 	return (copyout((caddr_t)domainname, (caddr_t)uap->domainname, uap->len));
28726f9a767SRodney W. Grimes }
28826f9a767SRodney W. Grimes 
289d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
29026f9a767SRodney W. Grimes struct setdomainname_args {
29126f9a767SRodney W. Grimes         char    *domainname;
292f35a53bfSBruce Evans         int     len;
29326f9a767SRodney W. Grimes };
294d2d3e875SBruce Evans #endif
29526f9a767SRodney W. Grimes 
29626f9a767SRodney W. Grimes /* ARGSUSED */
29726f9a767SRodney W. Grimes int
29826f9a767SRodney W. Grimes setdomainname(p, uap, retval)
29926f9a767SRodney W. Grimes         struct proc *p;
30026f9a767SRodney W. Grimes         struct setdomainname_args *uap;
30126f9a767SRodney W. Grimes         int *retval;
30226f9a767SRodney W. Grimes {
30327aef046SPoul-Henning Kamp         int error, domainnamelen;
30426f9a767SRodney W. Grimes 
305bb56ec4aSPoul-Henning Kamp         if ((error = suser(p->p_ucred, &p->p_acflag)))
30626f9a767SRodney W. Grimes                 return (error);
307f35a53bfSBruce Evans         if ((u_int)uap->len > sizeof (domainname) - 1)
30826f9a767SRodney W. Grimes                 return EINVAL;
30926f9a767SRodney W. Grimes         domainnamelen = uap->len;
31026f9a767SRodney W. Grimes         error = copyin((caddr_t)uap->domainname, domainname, uap->len);
31126f9a767SRodney W. Grimes         domainname[domainnamelen] = 0;
31226f9a767SRodney W. Grimes         return (error);
31326f9a767SRodney W. Grimes }
31426f9a767SRodney W. Grimes 
315