Add new example kernel module: sysctl
sysctl - demonstrates adding a sysctl handle dynamically. Patch by <Siddharth Muralee> Polishing by myself.
This commit is contained in:
parent
18eacdc36b
commit
e5c3c7a7c4
|
@ -1,4 +1,4 @@
|
|||
# $NetBSD: Makefile,v 1.2 2017/04/15 18:22:35 kamil Exp $
|
||||
# $NetBSD: Makefile,v 1.3 2018/04/13 01:20:27 kamil Exp $
|
||||
|
||||
.include <bsd.own.mk>
|
||||
|
||||
|
@ -8,5 +8,6 @@ SUBDIR+= luareadhappy # Needs an additional Lua script
|
|||
SUBDIR+= ping # Needs an additional helper program
|
||||
SUBDIR+= properties
|
||||
SUBDIR+= readhappy
|
||||
SUBDIR+= sysctl
|
||||
|
||||
.include <bsd.subdir.mk>
|
||||
|
|
|
@ -1,17 +1,18 @@
|
|||
$NetBSD: README,v 1.4 2017/04/23 23:39:42 sevan Exp $
|
||||
$NetBSD: README,v 1.5 2018/04/13 01:20:27 kamil Exp $
|
||||
|
||||
Kernel Developer's Manual
|
||||
|
||||
DESCRIPTION
|
||||
The kernel example dynamic modules.
|
||||
|
||||
This directory contains the following example modules:
|
||||
This directory contains the following example modules
|
||||
* hello - the simplest `hello world' module
|
||||
* properties - handle incoming properties during the module load
|
||||
* readhappy - basic implementation of read(9) with happy numbers
|
||||
* ping - basic ioctl(9)
|
||||
* luahello - the simplest `hello world' Lua module
|
||||
* luareadhappy - demonstrates calling Lua code from C
|
||||
* sysctl - demonstrates adding a sysctl handle dynamically
|
||||
|
||||
To build the examples you need a local copy of NetBSD sources. You also
|
||||
need the comp set with toolchain. To build the module just enter a
|
||||
|
@ -56,5 +57,8 @@ HISTORY
|
|||
ping, luahello and luareadhappy) first appeared in NetBSD 8.0; they were
|
||||
written by Kamil Rytarowski.
|
||||
|
||||
The sysctl module first appeared in NetBSD 9.0 and was authored by
|
||||
Siddharth Muralee.
|
||||
|
||||
AUTHORS
|
||||
This document was written by Kamil Rytarowski.
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
# $NetBSD: Makefile,v 1.1 2018/04/13 01:20:28 kamil Exp $
|
||||
|
||||
.include "../Makefile.inc"
|
||||
|
||||
#S?= /usr/src/sys
|
||||
|
||||
KMOD= sysctl
|
||||
SRCS= sysctl.c
|
||||
|
||||
.include <bsd.kmodule.mk>
|
|
@ -0,0 +1,118 @@
|
|||
/* $NetBSD: sysctl.c,v 1.1 2018/04/13 01:20:28 kamil Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2018 The NetBSD Foundation, Inc.
|
||||
* 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.
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
||||
* ``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 FOUNDATION OR CONTRIBUTORS
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__KERNEL_RCSID(0, "$NetBSD: sysctl.c,v 1.1 2018/04/13 01:20:28 kamil Exp $");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
/*
|
||||
* Check if sysctl -A contains an entry
|
||||
* example_subroot1.sysctl_example=0
|
||||
* to test this module
|
||||
*
|
||||
*/
|
||||
|
||||
MODULE(MODULE_CLASS_MISC, sysctl, NULL);
|
||||
|
||||
static int sysctl_example;
|
||||
|
||||
static struct sysctllog *example_sysctl_log;
|
||||
|
||||
static void sysctl_example_setup(struct sysctllog **);
|
||||
|
||||
|
||||
/*
|
||||
* sysctl_example_setup :
|
||||
* It first creates a subtree by adding a node to the tree.
|
||||
* This node is named as example_subroot1.
|
||||
*
|
||||
* It then creates a node in subtree for the example variable which
|
||||
* is an integer and is defined in this file itself.
|
||||
*
|
||||
* ROOT
|
||||
* |
|
||||
* -------
|
||||
* |
|
||||
* examples_subroot1
|
||||
* |
|
||||
* |
|
||||
* sysctl_example (INT)
|
||||
*
|
||||
*/
|
||||
|
||||
static void
|
||||
sysctl_example_setup(struct sysctllog **clog)
|
||||
{
|
||||
const struct sysctlnode *rnode;
|
||||
|
||||
sysctl_createv(clog, 0, NULL, &rnode,
|
||||
CTLFLAG_PERMANENT,
|
||||
CTLTYPE_NODE, "example_subroot1",
|
||||
NULL,
|
||||
NULL, 0,
|
||||
NULL, 0,
|
||||
CTL_CREATE, CTL_EOL);
|
||||
|
||||
sysctl_createv(clog, 0, &rnode, NULL,
|
||||
CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
|
||||
CTLTYPE_INT, "sysctl_example",
|
||||
SYSCTL_DESCR("An example for sysctl_example"),
|
||||
NULL, 0,
|
||||
&sysctl_example, 0,
|
||||
CTL_CREATE, CTL_EOL);
|
||||
}
|
||||
|
||||
/*
|
||||
* The sysctl_example modcmd has two functions.
|
||||
* 1. Call the sysctl_example_setup function to create a sysctl
|
||||
* handle when the module is loaded in the kernel.
|
||||
* 2. Remove the sysctl entry from the kernel once the module
|
||||
* is unloaded.
|
||||
*/
|
||||
|
||||
|
||||
static int
|
||||
sysctl_modcmd(modcmd_t cmd, void *arg)
|
||||
{
|
||||
switch(cmd) {
|
||||
case MODULE_CMD_INIT:
|
||||
printf("sysctl module inserted");
|
||||
sysctl_example_setup(&example_sysctl_log);
|
||||
break;
|
||||
case MODULE_CMD_FINI:
|
||||
printf("sysctl module unloaded");
|
||||
sysctl_teardown(&example_sysctl_log);
|
||||
break;
|
||||
default:
|
||||
return ENOTTY;
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue