linux_sysctl.c revision 156874
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
9111798Sdes *    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
29116173Sobrien#include <sys/cdefs.h>
30116173Sobrien__FBSDID("$FreeBSD: head/sys/compat/linux/linux_sysctl.c 156874 2006-03-19 11:10:33Z ru $");
31116173Sobrien
32156874Sru#include "opt_compat.h"
33156874Sru
3483221Smarcel#include <sys/param.h>
35103839Smini#include <sys/lock.h>
36102954Sbde#include <sys/malloc.h>
37103839Smini#include <sys/mutex.h>
38103839Smini#include <sys/proc.h>
39103839Smini#include <sys/sysctl.h>
4083221Smarcel#include <sys/systm.h>
41108541Salfred#include <sys/sbuf.h>
4283221Smarcel
43140214Sobrien#ifdef COMPAT_LINUX32
44140214Sobrien#include <machine/../linux32/linux.h>
45140214Sobrien#include <machine/../linux32/linux32_proto.h>
46140214Sobrien#else
4783221Smarcel#include <machine/../linux/linux.h>
4883221Smarcel#include <machine/../linux/linux_proto.h>
49133816Stjr#endif
5083221Smarcel
51108541Salfred#include <compat/linux/linux_util.h>
52108541Salfred
5383221Smarcel#define	LINUX_CTL_KERN		1
5483221Smarcel#define	LINUX_CTL_VM		2
5583221Smarcel#define	LINUX_CTL_NET		3
5683221Smarcel#define	LINUX_CTL_PROC		4
5783221Smarcel#define	LINUX_CTL_FS		5
5883221Smarcel#define	LINUX_CTL_DEBUG		6
5983221Smarcel#define	LINUX_CTL_DEV		7
6083221Smarcel#define	LINUX_CTL_BUS		8
6183221Smarcel
6283221Smarcel/* CTL_KERN names */
6383221Smarcel#define	LINUX_KERN_OSTYPE	1
6483221Smarcel#define	LINUX_KERN_OSRELEASE	2
6583221Smarcel#define	LINUX_KERN_OSREV	3
6683221Smarcel#define	LINUX_KERN_VERSION	4
6783221Smarcel
6883221Smarcelstatic int
6983221Smarcelhandle_string(struct l___sysctl_args *la, char *value)
7083221Smarcel{
7183221Smarcel	int error;
7283221Smarcel
73133816Stjr	if (la->oldval != 0) {
7483221Smarcel		l_int len = strlen(value);
75133816Stjr		error = copyout(value, PTRIN(la->oldval), len + 1);
76133816Stjr		if (!error && la->oldlenp != 0)
77133816Stjr			error = copyout(&len, PTRIN(la->oldlenp), sizeof(len));
7883221Smarcel		if (error)
7983221Smarcel			return (error);
8083221Smarcel	}
8183221Smarcel
82133816Stjr	if (la->newval != 0)
8383221Smarcel		return (ENOTDIR);
8483221Smarcel
8583221Smarcel	return (0);
8683221Smarcel}
8783221Smarcel
8883221Smarcelint
8983366Sjulianlinux_sysctl(struct thread *td, struct linux_sysctl_args *args)
9083221Smarcel{
9183221Smarcel	struct l___sysctl_args la;
92108541Salfred	struct sbuf *sb;
9383221Smarcel	l_int *mib;
9483221Smarcel	int error, i;
9583221Smarcel
96111797Sdes	error = copyin(args->args, &la, sizeof(la));
9783221Smarcel	if (error)
9883221Smarcel		return (error);
9983221Smarcel
100102814Siedowse	if (la.nlen <= 0 || la.nlen > LINUX_CTL_MAXNAME)
10183221Smarcel		return (ENOTDIR);
10283221Smarcel
103111119Simp	mib = malloc(la.nlen * sizeof(l_int), M_TEMP, M_WAITOK);
104133816Stjr	error = copyin(PTRIN(la.name), mib, la.nlen * sizeof(l_int));
105102814Siedowse	if (error) {
106102814Siedowse		free(mib, M_TEMP);
10783221Smarcel		return (error);
108102814Siedowse	}
10983221Smarcel
11083221Smarcel	switch (mib[0]) {
11183221Smarcel	case LINUX_CTL_KERN:
11283221Smarcel		if (la.nlen < 2)
11383221Smarcel			break;
11483221Smarcel
11583221Smarcel		switch (mib[1]) {
11683221Smarcel		case LINUX_KERN_VERSION:
117102814Siedowse			error = handle_string(&la, version);
118102814Siedowse			free(mib, M_TEMP);
119102814Siedowse			return (error);
12083221Smarcel		default:
12183221Smarcel			break;
12283221Smarcel		}
12383221Smarcel		break;
12483221Smarcel	default:
12583221Smarcel		break;
12683221Smarcel	}
12783221Smarcel
128108541Salfred	sb = sbuf_new(NULL, NULL, 20 + la.nlen * 5, SBUF_AUTOEXTEND);
129108541Salfred	if (sb == NULL) {
130108541Salfred		linux_msg(td, "sysctl is not implemented");
131108541Salfred	} else {
132108541Salfred		sbuf_printf(sb, "sysctl ");
133108541Salfred		for (i = 0; i < la.nlen; i++)
134108541Salfred			sbuf_printf(sb, "%c%d", (i) ? ',' : '{', mib[i]);
135108541Salfred		sbuf_printf(sb, "} is not implemented");
136108541Salfred		sbuf_finish(sb);
137108541Salfred		linux_msg(td, "%s", sbuf_data(sb));
138108541Salfred		sbuf_delete(sb);
139108541Salfred	}
14083221Smarcel
141102814Siedowse	free(mib, M_TEMP);
14283221Smarcel	return (ENOTDIR);
14383221Smarcel}
144