Module Name:    src
Committed By:   kamil
Date:           Sat Apr 15 04:27:30 UTC 2017

Added Files:
        src/sys/modules/examples/luareadhappy: Makefile happy.lua
            luareadhappy.c

Log Message:
Add new example kernel module in Lua luareadhappy

This example presents a C module with its device (/dev entry) and its
content generator with algorithm defined in Lua state. The Lua state can
be changed dynamically from userland, without interruption of read(2) over
the device.

This is an example how to call Lua code from C.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/modules/examples/luareadhappy/Makefile \
    src/sys/modules/examples/luareadhappy/happy.lua \
    src/sys/modules/examples/luareadhappy/luareadhappy.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Added files:

Index: src/sys/modules/examples/luareadhappy/Makefile
diff -u /dev/null src/sys/modules/examples/luareadhappy/Makefile:1.1
--- /dev/null	Sat Apr 15 04:27:30 2017
+++ src/sys/modules/examples/luareadhappy/Makefile	Sat Apr 15 04:27:30 2017
@@ -0,0 +1,14 @@
+#	$NetBSD: Makefile,v 1.1 2017/04/15 04:27:30 kamil Exp $
+
+#.include "../Makefile.inc"
+
+#S?=	/usr/src/sys
+
+KMOD=	luareadhappy
+SRCS=	luareadhappy.c
+
+CPPFLAGS+=	-I${S}/../external/mit/lua/dist/src \
+		-I${S}/modules/lua \
+		-I${S}/sys
+
+.include <bsd.kmodule.mk>
Index: src/sys/modules/examples/luareadhappy/happy.lua
diff -u /dev/null src/sys/modules/examples/luareadhappy/happy.lua:1.1
--- /dev/null	Sat Apr 15 04:27:30 2017
+++ src/sys/modules/examples/luareadhappy/happy.lua	Sat Apr 15 04:27:30 2017
@@ -0,0 +1,77 @@
+--	$NetBSD: happy.lua,v 1.1 2017/04/15 04:27:30 kamil 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.
+--
+--
+-- Commentary:
+-- 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).
+--
+-- For more information on happy numbers, and the algorithms, see
+--      http://en.wikipedia.org/wiki/Happy_number
+--
+-- The happy number generator is here only to have something that the user
+-- can read from our device.  Any other arbitrary data generator could
+-- have been used.  The algorithm is not critical to the implementation
+-- of the module.
+
+local HAPPY_NUMBER = 1
+
+-- If n is not happy then its sequence ends in the cycle:
+-- 4, 16, 37, 58, 89, 145, 42, 20, 4, ...
+local SAD_NUMBER = 4
+
+-- This following algorithm is designed for numbers of the integer type.
+-- Integer numbers are used by default in the NetBSD kernel, as there would be
+-- need for additional overhead in context-switch with support for floats.
+
+function dsum(n)
+	local sum = 0
+	while n > 0 do
+		local x = n % 10
+		sum = sum + (x * x)
+		n = n / 10
+	end
+	return sum
+end
+
+function is_happy(n)
+	while true do
+		local total = dsum(n)
+
+		if total == HAPPY_NUMBER then
+			return 1
+		end
+		if total == SAD_NUMBER then
+			return 0
+		end
+
+		n = total
+	end
+end
Index: src/sys/modules/examples/luareadhappy/luareadhappy.c
diff -u /dev/null src/sys/modules/examples/luareadhappy/luareadhappy.c:1.1
--- /dev/null	Sat Apr 15 04:27:30 2017
+++ src/sys/modules/examples/luareadhappy/luareadhappy.c	Sat Apr 15 04:27:30 2017
@@ -0,0 +1,217 @@
+/*	$NetBSD: luareadhappy.c,v 1.1 2017/04/15 04:27:30 kamil 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: luareadhappy.c,v 1.1 2017/04/15 04:27:30 kamil Exp $");
+
+#include <sys/param.h>
+#include <sys/conf.h>
+#include <sys/device.h>
+#include <sys/kernel.h>
+#include <sys/lua.h>
+#include <sys/module.h>
+#include <lua.h>
+
+/*
+ * Create a device /dev/happy from which you can read sequential
+ * happy numbers.
+ *
+ * To use this device you need to do:
+ *     mknod /dev/happy c 210 0
+ *
+ * Commentary:
+ * 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).
+ *
+ * For more information on happy numbers, and the algorithms, see
+ *	http://en.wikipedia.org/wiki/Happy_number 
+ *
+ * The happy number generator is here only to have something that the user
+ * can read from our device.  Any other arbitrary data generator could
+ * have been used.  The algorithm is not critical to the implementation
+ * of the module.
+ */
+
+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;
+	klua_State	*kL;
+};
+
+static struct happy_softc sc;
+
+/* Function that calls a Lua routine and returns whether a number is happy */
+static int
+check_happy(unsigned n)
+{
+	int rv;
+
+	klua_lock(sc.kL);
+	lua_getglobal(sc.kL->L, "is_happy");
+
+        if (!lua_isfunction(sc.kL->L, -1)) {
+		lua_pop(sc.kL->L, 1);
+		klua_unlock(sc.kL);
+		return -1;
+        }
+
+        lua_pushnumber(sc.kL->L, n);
+        if (lua_pcall(sc.kL->L, 1 /* args */, 1 /* res */, 0) != 0) {
+		lua_pop(sc.kL->L, 2);
+		klua_unlock(sc.kL);
+		return -1;
+        }
+
+        if (!lua_isnumber(sc.kL->L, -1)) {
+		lua_pop(sc.kL->L, 1);
+		klua_unlock(sc.kL);
+		return -1;
+        }
+
+        rv = lua_tointeger(sc.kL->L, -1);
+
+        lua_pop(sc.kL->L, 1);
+        klua_unlock(sc.kL);
+
+	/* Consistency check */
+	if (rv != 0 && rv != 1)
+		rv = -1;
+
+	return rv;
+}
+
+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)
+{
+	int rv;
+	char line[80];
+
+	/* Get next happy number */
+	while ((rv = check_happy(++sc.last)) == 0)
+		continue;
+
+	/* Something went wrong */
+	if (rv == -1)
+		return ECANCELED;
+
+	/* 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, "lua");
+
+static int
+happy_modcmd(modcmd_t cmd, void *arg __unused)
+{
+	/* The major should be verified and changed if needed to avoid
+	 * conflicts with other devices. */
+	int cmajor = 210, bmajor = -1;
+
+	switch (cmd) {
+	case MODULE_CMD_INIT:
+		if (devsw_attach("happy", NULL, &bmajor, &happy_cdevsw,
+		                 &cmajor))
+			return ENXIO;
+		if ((sc.kL = kluaL_newstate("happy",
+		                            "Example Happy Number calculator",
+		                            IPL_NONE)) == NULL) {
+			devsw_detach(NULL, &happy_cdevsw);
+			return ENXIO;
+		}
+		return 0;
+	case MODULE_CMD_FINI:
+		if (sc.refcnt > 0)
+			return EBUSY;
+
+		klua_close(sc.kL);
+
+		devsw_detach(NULL, &happy_cdevsw);
+		return 0;
+	default:
+		return ENOTTY;
+	}
+}

Reply via email to