| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
bbsd.c - description |
fork.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
begin : Mon Oct 18 2004 |
| 5 |
copyright : (C) 2004 by leaf |
copyright : (C) 2004 by Leaflet |
| 6 |
email : leaflet@leafok.com |
email : leaflet@leafok.com |
| 7 |
***************************************************************************/ |
***************************************************************************/ |
| 8 |
|
|
| 16 |
***************************************************************************/ |
***************************************************************************/ |
| 17 |
|
|
| 18 |
#include "common.h" |
#include "common.h" |
| 19 |
#include <unistd.h> |
#include "io.h" |
| 20 |
#include <signal.h> |
#include <string.h> |
| 21 |
#include <sys/param.h> |
|
| 22 |
#include <sys/types.h> |
int |
| 23 |
#include <sys/stat.h> |
fork_server () |
| 24 |
|
{ |
| 25 |
|
int pid; |
| 26 |
|
|
| 27 |
|
if (pid = fork ()) |
| 28 |
|
return 0; |
| 29 |
|
else if (pid < 0) |
| 30 |
|
return -1; |
| 31 |
|
|
| 32 |
|
log_std ("Child process start\n"); |
| 33 |
|
|
| 34 |
|
if (close (socket_server) == -1) |
| 35 |
|
{ |
| 36 |
|
log_error ("Close server socket failed\n"); |
| 37 |
|
return -2; |
| 38 |
|
} |
| 39 |
|
|
| 40 |
|
//Redirect Input |
| 41 |
|
close (0); |
| 42 |
|
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 |
| 49 |
|
close (1); |
| 50 |
|
if (dup2 (socket_client, 1) == -1) |
| 51 |
|
{ |
| 52 |
|
log_error ("Redirect stdout to client socket failed\n"); |
| 53 |
|
return -4; |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
bbs_main (); |
| 57 |
|
|
| 58 |
|
if (close (socket_client) == -1) |
| 59 |
|
{ |
| 60 |
|
log_error ("Close client socket failed\n"); |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
log_std ("Child process exit\n"); |
| 64 |
|
|
| 65 |
|
//Close Input and Output for client |
| 66 |
|
close (0); |
| 67 |
|
close (1); |
| 68 |
|
|
| 69 |
|
log_end (); |
| 70 |
|
|
| 71 |
|
//Exit child process normally |
| 72 |
|
exit (0); |
| 73 |
|
|
| 74 |
|
return 0; |
| 75 |
|
} |