| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
fork.c - description |
fork.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
begin : Mon Oct 18 2004 |
| 5 |
copyright : (C) 2004 by Leaflet |
copyright : (C) 2004 by Leaflet |
| 6 |
email : leaflet@leafok.com |
email : leaflet@leafok.com |
| 7 |
***************************************************************************/ |
***************************************************************************/ |
| 8 |
|
|
| 9 |
/*************************************************************************** |
/*************************************************************************** |
| 16 |
***************************************************************************/ |
***************************************************************************/ |
| 17 |
|
|
| 18 |
#include "common.h" |
#include "common.h" |
| 19 |
|
#include "io.h" |
| 20 |
|
#include <string.h> |
| 21 |
|
|
| 22 |
int |
int fork_server() |
|
fork_server(int sock_server, int sock_client, struct sockaddr_in * p_sin) |
|
| 23 |
{ |
{ |
| 24 |
int pid; |
int pid; |
|
|
|
|
if (pid = fork ()) |
|
|
return 0; |
|
|
else if (pid < 0) |
|
|
return -1; |
|
|
|
|
|
log_std ("Child process start\n"); |
|
|
|
|
|
if (close(sock_server) == -1) |
|
|
{ |
|
|
log_error("Close server socket failed\n"); |
|
|
} |
|
|
|
|
|
socket_client = sock_client; |
|
|
strcpy(hostaddr_client,inet_ntoa(p_sin->sin_addr)); |
|
|
port_client = ntohs(p_sin->sin_port); |
|
|
|
|
|
bbs_main(); |
|
|
|
|
|
if (close(sock_client) == -1) |
|
|
{ |
|
|
log_error("Close client socket failed\n"); |
|
|
} |
|
| 25 |
|
|
| 26 |
log_std ("Child process exit\n"); |
if (pid = fork()) |
| 27 |
|
{ |
| 28 |
|
SYS_child_process_count++; |
| 29 |
|
log_std("Child process (%d) start\n", pid); |
| 30 |
|
return 0; |
| 31 |
|
} |
| 32 |
|
else if (pid < 0) |
| 33 |
|
return -1; |
| 34 |
|
|
| 35 |
|
if (close(socket_server) == -1) |
| 36 |
|
{ |
| 37 |
|
log_error("Close server socket failed\n"); |
| 38 |
|
return -2; |
| 39 |
|
} |
| 40 |
|
|
| 41 |
|
// Redirect Input |
| 42 |
|
close(0); |
| 43 |
|
if (dup2(socket_client, 0) == -1) |
| 44 |
|
{ |
| 45 |
|
log_error("Redirect stdin to client socket failed\n"); |
| 46 |
|
return -3; |
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
// Redirect Output |
| 50 |
|
close(1); |
| 51 |
|
if (dup2(socket_client, 1) == -1) |
| 52 |
|
{ |
| 53 |
|
log_error("Redirect stdout to client socket failed\n"); |
| 54 |
|
return -4; |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
bbs_main(); |
| 58 |
|
|
| 59 |
|
if (close(socket_client) == -1) |
| 60 |
|
{ |
| 61 |
|
log_error("Close client socket failed\n"); |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
// Close Input and Output for client |
| 65 |
|
close(0); |
| 66 |
|
close(1); |
| 67 |
|
|
| 68 |
log_end(); |
log_std("Process exit normally\n"); |
| 69 |
|
|
| 70 |
//Exit child process normally |
log_end(); |
|
exit(0); |
|
| 71 |
|
|
| 72 |
return 0; |
// Exit child process normally |
| 73 |
|
exit(0); |
| 74 |
|
|
| 75 |
|
return 0; |
| 76 |
} |
} |