tech-kern archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
(patch) Improved documentation and examples of dynamic modules
I'm attaching a patch with enhanced documentation of modules(9).
Please see the attached patch. Feel free to tweak it for your needs.
I know that the example modules aren't MPSAFE just to not overkill the examples.
In the place of the original example (properties) I have added the following list:
* hello - the simplest `hello world' module
* properties - handle incoming properties during the module load
* happy - basic implementation of read(9) with happy numbers
* ping - basic ioctl(9)
* hellolua - the simplest `hello world' Lua module
I would like to see more examples!
Approved please commit.
Index: distrib/sets/lists/comp/mi
===================================================================
RCS file: /home/kamil/netbsd-cvs/netbsd/src/distrib/sets/lists/comp/mi,v
retrieving revision 1.1957
diff -u -r1.1957 mi
--- distrib/sets/lists/comp/mi 6 May 2015 15:57:07 -0000 1.1957
+++ distrib/sets/lists/comp/mi 8 May 2015 13:40:07 -0000
@@ -9741,6 +9741,7 @@
./usr/share/man/cat9/MGET.0 comp-sys-catman .cat
./usr/share/man/cat9/MGETHDR.0 comp-sys-catman .cat
./usr/share/man/cat9/MH_ALIGN.0 comp-sys-catman .cat
+./usr/share/man/cat9/MODULE.0 comp-sys-catman .cat
./usr/share/man/cat9/M_ALIGN.0 comp-sys-catman .cat
./usr/share/man/cat9/M_COPY_PKTHDR.0 comp-sys-catman .cat
./usr/share/man/cat9/M_LEADINGSPACE.0 comp-sys-catman .cat
@@ -10529,6 +10530,7 @@
./usr/share/man/cat9/microseq.0 comp-sys-catman .cat
./usr/share/man/cat9/microtime.0 comp-sys-catman .cat
./usr/share/man/cat9/microuptime.0 comp-sys-catman .cat
+./usr/share/man/cat9/modcmd.0 comp-sys-catman .cat
./usr/share/man/cat9/module.0 comp-sys-catman .cat
./usr/share/man/cat9/module_autoload.0 comp-sys-catman .cat
./usr/share/man/cat9/module_builtin_require_force.0 comp-sys-catman .cat
@@ -16554,6 +16556,7 @@
./usr/share/man/html9/MGET.html comp-sys-htmlman html
./usr/share/man/html9/MGETHDR.html comp-sys-htmlman html
./usr/share/man/html9/MH_ALIGN.html comp-sys-htmlman html
+./usr/share/man/html9/MODULE.html comp-sys-htmlman html
./usr/share/man/html9/M_ALIGN.html comp-sys-htmlman html
./usr/share/man/html9/M_COPY_PKTHDR.html comp-sys-htmlman html
./usr/share/man/html9/M_LEADINGSPACE.html comp-sys-htmlman html
@@ -17307,6 +17310,7 @@
./usr/share/man/html9/microseq.html comp-sys-htmlman html
./usr/share/man/html9/microtime.html comp-sys-htmlman html
./usr/share/man/html9/microuptime.html comp-sys-htmlman html
+./usr/share/man/html9/modcmd.html comp-sys-htmlman html
./usr/share/man/html9/module.html comp-sys-htmlman html
./usr/share/man/html9/module_autoload.html comp-sys-htmlman html
./usr/share/man/html9/module_builtin_require_force.html comp-sys-htmlman html
@@ -23471,6 +23475,7 @@
./usr/share/man/man9/MGET.9 comp-sys-man .man
./usr/share/man/man9/MGETHDR.9 comp-sys-man .man
./usr/share/man/man9/MH_ALIGN.9 comp-sys-man .man
+./usr/share/man/man9/MODULE.9 comp-sys-man .man
./usr/share/man/man9/M_ALIGN.9 comp-sys-man .man
./usr/share/man/man9/M_COPY_PKTHDR.9 comp-sys-man .man
./usr/share/man/man9/M_LEADINGSPACE.9 comp-sys-man .man
@@ -24260,6 +24265,7 @@
./usr/share/man/man9/microseq.9 comp-sys-man .man
./usr/share/man/man9/microtime.9 comp-sys-man .man
./usr/share/man/man9/microuptime.9 comp-sys-man .man
+./usr/share/man/man9/modcmd.9 comp-sys-man .man
./usr/share/man/man9/module.9 comp-sys-man .man
./usr/share/man/man9/module_autoload.9 comp-sys-man .man
./usr/share/man/man9/module_builtin_require_force.9 comp-sys-man .man
Index: share/man/man9/Makefile
===================================================================
RCS file: /home/kamil/netbsd-cvs/netbsd/src/share/man/man9/Makefile,v
retrieving revision 1.386
diff -u -r1.386 Makefile
--- share/man/man9/Makefile 27 Apr 2015 07:03:57 -0000 1.386
+++ share/man/man9/Makefile 7 May 2015 14:53:57 -0000
@@ -492,7 +492,9 @@
microuptime.9 getmicrouptime.9 \
microuptime.9 nanouptime.9 \
microuptime.9 getnanouptime.9
-MLINKS+=module.9 module_autoload.9 \
+MLINKS+=module.9 modcmd.9 \
+ module.9 MODULE.9 \
+ module.9 module_autoload.9 \
module.9 module_builtin_require_force.9 \
module.9 module_find_section.9 \
module.9 module_hold.9 \
Index: share/man/man9/module.9
===================================================================
RCS file: /home/kamil/netbsd-cvs/netbsd/src/share/man/man9/module.9,v
retrieving revision 1.30
diff -u -r1.30 module.9
--- share/man/man9/module.9 18 Mar 2014 18:20:40 -0000 1.30
+++ share/man/man9/module.9 7 May 2015 15:05:01 -0000
@@ -446,7 +446,11 @@
deals with resolution of relocatable symbols.
.Fn module_init_md
is for finding modules passed in by the boot loader.
+.Sh EXAMPLES
+A list of example modules is located in
+.Pa sys/modules/examples .
.Sh SEE ALSO
+.Xr lua 9lua ,
.Xr modctl 2 ,
.Xr module 7
.Sh HISTORY
Index: sys/modules/example/Makefile
===================================================================
RCS file: sys/modules/example/Makefile
diff -N sys/modules/example/Makefile
--- sys/modules/example/Makefile 2 May 2008 14:20:50 -0000 1.3
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,6 +0,0 @@
-# $NetBSD: Makefile,v 1.2 2008/02/10 10:51:18 jmmv Exp $
-
-KMOD= example
-SRCS= example.c
-
-.include <bsd.kmodule.mk>
Index: sys/modules/example/example.c
===================================================================
RCS file: sys/modules/example/example.c
diff -N sys/modules/example/example.c
--- sys/modules/example/example.c 25 Oct 2010 22:41:42 -0000 1.7
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,93 +0,0 @@
-/* $NetBSD: example.c,v 1.5 2009/10/20 05:33:30 jnemeth Exp $ */
-
-/*-
- * Copyright (c) 2008 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: example.c,v 1.5 2009/10/20 05:33:30 jnemeth Exp $");
-
-#include <sys/param.h>
-#include <sys/kernel.h>
-#include <sys/module.h>
-
-/*
- * Last parameter of MODULE macro is a list of names (as string; names are
- * separated by commas) of dependencies. If module has no dependencies,
- * then NULL should be passed.
- */
-
-MODULE(MODULE_CLASS_MISC, example, NULL);
-
-static
-void
-handle_props(prop_dictionary_t props)
-{
- const char *msg;
- prop_string_t str;
-
- if (props != NULL) {
- str = prop_dictionary_get(props, "msg");
- } else {
- printf("No property dictionary was provided.\n");
- str = NULL;
- }
- if (str == NULL)
- printf("The 'msg' property was not given.\n");
- else if (prop_object_type(str) != PROP_TYPE_STRING)
- printf("The 'msg' property is not a string.\n");
- else {
- msg = prop_string_cstring_nocopy(str);
- if (msg == NULL)
- printf("Failed to process the 'msg' property.\n");
- else
- printf("The 'msg' property is: %s\n", msg);
- }
-}
-
-static int
-example_modcmd(modcmd_t cmd, void *arg)
-{
-
- switch (cmd) {
- case MODULE_CMD_INIT:
- printf("Example module loaded.\n");
- handle_props(arg);
- break;
-
- case MODULE_CMD_FINI:
- printf("Example module unloaded.\n");
- break;
-
- case MODULE_CMD_STAT:
- printf("Example module status queried.\n");
- return ENOTTY;
-
- default:
- return ENOTTY;
- }
-
- return 0;
-}
Index: sys/modules/examples/README
===================================================================
RCS file: sys/modules/examples/README
diff -N sys/modules/examples/README
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/README 8 May 2015 13:33:26 -0000
@@ -0,0 +1,52 @@
+ $NetBSD: $
+
+ Kernel Developer's Manual
+
+DESCRIPTION
+ The kernel example dynamic modules.
+
+ This directory contains the following example modules:
+ * hello - the simplest `hello world' module
+ * properties - handle incoming properties during the module load
+ * happy - basic implementation of read(9) with happy numbers
+ * ping - basic ioctl(9)
+ * hellolua - the simplest `hello world' Lua module
+
+ 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
+ directory with example modules and use make(1):
+
+ # make
+
+ To load, unload and stat the module use modload(8), modunload(8) and
+ modstat(8).
+
+ The S parameter in the Makefile files points to src/sys and it can be
+ overloaded in this way:
+
+ # make S=/data/netbsd/src/sys
+
+ The code of a module does not need to be in src/sys unless you use
+ the autoconf(9) framework.
+
+ A cross-built of a module for a target platform is possible with the
+ build.sh framework. You need to generate the toolchain and set
+ appropriately PATH to point bin/ in the TOOLDIR path. An example command
+ to cross-build a module with the amd64 toolchain is as follows:
+
+ # nbmake-amd64 S=/data/netbsd/src/sys
+
+SEE ALSO
+ lua(9lua), modctl(2), modload(8), module(7), module(9), modstat(8),
+ modunload(8)
+
+HISTORY
+ An example of handling incoming properties first appeared in NetBSD 5.0
+ and was written by Julio Merino with further modifications by Martin
+ Husemann, Adam Hamsik, John Nemeth and Mindaugas Rasiukevicius.
+
+ This document and additional modules (hello, happy and ping, hellolua)
+ first appeared in NetBSD 8.0 and they were written by Kamil Rytarowski.
+
+AUTHORS
+ This document was written by Kamil Rytarowski.
Index: sys/modules/examples/happy/Makefile
===================================================================
RCS file: sys/modules/examples/happy/Makefile
diff -N sys/modules/examples/happy/Makefile
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/happy/Makefile 7 May 2015 20:03:49 -0000
@@ -0,0 +1,7 @@
+# $NetBSD: Makefile,v 1.2 2008/02/10 10:51:18 jmmv Exp $
+
+S?= /usr/src/sys
+KMOD= happy
+SRCS= happy.c
+
+.include <bsd.kmodule.mk>
Index: sys/modules/examples/happy/happy.c
===================================================================
RCS file: sys/modules/examples/happy/happy.c
diff -N sys/modules/examples/happy/happy.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/happy/happy.c 8 May 2015 13:25:09 -0000
@@ -0,0 +1,176 @@
+/* $NetBSD: $ */
+
+/*-
+ * Copyright (c) 2015 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: $");
+
+#include <sys/param.h>
+#include <sys/conf.h>
+#include <sys/device.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+
+/*
+ * Create a device /dev/happy to generate happy numbers.
+ *
+ * To use this device you need to do:
+ * mknod /dev/happy c 210 0
+ *
+ * A happy number is a number defined by the following process: Starting with
+ * any positive integer, replace the number by the sum of the squares of its
+ * digits, and repeat the process until the number equals 1 (where it will
+ * stay), or it loops endlessly in a cycle which does not include 1. Those
+ * numbers for which this process ends in 1 are happy numbers, while those that
+ * do not end in 1 are unhappy numbers (or sad numbers).
+ */
+
+static unsigned
+dsum(unsigned n)
+{
+ unsigned sum, x;
+ for (sum = 0; n; n /= 10) x = n % 10, sum += x * x;
+ return sum;
+}
+
+static int
+check_happy(unsigned n)
+{
+ unsigned cache[1000] = {0};
+
+ while (1) {
+ unsigned total;
+
+ /* 4 digit numbers can't cycle */
+ while ((total = dsum(n)) > 999)
+ n = total;
+
+ if (total == 1)
+ return 1;
+ if (cache[total])
+ return 0;
+
+ n = total;
+ cache[total] = 1;
+ }
+}
+
+dev_type_open(happy_open);
+dev_type_close(happy_close);
+dev_type_read(happy_read);
+
+static struct cdevsw happy_cdevsw = {
+ .d_open = happy_open,
+ .d_close = happy_close,
+ .d_read = happy_read,
+ .d_write = nowrite,
+ .d_ioctl = noioctl,
+ .d_stop = nostop,
+ .d_tty = notty,
+ .d_poll = nopoll,
+ .d_mmap = nommap,
+ .d_kqfilter = nokqfilter,
+ .d_discard = nodiscard,
+ .d_flag = D_OTHER
+};
+
+
+struct happy_softc {
+ int refcnt;
+ unsigned last;
+};
+
+static struct happy_softc sc;
+
+int
+happy_open(dev_t self __unused, int flag __unused, int mode __unused, struct lwp *l __unused)
+{
+ if (sc.refcnt > 0)
+ return EBUSY;
+
+ sc.last = 0;
+ ++sc.refcnt;
+
+ return 0;
+}
+
+int
+happy_close(dev_t self __unused, int flag __unused, int mode __unused, struct lwp *l __unused)
+{
+ --sc.refcnt;
+
+ return 0;
+}
+
+int
+happy_read(dev_t self __unused, struct uio *uio, int flags __unused)
+{
+ char line[80];
+
+ /* Get next happy number */
+ while (check_happy(++sc.last) == 0)
+ continue;
+
+ /* Print it into line[] with trailing \n */
+ int len = snprintf(line, sizeof(line), "%u\n", sc.last);
+
+ /* Is there room? */
+ if (uio->uio_resid < len) {
+ --sc.last; /* Step back */
+ return EINVAL;
+ }
+
+ /* Send it to User-Space */
+ int e;
+ if ((e = uiomove(line, len, uio)))
+ return e;
+
+ return 0;
+}
+
+MODULE(MODULE_CLASS_MISC, happy, NULL);
+
+static int
+happy_modcmd(modcmd_t cmd, void *arg __unused)
+{
+ int cmajor = 210, bmajor = -1;
+
+ switch (cmd) {
+ case MODULE_CMD_INIT:
+ if (devsw_attach("happy", NULL, &bmajor, &happy_cdevsw, &cmajor))
+ return ENXIO;
+ return 0;
+ case MODULE_CMD_FINI:
+ if (sc.refcnt > 0)
+ return EBUSY;
+
+ devsw_detach(NULL, &happy_cdevsw);
+ return 0;
+ default:
+ return ENOTTY;
+ }
+}
Index: sys/modules/examples/hello/Makefile
===================================================================
RCS file: sys/modules/examples/hello/Makefile
diff -N sys/modules/examples/hello/Makefile
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/hello/Makefile 7 May 2015 15:43:28 -0000
@@ -0,0 +1,7 @@
+# $NetBSD: Makefile,v 1.2 2008/02/10 10:51:18 jmmv Exp $
+
+S?= /usr/src/sys
+KMOD= hello
+SRCS= hello.c
+
+.include <bsd.kmodule.mk>
Index: sys/modules/examples/hello/hello.c
===================================================================
RCS file: sys/modules/examples/hello/hello.c
diff -N sys/modules/examples/hello/hello.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/hello/hello.c 7 May 2015 16:50:19 -0000
@@ -0,0 +1,64 @@
+/* $NetBSD: $ */
+
+/*-
+ * Copyright (c) 2015 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: $");
+
+#include <sys/param.h>
+#include <sys/module.h>
+
+/*
+ * Last parameter of MODULE macro is a list of names (as string; names are
+ * separated by commas) of dependencies. If module has no dependencies,
+ * then NULL should be passed.
+ */
+
+MODULE(MODULE_CLASS_MISC, hello, NULL);
+
+static int
+hello_modcmd(modcmd_t cmd, void *arg __unused)
+{
+ switch (cmd) {
+ case MODULE_CMD_INIT:
+ printf("Example module loaded.\n");
+ break;
+
+ case MODULE_CMD_FINI:
+ printf("Example module unloaded.\n");
+ break;
+
+ case MODULE_CMD_STAT:
+ printf("Example module status queried.\n");
+ break;
+
+ default:
+ return ENOTTY;
+ }
+
+ return 0;
+}
Index: sys/modules/examples/hellolua/hellolua.lua
===================================================================
RCS file: sys/modules/examples/hellolua/hellolua.lua
diff -N sys/modules/examples/hellolua/hellolua.lua
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/hellolua/hellolua.lua 7 May 2015 20:29:02 -0000
@@ -0,0 +1,47 @@
+# $NetBSD: $
+#
+#
+# Copyright (c) 2015 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.
+#
+#
+# Print `Hello Lua world' in dmesg(8)
+#
+# Tutorial
+# 1. Load the lua and luasystm modules.
+# modload lua
+# modload luasystm
+#
+# 2. Create Lua state for our code
+# luactl create state1
+#
+# 3. Require systm(9lua) for state1
+# luactl require state1 systm
+#
+# 4. Load our code in state1
+# luactl load state1 ./hellolua.lua
+#
+# NB. The path with our code must contain at least single '/' character
+
+systm.print("Hello Lua world!\n")
Index: sys/modules/examples/ping/Makefile
===================================================================
RCS file: sys/modules/examples/ping/Makefile
diff -N sys/modules/examples/ping/Makefile
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/ping/Makefile 7 May 2015 20:04:15 -0000
@@ -0,0 +1,7 @@
+# $NetBSD: $
+
+S?= /usr/src/sys
+KMOD= ping
+SRCS= ping.c
+
+.include <bsd.kmodule.mk>
Index: sys/modules/examples/ping/cmd_ping.c
===================================================================
RCS file: sys/modules/examples/ping/cmd_ping.c
diff -N sys/modules/examples/ping/cmd_ping.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/ping/cmd_ping.c 8 May 2015 13:32:35 -0000
@@ -0,0 +1,58 @@
+/* $NetBSD: $ */
+
+/*-
+ * Copyright (c) 2015 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 <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#include "ping.h"
+
+#define _PATH_DEV_PING "/dev/ping"
+
+int main(int argc, char **argv)
+{
+ int devfd;
+
+ setprogname(argv[0]);
+
+ if ((devfd = open(_PATH_DEV_PING, O_RDWR)) == -1)
+ err(EXIT_FAILURE, "Cannot open %s", _PATH_DEV_PING);
+
+ if (ioctl(devfd, CMD_PING) == -1)
+ err(EXIT_FAILURE, "ping failed");
+
+ printf("%s: ping sent successfully\n", getprogname());
+
+ if (close(devfd) == -1)
+ err(EXIT_FAILURE, "Cannot close %s", _PATH_DEV_PING);
+
+ return EXIT_SUCCESS;
+}
Index: sys/modules/examples/ping/ping.c
===================================================================
RCS file: sys/modules/examples/ping/ping.c
diff -N sys/modules/examples/ping/ping.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/ping/ping.c 8 May 2015 13:25:56 -0000
@@ -0,0 +1,126 @@
+/* $NetBSD: $ */
+
+/*-
+ * Copyright (c) 2015 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: $");
+
+#include <sys/param.h>
+#include <sys/conf.h>
+#include <sys/device.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+
+#include "ping.h"
+
+/*
+ * Create a device /dev/ping in order to test this module.
+ *
+ * To use this device you need to do:
+ * mknod /dev/ping c 210 0
+ *
+ */
+
+dev_type_open(ping_open);
+dev_type_close(ping_close);
+dev_type_ioctl(ping_ioctl);
+
+static struct cdevsw ping_cdevsw = {
+ .d_open = ping_open,
+ .d_close = ping_close,
+ .d_read = noread,
+ .d_write = nowrite,
+ .d_ioctl = ping_ioctl,
+ .d_stop = nostop,
+ .d_tty = notty,
+ .d_poll = nopoll,
+ .d_mmap = nommap,
+ .d_kqfilter = nokqfilter,
+ .d_discard = nodiscard,
+ .d_flag = D_OTHER
+};
+
+
+struct ping_softc {
+ int refcnt;
+};
+
+static struct ping_softc sc;
+
+int
+ping_open(dev_t self __unused, int flag __unused, int mode __unused, struct lwp *l __unused)
+{
+ if (sc.refcnt > 0)
+ return EBUSY;
+
+ ++sc.refcnt;
+
+ return 0;
+}
+
+int
+ping_close(dev_t self __unused, int flag __unused, int mode __unused, struct lwp *l __unused)
+{
+ --sc.refcnt;
+
+ return 0;
+}
+
+int
+ping_ioctl(dev_t self __unused, u_long cmd, void *data, int flag, struct lwp *l __unused)
+{
+ switch(cmd) {
+ case CMD_PING:
+ printf("ping: pong!\n");
+ return 0;
+ default:
+ return 1;
+ }
+}
+
+MODULE(MODULE_CLASS_MISC, ping, NULL);
+
+static int
+ping_modcmd(modcmd_t cmd, void *arg __unused)
+{
+ int cmajor = 210, bmajor = -1;
+
+ switch (cmd) {
+ case MODULE_CMD_INIT:
+ if (devsw_attach("ping", NULL, &bmajor, &ping_cdevsw, &cmajor))
+ return ENXIO;
+ return 0;
+ case MODULE_CMD_FINI:
+ if (sc.refcnt > 0)
+ return EBUSY;
+
+ devsw_detach(NULL, &ping_cdevsw);
+ return 0;
+ default:
+ return ENOTTY;
+ }
+}
Index: sys/modules/examples/ping/ping.h
===================================================================
RCS file: sys/modules/examples/ping/ping.h
diff -N sys/modules/examples/ping/ping.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/ping/ping.h 7 May 2015 20:09:18 -0000
@@ -0,0 +1,8 @@
+#ifndef _PING_H_
+#define _PING_H_
+
+#include <sys/ioccom.h>
+
+#define CMD_PING _IO('p', 2)
+
+#endif /* _PING_H_ */
Index: sys/modules/examples/properties/Makefile
===================================================================
RCS file: sys/modules/examples/properties/Makefile
diff -N sys/modules/examples/properties/Makefile
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/properties/Makefile 7 May 2015 15:43:44 -0000
@@ -0,0 +1,7 @@
+# $NetBSD: Makefile,v 1.2 2008/02/10 10:51:18 jmmv Exp $
+
+S?= /usr/src/sys
+KMOD= properties
+SRCS= properties.c
+
+.include <bsd.kmodule.mk>
Index: sys/modules/examples/properties/properties.c
===================================================================
RCS file: sys/modules/examples/properties/properties.c
diff -N sys/modules/examples/properties/properties.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/modules/examples/properties/properties.c 7 May 2015 16:51:50 -0000
@@ -0,0 +1,75 @@
+/* $NetBSD: $ */
+
+/*-
+ * Copyright (c) 2008 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: $");
+
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/module.h>
+
+MODULE(MODULE_CLASS_MISC, properties, NULL);
+
+static void
+handle_props(prop_dictionary_t props)
+{
+ const char *msg;
+ prop_string_t str;
+
+ if (props != NULL) {
+ str = prop_dictionary_get(props, "msg");
+ } else {
+ printf("No property dictionary was provided.\n");
+ str = NULL;
+ }
+ if (str == NULL)
+ printf("The 'msg' property was not given.\n");
+ else if (prop_object_type(str) != PROP_TYPE_STRING)
+ printf("The 'msg' property is not a string.\n");
+ else {
+ msg = prop_string_cstring_nocopy(str);
+ if (msg == NULL)
+ printf("Failed to process the 'msg' property.\n");
+ else
+ printf("The 'msg' property is: %s\n", msg);
+ }
+}
+
+static int
+properties_modcmd(modcmd_t cmd, void *arg)
+{
+ switch (cmd) {
+ case MODULE_CMD_INIT:
+ handle_props(arg);
+ return 0;
+ case MODULE_CMD_FINI:
+ return 0;
+ default:
+ return ENOTTY;
+ }
+}
Home |
Main Index |
Thread Index |
Old Index