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

Contents of /lbbs/src/fork.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (show annotations)
Sun Jun 1 03:07:42 2025 UTC (9 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.21: +1 -1 lines
Content type: text/x-csrc
Add max connection and connection per ip limit

1 /***************************************************************************
2 fork.c - description
3 -------------------
4 Copyright : (C) 2004-2025 by Leaflet
5 Email : leaflet@leafok.com
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * 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 *
12 * the Free Software Foundation; either version 3 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17 #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 fork_server()
31 {
32 int pid;
33
34 pid = fork();
35
36 if (pid > 0) // Parent process
37 {
38 SYS_child_process_count++;
39 log_std("Child process (%d) start\n", pid);
40 return pid;
41 }
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 bbs_main();
74
75 // Child process exit
76 SYS_server_exit = 1;
77
78 if (close(socket_client) == -1)
79 {
80 log_error("Close client socket failed\n");
81 }
82
83 // Close Input and Output for client
84 close(STDIN_FILENO);
85 close(STDOUT_FILENO);
86
87 log_std("Process exit normally\n");
88 log_end();
89
90 _exit(0);
91
92 return 0;
93 }

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