| 20 |
#include "log.h" |
#include "log.h" |
| 21 |
#include "io.h" |
#include "io.h" |
| 22 |
#include "fork.h" |
#include "fork.h" |
| 23 |
|
#include <stdio.h> |
| 24 |
#include <string.h> |
#include <string.h> |
| 25 |
#include <unistd.h> |
#include <unistd.h> |
| 26 |
#include <stdlib.h> |
#include <stdlib.h> |
| 29 |
{ |
{ |
| 30 |
int pid; |
int pid; |
| 31 |
|
|
| 32 |
if (pid = fork()) |
pid = fork(); |
| 33 |
|
|
| 34 |
|
if (pid > 0) // Parent process |
| 35 |
{ |
{ |
| 36 |
SYS_child_process_count++; |
SYS_child_process_count++; |
| 37 |
log_std("Child process (%d) start\n", pid); |
log_std("Child process (%d) start\n", pid); |
| 38 |
return 0; |
return 0; |
| 39 |
} |
} |
| 40 |
else if (pid < 0) |
else if (pid < 0) // Error |
| 41 |
|
{ |
| 42 |
return -1; |
return -1; |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
// Child process |
| 46 |
if (close(socket_server) == -1) |
if (close(socket_server) == -1) |
| 47 |
{ |
{ |
| 48 |
log_error("Close server socket failed\n"); |
log_error("Close server socket failed\n"); |
| 50 |
} |
} |
| 51 |
|
|
| 52 |
// Redirect Input |
// Redirect Input |
| 53 |
close(0); |
close(STDIN_FILENO); |
| 54 |
if (dup2(socket_client, 0) == -1) |
if (dup2(socket_client, STDIN_FILENO) == -1) |
| 55 |
{ |
{ |
| 56 |
log_error("Redirect stdin to client socket failed\n"); |
log_error("Redirect stdin to client socket failed\n"); |
| 57 |
return -3; |
return -3; |
| 58 |
} |
} |
| 59 |
|
|
| 60 |
// Redirect Output |
// Redirect Output |
| 61 |
close(1); |
close(STDOUT_FILENO); |
| 62 |
if (dup2(socket_client, 1) == -1) |
if (dup2(socket_client, STDOUT_FILENO) == -1) |
| 63 |
{ |
{ |
| 64 |
log_error("Redirect stdout to client socket failed\n"); |
log_error("Redirect stdout to client socket failed\n"); |
| 65 |
return -4; |
return -4; |
| 73 |
} |
} |
| 74 |
|
|
| 75 |
// Close Input and Output for client |
// Close Input and Output for client |
| 76 |
close(0); |
close(STDIN_FILENO); |
| 77 |
close(1); |
close(STDOUT_FILENO); |
| 78 |
|
|
| 79 |
log_std("Process exit normally\n"); |
log_std("Process exit normally\n"); |
| 80 |
|
|