moonbridge

view moonbridge.c @ 100:df1ab25c6513

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

Impressum / About Us