Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/***********************************************************************************************************************************
Server Command
***********************************************************************************************************************************/
#include <build.h>
 
#include <sys/wait.h>
#ifdef HAVE_LIBSYSTEMD
#include <systemd/sd-daemon.h>
#endif
 
#include "command/exit.h"
#include "command/remote/remote.h"
#include "command/server/server.h"
#include "common/debug.h"
#include "common/fork.h"
#include "common/io/socket/server.h"
#include "common/io/tls/server.h"
#include "config/config.h"
#include "config/load.h"
#include "protocol/helper.h"
 
/***********************************************************************************************************************************
Local variables
***********************************************************************************************************************************/
static struct ServerLocal
{
    MemContext *memContext;                                         // Mem context for server
 
    unsigned int argListSize;                                       // Argument list size
    const char **argList;                                           // Argument list
 
    List *processList;                                              // List of child processes
 
    bool sigHup;                                                    // SIGHUP was caught
    bool sigTerm;                                                   // SIGTERM was caught
 
    IoServer *socketServer;                                         // Socket server
    IoServer *tlsServer;                                            // TLS server
} serverLocal;
 
/***********************************************************************************************************************************
Initialization can be redone when options change
***********************************************************************************************************************************/
static void
cmdServerInit(void)
{
    // Initialize mem context
    if (serverLocal.memContext == NULL)
    {
        MEM_CONTEXT_BEGIN(memContextTop())
        {
            MEM_CONTEXT_NEW_BEGIN(Server, .childQty = MEM_CONTEXT_QTY_MAX)
            {
                serverLocal.memContext = MEM_CONTEXT_NEW();
                serverLocal.processList = lstNewP(sizeof(pid_t));
            }
            MEM_CONTEXT_NEW_END();
        }
        MEM_CONTEXT_END();
    }
 
    MEM_CONTEXT_BEGIN(serverLocal.memContext)
    {
        // Free old servers
        ioServerFree(serverLocal.socketServer);
        ioServerFree(serverLocal.tlsServer);
 
        // Create new servers
        serverLocal.socketServer = sckServerNew(
            cfgOptionStr(cfgOptTlsServerAddress), cfgOptionUInt(cfgOptTlsServerPort), cfgOptionUInt64(cfgOptProtocolTimeout));
        serverLocal.tlsServer = tlsServerNew(
            cfgOptionStr(cfgOptTlsServerAddress), cfgOptionStr(cfgOptTlsServerCaFile), cfgOptionStr(cfgOptTlsServerKeyFile),
            cfgOptionStr(cfgOptTlsServerCertFile), cfgOptionUInt64(cfgOptProtocolTimeout));
    }
    MEM_CONTEXT_END();
}
 
/***********************************************************************************************************************************
Handlers to set flags on signals
***********************************************************************************************************************************/
static void
cmdServerSigHup(const int signalType)
{
    (void)signalType;
    serverLocal.sigHup = true;
}
 
static void
cmdServerSigTerm(const int signalType)
{
    (void)signalType;
    serverLocal.sigTerm = true;
}
 
/***********************************************************************************************************************************
Handler to reap child processes
***********************************************************************************************************************************/
static void
cmdServerSigChild(const int signalType, siginfo_t *const signalInfo, void *const context)
{
    (void)signalType;
    (void)context;
 
    // Find the process and remove it
    for (unsigned int processIdx = 0; processIdx < lstSize(serverLocal.processList); processIdx++)
    {
        if (*(int *)lstGet(serverLocal.processList, processIdx) == signalInfo->si_pid)
        {
            ASSERT(signalInfo->si_code == CLD_EXITED);
            lstRemoveIdx(serverLocal.processList, processIdx);
        }
    }
}
 
/**********************************************************************************************************************************/
FN_EXTERN void
cmdServer(const unsigned int argListSize, const char *argList[])
{
    FUNCTION_LOG_BEGIN(logLevelDebug);
        FUNCTION_LOG_PARAM(UINT, argListSize);
        FUNCTION_LOG_PARAM(CHARPY, argList);
    FUNCTION_LOG_END();
 
    // Initialize server
    cmdServerInit();
 
    // Set arguments used for reload
    serverLocal.argListSize = argListSize;
    serverLocal.argList = argList;
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        // Set signal handlers
        sigaction(SIGHUP, &(struct sigaction){.sa_handler = cmdServerSigHup}, NULL);
        sigaction(SIGTERM, &(struct sigaction){.sa_handler = cmdServerSigTerm}, NULL);
        sigaction(
            SIGCHLD, &(struct sigaction){.sa_sigaction = cmdServerSigChild, .sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_SIGINFO},
            NULL);
 
        // Notify systemd that we are ready to accept connections
#ifdef HAVE_LIBSYSTEMD
        sd_notify(0, "READY=1");
#endif
 
        // Accept connections indefinitely. The only way to exit this loop is for the process to receive a signal.
        do
        {
            // Accept a new connection
            IoSession *const socketSession = ioServerAccept(serverLocal.socketServer, NULL);
 
            if (socketSession != NULL)
            {
                // Fork off the child process
                pid_t pid = forkSafe();
 
                if (pid == 0)
                {
                    // Reset SIGCHLD to default
                    sigaction(SIGCHLD, &(struct sigaction){.sa_handler = SIG_DFL}, NULL);
 
                    // Set standard signal handlers
                    exitInit();
 
                    // Close the server socket so we don't hold the port open if the parent exits first
                    ioServerFree(serverLocal.socketServer);
 
                    // Disable logging and close log file
                    logClose();
 
                    // Start standard remote processing if a server is returned
                    ProtocolServer *server = protocolServer(serverLocal.tlsServer, socketSession);
 
                    if (server != NULL)
                        cmdRemote(server);
 
                    break;
                }
                // Add process to list
                else
                    lstAdd(serverLocal.processList, &pid);
 
                // Free the socket since the child is now using it
                ioSessionFree(socketSession);
            }
 
            // Reload configuration
            if (serverLocal.sigHup)
            {
                LOG_DETAIL("configuration reload begin");
 
                // Reload configuration
                cfgLoad(serverLocal.argListSize, serverLocal.argList);
 
                // Reinitialize server
                cmdServerInit();
 
                LOG_DETAIL("configuration reload end");
 
                // Reset flag
                serverLocal.sigHup = false;
            }
        }
        while (!serverLocal.sigTerm);
    }
    MEM_CONTEXT_TEMP_END();
 
    // Notify systemd that we are shutting down
#ifdef HAVE_LIBSYSTEMD
    sd_notify(0, "STOPPING=1");
#endif
 
    // Terminate any remaining children on SIGTERM. Disable the callback so it does not fire in the middle of the loop.
    if (serverLocal.sigTerm)
    {
        sigaction(SIGCHLD, &(struct sigaction){.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT}, NULL);
 
        for (unsigned int processIdx = 0; processIdx < lstSize(serverLocal.processList); processIdx++)
        {
            pid_t pid = *(int *)lstGet(serverLocal.processList, processIdx);
 
            LOG_WARN_FMT("terminate child process %d", pid);
            kill(pid, SIGTERM);
        }
    }
 
    FUNCTION_LOG_RETURN_VOID();
}