--- lbbs/src/fork.c 2004/10/11 11:15:33 1.1 +++ lbbs/src/fork.c 2025/04/30 09:18:19 1.13 @@ -1,9 +1,9 @@ /*************************************************************************** - fork.c - description - ------------------- - begin : Mon Oct 11 2004 - copyright : (C) 2004 by Leaf - email : leaflet@leafok.com + fork.c - description + ------------------- + begin : Mon Oct 18 2004 + copyright : (C) 2004 by Leaflet + email : leaflet@leafok.com ***************************************************************************/ /*************************************************************************** @@ -15,5 +15,67 @@ * * ***************************************************************************/ - #include "common.h" - \ No newline at end of file +#include "common.h" +#include "bbs_main.h" +#include "log.h" +#include "io.h" +#include "fork.h" +#include +#include +#include + +int fork_server() +{ + int pid; + + if (pid = fork()) + { + SYS_child_process_count++; + log_std("Child process (%d) start\n", pid); + return 0; + } + else if (pid < 0) + return -1; + + if (close(socket_server) == -1) + { + log_error("Close server socket failed\n"); + return -2; + } + + // Redirect Input + close(0); + if (dup2(socket_client, 0) == -1) + { + log_error("Redirect stdin to client socket failed\n"); + return -3; + } + + // Redirect Output + close(1); + if (dup2(socket_client, 1) == -1) + { + log_error("Redirect stdout to client socket failed\n"); + return -4; + } + + bbs_main(); + + if (close(socket_client) == -1) + { + log_error("Close client socket failed\n"); + } + + // Close Input and Output for client + close(0); + close(1); + + log_std("Process exit normally\n"); + + log_end(); + + // Exit child process normally + exit(0); + + return 0; +}