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