Hi there!
I am trying to get Unix sockets to work with Lua on Linux and since Lua seems to have no direct support for those, and nor does LuaSocket, and nor do I know enough C to add them in myself, I had to go and google up someone else's code (no small task, as it turned out).
The code I did manage to find eventually seems to work, but there's one thing that bugs me. The original file had the following line in it:
But this function is missing from Linux, so I used the simplest workaround I could think of - replacing it with:
So my question is: am I in trouble and if yes, then how big is this trouble and is there any better way to go about fixing this.
Here's the complete function in question for reference:
I am trying to get Unix sockets to work with Lua on Linux and since Lua seems to have no direct support for those, and nor does LuaSocket, and nor do I know enough C to add them in myself, I had to go and google up someone else's code (no small task, as it turned out).
The code I did manage to find eventually seems to work, but there's one thing that bugs me. The original file had the following line in it:
strlcpy(addr.sun_path, socket_filename, sizeof(addr.sun_path));But this function is missing from Linux, so I used the simplest workaround I could think of - replacing it with:
strcpy(addr.sun_path, socket_filename);So my question is: am I in trouble and if yes, then how big is this trouble and is there any better way to go about fixing this.
Here's the complete function in question for reference:
static int
socket_unix_server(lua_State *L) /* path */
{
assert(L);
const char *socket_filename = luaL_checkstring(L, 1);
/* make sure we don't overwrite a regular file */
struct stat st;
if(lstat(socket_filename, &st) == 0)
{
if(S_ISREG(st.st_mode))
{
return luaL_error(L, "file already exists and is not a socket");
}
}
struct sockaddr_un addr;
bzero(&addr, sizeof(addr));
int fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(fd == -1)
return luaL_error(L, "%s", strerror(errno));
if(unlink(socket_filename) != 0 && errno != ENOENT)
return luaL_error(L, "%s", strerror(errno));
addr.sun_family = AF_UNIX;
//strlcpy(addr.sun_path, socket_filename, sizeof(addr.sun_path));
strcpy(addr.sun_path, socket_filename);
if(bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) != 0)
return luaL_error(L, "%s", strerror(errno));
int rc = listen(fd, 20);
if(rc != 0)
return luaL_error(L, "%s", strerror(errno));
lua_pushinteger(L, fd);
return 1;
}