moonbridge

view moonbridge.c @ 101:baa30bfec659

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

Impressum / About Us