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

Contents of /lbbs/src/fork.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.24 - (show annotations)
Wed Jun 4 13:42:53 2025 UTC (9 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.23: +2 -2 lines
Content type: text/x-csrc
Rename log_std() to log_common()

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(void)
31 {
32 int pid;
33
34 pid = fork();
35
36 if (pid > 0) // Parent process
37 {
38 SYS_child_process_count++;
39 log_common("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 }
53
54 // Redirect Input
55 close(STDIN_FILENO);
56 if (dup2(socket_client, STDIN_FILENO) == -1)
57 {
58 log_error("Redirect stdin to client socket failed\n");
59 goto cleanup;
60 }
61
62 // Redirect Output
63 close(STDOUT_FILENO);
64 if (dup2(socket_client, STDOUT_FILENO) == -1)
65 {
66 log_error("Redirect stdout to client socket failed\n");
67 goto cleanup;
68 }
69
70 SYS_child_process_count = 0;
71
72 bbs_main();
73
74 cleanup:
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_common("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