moonbridge

view moonbridge.c @ 125:d9cc81641175

Code cleanup; Allow binding to specific interfaces
author jbe
date Tue Apr 14 00:26:38 2015 +0200 (2015-04-14)
parents 61a2f55b3538
children e0ab9fd00cb1
line source
2 /*** Version ***/
3 #define MOONBR_VERSION_STRING "0.4.0"
6 /*** Compile-time configuration ***/
8 #define MOONBR_LUA_PANIC_BUG_WORKAROUND 1
11 /*** C preprocessor macros for portability support ***/
13 #ifndef __has_include
14 #define __has_include(x) 0
15 #endif
18 /*** Include directives for used system libraries ***/
20 #if defined(__linux__)
21 #define _GNU_SOURCE
22 #endif
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <syslog.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <time.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <netinet/in.h>
36 #include <netdb.h>
37 #include <arpa/inet.h>
38 #include <poll.h>
39 #include <signal.h>
40 #include <sys/wait.h>
41 #include <sys/resource.h>
42 #include <sys/file.h>
43 #if defined(__FreeBSD__) || __has_include(<libutil.h>)
44 #include <libutil.h>
45 #endif
46 #if defined(__linux__) || __has_include(<bsd/stdio.h>)
47 #include <bsd/stdio.h>
48 #endif
49 #if defined(__linux__) || __has_include(<bsd/libutil.h>)
50 #include <bsd/libutil.h>
51 #endif
52 #if defined(__linux__) || __has_include(<bsd/unistd.h>)
53 #include <bsd/unistd.h>
54 #endif
57 /*** Fallback definitions for missing constants on some platforms ***/
59 /* INFTIM is used as timeout parameter for poll() */
60 #ifndef INFTIM
61 #define INFTIM -1
62 #endif
65 /*** Include directives for Lua ***/
67 #include <lua.h>
68 #include <lauxlib.h>
69 #include <lualib.h>
72 /*** Include directive for moonbridge_io library ***/
74 #include "moonbridge_io.h"
77 /*** Constants ***/
79 /* Backlog option for listen() call */
80 #define MOONBR_LISTEN_BACKLOG 1024
82 /* Maximum length of a timestamp used for strftime() */
83 #define MOONBR_LOG_MAXTIMELEN 40
85 /* Maximum length of a log message */
86 #define MOONBR_LOG_MAXMSGLEN 4095
88 /* Exitcodes passed to exit() call */
89 #define MOONBR_EXITCODE_GRACEFUL 0
90 #define MOONBR_EXITCODE_CMDLINEERROR 1
91 #define MOONBR_EXITCODE_ALREADYRUNNING 2
92 #define MOONBR_EXITCODE_STARTUPERROR 3
93 #define MOONBR_EXITCODE_RUNTIMEERROR 4
95 /* Maximum length of a line sent to stderr by child processes */
96 #define MOONBR_MAXERRORLINELEN 1024
98 /* Maximum length of an error string returned by strerror() */
99 #define MOONBR_MAXSTRERRORLEN 80
101 /* Status bytes exchanged between master and child processes */
102 #define MOONBR_STATUS_IDLE 'I'
103 #define MOONBR_COMMAND_CONNECT 'C'
104 #define MOONBR_COMMAND_TERMINATE 'T'
105 #define MOONBR_STATUS_GOODBYE 'B'
107 /* Constant file descriptors */
108 #define MOONBR_FD_STDERR 2
109 #define MOONBR_FD_CONTROL 3
110 #define MOONBR_FD_END 4
112 /* Return values of moonbr_try_destroy_worker() */
113 #define MOONBR_DESTROY_NONE 0
114 #define MOONBR_DESTROY_PREPARE 1
115 #define MOONBR_DESTROY_IDLE_OR_ASSIGNED 2
118 /*** Types ***/
120 /* Enum for 'moonbr_pstate' */
121 #define MOONBR_PSTATE_STARTUP 0
122 #define MOONBR_PSTATE_RUNNING 1
123 #define MOONBR_PSTATE_FORKED 2
125 /* Enum for 'proto' field of struct moonbr_listener */
126 #define MOONBR_PROTO_INTERVAL 1
127 #define MOONBR_PROTO_LOCAL 2
128 #define MOONBR_PROTO_TCP 3
130 /* Data structure for a pool's listener that can accept incoming connections */
131 struct moonbr_listener {
132 struct moonbr_pool *pool;
133 struct moonbr_listener *prev_listener; /* previous idle or(!) connected listener */
134 struct moonbr_listener *next_listener; /* next idle or(!) connected listener */
135 int proto;
136 union {
137 struct {
138 char *name; /* name of interval passed to 'connect' function as 'interval' field in table */
139 int strict; /* nonzero = runtime of 'connect' function does not delay interval */
140 struct timeval delay; /* interval between invocations of 'connect' function */
141 struct timeval wakeup; /* point in time of next invocation */
142 } interval;
143 struct {
144 union {
145 struct sockaddr addr_abstract;
146 struct sockaddr_un addr_un;
147 struct sockaddr_in addr_in;
148 struct sockaddr_in6 addr_in6;
149 } addr;
150 socklen_t addrlen;
151 } socket;
152 } type_specific;
153 union {
154 struct {
155 char ip[INET6_ADDRSTRLEN]; /* IP to listen on */
156 int port; /* port number to listen on (in host endianess) */
157 } tcp;
158 } proto_specific;
159 int listenfd; /* -1 = none */
160 int pollidx; /* -1 = none */
161 };
163 /* Data structure for a child process that is handling incoming connections */
164 struct moonbr_worker {
165 struct moonbr_pool *pool;
166 struct moonbr_worker *prev_worker;
167 struct moonbr_worker *next_worker;
168 struct moonbr_worker *prev_idle_worker;
169 struct moonbr_worker *next_idle_worker;
170 int idle; /* nonzero = waiting for command from parent process */
171 int assigned; /* nonzero = currently handling a connection */
172 pid_t pid;
173 int controlfd; /* socket to send/receive control message to/from child process */
174 int errorfd; /* socket to receive error output from child process' stderr */
175 char *errorlinebuf; /* optional buffer for collecting stderr data from child process */
176 int errorlinelen; /* number of bytes stored in 'errorlinebuf' */
177 int errorlineovf; /* nonzero = line length overflow */
178 struct timeval idle_expiration; /* point in time until child process may stay in idle state */
179 struct moonbr_listener *restart_interval_listener; /* set while interval listener is assigned */
180 };
182 /* Data structure for a pool of workers and listeners */
183 struct moonbr_pool {
184 int poolnum; /* number of pool for log output */
185 struct moonbr_pool *next_pool; /* next entry in linked list starting with 'moonbr_first_pool' */
186 struct moonbr_worker *first_worker; /* first worker of pool */
187 struct moonbr_worker *last_worker; /* last worker of pool */
188 struct moonbr_worker *first_idle_worker; /* first idle worker of pool */
189 struct moonbr_worker *last_idle_worker; /* last idle worker of pool */
190 int idle_worker_count;
191 int unassigned_worker_count;
192 int total_worker_count;
193 int worker_count_stat; /* only needed for statistics */
194 int pre_fork; /* desired minimum number of unassigned workers */
195 int min_fork; /* desired minimum number of workers in total */
196 int max_fork; /* maximum number of workers */
197 struct timeval fork_delay; /* delay after each fork() until a fork may happen again */
198 struct timeval fork_wakeup; /* point in time when a fork may happen again (unless a worker terminates before) */
199 struct timeval fork_error_delay; /* delay between fork()s when an error during fork or preparation occurred */
200 struct timeval fork_error_wakeup; /* point in time when fork may happen again if an error in preparation occurred */
201 int use_fork_error_wakeup; /* nonzero = error in preparation occured; gets reset on next fork */
202 struct timeval exit_delay; /* delay for terminating excessive workers (unassigned_worker_count > pre_fork) */
203 struct timeval exit_wakeup; /* point in time when terminating an excessive worker */
204 struct timeval idle_timeout; /* delay before an idle worker is terminated */
205 size_t memory_limit; /* maximum bytes of memory that the Lua machine may allocate */
206 int listener_count; /* total number of listeners of pool (and size of 'listener' array at end of this struct) */
207 struct moonbr_listener *first_idle_listener; /* first listener that is idle (i.e. has no waiting connection) */
208 struct moonbr_listener *last_idle_listener; /* last listener that is idle (i.e. has no waiting connection) */
209 struct moonbr_listener *first_connected_listener; /* first listener that has a pending connection */
210 struct moonbr_listener *last_connected_listener; /* last listener that has a pending connection */
211 struct moonbr_listener listener[1]; /* static array of variable(!) size to contain 'listener' structures */
212 };
214 /* Enum for 'channel' field of struct moonbr_poll_worker */
215 #define MOONBR_POLL_WORKER_CONTROLCHANNEL 1
216 #define MOONBR_POLL_WORKER_ERRORCHANNEL 2
218 /* Structure to refer from 'moonbr_poll_worker_fds' entry to worker structure */
219 struct moonbr_poll_worker {
220 struct moonbr_worker *worker;
221 int channel; /* field indicating whether file descriptor is 'controlfd' or 'errorfd' */
222 };
224 /* Variable indicating that clean shutdown was requested */
225 static int moonbr_shutdown_in_progress = 0;
228 /*** Macros for Lua registry ***/
230 /* Lightuserdata keys for Lua registry to store 'prepare', 'connect', and 'finish' functions */
231 #define moonbr_luakey_prepare_func(pool) ((void *)(intptr_t)(pool) + 0)
232 #define moonbr_luakey_connect_func(pool) ((void *)(intptr_t)(pool) + 1)
233 #define moonbr_luakey_finish_func(pool) ((void *)(intptr_t)(pool) + 2)
236 /*** Global variables ***/
238 /* State of process execution */
239 static int moonbr_pstate = MOONBR_PSTATE_STARTUP;
241 /* Process ID of the main process */
242 static pid_t moonbr_masterpid;
244 /* Condition variables set by the signal handler */
245 static volatile sig_atomic_t moonbr_cond_poll = 0;
246 static volatile sig_atomic_t moonbr_cond_terminate = 0;
247 static volatile sig_atomic_t moonbr_cond_interrupt = 0;
248 static volatile sig_atomic_t moonbr_cond_child = 0;
250 /* Socket pair to denote signal delivery when signal handler was called just before poll() */
251 static int moonbr_poll_signalfds[2];
252 #define moonbr_poll_signalfd_read moonbr_poll_signalfds[0]
253 #define moonbr_poll_signalfd_write moonbr_poll_signalfds[1]
255 /* Global variables for pidfile and logging */
256 static struct pidfh *moonbr_pidfh = NULL;
257 static FILE *moonbr_logfile = NULL;
258 static int moonbr_use_syslog = 0;
260 /* First and last entry of linked list of all created pools during initialization */
261 static struct moonbr_pool *moonbr_first_pool = NULL;
262 static struct moonbr_pool *moonbr_last_pool = NULL;
264 /* Total count of pools */
265 static int moonbr_pool_count = 0;
267 /* Set to a nonzero value if dynamic part of 'moonbr_poll_fds' ('moonbr_poll_worker_fds') needs an update */
268 static int moonbr_poll_refresh_needed = 0;
270 /* Array passed to poll(), consisting of static part and dynamic part ('moonbr_poll_worker_fds') */
271 static struct pollfd *moonbr_poll_fds = NULL; /* the array */
272 static int moonbr_poll_fds_bufsize = 0; /* memory allocated for this number of elements */
273 static int moonbr_poll_fds_count = 0; /* total number of elements */
274 static int moonbr_poll_fds_static_count; /* number of elements in static part */
276 /* Dynamic part of 'moonbr_poll_fds' array */
277 #define moonbr_poll_worker_fds (moonbr_poll_fds+moonbr_poll_fds_static_count)
279 /* Additional information for dynamic part of 'moonbr_poll_fds' array */
280 struct moonbr_poll_worker *moonbr_poll_workers; /* the array */
281 static int moonbr_poll_workers_bufsize = 0; /* memory allocated for this number of elements */
282 static int moonbr_poll_worker_count = 0; /* number of elements in array */
284 /* Variable set to nonzero value to disallow further calls of 'listen' function */
285 static int moonbr_booted = 0;
287 /* Verbosity settings */
288 static int moonbr_debug = 0;
289 static int moonbr_stat = 0;
291 /* Memory consumption by Lua machine */
292 static size_t moonbr_memory_usage = 0;
293 static size_t moonbr_memory_limit = 0;
296 /*** Functions for signal handling ***/
298 /* Signal handler for master and child processes */
299 static void moonbr_signal(int sig) {
300 if (getpid() == moonbr_masterpid) {
301 /* master process */
302 switch (sig) {
303 case SIGHUP:
304 case SIGINT:
305 /* fast shutdown requested */
306 moonbr_cond_interrupt = 1;
307 break;
308 case SIGTERM:
309 /* clean shutdown requested */
310 moonbr_cond_terminate = 1;
311 break;
312 case SIGCHLD:
313 /* child process terminated */
314 moonbr_cond_child = 1;
315 break;
316 }
317 if (moonbr_cond_poll) {
318 /* avoid race condition if signal handler is invoked right before poll() */
319 char buf[1] = {0};
320 write(moonbr_poll_signalfd_write, buf, 1);
321 }
322 } else {
323 /* child process forwards certain signals to parent process */
324 switch (sig) {
325 case SIGHUP:
326 case SIGINT:
327 case SIGTERM:
328 kill(moonbr_masterpid, sig);
329 }
330 }
331 }
333 /* Initialize signal handling */
334 static void moonbr_signal_init(){
335 moonbr_masterpid = getpid();
336 signal(SIGHUP, moonbr_signal);
337 signal(SIGINT, moonbr_signal);
338 signal(SIGTERM, moonbr_signal);
339 signal(SIGCHLD, moonbr_signal);
340 }
343 /*** Functions for logging in master process ***/
345 /* Logs a pre-formatted message with given syslog() priority */
346 static void moonbr_log_msg(int priority, const char *msg) {
347 if (moonbr_logfile) {
348 /* logging to logfile desired (timestamp is prepended in that case) */
349 time_t now_time = 0;
350 struct tm now_tmstruct;
351 char timestr[MOONBR_LOG_MAXTIMELEN+1];
352 time(&now_time);
353 localtime_r(&now_time, &now_tmstruct);
354 if (!strftime(
355 timestr, MOONBR_LOG_MAXTIMELEN+1, "%Y-%m-%d %H:%M:%S %Z: ", &now_tmstruct
356 )) timestr[0] = 0;
357 fprintf(moonbr_logfile, "%s%s\n", timestr, msg);
358 }
359 if (moonbr_use_syslog) {
360 /* logging through syslog desired */
361 syslog(priority, "%s", msg);
362 }
363 }
365 /* Formats a message via vsnprintf() and logs it with given syslog() priority */
366 static void moonbr_log(int priority, const char *message, ...) {
367 char msgbuf[MOONBR_LOG_MAXMSGLEN+1]; /* buffer of static size to store formatted message */
368 int msglen; /* length of full message (may exceed MOONBR_LOG_MAXMSGLEN) */
369 {
370 /* pass variable arguments to vsnprintf() to format message */
371 va_list ap;
372 va_start(ap, message);
373 msglen = vsnprintf(msgbuf, MOONBR_LOG_MAXMSGLEN+1, message, ap);
374 va_end(ap);
375 }
376 {
377 /* split and log message line by line */
378 char *line = msgbuf;
379 while (1) {
380 char *endptr = strchr(line, '\n');
381 if (endptr) {
382 /* terminate string where newline character is found */
383 *endptr = 0;
384 } else if (line != msgbuf && msglen > MOONBR_LOG_MAXMSGLEN) {
385 /* break if line is incomplete and not the first line */
386 break;
387 }
388 moonbr_log_msg(priority, line);
389 if (!endptr) break; /* break if end of formatted message is reached */
390 line = endptr+1; /* otherwise continue with remaining message */
391 }
392 }
393 if (msglen > MOONBR_LOG_MAXMSGLEN) {
394 /* print warning if message was truncated */
395 moonbr_log_msg(priority, "Previous log message has been truncated due to excessive length");
396 }
397 }
400 /*** Termination function ***/
402 /* Kill all child processes, remove PID file (if existent), and exit master process with given exitcode */
403 static void moonbr_terminate(int exitcode) {
404 {
405 struct moonbr_pool *pool;
406 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
407 {
408 struct moonbr_worker *worker;
409 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
410 moonbr_log(LOG_INFO, "Sending SIGKILL to child with PID %i", (int)worker->pid);
411 if (kill(worker->pid, SIGKILL)) {
412 moonbr_log(LOG_ERR, "Error while killing child process: %s", strerror(errno));
413 }
414 }
415 }
416 {
417 int i;
418 for (i=0; i<pool->listener_count; i++) {
419 struct moonbr_listener *listener = &pool->listener[i];
420 if (listener->proto == MOONBR_PROTO_LOCAL) {
421 moonbr_log(LOG_INFO, "Unlinking local socket \"%s\"", listener->type_specific.socket.addr.addr_un.sun_path);
422 if (unlink(listener->type_specific.socket.addr.addr_un.sun_path)) {
423 moonbr_log(LOG_ERR, "Error while unlinking local socket: %s", strerror(errno));
424 }
425 }
426 }
427 }
428 }
429 }
430 moonbr_log(exitcode ? LOG_ERR : LOG_NOTICE, "Terminating with exit code %i", exitcode);
431 if (moonbr_pidfh && pidfile_remove(moonbr_pidfh)) {
432 moonbr_log(LOG_ERR, "Error while removing PID file: %s", strerror(errno));
433 }
434 exit(exitcode);
435 }
437 /* Terminate with either MOONBR_EXITCODE_STARTUPERROR or MOONBR_EXITCODE_RUNTIMEERROR */
438 #define moonbr_terminate_error() \
439 moonbr_terminate( \
440 moonbr_pstate == MOONBR_PSTATE_STARTUP ? \
441 MOONBR_EXITCODE_STARTUPERROR : \
442 MOONBR_EXITCODE_RUNTIMEERROR \
443 )
446 /*** Helper functions ***/
448 /* Fills a 'struct timeval' structure with the current time (using CLOCK_MONOTONIC) */
449 static void moonbr_now(struct timeval *now) {
450 struct timespec ts = {0, };
451 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
452 moonbr_log(LOG_CRIT, "Error in clock_gettime() call: %s", strerror(errno));
453 moonbr_terminate_error();
454 }
455 *now = (struct timeval){ .tv_sec = ts.tv_sec, .tv_usec = ts.tv_nsec / 1000 };
456 }
458 /* Formats a 'struct timeval' value (not thread-safe) */
459 static char *moonbr_format_timeval(struct timeval *t) {
460 static char buf[32];
461 snprintf(buf, 32, "%ji.%06ji seconds", (intmax_t)t->tv_sec, (intmax_t)t->tv_usec);
462 return buf;
463 }
466 /*** Functions for pool creation and startup ***/
468 /* Creates a 'struct moonbr_pool' structure with a given number of listeners */
469 static struct moonbr_pool *moonbr_create_pool(int listener_count) {
470 struct moonbr_pool *pool;
471 pool = calloc(1,
472 sizeof(struct moonbr_pool) + /* size of 'struct moonbr_pool' with one listener */
473 (listener_count-1) * sizeof(struct moonbr_listener) /* size of extra listeners */
474 );
475 if (!pool) {
476 moonbr_log(LOG_CRIT, "Memory allocation error");
477 moonbr_terminate_error();
478 }
479 pool->listener_count = listener_count;
480 {
481 /* initialization of listeners */
482 int i;
483 for (i=0; i<listener_count; i++) {
484 struct moonbr_listener *listener = &pool->listener[i];
485 listener->pool = pool;
486 listener->listenfd = -1;
487 listener->pollidx = -1;
488 }
489 }
490 return pool;
491 }
493 /* Destroys a 'struct moonbr_pool' structure before it has been started */
494 static void moonbr_destroy_pool(struct moonbr_pool *pool) {
495 int i;
496 for (i=0; i<pool->listener_count; i++) {
497 struct moonbr_listener *listener = &pool->listener[i];
498 if (
499 listener->proto == MOONBR_PROTO_INTERVAL &&
500 listener->type_specific.interval.name
501 ) {
502 free(listener->type_specific.interval.name);
503 }
504 }
505 free(pool);
506 }
508 /* Starts a all listeners in a pool */
509 static int moonbr_start_pool(struct moonbr_pool *pool) {
510 moonbr_log(LOG_INFO, "Creating pool", pool->poolnum);
511 {
512 int i;
513 for (i=0; i<pool->listener_count; i++) {
514 struct moonbr_listener *listener = &pool->listener[i];
515 switch (listener->proto) {
516 case MOONBR_PROTO_INTERVAL:
517 /* nothing to do here: starting intervals is performed in moonbr_run() function */
518 if (!listener->type_specific.interval.name) {
519 moonbr_log(LOG_INFO, "Adding unnamed interval listener");
520 } else {
521 moonbr_log(LOG_INFO, "Adding interval listener \"%s\"", listener->type_specific.interval.name);
522 }
523 break;
524 case MOONBR_PROTO_LOCAL:
525 moonbr_log(LOG_INFO, "Adding local socket listener for path \"%s\"", listener->type_specific.socket.addr.addr_un.sun_path);
526 listener->listenfd = socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
527 if (listener->listenfd == -1) goto moonbr_start_pool_error;
528 if (!unlink(listener->type_specific.socket.addr.addr_un.sun_path)) {
529 moonbr_log(LOG_WARNING, "Unlinked named socket \"%s\" prior to listening", listener->type_specific.socket.addr.addr_un.sun_path);
530 } else {
531 if (errno != ENOENT) {
532 moonbr_log(LOG_ERR, "Could not unlink named socket \"%s\" prior to listening: %s", listener->type_specific.socket.addr.addr_un.sun_path, strerror(errno));
533 }
534 }
535 if (
536 bind(listener->listenfd, &listener->type_specific.socket.addr.addr_abstract, listener->type_specific.socket.addrlen)
537 ) goto moonbr_start_pool_error;
538 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
539 break;
540 case MOONBR_PROTO_TCP:
541 moonbr_log(LOG_INFO, "Adding TCP listener on interface \"%s\", port %i", listener->proto_specific.tcp.ip, listener->proto_specific.tcp.port);
542 listener->listenfd = socket(listener->type_specific.socket.addr.addr_abstract.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
543 if (listener->listenfd == -1) goto moonbr_start_pool_error;
544 {
545 /* avoid "Address already in use" error when restarting service */
546 static const int reuseval = 1;
547 if (setsockopt(
548 listener->listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval)
549 )) goto moonbr_start_pool_error;
550 }
551 {
552 /* default to send TCP RST when process terminates unexpectedly */
553 static const struct linger lingerval = {
554 .l_onoff = 1,
555 .l_linger = 0
556 };
557 if (setsockopt(
558 listener->listenfd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval)
559 )) goto moonbr_start_pool_error;
560 }
561 if (
562 bind(listener->listenfd, &listener->type_specific.socket.addr.addr_abstract, listener->type_specific.socket.addrlen)
563 ) goto moonbr_start_pool_error;
564 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
565 break;
566 default:
567 moonbr_log(LOG_CRIT, "Internal error (should not happen): Unexpected value in listener.proto field");
568 moonbr_terminate_error();
569 }
570 }
571 goto moonbr_start_pool_ok;
572 moonbr_start_pool_error:
573 {
574 int j = i;
575 int errno2 = errno;
576 for (; i>=0; i--) {
577 struct moonbr_listener *listener = &pool->listener[i];
578 if (listener->listenfd != -1) close(listener->listenfd);
579 }
580 errno = errno2;
581 return j;
582 }
583 }
584 moonbr_start_pool_ok:
585 pool->poolnum = ++moonbr_pool_count;
586 moonbr_log(LOG_INFO, "Pool #%i created", pool->poolnum);
587 if (moonbr_last_pool) moonbr_last_pool->next_pool = pool;
588 else moonbr_first_pool = pool;
589 moonbr_last_pool = pool;
590 return -1;
591 }
594 /*** Function to send data and a file descriptor to child process */
596 /* Sends control message of one bye plus optional file descriptor plus optional pointer to child process */
597 static void moonbr_send_control_message(struct moonbr_worker *worker, char status, int fd, void *ptr) {
598 {
599 struct iovec iovector = { .iov_base = &status, .iov_len = 1 }; /* carrying status byte */
600 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to transfer file descriptor */
601 struct msghdr message = { .msg_iov = &iovector, .msg_iovlen = 1 }; /* data structure passed to sendmsg() call */
602 if (moonbr_debug) {
603 if (fd == -1) {
604 moonbr_log(LOG_DEBUG, "Sending control message \"%c\" to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
605 } else {
606 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);
607 }
608 }
609 if (fd != -1) {
610 /* attach control message with file descriptor */
611 message.msg_control = control_message_buffer;
612 message.msg_controllen = CMSG_SPACE(sizeof(int));
613 {
614 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
615 control_message->cmsg_level = SOL_SOCKET;
616 control_message->cmsg_type = SCM_RIGHTS;
617 control_message->cmsg_len = CMSG_LEN(sizeof(int));
618 memcpy(CMSG_DATA(control_message), &fd, sizeof(int));
619 }
620 }
621 while (sendmsg(worker->controlfd, &message, MSG_NOSIGNAL) < 0) {
622 if (errno == EPIPE) {
623 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));
624 return; /* do not close socket; socket is closed when reading from it */
625 }
626 if (errno != EINTR) {
627 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));
628 moonbr_terminate_error();
629 }
630 }
631 }
632 if (ptr) {
633 char buf[sizeof(void *)];
634 char *pos = buf;
635 int len = sizeof(void *);
636 ssize_t written;
637 if (moonbr_debug) {
638 moonbr_log(LOG_DEBUG, "Sending memory pointer to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
639 }
640 memcpy(buf, &ptr, sizeof(void *));
641 while (len) {
642 written = send(worker->controlfd, pos, len, MSG_NOSIGNAL);
643 if (written > 0) {
644 pos += written;
645 len -= written;
646 } else if (errno == EPIPE) {
647 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));
648 return; /* do not close socket; socket is closed when reading from it */
649 } else if (errno != EINTR) {
650 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));
651 moonbr_terminate_error();
652 }
653 }
654 }
655 }
658 /*** Functions running in child process ***/
660 /* Logs an error in child process */
661 static void moonbr_child_log(const char *message) {
662 fprintf(stderr, "%s\n", message);
663 }
665 /* Logs a fatal error in child process and terminates process with error status */
666 static void moonbr_child_log_fatal(const char *message) {
667 moonbr_child_log(message);
668 exit(1);
669 }
671 /* Logs an error in child process while appending error string for global errno variable */
672 static void moonbr_child_log_errno(const char *message) {
673 char errmsg[MOONBR_MAXSTRERRORLEN];
674 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
675 fprintf(stderr, "%s: %s\n", message, errmsg);
676 }
678 /* Logs a fatal error in child process while appending error string for errno and terminating process */
679 static void moonbr_child_log_errno_fatal(const char *message) {
680 moonbr_child_log_errno(message);
681 exit(1);
682 }
684 /* Receives a control message consisting of one character plus an optional file descriptor from parent process */
685 static void moonbr_child_receive_control_message(int socketfd, char *status, int *fd) {
686 struct iovec iovector = { .iov_base = status, .iov_len = 1 }; /* reference to status byte variable */
687 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to receive file descriptor */
688 struct msghdr message = { /* data structure passed to recvmsg() call */
689 .msg_iov = &iovector,
690 .msg_iovlen = 1,
691 .msg_control = control_message_buffer,
692 .msg_controllen = CMSG_SPACE(sizeof(int))
693 };
694 {
695 int received;
696 while ((received = recvmsg(socketfd, &message, MSG_CMSG_CLOEXEC)) < 0) {
697 if (errno != EINTR) {
698 moonbr_child_log_errno_fatal("Error while trying to receive connection socket from parent process");
699 }
700 }
701 if (!received) {
702 moonbr_child_log_fatal("Unexpected EOF while trying to receive connection socket from parent process");
703 }
704 }
705 {
706 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
707 if (control_message) {
708 if (control_message->cmsg_level != SOL_SOCKET) {
709 moonbr_child_log_fatal("Received control message with cmsg_level not equal to SOL_SOCKET");
710 }
711 if (control_message->cmsg_type != SCM_RIGHTS) {
712 moonbr_child_log_fatal("Received control message with cmsg_type not equal to SCM_RIGHTS");
713 }
714 memcpy(fd, CMSG_DATA(control_message), sizeof(int));
715 } else {
716 *fd = -1;
717 }
718 }
719 }
721 /* Receives a pointer from parent process */
722 static void *moonbr_child_receive_pointer(int socketfd) {
723 char buf[sizeof(void *)];
724 char *pos = buf;
725 int len = sizeof(void *);
726 ssize_t bytes_read;
727 while (len) {
728 bytes_read = recv(socketfd, pos, len, 0);
729 if (bytes_read > 0) {
730 pos += bytes_read;
731 len -= bytes_read;
732 } else if (!bytes_read) {
733 moonbr_child_log_fatal("Unexpected EOF while trying to receive memory pointer from parent process");
734 } else if (errno != EINTR) {
735 moonbr_child_log_errno_fatal("Error while trying to receive memory pointer from parent process");
736 }
737 }
738 {
739 void *ptr; /* avoid breaking strict-aliasing rules */
740 memcpy(&ptr, buf, sizeof(void *));
741 return ptr;
742 }
743 }
745 /* Main function of child process to be called after fork() and file descriptor rearrangement */
746 void moonbr_child_run(struct moonbr_pool *pool, lua_State *L) {
747 char controlmsg;
748 int fd;
749 struct itimerval notimer = { { 0, }, { 0, } };
750 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
751 if (lua_isnil(L, -1)) lua_pop(L, 1);
752 else if (lua_pcall(L, 0, 0, 1)) {
753 fprintf(stderr, "Error in \"prepare\" function: %s\n", lua_tostring(L, -1));
754 exit(1);
755 }
756 while (1) {
757 struct moonbr_listener *listener;
758 if (setitimer(ITIMER_REAL, &notimer, NULL)) {
759 moonbr_child_log_errno_fatal("Could not reset ITIMER_REAL via setitimer()");
760 }
761 controlmsg = MOONBR_STATUS_IDLE;
762 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
763 moonbr_child_log_errno_fatal("Error while sending ready message to parent process");
764 }
765 moonbr_child_receive_control_message(MOONBR_FD_CONTROL, &controlmsg, &fd);
766 if (!(
767 (controlmsg == MOONBR_COMMAND_TERMINATE && fd == -1) ||
768 (controlmsg == MOONBR_COMMAND_CONNECT)
769 )) {
770 moonbr_child_log_fatal("Received illegal control message from parent process");
771 }
772 if (controlmsg == MOONBR_COMMAND_TERMINATE) break;
773 listener = moonbr_child_receive_pointer(MOONBR_FD_CONTROL);
774 if (listener->proto == MOONBR_PROTO_INTERVAL && fd >= 0) {
775 moonbr_child_log_fatal("Received unexpected file descriptor from parent process");
776 } else if (listener->proto != MOONBR_PROTO_INTERVAL && fd < 0) {
777 moonbr_child_log_fatal("Missing file descriptor from parent process");
778 }
779 if (fd >= 0) moonbr_io_pushhandle(L, fd);
780 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
781 if (fd < 0) {
782 lua_newtable(L);
783 lua_pushstring(L,
784 listener->type_specific.interval.name ?
785 listener->type_specific.interval.name : ""
786 );
787 lua_setfield(L, -2, "interval");
788 } else {
789 lua_pushvalue(L, -2);
790 }
791 if (lua_pcall(L, 1, 1, 1)) {
792 fprintf(stderr, "Error in \"connect\" function: %s\n", lua_tostring(L, -1));
793 exit(1);
794 }
795 if (fd >= 0) moonbr_io_closehandle(L, -2, 0); /* attemt clean close */
796 if (lua_type(L, -1) != LUA_TBOOLEAN || !lua_toboolean(L, -1)) break;
797 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
798 lua_settop(L, 2);
799 #else
800 lua_settop(L, 1);
801 #endif
802 }
803 controlmsg = MOONBR_STATUS_GOODBYE;
804 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
805 moonbr_child_log_errno_fatal("Error while sending goodbye message to parent process");
806 }
807 if (close(MOONBR_FD_CONTROL) && errno != EINTR) {
808 moonbr_child_log_errno("Error while closing control socket");
809 }
810 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
811 if (lua_isnil(L, -1)) lua_pop(L, 1);
812 else if (lua_pcall(L, 0, 0, 1)) {
813 fprintf(stderr, "Error in \"finish\" function: %s\n", lua_tostring(L, -1));
814 exit(1);
815 }
816 lua_close(L);
817 exit(0);
818 }
821 /*** Functions to spawn child process ***/
823 /* Helper function to send an error message to a file descriptor (not needing a file stream) */
824 static void moonbr_child_emergency_print(int fd, char *message) {
825 size_t len = strlen(message);
826 ssize_t written;
827 while (len) {
828 written = write(fd, message, len);
829 if (written > 0) {
830 message += written;
831 len -= written;
832 } else {
833 if (written != -1 || errno != EINTR) break;
834 }
835 }
836 }
838 /* Helper function to send an error message plus a text for errno to a file descriptor and terminate the process */
839 static void moonbr_child_emergency_error(int fd, char *message) {
840 int errno2 = errno;
841 moonbr_child_emergency_print(fd, message);
842 moonbr_child_emergency_print(fd, ": ");
843 moonbr_child_emergency_print(fd, strerror(errno2));
844 moonbr_child_emergency_print(fd, "\n");
845 exit(1);
846 }
848 /* Creates a child process and (in case of success) registers it in the 'struct moonbr_pool' structure */
849 static int moonbr_create_worker(struct moonbr_pool *pool, lua_State *L) {
850 struct moonbr_worker *worker;
851 worker = calloc(1, sizeof(struct moonbr_worker));
852 if (!worker) {
853 moonbr_log(LOG_CRIT, "Memory allocation error");
854 return -1;
855 }
856 worker->pool = pool;
857 {
858 int controlfds[2];
859 int errorfds[2];
860 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, controlfds)) {
861 moonbr_log(LOG_ERR, "Could not create control socket pair for communcation with child process: %s", strerror(errno));
862 free(worker);
863 return -1;
864 }
865 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, errorfds)) {
866 moonbr_log(LOG_ERR, "Could not create socket pair to redirect stderr of child process: %s", strerror(errno));
867 close(controlfds[0]);
868 close(controlfds[1]);
869 free(worker);
870 return -1;
871 }
872 if (moonbr_logfile && fflush(moonbr_logfile)) {
873 moonbr_log(LOG_CRIT, "Could not flush log file prior to forking: %s", strerror(errno));
874 moonbr_terminate_error();
875 }
876 worker->pid = fork();
877 if (worker->pid == -1) {
878 moonbr_log(LOG_ERR, "Could not fork: %s", strerror(errno));
879 close(controlfds[0]);
880 close(controlfds[1]);
881 close(errorfds[0]);
882 close(errorfds[1]);
883 free(worker);
884 return -1;
885 } else if (!worker->pid) {
886 moonbr_pstate = MOONBR_PSTATE_FORKED;
887 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
888 lua_pushliteral(L, "Failed to pass error message due to bug in Lua panic handler (hint: not enough memory?)");
889 #endif
890 moonbr_memory_limit = pool->memory_limit;
891 if (moonbr_pidfh && pidfile_close(moonbr_pidfh)) {
892 moonbr_child_emergency_error(errorfds[1], "Could not close PID file in forked child process");
893 }
894 if (moonbr_logfile && moonbr_logfile != stderr && fclose(moonbr_logfile)) {
895 moonbr_child_emergency_error(errorfds[1], "Could not close log file in forked child process");
896 }
897 if (dup2(errorfds[1], MOONBR_FD_STDERR) == -1) {
898 moonbr_child_emergency_error(errorfds[1], "Could not duplicate socket to stderr file descriptor");
899 }
900 if (dup2(controlfds[1], MOONBR_FD_CONTROL) == -1) {
901 moonbr_child_emergency_error(errorfds[1], "Could not duplicate control socket");
902 }
903 closefrom(MOONBR_FD_END);
904 moonbr_child_run(pool, L);
905 }
906 if (moonbr_stat) {
907 moonbr_log(LOG_INFO, "Created new worker in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
908 }
909 worker->controlfd = controlfds[0];
910 worker->errorfd = errorfds[0];
911 if (close(controlfds[1]) && errno != EINTR) {
912 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
913 moonbr_terminate_error();
914 }
915 if (close(errorfds[1]) && errno != EINTR) {
916 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
917 moonbr_terminate_error();
918 }
919 }
920 worker->prev_worker = pool->last_worker;
921 if (worker->prev_worker) worker->prev_worker->next_worker = worker;
922 else pool->first_worker = worker;
923 pool->last_worker = worker;
924 pool->unassigned_worker_count++;
925 pool->total_worker_count++;
926 pool->worker_count_stat = 1;
927 moonbr_poll_refresh_needed = 1;
928 return 0; /* return zero only in case of success */
929 }
932 /*** Functions to handle previously created 'struct moonbr_worker' structures ***/
934 #define moonbr_try_destroy_worker_stat(str, field) \
935 moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: " str " %li", worker->pool->poolnum, (int)worker->pid, (long)childusage.field);
937 /* Destroys a worker structure if socket connections have been closed and child process has terminated */
938 static int moonbr_try_destroy_worker(struct moonbr_worker *worker) {
939 if (worker->controlfd != -1 || worker->errorfd != -1) return MOONBR_DESTROY_NONE;
940 {
941 int childstatus;
942 struct rusage childusage;
943 {
944 pid_t waitedpid;
945 while (
946 (waitedpid = wait4(worker->pid, &childstatus, WNOHANG, &childusage)) == -1
947 ) {
948 if (errno != EINTR) {
949 moonbr_log(LOG_CRIT, "Error in wait4() call: %s", strerror(errno));
950 moonbr_terminate_error();
951 }
952 }
953 if (!waitedpid) return 0; /* return 0 if worker couldn't be destroyed */
954 if (waitedpid != worker->pid) {
955 moonbr_log(LOG_CRIT, "Wrong PID returned by wait4() call");
956 moonbr_terminate_error();
957 }
958 }
959 if (WIFEXITED(childstatus)) {
960 if (WEXITSTATUS(childstatus) || moonbr_stat) {
961 moonbr_log(
962 WEXITSTATUS(childstatus) ? LOG_WARNING : LOG_INFO,
963 "Child process in pool #%i with PID %i returned with exit code %i", worker->pool->poolnum, (int)worker->pid, WEXITSTATUS(childstatus)
964 );
965 }
966 } else if (WIFSIGNALED(childstatus)) {
967 if (WCOREDUMP(childstatus)) {
968 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));
969 } else if (WTERMSIG(childstatus) == SIGALRM) {
970 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i exited prematurely due to timeout", worker->pool->poolnum, (int)worker->pid);
971 } else {
972 moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
973 }
974 } else {
975 moonbr_log(LOG_CRIT, "Illegal exit status from child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
976 moonbr_terminate_error();
977 }
978 if (moonbr_stat) {
979 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));
980 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));
981 moonbr_try_destroy_worker_stat("max resident set size", ru_maxrss);
982 moonbr_try_destroy_worker_stat("integral shared memory size", ru_ixrss);
983 moonbr_try_destroy_worker_stat("integral unshared data", ru_idrss);
984 moonbr_try_destroy_worker_stat("integral unshared stack", ru_isrss);
985 moonbr_try_destroy_worker_stat("page replaims", ru_minflt);
986 moonbr_try_destroy_worker_stat("page faults", ru_majflt);
987 moonbr_try_destroy_worker_stat("swaps", ru_nswap);
988 moonbr_try_destroy_worker_stat("block input operations", ru_inblock);
989 moonbr_try_destroy_worker_stat("block output operations", ru_oublock);
990 moonbr_try_destroy_worker_stat("messages sent", ru_msgsnd);
991 moonbr_try_destroy_worker_stat("messages received", ru_msgrcv);
992 moonbr_try_destroy_worker_stat("signals received", ru_nsignals);
993 moonbr_try_destroy_worker_stat("voluntary context switches", ru_nvcsw);
994 moonbr_try_destroy_worker_stat("involuntary context switches", ru_nivcsw);
995 }
996 }
997 {
998 int retval = (
999 (worker->idle || worker->assigned) ?
1000 MOONBR_DESTROY_IDLE_OR_ASSIGNED :
1001 MOONBR_DESTROY_PREPARE
1002 );
1003 if (worker->prev_worker) worker->prev_worker->next_worker = worker->next_worker;
1004 else worker->pool->first_worker = worker->next_worker;
1005 if (worker->next_worker) worker->next_worker->prev_worker = worker->prev_worker;
1006 else worker->pool->last_worker = worker->prev_worker;
1007 if (worker->idle) {
1008 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker->next_idle_worker;
1009 else worker->pool->first_idle_worker = worker->next_idle_worker;
1010 if (worker->next_idle_worker) worker->next_idle_worker->prev_idle_worker = worker->prev_idle_worker;
1011 else worker->pool->last_idle_worker = worker->prev_idle_worker;
1012 worker->pool->idle_worker_count--;
1014 if (!worker->assigned) worker->pool->unassigned_worker_count--;
1015 worker->pool->total_worker_count--;
1016 worker->pool->worker_count_stat = 1;
1017 if (worker->errorlinebuf) free(worker->errorlinebuf);
1018 free(worker);
1019 return retval;
1023 /* Marks a worker as idle and stores it in a queue, optionally setting 'idle_expiration' value */
1024 static void moonbr_add_idle_worker(struct moonbr_worker *worker) {
1025 worker->prev_idle_worker = worker->pool->last_idle_worker;
1026 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker;
1027 else worker->pool->first_idle_worker = worker;
1028 worker->pool->last_idle_worker = worker;
1029 worker->idle = 1;
1030 worker->pool->idle_worker_count++;
1031 if (worker->assigned) {
1032 worker->assigned = 0;
1033 worker->pool->unassigned_worker_count++;
1035 worker->pool->worker_count_stat = 1;
1036 if (timerisset(&worker->pool->idle_timeout)) {
1037 struct timeval now;
1038 moonbr_now(&now);
1039 timeradd(&now, &worker->pool->idle_timeout, &worker->idle_expiration);
1043 /* Pops a worker from the queue of idle workers (idle queue must not be empty) */
1044 static struct moonbr_worker *moonbr_pop_idle_worker(struct moonbr_pool *pool) {
1045 struct moonbr_worker *worker;
1046 worker = pool->first_idle_worker;
1047 pool->first_idle_worker = worker->next_idle_worker;
1048 if (pool->first_idle_worker) pool->first_idle_worker->prev_idle_worker = NULL;
1049 else pool->last_idle_worker = NULL;
1050 worker->next_idle_worker = NULL;
1051 worker->idle = 0;
1052 worker->pool->idle_worker_count--;
1053 worker->assigned = 1;
1054 worker->pool->unassigned_worker_count--;
1055 worker->pool->worker_count_stat = 1;
1056 return worker;
1060 /*** Functions for queues of 'struct moonbr_listener' ***/
1062 /* Appends a 'struct moonbr_listener' to the queue of idle listeners and registers it for poll() */
1063 static void moonbr_add_idle_listener(struct moonbr_listener *listener) {
1064 listener->prev_listener = listener->pool->last_idle_listener;
1065 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
1066 else listener->pool->first_idle_listener = listener;
1067 listener->pool->last_idle_listener = listener;
1068 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events |= POLLIN;
1071 /* Removes a 'struct moonbr_listener' from the queue of idle listeners and unregisters it from poll() */
1072 static void moonbr_remove_idle_listener(struct moonbr_listener *listener) {
1073 if (listener->prev_listener) listener->prev_listener->next_listener = listener->next_listener;
1074 else listener->pool->first_idle_listener = listener->next_listener;
1075 if (listener->next_listener) listener->next_listener->prev_listener = listener->prev_listener;
1076 else listener->pool->last_idle_listener = listener->prev_listener;
1077 listener->prev_listener = NULL;
1078 listener->next_listener = NULL;
1079 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events &= ~POLLIN;
1082 /* Adds a listener to the queue of connected listeners (i.e. waiting to have their incoming connection accepted) */
1083 static void moonbr_add_connected_listener(struct moonbr_listener *listener) {
1084 listener->prev_listener = listener->pool->last_connected_listener;
1085 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
1086 else listener->pool->first_connected_listener = listener;
1087 listener->pool->last_connected_listener = listener;
1090 /* Removes and returns the first connected listener in the queue */
1091 static struct moonbr_listener *moonbr_pop_connected_listener(struct moonbr_pool *pool) {
1092 struct moonbr_listener *listener = pool->first_connected_listener;
1093 listener->pool->first_connected_listener = listener->next_listener;
1094 if (listener->pool->first_connected_listener) listener->pool->first_connected_listener->prev_listener = NULL;
1095 else listener->pool->last_connected_listener = NULL;
1096 listener->next_listener = NULL;
1097 return listener;
1101 /*** Functions to handle polling ***/
1103 /* Returns an index to a new initialized entry in moonbr_poll_fds[] */
1104 int moonbr_poll_fds_nextindex() {
1105 if (moonbr_poll_fds_count >= moonbr_poll_fds_bufsize) {
1106 if (moonbr_poll_fds_bufsize) moonbr_poll_fds_bufsize *= 2;
1107 else moonbr_poll_fds_bufsize = 1;
1108 moonbr_poll_fds = realloc(
1109 moonbr_poll_fds, moonbr_poll_fds_bufsize * sizeof(struct pollfd)
1110 );
1111 if (!moonbr_poll_fds) {
1112 moonbr_log(LOG_CRIT, "Memory allocation error");
1113 moonbr_terminate_error();
1116 moonbr_poll_fds[moonbr_poll_fds_count] = (struct pollfd){0, };
1117 return moonbr_poll_fds_count++;
1120 /* Returns an index to a new initialized entry in moonbr_poll_workers[] */
1121 int moonbr_poll_workers_nextindex() {
1122 if (moonbr_poll_worker_count >= moonbr_poll_workers_bufsize) {
1123 if (moonbr_poll_workers_bufsize) moonbr_poll_workers_bufsize *= 2;
1124 else moonbr_poll_workers_bufsize = 1;
1125 moonbr_poll_workers = realloc(
1126 moonbr_poll_workers, moonbr_poll_workers_bufsize * sizeof(struct moonbr_poll_worker)
1127 );
1128 if (!moonbr_poll_workers) {
1129 moonbr_log(LOG_CRIT, "Memory allocation error");
1130 moonbr_terminate_error();
1133 moonbr_poll_workers[moonbr_poll_worker_count] = (struct moonbr_poll_worker){0, };
1134 return moonbr_poll_worker_count++;
1137 /* Queues all listeners as idle, and initializes static part of moonbr_poll_fds[], which is passed to poll() */
1138 static void moonbr_poll_init() {
1139 if (socketpair(
1140 PF_LOCAL,
1141 SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1142 0,
1143 moonbr_poll_signalfds
1144 )) {
1145 moonbr_log(LOG_CRIT, "Could not create socket pair for signal delivery during polling: %s", strerror(errno));
1146 moonbr_terminate_error();
1149 int j = moonbr_poll_fds_nextindex();
1150 struct pollfd *pollfd = &moonbr_poll_fds[j];
1151 pollfd->fd = moonbr_poll_signalfd_read;
1152 pollfd->events = POLLIN;
1155 struct moonbr_pool *pool;
1156 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1157 int i;
1158 for (i=0; i<pool->listener_count; i++) {
1159 struct moonbr_listener *listener = &pool->listener[i];
1160 if (listener->listenfd != -1) {
1161 int j = moonbr_poll_fds_nextindex();
1162 listener->pollidx = j;
1163 moonbr_poll_fds[j].fd = listener->listenfd;
1165 moonbr_add_idle_listener(listener);
1169 moonbr_poll_fds_static_count = moonbr_poll_fds_count; /* remember size of static part of array */
1172 /* Disables polling of all listeners (required for clean shutdown) */
1173 static void moonbr_poll_shutdown() {
1174 int i;
1175 for (i=1; i<moonbr_poll_fds_static_count; i++) {
1176 moonbr_poll_fds[i].fd = -1;
1180 /* (Re)builds dynamic part of moonbr_poll_fds[] array, and (re)builds moonbr_poll_workers[] array */
1181 static void moonbr_poll_refresh() {
1182 moonbr_poll_refresh_needed = 0;
1183 moonbr_poll_fds_count = moonbr_poll_fds_static_count;
1184 moonbr_poll_worker_count = 0;
1186 struct moonbr_pool *pool;
1187 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1188 struct moonbr_worker *worker;
1189 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
1190 if (worker->controlfd != -1) {
1191 int j = moonbr_poll_fds_nextindex();
1192 int k = moonbr_poll_workers_nextindex();
1193 struct pollfd *pollfd = &moonbr_poll_fds[j];
1194 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1195 pollfd->fd = worker->controlfd;
1196 pollfd->events = POLLIN;
1197 poll_worker->channel = MOONBR_POLL_WORKER_CONTROLCHANNEL;
1198 poll_worker->worker = worker;
1200 if (worker->errorfd != -1) {
1201 int j = moonbr_poll_fds_nextindex();
1202 int k = moonbr_poll_workers_nextindex();
1203 struct pollfd *pollfd = &moonbr_poll_fds[j];
1204 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1205 pollfd->fd = worker->errorfd;
1206 pollfd->events = POLLIN;
1207 poll_worker->channel = MOONBR_POLL_WORKER_ERRORCHANNEL;
1208 poll_worker->worker = worker;
1215 /* resets socket and 'revents' field of moonbr_poll_fds[] for signal delivery just before poll() is called */
1216 static void moonbr_poll_reset_signal() {
1217 ssize_t readcount;
1218 char buf[1];
1219 moonbr_poll_fds[0].revents = 0;
1220 while ((readcount = read(moonbr_poll_signalfd_read, buf, 1)) < 0) {
1221 if (errno == EAGAIN) break;
1222 if (errno != EINTR) {
1223 moonbr_log(LOG_CRIT, "Error while reading from signal delivery socket: %s", strerror(errno));
1224 moonbr_terminate_error();
1227 if (!readcount) {
1228 moonbr_log(LOG_CRIT, "Unexpected EOF when reading from signal delivery socket: %s", strerror(errno));
1229 moonbr_terminate_error();
1234 /*** Shutdown initiation ***/
1236 /* Sets global variable 'moonbr_shutdown_in_progress', closes listeners, and demands worker termination */
1237 static void moonbr_initiate_shutdown() {
1238 struct moonbr_pool *pool;
1239 int i;
1240 if (moonbr_shutdown_in_progress) {
1241 moonbr_log(LOG_NOTICE, "Shutdown already in progress");
1242 return;
1244 moonbr_shutdown_in_progress = 1;
1245 moonbr_log(LOG_NOTICE, "Initiate shutdown");
1246 for (pool = moonbr_first_pool; pool; pool = pool->next_pool) {
1247 for (i=0; i<pool->listener_count; i++) {
1248 struct moonbr_listener *listener = &pool->listener[i];
1249 if (listener->listenfd != -1) {
1250 if (close(listener->listenfd) && errno != EINTR) {
1251 moonbr_log(LOG_CRIT, "Could not close listening socket: %s", strerror(errno));
1252 moonbr_terminate_error();
1256 pool->pre_fork = 0;
1257 pool->min_fork = 0;
1258 pool->max_fork = 0;
1259 timerclear(&pool->exit_delay);
1261 moonbr_poll_shutdown(); /* avoids loops due to error condition when polling closed listeners */
1265 /*** Functions to communicate with child processes ***/
1267 /* Tells child process to terminate */
1268 static void moonbr_terminate_idle_worker(struct moonbr_worker *worker) {
1269 moonbr_send_control_message(worker, MOONBR_COMMAND_TERMINATE, -1, NULL);
1272 /* Handles status messages from child process */
1273 static void moonbr_read_controlchannel(struct moonbr_worker *worker) {
1274 char controlmsg;
1276 ssize_t bytes_read;
1277 while ((bytes_read = read(worker->controlfd, &controlmsg, 1)) <= 0) {
1278 if (bytes_read == 0 || errno == ECONNRESET) {
1279 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i unexpectedly closed control socket", worker->pool->poolnum, (int)worker->pid);
1280 if (close(worker->controlfd) && errno != EINTR) {
1281 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));
1282 moonbr_terminate_error();
1284 worker->controlfd = -1;
1285 moonbr_poll_refresh_needed = 1;
1286 return;
1288 if (errno != EINTR) {
1289 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));
1290 moonbr_terminate_error();
1294 if (worker->idle) {
1295 moonbr_log(LOG_CRIT, "Unexpected data from supposedly idle child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1296 moonbr_terminate_error();
1298 if (moonbr_debug) {
1299 moonbr_log(LOG_DEBUG, "Received control message from child in pool #%i with PID %i: \"%c\"", worker->pool->poolnum, (int)worker->pid, (int)controlmsg);
1301 switch (controlmsg) {
1302 case MOONBR_STATUS_IDLE:
1303 if (moonbr_stat) {
1304 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i reports as idle", worker->pool->poolnum, (int)worker->pid);
1306 moonbr_add_idle_worker(worker);
1307 break;
1308 case MOONBR_STATUS_GOODBYE:
1309 if (moonbr_stat) {
1310 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i announced termination", worker->pool->poolnum, (int)worker->pid);
1312 if (close(worker->controlfd) && errno != EINTR) {
1313 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));
1314 moonbr_terminate_error();
1316 worker->controlfd = -1;
1317 moonbr_poll_refresh_needed = 1;
1318 break;
1319 default:
1320 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);
1321 moonbr_terminate_error();
1325 /* Handles stderr stream from child process */
1326 static void moonbr_read_errorchannel(struct moonbr_worker *worker) {
1327 char staticbuf[MOONBR_MAXERRORLINELEN+1];
1328 char *buf = worker->errorlinebuf;
1329 if (!buf) buf = staticbuf;
1331 ssize_t bytes_read;
1332 while (
1333 (bytes_read = read(
1334 worker->errorfd,
1335 buf + worker->errorlinelen,
1336 MOONBR_MAXERRORLINELEN+1 - worker->errorlinelen
1337 )) <= 0
1338 ) {
1339 if (bytes_read == 0 || errno == ECONNRESET) {
1340 if (moonbr_debug) {
1341 moonbr_log(LOG_DEBUG, "Child process in pool #%i with PID %i closed stderr socket", worker->pool->poolnum, (int)worker->pid);
1343 if (close(worker->errorfd) && errno != EINTR) {
1344 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));
1345 moonbr_terminate_error();
1347 worker->errorfd = -1;
1348 moonbr_poll_refresh_needed = 1;
1349 break;
1351 if (errno != EINTR) {
1352 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));
1353 moonbr_terminate_error();
1356 worker->errorlinelen += bytes_read;
1359 int i;
1360 for (i=0; i<worker->errorlinelen; i++) {
1361 if (buf[i] == '\n') buf[i] = 0;
1362 if (!buf[i]) {
1363 if (worker->errorlineovf) {
1364 worker->errorlineovf = 0;
1365 } else {
1366 moonbr_log(LOG_WARNING, "Error log from process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, buf);
1368 worker->errorlinelen -= i+1;
1369 memmove(buf, buf+i+1, worker->errorlinelen);
1370 i = -1;
1373 if (i > MOONBR_MAXERRORLINELEN) {
1374 buf[MOONBR_MAXERRORLINELEN] = 0;
1375 if (!worker->errorlineovf) {
1376 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);
1378 worker->errorlinelen = 0;
1379 worker->errorlineovf = 1;
1382 if (!worker->errorlinebuf && worker->errorlinelen) { /* allocate buffer on heap only if necessary */
1383 worker->errorlinebuf = malloc((MOONBR_MAXERRORLINELEN+1) * sizeof(char));
1384 if (!worker->errorlinebuf) {
1385 moonbr_log(LOG_CRIT, "Memory allocation error");
1386 moonbr_terminate_error();
1388 memcpy(worker->errorlinebuf, staticbuf, worker->errorlinelen);
1393 /*** Handler for incoming connections ***/
1395 /* Accepts one or more incoming connections on listener socket and passes it to worker(s) popped from idle queue */
1396 static void moonbr_connect(struct moonbr_pool *pool) {
1397 struct moonbr_listener *listener = moonbr_pop_connected_listener(pool);
1398 struct moonbr_worker *worker;
1399 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1400 worker = moonbr_pop_idle_worker(pool);
1401 if (moonbr_stat) {
1402 moonbr_log(LOG_INFO, "Dispatching interval timer \"%s\" of pool #%i to PID %i", listener->type_specific.interval.name, listener->pool->poolnum, (int)worker->pid);
1404 worker->restart_interval_listener = listener;
1405 moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, -1, listener);
1406 /* do not push listener to queue of idle listeners yet */
1407 } else {
1408 int peerfd;
1409 do {
1410 peerfd = accept4(listener->listenfd, NULL, NULL, SOCK_CLOEXEC);
1411 if (peerfd == -1) {
1412 if (errno == EWOULDBLOCK) {
1413 break;
1414 } else if (errno == ECONNABORTED) {
1415 moonbr_log(LOG_WARNING, "Connection aborted before accepting it");
1416 break;
1417 } else if (errno != EINTR) {
1418 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1419 moonbr_terminate_error();
1421 } else {
1422 worker = moonbr_pop_idle_worker(pool);
1423 if (moonbr_stat) {
1424 moonbr_log(LOG_INFO, "Dispatching connection for pool #%i to PID %i", listener->pool->poolnum, (int)worker->pid);
1426 moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, peerfd, listener);
1427 if (close(peerfd) && errno != EINTR) {
1428 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1429 moonbr_terminate_error();
1432 } while (pool->first_idle_worker);
1433 moonbr_add_idle_listener(listener);
1438 /*** Functions to initialize and restart interval timers ***/
1440 /* Initializes all interval timers */
1441 static void moonbr_interval_initialize() {
1442 struct timeval now;
1443 struct moonbr_pool *pool;
1444 moonbr_now(&now);
1445 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1446 int i;
1447 for (i=0; i<pool->listener_count; i++) {
1448 struct moonbr_listener *listener = &pool->listener[i];
1449 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1450 timeradd(
1451 &now,
1452 &listener->type_specific.interval.delay,
1453 &listener->type_specific.interval.wakeup
1454 );
1460 /* If necessary, restarts interval timers and queues interval listener as idle after a worker changed status */
1461 static void moonbr_interval_restart(
1462 struct moonbr_worker *worker,
1463 struct timeval *now /* passed to synchronize with moonbr_run() function */
1464 ) {
1465 struct moonbr_listener *listener = worker->restart_interval_listener;
1466 if (listener) {
1467 moonbr_add_idle_listener(listener);
1468 worker->restart_interval_listener = NULL;
1469 if (listener->type_specific.interval.strict) {
1470 timeradd(
1471 &listener->type_specific.interval.wakeup,
1472 &listener->type_specific.interval.delay,
1473 &listener->type_specific.interval.wakeup
1474 );
1475 if (timercmp(&listener->type_specific.interval.wakeup, now, <)) {
1476 listener->type_specific.interval.wakeup = *now;
1478 } else {
1479 timeradd(
1480 now,
1481 &listener->type_specific.interval.delay,
1482 &listener->type_specific.interval.wakeup
1483 );
1489 /*** Main loop and helper functions ***/
1491 /* Stores the earliest required wakeup time in 'wait' variable */
1492 static void moonbr_calc_wait(struct timeval *wait, struct timeval *wakeup) {
1493 if (!timerisset(wait) || timercmp(wakeup, wait, <)) *wait = *wakeup;
1496 /* Main loop of Moonbridge system (including initialization of signal handlers and polling structures) */
1497 static void moonbr_run(lua_State *L) {
1498 struct timeval now;
1499 struct moonbr_pool *pool;
1500 struct moonbr_worker *worker;
1501 struct moonbr_worker *next_worker; /* needed when worker is removed during iteration of workers */
1502 struct moonbr_listener *listener;
1503 struct moonbr_listener *next_listener; /* needed when listener is removed during iteration of listeners */
1504 int i;
1505 moonbr_poll_init(); /* must be executed before moonbr_signal_init() */
1506 moonbr_signal_init();
1507 moonbr_interval_initialize();
1508 moonbr_pstate = MOONBR_PSTATE_RUNNING;
1509 while (1) {
1510 struct timeval wait = {0, }; /* point in time when premature wakeup of poll() is required */
1511 if (moonbr_cond_interrupt) {
1512 moonbr_log(LOG_WARNING, "Fast shutdown requested");
1513 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
1515 if (moonbr_cond_terminate) {
1516 moonbr_initiate_shutdown();
1517 moonbr_cond_terminate = 0;
1519 moonbr_cond_child = 0; /* must not be reset between moonbr_try_destroy_worker() and poll() */
1520 moonbr_now(&now);
1521 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1522 int terminated_worker_count = 0; /* allows shortcut for new worker creation */
1523 /* terminate idle workers when expired */
1524 if (timerisset(&pool->idle_timeout)) {
1525 while ((worker = pool->first_idle_worker) != NULL) {
1526 if (timercmp(&worker->idle_expiration, &now, >)) break;
1527 moonbr_pop_idle_worker(pool);
1528 moonbr_terminate_idle_worker(worker);
1531 /* mark listeners as connected when incoming connection is pending */
1532 for (listener=pool->first_idle_listener; listener; listener=next_listener) {
1533 next_listener = listener->next_listener; /* extra variable necessary due to changing list */
1534 if (listener->pollidx != -1) {
1535 if (moonbr_poll_fds[listener->pollidx].revents) {
1536 moonbr_poll_fds[listener->pollidx].revents = 0;
1537 moonbr_remove_idle_listener(listener);
1538 moonbr_add_connected_listener(listener);
1540 } else if (listener->proto == MOONBR_PROTO_INTERVAL) {
1541 if (!timercmp(&listener->type_specific.interval.wakeup, &now, >)) {
1542 moonbr_remove_idle_listener(listener);
1543 moonbr_add_connected_listener(listener);
1545 } else {
1546 moonbr_log(LOG_CRIT, "Internal error (should not happen): Listener is neither an interval timer nor has the 'pollidx' value set");
1547 moonbr_terminate_error();
1550 /* process input from child processes */
1551 for (i=0; i<moonbr_poll_worker_count; i++) {
1552 if (moonbr_poll_worker_fds[i].revents) {
1553 moonbr_poll_worker_fds[i].revents = 0;
1554 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[i];
1555 switch (poll_worker->channel) {
1556 case MOONBR_POLL_WORKER_CONTROLCHANNEL:
1557 moonbr_read_controlchannel(poll_worker->worker);
1558 moonbr_interval_restart(poll_worker->worker, &now);
1559 break;
1560 case MOONBR_POLL_WORKER_ERRORCHANNEL:
1561 moonbr_read_errorchannel(poll_worker->worker);
1562 break;
1566 /* collect dead child processes */
1567 for (worker=pool->first_worker; worker; worker=next_worker) {
1568 next_worker = worker->next_worker; /* extra variable necessary due to changing list */
1569 switch (moonbr_try_destroy_worker(worker)) {
1570 case MOONBR_DESTROY_PREPARE:
1571 pool->use_fork_error_wakeup = 1;
1572 break;
1573 case MOONBR_DESTROY_IDLE_OR_ASSIGNED:
1574 terminated_worker_count++;
1575 break;
1578 /* connect listeners with idle workers */
1579 if (!moonbr_shutdown_in_progress) {
1580 while (pool->first_connected_listener && pool->first_idle_worker) {
1581 moonbr_connect(pool);
1584 /* create new worker processes */
1585 while (
1586 pool->total_worker_count < pool->max_fork && (
1587 pool->unassigned_worker_count < pool->pre_fork ||
1588 pool->total_worker_count < pool->min_fork
1590 ) {
1591 if (pool->use_fork_error_wakeup) {
1592 if (timercmp(&pool->fork_error_wakeup, &now, >)) {
1593 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
1594 break;
1596 } else {
1597 if (terminated_worker_count) {
1598 terminated_worker_count--;
1599 } else if (timercmp(&pool->fork_wakeup, &now, >)) {
1600 moonbr_calc_wait(&wait, &pool->fork_wakeup);
1601 break;
1604 if (moonbr_create_worker(pool, L)) {
1605 /* on error, enforce error delay */
1606 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
1607 pool->use_fork_error_wakeup = 1;
1608 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
1609 break;
1610 } else {
1611 /* normal fork delay on success */
1612 timeradd(&now, &pool->fork_delay, &pool->fork_wakeup);
1613 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
1614 pool->use_fork_error_wakeup = 0; /* gets set later if error occures during preparation */
1617 /* terminate excessive worker processes */
1618 while (
1619 pool->total_worker_count > pool->min_fork &&
1620 pool->idle_worker_count > pool->pre_fork
1621 ) {
1622 if (timerisset(&pool->exit_wakeup)) {
1623 if (timercmp(&pool->exit_wakeup, &now, >)) {
1624 moonbr_calc_wait(&wait, &pool->exit_wakeup);
1625 break;
1627 moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
1628 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
1629 } else {
1630 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
1631 break;
1634 if (!(
1635 pool->total_worker_count > pool->min_fork &&
1636 pool->idle_worker_count > pool->pre_fork
1637 )) {
1638 timerclear(&pool->exit_wakeup); /* timer gets restarted later when there are excessive workers */
1640 /* optionally output worker count stats */
1641 if (moonbr_stat && pool->worker_count_stat) {
1642 pool->worker_count_stat = 0;
1643 moonbr_log(
1644 LOG_INFO,
1645 "Worker count for pool #%i: %i idle, %i assigned, %i total",
1646 pool->poolnum, pool->idle_worker_count,
1647 pool->total_worker_count - pool->unassigned_worker_count,
1648 pool->total_worker_count);
1650 /* calculate wakeup time for interval listeners */
1651 for (listener=pool->first_idle_listener; listener; listener=listener->next_listener) {
1652 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1653 moonbr_calc_wait(&wait, &listener->type_specific.interval.wakeup);
1656 /* calculate wakeup time for idle workers (only first idle worker is significant) */
1657 if (timerisset(&pool->idle_timeout) && pool->first_idle_worker) {
1658 moonbr_calc_wait(&wait, &pool->first_idle_worker->idle_expiration);
1661 /* check if shutdown is complete */
1662 if (moonbr_shutdown_in_progress) {
1663 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1664 if (pool->first_worker) break;
1666 if (!pool) {
1667 moonbr_log(LOG_INFO, "All worker threads have terminated");
1668 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
1671 if (moonbr_poll_refresh_needed) moonbr_poll_refresh();
1672 moonbr_cond_poll = 1;
1673 if (!moonbr_cond_child && !moonbr_cond_terminate && !moonbr_cond_interrupt) {
1674 int timeout;
1675 if (timerisset(&wait)) {
1676 if (timercmp(&wait, &now, <)) {
1677 moonbr_log(LOG_CRIT, "Internal error (should not happen): Future is in the past");
1678 moonbr_terminate_error();
1680 timersub(&wait, &now, &wait);
1681 timeout = wait.tv_sec * 1000 + wait.tv_usec / 1000;
1682 } else {
1683 timeout = INFTIM;
1685 if (moonbr_debug) {
1686 moonbr_log(LOG_DEBUG, "Waiting for I/O");
1688 poll(moonbr_poll_fds, moonbr_poll_fds_count, timeout);
1689 } else {
1690 if (moonbr_debug) {
1691 moonbr_log(LOG_DEBUG, "Do not wait for I/O");
1694 moonbr_cond_poll = 0;
1695 moonbr_poll_reset_signal();
1700 /*** Lua interface ***/
1702 static int moonbr_lua_panic(lua_State *L) {
1703 const char *errmsg;
1704 errmsg = lua_tostring(L, -1);
1705 if (!errmsg) {
1706 if (lua_isnoneornil(L, -1)) errmsg = "(error message is nil)";
1707 else errmsg = "(error message is not a string)";
1709 if (moonbr_pstate == MOONBR_PSTATE_FORKED) {
1710 fprintf(stderr, "Uncaught Lua error: %s\n", errmsg);
1711 exit(1);
1712 } else {
1713 moonbr_log(LOG_CRIT, "Uncaught Lua error: %s", errmsg);
1714 moonbr_terminate_error();
1716 return 0;
1719 static int moonbr_addtraceback(lua_State *L) {
1720 luaL_traceback(L, L, luaL_tolstring(L, 1, NULL), 1);
1721 return 1;
1724 /* Memory allocator that allows limiting memory consumption */
1725 static void *moonbr_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
1726 (void)ud; /* not used */
1727 if (nsize == 0) {
1728 if (ptr) {
1729 moonbr_memory_usage -= osize;
1730 free(ptr);
1732 return NULL;
1733 } else if (ptr) {
1734 if (
1735 moonbr_memory_limit &&
1736 nsize > osize &&
1737 moonbr_memory_usage + (nsize - osize) > moonbr_memory_limit
1738 ) {
1739 return NULL;
1740 } else {
1741 ptr = realloc(ptr, nsize);
1742 if (ptr) moonbr_memory_usage += nsize - osize;
1744 } else {
1745 if (
1746 moonbr_memory_limit &&
1747 moonbr_memory_usage + nsize > moonbr_memory_limit
1748 ) {
1749 return NULL;
1750 } else {
1751 ptr = realloc(ptr, nsize);
1752 if (ptr) moonbr_memory_usage += nsize;
1755 return ptr;
1758 /* New method for Lua file objects: read until terminator or length exceeded */
1759 static int moonbr_readuntil(lua_State *L) {
1760 luaL_Stream *stream;
1761 FILE *file;
1762 const char *terminatorstr;
1763 size_t terminatorlen;
1764 luaL_Buffer buf;
1765 lua_Integer maxlen;
1766 char terminator;
1767 int byte;
1768 stream = luaL_checkudata(L, 1, LUA_FILEHANDLE);
1769 terminatorstr = luaL_checklstring(L, 2, &terminatorlen);
1770 luaL_argcheck(L, terminatorlen == 1, 2, "single byte expected");
1771 maxlen = luaL_optinteger(L, 3, 0);
1772 if (!stream->closef) luaL_error(L, "attempt to use a closed file");
1773 file = stream->f;
1774 luaL_buffinit(L, &buf);
1775 if (!maxlen) maxlen = -1;
1776 terminator = terminatorstr[0];
1777 while (maxlen > 0 ? maxlen-- : maxlen) {
1778 byte = fgetc(file);
1779 if (byte == EOF) {
1780 if (ferror(file)) {
1781 char errmsg[MOONBR_MAXSTRERRORLEN];
1782 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
1783 lua_pushnil(L);
1784 lua_pushstring(L, errmsg);
1785 return 2;
1786 } else {
1787 break;
1790 luaL_addchar(&buf, byte);
1791 if (byte == terminator) break;
1793 luaL_pushresult(&buf);
1794 if (!lua_rawlen(L, -1)) lua_pushnil(L);
1795 return 1;
1798 static int moonbr_lua_tonatural(lua_State *L, int idx) {
1799 int isnum;
1800 lua_Number n;
1801 n = lua_tonumberx(L, idx, &isnum);
1802 if (isnum && n>=0 && n<INT_MAX && (lua_Number)(int)n == n) return n;
1803 else return -1;
1806 static int moonbr_lua_totimeval(lua_State *L, int idx, struct timeval *value) {
1807 int isnum;
1808 lua_Number n;
1809 n = lua_tonumberx(L, idx, &isnum);
1810 if (isnum && n>=0 && n<=100000000) {
1811 value->tv_sec = n;
1812 value->tv_usec = 1e6 * (n - value->tv_sec);
1813 return 1;
1814 } else {
1815 return 0;
1819 static int moonbr_timeout(lua_State *L) {
1820 struct itimerval oldval;
1821 if (lua_isnoneornil(L, 1) && lua_isnoneornil(L, 2)) {
1822 getitimer(ITIMER_REAL, &oldval);
1823 } else {
1824 struct itimerval newval = {};
1825 timerclear(&newval.it_interval);
1826 timerclear(&newval.it_value);
1827 if (lua_toboolean(L, 1)) {
1828 luaL_argcheck(
1829 L, moonbr_lua_totimeval(L, 1, &newval.it_value), 1,
1830 "interval in seconds expected"
1831 );
1833 if (lua_isnoneornil(L, 2)) {
1834 if (setitimer(ITIMER_REAL, &newval, &oldval)) {
1835 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1836 moonbr_terminate_error();
1838 } else {
1839 getitimer(ITIMER_REAL, &oldval);
1840 if (!timerisset(&oldval.it_value)) {
1841 if (setitimer(ITIMER_REAL, &newval, NULL)) {
1842 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1843 moonbr_terminate_error();
1845 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
1846 timerclear(&newval.it_value);
1847 if (setitimer(ITIMER_REAL, &newval, NULL)) {
1848 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1849 moonbr_terminate_error();
1851 } else if (timercmp(&newval.it_value, &oldval.it_value, <)) {
1852 struct itimerval remval;
1853 if (setitimer(ITIMER_REAL, &newval, NULL)) {
1854 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1855 moonbr_terminate_error();
1857 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
1858 getitimer(ITIMER_REAL, &remval);
1859 timersub(&oldval.it_value, &newval.it_value, &newval.it_value);
1860 timeradd(&newval.it_value, &remval.it_value, &newval.it_value);
1861 if (setitimer(ITIMER_REAL, &newval, NULL)) {
1862 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1863 moonbr_terminate_error();
1865 } else {
1866 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
1868 return lua_gettop(L) - 1;
1871 lua_pushnumber(L, oldval.it_value.tv_sec + 1e-6 * oldval.it_value.tv_usec);
1872 return 1;
1875 #define moonbr_listen_init_pool_forkoption(luaname, cname, defval) { \
1876 lua_getfield(L, 2, luaname); \
1877 pool->cname = lua_isnil(L, -1) ? (defval) : moonbr_lua_tonatural(L, -1); \
1878 } while(0)
1880 #define moonbr_listen_init_pool_timeoption(luaname, cname, defval, defvalu) ( \
1881 lua_getfield(L, 2, luaname), \
1882 lua_isnil(L, -1) ? ( \
1883 pool->cname.tv_sec = (defval), pool->cname.tv_usec = (defvalu), \
1884 1 \
1885 ) : ( \
1886 (lua_isboolean(L, -1) && !lua_toboolean(L, -1)) ? ( \
1887 pool->cname.tv_sec = 0, pool->cname.tv_usec = 0, \
1888 1 \
1889 ) : ( \
1890 moonbr_lua_totimeval(L, -1, &pool->cname) \
1891 ) \
1892 ) \
1895 static int moonbr_listen_init_pool(lua_State *L) {
1896 struct moonbr_pool *pool;
1897 const char *proto;
1898 int i;
1899 pool = lua_touserdata(L, 1);
1900 for (i=0; i<pool->listener_count; i++) {
1901 struct moonbr_listener *listener = &pool->listener[i];
1902 lua_settop(L, 2);
1903 #if LUA_VERSION_NUM >= 503
1904 lua_geti(L, 2, i+1);
1905 #else
1906 lua_pushinteger(L, i+1);
1907 lua_gettable(L, 2);
1908 #endif
1909 lua_getfield(L, 3, "proto");
1910 proto = lua_tostring(L, -1);
1911 if (proto && !strcmp(proto, "interval")) {
1912 listener->proto = MOONBR_PROTO_INTERVAL;
1913 lua_getfield(L, 3, "name");
1915 const char *name = lua_tostring(L, -1);
1916 if (name) {
1917 if (asprintf(&listener->type_specific.interval.name, "%s", name) < 0) {
1918 moonbr_log(LOG_CRIT, "Memory allocation_error");
1919 moonbr_terminate_error();
1923 lua_getfield(L, 3, "delay");
1924 if (
1925 !moonbr_lua_totimeval(L, -1, &listener->type_specific.interval.delay) ||
1926 !timerisset(&listener->type_specific.interval.delay)
1927 ) {
1928 luaL_error(L, "No valid interval delay specified; use listen{{proto=\"interval\", delay=...}, ...}");
1930 lua_getfield(L, 3, "strict");
1931 if (!lua_isnil(L, -1)) {
1932 if (lua_isboolean(L, -1)) {
1933 if (lua_toboolean(L, -1)) listener->type_specific.interval.strict = 1;
1934 } else {
1935 luaL_error(L, "Option \"strict\" must be a boolean if set; use listen{{proto=\"interval\", strict=true, ...}, ...}");
1938 } else if (proto && !strcmp(proto, "local")) {
1939 const char *path;
1940 const int path_maxlen = (
1941 sizeof(listener->type_specific.socket.addr.addr_un) -
1942 ((void *)listener->type_specific.socket.addr.addr_un.sun_path - (void *)&listener->type_specific.socket.addr.addr_un)
1943 ) - 1; /* one byte for termination */
1944 listener->proto = MOONBR_PROTO_LOCAL;
1945 lua_getfield(L, 3, "path");
1946 path = lua_tostring(L, -1);
1947 if (!path) {
1948 luaL_error(L, "No valid path specified for local socket; use listen{{proto=\"local\", path=...}, ...}");
1950 if (strlen(path) > path_maxlen) {
1951 luaL_error(L, "Path name for local socket exceeded maximum length of %i characters", path_maxlen);
1953 strcpy(listener->type_specific.socket.addr.addr_un.sun_path, path);
1954 } else if (proto && !strcmp(proto, "tcp")) {
1955 const char *host, *port;
1956 struct addrinfo hints = { 0, };
1957 struct addrinfo *res, *addrinfo;
1958 int errcode;
1959 const char *ip;
1960 lua_getfield(L, 3, "host");
1961 host = lua_isnil(L, -1) ? "::" : lua_tostring(L, -1);
1962 if (!host) {
1963 luaL_error(L, "No host specified; use listen{{proto=\"tcp\", host=...}, ...}");
1965 lua_getfield(L, 3, "port");
1966 port = lua_tostring(L, -1);
1967 if (!port) {
1968 luaL_error(L, "No port specified; use listen{{proto=\"tcp\", host=...}, ...}");
1970 hints.ai_family = AF_UNSPEC;
1971 hints.ai_socktype = SOCK_STREAM;
1972 hints.ai_protocol = IPPROTO_TCP;
1973 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
1974 errcode = getaddrinfo(host, port, &hints, &res);
1975 if (errcode) {
1976 freeaddrinfo(res);
1977 if (errcode == EAI_SYSTEM) {
1978 char errmsg[MOONBR_MAXSTRERRORLEN];
1979 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
1980 luaL_error(L, "Could not resolve host: %s: %s", gai_strerror(errcode), errmsg);
1981 } else {
1982 luaL_error(L, "Could not resolve host: %s", gai_strerror(errcode));
1985 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1986 if (addrinfo->ai_family == AF_INET6) goto moonbr_listen_init_pool_found;
1988 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1989 if (addrinfo->ai_family == AF_INET) goto moonbr_listen_init_pool_found;
1991 addrinfo = res;
1992 moonbr_listen_init_pool_found:
1993 if (addrinfo->ai_addrlen > sizeof(listener->type_specific.socket.addr)) {
1994 moonbr_log(LOG_CRIT, "Size of ai_addrlen is unexpectedly big (should not happen)");
1995 moonbr_terminate_error();
1997 memcpy(&listener->type_specific.socket.addr, addrinfo->ai_addr, addrinfo->ai_addrlen);
1998 listener->type_specific.socket.addrlen = addrinfo->ai_addrlen;
1999 switch (addrinfo->ai_family) {
2000 case AF_INET6:
2001 ip = inet_ntop(
2002 addrinfo->ai_family,
2003 &((struct sockaddr_in6 *)addrinfo->ai_addr)->sin6_addr,
2004 listener->proto_specific.tcp.ip,
2005 INET6_ADDRSTRLEN
2006 );
2007 if (!ip) {
2008 moonbr_log(LOG_CRIT, "System error in inet_ntop call: %s", strerror(errno));
2009 moonbr_terminate_error();
2011 listener->proto_specific.tcp.port = ntohs(((struct sockaddr_in6 *)addrinfo->ai_addr)->sin6_port);
2012 break;
2013 case AF_INET:
2014 ip = inet_ntop(
2015 addrinfo->ai_family,
2016 &((struct sockaddr_in *)addrinfo->ai_addr)->sin_addr,
2017 listener->proto_specific.tcp.ip,
2018 INET6_ADDRSTRLEN
2019 );
2020 if (!ip) {
2021 moonbr_log(LOG_CRIT, "System error in inet_ntop call: %s", strerror(errno));
2022 moonbr_terminate_error();
2024 listener->proto_specific.tcp.port = ntohs(((struct sockaddr_in *)addrinfo->ai_addr)->sin_port);
2025 break;
2026 default:
2027 strcpy(listener->proto_specific.tcp.ip, "unknown");
2028 listener->proto_specific.tcp.port = 0;
2030 listener->proto = MOONBR_PROTO_TCP;
2031 } else if (proto) {
2032 luaL_error(L, "Unknown protocol \"%s\"", proto);
2033 } else {
2034 luaL_error(L, "No valid protocol specified; use listen{{proto=..., ...}, ...}");
2037 lua_settop(L, 2);
2038 moonbr_listen_init_pool_forkoption("pre_fork", pre_fork, 1);
2039 moonbr_listen_init_pool_forkoption("min_fork", min_fork, pool->pre_fork > 2 ? pool->pre_fork : 2);
2040 moonbr_listen_init_pool_forkoption("max_fork", max_fork, pool->min_fork > 16 ? pool->min_fork : 16);
2041 if (!moonbr_listen_init_pool_timeoption("fork_delay", fork_delay, 0, 250000)) {
2042 luaL_error(L, "Option \"fork_delay\" is expected to be a non-negative number");
2044 if (!moonbr_listen_init_pool_timeoption("fork_error_delay", fork_error_delay, 2, 0)) {
2045 luaL_error(L, "Option \"fork_error_delay\" is expected to be a non-negative number");
2047 if (!moonbr_listen_init_pool_timeoption("exit_delay", exit_delay, 60, 0)) {
2048 luaL_error(L, "Option \"exit_delay\" is expected to be a non-negative number");
2050 if (timercmp(&pool->fork_error_delay, &pool->fork_delay, <)) {
2051 pool->fork_error_delay = pool->fork_delay;
2053 if (!moonbr_listen_init_pool_timeoption("idle_timeout", idle_timeout, 0, 0)) {
2054 luaL_error(L, "Option \"idle_timeout\" is expected to be a non-negative number");
2056 lua_getfield(L, 2, "memory_limit");
2057 if (!lua_isnil(L, -1)) {
2058 int isnum;
2059 lua_Number n;
2060 n = lua_tonumberx(L, -1, &isnum);
2061 if (n < 0 || !isnum) {
2062 luaL_error(L, "Option \"memory_limit\" is expected to be a non-negative number");
2064 pool->memory_limit = n;
2066 lua_settop(L, 2);
2067 lua_getfield(L, 2, "prepare");
2068 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2069 luaL_error(L, "Option \"prepare\" must be nil or a function");
2071 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2072 lua_getfield(L, 2, "connect");
2073 if (!lua_isfunction(L, -1)) {
2074 luaL_error(L, "Option \"connect\" must be a function; use listen{{...}, {...}, connect=function(socket) ... end, ...}");
2076 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2077 lua_getfield(L, 2, "finish");
2078 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2079 luaL_error(L, "Option \"finish\" must be nil or a function");
2081 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2082 return 0;
2085 static int moonbr_listen(lua_State *L) {
2086 struct moonbr_pool *pool;
2087 lua_Integer listener_count;
2088 if (moonbr_booted) luaL_error(L, "Moonbridge bootup is already complete");
2089 luaL_checktype(L, 1, LUA_TTABLE);
2090 listener_count = luaL_len(L, 1);
2091 if (!listener_count) luaL_error(L, "No listen ports specified; use listen{{proto=..., port=...},...}");
2092 if (listener_count > 100) luaL_error(L, "Too many listeners");
2093 pool = moonbr_create_pool(listener_count);
2094 lua_pushcfunction(L, moonbr_listen_init_pool);
2095 lua_pushlightuserdata(L, pool);
2096 lua_pushvalue(L, 1);
2097 if (lua_pcall(L, 2, 0, 0)) goto moonbr_listen_error;
2099 int i;
2100 i = moonbr_start_pool(pool);
2101 if (i >= 0) {
2102 lua_pushfstring(L, "Could not initialize listener #%d: %s", i+1, strerror(errno));
2103 moonbr_listen_error:
2104 moonbr_destroy_pool(pool);
2105 lua_pushnil(L);
2106 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2107 lua_pushnil(L);
2108 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2109 lua_pushnil(L);
2110 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2111 lua_error(L);
2114 return 0;
2118 /*** Function to modify Lua's library path and/or cpath ***/
2120 #if defined(MOONBR_LUA_PATH) || defined(MOONBR_LUA_CPATH)
2121 static void moonbr_modify_path(lua_State *L, char *key, char *value) {
2122 int stackbase;
2123 stackbase = lua_gettop(L);
2124 lua_getglobal(L, "package");
2125 lua_getfield(L, stackbase+1, key);
2127 const char *current_str;
2128 size_t current_strlen;
2129 luaL_Buffer buf;
2130 current_str = lua_tolstring(L, stackbase+2, &current_strlen);
2131 luaL_buffinit(L, &buf);
2132 if (current_str) {
2133 lua_pushvalue(L, stackbase+2);
2134 luaL_addvalue(&buf);
2135 if (current_strlen && current_str[current_strlen-1] != ';') {
2136 luaL_addchar(&buf, ';');
2139 luaL_addstring(&buf, value);
2140 luaL_pushresult(&buf);
2142 lua_setfield(L, stackbase+1, key);
2143 lua_settop(L, stackbase);
2145 #endif
2148 /*** Main function and command line invokation ***/
2150 static void moonbr_usage(int err, const char *cmd) {
2151 FILE *out;
2152 out = err ? stderr : stdout;
2153 if (!cmd) cmd = "moonbridge";
2154 fprintf(out, "Get this help message: %s {-h|--help}\n", cmd);
2155 fprintf(out, "Usage: %s \\\n", cmd);
2156 fprintf(out, " [-b|--background] \\\n");
2157 fprintf(out, " [-d|--debug] \\\n");
2158 fprintf(out, " [-f|--logfacility {DAEMON|USER|0|1|...|7}] \\\n");
2159 fprintf(out, " [-i|--logident <syslog ident> \\\n");
2160 fprintf(out, " [-l|--logfile <logfile>] \\\n");
2161 fprintf(out, " [-p|--pidfile <pidfile>] \\\n");
2162 fprintf(out, " [-s|--stats] \\\n");
2163 fprintf(out, " -- <Lua script> [<cmdline options for Lua script>]\n");
2164 exit(err);
2167 #define moonbr_usage_error() moonbr_usage(MOONBR_EXITCODE_CMDLINEERROR, argc ? argv[0] : NULL)
2169 int main(int argc, char **argv) {
2171 int daemonize = 0;
2172 int log_facility = LOG_USER;
2173 const char *log_ident = "moonbridge";
2174 const char *log_filename = NULL;
2175 const char *pid_filename = NULL;
2176 int option;
2177 struct option longopts[] = {
2178 { "background", no_argument, NULL, 'b' },
2179 { "debug", no_argument, NULL, 'd' },
2180 { "logfacility", required_argument, NULL, 'f' },
2181 { "help", no_argument, NULL, 'h' },
2182 { "logident", required_argument, NULL, 'i' },
2183 { "logfile", required_argument, NULL, 'l' },
2184 { "pidfile", required_argument, NULL, 'p' },
2185 { "stats", no_argument, NULL, 's' }
2186 };
2187 while ((option = getopt_long(argc, argv, "bdf:hi:l:p:s", longopts, NULL)) != -1) {
2188 switch (option) {
2189 case 'b':
2190 daemonize = 1;
2191 break;
2192 case 'd':
2193 moonbr_debug = 1;
2194 moonbr_stat = 1;
2195 break;
2196 case 'f':
2197 if (!strcmp(optarg, "DAEMON")) {
2198 log_facility = LOG_DAEMON;
2199 } else if (!strcmp(optarg, "USER")) {
2200 log_facility = LOG_USER;
2201 } else if (!strcmp(optarg, "0")) {
2202 log_facility = LOG_LOCAL0;
2203 } else if (!strcmp(optarg, "1")) {
2204 log_facility = LOG_LOCAL1;
2205 } else if (!strcmp(optarg, "2")) {
2206 log_facility = LOG_LOCAL2;
2207 } else if (!strcmp(optarg, "3")) {
2208 log_facility = LOG_LOCAL3;
2209 } else if (!strcmp(optarg, "4")) {
2210 log_facility = LOG_LOCAL4;
2211 } else if (!strcmp(optarg, "5")) {
2212 log_facility = LOG_LOCAL5;
2213 } else if (!strcmp(optarg, "6")) {
2214 log_facility = LOG_LOCAL6;
2215 } else if (!strcmp(optarg, "7")) {
2216 log_facility = LOG_LOCAL7;
2217 } else {
2218 moonbr_usage_error();
2220 moonbr_use_syslog = 1;
2221 break;
2222 case 'h':
2223 moonbr_usage(MOONBR_EXITCODE_GRACEFUL, argv[0]);
2224 break;
2225 case 'i':
2226 log_ident = optarg;
2227 moonbr_use_syslog = 1;
2228 break;
2229 case 'l':
2230 log_filename = optarg;
2231 break;
2232 case 'p':
2233 pid_filename = optarg;
2234 break;
2235 case 's':
2236 moonbr_stat = 1;
2237 break;
2238 default:
2239 moonbr_usage_error();
2242 if (argc - optind < 1) moonbr_usage_error();
2243 if (pid_filename) {
2244 pid_t otherpid;
2245 while ((moonbr_pidfh = pidfile_open(pid_filename, 0644, &otherpid)) == NULL) {
2246 if (errno == EEXIST) {
2247 if (otherpid == -1) {
2248 fprintf(stderr, "PID file \"%s\" is already locked\n", pid_filename);
2249 } else {
2250 fprintf(stderr, "PID file \"%s\" is already locked by process with PID: %i\n", pid_filename, (int)otherpid);
2252 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2253 } else if (errno != EINTR) {
2254 fprintf(stderr, "Could not write PID file \"%s\": %s\n", pid_filename, strerror(errno));
2255 exit(MOONBR_EXITCODE_STARTUPERROR);
2259 if (log_filename) {
2260 int logfd;
2261 while (
2262 ( logfd = flopen(
2263 log_filename,
2264 O_WRONLY|O_NONBLOCK|O_CREAT|O_APPEND|O_CLOEXEC,
2265 0640
2267 ) < 0
2268 ) {
2269 if (errno == EWOULDBLOCK) {
2270 fprintf(stderr, "Logfile \"%s\" is locked\n", log_filename);
2271 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2272 } else if (errno != EINTR) {
2273 fprintf(stderr, "Could not open logfile \"%s\": %s\n", log_filename, strerror(errno));
2274 exit(MOONBR_EXITCODE_STARTUPERROR);
2277 moonbr_logfile = fdopen(logfd, "a");
2278 if (!moonbr_logfile) {
2279 fprintf(stderr, "Could not open write stream to logfile \"%s\": %s\n", log_filename, strerror(errno));
2280 exit(MOONBR_EXITCODE_STARTUPERROR);
2283 if (daemonize == 0 && !moonbr_logfile) moonbr_logfile = stderr;
2284 if (moonbr_logfile) setlinebuf(moonbr_logfile);
2285 else moonbr_use_syslog = 1;
2286 if (moonbr_use_syslog) openlog(log_ident, LOG_NDELAY | LOG_PID, log_facility);
2287 if (daemonize) {
2288 if (daemon(1, 0)) {
2289 moonbr_log(LOG_ERR, "Could not daemonize moonbridge process");
2290 moonbr_terminate_error();
2294 moonbr_log(LOG_NOTICE, "Starting moonbridge server");
2295 if (moonbr_pidfh && pidfile_write(moonbr_pidfh)) {
2296 moonbr_log(LOG_ERR, "Could not write pidfile (after locking)");
2299 lua_State *L;
2300 L = lua_newstate(moonbr_alloc, NULL);
2301 if (!L) {
2302 moonbr_log(LOG_CRIT, "Could not initialize Lua state");
2303 moonbr_terminate_error();
2305 lua_atpanic(L, moonbr_lua_panic);
2306 lua_pushliteral(L, MOONBR_VERSION_STRING);
2307 lua_setglobal(L, "_MOONBRIDGE_VERSION");
2308 luaL_openlibs(L);
2309 luaL_requiref(L, "moonbridge_io", luaopen_moonbridge_io, 1);
2310 lua_pop(L, 1);
2311 #ifdef MOONBR_LUA_PATH
2312 moonbr_modify_path(L, "path", MOONBR_LUA_PATH);
2313 #endif
2314 #ifdef MOONBR_LUA_CPATH
2315 moonbr_modify_path(L, "cpath", MOONBR_LUA_CPATH);
2316 #endif
2317 if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
2318 moonbr_log(LOG_CRIT, "Lua metatable LUA_FILEHANDLE does not exist");
2319 moonbr_terminate_error();
2321 lua_getfield(L, -1, "__index");
2322 lua_pushcfunction(L, moonbr_readuntil);
2323 lua_setfield(L, -2, "readuntil");
2324 lua_pop(L, 2);
2325 lua_pushcfunction(L, moonbr_timeout);
2326 lua_setglobal(L, "timeout");
2327 lua_pushcfunction(L, moonbr_listen);
2328 lua_setglobal(L, "listen");
2329 lua_pushcfunction(L, moonbr_addtraceback); /* on stack position 1 */
2330 moonbr_log(LOG_INFO, "Loading \"%s\"", argv[optind]);
2331 if (luaL_loadfile(L, argv[optind])) {
2332 moonbr_log(LOG_ERR, "Error while loading \"%s\": %s", argv[optind], lua_tostring(L, -1));
2333 moonbr_terminate_error();
2335 { int i; for (i=optind+1; i<argc; i++) lua_pushstring(L, argv[i]); }
2336 if (lua_pcall(L, argc-(optind+1), 0, 1)) {
2337 moonbr_log(LOG_ERR, "Error while executing \"%s\": %s", argv[optind], lua_tostring(L, -1));
2338 moonbr_terminate_error();
2340 if (!moonbr_first_pool) {
2341 moonbr_log(LOG_WARNING, "No listener initialized.");
2342 moonbr_terminate_error();
2344 lua_getglobal(L, "listen");
2345 lua_pushcfunction(L, moonbr_listen);
2346 if (lua_compare(L, -2, -1, LUA_OPEQ)) {
2347 lua_pushnil(L);
2348 lua_setglobal(L, "listen");
2350 lua_settop(L, 1);
2351 lua_gc(L, LUA_GCCOLLECT, 0); // collect garbage before forking later
2352 moonbr_run(L);
2354 return 0;

Impressum / About Us