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