Module Name: src Committed By: mbalmer Date: Wed Oct 16 19:48:21 UTC 2013
Added Files: src/sbin/luactl: Makefile luactl.8 luactl.c Log Message: luactl(8) is a command to control lua(4) To generate a diff of this commit: cvs rdiff -u -r0 -r1.1 src/sbin/luactl/Makefile src/sbin/luactl/luactl.8 \ src/sbin/luactl/luactl.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Added files: Index: src/sbin/luactl/Makefile diff -u /dev/null src/sbin/luactl/Makefile:1.1 --- /dev/null Wed Oct 16 19:48:21 2013 +++ src/sbin/luactl/Makefile Wed Oct 16 19:48:21 2013 @@ -0,0 +1,8 @@ +# $NetBSD: Makefile,v 1.1 2013/10/16 19:48:21 mbalmer Exp $ + +PROG= luactl +SRCS= luactl.c + +MAN= luactl.8 + +.include <bsd.prog.mk> Index: src/sbin/luactl/luactl.8 diff -u /dev/null src/sbin/luactl/luactl.8:1.1 --- /dev/null Wed Oct 16 19:48:21 2013 +++ src/sbin/luactl/luactl.8 Wed Oct 16 19:48:21 2013 @@ -0,0 +1,126 @@ +.\" $NetBSD: luactl.8,v 1.1 2013/10/16 19:48:21 mbalmer Exp $ +.\" +.\" Copyright (c) 2011 Marc Balmer <m...@msys.ch> +.\" +.\" Permission to use, copy, modify, and distribute this software for any +.\" purpose with or without fee is hereby granted, provided that the above +.\" copyright notice and this permission notice appear in all copies. +.\" +.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +.\" +.Dd August 26, 2011 +.Dt LUACTL 8 +.Os +.Sh NAME +.Nm luactl +.Nd control kernel Lua states +.Sh SYNOPSIS +.Nm luactl +.Op Fl cq +.Nm luactl +.Op Fl cq +create +.Ar name +.Op desc +.Nm luactl +.Op Fl cq +destroy +.Ar name +.Nm luactl +.Op Fl cq +require +.Ar name module +.Nm luactl +.Op Fl cq +load +.Ar name path +.Sh DESCRIPTION +The +.Nm +program allows the manipulation of Lua states in the kernel. +Lua states are created using the +.Sq create +command (see below), +Lua bindings are provided as modules. +To make a Lua binding available to a state, it must be +.Sq required . +If a kernel subsystem is to use a Lua state, a state has to be +.Sq assigned +to it. +Once a module has been +.Sq required +by a state, it can not be unloaded from memory using the +.Xr modunload 8 +command until the state using it has been destroyed. +.Pp +Lua code can be loaded from the filesystem into a state at anytime, please +note that code loaded into a state is immediately executed. +.Pp +When executed without a command, +.Nm +reads information about the +.Tn Lua +states and displays it. +.Pp +The options are as follows: +.Bl -tag -width Ds +.It Fl c +Create a Lua state before executing the command. +This flag is used for the require, assing, and, load commands only, it +is ignored for all other commands. +.It Fl q +Operate quietly i.e. nothing is printed to stdout. +.El +.Sh COMMANDS +.Bl -tag -width Ds +.It create Ar name Op Ar desc +Create a Lua state with name +.Ar name +and optional description +.Ar desc . +.It destroy Ar name +Destroy the Lua state +.Ar name . +.It require Ar name module +Let the Lua state +.Ar name +use the bindings provided in module +.Ar module . +This is the equivalent of userland Lua code calling the +.Sq require +function. +.It load Ar name Pa path +Load Lua code in file +.Pa path +into the Lua state +.Ar name . +Note that the path name must contain at least one path separation character +.Pq Sq / . +.El +.Sh FILES +.Bl -tag -width "/dev/lua" -compact +.It /dev/lua +Lua device file. +.El +.Sh SEE ALSO +.Xr lua 4 , +.Xr lua 9 , +.Xr modload 8 , +.Xr module 7 , +.Xr modunload 8 +.Sh HISTORY +The +.Nm +command first appeared in +.Nx 6.0 . +.Sh AUTHORS +The +.Nm +program was written by +.An Marc Balmer Aq m...@msys.ch . Index: src/sbin/luactl/luactl.c diff -u /dev/null src/sbin/luactl/luactl.c:1.1 --- /dev/null Wed Oct 16 19:48:21 2013 +++ src/sbin/luactl/luactl.c Wed Oct 16 19:48:21 2013 @@ -0,0 +1,230 @@ +/* $NetBSD: luactl.c,v 1.1 2013/10/16 19:48:21 mbalmer Exp $ */ + +/* + * Copyright (c) 2011, Marc Balmer <mbal...@netbsd.org>. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the Author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Program to control Lua devices. + */ + +#include <stdbool.h> +#include <sys/param.h> +#include <sys/lua.h> +#include <sys/ioctl.h> + +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <paths.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +int devfd = -1; +int quiet = 0; +int docreate = 0; + +static void getinfo(void); +static void create(char *, char *); +static void destroy(char *); + +static void require(char *, char *); +static void load(char *, char *); + +static void usage(void); + +#define _PATH_DEV_LUA "/dev/lua" + +int +main(int argc, char *argv[]) +{ + int ch; + + while ((ch = getopt(argc, argv, "cq")) != -1) + switch (ch) { + case 'c': + docreate = 1; + break; + case 'q': + quiet = 1; + break; + default: + usage(); + /* NOTREACHED */ + } + argc -= optind; + argv += optind; + + if ((devfd = open(_PATH_DEV_LUA, O_RDWR)) == -1) + err(EXIT_FAILURE, "%s", _PATH_DEV_LUA); + + if (argc == 0) + getinfo(); + else if (!strcmp(argv[0], "create")) { + if (argc == 2) + create(argv[1], NULL); + else if (argc == 3) + create(argv[1], argv[2]); + else + usage(); + } else if (!strcmp(argv[0], "destroy")) { + if (argc != 2) + usage(); + destroy(argv[1]); + } else if (!strcmp(argv[0], "require")) { + if (argc != 3) + usage(); + if (docreate) + create(argv[1], NULL); + require(argv[1], argv[2]); + } else if (!strcmp(argv[0], "load")) { + if (argc != 3) + usage(); + if (docreate) + create(argv[1], NULL); + load(argv[1], argv[2]); + } else + usage(); + + return EXIT_SUCCESS; +} + +static void +getinfo(void) +{ + struct lua_info info; + int n; + + info.states = NULL; + if (ioctl(devfd, LUAINFO, &info) == -1) + err(EXIT_FAILURE, "LUAINFO"); + + if (info.num_states > 0) { + info.states = calloc(info.num_states, + sizeof(struct lua_state_info)); + if (info.states == NULL) + err(EXIT_FAILURE, "calloc"); + if (ioctl(devfd, LUAINFO, &info) == -1) + err(EXIT_FAILURE, "LUAINFO"); + } + printf("%d active state%s:\n", info.num_states, + info.num_states == 1 ? "" : "s"); + if (info.num_states > 0) + printf("%-16s %-8s Description\n", "Name", "Creator"); + for (n = 0; n < info.num_states; n++) + printf("%-16s %-8s %s\n", info.states[n].name, + info.states[n].user == true ? "user" : "kernel", + info.states[n].desc); +} + +static void +create(char *name, char *desc) +{ + struct lua_create cr; + + strlcpy(cr.name, name, sizeof(cr.name)); + if (desc != NULL) + strlcpy(cr.desc, desc, sizeof(cr.desc)); + else + cr.desc[0] = '\0'; + + if (ioctl(devfd, LUACREATE, &cr) == -1) + err(EXIT_FAILURE, "LUACREATE"); + + if (quiet) + return; + + printf("%s created\n", name); +} + +static void +destroy(char *name) +{ + struct lua_create cr; + + strlcpy(cr.name, name, sizeof(cr.name)); + + if (ioctl(devfd, LUADESTROY, &cr) == -1) + err(EXIT_FAILURE, "LUADESTROY"); + + if (quiet) + return; + + printf("%s destroyed\n", name); +} + +static void +require(char *name, char *module) +{ + struct lua_require r; + + strlcpy(r.state, name, sizeof(r.state)); + strlcpy(r.module, module, sizeof(r.module)); + + if (ioctl(devfd, LUAREQUIRE, &r) == -1) + err(EXIT_FAILURE, "LUAREQUIRE"); + + if (quiet) + return; + + printf("%s required by %s\n", module, name); +} + +static void +load(char *name, char *path) +{ + struct lua_load l; + + strlcpy(l.state, name, sizeof(l.state)); + strlcpy(l.path, path, sizeof(l.path)); + + if (ioctl(devfd, LUALOAD, &l) == -1) + err(EXIT_FAILURE, "LUALOAD"); + + if (quiet) + return; + + printf("%s loaded into %s\n", path, name); +} + +static void +usage(void) +{ + const char *p; + + p = getprogname(); + fprintf(stderr, "usage: %s [-cq]\n", p); + fprintf(stderr, " %s [-cq] create name [desc]\n", p); + fprintf(stderr, " %s [-cq] destroy name\n", p); + fprintf(stderr, " %s [-cq] require name module\n", p); + fprintf(stderr, " %s [-cq] load name path\n", p); + + exit(EXIT_FAILURE); +}