linux_sysctl.c revision 103839
1/*-
2 * Copyright (c) 2001 Marcel Moolenaar
3 * 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 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/compat/linux/linux_sysctl.c 103839 2002-09-23 06:17:54Z mini $
29 */
30
31#include <sys/param.h>
32#include <sys/lock.h>
33#include <sys/malloc.h>
34#include <sys/mutex.h>
35#include <sys/proc.h>
36#include <sys/sysctl.h>
37#include <sys/systm.h>
38
39#include <machine/../linux/linux.h>
40#include <machine/../linux/linux_proto.h>
41
42#define	LINUX_CTL_KERN		1
43#define	LINUX_CTL_VM		2
44#define	LINUX_CTL_NET		3
45#define	LINUX_CTL_PROC		4
46#define	LINUX_CTL_FS		5
47#define	LINUX_CTL_DEBUG		6
48#define	LINUX_CTL_DEV		7
49#define	LINUX_CTL_BUS		8
50
51/* CTL_KERN names */
52#define	LINUX_KERN_OSTYPE	1
53#define	LINUX_KERN_OSRELEASE	2
54#define	LINUX_KERN_OSREV	3
55#define	LINUX_KERN_VERSION	4
56
57static int
58handle_string(struct l___sysctl_args *la, char *value)
59{
60	int error;
61
62	if (la->oldval != NULL) {
63		l_int len = strlen(value);
64		error = copyout(value, la->oldval, len + 1);
65		if (!error && la->oldlenp != NULL)
66			error = copyout(&len, la->oldlenp, sizeof(len));
67		if (error)
68			return (error);
69	}
70
71	if (la->newval != NULL)
72		return (ENOTDIR);
73
74	return (0);
75}
76
77int
78linux_sysctl(struct thread *td, struct linux_sysctl_args *args)
79{
80	struct l___sysctl_args la;
81	l_int *mib;
82	int error, i;
83
84	error = copyin((caddr_t)args->args, &la, sizeof(la));
85	if (error)
86		return (error);
87
88	if (la.nlen <= 0 || la.nlen > LINUX_CTL_MAXNAME)
89		return (ENOTDIR);
90
91	mib = malloc(la.nlen * sizeof(l_int), M_TEMP, M_WAITOK);
92	error = copyin(la.name, mib, la.nlen * sizeof(l_int));
93	if (error) {
94		free(mib, M_TEMP);
95		return (error);
96	}
97
98	switch (mib[0]) {
99	case LINUX_CTL_KERN:
100		if (la.nlen < 2)
101			break;
102
103		switch (mib[1]) {
104		case LINUX_KERN_VERSION:
105			error = handle_string(&la, version);
106			free(mib, M_TEMP);
107			return (error);
108		default:
109			break;
110		}
111		break;
112	default:
113		break;
114	}
115
116	printf("linux: sysctl: unhandled name=");
117	for (i = 0; i < la.nlen; i++)
118		printf("%c%d", (i) ? ',' : '{', mib[i]);
119	printf("}\n");
120
121	free(mib, M_TEMP);
122	return (ENOTDIR);
123}
124
125int
126linux_sethostname(struct thread *td, struct linux_sethostname_args *uap)
127{
128	int name[2];
129	int error;
130
131	name[0] = CTL_KERN;
132	name[1] = KERN_HOSTNAME;
133	mtx_lock(&Giant);
134	if ((error = suser_cred(td->td_ucred, PRISON_ROOT)) == 0) {
135		error = userland_sysctl(td, name, 2, 0, 0, 0,
136		    uap->hostname, uap->len, 0);
137	}
138	mtx_unlock(&Giant);
139	return (error);
140}
141