linux_sysctl.c revision 108541
183221Smarcel/*-
283221Smarcel * Copyright (c) 2001 Marcel Moolenaar
383221Smarcel * All rights reserved.
483221Smarcel *
583221Smarcel * Redistribution and use in source and binary forms, with or without
683221Smarcel * modification, are permitted provided that the following conditions
783221Smarcel * are met:
883221Smarcel * 1. Redistributions of source code must retain the above copyright
983221Smarcel *    notice, this list of conditions and the following disclaimer
1083221Smarcel *    in this position and unchanged.
1183221Smarcel * 2. Redistributions in binary form must reproduce the above copyright
1283221Smarcel *    notice, this list of conditions and the following disclaimer in the
1383221Smarcel *    documentation and/or other materials provided with the distribution.
1483221Smarcel * 3. The name of the author may not be used to endorse or promote products
1583221Smarcel *    derived from this software without specific prior written permission.
1683221Smarcel *
1783221Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1883221Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1983221Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2083221Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2183221Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2283221Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2383221Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2483221Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2583221Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2683221Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2783221Smarcel *
2883221Smarcel * $FreeBSD: head/sys/compat/linux/linux_sysctl.c 108541 2003-01-02 02:19:10Z alfred $
2983221Smarcel */
3083221Smarcel
3183221Smarcel#include <sys/param.h>
32103839Smini#include <sys/lock.h>
33102954Sbde#include <sys/malloc.h>
34103839Smini#include <sys/mutex.h>
35103839Smini#include <sys/proc.h>
36103839Smini#include <sys/sysctl.h>
3783221Smarcel#include <sys/systm.h>
38108541Salfred#include <sys/sbuf.h>
3983221Smarcel
4083221Smarcel#include <machine/../linux/linux.h>
4183221Smarcel#include <machine/../linux/linux_proto.h>
4283221Smarcel
43108541Salfred#include <compat/linux/linux_util.h>
44108541Salfred
4583221Smarcel#define	LINUX_CTL_KERN		1
4683221Smarcel#define	LINUX_CTL_VM		2
4783221Smarcel#define	LINUX_CTL_NET		3
4883221Smarcel#define	LINUX_CTL_PROC		4
4983221Smarcel#define	LINUX_CTL_FS		5
5083221Smarcel#define	LINUX_CTL_DEBUG		6
5183221Smarcel#define	LINUX_CTL_DEV		7
5283221Smarcel#define	LINUX_CTL_BUS		8
5383221Smarcel
5483221Smarcel/* CTL_KERN names */
5583221Smarcel#define	LINUX_KERN_OSTYPE	1
5683221Smarcel#define	LINUX_KERN_OSRELEASE	2
5783221Smarcel#define	LINUX_KERN_OSREV	3
5883221Smarcel#define	LINUX_KERN_VERSION	4
5983221Smarcel
6083221Smarcelstatic int
6183221Smarcelhandle_string(struct l___sysctl_args *la, char *value)
6283221Smarcel{
6383221Smarcel	int error;
6483221Smarcel
6583221Smarcel	if (la->oldval != NULL) {
6683221Smarcel		l_int len = strlen(value);
6783221Smarcel		error = copyout(value, la->oldval, len + 1);
6883221Smarcel		if (!error && la->oldlenp != NULL)
6983221Smarcel			error = copyout(&len, la->oldlenp, sizeof(len));
7083221Smarcel		if (error)
7183221Smarcel			return (error);
7283221Smarcel	}
7383221Smarcel
7483221Smarcel	if (la->newval != NULL)
7583221Smarcel		return (ENOTDIR);
7683221Smarcel
7783221Smarcel	return (0);
7883221Smarcel}
7983221Smarcel
8083221Smarcelint
8183366Sjulianlinux_sysctl(struct thread *td, struct linux_sysctl_args *args)
8283221Smarcel{
8383221Smarcel	struct l___sysctl_args la;
84108541Salfred	struct sbuf *sb;
8583221Smarcel	l_int *mib;
8683221Smarcel	int error, i;
8783221Smarcel
8883221Smarcel	error = copyin((caddr_t)args->args, &la, sizeof(la));
8983221Smarcel	if (error)
9083221Smarcel		return (error);
9183221Smarcel
92102814Siedowse	if (la.nlen <= 0 || la.nlen > LINUX_CTL_MAXNAME)
9383221Smarcel		return (ENOTDIR);
9483221Smarcel
95102814Siedowse	mib = malloc(la.nlen * sizeof(l_int), M_TEMP, M_WAITOK);
9683221Smarcel	error = copyin(la.name, mib, la.nlen * sizeof(l_int));
97102814Siedowse	if (error) {
98102814Siedowse		free(mib, M_TEMP);
9983221Smarcel		return (error);
100102814Siedowse	}
10183221Smarcel
10283221Smarcel	switch (mib[0]) {
10383221Smarcel	case LINUX_CTL_KERN:
10483221Smarcel		if (la.nlen < 2)
10583221Smarcel			break;
10683221Smarcel
10783221Smarcel		switch (mib[1]) {
10883221Smarcel		case LINUX_KERN_VERSION:
109102814Siedowse			error = handle_string(&la, version);
110102814Siedowse			free(mib, M_TEMP);
111102814Siedowse			return (error);
11283221Smarcel		default:
11383221Smarcel			break;
11483221Smarcel		}
11583221Smarcel		break;
11683221Smarcel	default:
11783221Smarcel		break;
11883221Smarcel	}
11983221Smarcel
120108541Salfred	sb = sbuf_new(NULL, NULL, 20 + la.nlen * 5, SBUF_AUTOEXTEND);
121108541Salfred	if (sb == NULL) {
122108541Salfred		linux_msg(td, "sysctl is not implemented");
123108541Salfred	} else {
124108541Salfred		sbuf_printf(sb, "sysctl ");
125108541Salfred		for (i = 0; i < la.nlen; i++)
126108541Salfred			sbuf_printf(sb, "%c%d", (i) ? ',' : '{', mib[i]);
127108541Salfred		sbuf_printf(sb, "} is not implemented");
128108541Salfred		sbuf_finish(sb);
129108541Salfred		linux_msg(td, "%s", sbuf_data(sb));
130108541Salfred		sbuf_delete(sb);
131108541Salfred	}
13283221Smarcel
133102814Siedowse	free(mib, M_TEMP);
13483221Smarcel	return (ENOTDIR);
13583221Smarcel}
136