xref: /freebsd/sys/kern/kern_xxx.c (revision 56f21b9d74a516a6c2f67d09e1b6c588bfa54c6a)
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  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)kern_xxx.c	8.2 (Berkeley) 11/14/93
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32677b542eSDavid E. O'Brien #include <sys/cdefs.h>
33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
34677b542eSDavid E. O'Brien 
355591b823SEivind Eklund #include "opt_compat.h"
365591b823SEivind Eklund 
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>
426f1e8c18SMatthew Dillon #include <sys/lock.h>
436f1e8c18SMatthew Dillon #include <sys/mutex.h>
44df8bae1dSRodney W. Grimes #include <sys/sysctl.h>
4526f9a767SRodney W. Grimes #include <sys/utsname.h>
4626f9a767SRodney W. Grimes 
47df8bae1dSRodney W. Grimes 
481930e303SPoul-Henning Kamp #if defined(COMPAT_43)
49df8bae1dSRodney W. Grimes 
50d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
51df8bae1dSRodney W. Grimes struct gethostname_args {
52df8bae1dSRodney W. Grimes 	char	*hostname;
53df8bae1dSRodney W. Grimes 	u_int	len;
54df8bae1dSRodney W. Grimes };
55d2d3e875SBruce Evans #endif
566f1e8c18SMatthew Dillon /*
576f1e8c18SMatthew Dillon  * MPSAFE
586f1e8c18SMatthew Dillon  */
59df8bae1dSRodney W. Grimes /* ARGSUSED */
6026f9a767SRodney W. Grimes int
61b40ce416SJulian Elischer ogethostname(td, uap)
62b40ce416SJulian Elischer 	struct thread *td;
63df8bae1dSRodney W. Grimes 	struct gethostname_args *uap;
64df8bae1dSRodney W. Grimes {
65b8da2396SPoul-Henning Kamp 	int name[2];
666f1e8c18SMatthew Dillon 	int error;
67069e9bc1SDoug Rabson 	size_t len = uap->len;
68df8bae1dSRodney W. Grimes 
69b8da2396SPoul-Henning Kamp 	name[0] = CTL_KERN;
70b8da2396SPoul-Henning Kamp 	name[1] = KERN_HOSTNAME;
716f1e8c18SMatthew Dillon 	mtx_lock(&Giant);
72b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->hostname, &len, 1, 0, 0, 0);
736f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
746f1e8c18SMatthew Dillon 	return(error);
75df8bae1dSRodney W. Grimes }
76df8bae1dSRodney W. Grimes 
77d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
78df8bae1dSRodney W. Grimes struct sethostname_args {
79df8bae1dSRodney W. Grimes 	char	*hostname;
80df8bae1dSRodney W. Grimes 	u_int	len;
81df8bae1dSRodney W. Grimes };
82d2d3e875SBruce Evans #endif
836f1e8c18SMatthew Dillon /*
846f1e8c18SMatthew Dillon  * MPSAFE
856f1e8c18SMatthew Dillon  */
86df8bae1dSRodney W. Grimes /* ARGSUSED */
8726f9a767SRodney W. Grimes int
88b40ce416SJulian Elischer osethostname(td, uap)
89b40ce416SJulian Elischer 	struct thread *td;
90df8bae1dSRodney W. Grimes 	register struct sethostname_args *uap;
91df8bae1dSRodney W. Grimes {
92b8da2396SPoul-Henning Kamp 	int name[2];
93df8bae1dSRodney W. Grimes 	int error;
94df8bae1dSRodney W. Grimes 
95b8da2396SPoul-Henning Kamp 	name[0] = CTL_KERN;
96b8da2396SPoul-Henning Kamp 	name[1] = KERN_HOSTNAME;
976f1e8c18SMatthew Dillon 	mtx_lock(&Giant);
9856f21b9dSColin Percival 	if ((error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL)) == 0) {
99b40ce416SJulian Elischer 		error = userland_sysctl(td, name, 2, 0, 0, 0,
1006f1e8c18SMatthew Dillon 		    uap->hostname, uap->len, 0);
1016f1e8c18SMatthew Dillon 	}
1026f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
103df8bae1dSRodney W. Grimes 	return (error);
104df8bae1dSRodney W. Grimes }
105df8bae1dSRodney W. Grimes 
106d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1072635f86cSBruce Evans struct ogethostid_args {
108df8bae1dSRodney W. Grimes 	int	dummy;
109df8bae1dSRodney W. Grimes };
110d2d3e875SBruce Evans #endif
1116f1e8c18SMatthew Dillon /*
1126f1e8c18SMatthew Dillon  * MPSAFE
1136f1e8c18SMatthew Dillon  */
114df8bae1dSRodney W. Grimes /* ARGSUSED */
11526f9a767SRodney W. Grimes int
116b40ce416SJulian Elischer ogethostid(td, uap)
117b40ce416SJulian Elischer 	struct thread *td;
1182635f86cSBruce Evans 	struct ogethostid_args *uap;
119df8bae1dSRodney W. Grimes {
120df8bae1dSRodney W. Grimes 
121b40ce416SJulian Elischer 	*(long *)(td->td_retval) = hostid;
122df8bae1dSRodney W. Grimes 	return (0);
123df8bae1dSRodney W. Grimes }
1241930e303SPoul-Henning Kamp #endif /* COMPAT_43 */
125df8bae1dSRodney W. Grimes 
126df8bae1dSRodney W. Grimes #ifdef COMPAT_43
127d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1282635f86cSBruce Evans struct osethostid_args {
129df8bae1dSRodney W. Grimes 	long	hostid;
130df8bae1dSRodney W. Grimes };
131d2d3e875SBruce Evans #endif
1326f1e8c18SMatthew Dillon /*
1336f1e8c18SMatthew Dillon  * MPSAFE
1346f1e8c18SMatthew Dillon  */
135df8bae1dSRodney W. Grimes /* ARGSUSED */
13626f9a767SRodney W. Grimes int
137b40ce416SJulian Elischer osethostid(td, uap)
138b40ce416SJulian Elischer 	struct thread *td;
1392635f86cSBruce Evans 	struct osethostid_args *uap;
140df8bae1dSRodney W. Grimes {
141df8bae1dSRodney W. Grimes 	int error;
142df8bae1dSRodney W. Grimes 
14344731cabSJohn Baldwin 	if ((error = suser(td)))
144c89d555cSTim J. Robbins 		return (error);
145c89d555cSTim J. Robbins 	mtx_lock(&Giant);
146df8bae1dSRodney W. Grimes 	hostid = uap->hostid;
1476f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
148c89d555cSTim J. Robbins 	return (0);
149df8bae1dSRodney W. Grimes }
150df8bae1dSRodney W. Grimes 
1516f1e8c18SMatthew Dillon /*
1526f1e8c18SMatthew Dillon  * MPSAFE
1536f1e8c18SMatthew Dillon  */
15426f9a767SRodney W. Grimes int
155b40ce416SJulian Elischer oquota(td, uap)
156b40ce416SJulian Elischer 	struct thread *td;
1572635f86cSBruce Evans 	struct oquota_args *uap;
158df8bae1dSRodney W. Grimes {
159df8bae1dSRodney W. Grimes 	return (ENOSYS);
160df8bae1dSRodney W. Grimes }
161df8bae1dSRodney W. Grimes #endif /* COMPAT_43 */
16226f9a767SRodney W. Grimes 
16332e47970SPeter Wemm /*
16432e47970SPeter Wemm  * This is the FreeBSD-1.1 compatable uname(2) interface.  These
16532e47970SPeter Wemm  * days it is done in libc as a wrapper around a bunch of sysctl's.
16632e47970SPeter Wemm  * This must maintain the old 1.1 binary ABI.
16732e47970SPeter Wemm  */
16832e47970SPeter Wemm #if SYS_NMLN != 32
16932e47970SPeter Wemm #error "FreeBSD-1.1 uname syscall has been broken"
17032e47970SPeter Wemm #endif
171d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
17226f9a767SRodney W. Grimes struct uname_args {
17326f9a767SRodney W. Grimes         struct utsname  *name;
17426f9a767SRodney W. Grimes };
175d2d3e875SBruce Evans #endif
17626f9a767SRodney W. Grimes 
1776f1e8c18SMatthew Dillon /*
1786f1e8c18SMatthew Dillon  * MPSAFE
1796f1e8c18SMatthew Dillon  */
18026f9a767SRodney W. Grimes /* ARGSUSED */
18126f9a767SRodney W. Grimes int
182b40ce416SJulian Elischer uname(td, uap)
183b40ce416SJulian Elischer 	struct thread *td;
18426f9a767SRodney W. Grimes 	struct uname_args *uap;
18526f9a767SRodney W. Grimes {
1866f1e8c18SMatthew Dillon 	int name[2], error;
187069e9bc1SDoug Rabson 	size_t len;
18826f9a767SRodney W. Grimes 	char *s, *us;
18926f9a767SRodney W. Grimes 
190b8da2396SPoul-Henning Kamp 	name[0] = CTL_KERN;
191b8da2396SPoul-Henning Kamp 	name[1] = KERN_OSTYPE;
1926f1e8c18SMatthew Dillon 	len = sizeof (uap->name->sysname);
1936f1e8c18SMatthew Dillon 	mtx_lock(&Giant);
194b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->name->sysname, &len,
195b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
1966f1e8c18SMatthew Dillon 	if (error)
1976f1e8c18SMatthew Dillon 		goto done2;
19826f9a767SRodney W. Grimes 	subyte( uap->name->sysname + sizeof(uap->name->sysname) - 1, 0);
19926f9a767SRodney W. Grimes 
200b8da2396SPoul-Henning Kamp 	name[1] = KERN_HOSTNAME;
20126f9a767SRodney W. Grimes 	len = sizeof uap->name->nodename;
202b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->name->nodename, &len,
203b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
2046f1e8c18SMatthew Dillon 	if (error)
2056f1e8c18SMatthew Dillon 		goto done2;
20626f9a767SRodney W. Grimes 	subyte( uap->name->nodename + sizeof(uap->name->nodename) - 1, 0);
20726f9a767SRodney W. Grimes 
208b8da2396SPoul-Henning Kamp 	name[1] = KERN_OSRELEASE;
20926f9a767SRodney W. Grimes 	len = sizeof uap->name->release;
210b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->name->release, &len,
211b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
2126f1e8c18SMatthew Dillon 	if (error)
2136f1e8c18SMatthew Dillon 		goto done2;
21426f9a767SRodney W. Grimes 	subyte( uap->name->release + sizeof(uap->name->release) - 1, 0);
21526f9a767SRodney W. Grimes 
21626f9a767SRodney W. Grimes /*
21726f9a767SRodney W. Grimes 	name = KERN_VERSION;
21826f9a767SRodney W. Grimes 	len = sizeof uap->name->version;
219b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->name->version, &len,
220b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
2216f1e8c18SMatthew Dillon 	if (error)
2226f1e8c18SMatthew Dillon 		goto done2;
22326f9a767SRodney W. Grimes 	subyte( uap->name->version + sizeof(uap->name->version) - 1, 0);
22426f9a767SRodney W. Grimes */
22526f9a767SRodney W. Grimes 
22626f9a767SRodney W. Grimes /*
22726f9a767SRodney W. Grimes  * this stupid hackery to make the version field look like FreeBSD 1.1
22826f9a767SRodney W. Grimes  */
22926f9a767SRodney W. Grimes 	for(s = version; *s && *s != '#'; s++);
23026f9a767SRodney W. Grimes 
23126f9a767SRodney W. Grimes 	for(us = uap->name->version; *s && *s != ':'; s++) {
2326f1e8c18SMatthew Dillon 		error = subyte( us++, *s);
2336f1e8c18SMatthew Dillon 		if (error)
2346f1e8c18SMatthew Dillon 			goto done2;
23526f9a767SRodney W. Grimes 	}
2366f1e8c18SMatthew Dillon 	error = subyte( us++, 0);
2376f1e8c18SMatthew Dillon 	if (error)
2386f1e8c18SMatthew Dillon 		goto done2;
23926f9a767SRodney W. Grimes 
240069e9bc1SDoug Rabson 	name[0] = CTL_HW;
241b8da2396SPoul-Henning Kamp 	name[1] = HW_MACHINE;
24226f9a767SRodney W. Grimes 	len = sizeof uap->name->machine;
243b40ce416SJulian Elischer 	error = userland_sysctl(td, name, 2, uap->name->machine, &len,
244b8da2396SPoul-Henning Kamp 		1, 0, 0, 0);
2456f1e8c18SMatthew Dillon 	if (error)
2466f1e8c18SMatthew Dillon 		goto done2;
24726f9a767SRodney W. Grimes 	subyte( uap->name->machine + sizeof(uap->name->machine) - 1, 0);
2486f1e8c18SMatthew Dillon done2:
2496f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
2506f1e8c18SMatthew Dillon 	return (error);
25126f9a767SRodney W. Grimes }
25226f9a767SRodney W. Grimes 
253d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
25426f9a767SRodney W. Grimes struct getdomainname_args {
25526f9a767SRodney W. Grimes         char    *domainname;
256f35a53bfSBruce Evans         int     len;
25726f9a767SRodney W. Grimes };
258d2d3e875SBruce Evans #endif
259f35a53bfSBruce Evans 
2606f1e8c18SMatthew Dillon /*
2616f1e8c18SMatthew Dillon  * MPSAFE
2626f1e8c18SMatthew Dillon  */
26326f9a767SRodney W. Grimes /* ARGSUSED */
26426f9a767SRodney W. Grimes int
265b40ce416SJulian Elischer getdomainname(td, uap)
266b40ce416SJulian Elischer         struct thread *td;
26726f9a767SRodney W. Grimes         struct getdomainname_args *uap;
26826f9a767SRodney W. Grimes {
2696f1e8c18SMatthew Dillon 	int domainnamelen;
2706f1e8c18SMatthew Dillon 	int error;
2716f1e8c18SMatthew Dillon 
2726f1e8c18SMatthew Dillon 	mtx_lock(&Giant);
2736f1e8c18SMatthew Dillon 	domainnamelen = strlen(domainname) + 1;
274b5e80ae3SJacques Vidrine 	if ((u_int)uap->len > domainnamelen)
275b5e80ae3SJacques Vidrine 		uap->len = domainnamelen;
27601609114SAlfred Perlstein 	error = copyout(domainname, uap->domainname, uap->len);
2776f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
2786f1e8c18SMatthew Dillon 	return (error);
27926f9a767SRodney W. Grimes }
28026f9a767SRodney W. Grimes 
281d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
28226f9a767SRodney W. Grimes struct setdomainname_args {
28326f9a767SRodney W. Grimes         char    *domainname;
284f35a53bfSBruce Evans         int     len;
28526f9a767SRodney W. Grimes };
286d2d3e875SBruce Evans #endif
28726f9a767SRodney W. Grimes 
2886f1e8c18SMatthew Dillon /*
2896f1e8c18SMatthew Dillon  * MPSAFE
2906f1e8c18SMatthew Dillon  */
29126f9a767SRodney W. Grimes /* ARGSUSED */
29226f9a767SRodney W. Grimes int
293b40ce416SJulian Elischer setdomainname(td, uap)
294b40ce416SJulian Elischer         struct thread *td;
29526f9a767SRodney W. Grimes         struct setdomainname_args *uap;
29626f9a767SRodney W. Grimes {
29727aef046SPoul-Henning Kamp         int error, domainnamelen;
29826f9a767SRodney W. Grimes 
2996f1e8c18SMatthew Dillon 	mtx_lock(&Giant);
30044731cabSJohn Baldwin         if ((error = suser(td)))
3016f1e8c18SMatthew Dillon 		goto done2;
3026f1e8c18SMatthew Dillon         if ((u_int)uap->len > sizeof (domainname) - 1) {
3036f1e8c18SMatthew Dillon 		error = EINVAL;
3046f1e8c18SMatthew Dillon 		goto done2;
3056f1e8c18SMatthew Dillon 	}
30626f9a767SRodney W. Grimes         domainnamelen = uap->len;
30701609114SAlfred Perlstein         error = copyin(uap->domainname, domainname, uap->len);
30826f9a767SRodney W. Grimes         domainname[domainnamelen] = 0;
3096f1e8c18SMatthew Dillon done2:
3106f1e8c18SMatthew Dillon 	mtx_unlock(&Giant);
31126f9a767SRodney W. Grimes         return (error);
31226f9a767SRodney W. Grimes }
31326f9a767SRodney W. Grimes 
314