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