summaryrefslogtreecommitdiffstats
path: root/sys/compat/linux/linux_mib.c
blob: 4f7f199b320b172233e0767954600c62ca9a5faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*-
 * Copyright (c) 1999 Marcel Moolenaar
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer 
 *    in this position and unchanged.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/jail.h>
#include <sys/lock.h>
#include <sys/mutex.h>

#include <machine/../linux/linux.h>
#include <compat/linux/linux_mib.h>

struct linux_prison {
	char	pr_osname[LINUX_MAX_UTSNAME];
	char	pr_osrelease[LINUX_MAX_UTSNAME];
	int	pr_oss_version;
};

SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0,
	    "Linux mode");

static char	linux_osname[LINUX_MAX_UTSNAME] = "Linux";

static int
linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
{
	char osname[LINUX_MAX_UTSNAME];
	int error;

	linux_get_osname(req->td->td_proc, osname);
	error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
	if (error || req->newptr == NULL)
		return (error);
	error = linux_set_osname(req->td->td_proc, osname);
	return (error);
}

SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON,
	    0, 0, linux_sysctl_osname, "A",
	    "Linux kernel OS name");

static char	linux_osrelease[LINUX_MAX_UTSNAME] = "2.4.2";

static int
linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
{
	char osrelease[LINUX_MAX_UTSNAME];
	int error;

	linux_get_osrelease(req->td->td_proc, osrelease);
	error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
	if (error || req->newptr == NULL)
		return (error);
	error = linux_set_osrelease(req->td->td_proc, osrelease);
	return (error);
}

SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
	    CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON,
	    0, 0, linux_sysctl_osrelease, "A",
	    "Linux kernel OS release");

static int	linux_oss_version = 0x030600;

static int
linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
{
	int oss_version;
	int error;

	oss_version = linux_get_oss_version(req->td->td_proc);
	error = sysctl_handle_int(oidp, &oss_version, 0, req);
	if (error || req->newptr == NULL)
		return (error);
	error = linux_set_oss_version(req->td->td_proc, oss_version);
	return (error);
}

SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
	    0, 0, linux_sysctl_oss_version, "I",
	    "Linux OSS version");

/*
 * Returns holding the prison mutex if return non-NULL.
 */
static struct linux_prison *
linux_get_prison(struct proc *p)
{
	register struct prison *pr;
	register struct linux_prison *lpr;

	if (!jailed(p->p_ucred))
		return (NULL);

	pr = p->p_ucred->cr_prison;

	/*
	 * Rather than hold the prison mutex during allocation, check to
	 * see if we need to allocate while holding the mutex, release it,
	 * allocate, then once we've allocated the memory, check again to
	 * see if it's still needed, and set if appropriate.  If it's not,
	 * we release the mutex again to FREE(), and grab it again so as
	 * to release holding the lock.
	 */
	mtx_lock(&pr->pr_mtx);
	if (pr->pr_linux == NULL) {
		mtx_unlock(&pr->pr_mtx);
		MALLOC(lpr, struct linux_prison *, sizeof *lpr,
		    M_PRISON, M_ZERO);
		mtx_lock(&pr->pr_mtx);
		if (pr->pr_linux == NULL) {
			pr->pr_linux = lpr;
		} else {
			mtx_unlock(&pr->pr_mtx);
			FREE(lpr, M_PRISON);
			mtx_lock(&pr->pr_mtx);
		}
	}

	return (pr->pr_linux);
}

void
linux_get_osname(p, dst)
	struct proc *p;
	char *dst;
{
	register struct prison *pr;
	register struct linux_prison *lpr;

	if (p->p_ucred->cr_prison == NULL) {
		bcopy(linux_osname, dst, LINUX_MAX_UTSNAME);
		return;
	}

	pr = p->p_ucred->cr_prison;

	mtx_lock(&pr->pr_mtx);
	if (pr->pr_linux != NULL) {
		lpr = (struct linux_prison *)pr->pr_linux;
		if (lpr->pr_osname[0]) {
			bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
			mtx_unlock(&pr->pr_mtx);
			return;
		}
	}
	mtx_unlock(&pr->pr_mtx);
	bcopy(linux_osname, dst, LINUX_MAX_UTSNAME);
}

int
linux_set_osname(p, osname)
	struct proc *p;
	char *osname;
{
	register struct linux_prison *lpr;

	lpr = linux_get_prison(p);
	if (lpr != NULL) {
		strcpy(lpr->pr_osname, osname);
		mtx_unlock(&p->p_ucred->cr_prison->pr_mtx);
	} else {
		strcpy(linux_osname, osname);
	}

	return (0);
}

void
linux_get_osrelease(p, dst)
	struct proc *p;
	char *dst;
{
	register struct prison *pr;
	struct linux_prison *lpr;

	if (p->p_ucred->cr_prison == NULL) {
		bcopy(linux_osrelease, dst, LINUX_MAX_UTSNAME);
		return;
	}

	pr = p->p_ucred->cr_prison;

	mtx_lock(&pr->pr_mtx);
	if (pr->pr_linux != NULL) {
		lpr = (struct linux_prison *) pr->pr_linux;
		if (lpr->pr_osrelease[0]) {
			bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
			mtx_unlock(&pr->pr_mtx);
			return;
		}
	}
	mtx_unlock(&pr->pr_mtx);
	bcopy(linux_osrelease, dst, LINUX_MAX_UTSNAME);
}

int
linux_set_osrelease(p, osrelease)
	struct proc *p;
	char *osrelease;
{
	register struct linux_prison *lpr;

	lpr = linux_get_prison(p);
	if (lpr != NULL) {
		strcpy(lpr->pr_osrelease, osrelease);
		mtx_unlock(&p->p_ucred->cr_prison->pr_mtx);
	} else {
		strcpy(linux_osrelease, osrelease);
	}

	return (0);
}

int
linux_get_oss_version(p)
	struct proc *p;
{
	register struct prison *pr;
	register struct linux_prison *lpr;
	int version;

	if (p->p_ucred->cr_prison == NULL)
		return (linux_oss_version);

	pr = p->p_ucred->cr_prison;

	mtx_lock(&pr->pr_mtx);
	if (pr->pr_linux != NULL) {
		lpr = (struct linux_prison *) pr->pr_linux;
		if (lpr->pr_oss_version) {
			version = lpr->pr_oss_version;
		} else {
			version = linux_oss_version;
		}
	} else {
		version = linux_oss_version;
	}
	mtx_unlock(&pr->pr_mtx);

	return (version);
}

int
linux_set_oss_version(p, oss_version)
	struct proc *p;
	int oss_version;
{
	register struct linux_prison *lpr;

	lpr = linux_get_prison(p);
	if (lpr != NULL) {
		lpr->pr_oss_version = oss_version;
		mtx_unlock(&p->p_ucred->cr_prison->pr_mtx);
	} else {
		linux_oss_version = oss_version;
	}

	return (0);
}

#ifdef DEBUG

u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))];

static int
linux_debug(int syscall, int toggle, int global)
{

	if (global) {
		char c = toggle ? 0 : 0xff;

		memset(linux_debug_map, c, sizeof(linux_debug_map));
		return (0);
	}
	if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL)
		return (EINVAL);
	if (toggle)
		clrbit(linux_debug_map, syscall);
	else
		setbit(linux_debug_map, syscall);
	return (0);
}

/*
 * Usage: sysctl linux.debug=<syscall_nr>.<0/1>
 *
 *    E.g.: sysctl linux.debug=21.0
 *
 * As a special case, syscall "all" will apply to all syscalls globally.
 */
#define LINUX_MAX_DEBUGSTR	16
static int
linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
{
	char value[LINUX_MAX_DEBUGSTR], *p;
	int error, sysc, toggle;
	int global = 0;

	value[0] = '\0';
	error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
	if (error || req->newptr == NULL)
		return (error);
	for (p = value; *p != '\0' && *p != '.'; p++);
	if (*p == '\0')
		return (EINVAL);
	*p++ = '\0';
	sysc = strtol(value, NULL, 0);
	toggle = strtol(p, NULL, 0);
	if (strcmp(value, "all") == 0)
		global = 1;
	error = linux_debug(sysc, toggle, global);
	return (error);
}

SYSCTL_PROC(_compat_linux, OID_AUTO, debug,
            CTLTYPE_STRING | CTLFLAG_RW,
            0, 0, linux_sysctl_debug, "A",
            "Linux debugging control");

#endif /* DEBUG */
OpenPOWER on IntegriCloud