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

Contents of /lbbs/src/fork.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations)
Sat May 3 13:41:21 2025 UTC (10 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.13: +14 -8 lines
Content type: text/x-csrc
Refine

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

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