| 19 |
#include "log.h" |
#include "log.h" |
| 20 |
#include "io.h" |
#include "io.h" |
| 21 |
#include "fork.h" |
#include "fork.h" |
| 22 |
|
#include "menu.h" |
| 23 |
#include <stdio.h> |
#include <stdio.h> |
| 24 |
#include <string.h> |
#include <string.h> |
| 25 |
|
#include <signal.h> |
| 26 |
#include <unistd.h> |
#include <unistd.h> |
| 27 |
#include <stdlib.h> |
#include <stdlib.h> |
| 28 |
#include <errno.h> |
#include <errno.h> |
| 29 |
|
|
| 30 |
int fork_server() |
int fork_server(void) |
| 31 |
{ |
{ |
| 32 |
int pid; |
int pid; |
| 33 |
|
|
| 37 |
{ |
{ |
| 38 |
SYS_child_process_count++; |
SYS_child_process_count++; |
| 39 |
log_std("Child process (%d) start\n", pid); |
log_std("Child process (%d) start\n", pid); |
| 40 |
return 0; |
return pid; |
| 41 |
} |
} |
| 42 |
else if (pid < 0) // Error |
else if (pid < 0) // Error |
| 43 |
{ |
{ |
| 49 |
if (close(socket_server) == -1) |
if (close(socket_server) == -1) |
| 50 |
{ |
{ |
| 51 |
log_error("Close server socket failed\n"); |
log_error("Close server socket failed\n"); |
|
return -2; |
|
| 52 |
} |
} |
| 53 |
|
|
| 54 |
// Redirect Input |
// Redirect Input |
| 56 |
if (dup2(socket_client, STDIN_FILENO) == -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; |
goto cleanup; |
| 60 |
} |
} |
| 61 |
|
|
| 62 |
// Redirect Output |
// Redirect Output |
| 64 |
if (dup2(socket_client, STDOUT_FILENO) == -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; |
goto cleanup; |
| 68 |
} |
} |
| 69 |
|
|
| 70 |
|
SYS_child_process_count = 0; |
| 71 |
|
|
| 72 |
bbs_main(); |
bbs_main(); |
| 73 |
|
|
| 74 |
|
cleanup: |
| 75 |
// Child process exit |
// Child process exit |
| 76 |
SYS_server_exit = 1; |
SYS_server_exit = 1; |
| 77 |
|
|
| 85 |
close(STDOUT_FILENO); |
close(STDOUT_FILENO); |
| 86 |
|
|
| 87 |
log_std("Process exit normally\n"); |
log_std("Process exit normally\n"); |
|
|
|
| 88 |
log_end(); |
log_end(); |
| 89 |
|
|
| 90 |
|
_exit(0); |
| 91 |
|
|
| 92 |
return 0; |
return 0; |
| 93 |
} |
} |