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