xref: /freebsd/sys/compat/linux/linux_sysctl.c (revision acd3428b7d3e94cef0e1881c868cb4b131d4ff41)
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 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_compat.h"
33 
34 #include <sys/param.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/proc.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
41 #include <sys/sbuf.h>
42 
43 #ifdef COMPAT_LINUX32
44 #include <machine/../linux32/linux.h>
45 #include <machine/../linux32/linux32_proto.h>
46 #else
47 #include <machine/../linux/linux.h>
48 #include <machine/../linux/linux_proto.h>
49 #endif
50 
51 #include <compat/linux/linux_util.h>
52 
53 #define	LINUX_CTL_KERN		1
54 #define	LINUX_CTL_VM		2
55 #define	LINUX_CTL_NET		3
56 #define	LINUX_CTL_PROC		4
57 #define	LINUX_CTL_FS		5
58 #define	LINUX_CTL_DEBUG		6
59 #define	LINUX_CTL_DEV		7
60 #define	LINUX_CTL_BUS		8
61 
62 /* CTL_KERN names */
63 #define	LINUX_KERN_OSTYPE	1
64 #define	LINUX_KERN_OSRELEASE	2
65 #define	LINUX_KERN_OSREV	3
66 #define	LINUX_KERN_VERSION	4
67 
68 static int
69 handle_string(struct l___sysctl_args *la, char *value)
70 {
71 	int error;
72 
73 	if (la->oldval != 0) {
74 		l_int len = strlen(value);
75 		error = copyout(value, PTRIN(la->oldval), len + 1);
76 		if (!error && la->oldlenp != 0)
77 			error = copyout(&len, PTRIN(la->oldlenp), sizeof(len));
78 		if (error)
79 			return (error);
80 	}
81 
82 	if (la->newval != 0)
83 		return (ENOTDIR);
84 
85 	return (0);
86 }
87 
88 int
89 linux_sysctl(struct thread *td, struct linux_sysctl_args *args)
90 {
91 	struct l___sysctl_args la;
92 	struct sbuf *sb;
93 	l_int *mib;
94 	int error, i;
95 
96 	error = copyin(args->args, &la, sizeof(la));
97 	if (error)
98 		return (error);
99 
100 	if (la.nlen <= 0 || la.nlen > LINUX_CTL_MAXNAME)
101 		return (ENOTDIR);
102 
103 	mib = malloc(la.nlen * sizeof(l_int), M_TEMP, M_WAITOK);
104 	error = copyin(PTRIN(la.name), mib, la.nlen * sizeof(l_int));
105 	if (error) {
106 		free(mib, M_TEMP);
107 		return (error);
108 	}
109 
110 	switch (mib[0]) {
111 	case LINUX_CTL_KERN:
112 		if (la.nlen < 2)
113 			break;
114 
115 		switch (mib[1]) {
116 		case LINUX_KERN_VERSION:
117 			error = handle_string(&la, version);
118 			free(mib, M_TEMP);
119 			return (error);
120 		default:
121 			break;
122 		}
123 		break;
124 	default:
125 		break;
126 	}
127 
128 	sb = sbuf_new(NULL, NULL, 20 + la.nlen * 5, SBUF_AUTOEXTEND);
129 	if (sb == NULL) {
130 		linux_msg(td, "sysctl is not implemented");
131 	} else {
132 		sbuf_printf(sb, "sysctl ");
133 		for (i = 0; i < la.nlen; i++)
134 			sbuf_printf(sb, "%c%d", (i) ? ',' : '{', mib[i]);
135 		sbuf_printf(sb, "} is not implemented");
136 		sbuf_finish(sb);
137 		linux_msg(td, "%s", sbuf_data(sb));
138 		sbuf_delete(sb);
139 	}
140 
141 	free(mib, M_TEMP);
142 	return (ENOTDIR);
143 }
144