rev |
line source |
jbe@58
|
1
|
jbe@58
|
2 /*** Version ***/
|
jbe@229
|
3 #define MOONBR_VERSION_STRING "1.0.1"
|
jbe@58
|
4
|
jbe@0
|
5
|
jbe@0
|
6 /*** Compile-time configuration ***/
|
jbe@0
|
7
|
jbe@0
|
8 #define MOONBR_LUA_PANIC_BUG_WORKAROUND 1
|
jbe@0
|
9
|
jbe@0
|
10
|
jbe@0
|
11 /*** C preprocessor macros for portability support ***/
|
jbe@0
|
12
|
jbe@0
|
13 #ifndef __has_include
|
jbe@0
|
14 #define __has_include(x) 0
|
jbe@0
|
15 #endif
|
jbe@0
|
16
|
jbe@0
|
17
|
jbe@0
|
18 /*** Include directives for used system libraries ***/
|
jbe@0
|
19
|
jbe@0
|
20 #include <stdlib.h>
|
jbe@0
|
21 #include <stdint.h>
|
jbe@135
|
22 #include <string.h>
|
jbe@0
|
23 #include <errno.h>
|
jbe@135
|
24 #include <unistd.h>
|
jbe@135
|
25 #include <signal.h>
|
jbe@135
|
26 #include <sys/wait.h>
|
jbe@135
|
27 #include <sys/resource.h>
|
jbe@135
|
28 #include <poll.h>
|
jbe@136
|
29 #include <time.h>
|
jbe@0
|
30 #include <sys/time.h>
|
jbe@0
|
31 #include <sys/socket.h>
|
jbe@0
|
32 #include <sys/un.h>
|
jbe@0
|
33 #include <netinet/in.h>
|
jbe@125
|
34 #include <netdb.h>
|
jbe@125
|
35 #include <arpa/inet.h>
|
jbe@135
|
36 #include <getopt.h>
|
jbe@0
|
37 #include <sys/file.h>
|
jbe@135
|
38 #include <syslog.h>
|
jbe@0
|
39 #if defined(__FreeBSD__) || __has_include(<libutil.h>)
|
jbe@0
|
40 #include <libutil.h>
|
jbe@0
|
41 #endif
|
jbe@0
|
42 #if defined(__linux__) || __has_include(<bsd/libutil.h>)
|
jbe@0
|
43 #include <bsd/libutil.h>
|
jbe@0
|
44 #endif
|
jbe@0
|
45 #if defined(__linux__) || __has_include(<bsd/unistd.h>)
|
jbe@0
|
46 #include <bsd/unistd.h>
|
jbe@0
|
47 #endif
|
jbe@0
|
48
|
jbe@0
|
49
|
jbe@0
|
50 /*** Fallback definitions for missing constants on some platforms ***/
|
jbe@0
|
51
|
jbe@0
|
52 /* INFTIM is used as timeout parameter for poll() */
|
jbe@0
|
53 #ifndef INFTIM
|
jbe@0
|
54 #define INFTIM -1
|
jbe@0
|
55 #endif
|
jbe@0
|
56
|
jbe@0
|
57
|
jbe@0
|
58 /*** Include directives for Lua ***/
|
jbe@0
|
59
|
jbe@0
|
60 #include <lua.h>
|
jbe@0
|
61 #include <lauxlib.h>
|
jbe@0
|
62 #include <lualib.h>
|
jbe@0
|
63
|
jbe@0
|
64
|
jbe@79
|
65 /*** Include directive for moonbridge_io library ***/
|
jbe@79
|
66
|
jbe@79
|
67 #include "moonbridge_io.h"
|
jbe@79
|
68
|
jbe@79
|
69
|
jbe@0
|
70 /*** Constants ***/
|
jbe@0
|
71
|
jbe@0
|
72 /* Backlog option for listen() call */
|
jbe@0
|
73 #define MOONBR_LISTEN_BACKLOG 1024
|
jbe@0
|
74
|
jbe@0
|
75 /* Maximum length of a timestamp used for strftime() */
|
jbe@0
|
76 #define MOONBR_LOG_MAXTIMELEN 40
|
jbe@0
|
77
|
jbe@0
|
78 /* Maximum length of a log message */
|
jbe@0
|
79 #define MOONBR_LOG_MAXMSGLEN 4095
|
jbe@0
|
80
|
jbe@0
|
81 /* Exitcodes passed to exit() call */
|
jbe@0
|
82 #define MOONBR_EXITCODE_GRACEFUL 0
|
jbe@0
|
83 #define MOONBR_EXITCODE_CMDLINEERROR 1
|
jbe@0
|
84 #define MOONBR_EXITCODE_ALREADYRUNNING 2
|
jbe@0
|
85 #define MOONBR_EXITCODE_STARTUPERROR 3
|
jbe@0
|
86 #define MOONBR_EXITCODE_RUNTIMEERROR 4
|
jbe@0
|
87
|
jbe@0
|
88 /* Maximum length of a line sent to stderr by child processes */
|
jbe@0
|
89 #define MOONBR_MAXERRORLINELEN 1024
|
jbe@0
|
90
|
jbe@0
|
91 /* Maximum length of an error string returned by strerror() */
|
jbe@0
|
92 #define MOONBR_MAXSTRERRORLEN 80
|
jbe@0
|
93
|
jbe@213
|
94 /* Error message for noncompliant strerror_r() implementation on GNU systems */
|
jbe@213
|
95 #define MOONBR_STRERROR_R_MSG "Error detail unavailable due to noncompliant strerror_r() implementation"
|
jbe@213
|
96
|
jbe@0
|
97 /* Status bytes exchanged between master and child processes */
|
jbe@124
|
98 #define MOONBR_STATUS_IDLE 'I'
|
jbe@124
|
99 #define MOONBR_COMMAND_CONNECT 'C'
|
jbe@124
|
100 #define MOONBR_COMMAND_TERMINATE 'T'
|
jbe@124
|
101 #define MOONBR_STATUS_GOODBYE 'B'
|
jbe@0
|
102
|
jbe@0
|
103 /* Constant file descriptors */
|
jbe@0
|
104 #define MOONBR_FD_STDERR 2
|
jbe@0
|
105 #define MOONBR_FD_CONTROL 3
|
jbe@0
|
106 #define MOONBR_FD_END 4
|
jbe@0
|
107
|
jbe@0
|
108 /* Return values of moonbr_try_destroy_worker() */
|
jbe@0
|
109 #define MOONBR_DESTROY_NONE 0
|
jbe@0
|
110 #define MOONBR_DESTROY_PREPARE 1
|
jbe@0
|
111 #define MOONBR_DESTROY_IDLE_OR_ASSIGNED 2
|
jbe@0
|
112
|
jbe@0
|
113
|
jbe@0
|
114 /*** Types ***/
|
jbe@0
|
115
|
jbe@0
|
116 /* Enum for 'moonbr_pstate' */
|
jbe@0
|
117 #define MOONBR_PSTATE_STARTUP 0
|
jbe@0
|
118 #define MOONBR_PSTATE_RUNNING 1
|
jbe@0
|
119 #define MOONBR_PSTATE_FORKED 2
|
jbe@0
|
120
|
jbe@0
|
121 /* Enum for 'proto' field of struct moonbr_listener */
|
jbe@127
|
122 #define MOONBR_PROTO_MAIN 1
|
jbe@127
|
123 #define MOONBR_PROTO_INTERVAL 2
|
jbe@127
|
124 #define MOONBR_PROTO_LOCAL 3
|
jbe@127
|
125 #define MOONBR_PROTO_TCP 4
|
jbe@0
|
126
|
jbe@0
|
127 /* Data structure for a pool's listener that can accept incoming connections */
|
jbe@0
|
128 struct moonbr_listener {
|
jbe@0
|
129 struct moonbr_pool *pool;
|
jbe@0
|
130 struct moonbr_listener *prev_listener; /* previous idle or(!) connected listener */
|
jbe@0
|
131 struct moonbr_listener *next_listener; /* next idle or(!) connected listener */
|
jbe@0
|
132 int proto;
|
jbe@0
|
133 union {
|
jbe@0
|
134 struct {
|
jbe@0
|
135 char *name; /* name of interval passed to 'connect' function as 'interval' field in table */
|
jbe@0
|
136 int strict; /* nonzero = runtime of 'connect' function does not delay interval */
|
jbe@0
|
137 struct timeval delay; /* interval between invocations of 'connect' function */
|
jbe@0
|
138 struct timeval wakeup; /* point in time of next invocation */
|
jbe@0
|
139 } interval;
|
jbe@0
|
140 struct {
|
jbe@125
|
141 union {
|
jbe@125
|
142 struct sockaddr addr_abstract;
|
jbe@125
|
143 struct sockaddr_un addr_un;
|
jbe@125
|
144 struct sockaddr_in addr_in;
|
jbe@125
|
145 struct sockaddr_in6 addr_in6;
|
jbe@125
|
146 } addr;
|
jbe@125
|
147 socklen_t addrlen;
|
jbe@125
|
148 } socket;
|
jbe@125
|
149 } type_specific;
|
jbe@125
|
150 union {
|
jbe@0
|
151 struct {
|
jbe@125
|
152 char ip[INET6_ADDRSTRLEN]; /* IP to listen on */
|
jbe@0
|
153 int port; /* port number to listen on (in host endianess) */
|
jbe@0
|
154 } tcp;
|
jbe@0
|
155 } proto_specific;
|
jbe@0
|
156 int listenfd; /* -1 = none */
|
jbe@0
|
157 int pollidx; /* -1 = none */
|
jbe@0
|
158 };
|
jbe@0
|
159
|
jbe@0
|
160 /* Data structure for a child process that is handling incoming connections */
|
jbe@0
|
161 struct moonbr_worker {
|
jbe@0
|
162 struct moonbr_pool *pool;
|
jbe@0
|
163 struct moonbr_worker *prev_worker;
|
jbe@0
|
164 struct moonbr_worker *next_worker;
|
jbe@0
|
165 struct moonbr_worker *prev_idle_worker;
|
jbe@0
|
166 struct moonbr_worker *next_idle_worker;
|
jbe@127
|
167 int main; /* nonzero = terminate Moonbridge when this worker dies */
|
jbe@0
|
168 int idle; /* nonzero = waiting for command from parent process */
|
jbe@0
|
169 int assigned; /* nonzero = currently handling a connection */
|
jbe@0
|
170 pid_t pid;
|
jbe@0
|
171 int controlfd; /* socket to send/receive control message to/from child process */
|
jbe@0
|
172 int errorfd; /* socket to receive error output from child process' stderr */
|
jbe@0
|
173 char *errorlinebuf; /* optional buffer for collecting stderr data from child process */
|
jbe@0
|
174 int errorlinelen; /* number of bytes stored in 'errorlinebuf' */
|
jbe@0
|
175 int errorlineovf; /* nonzero = line length overflow */
|
jbe@0
|
176 struct timeval idle_expiration; /* point in time until child process may stay in idle state */
|
jbe@0
|
177 struct moonbr_listener *restart_interval_listener; /* set while interval listener is assigned */
|
jbe@0
|
178 };
|
jbe@0
|
179
|
jbe@0
|
180 /* Data structure for a pool of workers and listeners */
|
jbe@0
|
181 struct moonbr_pool {
|
jbe@0
|
182 int poolnum; /* number of pool for log output */
|
jbe@0
|
183 struct moonbr_pool *next_pool; /* next entry in linked list starting with 'moonbr_first_pool' */
|
jbe@0
|
184 struct moonbr_worker *first_worker; /* first worker of pool */
|
jbe@0
|
185 struct moonbr_worker *last_worker; /* last worker of pool */
|
jbe@0
|
186 struct moonbr_worker *first_idle_worker; /* first idle worker of pool */
|
jbe@0
|
187 struct moonbr_worker *last_idle_worker; /* last idle worker of pool */
|
jbe@0
|
188 int idle_worker_count;
|
jbe@0
|
189 int unassigned_worker_count;
|
jbe@0
|
190 int total_worker_count;
|
jbe@0
|
191 int worker_count_stat; /* only needed for statistics */
|
jbe@0
|
192 int pre_fork; /* desired minimum number of unassigned workers */
|
jbe@0
|
193 int min_fork; /* desired minimum number of workers in total */
|
jbe@0
|
194 int max_fork; /* maximum number of workers */
|
jbe@0
|
195 struct timeval fork_delay; /* delay after each fork() until a fork may happen again */
|
jbe@0
|
196 struct timeval fork_wakeup; /* point in time when a fork may happen again (unless a worker terminates before) */
|
jbe@0
|
197 struct timeval fork_error_delay; /* delay between fork()s when an error during fork or preparation occurred */
|
jbe@0
|
198 struct timeval fork_error_wakeup; /* point in time when fork may happen again if an error in preparation occurred */
|
jbe@0
|
199 int use_fork_error_wakeup; /* nonzero = error in preparation occured; gets reset on next fork */
|
jbe@0
|
200 struct timeval exit_delay; /* delay for terminating excessive workers (unassigned_worker_count > pre_fork) */
|
jbe@0
|
201 struct timeval exit_wakeup; /* point in time when terminating an excessive worker */
|
jbe@0
|
202 struct timeval idle_timeout; /* delay before an idle worker is terminated */
|
jbe@0
|
203 size_t memory_limit; /* maximum bytes of memory that the Lua machine may allocate */
|
jbe@0
|
204 int listener_count; /* total number of listeners of pool (and size of 'listener' array at end of this struct) */
|
jbe@0
|
205 struct moonbr_listener *first_idle_listener; /* first listener that is idle (i.e. has no waiting connection) */
|
jbe@0
|
206 struct moonbr_listener *last_idle_listener; /* last listener that is idle (i.e. has no waiting connection) */
|
jbe@0
|
207 struct moonbr_listener *first_connected_listener; /* first listener that has a pending connection */
|
jbe@0
|
208 struct moonbr_listener *last_connected_listener; /* last listener that has a pending connection */
|
jbe@0
|
209 struct moonbr_listener listener[1]; /* static array of variable(!) size to contain 'listener' structures */
|
jbe@0
|
210 };
|
jbe@0
|
211
|
jbe@0
|
212 /* Enum for 'channel' field of struct moonbr_poll_worker */
|
jbe@0
|
213 #define MOONBR_POLL_WORKER_CONTROLCHANNEL 1
|
jbe@0
|
214 #define MOONBR_POLL_WORKER_ERRORCHANNEL 2
|
jbe@0
|
215
|
jbe@0
|
216 /* Structure to refer from 'moonbr_poll_worker_fds' entry to worker structure */
|
jbe@0
|
217 struct moonbr_poll_worker {
|
jbe@0
|
218 struct moonbr_worker *worker;
|
jbe@0
|
219 int channel; /* field indicating whether file descriptor is 'controlfd' or 'errorfd' */
|
jbe@0
|
220 };
|
jbe@0
|
221
|
jbe@0
|
222 /* Variable indicating that clean shutdown was requested */
|
jbe@0
|
223 static int moonbr_shutdown_in_progress = 0;
|
jbe@0
|
224
|
jbe@0
|
225
|
jbe@0
|
226 /*** Macros for Lua registry ***/
|
jbe@0
|
227
|
jbe@0
|
228 /* Lightuserdata keys for Lua registry to store 'prepare', 'connect', and 'finish' functions */
|
jbe@0
|
229 #define moonbr_luakey_prepare_func(pool) ((void *)(intptr_t)(pool) + 0)
|
jbe@0
|
230 #define moonbr_luakey_connect_func(pool) ((void *)(intptr_t)(pool) + 1)
|
jbe@0
|
231 #define moonbr_luakey_finish_func(pool) ((void *)(intptr_t)(pool) + 2)
|
jbe@0
|
232
|
jbe@0
|
233
|
jbe@0
|
234 /*** Global variables ***/
|
jbe@0
|
235
|
jbe@0
|
236 /* State of process execution */
|
jbe@0
|
237 static int moonbr_pstate = MOONBR_PSTATE_STARTUP;
|
jbe@0
|
238
|
jbe@0
|
239 /* Process ID of the main process */
|
jbe@0
|
240 static pid_t moonbr_masterpid;
|
jbe@0
|
241
|
jbe@0
|
242 /* Condition variables set by the signal handler */
|
jbe@0
|
243 static volatile sig_atomic_t moonbr_cond_poll = 0;
|
jbe@0
|
244 static volatile sig_atomic_t moonbr_cond_terminate = 0;
|
jbe@0
|
245 static volatile sig_atomic_t moonbr_cond_interrupt = 0;
|
jbe@0
|
246 static volatile sig_atomic_t moonbr_cond_child = 0;
|
jbe@0
|
247
|
jbe@0
|
248 /* Socket pair to denote signal delivery when signal handler was called just before poll() */
|
jbe@0
|
249 static int moonbr_poll_signalfds[2];
|
jbe@0
|
250 #define moonbr_poll_signalfd_read moonbr_poll_signalfds[0]
|
jbe@0
|
251 #define moonbr_poll_signalfd_write moonbr_poll_signalfds[1]
|
jbe@0
|
252
|
jbe@0
|
253 /* Global variables for pidfile and logging */
|
jbe@0
|
254 static struct pidfh *moonbr_pidfh = NULL;
|
jbe@0
|
255 static FILE *moonbr_logfile = NULL;
|
jbe@0
|
256 static int moonbr_use_syslog = 0;
|
jbe@0
|
257
|
jbe@0
|
258 /* First and last entry of linked list of all created pools during initialization */
|
jbe@0
|
259 static struct moonbr_pool *moonbr_first_pool = NULL;
|
jbe@0
|
260 static struct moonbr_pool *moonbr_last_pool = NULL;
|
jbe@0
|
261
|
jbe@0
|
262 /* Total count of pools */
|
jbe@0
|
263 static int moonbr_pool_count = 0;
|
jbe@0
|
264
|
jbe@0
|
265 /* Set to a nonzero value if dynamic part of 'moonbr_poll_fds' ('moonbr_poll_worker_fds') needs an update */
|
jbe@0
|
266 static int moonbr_poll_refresh_needed = 0;
|
jbe@0
|
267
|
jbe@0
|
268 /* Array passed to poll(), consisting of static part and dynamic part ('moonbr_poll_worker_fds') */
|
jbe@0
|
269 static struct pollfd *moonbr_poll_fds = NULL; /* the array */
|
jbe@0
|
270 static int moonbr_poll_fds_bufsize = 0; /* memory allocated for this number of elements */
|
jbe@0
|
271 static int moonbr_poll_fds_count = 0; /* total number of elements */
|
jbe@0
|
272 static int moonbr_poll_fds_static_count; /* number of elements in static part */
|
jbe@0
|
273
|
jbe@0
|
274 /* Dynamic part of 'moonbr_poll_fds' array */
|
jbe@0
|
275 #define moonbr_poll_worker_fds (moonbr_poll_fds+moonbr_poll_fds_static_count)
|
jbe@0
|
276
|
jbe@0
|
277 /* Additional information for dynamic part of 'moonbr_poll_fds' array */
|
jbe@0
|
278 struct moonbr_poll_worker *moonbr_poll_workers; /* the array */
|
jbe@0
|
279 static int moonbr_poll_workers_bufsize = 0; /* memory allocated for this number of elements */
|
jbe@0
|
280 static int moonbr_poll_worker_count = 0; /* number of elements in array */
|
jbe@0
|
281
|
jbe@0
|
282 /* Variable set to nonzero value to disallow further calls of 'listen' function */
|
jbe@0
|
283 static int moonbr_booted = 0;
|
jbe@0
|
284
|
jbe@0
|
285 /* Verbosity settings */
|
jbe@0
|
286 static int moonbr_debug = 0;
|
jbe@0
|
287 static int moonbr_stat = 0;
|
jbe@0
|
288
|
jbe@0
|
289 /* Memory consumption by Lua machine */
|
jbe@0
|
290 static size_t moonbr_memory_usage = 0;
|
jbe@0
|
291 static size_t moonbr_memory_limit = 0;
|
jbe@0
|
292
|
jbe@0
|
293
|
jbe@0
|
294 /*** Functions for signal handling ***/
|
jbe@0
|
295
|
jbe@0
|
296 /* Signal handler for master and child processes */
|
jbe@0
|
297 static void moonbr_signal(int sig) {
|
jbe@0
|
298 if (getpid() == moonbr_masterpid) {
|
jbe@0
|
299 /* master process */
|
jbe@0
|
300 switch (sig) {
|
jbe@0
|
301 case SIGHUP:
|
jbe@0
|
302 case SIGINT:
|
jbe@0
|
303 /* fast shutdown requested */
|
jbe@0
|
304 moonbr_cond_interrupt = 1;
|
jbe@0
|
305 break;
|
jbe@0
|
306 case SIGTERM:
|
jbe@0
|
307 /* clean shutdown requested */
|
jbe@0
|
308 moonbr_cond_terminate = 1;
|
jbe@0
|
309 break;
|
jbe@0
|
310 case SIGCHLD:
|
jbe@0
|
311 /* child process terminated */
|
jbe@0
|
312 moonbr_cond_child = 1;
|
jbe@0
|
313 break;
|
jbe@0
|
314 }
|
jbe@0
|
315 if (moonbr_cond_poll) {
|
jbe@0
|
316 /* avoid race condition if signal handler is invoked right before poll() */
|
jbe@0
|
317 char buf[1] = {0};
|
jbe@20
|
318 write(moonbr_poll_signalfd_write, buf, 1);
|
jbe@0
|
319 }
|
jbe@0
|
320 } else {
|
jbe@0
|
321 /* child process forwards certain signals to parent process */
|
jbe@0
|
322 switch (sig) {
|
jbe@0
|
323 case SIGHUP:
|
jbe@0
|
324 case SIGINT:
|
jbe@0
|
325 case SIGTERM:
|
jbe@0
|
326 kill(moonbr_masterpid, sig);
|
jbe@0
|
327 }
|
jbe@0
|
328 }
|
jbe@0
|
329 }
|
jbe@0
|
330
|
jbe@0
|
331 /* Initialize signal handling */
|
jbe@0
|
332 static void moonbr_signal_init(){
|
jbe@0
|
333 moonbr_masterpid = getpid();
|
jbe@0
|
334 signal(SIGHUP, moonbr_signal);
|
jbe@0
|
335 signal(SIGINT, moonbr_signal);
|
jbe@0
|
336 signal(SIGTERM, moonbr_signal);
|
jbe@0
|
337 signal(SIGCHLD, moonbr_signal);
|
jbe@223
|
338 /* signal(SIGUSR1, moonbr_signal); */ /* might be used to terminate children gracefully */
|
jbe@0
|
339 }
|
jbe@0
|
340
|
jbe@0
|
341
|
jbe@0
|
342 /*** Functions for logging in master process ***/
|
jbe@0
|
343
|
jbe@0
|
344 /* Logs a pre-formatted message with given syslog() priority */
|
jbe@0
|
345 static void moonbr_log_msg(int priority, const char *msg) {
|
jbe@0
|
346 if (moonbr_logfile) {
|
jbe@0
|
347 /* logging to logfile desired (timestamp is prepended in that case) */
|
jbe@0
|
348 time_t now_time = 0;
|
jbe@0
|
349 struct tm now_tmstruct;
|
jbe@0
|
350 char timestr[MOONBR_LOG_MAXTIMELEN+1];
|
jbe@0
|
351 time(&now_time);
|
jbe@0
|
352 localtime_r(&now_time, &now_tmstruct);
|
jbe@0
|
353 if (!strftime(
|
jbe@0
|
354 timestr, MOONBR_LOG_MAXTIMELEN+1, "%Y-%m-%d %H:%M:%S %Z: ", &now_tmstruct
|
jbe@0
|
355 )) timestr[0] = 0;
|
jbe@0
|
356 fprintf(moonbr_logfile, "%s%s\n", timestr, msg);
|
jbe@0
|
357 }
|
jbe@0
|
358 if (moonbr_use_syslog) {
|
jbe@0
|
359 /* logging through syslog desired */
|
jbe@0
|
360 syslog(priority, "%s", msg);
|
jbe@0
|
361 }
|
jbe@0
|
362 }
|
jbe@0
|
363
|
jbe@0
|
364 /* Formats a message via vsnprintf() and logs it with given syslog() priority */
|
jbe@0
|
365 static void moonbr_log(int priority, const char *message, ...) {
|
jbe@0
|
366 char msgbuf[MOONBR_LOG_MAXMSGLEN+1]; /* buffer of static size to store formatted message */
|
jbe@0
|
367 int msglen; /* length of full message (may exceed MOONBR_LOG_MAXMSGLEN) */
|
jbe@0
|
368 {
|
jbe@0
|
369 /* pass variable arguments to vsnprintf() to format message */
|
jbe@0
|
370 va_list ap;
|
jbe@0
|
371 va_start(ap, message);
|
jbe@0
|
372 msglen = vsnprintf(msgbuf, MOONBR_LOG_MAXMSGLEN+1, message, ap);
|
jbe@0
|
373 va_end(ap);
|
jbe@0
|
374 }
|
jbe@0
|
375 {
|
jbe@0
|
376 /* split and log message line by line */
|
jbe@0
|
377 char *line = msgbuf;
|
jbe@0
|
378 while (1) {
|
jbe@0
|
379 char *endptr = strchr(line, '\n');
|
jbe@0
|
380 if (endptr) {
|
jbe@0
|
381 /* terminate string where newline character is found */
|
jbe@0
|
382 *endptr = 0;
|
jbe@0
|
383 } else if (line != msgbuf && msglen > MOONBR_LOG_MAXMSGLEN) {
|
jbe@0
|
384 /* break if line is incomplete and not the first line */
|
jbe@0
|
385 break;
|
jbe@0
|
386 }
|
jbe@0
|
387 moonbr_log_msg(priority, line);
|
jbe@0
|
388 if (!endptr) break; /* break if end of formatted message is reached */
|
jbe@0
|
389 line = endptr+1; /* otherwise continue with remaining message */
|
jbe@0
|
390 }
|
jbe@0
|
391 }
|
jbe@0
|
392 if (msglen > MOONBR_LOG_MAXMSGLEN) {
|
jbe@0
|
393 /* print warning if message was truncated */
|
jbe@0
|
394 moonbr_log_msg(priority, "Previous log message has been truncated due to excessive length");
|
jbe@0
|
395 }
|
jbe@0
|
396 }
|
jbe@0
|
397
|
jbe@0
|
398
|
jbe@0
|
399 /*** Termination function ***/
|
jbe@0
|
400
|
jbe@0
|
401 /* Kill all child processes, remove PID file (if existent), and exit master process with given exitcode */
|
jbe@0
|
402 static void moonbr_terminate(int exitcode) {
|
jbe@0
|
403 {
|
jbe@0
|
404 struct moonbr_pool *pool;
|
jbe@0
|
405 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
|
jbe@0
|
406 {
|
jbe@0
|
407 struct moonbr_worker *worker;
|
jbe@0
|
408 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
|
jbe@0
|
409 moonbr_log(LOG_INFO, "Sending SIGKILL to child with PID %i", (int)worker->pid);
|
jbe@0
|
410 if (kill(worker->pid, SIGKILL)) {
|
jbe@0
|
411 moonbr_log(LOG_ERR, "Error while killing child process: %s", strerror(errno));
|
jbe@0
|
412 }
|
jbe@0
|
413 }
|
jbe@0
|
414 }
|
jbe@0
|
415 {
|
jbe@0
|
416 int i;
|
jbe@0
|
417 for (i=0; i<pool->listener_count; i++) {
|
jbe@0
|
418 struct moonbr_listener *listener = &pool->listener[i];
|
jbe@0
|
419 if (listener->proto == MOONBR_PROTO_LOCAL) {
|
jbe@125
|
420 moonbr_log(LOG_INFO, "Unlinking local socket \"%s\"", listener->type_specific.socket.addr.addr_un.sun_path);
|
jbe@125
|
421 if (unlink(listener->type_specific.socket.addr.addr_un.sun_path)) {
|
jbe@0
|
422 moonbr_log(LOG_ERR, "Error while unlinking local socket: %s", strerror(errno));
|
jbe@0
|
423 }
|
jbe@0
|
424 }
|
jbe@0
|
425 }
|
jbe@0
|
426 }
|
jbe@0
|
427 }
|
jbe@0
|
428 }
|
jbe@0
|
429 moonbr_log(exitcode ? LOG_ERR : LOG_NOTICE, "Terminating with exit code %i", exitcode);
|
jbe@0
|
430 if (moonbr_pidfh && pidfile_remove(moonbr_pidfh)) {
|
jbe@0
|
431 moonbr_log(LOG_ERR, "Error while removing PID file: %s", strerror(errno));
|
jbe@0
|
432 }
|
jbe@0
|
433 exit(exitcode);
|
jbe@0
|
434 }
|
jbe@0
|
435
|
jbe@0
|
436 /* Terminate with either MOONBR_EXITCODE_STARTUPERROR or MOONBR_EXITCODE_RUNTIMEERROR */
|
jbe@0
|
437 #define moonbr_terminate_error() \
|
jbe@0
|
438 moonbr_terminate( \
|
jbe@0
|
439 moonbr_pstate == MOONBR_PSTATE_STARTUP ? \
|
jbe@0
|
440 MOONBR_EXITCODE_STARTUPERROR : \
|
jbe@0
|
441 MOONBR_EXITCODE_RUNTIMEERROR \
|
jbe@0
|
442 )
|
jbe@0
|
443
|
jbe@0
|
444
|
jbe@0
|
445 /*** Helper functions ***/
|
jbe@0
|
446
|
jbe@0
|
447 /* Fills a 'struct timeval' structure with the current time (using CLOCK_MONOTONIC) */
|
jbe@0
|
448 static void moonbr_now(struct timeval *now) {
|
jbe@0
|
449 struct timespec ts = {0, };
|
jbe@0
|
450 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
|
jbe@0
|
451 moonbr_log(LOG_CRIT, "Error in clock_gettime() call: %s", strerror(errno));
|
jbe@0
|
452 moonbr_terminate_error();
|
jbe@0
|
453 }
|
jbe@0
|
454 *now = (struct timeval){ .tv_sec = ts.tv_sec, .tv_usec = ts.tv_nsec / 1000 };
|
jbe@0
|
455 }
|
jbe@0
|
456
|
jbe@0
|
457 /* Formats a 'struct timeval' value (not thread-safe) */
|
jbe@0
|
458 static char *moonbr_format_timeval(struct timeval *t) {
|
jbe@0
|
459 static char buf[32];
|
jbe@0
|
460 snprintf(buf, 32, "%ji.%06ji seconds", (intmax_t)t->tv_sec, (intmax_t)t->tv_usec);
|
jbe@0
|
461 return buf;
|
jbe@0
|
462 }
|
jbe@0
|
463
|
jbe@0
|
464
|
jbe@0
|
465 /*** Functions for pool creation and startup ***/
|
jbe@0
|
466
|
jbe@0
|
467 /* Creates a 'struct moonbr_pool' structure with a given number of listeners */
|
jbe@0
|
468 static struct moonbr_pool *moonbr_create_pool(int listener_count) {
|
jbe@0
|
469 struct moonbr_pool *pool;
|
jbe@0
|
470 pool = calloc(1,
|
jbe@0
|
471 sizeof(struct moonbr_pool) + /* size of 'struct moonbr_pool' with one listener */
|
jbe@0
|
472 (listener_count-1) * sizeof(struct moonbr_listener) /* size of extra listeners */
|
jbe@0
|
473 );
|
jbe@0
|
474 if (!pool) {
|
jbe@0
|
475 moonbr_log(LOG_CRIT, "Memory allocation error");
|
jbe@0
|
476 moonbr_terminate_error();
|
jbe@0
|
477 }
|
jbe@0
|
478 pool->listener_count = listener_count;
|
jbe@0
|
479 {
|
jbe@0
|
480 /* initialization of listeners */
|
jbe@0
|
481 int i;
|
jbe@0
|
482 for (i=0; i<listener_count; i++) {
|
jbe@0
|
483 struct moonbr_listener *listener = &pool->listener[i];
|
jbe@0
|
484 listener->pool = pool;
|
jbe@0
|
485 listener->listenfd = -1;
|
jbe@0
|
486 listener->pollidx = -1;
|
jbe@0
|
487 }
|
jbe@0
|
488 }
|
jbe@0
|
489 return pool;
|
jbe@0
|
490 }
|
jbe@0
|
491
|
jbe@0
|
492 /* Destroys a 'struct moonbr_pool' structure before it has been started */
|
jbe@0
|
493 static void moonbr_destroy_pool(struct moonbr_pool *pool) {
|
jbe@0
|
494 int i;
|
jbe@0
|
495 for (i=0; i<pool->listener_count; i++) {
|
jbe@0
|
496 struct moonbr_listener *listener = &pool->listener[i];
|
jbe@0
|
497 if (
|
jbe@0
|
498 listener->proto == MOONBR_PROTO_INTERVAL &&
|
jbe@125
|
499 listener->type_specific.interval.name
|
jbe@0
|
500 ) {
|
jbe@125
|
501 free(listener->type_specific.interval.name);
|
jbe@0
|
502 }
|
jbe@0
|
503 }
|
jbe@0
|
504 free(pool);
|
jbe@0
|
505 }
|
jbe@0
|
506
|
jbe@0
|
507 /* Starts a all listeners in a pool */
|
jbe@0
|
508 static int moonbr_start_pool(struct moonbr_pool *pool) {
|
jbe@0
|
509 moonbr_log(LOG_INFO, "Creating pool", pool->poolnum);
|
jbe@0
|
510 {
|
jbe@0
|
511 int i;
|
jbe@0
|
512 for (i=0; i<pool->listener_count; i++) {
|
jbe@0
|
513 struct moonbr_listener *listener = &pool->listener[i];
|
jbe@0
|
514 switch (listener->proto) {
|
jbe@127
|
515 case MOONBR_PROTO_MAIN:
|
jbe@127
|
516 /* nothing to do here: starting main thread is performed in moonbr_run() function */
|
jbe@127
|
517 moonbr_log(LOG_INFO, "Adding main thread");
|
jbe@127
|
518 break;
|
jbe@0
|
519 case MOONBR_PROTO_INTERVAL:
|
jbe@17
|
520 /* nothing to do here: starting intervals is performed in moonbr_run() function */
|
jbe@125
|
521 if (!listener->type_specific.interval.name) {
|
jbe@0
|
522 moonbr_log(LOG_INFO, "Adding unnamed interval listener");
|
jbe@0
|
523 } else {
|
jbe@125
|
524 moonbr_log(LOG_INFO, "Adding interval listener \"%s\"", listener->type_specific.interval.name);
|
jbe@0
|
525 }
|
jbe@0
|
526 break;
|
jbe@0
|
527 case MOONBR_PROTO_LOCAL:
|
jbe@125
|
528 moonbr_log(LOG_INFO, "Adding local socket listener for path \"%s\"", listener->type_specific.socket.addr.addr_un.sun_path);
|
jbe@125
|
529 listener->listenfd = socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
|
jbe@125
|
530 if (listener->listenfd == -1) goto moonbr_start_pool_error;
|
jbe@125
|
531 if (!unlink(listener->type_specific.socket.addr.addr_un.sun_path)) {
|
jbe@125
|
532 moonbr_log(LOG_WARNING, "Unlinked named socket \"%s\" prior to listening", listener->type_specific.socket.addr.addr_un.sun_path);
|
jbe@125
|
533 } else {
|
jbe@125
|
534 if (errno != ENOENT) {
|
jbe@125
|
535 moonbr_log(LOG_ERR, "Could not unlink named socket \"%s\" prior to listening: %s", listener->type_specific.socket.addr.addr_un.sun_path, strerror(errno));
|
jbe@125
|
536 }
|
jbe@125
|
537 }
|
jbe@125
|
538 if (
|
jbe@125
|
539 bind(listener->listenfd, &listener->type_specific.socket.addr.addr_abstract, listener->type_specific.socket.addrlen)
|
jbe@125
|
540 ) goto moonbr_start_pool_error;
|
jbe@125
|
541 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
|
jbe@125
|
542 break;
|
jbe@125
|
543 case MOONBR_PROTO_TCP:
|
jbe@125
|
544 moonbr_log(LOG_INFO, "Adding TCP listener on interface \"%s\", port %i", listener->proto_specific.tcp.ip, listener->proto_specific.tcp.port);
|
jbe@134
|
545 listener->listenfd = socket(listener->type_specific.socket.addr.addr_abstract.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); /* NOTE: not correctly using PF_* but AF_* constants here */
|
jbe@125
|
546 if (listener->listenfd == -1) goto moonbr_start_pool_error;
|
jbe@0
|
547 {
|
jbe@125
|
548 /* avoid "Address already in use" error when restarting service */
|
jbe@125
|
549 static const int reuseval = 1;
|
jbe@125
|
550 if (setsockopt(
|
jbe@125
|
551 listener->listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval)
|
jbe@125
|
552 )) goto moonbr_start_pool_error;
|
jbe@0
|
553 }
|
jbe@0
|
554 {
|
jbe@125
|
555 /* default to send TCP RST when process terminates unexpectedly */
|
jbe@125
|
556 static const struct linger lingerval = {
|
jbe@125
|
557 .l_onoff = 1,
|
jbe@125
|
558 .l_linger = 0
|
jbe@0
|
559 };
|
jbe@125
|
560 if (setsockopt(
|
jbe@125
|
561 listener->listenfd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval)
|
jbe@125
|
562 )) goto moonbr_start_pool_error;
|
jbe@0
|
563 }
|
jbe@125
|
564 if (
|
jbe@125
|
565 bind(listener->listenfd, &listener->type_specific.socket.addr.addr_abstract, listener->type_specific.socket.addrlen)
|
jbe@125
|
566 ) goto moonbr_start_pool_error;
|
jbe@125
|
567 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
|
jbe@0
|
568 break;
|
jbe@0
|
569 default:
|
jbe@0
|
570 moonbr_log(LOG_CRIT, "Internal error (should not happen): Unexpected value in listener.proto field");
|
jbe@0
|
571 moonbr_terminate_error();
|
jbe@0
|
572 }
|
jbe@0
|
573 }
|
jbe@0
|
574 goto moonbr_start_pool_ok;
|
jbe@0
|
575 moonbr_start_pool_error:
|
jbe@0
|
576 {
|
jbe@0
|
577 int j = i;
|
jbe@0
|
578 int errno2 = errno;
|
jbe@0
|
579 for (; i>=0; i--) {
|
jbe@0
|
580 struct moonbr_listener *listener = &pool->listener[i];
|
jbe@0
|
581 if (listener->listenfd != -1) close(listener->listenfd);
|
jbe@0
|
582 }
|
jbe@0
|
583 errno = errno2;
|
jbe@0
|
584 return j;
|
jbe@0
|
585 }
|
jbe@0
|
586 }
|
jbe@0
|
587 moonbr_start_pool_ok:
|
jbe@0
|
588 pool->poolnum = ++moonbr_pool_count;
|
jbe@0
|
589 moonbr_log(LOG_INFO, "Pool #%i created", pool->poolnum);
|
jbe@0
|
590 if (moonbr_last_pool) moonbr_last_pool->next_pool = pool;
|
jbe@0
|
591 else moonbr_first_pool = pool;
|
jbe@0
|
592 moonbr_last_pool = pool;
|
jbe@0
|
593 return -1;
|
jbe@0
|
594 }
|
jbe@0
|
595
|
jbe@0
|
596
|
jbe@0
|
597 /*** Function to send data and a file descriptor to child process */
|
jbe@0
|
598
|
jbe@0
|
599 /* Sends control message of one bye plus optional file descriptor plus optional pointer to child process */
|
jbe@0
|
600 static void moonbr_send_control_message(struct moonbr_worker *worker, char status, int fd, void *ptr) {
|
jbe@0
|
601 {
|
jbe@0
|
602 struct iovec iovector = { .iov_base = &status, .iov_len = 1 }; /* carrying status byte */
|
jbe@0
|
603 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to transfer file descriptor */
|
jbe@0
|
604 struct msghdr message = { .msg_iov = &iovector, .msg_iovlen = 1 }; /* data structure passed to sendmsg() call */
|
jbe@0
|
605 if (moonbr_debug) {
|
jbe@0
|
606 if (fd == -1) {
|
jbe@0
|
607 moonbr_log(LOG_DEBUG, "Sending control message \"%c\" to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
|
jbe@0
|
608 } else {
|
jbe@0
|
609 moonbr_log(LOG_DEBUG, "Sending control message \"%c\" with file descriptor #%i to child process in pool #%i (PID %i)", (int)status, fd, worker->pool->poolnum, (int)worker->pid);
|
jbe@0
|
610 }
|
jbe@0
|
611 }
|
jbe@0
|
612 if (fd != -1) {
|
jbe@0
|
613 /* attach control message with file descriptor */
|
jbe@0
|
614 message.msg_control = control_message_buffer;
|
jbe@0
|
615 message.msg_controllen = CMSG_SPACE(sizeof(int));
|
jbe@0
|
616 {
|
jbe@0
|
617 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
|
jbe@0
|
618 control_message->cmsg_level = SOL_SOCKET;
|
jbe@0
|
619 control_message->cmsg_type = SCM_RIGHTS;
|
jbe@0
|
620 control_message->cmsg_len = CMSG_LEN(sizeof(int));
|
jbe@18
|
621 memcpy(CMSG_DATA(control_message), &fd, sizeof(int));
|
jbe@0
|
622 }
|
jbe@0
|
623 }
|
jbe@0
|
624 while (sendmsg(worker->controlfd, &message, MSG_NOSIGNAL) < 0) {
|
jbe@0
|
625 if (errno == EPIPE) {
|
jbe@0
|
626 moonbr_log(LOG_ERR, "Error while communicating with idle child process in pool #%i (PID %i): %s", worker->pool->poolnum, (int)worker->pid, strerror(errno));
|
jbe@0
|
627 return; /* do not close socket; socket is closed when reading from it */
|
jbe@0
|
628 }
|
jbe@0
|
629 if (errno != EINTR) {
|
jbe@0
|
630 moonbr_log(LOG_CRIT, "Unexpected error while communicating with idle child process in pool #%i (PID %i): %s", worker->pool->poolnum, (int)worker->pid, strerror(errno));
|
jbe@0
|
631 moonbr_terminate_error();
|
jbe@0
|
632 }
|
jbe@0
|
633 }
|
jbe@0
|
634 }
|
jbe@0
|
635 if (ptr) {
|
jbe@0
|
636 char buf[sizeof(void *)];
|
jbe@0
|
637 char *pos = buf;
|
jbe@0
|
638 int len = sizeof(void *);
|
jbe@0
|
639 ssize_t written;
|
jbe@0
|
640 if (moonbr_debug) {
|
jbe@0
|
641 moonbr_log(LOG_DEBUG, "Sending memory pointer to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
|
jbe@0
|
642 }
|
jbe@18
|
643 memcpy(buf, &ptr, sizeof(void *));
|
jbe@0
|
644 while (len) {
|
jbe@0
|
645 written = send(worker->controlfd, pos, len, MSG_NOSIGNAL);
|
jbe@0
|
646 if (written > 0) {
|
jbe@0
|
647 pos += written;
|
jbe@0
|
648 len -= written;
|
jbe@0
|
649 } else if (errno == EPIPE) {
|
jbe@0
|
650 moonbr_log(LOG_ERR, "Error while communicating with idle child process in pool #%i (PID %i): %s", worker->pool->poolnum, (int)worker->pid, strerror(errno));
|
jbe@0
|
651 return; /* do not close socket; socket is closed when reading from it */
|
jbe@0
|
652 } else if (errno != EINTR) {
|
jbe@0
|
653 moonbr_log(LOG_CRIT, "Unexpected error while communicating with idle child process in pool #%i (PID %i): %s", worker->pool->poolnum, (int)worker->pid, strerror(errno));
|
jbe@0
|
654 moonbr_terminate_error();
|
jbe@0
|
655 }
|
jbe@0
|
656 }
|
jbe@0
|
657 }
|
jbe@0
|
658 }
|
jbe@0
|
659
|
jbe@0
|
660
|
jbe@0
|
661 /*** Functions running in child process ***/
|
jbe@0
|
662
|
jbe@0
|
663 /* Logs an error in child process */
|
jbe@0
|
664 static void moonbr_child_log(const char *message) {
|
jbe@0
|
665 fprintf(stderr, "%s\n", message);
|
jbe@0
|
666 }
|
jbe@0
|
667
|
jbe@0
|
668 /* Logs a fatal error in child process and terminates process with error status */
|
jbe@0
|
669 static void moonbr_child_log_fatal(const char *message) {
|
jbe@0
|
670 moonbr_child_log(message);
|
jbe@0
|
671 exit(1);
|
jbe@0
|
672 }
|
jbe@0
|
673
|
jbe@0
|
674 /* Logs an error in child process while appending error string for global errno variable */
|
jbe@0
|
675 static void moonbr_child_log_errno(const char *message) {
|
jbe@213
|
676 char errmsg[MOONBR_MAXSTRERRORLEN] = MOONBR_STRERROR_R_MSG;
|
jbe@20
|
677 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
|
jbe@0
|
678 fprintf(stderr, "%s: %s\n", message, errmsg);
|
jbe@0
|
679 }
|
jbe@0
|
680
|
jbe@0
|
681 /* Logs a fatal error in child process while appending error string for errno and terminating process */
|
jbe@0
|
682 static void moonbr_child_log_errno_fatal(const char *message) {
|
jbe@0
|
683 moonbr_child_log_errno(message);
|
jbe@0
|
684 exit(1);
|
jbe@0
|
685 }
|
jbe@0
|
686
|
jbe@0
|
687 /* Receives a control message consisting of one character plus an optional file descriptor from parent process */
|
jbe@0
|
688 static void moonbr_child_receive_control_message(int socketfd, char *status, int *fd) {
|
jbe@0
|
689 struct iovec iovector = { .iov_base = status, .iov_len = 1 }; /* reference to status byte variable */
|
jbe@0
|
690 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to receive file descriptor */
|
jbe@0
|
691 struct msghdr message = { /* data structure passed to recvmsg() call */
|
jbe@0
|
692 .msg_iov = &iovector,
|
jbe@0
|
693 .msg_iovlen = 1,
|
jbe@0
|
694 .msg_control = control_message_buffer,
|
jbe@0
|
695 .msg_controllen = CMSG_SPACE(sizeof(int))
|
jbe@0
|
696 };
|
jbe@0
|
697 {
|
jbe@0
|
698 int received;
|
jbe@0
|
699 while ((received = recvmsg(socketfd, &message, MSG_CMSG_CLOEXEC)) < 0) {
|
jbe@0
|
700 if (errno != EINTR) {
|
jbe@0
|
701 moonbr_child_log_errno_fatal("Error while trying to receive connection socket from parent process");
|
jbe@0
|
702 }
|
jbe@0
|
703 }
|
jbe@0
|
704 if (!received) {
|
jbe@0
|
705 moonbr_child_log_fatal("Unexpected EOF while trying to receive connection socket from parent process");
|
jbe@0
|
706 }
|
jbe@0
|
707 }
|
jbe@0
|
708 {
|
jbe@0
|
709 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
|
jbe@0
|
710 if (control_message) {
|
jbe@0
|
711 if (control_message->cmsg_level != SOL_SOCKET) {
|
jbe@0
|
712 moonbr_child_log_fatal("Received control message with cmsg_level not equal to SOL_SOCKET");
|
jbe@0
|
713 }
|
jbe@0
|
714 if (control_message->cmsg_type != SCM_RIGHTS) {
|
jbe@0
|
715 moonbr_child_log_fatal("Received control message with cmsg_type not equal to SCM_RIGHTS");
|
jbe@0
|
716 }
|
jbe@18
|
717 memcpy(fd, CMSG_DATA(control_message), sizeof(int));
|
jbe@0
|
718 } else {
|
jbe@0
|
719 *fd = -1;
|
jbe@0
|
720 }
|
jbe@0
|
721 }
|
jbe@0
|
722 }
|
jbe@0
|
723
|
jbe@0
|
724 /* Receives a pointer from parent process */
|
jbe@0
|
725 static void *moonbr_child_receive_pointer(int socketfd) {
|
jbe@0
|
726 char buf[sizeof(void *)];
|
jbe@0
|
727 char *pos = buf;
|
jbe@0
|
728 int len = sizeof(void *);
|
jbe@0
|
729 ssize_t bytes_read;
|
jbe@0
|
730 while (len) {
|
jbe@0
|
731 bytes_read = recv(socketfd, pos, len, 0);
|
jbe@0
|
732 if (bytes_read > 0) {
|
jbe@0
|
733 pos += bytes_read;
|
jbe@0
|
734 len -= bytes_read;
|
jbe@0
|
735 } else if (!bytes_read) {
|
jbe@0
|
736 moonbr_child_log_fatal("Unexpected EOF while trying to receive memory pointer from parent process");
|
jbe@0
|
737 } else if (errno != EINTR) {
|
jbe@0
|
738 moonbr_child_log_errno_fatal("Error while trying to receive memory pointer from parent process");
|
jbe@0
|
739 }
|
jbe@0
|
740 }
|
jbe@18
|
741 {
|
jbe@18
|
742 void *ptr; /* avoid breaking strict-aliasing rules */
|
jbe@18
|
743 memcpy(&ptr, buf, sizeof(void *));
|
jbe@18
|
744 return ptr;
|
jbe@18
|
745 }
|
jbe@0
|
746 }
|
jbe@0
|
747
|
jbe@0
|
748 /* Main function of child process to be called after fork() and file descriptor rearrangement */
|
jbe@0
|
749 void moonbr_child_run(struct moonbr_pool *pool, lua_State *L) {
|
jbe@0
|
750 char controlmsg;
|
jbe@88
|
751 int fd;
|
jbe@0
|
752 struct itimerval notimer = { { 0, }, { 0, } };
|
jbe@0
|
753 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
|
jbe@0
|
754 if (lua_isnil(L, -1)) lua_pop(L, 1);
|
jbe@0
|
755 else if (lua_pcall(L, 0, 0, 1)) {
|
jbe@0
|
756 fprintf(stderr, "Error in \"prepare\" function: %s\n", lua_tostring(L, -1));
|
jbe@0
|
757 exit(1);
|
jbe@0
|
758 }
|
jbe@0
|
759 while (1) {
|
jbe@0
|
760 struct moonbr_listener *listener;
|
jbe@0
|
761 if (setitimer(ITIMER_REAL, ¬imer, NULL)) {
|
jbe@0
|
762 moonbr_child_log_errno_fatal("Could not reset ITIMER_REAL via setitimer()");
|
jbe@0
|
763 }
|
jbe@0
|
764 controlmsg = MOONBR_STATUS_IDLE;
|
jbe@0
|
765 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
|
jbe@0
|
766 moonbr_child_log_errno_fatal("Error while sending ready message to parent process");
|
jbe@0
|
767 }
|
jbe@88
|
768 moonbr_child_receive_control_message(MOONBR_FD_CONTROL, &controlmsg, &fd);
|
jbe@0
|
769 if (!(
|
jbe@124
|
770 (controlmsg == MOONBR_COMMAND_TERMINATE && fd == -1) ||
|
jbe@124
|
771 (controlmsg == MOONBR_COMMAND_CONNECT)
|
jbe@0
|
772 )) {
|
jbe@0
|
773 moonbr_child_log_fatal("Received illegal control message from parent process");
|
jbe@0
|
774 }
|
jbe@0
|
775 if (controlmsg == MOONBR_COMMAND_TERMINATE) break;
|
jbe@0
|
776 listener = moonbr_child_receive_pointer(MOONBR_FD_CONTROL);
|
jbe@127
|
777 if (
|
jbe@127
|
778 listener->proto != MOONBR_PROTO_LOCAL &&
|
jbe@127
|
779 listener->proto != MOONBR_PROTO_TCP &&
|
jbe@127
|
780 fd >= 0
|
jbe@127
|
781 ) {
|
jbe@124
|
782 moonbr_child_log_fatal("Received unexpected file descriptor from parent process");
|
jbe@127
|
783 } else if (
|
jbe@127
|
784 listener->proto != MOONBR_PROTO_MAIN &&
|
jbe@127
|
785 listener->proto != MOONBR_PROTO_INTERVAL &&
|
jbe@127
|
786 fd < 0
|
jbe@127
|
787 ) {
|
jbe@124
|
788 moonbr_child_log_fatal("Missing file descriptor from parent process");
|
jbe@124
|
789 }
|
jbe@119
|
790 if (fd >= 0) moonbr_io_pushhandle(L, fd);
|
jbe@0
|
791 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
|
jbe@119
|
792 if (fd < 0) {
|
jbe@0
|
793 lua_newtable(L);
|
jbe@131
|
794 if (listener->proto == MOONBR_PROTO_MAIN) {
|
jbe@131
|
795 lua_pushboolean(L, 1);
|
jbe@131
|
796 lua_setfield(L, -2, "main");
|
jbe@131
|
797 } else if (listener->proto == MOONBR_PROTO_INTERVAL) {
|
jbe@131
|
798 lua_pushstring(L,
|
jbe@131
|
799 listener->type_specific.interval.name ?
|
jbe@131
|
800 listener->type_specific.interval.name : ""
|
jbe@131
|
801 );
|
jbe@131
|
802 lua_setfield(L, -2, "interval");
|
jbe@131
|
803 }
|
jbe@0
|
804 } else {
|
jbe@88
|
805 lua_pushvalue(L, -2);
|
jbe@0
|
806 }
|
jbe@0
|
807 if (lua_pcall(L, 1, 1, 1)) {
|
jbe@0
|
808 fprintf(stderr, "Error in \"connect\" function: %s\n", lua_tostring(L, -1));
|
jbe@0
|
809 exit(1);
|
jbe@0
|
810 }
|
jbe@119
|
811 if (fd >= 0) moonbr_io_closehandle(L, -2, 0); /* attemt clean close */
|
jbe@0
|
812 if (lua_type(L, -1) != LUA_TBOOLEAN || !lua_toboolean(L, -1)) break;
|
jbe@0
|
813 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
|
jbe@0
|
814 lua_settop(L, 2);
|
jbe@0
|
815 #else
|
jbe@0
|
816 lua_settop(L, 1);
|
jbe@0
|
817 #endif
|
jbe@0
|
818 }
|
jbe@0
|
819 controlmsg = MOONBR_STATUS_GOODBYE;
|
jbe@0
|
820 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
|
jbe@0
|
821 moonbr_child_log_errno_fatal("Error while sending goodbye message to parent process");
|
jbe@0
|
822 }
|
jbe@0
|
823 if (close(MOONBR_FD_CONTROL) && errno != EINTR) {
|
jbe@0
|
824 moonbr_child_log_errno("Error while closing control socket");
|
jbe@0
|
825 }
|
jbe@0
|
826 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
|
jbe@0
|
827 if (lua_isnil(L, -1)) lua_pop(L, 1);
|
jbe@0
|
828 else if (lua_pcall(L, 0, 0, 1)) {
|
jbe@0
|
829 fprintf(stderr, "Error in \"finish\" function: %s\n", lua_tostring(L, -1));
|
jbe@0
|
830 exit(1);
|
jbe@0
|
831 }
|
jbe@0
|
832 lua_close(L);
|
jbe@0
|
833 exit(0);
|
jbe@0
|
834 }
|
jbe@0
|
835
|
jbe@0
|
836
|
jbe@0
|
837 /*** Functions to spawn child process ***/
|
jbe@0
|
838
|
jbe@0
|
839 /* Helper function to send an error message to a file descriptor (not needing a file stream) */
|
jbe@0
|
840 static void moonbr_child_emergency_print(int fd, char *message) {
|
jbe@0
|
841 size_t len = strlen(message);
|
jbe@0
|
842 ssize_t written;
|
jbe@0
|
843 while (len) {
|
jbe@0
|
844 written = write(fd, message, len);
|
jbe@0
|
845 if (written > 0) {
|
jbe@0
|
846 message += written;
|
jbe@0
|
847 len -= written;
|
jbe@0
|
848 } else {
|
jbe@0
|
849 if (written != -1 || errno != EINTR) break;
|
jbe@0
|
850 }
|
jbe@0
|
851 }
|
jbe@0
|
852 }
|
jbe@0
|
853
|
jbe@0
|
854 /* Helper function to send an error message plus a text for errno to a file descriptor and terminate the process */
|
jbe@0
|
855 static void moonbr_child_emergency_error(int fd, char *message) {
|
jbe@0
|
856 int errno2 = errno;
|
jbe@0
|
857 moonbr_child_emergency_print(fd, message);
|
jbe@0
|
858 moonbr_child_emergency_print(fd, ": ");
|
jbe@0
|
859 moonbr_child_emergency_print(fd, strerror(errno2));
|
jbe@0
|
860 moonbr_child_emergency_print(fd, "\n");
|
jbe@0
|
861 exit(1);
|
jbe@0
|
862 }
|
jbe@0
|
863
|
jbe@0
|
864 /* Creates a child process and (in case of success) registers it in the 'struct moonbr_pool' structure */
|
jbe@0
|
865 static int moonbr_create_worker(struct moonbr_pool *pool, lua_State *L) {
|
jbe@0
|
866 struct moonbr_worker *worker;
|
jbe@0
|
867 worker = calloc(1, sizeof(struct moonbr_worker));
|
jbe@0
|
868 if (!worker) {
|
jbe@0
|
869 moonbr_log(LOG_CRIT, "Memory allocation error");
|
jbe@0
|
870 return -1;
|
jbe@0
|
871 }
|
jbe@0
|
872 worker->pool = pool;
|
jbe@0
|
873 {
|
jbe@0
|
874 int controlfds[2];
|
jbe@0
|
875 int errorfds[2];
|
jbe@0
|
876 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, controlfds)) {
|
jbe@0
|
877 moonbr_log(LOG_ERR, "Could not create control socket pair for communcation with child process: %s", strerror(errno));
|
jbe@0
|
878 free(worker);
|
jbe@0
|
879 return -1;
|
jbe@0
|
880 }
|
jbe@0
|
881 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, errorfds)) {
|
jbe@0
|
882 moonbr_log(LOG_ERR, "Could not create socket pair to redirect stderr of child process: %s", strerror(errno));
|
jbe@0
|
883 close(controlfds[0]);
|
jbe@0
|
884 close(controlfds[1]);
|
jbe@0
|
885 free(worker);
|
jbe@0
|
886 return -1;
|
jbe@0
|
887 }
|
jbe@0
|
888 if (moonbr_logfile && fflush(moonbr_logfile)) {
|
jbe@0
|
889 moonbr_log(LOG_CRIT, "Could not flush log file prior to forking: %s", strerror(errno));
|
jbe@0
|
890 moonbr_terminate_error();
|
jbe@0
|
891 }
|
jbe@0
|
892 worker->pid = fork();
|
jbe@0
|
893 if (worker->pid == -1) {
|
jbe@0
|
894 moonbr_log(LOG_ERR, "Could not fork: %s", strerror(errno));
|
jbe@0
|
895 close(controlfds[0]);
|
jbe@0
|
896 close(controlfds[1]);
|
jbe@0
|
897 close(errorfds[0]);
|
jbe@0
|
898 close(errorfds[1]);
|
jbe@0
|
899 free(worker);
|
jbe@0
|
900 return -1;
|
jbe@0
|
901 } else if (!worker->pid) {
|
jbe@0
|
902 moonbr_pstate = MOONBR_PSTATE_FORKED;
|
jbe@0
|
903 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
|
jbe@0
|
904 lua_pushliteral(L, "Failed to pass error message due to bug in Lua panic handler (hint: not enough memory?)");
|
jbe@0
|
905 #endif
|
jbe@0
|
906 moonbr_memory_limit = pool->memory_limit;
|
jbe@0
|
907 if (moonbr_pidfh && pidfile_close(moonbr_pidfh)) {
|
jbe@0
|
908 moonbr_child_emergency_error(errorfds[1], "Could not close PID file in forked child process");
|
jbe@0
|
909 }
|
jbe@0
|
910 if (moonbr_logfile && moonbr_logfile != stderr && fclose(moonbr_logfile)) {
|
jbe@0
|
911 moonbr_child_emergency_error(errorfds[1], "Could not close log file in forked child process");
|
jbe@0
|
912 }
|
jbe@0
|
913 if (dup2(errorfds[1], MOONBR_FD_STDERR) == -1) {
|
jbe@0
|
914 moonbr_child_emergency_error(errorfds[1], "Could not duplicate socket to stderr file descriptor");
|
jbe@0
|
915 }
|
jbe@0
|
916 if (dup2(controlfds[1], MOONBR_FD_CONTROL) == -1) {
|
jbe@0
|
917 moonbr_child_emergency_error(errorfds[1], "Could not duplicate control socket");
|
jbe@0
|
918 }
|
jbe@0
|
919 closefrom(MOONBR_FD_END);
|
jbe@0
|
920 moonbr_child_run(pool, L);
|
jbe@0
|
921 }
|
jbe@0
|
922 if (moonbr_stat) {
|
jbe@0
|
923 moonbr_log(LOG_INFO, "Created new worker in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
|
jbe@0
|
924 }
|
jbe@0
|
925 worker->controlfd = controlfds[0];
|
jbe@0
|
926 worker->errorfd = errorfds[0];
|
jbe@0
|
927 if (close(controlfds[1]) && errno != EINTR) {
|
jbe@0
|
928 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
|
jbe@0
|
929 moonbr_terminate_error();
|
jbe@0
|
930 }
|
jbe@0
|
931 if (close(errorfds[1]) && errno != EINTR) {
|
jbe@0
|
932 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
|
jbe@0
|
933 moonbr_terminate_error();
|
jbe@0
|
934 }
|
jbe@0
|
935 }
|
jbe@0
|
936 worker->prev_worker = pool->last_worker;
|
jbe@0
|
937 if (worker->prev_worker) worker->prev_worker->next_worker = worker;
|
jbe@0
|
938 else pool->first_worker = worker;
|
jbe@0
|
939 pool->last_worker = worker;
|
jbe@0
|
940 pool->unassigned_worker_count++;
|
jbe@0
|
941 pool->total_worker_count++;
|
jbe@0
|
942 pool->worker_count_stat = 1;
|
jbe@0
|
943 moonbr_poll_refresh_needed = 1;
|
jbe@0
|
944 return 0; /* return zero only in case of success */
|
jbe@0
|
945 }
|
jbe@0
|
946
|
jbe@0
|
947
|
jbe@0
|
948 /*** Functions for queues of 'struct moonbr_listener' ***/
|
jbe@0
|
949
|
jbe@0
|
950 /* Appends a 'struct moonbr_listener' to the queue of idle listeners and registers it for poll() */
|
jbe@0
|
951 static void moonbr_add_idle_listener(struct moonbr_listener *listener) {
|
jbe@0
|
952 listener->prev_listener = listener->pool->last_idle_listener;
|
jbe@0
|
953 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
|
jbe@0
|
954 else listener->pool->first_idle_listener = listener;
|
jbe@0
|
955 listener->pool->last_idle_listener = listener;
|
jbe@0
|
956 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events |= POLLIN;
|
jbe@0
|
957 }
|
jbe@0
|
958
|
jbe@0
|
959 /* Removes a 'struct moonbr_listener' from the queue of idle listeners and unregisters it from poll() */
|
jbe@0
|
960 static void moonbr_remove_idle_listener(struct moonbr_listener *listener) {
|
jbe@0
|
961 if (listener->prev_listener) listener->prev_listener->next_listener = listener->next_listener;
|
jbe@0
|
962 else listener->pool->first_idle_listener = listener->next_listener;
|
jbe@0
|
963 if (listener->next_listener) listener->next_listener->prev_listener = listener->prev_listener;
|
jbe@0
|
964 else listener->pool->last_idle_listener = listener->prev_listener;
|
jbe@0
|
965 listener->prev_listener = NULL;
|
jbe@0
|
966 listener->next_listener = NULL;
|
jbe@0
|
967 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events &= ~POLLIN;
|
jbe@0
|
968 }
|
jbe@0
|
969
|
jbe@0
|
970 /* Adds a listener to the queue of connected listeners (i.e. waiting to have their incoming connection accepted) */
|
jbe@0
|
971 static void moonbr_add_connected_listener(struct moonbr_listener *listener) {
|
jbe@0
|
972 listener->prev_listener = listener->pool->last_connected_listener;
|
jbe@0
|
973 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
|
jbe@0
|
974 else listener->pool->first_connected_listener = listener;
|
jbe@0
|
975 listener->pool->last_connected_listener = listener;
|
jbe@0
|
976 }
|
jbe@0
|
977
|
jbe@0
|
978 /* Removes and returns the first connected listener in the queue */
|
jbe@0
|
979 static struct moonbr_listener *moonbr_pop_connected_listener(struct moonbr_pool *pool) {
|
jbe@0
|
980 struct moonbr_listener *listener = pool->first_connected_listener;
|
jbe@0
|
981 listener->pool->first_connected_listener = listener->next_listener;
|
jbe@0
|
982 if (listener->pool->first_connected_listener) listener->pool->first_connected_listener->prev_listener = NULL;
|
jbe@0
|
983 else listener->pool->last_connected_listener = NULL;
|
jbe@0
|
984 listener->next_listener = NULL;
|
jbe@0
|
985 return listener;
|
jbe@0
|
986 }
|
jbe@0
|
987
|
jbe@0
|
988
|
jbe@0
|
989 /*** Functions to handle polling ***/
|
jbe@0
|
990
|
jbe@0
|
991 /* Returns an index to a new initialized entry in moonbr_poll_fds[] */
|
jbe@0
|
992 int moonbr_poll_fds_nextindex() {
|
jbe@0
|
993 if (moonbr_poll_fds_count >= moonbr_poll_fds_bufsize) {
|
jbe@0
|
994 if (moonbr_poll_fds_bufsize) moonbr_poll_fds_bufsize *= 2;
|
jbe@0
|
995 else moonbr_poll_fds_bufsize = 1;
|
jbe@0
|
996 moonbr_poll_fds = realloc(
|
jbe@0
|
997 moonbr_poll_fds, moonbr_poll_fds_bufsize * sizeof(struct pollfd)
|
jbe@0
|
998 );
|
jbe@0
|
999 if (!moonbr_poll_fds) {
|
jbe@0
|
1000 moonbr_log(LOG_CRIT, "Memory allocation error");
|
jbe@0
|
1001 moonbr_terminate_error();
|
jbe@0
|
1002 }
|
jbe@0
|
1003 }
|
jbe@0
|
1004 moonbr_poll_fds[moonbr_poll_fds_count] = (struct pollfd){0, };
|
jbe@0
|
1005 return moonbr_poll_fds_count++;
|
jbe@0
|
1006 }
|
jbe@0
|
1007
|
jbe@0
|
1008 /* Returns an index to a new initialized entry in moonbr_poll_workers[] */
|
jbe@0
|
1009 int moonbr_poll_workers_nextindex() {
|
jbe@0
|
1010 if (moonbr_poll_worker_count >= moonbr_poll_workers_bufsize) {
|
jbe@0
|
1011 if (moonbr_poll_workers_bufsize) moonbr_poll_workers_bufsize *= 2;
|
jbe@0
|
1012 else moonbr_poll_workers_bufsize = 1;
|
jbe@0
|
1013 moonbr_poll_workers = realloc(
|
jbe@0
|
1014 moonbr_poll_workers, moonbr_poll_workers_bufsize * sizeof(struct moonbr_poll_worker)
|
jbe@0
|
1015 );
|
jbe@0
|
1016 if (!moonbr_poll_workers) {
|
jbe@0
|
1017 moonbr_log(LOG_CRIT, "Memory allocation error");
|
jbe@0
|
1018 moonbr_terminate_error();
|
jbe@0
|
1019 }
|
jbe@0
|
1020 }
|
jbe@0
|
1021 moonbr_poll_workers[moonbr_poll_worker_count] = (struct moonbr_poll_worker){0, };
|
jbe@0
|
1022 return moonbr_poll_worker_count++;
|
jbe@0
|
1023 }
|
jbe@0
|
1024
|
jbe@0
|
1025 /* Queues all listeners as idle, and initializes static part of moonbr_poll_fds[], which is passed to poll() */
|
jbe@0
|
1026 static void moonbr_poll_init() {
|
jbe@0
|
1027 if (socketpair(
|
jbe@0
|
1028 PF_LOCAL,
|
jbe@0
|
1029 SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
|
jbe@0
|
1030 0,
|
jbe@0
|
1031 moonbr_poll_signalfds
|
jbe@0
|
1032 )) {
|
jbe@0
|
1033 moonbr_log(LOG_CRIT, "Could not create socket pair for signal delivery during polling: %s", strerror(errno));
|
jbe@0
|
1034 moonbr_terminate_error();
|
jbe@0
|
1035 }
|
jbe@0
|
1036 {
|
jbe@0
|
1037 int j = moonbr_poll_fds_nextindex();
|
jbe@0
|
1038 struct pollfd *pollfd = &moonbr_poll_fds[j];
|
jbe@0
|
1039 pollfd->fd = moonbr_poll_signalfd_read;
|
jbe@0
|
1040 pollfd->events = POLLIN;
|
jbe@0
|
1041 }
|
jbe@0
|
1042 {
|
jbe@0
|
1043 struct moonbr_pool *pool;
|
jbe@0
|
1044 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
|
jbe@0
|
1045 int i;
|
jbe@0
|
1046 for (i=0; i<pool->listener_count; i++) {
|
jbe@0
|
1047 struct moonbr_listener *listener = &pool->listener[i];
|
jbe@0
|
1048 if (listener->listenfd != -1) {
|
jbe@0
|
1049 int j = moonbr_poll_fds_nextindex();
|
jbe@0
|
1050 listener->pollidx = j;
|
jbe@0
|
1051 moonbr_poll_fds[j].fd = listener->listenfd;
|
jbe@0
|
1052 }
|
jbe@0
|
1053 moonbr_add_idle_listener(listener);
|
jbe@0
|
1054 }
|
jbe@0
|
1055 }
|
jbe@0
|
1056 }
|
jbe@0
|
1057 moonbr_poll_fds_static_count = moonbr_poll_fds_count; /* remember size of static part of array */
|
jbe@0
|
1058 }
|
jbe@0
|
1059
|
jbe@0
|
1060 /* Disables polling of all listeners (required for clean shutdown) */
|
jbe@0
|
1061 static void moonbr_poll_shutdown() {
|
jbe@0
|
1062 int i;
|
jbe@0
|
1063 for (i=1; i<moonbr_poll_fds_static_count; i++) {
|
jbe@0
|
1064 moonbr_poll_fds[i].fd = -1;
|
jbe@0
|
1065 }
|
jbe@0
|
1066 }
|
jbe@0
|
1067
|
jbe@0
|
1068 /* (Re)builds dynamic part of moonbr_poll_fds[] array, and (re)builds moonbr_poll_workers[] array */
|
jbe@0
|
1069 static void moonbr_poll_refresh() {
|
jbe@0
|
1070 moonbr_poll_refresh_needed = 0;
|
jbe@0
|
1071 moonbr_poll_fds_count = moonbr_poll_fds_static_count;
|
jbe@0
|
1072 moonbr_poll_worker_count = 0;
|
jbe@0
|
1073 {
|
jbe@0
|
1074 struct moonbr_pool *pool;
|
jbe@0
|
1075 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
|
jbe@0
|
1076 struct moonbr_worker *worker;
|
jbe@0
|
1077 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
|
jbe@0
|
1078 if (worker->controlfd != -1) {
|
jbe@0
|
1079 int j = moonbr_poll_fds_nextindex();
|
jbe@0
|
1080 int k = moonbr_poll_workers_nextindex();
|
jbe@0
|
1081 struct pollfd *pollfd = &moonbr_poll_fds[j];
|
jbe@0
|
1082 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
|
jbe@0
|
1083 pollfd->fd = worker->controlfd;
|
jbe@0
|
1084 pollfd->events = POLLIN;
|
jbe@0
|
1085 poll_worker->channel = MOONBR_POLL_WORKER_CONTROLCHANNEL;
|
jbe@0
|
1086 poll_worker->worker = worker;
|
jbe@0
|
1087 }
|
jbe@0
|
1088 if (worker->errorfd != -1) {
|
jbe@0
|
1089 int j = moonbr_poll_fds_nextindex();
|
jbe@0
|
1090 int k = moonbr_poll_workers_nextindex();
|
jbe@0
|
1091 struct pollfd *pollfd = &moonbr_poll_fds[j];
|
jbe@0
|
1092 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
|
jbe@0
|
1093 pollfd->fd = worker->errorfd;
|
jbe@0
|
1094 pollfd->events = POLLIN;
|
jbe@0
|
1095 poll_worker->channel = MOONBR_POLL_WORKER_ERRORCHANNEL;
|
jbe@0
|
1096 poll_worker->worker = worker;
|
jbe@0
|
1097 }
|
jbe@0
|
1098 }
|
jbe@0
|
1099 }
|
jbe@0
|
1100 }
|
jbe@0
|
1101 }
|
jbe@0
|
1102
|
jbe@0
|
1103 /* resets socket and 'revents' field of moonbr_poll_fds[] for signal delivery just before poll() is called */
|
jbe@0
|
1104 static void moonbr_poll_reset_signal() {
|
jbe@0
|
1105 ssize_t readcount;
|
jbe@0
|
1106 char buf[1];
|
jbe@0
|
1107 moonbr_poll_fds[0].revents = 0;
|
jbe@0
|
1108 while ((readcount = read(moonbr_poll_signalfd_read, buf, 1)) < 0) {
|
jbe@0
|
1109 if (errno == EAGAIN) break;
|
jbe@0
|
1110 if (errno != EINTR) {
|
jbe@0
|
1111 moonbr_log(LOG_CRIT, "Error while reading from signal delivery socket: %s", strerror(errno));
|
jbe@0
|
1112 moonbr_terminate_error();
|
jbe@0
|
1113 }
|
jbe@0
|
1114 }
|
jbe@0
|
1115 if (!readcount) {
|
jbe@0
|
1116 moonbr_log(LOG_CRIT, "Unexpected EOF when reading from signal delivery socket: %s", strerror(errno));
|
jbe@0
|
1117 moonbr_terminate_error();
|
jbe@0
|
1118 }
|
jbe@0
|
1119 }
|
jbe@0
|
1120
|
jbe@0
|
1121
|
jbe@0
|
1122 /*** Shutdown initiation ***/
|
jbe@0
|
1123
|
jbe@0
|
1124 /* Sets global variable 'moonbr_shutdown_in_progress', closes listeners, and demands worker termination */
|
jbe@0
|
1125 static void moonbr_initiate_shutdown() {
|
jbe@0
|
1126 struct moonbr_pool *pool;
|
jbe@0
|
1127 int i;
|
jbe@0
|
1128 if (moonbr_shutdown_in_progress) {
|
jbe@0
|
1129 moonbr_log(LOG_NOTICE, "Shutdown already in progress");
|
jbe@0
|
1130 return;
|
jbe@0
|
1131 }
|
jbe@0
|
1132 moonbr_shutdown_in_progress = 1;
|
jbe@0
|
1133 moonbr_log(LOG_NOTICE, "Initiate shutdown");
|
jbe@0
|
1134 for (pool = moonbr_first_pool; pool; pool = pool->next_pool) {
|
jbe@0
|
1135 for (i=0; i<pool->listener_count; i++) {
|
jbe@0
|
1136 struct moonbr_listener *listener = &pool->listener[i];
|
jbe@0
|
1137 if (listener->listenfd != -1) {
|
jbe@0
|
1138 if (close(listener->listenfd) && errno != EINTR) {
|
jbe@0
|
1139 moonbr_log(LOG_CRIT, "Could not close listening socket: %s", strerror(errno));
|
jbe@0
|
1140 moonbr_terminate_error();
|
jbe@0
|
1141 }
|
jbe@0
|
1142 }
|
jbe@0
|
1143 }
|
jbe@0
|
1144 }
|
jbe@0
|
1145 moonbr_poll_shutdown(); /* avoids loops due to error condition when polling closed listeners */
|
jbe@223
|
1146 /* sending SIGUSR1 to children might be used to terminate children gracefully */
|
jbe@223
|
1147 /*
|
jbe@195
|
1148 {
|
jbe@195
|
1149 pid_t pgrp = getpgrp();
|
jbe@195
|
1150 moonbr_log(LOG_INFO, "Sending SIGUSR1 to all processes in group %i", (int)pgrp);
|
jbe@195
|
1151 if (killpg(pgrp, SIGUSR1)) {
|
jbe@195
|
1152 moonbr_log(LOG_WARNING, "Error while sending SIGUSR1 to own process group: %s", strerror(errno));
|
jbe@195
|
1153 }
|
jbe@195
|
1154 }
|
jbe@223
|
1155 */
|
jbe@0
|
1156 }
|
jbe@0
|
1157
|
jbe@0
|
1158
|
jbe@128
|
1159 /*** Functions to handle previously created 'struct moonbr_worker' structures ***/
|
jbe@128
|
1160
|
jbe@128
|
1161 #define moonbr_try_destroy_worker_stat(str, field) \
|
jbe@128
|
1162 moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: " str " %li", worker->pool->poolnum, (int)worker->pid, (long)childusage.field);
|
jbe@128
|
1163
|
jbe@128
|
1164 /* Destroys a worker structure if socket connections have been closed and child process has terminated */
|
jbe@128
|
1165 static int moonbr_try_destroy_worker(struct moonbr_worker *worker) {
|
jbe@128
|
1166 if (worker->controlfd != -1 || worker->errorfd != -1) return MOONBR_DESTROY_NONE;
|
jbe@128
|
1167 {
|
jbe@128
|
1168 int childstatus;
|
jbe@128
|
1169 struct rusage childusage;
|
jbe@128
|
1170 {
|
jbe@128
|
1171 pid_t waitedpid;
|
jbe@128
|
1172 while (
|
jbe@128
|
1173 (waitedpid = wait4(worker->pid, &childstatus, WNOHANG, &childusage)) == -1
|
jbe@128
|
1174 ) {
|
jbe@128
|
1175 if (errno != EINTR) {
|
jbe@128
|
1176 moonbr_log(LOG_CRIT, "Error in wait4() call: %s", strerror(errno));
|
jbe@128
|
1177 moonbr_terminate_error();
|
jbe@128
|
1178 }
|
jbe@128
|
1179 }
|
jbe@128
|
1180 if (!waitedpid) return 0; /* return 0 if worker couldn't be destroyed */
|
jbe@128
|
1181 if (waitedpid != worker->pid) {
|
jbe@128
|
1182 moonbr_log(LOG_CRIT, "Wrong PID returned by wait4() call");
|
jbe@128
|
1183 moonbr_terminate_error();
|
jbe@128
|
1184 }
|
jbe@128
|
1185 }
|
jbe@128
|
1186 if (WIFEXITED(childstatus)) {
|
jbe@128
|
1187 if (WEXITSTATUS(childstatus) || moonbr_stat) {
|
jbe@128
|
1188 moonbr_log(
|
jbe@128
|
1189 WEXITSTATUS(childstatus) ? LOG_WARNING : LOG_INFO,
|
jbe@128
|
1190 "Child process in pool #%i with PID %i returned with exit code %i", worker->pool->poolnum, (int)worker->pid, WEXITSTATUS(childstatus)
|
jbe@128
|
1191 );
|
jbe@128
|
1192 }
|
jbe@128
|
1193 } else if (WIFSIGNALED(childstatus)) {
|
jbe@128
|
1194 if (WCOREDUMP(childstatus)) {
|
jbe@128
|
1195 moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i (core dump was created)", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
|
jbe@128
|
1196 } else if (WTERMSIG(childstatus) == SIGALRM) {
|
jbe@128
|
1197 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i exited prematurely due to timeout", worker->pool->poolnum, (int)worker->pid);
|
jbe@128
|
1198 } else {
|
jbe@128
|
1199 moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
|
jbe@128
|
1200 }
|
jbe@128
|
1201 } else {
|
jbe@128
|
1202 moonbr_log(LOG_CRIT, "Illegal exit status from child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
|
jbe@128
|
1203 moonbr_terminate_error();
|
jbe@128
|
1204 }
|
jbe@128
|
1205 if (moonbr_stat) {
|
jbe@128
|
1206 moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: user time %s", worker->pool->poolnum, (int)worker->pid, moonbr_format_timeval(&childusage.ru_utime));
|
jbe@128
|
1207 moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: system time %s", worker->pool->poolnum, (int)worker->pid, moonbr_format_timeval(&childusage.ru_stime));
|
jbe@128
|
1208 moonbr_try_destroy_worker_stat("max resident set size", ru_maxrss);
|
jbe@128
|
1209 moonbr_try_destroy_worker_stat("integral shared memory size", ru_ixrss);
|
jbe@128
|
1210 moonbr_try_destroy_worker_stat("integral unshared data", ru_idrss);
|
jbe@128
|
1211 moonbr_try_destroy_worker_stat("integral unshared stack", ru_isrss);
|
jbe@128
|
1212 moonbr_try_destroy_worker_stat("page replaims", ru_minflt);
|
jbe@128
|
1213 moonbr_try_destroy_worker_stat("page faults", ru_majflt);
|
jbe@128
|
1214 moonbr_try_destroy_worker_stat("swaps", ru_nswap);
|
jbe@128
|
1215 moonbr_try_destroy_worker_stat("block input operations", ru_inblock);
|
jbe@128
|
1216 moonbr_try_destroy_worker_stat("block output operations", ru_oublock);
|
jbe@128
|
1217 moonbr_try_destroy_worker_stat("messages sent", ru_msgsnd);
|
jbe@128
|
1218 moonbr_try_destroy_worker_stat("messages received", ru_msgrcv);
|
jbe@128
|
1219 moonbr_try_destroy_worker_stat("signals received", ru_nsignals);
|
jbe@128
|
1220 moonbr_try_destroy_worker_stat("voluntary context switches", ru_nvcsw);
|
jbe@128
|
1221 moonbr_try_destroy_worker_stat("involuntary context switches", ru_nivcsw);
|
jbe@128
|
1222 }
|
jbe@128
|
1223 }
|
jbe@128
|
1224 {
|
jbe@128
|
1225 int retval = (
|
jbe@128
|
1226 (worker->idle || worker->assigned) ?
|
jbe@128
|
1227 MOONBR_DESTROY_IDLE_OR_ASSIGNED :
|
jbe@128
|
1228 MOONBR_DESTROY_PREPARE
|
jbe@128
|
1229 );
|
jbe@128
|
1230 if (worker->main) moonbr_initiate_shutdown();
|
jbe@128
|
1231 if (worker->prev_worker) worker->prev_worker->next_worker = worker->next_worker;
|
jbe@128
|
1232 else worker->pool->first_worker = worker->next_worker;
|
jbe@128
|
1233 if (worker->next_worker) worker->next_worker->prev_worker = worker->prev_worker;
|
jbe@128
|
1234 else worker->pool->last_worker = worker->prev_worker;
|
jbe@128
|
1235 if (worker->idle) {
|
jbe@128
|
1236 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker->next_idle_worker;
|
jbe@128
|
1237 else worker->pool->first_idle_worker = worker->next_idle_worker;
|
jbe@128
|
1238 if (worker->next_idle_worker) worker->next_idle_worker->prev_idle_worker = worker->prev_idle_worker;
|
jbe@128
|
1239 else worker->pool->last_idle_worker = worker->prev_idle_worker;
|
jbe@128
|
1240 worker->pool->idle_worker_count--;
|
jbe@128
|
1241 }
|
jbe@128
|
1242 if (!worker->assigned) worker->pool->unassigned_worker_count--;
|
jbe@128
|
1243 worker->pool->total_worker_count--;
|
jbe@128
|
1244 worker->pool->worker_count_stat = 1;
|
jbe@128
|
1245 if (worker->errorlinebuf) free(worker->errorlinebuf);
|
jbe@128
|
1246 free(worker);
|
jbe@128
|
1247 return retval;
|
jbe@128
|
1248 }
|
jbe@128
|
1249 }
|
jbe@128
|
1250
|
jbe@128
|
1251 /* Marks a worker as idle and stores it in a queue, optionally setting 'idle_expiration' value */
|
jbe@128
|
1252 static void moonbr_add_idle_worker(struct moonbr_worker *worker) {
|
jbe@128
|
1253 worker->prev_idle_worker = worker->pool->last_idle_worker;
|
jbe@128
|
1254 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker;
|
jbe@128
|
1255 else worker->pool->first_idle_worker = worker;
|
jbe@128
|
1256 worker->pool->last_idle_worker = worker;
|
jbe@128
|
1257 worker->idle = 1;
|
jbe@128
|
1258 worker->pool->idle_worker_count++;
|
jbe@128
|
1259 if (worker->assigned) {
|
jbe@128
|
1260 worker->assigned = 0;
|
jbe@128
|
1261 worker->pool->unassigned_worker_count++;
|
jbe@128
|
1262 }
|
jbe@128
|
1263 worker->pool->worker_count_stat = 1;
|
jbe@128
|
1264 if (timerisset(&worker->pool->idle_timeout)) {
|
jbe@128
|
1265 struct timeval now;
|
jbe@128
|
1266 moonbr_now(&now);
|
jbe@128
|
1267 timeradd(&now, &worker->pool->idle_timeout, &worker->idle_expiration);
|
jbe@128
|
1268 }
|
jbe@128
|
1269 }
|
jbe@128
|
1270
|
jbe@128
|
1271 /* Pops a worker from the queue of idle workers (idle queue must not be empty) */
|
jbe@128
|
1272 static struct moonbr_worker *moonbr_pop_idle_worker(struct moonbr_pool *pool) {
|
jbe@128
|
1273 struct moonbr_worker *worker;
|
jbe@128
|
1274 worker = pool->first_idle_worker;
|
jbe@128
|
1275 pool->first_idle_worker = worker->next_idle_worker;
|
jbe@128
|
1276 if (pool->first_idle_worker) pool->first_idle_worker->prev_idle_worker = NULL;
|
jbe@128
|
1277 else pool->last_idle_worker = NULL;
|
jbe@128
|
1278 worker->next_idle_worker = NULL;
|
jbe@128
|
1279 worker->idle = 0;
|
jbe@128
|
1280 worker->pool->idle_worker_count--;
|
jbe@128
|
1281 worker->assigned = 1;
|
jbe@128
|
1282 worker->pool->unassigned_worker_count--;
|
jbe@128
|
1283 worker->pool->worker_count_stat = 1;
|
jbe@128
|
1284 return worker;
|
jbe@128
|
1285 }
|
jbe@128
|
1286
|
jbe@128
|
1287
|
jbe@0
|
1288 /*** Functions to communicate with child processes ***/
|
jbe@0
|
1289
|
jbe@0
|
1290 /* Tells child process to terminate */
|
jbe@0
|
1291 static void moonbr_terminate_idle_worker(struct moonbr_worker *worker) {
|
jbe@0
|
1292 moonbr_send_control_message(worker, MOONBR_COMMAND_TERMINATE, -1, NULL);
|
jbe@0
|
1293 }
|
jbe@0
|
1294
|
jbe@0
|
1295 /* Handles status messages from child process */
|
jbe@0
|
1296 static void moonbr_read_controlchannel(struct moonbr_worker *worker) {
|
jbe@0
|
1297 char controlmsg;
|
jbe@0
|
1298 {
|
jbe@0
|
1299 ssize_t bytes_read;
|
jbe@0
|
1300 while ((bytes_read = read(worker->controlfd, &controlmsg, 1)) <= 0) {
|
jbe@0
|
1301 if (bytes_read == 0 || errno == ECONNRESET) {
|
jbe@0
|
1302 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i unexpectedly closed control socket", worker->pool->poolnum, (int)worker->pid);
|
jbe@0
|
1303 if (close(worker->controlfd) && errno != EINTR) {
|
jbe@0
|
1304 moonbr_log(LOG_CRIT, "Error while closing control socket to child process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, strerror(errno));
|
jbe@0
|
1305 moonbr_terminate_error();
|
jbe@0
|
1306 }
|
jbe@0
|
1307 worker->controlfd = -1;
|
jbe@0
|
1308 moonbr_poll_refresh_needed = 1;
|
jbe@0
|
1309 return;
|
jbe@0
|
1310 }
|
jbe@0
|
1311 if (errno != EINTR) {
|
jbe@0
|
1312 moonbr_log(LOG_CRIT, "Unexpected error while reading control socket from child process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, strerror(errno));
|
jbe@0
|
1313 moonbr_terminate_error();
|
jbe@0
|
1314 }
|
jbe@0
|
1315 }
|
jbe@0
|
1316 }
|
jbe@0
|
1317 if (worker->idle) {
|
jbe@0
|
1318 moonbr_log(LOG_CRIT, "Unexpected data from supposedly idle child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
|
jbe@0
|
1319 moonbr_terminate_error();
|
jbe@0
|
1320 }
|
jbe@0
|
1321 if (moonbr_debug) {
|
jbe@0
|
1322 moonbr_log(LOG_DEBUG, "Received control message from child in pool #%i with PID %i: \"%c\"", worker->pool->poolnum, (int)worker->pid, (int)controlmsg);
|
jbe@0
|
1323 }
|
jbe@0
|
1324 switch (controlmsg) {
|
jbe@0
|
1325 case MOONBR_STATUS_IDLE:
|
jbe@0
|
1326 if (moonbr_stat) {
|
jbe@0
|
1327 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i reports as idle", worker->pool->poolnum, (int)worker->pid);
|
jbe@0
|
1328 }
|
jbe@0
|
1329 moonbr_add_idle_worker(worker);
|
jbe@0
|
1330 break;
|
jbe@0
|
1331 case MOONBR_STATUS_GOODBYE:
|
jbe@0
|
1332 if (moonbr_stat) {
|
jbe@0
|
1333 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i announced termination", worker->pool->poolnum, (int)worker->pid);
|
jbe@0
|
1334 }
|
jbe@0
|
1335 if (close(worker->controlfd) && errno != EINTR) {
|
jbe@0
|
1336 moonbr_log(LOG_CRIT, "Error while closing control socket to child process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, strerror(errno));
|
jbe@0
|
1337 moonbr_terminate_error();
|
jbe@0
|
1338 }
|
jbe@0
|
1339 worker->controlfd = -1;
|
jbe@0
|
1340 moonbr_poll_refresh_needed = 1;
|
jbe@0
|
1341 break;
|
jbe@0
|
1342 default:
|
jbe@0
|
1343 moonbr_log(LOG_CRIT, "Received illegal data (\"%c\") while reading control socket from child process in pool #%i with PID %i", (int)controlmsg, worker->pool->poolnum, (int)worker->pid);
|
jbe@0
|
1344 moonbr_terminate_error();
|
jbe@0
|
1345 }
|
jbe@0
|
1346 }
|
jbe@0
|
1347
|
jbe@0
|
1348 /* Handles stderr stream from child process */
|
jbe@0
|
1349 static void moonbr_read_errorchannel(struct moonbr_worker *worker) {
|
jbe@0
|
1350 char staticbuf[MOONBR_MAXERRORLINELEN+1];
|
jbe@0
|
1351 char *buf = worker->errorlinebuf;
|
jbe@0
|
1352 if (!buf) buf = staticbuf;
|
jbe@0
|
1353 {
|
jbe@0
|
1354 ssize_t bytes_read;
|
jbe@0
|
1355 while (
|
jbe@0
|
1356 (bytes_read = read(
|
jbe@0
|
1357 worker->errorfd,
|
jbe@0
|
1358 buf + worker->errorlinelen,
|
jbe@0
|
1359 MOONBR_MAXERRORLINELEN+1 - worker->errorlinelen
|
jbe@0
|
1360 )) <= 0
|
jbe@0
|
1361 ) {
|
jbe@0
|
1362 if (bytes_read == 0 || errno == ECONNRESET) {
|
jbe@0
|
1363 if (moonbr_debug) {
|
jbe@0
|
1364 moonbr_log(LOG_DEBUG, "Child process in pool #%i with PID %i closed stderr socket", worker->pool->poolnum, (int)worker->pid);
|
jbe@0
|
1365 }
|
jbe@0
|
1366 if (close(worker->errorfd) && errno != EINTR) {
|
jbe@0
|
1367 moonbr_log(LOG_CRIT, "Error while closing stderr socket to child process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, strerror(errno));
|
jbe@0
|
1368 moonbr_terminate_error();
|
jbe@0
|
1369 }
|
jbe@0
|
1370 worker->errorfd = -1;
|
jbe@0
|
1371 moonbr_poll_refresh_needed = 1;
|
jbe@0
|
1372 break;
|
jbe@0
|
1373 }
|
jbe@0
|
1374 if (errno != EINTR) {
|
jbe@0
|
1375 moonbr_log(LOG_CRIT, "Unexpected error while reading stderr from child process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, strerror(errno));
|
jbe@0
|
1376 moonbr_terminate_error();
|
jbe@0
|
1377 }
|
jbe@0
|
1378 }
|
jbe@0
|
1379 worker->errorlinelen += bytes_read;
|
jbe@0
|
1380 }
|
jbe@0
|
1381 {
|
jbe@0
|
1382 int i;
|
jbe@0
|
1383 for (i=0; i<worker->errorlinelen; i++) {
|
jbe@0
|
1384 if (buf[i] == '\n') buf[i] = 0;
|
jbe@0
|
1385 if (!buf[i]) {
|
jbe@0
|
1386 if (worker->errorlineovf) {
|
jbe@0
|
1387 worker->errorlineovf = 0;
|
jbe@0
|
1388 } else {
|
jbe@0
|
1389 moonbr_log(LOG_WARNING, "Error log from process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, buf);
|
jbe@0
|
1390 }
|
jbe@0
|
1391 worker->errorlinelen -= i+1;
|
jbe@0
|
1392 memmove(buf, buf+i+1, worker->errorlinelen);
|
jbe@0
|
1393 i = -1;
|
jbe@0
|
1394 }
|
jbe@0
|
1395 }
|
jbe@0
|
1396 if (i > MOONBR_MAXERRORLINELEN) {
|
jbe@0
|
1397 buf[MOONBR_MAXERRORLINELEN] = 0;
|
jbe@0
|
1398 if (!worker->errorlineovf) {
|
jbe@0
|
1399 moonbr_log(LOG_WARNING, "Error log from process in pool #%i with PID %i (line has been truncated): %s", worker->pool->poolnum, (int)worker->pid, buf);
|
jbe@0
|
1400 }
|
jbe@0
|
1401 worker->errorlinelen = 0;
|
jbe@0
|
1402 worker->errorlineovf = 1;
|
jbe@0
|
1403 }
|
jbe@0
|
1404 }
|
jbe@0
|
1405 if (!worker->errorlinebuf && worker->errorlinelen) { /* allocate buffer on heap only if necessary */
|
jbe@0
|
1406 worker->errorlinebuf = malloc((MOONBR_MAXERRORLINELEN+1) * sizeof(char));
|
jbe@0
|
1407 if (!worker->errorlinebuf) {
|
jbe@0
|
1408 moonbr_log(LOG_CRIT, "Memory allocation error");
|
jbe@0
|
1409 moonbr_terminate_error();
|
jbe@0
|
1410 }
|
jbe@0
|
1411 memcpy(worker->errorlinebuf, staticbuf, worker->errorlinelen);
|
jbe@0
|
1412 }
|
jbe@0
|
1413 }
|
jbe@0
|
1414
|
jbe@0
|
1415
|
jbe@0
|
1416 /*** Handler for incoming connections ***/
|
jbe@0
|
1417
|
jbe@0
|
1418 /* Accepts one or more incoming connections on listener socket and passes it to worker(s) popped from idle queue */
|
jbe@0
|
1419 static void moonbr_connect(struct moonbr_pool *pool) {
|
jbe@0
|
1420 struct moonbr_listener *listener = moonbr_pop_connected_listener(pool);
|
jbe@0
|
1421 struct moonbr_worker *worker;
|
jbe@127
|
1422 if (listener->proto == MOONBR_PROTO_MAIN) {
|
jbe@127
|
1423 worker = moonbr_pop_idle_worker(pool);
|
jbe@127
|
1424 if (moonbr_stat) {
|
jbe@127
|
1425 moonbr_log(LOG_INFO, "Dispatching main thread of pool #%i to PID %i", listener->pool->poolnum, (int)worker->pid);
|
jbe@127
|
1426 }
|
jbe@127
|
1427 worker->main = 1;
|
jbe@127
|
1428 moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, -1, listener);
|
jbe@127
|
1429 /* do not push listener to queue of idle listeners */
|
jbe@127
|
1430 } else if (listener->proto == MOONBR_PROTO_INTERVAL) {
|
jbe@0
|
1431 worker = moonbr_pop_idle_worker(pool);
|
jbe@0
|
1432 if (moonbr_stat) {
|
jbe@125
|
1433 moonbr_log(LOG_INFO, "Dispatching interval timer \"%s\" of pool #%i to PID %i", listener->type_specific.interval.name, listener->pool->poolnum, (int)worker->pid);
|
jbe@0
|
1434 }
|
jbe@0
|
1435 worker->restart_interval_listener = listener;
|
jbe@124
|
1436 moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, -1, listener);
|
jbe@0
|
1437 /* do not push listener to queue of idle listeners yet */
|
jbe@125
|
1438 } else {
|
jbe@125
|
1439 int peerfd;
|
jbe@0
|
1440 do {
|
jbe@214
|
1441 #if defined(__linux__) && !defined(_GNU_SOURCE)
|
jbe@212
|
1442 peerfd = accept(listener->listenfd, NULL, NULL);
|
jbe@212
|
1443 if (peerfd != -1) {
|
jbe@212
|
1444 if (fcntl(peerfd, F_SETFD, FD_CLOEXEC) == -1) {
|
jbe@212
|
1445 moonbr_log(LOG_ERR, "Error in fcntl() call: %s", strerror(errno));
|
jbe@212
|
1446 moonbr_terminate_error();
|
jbe@212
|
1447 }
|
jbe@212
|
1448 }
|
jbe@214
|
1449 #else
|
jbe@214
|
1450 peerfd = accept4(listener->listenfd, NULL, NULL, SOCK_CLOEXEC);
|
jbe@214
|
1451 #endif
|
jbe@0
|
1452 if (peerfd == -1) {
|
jbe@0
|
1453 if (errno == EWOULDBLOCK) {
|
jbe@0
|
1454 break;
|
jbe@0
|
1455 } else if (errno == ECONNABORTED) {
|
jbe@125
|
1456 moonbr_log(LOG_WARNING, "Connection aborted before accepting it");
|
jbe@0
|
1457 break;
|
jbe@0
|
1458 } else if (errno != EINTR) {
|
jbe@0
|
1459 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
|
jbe@0
|
1460 moonbr_terminate_error();
|
jbe@0
|
1461 }
|
jbe@0
|
1462 } else {
|
jbe@0
|
1463 worker = moonbr_pop_idle_worker(pool);
|
jbe@0
|
1464 if (moonbr_stat) {
|
jbe@125
|
1465 moonbr_log(LOG_INFO, "Dispatching connection for pool #%i to PID %i", listener->pool->poolnum, (int)worker->pid);
|
jbe@0
|
1466 }
|
jbe@124
|
1467 moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, peerfd, listener);
|
jbe@0
|
1468 if (close(peerfd) && errno != EINTR) {
|
jbe@0
|
1469 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
|
jbe@0
|
1470 moonbr_terminate_error();
|
jbe@0
|
1471 }
|
jbe@0
|
1472 }
|
jbe@0
|
1473 } while (pool->first_idle_worker);
|
jbe@0
|
1474 moonbr_add_idle_listener(listener);
|
jbe@0
|
1475 }
|
jbe@0
|
1476 }
|
jbe@0
|
1477
|
jbe@0
|
1478
|
jbe@0
|
1479 /*** Functions to initialize and restart interval timers ***/
|
jbe@0
|
1480
|
jbe@0
|
1481 /* Initializes all interval timers */
|
jbe@0
|
1482 static void moonbr_interval_initialize() {
|
jbe@0
|
1483 struct timeval now;
|
jbe@0
|
1484 struct moonbr_pool *pool;
|
jbe@0
|
1485 moonbr_now(&now);
|
jbe@0
|
1486 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
|
jbe@0
|
1487 int i;
|
jbe@0
|
1488 for (i=0; i<pool->listener_count; i++) {
|
jbe@0
|
1489 struct moonbr_listener *listener = &pool->listener[i];
|
jbe@0
|
1490 if (listener->proto == MOONBR_PROTO_INTERVAL) {
|
jbe@0
|
1491 timeradd(
|
jbe@0
|
1492 &now,
|
jbe@125
|
1493 &listener->type_specific.interval.delay,
|
jbe@125
|
1494 &listener->type_specific.interval.wakeup
|
jbe@0
|
1495 );
|
jbe@0
|
1496 }
|
jbe@0
|
1497 }
|
jbe@0
|
1498 }
|
jbe@0
|
1499 }
|
jbe@0
|
1500
|
jbe@0
|
1501 /* If necessary, restarts interval timers and queues interval listener as idle after a worker changed status */
|
jbe@0
|
1502 static void moonbr_interval_restart(
|
jbe@0
|
1503 struct moonbr_worker *worker,
|
jbe@0
|
1504 struct timeval *now /* passed to synchronize with moonbr_run() function */
|
jbe@0
|
1505 ) {
|
jbe@0
|
1506 struct moonbr_listener *listener = worker->restart_interval_listener;
|
jbe@0
|
1507 if (listener) {
|
jbe@0
|
1508 moonbr_add_idle_listener(listener);
|
jbe@0
|
1509 worker->restart_interval_listener = NULL;
|
jbe@125
|
1510 if (listener->type_specific.interval.strict) {
|
jbe@0
|
1511 timeradd(
|
jbe@125
|
1512 &listener->type_specific.interval.wakeup,
|
jbe@125
|
1513 &listener->type_specific.interval.delay,
|
jbe@125
|
1514 &listener->type_specific.interval.wakeup
|
jbe@0
|
1515 );
|
jbe@125
|
1516 if (timercmp(&listener->type_specific.interval.wakeup, now, <)) {
|
jbe@125
|
1517 listener->type_specific.interval.wakeup = *now;
|
jbe@0
|
1518 }
|
jbe@0
|
1519 } else {
|
jbe@0
|
1520 timeradd(
|
jbe@0
|
1521 now,
|
jbe@125
|
1522 &listener->type_specific.interval.delay,
|
jbe@125
|
1523 &listener->type_specific.interval.wakeup
|
jbe@0
|
1524 );
|
jbe@0
|
1525 }
|
jbe@0
|
1526 }
|
jbe@0
|
1527 }
|
jbe@0
|
1528
|
jbe@0
|
1529
|
jbe@0
|
1530 /*** Main loop and helper functions ***/
|
jbe@0
|
1531
|
jbe@0
|
1532 /* Stores the earliest required wakeup time in 'wait' variable */
|
jbe@0
|
1533 static void moonbr_calc_wait(struct timeval *wait, struct timeval *wakeup) {
|
jbe@0
|
1534 if (!timerisset(wait) || timercmp(wakeup, wait, <)) *wait = *wakeup;
|
jbe@0
|
1535 }
|
jbe@0
|
1536
|
jbe@0
|
1537 /* Main loop of Moonbridge system (including initialization of signal handlers and polling structures) */
|
jbe@0
|
1538 static void moonbr_run(lua_State *L) {
|
jbe@0
|
1539 struct timeval now;
|
jbe@0
|
1540 struct moonbr_pool *pool;
|
jbe@0
|
1541 struct moonbr_worker *worker;
|
jbe@0
|
1542 struct moonbr_worker *next_worker; /* needed when worker is removed during iteration of workers */
|
jbe@0
|
1543 struct moonbr_listener *listener;
|
jbe@0
|
1544 struct moonbr_listener *next_listener; /* needed when listener is removed during iteration of listeners */
|
jbe@0
|
1545 int i;
|
jbe@0
|
1546 moonbr_poll_init(); /* must be executed before moonbr_signal_init() */
|
jbe@0
|
1547 moonbr_signal_init();
|
jbe@0
|
1548 moonbr_interval_initialize();
|
jbe@0
|
1549 moonbr_pstate = MOONBR_PSTATE_RUNNING;
|
jbe@0
|
1550 while (1) {
|
jbe@0
|
1551 struct timeval wait = {0, }; /* point in time when premature wakeup of poll() is required */
|
jbe@0
|
1552 if (moonbr_cond_interrupt) {
|
jbe@0
|
1553 moonbr_log(LOG_WARNING, "Fast shutdown requested");
|
jbe@0
|
1554 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
|
jbe@0
|
1555 }
|
jbe@0
|
1556 if (moonbr_cond_terminate) {
|
jbe@0
|
1557 moonbr_initiate_shutdown();
|
jbe@0
|
1558 moonbr_cond_terminate = 0;
|
jbe@0
|
1559 }
|
jbe@0
|
1560 moonbr_cond_child = 0; /* must not be reset between moonbr_try_destroy_worker() and poll() */
|
jbe@0
|
1561 moonbr_now(&now);
|
jbe@0
|
1562 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
|
jbe@0
|
1563 int terminated_worker_count = 0; /* allows shortcut for new worker creation */
|
jbe@0
|
1564 /* terminate idle workers when expired */
|
jbe@0
|
1565 if (timerisset(&pool->idle_timeout)) {
|
jbe@0
|
1566 while ((worker = pool->first_idle_worker) != NULL) {
|
jbe@0
|
1567 if (timercmp(&worker->idle_expiration, &now, >)) break;
|
jbe@0
|
1568 moonbr_pop_idle_worker(pool);
|
jbe@0
|
1569 moonbr_terminate_idle_worker(worker);
|
jbe@0
|
1570 }
|
jbe@0
|
1571 }
|
jbe@0
|
1572 /* mark listeners as connected when incoming connection is pending */
|
jbe@0
|
1573 for (listener=pool->first_idle_listener; listener; listener=next_listener) {
|
jbe@0
|
1574 next_listener = listener->next_listener; /* extra variable necessary due to changing list */
|
jbe@0
|
1575 if (listener->pollidx != -1) {
|
jbe@0
|
1576 if (moonbr_poll_fds[listener->pollidx].revents) {
|
jbe@0
|
1577 moonbr_poll_fds[listener->pollidx].revents = 0;
|
jbe@0
|
1578 moonbr_remove_idle_listener(listener);
|
jbe@0
|
1579 moonbr_add_connected_listener(listener);
|
jbe@0
|
1580 }
|
jbe@127
|
1581 } else if (
|
jbe@127
|
1582 listener->proto != MOONBR_PROTO_INTERVAL ||
|
jbe@127
|
1583 !timercmp(&listener->type_specific.interval.wakeup, &now, >)
|
jbe@127
|
1584 ) {
|
jbe@127
|
1585 moonbr_remove_idle_listener(listener);
|
jbe@127
|
1586 moonbr_add_connected_listener(listener);
|
jbe@0
|
1587 }
|
jbe@0
|
1588 }
|
jbe@0
|
1589 /* process input from child processes */
|
jbe@0
|
1590 for (i=0; i<moonbr_poll_worker_count; i++) {
|
jbe@0
|
1591 if (moonbr_poll_worker_fds[i].revents) {
|
jbe@0
|
1592 moonbr_poll_worker_fds[i].revents = 0;
|
jbe@0
|
1593 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[i];
|
jbe@0
|
1594 switch (poll_worker->channel) {
|
jbe@0
|
1595 case MOONBR_POLL_WORKER_CONTROLCHANNEL:
|
jbe@0
|
1596 moonbr_read_controlchannel(poll_worker->worker);
|
jbe@0
|
1597 moonbr_interval_restart(poll_worker->worker, &now);
|
jbe@0
|
1598 break;
|
jbe@0
|
1599 case MOONBR_POLL_WORKER_ERRORCHANNEL:
|
jbe@0
|
1600 moonbr_read_errorchannel(poll_worker->worker);
|
jbe@0
|
1601 break;
|
jbe@0
|
1602 }
|
jbe@0
|
1603 }
|
jbe@0
|
1604 }
|
jbe@0
|
1605 /* collect dead child processes */
|
jbe@0
|
1606 for (worker=pool->first_worker; worker; worker=next_worker) {
|
jbe@0
|
1607 next_worker = worker->next_worker; /* extra variable necessary due to changing list */
|
jbe@0
|
1608 switch (moonbr_try_destroy_worker(worker)) {
|
jbe@0
|
1609 case MOONBR_DESTROY_PREPARE:
|
jbe@0
|
1610 pool->use_fork_error_wakeup = 1;
|
jbe@0
|
1611 break;
|
jbe@0
|
1612 case MOONBR_DESTROY_IDLE_OR_ASSIGNED:
|
jbe@0
|
1613 terminated_worker_count++;
|
jbe@0
|
1614 break;
|
jbe@0
|
1615 }
|
jbe@0
|
1616 }
|
jbe@0
|
1617 if (!moonbr_shutdown_in_progress) {
|
jbe@130
|
1618 /* connect listeners with idle workers */
|
jbe@0
|
1619 while (pool->first_connected_listener && pool->first_idle_worker) {
|
jbe@0
|
1620 moonbr_connect(pool);
|
jbe@0
|
1621 }
|
jbe@130
|
1622 /* create new worker processes */
|
jbe@130
|
1623 while (
|
jbe@130
|
1624 pool->total_worker_count < pool->max_fork && (
|
jbe@130
|
1625 pool->unassigned_worker_count < pool->pre_fork ||
|
jbe@130
|
1626 pool->total_worker_count < pool->min_fork
|
jbe@130
|
1627 )
|
jbe@130
|
1628 ) {
|
jbe@130
|
1629 if (pool->use_fork_error_wakeup) {
|
jbe@130
|
1630 if (timercmp(&pool->fork_error_wakeup, &now, >)) {
|
jbe@130
|
1631 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
|
jbe@130
|
1632 break;
|
jbe@130
|
1633 }
|
jbe@130
|
1634 } else {
|
jbe@130
|
1635 if (terminated_worker_count) {
|
jbe@130
|
1636 terminated_worker_count--;
|
jbe@130
|
1637 } else if (timercmp(&pool->fork_wakeup, &now, >)) {
|
jbe@130
|
1638 moonbr_calc_wait(&wait, &pool->fork_wakeup);
|
jbe@130
|
1639 break;
|
jbe@130
|
1640 }
|
jbe@0
|
1641 }
|
jbe@130
|
1642 if (moonbr_create_worker(pool, L)) {
|
jbe@130
|
1643 /* on error, enforce error delay */
|
jbe@130
|
1644 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
|
jbe@130
|
1645 pool->use_fork_error_wakeup = 1;
|
jbe@130
|
1646 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
|
jbe@130
|
1647 break;
|
jbe@130
|
1648 } else {
|
jbe@130
|
1649 /* normal fork delay on success */
|
jbe@130
|
1650 timeradd(&now, &pool->fork_delay, &pool->fork_wakeup);
|
jbe@130
|
1651 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
|
jbe@130
|
1652 pool->use_fork_error_wakeup = 0; /* gets set later if error occures during preparation */
|
jbe@130
|
1653 }
|
jbe@130
|
1654 }
|
jbe@130
|
1655 /* terminate excessive worker processes */
|
jbe@130
|
1656 while (
|
jbe@130
|
1657 pool->total_worker_count > pool->min_fork &&
|
jbe@130
|
1658 pool->idle_worker_count > pool->pre_fork
|
jbe@130
|
1659 ) {
|
jbe@130
|
1660 if (timerisset(&pool->exit_wakeup)) {
|
jbe@130
|
1661 if (timercmp(&pool->exit_wakeup, &now, >)) {
|
jbe@130
|
1662 moonbr_calc_wait(&wait, &pool->exit_wakeup);
|
jbe@130
|
1663 break;
|
jbe@130
|
1664 }
|
jbe@130
|
1665 moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
|
jbe@130
|
1666 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
|
jbe@130
|
1667 } else {
|
jbe@130
|
1668 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
|
jbe@0
|
1669 break;
|
jbe@0
|
1670 }
|
jbe@0
|
1671 }
|
jbe@130
|
1672 if (!(
|
jbe@130
|
1673 pool->total_worker_count > pool->min_fork &&
|
jbe@130
|
1674 pool->idle_worker_count > pool->pre_fork
|
jbe@130
|
1675 )) {
|
jbe@130
|
1676 timerclear(&pool->exit_wakeup); /* timer gets restarted later when there are excessive workers */
|
jbe@0
|
1677 }
|
jbe@0
|
1678 }
|
jbe@0
|
1679 /* optionally output worker count stats */
|
jbe@0
|
1680 if (moonbr_stat && pool->worker_count_stat) {
|
jbe@0
|
1681 pool->worker_count_stat = 0;
|
jbe@0
|
1682 moonbr_log(
|
jbe@0
|
1683 LOG_INFO,
|
jbe@0
|
1684 "Worker count for pool #%i: %i idle, %i assigned, %i total",
|
jbe@0
|
1685 pool->poolnum, pool->idle_worker_count,
|
jbe@0
|
1686 pool->total_worker_count - pool->unassigned_worker_count,
|
jbe@0
|
1687 pool->total_worker_count);
|
jbe@0
|
1688 }
|
jbe@0
|
1689 /* calculate wakeup time for interval listeners */
|
jbe@0
|
1690 for (listener=pool->first_idle_listener; listener; listener=listener->next_listener) {
|
jbe@0
|
1691 if (listener->proto == MOONBR_PROTO_INTERVAL) {
|
jbe@125
|
1692 moonbr_calc_wait(&wait, &listener->type_specific.interval.wakeup);
|
jbe@0
|
1693 }
|
jbe@0
|
1694 }
|
jbe@0
|
1695 /* calculate wakeup time for idle workers (only first idle worker is significant) */
|
jbe@0
|
1696 if (timerisset(&pool->idle_timeout) && pool->first_idle_worker) {
|
jbe@0
|
1697 moonbr_calc_wait(&wait, &pool->first_idle_worker->idle_expiration);
|
jbe@0
|
1698 }
|
jbe@0
|
1699 }
|
jbe@130
|
1700 /* terminate idle workers in case of shutdown and check if shutdown is complete */
|
jbe@0
|
1701 if (moonbr_shutdown_in_progress) {
|
jbe@195
|
1702 int remaining = 0;
|
jbe@0
|
1703 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
|
jbe@195
|
1704 while (pool->idle_worker_count) {
|
jbe@195
|
1705 moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
|
jbe@130
|
1706 }
|
jbe@195
|
1707 if (pool->first_worker) remaining = 1;
|
jbe@0
|
1708 }
|
jbe@195
|
1709 if (!remaining) {
|
jbe@0
|
1710 moonbr_log(LOG_INFO, "All worker threads have terminated");
|
jbe@0
|
1711 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
|
jbe@0
|
1712 }
|
jbe@0
|
1713 }
|
jbe@0
|
1714 if (moonbr_poll_refresh_needed) moonbr_poll_refresh();
|
jbe@0
|
1715 moonbr_cond_poll = 1;
|
jbe@0
|
1716 if (!moonbr_cond_child && !moonbr_cond_terminate && !moonbr_cond_interrupt) {
|
jbe@0
|
1717 int timeout;
|
jbe@0
|
1718 if (timerisset(&wait)) {
|
jbe@0
|
1719 if (timercmp(&wait, &now, <)) {
|
jbe@0
|
1720 moonbr_log(LOG_CRIT, "Internal error (should not happen): Future is in the past");
|
jbe@0
|
1721 moonbr_terminate_error();
|
jbe@0
|
1722 }
|
jbe@0
|
1723 timersub(&wait, &now, &wait);
|
jbe@0
|
1724 timeout = wait.tv_sec * 1000 + wait.tv_usec / 1000;
|
jbe@0
|
1725 } else {
|
jbe@0
|
1726 timeout = INFTIM;
|
jbe@0
|
1727 }
|
jbe@0
|
1728 if (moonbr_debug) {
|
jbe@0
|
1729 moonbr_log(LOG_DEBUG, "Waiting for I/O");
|
jbe@0
|
1730 }
|
jbe@0
|
1731 poll(moonbr_poll_fds, moonbr_poll_fds_count, timeout);
|
jbe@0
|
1732 } else {
|
jbe@0
|
1733 if (moonbr_debug) {
|
jbe@0
|
1734 moonbr_log(LOG_DEBUG, "Do not wait for I/O");
|
jbe@0
|
1735 }
|
jbe@0
|
1736 }
|
jbe@0
|
1737 moonbr_cond_poll = 0;
|
jbe@0
|
1738 moonbr_poll_reset_signal();
|
jbe@0
|
1739 }
|
jbe@0
|
1740 }
|
jbe@0
|
1741
|
jbe@0
|
1742
|
jbe@0
|
1743 /*** Lua interface ***/
|
jbe@0
|
1744
|
jbe@0
|
1745 static int moonbr_lua_panic(lua_State *L) {
|
jbe@0
|
1746 const char *errmsg;
|
jbe@0
|
1747 errmsg = lua_tostring(L, -1);
|
jbe@0
|
1748 if (!errmsg) {
|
jbe@0
|
1749 if (lua_isnoneornil(L, -1)) errmsg = "(error message is nil)";
|
jbe@0
|
1750 else errmsg = "(error message is not a string)";
|
jbe@0
|
1751 }
|
jbe@0
|
1752 if (moonbr_pstate == MOONBR_PSTATE_FORKED) {
|
jbe@0
|
1753 fprintf(stderr, "Uncaught Lua error: %s\n", errmsg);
|
jbe@0
|
1754 exit(1);
|
jbe@0
|
1755 } else {
|
jbe@0
|
1756 moonbr_log(LOG_CRIT, "Uncaught Lua error: %s", errmsg);
|
jbe@0
|
1757 moonbr_terminate_error();
|
jbe@0
|
1758 }
|
jbe@0
|
1759 return 0;
|
jbe@0
|
1760 }
|
jbe@0
|
1761
|
jbe@0
|
1762 static int moonbr_addtraceback(lua_State *L) {
|
jbe@0
|
1763 luaL_traceback(L, L, luaL_tolstring(L, 1, NULL), 1);
|
jbe@0
|
1764 return 1;
|
jbe@0
|
1765 }
|
jbe@0
|
1766
|
jbe@0
|
1767 /* Memory allocator that allows limiting memory consumption */
|
jbe@0
|
1768 static void *moonbr_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
|
jbe@0
|
1769 (void)ud; /* not used */
|
jbe@0
|
1770 if (nsize == 0) {
|
jbe@0
|
1771 if (ptr) {
|
jbe@0
|
1772 moonbr_memory_usage -= osize;
|
jbe@0
|
1773 free(ptr);
|
jbe@0
|
1774 }
|
jbe@0
|
1775 return NULL;
|
jbe@0
|
1776 } else if (ptr) {
|
jbe@0
|
1777 if (
|
jbe@0
|
1778 moonbr_memory_limit &&
|
jbe@0
|
1779 nsize > osize &&
|
jbe@0
|
1780 moonbr_memory_usage + (nsize - osize) > moonbr_memory_limit
|
jbe@0
|
1781 ) {
|
jbe@0
|
1782 return NULL;
|
jbe@0
|
1783 } else {
|
jbe@0
|
1784 ptr = realloc(ptr, nsize);
|
jbe@0
|
1785 if (ptr) moonbr_memory_usage += nsize - osize;
|
jbe@0
|
1786 }
|
jbe@0
|
1787 } else {
|
jbe@0
|
1788 if (
|
jbe@0
|
1789 moonbr_memory_limit &&
|
jbe@0
|
1790 moonbr_memory_usage + nsize > moonbr_memory_limit
|
jbe@0
|
1791 ) {
|
jbe@0
|
1792 return NULL;
|
jbe@0
|
1793 } else {
|
jbe@0
|
1794 ptr = realloc(ptr, nsize);
|
jbe@0
|
1795 if (ptr) moonbr_memory_usage += nsize;
|
jbe@0
|
1796 }
|
jbe@0
|
1797 }
|
jbe@0
|
1798 return ptr;
|
jbe@0
|
1799 }
|
jbe@0
|
1800
|
jbe@66
|
1801 static int moonbr_lua_tonatural(lua_State *L, int idx) {
|
jbe@66
|
1802 int isnum;
|
jbe@66
|
1803 lua_Number n;
|
jbe@66
|
1804 n = lua_tonumberx(L, idx, &isnum);
|
jbe@66
|
1805 if (isnum && n>=0 && n<INT_MAX && (lua_Number)(int)n == n) return n;
|
jbe@66
|
1806 else return -1;
|
jbe@66
|
1807 }
|
jbe@66
|
1808
|
jbe@66
|
1809 static int moonbr_lua_totimeval(lua_State *L, int idx, struct timeval *value) {
|
jbe@66
|
1810 int isnum;
|
jbe@66
|
1811 lua_Number n;
|
jbe@66
|
1812 n = lua_tonumberx(L, idx, &isnum);
|
jbe@66
|
1813 if (isnum && n>=0 && n<=100000000) {
|
jbe@66
|
1814 value->tv_sec = n;
|
jbe@66
|
1815 value->tv_usec = 1e6 * (n - value->tv_sec);
|
jbe@66
|
1816 return 1;
|
jbe@66
|
1817 } else {
|
jbe@66
|
1818 return 0;
|
jbe@66
|
1819 }
|
jbe@66
|
1820 }
|
jbe@66
|
1821
|
jbe@0
|
1822 static int moonbr_timeout(lua_State *L) {
|
jbe@0
|
1823 struct itimerval oldval;
|
jbe@0
|
1824 if (lua_isnoneornil(L, 1) && lua_isnoneornil(L, 2)) {
|
jbe@0
|
1825 getitimer(ITIMER_REAL, &oldval);
|
jbe@0
|
1826 } else {
|
jbe@0
|
1827 struct itimerval newval = {};
|
jbe@39
|
1828 timerclear(&newval.it_interval);
|
jbe@39
|
1829 timerclear(&newval.it_value);
|
jbe@0
|
1830 if (lua_toboolean(L, 1)) {
|
jbe@0
|
1831 luaL_argcheck(
|
jbe@0
|
1832 L, moonbr_lua_totimeval(L, 1, &newval.it_value), 1,
|
jbe@0
|
1833 "interval in seconds expected"
|
jbe@0
|
1834 );
|
jbe@0
|
1835 }
|
jbe@0
|
1836 if (lua_isnoneornil(L, 2)) {
|
jbe@0
|
1837 if (setitimer(ITIMER_REAL, &newval, &oldval)) {
|
jbe@0
|
1838 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
|
jbe@0
|
1839 moonbr_terminate_error();
|
jbe@0
|
1840 }
|
jbe@0
|
1841 } else {
|
jbe@0
|
1842 getitimer(ITIMER_REAL, &oldval);
|
jbe@39
|
1843 if (!timerisset(&oldval.it_value)) {
|
jbe@39
|
1844 if (setitimer(ITIMER_REAL, &newval, NULL)) {
|
jbe@39
|
1845 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
|
jbe@39
|
1846 moonbr_terminate_error();
|
jbe@39
|
1847 }
|
jbe@39
|
1848 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
|
jbe@39
|
1849 timerclear(&newval.it_value);
|
jbe@39
|
1850 if (setitimer(ITIMER_REAL, &newval, NULL)) {
|
jbe@39
|
1851 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
|
jbe@39
|
1852 moonbr_terminate_error();
|
jbe@39
|
1853 }
|
jbe@39
|
1854 } else if (timercmp(&newval.it_value, &oldval.it_value, <)) {
|
jbe@0
|
1855 struct itimerval remval;
|
jbe@0
|
1856 if (setitimer(ITIMER_REAL, &newval, NULL)) {
|
jbe@0
|
1857 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
|
jbe@0
|
1858 moonbr_terminate_error();
|
jbe@0
|
1859 }
|
jbe@0
|
1860 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
|
jbe@0
|
1861 getitimer(ITIMER_REAL, &remval);
|
jbe@0
|
1862 timersub(&oldval.it_value, &newval.it_value, &newval.it_value);
|
jbe@0
|
1863 timeradd(&newval.it_value, &remval.it_value, &newval.it_value);
|
jbe@0
|
1864 if (setitimer(ITIMER_REAL, &newval, NULL)) {
|
jbe@0
|
1865 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
|
jbe@0
|
1866 moonbr_terminate_error();
|
jbe@0
|
1867 }
|
jbe@0
|
1868 } else {
|
jbe@0
|
1869 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
|
jbe@0
|
1870 }
|
jbe@0
|
1871 return lua_gettop(L) - 1;
|
jbe@0
|
1872 }
|
jbe@0
|
1873 }
|
jbe@115
|
1874 lua_pushnumber(L, oldval.it_value.tv_sec + 1e-6 * oldval.it_value.tv_usec);
|
jbe@0
|
1875 return 1;
|
jbe@0
|
1876 }
|
jbe@0
|
1877
|
jbe@0
|
1878 #define moonbr_listen_init_pool_forkoption(luaname, cname, defval) { \
|
jbe@0
|
1879 lua_getfield(L, 2, luaname); \
|
jbe@0
|
1880 pool->cname = lua_isnil(L, -1) ? (defval) : moonbr_lua_tonatural(L, -1); \
|
jbe@0
|
1881 } while(0)
|
jbe@0
|
1882
|
jbe@0
|
1883 #define moonbr_listen_init_pool_timeoption(luaname, cname, defval, defvalu) ( \
|
jbe@0
|
1884 lua_getfield(L, 2, luaname), \
|
jbe@0
|
1885 lua_isnil(L, -1) ? ( \
|
jbe@0
|
1886 pool->cname.tv_sec = (defval), pool->cname.tv_usec = (defvalu), \
|
jbe@0
|
1887 1 \
|
jbe@0
|
1888 ) : ( \
|
jbe@0
|
1889 (lua_isboolean(L, -1) && !lua_toboolean(L, -1)) ? ( \
|
jbe@0
|
1890 pool->cname.tv_sec = 0, pool->cname.tv_usec = 0, \
|
jbe@0
|
1891 1 \
|
jbe@0
|
1892 ) : ( \
|
jbe@0
|
1893 moonbr_lua_totimeval(L, -1, &pool->cname) \
|
jbe@0
|
1894 ) \
|
jbe@0
|
1895 ) \
|
jbe@0
|
1896 )
|
jbe@0
|
1897
|
jbe@0
|
1898 static int moonbr_listen_init_pool(lua_State *L) {
|
jbe@0
|
1899 struct moonbr_pool *pool;
|
jbe@0
|
1900 const char *proto;
|
jbe@0
|
1901 int i;
|
jbe@127
|
1902 int dynamic = 0; /* nonzero = listeners exist which require dynamic worker creation */
|
jbe@0
|
1903 pool = lua_touserdata(L, 1);
|
jbe@0
|
1904 for (i=0; i<pool->listener_count; i++) {
|
jbe@0
|
1905 struct moonbr_listener *listener = &pool->listener[i];
|
jbe@0
|
1906 lua_settop(L, 2);
|
jbe@3
|
1907 #if LUA_VERSION_NUM >= 503
|
jbe@3
|
1908 lua_geti(L, 2, i+1);
|
jbe@3
|
1909 #else
|
jbe@0
|
1910 lua_pushinteger(L, i+1);
|
jbe@0
|
1911 lua_gettable(L, 2);
|
jbe@3
|
1912 #endif
|
jbe@0
|
1913 lua_getfield(L, 3, "proto");
|
jbe@0
|
1914 proto = lua_tostring(L, -1);
|
jbe@127
|
1915 if (proto && !strcmp(proto, "main")) {
|
jbe@127
|
1916 listener->proto = MOONBR_PROTO_MAIN;
|
jbe@127
|
1917 } else if (proto && !strcmp(proto, "interval")) {
|
jbe@127
|
1918 dynamic = 1;
|
jbe@0
|
1919 listener->proto = MOONBR_PROTO_INTERVAL;
|
jbe@0
|
1920 lua_getfield(L, 3, "name");
|
jbe@0
|
1921 {
|
jbe@0
|
1922 const char *name = lua_tostring(L, -1);
|
jbe@0
|
1923 if (name) {
|
jbe@211
|
1924 char *name_dup = strdup(name);
|
jbe@211
|
1925 if (!name_dup) {
|
jbe@0
|
1926 moonbr_log(LOG_CRIT, "Memory allocation_error");
|
jbe@0
|
1927 moonbr_terminate_error();
|
jbe@0
|
1928 }
|
jbe@211
|
1929 listener->type_specific.interval.name = name_dup;
|
jbe@0
|
1930 }
|
jbe@0
|
1931 }
|
jbe@0
|
1932 lua_getfield(L, 3, "delay");
|
jbe@0
|
1933 if (
|
jbe@125
|
1934 !moonbr_lua_totimeval(L, -1, &listener->type_specific.interval.delay) ||
|
jbe@125
|
1935 !timerisset(&listener->type_specific.interval.delay)
|
jbe@0
|
1936 ) {
|
jbe@0
|
1937 luaL_error(L, "No valid interval delay specified; use listen{{proto=\"interval\", delay=...}, ...}");
|
jbe@0
|
1938 }
|
jbe@0
|
1939 lua_getfield(L, 3, "strict");
|
jbe@0
|
1940 if (!lua_isnil(L, -1)) {
|
jbe@0
|
1941 if (lua_isboolean(L, -1)) {
|
jbe@125
|
1942 if (lua_toboolean(L, -1)) listener->type_specific.interval.strict = 1;
|
jbe@0
|
1943 } else {
|
jbe@0
|
1944 luaL_error(L, "Option \"strict\" must be a boolean if set; use listen{{proto=\"interval\", strict=true, ...}, ...}");
|
jbe@0
|
1945 }
|
jbe@0
|
1946 }
|
jbe@0
|
1947 } else if (proto && !strcmp(proto, "local")) {
|
jbe@125
|
1948 const char *path;
|
jbe@125
|
1949 const int path_maxlen = (
|
jbe@125
|
1950 sizeof(listener->type_specific.socket.addr.addr_un) -
|
jbe@125
|
1951 ((void *)listener->type_specific.socket.addr.addr_un.sun_path - (void *)&listener->type_specific.socket.addr.addr_un)
|
jbe@125
|
1952 ) - 1; /* one byte for termination */
|
jbe@127
|
1953 dynamic = 1;
|
jbe@0
|
1954 listener->proto = MOONBR_PROTO_LOCAL;
|
jbe@0
|
1955 lua_getfield(L, 3, "path");
|
jbe@125
|
1956 path = lua_tostring(L, -1);
|
jbe@125
|
1957 if (!path) {
|
jbe@125
|
1958 luaL_error(L, "No valid path specified for local socket; use listen{{proto=\"local\", path=...}, ...}");
|
jbe@125
|
1959 }
|
jbe@125
|
1960 if (strlen(path) > path_maxlen) {
|
jbe@125
|
1961 luaL_error(L, "Path name for local socket exceeded maximum length of %i characters", path_maxlen);
|
jbe@125
|
1962 }
|
jbe@125
|
1963 strcpy(listener->type_specific.socket.addr.addr_un.sun_path, path);
|
jbe@125
|
1964 } else if (proto && !strcmp(proto, "tcp")) {
|
jbe@125
|
1965 const char *host, *port;
|
jbe@125
|
1966 struct addrinfo hints = { 0, };
|
jbe@125
|
1967 struct addrinfo *res, *addrinfo;
|
jbe@125
|
1968 int errcode;
|
jbe@125
|
1969 const char *ip;
|
jbe@127
|
1970 dynamic = 1;
|
jbe@125
|
1971 lua_getfield(L, 3, "host");
|
jbe@125
|
1972 host = lua_isnil(L, -1) ? "::" : lua_tostring(L, -1);
|
jbe@125
|
1973 if (!host) {
|
jbe@125
|
1974 luaL_error(L, "No host specified; use listen{{proto=\"tcp\", host=...}, ...}");
|
jbe@125
|
1975 }
|
jbe@125
|
1976 lua_getfield(L, 3, "port");
|
jbe@125
|
1977 port = lua_tostring(L, -1);
|
jbe@125
|
1978 if (!port) {
|
jbe@125
|
1979 luaL_error(L, "No port specified; use listen{{proto=\"tcp\", host=...}, ...}");
|
jbe@125
|
1980 }
|
jbe@125
|
1981 hints.ai_family = AF_UNSPEC;
|
jbe@125
|
1982 hints.ai_socktype = SOCK_STREAM;
|
jbe@125
|
1983 hints.ai_protocol = IPPROTO_TCP;
|
jbe@125
|
1984 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
|
jbe@125
|
1985 errcode = getaddrinfo(host, port, &hints, &res);
|
jbe@125
|
1986 if (errcode) {
|
jbe@125
|
1987 freeaddrinfo(res);
|
jbe@125
|
1988 if (errcode == EAI_SYSTEM) {
|
jbe@213
|
1989 char errmsg[MOONBR_MAXSTRERRORLEN] = MOONBR_STRERROR_R_MSG;
|
jbe@125
|
1990 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
|
jbe@125
|
1991 luaL_error(L, "Could not resolve host: %s: %s", gai_strerror(errcode), errmsg);
|
jbe@125
|
1992 } else {
|
jbe@125
|
1993 luaL_error(L, "Could not resolve host: %s", gai_strerror(errcode));
|
jbe@0
|
1994 }
|
jbe@0
|
1995 }
|
jbe@125
|
1996 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
|
jbe@125
|
1997 if (addrinfo->ai_family == AF_INET6) goto moonbr_listen_init_pool_found;
|
jbe@125
|
1998 }
|
jbe@125
|
1999 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
|
jbe@125
|
2000 if (addrinfo->ai_family == AF_INET) goto moonbr_listen_init_pool_found;
|
jbe@0
|
2001 }
|
jbe@125
|
2002 addrinfo = res;
|
jbe@125
|
2003 moonbr_listen_init_pool_found:
|
jbe@125
|
2004 if (addrinfo->ai_addrlen > sizeof(listener->type_specific.socket.addr)) {
|
jbe@125
|
2005 moonbr_log(LOG_CRIT, "Size of ai_addrlen is unexpectedly big (should not happen)");
|
jbe@125
|
2006 moonbr_terminate_error();
|
jbe@0
|
2007 }
|
jbe@125
|
2008 memcpy(&listener->type_specific.socket.addr, addrinfo->ai_addr, addrinfo->ai_addrlen);
|
jbe@125
|
2009 listener->type_specific.socket.addrlen = addrinfo->ai_addrlen;
|
jbe@125
|
2010 switch (addrinfo->ai_family) {
|
jbe@125
|
2011 case AF_INET6:
|
jbe@125
|
2012 ip = inet_ntop(
|
jbe@125
|
2013 addrinfo->ai_family,
|
jbe@125
|
2014 &((struct sockaddr_in6 *)addrinfo->ai_addr)->sin6_addr,
|
jbe@125
|
2015 listener->proto_specific.tcp.ip,
|
jbe@125
|
2016 INET6_ADDRSTRLEN
|
jbe@125
|
2017 );
|
jbe@125
|
2018 if (!ip) {
|
jbe@125
|
2019 moonbr_log(LOG_CRIT, "System error in inet_ntop call: %s", strerror(errno));
|
jbe@125
|
2020 moonbr_terminate_error();
|
jbe@125
|
2021 }
|
jbe@125
|
2022 listener->proto_specific.tcp.port = ntohs(((struct sockaddr_in6 *)addrinfo->ai_addr)->sin6_port);
|
jbe@125
|
2023 break;
|
jbe@125
|
2024 case AF_INET:
|
jbe@125
|
2025 ip = inet_ntop(
|
jbe@125
|
2026 addrinfo->ai_family,
|
jbe@125
|
2027 &((struct sockaddr_in *)addrinfo->ai_addr)->sin_addr,
|
jbe@125
|
2028 listener->proto_specific.tcp.ip,
|
jbe@125
|
2029 INET6_ADDRSTRLEN
|
jbe@125
|
2030 );
|
jbe@125
|
2031 if (!ip) {
|
jbe@125
|
2032 moonbr_log(LOG_CRIT, "System error in inet_ntop call: %s", strerror(errno));
|
jbe@125
|
2033 moonbr_terminate_error();
|
jbe@125
|
2034 }
|
jbe@125
|
2035 listener->proto_specific.tcp.port = ntohs(((struct sockaddr_in *)addrinfo->ai_addr)->sin_port);
|
jbe@125
|
2036 break;
|
jbe@125
|
2037 default:
|
jbe@125
|
2038 strcpy(listener->proto_specific.tcp.ip, "unknown");
|
jbe@125
|
2039 listener->proto_specific.tcp.port = 0;
|
jbe@0
|
2040 }
|
jbe@125
|
2041 listener->proto = MOONBR_PROTO_TCP;
|
jbe@125
|
2042 } else if (proto) {
|
jbe@125
|
2043 luaL_error(L, "Unknown protocol \"%s\"", proto);
|
jbe@125
|
2044 } else {
|
jbe@125
|
2045 luaL_error(L, "No valid protocol specified; use listen{{proto=..., ...}, ...}");
|
jbe@0
|
2046 }
|
jbe@0
|
2047 }
|
jbe@0
|
2048 lua_settop(L, 2);
|
jbe@127
|
2049 if (dynamic) {
|
jbe@127
|
2050 moonbr_listen_init_pool_forkoption("pre_fork", pre_fork, 1);
|
jbe@127
|
2051 moonbr_listen_init_pool_forkoption("min_fork", min_fork, pool->pre_fork > 2 ? pool->pre_fork : 2);
|
jbe@127
|
2052 moonbr_listen_init_pool_forkoption("max_fork", max_fork, pool->min_fork > 16 ? pool->min_fork : 16);
|
jbe@127
|
2053 if (!moonbr_listen_init_pool_timeoption("fork_delay", fork_delay, 0, 250000)) {
|
jbe@127
|
2054 luaL_error(L, "Option \"fork_delay\" is expected to be a non-negative number");
|
jbe@127
|
2055 }
|
jbe@127
|
2056 if (!moonbr_listen_init_pool_timeoption("fork_error_delay", fork_error_delay, 2, 0)) {
|
jbe@127
|
2057 luaL_error(L, "Option \"fork_error_delay\" is expected to be a non-negative number");
|
jbe@127
|
2058 }
|
jbe@127
|
2059 if (!moonbr_listen_init_pool_timeoption("exit_delay", exit_delay, 60, 0)) {
|
jbe@127
|
2060 luaL_error(L, "Option \"exit_delay\" is expected to be a non-negative number");
|
jbe@127
|
2061 }
|
jbe@127
|
2062 if (timercmp(&pool->fork_error_delay, &pool->fork_delay, <)) {
|
jbe@127
|
2063 pool->fork_error_delay = pool->fork_delay;
|
jbe@127
|
2064 }
|
jbe@127
|
2065 if (!moonbr_listen_init_pool_timeoption("idle_timeout", idle_timeout, 0, 0)) {
|
jbe@127
|
2066 luaL_error(L, "Option \"idle_timeout\" is expected to be a non-negative number");
|
jbe@127
|
2067 }
|
jbe@127
|
2068 } else {
|
jbe@127
|
2069 pool->pre_fork = 0;
|
jbe@127
|
2070 pool->min_fork = pool->listener_count;
|
jbe@127
|
2071 pool->max_fork = pool->listener_count;
|
jbe@0
|
2072 }
|
jbe@0
|
2073 lua_getfield(L, 2, "memory_limit");
|
jbe@0
|
2074 if (!lua_isnil(L, -1)) {
|
jbe@0
|
2075 int isnum;
|
jbe@0
|
2076 lua_Number n;
|
jbe@0
|
2077 n = lua_tonumberx(L, -1, &isnum);
|
jbe@0
|
2078 if (n < 0 || !isnum) {
|
jbe@0
|
2079 luaL_error(L, "Option \"memory_limit\" is expected to be a non-negative number");
|
jbe@0
|
2080 }
|
jbe@0
|
2081 pool->memory_limit = n;
|
jbe@0
|
2082 }
|
jbe@0
|
2083 lua_settop(L, 2);
|
jbe@0
|
2084 lua_getfield(L, 2, "prepare");
|
jbe@0
|
2085 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
|
jbe@0
|
2086 luaL_error(L, "Option \"prepare\" must be nil or a function");
|
jbe@0
|
2087 }
|
jbe@0
|
2088 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
|
jbe@0
|
2089 lua_getfield(L, 2, "connect");
|
jbe@0
|
2090 if (!lua_isfunction(L, -1)) {
|
jbe@0
|
2091 luaL_error(L, "Option \"connect\" must be a function; use listen{{...}, {...}, connect=function(socket) ... end, ...}");
|
jbe@0
|
2092 }
|
jbe@0
|
2093 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
|
jbe@0
|
2094 lua_getfield(L, 2, "finish");
|
jbe@0
|
2095 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
|
jbe@0
|
2096 luaL_error(L, "Option \"finish\" must be nil or a function");
|
jbe@0
|
2097 }
|
jbe@0
|
2098 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
|
jbe@0
|
2099 return 0;
|
jbe@0
|
2100 }
|
jbe@0
|
2101
|
jbe@0
|
2102 static int moonbr_listen(lua_State *L) {
|
jbe@0
|
2103 struct moonbr_pool *pool;
|
jbe@0
|
2104 lua_Integer listener_count;
|
jbe@0
|
2105 if (moonbr_booted) luaL_error(L, "Moonbridge bootup is already complete");
|
jbe@0
|
2106 luaL_checktype(L, 1, LUA_TTABLE);
|
jbe@0
|
2107 listener_count = luaL_len(L, 1);
|
jbe@0
|
2108 if (!listener_count) luaL_error(L, "No listen ports specified; use listen{{proto=..., port=...},...}");
|
jbe@0
|
2109 if (listener_count > 100) luaL_error(L, "Too many listeners");
|
jbe@0
|
2110 pool = moonbr_create_pool(listener_count);
|
jbe@0
|
2111 lua_pushcfunction(L, moonbr_listen_init_pool);
|
jbe@0
|
2112 lua_pushlightuserdata(L, pool);
|
jbe@0
|
2113 lua_pushvalue(L, 1);
|
jbe@0
|
2114 if (lua_pcall(L, 2, 0, 0)) goto moonbr_listen_error;
|
jbe@0
|
2115 {
|
jbe@0
|
2116 int i;
|
jbe@0
|
2117 i = moonbr_start_pool(pool);
|
jbe@0
|
2118 if (i >= 0) {
|
jbe@125
|
2119 lua_pushfstring(L, "Could not initialize listener #%d: %s", i+1, strerror(errno));
|
jbe@125
|
2120 moonbr_listen_error:
|
jbe@125
|
2121 moonbr_destroy_pool(pool);
|
jbe@125
|
2122 lua_pushnil(L);
|
jbe@125
|
2123 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
|
jbe@125
|
2124 lua_pushnil(L);
|
jbe@125
|
2125 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
|
jbe@125
|
2126 lua_pushnil(L);
|
jbe@125
|
2127 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
|
jbe@125
|
2128 lua_error(L);
|
jbe@0
|
2129 }
|
jbe@0
|
2130 }
|
jbe@0
|
2131 return 0;
|
jbe@0
|
2132 }
|
jbe@0
|
2133
|
jbe@0
|
2134
|
jbe@9
|
2135 /*** Function to modify Lua's library path and/or cpath ***/
|
jbe@9
|
2136
|
jbe@9
|
2137 #if defined(MOONBR_LUA_PATH) || defined(MOONBR_LUA_CPATH)
|
jbe@9
|
2138 static void moonbr_modify_path(lua_State *L, char *key, char *value) {
|
jbe@9
|
2139 int stackbase;
|
jbe@9
|
2140 stackbase = lua_gettop(L);
|
jbe@9
|
2141 lua_getglobal(L, "package");
|
jbe@9
|
2142 lua_getfield(L, stackbase+1, key);
|
jbe@9
|
2143 {
|
jbe@9
|
2144 const char *current_str;
|
jbe@9
|
2145 size_t current_strlen;
|
jbe@9
|
2146 luaL_Buffer buf;
|
jbe@9
|
2147 current_str = lua_tolstring(L, stackbase+2, ¤t_strlen);
|
jbe@9
|
2148 luaL_buffinit(L, &buf);
|
jbe@9
|
2149 if (current_str) {
|
jbe@9
|
2150 lua_pushvalue(L, stackbase+2);
|
jbe@9
|
2151 luaL_addvalue(&buf);
|
jbe@9
|
2152 if (current_strlen && current_str[current_strlen-1] != ';') {
|
jbe@9
|
2153 luaL_addchar(&buf, ';');
|
jbe@9
|
2154 }
|
jbe@9
|
2155 }
|
jbe@9
|
2156 luaL_addstring(&buf, value);
|
jbe@9
|
2157 luaL_pushresult(&buf);
|
jbe@9
|
2158 }
|
jbe@9
|
2159 lua_setfield(L, stackbase+1, key);
|
jbe@9
|
2160 lua_settop(L, stackbase);
|
jbe@9
|
2161 }
|
jbe@9
|
2162 #endif
|
jbe@9
|
2163
|
jbe@9
|
2164
|
jbe@0
|
2165 /*** Main function and command line invokation ***/
|
jbe@0
|
2166
|
jbe@0
|
2167 static void moonbr_usage(int err, const char *cmd) {
|
jbe@0
|
2168 FILE *out;
|
jbe@0
|
2169 out = err ? stderr : stdout;
|
jbe@0
|
2170 if (!cmd) cmd = "moonbridge";
|
jbe@0
|
2171 fprintf(out, "Get this help message: %s {-h|--help}\n", cmd);
|
jbe@0
|
2172 fprintf(out, "Usage: %s \\\n", cmd);
|
jbe@0
|
2173 fprintf(out, " [-b|--background] \\\n");
|
jbe@0
|
2174 fprintf(out, " [-d|--debug] \\\n");
|
jbe@0
|
2175 fprintf(out, " [-f|--logfacility {DAEMON|USER|0|1|...|7}] \\\n");
|
jbe@0
|
2176 fprintf(out, " [-i|--logident <syslog ident> \\\n");
|
jbe@0
|
2177 fprintf(out, " [-l|--logfile <logfile>] \\\n");
|
jbe@0
|
2178 fprintf(out, " [-p|--pidfile <pidfile>] \\\n");
|
jbe@0
|
2179 fprintf(out, " [-s|--stats] \\\n");
|
jbe@2
|
2180 fprintf(out, " -- <Lua script> [<cmdline options for Lua script>]\n");
|
jbe@0
|
2181 exit(err);
|
jbe@0
|
2182 }
|
jbe@0
|
2183
|
jbe@0
|
2184 #define moonbr_usage_error() moonbr_usage(MOONBR_EXITCODE_CMDLINEERROR, argc ? argv[0] : NULL)
|
jbe@0
|
2185
|
jbe@0
|
2186 int main(int argc, char **argv) {
|
jbe@0
|
2187 {
|
jbe@0
|
2188 int daemonize = 0;
|
jbe@0
|
2189 int log_facility = LOG_USER;
|
jbe@0
|
2190 const char *log_ident = "moonbridge";
|
jbe@0
|
2191 const char *log_filename = NULL;
|
jbe@0
|
2192 const char *pid_filename = NULL;
|
jbe@0
|
2193 int option;
|
jbe@0
|
2194 struct option longopts[] = {
|
jbe@0
|
2195 { "background", no_argument, NULL, 'b' },
|
jbe@0
|
2196 { "debug", no_argument, NULL, 'd' },
|
jbe@0
|
2197 { "logfacility", required_argument, NULL, 'f' },
|
jbe@0
|
2198 { "help", no_argument, NULL, 'h' },
|
jbe@0
|
2199 { "logident", required_argument, NULL, 'i' },
|
jbe@0
|
2200 { "logfile", required_argument, NULL, 'l' },
|
jbe@0
|
2201 { "pidfile", required_argument, NULL, 'p' },
|
jbe@231
|
2202 { "stats", no_argument, NULL, 's' },
|
jbe@231
|
2203 { NULL, 0, NULL, 0 }
|
jbe@0
|
2204 };
|
jbe@0
|
2205 while ((option = getopt_long(argc, argv, "bdf:hi:l:p:s", longopts, NULL)) != -1) {
|
jbe@0
|
2206 switch (option) {
|
jbe@0
|
2207 case 'b':
|
jbe@0
|
2208 daemonize = 1;
|
jbe@0
|
2209 break;
|
jbe@0
|
2210 case 'd':
|
jbe@0
|
2211 moonbr_debug = 1;
|
jbe@0
|
2212 moonbr_stat = 1;
|
jbe@0
|
2213 break;
|
jbe@0
|
2214 case 'f':
|
jbe@0
|
2215 if (!strcmp(optarg, "DAEMON")) {
|
jbe@0
|
2216 log_facility = LOG_DAEMON;
|
jbe@0
|
2217 } else if (!strcmp(optarg, "USER")) {
|
jbe@0
|
2218 log_facility = LOG_USER;
|
jbe@0
|
2219 } else if (!strcmp(optarg, "0")) {
|
jbe@0
|
2220 log_facility = LOG_LOCAL0;
|
jbe@0
|
2221 } else if (!strcmp(optarg, "1")) {
|
jbe@0
|
2222 log_facility = LOG_LOCAL1;
|
jbe@0
|
2223 } else if (!strcmp(optarg, "2")) {
|
jbe@0
|
2224 log_facility = LOG_LOCAL2;
|
jbe@0
|
2225 } else if (!strcmp(optarg, "3")) {
|
jbe@0
|
2226 log_facility = LOG_LOCAL3;
|
jbe@0
|
2227 } else if (!strcmp(optarg, "4")) {
|
jbe@0
|
2228 log_facility = LOG_LOCAL4;
|
jbe@0
|
2229 } else if (!strcmp(optarg, "5")) {
|
jbe@0
|
2230 log_facility = LOG_LOCAL5;
|
jbe@0
|
2231 } else if (!strcmp(optarg, "6")) {
|
jbe@0
|
2232 log_facility = LOG_LOCAL6;
|
jbe@0
|
2233 } else if (!strcmp(optarg, "7")) {
|
jbe@0
|
2234 log_facility = LOG_LOCAL7;
|
jbe@0
|
2235 } else {
|
jbe@0
|
2236 moonbr_usage_error();
|
jbe@0
|
2237 }
|
jbe@0
|
2238 moonbr_use_syslog = 1;
|
jbe@0
|
2239 break;
|
jbe@0
|
2240 case 'h':
|
jbe@0
|
2241 moonbr_usage(MOONBR_EXITCODE_GRACEFUL, argv[0]);
|
jbe@0
|
2242 break;
|
jbe@0
|
2243 case 'i':
|
jbe@0
|
2244 log_ident = optarg;
|
jbe@0
|
2245 moonbr_use_syslog = 1;
|
jbe@0
|
2246 break;
|
jbe@0
|
2247 case 'l':
|
jbe@0
|
2248 log_filename = optarg;
|
jbe@0
|
2249 break;
|
jbe@0
|
2250 case 'p':
|
jbe@0
|
2251 pid_filename = optarg;
|
jbe@0
|
2252 break;
|
jbe@0
|
2253 case 's':
|
jbe@0
|
2254 moonbr_stat = 1;
|
jbe@0
|
2255 break;
|
jbe@0
|
2256 default:
|
jbe@0
|
2257 moonbr_usage_error();
|
jbe@0
|
2258 }
|
jbe@0
|
2259 }
|
jbe@2
|
2260 if (argc - optind < 1) moonbr_usage_error();
|
jbe@0
|
2261 if (pid_filename) {
|
jbe@0
|
2262 pid_t otherpid;
|
jbe@0
|
2263 while ((moonbr_pidfh = pidfile_open(pid_filename, 0644, &otherpid)) == NULL) {
|
jbe@0
|
2264 if (errno == EEXIST) {
|
jbe@0
|
2265 if (otherpid == -1) {
|
jbe@0
|
2266 fprintf(stderr, "PID file \"%s\" is already locked\n", pid_filename);
|
jbe@0
|
2267 } else {
|
jbe@0
|
2268 fprintf(stderr, "PID file \"%s\" is already locked by process with PID: %i\n", pid_filename, (int)otherpid);
|
jbe@0
|
2269 }
|
jbe@0
|
2270 exit(MOONBR_EXITCODE_ALREADYRUNNING);
|
jbe@0
|
2271 } else if (errno != EINTR) {
|
jbe@0
|
2272 fprintf(stderr, "Could not write PID file \"%s\": %s\n", pid_filename, strerror(errno));
|
jbe@0
|
2273 exit(MOONBR_EXITCODE_STARTUPERROR);
|
jbe@0
|
2274 }
|
jbe@0
|
2275 }
|
jbe@0
|
2276 }
|
jbe@0
|
2277 if (log_filename) {
|
jbe@0
|
2278 int logfd;
|
jbe@0
|
2279 while (
|
jbe@0
|
2280 ( logfd = flopen(
|
jbe@0
|
2281 log_filename,
|
jbe@0
|
2282 O_WRONLY|O_NONBLOCK|O_CREAT|O_APPEND|O_CLOEXEC,
|
jbe@0
|
2283 0640
|
jbe@0
|
2284 )
|
jbe@0
|
2285 ) < 0
|
jbe@0
|
2286 ) {
|
jbe@0
|
2287 if (errno == EWOULDBLOCK) {
|
jbe@0
|
2288 fprintf(stderr, "Logfile \"%s\" is locked\n", log_filename);
|
jbe@0
|
2289 exit(MOONBR_EXITCODE_ALREADYRUNNING);
|
jbe@0
|
2290 } else if (errno != EINTR) {
|
jbe@0
|
2291 fprintf(stderr, "Could not open logfile \"%s\": %s\n", log_filename, strerror(errno));
|
jbe@0
|
2292 exit(MOONBR_EXITCODE_STARTUPERROR);
|
jbe@0
|
2293 }
|
jbe@0
|
2294 }
|
jbe@0
|
2295 moonbr_logfile = fdopen(logfd, "a");
|
jbe@0
|
2296 if (!moonbr_logfile) {
|
jbe@0
|
2297 fprintf(stderr, "Could not open write stream to logfile \"%s\": %s\n", log_filename, strerror(errno));
|
jbe@0
|
2298 exit(MOONBR_EXITCODE_STARTUPERROR);
|
jbe@0
|
2299 }
|
jbe@0
|
2300 }
|
jbe@0
|
2301 if (daemonize == 0 && !moonbr_logfile) moonbr_logfile = stderr;
|
jbe@0
|
2302 if (moonbr_logfile) setlinebuf(moonbr_logfile);
|
jbe@0
|
2303 else moonbr_use_syslog = 1;
|
jbe@0
|
2304 if (moonbr_use_syslog) openlog(log_ident, LOG_NDELAY | LOG_PID, log_facility);
|
jbe@0
|
2305 if (daemonize) {
|
jbe@0
|
2306 if (daemon(1, 0)) {
|
jbe@0
|
2307 moonbr_log(LOG_ERR, "Could not daemonize moonbridge process");
|
jbe@0
|
2308 moonbr_terminate_error();
|
jbe@0
|
2309 }
|
jbe@0
|
2310 }
|
jbe@0
|
2311 }
|
jbe@0
|
2312 moonbr_log(LOG_NOTICE, "Starting moonbridge server");
|
jbe@0
|
2313 if (moonbr_pidfh && pidfile_write(moonbr_pidfh)) {
|
jbe@0
|
2314 moonbr_log(LOG_ERR, "Could not write pidfile (after locking)");
|
jbe@0
|
2315 }
|
jbe@0
|
2316 {
|
jbe@0
|
2317 lua_State *L;
|
jbe@0
|
2318 L = lua_newstate(moonbr_alloc, NULL);
|
jbe@0
|
2319 if (!L) {
|
jbe@0
|
2320 moonbr_log(LOG_CRIT, "Could not initialize Lua state");
|
jbe@0
|
2321 moonbr_terminate_error();
|
jbe@0
|
2322 }
|
jbe@0
|
2323 lua_atpanic(L, moonbr_lua_panic);
|
jbe@58
|
2324 lua_pushliteral(L, MOONBR_VERSION_STRING);
|
jbe@58
|
2325 lua_setglobal(L, "_MOONBRIDGE_VERSION");
|
jbe@0
|
2326 luaL_openlibs(L);
|
jbe@89
|
2327 luaL_requiref(L, "moonbridge_io", luaopen_moonbridge_io, 1);
|
jbe@89
|
2328 lua_pop(L, 1);
|
jbe@8
|
2329 #ifdef MOONBR_LUA_PATH
|
jbe@9
|
2330 moonbr_modify_path(L, "path", MOONBR_LUA_PATH);
|
jbe@9
|
2331 #endif
|
jbe@9
|
2332 #ifdef MOONBR_LUA_CPATH
|
jbe@9
|
2333 moonbr_modify_path(L, "cpath", MOONBR_LUA_CPATH);
|
jbe@8
|
2334 #endif
|
jbe@0
|
2335 lua_pushcfunction(L, moonbr_timeout);
|
jbe@0
|
2336 lua_setglobal(L, "timeout");
|
jbe@0
|
2337 lua_pushcfunction(L, moonbr_listen);
|
jbe@0
|
2338 lua_setglobal(L, "listen");
|
jbe@17
|
2339 lua_pushcfunction(L, moonbr_addtraceback); /* on stack position 1 */
|
jbe@2
|
2340 moonbr_log(LOG_INFO, "Loading \"%s\"", argv[optind]);
|
jbe@2
|
2341 if (luaL_loadfile(L, argv[optind])) {
|
jbe@2
|
2342 moonbr_log(LOG_ERR, "Error while loading \"%s\": %s", argv[optind], lua_tostring(L, -1));
|
jbe@2
|
2343 moonbr_terminate_error();
|
jbe@2
|
2344 }
|
jbe@2
|
2345 { int i; for (i=optind+1; i<argc; i++) lua_pushstring(L, argv[i]); }
|
jbe@2
|
2346 if (lua_pcall(L, argc-(optind+1), 0, 1)) {
|
jbe@2
|
2347 moonbr_log(LOG_ERR, "Error while executing \"%s\": %s", argv[optind], lua_tostring(L, -1));
|
jbe@2
|
2348 moonbr_terminate_error();
|
jbe@0
|
2349 }
|
jbe@6
|
2350 if (!moonbr_first_pool) {
|
jbe@6
|
2351 moonbr_log(LOG_WARNING, "No listener initialized.");
|
jbe@6
|
2352 moonbr_terminate_error();
|
jbe@6
|
2353 }
|
jbe@0
|
2354 lua_getglobal(L, "listen");
|
jbe@0
|
2355 lua_pushcfunction(L, moonbr_listen);
|
jbe@0
|
2356 if (lua_compare(L, -2, -1, LUA_OPEQ)) {
|
jbe@0
|
2357 lua_pushnil(L);
|
jbe@0
|
2358 lua_setglobal(L, "listen");
|
jbe@0
|
2359 }
|
jbe@0
|
2360 lua_settop(L, 1);
|
jbe@223
|
2361 lua_gc(L, LUA_GCCOLLECT, 0); /* collect garbage before forking later */
|
jbe@0
|
2362 moonbr_run(L);
|
jbe@0
|
2363 }
|
jbe@0
|
2364 return 0;
|
jbe@0
|
2365 }
|
jbe@0
|
2366
|