Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178106020
D60433.1795977554.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
D60433.1795977554.diff
View Options
diff --git a/libexec/flua/modules/lposix.3lua b/libexec/flua/modules/lposix.3lua
--- a/libexec/flua/modules/lposix.3lua
+++ b/libexec/flua/modules/lposix.3lua
@@ -10,6 +10,7 @@
.Nm posix.fnmatch ,
.Nm posix.libgen ,
.Nm posix.stdlib ,
+.Nm posix.sys.socket ,
.Nm posix.sys.stat ,
.Nm posix.sys.utsname ,
.Nm posix.sys.wait ,
@@ -41,6 +42,25 @@
.It
.Fn realpath path
.El
+.It Va posix.sys.socket
+Socket operations.
+Only
+.Dv AF_UNIX
+sockets are supported; the sockaddr table has the fields
+.Va family
+(optional, defaults to
+.Dv AF_UNIX )
+and
+.Va path .
+.Bl -bullet -compact
+.It
+.Fo socket
+.Fa domain type
+.Op Fa protocol
+.Fc
+.It
+.Fn connect fd sockaddr
+.El
.It Va posix.sys.stat
File status operations.
.Bl -bullet -compact
@@ -103,8 +123,32 @@
posix.sys.wait.wait()
end
.Ed
+.Pp
+Connect to a
+.Ux Ns -domain
+socket and exchange a message:
+.Bd -literal -offset indent
+local socket = posix.sys.socket
+local unistd = posix.unistd
+
+local fd, err = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+if not fd then
+ error("socket: " .. err)
+end
+local ok, err = socket.connect(fd, { path = "/var/run/example.sock" })
+if not ok then
+ error("connect: " .. err)
+end
+unistd.write(fd, "hello\en")
+local reply = unistd.read(fd, 1024)
+print(reply)
+unistd.close(fd)
+.Ed
.Sh SEE ALSO
-.Xr flua 1
+.Xr flua 1 ,
+.Xr connect 2 ,
+.Xr socket 2 ,
+.Xr unix 4
.Sh AUTHORS
The
.Nm
diff --git a/libexec/flua/modules/lposix.c b/libexec/flua/modules/lposix.c
--- a/libexec/flua/modules/lposix.c
+++ b/libexec/flua/modules/lposix.c
@@ -4,7 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <sys/socket.h>
#include <sys/stat.h>
+#include <sys/un.h>
#include <sys/utsname.h>
#include <sys/wait.h>
@@ -544,6 +546,76 @@
return (3);
}
+static int
+lua_socket(lua_State *L)
+{
+ int domain, type, protocol, fd;
+
+ enforce_max_args(L, 3);
+ domain = luaL_checkinteger(L, 1);
+ type = luaL_checkinteger(L, 2);
+ protocol = luaL_optinteger(L, 3, 0);
+
+ fd = socket(domain, type, protocol);
+ if (fd < 0) {
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(errno));
+ lua_pushinteger(L, errno);
+ return (3);
+ }
+
+ lua_pushinteger(L, fd);
+ return (1);
+}
+
+/*
+ * connect(fd, { family = AF_UNIX, path = "/path/to/socket" })
+ *
+ * Only AF_UNIX stream/dgram sockets are supported
+ */
+static int
+lua_connect(lua_State *L)
+{
+ struct sockaddr_un sun;
+ const char *path;
+ size_t pathlen;
+ int error, family, fd;
+
+ enforce_max_args(L, 2);
+ fd = luaL_checkinteger(L, 1);
+ luaL_checktype(L, 2, LUA_TTABLE);
+
+ lua_getfield(L, 2, "family");
+ family = luaL_optinteger(L, -1, AF_UNIX);
+ lua_pop(L, 1);
+ if (family != AF_UNIX)
+ return (luaL_argerror(L, 2, "only AF_UNIX is supported"));
+
+ lua_getfield(L, 2, "path");
+ path = lua_tolstring(L, -1, &pathlen);
+ if (path == NULL)
+ return (luaL_argerror(L, 2, "sockaddr table needs a 'path'"));
+ if (pathlen >= sizeof(sun.sun_path))
+ return (luaL_argerror(L, 2, "path too long"));
+
+ memset(&sun, 0, sizeof(sun));
+ sun.sun_family = AF_UNIX;
+ memcpy(sun.sun_path, path, pathlen);
+ sun.sun_len = SUN_LEN(&sun);
+ lua_pop(L, 1);
+
+ if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
+ error = errno;
+ lua_pushnil(L);
+ lua_pushstring(L, strerror(error));
+ lua_pushinteger(L, error);
+ return (3);
+ }
+
+ lua_pushinteger(L, 0);
+ return (1);
+}
+
#define REG_DEF(n, func) { #n, func }
#define REG_SIMPLE(n) REG_DEF(n, lua_ ## n)
static const struct luaL_Reg libgenlib[] = {
@@ -567,6 +639,12 @@
{ NULL, NULL },
};
+static const struct luaL_Reg sys_socketlib[] = {
+ REG_SIMPLE(socket),
+ REG_SIMPLE(connect),
+ { NULL, NULL },
+};
+
static const struct luaL_Reg sys_utsnamelib[] = {
REG_SIMPLE(uname),
{ NULL, NULL },
@@ -626,6 +704,23 @@
return 1;
}
+static int
+luaopen_posix_sys_socket(lua_State *L)
+{
+ luaL_newlib(L, sys_socketlib);
+
+#define setkv(f) do { \
+ lua_pushinteger(L, f); \
+ lua_setfield(L, -2, #f); \
+} while (0)
+ setkv(AF_UNIX);
+ setkv(SOCK_STREAM);
+ setkv(SOCK_DGRAM);
+#undef setkv
+
+ return (1);
+}
+
static int
luaopen_posix_sys_stat(lua_State *L)
{
@@ -688,6 +783,8 @@
lua_setfield(L, -2, "stdlib");
lua_newtable(L); /* posix.sys */
+ luaL_requiref(L, "posix.sys.socket", luaopen_posix_sys_socket, 0);
+ lua_setfield(L, -2, "socket");
luaL_requiref(L, "posix.sys.stat", luaopen_posix_sys_stat, 0);
lua_setfield(L, -2, "stat");
luaL_requiref(L, "posix.sys.utsname", luaopen_posix_sys_utsname, 0);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 29, 6:39 PM (11 h, 52 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42094460
Default Alt Text
D60433.1795977554.diff (4 KB)
Attached To
Mode
D60433: flua: lposix: Add Unix domain socket support
Attached
Detach File
Event Timeline