moonbridge

view moonbridge.c @ 115:7014436d88ea

Added helper function moonbridge_io.timeref(...); HTTP module sends 408 Request Timeout
author jbe
date Fri Apr 10 00:33:07 2015 +0200 (2015-04-10)
parents 113185a57b06
children 84aa2b8dcf79
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 }
336 /*** Functions for logging in master process ***/
338 /* Logs a pre-formatted message with given syslog() priority */
339 static void moonbr_log_msg(int priority, const char *msg) {
340 if (moonbr_logfile) {
341 /* logging to logfile desired (timestamp is prepended in that case) */
342 time_t now_time = 0;
343 struct tm now_tmstruct;
344 char timestr[MOONBR_LOG_MAXTIMELEN+1];
345 time(&now_time);
346 localtime_r(&now_time, &now_tmstruct);
347 if (!strftime(
348 timestr, MOONBR_LOG_MAXTIMELEN+1, "%Y-%m-%d %H:%M:%S %Z: ", &now_tmstruct
349 )) timestr[0] = 0;
350 fprintf(moonbr_logfile, "%s%s\n", timestr, msg);
351 }
352 if (moonbr_use_syslog) {
353 /* logging through syslog desired */
354 syslog(priority, "%s", msg);
355 }
356 }
358 /* Formats a message via vsnprintf() and logs it with given syslog() priority */
359 static void moonbr_log(int priority, const char *message, ...) {
360 char msgbuf[MOONBR_LOG_MAXMSGLEN+1]; /* buffer of static size to store formatted message */
361 int msglen; /* length of full message (may exceed MOONBR_LOG_MAXMSGLEN) */
362 {
363 /* pass variable arguments to vsnprintf() to format message */
364 va_list ap;
365 va_start(ap, message);
366 msglen = vsnprintf(msgbuf, MOONBR_LOG_MAXMSGLEN+1, message, ap);
367 va_end(ap);
368 }
369 {
370 /* split and log message line by line */
371 char *line = msgbuf;
372 while (1) {
373 char *endptr = strchr(line, '\n');
374 if (endptr) {
375 /* terminate string where newline character is found */
376 *endptr = 0;
377 } else if (line != msgbuf && msglen > MOONBR_LOG_MAXMSGLEN) {
378 /* break if line is incomplete and not the first line */
379 break;
380 }
381 moonbr_log_msg(priority, line);
382 if (!endptr) break; /* break if end of formatted message is reached */
383 line = endptr+1; /* otherwise continue with remaining message */
384 }
385 }
386 if (msglen > MOONBR_LOG_MAXMSGLEN) {
387 /* print warning if message was truncated */
388 moonbr_log_msg(priority, "Previous log message has been truncated due to excessive length");
389 }
390 }
393 /*** Termination function ***/
395 /* Kill all child processes, remove PID file (if existent), and exit master process with given exitcode */
396 static void moonbr_terminate(int exitcode) {
397 {
398 struct moonbr_pool *pool;
399 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
400 {
401 struct moonbr_worker *worker;
402 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
403 moonbr_log(LOG_INFO, "Sending SIGKILL to child with PID %i", (int)worker->pid);
404 if (kill(worker->pid, SIGKILL)) {
405 moonbr_log(LOG_ERR, "Error while killing child process: %s", strerror(errno));
406 }
407 }
408 }
409 {
410 int i;
411 for (i=0; i<pool->listener_count; i++) {
412 struct moonbr_listener *listener = &pool->listener[i];
413 if (listener->proto == MOONBR_PROTO_LOCAL) {
414 moonbr_log(LOG_INFO, "Unlinking local socket \"%s\"", listener->proto_specific.local.path);
415 if (unlink(listener->proto_specific.local.path)) {
416 moonbr_log(LOG_ERR, "Error while unlinking local socket: %s", strerror(errno));
417 }
418 }
419 }
420 }
421 }
422 }
423 moonbr_log(exitcode ? LOG_ERR : LOG_NOTICE, "Terminating with exit code %i", exitcode);
424 if (moonbr_pidfh && pidfile_remove(moonbr_pidfh)) {
425 moonbr_log(LOG_ERR, "Error while removing PID file: %s", strerror(errno));
426 }
427 exit(exitcode);
428 }
430 /* Terminate with either MOONBR_EXITCODE_STARTUPERROR or MOONBR_EXITCODE_RUNTIMEERROR */
431 #define moonbr_terminate_error() \
432 moonbr_terminate( \
433 moonbr_pstate == MOONBR_PSTATE_STARTUP ? \
434 MOONBR_EXITCODE_STARTUPERROR : \
435 MOONBR_EXITCODE_RUNTIMEERROR \
436 )
439 /*** Helper functions ***/
441 /* Fills a 'struct timeval' structure with the current time (using CLOCK_MONOTONIC) */
442 static void moonbr_now(struct timeval *now) {
443 struct timespec ts = {0, };
444 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
445 moonbr_log(LOG_CRIT, "Error in clock_gettime() call: %s", strerror(errno));
446 moonbr_terminate_error();
447 }
448 *now = (struct timeval){ .tv_sec = ts.tv_sec, .tv_usec = ts.tv_nsec / 1000 };
449 }
451 /* Formats a 'struct timeval' value (not thread-safe) */
452 static char *moonbr_format_timeval(struct timeval *t) {
453 static char buf[32];
454 snprintf(buf, 32, "%ji.%06ji seconds", (intmax_t)t->tv_sec, (intmax_t)t->tv_usec);
455 return buf;
456 }
459 /*** Functions for pool creation and startup ***/
461 /* Creates a 'struct moonbr_pool' structure with a given number of listeners */
462 static struct moonbr_pool *moonbr_create_pool(int listener_count) {
463 struct moonbr_pool *pool;
464 pool = calloc(1,
465 sizeof(struct moonbr_pool) + /* size of 'struct moonbr_pool' with one listener */
466 (listener_count-1) * sizeof(struct moonbr_listener) /* size of extra listeners */
467 );
468 if (!pool) {
469 moonbr_log(LOG_CRIT, "Memory allocation error");
470 moonbr_terminate_error();
471 }
472 pool->listener_count = listener_count;
473 {
474 /* initialization of listeners */
475 int i;
476 for (i=0; i<listener_count; i++) {
477 struct moonbr_listener *listener = &pool->listener[i];
478 listener->pool = pool;
479 listener->listenfd = -1;
480 listener->pollidx = -1;
481 }
482 }
483 return pool;
484 }
486 /* Destroys a 'struct moonbr_pool' structure before it has been started */
487 static void moonbr_destroy_pool(struct moonbr_pool *pool) {
488 int i;
489 for (i=0; i<pool->listener_count; i++) {
490 struct moonbr_listener *listener = &pool->listener[i];
491 if (
492 listener->proto == MOONBR_PROTO_INTERVAL &&
493 listener->proto_specific.interval.name
494 ) {
495 free(listener->proto_specific.interval.name);
496 }
497 if (
498 listener->proto == MOONBR_PROTO_LOCAL &&
499 listener->proto_specific.local.path
500 ) {
501 free(listener->proto_specific.local.path);
502 }
503 }
504 free(pool);
505 }
507 /* Starts a all listeners in a pool */
508 static int moonbr_start_pool(struct moonbr_pool *pool) {
509 moonbr_log(LOG_INFO, "Creating pool", pool->poolnum);
510 {
511 int i;
512 for (i=0; i<pool->listener_count; i++) {
513 struct moonbr_listener *listener = &pool->listener[i];
514 switch (listener->proto) {
515 case MOONBR_PROTO_INTERVAL:
516 /* nothing to do here: starting intervals is performed in moonbr_run() function */
517 if (!listener->proto_specific.interval.name) {
518 moonbr_log(LOG_INFO, "Adding unnamed interval listener");
519 } else {
520 moonbr_log(LOG_INFO, "Adding interval listener \"%s\"", listener->proto_specific.interval.name);
521 }
522 break;
523 case MOONBR_PROTO_LOCAL:
524 moonbr_log(LOG_INFO, "Adding local socket listener for path \"%s\"", listener->proto_specific.local.path);
525 {
526 struct sockaddr_un servaddr = { .sun_family = AF_UNIX };
527 const int path_maxlen = sizeof(struct sockaddr_un) - (
528 (void *)servaddr.sun_path - (void *)&servaddr
529 );
530 if (
531 snprintf(
532 servaddr.sun_path,
533 path_maxlen,
534 "%s",
535 listener->proto_specific.local.path
536 ) >= path_maxlen
537 ) {
538 errno = ENAMETOOLONG;
539 };
540 listener->listenfd = socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
541 if (listener->listenfd == -1) goto moonbr_start_pool_error;
542 if (!unlink(listener->proto_specific.local.path)) {
543 moonbr_log(LOG_WARNING, "Unlinked named socket \"%s\" prior to listening", listener->proto_specific.local.path);
544 } else {
545 if (errno != ENOENT) {
546 moonbr_log(LOG_ERR, "Could not unlink named socket \"%s\" prior to listening: %s", listener->proto_specific.local.path, strerror(errno));
547 }
548 }
549 if (
550 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
551 ) goto moonbr_start_pool_error;
552 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
553 }
554 break;
555 case MOONBR_PROTO_TCP6:
556 if (listener->proto_specific.tcp.localhost_only) {
557 moonbr_log(LOG_INFO, "Adding localhost TCP/IPv6 listener on port %i", listener->proto_specific.tcp.port);
558 } else {
559 moonbr_log(LOG_INFO, "Adding public TCP/IPv6 listener on port %i", listener->proto_specific.tcp.port);
560 }
561 {
562 struct sockaddr_in6 servaddr = {
563 .sin6_family = AF_INET6,
564 .sin6_port = htons(listener->proto_specific.tcp.port)
565 };
566 listener->listenfd = socket(PF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
567 if (listener->listenfd == -1) goto moonbr_start_pool_error;
568 {
569 /* avoid "Address already in use" error when restarting service */
570 static const int reuseval = 1;
571 if (setsockopt(
572 listener->listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval)
573 )) goto moonbr_start_pool_error;
574 }
575 {
576 /* default to send TCP RST when process terminates unexpectedly */
577 static const struct linger lingerval = {
578 .l_onoff = 1,
579 .l_linger = 0
580 };
581 if (setsockopt(
582 listener->listenfd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval)
583 )) goto moonbr_start_pool_error;
584 }
585 if (listener->proto_specific.tcp.localhost_only) {
586 servaddr.sin6_addr.s6_addr[15] = 1;
587 }
588 if (
589 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
590 ) goto moonbr_start_pool_error;
591 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
592 }
593 break;
594 case MOONBR_PROTO_TCP4:
595 if (listener->proto_specific.tcp.localhost_only) {
596 moonbr_log(LOG_INFO, "Adding localhost TCP/IPv4 listener on port %i", listener->proto_specific.tcp.port);
597 } else {
598 moonbr_log(LOG_INFO, "Adding public TCP/IPv4 listener on port %i", listener->proto_specific.tcp.port);
599 }
600 {
601 struct sockaddr_in servaddr = {
602 .sin_family = AF_INET,
603 .sin_port = htons(listener->proto_specific.tcp.port)
604 };
605 listener->listenfd = socket(PF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
606 if (listener->listenfd == -1) goto moonbr_start_pool_error;
607 {
608 /* avoid "Address already in use" error when restarting service */
609 static const int reuseval = 1;
610 if (setsockopt(
611 listener->listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval)
612 )) goto moonbr_start_pool_error;
613 }
614 {
615 /* default to send TCP RST when process terminates unexpectedly */
616 static const struct linger lingerval = {
617 .l_onoff = 1,
618 .l_linger = 0
619 };
620 if (setsockopt(
621 listener->listenfd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval)
622 )) goto moonbr_start_pool_error;
623 }
624 if (listener->proto_specific.tcp.localhost_only) {
625 ((uint8_t *)&servaddr.sin_addr.s_addr)[0] = 127;
626 ((uint8_t *)&servaddr.sin_addr.s_addr)[3] = 1;
627 }
628 if (
629 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
630 ) goto moonbr_start_pool_error;
631 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
632 }
633 break;
634 default:
635 moonbr_log(LOG_CRIT, "Internal error (should not happen): Unexpected value in listener.proto field");
636 moonbr_terminate_error();
637 }
638 }
639 goto moonbr_start_pool_ok;
640 moonbr_start_pool_error:
641 {
642 int j = i;
643 int errno2 = errno;
644 for (; i>=0; i--) {
645 struct moonbr_listener *listener = &pool->listener[i];
646 if (listener->listenfd != -1) close(listener->listenfd);
647 }
648 errno = errno2;
649 return j;
650 }
651 }
652 moonbr_start_pool_ok:
653 pool->poolnum = ++moonbr_pool_count;
654 moonbr_log(LOG_INFO, "Pool #%i created", pool->poolnum);
655 if (moonbr_last_pool) moonbr_last_pool->next_pool = pool;
656 else moonbr_first_pool = pool;
657 moonbr_last_pool = pool;
658 return -1;
659 }
662 /*** Function to send data and a file descriptor to child process */
664 /* Sends control message of one bye plus optional file descriptor plus optional pointer to child process */
665 static void moonbr_send_control_message(struct moonbr_worker *worker, char status, int fd, void *ptr) {
666 {
667 struct iovec iovector = { .iov_base = &status, .iov_len = 1 }; /* carrying status byte */
668 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to transfer file descriptor */
669 struct msghdr message = { .msg_iov = &iovector, .msg_iovlen = 1 }; /* data structure passed to sendmsg() call */
670 if (moonbr_debug) {
671 if (fd == -1) {
672 moonbr_log(LOG_DEBUG, "Sending control message \"%c\" to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
673 } else {
674 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);
675 }
676 }
677 if (fd != -1) {
678 /* attach control message with file descriptor */
679 message.msg_control = control_message_buffer;
680 message.msg_controllen = CMSG_SPACE(sizeof(int));
681 {
682 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
683 control_message->cmsg_level = SOL_SOCKET;
684 control_message->cmsg_type = SCM_RIGHTS;
685 control_message->cmsg_len = CMSG_LEN(sizeof(int));
686 memcpy(CMSG_DATA(control_message), &fd, sizeof(int));
687 }
688 }
689 while (sendmsg(worker->controlfd, &message, MSG_NOSIGNAL) < 0) {
690 if (errno == EPIPE) {
691 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));
692 return; /* do not close socket; socket is closed when reading from it */
693 }
694 if (errno != EINTR) {
695 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));
696 moonbr_terminate_error();
697 }
698 }
699 }
700 if (ptr) {
701 char buf[sizeof(void *)];
702 char *pos = buf;
703 int len = sizeof(void *);
704 ssize_t written;
705 if (moonbr_debug) {
706 moonbr_log(LOG_DEBUG, "Sending memory pointer to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
707 }
708 memcpy(buf, &ptr, sizeof(void *));
709 while (len) {
710 written = send(worker->controlfd, pos, len, MSG_NOSIGNAL);
711 if (written > 0) {
712 pos += written;
713 len -= written;
714 } else if (errno == EPIPE) {
715 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));
716 return; /* do not close socket; socket is closed when reading from it */
717 } else if (errno != EINTR) {
718 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));
719 moonbr_terminate_error();
720 }
721 }
722 }
723 }
726 /*** Functions running in child process ***/
728 /* Logs an error in child process */
729 static void moonbr_child_log(const char *message) {
730 fprintf(stderr, "%s\n", message);
731 }
733 /* Logs a fatal error in child process and terminates process with error status */
734 static void moonbr_child_log_fatal(const char *message) {
735 moonbr_child_log(message);
736 exit(1);
737 }
739 /* Logs an error in child process while appending error string for global errno variable */
740 static void moonbr_child_log_errno(const char *message) {
741 char errmsg[MOONBR_MAXSTRERRORLEN];
742 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
743 fprintf(stderr, "%s: %s\n", message, errmsg);
744 }
746 /* Logs a fatal error in child process while appending error string for errno and terminating process */
747 static void moonbr_child_log_errno_fatal(const char *message) {
748 moonbr_child_log_errno(message);
749 exit(1);
750 }
752 /* Receives a control message consisting of one character plus an optional file descriptor from parent process */
753 static void moonbr_child_receive_control_message(int socketfd, char *status, int *fd) {
754 struct iovec iovector = { .iov_base = status, .iov_len = 1 }; /* reference to status byte variable */
755 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to receive file descriptor */
756 struct msghdr message = { /* data structure passed to recvmsg() call */
757 .msg_iov = &iovector,
758 .msg_iovlen = 1,
759 .msg_control = control_message_buffer,
760 .msg_controllen = CMSG_SPACE(sizeof(int))
761 };
762 {
763 int received;
764 while ((received = recvmsg(socketfd, &message, MSG_CMSG_CLOEXEC)) < 0) {
765 if (errno != EINTR) {
766 moonbr_child_log_errno_fatal("Error while trying to receive connection socket from parent process");
767 }
768 }
769 if (!received) {
770 moonbr_child_log_fatal("Unexpected EOF while trying to receive connection socket from parent process");
771 }
772 }
773 {
774 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
775 if (control_message) {
776 if (control_message->cmsg_level != SOL_SOCKET) {
777 moonbr_child_log_fatal("Received control message with cmsg_level not equal to SOL_SOCKET");
778 }
779 if (control_message->cmsg_type != SCM_RIGHTS) {
780 moonbr_child_log_fatal("Received control message with cmsg_type not equal to SCM_RIGHTS");
781 }
782 memcpy(fd, CMSG_DATA(control_message), sizeof(int));
783 } else {
784 *fd = -1;
785 }
786 }
787 }
789 /* Receives a pointer from parent process */
790 static void *moonbr_child_receive_pointer(int socketfd) {
791 char buf[sizeof(void *)];
792 char *pos = buf;
793 int len = sizeof(void *);
794 ssize_t bytes_read;
795 while (len) {
796 bytes_read = recv(socketfd, pos, len, 0);
797 if (bytes_read > 0) {
798 pos += bytes_read;
799 len -= bytes_read;
800 } else if (!bytes_read) {
801 moonbr_child_log_fatal("Unexpected EOF while trying to receive memory pointer from parent process");
802 } else if (errno != EINTR) {
803 moonbr_child_log_errno_fatal("Error while trying to receive memory pointer from parent process");
804 }
805 }
806 {
807 void *ptr; /* avoid breaking strict-aliasing rules */
808 memcpy(&ptr, buf, sizeof(void *));
809 return ptr;
810 }
811 }
813 /* Main function of child process to be called after fork() and file descriptor rearrangement */
814 void moonbr_child_run(struct moonbr_pool *pool, lua_State *L) {
815 char controlmsg;
816 int fd;
817 struct itimerval notimer = { { 0, }, { 0, } };
818 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
819 if (lua_isnil(L, -1)) lua_pop(L, 1);
820 else if (lua_pcall(L, 0, 0, 1)) {
821 fprintf(stderr, "Error in \"prepare\" function: %s\n", lua_tostring(L, -1));
822 exit(1);
823 }
824 while (1) {
825 struct moonbr_listener *listener;
826 if (setitimer(ITIMER_REAL, &notimer, NULL)) {
827 moonbr_child_log_errno_fatal("Could not reset ITIMER_REAL via setitimer()");
828 }
829 controlmsg = MOONBR_STATUS_IDLE;
830 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
831 moonbr_child_log_errno_fatal("Error while sending ready message to parent process");
832 }
833 moonbr_child_receive_control_message(MOONBR_FD_CONTROL, &controlmsg, &fd);
834 if (!(
835 (controlmsg == MOONBR_COMMAND_TERMINATE && fd == -1) ||
836 (controlmsg == MOONBR_SOCKETTYPE_INTERVAL && fd == -1) ||
837 (controlmsg == MOONBR_SOCKETTYPE_LOCAL && fd != -1) ||
838 (controlmsg == MOONBR_SOCKETTYPE_NETWORK && fd != -1)
839 )) {
840 moonbr_child_log_fatal("Received illegal control message from parent process");
841 }
842 if (controlmsg == MOONBR_COMMAND_TERMINATE) break;
843 listener = moonbr_child_receive_pointer(MOONBR_FD_CONTROL);
844 if (fd) moonbr_io_pushhandle(L, fd);
845 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
846 if (!fd) {
847 lua_newtable(L);
848 lua_pushstring(L,
849 listener->proto_specific.interval.name ?
850 listener->proto_specific.interval.name : ""
851 );
852 lua_setfield(L, -2, "interval");
853 } else {
854 lua_pushvalue(L, -2);
855 }
856 if (lua_pcall(L, 1, 1, 1)) {
857 fprintf(stderr, "Error in \"connect\" function: %s\n", lua_tostring(L, -1));
858 exit(1);
859 }
860 if (fd) moonbr_io_closehandle(L, -2, 0); /* attemt clean close */
861 if (lua_type(L, -1) != LUA_TBOOLEAN || !lua_toboolean(L, -1)) break;
862 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
863 lua_settop(L, 2);
864 #else
865 lua_settop(L, 1);
866 #endif
867 }
868 controlmsg = MOONBR_STATUS_GOODBYE;
869 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
870 moonbr_child_log_errno_fatal("Error while sending goodbye message to parent process");
871 }
872 if (close(MOONBR_FD_CONTROL) && errno != EINTR) {
873 moonbr_child_log_errno("Error while closing control socket");
874 }
875 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
876 if (lua_isnil(L, -1)) lua_pop(L, 1);
877 else if (lua_pcall(L, 0, 0, 1)) {
878 fprintf(stderr, "Error in \"finish\" function: %s\n", lua_tostring(L, -1));
879 exit(1);
880 }
881 lua_close(L);
882 exit(0);
883 }
886 /*** Functions to spawn child process ***/
888 /* Helper function to send an error message to a file descriptor (not needing a file stream) */
889 static void moonbr_child_emergency_print(int fd, char *message) {
890 size_t len = strlen(message);
891 ssize_t written;
892 while (len) {
893 written = write(fd, message, len);
894 if (written > 0) {
895 message += written;
896 len -= written;
897 } else {
898 if (written != -1 || errno != EINTR) break;
899 }
900 }
901 }
903 /* Helper function to send an error message plus a text for errno to a file descriptor and terminate the process */
904 static void moonbr_child_emergency_error(int fd, char *message) {
905 int errno2 = errno;
906 moonbr_child_emergency_print(fd, message);
907 moonbr_child_emergency_print(fd, ": ");
908 moonbr_child_emergency_print(fd, strerror(errno2));
909 moonbr_child_emergency_print(fd, "\n");
910 exit(1);
911 }
913 /* Creates a child process and (in case of success) registers it in the 'struct moonbr_pool' structure */
914 static int moonbr_create_worker(struct moonbr_pool *pool, lua_State *L) {
915 struct moonbr_worker *worker;
916 worker = calloc(1, sizeof(struct moonbr_worker));
917 if (!worker) {
918 moonbr_log(LOG_CRIT, "Memory allocation error");
919 return -1;
920 }
921 worker->pool = pool;
922 {
923 int controlfds[2];
924 int errorfds[2];
925 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, controlfds)) {
926 moonbr_log(LOG_ERR, "Could not create control socket pair for communcation with child process: %s", strerror(errno));
927 free(worker);
928 return -1;
929 }
930 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, errorfds)) {
931 moonbr_log(LOG_ERR, "Could not create socket pair to redirect stderr of child process: %s", strerror(errno));
932 close(controlfds[0]);
933 close(controlfds[1]);
934 free(worker);
935 return -1;
936 }
937 if (moonbr_logfile && fflush(moonbr_logfile)) {
938 moonbr_log(LOG_CRIT, "Could not flush log file prior to forking: %s", strerror(errno));
939 moonbr_terminate_error();
940 }
941 worker->pid = fork();
942 if (worker->pid == -1) {
943 moonbr_log(LOG_ERR, "Could not fork: %s", strerror(errno));
944 close(controlfds[0]);
945 close(controlfds[1]);
946 close(errorfds[0]);
947 close(errorfds[1]);
948 free(worker);
949 return -1;
950 } else if (!worker->pid) {
951 moonbr_pstate = MOONBR_PSTATE_FORKED;
952 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
953 lua_pushliteral(L, "Failed to pass error message due to bug in Lua panic handler (hint: not enough memory?)");
954 #endif
955 moonbr_memory_limit = pool->memory_limit;
956 if (moonbr_pidfh && pidfile_close(moonbr_pidfh)) {
957 moonbr_child_emergency_error(errorfds[1], "Could not close PID file in forked child process");
958 }
959 if (moonbr_logfile && moonbr_logfile != stderr && fclose(moonbr_logfile)) {
960 moonbr_child_emergency_error(errorfds[1], "Could not close log file in forked child process");
961 }
962 if (dup2(errorfds[1], MOONBR_FD_STDERR) == -1) {
963 moonbr_child_emergency_error(errorfds[1], "Could not duplicate socket to stderr file descriptor");
964 }
965 if (dup2(controlfds[1], MOONBR_FD_CONTROL) == -1) {
966 moonbr_child_emergency_error(errorfds[1], "Could not duplicate control socket");
967 }
968 closefrom(MOONBR_FD_END);
969 moonbr_child_run(pool, L);
970 }
971 if (moonbr_stat) {
972 moonbr_log(LOG_INFO, "Created new worker in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
973 }
974 worker->controlfd = controlfds[0];
975 worker->errorfd = errorfds[0];
976 if (close(controlfds[1]) && errno != EINTR) {
977 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
978 moonbr_terminate_error();
979 }
980 if (close(errorfds[1]) && errno != EINTR) {
981 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
982 moonbr_terminate_error();
983 }
984 }
985 worker->prev_worker = pool->last_worker;
986 if (worker->prev_worker) worker->prev_worker->next_worker = worker;
987 else pool->first_worker = worker;
988 pool->last_worker = worker;
989 pool->unassigned_worker_count++;
990 pool->total_worker_count++;
991 pool->worker_count_stat = 1;
992 moonbr_poll_refresh_needed = 1;
993 return 0; /* return zero only in case of success */
994 }
997 /*** Functions to handle previously created 'struct moonbr_worker' structures ***/
999 #define moonbr_try_destroy_worker_stat(str, field) \
1000 moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: " str " %li", worker->pool->poolnum, (int)worker->pid, (long)childusage.field);
1002 /* Destroys a worker structure if socket connections have been closed and child process has terminated */
1003 static int moonbr_try_destroy_worker(struct moonbr_worker *worker) {
1004 if (worker->controlfd != -1 || worker->errorfd != -1) return MOONBR_DESTROY_NONE;
1006 int childstatus;
1007 struct rusage childusage;
1009 pid_t waitedpid;
1010 while (
1011 (waitedpid = wait4(worker->pid, &childstatus, WNOHANG, &childusage)) == -1
1012 ) {
1013 if (errno != EINTR) {
1014 moonbr_log(LOG_CRIT, "Error in wait4() call: %s", strerror(errno));
1015 moonbr_terminate_error();
1018 if (!waitedpid) return 0; /* return 0 if worker couldn't be destroyed */
1019 if (waitedpid != worker->pid) {
1020 moonbr_log(LOG_CRIT, "Wrong PID returned by wait4() call");
1021 moonbr_terminate_error();
1024 if (WIFEXITED(childstatus)) {
1025 if (WEXITSTATUS(childstatus) || moonbr_stat) {
1026 moonbr_log(
1027 WEXITSTATUS(childstatus) ? LOG_WARNING : LOG_INFO,
1028 "Child process in pool #%i with PID %i returned with exit code %i", worker->pool->poolnum, (int)worker->pid, WEXITSTATUS(childstatus)
1029 );
1031 } else if (WIFSIGNALED(childstatus)) {
1032 if (WCOREDUMP(childstatus)) {
1033 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));
1034 } else if (WTERMSIG(childstatus) == SIGALRM) {
1035 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i exited prematurely due to timeout", worker->pool->poolnum, (int)worker->pid);
1036 } else {
1037 moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
1039 } else {
1040 moonbr_log(LOG_CRIT, "Illegal exit status from child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1041 moonbr_terminate_error();
1043 if (moonbr_stat) {
1044 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));
1045 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));
1046 moonbr_try_destroy_worker_stat("max resident set size", ru_maxrss);
1047 moonbr_try_destroy_worker_stat("integral shared memory size", ru_ixrss);
1048 moonbr_try_destroy_worker_stat("integral unshared data", ru_idrss);
1049 moonbr_try_destroy_worker_stat("integral unshared stack", ru_isrss);
1050 moonbr_try_destroy_worker_stat("page replaims", ru_minflt);
1051 moonbr_try_destroy_worker_stat("page faults", ru_majflt);
1052 moonbr_try_destroy_worker_stat("swaps", ru_nswap);
1053 moonbr_try_destroy_worker_stat("block input operations", ru_inblock);
1054 moonbr_try_destroy_worker_stat("block output operations", ru_oublock);
1055 moonbr_try_destroy_worker_stat("messages sent", ru_msgsnd);
1056 moonbr_try_destroy_worker_stat("messages received", ru_msgrcv);
1057 moonbr_try_destroy_worker_stat("signals received", ru_nsignals);
1058 moonbr_try_destroy_worker_stat("voluntary context switches", ru_nvcsw);
1059 moonbr_try_destroy_worker_stat("involuntary context switches", ru_nivcsw);
1063 int retval = (
1064 (worker->idle || worker->assigned) ?
1065 MOONBR_DESTROY_IDLE_OR_ASSIGNED :
1066 MOONBR_DESTROY_PREPARE
1067 );
1068 if (worker->prev_worker) worker->prev_worker->next_worker = worker->next_worker;
1069 else worker->pool->first_worker = worker->next_worker;
1070 if (worker->next_worker) worker->next_worker->prev_worker = worker->prev_worker;
1071 else worker->pool->last_worker = worker->prev_worker;
1072 if (worker->idle) {
1073 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker->next_idle_worker;
1074 else worker->pool->first_idle_worker = worker->next_idle_worker;
1075 if (worker->next_idle_worker) worker->next_idle_worker->prev_idle_worker = worker->prev_idle_worker;
1076 else worker->pool->last_idle_worker = worker->prev_idle_worker;
1077 worker->pool->idle_worker_count--;
1079 if (!worker->assigned) worker->pool->unassigned_worker_count--;
1080 worker->pool->total_worker_count--;
1081 worker->pool->worker_count_stat = 1;
1082 if (worker->errorlinebuf) free(worker->errorlinebuf);
1083 free(worker);
1084 return retval;
1088 /* Marks a worker as idle and stores it in a queue, optionally setting 'idle_expiration' value */
1089 static void moonbr_add_idle_worker(struct moonbr_worker *worker) {
1090 worker->prev_idle_worker = worker->pool->last_idle_worker;
1091 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker;
1092 else worker->pool->first_idle_worker = worker;
1093 worker->pool->last_idle_worker = worker;
1094 worker->idle = 1;
1095 worker->pool->idle_worker_count++;
1096 if (worker->assigned) {
1097 worker->assigned = 0;
1098 worker->pool->unassigned_worker_count++;
1100 worker->pool->worker_count_stat = 1;
1101 if (timerisset(&worker->pool->idle_timeout)) {
1102 struct timeval now;
1103 moonbr_now(&now);
1104 timeradd(&now, &worker->pool->idle_timeout, &worker->idle_expiration);
1108 /* Pops a worker from the queue of idle workers (idle queue must not be empty) */
1109 static struct moonbr_worker *moonbr_pop_idle_worker(struct moonbr_pool *pool) {
1110 struct moonbr_worker *worker;
1111 worker = pool->first_idle_worker;
1112 pool->first_idle_worker = worker->next_idle_worker;
1113 if (pool->first_idle_worker) pool->first_idle_worker->prev_idle_worker = NULL;
1114 else pool->last_idle_worker = NULL;
1115 worker->next_idle_worker = NULL;
1116 worker->idle = 0;
1117 worker->pool->idle_worker_count--;
1118 worker->assigned = 1;
1119 worker->pool->unassigned_worker_count--;
1120 worker->pool->worker_count_stat = 1;
1121 return worker;
1125 /*** Functions for queues of 'struct moonbr_listener' ***/
1127 /* Appends a 'struct moonbr_listener' to the queue of idle listeners and registers it for poll() */
1128 static void moonbr_add_idle_listener(struct moonbr_listener *listener) {
1129 listener->prev_listener = listener->pool->last_idle_listener;
1130 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
1131 else listener->pool->first_idle_listener = listener;
1132 listener->pool->last_idle_listener = listener;
1133 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events |= POLLIN;
1136 /* Removes a 'struct moonbr_listener' from the queue of idle listeners and unregisters it from poll() */
1137 static void moonbr_remove_idle_listener(struct moonbr_listener *listener) {
1138 if (listener->prev_listener) listener->prev_listener->next_listener = listener->next_listener;
1139 else listener->pool->first_idle_listener = listener->next_listener;
1140 if (listener->next_listener) listener->next_listener->prev_listener = listener->prev_listener;
1141 else listener->pool->last_idle_listener = listener->prev_listener;
1142 listener->prev_listener = NULL;
1143 listener->next_listener = NULL;
1144 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events &= ~POLLIN;
1147 /* Adds a listener to the queue of connected listeners (i.e. waiting to have their incoming connection accepted) */
1148 static void moonbr_add_connected_listener(struct moonbr_listener *listener) {
1149 listener->prev_listener = listener->pool->last_connected_listener;
1150 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
1151 else listener->pool->first_connected_listener = listener;
1152 listener->pool->last_connected_listener = listener;
1155 /* Removes and returns the first connected listener in the queue */
1156 static struct moonbr_listener *moonbr_pop_connected_listener(struct moonbr_pool *pool) {
1157 struct moonbr_listener *listener = pool->first_connected_listener;
1158 listener->pool->first_connected_listener = listener->next_listener;
1159 if (listener->pool->first_connected_listener) listener->pool->first_connected_listener->prev_listener = NULL;
1160 else listener->pool->last_connected_listener = NULL;
1161 listener->next_listener = NULL;
1162 return listener;
1166 /*** Functions to handle polling ***/
1168 /* Returns an index to a new initialized entry in moonbr_poll_fds[] */
1169 int moonbr_poll_fds_nextindex() {
1170 if (moonbr_poll_fds_count >= moonbr_poll_fds_bufsize) {
1171 if (moonbr_poll_fds_bufsize) moonbr_poll_fds_bufsize *= 2;
1172 else moonbr_poll_fds_bufsize = 1;
1173 moonbr_poll_fds = realloc(
1174 moonbr_poll_fds, moonbr_poll_fds_bufsize * sizeof(struct pollfd)
1175 );
1176 if (!moonbr_poll_fds) {
1177 moonbr_log(LOG_CRIT, "Memory allocation error");
1178 moonbr_terminate_error();
1181 moonbr_poll_fds[moonbr_poll_fds_count] = (struct pollfd){0, };
1182 return moonbr_poll_fds_count++;
1185 /* Returns an index to a new initialized entry in moonbr_poll_workers[] */
1186 int moonbr_poll_workers_nextindex() {
1187 if (moonbr_poll_worker_count >= moonbr_poll_workers_bufsize) {
1188 if (moonbr_poll_workers_bufsize) moonbr_poll_workers_bufsize *= 2;
1189 else moonbr_poll_workers_bufsize = 1;
1190 moonbr_poll_workers = realloc(
1191 moonbr_poll_workers, moonbr_poll_workers_bufsize * sizeof(struct moonbr_poll_worker)
1192 );
1193 if (!moonbr_poll_workers) {
1194 moonbr_log(LOG_CRIT, "Memory allocation error");
1195 moonbr_terminate_error();
1198 moonbr_poll_workers[moonbr_poll_worker_count] = (struct moonbr_poll_worker){0, };
1199 return moonbr_poll_worker_count++;
1202 /* Queues all listeners as idle, and initializes static part of moonbr_poll_fds[], which is passed to poll() */
1203 static void moonbr_poll_init() {
1204 if (socketpair(
1205 PF_LOCAL,
1206 SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1207 0,
1208 moonbr_poll_signalfds
1209 )) {
1210 moonbr_log(LOG_CRIT, "Could not create socket pair for signal delivery during polling: %s", strerror(errno));
1211 moonbr_terminate_error();
1214 int j = moonbr_poll_fds_nextindex();
1215 struct pollfd *pollfd = &moonbr_poll_fds[j];
1216 pollfd->fd = moonbr_poll_signalfd_read;
1217 pollfd->events = POLLIN;
1220 struct moonbr_pool *pool;
1221 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1222 int i;
1223 for (i=0; i<pool->listener_count; i++) {
1224 struct moonbr_listener *listener = &pool->listener[i];
1225 if (listener->listenfd != -1) {
1226 int j = moonbr_poll_fds_nextindex();
1227 listener->pollidx = j;
1228 moonbr_poll_fds[j].fd = listener->listenfd;
1230 moonbr_add_idle_listener(listener);
1234 moonbr_poll_fds_static_count = moonbr_poll_fds_count; /* remember size of static part of array */
1237 /* Disables polling of all listeners (required for clean shutdown) */
1238 static void moonbr_poll_shutdown() {
1239 int i;
1240 for (i=1; i<moonbr_poll_fds_static_count; i++) {
1241 moonbr_poll_fds[i].fd = -1;
1245 /* (Re)builds dynamic part of moonbr_poll_fds[] array, and (re)builds moonbr_poll_workers[] array */
1246 static void moonbr_poll_refresh() {
1247 moonbr_poll_refresh_needed = 0;
1248 moonbr_poll_fds_count = moonbr_poll_fds_static_count;
1249 moonbr_poll_worker_count = 0;
1251 struct moonbr_pool *pool;
1252 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1253 struct moonbr_worker *worker;
1254 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
1255 if (worker->controlfd != -1) {
1256 int j = moonbr_poll_fds_nextindex();
1257 int k = moonbr_poll_workers_nextindex();
1258 struct pollfd *pollfd = &moonbr_poll_fds[j];
1259 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1260 pollfd->fd = worker->controlfd;
1261 pollfd->events = POLLIN;
1262 poll_worker->channel = MOONBR_POLL_WORKER_CONTROLCHANNEL;
1263 poll_worker->worker = worker;
1265 if (worker->errorfd != -1) {
1266 int j = moonbr_poll_fds_nextindex();
1267 int k = moonbr_poll_workers_nextindex();
1268 struct pollfd *pollfd = &moonbr_poll_fds[j];
1269 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1270 pollfd->fd = worker->errorfd;
1271 pollfd->events = POLLIN;
1272 poll_worker->channel = MOONBR_POLL_WORKER_ERRORCHANNEL;
1273 poll_worker->worker = worker;
1280 /* resets socket and 'revents' field of moonbr_poll_fds[] for signal delivery just before poll() is called */
1281 static void moonbr_poll_reset_signal() {
1282 ssize_t readcount;
1283 char buf[1];
1284 moonbr_poll_fds[0].revents = 0;
1285 while ((readcount = read(moonbr_poll_signalfd_read, buf, 1)) < 0) {
1286 if (errno == EAGAIN) break;
1287 if (errno != EINTR) {
1288 moonbr_log(LOG_CRIT, "Error while reading from signal delivery socket: %s", strerror(errno));
1289 moonbr_terminate_error();
1292 if (!readcount) {
1293 moonbr_log(LOG_CRIT, "Unexpected EOF when reading from signal delivery socket: %s", strerror(errno));
1294 moonbr_terminate_error();
1299 /*** Shutdown initiation ***/
1301 /* Sets global variable 'moonbr_shutdown_in_progress', closes listeners, and demands worker termination */
1302 static void moonbr_initiate_shutdown() {
1303 struct moonbr_pool *pool;
1304 int i;
1305 if (moonbr_shutdown_in_progress) {
1306 moonbr_log(LOG_NOTICE, "Shutdown already in progress");
1307 return;
1309 moonbr_shutdown_in_progress = 1;
1310 moonbr_log(LOG_NOTICE, "Initiate shutdown");
1311 for (pool = moonbr_first_pool; pool; pool = pool->next_pool) {
1312 for (i=0; i<pool->listener_count; i++) {
1313 struct moonbr_listener *listener = &pool->listener[i];
1314 if (listener->listenfd != -1) {
1315 if (close(listener->listenfd) && errno != EINTR) {
1316 moonbr_log(LOG_CRIT, "Could not close listening socket: %s", strerror(errno));
1317 moonbr_terminate_error();
1321 pool->pre_fork = 0;
1322 pool->min_fork = 0;
1323 pool->max_fork = 0;
1324 timerclear(&pool->exit_delay);
1326 moonbr_poll_shutdown(); /* avoids loops due to error condition when polling closed listeners */
1330 /*** Functions to communicate with child processes ***/
1332 /* Tells child process to terminate */
1333 static void moonbr_terminate_idle_worker(struct moonbr_worker *worker) {
1334 moonbr_send_control_message(worker, MOONBR_COMMAND_TERMINATE, -1, NULL);
1337 /* Handles status messages from child process */
1338 static void moonbr_read_controlchannel(struct moonbr_worker *worker) {
1339 char controlmsg;
1341 ssize_t bytes_read;
1342 while ((bytes_read = read(worker->controlfd, &controlmsg, 1)) <= 0) {
1343 if (bytes_read == 0 || errno == ECONNRESET) {
1344 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i unexpectedly closed control socket", worker->pool->poolnum, (int)worker->pid);
1345 if (close(worker->controlfd) && errno != EINTR) {
1346 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));
1347 moonbr_terminate_error();
1349 worker->controlfd = -1;
1350 moonbr_poll_refresh_needed = 1;
1351 return;
1353 if (errno != EINTR) {
1354 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));
1355 moonbr_terminate_error();
1359 if (worker->idle) {
1360 moonbr_log(LOG_CRIT, "Unexpected data from supposedly idle child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1361 moonbr_terminate_error();
1363 if (moonbr_debug) {
1364 moonbr_log(LOG_DEBUG, "Received control message from child in pool #%i with PID %i: \"%c\"", worker->pool->poolnum, (int)worker->pid, (int)controlmsg);
1366 switch (controlmsg) {
1367 case MOONBR_STATUS_IDLE:
1368 if (moonbr_stat) {
1369 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i reports as idle", worker->pool->poolnum, (int)worker->pid);
1371 moonbr_add_idle_worker(worker);
1372 break;
1373 case MOONBR_STATUS_GOODBYE:
1374 if (moonbr_stat) {
1375 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i announced termination", worker->pool->poolnum, (int)worker->pid);
1377 if (close(worker->controlfd) && errno != EINTR) {
1378 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));
1379 moonbr_terminate_error();
1381 worker->controlfd = -1;
1382 moonbr_poll_refresh_needed = 1;
1383 break;
1384 default:
1385 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);
1386 moonbr_terminate_error();
1390 /* Handles stderr stream from child process */
1391 static void moonbr_read_errorchannel(struct moonbr_worker *worker) {
1392 char staticbuf[MOONBR_MAXERRORLINELEN+1];
1393 char *buf = worker->errorlinebuf;
1394 if (!buf) buf = staticbuf;
1396 ssize_t bytes_read;
1397 while (
1398 (bytes_read = read(
1399 worker->errorfd,
1400 buf + worker->errorlinelen,
1401 MOONBR_MAXERRORLINELEN+1 - worker->errorlinelen
1402 )) <= 0
1403 ) {
1404 if (bytes_read == 0 || errno == ECONNRESET) {
1405 if (moonbr_debug) {
1406 moonbr_log(LOG_DEBUG, "Child process in pool #%i with PID %i closed stderr socket", worker->pool->poolnum, (int)worker->pid);
1408 if (close(worker->errorfd) && errno != EINTR) {
1409 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));
1410 moonbr_terminate_error();
1412 worker->errorfd = -1;
1413 moonbr_poll_refresh_needed = 1;
1414 break;
1416 if (errno != EINTR) {
1417 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));
1418 moonbr_terminate_error();
1421 worker->errorlinelen += bytes_read;
1424 int i;
1425 for (i=0; i<worker->errorlinelen; i++) {
1426 if (buf[i] == '\n') buf[i] = 0;
1427 if (!buf[i]) {
1428 if (worker->errorlineovf) {
1429 worker->errorlineovf = 0;
1430 } else {
1431 moonbr_log(LOG_WARNING, "Error log from process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, buf);
1433 worker->errorlinelen -= i+1;
1434 memmove(buf, buf+i+1, worker->errorlinelen);
1435 i = -1;
1438 if (i > MOONBR_MAXERRORLINELEN) {
1439 buf[MOONBR_MAXERRORLINELEN] = 0;
1440 if (!worker->errorlineovf) {
1441 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);
1443 worker->errorlinelen = 0;
1444 worker->errorlineovf = 1;
1447 if (!worker->errorlinebuf && worker->errorlinelen) { /* allocate buffer on heap only if necessary */
1448 worker->errorlinebuf = malloc((MOONBR_MAXERRORLINELEN+1) * sizeof(char));
1449 if (!worker->errorlinebuf) {
1450 moonbr_log(LOG_CRIT, "Memory allocation error");
1451 moonbr_terminate_error();
1453 memcpy(worker->errorlinebuf, staticbuf, worker->errorlinelen);
1458 /*** Handler for incoming connections ***/
1460 /* Accepts one or more incoming connections on listener socket and passes it to worker(s) popped from idle queue */
1461 static void moonbr_connect(struct moonbr_pool *pool) {
1462 struct moonbr_listener *listener = moonbr_pop_connected_listener(pool);
1463 struct moonbr_worker *worker;
1464 switch (listener->proto) {
1465 case MOONBR_PROTO_INTERVAL:
1466 worker = moonbr_pop_idle_worker(pool);
1467 if (moonbr_stat) {
1468 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);
1470 worker->restart_interval_listener = listener;
1471 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_INTERVAL, -1, listener);
1472 /* do not push listener to queue of idle listeners yet */
1473 break;
1474 case MOONBR_PROTO_LOCAL:
1475 do {
1476 int peerfd;
1477 struct sockaddr_un peeraddr;
1478 socklen_t peeraddr_len = sizeof(struct sockaddr_un);
1479 peerfd = accept4(
1480 listener->listenfd,
1481 (struct sockaddr *)&peeraddr,
1482 &peeraddr_len,
1483 SOCK_CLOEXEC
1484 );
1485 if (peerfd == -1) {
1486 if (errno == EWOULDBLOCK) {
1487 break;
1488 } else if (errno == ECONNABORTED) {
1489 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"local\", path=\"%s\")", listener->proto_specific.local.path);
1490 break;
1491 } else if (errno != EINTR) {
1492 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1493 moonbr_terminate_error();
1495 } else {
1496 worker = moonbr_pop_idle_worker(pool);
1497 if (moonbr_stat) {
1498 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);
1500 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_LOCAL, peerfd, listener);
1501 if (close(peerfd) && errno != EINTR) {
1502 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1503 moonbr_terminate_error();
1506 } while (pool->first_idle_worker);
1507 moonbr_add_idle_listener(listener);
1508 break;
1509 case MOONBR_PROTO_TCP6:
1510 do {
1511 int peerfd;
1512 struct sockaddr_in6 peeraddr;
1513 socklen_t peeraddr_len = sizeof(struct sockaddr_in6);
1514 peerfd = accept4(
1515 listener->listenfd,
1516 (struct sockaddr *)&peeraddr,
1517 &peeraddr_len,
1518 SOCK_CLOEXEC
1519 );
1520 if (peerfd == -1) {
1521 if (errno == EWOULDBLOCK) {
1522 break;
1523 } else if (errno == ECONNABORTED) {
1524 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"tcp6\", port=%i)", listener->proto_specific.tcp.port);
1525 break;
1526 } else if (errno != EINTR) {
1527 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1528 moonbr_terminate_error();
1530 } else {
1531 worker = moonbr_pop_idle_worker(pool);
1532 if (moonbr_stat) {
1533 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);
1535 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_NETWORK, peerfd, listener);
1536 if (close(peerfd) && errno != EINTR) {
1537 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1538 moonbr_terminate_error();
1541 } while (pool->first_idle_worker);
1542 moonbr_add_idle_listener(listener);
1543 break;
1544 case MOONBR_PROTO_TCP4:
1545 do {
1546 int peerfd;
1547 struct sockaddr_in peeraddr;
1548 socklen_t peeraddr_len = sizeof(struct sockaddr_in);
1549 peerfd = accept4(
1550 listener->listenfd,
1551 (struct sockaddr *)&peeraddr,
1552 &peeraddr_len,
1553 SOCK_CLOEXEC
1554 );
1555 if (peerfd == -1) {
1556 if (errno == EWOULDBLOCK) {
1557 break;
1558 } else if (errno == ECONNABORTED) {
1559 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"tcp4\", port=%i)", listener->proto_specific.tcp.port);
1560 break;
1561 } else if (errno != EINTR) {
1562 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1563 moonbr_terminate_error();
1565 } else {
1566 worker = moonbr_pop_idle_worker(pool);
1567 if (moonbr_stat) {
1568 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);
1570 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_NETWORK, peerfd, listener);
1571 if (close(peerfd) && errno != EINTR) {
1572 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1573 moonbr_terminate_error();
1576 } while (pool->first_idle_worker);
1577 moonbr_add_idle_listener(listener);
1578 break;
1579 default:
1580 moonbr_log(LOG_ERR, "Internal error (should not happen): Unexpected value in listener.proto field");
1581 moonbr_terminate_error();
1586 /*** Functions to initialize and restart interval timers ***/
1588 /* Initializes all interval timers */
1589 static void moonbr_interval_initialize() {
1590 struct timeval now;
1591 struct moonbr_pool *pool;
1592 moonbr_now(&now);
1593 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1594 int i;
1595 for (i=0; i<pool->listener_count; i++) {
1596 struct moonbr_listener *listener = &pool->listener[i];
1597 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1598 timeradd(
1599 &now,
1600 &listener->proto_specific.interval.delay,
1601 &listener->proto_specific.interval.wakeup
1602 );
1608 /* If necessary, restarts interval timers and queues interval listener as idle after a worker changed status */
1609 static void moonbr_interval_restart(
1610 struct moonbr_worker *worker,
1611 struct timeval *now /* passed to synchronize with moonbr_run() function */
1612 ) {
1613 struct moonbr_listener *listener = worker->restart_interval_listener;
1614 if (listener) {
1615 moonbr_add_idle_listener(listener);
1616 worker->restart_interval_listener = NULL;
1617 if (listener->proto_specific.interval.strict) {
1618 timeradd(
1619 &listener->proto_specific.interval.wakeup,
1620 &listener->proto_specific.interval.delay,
1621 &listener->proto_specific.interval.wakeup
1622 );
1623 if (timercmp(&listener->proto_specific.interval.wakeup, now, <)) {
1624 listener->proto_specific.interval.wakeup = *now;
1626 } else {
1627 timeradd(
1628 now,
1629 &listener->proto_specific.interval.delay,
1630 &listener->proto_specific.interval.wakeup
1631 );
1637 /*** Main loop and helper functions ***/
1639 /* Stores the earliest required wakeup time in 'wait' variable */
1640 static void moonbr_calc_wait(struct timeval *wait, struct timeval *wakeup) {
1641 if (!timerisset(wait) || timercmp(wakeup, wait, <)) *wait = *wakeup;
1644 /* Main loop of Moonbridge system (including initialization of signal handlers and polling structures) */
1645 static void moonbr_run(lua_State *L) {
1646 struct timeval now;
1647 struct moonbr_pool *pool;
1648 struct moonbr_worker *worker;
1649 struct moonbr_worker *next_worker; /* needed when worker is removed during iteration of workers */
1650 struct moonbr_listener *listener;
1651 struct moonbr_listener *next_listener; /* needed when listener is removed during iteration of listeners */
1652 int i;
1653 moonbr_poll_init(); /* must be executed before moonbr_signal_init() */
1654 moonbr_signal_init();
1655 moonbr_interval_initialize();
1656 moonbr_pstate = MOONBR_PSTATE_RUNNING;
1657 while (1) {
1658 struct timeval wait = {0, }; /* point in time when premature wakeup of poll() is required */
1659 if (moonbr_cond_interrupt) {
1660 moonbr_log(LOG_WARNING, "Fast shutdown requested");
1661 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
1663 if (moonbr_cond_terminate) {
1664 moonbr_initiate_shutdown();
1665 moonbr_cond_terminate = 0;
1667 moonbr_cond_child = 0; /* must not be reset between moonbr_try_destroy_worker() and poll() */
1668 moonbr_now(&now);
1669 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1670 int terminated_worker_count = 0; /* allows shortcut for new worker creation */
1671 /* terminate idle workers when expired */
1672 if (timerisset(&pool->idle_timeout)) {
1673 while ((worker = pool->first_idle_worker) != NULL) {
1674 if (timercmp(&worker->idle_expiration, &now, >)) break;
1675 moonbr_pop_idle_worker(pool);
1676 moonbr_terminate_idle_worker(worker);
1679 /* mark listeners as connected when incoming connection is pending */
1680 for (listener=pool->first_idle_listener; listener; listener=next_listener) {
1681 next_listener = listener->next_listener; /* extra variable necessary due to changing list */
1682 if (listener->pollidx != -1) {
1683 if (moonbr_poll_fds[listener->pollidx].revents) {
1684 moonbr_poll_fds[listener->pollidx].revents = 0;
1685 moonbr_remove_idle_listener(listener);
1686 moonbr_add_connected_listener(listener);
1688 } else if (listener->proto == MOONBR_PROTO_INTERVAL) {
1689 if (!timercmp(&listener->proto_specific.interval.wakeup, &now, >)) {
1690 moonbr_remove_idle_listener(listener);
1691 moonbr_add_connected_listener(listener);
1693 } else {
1694 moonbr_log(LOG_CRIT, "Internal error (should not happen): Listener is neither an interval timer nor has the 'pollidx' value set");
1695 moonbr_terminate_error();
1698 /* process input from child processes */
1699 for (i=0; i<moonbr_poll_worker_count; i++) {
1700 if (moonbr_poll_worker_fds[i].revents) {
1701 moonbr_poll_worker_fds[i].revents = 0;
1702 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[i];
1703 switch (poll_worker->channel) {
1704 case MOONBR_POLL_WORKER_CONTROLCHANNEL:
1705 moonbr_read_controlchannel(poll_worker->worker);
1706 moonbr_interval_restart(poll_worker->worker, &now);
1707 break;
1708 case MOONBR_POLL_WORKER_ERRORCHANNEL:
1709 moonbr_read_errorchannel(poll_worker->worker);
1710 break;
1714 /* collect dead child processes */
1715 for (worker=pool->first_worker; worker; worker=next_worker) {
1716 next_worker = worker->next_worker; /* extra variable necessary due to changing list */
1717 switch (moonbr_try_destroy_worker(worker)) {
1718 case MOONBR_DESTROY_PREPARE:
1719 pool->use_fork_error_wakeup = 1;
1720 break;
1721 case MOONBR_DESTROY_IDLE_OR_ASSIGNED:
1722 terminated_worker_count++;
1723 break;
1726 /* connect listeners with idle workers */
1727 if (!moonbr_shutdown_in_progress) {
1728 while (pool->first_connected_listener && pool->first_idle_worker) {
1729 moonbr_connect(pool);
1732 /* create new worker processes */
1733 while (
1734 pool->total_worker_count < pool->max_fork && (
1735 pool->unassigned_worker_count < pool->pre_fork ||
1736 pool->total_worker_count < pool->min_fork
1738 ) {
1739 if (pool->use_fork_error_wakeup) {
1740 if (timercmp(&pool->fork_error_wakeup, &now, >)) {
1741 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
1742 break;
1744 } else {
1745 if (terminated_worker_count) {
1746 terminated_worker_count--;
1747 } else if (timercmp(&pool->fork_wakeup, &now, >)) {
1748 moonbr_calc_wait(&wait, &pool->fork_wakeup);
1749 break;
1752 if (moonbr_create_worker(pool, L)) {
1753 /* on error, enforce error delay */
1754 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
1755 pool->use_fork_error_wakeup = 1;
1756 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
1757 break;
1758 } else {
1759 /* normal fork delay on success */
1760 timeradd(&now, &pool->fork_delay, &pool->fork_wakeup);
1761 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
1762 pool->use_fork_error_wakeup = 0; /* gets set later if error occures during preparation */
1765 /* terminate excessive worker processes */
1766 while (
1767 pool->total_worker_count > pool->min_fork &&
1768 pool->idle_worker_count > pool->pre_fork
1769 ) {
1770 if (timerisset(&pool->exit_wakeup)) {
1771 if (timercmp(&pool->exit_wakeup, &now, >)) {
1772 moonbr_calc_wait(&wait, &pool->exit_wakeup);
1773 break;
1775 moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
1776 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
1777 } else {
1778 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
1779 break;
1782 if (!(
1783 pool->total_worker_count > pool->min_fork &&
1784 pool->idle_worker_count > pool->pre_fork
1785 )) {
1786 timerclear(&pool->exit_wakeup); /* timer gets restarted later when there are excessive workers */
1788 /* optionally output worker count stats */
1789 if (moonbr_stat && pool->worker_count_stat) {
1790 pool->worker_count_stat = 0;
1791 moonbr_log(
1792 LOG_INFO,
1793 "Worker count for pool #%i: %i idle, %i assigned, %i total",
1794 pool->poolnum, pool->idle_worker_count,
1795 pool->total_worker_count - pool->unassigned_worker_count,
1796 pool->total_worker_count);
1798 /* calculate wakeup time for interval listeners */
1799 for (listener=pool->first_idle_listener; listener; listener=listener->next_listener) {
1800 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1801 moonbr_calc_wait(&wait, &listener->proto_specific.interval.wakeup);
1804 /* calculate wakeup time for idle workers (only first idle worker is significant) */
1805 if (timerisset(&pool->idle_timeout) && pool->first_idle_worker) {
1806 moonbr_calc_wait(&wait, &pool->first_idle_worker->idle_expiration);
1809 /* check if shutdown is complete */
1810 if (moonbr_shutdown_in_progress) {
1811 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1812 if (pool->first_worker) break;
1814 if (!pool) {
1815 moonbr_log(LOG_INFO, "All worker threads have terminated");
1816 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
1819 if (moonbr_poll_refresh_needed) moonbr_poll_refresh();
1820 moonbr_cond_poll = 1;
1821 if (!moonbr_cond_child && !moonbr_cond_terminate && !moonbr_cond_interrupt) {
1822 int timeout;
1823 if (timerisset(&wait)) {
1824 if (timercmp(&wait, &now, <)) {
1825 moonbr_log(LOG_CRIT, "Internal error (should not happen): Future is in the past");
1826 moonbr_terminate_error();
1828 timersub(&wait, &now, &wait);
1829 timeout = wait.tv_sec * 1000 + wait.tv_usec / 1000;
1830 } else {
1831 timeout = INFTIM;
1833 if (moonbr_debug) {
1834 moonbr_log(LOG_DEBUG, "Waiting for I/O");
1836 poll(moonbr_poll_fds, moonbr_poll_fds_count, timeout);
1837 } else {
1838 if (moonbr_debug) {
1839 moonbr_log(LOG_DEBUG, "Do not wait for I/O");
1842 moonbr_cond_poll = 0;
1843 moonbr_poll_reset_signal();
1848 /*** Lua interface ***/
1850 static int moonbr_lua_panic(lua_State *L) {
1851 const char *errmsg;
1852 errmsg = lua_tostring(L, -1);
1853 if (!errmsg) {
1854 if (lua_isnoneornil(L, -1)) errmsg = "(error message is nil)";
1855 else errmsg = "(error message is not a string)";
1857 if (moonbr_pstate == MOONBR_PSTATE_FORKED) {
1858 fprintf(stderr, "Uncaught Lua error: %s\n", errmsg);
1859 exit(1);
1860 } else {
1861 moonbr_log(LOG_CRIT, "Uncaught Lua error: %s", errmsg);
1862 moonbr_terminate_error();
1864 return 0;
1867 static int moonbr_addtraceback(lua_State *L) {
1868 luaL_traceback(L, L, luaL_tolstring(L, 1, NULL), 1);
1869 return 1;
1872 /* Memory allocator that allows limiting memory consumption */
1873 static void *moonbr_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
1874 (void)ud; /* not used */
1875 if (nsize == 0) {
1876 if (ptr) {
1877 moonbr_memory_usage -= osize;
1878 free(ptr);
1880 return NULL;
1881 } else if (ptr) {
1882 if (
1883 moonbr_memory_limit &&
1884 nsize > osize &&
1885 moonbr_memory_usage + (nsize - osize) > moonbr_memory_limit
1886 ) {
1887 return NULL;
1888 } else {
1889 ptr = realloc(ptr, nsize);
1890 if (ptr) moonbr_memory_usage += nsize - osize;
1892 } else {
1893 if (
1894 moonbr_memory_limit &&
1895 moonbr_memory_usage + nsize > moonbr_memory_limit
1896 ) {
1897 return NULL;
1898 } else {
1899 ptr = realloc(ptr, nsize);
1900 if (ptr) moonbr_memory_usage += nsize;
1903 return ptr;
1906 /* New method for Lua file objects: read until terminator or length exceeded */
1907 static int moonbr_readuntil(lua_State *L) {
1908 luaL_Stream *stream;
1909 FILE *file;
1910 const char *terminatorstr;
1911 size_t terminatorlen;
1912 luaL_Buffer buf;
1913 lua_Integer maxlen;
1914 char terminator;
1915 int byte;
1916 stream = luaL_checkudata(L, 1, LUA_FILEHANDLE);
1917 terminatorstr = luaL_checklstring(L, 2, &terminatorlen);
1918 luaL_argcheck(L, terminatorlen == 1, 2, "single byte expected");
1919 maxlen = luaL_optinteger(L, 3, 0);
1920 if (!stream->closef) luaL_error(L, "attempt to use a closed file");
1921 file = stream->f;
1922 luaL_buffinit(L, &buf);
1923 if (!maxlen) maxlen = -1;
1924 terminator = terminatorstr[0];
1925 while (maxlen > 0 ? maxlen-- : maxlen) {
1926 byte = fgetc(file);
1927 if (byte == EOF) {
1928 if (ferror(file)) {
1929 char errmsg[MOONBR_MAXSTRERRORLEN];
1930 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
1931 lua_pushnil(L);
1932 lua_pushstring(L, errmsg);
1933 return 2;
1934 } else {
1935 break;
1938 luaL_addchar(&buf, byte);
1939 if (byte == terminator) break;
1941 luaL_pushresult(&buf);
1942 if (!lua_rawlen(L, -1)) lua_pushnil(L);
1943 return 1;
1946 static int moonbr_lua_tonatural(lua_State *L, int idx) {
1947 int isnum;
1948 lua_Number n;
1949 n = lua_tonumberx(L, idx, &isnum);
1950 if (isnum && n>=0 && n<INT_MAX && (lua_Number)(int)n == n) return n;
1951 else return -1;
1954 static int moonbr_lua_totimeval(lua_State *L, int idx, struct timeval *value) {
1955 int isnum;
1956 lua_Number n;
1957 n = lua_tonumberx(L, idx, &isnum);
1958 if (isnum && n>=0 && n<=100000000) {
1959 value->tv_sec = n;
1960 value->tv_usec = 1e6 * (n - value->tv_sec);
1961 return 1;
1962 } else {
1963 return 0;
1967 static int moonbr_timeout(lua_State *L) {
1968 struct itimerval oldval;
1969 if (lua_isnoneornil(L, 1) && lua_isnoneornil(L, 2)) {
1970 getitimer(ITIMER_REAL, &oldval);
1971 } else {
1972 struct itimerval newval = {};
1973 timerclear(&newval.it_interval);
1974 timerclear(&newval.it_value);
1975 if (lua_toboolean(L, 1)) {
1976 luaL_argcheck(
1977 L, moonbr_lua_totimeval(L, 1, &newval.it_value), 1,
1978 "interval in seconds expected"
1979 );
1981 if (lua_isnoneornil(L, 2)) {
1982 if (setitimer(ITIMER_REAL, &newval, &oldval)) {
1983 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1984 moonbr_terminate_error();
1986 } else {
1987 getitimer(ITIMER_REAL, &oldval);
1988 if (!timerisset(&oldval.it_value)) {
1989 if (setitimer(ITIMER_REAL, &newval, NULL)) {
1990 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1991 moonbr_terminate_error();
1993 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
1994 timerclear(&newval.it_value);
1995 if (setitimer(ITIMER_REAL, &newval, NULL)) {
1996 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1997 moonbr_terminate_error();
1999 } else if (timercmp(&newval.it_value, &oldval.it_value, <)) {
2000 struct itimerval remval;
2001 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2002 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2003 moonbr_terminate_error();
2005 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
2006 getitimer(ITIMER_REAL, &remval);
2007 timersub(&oldval.it_value, &newval.it_value, &newval.it_value);
2008 timeradd(&newval.it_value, &remval.it_value, &newval.it_value);
2009 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2010 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2011 moonbr_terminate_error();
2013 } else {
2014 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
2016 return lua_gettop(L) - 1;
2019 lua_pushnumber(L, oldval.it_value.tv_sec + 1e-6 * oldval.it_value.tv_usec);
2020 return 1;
2023 #define moonbr_listen_init_pool_forkoption(luaname, cname, defval) { \
2024 lua_getfield(L, 2, luaname); \
2025 pool->cname = lua_isnil(L, -1) ? (defval) : moonbr_lua_tonatural(L, -1); \
2026 } while(0)
2028 #define moonbr_listen_init_pool_timeoption(luaname, cname, defval, defvalu) ( \
2029 lua_getfield(L, 2, luaname), \
2030 lua_isnil(L, -1) ? ( \
2031 pool->cname.tv_sec = (defval), pool->cname.tv_usec = (defvalu), \
2032 1 \
2033 ) : ( \
2034 (lua_isboolean(L, -1) && !lua_toboolean(L, -1)) ? ( \
2035 pool->cname.tv_sec = 0, pool->cname.tv_usec = 0, \
2036 1 \
2037 ) : ( \
2038 moonbr_lua_totimeval(L, -1, &pool->cname) \
2039 ) \
2040 ) \
2043 static int moonbr_listen_init_pool(lua_State *L) {
2044 struct moonbr_pool *pool;
2045 const char *proto;
2046 int i;
2047 pool = lua_touserdata(L, 1);
2048 for (i=0; i<pool->listener_count; i++) {
2049 struct moonbr_listener *listener = &pool->listener[i];
2050 lua_settop(L, 2);
2051 #if LUA_VERSION_NUM >= 503
2052 lua_geti(L, 2, i+1);
2053 #else
2054 lua_pushinteger(L, i+1);
2055 lua_gettable(L, 2);
2056 #endif
2057 lua_getfield(L, 3, "proto");
2058 proto = lua_tostring(L, -1);
2059 if (proto && !strcmp(proto, "interval")) {
2060 listener->proto = MOONBR_PROTO_INTERVAL;
2061 lua_getfield(L, 3, "name");
2063 const char *name = lua_tostring(L, -1);
2064 if (name) {
2065 if (asprintf(&listener->proto_specific.interval.name, "%s", name) < 0) {
2066 moonbr_log(LOG_CRIT, "Memory allocation_error");
2067 moonbr_terminate_error();
2071 lua_getfield(L, 3, "delay");
2072 if (
2073 !moonbr_lua_totimeval(L, -1, &listener->proto_specific.interval.delay) ||
2074 !timerisset(&listener->proto_specific.interval.delay)
2075 ) {
2076 luaL_error(L, "No valid interval delay specified; use listen{{proto=\"interval\", delay=...}, ...}");
2078 lua_getfield(L, 3, "strict");
2079 if (!lua_isnil(L, -1)) {
2080 if (lua_isboolean(L, -1)) {
2081 if (lua_toboolean(L, -1)) listener->proto_specific.interval.strict = 1;
2082 } else {
2083 luaL_error(L, "Option \"strict\" must be a boolean if set; use listen{{proto=\"interval\", strict=true, ...}, ...}");
2086 } else if (proto && !strcmp(proto, "local")) {
2087 listener->proto = MOONBR_PROTO_LOCAL;
2088 lua_getfield(L, 3, "path");
2090 const char *path = lua_tostring(L, -1);
2091 if (!path) {
2092 luaL_error(L, "No valid path specified for local socket; use listen{{proto=\"local\", path=...}, ...}");
2094 if (asprintf(&listener->proto_specific.local.path, "%s", path) < 0) {
2095 moonbr_log(LOG_CRIT, "Memory allocation_error");
2096 moonbr_terminate_error();
2099 } else if (proto && !strcmp(proto, "tcp6")) {
2100 listener->proto = MOONBR_PROTO_TCP6;
2101 lua_getfield(L, 3, "port");
2102 listener->proto_specific.tcp.port = lua_tointeger(L, -1);
2103 if (
2104 listener->proto_specific.tcp.port < 1 ||
2105 listener->proto_specific.tcp.port > 65535
2106 ) {
2107 luaL_error(L, "No valid port number specified; use listen{{proto=\"tcp6\", port=...}, ...}");
2109 lua_getfield(L, 3, "localhost");
2110 if (!lua_isnil(L, -1)) {
2111 if (lua_isboolean(L, -1)) {
2112 if (lua_toboolean(L, -1)) listener->proto_specific.tcp.localhost_only = 1;
2113 } else {
2114 luaL_error(L, "Option \"localhost\" must be a boolean if set; use listen{{proto=\"tcp6\", localhost=true, ...}, ...}");
2117 } else if (proto && !strcmp(proto, "tcp4")) {
2118 listener->proto = MOONBR_PROTO_TCP4;
2119 lua_getfield(L, 3, "port");
2120 listener->proto_specific.tcp.port = lua_tointeger(L, -1);
2121 if (
2122 listener->proto_specific.tcp.port < 1 ||
2123 listener->proto_specific.tcp.port > 65535
2124 ) {
2125 luaL_error(L, "No valid port number specified; use listen{{proto=\"tcp4\", port=...}, ...}");
2127 lua_getfield(L, 3, "localhost");
2128 if (!lua_isnil(L, -1)) {
2129 if (lua_isboolean(L, -1)) {
2130 if (lua_toboolean(L, -1)) listener->proto_specific.tcp.localhost_only = 1;
2131 } else {
2132 luaL_error(L, "Option \"localhost\" must be a boolean if set; use listen{{proto=\"tcp4\", localhost=true, ...}, ...}");
2137 lua_settop(L, 2);
2138 moonbr_listen_init_pool_forkoption("pre_fork", pre_fork, 1);
2139 moonbr_listen_init_pool_forkoption("min_fork", min_fork, pool->pre_fork > 2 ? pool->pre_fork : 2);
2140 moonbr_listen_init_pool_forkoption("max_fork", max_fork, pool->min_fork > 16 ? pool->min_fork : 16);
2141 if (!moonbr_listen_init_pool_timeoption("fork_delay", fork_delay, 0, 250000)) {
2142 luaL_error(L, "Option \"fork_delay\" is expected to be a non-negative number");
2144 if (!moonbr_listen_init_pool_timeoption("fork_error_delay", fork_error_delay, 2, 0)) {
2145 luaL_error(L, "Option \"fork_error_delay\" is expected to be a non-negative number");
2147 if (!moonbr_listen_init_pool_timeoption("exit_delay", exit_delay, 60, 0)) {
2148 luaL_error(L, "Option \"exit_delay\" is expected to be a non-negative number");
2150 if (timercmp(&pool->fork_error_delay, &pool->fork_delay, <)) {
2151 pool->fork_error_delay = pool->fork_delay;
2153 if (!moonbr_listen_init_pool_timeoption("idle_timeout", idle_timeout, 0, 0)) {
2154 luaL_error(L, "Option \"idle_timeout\" is expected to be a non-negative number");
2156 lua_getfield(L, 2, "memory_limit");
2157 if (!lua_isnil(L, -1)) {
2158 int isnum;
2159 lua_Number n;
2160 n = lua_tonumberx(L, -1, &isnum);
2161 if (n < 0 || !isnum) {
2162 luaL_error(L, "Option \"memory_limit\" is expected to be a non-negative number");
2164 pool->memory_limit = n;
2166 lua_settop(L, 2);
2167 lua_getfield(L, 2, "prepare");
2168 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2169 luaL_error(L, "Option \"prepare\" must be nil or a function");
2171 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2172 lua_getfield(L, 2, "connect");
2173 if (!lua_isfunction(L, -1)) {
2174 luaL_error(L, "Option \"connect\" must be a function; use listen{{...}, {...}, connect=function(socket) ... end, ...}");
2176 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2177 lua_getfield(L, 2, "finish");
2178 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2179 luaL_error(L, "Option \"finish\" must be nil or a function");
2181 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2182 return 0;
2185 static int moonbr_listen(lua_State *L) {
2186 struct moonbr_pool *pool;
2187 lua_Integer listener_count;
2188 if (moonbr_booted) luaL_error(L, "Moonbridge bootup is already complete");
2189 luaL_checktype(L, 1, LUA_TTABLE);
2190 listener_count = luaL_len(L, 1);
2191 if (!listener_count) luaL_error(L, "No listen ports specified; use listen{{proto=..., port=...},...}");
2192 if (listener_count > 100) luaL_error(L, "Too many listeners");
2193 pool = moonbr_create_pool(listener_count);
2194 lua_pushcfunction(L, moonbr_listen_init_pool);
2195 lua_pushlightuserdata(L, pool);
2196 lua_pushvalue(L, 1);
2197 if (lua_pcall(L, 2, 0, 0)) goto moonbr_listen_error;
2199 int i;
2200 i = moonbr_start_pool(pool);
2201 if (i >= 0) {
2202 struct moonbr_listener *listener = &pool->listener[i];
2203 switch (listener->proto) {
2204 case MOONBR_PROTO_INTERVAL:
2205 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"interval\"): %s", i+1, strerror(errno));
2206 break;
2207 case MOONBR_PROTO_LOCAL:
2208 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"local\", path=\"%s\"): %s", i+1, listener->proto_specific.local.path, strerror(errno));
2209 break;
2210 case MOONBR_PROTO_TCP6:
2211 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"tcp6\", port=%d): %s", i+1, listener->proto_specific.tcp.port, strerror(errno));
2212 break;
2213 case MOONBR_PROTO_TCP4:
2214 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"tcp4\", port=%d): %s", i+1, listener->proto_specific.tcp.port, strerror(errno));
2215 break;
2216 default:
2217 moonbr_log(LOG_ERR, "Internal error (should not happen): Unexpected value in listener.proto field");
2218 moonbr_terminate_error();
2220 goto moonbr_listen_error;
2223 return 0;
2224 moonbr_listen_error:
2225 moonbr_destroy_pool(pool);
2226 lua_pushnil(L);
2227 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2228 lua_pushnil(L);
2229 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2230 lua_pushnil(L);
2231 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2232 lua_error(L);
2233 return 0; /* avoid compiler warning */
2237 /*** Function to modify Lua's library path and/or cpath ***/
2239 #if defined(MOONBR_LUA_PATH) || defined(MOONBR_LUA_CPATH)
2240 static void moonbr_modify_path(lua_State *L, char *key, char *value) {
2241 int stackbase;
2242 stackbase = lua_gettop(L);
2243 lua_getglobal(L, "package");
2244 lua_getfield(L, stackbase+1, key);
2246 const char *current_str;
2247 size_t current_strlen;
2248 luaL_Buffer buf;
2249 current_str = lua_tolstring(L, stackbase+2, &current_strlen);
2250 luaL_buffinit(L, &buf);
2251 if (current_str) {
2252 lua_pushvalue(L, stackbase+2);
2253 luaL_addvalue(&buf);
2254 if (current_strlen && current_str[current_strlen-1] != ';') {
2255 luaL_addchar(&buf, ';');
2258 luaL_addstring(&buf, value);
2259 luaL_pushresult(&buf);
2261 lua_setfield(L, stackbase+1, key);
2262 lua_settop(L, stackbase);
2264 #endif
2267 /*** Main function and command line invokation ***/
2269 static void moonbr_usage(int err, const char *cmd) {
2270 FILE *out;
2271 out = err ? stderr : stdout;
2272 if (!cmd) cmd = "moonbridge";
2273 fprintf(out, "Get this help message: %s {-h|--help}\n", cmd);
2274 fprintf(out, "Usage: %s \\\n", cmd);
2275 fprintf(out, " [-b|--background] \\\n");
2276 fprintf(out, " [-d|--debug] \\\n");
2277 fprintf(out, " [-f|--logfacility {DAEMON|USER|0|1|...|7}] \\\n");
2278 fprintf(out, " [-i|--logident <syslog ident> \\\n");
2279 fprintf(out, " [-l|--logfile <logfile>] \\\n");
2280 fprintf(out, " [-p|--pidfile <pidfile>] \\\n");
2281 fprintf(out, " [-s|--stats] \\\n");
2282 fprintf(out, " -- <Lua script> [<cmdline options for Lua script>]\n");
2283 exit(err);
2286 #define moonbr_usage_error() moonbr_usage(MOONBR_EXITCODE_CMDLINEERROR, argc ? argv[0] : NULL)
2288 int main(int argc, char **argv) {
2290 int daemonize = 0;
2291 int log_facility = LOG_USER;
2292 const char *log_ident = "moonbridge";
2293 const char *log_filename = NULL;
2294 const char *pid_filename = NULL;
2295 int option;
2296 struct option longopts[] = {
2297 { "background", no_argument, NULL, 'b' },
2298 { "debug", no_argument, NULL, 'd' },
2299 { "logfacility", required_argument, NULL, 'f' },
2300 { "help", no_argument, NULL, 'h' },
2301 { "logident", required_argument, NULL, 'i' },
2302 { "logfile", required_argument, NULL, 'l' },
2303 { "pidfile", required_argument, NULL, 'p' },
2304 { "stats", no_argument, NULL, 's' }
2305 };
2306 while ((option = getopt_long(argc, argv, "bdf:hi:l:p:s", longopts, NULL)) != -1) {
2307 switch (option) {
2308 case 'b':
2309 daemonize = 1;
2310 break;
2311 case 'd':
2312 moonbr_debug = 1;
2313 moonbr_stat = 1;
2314 break;
2315 case 'f':
2316 if (!strcmp(optarg, "DAEMON")) {
2317 log_facility = LOG_DAEMON;
2318 } else if (!strcmp(optarg, "USER")) {
2319 log_facility = LOG_USER;
2320 } else if (!strcmp(optarg, "0")) {
2321 log_facility = LOG_LOCAL0;
2322 } else if (!strcmp(optarg, "1")) {
2323 log_facility = LOG_LOCAL1;
2324 } else if (!strcmp(optarg, "2")) {
2325 log_facility = LOG_LOCAL2;
2326 } else if (!strcmp(optarg, "3")) {
2327 log_facility = LOG_LOCAL3;
2328 } else if (!strcmp(optarg, "4")) {
2329 log_facility = LOG_LOCAL4;
2330 } else if (!strcmp(optarg, "5")) {
2331 log_facility = LOG_LOCAL5;
2332 } else if (!strcmp(optarg, "6")) {
2333 log_facility = LOG_LOCAL6;
2334 } else if (!strcmp(optarg, "7")) {
2335 log_facility = LOG_LOCAL7;
2336 } else {
2337 moonbr_usage_error();
2339 moonbr_use_syslog = 1;
2340 break;
2341 case 'h':
2342 moonbr_usage(MOONBR_EXITCODE_GRACEFUL, argv[0]);
2343 break;
2344 case 'i':
2345 log_ident = optarg;
2346 moonbr_use_syslog = 1;
2347 break;
2348 case 'l':
2349 log_filename = optarg;
2350 break;
2351 case 'p':
2352 pid_filename = optarg;
2353 break;
2354 case 's':
2355 moonbr_stat = 1;
2356 break;
2357 default:
2358 moonbr_usage_error();
2361 if (argc - optind < 1) moonbr_usage_error();
2362 if (pid_filename) {
2363 pid_t otherpid;
2364 while ((moonbr_pidfh = pidfile_open(pid_filename, 0644, &otherpid)) == NULL) {
2365 if (errno == EEXIST) {
2366 if (otherpid == -1) {
2367 fprintf(stderr, "PID file \"%s\" is already locked\n", pid_filename);
2368 } else {
2369 fprintf(stderr, "PID file \"%s\" is already locked by process with PID: %i\n", pid_filename, (int)otherpid);
2371 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2372 } else if (errno != EINTR) {
2373 fprintf(stderr, "Could not write PID file \"%s\": %s\n", pid_filename, strerror(errno));
2374 exit(MOONBR_EXITCODE_STARTUPERROR);
2378 if (log_filename) {
2379 int logfd;
2380 while (
2381 ( logfd = flopen(
2382 log_filename,
2383 O_WRONLY|O_NONBLOCK|O_CREAT|O_APPEND|O_CLOEXEC,
2384 0640
2386 ) < 0
2387 ) {
2388 if (errno == EWOULDBLOCK) {
2389 fprintf(stderr, "Logfile \"%s\" is locked\n", log_filename);
2390 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2391 } else if (errno != EINTR) {
2392 fprintf(stderr, "Could not open logfile \"%s\": %s\n", log_filename, strerror(errno));
2393 exit(MOONBR_EXITCODE_STARTUPERROR);
2396 moonbr_logfile = fdopen(logfd, "a");
2397 if (!moonbr_logfile) {
2398 fprintf(stderr, "Could not open write stream to logfile \"%s\": %s\n", log_filename, strerror(errno));
2399 exit(MOONBR_EXITCODE_STARTUPERROR);
2402 if (daemonize == 0 && !moonbr_logfile) moonbr_logfile = stderr;
2403 if (moonbr_logfile) setlinebuf(moonbr_logfile);
2404 else moonbr_use_syslog = 1;
2405 if (moonbr_use_syslog) openlog(log_ident, LOG_NDELAY | LOG_PID, log_facility);
2406 if (daemonize) {
2407 if (daemon(1, 0)) {
2408 moonbr_log(LOG_ERR, "Could not daemonize moonbridge process");
2409 moonbr_terminate_error();
2413 moonbr_log(LOG_NOTICE, "Starting moonbridge server");
2414 if (moonbr_pidfh && pidfile_write(moonbr_pidfh)) {
2415 moonbr_log(LOG_ERR, "Could not write pidfile (after locking)");
2418 lua_State *L;
2419 L = lua_newstate(moonbr_alloc, NULL);
2420 if (!L) {
2421 moonbr_log(LOG_CRIT, "Could not initialize Lua state");
2422 moonbr_terminate_error();
2424 lua_atpanic(L, moonbr_lua_panic);
2425 lua_pushliteral(L, MOONBR_VERSION_STRING);
2426 lua_setglobal(L, "_MOONBRIDGE_VERSION");
2427 luaL_openlibs(L);
2428 luaL_requiref(L, "moonbridge_io", luaopen_moonbridge_io, 1);
2429 lua_pop(L, 1);
2430 #ifdef MOONBR_LUA_PATH
2431 moonbr_modify_path(L, "path", MOONBR_LUA_PATH);
2432 #endif
2433 #ifdef MOONBR_LUA_CPATH
2434 moonbr_modify_path(L, "cpath", MOONBR_LUA_CPATH);
2435 #endif
2436 if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
2437 moonbr_log(LOG_CRIT, "Lua metatable LUA_FILEHANDLE does not exist");
2438 moonbr_terminate_error();
2440 lua_getfield(L, -1, "__index");
2441 lua_pushcfunction(L, moonbr_readuntil);
2442 lua_setfield(L, -2, "readuntil");
2443 lua_pop(L, 2);
2444 lua_pushcfunction(L, moonbr_timeout);
2445 lua_setglobal(L, "timeout");
2446 lua_pushcfunction(L, moonbr_listen);
2447 lua_setglobal(L, "listen");
2448 lua_pushcfunction(L, moonbr_addtraceback); /* on stack position 1 */
2449 moonbr_log(LOG_INFO, "Loading \"%s\"", argv[optind]);
2450 if (luaL_loadfile(L, argv[optind])) {
2451 moonbr_log(LOG_ERR, "Error while loading \"%s\": %s", argv[optind], lua_tostring(L, -1));
2452 moonbr_terminate_error();
2454 { int i; for (i=optind+1; i<argc; i++) lua_pushstring(L, argv[i]); }
2455 if (lua_pcall(L, argc-(optind+1), 0, 1)) {
2456 moonbr_log(LOG_ERR, "Error while executing \"%s\": %s", argv[optind], lua_tostring(L, -1));
2457 moonbr_terminate_error();
2459 if (!moonbr_first_pool) {
2460 moonbr_log(LOG_WARNING, "No listener initialized.");
2461 moonbr_terminate_error();
2463 lua_getglobal(L, "listen");
2464 lua_pushcfunction(L, moonbr_listen);
2465 if (lua_compare(L, -2, -1, LUA_OPEQ)) {
2466 lua_pushnil(L);
2467 lua_setglobal(L, "listen");
2469 lua_settop(L, 1);
2470 lua_gc(L, LUA_GCCOLLECT, 0); // collect garbage before forking later
2471 moonbr_run(L);
2473 return 0;

Impressum / About Us