moonbridge

view moonbridge.c @ 79:22dbb9d09f02

Stub for new non-blocking I/O library
author jbe
date Sun Apr 05 18:01:31 2015 +0200 (2015-04-05)
parents 0ec070d6f5d9
children fca51922b708
line source
2 /*** Version ***/
3 #define MOONBR_VERSION_STRING "0.4.0"
6 /*** Compile-time configuration ***/
8 #define MOONBR_LUA_PANIC_BUG_WORKAROUND 1
11 /*** C preprocessor macros for portability support ***/
13 #ifndef __has_include
14 #define __has_include(x) 0
15 #endif
18 /*** Include directives for used system libraries ***/
20 #if defined(__linux__)
21 #define _GNU_SOURCE
22 #endif
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <syslog.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <time.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <netinet/in.h>
36 #include <poll.h>
37 #include <signal.h>
38 #include <sys/wait.h>
39 #include <sys/resource.h>
40 #include <sys/file.h>
41 #if defined(__FreeBSD__) || __has_include(<libutil.h>)
42 #include <libutil.h>
43 #endif
44 #if defined(__linux__) || __has_include(<bsd/stdio.h>)
45 #include <bsd/stdio.h>
46 #endif
47 #if defined(__linux__) || __has_include(<bsd/libutil.h>)
48 #include <bsd/libutil.h>
49 #endif
50 #if defined(__linux__) || __has_include(<bsd/unistd.h>)
51 #include <bsd/unistd.h>
52 #endif
55 /*** Fallback definitions for missing constants on some platforms ***/
57 /* INFTIM is used as timeout parameter for poll() */
58 #ifndef INFTIM
59 #define INFTIM -1
60 #endif
63 /*** Include directives for Lua ***/
65 #include <lua.h>
66 #include <lauxlib.h>
67 #include <lualib.h>
70 /*** Include directive for moonbridge_io library ***/
72 #include "moonbridge_io.h"
75 /*** Constants ***/
77 /* Backlog option for listen() call */
78 #define MOONBR_LISTEN_BACKLOG 1024
80 /* Maximum length of a timestamp used for strftime() */
81 #define MOONBR_LOG_MAXTIMELEN 40
83 /* Maximum length of a log message */
84 #define MOONBR_LOG_MAXMSGLEN 4095
86 /* Exitcodes passed to exit() call */
87 #define MOONBR_EXITCODE_GRACEFUL 0
88 #define MOONBR_EXITCODE_CMDLINEERROR 1
89 #define MOONBR_EXITCODE_ALREADYRUNNING 2
90 #define MOONBR_EXITCODE_STARTUPERROR 3
91 #define MOONBR_EXITCODE_RUNTIMEERROR 4
93 /* Maximum length of a line sent to stderr by child processes */
94 #define MOONBR_MAXERRORLINELEN 1024
96 /* Maximum length of an error string returned by strerror() */
97 #define MOONBR_MAXSTRERRORLEN 80
99 /* Status bytes exchanged between master and child processes */
100 #define MOONBR_SOCKETTYPE_INTERVAL 'I'
101 #define MOONBR_SOCKETTYPE_LOCAL 'L'
102 #define MOONBR_SOCKETTYPE_NETWORK 'N'
103 #define MOONBR_STATUS_IDLE '1'
104 #define MOONBR_COMMAND_TERMINATE '2'
105 #define MOONBR_STATUS_GOODBYE '3'
107 /* Constant file descriptors */
108 #define MOONBR_FD_STDERR 2
109 #define MOONBR_FD_CONTROL 3
110 #define MOONBR_FD_END 4
112 /* Return values of moonbr_try_destroy_worker() */
113 #define MOONBR_DESTROY_NONE 0
114 #define MOONBR_DESTROY_PREPARE 1
115 #define MOONBR_DESTROY_IDLE_OR_ASSIGNED 2
118 /*** Types ***/
120 /* Enum for 'moonbr_pstate' */
121 #define MOONBR_PSTATE_STARTUP 0
122 #define MOONBR_PSTATE_RUNNING 1
123 #define MOONBR_PSTATE_FORKED 2
125 /* Enum for 'proto' field of struct moonbr_listener */
126 #define MOONBR_PROTO_INTERVAL 1
127 #define MOONBR_PROTO_LOCAL 2
128 #define MOONBR_PROTO_TCP6 3
129 #define MOONBR_PROTO_TCP4 4
131 /* Data structure for a pool's listener that can accept incoming connections */
132 struct moonbr_listener {
133 struct moonbr_pool *pool;
134 struct moonbr_listener *prev_listener; /* previous idle or(!) connected listener */
135 struct moonbr_listener *next_listener; /* next idle or(!) connected listener */
136 int proto;
137 union {
138 struct {
139 char *name; /* name of interval passed to 'connect' function as 'interval' field in table */
140 int strict; /* nonzero = runtime of 'connect' function does not delay interval */
141 struct timeval delay; /* interval between invocations of 'connect' function */
142 struct timeval wakeup; /* point in time of next invocation */
143 } interval;
144 struct {
145 char *path; /* full path name (i.e. filename with path) of UNIX domain socket */
146 } local;
147 struct {
148 int port; /* port number to listen on (in host endianess) */
149 int localhost_only; /* nonzero = listen on localhost only */
150 } tcp;
151 } proto_specific;
152 int listenfd; /* -1 = none */
153 int pollidx; /* -1 = none */
154 };
156 /* Data structure for a child process that is handling incoming connections */
157 struct moonbr_worker {
158 struct moonbr_pool *pool;
159 struct moonbr_worker *prev_worker;
160 struct moonbr_worker *next_worker;
161 struct moonbr_worker *prev_idle_worker;
162 struct moonbr_worker *next_idle_worker;
163 int idle; /* nonzero = waiting for command from parent process */
164 int assigned; /* nonzero = currently handling a connection */
165 pid_t pid;
166 int controlfd; /* socket to send/receive control message to/from child process */
167 int errorfd; /* socket to receive error output from child process' stderr */
168 char *errorlinebuf; /* optional buffer for collecting stderr data from child process */
169 int errorlinelen; /* number of bytes stored in 'errorlinebuf' */
170 int errorlineovf; /* nonzero = line length overflow */
171 struct timeval idle_expiration; /* point in time until child process may stay in idle state */
172 struct moonbr_listener *restart_interval_listener; /* set while interval listener is assigned */
173 };
175 /* Data structure for a pool of workers and listeners */
176 struct moonbr_pool {
177 int poolnum; /* number of pool for log output */
178 struct moonbr_pool *next_pool; /* next entry in linked list starting with 'moonbr_first_pool' */
179 struct moonbr_worker *first_worker; /* first worker of pool */
180 struct moonbr_worker *last_worker; /* last worker of pool */
181 struct moonbr_worker *first_idle_worker; /* first idle worker of pool */
182 struct moonbr_worker *last_idle_worker; /* last idle worker of pool */
183 int idle_worker_count;
184 int unassigned_worker_count;
185 int total_worker_count;
186 int worker_count_stat; /* only needed for statistics */
187 int pre_fork; /* desired minimum number of unassigned workers */
188 int min_fork; /* desired minimum number of workers in total */
189 int max_fork; /* maximum number of workers */
190 struct timeval fork_delay; /* delay after each fork() until a fork may happen again */
191 struct timeval fork_wakeup; /* point in time when a fork may happen again (unless a worker terminates before) */
192 struct timeval fork_error_delay; /* delay between fork()s when an error during fork or preparation occurred */
193 struct timeval fork_error_wakeup; /* point in time when fork may happen again if an error in preparation occurred */
194 int use_fork_error_wakeup; /* nonzero = error in preparation occured; gets reset on next fork */
195 struct timeval exit_delay; /* delay for terminating excessive workers (unassigned_worker_count > pre_fork) */
196 struct timeval exit_wakeup; /* point in time when terminating an excessive worker */
197 struct timeval idle_timeout; /* delay before an idle worker is terminated */
198 size_t memory_limit; /* maximum bytes of memory that the Lua machine may allocate */
199 int listener_count; /* total number of listeners of pool (and size of 'listener' array at end of this struct) */
200 struct moonbr_listener *first_idle_listener; /* first listener that is idle (i.e. has no waiting connection) */
201 struct moonbr_listener *last_idle_listener; /* last listener that is idle (i.e. has no waiting connection) */
202 struct moonbr_listener *first_connected_listener; /* first listener that has a pending connection */
203 struct moonbr_listener *last_connected_listener; /* last listener that has a pending connection */
204 struct moonbr_listener listener[1]; /* static array of variable(!) size to contain 'listener' structures */
205 };
207 /* Enum for 'channel' field of struct moonbr_poll_worker */
208 #define MOONBR_POLL_WORKER_CONTROLCHANNEL 1
209 #define MOONBR_POLL_WORKER_ERRORCHANNEL 2
211 /* Structure to refer from 'moonbr_poll_worker_fds' entry to worker structure */
212 struct moonbr_poll_worker {
213 struct moonbr_worker *worker;
214 int channel; /* field indicating whether file descriptor is 'controlfd' or 'errorfd' */
215 };
217 /* Variable indicating that clean shutdown was requested */
218 static int moonbr_shutdown_in_progress = 0;
221 /*** Macros for Lua registry ***/
223 /* Lightuserdata keys for Lua registry to store 'prepare', 'connect', and 'finish' functions */
224 #define moonbr_luakey_prepare_func(pool) ((void *)(intptr_t)(pool) + 0)
225 #define moonbr_luakey_connect_func(pool) ((void *)(intptr_t)(pool) + 1)
226 #define moonbr_luakey_finish_func(pool) ((void *)(intptr_t)(pool) + 2)
229 /*** Global variables ***/
231 /* State of process execution */
232 static int moonbr_pstate = MOONBR_PSTATE_STARTUP;
234 /* Process ID of the main process */
235 static pid_t moonbr_masterpid;
237 /* Condition variables set by the signal handler */
238 static volatile sig_atomic_t moonbr_cond_poll = 0;
239 static volatile sig_atomic_t moonbr_cond_terminate = 0;
240 static volatile sig_atomic_t moonbr_cond_interrupt = 0;
241 static volatile sig_atomic_t moonbr_cond_child = 0;
243 /* Socket pair to denote signal delivery when signal handler was called just before poll() */
244 static int moonbr_poll_signalfds[2];
245 #define moonbr_poll_signalfd_read moonbr_poll_signalfds[0]
246 #define moonbr_poll_signalfd_write moonbr_poll_signalfds[1]
248 /* Global variables for pidfile and logging */
249 static struct pidfh *moonbr_pidfh = NULL;
250 static FILE *moonbr_logfile = NULL;
251 static int moonbr_use_syslog = 0;
253 /* First and last entry of linked list of all created pools during initialization */
254 static struct moonbr_pool *moonbr_first_pool = NULL;
255 static struct moonbr_pool *moonbr_last_pool = NULL;
257 /* Total count of pools */
258 static int moonbr_pool_count = 0;
260 /* Set to a nonzero value if dynamic part of 'moonbr_poll_fds' ('moonbr_poll_worker_fds') needs an update */
261 static int moonbr_poll_refresh_needed = 0;
263 /* Array passed to poll(), consisting of static part and dynamic part ('moonbr_poll_worker_fds') */
264 static struct pollfd *moonbr_poll_fds = NULL; /* the array */
265 static int moonbr_poll_fds_bufsize = 0; /* memory allocated for this number of elements */
266 static int moonbr_poll_fds_count = 0; /* total number of elements */
267 static int moonbr_poll_fds_static_count; /* number of elements in static part */
269 /* Dynamic part of 'moonbr_poll_fds' array */
270 #define moonbr_poll_worker_fds (moonbr_poll_fds+moonbr_poll_fds_static_count)
272 /* Additional information for dynamic part of 'moonbr_poll_fds' array */
273 struct moonbr_poll_worker *moonbr_poll_workers; /* the array */
274 static int moonbr_poll_workers_bufsize = 0; /* memory allocated for this number of elements */
275 static int moonbr_poll_worker_count = 0; /* number of elements in array */
277 /* Variable set to nonzero value to disallow further calls of 'listen' function */
278 static int moonbr_booted = 0;
280 /* Global variables to store information on connection socket in child process */
281 static int moonbr_child_peersocket_type; /* type of socket by MOONBR_SOCKETTYPE constant */
282 static int moonbr_child_peersocket_fd; /* Original file descriptor of peer socket */
283 static luaL_Stream *moonbr_child_peersocket_inputstream; /* Lua input stream of socket */
284 static luaL_Stream *moonbr_child_peersocket_outputstream; /* Lua output stream of socket */
286 /* Verbosity settings */
287 static int moonbr_debug = 0;
288 static int moonbr_stat = 0;
290 /* Memory consumption by Lua machine */
291 static size_t moonbr_memory_usage = 0;
292 static size_t moonbr_memory_limit = 0;
295 /*** Functions for signal handling ***/
297 /* Signal handler for master and child processes */
298 static void moonbr_signal(int sig) {
299 if (getpid() == moonbr_masterpid) {
300 /* master process */
301 switch (sig) {
302 case SIGHUP:
303 case SIGINT:
304 /* fast shutdown requested */
305 moonbr_cond_interrupt = 1;
306 break;
307 case SIGTERM:
308 /* clean shutdown requested */
309 moonbr_cond_terminate = 1;
310 break;
311 case SIGCHLD:
312 /* child process terminated */
313 moonbr_cond_child = 1;
314 break;
315 }
316 if (moonbr_cond_poll) {
317 /* avoid race condition if signal handler is invoked right before poll() */
318 char buf[1] = {0};
319 write(moonbr_poll_signalfd_write, buf, 1);
320 }
321 } else {
322 /* child process forwards certain signals to parent process */
323 switch (sig) {
324 case SIGHUP:
325 case SIGINT:
326 case SIGTERM:
327 kill(moonbr_masterpid, sig);
328 }
329 }
330 }
332 /* Initialize signal handling */
333 static void moonbr_signal_init(){
334 moonbr_masterpid = getpid();
335 signal(SIGHUP, moonbr_signal);
336 signal(SIGINT, moonbr_signal);
337 signal(SIGTERM, moonbr_signal);
338 signal(SIGCHLD, moonbr_signal);
339 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */
340 }
343 /*** Functions for logging in master process ***/
345 /* Logs a pre-formatted message with given syslog() priority */
346 static void moonbr_log_msg(int priority, const char *msg) {
347 if (moonbr_logfile) {
348 /* logging to logfile desired (timestamp is prepended in that case) */
349 time_t now_time = 0;
350 struct tm now_tmstruct;
351 char timestr[MOONBR_LOG_MAXTIMELEN+1];
352 time(&now_time);
353 localtime_r(&now_time, &now_tmstruct);
354 if (!strftime(
355 timestr, MOONBR_LOG_MAXTIMELEN+1, "%Y-%m-%d %H:%M:%S %Z: ", &now_tmstruct
356 )) timestr[0] = 0;
357 fprintf(moonbr_logfile, "%s%s\n", timestr, msg);
358 }
359 if (moonbr_use_syslog) {
360 /* logging through syslog desired */
361 syslog(priority, "%s", msg);
362 }
363 }
365 /* Formats a message via vsnprintf() and logs it with given syslog() priority */
366 static void moonbr_log(int priority, const char *message, ...) {
367 char msgbuf[MOONBR_LOG_MAXMSGLEN+1]; /* buffer of static size to store formatted message */
368 int msglen; /* length of full message (may exceed MOONBR_LOG_MAXMSGLEN) */
369 {
370 /* pass variable arguments to vsnprintf() to format message */
371 va_list ap;
372 va_start(ap, message);
373 msglen = vsnprintf(msgbuf, MOONBR_LOG_MAXMSGLEN+1, message, ap);
374 va_end(ap);
375 }
376 {
377 /* split and log message line by line */
378 char *line = msgbuf;
379 while (1) {
380 char *endptr = strchr(line, '\n');
381 if (endptr) {
382 /* terminate string where newline character is found */
383 *endptr = 0;
384 } else if (line != msgbuf && msglen > MOONBR_LOG_MAXMSGLEN) {
385 /* break if line is incomplete and not the first line */
386 break;
387 }
388 moonbr_log_msg(priority, line);
389 if (!endptr) break; /* break if end of formatted message is reached */
390 line = endptr+1; /* otherwise continue with remaining message */
391 }
392 }
393 if (msglen > MOONBR_LOG_MAXMSGLEN) {
394 /* print warning if message was truncated */
395 moonbr_log_msg(priority, "Previous log message has been truncated due to excessive length");
396 }
397 }
400 /*** Termination function ***/
402 /* Kill all child processes, remove PID file (if existent), and exit master process with given exitcode */
403 static void moonbr_terminate(int exitcode) {
404 {
405 struct moonbr_pool *pool;
406 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
407 {
408 struct moonbr_worker *worker;
409 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
410 moonbr_log(LOG_INFO, "Sending SIGKILL to child with PID %i", (int)worker->pid);
411 if (kill(worker->pid, SIGKILL)) {
412 moonbr_log(LOG_ERR, "Error while killing child process: %s", strerror(errno));
413 }
414 }
415 }
416 {
417 int i;
418 for (i=0; i<pool->listener_count; i++) {
419 struct moonbr_listener *listener = &pool->listener[i];
420 if (listener->proto == MOONBR_PROTO_LOCAL) {
421 moonbr_log(LOG_INFO, "Unlinking local socket \"%s\"", listener->proto_specific.local.path);
422 if (unlink(listener->proto_specific.local.path)) {
423 moonbr_log(LOG_ERR, "Error while unlinking local socket: %s", strerror(errno));
424 }
425 }
426 }
427 }
428 }
429 }
430 moonbr_log(exitcode ? LOG_ERR : LOG_NOTICE, "Terminating with exit code %i", exitcode);
431 if (moonbr_pidfh && pidfile_remove(moonbr_pidfh)) {
432 moonbr_log(LOG_ERR, "Error while removing PID file: %s", strerror(errno));
433 }
434 exit(exitcode);
435 }
437 /* Terminate with either MOONBR_EXITCODE_STARTUPERROR or MOONBR_EXITCODE_RUNTIMEERROR */
438 #define moonbr_terminate_error() \
439 moonbr_terminate( \
440 moonbr_pstate == MOONBR_PSTATE_STARTUP ? \
441 MOONBR_EXITCODE_STARTUPERROR : \
442 MOONBR_EXITCODE_RUNTIMEERROR \
443 )
446 /*** Helper functions ***/
448 /* Fills a 'struct timeval' structure with the current time (using CLOCK_MONOTONIC) */
449 static void moonbr_now(struct timeval *now) {
450 struct timespec ts = {0, };
451 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
452 moonbr_log(LOG_CRIT, "Error in clock_gettime() call: %s", strerror(errno));
453 moonbr_terminate_error();
454 }
455 *now = (struct timeval){ .tv_sec = ts.tv_sec, .tv_usec = ts.tv_nsec / 1000 };
456 }
458 /* Formats a 'struct timeval' value (not thread-safe) */
459 static char *moonbr_format_timeval(struct timeval *t) {
460 static char buf[32];
461 snprintf(buf, 32, "%ji.%06ji seconds", (intmax_t)t->tv_sec, (intmax_t)t->tv_usec);
462 return buf;
463 }
466 /*** Functions for pool creation and startup ***/
468 /* Creates a 'struct moonbr_pool' structure with a given number of listeners */
469 static struct moonbr_pool *moonbr_create_pool(int listener_count) {
470 struct moonbr_pool *pool;
471 pool = calloc(1,
472 sizeof(struct moonbr_pool) + /* size of 'struct moonbr_pool' with one listener */
473 (listener_count-1) * sizeof(struct moonbr_listener) /* size of extra listeners */
474 );
475 if (!pool) {
476 moonbr_log(LOG_CRIT, "Memory allocation error");
477 moonbr_terminate_error();
478 }
479 pool->listener_count = listener_count;
480 {
481 /* initialization of listeners */
482 int i;
483 for (i=0; i<listener_count; i++) {
484 struct moonbr_listener *listener = &pool->listener[i];
485 listener->pool = pool;
486 listener->listenfd = -1;
487 listener->pollidx = -1;
488 }
489 }
490 return pool;
491 }
493 /* Destroys a 'struct moonbr_pool' structure before it has been started */
494 static void moonbr_destroy_pool(struct moonbr_pool *pool) {
495 int i;
496 for (i=0; i<pool->listener_count; i++) {
497 struct moonbr_listener *listener = &pool->listener[i];
498 if (
499 listener->proto == MOONBR_PROTO_INTERVAL &&
500 listener->proto_specific.interval.name
501 ) {
502 free(listener->proto_specific.interval.name);
503 }
504 if (
505 listener->proto == MOONBR_PROTO_LOCAL &&
506 listener->proto_specific.local.path
507 ) {
508 free(listener->proto_specific.local.path);
509 }
510 }
511 free(pool);
512 }
514 /* Starts a all listeners in a pool */
515 static int moonbr_start_pool(struct moonbr_pool *pool) {
516 moonbr_log(LOG_INFO, "Creating pool", pool->poolnum);
517 {
518 int i;
519 for (i=0; i<pool->listener_count; i++) {
520 struct moonbr_listener *listener = &pool->listener[i];
521 switch (listener->proto) {
522 case MOONBR_PROTO_INTERVAL:
523 /* nothing to do here: starting intervals is performed in moonbr_run() function */
524 if (!listener->proto_specific.interval.name) {
525 moonbr_log(LOG_INFO, "Adding unnamed interval listener");
526 } else {
527 moonbr_log(LOG_INFO, "Adding interval listener \"%s\"", listener->proto_specific.interval.name);
528 }
529 break;
530 case MOONBR_PROTO_LOCAL:
531 moonbr_log(LOG_INFO, "Adding local socket listener for path \"%s\"", listener->proto_specific.local.path);
532 {
533 struct sockaddr_un servaddr = { .sun_family = AF_UNIX };
534 const int path_maxlen = sizeof(struct sockaddr_un) - (
535 (void *)&servaddr.sun_path - (void *)&servaddr
536 );
537 if (
538 snprintf(
539 servaddr.sun_path,
540 path_maxlen,
541 "%s",
542 listener->proto_specific.local.path
543 ) >= path_maxlen
544 ) {
545 errno = ENAMETOOLONG;
546 };
547 listener->listenfd = socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
548 if (listener->listenfd == -1) goto moonbr_start_pool_error;
549 if (!unlink(listener->proto_specific.local.path)) {
550 moonbr_log(LOG_WARNING, "Unlinked named socket \"%s\" prior to listening", listener->proto_specific.local.path);
551 } else {
552 if (errno != ENOENT) {
553 moonbr_log(LOG_ERR, "Could not unlink named socket \"%s\" prior to listening: %s", listener->proto_specific.local.path, strerror(errno));
554 }
555 }
556 if (
557 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
558 ) goto moonbr_start_pool_error;
559 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
560 }
561 break;
562 case MOONBR_PROTO_TCP6:
563 if (listener->proto_specific.tcp.localhost_only) {
564 moonbr_log(LOG_INFO, "Adding localhost TCP/IPv6 listener on port %i", listener->proto_specific.tcp.port);
565 } else {
566 moonbr_log(LOG_INFO, "Adding public TCP/IPv6 listener on port %i", listener->proto_specific.tcp.port);
567 }
568 {
569 struct sockaddr_in6 servaddr = {
570 .sin6_family = AF_INET6,
571 .sin6_port = htons(listener->proto_specific.tcp.port)
572 };
573 listener->listenfd = socket(PF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
574 if (listener->listenfd == -1) goto moonbr_start_pool_error;
575 {
576 /* avoid "Address already in use" error when restarting service */
577 static const int reuseval = 1;
578 if (setsockopt(
579 listener->listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval)
580 )) goto moonbr_start_pool_error;
581 }
582 {
583 /* default to send TCP RST when process terminates unexpectedly */
584 static const struct linger lingerval = {
585 .l_onoff = 1,
586 .l_linger = 0
587 };
588 if (setsockopt(
589 listener->listenfd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval)
590 )) goto moonbr_start_pool_error;
591 }
592 if (listener->proto_specific.tcp.localhost_only) {
593 servaddr.sin6_addr.s6_addr[15] = 1;
594 }
595 if (
596 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
597 ) goto moonbr_start_pool_error;
598 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
599 }
600 break;
601 case MOONBR_PROTO_TCP4:
602 if (listener->proto_specific.tcp.localhost_only) {
603 moonbr_log(LOG_INFO, "Adding localhost TCP/IPv4 listener on port %i", listener->proto_specific.tcp.port);
604 } else {
605 moonbr_log(LOG_INFO, "Adding public TCP/IPv4 listener on port %i", listener->proto_specific.tcp.port);
606 }
607 {
608 struct sockaddr_in servaddr = {
609 .sin_family = AF_INET,
610 .sin_port = htons(listener->proto_specific.tcp.port)
611 };
612 listener->listenfd = socket(PF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
613 if (listener->listenfd == -1) goto moonbr_start_pool_error;
614 {
615 /* avoid "Address already in use" error when restarting service */
616 static const int reuseval = 1;
617 if (setsockopt(
618 listener->listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval)
619 )) goto moonbr_start_pool_error;
620 }
621 {
622 /* default to send TCP RST when process terminates unexpectedly */
623 static const struct linger lingerval = {
624 .l_onoff = 1,
625 .l_linger = 0
626 };
627 if (setsockopt(
628 listener->listenfd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval)
629 )) goto moonbr_start_pool_error;
630 }
631 if (listener->proto_specific.tcp.localhost_only) {
632 ((uint8_t *)&servaddr.sin_addr.s_addr)[0] = 127;
633 ((uint8_t *)&servaddr.sin_addr.s_addr)[3] = 1;
634 }
635 if (
636 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
637 ) goto moonbr_start_pool_error;
638 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
639 }
640 break;
641 default:
642 moonbr_log(LOG_CRIT, "Internal error (should not happen): Unexpected value in listener.proto field");
643 moonbr_terminate_error();
644 }
645 }
646 goto moonbr_start_pool_ok;
647 moonbr_start_pool_error:
648 {
649 int j = i;
650 int errno2 = errno;
651 for (; i>=0; i--) {
652 struct moonbr_listener *listener = &pool->listener[i];
653 if (listener->listenfd != -1) close(listener->listenfd);
654 }
655 errno = errno2;
656 return j;
657 }
658 }
659 moonbr_start_pool_ok:
660 pool->poolnum = ++moonbr_pool_count;
661 moonbr_log(LOG_INFO, "Pool #%i created", pool->poolnum);
662 if (moonbr_last_pool) moonbr_last_pool->next_pool = pool;
663 else moonbr_first_pool = pool;
664 moonbr_last_pool = pool;
665 return -1;
666 }
669 /*** Function to send data and a file descriptor to child process */
671 /* Sends control message of one bye plus optional file descriptor plus optional pointer to child process */
672 static void moonbr_send_control_message(struct moonbr_worker *worker, char status, int fd, void *ptr) {
673 {
674 struct iovec iovector = { .iov_base = &status, .iov_len = 1 }; /* carrying status byte */
675 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to transfer file descriptor */
676 struct msghdr message = { .msg_iov = &iovector, .msg_iovlen = 1 }; /* data structure passed to sendmsg() call */
677 if (moonbr_debug) {
678 if (fd == -1) {
679 moonbr_log(LOG_DEBUG, "Sending control message \"%c\" to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
680 } else {
681 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);
682 }
683 }
684 if (fd != -1) {
685 /* attach control message with file descriptor */
686 message.msg_control = control_message_buffer;
687 message.msg_controllen = CMSG_SPACE(sizeof(int));
688 {
689 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
690 control_message->cmsg_level = SOL_SOCKET;
691 control_message->cmsg_type = SCM_RIGHTS;
692 control_message->cmsg_len = CMSG_LEN(sizeof(int));
693 memcpy(CMSG_DATA(control_message), &fd, sizeof(int));
694 }
695 }
696 while (sendmsg(worker->controlfd, &message, MSG_NOSIGNAL) < 0) {
697 if (errno == EPIPE) {
698 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));
699 return; /* do not close socket; socket is closed when reading from it */
700 }
701 if (errno != EINTR) {
702 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));
703 moonbr_terminate_error();
704 }
705 }
706 }
707 if (ptr) {
708 char buf[sizeof(void *)];
709 char *pos = buf;
710 int len = sizeof(void *);
711 ssize_t written;
712 if (moonbr_debug) {
713 moonbr_log(LOG_DEBUG, "Sending memory pointer to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
714 }
715 memcpy(buf, &ptr, sizeof(void *));
716 while (len) {
717 written = send(worker->controlfd, pos, len, MSG_NOSIGNAL);
718 if (written > 0) {
719 pos += written;
720 len -= written;
721 } else if (errno == EPIPE) {
722 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));
723 return; /* do not close socket; socket is closed when reading from it */
724 } else if (errno != EINTR) {
725 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));
726 moonbr_terminate_error();
727 }
728 }
729 }
730 }
733 /*** Functions running in child process ***/
735 /* Logs an error in child process */
736 static void moonbr_child_log(const char *message) {
737 fprintf(stderr, "%s\n", message);
738 }
740 /* Logs a fatal error in child process and terminates process with error status */
741 static void moonbr_child_log_fatal(const char *message) {
742 moonbr_child_log(message);
743 exit(1);
744 }
746 /* Logs an error in child process while appending error string for global errno variable */
747 static void moonbr_child_log_errno(const char *message) {
748 char errmsg[MOONBR_MAXSTRERRORLEN];
749 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
750 fprintf(stderr, "%s: %s\n", message, errmsg);
751 }
753 /* Logs a fatal error in child process while appending error string for errno and terminating process */
754 static void moonbr_child_log_errno_fatal(const char *message) {
755 moonbr_child_log_errno(message);
756 exit(1);
757 }
759 /* Receives a control message consisting of one character plus an optional file descriptor from parent process */
760 static void moonbr_child_receive_control_message(int socketfd, char *status, int *fd) {
761 struct iovec iovector = { .iov_base = status, .iov_len = 1 }; /* reference to status byte variable */
762 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to receive file descriptor */
763 struct msghdr message = { /* data structure passed to recvmsg() call */
764 .msg_iov = &iovector,
765 .msg_iovlen = 1,
766 .msg_control = control_message_buffer,
767 .msg_controllen = CMSG_SPACE(sizeof(int))
768 };
769 {
770 int received;
771 while ((received = recvmsg(socketfd, &message, MSG_CMSG_CLOEXEC)) < 0) {
772 if (errno != EINTR) {
773 moonbr_child_log_errno_fatal("Error while trying to receive connection socket from parent process");
774 }
775 }
776 if (!received) {
777 moonbr_child_log_fatal("Unexpected EOF while trying to receive connection socket from parent process");
778 }
779 }
780 {
781 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
782 if (control_message) {
783 if (control_message->cmsg_level != SOL_SOCKET) {
784 moonbr_child_log_fatal("Received control message with cmsg_level not equal to SOL_SOCKET");
785 }
786 if (control_message->cmsg_type != SCM_RIGHTS) {
787 moonbr_child_log_fatal("Received control message with cmsg_type not equal to SCM_RIGHTS");
788 }
789 memcpy(fd, CMSG_DATA(control_message), sizeof(int));
790 } else {
791 *fd = -1;
792 }
793 }
794 }
796 /* Receives a pointer from parent process */
797 static void *moonbr_child_receive_pointer(int socketfd) {
798 char buf[sizeof(void *)];
799 char *pos = buf;
800 int len = sizeof(void *);
801 ssize_t bytes_read;
802 while (len) {
803 bytes_read = recv(socketfd, pos, len, 0);
804 if (bytes_read > 0) {
805 pos += bytes_read;
806 len -= bytes_read;
807 } else if (!bytes_read) {
808 moonbr_child_log_fatal("Unexpected EOF while trying to receive memory pointer from parent process");
809 } else if (errno != EINTR) {
810 moonbr_child_log_errno_fatal("Error while trying to receive memory pointer from parent process");
811 }
812 }
813 {
814 void *ptr; /* avoid breaking strict-aliasing rules */
815 memcpy(&ptr, buf, sizeof(void *));
816 return ptr;
817 }
818 }
820 /* Closes the input stream from peer unless it has already been closed */
821 static int moonbr_child_close_peersocket_inputstream(
822 int cleanshut, /* nonzero = use shutdown() if applicable */
823 int mark /* nonzero = mark the stream as closed for Lua */
824 ) {
825 int err = 0; /* nonzero = error occurred */
826 int errno2; /* stores previous errno values that take precedence */
827 if (moonbr_child_peersocket_inputstream->f) {
828 /* NOTE: shutdown() with SHUT_RD shows different behavior on different
829 * operating systems and particularly causes problems with Linux. Hence it
830 * is disabled here. */
831 /*
832 if (cleanshut && moonbr_child_peersocket_type == MOONBR_SOCKETTYPE_NETWORK) {
833 if (shutdown(moonbr_child_peersocket_fd, SHUT_RD)) {
834 errno2 = errno;
835 err = -1;
836 }
837 }
838 */
839 if (fclose(moonbr_child_peersocket_inputstream->f)) {
840 if (!err) errno2 = errno;
841 err = -1;
842 }
843 moonbr_child_peersocket_inputstream->f = NULL;
844 }
845 if (mark) moonbr_child_peersocket_inputstream->closef = NULL;
846 if (err) errno = errno2;
847 return err;
848 }
850 /* Closes the output stream to peer unless it has already been closed */
851 static int moonbr_child_close_peersocket_outputstream(
852 int cleanshut, /* nonzero = use fflush() and shutdown() if applicable */
853 int mark /* nonzero = mark the stream as closed for Lua */
854 ) {
855 int err = 0; /* nonzero = error occurred */
856 int errno2; /* stores previous errno values that take precedence */
857 if (moonbr_child_peersocket_outputstream->f) {
858 if (moonbr_child_peersocket_type == MOONBR_SOCKETTYPE_NETWORK) {
859 if (cleanshut) {
860 if (fflush(moonbr_child_peersocket_outputstream->f)) {
861 errno2 = errno;
862 err = -1;
863 } else {
864 if (shutdown(moonbr_child_peersocket_fd, SHUT_WR)) {
865 errno2 = errno;
866 err = -1;
867 }
868 }
869 } else {
870 fpurge(moonbr_child_peersocket_outputstream->f);
871 }
872 }
873 if (fclose(moonbr_child_peersocket_outputstream->f)) {
874 if (!err) errno2 = errno;
875 err = -1;
876 }
877 moonbr_child_peersocket_outputstream->f = NULL;
878 }
879 if (mark) moonbr_child_peersocket_outputstream->closef = NULL;
880 if (err) errno = errno2;
881 return err;
882 }
884 /* Perform a clean shutdown of input and output stream (may be called multiple times) */
885 static int moonbr_child_close_peersocket(int timeout) {
886 int errprio = 0;
887 int errno2;
888 if (moonbr_child_peersocket_fd == -1) return 0;
889 if (moonbr_child_close_peersocket_inputstream(1, 1)) {
890 errprio = 1;
891 errno2 = errno;
892 }
893 if (moonbr_child_close_peersocket_outputstream(1, 1)) {
894 errprio = 4;
895 errno2 = errno;
896 }
897 if (moonbr_child_peersocket_type == MOONBR_SOCKETTYPE_NETWORK) {
898 struct linger lingerval = { 0, };
899 if (timeout && !errprio) {
900 lingerval.l_onoff = 1;
901 lingerval.l_linger = timeout;
902 }
903 if (setsockopt(moonbr_child_peersocket_fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
904 if (errprio < 2) {
905 errprio = 2;
906 errno2 = errno;
907 }
908 }
909 }
910 if (close(moonbr_child_peersocket_fd)) {
911 if (errprio < 3) {
912 errprio = 3;
913 errno2 = errno;
914 }
915 }
916 moonbr_child_peersocket_fd = -1;
917 if (errprio) {
918 errno = errno2;
919 return -1;
920 }
921 return 0;
922 }
924 /* Close socket and cause reset of TCP connection (TCP RST aka "Connection reset by peer") if possible */
925 static int moonbr_child_cancel_peersocket() {
926 int err = 0;
927 if (moonbr_child_close_peersocket_inputstream(0, 1)) err = -1;
928 if (moonbr_child_close_peersocket_outputstream(0, 1)) err = -1;
929 if (close(moonbr_child_peersocket_fd)) err = -1;
930 moonbr_child_peersocket_fd = -1;
931 return err;
932 }
934 /* Lua method for socket object to read from input stream */
935 static int moonbr_child_lua_read_stream(lua_State *L) {
936 lua_getfield(L, 1, "input");
937 lua_getfield(L, -1, "read");
938 lua_insert(L, 1);
939 lua_replace(L, 2);
940 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
941 return lua_gettop(L);
942 }
944 /* Lua method for socket object to read from input stream until terminator */
945 static int moonbr_child_lua_readuntil_stream(lua_State *L) {
946 lua_getfield(L, 1, "input");
947 lua_getfield(L, -1, "readuntil");
948 lua_insert(L, 1);
949 lua_replace(L, 2);
950 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
951 return lua_gettop(L);
952 }
954 /* Lua method for socket object to iterate over input stream */
955 static int moonbr_child_lua_lines_stream(lua_State *L) {
956 lua_getfield(L, 1, "input");
957 lua_getfield(L, -1, "lines");
958 lua_insert(L, 1);
959 lua_replace(L, 2);
960 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
961 return lua_gettop(L);
962 }
964 /* Lua method for socket object to write to output stream */
965 static int moonbr_child_lua_write_stream(lua_State *L) {
966 lua_getfield(L, 1, "output");
967 lua_getfield(L, -1, "write");
968 lua_insert(L, 1);
969 lua_replace(L, 2);
970 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
971 return lua_gettop(L);
972 }
974 /* Lua method for socket object to flush the output stream */
975 static int moonbr_child_lua_flush_stream(lua_State *L) {
976 lua_getfield(L, 1, "output");
977 lua_getfield(L, -1, "flush");
978 lua_insert(L, 1);
979 lua_replace(L, 2);
980 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
981 return lua_gettop(L);
982 }
984 /* Lua function to close a single stream (input or output) from/to peer */
985 static int moonbr_child_lua_close_stream(lua_State *L) {
986 luaL_Stream *stream = lua_touserdata(L, 1);
987 if (stream == moonbr_child_peersocket_inputstream) {
988 if (moonbr_child_close_peersocket_inputstream(1, 0)) { /* don't mark as closed as it's done by Lua */
989 char errmsg[MOONBR_MAXSTRERRORLEN];
990 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
991 lua_pushnil(L);
992 lua_pushfstring(L, "Could not close input stream from peer: %s", errmsg);
993 return 2;
994 }
995 } else if (stream == moonbr_child_peersocket_outputstream) {
996 if (moonbr_child_close_peersocket_outputstream(1, 0)) { /* don't mark as closed as it's done by Lua */
997 char errmsg[MOONBR_MAXSTRERRORLEN];
998 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
999 lua_pushnil(L);
1000 lua_pushfstring(L, "Could not close output stream to peer: %s", errmsg);
1001 return 2;
1003 } else {
1004 luaL_argerror(L, 1, "Not a connection socket");
1006 lua_pushboolean(L, 1);
1007 return 1;
1010 /* Lua function to close both input and output stream from/to peer */
1011 static int moonbr_child_lua_close_both_streams(lua_State *L) {
1012 int timeout = 0;
1013 if (!lua_isnoneornil(L, 2)) {
1014 lua_Integer n = luaL_checkinteger(L, 2);
1015 luaL_argcheck(L, n >= 0 && n <= INT_MAX, 2, "out of range");
1016 timeout = n;
1018 if (moonbr_child_peersocket_fd == -1) {
1019 luaL_error(L, "Connection with peer has already been explicitly closed");
1021 if (moonbr_child_close_peersocket(timeout)) {
1022 char errmsg[MOONBR_MAXSTRERRORLEN];
1023 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
1024 lua_pushnil(L);
1025 lua_pushfstring(L, "Could not close socket connection with peer: %s", errmsg);
1026 return 2;
1028 lua_pushboolean(L, 1);
1029 return 1;
1032 /* Lua function to close both input and output stream from/to peer */
1033 static int moonbr_child_lua_cancel_both_streams(lua_State *L) {
1034 if (moonbr_child_peersocket_fd == -1) {
1035 luaL_error(L, "Connection with peer has already been explicitly closed");
1037 if (moonbr_child_cancel_peersocket()) {
1038 char errmsg[MOONBR_MAXSTRERRORLEN];
1039 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
1040 lua_pushnil(L);
1041 lua_pushfstring(L, "Could not cancel socket connection with peer: %s", errmsg);
1042 return 2;
1044 lua_pushboolean(L, 1);
1045 return 1;
1048 /* Methods of (bidirectional) socket object passed to handler */
1049 static luaL_Reg moonbr_child_lua_socket_functions[] = {
1050 {"read", moonbr_child_lua_read_stream},
1051 {"readuntil", moonbr_child_lua_readuntil_stream},
1052 {"lines", moonbr_child_lua_lines_stream},
1053 {"write", moonbr_child_lua_write_stream},
1054 {"flush", moonbr_child_lua_flush_stream},
1055 {"close", moonbr_child_lua_close_both_streams},
1056 {"cancel", moonbr_child_lua_cancel_both_streams},
1057 {NULL, NULL}
1058 };
1060 /* Main function of child process to be called after fork() and file descriptor rearrangement */
1061 void moonbr_child_run(struct moonbr_pool *pool, lua_State *L) {
1062 char controlmsg;
1063 struct itimerval notimer = { { 0, }, { 0, } };
1064 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
1065 if (lua_isnil(L, -1)) lua_pop(L, 1);
1066 else if (lua_pcall(L, 0, 0, 1)) {
1067 fprintf(stderr, "Error in \"prepare\" function: %s\n", lua_tostring(L, -1));
1068 exit(1);
1070 while (1) {
1071 struct moonbr_listener *listener;
1072 if (setitimer(ITIMER_REAL, &notimer, NULL)) {
1073 moonbr_child_log_errno_fatal("Could not reset ITIMER_REAL via setitimer()");
1075 controlmsg = MOONBR_STATUS_IDLE;
1076 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
1077 moonbr_child_log_errno_fatal("Error while sending ready message to parent process");
1079 moonbr_child_receive_control_message(
1080 MOONBR_FD_CONTROL,
1081 &controlmsg,
1082 &moonbr_child_peersocket_fd
1083 );
1084 if (!(
1085 (controlmsg == MOONBR_COMMAND_TERMINATE && moonbr_child_peersocket_fd == -1) ||
1086 (controlmsg == MOONBR_SOCKETTYPE_INTERVAL && moonbr_child_peersocket_fd == -1) ||
1087 (controlmsg == MOONBR_SOCKETTYPE_LOCAL && moonbr_child_peersocket_fd != -1) ||
1088 (controlmsg == MOONBR_SOCKETTYPE_NETWORK && moonbr_child_peersocket_fd != -1)
1089 )) {
1090 moonbr_child_log_fatal("Received illegal control message from parent process");
1092 if (controlmsg == MOONBR_COMMAND_TERMINATE) break;
1093 listener = moonbr_child_receive_pointer(MOONBR_FD_CONTROL);
1094 moonbr_child_peersocket_type = controlmsg;
1095 if (moonbr_child_peersocket_fd != -1) {
1097 int clonedfd;
1098 clonedfd = dup(moonbr_child_peersocket_fd);
1099 if (!clonedfd) {
1100 moonbr_child_log_errno_fatal("Could not duplicate file descriptor for input stream");
1102 moonbr_child_peersocket_inputstream = lua_newuserdata(L, sizeof(luaL_Stream));
1103 if (!moonbr_child_peersocket_inputstream) {
1104 moonbr_child_log_fatal("Memory allocation error");
1106 moonbr_child_peersocket_inputstream->f = fdopen(clonedfd, "rb");
1107 if (!moonbr_child_peersocket_inputstream->f) {
1108 moonbr_child_log_errno_fatal("Could not open input stream for remote connection");
1110 moonbr_child_peersocket_inputstream->closef = moonbr_child_lua_close_stream;
1111 if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
1112 moonbr_child_log_fatal("Lua metatable LUA_FILEHANDLE does not exist");
1114 lua_setmetatable(L, -2);
1117 int clonedfd;
1118 clonedfd = dup(moonbr_child_peersocket_fd);
1119 if (!clonedfd) {
1120 moonbr_child_log_errno_fatal("Could not duplicate file descriptor for output stream");
1122 moonbr_child_peersocket_outputstream = lua_newuserdata(L, sizeof(luaL_Stream));
1123 if (!moonbr_child_peersocket_outputstream) {
1124 moonbr_child_log_fatal("Memory allocation error");
1126 moonbr_child_peersocket_outputstream->f = fdopen(clonedfd, "wb");
1127 if (!moonbr_child_peersocket_outputstream->f) {
1128 moonbr_child_log_errno_fatal("Could not open output stream for remote connection");
1130 moonbr_child_peersocket_outputstream->closef = moonbr_child_lua_close_stream;
1131 if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
1132 moonbr_child_log_fatal("Lua metatable LUA_FILEHANDLE does not exist");
1134 lua_setmetatable(L, -2);
1137 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
1138 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1139 lua_newtable(L);
1140 lua_pushstring(L,
1141 listener->proto_specific.interval.name ?
1142 listener->proto_specific.interval.name : ""
1143 );
1144 lua_setfield(L, -2, "interval");
1145 } else {
1146 lua_newtable(L);
1147 lua_pushvalue(L, -4);
1148 lua_setfield(L, -2, "input");
1149 lua_pushvalue(L, -3);
1150 lua_setfield(L, -2, "output");
1151 luaL_setfuncs(L, moonbr_child_lua_socket_functions, 0);
1152 if (listener->proto == MOONBR_PROTO_TCP6) {
1153 struct sockaddr_in6 addr;
1154 socklen_t addr_len = sizeof(struct sockaddr_in6);
1155 if (getsockname(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1156 moonbr_child_log_errno("Could not get local IP address/port");
1157 } else {
1158 lua_pushlstring(L, (char *)addr.sin6_addr.s6_addr, 16);
1159 lua_setfield(L, -2, "local_ip6");
1160 lua_pushinteger(L, ntohs(addr.sin6_port));
1161 lua_setfield(L, -2, "local_tcpport");
1163 if (getpeername(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1164 moonbr_child_log_errno("Could not get remote IP address/port");
1165 } else {
1166 lua_pushlstring(L, (char *)addr.sin6_addr.s6_addr, 16);
1167 lua_setfield(L, -2, "remote_ip6");
1168 lua_pushinteger(L, ntohs(addr.sin6_port));
1169 lua_setfield(L, -2, "remote_tcpport");
1171 } else if (listener->proto == MOONBR_PROTO_TCP4) {
1172 struct sockaddr_in addr;
1173 socklen_t addr_len = sizeof(struct sockaddr_in);
1174 if (getsockname(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1175 moonbr_child_log_errno("Could not get local IP address/port");
1176 } else {
1177 lua_pushlstring(L, (char *)&addr.sin_addr.s_addr, 4);
1178 lua_setfield(L, -2, "local_ip4");
1179 lua_pushinteger(L, ntohs(addr.sin_port));
1180 lua_setfield(L, -2, "local_tcpport");
1182 if (getpeername(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1183 moonbr_child_log_errno("Could not get remote IP address/port");
1184 } else {
1185 lua_pushlstring(L, (char *)&addr.sin_addr.s_addr, 4);
1186 lua_setfield(L, -2, "remote_ip4");
1187 lua_pushinteger(L, ntohs(addr.sin_port));
1188 lua_setfield(L, -2, "remote_tcpport");
1192 if (lua_pcall(L, 1, 1, 1)) {
1193 fprintf(stderr, "Error in \"connect\" function: %s\n", lua_tostring(L, -1));
1194 exit(1);
1196 if (moonbr_child_close_peersocket(0)) {
1197 moonbr_child_log_errno("Could not close socket connection with peer");
1199 if (lua_type(L, -1) != LUA_TBOOLEAN || !lua_toboolean(L, -1)) break;
1200 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
1201 lua_settop(L, 2);
1202 #else
1203 lua_settop(L, 1);
1204 #endif
1206 controlmsg = MOONBR_STATUS_GOODBYE;
1207 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
1208 moonbr_child_log_errno_fatal("Error while sending goodbye message to parent process");
1210 if (close(MOONBR_FD_CONTROL) && errno != EINTR) {
1211 moonbr_child_log_errno("Error while closing control socket");
1213 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
1214 if (lua_isnil(L, -1)) lua_pop(L, 1);
1215 else if (lua_pcall(L, 0, 0, 1)) {
1216 fprintf(stderr, "Error in \"finish\" function: %s\n", lua_tostring(L, -1));
1217 exit(1);
1219 lua_close(L);
1220 exit(0);
1224 /*** Functions to spawn child process ***/
1226 /* Helper function to send an error message to a file descriptor (not needing a file stream) */
1227 static void moonbr_child_emergency_print(int fd, char *message) {
1228 size_t len = strlen(message);
1229 ssize_t written;
1230 while (len) {
1231 written = write(fd, message, len);
1232 if (written > 0) {
1233 message += written;
1234 len -= written;
1235 } else {
1236 if (written != -1 || errno != EINTR) break;
1241 /* Helper function to send an error message plus a text for errno to a file descriptor and terminate the process */
1242 static void moonbr_child_emergency_error(int fd, char *message) {
1243 int errno2 = errno;
1244 moonbr_child_emergency_print(fd, message);
1245 moonbr_child_emergency_print(fd, ": ");
1246 moonbr_child_emergency_print(fd, strerror(errno2));
1247 moonbr_child_emergency_print(fd, "\n");
1248 exit(1);
1251 /* Creates a child process and (in case of success) registers it in the 'struct moonbr_pool' structure */
1252 static int moonbr_create_worker(struct moonbr_pool *pool, lua_State *L) {
1253 struct moonbr_worker *worker;
1254 worker = calloc(1, sizeof(struct moonbr_worker));
1255 if (!worker) {
1256 moonbr_log(LOG_CRIT, "Memory allocation error");
1257 return -1;
1259 worker->pool = pool;
1261 int controlfds[2];
1262 int errorfds[2];
1263 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, controlfds)) {
1264 moonbr_log(LOG_ERR, "Could not create control socket pair for communcation with child process: %s", strerror(errno));
1265 free(worker);
1266 return -1;
1268 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, errorfds)) {
1269 moonbr_log(LOG_ERR, "Could not create socket pair to redirect stderr of child process: %s", strerror(errno));
1270 close(controlfds[0]);
1271 close(controlfds[1]);
1272 free(worker);
1273 return -1;
1275 if (moonbr_logfile && fflush(moonbr_logfile)) {
1276 moonbr_log(LOG_CRIT, "Could not flush log file prior to forking: %s", strerror(errno));
1277 moonbr_terminate_error();
1279 worker->pid = fork();
1280 if (worker->pid == -1) {
1281 moonbr_log(LOG_ERR, "Could not fork: %s", strerror(errno));
1282 close(controlfds[0]);
1283 close(controlfds[1]);
1284 close(errorfds[0]);
1285 close(errorfds[1]);
1286 free(worker);
1287 return -1;
1288 } else if (!worker->pid) {
1289 moonbr_pstate = MOONBR_PSTATE_FORKED;
1290 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
1291 lua_pushliteral(L, "Failed to pass error message due to bug in Lua panic handler (hint: not enough memory?)");
1292 #endif
1293 moonbr_memory_limit = pool->memory_limit;
1294 if (moonbr_pidfh && pidfile_close(moonbr_pidfh)) {
1295 moonbr_child_emergency_error(errorfds[1], "Could not close PID file in forked child process");
1297 if (moonbr_logfile && moonbr_logfile != stderr && fclose(moonbr_logfile)) {
1298 moonbr_child_emergency_error(errorfds[1], "Could not close log file in forked child process");
1300 if (dup2(errorfds[1], MOONBR_FD_STDERR) == -1) {
1301 moonbr_child_emergency_error(errorfds[1], "Could not duplicate socket to stderr file descriptor");
1303 if (dup2(controlfds[1], MOONBR_FD_CONTROL) == -1) {
1304 moonbr_child_emergency_error(errorfds[1], "Could not duplicate control socket");
1306 closefrom(MOONBR_FD_END);
1307 moonbr_child_run(pool, L);
1309 if (moonbr_stat) {
1310 moonbr_log(LOG_INFO, "Created new worker in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1312 worker->controlfd = controlfds[0];
1313 worker->errorfd = errorfds[0];
1314 if (close(controlfds[1]) && errno != EINTR) {
1315 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
1316 moonbr_terminate_error();
1318 if (close(errorfds[1]) && errno != EINTR) {
1319 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
1320 moonbr_terminate_error();
1323 worker->prev_worker = pool->last_worker;
1324 if (worker->prev_worker) worker->prev_worker->next_worker = worker;
1325 else pool->first_worker = worker;
1326 pool->last_worker = worker;
1327 pool->unassigned_worker_count++;
1328 pool->total_worker_count++;
1329 pool->worker_count_stat = 1;
1330 moonbr_poll_refresh_needed = 1;
1331 return 0; /* return zero only in case of success */
1335 /*** Functions to handle previously created 'struct moonbr_worker' structures ***/
1337 #define moonbr_try_destroy_worker_stat(str, field) \
1338 moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: " str " %li", worker->pool->poolnum, (int)worker->pid, (long)childusage.field);
1340 /* Destroys a worker structure if socket connections have been closed and child process has terminated */
1341 static int moonbr_try_destroy_worker(struct moonbr_worker *worker) {
1342 if (worker->controlfd != -1 || worker->errorfd != -1) return MOONBR_DESTROY_NONE;
1344 int childstatus;
1345 struct rusage childusage;
1347 pid_t waitedpid;
1348 while (
1349 (waitedpid = wait4(worker->pid, &childstatus, WNOHANG, &childusage)) == -1
1350 ) {
1351 if (errno != EINTR) {
1352 moonbr_log(LOG_CRIT, "Error in wait4() call: %s", strerror(errno));
1353 moonbr_terminate_error();
1356 if (!waitedpid) return 0; /* return 0 if worker couldn't be destroyed */
1357 if (waitedpid != worker->pid) {
1358 moonbr_log(LOG_CRIT, "Wrong PID returned by wait4() call");
1359 moonbr_terminate_error();
1362 if (WIFEXITED(childstatus)) {
1363 if (WEXITSTATUS(childstatus) || moonbr_stat) {
1364 moonbr_log(
1365 WEXITSTATUS(childstatus) ? LOG_WARNING : LOG_INFO,
1366 "Child process in pool #%i with PID %i returned with exit code %i", worker->pool->poolnum, (int)worker->pid, WEXITSTATUS(childstatus)
1367 );
1369 } else if (WIFSIGNALED(childstatus)) {
1370 if (WCOREDUMP(childstatus)) {
1371 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));
1372 } else if (WTERMSIG(childstatus) == SIGALRM) {
1373 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i exited prematurely due to timeout", worker->pool->poolnum, (int)worker->pid);
1374 } else {
1375 moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
1377 } else {
1378 moonbr_log(LOG_CRIT, "Illegal exit status from child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1379 moonbr_terminate_error();
1381 if (moonbr_stat) {
1382 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));
1383 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));
1384 moonbr_try_destroy_worker_stat("max resident set size", ru_maxrss);
1385 moonbr_try_destroy_worker_stat("integral shared memory size", ru_ixrss);
1386 moonbr_try_destroy_worker_stat("integral unshared data", ru_idrss);
1387 moonbr_try_destroy_worker_stat("integral unshared stack", ru_isrss);
1388 moonbr_try_destroy_worker_stat("page replaims", ru_minflt);
1389 moonbr_try_destroy_worker_stat("page faults", ru_majflt);
1390 moonbr_try_destroy_worker_stat("swaps", ru_nswap);
1391 moonbr_try_destroy_worker_stat("block input operations", ru_inblock);
1392 moonbr_try_destroy_worker_stat("block output operations", ru_oublock);
1393 moonbr_try_destroy_worker_stat("messages sent", ru_msgsnd);
1394 moonbr_try_destroy_worker_stat("messages received", ru_msgrcv);
1395 moonbr_try_destroy_worker_stat("signals received", ru_nsignals);
1396 moonbr_try_destroy_worker_stat("voluntary context switches", ru_nvcsw);
1397 moonbr_try_destroy_worker_stat("involuntary context switches", ru_nivcsw);
1401 int retval = (
1402 (worker->idle || worker->assigned) ?
1403 MOONBR_DESTROY_IDLE_OR_ASSIGNED :
1404 MOONBR_DESTROY_PREPARE
1405 );
1406 if (worker->prev_worker) worker->prev_worker->next_worker = worker->next_worker;
1407 else worker->pool->first_worker = worker->next_worker;
1408 if (worker->next_worker) worker->next_worker->prev_worker = worker->prev_worker;
1409 else worker->pool->last_worker = worker->prev_worker;
1410 if (worker->idle) {
1411 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker->next_idle_worker;
1412 else worker->pool->first_idle_worker = worker->next_idle_worker;
1413 if (worker->next_idle_worker) worker->next_idle_worker->prev_idle_worker = worker->prev_idle_worker;
1414 else worker->pool->last_idle_worker = worker->prev_idle_worker;
1415 worker->pool->idle_worker_count--;
1417 if (!worker->assigned) worker->pool->unassigned_worker_count--;
1418 worker->pool->total_worker_count--;
1419 worker->pool->worker_count_stat = 1;
1420 if (worker->errorlinebuf) free(worker->errorlinebuf);
1421 free(worker);
1422 return retval;
1426 /* Marks a worker as idle and stores it in a queue, optionally setting 'idle_expiration' value */
1427 static void moonbr_add_idle_worker(struct moonbr_worker *worker) {
1428 worker->prev_idle_worker = worker->pool->last_idle_worker;
1429 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker;
1430 else worker->pool->first_idle_worker = worker;
1431 worker->pool->last_idle_worker = worker;
1432 worker->idle = 1;
1433 worker->pool->idle_worker_count++;
1434 if (worker->assigned) {
1435 worker->assigned = 0;
1436 worker->pool->unassigned_worker_count++;
1438 worker->pool->worker_count_stat = 1;
1439 if (timerisset(&worker->pool->idle_timeout)) {
1440 struct timeval now;
1441 moonbr_now(&now);
1442 timeradd(&now, &worker->pool->idle_timeout, &worker->idle_expiration);
1446 /* Pops a worker from the queue of idle workers (idle queue must not be empty) */
1447 static struct moonbr_worker *moonbr_pop_idle_worker(struct moonbr_pool *pool) {
1448 struct moonbr_worker *worker;
1449 worker = pool->first_idle_worker;
1450 pool->first_idle_worker = worker->next_idle_worker;
1451 if (pool->first_idle_worker) pool->first_idle_worker->prev_idle_worker = NULL;
1452 else pool->last_idle_worker = NULL;
1453 worker->next_idle_worker = NULL;
1454 worker->idle = 0;
1455 worker->pool->idle_worker_count--;
1456 worker->assigned = 1;
1457 worker->pool->unassigned_worker_count--;
1458 worker->pool->worker_count_stat = 1;
1459 return worker;
1463 /*** Functions for queues of 'struct moonbr_listener' ***/
1465 /* Appends a 'struct moonbr_listener' to the queue of idle listeners and registers it for poll() */
1466 static void moonbr_add_idle_listener(struct moonbr_listener *listener) {
1467 listener->prev_listener = listener->pool->last_idle_listener;
1468 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
1469 else listener->pool->first_idle_listener = listener;
1470 listener->pool->last_idle_listener = listener;
1471 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events |= POLLIN;
1474 /* Removes a 'struct moonbr_listener' from the queue of idle listeners and unregisters it from poll() */
1475 static void moonbr_remove_idle_listener(struct moonbr_listener *listener) {
1476 if (listener->prev_listener) listener->prev_listener->next_listener = listener->next_listener;
1477 else listener->pool->first_idle_listener = listener->next_listener;
1478 if (listener->next_listener) listener->next_listener->prev_listener = listener->prev_listener;
1479 else listener->pool->last_idle_listener = listener->prev_listener;
1480 listener->prev_listener = NULL;
1481 listener->next_listener = NULL;
1482 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events &= ~POLLIN;
1485 /* Adds a listener to the queue of connected listeners (i.e. waiting to have their incoming connection accepted) */
1486 static void moonbr_add_connected_listener(struct moonbr_listener *listener) {
1487 listener->prev_listener = listener->pool->last_connected_listener;
1488 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
1489 else listener->pool->first_connected_listener = listener;
1490 listener->pool->last_connected_listener = listener;
1493 /* Removes and returns the first connected listener in the queue */
1494 static struct moonbr_listener *moonbr_pop_connected_listener(struct moonbr_pool *pool) {
1495 struct moonbr_listener *listener = pool->first_connected_listener;
1496 listener->pool->first_connected_listener = listener->next_listener;
1497 if (listener->pool->first_connected_listener) listener->pool->first_connected_listener->prev_listener = NULL;
1498 else listener->pool->last_connected_listener = NULL;
1499 listener->next_listener = NULL;
1500 return listener;
1504 /*** Functions to handle polling ***/
1506 /* Returns an index to a new initialized entry in moonbr_poll_fds[] */
1507 int moonbr_poll_fds_nextindex() {
1508 if (moonbr_poll_fds_count >= moonbr_poll_fds_bufsize) {
1509 if (moonbr_poll_fds_bufsize) moonbr_poll_fds_bufsize *= 2;
1510 else moonbr_poll_fds_bufsize = 1;
1511 moonbr_poll_fds = realloc(
1512 moonbr_poll_fds, moonbr_poll_fds_bufsize * sizeof(struct pollfd)
1513 );
1514 if (!moonbr_poll_fds) {
1515 moonbr_log(LOG_CRIT, "Memory allocation error");
1516 moonbr_terminate_error();
1519 moonbr_poll_fds[moonbr_poll_fds_count] = (struct pollfd){0, };
1520 return moonbr_poll_fds_count++;
1523 /* Returns an index to a new initialized entry in moonbr_poll_workers[] */
1524 int moonbr_poll_workers_nextindex() {
1525 if (moonbr_poll_worker_count >= moonbr_poll_workers_bufsize) {
1526 if (moonbr_poll_workers_bufsize) moonbr_poll_workers_bufsize *= 2;
1527 else moonbr_poll_workers_bufsize = 1;
1528 moonbr_poll_workers = realloc(
1529 moonbr_poll_workers, moonbr_poll_workers_bufsize * sizeof(struct moonbr_poll_worker)
1530 );
1531 if (!moonbr_poll_workers) {
1532 moonbr_log(LOG_CRIT, "Memory allocation error");
1533 moonbr_terminate_error();
1536 moonbr_poll_workers[moonbr_poll_worker_count] = (struct moonbr_poll_worker){0, };
1537 return moonbr_poll_worker_count++;
1540 /* Queues all listeners as idle, and initializes static part of moonbr_poll_fds[], which is passed to poll() */
1541 static void moonbr_poll_init() {
1542 if (socketpair(
1543 PF_LOCAL,
1544 SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1545 0,
1546 moonbr_poll_signalfds
1547 )) {
1548 moonbr_log(LOG_CRIT, "Could not create socket pair for signal delivery during polling: %s", strerror(errno));
1549 moonbr_terminate_error();
1552 int j = moonbr_poll_fds_nextindex();
1553 struct pollfd *pollfd = &moonbr_poll_fds[j];
1554 pollfd->fd = moonbr_poll_signalfd_read;
1555 pollfd->events = POLLIN;
1558 struct moonbr_pool *pool;
1559 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1560 int i;
1561 for (i=0; i<pool->listener_count; i++) {
1562 struct moonbr_listener *listener = &pool->listener[i];
1563 if (listener->listenfd != -1) {
1564 int j = moonbr_poll_fds_nextindex();
1565 listener->pollidx = j;
1566 moonbr_poll_fds[j].fd = listener->listenfd;
1568 moonbr_add_idle_listener(listener);
1572 moonbr_poll_fds_static_count = moonbr_poll_fds_count; /* remember size of static part of array */
1575 /* Disables polling of all listeners (required for clean shutdown) */
1576 static void moonbr_poll_shutdown() {
1577 int i;
1578 for (i=1; i<moonbr_poll_fds_static_count; i++) {
1579 moonbr_poll_fds[i].fd = -1;
1583 /* (Re)builds dynamic part of moonbr_poll_fds[] array, and (re)builds moonbr_poll_workers[] array */
1584 static void moonbr_poll_refresh() {
1585 moonbr_poll_refresh_needed = 0;
1586 moonbr_poll_fds_count = moonbr_poll_fds_static_count;
1587 moonbr_poll_worker_count = 0;
1589 struct moonbr_pool *pool;
1590 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1591 struct moonbr_worker *worker;
1592 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
1593 if (worker->controlfd != -1) {
1594 int j = moonbr_poll_fds_nextindex();
1595 int k = moonbr_poll_workers_nextindex();
1596 struct pollfd *pollfd = &moonbr_poll_fds[j];
1597 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1598 pollfd->fd = worker->controlfd;
1599 pollfd->events = POLLIN;
1600 poll_worker->channel = MOONBR_POLL_WORKER_CONTROLCHANNEL;
1601 poll_worker->worker = worker;
1603 if (worker->errorfd != -1) {
1604 int j = moonbr_poll_fds_nextindex();
1605 int k = moonbr_poll_workers_nextindex();
1606 struct pollfd *pollfd = &moonbr_poll_fds[j];
1607 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1608 pollfd->fd = worker->errorfd;
1609 pollfd->events = POLLIN;
1610 poll_worker->channel = MOONBR_POLL_WORKER_ERRORCHANNEL;
1611 poll_worker->worker = worker;
1618 /* resets socket and 'revents' field of moonbr_poll_fds[] for signal delivery just before poll() is called */
1619 static void moonbr_poll_reset_signal() {
1620 ssize_t readcount;
1621 char buf[1];
1622 moonbr_poll_fds[0].revents = 0;
1623 while ((readcount = read(moonbr_poll_signalfd_read, buf, 1)) < 0) {
1624 if (errno == EAGAIN) break;
1625 if (errno != EINTR) {
1626 moonbr_log(LOG_CRIT, "Error while reading from signal delivery socket: %s", strerror(errno));
1627 moonbr_terminate_error();
1630 if (!readcount) {
1631 moonbr_log(LOG_CRIT, "Unexpected EOF when reading from signal delivery socket: %s", strerror(errno));
1632 moonbr_terminate_error();
1637 /*** Shutdown initiation ***/
1639 /* Sets global variable 'moonbr_shutdown_in_progress', closes listeners, and demands worker termination */
1640 static void moonbr_initiate_shutdown() {
1641 struct moonbr_pool *pool;
1642 int i;
1643 if (moonbr_shutdown_in_progress) {
1644 moonbr_log(LOG_NOTICE, "Shutdown already in progress");
1645 return;
1647 moonbr_shutdown_in_progress = 1;
1648 moonbr_log(LOG_NOTICE, "Initiate shutdown");
1649 for (pool = moonbr_first_pool; pool; pool = pool->next_pool) {
1650 for (i=0; i<pool->listener_count; i++) {
1651 struct moonbr_listener *listener = &pool->listener[i];
1652 if (listener->listenfd != -1) {
1653 if (close(listener->listenfd) && errno != EINTR) {
1654 moonbr_log(LOG_CRIT, "Could not close listening socket: %s", strerror(errno));
1655 moonbr_terminate_error();
1659 pool->pre_fork = 0;
1660 pool->min_fork = 0;
1661 pool->max_fork = 0;
1662 timerclear(&pool->exit_delay);
1664 moonbr_poll_shutdown(); /* avoids loops due to error condition when polling closed listeners */
1668 /*** Functions to communicate with child processes ***/
1670 /* Tells child process to terminate */
1671 static void moonbr_terminate_idle_worker(struct moonbr_worker *worker) {
1672 moonbr_send_control_message(worker, MOONBR_COMMAND_TERMINATE, -1, NULL);
1675 /* Handles status messages from child process */
1676 static void moonbr_read_controlchannel(struct moonbr_worker *worker) {
1677 char controlmsg;
1679 ssize_t bytes_read;
1680 while ((bytes_read = read(worker->controlfd, &controlmsg, 1)) <= 0) {
1681 if (bytes_read == 0 || errno == ECONNRESET) {
1682 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i unexpectedly closed control socket", worker->pool->poolnum, (int)worker->pid);
1683 if (close(worker->controlfd) && errno != EINTR) {
1684 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));
1685 moonbr_terminate_error();
1687 worker->controlfd = -1;
1688 moonbr_poll_refresh_needed = 1;
1689 return;
1691 if (errno != EINTR) {
1692 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));
1693 moonbr_terminate_error();
1697 if (worker->idle) {
1698 moonbr_log(LOG_CRIT, "Unexpected data from supposedly idle child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1699 moonbr_terminate_error();
1701 if (moonbr_debug) {
1702 moonbr_log(LOG_DEBUG, "Received control message from child in pool #%i with PID %i: \"%c\"", worker->pool->poolnum, (int)worker->pid, (int)controlmsg);
1704 switch (controlmsg) {
1705 case MOONBR_STATUS_IDLE:
1706 if (moonbr_stat) {
1707 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i reports as idle", worker->pool->poolnum, (int)worker->pid);
1709 moonbr_add_idle_worker(worker);
1710 break;
1711 case MOONBR_STATUS_GOODBYE:
1712 if (moonbr_stat) {
1713 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i announced termination", worker->pool->poolnum, (int)worker->pid);
1715 if (close(worker->controlfd) && errno != EINTR) {
1716 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));
1717 moonbr_terminate_error();
1719 worker->controlfd = -1;
1720 moonbr_poll_refresh_needed = 1;
1721 break;
1722 default:
1723 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);
1724 moonbr_terminate_error();
1728 /* Handles stderr stream from child process */
1729 static void moonbr_read_errorchannel(struct moonbr_worker *worker) {
1730 char staticbuf[MOONBR_MAXERRORLINELEN+1];
1731 char *buf = worker->errorlinebuf;
1732 if (!buf) buf = staticbuf;
1734 ssize_t bytes_read;
1735 while (
1736 (bytes_read = read(
1737 worker->errorfd,
1738 buf + worker->errorlinelen,
1739 MOONBR_MAXERRORLINELEN+1 - worker->errorlinelen
1740 )) <= 0
1741 ) {
1742 if (bytes_read == 0 || errno == ECONNRESET) {
1743 if (moonbr_debug) {
1744 moonbr_log(LOG_DEBUG, "Child process in pool #%i with PID %i closed stderr socket", worker->pool->poolnum, (int)worker->pid);
1746 if (close(worker->errorfd) && errno != EINTR) {
1747 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));
1748 moonbr_terminate_error();
1750 worker->errorfd = -1;
1751 moonbr_poll_refresh_needed = 1;
1752 break;
1754 if (errno != EINTR) {
1755 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));
1756 moonbr_terminate_error();
1759 worker->errorlinelen += bytes_read;
1762 int i;
1763 for (i=0; i<worker->errorlinelen; i++) {
1764 if (buf[i] == '\n') buf[i] = 0;
1765 if (!buf[i]) {
1766 if (worker->errorlineovf) {
1767 worker->errorlineovf = 0;
1768 } else {
1769 moonbr_log(LOG_WARNING, "Error log from process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, buf);
1771 worker->errorlinelen -= i+1;
1772 memmove(buf, buf+i+1, worker->errorlinelen);
1773 i = -1;
1776 if (i > MOONBR_MAXERRORLINELEN) {
1777 buf[MOONBR_MAXERRORLINELEN] = 0;
1778 if (!worker->errorlineovf) {
1779 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);
1781 worker->errorlinelen = 0;
1782 worker->errorlineovf = 1;
1785 if (!worker->errorlinebuf && worker->errorlinelen) { /* allocate buffer on heap only if necessary */
1786 worker->errorlinebuf = malloc((MOONBR_MAXERRORLINELEN+1) * sizeof(char));
1787 if (!worker->errorlinebuf) {
1788 moonbr_log(LOG_CRIT, "Memory allocation error");
1789 moonbr_terminate_error();
1791 memcpy(worker->errorlinebuf, staticbuf, worker->errorlinelen);
1796 /*** Handler for incoming connections ***/
1798 /* Accepts one or more incoming connections on listener socket and passes it to worker(s) popped from idle queue */
1799 static void moonbr_connect(struct moonbr_pool *pool) {
1800 struct moonbr_listener *listener = moonbr_pop_connected_listener(pool);
1801 struct moonbr_worker *worker;
1802 switch (listener->proto) {
1803 case MOONBR_PROTO_INTERVAL:
1804 worker = moonbr_pop_idle_worker(pool);
1805 if (moonbr_stat) {
1806 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);
1808 worker->restart_interval_listener = listener;
1809 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_INTERVAL, -1, listener);
1810 /* do not push listener to queue of idle listeners yet */
1811 break;
1812 case MOONBR_PROTO_LOCAL:
1813 do {
1814 int peerfd;
1815 struct sockaddr_un peeraddr;
1816 socklen_t peeraddr_len = sizeof(struct sockaddr_un);
1817 peerfd = accept4(
1818 listener->listenfd,
1819 (struct sockaddr *)&peeraddr,
1820 &peeraddr_len,
1821 SOCK_CLOEXEC
1822 );
1823 if (peerfd == -1) {
1824 if (errno == EWOULDBLOCK) {
1825 break;
1826 } else if (errno == ECONNABORTED) {
1827 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"local\", path=\"%s\")", listener->proto_specific.local.path);
1828 break;
1829 } else if (errno != EINTR) {
1830 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1831 moonbr_terminate_error();
1833 } else {
1834 worker = moonbr_pop_idle_worker(pool);
1835 if (moonbr_stat) {
1836 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);
1838 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_LOCAL, peerfd, listener);
1839 if (close(peerfd) && errno != EINTR) {
1840 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1841 moonbr_terminate_error();
1844 } while (pool->first_idle_worker);
1845 moonbr_add_idle_listener(listener);
1846 break;
1847 case MOONBR_PROTO_TCP6:
1848 do {
1849 int peerfd;
1850 struct sockaddr_in6 peeraddr;
1851 socklen_t peeraddr_len = sizeof(struct sockaddr_in6);
1852 peerfd = accept4(
1853 listener->listenfd,
1854 (struct sockaddr *)&peeraddr,
1855 &peeraddr_len,
1856 SOCK_CLOEXEC
1857 );
1858 if (peerfd == -1) {
1859 if (errno == EWOULDBLOCK) {
1860 break;
1861 } else if (errno == ECONNABORTED) {
1862 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"tcp6\", port=%i)", listener->proto_specific.tcp.port);
1863 break;
1864 } else if (errno != EINTR) {
1865 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1866 moonbr_terminate_error();
1868 } else {
1869 worker = moonbr_pop_idle_worker(pool);
1870 if (moonbr_stat) {
1871 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);
1873 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_NETWORK, peerfd, listener);
1874 if (close(peerfd) && errno != EINTR) {
1875 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1876 moonbr_terminate_error();
1879 } while (pool->first_idle_worker);
1880 moonbr_add_idle_listener(listener);
1881 break;
1882 case MOONBR_PROTO_TCP4:
1883 do {
1884 int peerfd;
1885 struct sockaddr_in peeraddr;
1886 socklen_t peeraddr_len = sizeof(struct sockaddr_in);
1887 peerfd = accept4(
1888 listener->listenfd,
1889 (struct sockaddr *)&peeraddr,
1890 &peeraddr_len,
1891 SOCK_CLOEXEC
1892 );
1893 if (peerfd == -1) {
1894 if (errno == EWOULDBLOCK) {
1895 break;
1896 } else if (errno == ECONNABORTED) {
1897 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"tcp4\", port=%i)", listener->proto_specific.tcp.port);
1898 break;
1899 } else if (errno != EINTR) {
1900 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1901 moonbr_terminate_error();
1903 } else {
1904 worker = moonbr_pop_idle_worker(pool);
1905 if (moonbr_stat) {
1906 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);
1908 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_NETWORK, peerfd, listener);
1909 if (close(peerfd) && errno != EINTR) {
1910 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1911 moonbr_terminate_error();
1914 } while (pool->first_idle_worker);
1915 moonbr_add_idle_listener(listener);
1916 break;
1917 default:
1918 moonbr_log(LOG_ERR, "Internal error (should not happen): Unexpected value in listener.proto field");
1919 moonbr_terminate_error();
1924 /*** Functions to initialize and restart interval timers ***/
1926 /* Initializes all interval timers */
1927 static void moonbr_interval_initialize() {
1928 struct timeval now;
1929 struct moonbr_pool *pool;
1930 moonbr_now(&now);
1931 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1932 int i;
1933 for (i=0; i<pool->listener_count; i++) {
1934 struct moonbr_listener *listener = &pool->listener[i];
1935 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1936 timeradd(
1937 &now,
1938 &listener->proto_specific.interval.delay,
1939 &listener->proto_specific.interval.wakeup
1940 );
1946 /* If necessary, restarts interval timers and queues interval listener as idle after a worker changed status */
1947 static void moonbr_interval_restart(
1948 struct moonbr_worker *worker,
1949 struct timeval *now /* passed to synchronize with moonbr_run() function */
1950 ) {
1951 struct moonbr_listener *listener = worker->restart_interval_listener;
1952 if (listener) {
1953 moonbr_add_idle_listener(listener);
1954 worker->restart_interval_listener = NULL;
1955 if (listener->proto_specific.interval.strict) {
1956 timeradd(
1957 &listener->proto_specific.interval.wakeup,
1958 &listener->proto_specific.interval.delay,
1959 &listener->proto_specific.interval.wakeup
1960 );
1961 if (timercmp(&listener->proto_specific.interval.wakeup, now, <)) {
1962 listener->proto_specific.interval.wakeup = *now;
1964 } else {
1965 timeradd(
1966 now,
1967 &listener->proto_specific.interval.delay,
1968 &listener->proto_specific.interval.wakeup
1969 );
1975 /*** Main loop and helper functions ***/
1977 /* Stores the earliest required wakeup time in 'wait' variable */
1978 static void moonbr_calc_wait(struct timeval *wait, struct timeval *wakeup) {
1979 if (!timerisset(wait) || timercmp(wakeup, wait, <)) *wait = *wakeup;
1982 /* Main loop of Moonbridge system (including initialization of signal handlers and polling structures) */
1983 static void moonbr_run(lua_State *L) {
1984 struct timeval now;
1985 struct moonbr_pool *pool;
1986 struct moonbr_worker *worker;
1987 struct moonbr_worker *next_worker; /* needed when worker is removed during iteration of workers */
1988 struct moonbr_listener *listener;
1989 struct moonbr_listener *next_listener; /* needed when listener is removed during iteration of listeners */
1990 int i;
1991 moonbr_poll_init(); /* must be executed before moonbr_signal_init() */
1992 moonbr_signal_init();
1993 moonbr_interval_initialize();
1994 moonbr_pstate = MOONBR_PSTATE_RUNNING;
1995 while (1) {
1996 struct timeval wait = {0, }; /* point in time when premature wakeup of poll() is required */
1997 if (moonbr_cond_interrupt) {
1998 moonbr_log(LOG_WARNING, "Fast shutdown requested");
1999 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
2001 if (moonbr_cond_terminate) {
2002 moonbr_initiate_shutdown();
2003 moonbr_cond_terminate = 0;
2005 moonbr_cond_child = 0; /* must not be reset between moonbr_try_destroy_worker() and poll() */
2006 moonbr_now(&now);
2007 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
2008 int terminated_worker_count = 0; /* allows shortcut for new worker creation */
2009 /* terminate idle workers when expired */
2010 if (timerisset(&pool->idle_timeout)) {
2011 while ((worker = pool->first_idle_worker) != NULL) {
2012 if (timercmp(&worker->idle_expiration, &now, >)) break;
2013 moonbr_pop_idle_worker(pool);
2014 moonbr_terminate_idle_worker(worker);
2017 /* mark listeners as connected when incoming connection is pending */
2018 for (listener=pool->first_idle_listener; listener; listener=next_listener) {
2019 next_listener = listener->next_listener; /* extra variable necessary due to changing list */
2020 if (listener->pollidx != -1) {
2021 if (moonbr_poll_fds[listener->pollidx].revents) {
2022 moonbr_poll_fds[listener->pollidx].revents = 0;
2023 moonbr_remove_idle_listener(listener);
2024 moonbr_add_connected_listener(listener);
2026 } else if (listener->proto == MOONBR_PROTO_INTERVAL) {
2027 if (!timercmp(&listener->proto_specific.interval.wakeup, &now, >)) {
2028 moonbr_remove_idle_listener(listener);
2029 moonbr_add_connected_listener(listener);
2031 } else {
2032 moonbr_log(LOG_CRIT, "Internal error (should not happen): Listener is neither an interval timer nor has the 'pollidx' value set");
2033 moonbr_terminate_error();
2036 /* process input from child processes */
2037 for (i=0; i<moonbr_poll_worker_count; i++) {
2038 if (moonbr_poll_worker_fds[i].revents) {
2039 moonbr_poll_worker_fds[i].revents = 0;
2040 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[i];
2041 switch (poll_worker->channel) {
2042 case MOONBR_POLL_WORKER_CONTROLCHANNEL:
2043 moonbr_read_controlchannel(poll_worker->worker);
2044 moonbr_interval_restart(poll_worker->worker, &now);
2045 break;
2046 case MOONBR_POLL_WORKER_ERRORCHANNEL:
2047 moonbr_read_errorchannel(poll_worker->worker);
2048 break;
2052 /* collect dead child processes */
2053 for (worker=pool->first_worker; worker; worker=next_worker) {
2054 next_worker = worker->next_worker; /* extra variable necessary due to changing list */
2055 switch (moonbr_try_destroy_worker(worker)) {
2056 case MOONBR_DESTROY_PREPARE:
2057 pool->use_fork_error_wakeup = 1;
2058 break;
2059 case MOONBR_DESTROY_IDLE_OR_ASSIGNED:
2060 terminated_worker_count++;
2061 break;
2064 /* connect listeners with idle workers */
2065 if (!moonbr_shutdown_in_progress) {
2066 while (pool->first_connected_listener && pool->first_idle_worker) {
2067 moonbr_connect(pool);
2070 /* create new worker processes */
2071 while (
2072 pool->total_worker_count < pool->max_fork && (
2073 pool->unassigned_worker_count < pool->pre_fork ||
2074 pool->total_worker_count < pool->min_fork
2076 ) {
2077 if (pool->use_fork_error_wakeup) {
2078 if (timercmp(&pool->fork_error_wakeup, &now, >)) {
2079 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
2080 break;
2082 } else {
2083 if (terminated_worker_count) {
2084 terminated_worker_count--;
2085 } else if (timercmp(&pool->fork_wakeup, &now, >)) {
2086 moonbr_calc_wait(&wait, &pool->fork_wakeup);
2087 break;
2090 if (moonbr_create_worker(pool, L)) {
2091 /* on error, enforce error delay */
2092 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
2093 pool->use_fork_error_wakeup = 1;
2094 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
2095 break;
2096 } else {
2097 /* normal fork delay on success */
2098 timeradd(&now, &pool->fork_delay, &pool->fork_wakeup);
2099 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
2100 pool->use_fork_error_wakeup = 0; /* gets set later if error occures during preparation */
2103 /* terminate excessive worker processes */
2104 while (
2105 pool->total_worker_count > pool->min_fork &&
2106 pool->idle_worker_count > pool->pre_fork
2107 ) {
2108 if (timerisset(&pool->exit_wakeup)) {
2109 if (timercmp(&pool->exit_wakeup, &now, >)) {
2110 moonbr_calc_wait(&wait, &pool->exit_wakeup);
2111 break;
2113 moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
2114 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
2115 } else {
2116 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
2117 break;
2120 if (!(
2121 pool->total_worker_count > pool->min_fork &&
2122 pool->idle_worker_count > pool->pre_fork
2123 )) {
2124 timerclear(&pool->exit_wakeup); /* timer gets restarted later when there are excessive workers */
2126 /* optionally output worker count stats */
2127 if (moonbr_stat && pool->worker_count_stat) {
2128 pool->worker_count_stat = 0;
2129 moonbr_log(
2130 LOG_INFO,
2131 "Worker count for pool #%i: %i idle, %i assigned, %i total",
2132 pool->poolnum, pool->idle_worker_count,
2133 pool->total_worker_count - pool->unassigned_worker_count,
2134 pool->total_worker_count);
2136 /* calculate wakeup time for interval listeners */
2137 for (listener=pool->first_idle_listener; listener; listener=listener->next_listener) {
2138 if (listener->proto == MOONBR_PROTO_INTERVAL) {
2139 moonbr_calc_wait(&wait, &listener->proto_specific.interval.wakeup);
2142 /* calculate wakeup time for idle workers (only first idle worker is significant) */
2143 if (timerisset(&pool->idle_timeout) && pool->first_idle_worker) {
2144 moonbr_calc_wait(&wait, &pool->first_idle_worker->idle_expiration);
2147 /* check if shutdown is complete */
2148 if (moonbr_shutdown_in_progress) {
2149 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
2150 if (pool->first_worker) break;
2152 if (!pool) {
2153 moonbr_log(LOG_INFO, "All worker threads have terminated");
2154 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
2157 if (moonbr_poll_refresh_needed) moonbr_poll_refresh();
2158 moonbr_cond_poll = 1;
2159 if (!moonbr_cond_child && !moonbr_cond_terminate && !moonbr_cond_interrupt) {
2160 int timeout;
2161 if (timerisset(&wait)) {
2162 if (timercmp(&wait, &now, <)) {
2163 moonbr_log(LOG_CRIT, "Internal error (should not happen): Future is in the past");
2164 moonbr_terminate_error();
2166 timersub(&wait, &now, &wait);
2167 timeout = wait.tv_sec * 1000 + wait.tv_usec / 1000;
2168 } else {
2169 timeout = INFTIM;
2171 if (moonbr_debug) {
2172 moonbr_log(LOG_DEBUG, "Waiting for I/O");
2174 poll(moonbr_poll_fds, moonbr_poll_fds_count, timeout);
2175 } else {
2176 if (moonbr_debug) {
2177 moonbr_log(LOG_DEBUG, "Do not wait for I/O");
2180 moonbr_cond_poll = 0;
2181 moonbr_poll_reset_signal();
2186 /*** Lua interface ***/
2188 static int moonbr_lua_panic(lua_State *L) {
2189 const char *errmsg;
2190 errmsg = lua_tostring(L, -1);
2191 if (!errmsg) {
2192 if (lua_isnoneornil(L, -1)) errmsg = "(error message is nil)";
2193 else errmsg = "(error message is not a string)";
2195 if (moonbr_pstate == MOONBR_PSTATE_FORKED) {
2196 fprintf(stderr, "Uncaught Lua error: %s\n", errmsg);
2197 exit(1);
2198 } else {
2199 moonbr_log(LOG_CRIT, "Uncaught Lua error: %s", errmsg);
2200 moonbr_terminate_error();
2202 return 0;
2205 static int moonbr_addtraceback(lua_State *L) {
2206 luaL_traceback(L, L, luaL_tolstring(L, 1, NULL), 1);
2207 return 1;
2210 /* Memory allocator that allows limiting memory consumption */
2211 static void *moonbr_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
2212 (void)ud; /* not used */
2213 if (nsize == 0) {
2214 if (ptr) {
2215 moonbr_memory_usage -= osize;
2216 free(ptr);
2218 return NULL;
2219 } else if (ptr) {
2220 if (
2221 moonbr_memory_limit &&
2222 nsize > osize &&
2223 moonbr_memory_usage + (nsize - osize) > moonbr_memory_limit
2224 ) {
2225 return NULL;
2226 } else {
2227 ptr = realloc(ptr, nsize);
2228 if (ptr) moonbr_memory_usage += nsize - osize;
2230 } else {
2231 if (
2232 moonbr_memory_limit &&
2233 moonbr_memory_usage + nsize > moonbr_memory_limit
2234 ) {
2235 return NULL;
2236 } else {
2237 ptr = realloc(ptr, nsize);
2238 if (ptr) moonbr_memory_usage += nsize;
2241 return ptr;
2244 /* New method for Lua file objects: read until terminator or length exceeded */
2245 static int moonbr_readuntil(lua_State *L) {
2246 luaL_Stream *stream;
2247 FILE *file;
2248 const char *terminatorstr;
2249 size_t terminatorlen;
2250 luaL_Buffer buf;
2251 lua_Integer maxlen;
2252 char terminator;
2253 int byte;
2254 stream = luaL_checkudata(L, 1, LUA_FILEHANDLE);
2255 terminatorstr = luaL_checklstring(L, 2, &terminatorlen);
2256 luaL_argcheck(L, terminatorlen == 1, 2, "single byte expected");
2257 maxlen = luaL_optinteger(L, 3, 0);
2258 if (!stream->closef) luaL_error(L, "attempt to use a closed file");
2259 file = stream->f;
2260 luaL_buffinit(L, &buf);
2261 if (!maxlen) maxlen = -1;
2262 terminator = terminatorstr[0];
2263 while (maxlen > 0 ? maxlen-- : maxlen) {
2264 byte = fgetc(file);
2265 if (byte == EOF) {
2266 if (ferror(file)) {
2267 char errmsg[MOONBR_MAXSTRERRORLEN];
2268 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
2269 lua_pushnil(L);
2270 lua_pushstring(L, errmsg);
2271 return 2;
2272 } else {
2273 break;
2276 luaL_addchar(&buf, byte);
2277 if (byte == terminator) break;
2279 luaL_pushresult(&buf);
2280 if (!lua_rawlen(L, -1)) lua_pushnil(L);
2281 return 1;
2284 static int moonbr_lua_tonatural(lua_State *L, int idx) {
2285 int isnum;
2286 lua_Number n;
2287 n = lua_tonumberx(L, idx, &isnum);
2288 if (isnum && n>=0 && n<INT_MAX && (lua_Number)(int)n == n) return n;
2289 else return -1;
2292 static int moonbr_lua_totimeval(lua_State *L, int idx, struct timeval *value) {
2293 int isnum;
2294 lua_Number n;
2295 n = lua_tonumberx(L, idx, &isnum);
2296 if (isnum && n>=0 && n<=100000000) {
2297 value->tv_sec = n;
2298 value->tv_usec = 1e6 * (n - value->tv_sec);
2299 return 1;
2300 } else {
2301 return 0;
2305 static int moonbr_timeout(lua_State *L) {
2306 struct itimerval oldval;
2307 if (lua_isnoneornil(L, 1) && lua_isnoneornil(L, 2)) {
2308 getitimer(ITIMER_REAL, &oldval);
2309 } else {
2310 struct itimerval newval = {};
2311 timerclear(&newval.it_interval);
2312 timerclear(&newval.it_value);
2313 if (lua_toboolean(L, 1)) {
2314 luaL_argcheck(
2315 L, moonbr_lua_totimeval(L, 1, &newval.it_value), 1,
2316 "interval in seconds expected"
2317 );
2319 if (lua_isnoneornil(L, 2)) {
2320 if (setitimer(ITIMER_REAL, &newval, &oldval)) {
2321 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2322 moonbr_terminate_error();
2324 } else {
2325 getitimer(ITIMER_REAL, &oldval);
2326 if (!timerisset(&oldval.it_value)) {
2327 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2328 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2329 moonbr_terminate_error();
2331 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
2332 timerclear(&newval.it_value);
2333 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2334 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2335 moonbr_terminate_error();
2337 } else if (timercmp(&newval.it_value, &oldval.it_value, <)) {
2338 struct itimerval remval;
2339 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2340 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2341 moonbr_terminate_error();
2343 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
2344 getitimer(ITIMER_REAL, &remval);
2345 timersub(&oldval.it_value, &newval.it_value, &newval.it_value);
2346 timeradd(&newval.it_value, &remval.it_value, &newval.it_value);
2347 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2348 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2349 moonbr_terminate_error();
2351 } else {
2352 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
2354 return lua_gettop(L) - 1;
2357 lua_pushnumber(L, oldval.it_value.tv_sec + 1e-6 * oldval.it_value.tv_usec);
2358 return 1;
2361 #define moonbr_listen_init_pool_forkoption(luaname, cname, defval) { \
2362 lua_getfield(L, 2, luaname); \
2363 pool->cname = lua_isnil(L, -1) ? (defval) : moonbr_lua_tonatural(L, -1); \
2364 } while(0)
2366 #define moonbr_listen_init_pool_timeoption(luaname, cname, defval, defvalu) ( \
2367 lua_getfield(L, 2, luaname), \
2368 lua_isnil(L, -1) ? ( \
2369 pool->cname.tv_sec = (defval), pool->cname.tv_usec = (defvalu), \
2370 1 \
2371 ) : ( \
2372 (lua_isboolean(L, -1) && !lua_toboolean(L, -1)) ? ( \
2373 pool->cname.tv_sec = 0, pool->cname.tv_usec = 0, \
2374 1 \
2375 ) : ( \
2376 moonbr_lua_totimeval(L, -1, &pool->cname) \
2377 ) \
2378 ) \
2381 static int moonbr_listen_init_pool(lua_State *L) {
2382 struct moonbr_pool *pool;
2383 const char *proto;
2384 int i;
2385 pool = lua_touserdata(L, 1);
2386 for (i=0; i<pool->listener_count; i++) {
2387 struct moonbr_listener *listener = &pool->listener[i];
2388 lua_settop(L, 2);
2389 #if LUA_VERSION_NUM >= 503
2390 lua_geti(L, 2, i+1);
2391 #else
2392 lua_pushinteger(L, i+1);
2393 lua_gettable(L, 2);
2394 #endif
2395 lua_getfield(L, 3, "proto");
2396 proto = lua_tostring(L, -1);
2397 if (proto && !strcmp(proto, "interval")) {
2398 listener->proto = MOONBR_PROTO_INTERVAL;
2399 lua_getfield(L, 3, "name");
2401 const char *name = lua_tostring(L, -1);
2402 if (name) {
2403 if (asprintf(&listener->proto_specific.interval.name, "%s", name) < 0) {
2404 moonbr_log(LOG_CRIT, "Memory allocation_error");
2405 moonbr_terminate_error();
2409 lua_getfield(L, 3, "delay");
2410 if (
2411 !moonbr_lua_totimeval(L, -1, &listener->proto_specific.interval.delay) ||
2412 !timerisset(&listener->proto_specific.interval.delay)
2413 ) {
2414 luaL_error(L, "No valid interval delay specified; use listen{{proto=\"interval\", delay=...}, ...}");
2416 lua_getfield(L, 3, "strict");
2417 if (!lua_isnil(L, -1)) {
2418 if (lua_isboolean(L, -1)) {
2419 if (lua_toboolean(L, -1)) listener->proto_specific.interval.strict = 1;
2420 } else {
2421 luaL_error(L, "Option \"strict\" must be a boolean if set; use listen{{proto=\"interval\", strict=true, ...}, ...}");
2424 } else if (proto && !strcmp(proto, "local")) {
2425 listener->proto = MOONBR_PROTO_LOCAL;
2426 lua_getfield(L, 3, "path");
2428 const char *path = lua_tostring(L, -1);
2429 if (!path) {
2430 luaL_error(L, "No valid path specified for local socket; use listen{{proto=\"local\", path=...}, ...}");
2432 if (asprintf(&listener->proto_specific.local.path, "%s", path) < 0) {
2433 moonbr_log(LOG_CRIT, "Memory allocation_error");
2434 moonbr_terminate_error();
2437 } else if (proto && !strcmp(proto, "tcp6")) {
2438 listener->proto = MOONBR_PROTO_TCP6;
2439 lua_getfield(L, 3, "port");
2440 listener->proto_specific.tcp.port = lua_tointeger(L, -1);
2441 if (
2442 listener->proto_specific.tcp.port < 1 ||
2443 listener->proto_specific.tcp.port > 65535
2444 ) {
2445 luaL_error(L, "No valid port number specified; use listen{{proto=\"tcp6\", port=...}, ...}");
2447 lua_getfield(L, 3, "localhost");
2448 if (!lua_isnil(L, -1)) {
2449 if (lua_isboolean(L, -1)) {
2450 if (lua_toboolean(L, -1)) listener->proto_specific.tcp.localhost_only = 1;
2451 } else {
2452 luaL_error(L, "Option \"localhost\" must be a boolean if set; use listen{{proto=\"tcp6\", localhost=true, ...}, ...}");
2455 } else if (proto && !strcmp(proto, "tcp4")) {
2456 listener->proto = MOONBR_PROTO_TCP4;
2457 lua_getfield(L, 3, "port");
2458 listener->proto_specific.tcp.port = lua_tointeger(L, -1);
2459 if (
2460 listener->proto_specific.tcp.port < 1 ||
2461 listener->proto_specific.tcp.port > 65535
2462 ) {
2463 luaL_error(L, "No valid port number specified; use listen{{proto=\"tcp4\", port=...}, ...}");
2465 lua_getfield(L, 3, "localhost");
2466 if (!lua_isnil(L, -1)) {
2467 if (lua_isboolean(L, -1)) {
2468 if (lua_toboolean(L, -1)) listener->proto_specific.tcp.localhost_only = 1;
2469 } else {
2470 luaL_error(L, "Option \"localhost\" must be a boolean if set; use listen{{proto=\"tcp4\", localhost=true, ...}, ...}");
2475 lua_settop(L, 2);
2476 moonbr_listen_init_pool_forkoption("pre_fork", pre_fork, 1);
2477 moonbr_listen_init_pool_forkoption("min_fork", min_fork, pool->pre_fork > 2 ? pool->pre_fork : 2);
2478 moonbr_listen_init_pool_forkoption("max_fork", max_fork, pool->min_fork > 16 ? pool->min_fork : 16);
2479 if (!moonbr_listen_init_pool_timeoption("fork_delay", fork_delay, 0, 250000)) {
2480 luaL_error(L, "Option \"fork_delay\" is expected to be a non-negative number");
2482 if (!moonbr_listen_init_pool_timeoption("fork_error_delay", fork_error_delay, 2, 0)) {
2483 luaL_error(L, "Option \"fork_error_delay\" is expected to be a non-negative number");
2485 if (!moonbr_listen_init_pool_timeoption("exit_delay", exit_delay, 60, 0)) {
2486 luaL_error(L, "Option \"exit_delay\" is expected to be a non-negative number");
2488 if (timercmp(&pool->fork_error_delay, &pool->fork_delay, <)) {
2489 pool->fork_error_delay = pool->fork_delay;
2491 if (!moonbr_listen_init_pool_timeoption("idle_timeout", idle_timeout, 0, 0)) {
2492 luaL_error(L, "Option \"idle_timeout\" is expected to be a non-negative number");
2494 lua_getfield(L, 2, "memory_limit");
2495 if (!lua_isnil(L, -1)) {
2496 int isnum;
2497 lua_Number n;
2498 n = lua_tonumberx(L, -1, &isnum);
2499 if (n < 0 || !isnum) {
2500 luaL_error(L, "Option \"memory_limit\" is expected to be a non-negative number");
2502 pool->memory_limit = n;
2504 lua_settop(L, 2);
2505 lua_getfield(L, 2, "prepare");
2506 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2507 luaL_error(L, "Option \"prepare\" must be nil or a function");
2509 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2510 lua_getfield(L, 2, "connect");
2511 if (!lua_isfunction(L, -1)) {
2512 luaL_error(L, "Option \"connect\" must be a function; use listen{{...}, {...}, connect=function(socket) ... end, ...}");
2514 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2515 lua_getfield(L, 2, "finish");
2516 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2517 luaL_error(L, "Option \"finish\" must be nil or a function");
2519 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2520 return 0;
2523 static int moonbr_listen(lua_State *L) {
2524 struct moonbr_pool *pool;
2525 lua_Integer listener_count;
2526 if (moonbr_booted) luaL_error(L, "Moonbridge bootup is already complete");
2527 luaL_checktype(L, 1, LUA_TTABLE);
2528 listener_count = luaL_len(L, 1);
2529 if (!listener_count) luaL_error(L, "No listen ports specified; use listen{{proto=..., port=...},...}");
2530 if (listener_count > 100) luaL_error(L, "Too many listeners");
2531 pool = moonbr_create_pool(listener_count);
2532 lua_pushcfunction(L, moonbr_listen_init_pool);
2533 lua_pushlightuserdata(L, pool);
2534 lua_pushvalue(L, 1);
2535 if (lua_pcall(L, 2, 0, 0)) goto moonbr_listen_error;
2537 int i;
2538 i = moonbr_start_pool(pool);
2539 if (i >= 0) {
2540 struct moonbr_listener *listener = &pool->listener[i];
2541 switch (listener->proto) {
2542 case MOONBR_PROTO_INTERVAL:
2543 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"interval\"): %s", i+1, strerror(errno));
2544 break;
2545 case MOONBR_PROTO_LOCAL:
2546 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"local\", path=\"%s\"): %s", i+1, listener->proto_specific.local.path, strerror(errno));
2547 break;
2548 case MOONBR_PROTO_TCP6:
2549 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"tcp6\", port=%d): %s", i+1, listener->proto_specific.tcp.port, strerror(errno));
2550 break;
2551 case MOONBR_PROTO_TCP4:
2552 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"tcp4\", port=%d): %s", i+1, listener->proto_specific.tcp.port, strerror(errno));
2553 break;
2554 default:
2555 moonbr_log(LOG_ERR, "Internal error (should not happen): Unexpected value in listener.proto field");
2556 moonbr_terminate_error();
2558 goto moonbr_listen_error;
2561 return 0;
2562 moonbr_listen_error:
2563 moonbr_destroy_pool(pool);
2564 lua_pushnil(L);
2565 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2566 lua_pushnil(L);
2567 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2568 lua_pushnil(L);
2569 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2570 lua_error(L);
2571 return 0; /* avoid compiler warning */
2575 /*** Function to modify Lua's library path and/or cpath ***/
2577 #if defined(MOONBR_LUA_PATH) || defined(MOONBR_LUA_CPATH)
2578 static void moonbr_modify_path(lua_State *L, char *key, char *value) {
2579 int stackbase;
2580 stackbase = lua_gettop(L);
2581 lua_getglobal(L, "package");
2582 lua_getfield(L, stackbase+1, key);
2584 const char *current_str;
2585 size_t current_strlen;
2586 luaL_Buffer buf;
2587 current_str = lua_tolstring(L, stackbase+2, &current_strlen);
2588 luaL_buffinit(L, &buf);
2589 if (current_str) {
2590 lua_pushvalue(L, stackbase+2);
2591 luaL_addvalue(&buf);
2592 if (current_strlen && current_str[current_strlen-1] != ';') {
2593 luaL_addchar(&buf, ';');
2596 luaL_addstring(&buf, value);
2597 luaL_pushresult(&buf);
2599 lua_setfield(L, stackbase+1, key);
2600 lua_settop(L, stackbase);
2602 #endif
2605 /*** Main function and command line invokation ***/
2607 static void moonbr_usage(int err, const char *cmd) {
2608 FILE *out;
2609 out = err ? stderr : stdout;
2610 if (!cmd) cmd = "moonbridge";
2611 fprintf(out, "Get this help message: %s {-h|--help}\n", cmd);
2612 fprintf(out, "Usage: %s \\\n", cmd);
2613 fprintf(out, " [-b|--background] \\\n");
2614 fprintf(out, " [-d|--debug] \\\n");
2615 fprintf(out, " [-f|--logfacility {DAEMON|USER|0|1|...|7}] \\\n");
2616 fprintf(out, " [-i|--logident <syslog ident> \\\n");
2617 fprintf(out, " [-l|--logfile <logfile>] \\\n");
2618 fprintf(out, " [-p|--pidfile <pidfile>] \\\n");
2619 fprintf(out, " [-s|--stats] \\\n");
2620 fprintf(out, " -- <Lua script> [<cmdline options for Lua script>]\n");
2621 exit(err);
2624 #define moonbr_usage_error() moonbr_usage(MOONBR_EXITCODE_CMDLINEERROR, argc ? argv[0] : NULL)
2626 int main(int argc, char **argv) {
2628 int daemonize = 0;
2629 int log_facility = LOG_USER;
2630 const char *log_ident = "moonbridge";
2631 const char *log_filename = NULL;
2632 const char *pid_filename = NULL;
2633 int option;
2634 struct option longopts[] = {
2635 { "background", no_argument, NULL, 'b' },
2636 { "debug", no_argument, NULL, 'd' },
2637 { "logfacility", required_argument, NULL, 'f' },
2638 { "help", no_argument, NULL, 'h' },
2639 { "logident", required_argument, NULL, 'i' },
2640 { "logfile", required_argument, NULL, 'l' },
2641 { "pidfile", required_argument, NULL, 'p' },
2642 { "stats", no_argument, NULL, 's' }
2643 };
2644 while ((option = getopt_long(argc, argv, "bdf:hi:l:p:s", longopts, NULL)) != -1) {
2645 switch (option) {
2646 case 'b':
2647 daemonize = 1;
2648 break;
2649 case 'd':
2650 moonbr_debug = 1;
2651 moonbr_stat = 1;
2652 break;
2653 case 'f':
2654 if (!strcmp(optarg, "DAEMON")) {
2655 log_facility = LOG_DAEMON;
2656 } else if (!strcmp(optarg, "USER")) {
2657 log_facility = LOG_USER;
2658 } else if (!strcmp(optarg, "0")) {
2659 log_facility = LOG_LOCAL0;
2660 } else if (!strcmp(optarg, "1")) {
2661 log_facility = LOG_LOCAL1;
2662 } else if (!strcmp(optarg, "2")) {
2663 log_facility = LOG_LOCAL2;
2664 } else if (!strcmp(optarg, "3")) {
2665 log_facility = LOG_LOCAL3;
2666 } else if (!strcmp(optarg, "4")) {
2667 log_facility = LOG_LOCAL4;
2668 } else if (!strcmp(optarg, "5")) {
2669 log_facility = LOG_LOCAL5;
2670 } else if (!strcmp(optarg, "6")) {
2671 log_facility = LOG_LOCAL6;
2672 } else if (!strcmp(optarg, "7")) {
2673 log_facility = LOG_LOCAL7;
2674 } else {
2675 moonbr_usage_error();
2677 moonbr_use_syslog = 1;
2678 break;
2679 case 'h':
2680 moonbr_usage(MOONBR_EXITCODE_GRACEFUL, argv[0]);
2681 break;
2682 case 'i':
2683 log_ident = optarg;
2684 moonbr_use_syslog = 1;
2685 break;
2686 case 'l':
2687 log_filename = optarg;
2688 break;
2689 case 'p':
2690 pid_filename = optarg;
2691 break;
2692 case 's':
2693 moonbr_stat = 1;
2694 break;
2695 default:
2696 moonbr_usage_error();
2699 if (argc - optind < 1) moonbr_usage_error();
2700 if (pid_filename) {
2701 pid_t otherpid;
2702 while ((moonbr_pidfh = pidfile_open(pid_filename, 0644, &otherpid)) == NULL) {
2703 if (errno == EEXIST) {
2704 if (otherpid == -1) {
2705 fprintf(stderr, "PID file \"%s\" is already locked\n", pid_filename);
2706 } else {
2707 fprintf(stderr, "PID file \"%s\" is already locked by process with PID: %i\n", pid_filename, (int)otherpid);
2709 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2710 } else if (errno != EINTR) {
2711 fprintf(stderr, "Could not write PID file \"%s\": %s\n", pid_filename, strerror(errno));
2712 exit(MOONBR_EXITCODE_STARTUPERROR);
2716 if (log_filename) {
2717 int logfd;
2718 while (
2719 ( logfd = flopen(
2720 log_filename,
2721 O_WRONLY|O_NONBLOCK|O_CREAT|O_APPEND|O_CLOEXEC,
2722 0640
2724 ) < 0
2725 ) {
2726 if (errno == EWOULDBLOCK) {
2727 fprintf(stderr, "Logfile \"%s\" is locked\n", log_filename);
2728 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2729 } else if (errno != EINTR) {
2730 fprintf(stderr, "Could not open logfile \"%s\": %s\n", log_filename, strerror(errno));
2731 exit(MOONBR_EXITCODE_STARTUPERROR);
2734 moonbr_logfile = fdopen(logfd, "a");
2735 if (!moonbr_logfile) {
2736 fprintf(stderr, "Could not open write stream to logfile \"%s\": %s\n", log_filename, strerror(errno));
2737 exit(MOONBR_EXITCODE_STARTUPERROR);
2740 if (daemonize == 0 && !moonbr_logfile) moonbr_logfile = stderr;
2741 if (moonbr_logfile) setlinebuf(moonbr_logfile);
2742 else moonbr_use_syslog = 1;
2743 if (moonbr_use_syslog) openlog(log_ident, LOG_NDELAY | LOG_PID, log_facility);
2744 if (daemonize) {
2745 if (daemon(1, 0)) {
2746 moonbr_log(LOG_ERR, "Could not daemonize moonbridge process");
2747 moonbr_terminate_error();
2751 moonbr_log(LOG_NOTICE, "Starting moonbridge server");
2752 if (moonbr_pidfh && pidfile_write(moonbr_pidfh)) {
2753 moonbr_log(LOG_ERR, "Could not write pidfile (after locking)");
2756 lua_State *L;
2757 L = lua_newstate(moonbr_alloc, NULL);
2758 if (!L) {
2759 moonbr_log(LOG_CRIT, "Could not initialize Lua state");
2760 moonbr_terminate_error();
2762 lua_atpanic(L, moonbr_lua_panic);
2763 lua_pushliteral(L, MOONBR_VERSION_STRING);
2764 lua_setglobal(L, "_MOONBRIDGE_VERSION");
2765 luaL_openlibs(L);
2766 luaL_requiref(L, "moonbridge_io", luaopen_moonbridge_io, 0);
2767 lua_getglobal(L, "io");
2768 for (lua_pushnil(L); lua_next(L, -3); lua_pop(L, 1)) {
2769 lua_pushvalue(L, -2);
2770 lua_pushvalue(L, -2);
2771 lua_settable(L, -5);
2773 lua_pop(L, 2);
2774 #ifdef MOONBR_LUA_PATH
2775 moonbr_modify_path(L, "path", MOONBR_LUA_PATH);
2776 #endif
2777 #ifdef MOONBR_LUA_CPATH
2778 moonbr_modify_path(L, "cpath", MOONBR_LUA_CPATH);
2779 #endif
2780 if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
2781 moonbr_log(LOG_CRIT, "Lua metatable LUA_FILEHANDLE does not exist");
2782 moonbr_terminate_error();
2784 lua_getfield(L, -1, "__index");
2785 lua_pushcfunction(L, moonbr_readuntil);
2786 lua_setfield(L, -2, "readuntil");
2787 lua_pop(L, 2);
2788 lua_pushcfunction(L, moonbr_timeout);
2789 lua_setglobal(L, "timeout");
2790 lua_pushcfunction(L, moonbr_listen);
2791 lua_setglobal(L, "listen");
2792 lua_pushcfunction(L, moonbr_addtraceback); /* on stack position 1 */
2793 moonbr_log(LOG_INFO, "Loading \"%s\"", argv[optind]);
2794 if (luaL_loadfile(L, argv[optind])) {
2795 moonbr_log(LOG_ERR, "Error while loading \"%s\": %s", argv[optind], lua_tostring(L, -1));
2796 moonbr_terminate_error();
2798 { int i; for (i=optind+1; i<argc; i++) lua_pushstring(L, argv[i]); }
2799 if (lua_pcall(L, argc-(optind+1), 0, 1)) {
2800 moonbr_log(LOG_ERR, "Error while executing \"%s\": %s", argv[optind], lua_tostring(L, -1));
2801 moonbr_terminate_error();
2803 if (!moonbr_first_pool) {
2804 moonbr_log(LOG_WARNING, "No listener initialized.");
2805 moonbr_terminate_error();
2807 lua_getglobal(L, "listen");
2808 lua_pushcfunction(L, moonbr_listen);
2809 if (lua_compare(L, -2, -1, LUA_OPEQ)) {
2810 lua_pushnil(L);
2811 lua_setglobal(L, "listen");
2813 lua_settop(L, 1);
2814 lua_gc(L, LUA_GCCOLLECT, 0); // collect garbage before forking later
2815 moonbr_run(L);
2817 return 0;

Impressum / About Us