| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
fork.c - description |
fork.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
Copyright : (C) 2004-2025 by Leaflet |
| 5 |
copyright : (C) 2004 by Leaflet |
Email : leaflet@leafok.com |
|
email : leaflet@leafok.com |
|
| 6 |
***************************************************************************/ |
***************************************************************************/ |
| 7 |
|
|
| 8 |
/*************************************************************************** |
/*************************************************************************** |
| 9 |
* * |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
| 11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
| 12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 3 of the License, or * |
| 13 |
* (at your option) any later version. * |
* (at your option) any later version. * |
| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "common.h" |
#include "common.h" |
| 18 |
|
#include "bbs_main.h" |
| 19 |
|
#include "log.h" |
| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
|
#include "fork.h" |
| 22 |
|
#include <stdio.h> |
| 23 |
#include <string.h> |
#include <string.h> |
| 24 |
|
#include <signal.h> |
| 25 |
|
#include <unistd.h> |
| 26 |
|
#include <stdlib.h> |
| 27 |
|
#include <errno.h> |
| 28 |
|
|
| 29 |
int fork_server() |
int fork_server() |
| 30 |
{ |
{ |
| 31 |
int pid; |
int pid; |
| 32 |
|
|
| 33 |
if (pid = fork()) |
pid = fork(); |
| 34 |
|
|
| 35 |
|
if (pid > 0) // Parent process |
| 36 |
{ |
{ |
| 37 |
SYS_child_process_count++; |
SYS_child_process_count++; |
| 38 |
log_std("Child process (%d) start\n", pid); |
log_std("Child process (%d) start\n", pid); |
| 39 |
return 0; |
return 0; |
| 40 |
} |
} |
| 41 |
else if (pid < 0) |
else if (pid < 0) // Error |
| 42 |
|
{ |
| 43 |
|
log_error("fork() error (%d)\n", errno); |
| 44 |
return -1; |
return -1; |
| 45 |
|
} |
| 46 |
|
|
| 47 |
|
// Child process |
| 48 |
if (close(socket_server) == -1) |
if (close(socket_server) == -1) |
| 49 |
{ |
{ |
| 50 |
log_error("Close server socket failed\n"); |
log_error("Close server socket failed\n"); |
| 52 |
} |
} |
| 53 |
|
|
| 54 |
// Redirect Input |
// Redirect Input |
| 55 |
close(0); |
close(STDIN_FILENO); |
| 56 |
if (dup2(socket_client, 0) == -1) |
if (dup2(socket_client, STDIN_FILENO) == -1) |
| 57 |
{ |
{ |
| 58 |
log_error("Redirect stdin to client socket failed\n"); |
log_error("Redirect stdin to client socket failed\n"); |
| 59 |
return -3; |
return -3; |
| 60 |
} |
} |
| 61 |
|
|
| 62 |
// Redirect Output |
// Redirect Output |
| 63 |
close(1); |
close(STDOUT_FILENO); |
| 64 |
if (dup2(socket_client, 1) == -1) |
if (dup2(socket_client, STDOUT_FILENO) == -1) |
| 65 |
{ |
{ |
| 66 |
log_error("Redirect stdout to client socket failed\n"); |
log_error("Redirect stdout to client socket failed\n"); |
| 67 |
return -4; |
return -4; |
| 68 |
} |
} |
| 69 |
|
|
| 70 |
|
SYS_child_process_count = 0; |
| 71 |
|
|
| 72 |
|
// Reset signal handler to default |
| 73 |
|
signal(SIGHUP, SIG_DFL); |
| 74 |
|
signal(SIGCHLD, SIG_DFL); |
| 75 |
|
signal(SIGTERM, SIG_DFL); |
| 76 |
|
|
| 77 |
bbs_main(); |
bbs_main(); |
| 78 |
|
|
| 79 |
|
// Child process exit |
| 80 |
|
SYS_server_exit = 1; |
| 81 |
|
|
| 82 |
if (close(socket_client) == -1) |
if (close(socket_client) == -1) |
| 83 |
{ |
{ |
| 84 |
log_error("Close client socket failed\n"); |
log_error("Close client socket failed\n"); |
| 85 |
} |
} |
| 86 |
|
|
| 87 |
// Close Input and Output for client |
// Close Input and Output for client |
| 88 |
close(0); |
close(STDIN_FILENO); |
| 89 |
close(1); |
close(STDOUT_FILENO); |
| 90 |
|
|
| 91 |
log_std("Process exit normally\n"); |
log_std("Process exit normally\n"); |
| 92 |
|
|
| 93 |
log_end(); |
log_end(); |
| 94 |
|
|
|
// Exit child process normally |
|
|
exit(0); |
|
|
|
|
| 95 |
return 0; |
return 0; |
| 96 |
} |
} |