xref: /freebsd/sys/kern/kern_xxx.c (revision b40ce4165d5eb3a5de1515245055350ae3dbab8e)
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
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
375591b823SEivind Eklund #include "opt_compat.h"
385591b823SEivind Eklund 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40df8bae1dSRodney W. Grimes #include <sys/systm.h>
41d2d3e875SBruce Evans #include <sys/sysproto.h>
42df8bae1dSRodney W. Grimes #include <sys/kernel.h>
43df8bae1dSRodney W. Grimes #include <sys/proc.h>
446f1e8c18SMatthew Dillon #include <sys/lock.h>
456f1e8c18SMatthew Dillon #include <sys/mutex.h>
46df8bae1dSRodney W. Grimes #include <sys/sysctl.h>
4726f9a767SRodney W. Grimes #include <sys/utsname.h>
4826f9a767SRodney W. Grimes 
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
51df8bae1dSRodney W. Grimes 
52d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
53df8bae1dSRodney W. Grimes struct gethostname_args {
54df8bae1dSRodney W. Grimes 	char	*hostname;
55df8bae1dSRodney W. Grimes 	u_int	len;
56df8bae1dSRodney W. Grimes };
57d2d3e875SBruce Evans #endif
586f1e8c18SMatthew Dillon /*
596f1e8c18SMatthew Dillon  * MPSAFE
606f1e8c18SMatthew Dillon  */
61df8bae1dSRodney W. Grimes /* ARGSUSED */
6226f9a767SRodney W. Grimes int
63b40ce416SJulian Elischer ogethostname(td, uap)
64b40ce416SJulian Elischer 	struct thread *td;
65df8bae1dSRodney W. Grimes 	struct gethostname_args *uap;
66df8bae1dSRodney W. Grimes {
67b8da2396SPoul-Henning Kamp 	int name[2];
686f1e8c18SMatthew Dillon 	int error;
69069e9bc1SDoug Rabson 	size_t len = uap->len;
70df8bae1dSRodney W. Grimes 
71b8da2396SPoul-Henning Kamp 	name[0] = CTL_KERN;
72b8da2396SPoul-Henning Kamp 	name[1] = KERN_HOSTNAME;
736f1e8c18SMatthew Dillon 	mtx_lock(&Giant);
74b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->hostname, &len, 1, 0, 0, 0);
756f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
766f1e8c18SMatthew Dillon 	return(error);
77df8bae1dSRodney W. Grimes }
78df8bae1dSRodney W. Grimes 
79d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
80df8bae1dSRodney W. Grimes struct sethostname_args {
81df8bae1dSRodney W. Grimes 	char	*hostname;
82df8bae1dSRodney W. Grimes 	u_int	len;
83df8bae1dSRodney W. Grimes };
84d2d3e875SBruce Evans #endif
856f1e8c18SMatthew Dillon /*
866f1e8c18SMatthew Dillon  * MPSAFE
876f1e8c18SMatthew Dillon  */
88df8bae1dSRodney W. Grimes /* ARGSUSED */
8926f9a767SRodney W. Grimes int
90b40ce416SJulian Elischer osethostname(td, uap)
91b40ce416SJulian Elischer 	struct thread *td;
92df8bae1dSRodney W. Grimes 	register struct sethostname_args *uap;
93df8bae1dSRodney W. Grimes {
94b8da2396SPoul-Henning Kamp 	int name[2];
95df8bae1dSRodney W. Grimes 	int error;
96df8bae1dSRodney W. Grimes 
97b8da2396SPoul-Henning Kamp 	name[0] = CTL_KERN;
98b8da2396SPoul-Henning Kamp 	name[1] = KERN_HOSTNAME;
996f1e8c18SMatthew Dillon 	mtx_lock(&Giant);
100b40ce416SJulian Elischer 	if ((error = suser_xxx(0, td->td_proc, PRISON_ROOT)) == 0) {
101b40ce416SJulian Elischer 		error = userland_sysctl(td, name, 2, 0, 0, 0,
1026f1e8c18SMatthew Dillon 		    uap->hostname, uap->len, 0);
1036f1e8c18SMatthew Dillon 	}
1046f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
105df8bae1dSRodney W. Grimes 	return (error);
106df8bae1dSRodney W. Grimes }
107df8bae1dSRodney W. Grimes 
108d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1092635f86cSBruce Evans struct ogethostid_args {
110df8bae1dSRodney W. Grimes 	int	dummy;
111df8bae1dSRodney W. Grimes };
112d2d3e875SBruce Evans #endif
1136f1e8c18SMatthew Dillon /*
1146f1e8c18SMatthew Dillon  * MPSAFE
1156f1e8c18SMatthew Dillon  */
116df8bae1dSRodney W. Grimes /* ARGSUSED */
11726f9a767SRodney W. Grimes int
118b40ce416SJulian Elischer ogethostid(td, uap)
119b40ce416SJulian Elischer 	struct thread *td;
1202635f86cSBruce Evans 	struct ogethostid_args *uap;
121df8bae1dSRodney W. Grimes {
122df8bae1dSRodney W. Grimes 
123b40ce416SJulian Elischer 	*(long *)(td->td_retval) = hostid;
124df8bae1dSRodney W. Grimes 	return (0);
125df8bae1dSRodney W. Grimes }
126df8bae1dSRodney W. Grimes #endif /* COMPAT_43 || COMPAT_SUNOS */
127df8bae1dSRodney W. Grimes 
128df8bae1dSRodney W. Grimes #ifdef COMPAT_43
129d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1302635f86cSBruce Evans struct osethostid_args {
131df8bae1dSRodney W. Grimes 	long	hostid;
132df8bae1dSRodney W. Grimes };
133d2d3e875SBruce Evans #endif
1346f1e8c18SMatthew Dillon /*
1356f1e8c18SMatthew Dillon  * MPSAFE
1366f1e8c18SMatthew Dillon  */
137df8bae1dSRodney W. Grimes /* ARGSUSED */
13826f9a767SRodney W. Grimes int
139b40ce416SJulian Elischer osethostid(td, uap)
140b40ce416SJulian Elischer 	struct thread *td;
1412635f86cSBruce Evans 	struct osethostid_args *uap;
142df8bae1dSRodney W. Grimes {
143df8bae1dSRodney W. Grimes 	int error;
144df8bae1dSRodney W. Grimes 
1456f1e8c18SMatthew Dillon 	mtx_lock(&Giant);
146b40ce416SJulian Elischer 	if ((error = suser_td(td)))
147df8bae1dSRodney W. Grimes 		hostid = uap->hostid;
1486f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
1496f1e8c18SMatthew Dillon 	return (error);
150df8bae1dSRodney W. Grimes }
151df8bae1dSRodney W. Grimes 
1526f1e8c18SMatthew Dillon /*
1536f1e8c18SMatthew Dillon  * MPSAFE
1546f1e8c18SMatthew Dillon  */
15526f9a767SRodney W. Grimes int
156b40ce416SJulian Elischer oquota(td, uap)
157b40ce416SJulian Elischer 	struct thread *td;
1582635f86cSBruce Evans 	struct oquota_args *uap;
159df8bae1dSRodney W. Grimes {
160df8bae1dSRodney W. Grimes 	return (ENOSYS);
161df8bae1dSRodney W. Grimes }
162df8bae1dSRodney W. Grimes #endif /* COMPAT_43 */
16326f9a767SRodney W. Grimes 
16432e47970SPeter Wemm /*
16532e47970SPeter Wemm  * This is the FreeBSD-1.1 compatable uname(2) interface.  These
16632e47970SPeter Wemm  * days it is done in libc as a wrapper around a bunch of sysctl's.
16732e47970SPeter Wemm  * This must maintain the old 1.1 binary ABI.
16832e47970SPeter Wemm  */
16932e47970SPeter Wemm #if SYS_NMLN != 32
17032e47970SPeter Wemm #error "FreeBSD-1.1 uname syscall has been broken"
17132e47970SPeter Wemm #endif
172d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
17326f9a767SRodney W. Grimes struct uname_args {
17426f9a767SRodney W. Grimes         struct utsname  *name;
17526f9a767SRodney W. Grimes };
176d2d3e875SBruce Evans #endif
17726f9a767SRodney W. Grimes 
1786f1e8c18SMatthew Dillon /*
1796f1e8c18SMatthew Dillon  * MPSAFE
1806f1e8c18SMatthew Dillon  */
18126f9a767SRodney W. Grimes /* ARGSUSED */
18226f9a767SRodney W. Grimes int
183b40ce416SJulian Elischer uname(td, uap)
184b40ce416SJulian Elischer 	struct thread *td;
18526f9a767SRodney W. Grimes 	struct uname_args *uap;
18626f9a767SRodney W. Grimes {
1876f1e8c18SMatthew Dillon 	int name[2], error;
188069e9bc1SDoug Rabson 	size_t len;
18926f9a767SRodney W. Grimes 	char *s, *us;
19026f9a767SRodney W. Grimes 
191b8da2396SPoul-Henning Kamp 	name[0] = CTL_KERN;
192b8da2396SPoul-Henning Kamp 	name[1] = KERN_OSTYPE;
1936f1e8c18SMatthew Dillon 	len = sizeof (uap->name->sysname);
1946f1e8c18SMatthew Dillon 	mtx_lock(&Giant);
195b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->name->sysname, &len,
196b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
1976f1e8c18SMatthew Dillon 	if (error)
1986f1e8c18SMatthew Dillon 		goto done2;
19926f9a767SRodney W. Grimes 	subyte( uap->name->sysname + sizeof(uap->name->sysname) - 1, 0);
20026f9a767SRodney W. Grimes 
201b8da2396SPoul-Henning Kamp 	name[1] = KERN_HOSTNAME;
20226f9a767SRodney W. Grimes 	len = sizeof uap->name->nodename;
203b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->name->nodename, &len,
204b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
2056f1e8c18SMatthew Dillon 	if (error)
2066f1e8c18SMatthew Dillon 		goto done2;
20726f9a767SRodney W. Grimes 	subyte( uap->name->nodename + sizeof(uap->name->nodename) - 1, 0);
20826f9a767SRodney W. Grimes 
209b8da2396SPoul-Henning Kamp 	name[1] = KERN_OSRELEASE;
21026f9a767SRodney W. Grimes 	len = sizeof uap->name->release;
211b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->name->release, &len,
212b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
2136f1e8c18SMatthew Dillon 	if (error)
2146f1e8c18SMatthew Dillon 		goto done2;
21526f9a767SRodney W. Grimes 	subyte( uap->name->release + sizeof(uap->name->release) - 1, 0);
21626f9a767SRodney W. Grimes 
21726f9a767SRodney W. Grimes /*
21826f9a767SRodney W. Grimes 	name = KERN_VERSION;
21926f9a767SRodney W. Grimes 	len = sizeof uap->name->version;
220b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->name->version, &len,
221b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
2226f1e8c18SMatthew Dillon 	if (error)
2236f1e8c18SMatthew Dillon 		goto done2;
22426f9a767SRodney W. Grimes 	subyte( uap->name->version + sizeof(uap->name->version) - 1, 0);
22526f9a767SRodney W. Grimes */
22626f9a767SRodney W. Grimes 
22726f9a767SRodney W. Grimes /*
22826f9a767SRodney W. Grimes  * this stupid hackery to make the version field look like FreeBSD 1.1
22926f9a767SRodney W. Grimes  */
23026f9a767SRodney W. Grimes 	for(s = version; *s && *s != '#'; s++);
23126f9a767SRodney W. Grimes 
23226f9a767SRodney W. Grimes 	for(us = uap->name->version; *s && *s != ':'; s++) {
2336f1e8c18SMatthew Dillon 		error = subyte( us++, *s);
2346f1e8c18SMatthew Dillon 		if (error)
2356f1e8c18SMatthew Dillon 			goto done2;
23626f9a767SRodney W. Grimes 	}
2376f1e8c18SMatthew Dillon 	error = subyte( us++, 0);
2386f1e8c18SMatthew Dillon 	if (error)
2396f1e8c18SMatthew Dillon 		goto done2;
24026f9a767SRodney W. Grimes 
241069e9bc1SDoug Rabson 	name[0] = CTL_HW;
242b8da2396SPoul-Henning Kamp 	name[1] = HW_MACHINE;
24326f9a767SRodney W. Grimes 	len = sizeof uap->name->machine;
244b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->name->machine, &len,
245b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
2466f1e8c18SMatthew Dillon 	if (error)
2476f1e8c18SMatthew Dillon 		goto done2;
24826f9a767SRodney W. Grimes 	subyte( uap->name->machine + sizeof(uap->name->machine) - 1, 0);
2496f1e8c18SMatthew Dillon done2:
2506f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
2516f1e8c18SMatthew Dillon 	return (error);
25226f9a767SRodney W. Grimes }
25326f9a767SRodney W. Grimes 
254d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
25526f9a767SRodney W. Grimes struct getdomainname_args {
25626f9a767SRodney W. Grimes         char    *domainname;
257f35a53bfSBruce Evans         int     len;
25826f9a767SRodney W. Grimes };
259d2d3e875SBruce Evans #endif
260f35a53bfSBruce Evans 
2616f1e8c18SMatthew Dillon /*
2626f1e8c18SMatthew Dillon  * MPSAFE
2636f1e8c18SMatthew Dillon  */
26426f9a767SRodney W. Grimes /* ARGSUSED */
26526f9a767SRodney W. Grimes int
266b40ce416SJulian Elischer getdomainname(td, uap)
267b40ce416SJulian Elischer         struct thread *td;
26826f9a767SRodney W. Grimes         struct getdomainname_args *uap;
26926f9a767SRodney W. Grimes {
2706f1e8c18SMatthew Dillon 	int domainnamelen;
2716f1e8c18SMatthew Dillon 	int error;
2726f1e8c18SMatthew Dillon 
2736f1e8c18SMatthew Dillon 	mtx_lock(&Giant);
2746f1e8c18SMatthew Dillon 	domainnamelen = strlen(domainname) + 1;
275f35a53bfSBruce Evans 	if ((u_int)uap->len > domainnamelen + 1)
27626f9a767SRodney W. Grimes 		uap->len = domainnamelen + 1;
2776f1e8c18SMatthew Dillon 	error = copyout((caddr_t)domainname, (caddr_t)uap->domainname, uap->len);
2786f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
2796f1e8c18SMatthew Dillon 	return (error);
28026f9a767SRodney W. Grimes }
28126f9a767SRodney W. Grimes 
282d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
28326f9a767SRodney W. Grimes struct setdomainname_args {
28426f9a767SRodney W. Grimes         char    *domainname;
285f35a53bfSBruce Evans         int     len;
28626f9a767SRodney W. Grimes };
287d2d3e875SBruce Evans #endif
28826f9a767SRodney W. Grimes 
2896f1e8c18SMatthew Dillon /*
2906f1e8c18SMatthew Dillon  * MPSAFE
2916f1e8c18SMatthew Dillon  */
29226f9a767SRodney W. Grimes /* ARGSUSED */
29326f9a767SRodney W. Grimes int
294b40ce416SJulian Elischer setdomainname(td, uap)
295b40ce416SJulian Elischer         struct thread *td;
29626f9a767SRodney W. Grimes         struct setdomainname_args *uap;
29726f9a767SRodney W. Grimes {
29827aef046SPoul-Henning Kamp         int error, domainnamelen;
29926f9a767SRodney W. Grimes 
3006f1e8c18SMatthew Dillon 	mtx_lock(&Giant);
301b40ce416SJulian Elischer         if ((error = suser_td(td)))
3026f1e8c18SMatthew Dillon 		goto done2;
3036f1e8c18SMatthew Dillon         if ((u_int)uap->len > sizeof (domainname) - 1) {
3046f1e8c18SMatthew Dillon 		error = EINVAL;
3056f1e8c18SMatthew Dillon 		goto done2;
3066f1e8c18SMatthew Dillon 	}
30726f9a767SRodney W. Grimes         domainnamelen = uap->len;
30826f9a767SRodney W. Grimes         error = copyin((caddr_t)uap->domainname, domainname, uap->len);
30926f9a767SRodney W. Grimes         domainname[domainnamelen] = 0;
3106f1e8c18SMatthew Dillon done2:
3116f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
31226f9a767SRodney W. Grimes         return (error);
31326f9a767SRodney W. Grimes }
31426f9a767SRodney W. Grimes 
315