xref: /freebsd/sys/compat/linux/linux_sysctl.c (revision 66422f5b7a1a6055f0b2358268eb902aab6e2e3e)
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$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/malloc.h>
33 #include <sys/systm.h>
34 
35 #include <machine/../linux/linux.h>
36 #include <machine/../linux/linux_proto.h>
37 
38 #define	LINUX_CTL_KERN		1
39 #define	LINUX_CTL_VM		2
40 #define	LINUX_CTL_NET		3
41 #define	LINUX_CTL_PROC		4
42 #define	LINUX_CTL_FS		5
43 #define	LINUX_CTL_DEBUG		6
44 #define	LINUX_CTL_DEV		7
45 #define	LINUX_CTL_BUS		8
46 
47 /* CTL_KERN names */
48 #define	LINUX_KERN_OSTYPE	1
49 #define	LINUX_KERN_OSRELEASE	2
50 #define	LINUX_KERN_OSREV	3
51 #define	LINUX_KERN_VERSION	4
52 
53 static int
54 handle_string(struct l___sysctl_args *la, char *value)
55 {
56 	int error;
57 
58 	if (la->oldval != NULL) {
59 		l_int len = strlen(value);
60 		error = copyout(value, la->oldval, len + 1);
61 		if (!error && la->oldlenp != NULL)
62 			error = copyout(&len, la->oldlenp, sizeof(len));
63 		if (error)
64 			return (error);
65 	}
66 
67 	if (la->newval != NULL)
68 		return (ENOTDIR);
69 
70 	return (0);
71 }
72 
73 int
74 linux_sysctl(struct thread *td, struct linux_sysctl_args *args)
75 {
76 	struct l___sysctl_args la;
77 	l_int *mib;
78 	int error, i;
79 
80 	error = copyin((caddr_t)args->args, &la, sizeof(la));
81 	if (error)
82 		return (error);
83 
84 	if (la.nlen <= 0 || la.nlen > LINUX_CTL_MAXNAME)
85 		return (ENOTDIR);
86 
87 	mib = malloc(la.nlen * sizeof(l_int), M_TEMP, M_WAITOK);
88 	error = copyin(la.name, mib, la.nlen * sizeof(l_int));
89 	if (error) {
90 		free(mib, M_TEMP);
91 		return (error);
92 	}
93 
94 	switch (mib[0]) {
95 	case LINUX_CTL_KERN:
96 		if (la.nlen < 2)
97 			break;
98 
99 		switch (mib[1]) {
100 		case LINUX_KERN_VERSION:
101 			error = handle_string(&la, version);
102 			free(mib, M_TEMP);
103 			return (error);
104 		default:
105 			break;
106 		}
107 		break;
108 	default:
109 		break;
110 	}
111 
112 	printf("linux: sysctl: unhandled name=");
113 	for (i = 0; i < la.nlen; i++)
114 		printf("%c%d", (i) ? ',' : '{', mib[i]);
115 	printf("}\n");
116 
117 	free(mib, M_TEMP);
118 	return (ENOTDIR);
119 }
120