moonbridge

view moonbridge.c @ 335:f115f1d5a48c

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

Impressum / About Us