+static void lua_socket_poll_event(int fd, short events);
+
+static int getunixsocket(char *path, struct sockaddr_un *r) {
+ int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ int ret;
+
+ if(fd <= -1)
+ return -1;
+
+ memset(r, 0, sizeof(struct sockaddr_un));
+ r->sun_family = AF_UNIX;
+ strlcpy(r->sun_path, path, sizeof(r->sun_path));
+
+ /* WTB exceptions */
+ ret = fcntl(fd, F_GETFL, 0);
+ if(ret < 0) {
+ close(fd);
+ return -1;
+ }
+
+ ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
+ if(ret < 0) {
+ close(fd);
+ return -1;
+ }
+
+ return fd;
+}
+
+static void registerluasocket(lua_list *ll, lua_socket *ls, int mask, int settag) {
+ /* this whole identifier thing should probably use userdata stuff */
+ ls->identifier = nextidentifier++;
+
+ if(settag) {
+ ls->tag = luaL_ref(ll->l, LUA_REGISTRYINDEX);
+ ls->handler = luaL_ref(ll->l, LUA_REGISTRYINDEX);
+ }
+ ls->l = ll;
+
+ ls->next = ll->sockets;
+ ll->sockets = ls;
+
+ registerhandler(ls->fd, mask, lua_socket_poll_event);
+}