xref: /freebsd/sys/kern/kern_xxx.c (revision 7bd6fde3951af84ef3b68e4d1eadc1840c2fc1b3)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)kern_xxx.c	8.2 (Berkeley) 11/14/93
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_compat.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/kernel.h>
41 #include <sys/priv.h>
42 #include <sys/proc.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/sysctl.h>
46 #include <sys/utsname.h>
47 
48 
49 #if defined(COMPAT_43)
50 
51 #ifndef _SYS_SYSPROTO_H_
52 struct gethostname_args {
53 	char	*hostname;
54 	u_int	len;
55 };
56 #endif
57 /*
58  * MPSAFE
59  */
60 /* ARGSUSED */
61 int
62 ogethostname(td, uap)
63 	struct thread *td;
64 	struct gethostname_args *uap;
65 {
66 	int name[2];
67 	int error;
68 	size_t len = uap->len;
69 
70 	name[0] = CTL_KERN;
71 	name[1] = KERN_HOSTNAME;
72 	mtx_lock(&Giant);
73 	error = userland_sysctl(td, name, 2, uap->hostname, &len,
74 	    1, 0, 0, 0, 0);
75 	mtx_unlock(&Giant);
76 	return(error);
77 }
78 
79 #ifndef _SYS_SYSPROTO_H_
80 struct sethostname_args {
81 	char	*hostname;
82 	u_int	len;
83 };
84 #endif
85 /*
86  * MPSAFE
87  */
88 /* ARGSUSED */
89 int
90 osethostname(td, uap)
91 	struct thread *td;
92 	register struct sethostname_args *uap;
93 {
94 	int name[2];
95 	int error;
96 
97 	name[0] = CTL_KERN;
98 	name[1] = KERN_HOSTNAME;
99 	mtx_lock(&Giant);
100 	error = userland_sysctl(td, name, 2, 0, 0, 0, uap->hostname,
101 	    uap->len, 0, 0);
102 	mtx_unlock(&Giant);
103 	return (error);
104 }
105 
106 #ifndef _SYS_SYSPROTO_H_
107 struct ogethostid_args {
108 	int	dummy;
109 };
110 #endif
111 /*
112  * MPSAFE
113  */
114 /* ARGSUSED */
115 int
116 ogethostid(td, uap)
117 	struct thread *td;
118 	struct ogethostid_args *uap;
119 {
120 
121 	*(long *)(td->td_retval) = hostid;
122 	return (0);
123 }
124 #endif /* COMPAT_43 */
125 
126 #ifdef COMPAT_43
127 #ifndef _SYS_SYSPROTO_H_
128 struct osethostid_args {
129 	long	hostid;
130 };
131 #endif
132 /*
133  * MPSAFE
134  */
135 /* ARGSUSED */
136 int
137 osethostid(td, uap)
138 	struct thread *td;
139 	struct osethostid_args *uap;
140 {
141 	int error;
142 
143 	error = priv_check(td, PRIV_SETHOSTID);
144 	if (error)
145 		return (error);
146 	mtx_lock(&Giant);
147 	hostid = uap->hostid;
148 	mtx_unlock(&Giant);
149 	return (0);
150 }
151 
152 /*
153  * MPSAFE
154  */
155 int
156 oquota(td, uap)
157 	struct thread *td;
158 	struct oquota_args *uap;
159 {
160 	return (ENOSYS);
161 }
162 #endif /* COMPAT_43 */
163 
164 /*
165  * This is the FreeBSD-1.1 compatable uname(2) interface.  These
166  * days it is done in libc as a wrapper around a bunch of sysctl's.
167  * This must maintain the old 1.1 binary ABI.
168  */
169 #if SYS_NMLN != 32
170 #error "FreeBSD-1.1 uname syscall has been broken"
171 #endif
172 #ifndef _SYS_SYSPROTO_H_
173 struct uname_args {
174         struct utsname  *name;
175 };
176 #endif
177 
178 /*
179  * MPSAFE
180  */
181 /* ARGSUSED */
182 int
183 uname(td, uap)
184 	struct thread *td;
185 	struct uname_args *uap;
186 {
187 	int name[2], error;
188 	size_t len;
189 	char *s, *us;
190 
191 	name[0] = CTL_KERN;
192 	name[1] = KERN_OSTYPE;
193 	len = sizeof (uap->name->sysname);
194 	mtx_lock(&Giant);
195 	error = userland_sysctl(td, name, 2, uap->name->sysname, &len,
196 		1, 0, 0, 0, 0);
197 	if (error)
198 		goto done2;
199 	subyte( uap->name->sysname + sizeof(uap->name->sysname) - 1, 0);
200 
201 	name[1] = KERN_HOSTNAME;
202 	len = sizeof uap->name->nodename;
203 	error = userland_sysctl(td, name, 2, uap->name->nodename, &len,
204 		1, 0, 0, 0, 0);
205 	if (error)
206 		goto done2;
207 	subyte( uap->name->nodename + sizeof(uap->name->nodename) - 1, 0);
208 
209 	name[1] = KERN_OSRELEASE;
210 	len = sizeof uap->name->release;
211 	error = userland_sysctl(td, name, 2, uap->name->release, &len,
212 		1, 0, 0, 0, 0);
213 	if (error)
214 		goto done2;
215 	subyte( uap->name->release + sizeof(uap->name->release) - 1, 0);
216 
217 /*
218 	name = KERN_VERSION;
219 	len = sizeof uap->name->version;
220 	error = userland_sysctl(td, name, 2, uap->name->version, &len,
221 		1, 0, 0, 0, 0);
222 	if (error)
223 		goto done2;
224 	subyte( uap->name->version + sizeof(uap->name->version) - 1, 0);
225 */
226 
227 /*
228  * this stupid hackery to make the version field look like FreeBSD 1.1
229  */
230 	for(s = version; *s && *s != '#'; s++);
231 
232 	for(us = uap->name->version; *s && *s != ':'; s++) {
233 		error = subyte( us++, *s);
234 		if (error)
235 			goto done2;
236 	}
237 	error = subyte( us++, 0);
238 	if (error)
239 		goto done2;
240 
241 	name[0] = CTL_HW;
242 	name[1] = HW_MACHINE;
243 	len = sizeof uap->name->machine;
244 	error = userland_sysctl(td, name, 2, uap->name->machine, &len,
245 		1, 0, 0, 0, 0);
246 	if (error)
247 		goto done2;
248 	subyte( uap->name->machine + sizeof(uap->name->machine) - 1, 0);
249 done2:
250 	mtx_unlock(&Giant);
251 	return (error);
252 }
253 
254 #ifndef _SYS_SYSPROTO_H_
255 struct getdomainname_args {
256         char    *domainname;
257         int     len;
258 };
259 #endif
260 
261 /*
262  * MPSAFE
263  */
264 /* ARGSUSED */
265 int
266 getdomainname(td, uap)
267         struct thread *td;
268         struct getdomainname_args *uap;
269 {
270 	int domainnamelen;
271 	int error;
272 
273 	mtx_lock(&Giant);
274 	domainnamelen = strlen(domainname) + 1;
275 	if ((u_int)uap->len > domainnamelen)
276 		uap->len = domainnamelen;
277 	error = copyout(domainname, uap->domainname, uap->len);
278 	mtx_unlock(&Giant);
279 	return (error);
280 }
281 
282 #ifndef _SYS_SYSPROTO_H_
283 struct setdomainname_args {
284         char    *domainname;
285         int     len;
286 };
287 #endif
288 
289 /*
290  * MPSAFE
291  */
292 /* ARGSUSED */
293 int
294 setdomainname(td, uap)
295         struct thread *td;
296         struct setdomainname_args *uap;
297 {
298         int error, domainnamelen;
299 
300 	error = priv_check(td, PRIV_SETDOMAINNAME);
301 	if (error)
302 		return (error);
303 	mtx_lock(&Giant);
304         if ((u_int)uap->len > sizeof (domainname) - 1) {
305 		error = EINVAL;
306 		goto done2;
307 	}
308         domainnamelen = uap->len;
309         error = copyin(uap->domainname, domainname, uap->len);
310         domainname[domainnamelen] = 0;
311 done2:
312 	mtx_unlock(&Giant);
313         return (error);
314 }
315