| 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 |
|
|
| 30 |
return -1; |
return -1; |
| 31 |
|
|
| 32 |
log_std ("Child process start\n"); |
log_std ("Child process start\n"); |
| 33 |
|
|
| 34 |
if (close(sock_server) == -1) |
if (close(socket_server) == -1) |
| 35 |
{ |
{ |
| 36 |
log_error("Close server socket failed\n"); |
log_error("Close server socket failed\n"); |
| 37 |
return -2; |
return -2; |
| 38 |
} |
} |
| 39 |
|
|
| 40 |
socket_client = sock_client; |
//Redirect Input |
| 41 |
strcpy(hostaddr_client,(const char *)inet_ntoa(p_sin->sin_addr)); |
close(0); |
| 42 |
port_client = ntohs(p_sin->sin_port); |
if (dup2 (socket_client, 0) == -1) |
| 43 |
|
{ |
| 44 |
|
log_error("Redirect stdin to client socket failed\n"); |
| 45 |
|
return -3; |
| 46 |
|
} |
| 47 |
|
|
| 48 |
//Redirect Output |
//Redirect Output |
| 49 |
close(1); |
close(1); |
| 50 |
if (dup2 (socket_client, 1) == -1) |
if (dup2 (socket_client, 1) == -1) |
| 51 |
{ |
{ |
| 52 |
log_error("Redirect stdout to client socket failed\n"); |
log_error("Redirect stdout to client socket failed\n"); |
| 53 |
return -3; |
return -4; |
| 54 |
} |
} |
| 55 |
|
|
| 56 |
bbs_main(); |
bbs_main(); |
| 57 |
|
|
| 58 |
if (close(sock_client) == -1) |
if (close(socket_client) == -1) |
| 59 |
{ |
{ |
| 60 |
log_error("Close client socket failed\n"); |
log_error("Close client socket failed\n"); |
| 61 |
} |
} |
| 62 |
|
|
| 63 |
log_std ("Child process exit\n"); |
log_std ("Child process exit\n"); |
| 64 |
|
|
| 65 |
|
//Close Input and Output for client |
| 66 |
|
close(0); |
| 67 |
|
close(1); |
| 68 |
|
|
| 69 |
log_end(); |
log_end(); |
| 70 |
|
|
| 71 |
//Exit child process normally |
//Exit child process normally |