/[LeafOK_CVS]/lbbs/src/fork.c
ViewVC logotype

Diff of /lbbs/src/fork.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.5 by sysadm, Tue Oct 19 02:12:28 2004 UTC Revision 1.19 by sysadm, Thu May 15 05:14:57 2025 UTC
# Line 1  Line 1 
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 "menu.h"
23    #include <stdio.h>
24    #include <string.h>
25    #include <signal.h>
26    #include <unistd.h>
27    #include <stdlib.h>
28    #include <errno.h>
29    
30  int  int fork_server()
 fork_server(int sock_server, int sock_client, struct sockaddr_in * p_sin)  
31  {  {
32    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");  
     }  
     
   socket_client = sock_client;  
   strcpy(hostaddr_client,inet_ntoa(p_sin->sin_addr));  
   port_client = ntohs(p_sin->sin_port);  
   
   bbs_main();  
     
   if (close(sock_client) == -1)  
     {  
       log_error("Close client socket failed\n");  
     }  
33    
34    log_std ("Child process exit\n");          pid = fork();
35    
36    log_end();          if (pid > 0) // Parent process
37            {
38    //Exit child process normally                  SYS_child_process_count++;
39    exit(0);                  log_std("Child process (%d) start\n", pid);
40                    return 0;
41    return 0;          }
42            else if (pid < 0) // Error
43            {
44                    log_error("fork() error (%d)\n", errno);
45                    return -1;
46            }
47    
48            // Child process
49            if (close(socket_server) == -1)
50            {
51                    log_error("Close server socket failed\n");
52                    return -2;
53            }
54    
55            // Redirect Input
56            close(STDIN_FILENO);
57            if (dup2(socket_client, STDIN_FILENO) == -1)
58            {
59                    log_error("Redirect stdin to client socket failed\n");
60                    return -3;
61            }
62    
63            // Redirect Output
64            close(STDOUT_FILENO);
65            if (dup2(socket_client, STDOUT_FILENO) == -1)
66            {
67                    log_error("Redirect stdout to client socket failed\n");
68                    return -4;
69            }
70    
71            SYS_child_process_count = 0;
72    
73            // Reset signal handler to default
74            signal(SIGHUP, SIG_DFL);
75            signal(SIGCHLD, SIG_DFL);
76            signal(SIGTERM, SIG_DFL);
77            
78            bbs_main();
79    
80            // Child process exit
81            SYS_server_exit = 1;
82    
83            if (close(socket_client) == -1)
84            {
85                    log_error("Close client socket failed\n");
86            }
87    
88            // Close Input and Output for client
89            close(STDIN_FILENO);
90            close(STDOUT_FILENO);
91    
92            // Unload menu
93            unload_menu_shm(p_bbs_menu);
94            free(p_bbs_menu);
95            p_bbs_menu = NULL;
96    
97            log_std("Process exit normally\n");
98            log_end();
99    
100            _exit(0);
101            
102            return 0;
103  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1