xref: /netbsd-src/sys/modules/lua/lua.c (revision eaac9e3d28859567590b7fef63a96c293f76dbf9)
1 /*	$NetBSD: lua.c,v 1.6 2013/12/04 02:12:01 lneto Exp $ */
2 
3 /*
4  * Copyright (c) 2011, 2013 by Marc Balmer <mbalmer@NetBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the Author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /* Lua device driver */
32 
33 #include <sys/param.h>
34 #include <sys/fcntl.h>
35 #include <sys/conf.h>
36 #include <sys/condvar.h>
37 #include <sys/device.h>
38 #include <sys/ioctl.h>
39 #include <sys/kmem.h>
40 #include <sys/lock.h>
41 #include <sys/lua.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/namei.h>
45 #include <sys/queue.h>
46 #include <sys/sysctl.h>
47 #include <sys/vnode.h>
48 
49 #include <lauxlib.h>
50 
51 #include "luavar.h"
52 
53 struct lua_softc {
54 	device_t		 sc_dev;
55 
56 	kmutex_t		 sc_lock;
57 	kcondvar_t		 sc_inuse_cv;
58 	bool			 sc_inuse;
59 
60 	/* Locking access to state queues */
61 	kmutex_t		 sc_state_lock;
62 	kcondvar_t		 sc_state_cv;
63 	bool			 sc_state;
64 
65 	struct sysctllog	*sc_log;
66 };
67 
68 static device_t	sc_self;
69 static bool	lua_autoload_on = true;
70 static bool	lua_require_on = true;
71 static bool	lua_bytecode_on = false;
72 static int	lua_verbose;
73 static int	lua_max_instr;
74 
75 static LIST_HEAD(, lua_state)	lua_states;
76 static LIST_HEAD(, lua_module)	lua_modules;
77 
78 static int lua_match(device_t, cfdata_t, void *);
79 static void lua_attach(device_t, device_t, void *);
80 static int lua_detach(device_t, int);
81 static klua_State *klua_find(const char *);
82 static const char *lua_reader(lua_State *, void *, size_t *);
83 static void lua_maxcount(lua_State *, lua_Debug *);
84 
85 static int lua_require(lua_State *);
86 
87 CFATTACH_DECL_NEW(lua, sizeof(struct lua_softc),
88 	lua_match, lua_attach, lua_detach, NULL);
89 
90 dev_type_open(luaopen);
91 dev_type_close(luaclose);
92 dev_type_ioctl(luaioctl);
93 
94 const struct cdevsw lua_cdevsw = {
95 	luaopen, luaclose, noread, nowrite, luaioctl, nostop, notty,
96 	nopoll, nommap, nokqfilter, D_OTHER | D_MPSAFE
97 };
98 
99 struct lua_loadstate {
100 	struct vnode	*vp;
101 	size_t		 size;
102 	off_t		 off;
103 };
104 
105 extern struct cfdriver lua_cd;
106 
107 static int
108 lua_match(device_t parent, cfdata_t match, void *aux)
109 {
110 	return 1;
111 }
112 
113 static void
114 lua_attach(device_t parent, device_t self, void *aux)
115 {
116 	struct lua_softc *sc;
117 	const struct sysctlnode *node;
118 
119 	if (sc_self)
120 		return;
121 
122 	sc = device_private(self);
123 	sc->sc_dev = self;
124 	sc_self = self;
125 
126 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
127 	cv_init(&sc->sc_inuse_cv, "luactl");
128 
129 	mutex_init(&sc->sc_state_lock, MUTEX_DEFAULT, IPL_VM);
130 	cv_init(&sc->sc_state_cv, "luastate");
131 
132 	pmf_device_register(self, NULL, NULL);
133 
134 	/* Sysctl to provide some control over behaviour */
135 	sysctl_createv(NULL, 0, NULL, NULL,
136             CTLFLAG_PERMANENT,
137             CTLTYPE_NODE, "kern", NULL,
138             NULL, 0, NULL, 0,
139             CTL_KERN, CTL_EOL);
140         sysctl_createv(&sc->sc_log, 0, NULL, &node,
141             CTLFLAG_OWNDESC,
142             CTLTYPE_NODE, "lua",
143             SYSCTL_DESCR("Lua options"),
144             NULL, 0, NULL, 0,
145             CTL_KERN, CTL_CREATE, CTL_EOL);
146 
147         if (node == NULL) {
148 		printf(": can't create sysctl node\n");
149                 return;
150 	}
151 
152 	/*
153 	 * XXX Some of the sysctl values must not be changed after the
154 	 * securelevel has been raised.
155 	 */
156         sysctl_createv(&sc->sc_log, 0, &node, NULL,
157             CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
158             CTLTYPE_BOOL, "require",
159             SYSCTL_DESCR("Enable the require command"),
160             NULL, 0, &lua_require_on, 0,
161 	    CTL_CREATE, CTL_EOL);
162 
163         sysctl_createv(&sc->sc_log, 0, &node, NULL,
164             CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
165             CTLTYPE_BOOL, "autoload",
166             SYSCTL_DESCR("Enable automatic load of modules"),
167             NULL, 0, &lua_autoload_on, 0,
168 	    CTL_CREATE, CTL_EOL);
169 
170         sysctl_createv(&sc->sc_log, 0, &node, NULL,
171             CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
172             CTLTYPE_BOOL, "bytecode",
173             SYSCTL_DESCR("Enable loading of bytecode"),
174             NULL, 0, &lua_bytecode_on, 0,
175 	    CTL_CREATE, CTL_EOL);
176 
177         sysctl_createv(&sc->sc_log, 0, &node, NULL,
178             CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
179             CTLTYPE_INT, "verbose",
180             SYSCTL_DESCR("Enable verbose output"),
181             NULL, 0, &lua_verbose, 0,
182 	    CTL_CREATE, CTL_EOL);
183 
184         sysctl_createv(&sc->sc_log, 0, &node, NULL,
185             CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
186             CTLTYPE_INT, "maxcount",
187             SYSCTL_DESCR("Limit maximum instruction count"),
188             NULL, 0, &lua_max_instr, 0,
189 	    CTL_CREATE, CTL_EOL);
190 
191 	aprint_normal_dev(self, "%s  %s\n", LUA_RELEASE, LUA_COPYRIGHT);
192 }
193 
194 static int
195 lua_detach(device_t self, int flags)
196 {
197 	struct lua_softc *sc;
198 	struct lua_state *s;
199 
200 	sc = device_private(self);
201 	pmf_device_deregister(self);
202 
203 	if (sc->sc_log != NULL) {
204 		sysctl_teardown(&sc->sc_log);
205 		sc->sc_log = NULL;
206 	}
207 
208 	/* Traverse the list of states and close them */
209 	while ((s = LIST_FIRST(&lua_states)) != NULL) {
210 		LIST_REMOVE(s, lua_next);
211 		klua_close(s->K);
212 		if (lua_verbose)
213 			device_printf(self, "state %s destroyed\n",
214 			    s->lua_name);
215 		kmem_free(s, sizeof(struct lua_state));
216 	}
217 	mutex_destroy(&sc->sc_lock);
218 	cv_destroy(&sc->sc_inuse_cv);
219 	mutex_destroy(&sc->sc_state_lock);
220 	cv_destroy(&sc->sc_state_cv);
221 	sc_self = NULL;
222 	return 0;
223 }
224 
225 int
226 luaopen(dev_t dev, int flag, int mode, struct lwp *l)
227 {
228 	struct lua_softc *sc;
229 	int error = 0;
230 
231 	if (minor(dev) > 0)
232 		return ENXIO;
233 
234 	sc = device_lookup_private(&lua_cd, minor(dev));
235 	if (sc == NULL)
236 		return ENXIO;
237 
238 	mutex_enter(&sc->sc_lock);
239 	while (sc->sc_inuse == true) {
240 		error = cv_wait_sig(&sc->sc_inuse_cv, &sc->sc_lock);
241 		if (error)
242 			break;
243 	}
244 	if (!error)
245 		sc->sc_inuse = true;
246 	mutex_exit(&sc->sc_lock);
247 
248 	if (error)
249 		return error;
250 	return 0;
251 }
252 
253 int
254 luaclose(dev_t dev, int flag, int mode, struct lwp *l)
255 {
256 	struct lua_softc *sc;
257 
258 	if (minor(dev) > 0)
259 		return ENXIO;
260 	sc = device_lookup_private(&lua_cd, minor(dev));
261 	mutex_enter(&sc->sc_lock);
262 	sc->sc_inuse = false;
263 	cv_signal(&sc->sc_inuse_cv);
264 	mutex_exit(&sc->sc_lock);
265 	return 0;
266 }
267 
268 int
269 luaioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
270 {
271 	struct lua_softc *sc;
272 	struct lua_info *info;
273 	struct lua_create *create;
274 	struct lua_require *require;
275 	struct lua_load *load;
276 	struct lua_state *s;
277 	struct lua_module *m;
278 	kauth_cred_t cred;
279 	struct nameidata nd;
280 	struct pathbuf *pb;
281 	struct vattr va;
282 	struct lua_loadstate ls;
283 	int error, n;
284 	klua_State *K;
285 
286 	sc = device_lookup_private(&lua_cd, minor(dev));
287 	if (!device_is_active(sc->sc_dev))
288 		return EBUSY;
289 
290 	switch (cmd) {
291 	case LUAINFO:
292 		info = data;
293 		if (info->states == NULL) {
294 			info->num_states = 0;
295 			LIST_FOREACH(s, &lua_states, lua_next)
296 				info->num_states++;
297 		} else {
298 			n = 0;
299 			LIST_FOREACH(s, &lua_states, lua_next) {
300 				if (n > info->num_states)
301 					break;
302 				copyoutstr(s->lua_name, info->states[n].name,
303 				    MAX_LUA_NAME, NULL);
304 				copyoutstr(s->lua_desc, info->states[n].desc,
305 				    MAX_LUA_DESC, NULL);
306 				info->states[n].user = s->K->ks_user;
307 				n++;
308 			}
309 			info->num_states = n;
310 		}
311 		break;
312 	case LUACREATE:
313 		create = data;
314 
315 		if (*create->name == '_') {
316 			if (lua_verbose)
317 				device_printf(sc->sc_dev, "names of user "
318 				    "created states must not begin with '_'");
319 				return ENXIO;
320 		}
321 		LIST_FOREACH(s, &lua_states, lua_next)
322 			if (!strcmp(s->lua_name, create->name)) {
323 				if (lua_verbose)
324 					device_printf(sc->sc_dev,
325 					    "state %s exists\n", create->name);
326 				return EBUSY;
327 			}
328 
329 		K = klua_newstate(lua_alloc, NULL, create->name,
330 		    create->desc);
331 		K->ks_user = true;
332 
333 		if (K == NULL)
334 			return ENOMEM;
335 		if (lua_verbose)
336 			device_printf(sc->sc_dev, "state %s created\n",
337 			    create->name);
338 		break;
339 	case LUADESTROY:
340 		create = data;
341 
342 		K = klua_find(create->name);
343 
344 		if (K != NULL && (K->ks_user == true)) {
345 			klua_close(K);
346 			return 0;
347 		}
348 		return EBUSY;
349 	case LUAREQUIRE:	/* 'require' a module in a State */
350 		require = data;
351 		LIST_FOREACH(s, &lua_states, lua_next)
352 			if (!strcmp(s->lua_name, require->state))
353 				LIST_FOREACH(m, &lua_modules, mod_next)
354 					if (!strcmp(m->mod_name,
355 					    require->module)) {
356 					    	if (lua_verbose)
357 						    	device_printf(
358 						    	    sc->sc_dev,
359 					    		    "requiring module "
360 					    		    "%s to state %s\n",
361 					    		    m->mod_name,
362 					    		    s->lua_name);
363 					    	m->open(s->K->L);
364 					    	m->refcount++;
365 					    	LIST_INSERT_HEAD(
366 					    	    &s->lua_modules, m,
367 					    	    mod_next);
368 					    	return 0;
369 					}
370 		return ENXIO;
371 	case LUALOAD:
372 		load = data;
373 		if (strrchr(load->path, '/') == NULL)
374 			return ENXIO;
375 
376 		LIST_FOREACH(s, &lua_states, lua_next)
377 			if (!strcmp(s->lua_name, load->state)) {
378 				if (lua_verbose)
379 					device_printf(sc->sc_dev,
380 					    "loading %s into state %s\n",
381 					    load->path, s->lua_name);
382 				cred = kauth_cred_get();
383 				pb = pathbuf_create(load->path);
384 				if (pb == NULL)
385 					return ENOMEM;
386 				NDINIT(&nd, LOOKUP, FOLLOW | NOCHROOT, pb);
387 				pathbuf_destroy(pb);
388 				error = vn_open(&nd, FREAD, 0);
389 				if (error) {
390 					if (lua_verbose)
391 						device_printf(sc->sc_dev,
392 						    "error vn_open %d\n",
393 						    error);
394 					return error;
395 				}
396 				error = VOP_GETATTR(nd.ni_vp, &va,
397 				    kauth_cred_get());
398 				if (error) {
399 					VOP_UNLOCK(nd.ni_vp);
400 					vn_close(nd.ni_vp, FREAD,
401 					    kauth_cred_get());
402 					if (lua_verbose)
403 						device_printf(sc->sc_dev,
404 						    "erro VOP_GETATTR %d\n",
405 						    error);
406 					return error;
407 				}
408 				if (va.va_type != VREG) {
409 					VOP_UNLOCK(nd.ni_vp);
410 					vn_close(nd.ni_vp, FREAD,
411 					    kauth_cred_get());
412 					return EINVAL;
413 				}
414 				ls.vp = nd.ni_vp;
415 				ls.off = 0L;
416 				ls.size = va.va_size;
417 				VOP_UNLOCK(nd.ni_vp);
418 				error = lua_load(s->K->L, lua_reader, &ls,
419 				    strrchr(load->path, '/') + 1);
420 				vn_close(nd.ni_vp, FREAD, cred);
421 				switch (error) {
422 				case 0:	/* no error */
423 					break;
424 				case LUA_ERRSYNTAX:
425 					if (lua_verbose)
426 						device_printf(sc->sc_dev,
427 						    "syntax error\n");
428 					return EINVAL;
429 				case LUA_ERRMEM:
430 					if (lua_verbose)
431 						device_printf(sc->sc_dev,
432 						    "memory error\n");
433 					return ENOMEM;
434 				default:
435 					if (lua_verbose)
436 						device_printf(sc->sc_dev,
437 						    "load error %d: %s\n",
438 						    error,
439 						    lua_tostring(s->K->L, -1));
440 					return EINVAL;
441 				}
442 				if (lua_max_instr > 0)
443 					lua_sethook(s->K->L, lua_maxcount,
444 					    LUA_MASKCOUNT, lua_max_instr);
445 				error = lua_pcall(s->K->L, 0, LUA_MULTRET, 0);
446 				if (error) {
447 					if (lua_verbose) {
448 						device_printf(sc->sc_dev,
449 						    "execution error: %s\n",
450 						    lua_tostring(s->K->L, -1));
451 					}
452 					return EINVAL;
453 				}
454 				return 0;
455 			}
456 		return ENXIO;
457 	}
458 	return 0;
459 }
460 
461 static int
462 lua_require(lua_State *L)
463 {
464 	struct lua_state *s;
465 	struct lua_module *m, *md;
466 	const char *module;
467 	char name[MAXPATHLEN];
468 
469 	module = lua_tostring(L, -1);
470 	md = NULL;
471 	LIST_FOREACH(m, &lua_modules, mod_next)
472 		if (!strcmp(m->mod_name, module)) {
473 			md = m;
474 			break;
475 		}
476 
477 	if (md == NULL && lua_autoload_on && strchr(module, '/') == NULL) {
478 		snprintf(name, sizeof name, "lua%s", module);
479 		if (lua_verbose)
480 			device_printf(sc_self, "autoload %s\n", name);
481 		module_autoload(name, MODULE_CLASS_MISC);
482 		LIST_FOREACH(m, &lua_modules, mod_next)
483 			if (!strcmp(m->mod_name, module)) {
484 				md = m;
485 				break;
486 			}
487 	}
488 
489 	if (md != NULL)
490 		LIST_FOREACH(s, &lua_states, lua_next)
491 			if (s->K->L == L) {
492 				if (lua_verbose)
493 					device_printf(sc_self,
494 					    "require module %s\n",
495 					    md->mod_name);
496 				md->open(L);
497 				md->refcount++;
498 				LIST_INSERT_HEAD(&s->lua_modules, md, mod_next);
499 				return 0;
500 			}
501 
502 	lua_pushstring(L, "module not found");
503 	return lua_error(L);
504 }
505 
506 void *
507 lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
508 {
509 	void *nptr;
510 
511 	if (nsize == 0) {
512 		nptr = NULL;
513 		if (ptr != NULL)
514 			kmem_free(ptr, osize);
515 	} else {
516 		nptr = kmem_alloc(nsize, KM_SLEEP);
517 		if (ptr != NULL) {
518 			memcpy(nptr, ptr, osize < nsize ? osize : nsize);
519 			kmem_free(ptr, osize);
520 		}
521 	}
522 	return nptr;
523 }
524 
525 static const char *
526 lua_reader(lua_State *L, void *data, size_t *size)
527 {
528 	struct lua_loadstate *ls;
529 	static char buf[1024];
530 	size_t rsiz;
531 
532 	ls = data;
533 	if (ls->size < sizeof(buf))
534 		rsiz = ls->size;
535 	else
536 		rsiz = sizeof(buf);
537 	vn_rdwr(UIO_READ, ls->vp, buf, rsiz, ls->off, UIO_SYSSPACE,
538 	    0, curlwp->l_cred, NULL, curlwp);
539 	if (ls->off == 0L && lua_bytecode_on == false && buf[0] == 0x1b) {
540 		*size = 0L;
541 		lua_pushstring(L, "loading of bytecode is not allowed");
542 		lua_error(L);
543 		return NULL;
544 	} else {
545 		*size = rsiz;
546 		ls->off += *size;
547 		ls->size -= *size;
548 	}
549 	return buf;
550 }
551 
552 static void
553 lua_maxcount(lua_State *L, lua_Debug *d)
554 {
555 	lua_pushstring(L, "maximum instruction count exceeded");
556 	lua_error(L);
557 }
558 
559 int
560 lua_mod_register(const char *name, int (*open)(void *))
561 {
562 	struct lua_module *m;
563 
564 	LIST_FOREACH(m, &lua_modules, mod_next)
565 		if (!strcmp(m->mod_name, name))
566 			return EBUSY;
567 	m = kmem_zalloc(sizeof(struct lua_module), KM_SLEEP);
568 	strlcpy(m->mod_name, name, LUA_MAX_MODNAME);
569 	m->open = open;
570 	m->refcount = 0;
571 	LIST_INSERT_HEAD(&lua_modules, m, mod_next);
572 	if (lua_verbose)
573 		device_printf(sc_self, "registered lua module %s\n", name);
574 	return 0;
575 }
576 
577 int
578 lua_mod_unregister(const char *name)
579 {
580 	struct lua_module *m;
581 
582 	LIST_FOREACH(m, &lua_modules, mod_next)
583 		if (!strcmp(m->mod_name, name)) {
584 			if (m->refcount == 0) {
585 				LIST_REMOVE(m, mod_next);
586 				kmem_free(m, sizeof(struct lua_module));
587 				if (lua_verbose)
588 					device_printf(sc_self,
589 					    "unregistered lua module %s\n",
590 					    name);
591 				return 0;
592 			} else
593 				return EBUSY;
594 		}
595 	return 0;
596 }
597 
598 klua_State *
599 klua_newstate(lua_Alloc f, void *ud, const char *name, const char *desc)
600 {
601 	klua_State *K;
602 	struct lua_state *s;
603 	struct lua_softc *sc;
604 	int error = 0;
605 
606 	s = kmem_zalloc(sizeof(struct lua_state), KM_SLEEP);
607 	sc = device_private(sc_self);
608 	mutex_enter(&sc->sc_state_lock);
609 	while (sc->sc_state == true) {
610 		error = cv_wait_sig(&sc->sc_state_cv, &sc->sc_state_lock);
611 		if (error)
612 			break;
613 	}
614 	if (!error)
615 		sc->sc_state = true;
616 	mutex_exit(&sc->sc_state_lock);
617 
618 	if (error)
619 		return NULL;
620 
621 	K = kmem_zalloc(sizeof(klua_State), KM_SLEEP);
622 	K->L = lua_newstate(f, ud);
623 	K->ks_user = false;
624 	if (K->L == NULL) {
625 		kmem_free(K, sizeof(klua_State));
626 		K = NULL;
627 		goto finish;
628 	}
629 
630 	strlcpy(s->lua_name, name, MAX_LUA_NAME);
631 	strlcpy(s->lua_desc, desc, MAX_LUA_DESC);
632 	s->K = K;
633 
634 	if (lua_require_on || lua_autoload_on) {
635 		lua_pushcfunction(K->L, lua_require);
636 		lua_setglobal(K->L, "require");
637 	}
638 	LIST_INSERT_HEAD(&lua_states, s, lua_next);
639 
640 	mutex_init(&K->ks_lock, MUTEX_DEFAULT, IPL_VM);
641 	cv_init(&K->ks_inuse_cv, "luainuse");
642 
643 finish:
644 	mutex_enter(&sc->sc_state_lock);
645 	sc->sc_state = false;
646 	cv_signal(&sc->sc_state_cv);
647 	mutex_exit(&sc->sc_state_lock);
648 	return K;
649 }
650 
651 void
652 klua_close(klua_State *K)
653 {
654 	struct lua_state *s;
655 	struct lua_softc *sc;
656 	struct lua_module *m;
657 	int error = 0;
658 
659 	/* Notify the Lua state that it is about to be closed */
660 	if (klua_lock(K))
661 		return;		/* Nothing we can do about */
662 
663 	lua_getglobal(K->L, "onClose");
664 	if (lua_isfunction(K->L, -1))
665 		lua_pcall(K->L, -1, 0, 0);
666 
667 	/*
668 	 * Don't unlock, make sure no one uses the state until it is destroyed
669 	 * klua_unlock(K);
670 	 */
671 
672 	sc = device_private(sc_self);
673 	mutex_enter(&sc->sc_state_lock);
674 	while (sc->sc_state == true) {
675 		error = cv_wait_sig(&sc->sc_state_cv, &sc->sc_state_lock);
676 		if (error)
677 			break;
678 	}
679 	if (!error)
680 		sc->sc_state = true;
681 	mutex_exit(&sc->sc_state_lock);
682 
683 	if (error)
684 		return;		/* Nothing we can do... */
685 
686 	LIST_FOREACH(s, &lua_states, lua_next)
687 		if (s->K == K) {
688 			LIST_REMOVE(s, lua_next);
689 			LIST_FOREACH(m, &s->lua_modules, mod_next)
690 				m->refcount--;
691 			kmem_free(s, sizeof(struct lua_state));
692 		}
693 
694 	lua_close(K->L);
695 	cv_destroy(&K->ks_inuse_cv);
696 	mutex_destroy(&K->ks_lock);
697 	kmem_free(K, sizeof(klua_State));
698 
699 	mutex_enter(&sc->sc_state_lock);
700 	sc->sc_state = false;
701 	cv_signal(&sc->sc_state_cv);
702 	mutex_exit(&sc->sc_state_lock);
703 }
704 
705 static klua_State *
706 klua_find(const char *name)
707 {
708 	struct lua_state *s;
709 	struct lua_softc *sc;
710 	klua_State *K;
711 	int error = 0;
712 
713 	K = NULL;
714 	sc = device_private(sc_self);
715 	mutex_enter(&sc->sc_state_lock);
716 	while (sc->sc_state == true) {
717 		error = cv_wait_sig(&sc->sc_state_cv, &sc->sc_state_lock);
718 		if (error)
719 			break;
720 	}
721 	if (!error)
722 		sc->sc_state = true;
723 	mutex_exit(&sc->sc_state_lock);
724 
725 	if (error)
726 		return NULL;
727 
728 	LIST_FOREACH(s, &lua_states, lua_next)
729 		if (!strcmp(s->lua_name, name)) {
730 			K = s->K;
731 			break;
732 		}
733 
734 	mutex_enter(&sc->sc_state_lock);
735 	sc->sc_state = false;
736 	cv_signal(&sc->sc_state_cv);
737 	mutex_exit(&sc->sc_state_lock);
738 	return K;
739 }
740 
741 int
742 klua_lock(klua_State *K)
743 {
744 	int error;
745 
746 	error = 0;
747 	mutex_enter(&K->ks_lock);
748 	while (K->ks_inuse == true) {
749 		error = cv_wait_sig(&K->ks_inuse_cv, &K->ks_lock);
750 		if (error)
751 			break;
752 	}
753 	if (!error)
754 		K->ks_inuse = true;
755 	mutex_exit(&K->ks_lock);
756 
757 	if (error)
758 		return error;
759 	return 0;
760 }
761 
762 void
763 klua_unlock(klua_State *K)
764 {
765 	mutex_enter(&K->ks_lock);
766 	K->ks_inuse = false;
767 	cv_signal(&K->ks_inuse_cv);
768 	mutex_exit(&K->ks_lock);
769 }
770 
771 MODULE(MODULE_CLASS_MISC, lua, NULL);
772 
773 #ifdef _MODULE
774 static const struct cfiattrdata luabus_iattrdata = {
775 	"luabus", 0, { { NULL, NULL, 0 },}
776 };
777 static const struct cfiattrdata *const lua_attrs[] = {
778 	&luabus_iattrdata, NULL
779 };
780 CFDRIVER_DECL(lua, DV_DULL, lua_attrs);
781 extern struct cfattach lua_ca;
782 static int lualoc[] = {
783 	-1,
784 	-1,
785 	-1
786 };
787 static struct cfdata lua_cfdata[] = {
788 	{
789 		.cf_name = "lua",
790 		.cf_atname = "lua",
791 		.cf_unit = 0,
792 		.cf_fstate = FSTATE_STAR,
793 		.cf_loc = lualoc,
794 		.cf_flags = 0,
795 		.cf_pspec = NULL,
796 	},
797 	{ NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL }
798 };
799 #endif
800 
801 static int
802 lua_modcmd(modcmd_t cmd, void *opaque)
803 {
804 #ifdef _MODULE
805 	devmajor_t cmajor, bmajor;
806 	int error = 0;
807 
808 	cmajor = bmajor = NODEVMAJOR;
809 #endif
810 	switch (cmd) {
811 	case MODULE_CMD_INIT:
812 #ifdef _MODULE
813 		error = config_cfdriver_attach(&lua_cd);
814 		if (error)
815 			return error;
816 
817 		error = config_cfattach_attach(lua_cd.cd_name,
818 		    &lua_ca);
819 		if (error) {
820 			config_cfdriver_detach(&lua_cd);
821 			aprint_error("%s: unable to register cfattach\n",
822 			    lua_cd.cd_name);
823 			return error;
824 		}
825 		error = config_cfdata_attach(lua_cfdata, 1);
826 		if (error) {
827 			config_cfattach_detach(lua_cd.cd_name,
828 			    &lua_ca);
829 			config_cfdriver_detach(&lua_cd);
830 			aprint_error("%s: unable to register cfdata\n",
831 			    lua_cd.cd_name);
832 			return error;
833 		}
834 		error = devsw_attach(lua_cd.cd_name, NULL, &bmajor,
835 		    &lua_cdevsw, &cmajor);
836 		if (error) {
837 			aprint_error("%s: unable to register devsw\n",
838 			    lua_cd.cd_name);
839 			config_cfattach_detach(lua_cd.cd_name, &lua_ca);
840 			config_cfdriver_detach(&lua_cd);
841 			return error;
842 		}
843 		config_attach_pseudo(lua_cfdata);
844 #endif
845 		return 0;
846 	case MODULE_CMD_FINI:
847 #ifdef _MODULE
848 		error = config_cfdata_detach(lua_cfdata);
849 		if (error)
850 			return error;
851 
852 		config_cfattach_detach(lua_cd.cd_name, &lua_ca);
853 		config_cfdriver_detach(&lua_cd);
854 		devsw_detach(NULL, &lua_cdevsw);
855 #endif
856 		return 0;
857 	case MODULE_CMD_AUTOUNLOAD:
858 		/* no auto-unload */
859 		return EBUSY;
860 	default:
861 		return ENOTTY;
862 	}
863 }
864