/[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.7 by sysadm, Fri Oct 22 15:20:32 2004 UTC Revision 1.15 by sysadm, Tue May 6 05:31:26 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"  #include "io.h"
21    #include "fork.h"
22    #include <stdio.h>
23  #include <string.h>  #include <string.h>
24    #include <unistd.h>
25    #include <stdlib.h>
26    
27  int  int fork_server()
 fork_server()  
28  {  {
29    int pid;          int pid;
         
   if (pid = fork ())  
     return 0;  
   else if (pid < 0)  
     return -1;  
   
   log_std ("Child process start\n");  
     
   if (close(socket_server) == -1)  
     {  
       log_error("Close server socket failed\n");  
       return -2;  
     }  
   
   //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(socket_client) == -1)  
     {  
       log_error("Close client socket failed\n");  
     }  
30    
31    log_std ("Child process exit\n");          pid = fork();
32    
33    log_end();          if (pid > 0) // Parent process
34            {
35                    SYS_child_process_count++;
36                    log_std("Child process (%d) start\n", pid);
37                    return 0;
38            }
39            else if (pid < 0) // Error
40            {
41                    return -1;
42            }
43    
44            // Child process
45            if (close(socket_server) == -1)
46            {
47                    log_error("Close server socket failed\n");
48                    return -2;
49            }
50    
51            // Redirect Input
52            close(STDIN_FILENO);
53            if (dup2(socket_client, STDIN_FILENO) == -1)
54            {
55                    log_error("Redirect stdin to client socket failed\n");
56                    return -3;
57            }
58    
59            // Redirect Output
60            close(STDOUT_FILENO);
61            if (dup2(socket_client, STDOUT_FILENO) == -1)
62            {
63                    log_error("Redirect stdout to client socket failed\n");
64                    return -4;
65            }
66    
67            bbs_main();
68    
69            if (close(socket_client) == -1)
70            {
71                    log_error("Close client socket failed\n");
72            }
73    
74            // Close Input and Output for client
75            close(STDIN_FILENO);
76            close(STDOUT_FILENO);
77    
78    //Exit child process normally          log_std("Process exit normally\n");
   exit(0);  
79    
80    return 0;          log_end();
81    
82            // Exit child process normally
83            exit(0);
84    
85            return 0;
86  }  }


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

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