xref: /netbsd-src/sys/modules/examples/luareadhappy/luareadhappy.c (revision 67a548a800ad4f222e634c43037f2f08f1cde4bf)
1*67a548a8Skamil /*	$NetBSD: luareadhappy.c,v 1.2 2020/01/30 07:58:33 kamil Exp $	*/
2502c2ed2Skamil 
3502c2ed2Skamil /*-
4502c2ed2Skamil  * Copyright (c) 2015 The NetBSD Foundation, Inc.
5502c2ed2Skamil  * All rights reserved.
6502c2ed2Skamil  *
7502c2ed2Skamil  * Redistribution and use in source and binary forms, with or without
8502c2ed2Skamil  * modification, are permitted provided that the following conditions
9502c2ed2Skamil  * are met:
10502c2ed2Skamil  * 1. Redistributions of source code must retain the above copyright
11502c2ed2Skamil  *    notice, this list of conditions and the following disclaimer.
12502c2ed2Skamil  * 2. Redistributions in binary form must reproduce the above copyright
13502c2ed2Skamil  *    notice, this list of conditions and the following disclaimer in the
14502c2ed2Skamil  *    documentation and/or other materials provided with the distribution.
15502c2ed2Skamil  *
16502c2ed2Skamil  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17502c2ed2Skamil  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18502c2ed2Skamil  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19502c2ed2Skamil  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20502c2ed2Skamil  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21502c2ed2Skamil  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22502c2ed2Skamil  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23502c2ed2Skamil  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24502c2ed2Skamil  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25502c2ed2Skamil  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26502c2ed2Skamil  * POSSIBILITY OF SUCH DAMAGE.
27502c2ed2Skamil  */
28502c2ed2Skamil 
29502c2ed2Skamil #include <sys/cdefs.h>
30*67a548a8Skamil __KERNEL_RCSID(0, "$NetBSD: luareadhappy.c,v 1.2 2020/01/30 07:58:33 kamil Exp $");
31502c2ed2Skamil 
32502c2ed2Skamil #include <sys/param.h>
33502c2ed2Skamil #include <sys/conf.h>
34502c2ed2Skamil #include <sys/device.h>
35502c2ed2Skamil #include <sys/kernel.h>
36502c2ed2Skamil #include <sys/lua.h>
37502c2ed2Skamil #include <sys/module.h>
38502c2ed2Skamil #include <lua.h>
39502c2ed2Skamil 
40502c2ed2Skamil /*
41502c2ed2Skamil  * Create a device /dev/happy from which you can read sequential
42502c2ed2Skamil  * happy numbers.
43502c2ed2Skamil  *
44502c2ed2Skamil  * To use this device you need to do:
45*67a548a8Skamil  *     mknod /dev/happy c 351 0
46502c2ed2Skamil  *
47502c2ed2Skamil  * Commentary:
48502c2ed2Skamil  * A happy number is a number defined by the following process: Starting with
49502c2ed2Skamil  * any positive integer, replace the number by the sum of the squares of its
50502c2ed2Skamil  * digits, and repeat the process until the number equals 1 (where it will
51502c2ed2Skamil  * stay), or it loops endlessly in a cycle which does not include 1. Those
52502c2ed2Skamil  * numbers for which this process ends in 1 are happy numbers, while those that
53502c2ed2Skamil  * do not end in 1 are unhappy numbers (or sad numbers).
54502c2ed2Skamil  *
55502c2ed2Skamil  * For more information on happy numbers, and the algorithms, see
56502c2ed2Skamil  *	http://en.wikipedia.org/wiki/Happy_number
57502c2ed2Skamil  *
58502c2ed2Skamil  * The happy number generator is here only to have something that the user
59502c2ed2Skamil  * can read from our device.  Any other arbitrary data generator could
60502c2ed2Skamil  * have been used.  The algorithm is not critical to the implementation
61502c2ed2Skamil  * of the module.
62502c2ed2Skamil  */
63502c2ed2Skamil 
64502c2ed2Skamil dev_type_open(happy_open);
65502c2ed2Skamil dev_type_close(happy_close);
66502c2ed2Skamil dev_type_read(happy_read);
67502c2ed2Skamil 
68502c2ed2Skamil static struct cdevsw happy_cdevsw = {
69502c2ed2Skamil 	.d_open = happy_open,
70502c2ed2Skamil 	.d_close = happy_close,
71502c2ed2Skamil 	.d_read = happy_read,
72502c2ed2Skamil 	.d_write = nowrite,
73502c2ed2Skamil 	.d_ioctl = noioctl,
74502c2ed2Skamil 	.d_stop = nostop,
75502c2ed2Skamil 	.d_tty = notty,
76502c2ed2Skamil 	.d_poll = nopoll,
77502c2ed2Skamil 	.d_mmap = nommap,
78502c2ed2Skamil 	.d_kqfilter = nokqfilter,
79502c2ed2Skamil 	.d_discard = nodiscard,
80502c2ed2Skamil 	.d_flag = D_OTHER
81502c2ed2Skamil };
82502c2ed2Skamil 
83502c2ed2Skamil 
84502c2ed2Skamil struct happy_softc {
85502c2ed2Skamil 	int		 refcnt;
86502c2ed2Skamil 	unsigned	 last;
87502c2ed2Skamil 	klua_State	*kL;
88502c2ed2Skamil };
89502c2ed2Skamil 
90502c2ed2Skamil static struct happy_softc sc;
91502c2ed2Skamil 
92502c2ed2Skamil /* Function that calls a Lua routine and returns whether a number is happy */
93502c2ed2Skamil static int
check_happy(unsigned n)94502c2ed2Skamil check_happy(unsigned n)
95502c2ed2Skamil {
96502c2ed2Skamil 	int rv;
97502c2ed2Skamil 
98502c2ed2Skamil 	klua_lock(sc.kL);
99502c2ed2Skamil 	lua_getglobal(sc.kL->L, "is_happy");
100502c2ed2Skamil 
101502c2ed2Skamil         if (!lua_isfunction(sc.kL->L, -1)) {
102502c2ed2Skamil 		lua_pop(sc.kL->L, 1);
103502c2ed2Skamil 		klua_unlock(sc.kL);
104502c2ed2Skamil 		return -1;
105502c2ed2Skamil         }
106502c2ed2Skamil 
107502c2ed2Skamil         lua_pushnumber(sc.kL->L, n);
108502c2ed2Skamil         if (lua_pcall(sc.kL->L, 1 /* args */, 1 /* res */, 0) != 0) {
109502c2ed2Skamil 		lua_pop(sc.kL->L, 2);
110502c2ed2Skamil 		klua_unlock(sc.kL);
111502c2ed2Skamil 		return -1;
112502c2ed2Skamil         }
113502c2ed2Skamil 
114502c2ed2Skamil         if (!lua_isnumber(sc.kL->L, -1)) {
115502c2ed2Skamil 		lua_pop(sc.kL->L, 1);
116502c2ed2Skamil 		klua_unlock(sc.kL);
117502c2ed2Skamil 		return -1;
118502c2ed2Skamil         }
119502c2ed2Skamil 
120502c2ed2Skamil         rv = lua_tointeger(sc.kL->L, -1);
121502c2ed2Skamil 
122502c2ed2Skamil         lua_pop(sc.kL->L, 1);
123502c2ed2Skamil         klua_unlock(sc.kL);
124502c2ed2Skamil 
125502c2ed2Skamil 	/* Consistency check */
126502c2ed2Skamil 	if (rv != 0 && rv != 1)
127502c2ed2Skamil 		rv = -1;
128502c2ed2Skamil 
129502c2ed2Skamil 	return rv;
130502c2ed2Skamil }
131502c2ed2Skamil 
132502c2ed2Skamil int
happy_open(dev_t self __unused,int flag __unused,int mode __unused,struct lwp * l __unused)133502c2ed2Skamil happy_open(dev_t self __unused, int flag __unused, int mode __unused,
134502c2ed2Skamil            struct lwp *l __unused)
135502c2ed2Skamil {
136502c2ed2Skamil 	if (sc.refcnt > 0)
137502c2ed2Skamil 		return EBUSY;
138502c2ed2Skamil 
139502c2ed2Skamil 	sc.last = 0;
140502c2ed2Skamil 	++sc.refcnt;
141502c2ed2Skamil 
142502c2ed2Skamil 	return 0;
143502c2ed2Skamil }
144502c2ed2Skamil 
145502c2ed2Skamil int
happy_close(dev_t self __unused,int flag __unused,int mode __unused,struct lwp * l __unused)146502c2ed2Skamil happy_close(dev_t self __unused, int flag __unused, int mode __unused,
147502c2ed2Skamil             struct lwp *l __unused)
148502c2ed2Skamil {
149502c2ed2Skamil 	--sc.refcnt;
150502c2ed2Skamil 
151502c2ed2Skamil 	return 0;
152502c2ed2Skamil }
153502c2ed2Skamil 
154502c2ed2Skamil int
happy_read(dev_t self __unused,struct uio * uio,int flags __unused)155502c2ed2Skamil happy_read(dev_t self __unused, struct uio *uio, int flags __unused)
156502c2ed2Skamil {
157502c2ed2Skamil 	int rv;
158502c2ed2Skamil 	char line[80];
159502c2ed2Skamil 
160502c2ed2Skamil 	/* Get next happy number */
161502c2ed2Skamil 	while ((rv = check_happy(++sc.last)) == 0)
162502c2ed2Skamil 		continue;
163502c2ed2Skamil 
164502c2ed2Skamil 	/* Something went wrong */
165502c2ed2Skamil 	if (rv == -1)
166502c2ed2Skamil 		return ECANCELED;
167502c2ed2Skamil 
168502c2ed2Skamil 	/* Print it into line[] with trailing \n */
169502c2ed2Skamil 	int len = snprintf(line, sizeof(line), "%u\n", sc.last);
170502c2ed2Skamil 
171502c2ed2Skamil 	/* Is there room? */
172502c2ed2Skamil 	if (uio->uio_resid < len) {
173502c2ed2Skamil 		--sc.last; /* Step back */
174502c2ed2Skamil 		return EINVAL;
175502c2ed2Skamil 	}
176502c2ed2Skamil 
177502c2ed2Skamil 	/* Send it to User-Space */
178502c2ed2Skamil 	int e;
179502c2ed2Skamil 	if ((e = uiomove(line, len, uio)))
180502c2ed2Skamil 		return e;
181502c2ed2Skamil 
182502c2ed2Skamil 	return 0;
183502c2ed2Skamil }
184502c2ed2Skamil 
185502c2ed2Skamil MODULE(MODULE_CLASS_MISC, happy, "lua");
186502c2ed2Skamil 
187502c2ed2Skamil static int
happy_modcmd(modcmd_t cmd,void * arg __unused)188502c2ed2Skamil happy_modcmd(modcmd_t cmd, void *arg __unused)
189502c2ed2Skamil {
190502c2ed2Skamil 	/* The major should be verified and changed if needed to avoid
191502c2ed2Skamil 	 * conflicts with other devices. */
192*67a548a8Skamil 	int cmajor = 351, bmajor = -1;
193502c2ed2Skamil 
194502c2ed2Skamil 	switch (cmd) {
195502c2ed2Skamil 	case MODULE_CMD_INIT:
196502c2ed2Skamil 		if (devsw_attach("happy", NULL, &bmajor, &happy_cdevsw,
197502c2ed2Skamil 		                 &cmajor))
198502c2ed2Skamil 			return ENXIO;
199502c2ed2Skamil 		if ((sc.kL = kluaL_newstate("happy",
200502c2ed2Skamil 		                            "Example Happy Number calculator",
201502c2ed2Skamil 		                            IPL_NONE)) == NULL) {
202502c2ed2Skamil 			devsw_detach(NULL, &happy_cdevsw);
203502c2ed2Skamil 			return ENXIO;
204502c2ed2Skamil 		}
205502c2ed2Skamil 		return 0;
206502c2ed2Skamil 	case MODULE_CMD_FINI:
207502c2ed2Skamil 		if (sc.refcnt > 0)
208502c2ed2Skamil 			return EBUSY;
209502c2ed2Skamil 
210502c2ed2Skamil 		klua_close(sc.kL);
211502c2ed2Skamil 
212502c2ed2Skamil 		devsw_detach(NULL, &happy_cdevsw);
213502c2ed2Skamil 		return 0;
214502c2ed2Skamil 	default:
215502c2ed2Skamil 		return ENOTTY;
216502c2ed2Skamil 	}
217502c2ed2Skamil }
218