moonbridge

view moonbridge.c @ 7:4d7551c962d5

New method request:send_text_status_response(...) that was previously only available internally as error_response(...) function
author jbe
date Thu Jan 29 01:33:00 2015 +0100 (2015-01-29)
parents 0449f6a4005f
children 32e0838d16e6
line source
2 /*** Compile-time configuration ***/
4 #define MOONBR_LUA_PANIC_BUG_WORKAROUND 1
7 /*** C preprocessor macros for portability support ***/
9 #ifndef __has_include
10 #define __has_include(x) 0
11 #endif
14 /*** Include directives for used system libraries ***/
16 #if defined(__linux__)
17 #define _GNU_SOURCE
18 #endif
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdint.h>
22 #include <errno.h>
23 #include <getopt.h>
24 #include <syslog.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <time.h>
28 #include <sys/time.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include <netinet/in.h>
32 #include <poll.h>
33 #include <signal.h>
34 #include <sys/wait.h>
35 #include <sys/resource.h>
36 #include <sys/file.h>
37 #if defined(__FreeBSD__) || __has_include(<libutil.h>)
38 #include <libutil.h>
39 #endif
40 #if defined(__linux__) || __has_include(<bsd/libutil.h>)
41 #include <bsd/libutil.h>
42 #endif
43 #if defined(__linux__) || __has_include(<bsd/unistd.h>)
44 #include <bsd/unistd.h>
45 #endif
48 /*** Fallback definitions for missing constants on some platforms ***/
50 /* INFTIM is used as timeout parameter for poll() */
51 #ifndef INFTIM
52 #define INFTIM -1
53 #endif
56 /*** Include directives for Lua ***/
58 #include <lua.h>
59 #include <lauxlib.h>
60 #include <lualib.h>
63 /*** Constants ***/
65 /* Backlog option for listen() call */
66 #define MOONBR_LISTEN_BACKLOG 1024
68 /* Maximum length of a timestamp used for strftime() */
69 #define MOONBR_LOG_MAXTIMELEN 40
71 /* Maximum length of a log message */
72 #define MOONBR_LOG_MAXMSGLEN 4095
74 /* Exitcodes passed to exit() call */
75 #define MOONBR_EXITCODE_GRACEFUL 0
76 #define MOONBR_EXITCODE_CMDLINEERROR 1
77 #define MOONBR_EXITCODE_ALREADYRUNNING 2
78 #define MOONBR_EXITCODE_STARTUPERROR 3
79 #define MOONBR_EXITCODE_RUNTIMEERROR 4
81 /* Maximum length of a line sent to stderr by child processes */
82 #define MOONBR_MAXERRORLINELEN 1024
84 /* Maximum length of an error string returned by strerror() */
85 #define MOONBR_MAXSTRERRORLEN 80
87 /* Status bytes exchanged between master and child processes */
88 #define MOONBR_SOCKETTYPE_INTERVAL 'I'
89 #define MOONBR_SOCKETTYPE_LOCAL 'L'
90 #define MOONBR_SOCKETTYPE_NETWORK 'N'
91 #define MOONBR_STATUS_IDLE '1'
92 #define MOONBR_COMMAND_TERMINATE '2'
93 #define MOONBR_STATUS_GOODBYE '3'
95 /* Constant file descriptors */
96 #define MOONBR_FD_STDERR 2
97 #define MOONBR_FD_CONTROL 3
98 #define MOONBR_FD_END 4
100 /* Return values of moonbr_try_destroy_worker() */
101 #define MOONBR_DESTROY_NONE 0
102 #define MOONBR_DESTROY_PREPARE 1
103 #define MOONBR_DESTROY_IDLE_OR_ASSIGNED 2
106 /*** Types ***/
108 /* Enum for 'moonbr_pstate' */
109 #define MOONBR_PSTATE_STARTUP 0
110 #define MOONBR_PSTATE_RUNNING 1
111 #define MOONBR_PSTATE_FORKED 2
113 /* Enum for 'proto' field of struct moonbr_listener */
114 #define MOONBR_PROTO_INTERVAL 1
115 #define MOONBR_PROTO_LOCAL 2
116 #define MOONBR_PROTO_TCP6 3
117 #define MOONBR_PROTO_TCP4 4
119 /* Data structure for a pool's listener that can accept incoming connections */
120 struct moonbr_listener {
121 struct moonbr_pool *pool;
122 struct moonbr_listener *prev_listener; /* previous idle or(!) connected listener */
123 struct moonbr_listener *next_listener; /* next idle or(!) connected listener */
124 int proto;
125 union {
126 struct {
127 char *name; /* name of interval passed to 'connect' function as 'interval' field in table */
128 int strict; /* nonzero = runtime of 'connect' function does not delay interval */
129 struct timeval delay; /* interval between invocations of 'connect' function */
130 struct timeval wakeup; /* point in time of next invocation */
131 } interval;
132 struct {
133 char *path; /* full path name (i.e. filename with path) of UNIX domain socket */
134 } local;
135 struct {
136 int port; /* port number to listen on (in host endianess) */
137 int localhost_only; /* nonzero = listen on localhost only */
138 } tcp;
139 } proto_specific;
140 int listenfd; /* -1 = none */
141 int pollidx; /* -1 = none */
142 };
144 /* Data structure for a child process that is handling incoming connections */
145 struct moonbr_worker {
146 struct moonbr_pool *pool;
147 struct moonbr_worker *prev_worker;
148 struct moonbr_worker *next_worker;
149 struct moonbr_worker *prev_idle_worker;
150 struct moonbr_worker *next_idle_worker;
151 int idle; /* nonzero = waiting for command from parent process */
152 int assigned; /* nonzero = currently handling a connection */
153 pid_t pid;
154 int controlfd; /* socket to send/receive control message to/from child process */
155 int errorfd; /* socket to receive error output from child process' stderr */
156 char *errorlinebuf; /* optional buffer for collecting stderr data from child process */
157 int errorlinelen; /* number of bytes stored in 'errorlinebuf' */
158 int errorlineovf; /* nonzero = line length overflow */
159 struct timeval idle_expiration; /* point in time until child process may stay in idle state */
160 struct moonbr_listener *restart_interval_listener; /* set while interval listener is assigned */
161 };
163 /* Data structure for a pool of workers and listeners */
164 struct moonbr_pool {
165 int poolnum; /* number of pool for log output */
166 struct moonbr_pool *next_pool; /* next entry in linked list starting with 'moonbr_first_pool' */
167 struct moonbr_worker *first_worker; /* first worker of pool */
168 struct moonbr_worker *last_worker; /* last worker of pool */
169 struct moonbr_worker *first_idle_worker; /* first idle worker of pool */
170 struct moonbr_worker *last_idle_worker; /* last idle worker of pool */
171 int idle_worker_count;
172 int unassigned_worker_count;
173 int total_worker_count;
174 int worker_count_stat; /* only needed for statistics */
175 int pre_fork; /* desired minimum number of unassigned workers */
176 int min_fork; /* desired minimum number of workers in total */
177 int max_fork; /* maximum number of workers */
178 struct timeval fork_delay; /* delay after each fork() until a fork may happen again */
179 struct timeval fork_wakeup; /* point in time when a fork may happen again (unless a worker terminates before) */
180 struct timeval fork_error_delay; /* delay between fork()s when an error during fork or preparation occurred */
181 struct timeval fork_error_wakeup; /* point in time when fork may happen again if an error in preparation occurred */
182 int use_fork_error_wakeup; /* nonzero = error in preparation occured; gets reset on next fork */
183 struct timeval exit_delay; /* delay for terminating excessive workers (unassigned_worker_count > pre_fork) */
184 struct timeval exit_wakeup; /* point in time when terminating an excessive worker */
185 struct timeval idle_timeout; /* delay before an idle worker is terminated */
186 size_t memory_limit; /* maximum bytes of memory that the Lua machine may allocate */
187 int listener_count; /* total number of listeners of pool (and size of 'listener' array at end of this struct) */
188 struct moonbr_listener *first_idle_listener; /* first listener that is idle (i.e. has no waiting connection) */
189 struct moonbr_listener *last_idle_listener; /* last listener that is idle (i.e. has no waiting connection) */
190 struct moonbr_listener *first_connected_listener; /* first listener that has a pending connection */
191 struct moonbr_listener *last_connected_listener; /* last listener that has a pending connection */
192 struct moonbr_listener listener[1]; /* static array of variable(!) size to contain 'listener' structures */
193 };
195 /* Enum for 'channel' field of struct moonbr_poll_worker */
196 #define MOONBR_POLL_WORKER_CONTROLCHANNEL 1
197 #define MOONBR_POLL_WORKER_ERRORCHANNEL 2
199 /* Structure to refer from 'moonbr_poll_worker_fds' entry to worker structure */
200 struct moonbr_poll_worker {
201 struct moonbr_worker *worker;
202 int channel; /* field indicating whether file descriptor is 'controlfd' or 'errorfd' */
203 };
205 /* Variable indicating that clean shutdown was requested */
206 static int moonbr_shutdown_in_progress = 0;
209 /*** Macros for Lua registry ***/
211 /* Lightuserdata keys for Lua registry to store 'prepare', 'connect', and 'finish' functions */
212 #define moonbr_luakey_prepare_func(pool) ((void *)(intptr_t)(pool) + 0)
213 #define moonbr_luakey_connect_func(pool) ((void *)(intptr_t)(pool) + 1)
214 #define moonbr_luakey_finish_func(pool) ((void *)(intptr_t)(pool) + 2)
217 /*** Global variables ***/
219 /* State of process execution */
220 static int moonbr_pstate = MOONBR_PSTATE_STARTUP;
222 /* Process ID of the main process */
223 static pid_t moonbr_masterpid;
225 /* Condition variables set by the signal handler */
226 static volatile sig_atomic_t moonbr_cond_poll = 0;
227 static volatile sig_atomic_t moonbr_cond_terminate = 0;
228 static volatile sig_atomic_t moonbr_cond_interrupt = 0;
229 static volatile sig_atomic_t moonbr_cond_child = 0;
231 /* Socket pair to denote signal delivery when signal handler was called just before poll() */
232 static int moonbr_poll_signalfds[2];
233 #define moonbr_poll_signalfd_read moonbr_poll_signalfds[0]
234 #define moonbr_poll_signalfd_write moonbr_poll_signalfds[1]
236 /* Global variables for pidfile and logging */
237 static struct pidfh *moonbr_pidfh = NULL;
238 static FILE *moonbr_logfile = NULL;
239 static int moonbr_use_syslog = 0;
241 /* First and last entry of linked list of all created pools during initialization */
242 static struct moonbr_pool *moonbr_first_pool = NULL;
243 static struct moonbr_pool *moonbr_last_pool = NULL;
245 /* Total count of pools */
246 static int moonbr_pool_count = 0;
248 /* Set to a nonzero value if dynamic part of 'moonbr_poll_fds' ('moonbr_poll_worker_fds') needs an update */
249 static int moonbr_poll_refresh_needed = 0;
251 /* Array passed to poll(), consisting of static part and dynamic part ('moonbr_poll_worker_fds') */
252 static struct pollfd *moonbr_poll_fds = NULL; /* the array */
253 static int moonbr_poll_fds_bufsize = 0; /* memory allocated for this number of elements */
254 static int moonbr_poll_fds_count = 0; /* total number of elements */
255 static int moonbr_poll_fds_static_count; /* number of elements in static part */
257 /* Dynamic part of 'moonbr_poll_fds' array */
258 #define moonbr_poll_worker_fds (moonbr_poll_fds+moonbr_poll_fds_static_count)
260 /* Additional information for dynamic part of 'moonbr_poll_fds' array */
261 struct moonbr_poll_worker *moonbr_poll_workers; /* the array */
262 static int moonbr_poll_workers_bufsize = 0; /* memory allocated for this number of elements */
263 static int moonbr_poll_worker_count = 0; /* number of elements in array */
265 /* Variable set to nonzero value to disallow further calls of 'listen' function */
266 static int moonbr_booted = 0;
268 /* Global variables to store information on connection socket in child process */
269 static int moonbr_child_peersocket_type; /* type of socket by MOONBR_SOCKETTYPE constant */
270 static int moonbr_child_peersocket_fd; /* Original file descriptor of peer socket */
271 static luaL_Stream *moonbr_child_peersocket_inputstream; /* Lua input stream of socket */
272 static luaL_Stream *moonbr_child_peersocket_outputstream; /* Lua output stream of socket */
274 /* Verbosity settings */
275 static int moonbr_debug = 0;
276 static int moonbr_stat = 0;
278 /* Memory consumption by Lua machine */
279 static size_t moonbr_memory_usage = 0;
280 static size_t moonbr_memory_limit = 0;
283 /*** Functions for signal handling ***/
285 /* Signal handler for master and child processes */
286 static void moonbr_signal(int sig) {
287 if (getpid() == moonbr_masterpid) {
288 /* master process */
289 switch (sig) {
290 case SIGHUP:
291 case SIGINT:
292 /* fast shutdown requested */
293 moonbr_cond_interrupt = 1;
294 break;
295 case SIGTERM:
296 /* clean shutdown requested */
297 moonbr_cond_terminate = 1;
298 break;
299 case SIGCHLD:
300 /* child process terminated */
301 moonbr_cond_child = 1;
302 break;
303 }
304 if (moonbr_cond_poll) {
305 /* avoid race condition if signal handler is invoked right before poll() */
306 char buf[1] = {0};
307 write(moonbr_poll_signalfd_write, buf, 1);
308 }
309 } else {
310 /* child process forwards certain signals to parent process */
311 switch (sig) {
312 case SIGHUP:
313 case SIGINT:
314 case SIGTERM:
315 kill(moonbr_masterpid, sig);
316 }
317 }
318 }
320 /* Initialize signal handling */
321 static void moonbr_signal_init(){
322 moonbr_masterpid = getpid();
323 signal(SIGHUP, moonbr_signal);
324 signal(SIGINT, moonbr_signal);
325 signal(SIGTERM, moonbr_signal);
326 signal(SIGCHLD, moonbr_signal);
327 }
330 /*** Functions for logging in master process ***/
332 /* Logs a pre-formatted message with given syslog() priority */
333 static void moonbr_log_msg(int priority, const char *msg) {
334 if (moonbr_logfile) {
335 /* logging to logfile desired (timestamp is prepended in that case) */
336 time_t now_time = 0;
337 struct tm now_tmstruct;
338 char timestr[MOONBR_LOG_MAXTIMELEN+1];
339 time(&now_time);
340 localtime_r(&now_time, &now_tmstruct);
341 if (!strftime(
342 timestr, MOONBR_LOG_MAXTIMELEN+1, "%Y-%m-%d %H:%M:%S %Z: ", &now_tmstruct
343 )) timestr[0] = 0;
344 fprintf(moonbr_logfile, "%s%s\n", timestr, msg);
345 }
346 if (moonbr_use_syslog) {
347 /* logging through syslog desired */
348 syslog(priority, "%s", msg);
349 }
350 }
352 /* Formats a message via vsnprintf() and logs it with given syslog() priority */
353 static void moonbr_log(int priority, const char *message, ...) {
354 char msgbuf[MOONBR_LOG_MAXMSGLEN+1]; /* buffer of static size to store formatted message */
355 int msglen; /* length of full message (may exceed MOONBR_LOG_MAXMSGLEN) */
356 {
357 /* pass variable arguments to vsnprintf() to format message */
358 va_list ap;
359 va_start(ap, message);
360 msglen = vsnprintf(msgbuf, MOONBR_LOG_MAXMSGLEN+1, message, ap);
361 va_end(ap);
362 }
363 {
364 /* split and log message line by line */
365 char *line = msgbuf;
366 while (1) {
367 char *endptr = strchr(line, '\n');
368 if (endptr) {
369 /* terminate string where newline character is found */
370 *endptr = 0;
371 } else if (line != msgbuf && msglen > MOONBR_LOG_MAXMSGLEN) {
372 /* break if line is incomplete and not the first line */
373 break;
374 }
375 moonbr_log_msg(priority, line);
376 if (!endptr) break; /* break if end of formatted message is reached */
377 line = endptr+1; /* otherwise continue with remaining message */
378 }
379 }
380 if (msglen > MOONBR_LOG_MAXMSGLEN) {
381 /* print warning if message was truncated */
382 moonbr_log_msg(priority, "Previous log message has been truncated due to excessive length");
383 }
384 }
387 /*** Termination function ***/
389 /* Kill all child processes, remove PID file (if existent), and exit master process with given exitcode */
390 static void moonbr_terminate(int exitcode) {
391 {
392 struct moonbr_pool *pool;
393 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
394 {
395 struct moonbr_worker *worker;
396 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
397 moonbr_log(LOG_INFO, "Sending SIGKILL to child with PID %i", (int)worker->pid);
398 if (kill(worker->pid, SIGKILL)) {
399 moonbr_log(LOG_ERR, "Error while killing child process: %s", strerror(errno));
400 }
401 }
402 }
403 {
404 int i;
405 for (i=0; i<pool->listener_count; i++) {
406 struct moonbr_listener *listener = &pool->listener[i];
407 if (listener->proto == MOONBR_PROTO_LOCAL) {
408 moonbr_log(LOG_INFO, "Unlinking local socket \"%s\"", listener->proto_specific.local.path);
409 if (unlink(listener->proto_specific.local.path)) {
410 moonbr_log(LOG_ERR, "Error while unlinking local socket: %s", strerror(errno));
411 }
412 }
413 }
414 }
415 }
416 }
417 moonbr_log(exitcode ? LOG_ERR : LOG_NOTICE, "Terminating with exit code %i", exitcode);
418 if (moonbr_pidfh && pidfile_remove(moonbr_pidfh)) {
419 moonbr_log(LOG_ERR, "Error while removing PID file: %s", strerror(errno));
420 }
421 exit(exitcode);
422 }
424 /* Terminate with either MOONBR_EXITCODE_STARTUPERROR or MOONBR_EXITCODE_RUNTIMEERROR */
425 #define moonbr_terminate_error() \
426 moonbr_terminate( \
427 moonbr_pstate == MOONBR_PSTATE_STARTUP ? \
428 MOONBR_EXITCODE_STARTUPERROR : \
429 MOONBR_EXITCODE_RUNTIMEERROR \
430 )
433 /*** Helper functions ***/
435 /* Fills a 'struct timeval' structure with the current time (using CLOCK_MONOTONIC) */
436 static void moonbr_now(struct timeval *now) {
437 struct timespec ts = {0, };
438 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
439 moonbr_log(LOG_CRIT, "Error in clock_gettime() call: %s", strerror(errno));
440 moonbr_terminate_error();
441 }
442 *now = (struct timeval){ .tv_sec = ts.tv_sec, .tv_usec = ts.tv_nsec / 1000 };
443 }
445 /* Formats a 'struct timeval' value (not thread-safe) */
446 static char *moonbr_format_timeval(struct timeval *t) {
447 static char buf[32];
448 snprintf(buf, 32, "%ji.%06ji seconds", (intmax_t)t->tv_sec, (intmax_t)t->tv_usec);
449 return buf;
450 }
453 /*** Functions for pool creation and startup ***/
455 /* Creates a 'struct moonbr_pool' structure with a given number of listeners */
456 static struct moonbr_pool *moonbr_create_pool(int listener_count) {
457 struct moonbr_pool *pool;
458 pool = calloc(1,
459 sizeof(struct moonbr_pool) + /* size of 'struct moonbr_pool' with one listener */
460 (listener_count-1) * sizeof(struct moonbr_listener) /* size of extra listeners */
461 );
462 if (!pool) {
463 moonbr_log(LOG_CRIT, "Memory allocation error");
464 moonbr_terminate_error();
465 }
466 pool->listener_count = listener_count;
467 {
468 /* initialization of listeners */
469 int i;
470 for (i=0; i<listener_count; i++) {
471 struct moonbr_listener *listener = &pool->listener[i];
472 listener->pool = pool;
473 listener->listenfd = -1;
474 listener->pollidx = -1;
475 }
476 }
477 return pool;
478 }
480 /* Destroys a 'struct moonbr_pool' structure before it has been started */
481 static void moonbr_destroy_pool(struct moonbr_pool *pool) {
482 int i;
483 for (i=0; i<pool->listener_count; i++) {
484 struct moonbr_listener *listener = &pool->listener[i];
485 if (
486 listener->proto == MOONBR_PROTO_INTERVAL &&
487 listener->proto_specific.interval.name
488 ) {
489 free(listener->proto_specific.interval.name);
490 }
491 if (
492 listener->proto == MOONBR_PROTO_LOCAL &&
493 listener->proto_specific.local.path
494 ) {
495 free(listener->proto_specific.local.path);
496 }
497 }
498 free(pool);
499 }
501 /* Starts a all listeners in a pool */
502 static int moonbr_start_pool(struct moonbr_pool *pool) {
503 moonbr_log(LOG_INFO, "Creating pool", pool->poolnum);
504 {
505 int i;
506 for (i=0; i<pool->listener_count; i++) {
507 struct moonbr_listener *listener = &pool->listener[i];
508 switch (listener->proto) {
509 case MOONBR_PROTO_INTERVAL:
510 // nothing to do here: starting intervals is performed in moonbr_run() function
511 if (!listener->proto_specific.interval.name) {
512 moonbr_log(LOG_INFO, "Adding unnamed interval listener");
513 } else {
514 moonbr_log(LOG_INFO, "Adding interval listener \"%s\"", listener->proto_specific.interval.name);
515 }
516 break;
517 case MOONBR_PROTO_LOCAL:
518 moonbr_log(LOG_INFO, "Adding local socket listener for path \"%s\"", listener->proto_specific.local.path);
519 {
520 struct sockaddr_un servaddr = { .sun_family = AF_UNIX };
521 const int path_maxlen = sizeof(struct sockaddr_un) - (
522 (void *)&servaddr.sun_path - (void *)&servaddr
523 );
524 if (
525 snprintf(
526 servaddr.sun_path,
527 path_maxlen,
528 "%s",
529 listener->proto_specific.local.path
530 ) >= path_maxlen
531 ) {
532 errno = ENAMETOOLONG;
533 };
534 listener->listenfd = socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
535 if (listener->listenfd == -1) goto moonbr_start_pool_error;
536 if (!unlink(listener->proto_specific.local.path)) {
537 moonbr_log(LOG_WARNING, "Unlinked named socket \"%s\" prior to listening", listener->proto_specific.local.path);
538 } else {
539 if (errno != ENOENT) {
540 moonbr_log(LOG_ERR, "Could not unlink named socket \"%s\" prior to listening: %s", listener->proto_specific.local.path, strerror(errno));
541 }
542 }
543 if (
544 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
545 ) goto moonbr_start_pool_error;
546 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
547 }
548 break;
549 case MOONBR_PROTO_TCP6:
550 if (listener->proto_specific.tcp.localhost_only) {
551 moonbr_log(LOG_INFO, "Adding localhost TCP/IPv6 listener on port %i", listener->proto_specific.tcp.port);
552 } else {
553 moonbr_log(LOG_INFO, "Adding public TCP/IPv6 listener on port %i", listener->proto_specific.tcp.port);
554 }
555 {
556 struct sockaddr_in6 servaddr = {
557 .sin6_family = AF_INET6,
558 .sin6_port = htons(listener->proto_specific.tcp.port)
559 };
560 listener->listenfd = socket(PF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
561 if (listener->listenfd == -1) goto moonbr_start_pool_error;
562 {
563 /* avoid "Address already in use" error when restarting service */
564 static const int reuseval = 1;
565 if (setsockopt(
566 listener->listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval)
567 )) goto moonbr_start_pool_error;
568 }
569 {
570 /* default to send TCP RST when process terminates unexpectedly */
571 static const struct linger lingerval = {
572 .l_onoff = 1,
573 .l_linger = 0
574 };
575 if (setsockopt(
576 listener->listenfd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval)
577 )) goto moonbr_start_pool_error;
578 }
579 if (listener->proto_specific.tcp.localhost_only) {
580 servaddr.sin6_addr.s6_addr[15] = 1;
581 }
582 if (
583 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
584 ) goto moonbr_start_pool_error;
585 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
586 }
587 break;
588 case MOONBR_PROTO_TCP4:
589 if (listener->proto_specific.tcp.localhost_only) {
590 moonbr_log(LOG_INFO, "Adding localhost TCP/IPv4 listener on port %i", listener->proto_specific.tcp.port);
591 } else {
592 moonbr_log(LOG_INFO, "Adding public TCP/IPv4 listener on port %i", listener->proto_specific.tcp.port);
593 }
594 {
595 struct sockaddr_in servaddr = {
596 .sin_family = AF_INET,
597 .sin_port = htons(listener->proto_specific.tcp.port)
598 };
599 listener->listenfd = socket(PF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
600 if (listener->listenfd == -1) goto moonbr_start_pool_error;
601 {
602 /* avoid "Address already in use" error when restarting service */
603 static const int reuseval = 1;
604 if (setsockopt(
605 listener->listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval)
606 )) goto moonbr_start_pool_error;
607 }
608 {
609 /* default to send TCP RST when process terminates unexpectedly */
610 static const struct linger lingerval = {
611 .l_onoff = 1,
612 .l_linger = 0
613 };
614 if (setsockopt(
615 listener->listenfd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval)
616 )) goto moonbr_start_pool_error;
617 }
618 if (listener->proto_specific.tcp.localhost_only) {
619 ((uint8_t *)&servaddr.sin_addr.s_addr)[0] = 127;
620 ((uint8_t *)&servaddr.sin_addr.s_addr)[3] = 1;
621 }
622 if (
623 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
624 ) goto moonbr_start_pool_error;
625 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
626 }
627 break;
628 default:
629 moonbr_log(LOG_CRIT, "Internal error (should not happen): Unexpected value in listener.proto field");
630 moonbr_terminate_error();
631 }
632 }
633 goto moonbr_start_pool_ok;
634 moonbr_start_pool_error:
635 {
636 int j = i;
637 int errno2 = errno;
638 for (; i>=0; i--) {
639 struct moonbr_listener *listener = &pool->listener[i];
640 if (listener->listenfd != -1) close(listener->listenfd);
641 }
642 errno = errno2;
643 return j;
644 }
645 }
646 moonbr_start_pool_ok:
647 pool->poolnum = ++moonbr_pool_count;
648 moonbr_log(LOG_INFO, "Pool #%i created", pool->poolnum);
649 if (moonbr_last_pool) moonbr_last_pool->next_pool = pool;
650 else moonbr_first_pool = pool;
651 moonbr_last_pool = pool;
652 return -1;
653 }
656 /*** Function to send data and a file descriptor to child process */
658 /* Sends control message of one bye plus optional file descriptor plus optional pointer to child process */
659 static void moonbr_send_control_message(struct moonbr_worker *worker, char status, int fd, void *ptr) {
660 {
661 struct iovec iovector = { .iov_base = &status, .iov_len = 1 }; /* carrying status byte */
662 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to transfer file descriptor */
663 struct msghdr message = { .msg_iov = &iovector, .msg_iovlen = 1 }; /* data structure passed to sendmsg() call */
664 if (moonbr_debug) {
665 if (fd == -1) {
666 moonbr_log(LOG_DEBUG, "Sending control message \"%c\" to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
667 } else {
668 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);
669 }
670 }
671 if (fd != -1) {
672 /* attach control message with file descriptor */
673 message.msg_control = control_message_buffer;
674 message.msg_controllen = CMSG_SPACE(sizeof(int));
675 {
676 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
677 control_message->cmsg_level = SOL_SOCKET;
678 control_message->cmsg_type = SCM_RIGHTS;
679 control_message->cmsg_len = CMSG_LEN(sizeof(int));
680 *((int *)CMSG_DATA(control_message)) = fd;
681 }
682 }
683 while (sendmsg(worker->controlfd, &message, MSG_NOSIGNAL) < 0) {
684 if (errno == EPIPE) {
685 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));
686 return; /* do not close socket; socket is closed when reading from it */
687 }
688 if (errno != EINTR) {
689 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));
690 moonbr_terminate_error();
691 }
692 }
693 }
694 if (ptr) {
695 char buf[sizeof(void *)];
696 char *pos = buf;
697 int len = sizeof(void *);
698 ssize_t written;
699 if (moonbr_debug) {
700 moonbr_log(LOG_DEBUG, "Sending memory pointer to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
701 }
702 *((intptr_t *)buf) = (intptr_t)ptr;
703 while (len) {
704 written = send(worker->controlfd, pos, len, MSG_NOSIGNAL);
705 if (written > 0) {
706 pos += written;
707 len -= written;
708 } else if (errno == EPIPE) {
709 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));
710 return; /* do not close socket; socket is closed when reading from it */
711 } else if (errno != EINTR) {
712 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));
713 moonbr_terminate_error();
714 }
715 }
716 }
717 }
720 /*** Functions running in child process ***/
722 /* Logs an error in child process */
723 static void moonbr_child_log(const char *message) {
724 fprintf(stderr, "%s\n", message);
725 }
727 /* Logs a fatal error in child process and terminates process with error status */
728 static void moonbr_child_log_fatal(const char *message) {
729 moonbr_child_log(message);
730 exit(1);
731 }
733 /* Logs an error in child process while appending error string for global errno variable */
734 static void moonbr_child_log_errno(const char *message) {
735 char errmsg[MOONBR_MAXSTRERRORLEN];
736 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
737 fprintf(stderr, "%s: %s\n", message, errmsg);
738 }
740 /* Logs a fatal error in child process while appending error string for errno and terminating process */
741 static void moonbr_child_log_errno_fatal(const char *message) {
742 moonbr_child_log_errno(message);
743 exit(1);
744 }
746 /* Receives a control message consisting of one character plus an optional file descriptor from parent process */
747 static void moonbr_child_receive_control_message(int socketfd, char *status, int *fd) {
748 struct iovec iovector = { .iov_base = status, .iov_len = 1 }; /* reference to status byte variable */
749 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to receive file descriptor */
750 struct msghdr message = { /* data structure passed to recvmsg() call */
751 .msg_iov = &iovector,
752 .msg_iovlen = 1,
753 .msg_control = control_message_buffer,
754 .msg_controllen = CMSG_SPACE(sizeof(int))
755 };
756 {
757 int received;
758 while ((received = recvmsg(socketfd, &message, MSG_CMSG_CLOEXEC)) < 0) {
759 if (errno != EINTR) {
760 moonbr_child_log_errno_fatal("Error while trying to receive connection socket from parent process");
761 }
762 }
763 if (!received) {
764 moonbr_child_log_fatal("Unexpected EOF while trying to receive connection socket from parent process");
765 }
766 }
767 {
768 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
769 if (control_message) {
770 if (control_message->cmsg_level != SOL_SOCKET) {
771 moonbr_child_log_fatal("Received control message with cmsg_level not equal to SOL_SOCKET");
772 }
773 if (control_message->cmsg_type != SCM_RIGHTS) {
774 moonbr_child_log_fatal("Received control message with cmsg_type not equal to SCM_RIGHTS");
775 }
776 *fd = *((int *)CMSG_DATA(control_message));
777 } else {
778 *fd = -1;
779 }
780 }
781 }
783 /* Receives a pointer from parent process */
784 static void *moonbr_child_receive_pointer(int socketfd) {
785 char buf[sizeof(void *)];
786 char *pos = buf;
787 int len = sizeof(void *);
788 ssize_t bytes_read;
789 while (len) {
790 bytes_read = recv(socketfd, pos, len, 0);
791 if (bytes_read > 0) {
792 pos += bytes_read;
793 len -= bytes_read;
794 } else if (!bytes_read) {
795 moonbr_child_log_fatal("Unexpected EOF while trying to receive memory pointer from parent process");
796 } else if (errno != EINTR) {
797 moonbr_child_log_errno_fatal("Error while trying to receive memory pointer from parent process");
798 }
799 }
800 return (void *)*(intptr_t *)buf;
801 }
803 /* Throws a Lua error message with an error string for errno appended to it */
804 static void moonbr_child_lua_errno_error(lua_State *L, char *message) {
805 char errmsg[MOONBR_MAXSTRERRORLEN];
806 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
807 luaL_error(L, "%s: %s", message, errmsg);
808 }
810 /* Closes the input stream from peer unless it has already been closed */
811 static int moonbr_child_close_peersocket_inputstream(
812 int cleanshut, /* nonzero = use shutdown() if applicable */
813 int mark /* nonzero = mark the stream as closed for Lua */
814 ) {
815 int err = 0; /* nonzero = error occurred */
816 int errno2; /* stores previous errno values that take precedence */
817 if (moonbr_child_peersocket_inputstream->f) {
818 if (cleanshut && moonbr_child_peersocket_type == MOONBR_SOCKETTYPE_NETWORK) {
819 if (shutdown(moonbr_child_peersocket_fd, SHUT_RD)) {
820 errno2 = errno;
821 err = -1;
822 }
823 }
824 if (fclose(moonbr_child_peersocket_inputstream->f)) {
825 if (!err) errno2 = errno;
826 err = -1;
827 }
828 moonbr_child_peersocket_inputstream->f = NULL;
829 }
830 if (mark) moonbr_child_peersocket_inputstream->closef = NULL;
831 if (err) errno = errno2;
832 return err;
833 }
835 /* Closes the output stream to peer unless it has already been closed */
836 static int moonbr_child_close_peersocket_outputstream(
837 int cleanshut, /* nonzero = use fflush() and shutdown() if applicable */
838 int mark /* nonzero = mark the stream as closed for Lua */
839 ) {
840 int err = 0; /* nonzero = error occurred */
841 int errno2; /* stores previous errno values that take precedence */
842 if (moonbr_child_peersocket_outputstream->f) {
843 if (moonbr_child_peersocket_type == MOONBR_SOCKETTYPE_NETWORK) {
844 if (cleanshut) {
845 if (fflush(moonbr_child_peersocket_outputstream->f)) {
846 errno2 = errno;
847 err = -1;
848 } else {
849 if (shutdown(moonbr_child_peersocket_fd, SHUT_WR)) {
850 errno2 = errno;
851 err = -1;
852 }
853 }
854 } else {
855 fpurge(moonbr_child_peersocket_outputstream->f);
856 }
857 }
858 if (fclose(moonbr_child_peersocket_outputstream->f)) {
859 if (!err) errno2 = errno;
860 err = -1;
861 }
862 moonbr_child_peersocket_outputstream->f = NULL;
863 }
864 if (mark) moonbr_child_peersocket_outputstream->closef = NULL;
865 if (err) errno = errno2;
866 return err;
867 }
869 /* Perform a clean shutdown of input and output stream (may be called multiple times) */
870 static int moonbr_child_close_peersocket(int timeout) {
871 int errprio = 0;
872 int errno2;
873 if (moonbr_child_peersocket_fd == -1) return 0;
874 if (moonbr_child_close_peersocket_inputstream(1, 1)) {
875 errprio = 1;
876 errno2 = errno;
877 }
878 if (moonbr_child_close_peersocket_outputstream(1, 1)) {
879 errprio = 4;
880 errno2 = errno;
881 }
882 if (moonbr_child_peersocket_type == MOONBR_SOCKETTYPE_NETWORK) {
883 struct linger lingerval = { 0, };
884 if (timeout && !errprio) {
885 lingerval.l_onoff = 1;
886 lingerval.l_linger = timeout;
887 }
888 if (setsockopt(moonbr_child_peersocket_fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
889 if (errprio < 2) {
890 errprio = 2;
891 errno2 = errno;
892 }
893 }
894 }
895 if (close(moonbr_child_peersocket_fd)) {
896 if (errprio < 3) {
897 errprio = 3;
898 errno2 = errno;
899 }
900 }
901 moonbr_child_peersocket_fd = -1;
902 if (errprio) {
903 errno = errno2;
904 return -1;
905 }
906 return 0;
907 }
909 /* Close socket and cause reset of TCP connection (TCP RST aka "Connection reset by peer") if possible */
910 static int moonbr_child_cancel_peersocket() {
911 int err = 0;
912 if (moonbr_child_close_peersocket_inputstream(0, 1)) err = -1;
913 if (moonbr_child_close_peersocket_outputstream(0, 1)) err = -1;
914 if (close(moonbr_child_peersocket_fd)) err = -1;
915 moonbr_child_peersocket_fd = -1;
916 return err;
917 }
919 /* Lua method for socket object to read from input stream */
920 static int moonbr_child_lua_read_stream(lua_State *L) {
921 lua_getfield(L, 1, "input");
922 lua_getfield(L, -1, "read");
923 lua_insert(L, 1);
924 lua_replace(L, 2);
925 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
926 return lua_gettop(L);
927 }
929 /* Lua method for socket object to read from input stream until terminator */
930 static int moonbr_child_lua_readuntil_stream(lua_State *L) {
931 lua_getfield(L, 1, "input");
932 lua_getfield(L, -1, "readuntil");
933 lua_insert(L, 1);
934 lua_replace(L, 2);
935 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
936 return lua_gettop(L);
937 }
939 /* Lua method for socket object to iterate over input stream */
940 static int moonbr_child_lua_lines_stream(lua_State *L) {
941 lua_getfield(L, 1, "input");
942 lua_getfield(L, -1, "lines");
943 lua_insert(L, 1);
944 lua_replace(L, 2);
945 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
946 return lua_gettop(L);
947 }
949 /* Lua method for socket object to write to output stream */
950 static int moonbr_child_lua_write_stream(lua_State *L) {
951 lua_getfield(L, 1, "output");
952 lua_getfield(L, -1, "write");
953 lua_insert(L, 1);
954 lua_replace(L, 2);
955 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
956 return lua_gettop(L);
957 }
959 /* Lua method for socket object to flush the output stream */
960 static int moonbr_child_lua_flush_stream(lua_State *L) {
961 lua_getfield(L, 1, "output");
962 lua_getfield(L, -1, "flush");
963 lua_insert(L, 1);
964 lua_replace(L, 2);
965 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
966 return lua_gettop(L);
967 }
969 /* Lua function to close a single stream (input or output) from/to peer */
970 static int moonbr_child_lua_close_stream(lua_State *L) {
971 luaL_Stream *stream = lua_touserdata(L, 1);
972 if (stream == moonbr_child_peersocket_inputstream) {
973 if (moonbr_child_close_peersocket_inputstream(1, 0)) { /* don't mark as closed as it's done by Lua */
974 moonbr_child_lua_errno_error(L, "Could not close input stream");
975 }
976 } else if (stream == moonbr_child_peersocket_outputstream) {
977 if (moonbr_child_close_peersocket_outputstream(1, 0)) { /* don't mark as closed as it's done by Lua */
978 moonbr_child_lua_errno_error(L, "Could not close output stream");
979 }
980 } else {
981 luaL_argerror(L, 1, "Not a connection socket");
982 }
983 return 0;
984 }
986 /* Lua function to close both input and output stream from/to peer */
987 static int moonbr_child_lua_close_both_streams(lua_State *L) {
988 int timeout = 0;
989 if (!lua_isnoneornil(L, 2)) {
990 lua_Integer n = luaL_checkinteger(L, 2);
991 luaL_argcheck(L, n >= 0 && n <= INT_MAX, 2, "out of range");
992 timeout = n;
993 }
994 if (moonbr_child_peersocket_fd == -1) {
995 luaL_error(L, "Connection with peer has already been explicitly closed");
996 }
997 if (moonbr_child_close_peersocket(timeout)) {
998 moonbr_child_lua_errno_error(L, "Could not close socket connection with peer");
999 }
1000 return 0;
1003 /* Lua function to close both input and output stream from/to peer */
1004 static int moonbr_child_lua_cancel_both_streams(lua_State *L) {
1005 if (moonbr_child_peersocket_fd == -1) {
1006 luaL_error(L, "Connection with peer has already been explicitly closed");
1008 if (moonbr_child_cancel_peersocket()) {
1009 moonbr_child_lua_errno_error(L, "Could not cancel socket connection with peer");
1011 return 0;
1014 /* Methods of (bidirectional) socket object passed to handler */
1015 static luaL_Reg moonbr_child_lua_socket_functions[] = {
1016 {"read", moonbr_child_lua_read_stream},
1017 {"readuntil", moonbr_child_lua_readuntil_stream},
1018 {"lines", moonbr_child_lua_lines_stream},
1019 {"write", moonbr_child_lua_write_stream},
1020 {"flush", moonbr_child_lua_flush_stream},
1021 {"close", moonbr_child_lua_close_both_streams},
1022 {"cancel", moonbr_child_lua_cancel_both_streams},
1023 {NULL, NULL}
1024 };
1026 /* Main function of child process to be called after fork() and file descriptor rearrangement */
1027 void moonbr_child_run(struct moonbr_pool *pool, lua_State *L) {
1028 char controlmsg;
1029 struct itimerval notimer = { { 0, }, { 0, } };
1030 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
1031 if (lua_isnil(L, -1)) lua_pop(L, 1);
1032 else if (lua_pcall(L, 0, 0, 1)) {
1033 fprintf(stderr, "Error in \"prepare\" function: %s\n", lua_tostring(L, -1));
1034 exit(1);
1036 while (1) {
1037 struct moonbr_listener *listener;
1038 if (setitimer(ITIMER_REAL, &notimer, NULL)) {
1039 moonbr_child_log_errno_fatal("Could not reset ITIMER_REAL via setitimer()");
1041 controlmsg = MOONBR_STATUS_IDLE;
1042 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
1043 moonbr_child_log_errno_fatal("Error while sending ready message to parent process");
1045 moonbr_child_receive_control_message(
1046 MOONBR_FD_CONTROL,
1047 &controlmsg,
1048 &moonbr_child_peersocket_fd
1049 );
1050 if (!(
1051 (controlmsg == MOONBR_COMMAND_TERMINATE && moonbr_child_peersocket_fd == -1) ||
1052 (controlmsg == MOONBR_SOCKETTYPE_INTERVAL && moonbr_child_peersocket_fd == -1) ||
1053 (controlmsg == MOONBR_SOCKETTYPE_LOCAL && moonbr_child_peersocket_fd != -1) ||
1054 (controlmsg == MOONBR_SOCKETTYPE_NETWORK && moonbr_child_peersocket_fd != -1)
1055 )) {
1056 moonbr_child_log_fatal("Received illegal control message from parent process");
1058 if (controlmsg == MOONBR_COMMAND_TERMINATE) break;
1059 listener = moonbr_child_receive_pointer(MOONBR_FD_CONTROL);
1060 moonbr_child_peersocket_type = controlmsg;
1061 if (moonbr_child_peersocket_fd != -1) {
1063 int clonedfd;
1064 clonedfd = dup(moonbr_child_peersocket_fd);
1065 if (!clonedfd) {
1066 moonbr_child_log_errno_fatal("Could not duplicate file descriptor for input stream");
1068 moonbr_child_peersocket_inputstream = lua_newuserdata(L, sizeof(luaL_Stream));
1069 if (!moonbr_child_peersocket_inputstream) {
1070 moonbr_child_log_fatal("Memory allocation error");
1072 moonbr_child_peersocket_inputstream->f = fdopen(clonedfd, "rb");
1073 if (!moonbr_child_peersocket_inputstream->f) {
1074 moonbr_child_log_errno_fatal("Could not open input stream for remote connection");
1076 moonbr_child_peersocket_inputstream->closef = moonbr_child_lua_close_stream;
1077 if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
1078 moonbr_child_log_fatal("Lua metatable LUA_FILEHANDLE does not exist");
1080 lua_setmetatable(L, -2);
1083 int clonedfd;
1084 clonedfd = dup(moonbr_child_peersocket_fd);
1085 if (!clonedfd) {
1086 moonbr_child_log_errno_fatal("Could not duplicate file descriptor for output stream");
1088 moonbr_child_peersocket_outputstream = lua_newuserdata(L, sizeof(luaL_Stream));
1089 if (!moonbr_child_peersocket_outputstream) {
1090 moonbr_child_log_fatal("Memory allocation error");
1092 moonbr_child_peersocket_outputstream->f = fdopen(clonedfd, "wb");
1093 if (!moonbr_child_peersocket_outputstream->f) {
1094 moonbr_child_log_errno_fatal("Could not open output stream for remote connection");
1096 moonbr_child_peersocket_outputstream->closef = moonbr_child_lua_close_stream;
1097 if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
1098 moonbr_child_log_fatal("Lua metatable LUA_FILEHANDLE does not exist");
1100 lua_setmetatable(L, -2);
1103 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
1104 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1105 lua_newtable(L);
1106 lua_pushstring(L,
1107 listener->proto_specific.interval.name ?
1108 listener->proto_specific.interval.name : ""
1109 );
1110 lua_setfield(L, -2, "interval");
1111 } else {
1112 lua_newtable(L);
1113 lua_pushvalue(L, -4);
1114 lua_setfield(L, -2, "input");
1115 lua_pushvalue(L, -3);
1116 lua_setfield(L, -2, "output");
1117 luaL_setfuncs(L, moonbr_child_lua_socket_functions, 0);
1118 if (listener->proto == MOONBR_PROTO_TCP6) {
1119 struct sockaddr_in6 addr;
1120 socklen_t addr_len = sizeof(struct sockaddr_in6);
1121 if (getsockname(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1122 moonbr_child_log_errno("Could not get local IP address/port");
1123 } else {
1124 lua_pushlstring(L, (char *)addr.sin6_addr.s6_addr, 16);
1125 lua_setfield(L, -2, "local_ip6");
1126 lua_pushinteger(L, ntohs(addr.sin6_port));
1127 lua_setfield(L, -2, "local_tcpport");
1129 if (getpeername(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1130 moonbr_child_log_errno("Could not get remote IP address/port");
1131 } else {
1132 lua_pushlstring(L, (char *)addr.sin6_addr.s6_addr, 16);
1133 lua_setfield(L, -2, "remote_ip6");
1134 lua_pushinteger(L, ntohs(addr.sin6_port));
1135 lua_setfield(L, -2, "remote_tcpport");
1137 } else if (listener->proto == MOONBR_PROTO_TCP4) {
1138 struct sockaddr_in addr;
1139 socklen_t addr_len = sizeof(struct sockaddr_in);
1140 if (getsockname(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1141 moonbr_child_log_errno("Could not get local IP address/port");
1142 } else {
1143 lua_pushlstring(L, (char *)&addr.sin_addr.s_addr, 4);
1144 lua_setfield(L, -2, "local_ip4");
1145 lua_pushinteger(L, ntohs(addr.sin_port));
1146 lua_setfield(L, -2, "local_tcpport");
1148 if (getpeername(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1149 moonbr_child_log_errno("Could not get remote IP address/port");
1150 } else {
1151 lua_pushlstring(L, (char *)&addr.sin_addr.s_addr, 4);
1152 lua_setfield(L, -2, "remote_ip4");
1153 lua_pushinteger(L, ntohs(addr.sin_port));
1154 lua_setfield(L, -2, "remote_tcpport");
1158 if (lua_pcall(L, 1, 1, 1)) {
1159 fprintf(stderr, "Error in \"connect\" function: %s\n", lua_tostring(L, -1));
1160 exit(1);
1162 if (moonbr_child_close_peersocket(0)) {
1163 moonbr_child_log_errno("Could not close socket connection with peer");
1165 if (lua_type(L, -1) != LUA_TBOOLEAN || !lua_toboolean(L, -1)) break;
1166 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
1167 lua_settop(L, 2);
1168 #else
1169 lua_settop(L, 1);
1170 #endif
1172 controlmsg = MOONBR_STATUS_GOODBYE;
1173 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
1174 moonbr_child_log_errno_fatal("Error while sending goodbye message to parent process");
1176 if (close(MOONBR_FD_CONTROL) && errno != EINTR) {
1177 moonbr_child_log_errno("Error while closing control socket");
1179 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
1180 if (lua_isnil(L, -1)) lua_pop(L, 1);
1181 else if (lua_pcall(L, 0, 0, 1)) {
1182 fprintf(stderr, "Error in \"finish\" function: %s\n", lua_tostring(L, -1));
1183 exit(1);
1185 lua_close(L);
1186 exit(0);
1190 /*** Functions to spawn child process ***/
1192 /* Helper function to send an error message to a file descriptor (not needing a file stream) */
1193 static void moonbr_child_emergency_print(int fd, char *message) {
1194 size_t len = strlen(message);
1195 ssize_t written;
1196 while (len) {
1197 written = write(fd, message, len);
1198 if (written > 0) {
1199 message += written;
1200 len -= written;
1201 } else {
1202 if (written != -1 || errno != EINTR) break;
1207 /* Helper function to send an error message plus a text for errno to a file descriptor and terminate the process */
1208 static void moonbr_child_emergency_error(int fd, char *message) {
1209 int errno2 = errno;
1210 moonbr_child_emergency_print(fd, message);
1211 moonbr_child_emergency_print(fd, ": ");
1212 moonbr_child_emergency_print(fd, strerror(errno2));
1213 moonbr_child_emergency_print(fd, "\n");
1214 exit(1);
1217 /* Creates a child process and (in case of success) registers it in the 'struct moonbr_pool' structure */
1218 static int moonbr_create_worker(struct moonbr_pool *pool, lua_State *L) {
1219 struct moonbr_worker *worker;
1220 worker = calloc(1, sizeof(struct moonbr_worker));
1221 if (!worker) {
1222 moonbr_log(LOG_CRIT, "Memory allocation error");
1223 return -1;
1225 worker->pool = pool;
1227 int controlfds[2];
1228 int errorfds[2];
1229 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, controlfds)) {
1230 moonbr_log(LOG_ERR, "Could not create control socket pair for communcation with child process: %s", strerror(errno));
1231 free(worker);
1232 return -1;
1234 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, errorfds)) {
1235 moonbr_log(LOG_ERR, "Could not create socket pair to redirect stderr of child process: %s", strerror(errno));
1236 close(controlfds[0]);
1237 close(controlfds[1]);
1238 free(worker);
1239 return -1;
1241 if (moonbr_logfile && fflush(moonbr_logfile)) {
1242 moonbr_log(LOG_CRIT, "Could not flush log file prior to forking: %s", strerror(errno));
1243 moonbr_terminate_error();
1245 worker->pid = fork();
1246 if (worker->pid == -1) {
1247 moonbr_log(LOG_ERR, "Could not fork: %s", strerror(errno));
1248 close(controlfds[0]);
1249 close(controlfds[1]);
1250 close(errorfds[0]);
1251 close(errorfds[1]);
1252 free(worker);
1253 return -1;
1254 } else if (!worker->pid) {
1255 moonbr_pstate = MOONBR_PSTATE_FORKED;
1256 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
1257 lua_pushliteral(L, "Failed to pass error message due to bug in Lua panic handler (hint: not enough memory?)");
1258 #endif
1259 moonbr_memory_limit = pool->memory_limit;
1260 if (moonbr_pidfh && pidfile_close(moonbr_pidfh)) {
1261 moonbr_child_emergency_error(errorfds[1], "Could not close PID file in forked child process");
1263 if (moonbr_logfile && moonbr_logfile != stderr && fclose(moonbr_logfile)) {
1264 moonbr_child_emergency_error(errorfds[1], "Could not close log file in forked child process");
1266 if (dup2(errorfds[1], MOONBR_FD_STDERR) == -1) {
1267 moonbr_child_emergency_error(errorfds[1], "Could not duplicate socket to stderr file descriptor");
1269 if (dup2(controlfds[1], MOONBR_FD_CONTROL) == -1) {
1270 moonbr_child_emergency_error(errorfds[1], "Could not duplicate control socket");
1272 closefrom(MOONBR_FD_END);
1273 moonbr_child_run(pool, L);
1275 if (moonbr_stat) {
1276 moonbr_log(LOG_INFO, "Created new worker in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1278 worker->controlfd = controlfds[0];
1279 worker->errorfd = errorfds[0];
1280 if (close(controlfds[1]) && errno != EINTR) {
1281 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
1282 moonbr_terminate_error();
1284 if (close(errorfds[1]) && errno != EINTR) {
1285 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
1286 moonbr_terminate_error();
1289 worker->prev_worker = pool->last_worker;
1290 if (worker->prev_worker) worker->prev_worker->next_worker = worker;
1291 else pool->first_worker = worker;
1292 pool->last_worker = worker;
1293 pool->unassigned_worker_count++;
1294 pool->total_worker_count++;
1295 pool->worker_count_stat = 1;
1296 moonbr_poll_refresh_needed = 1;
1297 return 0; /* return zero only in case of success */
1301 /*** Functions to handle previously created 'struct moonbr_worker' structures ***/
1303 #define moonbr_try_destroy_worker_stat(str, field) \
1304 moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: " str " %li", worker->pool->poolnum, (int)worker->pid, (long)childusage.field);
1306 /* Destroys a worker structure if socket connections have been closed and child process has terminated */
1307 static int moonbr_try_destroy_worker(struct moonbr_worker *worker) {
1308 if (worker->controlfd != -1 || worker->errorfd != -1) return MOONBR_DESTROY_NONE;
1310 int childstatus;
1311 struct rusage childusage;
1313 pid_t waitedpid;
1314 while (
1315 (waitedpid = wait4(worker->pid, &childstatus, WNOHANG, &childusage)) == -1
1316 ) {
1317 if (errno != EINTR) {
1318 moonbr_log(LOG_CRIT, "Error in wait4() call: %s", strerror(errno));
1319 moonbr_terminate_error();
1322 if (!waitedpid) return 0; /* return 0 if worker couldn't be destroyed */
1323 if (waitedpid != worker->pid) {
1324 moonbr_log(LOG_CRIT, "Wrong PID returned by wait4() call");
1325 moonbr_terminate_error();
1328 if (WIFEXITED(childstatus)) {
1329 if (WEXITSTATUS(childstatus) || moonbr_stat) {
1330 moonbr_log(
1331 WEXITSTATUS(childstatus) ? LOG_WARNING : LOG_INFO,
1332 "Child process in pool #%i with PID %i returned with exit code %i", worker->pool->poolnum, (int)worker->pid, WEXITSTATUS(childstatus)
1333 );
1335 } else if (WIFSIGNALED(childstatus)) {
1336 if (WCOREDUMP(childstatus)) {
1337 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));
1338 } else if (WTERMSIG(childstatus) == SIGALRM) {
1339 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i exited prematurely due to timeout", worker->pool->poolnum, (int)worker->pid);
1340 } else {
1341 moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
1343 } else {
1344 moonbr_log(LOG_CRIT, "Illegal exit status from child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1345 moonbr_terminate_error();
1347 if (moonbr_stat) {
1348 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));
1349 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));
1350 moonbr_try_destroy_worker_stat("max resident set size", ru_maxrss);
1351 moonbr_try_destroy_worker_stat("integral shared memory size", ru_ixrss);
1352 moonbr_try_destroy_worker_stat("integral unshared data", ru_idrss);
1353 moonbr_try_destroy_worker_stat("integral unshared stack", ru_isrss);
1354 moonbr_try_destroy_worker_stat("page replaims", ru_minflt);
1355 moonbr_try_destroy_worker_stat("page faults", ru_majflt);
1356 moonbr_try_destroy_worker_stat("swaps", ru_nswap);
1357 moonbr_try_destroy_worker_stat("block input operations", ru_inblock);
1358 moonbr_try_destroy_worker_stat("block output operations", ru_oublock);
1359 moonbr_try_destroy_worker_stat("messages sent", ru_msgsnd);
1360 moonbr_try_destroy_worker_stat("messages received", ru_msgrcv);
1361 moonbr_try_destroy_worker_stat("signals received", ru_nsignals);
1362 moonbr_try_destroy_worker_stat("voluntary context switches", ru_nvcsw);
1363 moonbr_try_destroy_worker_stat("involuntary context switches", ru_nivcsw);
1367 int retval = (
1368 (worker->idle || worker->assigned) ?
1369 MOONBR_DESTROY_IDLE_OR_ASSIGNED :
1370 MOONBR_DESTROY_PREPARE
1371 );
1372 if (worker->prev_worker) worker->prev_worker->next_worker = worker->next_worker;
1373 else worker->pool->first_worker = worker->next_worker;
1374 if (worker->next_worker) worker->next_worker->prev_worker = worker->prev_worker;
1375 else worker->pool->last_worker = worker->prev_worker;
1376 if (worker->idle) {
1377 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker->next_idle_worker;
1378 else worker->pool->first_idle_worker = worker->next_idle_worker;
1379 if (worker->next_idle_worker) worker->next_idle_worker->prev_idle_worker = worker->prev_idle_worker;
1380 else worker->pool->last_idle_worker = worker->prev_idle_worker;
1381 worker->pool->idle_worker_count--;
1383 if (!worker->assigned) worker->pool->unassigned_worker_count--;
1384 worker->pool->total_worker_count--;
1385 worker->pool->worker_count_stat = 1;
1386 if (worker->errorlinebuf) free(worker->errorlinebuf);
1387 free(worker);
1388 return retval;
1392 /* Marks a worker as idle and stores it in a queue, optionally setting 'idle_expiration' value */
1393 static void moonbr_add_idle_worker(struct moonbr_worker *worker) {
1394 worker->prev_idle_worker = worker->pool->last_idle_worker;
1395 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker;
1396 else worker->pool->first_idle_worker = worker;
1397 worker->pool->last_idle_worker = worker;
1398 worker->idle = 1;
1399 worker->pool->idle_worker_count++;
1400 if (worker->assigned) {
1401 worker->assigned = 0;
1402 worker->pool->unassigned_worker_count++;
1404 worker->pool->worker_count_stat = 1;
1405 if (timerisset(&worker->pool->idle_timeout)) {
1406 struct timeval now;
1407 moonbr_now(&now);
1408 timeradd(&now, &worker->pool->idle_timeout, &worker->idle_expiration);
1412 /* Pops a worker from the queue of idle workers (idle queue must not be empty) */
1413 static struct moonbr_worker *moonbr_pop_idle_worker(struct moonbr_pool *pool) {
1414 struct moonbr_worker *worker;
1415 worker = pool->first_idle_worker;
1416 pool->first_idle_worker = worker->next_idle_worker;
1417 if (pool->first_idle_worker) pool->first_idle_worker->prev_idle_worker = NULL;
1418 else pool->last_idle_worker = NULL;
1419 worker->next_idle_worker = NULL;
1420 worker->idle = 0;
1421 worker->pool->idle_worker_count--;
1422 worker->assigned = 1;
1423 worker->pool->unassigned_worker_count--;
1424 worker->pool->worker_count_stat = 1;
1425 return worker;
1429 /*** Functions for queues of 'struct moonbr_listener' ***/
1431 /* Appends a 'struct moonbr_listener' to the queue of idle listeners and registers it for poll() */
1432 static void moonbr_add_idle_listener(struct moonbr_listener *listener) {
1433 listener->prev_listener = listener->pool->last_idle_listener;
1434 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
1435 else listener->pool->first_idle_listener = listener;
1436 listener->pool->last_idle_listener = listener;
1437 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events |= POLLIN;
1440 /* Removes a 'struct moonbr_listener' from the queue of idle listeners and unregisters it from poll() */
1441 static void moonbr_remove_idle_listener(struct moonbr_listener *listener) {
1442 if (listener->prev_listener) listener->prev_listener->next_listener = listener->next_listener;
1443 else listener->pool->first_idle_listener = listener->next_listener;
1444 if (listener->next_listener) listener->next_listener->prev_listener = listener->prev_listener;
1445 else listener->pool->last_idle_listener = listener->prev_listener;
1446 listener->prev_listener = NULL;
1447 listener->next_listener = NULL;
1448 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events &= ~POLLIN;
1451 /* Adds a listener to the queue of connected listeners (i.e. waiting to have their incoming connection accepted) */
1452 static void moonbr_add_connected_listener(struct moonbr_listener *listener) {
1453 listener->prev_listener = listener->pool->last_connected_listener;
1454 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
1455 else listener->pool->first_connected_listener = listener;
1456 listener->pool->last_connected_listener = listener;
1459 /* Removes and returns the first connected listener in the queue */
1460 static struct moonbr_listener *moonbr_pop_connected_listener(struct moonbr_pool *pool) {
1461 struct moonbr_listener *listener = pool->first_connected_listener;
1462 listener->pool->first_connected_listener = listener->next_listener;
1463 if (listener->pool->first_connected_listener) listener->pool->first_connected_listener->prev_listener = NULL;
1464 else listener->pool->last_connected_listener = NULL;
1465 listener->next_listener = NULL;
1466 return listener;
1470 /*** Functions to handle polling ***/
1472 /* Returns an index to a new initialized entry in moonbr_poll_fds[] */
1473 int moonbr_poll_fds_nextindex() {
1474 if (moonbr_poll_fds_count >= moonbr_poll_fds_bufsize) {
1475 if (moonbr_poll_fds_bufsize) moonbr_poll_fds_bufsize *= 2;
1476 else moonbr_poll_fds_bufsize = 1;
1477 moonbr_poll_fds = realloc(
1478 moonbr_poll_fds, moonbr_poll_fds_bufsize * sizeof(struct pollfd)
1479 );
1480 if (!moonbr_poll_fds) {
1481 moonbr_log(LOG_CRIT, "Memory allocation error");
1482 moonbr_terminate_error();
1485 moonbr_poll_fds[moonbr_poll_fds_count] = (struct pollfd){0, };
1486 return moonbr_poll_fds_count++;
1489 /* Returns an index to a new initialized entry in moonbr_poll_workers[] */
1490 int moonbr_poll_workers_nextindex() {
1491 if (moonbr_poll_worker_count >= moonbr_poll_workers_bufsize) {
1492 if (moonbr_poll_workers_bufsize) moonbr_poll_workers_bufsize *= 2;
1493 else moonbr_poll_workers_bufsize = 1;
1494 moonbr_poll_workers = realloc(
1495 moonbr_poll_workers, moonbr_poll_workers_bufsize * sizeof(struct moonbr_poll_worker)
1496 );
1497 if (!moonbr_poll_workers) {
1498 moonbr_log(LOG_CRIT, "Memory allocation error");
1499 moonbr_terminate_error();
1502 moonbr_poll_workers[moonbr_poll_worker_count] = (struct moonbr_poll_worker){0, };
1503 return moonbr_poll_worker_count++;
1506 /* Queues all listeners as idle, and initializes static part of moonbr_poll_fds[], which is passed to poll() */
1507 static void moonbr_poll_init() {
1508 if (socketpair(
1509 PF_LOCAL,
1510 SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1511 0,
1512 moonbr_poll_signalfds
1513 )) {
1514 moonbr_log(LOG_CRIT, "Could not create socket pair for signal delivery during polling: %s", strerror(errno));
1515 moonbr_terminate_error();
1518 int j = moonbr_poll_fds_nextindex();
1519 struct pollfd *pollfd = &moonbr_poll_fds[j];
1520 pollfd->fd = moonbr_poll_signalfd_read;
1521 pollfd->events = POLLIN;
1524 struct moonbr_pool *pool;
1525 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1526 int i;
1527 for (i=0; i<pool->listener_count; i++) {
1528 struct moonbr_listener *listener = &pool->listener[i];
1529 if (listener->listenfd != -1) {
1530 int j = moonbr_poll_fds_nextindex();
1531 listener->pollidx = j;
1532 moonbr_poll_fds[j].fd = listener->listenfd;
1534 moonbr_add_idle_listener(listener);
1538 moonbr_poll_fds_static_count = moonbr_poll_fds_count; /* remember size of static part of array */
1541 /* Disables polling of all listeners (required for clean shutdown) */
1542 static void moonbr_poll_shutdown() {
1543 int i;
1544 for (i=1; i<moonbr_poll_fds_static_count; i++) {
1545 moonbr_poll_fds[i].fd = -1;
1549 /* (Re)builds dynamic part of moonbr_poll_fds[] array, and (re)builds moonbr_poll_workers[] array */
1550 static void moonbr_poll_refresh() {
1551 moonbr_poll_refresh_needed = 0;
1552 moonbr_poll_fds_count = moonbr_poll_fds_static_count;
1553 moonbr_poll_worker_count = 0;
1555 struct moonbr_pool *pool;
1556 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1557 struct moonbr_worker *worker;
1558 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
1559 if (worker->controlfd != -1) {
1560 int j = moonbr_poll_fds_nextindex();
1561 int k = moonbr_poll_workers_nextindex();
1562 struct pollfd *pollfd = &moonbr_poll_fds[j];
1563 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1564 pollfd->fd = worker->controlfd;
1565 pollfd->events = POLLIN;
1566 poll_worker->channel = MOONBR_POLL_WORKER_CONTROLCHANNEL;
1567 poll_worker->worker = worker;
1569 if (worker->errorfd != -1) {
1570 int j = moonbr_poll_fds_nextindex();
1571 int k = moonbr_poll_workers_nextindex();
1572 struct pollfd *pollfd = &moonbr_poll_fds[j];
1573 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1574 pollfd->fd = worker->errorfd;
1575 pollfd->events = POLLIN;
1576 poll_worker->channel = MOONBR_POLL_WORKER_ERRORCHANNEL;
1577 poll_worker->worker = worker;
1584 /* resets socket and 'revents' field of moonbr_poll_fds[] for signal delivery just before poll() is called */
1585 static void moonbr_poll_reset_signal() {
1586 ssize_t readcount;
1587 char buf[1];
1588 moonbr_poll_fds[0].revents = 0;
1589 while ((readcount = read(moonbr_poll_signalfd_read, buf, 1)) < 0) {
1590 if (errno == EAGAIN) break;
1591 if (errno != EINTR) {
1592 moonbr_log(LOG_CRIT, "Error while reading from signal delivery socket: %s", strerror(errno));
1593 moonbr_terminate_error();
1596 if (!readcount) {
1597 moonbr_log(LOG_CRIT, "Unexpected EOF when reading from signal delivery socket: %s", strerror(errno));
1598 moonbr_terminate_error();
1603 /*** Shutdown initiation ***/
1605 /* Sets global variable 'moonbr_shutdown_in_progress', closes listeners, and demands worker termination */
1606 static void moonbr_initiate_shutdown() {
1607 struct moonbr_pool *pool;
1608 int i;
1609 if (moonbr_shutdown_in_progress) {
1610 moonbr_log(LOG_NOTICE, "Shutdown already in progress");
1611 return;
1613 moonbr_shutdown_in_progress = 1;
1614 moonbr_log(LOG_NOTICE, "Initiate shutdown");
1615 for (pool = moonbr_first_pool; pool; pool = pool->next_pool) {
1616 for (i=0; i<pool->listener_count; i++) {
1617 struct moonbr_listener *listener = &pool->listener[i];
1618 if (listener->listenfd != -1) {
1619 if (close(listener->listenfd) && errno != EINTR) {
1620 moonbr_log(LOG_CRIT, "Could not close listening socket: %s", strerror(errno));
1621 moonbr_terminate_error();
1625 pool->pre_fork = 0;
1626 pool->min_fork = 0;
1627 pool->max_fork = 0;
1628 timerclear(&pool->exit_delay);
1630 moonbr_poll_shutdown(); /* avoids loops due to error condition when polling closed listeners */
1634 /*** Functions to communicate with child processes ***/
1636 /* Tells child process to terminate */
1637 static void moonbr_terminate_idle_worker(struct moonbr_worker *worker) {
1638 moonbr_send_control_message(worker, MOONBR_COMMAND_TERMINATE, -1, NULL);
1641 /* Handles status messages from child process */
1642 static void moonbr_read_controlchannel(struct moonbr_worker *worker) {
1643 char controlmsg;
1645 ssize_t bytes_read;
1646 while ((bytes_read = read(worker->controlfd, &controlmsg, 1)) <= 0) {
1647 if (bytes_read == 0 || errno == ECONNRESET) {
1648 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i unexpectedly closed control socket", worker->pool->poolnum, (int)worker->pid);
1649 if (close(worker->controlfd) && errno != EINTR) {
1650 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));
1651 moonbr_terminate_error();
1653 worker->controlfd = -1;
1654 moonbr_poll_refresh_needed = 1;
1655 return;
1657 if (errno != EINTR) {
1658 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));
1659 moonbr_terminate_error();
1663 if (worker->idle) {
1664 moonbr_log(LOG_CRIT, "Unexpected data from supposedly idle child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1665 moonbr_terminate_error();
1667 if (moonbr_debug) {
1668 moonbr_log(LOG_DEBUG, "Received control message from child in pool #%i with PID %i: \"%c\"", worker->pool->poolnum, (int)worker->pid, (int)controlmsg);
1670 switch (controlmsg) {
1671 case MOONBR_STATUS_IDLE:
1672 if (moonbr_stat) {
1673 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i reports as idle", worker->pool->poolnum, (int)worker->pid);
1675 moonbr_add_idle_worker(worker);
1676 break;
1677 case MOONBR_STATUS_GOODBYE:
1678 if (moonbr_stat) {
1679 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i announced termination", worker->pool->poolnum, (int)worker->pid);
1681 if (close(worker->controlfd) && errno != EINTR) {
1682 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));
1683 moonbr_terminate_error();
1685 worker->controlfd = -1;
1686 moonbr_poll_refresh_needed = 1;
1687 break;
1688 default:
1689 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);
1690 moonbr_terminate_error();
1694 /* Handles stderr stream from child process */
1695 static void moonbr_read_errorchannel(struct moonbr_worker *worker) {
1696 char staticbuf[MOONBR_MAXERRORLINELEN+1];
1697 char *buf = worker->errorlinebuf;
1698 if (!buf) buf = staticbuf;
1700 ssize_t bytes_read;
1701 while (
1702 (bytes_read = read(
1703 worker->errorfd,
1704 buf + worker->errorlinelen,
1705 MOONBR_MAXERRORLINELEN+1 - worker->errorlinelen
1706 )) <= 0
1707 ) {
1708 if (bytes_read == 0 || errno == ECONNRESET) {
1709 if (moonbr_debug) {
1710 moonbr_log(LOG_DEBUG, "Child process in pool #%i with PID %i closed stderr socket", worker->pool->poolnum, (int)worker->pid);
1712 if (close(worker->errorfd) && errno != EINTR) {
1713 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));
1714 moonbr_terminate_error();
1716 worker->errorfd = -1;
1717 moonbr_poll_refresh_needed = 1;
1718 break;
1720 if (errno != EINTR) {
1721 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));
1722 moonbr_terminate_error();
1725 worker->errorlinelen += bytes_read;
1728 int i;
1729 for (i=0; i<worker->errorlinelen; i++) {
1730 if (buf[i] == '\n') buf[i] = 0;
1731 if (!buf[i]) {
1732 if (worker->errorlineovf) {
1733 worker->errorlineovf = 0;
1734 } else {
1735 moonbr_log(LOG_WARNING, "Error log from process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, buf);
1737 worker->errorlinelen -= i+1;
1738 memmove(buf, buf+i+1, worker->errorlinelen);
1739 i = -1;
1742 if (i > MOONBR_MAXERRORLINELEN) {
1743 buf[MOONBR_MAXERRORLINELEN] = 0;
1744 if (!worker->errorlineovf) {
1745 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);
1747 worker->errorlinelen = 0;
1748 worker->errorlineovf = 1;
1751 if (!worker->errorlinebuf && worker->errorlinelen) { /* allocate buffer on heap only if necessary */
1752 worker->errorlinebuf = malloc((MOONBR_MAXERRORLINELEN+1) * sizeof(char));
1753 if (!worker->errorlinebuf) {
1754 moonbr_log(LOG_CRIT, "Memory allocation error");
1755 moonbr_terminate_error();
1757 memcpy(worker->errorlinebuf, staticbuf, worker->errorlinelen);
1762 /*** Handler for incoming connections ***/
1764 /* Accepts one or more incoming connections on listener socket and passes it to worker(s) popped from idle queue */
1765 static void moonbr_connect(struct moonbr_pool *pool) {
1766 struct moonbr_listener *listener = moonbr_pop_connected_listener(pool);
1767 struct moonbr_worker *worker;
1768 switch (listener->proto) {
1769 case MOONBR_PROTO_INTERVAL:
1770 worker = moonbr_pop_idle_worker(pool);
1771 if (moonbr_stat) {
1772 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);
1774 worker->restart_interval_listener = listener;
1775 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_INTERVAL, -1, listener);
1776 /* do not push listener to queue of idle listeners yet */
1777 break;
1778 case MOONBR_PROTO_LOCAL:
1779 do {
1780 int peerfd;
1781 struct sockaddr_un peeraddr;
1782 socklen_t peeraddr_len = sizeof(struct sockaddr_un);
1783 peerfd = accept4(
1784 listener->listenfd,
1785 (struct sockaddr *)&peeraddr,
1786 &peeraddr_len,
1787 SOCK_CLOEXEC
1788 );
1789 if (peerfd == -1) {
1790 if (errno == EWOULDBLOCK) {
1791 break;
1792 } else if (errno == ECONNABORTED) {
1793 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"local\", path=\"%s\")", listener->proto_specific.local.path);
1794 break;
1795 } else if (errno != EINTR) {
1796 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1797 moonbr_terminate_error();
1799 } else {
1800 worker = moonbr_pop_idle_worker(pool);
1801 if (moonbr_stat) {
1802 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);
1804 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_LOCAL, peerfd, listener);
1805 if (close(peerfd) && errno != EINTR) {
1806 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1807 moonbr_terminate_error();
1810 } while (pool->first_idle_worker);
1811 moonbr_add_idle_listener(listener);
1812 break;
1813 case MOONBR_PROTO_TCP6:
1814 do {
1815 int peerfd;
1816 struct sockaddr_in6 peeraddr;
1817 socklen_t peeraddr_len = sizeof(struct sockaddr_in6);
1818 peerfd = accept4(
1819 listener->listenfd,
1820 (struct sockaddr *)&peeraddr,
1821 &peeraddr_len,
1822 SOCK_CLOEXEC
1823 );
1824 if (peerfd == -1) {
1825 if (errno == EWOULDBLOCK) {
1826 break;
1827 } else if (errno == ECONNABORTED) {
1828 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"tcp6\", port=%i)", listener->proto_specific.tcp.port);
1829 break;
1830 } else if (errno != EINTR) {
1831 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1832 moonbr_terminate_error();
1834 } else {
1835 worker = moonbr_pop_idle_worker(pool);
1836 if (moonbr_stat) {
1837 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);
1839 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_NETWORK, peerfd, listener);
1840 if (close(peerfd) && errno != EINTR) {
1841 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1842 moonbr_terminate_error();
1845 } while (pool->first_idle_worker);
1846 moonbr_add_idle_listener(listener);
1847 break;
1848 case MOONBR_PROTO_TCP4:
1849 do {
1850 int peerfd;
1851 struct sockaddr_in peeraddr;
1852 socklen_t peeraddr_len = sizeof(struct sockaddr_in);
1853 peerfd = accept4(
1854 listener->listenfd,
1855 (struct sockaddr *)&peeraddr,
1856 &peeraddr_len,
1857 SOCK_CLOEXEC
1858 );
1859 if (peerfd == -1) {
1860 if (errno == EWOULDBLOCK) {
1861 break;
1862 } else if (errno == ECONNABORTED) {
1863 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"tcp4\", port=%i)", listener->proto_specific.tcp.port);
1864 break;
1865 } else if (errno != EINTR) {
1866 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1867 moonbr_terminate_error();
1869 } else {
1870 worker = moonbr_pop_idle_worker(pool);
1871 if (moonbr_stat) {
1872 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);
1874 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_NETWORK, peerfd, listener);
1875 if (close(peerfd) && errno != EINTR) {
1876 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1877 moonbr_terminate_error();
1880 } while (pool->first_idle_worker);
1881 moonbr_add_idle_listener(listener);
1882 break;
1883 default:
1884 moonbr_log(LOG_ERR, "Internal error (should not happen): Unexpected value in listener.proto field");
1885 moonbr_terminate_error();
1890 /*** Functions to initialize and restart interval timers ***/
1892 /* Initializes all interval timers */
1893 static void moonbr_interval_initialize() {
1894 struct timeval now;
1895 struct moonbr_pool *pool;
1896 moonbr_now(&now);
1897 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1898 int i;
1899 for (i=0; i<pool->listener_count; i++) {
1900 struct moonbr_listener *listener = &pool->listener[i];
1901 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1902 timeradd(
1903 &now,
1904 &listener->proto_specific.interval.delay,
1905 &listener->proto_specific.interval.wakeup
1906 );
1912 /* If necessary, restarts interval timers and queues interval listener as idle after a worker changed status */
1913 static void moonbr_interval_restart(
1914 struct moonbr_worker *worker,
1915 struct timeval *now /* passed to synchronize with moonbr_run() function */
1916 ) {
1917 struct moonbr_listener *listener = worker->restart_interval_listener;
1918 if (listener) {
1919 moonbr_add_idle_listener(listener);
1920 worker->restart_interval_listener = NULL;
1921 if (listener->proto_specific.interval.strict) {
1922 timeradd(
1923 &listener->proto_specific.interval.wakeup,
1924 &listener->proto_specific.interval.delay,
1925 &listener->proto_specific.interval.wakeup
1926 );
1927 if (timercmp(&listener->proto_specific.interval.wakeup, now, <)) {
1928 listener->proto_specific.interval.wakeup = *now;
1930 } else {
1931 timeradd(
1932 now,
1933 &listener->proto_specific.interval.delay,
1934 &listener->proto_specific.interval.wakeup
1935 );
1941 /*** Main loop and helper functions ***/
1943 /* Stores the earliest required wakeup time in 'wait' variable */
1944 static void moonbr_calc_wait(struct timeval *wait, struct timeval *wakeup) {
1945 if (!timerisset(wait) || timercmp(wakeup, wait, <)) *wait = *wakeup;
1948 /* Main loop of Moonbridge system (including initialization of signal handlers and polling structures) */
1949 static void moonbr_run(lua_State *L) {
1950 struct timeval now;
1951 struct moonbr_pool *pool;
1952 struct moonbr_worker *worker;
1953 struct moonbr_worker *next_worker; /* needed when worker is removed during iteration of workers */
1954 struct moonbr_listener *listener;
1955 struct moonbr_listener *next_listener; /* needed when listener is removed during iteration of listeners */
1956 int i;
1957 moonbr_poll_init(); /* must be executed before moonbr_signal_init() */
1958 moonbr_signal_init();
1959 moonbr_interval_initialize();
1960 moonbr_pstate = MOONBR_PSTATE_RUNNING;
1961 while (1) {
1962 struct timeval wait = {0, }; /* point in time when premature wakeup of poll() is required */
1963 if (moonbr_cond_interrupt) {
1964 moonbr_log(LOG_WARNING, "Fast shutdown requested");
1965 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
1967 if (moonbr_cond_terminate) {
1968 moonbr_initiate_shutdown();
1969 moonbr_cond_terminate = 0;
1971 moonbr_cond_child = 0; /* must not be reset between moonbr_try_destroy_worker() and poll() */
1972 moonbr_now(&now);
1973 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1974 int terminated_worker_count = 0; /* allows shortcut for new worker creation */
1975 /* terminate idle workers when expired */
1976 if (timerisset(&pool->idle_timeout)) {
1977 while ((worker = pool->first_idle_worker) != NULL) {
1978 if (timercmp(&worker->idle_expiration, &now, >)) break;
1979 moonbr_pop_idle_worker(pool);
1980 moonbr_terminate_idle_worker(worker);
1983 /* mark listeners as connected when incoming connection is pending */
1984 for (listener=pool->first_idle_listener; listener; listener=next_listener) {
1985 next_listener = listener->next_listener; /* extra variable necessary due to changing list */
1986 if (listener->pollidx != -1) {
1987 if (moonbr_poll_fds[listener->pollidx].revents) {
1988 moonbr_poll_fds[listener->pollidx].revents = 0;
1989 moonbr_remove_idle_listener(listener);
1990 moonbr_add_connected_listener(listener);
1992 } else if (listener->proto == MOONBR_PROTO_INTERVAL) {
1993 if (!timercmp(&listener->proto_specific.interval.wakeup, &now, >)) {
1994 moonbr_remove_idle_listener(listener);
1995 moonbr_add_connected_listener(listener);
1997 } else {
1998 moonbr_log(LOG_CRIT, "Internal error (should not happen): Listener is neither an interval timer nor has the 'pollidx' value set");
1999 moonbr_terminate_error();
2002 /* process input from child processes */
2003 for (i=0; i<moonbr_poll_worker_count; i++) {
2004 if (moonbr_poll_worker_fds[i].revents) {
2005 moonbr_poll_worker_fds[i].revents = 0;
2006 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[i];
2007 switch (poll_worker->channel) {
2008 case MOONBR_POLL_WORKER_CONTROLCHANNEL:
2009 moonbr_read_controlchannel(poll_worker->worker);
2010 moonbr_interval_restart(poll_worker->worker, &now);
2011 break;
2012 case MOONBR_POLL_WORKER_ERRORCHANNEL:
2013 moonbr_read_errorchannel(poll_worker->worker);
2014 break;
2018 /* collect dead child processes */
2019 for (worker=pool->first_worker; worker; worker=next_worker) {
2020 next_worker = worker->next_worker; /* extra variable necessary due to changing list */
2021 switch (moonbr_try_destroy_worker(worker)) {
2022 case MOONBR_DESTROY_PREPARE:
2023 pool->use_fork_error_wakeup = 1;
2024 break;
2025 case MOONBR_DESTROY_IDLE_OR_ASSIGNED:
2026 terminated_worker_count++;
2027 break;
2030 /* connect listeners with idle workers */
2031 if (!moonbr_shutdown_in_progress) {
2032 while (pool->first_connected_listener && pool->first_idle_worker) {
2033 moonbr_connect(pool);
2036 /* create new worker processes */
2037 while (
2038 pool->total_worker_count < pool->max_fork && (
2039 pool->unassigned_worker_count < pool->pre_fork ||
2040 pool->total_worker_count < pool->min_fork
2042 ) {
2043 if (pool->use_fork_error_wakeup) {
2044 if (timercmp(&pool->fork_error_wakeup, &now, >)) {
2045 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
2046 break;
2048 } else {
2049 if (terminated_worker_count) {
2050 terminated_worker_count--;
2051 } else if (timercmp(&pool->fork_wakeup, &now, >)) {
2052 moonbr_calc_wait(&wait, &pool->fork_wakeup);
2053 break;
2056 if (moonbr_create_worker(pool, L)) {
2057 /* on error, enforce error delay */
2058 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
2059 pool->use_fork_error_wakeup = 1;
2060 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
2061 break;
2062 } else {
2063 /* normal fork delay on success */
2064 timeradd(&now, &pool->fork_delay, &pool->fork_wakeup);
2065 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
2066 pool->use_fork_error_wakeup = 0; /* gets set later if error occures during preparation */
2069 /* terminate excessive worker processes */
2070 while (
2071 pool->total_worker_count > pool->min_fork &&
2072 pool->idle_worker_count > pool->pre_fork
2073 ) {
2074 if (timerisset(&pool->exit_wakeup)) {
2075 if (timercmp(&pool->exit_wakeup, &now, >)) {
2076 moonbr_calc_wait(&wait, &pool->exit_wakeup);
2077 break;
2079 moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
2080 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
2081 } else {
2082 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
2083 break;
2086 if (!(
2087 pool->total_worker_count > pool->min_fork &&
2088 pool->idle_worker_count > pool->pre_fork
2089 )) {
2090 timerclear(&pool->exit_wakeup); /* timer gets restarted later when there are excessive workers */
2092 /* optionally output worker count stats */
2093 if (moonbr_stat && pool->worker_count_stat) {
2094 pool->worker_count_stat = 0;
2095 moonbr_log(
2096 LOG_INFO,
2097 "Worker count for pool #%i: %i idle, %i assigned, %i total",
2098 pool->poolnum, pool->idle_worker_count,
2099 pool->total_worker_count - pool->unassigned_worker_count,
2100 pool->total_worker_count);
2102 /* calculate wakeup time for interval listeners */
2103 for (listener=pool->first_idle_listener; listener; listener=listener->next_listener) {
2104 if (listener->proto == MOONBR_PROTO_INTERVAL) {
2105 moonbr_calc_wait(&wait, &listener->proto_specific.interval.wakeup);
2108 /* calculate wakeup time for idle workers (only first idle worker is significant) */
2109 if (timerisset(&pool->idle_timeout) && pool->first_idle_worker) {
2110 moonbr_calc_wait(&wait, &pool->first_idle_worker->idle_expiration);
2113 /* check if shutdown is complete */
2114 if (moonbr_shutdown_in_progress) {
2115 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
2116 if (pool->first_worker) break;
2118 if (!pool) {
2119 moonbr_log(LOG_INFO, "All worker threads have terminated");
2120 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
2123 if (moonbr_poll_refresh_needed) moonbr_poll_refresh();
2124 moonbr_cond_poll = 1;
2125 if (!moonbr_cond_child && !moonbr_cond_terminate && !moonbr_cond_interrupt) {
2126 int timeout;
2127 if (timerisset(&wait)) {
2128 if (timercmp(&wait, &now, <)) {
2129 moonbr_log(LOG_CRIT, "Internal error (should not happen): Future is in the past");
2130 moonbr_terminate_error();
2132 timersub(&wait, &now, &wait);
2133 timeout = wait.tv_sec * 1000 + wait.tv_usec / 1000;
2134 } else {
2135 timeout = INFTIM;
2137 if (moonbr_debug) {
2138 moonbr_log(LOG_DEBUG, "Waiting for I/O");
2140 poll(moonbr_poll_fds, moonbr_poll_fds_count, timeout);
2141 } else {
2142 if (moonbr_debug) {
2143 moonbr_log(LOG_DEBUG, "Do not wait for I/O");
2146 moonbr_cond_poll = 0;
2147 moonbr_poll_reset_signal();
2152 /*** Lua interface ***/
2154 static int moonbr_lua_panic(lua_State *L) {
2155 const char *errmsg;
2156 errmsg = lua_tostring(L, -1);
2157 if (!errmsg) {
2158 if (lua_isnoneornil(L, -1)) errmsg = "(error message is nil)";
2159 else errmsg = "(error message is not a string)";
2161 if (moonbr_pstate == MOONBR_PSTATE_FORKED) {
2162 fprintf(stderr, "Uncaught Lua error: %s\n", errmsg);
2163 exit(1);
2164 } else {
2165 moonbr_log(LOG_CRIT, "Uncaught Lua error: %s", errmsg);
2166 moonbr_terminate_error();
2168 return 0;
2171 static int moonbr_addtraceback(lua_State *L) {
2172 luaL_traceback(L, L, luaL_tolstring(L, 1, NULL), 1);
2173 return 1;
2176 /* Memory allocator that allows limiting memory consumption */
2177 static void *moonbr_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
2178 (void)ud; /* not used */
2179 if (nsize == 0) {
2180 if (ptr) {
2181 moonbr_memory_usage -= osize;
2182 free(ptr);
2184 return NULL;
2185 } else if (ptr) {
2186 if (
2187 moonbr_memory_limit &&
2188 nsize > osize &&
2189 moonbr_memory_usage + (nsize - osize) > moonbr_memory_limit
2190 ) {
2191 return NULL;
2192 } else {
2193 ptr = realloc(ptr, nsize);
2194 if (ptr) moonbr_memory_usage += nsize - osize;
2196 } else {
2197 if (
2198 moonbr_memory_limit &&
2199 moonbr_memory_usage + nsize > moonbr_memory_limit
2200 ) {
2201 return NULL;
2202 } else {
2203 ptr = realloc(ptr, nsize);
2204 if (ptr) moonbr_memory_usage += nsize;
2207 return ptr;
2210 /* New method for Lua file objects: read until terminator or length exceeded */
2211 static int moonbr_readuntil(lua_State *L) {
2212 luaL_Stream *stream;
2213 FILE *file;
2214 const char *terminatorstr;
2215 size_t terminatorlen;
2216 luaL_Buffer buf;
2217 lua_Integer maxlen;
2218 char terminator;
2219 int byte;
2220 stream = luaL_checkudata(L, 1, LUA_FILEHANDLE);
2221 terminatorstr = luaL_checklstring(L, 2, &terminatorlen);
2222 luaL_argcheck(L, terminatorlen == 1, 2, "single byte expected");
2223 maxlen = luaL_optinteger(L, 3, 0);
2224 if (!stream->closef) luaL_error(L, "attempt to use a closed file");
2225 file = stream->f;
2226 luaL_buffinit(L, &buf);
2227 if (!maxlen) maxlen = -1;
2228 terminator = terminatorstr[0];
2229 while (maxlen > 0 ? maxlen-- : maxlen) {
2230 byte = fgetc(file);
2231 if (byte == EOF) {
2232 if (ferror(file)) {
2233 char errmsg[MOONBR_MAXSTRERRORLEN];
2234 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
2235 luaL_error(L, "%s", errmsg);
2236 } else {
2237 break;
2240 luaL_addchar(&buf, byte);
2241 if (byte == terminator) break;
2243 luaL_pushresult(&buf);
2244 if (!lua_rawlen(L, -1)) lua_pushnil(L);
2245 return 1;
2248 static int moonbr_lua_tonatural(lua_State *L, int idx) {
2249 int isnum;
2250 lua_Number n;
2251 n = lua_tonumberx(L, idx, &isnum);
2252 if (isnum && n>=0 && n<INT_MAX && (lua_Number)(int)n == n) return n;
2253 else return -1;
2256 static int moonbr_lua_totimeval(lua_State *L, int idx, struct timeval *value) {
2257 int isnum;
2258 lua_Number n;
2259 n = lua_tonumberx(L, idx, &isnum);
2260 if (isnum && n>=0 && n<=100000000) {
2261 value->tv_sec = n;
2262 value->tv_usec = 1e6 * (n - value->tv_sec);
2263 return 1;
2264 } else {
2265 return 0;
2269 static int moonbr_timeout(lua_State *L) {
2270 struct itimerval oldval;
2271 if (lua_isnoneornil(L, 1) && lua_isnoneornil(L, 2)) {
2272 getitimer(ITIMER_REAL, &oldval);
2273 } else {
2274 struct itimerval newval = {};
2275 if (lua_toboolean(L, 1)) {
2276 luaL_argcheck(
2277 L, moonbr_lua_totimeval(L, 1, &newval.it_value), 1,
2278 "interval in seconds expected"
2279 );
2281 if (lua_isnoneornil(L, 2)) {
2282 if (setitimer(ITIMER_REAL, &newval, &oldval)) {
2283 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2284 moonbr_terminate_error();
2286 } else {
2287 getitimer(ITIMER_REAL, &oldval);
2288 if (timercmp(&newval.it_value, &oldval.it_value, <)) {
2289 struct itimerval remval;
2290 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2291 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2292 moonbr_terminate_error();
2294 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
2295 getitimer(ITIMER_REAL, &remval);
2296 timersub(&oldval.it_value, &newval.it_value, &newval.it_value);
2297 timeradd(&newval.it_value, &remval.it_value, &newval.it_value);
2298 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2299 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2300 moonbr_terminate_error();
2302 } else {
2303 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
2305 return lua_gettop(L) - 1;
2308 lua_pushnumber(L, oldval.it_value.tv_sec + 1e-6 * oldval.it_value.tv_usec);
2309 return 1;
2312 #define moonbr_listen_init_pool_forkoption(luaname, cname, defval) { \
2313 lua_getfield(L, 2, luaname); \
2314 pool->cname = lua_isnil(L, -1) ? (defval) : moonbr_lua_tonatural(L, -1); \
2315 } while(0)
2317 #define moonbr_listen_init_pool_timeoption(luaname, cname, defval, defvalu) ( \
2318 lua_getfield(L, 2, luaname), \
2319 lua_isnil(L, -1) ? ( \
2320 pool->cname.tv_sec = (defval), pool->cname.tv_usec = (defvalu), \
2321 1 \
2322 ) : ( \
2323 (lua_isboolean(L, -1) && !lua_toboolean(L, -1)) ? ( \
2324 pool->cname.tv_sec = 0, pool->cname.tv_usec = 0, \
2325 1 \
2326 ) : ( \
2327 moonbr_lua_totimeval(L, -1, &pool->cname) \
2328 ) \
2329 ) \
2332 static int moonbr_listen_init_pool(lua_State *L) {
2333 struct moonbr_pool *pool;
2334 const char *proto;
2335 int i;
2336 pool = lua_touserdata(L, 1);
2337 for (i=0; i<pool->listener_count; i++) {
2338 struct moonbr_listener *listener = &pool->listener[i];
2339 lua_settop(L, 2);
2340 #if LUA_VERSION_NUM >= 503
2341 lua_geti(L, 2, i+1);
2342 #else
2343 lua_pushinteger(L, i+1);
2344 lua_gettable(L, 2);
2345 #endif
2346 lua_getfield(L, 3, "proto");
2347 proto = lua_tostring(L, -1);
2348 if (proto && !strcmp(proto, "interval")) {
2349 listener->proto = MOONBR_PROTO_INTERVAL;
2350 lua_getfield(L, 3, "name");
2352 const char *name = lua_tostring(L, -1);
2353 if (name) {
2354 if (asprintf(&listener->proto_specific.interval.name, "%s", name) < 0) {
2355 moonbr_log(LOG_CRIT, "Memory allocation_error");
2356 moonbr_terminate_error();
2360 lua_getfield(L, 3, "delay");
2361 if (
2362 !moonbr_lua_totimeval(L, -1, &listener->proto_specific.interval.delay) ||
2363 !timerisset(&listener->proto_specific.interval.delay)
2364 ) {
2365 luaL_error(L, "No valid interval delay specified; use listen{{proto=\"interval\", delay=...}, ...}");
2367 lua_getfield(L, 3, "strict");
2368 if (!lua_isnil(L, -1)) {
2369 if (lua_isboolean(L, -1)) {
2370 if (lua_toboolean(L, -1)) listener->proto_specific.interval.strict = 1;
2371 } else {
2372 luaL_error(L, "Option \"strict\" must be a boolean if set; use listen{{proto=\"interval\", strict=true, ...}, ...}");
2375 } else if (proto && !strcmp(proto, "local")) {
2376 listener->proto = MOONBR_PROTO_LOCAL;
2377 lua_getfield(L, 3, "path");
2379 const char *path = lua_tostring(L, -1);
2380 if (!path) {
2381 luaL_error(L, "No valid path specified for local socket; use listen{{proto=\"local\", path=...}, ...}");
2383 if (asprintf(&listener->proto_specific.local.path, "%s", path) < 0) {
2384 moonbr_log(LOG_CRIT, "Memory allocation_error");
2385 moonbr_terminate_error();
2388 } else if (proto && !strcmp(proto, "tcp6")) {
2389 listener->proto = MOONBR_PROTO_TCP6;
2390 lua_getfield(L, 3, "port");
2391 listener->proto_specific.tcp.port = lua_tointeger(L, -1);
2392 if (
2393 listener->proto_specific.tcp.port < 1 ||
2394 listener->proto_specific.tcp.port > 65535
2395 ) {
2396 luaL_error(L, "No valid port number specified; use listen{{proto=\"tcp6\", port=...}, ...}");
2398 lua_getfield(L, 3, "localhost");
2399 if (!lua_isnil(L, -1)) {
2400 if (lua_isboolean(L, -1)) {
2401 if (lua_toboolean(L, -1)) listener->proto_specific.tcp.localhost_only = 1;
2402 } else {
2403 luaL_error(L, "Option \"localhost\" must be a boolean if set; use listen{{proto=\"tcp6\", localhost=true, ...}, ...}");
2406 } else if (proto && !strcmp(proto, "tcp4")) {
2407 listener->proto = MOONBR_PROTO_TCP4;
2408 lua_getfield(L, 3, "port");
2409 listener->proto_specific.tcp.port = lua_tointeger(L, -1);
2410 if (
2411 listener->proto_specific.tcp.port < 1 ||
2412 listener->proto_specific.tcp.port > 65535
2413 ) {
2414 luaL_error(L, "No valid port number specified; use listen{{proto=\"tcp4\", port=...}, ...}");
2416 lua_getfield(L, 3, "localhost");
2417 if (!lua_isnil(L, -1)) {
2418 if (lua_isboolean(L, -1)) {
2419 if (lua_toboolean(L, -1)) listener->proto_specific.tcp.localhost_only = 1;
2420 } else {
2421 luaL_error(L, "Option \"localhost\" must be a boolean if set; use listen{{proto=\"tcp4\", localhost=true, ...}, ...}");
2426 lua_settop(L, 2);
2427 moonbr_listen_init_pool_forkoption("pre_fork", pre_fork, 1);
2428 moonbr_listen_init_pool_forkoption("min_fork", min_fork, pool->pre_fork > 2 ? pool->pre_fork : 2);
2429 moonbr_listen_init_pool_forkoption("max_fork", max_fork, pool->min_fork > 16 ? pool->min_fork : 16);
2430 if (!moonbr_listen_init_pool_timeoption("fork_delay", fork_delay, 1, 0)) {
2431 luaL_error(L, "Option \"fork_delay\" is expected to be a non-negative number");
2433 if (!moonbr_listen_init_pool_timeoption("fork_error_delay", fork_error_delay, 2, 0)) {
2434 luaL_error(L, "Option \"fork_error_delay\" is expected to be a non-negative number");
2436 if (!moonbr_listen_init_pool_timeoption("exit_delay", exit_delay, 60, 0)) {
2437 luaL_error(L, "Option \"exit_delay\" is expected to be a non-negative number");
2439 if (timercmp(&pool->fork_error_delay, &pool->fork_delay, <)) {
2440 pool->fork_error_delay = pool->fork_delay;
2442 if (!moonbr_listen_init_pool_timeoption("idle_timeout", idle_timeout, 0, 0)) {
2443 luaL_error(L, "Option \"idle_timeout\" is expected to be a non-negative number");
2445 lua_getfield(L, 2, "memory_limit");
2446 if (!lua_isnil(L, -1)) {
2447 int isnum;
2448 lua_Number n;
2449 n = lua_tonumberx(L, -1, &isnum);
2450 if (n < 0 || !isnum) {
2451 luaL_error(L, "Option \"memory_limit\" is expected to be a non-negative number");
2453 pool->memory_limit = n;
2455 lua_settop(L, 2);
2456 lua_getfield(L, 2, "prepare");
2457 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2458 luaL_error(L, "Option \"prepare\" must be nil or a function");
2460 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2461 lua_getfield(L, 2, "connect");
2462 if (!lua_isfunction(L, -1)) {
2463 luaL_error(L, "Option \"connect\" must be a function; use listen{{...}, {...}, connect=function(socket) ... end, ...}");
2465 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2466 lua_getfield(L, 2, "finish");
2467 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2468 luaL_error(L, "Option \"finish\" must be nil or a function");
2470 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2471 return 0;
2474 static int moonbr_listen(lua_State *L) {
2475 struct moonbr_pool *pool;
2476 lua_Integer listener_count;
2477 if (moonbr_booted) luaL_error(L, "Moonbridge bootup is already complete");
2478 luaL_checktype(L, 1, LUA_TTABLE);
2479 listener_count = luaL_len(L, 1);
2480 if (!listener_count) luaL_error(L, "No listen ports specified; use listen{{proto=..., port=...},...}");
2481 if (listener_count > 100) luaL_error(L, "Too many listeners");
2482 pool = moonbr_create_pool(listener_count);
2483 lua_pushcfunction(L, moonbr_listen_init_pool);
2484 lua_pushlightuserdata(L, pool);
2485 lua_pushvalue(L, 1);
2486 if (lua_pcall(L, 2, 0, 0)) goto moonbr_listen_error;
2488 int i;
2489 i = moonbr_start_pool(pool);
2490 if (i >= 0) {
2491 struct moonbr_listener *listener = &pool->listener[i];
2492 switch (listener->proto) {
2493 case MOONBR_PROTO_INTERVAL:
2494 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"interval\"): %s", i+1, strerror(errno));
2495 break;
2496 case MOONBR_PROTO_LOCAL:
2497 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"local\", path=\"%s\"): %s", i+1, listener->proto_specific.local.path, strerror(errno));
2498 break;
2499 case MOONBR_PROTO_TCP6:
2500 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"tcp6\", port=%d): %s", i+1, listener->proto_specific.tcp.port, strerror(errno));
2501 break;
2502 case MOONBR_PROTO_TCP4:
2503 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"tcp4\", port=%d): %s", i+1, listener->proto_specific.tcp.port, strerror(errno));
2504 break;
2505 default:
2506 moonbr_log(LOG_ERR, "Internal error (should not happen): Unexpected value in listener.proto field");
2507 moonbr_terminate_error();
2509 goto moonbr_listen_error;
2512 return 0;
2513 moonbr_listen_error:
2514 moonbr_destroy_pool(pool);
2515 lua_pushnil(L);
2516 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2517 lua_pushnil(L);
2518 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2519 lua_pushnil(L);
2520 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2521 lua_error(L);
2522 return 0; // avoid compiler warning
2526 /*** Main function and command line invokation ***/
2528 static void moonbr_usage(int err, const char *cmd) {
2529 FILE *out;
2530 out = err ? stderr : stdout;
2531 if (!cmd) cmd = "moonbridge";
2532 fprintf(out, "Get this help message: %s {-h|--help}\n", cmd);
2533 fprintf(out, "Usage: %s \\\n", cmd);
2534 fprintf(out, " [-b|--background] \\\n");
2535 fprintf(out, " [-d|--debug] \\\n");
2536 fprintf(out, " [-f|--logfacility {DAEMON|USER|0|1|...|7}] \\\n");
2537 fprintf(out, " [-i|--logident <syslog ident> \\\n");
2538 fprintf(out, " [-l|--logfile <logfile>] \\\n");
2539 fprintf(out, " [-p|--pidfile <pidfile>] \\\n");
2540 fprintf(out, " [-s|--stats] \\\n");
2541 fprintf(out, " -- <Lua script> [<cmdline options for Lua script>]\n");
2542 exit(err);
2545 #define moonbr_usage_error() moonbr_usage(MOONBR_EXITCODE_CMDLINEERROR, argc ? argv[0] : NULL)
2547 int main(int argc, char **argv) {
2549 int daemonize = 0;
2550 int log_facility = LOG_USER;
2551 const char *log_ident = "moonbridge";
2552 const char *log_filename = NULL;
2553 const char *pid_filename = NULL;
2554 int option;
2555 struct option longopts[] = {
2556 { "background", no_argument, NULL, 'b' },
2557 { "debug", no_argument, NULL, 'd' },
2558 { "logfacility", required_argument, NULL, 'f' },
2559 { "help", no_argument, NULL, 'h' },
2560 { "logident", required_argument, NULL, 'i' },
2561 { "logfile", required_argument, NULL, 'l' },
2562 { "pidfile", required_argument, NULL, 'p' },
2563 { "stats", no_argument, NULL, 's' }
2564 };
2565 while ((option = getopt_long(argc, argv, "bdf:hi:l:p:s", longopts, NULL)) != -1) {
2566 switch (option) {
2567 case 'b':
2568 daemonize = 1;
2569 break;
2570 case 'd':
2571 moonbr_debug = 1;
2572 moonbr_stat = 1;
2573 break;
2574 case 'f':
2575 if (!strcmp(optarg, "DAEMON")) {
2576 log_facility = LOG_DAEMON;
2577 } else if (!strcmp(optarg, "USER")) {
2578 log_facility = LOG_USER;
2579 } else if (!strcmp(optarg, "0")) {
2580 log_facility = LOG_LOCAL0;
2581 } else if (!strcmp(optarg, "1")) {
2582 log_facility = LOG_LOCAL1;
2583 } else if (!strcmp(optarg, "2")) {
2584 log_facility = LOG_LOCAL2;
2585 } else if (!strcmp(optarg, "3")) {
2586 log_facility = LOG_LOCAL3;
2587 } else if (!strcmp(optarg, "4")) {
2588 log_facility = LOG_LOCAL4;
2589 } else if (!strcmp(optarg, "5")) {
2590 log_facility = LOG_LOCAL5;
2591 } else if (!strcmp(optarg, "6")) {
2592 log_facility = LOG_LOCAL6;
2593 } else if (!strcmp(optarg, "7")) {
2594 log_facility = LOG_LOCAL7;
2595 } else {
2596 moonbr_usage_error();
2598 moonbr_use_syslog = 1;
2599 break;
2600 case 'h':
2601 moonbr_usage(MOONBR_EXITCODE_GRACEFUL, argv[0]);
2602 break;
2603 case 'i':
2604 log_ident = optarg;
2605 moonbr_use_syslog = 1;
2606 break;
2607 case 'l':
2608 log_filename = optarg;
2609 break;
2610 case 'p':
2611 pid_filename = optarg;
2612 break;
2613 case 's':
2614 moonbr_stat = 1;
2615 break;
2616 default:
2617 moonbr_usage_error();
2620 if (argc - optind < 1) moonbr_usage_error();
2621 if (pid_filename) {
2622 pid_t otherpid;
2623 while ((moonbr_pidfh = pidfile_open(pid_filename, 0644, &otherpid)) == NULL) {
2624 if (errno == EEXIST) {
2625 if (otherpid == -1) {
2626 fprintf(stderr, "PID file \"%s\" is already locked\n", pid_filename);
2627 } else {
2628 fprintf(stderr, "PID file \"%s\" is already locked by process with PID: %i\n", pid_filename, (int)otherpid);
2630 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2631 } else if (errno != EINTR) {
2632 fprintf(stderr, "Could not write PID file \"%s\": %s\n", pid_filename, strerror(errno));
2633 exit(MOONBR_EXITCODE_STARTUPERROR);
2637 if (log_filename) {
2638 int logfd;
2639 while (
2640 ( logfd = flopen(
2641 log_filename,
2642 O_WRONLY|O_NONBLOCK|O_CREAT|O_APPEND|O_CLOEXEC,
2643 0640
2645 ) < 0
2646 ) {
2647 if (errno == EWOULDBLOCK) {
2648 fprintf(stderr, "Logfile \"%s\" is locked\n", log_filename);
2649 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2650 } else if (errno != EINTR) {
2651 fprintf(stderr, "Could not open logfile \"%s\": %s\n", log_filename, strerror(errno));
2652 exit(MOONBR_EXITCODE_STARTUPERROR);
2655 moonbr_logfile = fdopen(logfd, "a");
2656 if (!moonbr_logfile) {
2657 fprintf(stderr, "Could not open write stream to logfile \"%s\": %s\n", log_filename, strerror(errno));
2658 exit(MOONBR_EXITCODE_STARTUPERROR);
2661 if (daemonize == 0 && !moonbr_logfile) moonbr_logfile = stderr;
2662 if (moonbr_logfile) setlinebuf(moonbr_logfile);
2663 else moonbr_use_syslog = 1;
2664 if (moonbr_use_syslog) openlog(log_ident, LOG_NDELAY | LOG_PID, log_facility);
2665 if (daemonize) {
2666 if (daemon(1, 0)) {
2667 moonbr_log(LOG_ERR, "Could not daemonize moonbridge process");
2668 moonbr_terminate_error();
2672 moonbr_log(LOG_NOTICE, "Starting moonbridge server");
2673 if (moonbr_pidfh && pidfile_write(moonbr_pidfh)) {
2674 moonbr_log(LOG_ERR, "Could not write pidfile (after locking)");
2677 lua_State *L;
2678 L = lua_newstate(moonbr_alloc, NULL);
2679 if (!L) {
2680 moonbr_log(LOG_CRIT, "Could not initialize Lua state");
2681 moonbr_terminate_error();
2683 lua_atpanic(L, moonbr_lua_panic);
2684 luaL_openlibs(L);
2685 if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
2686 moonbr_log(LOG_CRIT, "Lua metatable LUA_FILEHANDLE does not exist");
2687 moonbr_terminate_error();
2689 lua_getfield(L, -1, "__index");
2690 lua_pushcfunction(L, moonbr_readuntil);
2691 lua_setfield(L, -2, "readuntil");
2692 lua_pop(L, 2);
2693 lua_pushcfunction(L, moonbr_timeout);
2694 lua_setglobal(L, "timeout");
2695 lua_pushcfunction(L, moonbr_listen);
2696 lua_setglobal(L, "listen");
2697 lua_pushcfunction(L, moonbr_addtraceback); // on stack position 1
2698 moonbr_log(LOG_INFO, "Loading \"%s\"", argv[optind]);
2699 if (luaL_loadfile(L, argv[optind])) {
2700 moonbr_log(LOG_ERR, "Error while loading \"%s\": %s", argv[optind], lua_tostring(L, -1));
2701 moonbr_terminate_error();
2703 { int i; for (i=optind+1; i<argc; i++) lua_pushstring(L, argv[i]); }
2704 if (lua_pcall(L, argc-(optind+1), 0, 1)) {
2705 moonbr_log(LOG_ERR, "Error while executing \"%s\": %s", argv[optind], lua_tostring(L, -1));
2706 moonbr_terminate_error();
2708 if (!moonbr_first_pool) {
2709 moonbr_log(LOG_WARNING, "No listener initialized.");
2710 moonbr_terminate_error();
2712 lua_getglobal(L, "listen");
2713 lua_pushcfunction(L, moonbr_listen);
2714 if (lua_compare(L, -2, -1, LUA_OPEQ)) {
2715 lua_pushnil(L);
2716 lua_setglobal(L, "listen");
2718 lua_settop(L, 1);
2719 moonbr_run(L);
2721 return 0;

Impressum / About Us