Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

src: Add new example kernel module: sysctl



details:   https://anonhg.NetBSD.org/src/rev/ec3014767d92
branches:  trunk
changeset: 318122:ec3014767d92
user:      kamil <kamil%NetBSD.org@localhost>
date:      Fri Apr 13 01:20:27 2018 +0000
description:
Add new example kernel module: sysctl

sysctl - demonstrates adding a sysctl handle dynamically.

Patch by <Siddharth Muralee>
Polishing by myself.

diffstat:

 sys/modules/examples/Makefile        |    3 +-
 sys/modules/examples/README          |    8 +-
 sys/modules/examples/sysctl/Makefile |   10 ++
 sys/modules/examples/sysctl/sysctl.c |  118 +++++++++++++++++++++++++++++++++++
 4 files changed, 136 insertions(+), 3 deletions(-)

diffs (185 lines):

diff -r be46f58ab756 -r ec3014767d92 sys/modules/examples/Makefile
--- a/sys/modules/examples/Makefile     Fri Apr 13 01:14:19 2018 +0000
+++ b/sys/modules/examples/Makefile     Fri Apr 13 01:20:27 2018 +0000
@@ -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+=       ping            # Needs an additional helper program
 SUBDIR+=       properties
 SUBDIR+=       readhappy
+SUBDIR+=       sysctl
 
 .include <bsd.subdir.mk>
diff -r be46f58ab756 -r ec3014767d92 sys/modules/examples/README
--- a/sys/modules/examples/README       Fri Apr 13 01:14:19 2018 +0000
+++ b/sys/modules/examples/README       Fri Apr 13 01:20:27 2018 +0000
@@ -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 @@
      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.
diff -r be46f58ab756 -r ec3014767d92 sys/modules/examples/sysctl/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/sysctl/Makefile      Fri Apr 13 01:20:27 2018 +0000
@@ -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>
diff -r be46f58ab756 -r ec3014767d92 sys/modules/examples/sysctl/sysctl.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/sysctl/sysctl.c      Fri Apr 13 01:20:27 2018 +0000
@@ -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;
+}



Home | Main Index | Thread Index | Old Index