hardcode 3 fds

This commit is contained in:
2026-05-19 21:27:02 +01:00
parent d5c2503150
commit a0a8bd0de4
+9 -9
View File
@@ -50,19 +50,19 @@ int open_socket(char * pathname) {
return fd;
}
int * fds_from_msg(struct msghdr *msg, size_t peer_fds_count) {
int *peer_fds = (int *) calloc(peer_fds_count, sizeof(int));
int * fds_from_msg(struct msghdr *msg) {
// * there may be multiple SCM_RIGHTS messages
// * we expect three fds
// * if we get some number other than three, we should
// close them and return NULL
int *peer_fds = (int *) calloc(3, sizeof(int));
for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(msg);
cmsg != NULL;
cmsg = CMSG_NXTHDR(msg, cmsg)) {
if(cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_RIGHTS) {
memcpy(peer_fds, CMSG_DATA(cmsg), peer_fds_count * sizeof(int));
memcpy(peer_fds, CMSG_DATA(cmsg), 3 * sizeof(int));
}
}
return peer_fds;
@@ -70,14 +70,14 @@ int * fds_from_msg(struct msghdr *msg, size_t peer_fds_count) {
int start_lua_child(lua_State *L,
char * command_line, int command_line_length,
int * fds, int fds_count) {
int * fds) {
char * word = command_line;
char * command_line_end = command_line + command_line_length;
char * pathname = NULL;
int i = 0;
fflush(stdout); fflush(stderr);
for(int i=0; i < fds_count; i++) {
for(int i=0; i < 3; i++) {
if(fds[i] > 0) dup2(fds[i], i);
}
@@ -103,11 +103,11 @@ int start_lua_child(lua_State *L,
pid_t fork_lua(lua_State *L,
char * command_line, int command_line_length,
int * fds, int fds_count) {
int * fds) {
pid_t pid = fork();
if(pid == 0) {
exit(start_lua_child(L, command_line, command_line_length,
fds, fds_count));
fds));
} else if(pid > 0) {
free(command_line);
return pid;
@@ -218,14 +218,14 @@ int main(int argc, char **argv)
if(pollfds[0].revents) {
if(find_peer(0)) {
int len = recvmsg(socket, &msg , 0);
int *peer_fds = fds_from_msg(&msg, 5);
int *peer_fds = fds_from_msg(&msg);
char *argline = malloc(len);
if(! argline) {
die("malloc failed");
}
memmove(argline, buf, len);
pid_t child_pid = fork_lua(L, argline, len, peer_fds, 5);
pid_t child_pid = fork_lua(L, argline, len, peer_fds);
if(child_pid >= 0) {
register_peer(child_pid, &peer_addr, msg.msg_namelen);
}