More experimental stdio door work

This commit is contained in:
Andrew Pamment 2016-03-26 10:58:23 +10:00
parent 84c8647d34
commit e6c99a06d1
2 changed files with 54 additions and 12 deletions

View File

@ -8,7 +8,7 @@ OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o
$(CC) -c -o $@ $< $(CFLAGS) $(CC) -c -o $@ $< $(CFLAGS)
magicka: $(OBJ) magicka: $(OBJ)
$(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 $(JAMLIB) $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 $(JAMLIB) -lutil
.PHONY: clean .PHONY: clean

64
doors.c
View File

@ -6,6 +6,11 @@
#include <unistd.h> #include <unistd.h>
#include <libgen.h> #include <libgen.h>
#include <sys/wait.h> #include <sys/wait.h>
#if defined(linux)
# include <pty.h>
#else
# include <libutil.h>
#endif
#include "bbs.h" #include "bbs.h"
extern struct bbs_config conf; extern struct bbs_config conf;
@ -109,6 +114,10 @@ void rundoor(int socket, struct user_record *user, char *cmd, int stdio) {
char c; char c;
int len; int len;
int status; int status;
int master;
int slave;
fd_set fdset;
int t;
if (write_door32sys(socket, user) != 0) { if (write_door32sys(socket, user) != 0) {
return; return;
@ -116,24 +125,57 @@ void rundoor(int socket, struct user_record *user, char *cmd, int stdio) {
if (stdio) { if (stdio) {
arguments[0] = strdup(basename(cmd)); arguments[0] = strdup(cmd);
sprintf(buffer, "%d", mynode); sprintf(buffer, "%d", mynode);
arguments[1] = strdup(buffer); arguments[1] = strdup(buffer);
sprintf(buffer, "%d", socket); sprintf(buffer, "%d", socket);
arguments[2] = strdup(buffer); arguments[2] = strdup(buffer);
arguments[3] = NULL; arguments[3] = NULL;
pid = fork(); if (openpty(&master, &slave, NULL, NULL, NULL) == 0) {
if (pid < 0) { pid = fork();
return; if (pid < 0) {
} else if (pid == 0) { return;
// forked process } else if (pid == 0) {
dup2(socket, 0); dup2(slave, 0);
dup2(socket, 1); dup2(slave, 1);
execvp(cmd, arguments); execvp(cmd, arguments);
} else { } else {
waitpid(pid, &status, 0); while(1) {
FD_ZERO(&fdset);
FD_SET(master, &fdset);
FD_SET(socket, &fdset);
if (master > socket) {
t = master + 1;
} else {
t = socket + 1;
}
ret = select(t, &fdset, NULL, NULL, NULL);
if (ret > 0) {
if (FD_ISSET(socket, &fdset)) {
len = read(socket, &c, 1);
if (len == 0) {
// socket closed
close(master);
disconnect(socket);
return;
}
write(master, &c, 1);
} else if (FD_ISSET(master, &fdset)) {
len = read(master, &c, 1);
if (len == 0) {
close(master);
break;
}
write(socket, &c, 1);
}
}
}
}
} }
free(arguments[0]);
free(arguments[1]);
free(arguments[2]);
} else { } else {
sprintf(buffer, "%s %d %d", cmd, mynode, socket); sprintf(buffer, "%s %d %d", cmd, mynode, socket);
system(buffer); system(buffer);