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