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