moonbridge

view moonbridge.c @ 124:61a2f55b3538

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

Impressum / About Us