Source-Changes-HG archive

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

[src/trunk]: src/sys/modules/examples A set of more comprehensive example ker...



details:   https://anonhg.NetBSD.org/src/rev/25e7f16c5e1b
branches:  trunk
changeset: 338168:25e7f16c5e1b
user:      pgoyette <pgoyette%NetBSD.org@localhost>
date:      Wed May 13 07:07:36 2015 +0000

description:
A set of more comprehensive example kernel modules to replace our
previous single example.

These examples were provided by Kamil Rytarowski.

XXX These modules are built as part of a system build, but they
XXX are NOT installed in $DESTDIR.

diffstat:

 sys/modules/examples/Makefile                |   11 +
 sys/modules/examples/Makefile.inc            |    9 +
 sys/modules/examples/README                  |   59 ++++++++
 sys/modules/examples/hello/Makefile          |    9 +
 sys/modules/examples/hello/hello.c           |   64 ++++++++
 sys/modules/examples/luahello/luahello.lua   |   47 ++++++
 sys/modules/examples/ping/Makefile           |   18 ++
 sys/modules/examples/ping/cmd_ping.c         |   61 ++++++++
 sys/modules/examples/ping/ping.c             |  131 ++++++++++++++++++
 sys/modules/examples/ping/ping.h             |    8 +
 sys/modules/examples/properties/Makefile     |    9 +
 sys/modules/examples/properties/properties.c |   75 ++++++++++
 sys/modules/examples/readhappy/Makefile      |    9 +
 sys/modules/examples/readhappy/readhappy.c   |  195 +++++++++++++++++++++++++++
 14 files changed, 705 insertions(+), 0 deletions(-)

diffs (truncated from 761 to 300 lines):

diff -r 8debdeebaab0 -r 25e7f16c5e1b sys/modules/examples/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/Makefile     Wed May 13 07:07:36 2015 +0000
@@ -0,0 +1,11 @@
+#      $NetBSD: Makefile,v 1.1 2015/05/13 07:07:36 pgoyette Exp $
+
+.include <bsd.own.mk>
+
+SUBDIR+=       hello
+#SUBDIR+=      luahello        # Nothing to build here, only text files
+SUBDIR+=       ping            # Needs an additional helper program
+SUBDIR+=       properties
+SUBDIR+=       readhappy
+
+.include <bsd.subdir.mk>
diff -r 8debdeebaab0 -r 25e7f16c5e1b sys/modules/examples/Makefile.inc
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/Makefile.inc Wed May 13 07:07:36 2015 +0000
@@ -0,0 +1,9 @@
+#      $NetBSD: Makefile.inc,v 1.1 2015/05/13 07:07:36 pgoyette Exp $
+
+# We only build the example modules; we don't want to install them anywhere
+
+kmodinstall:
+
+# Include parent's Makefile.inc
+
+.include "../Makefile.inc"
diff -r 8debdeebaab0 -r 25e7f16c5e1b sys/modules/examples/README
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/README       Wed May 13 07:07:36 2015 +0000
@@ -0,0 +1,59 @@
+       $NetBSD: README,v 1.1 2015/05/13 07:07:36 pgoyette Exp $
+
+                           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
+     * readhappy       - 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
+
+
+     The example modules should not be used on a production machine.
+
+     All modules that create a cdevsw should be verified that the major number
+     should not conflict with a real device.
+
+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, readhappy, properties,
+     and ping, hellolua) first appeared in NetBSD 8.0 and they were written
+     by Kamil Rytarowski.
+
+AUTHORS
+     This document was written by Kamil Rytarowski.
diff -r 8debdeebaab0 -r 25e7f16c5e1b sys/modules/examples/hello/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/hello/Makefile       Wed May 13 07:07:36 2015 +0000
@@ -0,0 +1,9 @@
+#      $NetBSD: Makefile,v 1.1 2015/05/13 07:07:36 pgoyette Exp $
+
+.include "../Makefile.inc"
+
+#S?=   /usr/src/sys
+KMOD=  hello
+SRCS=  hello.c
+
+.include <bsd.kmodule.mk>
diff -r 8debdeebaab0 -r 25e7f16c5e1b sys/modules/examples/hello/hello.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/hello/hello.c        Wed May 13 07:07:36 2015 +0000
@@ -0,0 +1,64 @@
+/*     $NetBSD: hello.c,v 1.1 2015/05/13 07:07:36 pgoyette Exp $       */
+
+/*-
+ * 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: hello.c,v 1.1 2015/05/13 07:07:36 pgoyette Exp $");
+
+#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;
+}
diff -r 8debdeebaab0 -r 25e7f16c5e1b sys/modules/examples/luahello/luahello.lua
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/luahello/luahello.lua        Wed May 13 07:07:36 2015 +0000
@@ -0,0 +1,47 @@
+#      $NetBSD: luahello.lua,v 1.1 2015/05/13 07:07:36 pgoyette Exp $
+#
+#
+# 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")
diff -r 8debdeebaab0 -r 25e7f16c5e1b sys/modules/examples/ping/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/ping/Makefile        Wed May 13 07:07:36 2015 +0000
@@ -0,0 +1,18 @@
+#      $NetBSD: Makefile,v 1.1 2015/05/13 07:07:36 pgoyette Exp $
+
+.include "../Makefile.inc"
+
+#S?=   /usr/src/sys
+
+KMOD=  ping
+SRCS=  ping.c
+
+.include <bsd.kmodule.mk>
+
+# To make use of this module, you'll need to separately build the
+# cmd_ping program, with a Makefile similar to
+#
+#      MKMAN=  NO
+#      PROG=   cmd_ping
+#      .include <bsd.prog.mk>
+
diff -r 8debdeebaab0 -r 25e7f16c5e1b sys/modules/examples/ping/cmd_ping.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/modules/examples/ping/cmd_ping.c      Wed May 13 07:07:36 2015 +0000
@@ -0,0 +1,61 @@
+/*     $NetBSD: cmd_ping.c,v 1.1 2015/05/13 07:07:36 pgoyette Exp $    */
+
+/*-
+ * 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>
+__RCSID("$NetBSD $");
+
+#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);
+



Home | Main Index | Thread Index | Old Index