| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
fork.c - description |
fork.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
Copyright : (C) 2004-2025 by Leaflet |
| 5 |
copyright : (C) 2004 by Leaflet |
Email : leaflet@leafok.com |
|
email : leaflet@leafok.com |
|
| 6 |
***************************************************************************/ |
***************************************************************************/ |
| 7 |
|
|
| 8 |
/*************************************************************************** |
/*************************************************************************** |
| 9 |
* * |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
| 11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
| 12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 3 of the License, or * |
| 13 |
* (at your option) any later version. * |
* (at your option) any later version. * |
| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "common.h" |
#include "common.h" |
| 18 |
|
#include "bbs_main.h" |
| 19 |
|
#include "log.h" |
| 20 |
|
#include "io.h" |
| 21 |
|
#include "fork.h" |
| 22 |
|
#include <stdio.h> |
| 23 |
|
#include <string.h> |
| 24 |
|
#include <unistd.h> |
| 25 |
|
#include <stdlib.h> |
| 26 |
|
#include <errno.h> |
| 27 |
|
|
| 28 |
int |
int fork_server() |
|
fork_server(int sock_server, int sock_client, struct sockaddr_in * p_sin) |
|
| 29 |
{ |
{ |
| 30 |
int pid; |
int pid; |
|
|
|
|
if (pid = fork ()) |
|
|
return 0; |
|
|
else if (pid < 0) |
|
|
return -1; |
|
|
|
|
|
log_std ("Child process start\n"); |
|
|
|
|
|
if (close(sock_server) == -1) |
|
|
{ |
|
|
log_error("Close server socket failed\n"); |
|
|
return -2; |
|
|
} |
|
|
|
|
|
socket_client = sock_client; |
|
|
strcpy(hostaddr_client,(const char *)inet_ntoa(p_sin->sin_addr)); |
|
|
port_client = ntohs(p_sin->sin_port); |
|
|
|
|
|
//Redirect Output |
|
|
close(1); |
|
|
if (dup2 (socket_client, 1) == -1) |
|
|
{ |
|
|
log_error("Redirect stdout to client socket failed\n"); |
|
|
return -3; |
|
|
} |
|
|
|
|
|
bbs_main(); |
|
|
|
|
|
if (close(sock_client) == -1) |
|
|
{ |
|
|
log_error("Close client socket failed\n"); |
|
|
} |
|
| 31 |
|
|
| 32 |
log_std ("Child process exit\n"); |
pid = fork(); |
| 33 |
|
|
| 34 |
log_end(); |
if (pid > 0) // Parent process |
| 35 |
|
{ |
| 36 |
|
SYS_child_process_count++; |
| 37 |
|
log_std("Child process (%d) start\n", pid); |
| 38 |
|
return 0; |
| 39 |
|
} |
| 40 |
|
else if (pid < 0) // Error |
| 41 |
|
{ |
| 42 |
|
log_error("fork() error (%d)\n", errno); |
| 43 |
|
return -1; |
| 44 |
|
} |
| 45 |
|
|
| 46 |
|
// Child process |
| 47 |
|
if (close(socket_server) == -1) |
| 48 |
|
{ |
| 49 |
|
log_error("Close server socket failed\n"); |
| 50 |
|
return -2; |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
// Redirect Input |
| 54 |
|
close(STDIN_FILENO); |
| 55 |
|
if (dup2(socket_client, STDIN_FILENO) == -1) |
| 56 |
|
{ |
| 57 |
|
log_error("Redirect stdin to client socket failed\n"); |
| 58 |
|
return -3; |
| 59 |
|
} |
| 60 |
|
|
| 61 |
|
// Redirect Output |
| 62 |
|
close(STDOUT_FILENO); |
| 63 |
|
if (dup2(socket_client, STDOUT_FILENO) == -1) |
| 64 |
|
{ |
| 65 |
|
log_error("Redirect stdout to client socket failed\n"); |
| 66 |
|
return -4; |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
bbs_main(); |
| 70 |
|
|
| 71 |
|
// Child process exit |
| 72 |
|
SYS_server_exit = 1; |
| 73 |
|
|
| 74 |
|
if (close(socket_client) == -1) |
| 75 |
|
{ |
| 76 |
|
log_error("Close client socket failed\n"); |
| 77 |
|
} |
| 78 |
|
|
| 79 |
|
// Close Input and Output for client |
| 80 |
|
close(STDIN_FILENO); |
| 81 |
|
close(STDOUT_FILENO); |
| 82 |
|
|
| 83 |
//Exit child process normally |
log_std("Process exit normally\n"); |
|
exit(0); |
|
| 84 |
|
|
| 85 |
return 0; |
log_end(); |
| 86 |
|
|
| 87 |
|
return 0; |
| 88 |
} |
} |