Annotation of src/sys/modules/lua/lua.c, revision 1.26
1.26 ! rin 1: /* $NetBSD: lua.c,v 1.25 2021/06/29 22:40:53 dholland 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;
1.25 dholland 287: struct vnode *vp;
1.1 mbalmer 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:
301: info = data;
302: if (info->states == NULL) {
303: info->num_states = 0;
304: LIST_FOREACH(s, &lua_states, lua_next)
305: info->num_states++;
306: } else {
307: n = 0;
308: LIST_FOREACH(s, &lua_states, lua_next) {
309: if (n > info->num_states)
310: break;
311: n++;
312: }
313: info->num_states = n;
1.24 martin 314: states = kmem_alloc(sizeof(*states) * n, KM_SLEEP);
315: if (copyin(info->states, states, sizeof(*states) * n)
316: == 0) {
317: n = 0;
318: LIST_FOREACH(s, &lua_states, lua_next) {
319: if (n > info->num_states)
320: break;
321: strcpy(states[n].name, s->lua_name);
322: strcpy(states[n].desc, s->lua_desc);
323: states[n].user = s->K->ks_user;
324: n++;
325: }
326: copyout(states, info->states,
327: sizeof(*states) * n);
328: kmem_free(states, sizeof(*states) * n);
329: }
1.1 mbalmer 330: }
331: break;
332: case LUACREATE:
333: create = data;
334:
335: if (*create->name == '_') {
336: if (lua_verbose)
337: device_printf(sc->sc_dev, "names of user "
338: "created states must not begin with '_'");
339: return ENXIO;
340: }
341: LIST_FOREACH(s, &lua_states, lua_next)
342: if (!strcmp(s->lua_name, create->name)) {
343: if (lua_verbose)
344: device_printf(sc->sc_dev,
345: "state %s exists\n", create->name);
346: return EBUSY;
347: }
348:
1.9 lneto 349: K = kluaL_newstate(create->name, create->desc, IPL_NONE);
1.1 mbalmer 350:
351: if (K == NULL)
352: return ENOMEM;
1.21 mbalmer 353:
354: K->ks_user = true;
355:
1.1 mbalmer 356: if (lua_verbose)
357: device_printf(sc->sc_dev, "state %s created\n",
358: create->name);
359: break;
360: case LUADESTROY:
361: create = data;
362:
363: K = klua_find(create->name);
364:
365: if (K != NULL && (K->ks_user == true)) {
366: klua_close(K);
367: return 0;
368: }
369: return EBUSY;
370: case LUAREQUIRE: /* 'require' a module in a State */
371: require = data;
372: LIST_FOREACH(s, &lua_states, lua_next)
1.17 salazar 373: if (!strcmp(s->lua_name, require->state)) {
374: LIST_FOREACH(m, &s->lua_modules, mod_next)
375: if (!strcmp(m->mod_name, require->module))
376: return ENXIO;
1.1 mbalmer 377: LIST_FOREACH(m, &lua_modules, mod_next)
378: if (!strcmp(m->mod_name,
379: require->module)) {
380: if (lua_verbose)
381: device_printf(
382: sc->sc_dev,
383: "requiring module "
384: "%s to state %s\n",
385: m->mod_name,
386: s->lua_name);
1.9 lneto 387: klua_lock(s->K);
1.15 lneto 388: luaL_requiref(
389: s->K->L,
390: m->mod_name,
391: m->open,
392: 1);
1.9 lneto 393: klua_unlock(s->K);
1.1 mbalmer 394: m->refcount++;
395: LIST_INSERT_HEAD(
396: &s->lua_modules, m,
397: mod_next);
398: return 0;
399: }
1.17 salazar 400: }
1.1 mbalmer 401: return ENXIO;
402: case LUALOAD:
403: load = data;
404: if (strrchr(load->path, '/') == NULL)
405: return ENXIO;
406:
407: LIST_FOREACH(s, &lua_states, lua_next)
408: if (!strcmp(s->lua_name, load->state)) {
409: if (lua_verbose)
410: device_printf(sc->sc_dev,
411: "loading %s into state %s\n",
412: load->path, s->lua_name);
413: cred = kauth_cred_get();
414: pb = pathbuf_create(load->path);
415: if (pb == NULL)
416: return ENOMEM;
1.25 dholland 417: error = vn_open(NULL, pb, NOCHROOT, FREAD, 0,
418: &vp, NULL, NULL);
1.1 mbalmer 419: pathbuf_destroy(pb);
420: if (error) {
421: if (lua_verbose)
422: device_printf(sc->sc_dev,
423: "error vn_open %d\n",
424: error);
425: return error;
426: }
1.25 dholland 427: error = VOP_GETATTR(vp, &va,
1.1 mbalmer 428: kauth_cred_get());
429: if (error) {
1.25 dholland 430: VOP_UNLOCK(vp);
431: vn_close(vp, FREAD,
1.1 mbalmer 432: kauth_cred_get());
433: if (lua_verbose)
434: device_printf(sc->sc_dev,
435: "erro VOP_GETATTR %d\n",
436: error);
437: return error;
438: }
439: if (va.va_type != VREG) {
1.25 dholland 440: VOP_UNLOCK(vp);
441: vn_close(vp, FREAD,
1.1 mbalmer 442: kauth_cred_get());
443: return EINVAL;
444: }
1.25 dholland 445: ls.vp = vp;
1.1 mbalmer 446: ls.off = 0L;
447: ls.size = va.va_size;
1.25 dholland 448: VOP_UNLOCK(vp);
1.9 lneto 449: klua_lock(s->K);
1.1 mbalmer 450: error = lua_load(s->K->L, lua_reader, &ls,
1.12 lneto 451: strrchr(load->path, '/') + 1, "bt");
1.25 dholland 452: vn_close(vp, FREAD, cred);
1.1 mbalmer 453: switch (error) {
454: case 0: /* no error */
455: break;
456: case LUA_ERRSYNTAX:
457: if (lua_verbose)
458: device_printf(sc->sc_dev,
459: "syntax error\n");
1.9 lneto 460: klua_unlock(s->K);
1.1 mbalmer 461: return EINVAL;
462: case LUA_ERRMEM:
463: if (lua_verbose)
464: device_printf(sc->sc_dev,
465: "memory error\n");
1.9 lneto 466: klua_unlock(s->K);
1.1 mbalmer 467: return ENOMEM;
468: default:
469: if (lua_verbose)
470: device_printf(sc->sc_dev,
471: "load error %d: %s\n",
472: error,
473: lua_tostring(s->K->L, -1));
1.9 lneto 474: klua_unlock(s->K);
1.1 mbalmer 475: return EINVAL;
476: }
477: if (lua_max_instr > 0)
478: lua_sethook(s->K->L, lua_maxcount,
479: LUA_MASKCOUNT, lua_max_instr);
480: error = lua_pcall(s->K->L, 0, LUA_MULTRET, 0);
481: if (error) {
482: if (lua_verbose) {
483: device_printf(sc->sc_dev,
484: "execution error: %s\n",
485: lua_tostring(s->K->L, -1));
486: }
1.9 lneto 487: klua_unlock(s->K);
1.1 mbalmer 488: return EINVAL;
489: }
1.9 lneto 490: klua_unlock(s->K);
1.1 mbalmer 491: return 0;
492: }
493: return ENXIO;
494: }
495: return 0;
496: }
497:
498: static int
499: lua_require(lua_State *L)
500: {
501: struct lua_state *s;
502: struct lua_module *m, *md;
503: const char *module;
504: char name[MAXPATHLEN];
505:
506: module = lua_tostring(L, -1);
507: md = NULL;
508: LIST_FOREACH(m, &lua_modules, mod_next)
509: if (!strcmp(m->mod_name, module)) {
510: md = m;
511: break;
512: }
513:
514: if (md == NULL && lua_autoload_on && strchr(module, '/') == NULL) {
515: snprintf(name, sizeof name, "lua%s", module);
516: if (lua_verbose)
517: device_printf(sc_self, "autoload %s\n", name);
1.3 mbalmer 518: module_autoload(name, MODULE_CLASS_MISC);
1.1 mbalmer 519: LIST_FOREACH(m, &lua_modules, mod_next)
520: if (!strcmp(m->mod_name, module)) {
521: md = m;
522: break;
523: }
524: }
525:
526: if (md != NULL)
527: LIST_FOREACH(s, &lua_states, lua_next)
528: if (s->K->L == L) {
529: if (lua_verbose)
530: device_printf(sc_self,
531: "require module %s\n",
532: md->mod_name);
1.15 lneto 533: luaL_requiref(L, md->mod_name, md->open, 0);
534:
1.23 mbalmer 535: LIST_FOREACH(m, &s->lua_modules, mod_next)
536: if (m == md)
537: return 1;
538:
1.1 mbalmer 539: md->refcount++;
540: LIST_INSERT_HEAD(&s->lua_modules, md, mod_next);
1.12 lneto 541: return 1;
1.1 mbalmer 542: }
543:
544: lua_pushstring(L, "module not found");
545: return lua_error(L);
546: }
547:
1.9 lneto 548: typedef struct {
549: size_t size;
1.26 ! rin 550: } alloc_header_t;
1.9 lneto 551:
552: static void *
1.1 mbalmer 553: lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
554: {
1.9 lneto 555: void *nptr = NULL;
556:
1.26 ! rin 557: /*
! 558: * Make sure that buffers allocated by lua_alloc() are aligned to
! 559: * 8-byte boundaries as done by kmem_alloc(9).
! 560: */
! 561: const size_t hdr_size = roundup(sizeof(alloc_header_t), 8);
1.9 lneto 562: alloc_header_t *hdr = (alloc_header_t *) ((char *) ptr - hdr_size);
1.5 mbalmer 563:
1.9 lneto 564: if (nsize == 0) { /* freeing */
1.1 mbalmer 565: if (ptr != NULL)
1.9 lneto 566: kmem_intr_free(hdr, hdr->size);
567: } else if (ptr != NULL && nsize <= hdr->size - hdr_size) /* shrinking */
568: return ptr; /* don't need to reallocate */
569: else { /* creating or expanding */
570: km_flag_t sleep = cpu_intr_p() || cpu_softintr_p() ?
571: KM_NOSLEEP : KM_SLEEP;
572:
573: size_t alloc_size = nsize + hdr_size;
574: alloc_header_t *nhdr = kmem_intr_alloc(alloc_size, sleep);
575: if (nhdr == NULL) /* failed to allocate */
576: return NULL;
577:
578: nhdr->size = alloc_size;
579: nptr = (void *) ((char *) nhdr + hdr_size);
580:
581: if (ptr != NULL) { /* expanding */
582: memcpy(nptr, ptr, osize);
583: kmem_intr_free(hdr, hdr->size);
1.5 mbalmer 584: }
585: }
586: return nptr;
1.1 mbalmer 587: }
588:
589: static const char *
590: lua_reader(lua_State *L, void *data, size_t *size)
591: {
592: struct lua_loadstate *ls;
593: static char buf[1024];
594: size_t rsiz;
595:
596: ls = data;
597: if (ls->size < sizeof(buf))
598: rsiz = ls->size;
599: else
600: rsiz = sizeof(buf);
601: vn_rdwr(UIO_READ, ls->vp, buf, rsiz, ls->off, UIO_SYSSPACE,
602: 0, curlwp->l_cred, NULL, curlwp);
603: if (ls->off == 0L && lua_bytecode_on == false && buf[0] == 0x1b) {
604: *size = 0L;
605: lua_pushstring(L, "loading of bytecode is not allowed");
606: lua_error(L);
607: return NULL;
608: } else {
609: *size = rsiz;
610: ls->off += *size;
611: ls->size -= *size;
612: }
613: return buf;
614: }
615:
616: static void
617: lua_maxcount(lua_State *L, lua_Debug *d)
618: {
619: lua_pushstring(L, "maximum instruction count exceeded");
620: lua_error(L);
621: }
622:
623: int
1.11 lneto 624: klua_mod_register(const char *name, lua_CFunction open)
1.1 mbalmer 625: {
626: struct lua_module *m;
627:
628: LIST_FOREACH(m, &lua_modules, mod_next)
629: if (!strcmp(m->mod_name, name))
630: return EBUSY;
1.5 mbalmer 631: m = kmem_zalloc(sizeof(struct lua_module), KM_SLEEP);
1.1 mbalmer 632: strlcpy(m->mod_name, name, LUA_MAX_MODNAME);
633: m->open = open;
634: m->refcount = 0;
635: LIST_INSERT_HEAD(&lua_modules, m, mod_next);
636: if (lua_verbose)
637: device_printf(sc_self, "registered lua module %s\n", name);
638: return 0;
639: }
640:
641: int
1.11 lneto 642: klua_mod_unregister(const char *name)
1.1 mbalmer 643: {
644: struct lua_module *m;
645:
646: LIST_FOREACH(m, &lua_modules, mod_next)
647: if (!strcmp(m->mod_name, name)) {
648: if (m->refcount == 0) {
649: LIST_REMOVE(m, mod_next);
1.5 mbalmer 650: kmem_free(m, sizeof(struct lua_module));
1.1 mbalmer 651: if (lua_verbose)
652: device_printf(sc_self,
653: "unregistered lua module %s\n",
654: name);
655: return 0;
656: } else
657: return EBUSY;
658: }
659: return 0;
660: }
661:
662: klua_State *
1.9 lneto 663: klua_newstate(lua_Alloc f, void *ud, const char *name, const char *desc,
1.14 mbalmer 664: int ipl)
1.1 mbalmer 665: {
666: klua_State *K;
667: struct lua_state *s;
668: struct lua_softc *sc;
1.4 mbalmer 669: int error = 0;
1.1 mbalmer 670:
1.5 mbalmer 671: s = kmem_zalloc(sizeof(struct lua_state), KM_SLEEP);
1.1 mbalmer 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:
1.16 christos 683: if (error) {
684: kmem_free(s, sizeof(struct lua_state));
1.1 mbalmer 685: return NULL;
1.16 christos 686: }
1.1 mbalmer 687:
1.5 mbalmer 688: K = kmem_zalloc(sizeof(klua_State), KM_SLEEP);
1.1 mbalmer 689: K->L = lua_newstate(f, ud);
690: K->ks_user = false;
691: if (K->L == NULL) {
1.5 mbalmer 692: kmem_free(K, sizeof(klua_State));
1.1 mbalmer 693: K = NULL;
694: goto finish;
695: }
696:
697: strlcpy(s->lua_name, name, MAX_LUA_NAME);
698: strlcpy(s->lua_desc, desc, MAX_LUA_DESC);
699: s->K = K;
700:
701: if (lua_require_on || lua_autoload_on) {
702: lua_pushcfunction(K->L, lua_require);
703: lua_setglobal(K->L, "require");
704: }
705: LIST_INSERT_HEAD(&lua_states, s, lua_next);
706:
1.9 lneto 707: mutex_init(&K->ks_lock, MUTEX_DEFAULT, ipl);
1.1 mbalmer 708:
709: finish:
710: mutex_enter(&sc->sc_state_lock);
711: sc->sc_state = false;
712: cv_signal(&sc->sc_state_cv);
713: mutex_exit(&sc->sc_state_lock);
714: return K;
715: }
716:
1.9 lneto 717: inline klua_State *
718: kluaL_newstate(const char *name, const char *desc, int ipl)
719: {
720: return klua_newstate(lua_alloc, NULL, name, desc, ipl);
721: }
722:
1.1 mbalmer 723: void
724: klua_close(klua_State *K)
725: {
726: struct lua_state *s;
727: struct lua_softc *sc;
728: struct lua_module *m;
1.4 mbalmer 729: int error = 0;
1.1 mbalmer 730:
1.14 mbalmer 731: /* XXX consider registering a handler instead of a fixed name. */
1.1 mbalmer 732: lua_getglobal(K->L, "onClose");
733: if (lua_isfunction(K->L, -1))
734: lua_pcall(K->L, -1, 0, 0);
735:
736: sc = device_private(sc_self);
737: mutex_enter(&sc->sc_state_lock);
738: while (sc->sc_state == true) {
739: error = cv_wait_sig(&sc->sc_state_cv, &sc->sc_state_lock);
740: if (error)
741: break;
742: }
743: if (!error)
744: sc->sc_state = true;
745: mutex_exit(&sc->sc_state_lock);
746:
747: if (error)
748: return; /* Nothing we can do... */
749:
750: LIST_FOREACH(s, &lua_states, lua_next)
751: if (s->K == K) {
752: LIST_REMOVE(s, lua_next);
753: LIST_FOREACH(m, &s->lua_modules, mod_next)
754: m->refcount--;
1.5 mbalmer 755: kmem_free(s, sizeof(struct lua_state));
1.1 mbalmer 756: }
757:
758: lua_close(K->L);
759: mutex_destroy(&K->ks_lock);
1.5 mbalmer 760: kmem_free(K, sizeof(klua_State));
1.1 mbalmer 761:
762: mutex_enter(&sc->sc_state_lock);
763: sc->sc_state = false;
764: cv_signal(&sc->sc_state_cv);
765: mutex_exit(&sc->sc_state_lock);
766: }
767:
768: static klua_State *
769: klua_find(const char *name)
770: {
771: struct lua_state *s;
772: struct lua_softc *sc;
773: klua_State *K;
1.4 mbalmer 774: int error = 0;
1.1 mbalmer 775:
776: K = NULL;
777: sc = device_private(sc_self);
778: mutex_enter(&sc->sc_state_lock);
779: while (sc->sc_state == true) {
780: error = cv_wait_sig(&sc->sc_state_cv, &sc->sc_state_lock);
781: if (error)
782: break;
783: }
784: if (!error)
785: sc->sc_state = true;
786: mutex_exit(&sc->sc_state_lock);
787:
788: if (error)
789: return NULL;
790:
791: LIST_FOREACH(s, &lua_states, lua_next)
792: if (!strcmp(s->lua_name, name)) {
793: K = s->K;
794: break;
795: }
796:
797: mutex_enter(&sc->sc_state_lock);
798: sc->sc_state = false;
799: cv_signal(&sc->sc_state_cv);
800: mutex_exit(&sc->sc_state_lock);
801: return K;
802: }
803:
1.9 lneto 804: inline void
1.1 mbalmer 805: klua_lock(klua_State *K)
806: {
807: mutex_enter(&K->ks_lock);
808: }
809:
1.9 lneto 810: inline void
1.1 mbalmer 811: klua_unlock(klua_State *K)
812: {
813: mutex_exit(&K->ks_lock);
814: }
815:
816: MODULE(MODULE_CLASS_MISC, lua, NULL);
817:
818: #ifdef _MODULE
819: static const struct cfiattrdata luabus_iattrdata = {
820: "luabus", 0, { { NULL, NULL, 0 },}
821: };
1.14 mbalmer 822:
1.1 mbalmer 823: static const struct cfiattrdata *const lua_attrs[] = {
824: &luabus_iattrdata, NULL
825: };
1.14 mbalmer 826:
1.1 mbalmer 827: CFDRIVER_DECL(lua, DV_DULL, lua_attrs);
828: extern struct cfattach lua_ca;
829: static int lualoc[] = {
830: -1,
831: -1,
832: -1
833: };
1.14 mbalmer 834:
1.1 mbalmer 835: static struct cfdata lua_cfdata[] = {
836: {
837: .cf_name = "lua",
838: .cf_atname = "lua",
839: .cf_unit = 0,
840: .cf_fstate = FSTATE_STAR,
841: .cf_loc = lualoc,
842: .cf_flags = 0,
843: .cf_pspec = NULL,
844: },
845: { NULL, NULL, 0, FSTATE_NOTFOUND, NULL, 0, NULL }
846: };
847: #endif
848:
849: static int
850: lua_modcmd(modcmd_t cmd, void *opaque)
851: {
852: #ifdef _MODULE
853: devmajor_t cmajor, bmajor;
854: int error = 0;
855:
856: cmajor = bmajor = NODEVMAJOR;
857: #endif
858: switch (cmd) {
859: case MODULE_CMD_INIT:
860: #ifdef _MODULE
861: error = config_cfdriver_attach(&lua_cd);
862: if (error)
863: return error;
864:
865: error = config_cfattach_attach(lua_cd.cd_name,
866: &lua_ca);
867: if (error) {
868: config_cfdriver_detach(&lua_cd);
869: aprint_error("%s: unable to register cfattach\n",
870: lua_cd.cd_name);
871: return error;
872: }
873: error = config_cfdata_attach(lua_cfdata, 1);
874: if (error) {
875: config_cfattach_detach(lua_cd.cd_name,
876: &lua_ca);
877: config_cfdriver_detach(&lua_cd);
878: aprint_error("%s: unable to register cfdata\n",
879: lua_cd.cd_name);
880: return error;
881: }
882: error = devsw_attach(lua_cd.cd_name, NULL, &bmajor,
883: &lua_cdevsw, &cmajor);
884: if (error) {
885: aprint_error("%s: unable to register devsw\n",
886: lua_cd.cd_name);
887: config_cfattach_detach(lua_cd.cd_name, &lua_ca);
888: config_cfdriver_detach(&lua_cd);
889: return error;
890: }
891: config_attach_pseudo(lua_cfdata);
892: #endif
893: return 0;
894: case MODULE_CMD_FINI:
895: #ifdef _MODULE
896: error = config_cfdata_detach(lua_cfdata);
897: if (error)
898: return error;
899:
900: config_cfattach_detach(lua_cd.cd_name, &lua_ca);
901: config_cfdriver_detach(&lua_cd);
902: devsw_detach(NULL, &lua_cdevsw);
903: #endif
904: return 0;
905: case MODULE_CMD_AUTOUNLOAD:
906: /* no auto-unload */
907: return EBUSY;
908: default:
909: return ENOTTY;
910: }
911: }
CVSweb <webmaster@jp.NetBSD.org>