Annotation of src/sys/modules/lua/lua.c, revision 1.13.2.5
1.13.2.5! snj 1: /* $NetBSD: lua.c,v 1.13.2.4 2017/07/23 05:55:03 snj Exp $ */
1.1 mbalmer 2:
3: /*
1.13.2.5! snj 4: * Copyright (c) 2011 - 2017 by Marc Balmer <mbalmer@NetBSD.org>.
1.9 lneto 5: * Copyright (c) 2014 by Lourival Vieira Neto <lneto@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,
1.13 dholland 107: .d_discard = nodiscard,
1.8 dholland 108: .d_flag = D_OTHER | D_MPSAFE
1.1 mbalmer 109: };
110:
111: struct lua_loadstate {
112: struct vnode *vp;
113: size_t size;
114: off_t off;
115: };
116:
117: extern struct cfdriver lua_cd;
118:
119: static int
120: lua_match(device_t parent, cfdata_t match, void *aux)
121: {
122: return 1;
123: }
124:
125: static void
126: lua_attach(device_t parent, device_t self, void *aux)
127: {
128: struct lua_softc *sc;
129: const struct sysctlnode *node;
130:
131: if (sc_self)
132: return;
133:
134: sc = device_private(self);
135: sc->sc_dev = self;
136: sc_self = self;
137:
138: mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
139: cv_init(&sc->sc_inuse_cv, "luactl");
140:
141: mutex_init(&sc->sc_state_lock, MUTEX_DEFAULT, IPL_VM);
142: cv_init(&sc->sc_state_cv, "luastate");
143:
144: pmf_device_register(self, NULL, NULL);
145:
146: /* Sysctl to provide some control over behaviour */
147: sysctl_createv(&sc->sc_log, 0, NULL, &node,
148: CTLFLAG_OWNDESC,
149: CTLTYPE_NODE, "lua",
150: SYSCTL_DESCR("Lua options"),
151: NULL, 0, NULL, 0,
152: CTL_KERN, CTL_CREATE, CTL_EOL);
153:
154: if (node == NULL) {
155: printf(": can't create sysctl node\n");
156: return;
157: }
158:
159: /*
160: * XXX Some of the sysctl values must not be changed after the
161: * securelevel has been raised.
162: */
163: sysctl_createv(&sc->sc_log, 0, &node, NULL,
164: CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
165: CTLTYPE_BOOL, "require",
166: SYSCTL_DESCR("Enable the require command"),
167: NULL, 0, &lua_require_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, "autoload",
173: SYSCTL_DESCR("Enable automatic load of modules"),
174: NULL, 0, &lua_autoload_on, 0,
175: CTL_CREATE, CTL_EOL);
176:
177: sysctl_createv(&sc->sc_log, 0, &node, NULL,
178: CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
179: CTLTYPE_BOOL, "bytecode",
180: SYSCTL_DESCR("Enable loading of bytecode"),
181: NULL, 0, &lua_bytecode_on, 0,
182: CTL_CREATE, CTL_EOL);
183:
184: sysctl_createv(&sc->sc_log, 0, &node, NULL,
185: CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
186: CTLTYPE_INT, "verbose",
187: SYSCTL_DESCR("Enable verbose output"),
188: NULL, 0, &lua_verbose, 0,
189: CTL_CREATE, CTL_EOL);
190:
191: sysctl_createv(&sc->sc_log, 0, &node, NULL,
192: CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
193: CTLTYPE_INT, "maxcount",
194: SYSCTL_DESCR("Limit maximum instruction count"),
195: NULL, 0, &lua_max_instr, 0,
196: CTL_CREATE, CTL_EOL);
197:
1.12 lneto 198: aprint_normal_dev(self, "%s\n", LUA_COPYRIGHT);
1.1 mbalmer 199: }
200:
201: static int
202: lua_detach(device_t self, int flags)
203: {
204: struct lua_softc *sc;
205: struct lua_state *s;
206:
207: sc = device_private(self);
208: pmf_device_deregister(self);
209:
210: if (sc->sc_log != NULL) {
211: sysctl_teardown(&sc->sc_log);
212: sc->sc_log = NULL;
213: }
214:
215: /* Traverse the list of states and close them */
216: while ((s = LIST_FIRST(&lua_states)) != NULL) {
217: LIST_REMOVE(s, lua_next);
218: klua_close(s->K);
219: if (lua_verbose)
220: device_printf(self, "state %s destroyed\n",
221: s->lua_name);
1.5 mbalmer 222: kmem_free(s, sizeof(struct lua_state));
1.1 mbalmer 223: }
224: mutex_destroy(&sc->sc_lock);
225: cv_destroy(&sc->sc_inuse_cv);
226: mutex_destroy(&sc->sc_state_lock);
227: cv_destroy(&sc->sc_state_cv);
228: sc_self = NULL;
229: return 0;
230: }
231:
232: int
233: luaopen(dev_t dev, int flag, int mode, struct lwp *l)
234: {
235: struct lua_softc *sc;
1.4 mbalmer 236: int error = 0;
1.1 mbalmer 237:
238: if (minor(dev) > 0)
239: return ENXIO;
240:
241: sc = device_lookup_private(&lua_cd, minor(dev));
242: if (sc == NULL)
243: return ENXIO;
244:
245: mutex_enter(&sc->sc_lock);
246: while (sc->sc_inuse == true) {
247: error = cv_wait_sig(&sc->sc_inuse_cv, &sc->sc_lock);
248: if (error)
249: break;
250: }
251: if (!error)
252: sc->sc_inuse = true;
253: mutex_exit(&sc->sc_lock);
254:
255: if (error)
256: return error;
257: return 0;
258: }
259:
260: int
261: luaclose(dev_t dev, int flag, int mode, struct lwp *l)
262: {
263: struct lua_softc *sc;
264:
265: if (minor(dev) > 0)
266: return ENXIO;
267: sc = device_lookup_private(&lua_cd, minor(dev));
268: mutex_enter(&sc->sc_lock);
269: sc->sc_inuse = false;
270: cv_signal(&sc->sc_inuse_cv);
271: mutex_exit(&sc->sc_lock);
272: return 0;
273: }
274:
275: int
276: luaioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
277: {
278: struct lua_softc *sc;
279: struct lua_info *info;
280: struct lua_create *create;
281: struct lua_require *require;
282: struct lua_load *load;
283: struct lua_state *s;
284: struct lua_module *m;
285: kauth_cred_t cred;
286: struct nameidata nd;
287: struct pathbuf *pb;
288: struct vattr va;
289: struct lua_loadstate ls;
290: int error, n;
291: klua_State *K;
292:
293: sc = device_lookup_private(&lua_cd, minor(dev));
294: if (!device_is_active(sc->sc_dev))
295: return EBUSY;
296:
297: switch (cmd) {
298: case LUAINFO:
299: info = data;
300: if (info->states == NULL) {
301: info->num_states = 0;
302: LIST_FOREACH(s, &lua_states, lua_next)
303: info->num_states++;
304: } else {
305: n = 0;
306: LIST_FOREACH(s, &lua_states, lua_next) {
307: if (n > info->num_states)
308: break;
309: copyoutstr(s->lua_name, info->states[n].name,
310: MAX_LUA_NAME, NULL);
311: copyoutstr(s->lua_desc, info->states[n].desc,
312: MAX_LUA_DESC, NULL);
313: info->states[n].user = s->K->ks_user;
314: n++;
315: }
316: info->num_states = n;
317: }
318: break;
319: case LUACREATE:
320: create = data;
321:
322: if (*create->name == '_') {
323: if (lua_verbose)
324: device_printf(sc->sc_dev, "names of user "
325: "created states must not begin with '_'");
326: return ENXIO;
327: }
328: LIST_FOREACH(s, &lua_states, lua_next)
329: if (!strcmp(s->lua_name, create->name)) {
330: if (lua_verbose)
331: device_printf(sc->sc_dev,
332: "state %s exists\n", create->name);
333: return EBUSY;
334: }
335:
1.9 lneto 336: K = kluaL_newstate(create->name, create->desc, IPL_NONE);
1.1 mbalmer 337:
338: if (K == NULL)
339: return ENOMEM;
1.13.2.4 snj 340:
341: K->ks_user = true;
342:
1.1 mbalmer 343: if (lua_verbose)
344: device_printf(sc->sc_dev, "state %s created\n",
345: create->name);
346: break;
347: case LUADESTROY:
348: create = data;
349:
350: K = klua_find(create->name);
351:
352: if (K != NULL && (K->ks_user == true)) {
353: klua_close(K);
354: return 0;
355: }
356: return EBUSY;
357: case LUAREQUIRE: /* 'require' a module in a State */
358: require = data;
359: LIST_FOREACH(s, &lua_states, lua_next)
1.13.2.3 martin 360: if (!strcmp(s->lua_name, require->state)) {
361: LIST_FOREACH(m, &s->lua_modules, mod_next)
362: if (!strcmp(m->mod_name, require->module))
363: return ENXIO;
1.1 mbalmer 364: LIST_FOREACH(m, &lua_modules, mod_next)
365: if (!strcmp(m->mod_name,
366: require->module)) {
367: if (lua_verbose)
368: device_printf(
369: sc->sc_dev,
370: "requiring module "
371: "%s to state %s\n",
372: m->mod_name,
373: s->lua_name);
1.9 lneto 374: klua_lock(s->K);
1.13.2.1 martin 375: luaL_requiref(
376: s->K->L,
377: m->mod_name,
378: m->open,
379: 1);
1.9 lneto 380: klua_unlock(s->K);
1.1 mbalmer 381: m->refcount++;
382: LIST_INSERT_HEAD(
383: &s->lua_modules, m,
384: mod_next);
385: return 0;
386: }
1.13.2.3 martin 387: }
1.1 mbalmer 388: return ENXIO;
389: case LUALOAD:
390: load = data;
391: if (strrchr(load->path, '/') == NULL)
392: return ENXIO;
393:
394: LIST_FOREACH(s, &lua_states, lua_next)
395: if (!strcmp(s->lua_name, load->state)) {
396: if (lua_verbose)
397: device_printf(sc->sc_dev,
398: "loading %s into state %s\n",
399: load->path, s->lua_name);
400: cred = kauth_cred_get();
401: pb = pathbuf_create(load->path);
402: if (pb == NULL)
403: return ENOMEM;
404: NDINIT(&nd, LOOKUP, FOLLOW | NOCHROOT, pb);
405: pathbuf_destroy(pb);
406: error = vn_open(&nd, FREAD, 0);
407: if (error) {
408: if (lua_verbose)
409: device_printf(sc->sc_dev,
410: "error vn_open %d\n",
411: error);
412: return error;
413: }
414: error = VOP_GETATTR(nd.ni_vp, &va,
415: kauth_cred_get());
416: if (error) {
417: VOP_UNLOCK(nd.ni_vp);
418: vn_close(nd.ni_vp, FREAD,
419: kauth_cred_get());
420: if (lua_verbose)
421: device_printf(sc->sc_dev,
422: "erro VOP_GETATTR %d\n",
423: error);
424: return error;
425: }
426: if (va.va_type != VREG) {
427: VOP_UNLOCK(nd.ni_vp);
428: vn_close(nd.ni_vp, FREAD,
429: kauth_cred_get());
430: return EINVAL;
431: }
432: ls.vp = nd.ni_vp;
433: ls.off = 0L;
434: ls.size = va.va_size;
435: VOP_UNLOCK(nd.ni_vp);
1.9 lneto 436: klua_lock(s->K);
1.1 mbalmer 437: error = lua_load(s->K->L, lua_reader, &ls,
1.12 lneto 438: strrchr(load->path, '/') + 1, "bt");
1.1 mbalmer 439: vn_close(nd.ni_vp, FREAD, cred);
440: switch (error) {
441: case 0: /* no error */
442: break;
443: case LUA_ERRSYNTAX:
444: if (lua_verbose)
445: device_printf(sc->sc_dev,
446: "syntax error\n");
1.9 lneto 447: klua_unlock(s->K);
1.1 mbalmer 448: return EINVAL;
449: case LUA_ERRMEM:
450: if (lua_verbose)
451: device_printf(sc->sc_dev,
452: "memory error\n");
1.9 lneto 453: klua_unlock(s->K);
1.1 mbalmer 454: return ENOMEM;
455: default:
456: if (lua_verbose)
457: device_printf(sc->sc_dev,
458: "load error %d: %s\n",
459: error,
460: lua_tostring(s->K->L, -1));
1.9 lneto 461: klua_unlock(s->K);
1.1 mbalmer 462: return EINVAL;
463: }
464: if (lua_max_instr > 0)
465: lua_sethook(s->K->L, lua_maxcount,
466: LUA_MASKCOUNT, lua_max_instr);
467: error = lua_pcall(s->K->L, 0, LUA_MULTRET, 0);
468: if (error) {
469: if (lua_verbose) {
470: device_printf(sc->sc_dev,
471: "execution error: %s\n",
472: lua_tostring(s->K->L, -1));
473: }
1.9 lneto 474: klua_unlock(s->K);
1.1 mbalmer 475: return EINVAL;
476: }
1.9 lneto 477: klua_unlock(s->K);
1.1 mbalmer 478: return 0;
479: }
480: return ENXIO;
481: }
482: return 0;
483: }
484:
485: static int
486: lua_require(lua_State *L)
487: {
488: struct lua_state *s;
489: struct lua_module *m, *md;
490: const char *module;
491: char name[MAXPATHLEN];
492:
493: module = lua_tostring(L, -1);
494: md = NULL;
495: LIST_FOREACH(m, &lua_modules, mod_next)
496: if (!strcmp(m->mod_name, module)) {
497: md = m;
498: break;
499: }
500:
501: if (md == NULL && lua_autoload_on && strchr(module, '/') == NULL) {
502: snprintf(name, sizeof name, "lua%s", module);
503: if (lua_verbose)
504: device_printf(sc_self, "autoload %s\n", name);
1.3 mbalmer 505: module_autoload(name, MODULE_CLASS_MISC);
1.1 mbalmer 506: LIST_FOREACH(m, &lua_modules, mod_next)
507: if (!strcmp(m->mod_name, module)) {
508: md = m;
509: break;
510: }
511: }
512:
513: if (md != NULL)
514: LIST_FOREACH(s, &lua_states, lua_next)
515: if (s->K->L == L) {
1.13.2.5! snj 516: LIST_FOREACH(m, &s->lua_modules, mod_next)
! 517: if (m == md)
! 518: return 1;
! 519:
1.1 mbalmer 520: if (lua_verbose)
521: device_printf(sc_self,
522: "require module %s\n",
523: md->mod_name);
1.13.2.1 martin 524: luaL_requiref(L, md->mod_name, md->open, 0);
525:
1.1 mbalmer 526: md->refcount++;
527: LIST_INSERT_HEAD(&s->lua_modules, md, mod_next);
1.12 lneto 528: return 1;
1.1 mbalmer 529: }
530:
531: lua_pushstring(L, "module not found");
532: return lua_error(L);
533: }
534:
1.9 lneto 535: typedef struct {
536: size_t size;
537: } __packed alloc_header_t;
538:
539: static void *
1.1 mbalmer 540: lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
541: {
1.9 lneto 542: void *nptr = NULL;
543:
544: const size_t hdr_size = sizeof(alloc_header_t);
545: alloc_header_t *hdr = (alloc_header_t *) ((char *) ptr - hdr_size);
1.5 mbalmer 546:
1.9 lneto 547: if (nsize == 0) { /* freeing */
1.1 mbalmer 548: if (ptr != NULL)
1.9 lneto 549: kmem_intr_free(hdr, hdr->size);
550: } else if (ptr != NULL && nsize <= hdr->size - hdr_size) /* shrinking */
551: return ptr; /* don't need to reallocate */
552: else { /* creating or expanding */
553: km_flag_t sleep = cpu_intr_p() || cpu_softintr_p() ?
554: KM_NOSLEEP : KM_SLEEP;
555:
556: size_t alloc_size = nsize + hdr_size;
557: alloc_header_t *nhdr = kmem_intr_alloc(alloc_size, sleep);
558: if (nhdr == NULL) /* failed to allocate */
559: return NULL;
560:
561: nhdr->size = alloc_size;
562: nptr = (void *) ((char *) nhdr + hdr_size);
563:
564: if (ptr != NULL) { /* expanding */
565: memcpy(nptr, ptr, osize);
566: kmem_intr_free(hdr, hdr->size);
1.5 mbalmer 567: }
568: }
569: return nptr;
1.1 mbalmer 570: }
571:
572: static const char *
573: lua_reader(lua_State *L, void *data, size_t *size)
574: {
575: struct lua_loadstate *ls;
576: static char buf[1024];
577: size_t rsiz;
578:
579: ls = data;
580: if (ls->size < sizeof(buf))
581: rsiz = ls->size;
582: else
583: rsiz = sizeof(buf);
584: vn_rdwr(UIO_READ, ls->vp, buf, rsiz, ls->off, UIO_SYSSPACE,
585: 0, curlwp->l_cred, NULL, curlwp);
586: if (ls->off == 0L && lua_bytecode_on == false && buf[0] == 0x1b) {
587: *size = 0L;
588: lua_pushstring(L, "loading of bytecode is not allowed");
589: lua_error(L);
590: return NULL;
591: } else {
592: *size = rsiz;
593: ls->off += *size;
594: ls->size -= *size;
595: }
596: return buf;
597: }
598:
599: static void
600: lua_maxcount(lua_State *L, lua_Debug *d)
601: {
602: lua_pushstring(L, "maximum instruction count exceeded");
603: lua_error(L);
604: }
605:
606: int
1.11 lneto 607: klua_mod_register(const char *name, lua_CFunction open)
1.1 mbalmer 608: {
609: struct lua_module *m;
610:
611: LIST_FOREACH(m, &lua_modules, mod_next)
612: if (!strcmp(m->mod_name, name))
613: return EBUSY;
1.5 mbalmer 614: m = kmem_zalloc(sizeof(struct lua_module), KM_SLEEP);
1.1 mbalmer 615: strlcpy(m->mod_name, name, LUA_MAX_MODNAME);
616: m->open = open;
617: m->refcount = 0;
618: LIST_INSERT_HEAD(&lua_modules, m, mod_next);
619: if (lua_verbose)
620: device_printf(sc_self, "registered lua module %s\n", name);
621: return 0;
622: }
623:
624: int
1.11 lneto 625: klua_mod_unregister(const char *name)
1.1 mbalmer 626: {
627: struct lua_module *m;
628:
629: LIST_FOREACH(m, &lua_modules, mod_next)
630: if (!strcmp(m->mod_name, name)) {
631: if (m->refcount == 0) {
632: LIST_REMOVE(m, mod_next);
1.5 mbalmer 633: kmem_free(m, sizeof(struct lua_module));
1.1 mbalmer 634: if (lua_verbose)
635: device_printf(sc_self,
636: "unregistered lua module %s\n",
637: name);
638: return 0;
639: } else
640: return EBUSY;
641: }
642: return 0;
643: }
644:
645: klua_State *
1.9 lneto 646: klua_newstate(lua_Alloc f, void *ud, const char *name, const char *desc,
647: int ipl)
1.1 mbalmer 648: {
649: klua_State *K;
650: struct lua_state *s;
651: struct lua_softc *sc;
1.4 mbalmer 652: int error = 0;
1.1 mbalmer 653:
1.5 mbalmer 654: s = kmem_zalloc(sizeof(struct lua_state), KM_SLEEP);
1.1 mbalmer 655: sc = device_private(sc_self);
656: mutex_enter(&sc->sc_state_lock);
657: while (sc->sc_state == true) {
658: error = cv_wait_sig(&sc->sc_state_cv, &sc->sc_state_lock);
659: if (error)
660: break;
661: }
662: if (!error)
663: sc->sc_state = true;
664: mutex_exit(&sc->sc_state_lock);
665:
1.13.2.2 martin 666: if (error) {
667: kmem_free(s, sizeof(struct lua_state));
1.1 mbalmer 668: return NULL;
1.13.2.2 martin 669: }
1.1 mbalmer 670:
1.5 mbalmer 671: K = kmem_zalloc(sizeof(klua_State), KM_SLEEP);
1.1 mbalmer 672: K->L = lua_newstate(f, ud);
673: K->ks_user = false;
674: if (K->L == NULL) {
1.5 mbalmer 675: kmem_free(K, sizeof(klua_State));
1.1 mbalmer 676: K = NULL;
677: goto finish;
678: }
679:
680: strlcpy(s->lua_name, name, MAX_LUA_NAME);
681: strlcpy(s->lua_desc, desc, MAX_LUA_DESC);
682: s->K = K;
683:
684: if (lua_require_on || lua_autoload_on) {
685: lua_pushcfunction(K->L, lua_require);
686: lua_setglobal(K->L, "require");
687: }
688: LIST_INSERT_HEAD(&lua_states, s, lua_next);
689:
1.9 lneto 690: mutex_init(&K->ks_lock, MUTEX_DEFAULT, ipl);
1.1 mbalmer 691:
692: finish:
693: mutex_enter(&sc->sc_state_lock);
694: sc->sc_state = false;
695: cv_signal(&sc->sc_state_cv);
696: mutex_exit(&sc->sc_state_lock);
697: return K;
698: }
699:
1.9 lneto 700: inline klua_State *
701: kluaL_newstate(const char *name, const char *desc, int ipl)
702: {
703: return klua_newstate(lua_alloc, NULL, name, desc, ipl);
704: }
705:
1.1 mbalmer 706: void
707: klua_close(klua_State *K)
708: {
709: struct lua_state *s;
710: struct lua_softc *sc;
711: struct lua_module *m;
1.4 mbalmer 712: int error = 0;
1.1 mbalmer 713:
714: lua_getglobal(K->L, "onClose");
715: if (lua_isfunction(K->L, -1))
716: lua_pcall(K->L, -1, 0, 0);
717:
718: sc = device_private(sc_self);
719: mutex_enter(&sc->sc_state_lock);
720: while (sc->sc_state == true) {
721: error = cv_wait_sig(&sc->sc_state_cv, &sc->sc_state_lock);
722: if (error)
723: break;
724: }
725: if (!error)
726: sc->sc_state = true;
727: mutex_exit(&sc->sc_state_lock);
728:
729: if (error)
730: return; /* Nothing we can do... */
731:
732: LIST_FOREACH(s, &lua_states, lua_next)
733: if (s->K == K) {
734: LIST_REMOVE(s, lua_next);
735: LIST_FOREACH(m, &s->lua_modules, mod_next)
736: m->refcount--;
1.5 mbalmer 737: kmem_free(s, sizeof(struct lua_state));
1.1 mbalmer 738: }
739:
740: lua_close(K->L);
741: mutex_destroy(&K->ks_lock);
1.5 mbalmer 742: kmem_free(K, sizeof(klua_State));
1.1 mbalmer 743:
744: mutex_enter(&sc->sc_state_lock);
745: sc->sc_state = false;
746: cv_signal(&sc->sc_state_cv);
747: mutex_exit(&sc->sc_state_lock);
748: }
749:
750: static klua_State *
751: klua_find(const char *name)
752: {
753: struct lua_state *s;
754: struct lua_softc *sc;
755: klua_State *K;
1.4 mbalmer 756: int error = 0;
1.1 mbalmer 757:
758: K = NULL;
759: sc = device_private(sc_self);
760: mutex_enter(&sc->sc_state_lock);
761: while (sc->sc_state == true) {
762: error = cv_wait_sig(&sc->sc_state_cv, &sc->sc_state_lock);
763: if (error)
764: break;
765: }
766: if (!error)
767: sc->sc_state = true;
768: mutex_exit(&sc->sc_state_lock);
769:
770: if (error)
771: return NULL;
772:
773: LIST_FOREACH(s, &lua_states, lua_next)
774: if (!strcmp(s->lua_name, name)) {
775: K = s->K;
776: break;
777: }
778:
779: mutex_enter(&sc->sc_state_lock);
780: sc->sc_state = false;
781: cv_signal(&sc->sc_state_cv);
782: mutex_exit(&sc->sc_state_lock);
783: return K;
784: }
785:
1.9 lneto 786: inline void
1.1 mbalmer 787: klua_lock(klua_State *K)
788: {
789: mutex_enter(&K->ks_lock);
790: }
791:
1.9 lneto 792: inline void
1.1 mbalmer 793: klua_unlock(klua_State *K)
794: {
795: mutex_exit(&K->ks_lock);
796: }
797:
798: MODULE(MODULE_CLASS_MISC, lua, NULL);
799:
800: #ifdef _MODULE
801: static const struct cfiattrdata luabus_iattrdata = {
802: "luabus", 0, { { NULL, NULL, 0 },}
803: };
804: static const struct cfiattrdata *const lua_attrs[] = {
805: &luabus_iattrdata, NULL
806: };
807: CFDRIVER_DECL(lua, DV_DULL, lua_attrs);
808: extern struct cfattach lua_ca;
809: static int lualoc[] = {
810: -1,
811: -1,
812: -1
813: };
814: static struct cfdata lua_cfdata[] = {
815: {
816: .cf_name = "lua",
817: .cf_atname = "lua",
818: .cf_unit = 0,
819: .cf_fstate = FSTATE_STAR,
820: .cf_loc = lualoc,
821: .cf_flags = 0,
822: .cf_pspec = NULL,
823: },
824: { NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL }
825: };
826: #endif
827:
828: static int
829: lua_modcmd(modcmd_t cmd, void *opaque)
830: {
831: #ifdef _MODULE
832: devmajor_t cmajor, bmajor;
833: int error = 0;
834:
835: cmajor = bmajor = NODEVMAJOR;
836: #endif
837: switch (cmd) {
838: case MODULE_CMD_INIT:
839: #ifdef _MODULE
840: error = config_cfdriver_attach(&lua_cd);
841: if (error)
842: return error;
843:
844: error = config_cfattach_attach(lua_cd.cd_name,
845: &lua_ca);
846: if (error) {
847: config_cfdriver_detach(&lua_cd);
848: aprint_error("%s: unable to register cfattach\n",
849: lua_cd.cd_name);
850: return error;
851: }
852: error = config_cfdata_attach(lua_cfdata, 1);
853: if (error) {
854: config_cfattach_detach(lua_cd.cd_name,
855: &lua_ca);
856: config_cfdriver_detach(&lua_cd);
857: aprint_error("%s: unable to register cfdata\n",
858: lua_cd.cd_name);
859: return error;
860: }
861: error = devsw_attach(lua_cd.cd_name, NULL, &bmajor,
862: &lua_cdevsw, &cmajor);
863: if (error) {
864: aprint_error("%s: unable to register devsw\n",
865: lua_cd.cd_name);
866: config_cfattach_detach(lua_cd.cd_name, &lua_ca);
867: config_cfdriver_detach(&lua_cd);
868: return error;
869: }
870: config_attach_pseudo(lua_cfdata);
871: #endif
872: return 0;
873: case MODULE_CMD_FINI:
874: #ifdef _MODULE
875: error = config_cfdata_detach(lua_cfdata);
876: if (error)
877: return error;
878:
879: config_cfattach_detach(lua_cd.cd_name, &lua_ca);
880: config_cfdriver_detach(&lua_cd);
881: devsw_detach(NULL, &lua_cdevsw);
882: #endif
883: return 0;
884: case MODULE_CMD_AUTOUNLOAD:
885: /* no auto-unload */
886: return EBUSY;
887: default:
888: return ENOTTY;
889: }
890: }
CVSweb <webmaster@jp.NetBSD.org>