luareadhappy.c revision 1.1.20.1 1 1.1 kamil /* $NetBSD: luareadhappy.c,v 1.1.20.1 2020/04/08 14:08:55 martin Exp $ */
2 1.1 kamil
3 1.1 kamil /*-
4 1.1 kamil * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.1 kamil * All rights reserved.
6 1.1 kamil *
7 1.1 kamil * Redistribution and use in source and binary forms, with or without
8 1.1 kamil * modification, are permitted provided that the following conditions
9 1.1 kamil * are met:
10 1.1 kamil * 1. Redistributions of source code must retain the above copyright
11 1.1 kamil * notice, this list of conditions and the following disclaimer.
12 1.1 kamil * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 kamil * notice, this list of conditions and the following disclaimer in the
14 1.1 kamil * documentation and/or other materials provided with the distribution.
15 1.1 kamil *
16 1.1 kamil * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 kamil * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 kamil * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 kamil * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 kamil * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 kamil * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 kamil * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 kamil * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 kamil * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 kamil * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 kamil * POSSIBILITY OF SUCH DAMAGE.
27 1.1 kamil */
28 1.1 kamil
29 1.1 kamil #include <sys/cdefs.h>
30 1.1.20.1 martin __KERNEL_RCSID(0, "$NetBSD: luareadhappy.c,v 1.1.20.1 2020/04/08 14:08:55 martin Exp $");
31 1.1 kamil
32 1.1 kamil #include <sys/param.h>
33 1.1 kamil #include <sys/conf.h>
34 1.1 kamil #include <sys/device.h>
35 1.1 kamil #include <sys/kernel.h>
36 1.1 kamil #include <sys/lua.h>
37 1.1 kamil #include <sys/module.h>
38 1.1 kamil #include <lua.h>
39 1.1 kamil
40 1.1 kamil /*
41 1.1 kamil * Create a device /dev/happy from which you can read sequential
42 1.1 kamil * happy numbers.
43 1.1 kamil *
44 1.1 kamil * To use this device you need to do:
45 1.1.20.1 martin * mknod /dev/happy c 351 0
46 1.1 kamil *
47 1.1 kamil * Commentary:
48 1.1 kamil * A happy number is a number defined by the following process: Starting with
49 1.1 kamil * any positive integer, replace the number by the sum of the squares of its
50 1.1 kamil * digits, and repeat the process until the number equals 1 (where it will
51 1.1 kamil * stay), or it loops endlessly in a cycle which does not include 1. Those
52 1.1 kamil * numbers for which this process ends in 1 are happy numbers, while those that
53 1.1 kamil * do not end in 1 are unhappy numbers (or sad numbers).
54 1.1 kamil *
55 1.1 kamil * For more information on happy numbers, and the algorithms, see
56 1.1 kamil * http://en.wikipedia.org/wiki/Happy_number
57 1.1 kamil *
58 1.1 kamil * The happy number generator is here only to have something that the user
59 1.1 kamil * can read from our device. Any other arbitrary data generator could
60 1.1 kamil * have been used. The algorithm is not critical to the implementation
61 1.1 kamil * of the module.
62 1.1 kamil */
63 1.1 kamil
64 1.1 kamil dev_type_open(happy_open);
65 1.1 kamil dev_type_close(happy_close);
66 1.1 kamil dev_type_read(happy_read);
67 1.1 kamil
68 1.1 kamil static struct cdevsw happy_cdevsw = {
69 1.1 kamil .d_open = happy_open,
70 1.1 kamil .d_close = happy_close,
71 1.1 kamil .d_read = happy_read,
72 1.1 kamil .d_write = nowrite,
73 1.1 kamil .d_ioctl = noioctl,
74 1.1 kamil .d_stop = nostop,
75 1.1 kamil .d_tty = notty,
76 1.1 kamil .d_poll = nopoll,
77 1.1 kamil .d_mmap = nommap,
78 1.1 kamil .d_kqfilter = nokqfilter,
79 1.1 kamil .d_discard = nodiscard,
80 1.1 kamil .d_flag = D_OTHER
81 1.1 kamil };
82 1.1 kamil
83 1.1 kamil
84 1.1 kamil struct happy_softc {
85 1.1 kamil int refcnt;
86 1.1 kamil unsigned last;
87 1.1 kamil klua_State *kL;
88 1.1 kamil };
89 1.1 kamil
90 1.1 kamil static struct happy_softc sc;
91 1.1 kamil
92 1.1 kamil /* Function that calls a Lua routine and returns whether a number is happy */
93 1.1 kamil static int
94 1.1 kamil check_happy(unsigned n)
95 1.1 kamil {
96 1.1 kamil int rv;
97 1.1 kamil
98 1.1 kamil klua_lock(sc.kL);
99 1.1 kamil lua_getglobal(sc.kL->L, "is_happy");
100 1.1 kamil
101 1.1 kamil if (!lua_isfunction(sc.kL->L, -1)) {
102 1.1 kamil lua_pop(sc.kL->L, 1);
103 1.1 kamil klua_unlock(sc.kL);
104 1.1 kamil return -1;
105 1.1 kamil }
106 1.1 kamil
107 1.1 kamil lua_pushnumber(sc.kL->L, n);
108 1.1 kamil if (lua_pcall(sc.kL->L, 1 /* args */, 1 /* res */, 0) != 0) {
109 1.1 kamil lua_pop(sc.kL->L, 2);
110 1.1 kamil klua_unlock(sc.kL);
111 1.1 kamil return -1;
112 1.1 kamil }
113 1.1 kamil
114 1.1 kamil if (!lua_isnumber(sc.kL->L, -1)) {
115 1.1 kamil lua_pop(sc.kL->L, 1);
116 1.1 kamil klua_unlock(sc.kL);
117 1.1 kamil return -1;
118 1.1 kamil }
119 1.1 kamil
120 1.1 kamil rv = lua_tointeger(sc.kL->L, -1);
121 1.1 kamil
122 1.1 kamil lua_pop(sc.kL->L, 1);
123 1.1 kamil klua_unlock(sc.kL);
124 1.1 kamil
125 1.1 kamil /* Consistency check */
126 1.1 kamil if (rv != 0 && rv != 1)
127 1.1 kamil rv = -1;
128 1.1 kamil
129 1.1 kamil return rv;
130 1.1 kamil }
131 1.1 kamil
132 1.1 kamil int
133 1.1 kamil happy_open(dev_t self __unused, int flag __unused, int mode __unused,
134 1.1 kamil struct lwp *l __unused)
135 1.1 kamil {
136 1.1 kamil if (sc.refcnt > 0)
137 1.1 kamil return EBUSY;
138 1.1 kamil
139 1.1 kamil sc.last = 0;
140 1.1 kamil ++sc.refcnt;
141 1.1 kamil
142 1.1 kamil return 0;
143 1.1 kamil }
144 1.1 kamil
145 1.1 kamil int
146 1.1 kamil happy_close(dev_t self __unused, int flag __unused, int mode __unused,
147 1.1 kamil struct lwp *l __unused)
148 1.1 kamil {
149 1.1 kamil --sc.refcnt;
150 1.1 kamil
151 1.1 kamil return 0;
152 1.1 kamil }
153 1.1 kamil
154 1.1 kamil int
155 1.1 kamil happy_read(dev_t self __unused, struct uio *uio, int flags __unused)
156 1.1 kamil {
157 1.1 kamil int rv;
158 1.1 kamil char line[80];
159 1.1 kamil
160 1.1 kamil /* Get next happy number */
161 1.1 kamil while ((rv = check_happy(++sc.last)) == 0)
162 1.1 kamil continue;
163 1.1 kamil
164 1.1 kamil /* Something went wrong */
165 1.1 kamil if (rv == -1)
166 1.1 kamil return ECANCELED;
167 1.1 kamil
168 1.1 kamil /* Print it into line[] with trailing \n */
169 1.1 kamil int len = snprintf(line, sizeof(line), "%u\n", sc.last);
170 1.1 kamil
171 1.1 kamil /* Is there room? */
172 1.1 kamil if (uio->uio_resid < len) {
173 1.1 kamil --sc.last; /* Step back */
174 1.1 kamil return EINVAL;
175 1.1 kamil }
176 1.1 kamil
177 1.1 kamil /* Send it to User-Space */
178 1.1 kamil int e;
179 1.1 kamil if ((e = uiomove(line, len, uio)))
180 1.1 kamil return e;
181 1.1 kamil
182 1.1 kamil return 0;
183 1.1 kamil }
184 1.1 kamil
185 1.1 kamil MODULE(MODULE_CLASS_MISC, happy, "lua");
186 1.1 kamil
187 1.1 kamil static int
188 1.1 kamil happy_modcmd(modcmd_t cmd, void *arg __unused)
189 1.1 kamil {
190 1.1 kamil /* The major should be verified and changed if needed to avoid
191 1.1 kamil * conflicts with other devices. */
192 1.1.20.1 martin int cmajor = 351, bmajor = -1;
193 1.1 kamil
194 1.1 kamil switch (cmd) {
195 1.1 kamil case MODULE_CMD_INIT:
196 1.1 kamil if (devsw_attach("happy", NULL, &bmajor, &happy_cdevsw,
197 1.1 kamil &cmajor))
198 1.1 kamil return ENXIO;
199 1.1 kamil if ((sc.kL = kluaL_newstate("happy",
200 1.1 kamil "Example Happy Number calculator",
201 1.1 kamil IPL_NONE)) == NULL) {
202 1.1 kamil devsw_detach(NULL, &happy_cdevsw);
203 1.1 kamil return ENXIO;
204 1.1 kamil }
205 1.1 kamil return 0;
206 1.1 kamil case MODULE_CMD_FINI:
207 1.1 kamil if (sc.refcnt > 0)
208 1.1 kamil return EBUSY;
209 1.1 kamil
210 1.1 kamil klua_close(sc.kL);
211 1.1 kamil
212 1.1 kamil devsw_detach(NULL, &happy_cdevsw);
213 1.1 kamil return 0;
214 1.1 kamil default:
215 1.1 kamil return ENOTTY;
216 1.1 kamil }
217 1.1 kamil }
218