--- lbbs/src/fork.c 2004/10/11 11:15:33 1.1 +++ lbbs/src/fork.c 2025/05/15 05:14:57 1.19 @@ -1,19 +1,103 @@ /*************************************************************************** - fork.c - description - ------------------- - begin : Mon Oct 11 2004 - copyright : (C) 2004 by Leaf - email : leaflet@leafok.com + fork.c - description + ------------------- + Copyright : (C) 2004-2025 by Leaflet + Email : leaflet@leafok.com ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * + * the Free Software Foundation; either version 3 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ - #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 "menu.h" +#include +#include +#include +#include +#include +#include + +int fork_server() +{ + int pid; + + pid = fork(); + + if (pid > 0) // Parent process + { + SYS_child_process_count++; + log_std("Child process (%d) start\n", pid); + return 0; + } + else if (pid < 0) // Error + { + log_error("fork() error (%d)\n", errno); + return -1; + } + + // Child process + if (close(socket_server) == -1) + { + log_error("Close server socket failed\n"); + return -2; + } + + // Redirect Input + close(STDIN_FILENO); + if (dup2(socket_client, STDIN_FILENO) == -1) + { + log_error("Redirect stdin to client socket failed\n"); + return -3; + } + + // Redirect Output + close(STDOUT_FILENO); + if (dup2(socket_client, STDOUT_FILENO) == -1) + { + log_error("Redirect stdout to client socket failed\n"); + return -4; + } + + SYS_child_process_count = 0; + + // Reset signal handler to default + signal(SIGHUP, SIG_DFL); + signal(SIGCHLD, SIG_DFL); + signal(SIGTERM, SIG_DFL); + + bbs_main(); + + // Child process exit + SYS_server_exit = 1; + + if (close(socket_client) == -1) + { + log_error("Close client socket failed\n"); + } + + // Close Input and Output for client + close(STDIN_FILENO); + close(STDOUT_FILENO); + + // Unload menu + unload_menu_shm(p_bbs_menu); + free(p_bbs_menu); + p_bbs_menu = NULL; + + log_std("Process exit normally\n"); + log_end(); + + _exit(0); + + return 0; +}