moonbridge

view moonbridge.c @ 131:89fc89b22e28

Example application uses new "main" protocol; Handler for "main" gets table with "main" set to true
author jbe
date Wed Apr 15 00:59:52 2015 +0200 (2015-04-15)
parents ee98e12427a9
children 11269ec33cd7
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_MAIN 1
127 #define MOONBR_PROTO_INTERVAL 2
128 #define MOONBR_PROTO_LOCAL 3
129 #define MOONBR_PROTO_TCP 4
131 /* Data structure for a pool's listener that can accept incoming connections */
132 struct moonbr_listener {
133 struct moonbr_pool *pool;
134 struct moonbr_listener *prev_listener; /* previous idle or(!) connected listener */
135 struct moonbr_listener *next_listener; /* next idle or(!) connected listener */
136 int proto;
137 union {
138 struct {
139 char *name; /* name of interval passed to 'connect' function as 'interval' field in table */
140 int strict; /* nonzero = runtime of 'connect' function does not delay interval */
141 struct timeval delay; /* interval between invocations of 'connect' function */
142 struct timeval wakeup; /* point in time of next invocation */
143 } interval;
144 struct {
145 union {
146 struct sockaddr addr_abstract;
147 struct sockaddr_un addr_un;
148 struct sockaddr_in addr_in;
149 struct sockaddr_in6 addr_in6;
150 } addr;
151 socklen_t addrlen;
152 } socket;
153 } type_specific;
154 union {
155 struct {
156 char ip[INET6_ADDRSTRLEN]; /* IP to listen on */
157 int port; /* port number to listen on (in host endianess) */
158 } tcp;
159 } proto_specific;
160 int listenfd; /* -1 = none */
161 int pollidx; /* -1 = none */
162 };
164 /* Data structure for a child process that is handling incoming connections */
165 struct moonbr_worker {
166 struct moonbr_pool *pool;
167 struct moonbr_worker *prev_worker;
168 struct moonbr_worker *next_worker;
169 struct moonbr_worker *prev_idle_worker;
170 struct moonbr_worker *next_idle_worker;
171 int main; /* nonzero = terminate Moonbridge when this worker dies */
172 int idle; /* nonzero = waiting for command from parent process */
173 int assigned; /* nonzero = currently handling a connection */
174 pid_t pid;
175 int controlfd; /* socket to send/receive control message to/from child process */
176 int errorfd; /* socket to receive error output from child process' stderr */
177 char *errorlinebuf; /* optional buffer for collecting stderr data from child process */
178 int errorlinelen; /* number of bytes stored in 'errorlinebuf' */
179 int errorlineovf; /* nonzero = line length overflow */
180 struct timeval idle_expiration; /* point in time until child process may stay in idle state */
181 struct moonbr_listener *restart_interval_listener; /* set while interval listener is assigned */
182 };
184 /* Data structure for a pool of workers and listeners */
185 struct moonbr_pool {
186 int poolnum; /* number of pool for log output */
187 struct moonbr_pool *next_pool; /* next entry in linked list starting with 'moonbr_first_pool' */
188 struct moonbr_worker *first_worker; /* first worker of pool */
189 struct moonbr_worker *last_worker; /* last worker of pool */
190 struct moonbr_worker *first_idle_worker; /* first idle worker of pool */
191 struct moonbr_worker *last_idle_worker; /* last idle worker of pool */
192 int idle_worker_count;
193 int unassigned_worker_count;
194 int total_worker_count;
195 int worker_count_stat; /* only needed for statistics */
196 int pre_fork; /* desired minimum number of unassigned workers */
197 int min_fork; /* desired minimum number of workers in total */
198 int max_fork; /* maximum number of workers */
199 struct timeval fork_delay; /* delay after each fork() until a fork may happen again */
200 struct timeval fork_wakeup; /* point in time when a fork may happen again (unless a worker terminates before) */
201 struct timeval fork_error_delay; /* delay between fork()s when an error during fork or preparation occurred */
202 struct timeval fork_error_wakeup; /* point in time when fork may happen again if an error in preparation occurred */
203 int use_fork_error_wakeup; /* nonzero = error in preparation occured; gets reset on next fork */
204 struct timeval exit_delay; /* delay for terminating excessive workers (unassigned_worker_count > pre_fork) */
205 struct timeval exit_wakeup; /* point in time when terminating an excessive worker */
206 struct timeval idle_timeout; /* delay before an idle worker is terminated */
207 size_t memory_limit; /* maximum bytes of memory that the Lua machine may allocate */
208 int listener_count; /* total number of listeners of pool (and size of 'listener' array at end of this struct) */
209 struct moonbr_listener *first_idle_listener; /* first listener that is idle (i.e. has no waiting connection) */
210 struct moonbr_listener *last_idle_listener; /* last listener that is idle (i.e. has no waiting connection) */
211 struct moonbr_listener *first_connected_listener; /* first listener that has a pending connection */
212 struct moonbr_listener *last_connected_listener; /* last listener that has a pending connection */
213 struct moonbr_listener listener[1]; /* static array of variable(!) size to contain 'listener' structures */
214 };
216 /* Enum for 'channel' field of struct moonbr_poll_worker */
217 #define MOONBR_POLL_WORKER_CONTROLCHANNEL 1
218 #define MOONBR_POLL_WORKER_ERRORCHANNEL 2
220 /* Structure to refer from 'moonbr_poll_worker_fds' entry to worker structure */
221 struct moonbr_poll_worker {
222 struct moonbr_worker *worker;
223 int channel; /* field indicating whether file descriptor is 'controlfd' or 'errorfd' */
224 };
226 /* Variable indicating that clean shutdown was requested */
227 static int moonbr_shutdown_in_progress = 0;
230 /*** Macros for Lua registry ***/
232 /* Lightuserdata keys for Lua registry to store 'prepare', 'connect', and 'finish' functions */
233 #define moonbr_luakey_prepare_func(pool) ((void *)(intptr_t)(pool) + 0)
234 #define moonbr_luakey_connect_func(pool) ((void *)(intptr_t)(pool) + 1)
235 #define moonbr_luakey_finish_func(pool) ((void *)(intptr_t)(pool) + 2)
238 /*** Global variables ***/
240 /* State of process execution */
241 static int moonbr_pstate = MOONBR_PSTATE_STARTUP;
243 /* Process ID of the main process */
244 static pid_t moonbr_masterpid;
246 /* Condition variables set by the signal handler */
247 static volatile sig_atomic_t moonbr_cond_poll = 0;
248 static volatile sig_atomic_t moonbr_cond_terminate = 0;
249 static volatile sig_atomic_t moonbr_cond_interrupt = 0;
250 static volatile sig_atomic_t moonbr_cond_child = 0;
252 /* Socket pair to denote signal delivery when signal handler was called just before poll() */
253 static int moonbr_poll_signalfds[2];
254 #define moonbr_poll_signalfd_read moonbr_poll_signalfds[0]
255 #define moonbr_poll_signalfd_write moonbr_poll_signalfds[1]
257 /* Global variables for pidfile and logging */
258 static struct pidfh *moonbr_pidfh = NULL;
259 static FILE *moonbr_logfile = NULL;
260 static int moonbr_use_syslog = 0;
262 /* First and last entry of linked list of all created pools during initialization */
263 static struct moonbr_pool *moonbr_first_pool = NULL;
264 static struct moonbr_pool *moonbr_last_pool = NULL;
266 /* Total count of pools */
267 static int moonbr_pool_count = 0;
269 /* Set to a nonzero value if dynamic part of 'moonbr_poll_fds' ('moonbr_poll_worker_fds') needs an update */
270 static int moonbr_poll_refresh_needed = 0;
272 /* Array passed to poll(), consisting of static part and dynamic part ('moonbr_poll_worker_fds') */
273 static struct pollfd *moonbr_poll_fds = NULL; /* the array */
274 static int moonbr_poll_fds_bufsize = 0; /* memory allocated for this number of elements */
275 static int moonbr_poll_fds_count = 0; /* total number of elements */
276 static int moonbr_poll_fds_static_count; /* number of elements in static part */
278 /* Dynamic part of 'moonbr_poll_fds' array */
279 #define moonbr_poll_worker_fds (moonbr_poll_fds+moonbr_poll_fds_static_count)
281 /* Additional information for dynamic part of 'moonbr_poll_fds' array */
282 struct moonbr_poll_worker *moonbr_poll_workers; /* the array */
283 static int moonbr_poll_workers_bufsize = 0; /* memory allocated for this number of elements */
284 static int moonbr_poll_worker_count = 0; /* number of elements in array */
286 /* Variable set to nonzero value to disallow further calls of 'listen' function */
287 static int moonbr_booted = 0;
289 /* Verbosity settings */
290 static int moonbr_debug = 0;
291 static int moonbr_stat = 0;
293 /* Memory consumption by Lua machine */
294 static size_t moonbr_memory_usage = 0;
295 static size_t moonbr_memory_limit = 0;
298 /*** Functions for signal handling ***/
300 /* Signal handler for master and child processes */
301 static void moonbr_signal(int sig) {
302 if (getpid() == moonbr_masterpid) {
303 /* master process */
304 switch (sig) {
305 case SIGHUP:
306 case SIGINT:
307 /* fast shutdown requested */
308 moonbr_cond_interrupt = 1;
309 break;
310 case SIGTERM:
311 /* clean shutdown requested */
312 moonbr_cond_terminate = 1;
313 break;
314 case SIGCHLD:
315 /* child process terminated */
316 moonbr_cond_child = 1;
317 break;
318 }
319 if (moonbr_cond_poll) {
320 /* avoid race condition if signal handler is invoked right before poll() */
321 char buf[1] = {0};
322 write(moonbr_poll_signalfd_write, buf, 1);
323 }
324 } else {
325 /* child process forwards certain signals to parent process */
326 switch (sig) {
327 case SIGHUP:
328 case SIGINT:
329 case SIGTERM:
330 kill(moonbr_masterpid, sig);
331 }
332 }
333 }
335 /* Initialize signal handling */
336 static void moonbr_signal_init(){
337 moonbr_masterpid = getpid();
338 signal(SIGHUP, moonbr_signal);
339 signal(SIGINT, moonbr_signal);
340 signal(SIGTERM, moonbr_signal);
341 signal(SIGCHLD, moonbr_signal);
342 }
345 /*** Functions for logging in master process ***/
347 /* Logs a pre-formatted message with given syslog() priority */
348 static void moonbr_log_msg(int priority, const char *msg) {
349 if (moonbr_logfile) {
350 /* logging to logfile desired (timestamp is prepended in that case) */
351 time_t now_time = 0;
352 struct tm now_tmstruct;
353 char timestr[MOONBR_LOG_MAXTIMELEN+1];
354 time(&now_time);
355 localtime_r(&now_time, &now_tmstruct);
356 if (!strftime(
357 timestr, MOONBR_LOG_MAXTIMELEN+1, "%Y-%m-%d %H:%M:%S %Z: ", &now_tmstruct
358 )) timestr[0] = 0;
359 fprintf(moonbr_logfile, "%s%s\n", timestr, msg);
360 }
361 if (moonbr_use_syslog) {
362 /* logging through syslog desired */
363 syslog(priority, "%s", msg);
364 }
365 }
367 /* Formats a message via vsnprintf() and logs it with given syslog() priority */
368 static void moonbr_log(int priority, const char *message, ...) {
369 char msgbuf[MOONBR_LOG_MAXMSGLEN+1]; /* buffer of static size to store formatted message */
370 int msglen; /* length of full message (may exceed MOONBR_LOG_MAXMSGLEN) */
371 {
372 /* pass variable arguments to vsnprintf() to format message */
373 va_list ap;
374 va_start(ap, message);
375 msglen = vsnprintf(msgbuf, MOONBR_LOG_MAXMSGLEN+1, message, ap);
376 va_end(ap);
377 }
378 {
379 /* split and log message line by line */
380 char *line = msgbuf;
381 while (1) {
382 char *endptr = strchr(line, '\n');
383 if (endptr) {
384 /* terminate string where newline character is found */
385 *endptr = 0;
386 } else if (line != msgbuf && msglen > MOONBR_LOG_MAXMSGLEN) {
387 /* break if line is incomplete and not the first line */
388 break;
389 }
390 moonbr_log_msg(priority, line);
391 if (!endptr) break; /* break if end of formatted message is reached */
392 line = endptr+1; /* otherwise continue with remaining message */
393 }
394 }
395 if (msglen > MOONBR_LOG_MAXMSGLEN) {
396 /* print warning if message was truncated */
397 moonbr_log_msg(priority, "Previous log message has been truncated due to excessive length");
398 }
399 }
402 /*** Termination function ***/
404 /* Kill all child processes, remove PID file (if existent), and exit master process with given exitcode */
405 static void moonbr_terminate(int exitcode) {
406 {
407 struct moonbr_pool *pool;
408 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
409 {
410 struct moonbr_worker *worker;
411 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
412 moonbr_log(LOG_INFO, "Sending SIGKILL to child with PID %i", (int)worker->pid);
413 if (kill(worker->pid, SIGKILL)) {
414 moonbr_log(LOG_ERR, "Error while killing child process: %s", strerror(errno));
415 }
416 }
417 }
418 {
419 int i;
420 for (i=0; i<pool->listener_count; i++) {
421 struct moonbr_listener *listener = &pool->listener[i];
422 if (listener->proto == MOONBR_PROTO_LOCAL) {
423 moonbr_log(LOG_INFO, "Unlinking local socket \"%s\"", listener->type_specific.socket.addr.addr_un.sun_path);
424 if (unlink(listener->type_specific.socket.addr.addr_un.sun_path)) {
425 moonbr_log(LOG_ERR, "Error while unlinking local socket: %s", strerror(errno));
426 }
427 }
428 }
429 }
430 }
431 }
432 moonbr_log(exitcode ? LOG_ERR : LOG_NOTICE, "Terminating with exit code %i", exitcode);
433 if (moonbr_pidfh && pidfile_remove(moonbr_pidfh)) {
434 moonbr_log(LOG_ERR, "Error while removing PID file: %s", strerror(errno));
435 }
436 exit(exitcode);
437 }
439 /* Terminate with either MOONBR_EXITCODE_STARTUPERROR or MOONBR_EXITCODE_RUNTIMEERROR */
440 #define moonbr_terminate_error() \
441 moonbr_terminate( \
442 moonbr_pstate == MOONBR_PSTATE_STARTUP ? \
443 MOONBR_EXITCODE_STARTUPERROR : \
444 MOONBR_EXITCODE_RUNTIMEERROR \
445 )
448 /*** Helper functions ***/
450 /* Fills a 'struct timeval' structure with the current time (using CLOCK_MONOTONIC) */
451 static void moonbr_now(struct timeval *now) {
452 struct timespec ts = {0, };
453 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
454 moonbr_log(LOG_CRIT, "Error in clock_gettime() call: %s", strerror(errno));
455 moonbr_terminate_error();
456 }
457 *now = (struct timeval){ .tv_sec = ts.tv_sec, .tv_usec = ts.tv_nsec / 1000 };
458 }
460 /* Formats a 'struct timeval' value (not thread-safe) */
461 static char *moonbr_format_timeval(struct timeval *t) {
462 static char buf[32];
463 snprintf(buf, 32, "%ji.%06ji seconds", (intmax_t)t->tv_sec, (intmax_t)t->tv_usec);
464 return buf;
465 }
468 /*** Functions for pool creation and startup ***/
470 /* Creates a 'struct moonbr_pool' structure with a given number of listeners */
471 static struct moonbr_pool *moonbr_create_pool(int listener_count) {
472 struct moonbr_pool *pool;
473 pool = calloc(1,
474 sizeof(struct moonbr_pool) + /* size of 'struct moonbr_pool' with one listener */
475 (listener_count-1) * sizeof(struct moonbr_listener) /* size of extra listeners */
476 );
477 if (!pool) {
478 moonbr_log(LOG_CRIT, "Memory allocation error");
479 moonbr_terminate_error();
480 }
481 pool->listener_count = listener_count;
482 {
483 /* initialization of listeners */
484 int i;
485 for (i=0; i<listener_count; i++) {
486 struct moonbr_listener *listener = &pool->listener[i];
487 listener->pool = pool;
488 listener->listenfd = -1;
489 listener->pollidx = -1;
490 }
491 }
492 return pool;
493 }
495 /* Destroys a 'struct moonbr_pool' structure before it has been started */
496 static void moonbr_destroy_pool(struct moonbr_pool *pool) {
497 int i;
498 for (i=0; i<pool->listener_count; i++) {
499 struct moonbr_listener *listener = &pool->listener[i];
500 if (
501 listener->proto == MOONBR_PROTO_INTERVAL &&
502 listener->type_specific.interval.name
503 ) {
504 free(listener->type_specific.interval.name);
505 }
506 }
507 free(pool);
508 }
510 /* Starts a all listeners in a pool */
511 static int moonbr_start_pool(struct moonbr_pool *pool) {
512 moonbr_log(LOG_INFO, "Creating pool", pool->poolnum);
513 {
514 int i;
515 for (i=0; i<pool->listener_count; i++) {
516 struct moonbr_listener *listener = &pool->listener[i];
517 switch (listener->proto) {
518 case MOONBR_PROTO_MAIN:
519 /* nothing to do here: starting main thread is performed in moonbr_run() function */
520 moonbr_log(LOG_INFO, "Adding main thread");
521 break;
522 case MOONBR_PROTO_INTERVAL:
523 /* nothing to do here: starting intervals is performed in moonbr_run() function */
524 if (!listener->type_specific.interval.name) {
525 moonbr_log(LOG_INFO, "Adding unnamed interval listener");
526 } else {
527 moonbr_log(LOG_INFO, "Adding interval listener \"%s\"", listener->type_specific.interval.name);
528 }
529 break;
530 case MOONBR_PROTO_LOCAL:
531 moonbr_log(LOG_INFO, "Adding local socket listener for path \"%s\"", listener->type_specific.socket.addr.addr_un.sun_path);
532 listener->listenfd = socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
533 if (listener->listenfd == -1) goto moonbr_start_pool_error;
534 if (!unlink(listener->type_specific.socket.addr.addr_un.sun_path)) {
535 moonbr_log(LOG_WARNING, "Unlinked named socket \"%s\" prior to listening", listener->type_specific.socket.addr.addr_un.sun_path);
536 } else {
537 if (errno != ENOENT) {
538 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));
539 }
540 }
541 if (
542 bind(listener->listenfd, &listener->type_specific.socket.addr.addr_abstract, listener->type_specific.socket.addrlen)
543 ) goto moonbr_start_pool_error;
544 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
545 break;
546 case MOONBR_PROTO_TCP:
547 moonbr_log(LOG_INFO, "Adding TCP listener on interface \"%s\", port %i", listener->proto_specific.tcp.ip, listener->proto_specific.tcp.port);
548 listener->listenfd = socket(listener->type_specific.socket.addr.addr_abstract.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
549 if (listener->listenfd == -1) goto moonbr_start_pool_error;
550 {
551 /* avoid "Address already in use" error when restarting service */
552 static const int reuseval = 1;
553 if (setsockopt(
554 listener->listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval)
555 )) goto moonbr_start_pool_error;
556 }
557 {
558 /* default to send TCP RST when process terminates unexpectedly */
559 static const struct linger lingerval = {
560 .l_onoff = 1,
561 .l_linger = 0
562 };
563 if (setsockopt(
564 listener->listenfd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval)
565 )) goto moonbr_start_pool_error;
566 }
567 if (
568 bind(listener->listenfd, &listener->type_specific.socket.addr.addr_abstract, listener->type_specific.socket.addrlen)
569 ) goto moonbr_start_pool_error;
570 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
571 break;
572 default:
573 moonbr_log(LOG_CRIT, "Internal error (should not happen): Unexpected value in listener.proto field");
574 moonbr_terminate_error();
575 }
576 }
577 goto moonbr_start_pool_ok;
578 moonbr_start_pool_error:
579 {
580 int j = i;
581 int errno2 = errno;
582 for (; i>=0; i--) {
583 struct moonbr_listener *listener = &pool->listener[i];
584 if (listener->listenfd != -1) close(listener->listenfd);
585 }
586 errno = errno2;
587 return j;
588 }
589 }
590 moonbr_start_pool_ok:
591 pool->poolnum = ++moonbr_pool_count;
592 moonbr_log(LOG_INFO, "Pool #%i created", pool->poolnum);
593 if (moonbr_last_pool) moonbr_last_pool->next_pool = pool;
594 else moonbr_first_pool = pool;
595 moonbr_last_pool = pool;
596 return -1;
597 }
600 /*** Function to send data and a file descriptor to child process */
602 /* Sends control message of one bye plus optional file descriptor plus optional pointer to child process */
603 static void moonbr_send_control_message(struct moonbr_worker *worker, char status, int fd, void *ptr) {
604 {
605 struct iovec iovector = { .iov_base = &status, .iov_len = 1 }; /* carrying status byte */
606 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to transfer file descriptor */
607 struct msghdr message = { .msg_iov = &iovector, .msg_iovlen = 1 }; /* data structure passed to sendmsg() call */
608 if (moonbr_debug) {
609 if (fd == -1) {
610 moonbr_log(LOG_DEBUG, "Sending control message \"%c\" to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
611 } else {
612 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);
613 }
614 }
615 if (fd != -1) {
616 /* attach control message with file descriptor */
617 message.msg_control = control_message_buffer;
618 message.msg_controllen = CMSG_SPACE(sizeof(int));
619 {
620 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
621 control_message->cmsg_level = SOL_SOCKET;
622 control_message->cmsg_type = SCM_RIGHTS;
623 control_message->cmsg_len = CMSG_LEN(sizeof(int));
624 memcpy(CMSG_DATA(control_message), &fd, sizeof(int));
625 }
626 }
627 while (sendmsg(worker->controlfd, &message, MSG_NOSIGNAL) < 0) {
628 if (errno == EPIPE) {
629 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));
630 return; /* do not close socket; socket is closed when reading from it */
631 }
632 if (errno != EINTR) {
633 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));
634 moonbr_terminate_error();
635 }
636 }
637 }
638 if (ptr) {
639 char buf[sizeof(void *)];
640 char *pos = buf;
641 int len = sizeof(void *);
642 ssize_t written;
643 if (moonbr_debug) {
644 moonbr_log(LOG_DEBUG, "Sending memory pointer to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
645 }
646 memcpy(buf, &ptr, sizeof(void *));
647 while (len) {
648 written = send(worker->controlfd, pos, len, MSG_NOSIGNAL);
649 if (written > 0) {
650 pos += written;
651 len -= written;
652 } else if (errno == EPIPE) {
653 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));
654 return; /* do not close socket; socket is closed when reading from it */
655 } else if (errno != EINTR) {
656 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));
657 moonbr_terminate_error();
658 }
659 }
660 }
661 }
664 /*** Functions running in child process ***/
666 /* Logs an error in child process */
667 static void moonbr_child_log(const char *message) {
668 fprintf(stderr, "%s\n", message);
669 }
671 /* Logs a fatal error in child process and terminates process with error status */
672 static void moonbr_child_log_fatal(const char *message) {
673 moonbr_child_log(message);
674 exit(1);
675 }
677 /* Logs an error in child process while appending error string for global errno variable */
678 static void moonbr_child_log_errno(const char *message) {
679 char errmsg[MOONBR_MAXSTRERRORLEN];
680 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
681 fprintf(stderr, "%s: %s\n", message, errmsg);
682 }
684 /* Logs a fatal error in child process while appending error string for errno and terminating process */
685 static void moonbr_child_log_errno_fatal(const char *message) {
686 moonbr_child_log_errno(message);
687 exit(1);
688 }
690 /* Receives a control message consisting of one character plus an optional file descriptor from parent process */
691 static void moonbr_child_receive_control_message(int socketfd, char *status, int *fd) {
692 struct iovec iovector = { .iov_base = status, .iov_len = 1 }; /* reference to status byte variable */
693 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to receive file descriptor */
694 struct msghdr message = { /* data structure passed to recvmsg() call */
695 .msg_iov = &iovector,
696 .msg_iovlen = 1,
697 .msg_control = control_message_buffer,
698 .msg_controllen = CMSG_SPACE(sizeof(int))
699 };
700 {
701 int received;
702 while ((received = recvmsg(socketfd, &message, MSG_CMSG_CLOEXEC)) < 0) {
703 if (errno != EINTR) {
704 moonbr_child_log_errno_fatal("Error while trying to receive connection socket from parent process");
705 }
706 }
707 if (!received) {
708 moonbr_child_log_fatal("Unexpected EOF while trying to receive connection socket from parent process");
709 }
710 }
711 {
712 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
713 if (control_message) {
714 if (control_message->cmsg_level != SOL_SOCKET) {
715 moonbr_child_log_fatal("Received control message with cmsg_level not equal to SOL_SOCKET");
716 }
717 if (control_message->cmsg_type != SCM_RIGHTS) {
718 moonbr_child_log_fatal("Received control message with cmsg_type not equal to SCM_RIGHTS");
719 }
720 memcpy(fd, CMSG_DATA(control_message), sizeof(int));
721 } else {
722 *fd = -1;
723 }
724 }
725 }
727 /* Receives a pointer from parent process */
728 static void *moonbr_child_receive_pointer(int socketfd) {
729 char buf[sizeof(void *)];
730 char *pos = buf;
731 int len = sizeof(void *);
732 ssize_t bytes_read;
733 while (len) {
734 bytes_read = recv(socketfd, pos, len, 0);
735 if (bytes_read > 0) {
736 pos += bytes_read;
737 len -= bytes_read;
738 } else if (!bytes_read) {
739 moonbr_child_log_fatal("Unexpected EOF while trying to receive memory pointer from parent process");
740 } else if (errno != EINTR) {
741 moonbr_child_log_errno_fatal("Error while trying to receive memory pointer from parent process");
742 }
743 }
744 {
745 void *ptr; /* avoid breaking strict-aliasing rules */
746 memcpy(&ptr, buf, sizeof(void *));
747 return ptr;
748 }
749 }
751 /* Main function of child process to be called after fork() and file descriptor rearrangement */
752 void moonbr_child_run(struct moonbr_pool *pool, lua_State *L) {
753 char controlmsg;
754 int fd;
755 struct itimerval notimer = { { 0, }, { 0, } };
756 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
757 if (lua_isnil(L, -1)) lua_pop(L, 1);
758 else if (lua_pcall(L, 0, 0, 1)) {
759 fprintf(stderr, "Error in \"prepare\" function: %s\n", lua_tostring(L, -1));
760 exit(1);
761 }
762 while (1) {
763 struct moonbr_listener *listener;
764 if (setitimer(ITIMER_REAL, &notimer, NULL)) {
765 moonbr_child_log_errno_fatal("Could not reset ITIMER_REAL via setitimer()");
766 }
767 controlmsg = MOONBR_STATUS_IDLE;
768 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
769 moonbr_child_log_errno_fatal("Error while sending ready message to parent process");
770 }
771 moonbr_child_receive_control_message(MOONBR_FD_CONTROL, &controlmsg, &fd);
772 if (!(
773 (controlmsg == MOONBR_COMMAND_TERMINATE && fd == -1) ||
774 (controlmsg == MOONBR_COMMAND_CONNECT)
775 )) {
776 moonbr_child_log_fatal("Received illegal control message from parent process");
777 }
778 if (controlmsg == MOONBR_COMMAND_TERMINATE) break;
779 listener = moonbr_child_receive_pointer(MOONBR_FD_CONTROL);
780 if (
781 listener->proto != MOONBR_PROTO_LOCAL &&
782 listener->proto != MOONBR_PROTO_TCP &&
783 fd >= 0
784 ) {
785 moonbr_child_log_fatal("Received unexpected file descriptor from parent process");
786 } else if (
787 listener->proto != MOONBR_PROTO_MAIN &&
788 listener->proto != MOONBR_PROTO_INTERVAL &&
789 fd < 0
790 ) {
791 moonbr_child_log_fatal("Missing file descriptor from parent process");
792 }
793 if (fd >= 0) moonbr_io_pushhandle(L, fd);
794 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
795 if (fd < 0) {
796 lua_newtable(L);
797 if (listener->proto == MOONBR_PROTO_MAIN) {
798 lua_pushboolean(L, 1);
799 lua_setfield(L, -2, "main");
800 } else if (listener->proto == MOONBR_PROTO_INTERVAL) {
801 lua_pushstring(L,
802 listener->type_specific.interval.name ?
803 listener->type_specific.interval.name : ""
804 );
805 lua_setfield(L, -2, "interval");
806 }
807 } else {
808 lua_pushvalue(L, -2);
809 }
810 if (lua_pcall(L, 1, 1, 1)) {
811 fprintf(stderr, "Error in \"connect\" function: %s\n", lua_tostring(L, -1));
812 exit(1);
813 }
814 if (fd >= 0) moonbr_io_closehandle(L, -2, 0); /* attemt clean close */
815 if (lua_type(L, -1) != LUA_TBOOLEAN || !lua_toboolean(L, -1)) break;
816 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
817 lua_settop(L, 2);
818 #else
819 lua_settop(L, 1);
820 #endif
821 }
822 controlmsg = MOONBR_STATUS_GOODBYE;
823 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
824 moonbr_child_log_errno_fatal("Error while sending goodbye message to parent process");
825 }
826 if (close(MOONBR_FD_CONTROL) && errno != EINTR) {
827 moonbr_child_log_errno("Error while closing control socket");
828 }
829 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
830 if (lua_isnil(L, -1)) lua_pop(L, 1);
831 else if (lua_pcall(L, 0, 0, 1)) {
832 fprintf(stderr, "Error in \"finish\" function: %s\n", lua_tostring(L, -1));
833 exit(1);
834 }
835 lua_close(L);
836 exit(0);
837 }
840 /*** Functions to spawn child process ***/
842 /* Helper function to send an error message to a file descriptor (not needing a file stream) */
843 static void moonbr_child_emergency_print(int fd, char *message) {
844 size_t len = strlen(message);
845 ssize_t written;
846 while (len) {
847 written = write(fd, message, len);
848 if (written > 0) {
849 message += written;
850 len -= written;
851 } else {
852 if (written != -1 || errno != EINTR) break;
853 }
854 }
855 }
857 /* Helper function to send an error message plus a text for errno to a file descriptor and terminate the process */
858 static void moonbr_child_emergency_error(int fd, char *message) {
859 int errno2 = errno;
860 moonbr_child_emergency_print(fd, message);
861 moonbr_child_emergency_print(fd, ": ");
862 moonbr_child_emergency_print(fd, strerror(errno2));
863 moonbr_child_emergency_print(fd, "\n");
864 exit(1);
865 }
867 /* Creates a child process and (in case of success) registers it in the 'struct moonbr_pool' structure */
868 static int moonbr_create_worker(struct moonbr_pool *pool, lua_State *L) {
869 struct moonbr_worker *worker;
870 worker = calloc(1, sizeof(struct moonbr_worker));
871 if (!worker) {
872 moonbr_log(LOG_CRIT, "Memory allocation error");
873 return -1;
874 }
875 worker->pool = pool;
876 {
877 int controlfds[2];
878 int errorfds[2];
879 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, controlfds)) {
880 moonbr_log(LOG_ERR, "Could not create control socket pair for communcation with child process: %s", strerror(errno));
881 free(worker);
882 return -1;
883 }
884 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, errorfds)) {
885 moonbr_log(LOG_ERR, "Could not create socket pair to redirect stderr of child process: %s", strerror(errno));
886 close(controlfds[0]);
887 close(controlfds[1]);
888 free(worker);
889 return -1;
890 }
891 if (moonbr_logfile && fflush(moonbr_logfile)) {
892 moonbr_log(LOG_CRIT, "Could not flush log file prior to forking: %s", strerror(errno));
893 moonbr_terminate_error();
894 }
895 worker->pid = fork();
896 if (worker->pid == -1) {
897 moonbr_log(LOG_ERR, "Could not fork: %s", strerror(errno));
898 close(controlfds[0]);
899 close(controlfds[1]);
900 close(errorfds[0]);
901 close(errorfds[1]);
902 free(worker);
903 return -1;
904 } else if (!worker->pid) {
905 moonbr_pstate = MOONBR_PSTATE_FORKED;
906 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
907 lua_pushliteral(L, "Failed to pass error message due to bug in Lua panic handler (hint: not enough memory?)");
908 #endif
909 moonbr_memory_limit = pool->memory_limit;
910 if (moonbr_pidfh && pidfile_close(moonbr_pidfh)) {
911 moonbr_child_emergency_error(errorfds[1], "Could not close PID file in forked child process");
912 }
913 if (moonbr_logfile && moonbr_logfile != stderr && fclose(moonbr_logfile)) {
914 moonbr_child_emergency_error(errorfds[1], "Could not close log file in forked child process");
915 }
916 if (dup2(errorfds[1], MOONBR_FD_STDERR) == -1) {
917 moonbr_child_emergency_error(errorfds[1], "Could not duplicate socket to stderr file descriptor");
918 }
919 if (dup2(controlfds[1], MOONBR_FD_CONTROL) == -1) {
920 moonbr_child_emergency_error(errorfds[1], "Could not duplicate control socket");
921 }
922 closefrom(MOONBR_FD_END);
923 moonbr_child_run(pool, L);
924 }
925 if (moonbr_stat) {
926 moonbr_log(LOG_INFO, "Created new worker in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
927 }
928 worker->controlfd = controlfds[0];
929 worker->errorfd = errorfds[0];
930 if (close(controlfds[1]) && errno != EINTR) {
931 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
932 moonbr_terminate_error();
933 }
934 if (close(errorfds[1]) && errno != EINTR) {
935 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
936 moonbr_terminate_error();
937 }
938 }
939 worker->prev_worker = pool->last_worker;
940 if (worker->prev_worker) worker->prev_worker->next_worker = worker;
941 else pool->first_worker = worker;
942 pool->last_worker = worker;
943 pool->unassigned_worker_count++;
944 pool->total_worker_count++;
945 pool->worker_count_stat = 1;
946 moonbr_poll_refresh_needed = 1;
947 return 0; /* return zero only in case of success */
948 }
951 /*** Functions for queues of 'struct moonbr_listener' ***/
953 /* Appends a 'struct moonbr_listener' to the queue of idle listeners and registers it for poll() */
954 static void moonbr_add_idle_listener(struct moonbr_listener *listener) {
955 listener->prev_listener = listener->pool->last_idle_listener;
956 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
957 else listener->pool->first_idle_listener = listener;
958 listener->pool->last_idle_listener = listener;
959 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events |= POLLIN;
960 }
962 /* Removes a 'struct moonbr_listener' from the queue of idle listeners and unregisters it from poll() */
963 static void moonbr_remove_idle_listener(struct moonbr_listener *listener) {
964 if (listener->prev_listener) listener->prev_listener->next_listener = listener->next_listener;
965 else listener->pool->first_idle_listener = listener->next_listener;
966 if (listener->next_listener) listener->next_listener->prev_listener = listener->prev_listener;
967 else listener->pool->last_idle_listener = listener->prev_listener;
968 listener->prev_listener = NULL;
969 listener->next_listener = NULL;
970 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events &= ~POLLIN;
971 }
973 /* Adds a listener to the queue of connected listeners (i.e. waiting to have their incoming connection accepted) */
974 static void moonbr_add_connected_listener(struct moonbr_listener *listener) {
975 listener->prev_listener = listener->pool->last_connected_listener;
976 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
977 else listener->pool->first_connected_listener = listener;
978 listener->pool->last_connected_listener = listener;
979 }
981 /* Removes and returns the first connected listener in the queue */
982 static struct moonbr_listener *moonbr_pop_connected_listener(struct moonbr_pool *pool) {
983 struct moonbr_listener *listener = pool->first_connected_listener;
984 listener->pool->first_connected_listener = listener->next_listener;
985 if (listener->pool->first_connected_listener) listener->pool->first_connected_listener->prev_listener = NULL;
986 else listener->pool->last_connected_listener = NULL;
987 listener->next_listener = NULL;
988 return listener;
989 }
992 /*** Functions to handle polling ***/
994 /* Returns an index to a new initialized entry in moonbr_poll_fds[] */
995 int moonbr_poll_fds_nextindex() {
996 if (moonbr_poll_fds_count >= moonbr_poll_fds_bufsize) {
997 if (moonbr_poll_fds_bufsize) moonbr_poll_fds_bufsize *= 2;
998 else moonbr_poll_fds_bufsize = 1;
999 moonbr_poll_fds = realloc(
1000 moonbr_poll_fds, moonbr_poll_fds_bufsize * sizeof(struct pollfd)
1001 );
1002 if (!moonbr_poll_fds) {
1003 moonbr_log(LOG_CRIT, "Memory allocation error");
1004 moonbr_terminate_error();
1007 moonbr_poll_fds[moonbr_poll_fds_count] = (struct pollfd){0, };
1008 return moonbr_poll_fds_count++;
1011 /* Returns an index to a new initialized entry in moonbr_poll_workers[] */
1012 int moonbr_poll_workers_nextindex() {
1013 if (moonbr_poll_worker_count >= moonbr_poll_workers_bufsize) {
1014 if (moonbr_poll_workers_bufsize) moonbr_poll_workers_bufsize *= 2;
1015 else moonbr_poll_workers_bufsize = 1;
1016 moonbr_poll_workers = realloc(
1017 moonbr_poll_workers, moonbr_poll_workers_bufsize * sizeof(struct moonbr_poll_worker)
1018 );
1019 if (!moonbr_poll_workers) {
1020 moonbr_log(LOG_CRIT, "Memory allocation error");
1021 moonbr_terminate_error();
1024 moonbr_poll_workers[moonbr_poll_worker_count] = (struct moonbr_poll_worker){0, };
1025 return moonbr_poll_worker_count++;
1028 /* Queues all listeners as idle, and initializes static part of moonbr_poll_fds[], which is passed to poll() */
1029 static void moonbr_poll_init() {
1030 if (socketpair(
1031 PF_LOCAL,
1032 SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1033 0,
1034 moonbr_poll_signalfds
1035 )) {
1036 moonbr_log(LOG_CRIT, "Could not create socket pair for signal delivery during polling: %s", strerror(errno));
1037 moonbr_terminate_error();
1040 int j = moonbr_poll_fds_nextindex();
1041 struct pollfd *pollfd = &moonbr_poll_fds[j];
1042 pollfd->fd = moonbr_poll_signalfd_read;
1043 pollfd->events = POLLIN;
1046 struct moonbr_pool *pool;
1047 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1048 int i;
1049 for (i=0; i<pool->listener_count; i++) {
1050 struct moonbr_listener *listener = &pool->listener[i];
1051 if (listener->listenfd != -1) {
1052 int j = moonbr_poll_fds_nextindex();
1053 listener->pollidx = j;
1054 moonbr_poll_fds[j].fd = listener->listenfd;
1056 moonbr_add_idle_listener(listener);
1060 moonbr_poll_fds_static_count = moonbr_poll_fds_count; /* remember size of static part of array */
1063 /* Disables polling of all listeners (required for clean shutdown) */
1064 static void moonbr_poll_shutdown() {
1065 int i;
1066 for (i=1; i<moonbr_poll_fds_static_count; i++) {
1067 moonbr_poll_fds[i].fd = -1;
1071 /* (Re)builds dynamic part of moonbr_poll_fds[] array, and (re)builds moonbr_poll_workers[] array */
1072 static void moonbr_poll_refresh() {
1073 moonbr_poll_refresh_needed = 0;
1074 moonbr_poll_fds_count = moonbr_poll_fds_static_count;
1075 moonbr_poll_worker_count = 0;
1077 struct moonbr_pool *pool;
1078 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1079 struct moonbr_worker *worker;
1080 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
1081 if (worker->controlfd != -1) {
1082 int j = moonbr_poll_fds_nextindex();
1083 int k = moonbr_poll_workers_nextindex();
1084 struct pollfd *pollfd = &moonbr_poll_fds[j];
1085 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1086 pollfd->fd = worker->controlfd;
1087 pollfd->events = POLLIN;
1088 poll_worker->channel = MOONBR_POLL_WORKER_CONTROLCHANNEL;
1089 poll_worker->worker = worker;
1091 if (worker->errorfd != -1) {
1092 int j = moonbr_poll_fds_nextindex();
1093 int k = moonbr_poll_workers_nextindex();
1094 struct pollfd *pollfd = &moonbr_poll_fds[j];
1095 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1096 pollfd->fd = worker->errorfd;
1097 pollfd->events = POLLIN;
1098 poll_worker->channel = MOONBR_POLL_WORKER_ERRORCHANNEL;
1099 poll_worker->worker = worker;
1106 /* resets socket and 'revents' field of moonbr_poll_fds[] for signal delivery just before poll() is called */
1107 static void moonbr_poll_reset_signal() {
1108 ssize_t readcount;
1109 char buf[1];
1110 moonbr_poll_fds[0].revents = 0;
1111 while ((readcount = read(moonbr_poll_signalfd_read, buf, 1)) < 0) {
1112 if (errno == EAGAIN) break;
1113 if (errno != EINTR) {
1114 moonbr_log(LOG_CRIT, "Error while reading from signal delivery socket: %s", strerror(errno));
1115 moonbr_terminate_error();
1118 if (!readcount) {
1119 moonbr_log(LOG_CRIT, "Unexpected EOF when reading from signal delivery socket: %s", strerror(errno));
1120 moonbr_terminate_error();
1125 /*** Shutdown initiation ***/
1127 /* Sets global variable 'moonbr_shutdown_in_progress', closes listeners, and demands worker termination */
1128 static void moonbr_initiate_shutdown() {
1129 struct moonbr_pool *pool;
1130 int i;
1131 if (moonbr_shutdown_in_progress) {
1132 moonbr_log(LOG_NOTICE, "Shutdown already in progress");
1133 return;
1135 moonbr_shutdown_in_progress = 1;
1136 moonbr_log(LOG_NOTICE, "Initiate shutdown");
1137 for (pool = moonbr_first_pool; pool; pool = pool->next_pool) {
1138 for (i=0; i<pool->listener_count; i++) {
1139 struct moonbr_listener *listener = &pool->listener[i];
1140 if (listener->listenfd != -1) {
1141 if (close(listener->listenfd) && errno != EINTR) {
1142 moonbr_log(LOG_CRIT, "Could not close listening socket: %s", strerror(errno));
1143 moonbr_terminate_error();
1148 moonbr_poll_shutdown(); /* avoids loops due to error condition when polling closed listeners */
1152 /*** Functions to handle previously created 'struct moonbr_worker' structures ***/
1154 #define moonbr_try_destroy_worker_stat(str, field) \
1155 moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: " str " %li", worker->pool->poolnum, (int)worker->pid, (long)childusage.field);
1157 /* Destroys a worker structure if socket connections have been closed and child process has terminated */
1158 static int moonbr_try_destroy_worker(struct moonbr_worker *worker) {
1159 if (worker->controlfd != -1 || worker->errorfd != -1) return MOONBR_DESTROY_NONE;
1161 int childstatus;
1162 struct rusage childusage;
1164 pid_t waitedpid;
1165 while (
1166 (waitedpid = wait4(worker->pid, &childstatus, WNOHANG, &childusage)) == -1
1167 ) {
1168 if (errno != EINTR) {
1169 moonbr_log(LOG_CRIT, "Error in wait4() call: %s", strerror(errno));
1170 moonbr_terminate_error();
1173 if (!waitedpid) return 0; /* return 0 if worker couldn't be destroyed */
1174 if (waitedpid != worker->pid) {
1175 moonbr_log(LOG_CRIT, "Wrong PID returned by wait4() call");
1176 moonbr_terminate_error();
1179 if (WIFEXITED(childstatus)) {
1180 if (WEXITSTATUS(childstatus) || moonbr_stat) {
1181 moonbr_log(
1182 WEXITSTATUS(childstatus) ? LOG_WARNING : LOG_INFO,
1183 "Child process in pool #%i with PID %i returned with exit code %i", worker->pool->poolnum, (int)worker->pid, WEXITSTATUS(childstatus)
1184 );
1186 } else if (WIFSIGNALED(childstatus)) {
1187 if (WCOREDUMP(childstatus)) {
1188 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));
1189 } else if (WTERMSIG(childstatus) == SIGALRM) {
1190 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i exited prematurely due to timeout", worker->pool->poolnum, (int)worker->pid);
1191 } else {
1192 moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
1194 } else {
1195 moonbr_log(LOG_CRIT, "Illegal exit status from child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1196 moonbr_terminate_error();
1198 if (moonbr_stat) {
1199 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));
1200 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));
1201 moonbr_try_destroy_worker_stat("max resident set size", ru_maxrss);
1202 moonbr_try_destroy_worker_stat("integral shared memory size", ru_ixrss);
1203 moonbr_try_destroy_worker_stat("integral unshared data", ru_idrss);
1204 moonbr_try_destroy_worker_stat("integral unshared stack", ru_isrss);
1205 moonbr_try_destroy_worker_stat("page replaims", ru_minflt);
1206 moonbr_try_destroy_worker_stat("page faults", ru_majflt);
1207 moonbr_try_destroy_worker_stat("swaps", ru_nswap);
1208 moonbr_try_destroy_worker_stat("block input operations", ru_inblock);
1209 moonbr_try_destroy_worker_stat("block output operations", ru_oublock);
1210 moonbr_try_destroy_worker_stat("messages sent", ru_msgsnd);
1211 moonbr_try_destroy_worker_stat("messages received", ru_msgrcv);
1212 moonbr_try_destroy_worker_stat("signals received", ru_nsignals);
1213 moonbr_try_destroy_worker_stat("voluntary context switches", ru_nvcsw);
1214 moonbr_try_destroy_worker_stat("involuntary context switches", ru_nivcsw);
1218 int retval = (
1219 (worker->idle || worker->assigned) ?
1220 MOONBR_DESTROY_IDLE_OR_ASSIGNED :
1221 MOONBR_DESTROY_PREPARE
1222 );
1223 if (worker->main) moonbr_initiate_shutdown();
1224 if (worker->prev_worker) worker->prev_worker->next_worker = worker->next_worker;
1225 else worker->pool->first_worker = worker->next_worker;
1226 if (worker->next_worker) worker->next_worker->prev_worker = worker->prev_worker;
1227 else worker->pool->last_worker = worker->prev_worker;
1228 if (worker->idle) {
1229 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker->next_idle_worker;
1230 else worker->pool->first_idle_worker = worker->next_idle_worker;
1231 if (worker->next_idle_worker) worker->next_idle_worker->prev_idle_worker = worker->prev_idle_worker;
1232 else worker->pool->last_idle_worker = worker->prev_idle_worker;
1233 worker->pool->idle_worker_count--;
1235 if (!worker->assigned) worker->pool->unassigned_worker_count--;
1236 worker->pool->total_worker_count--;
1237 worker->pool->worker_count_stat = 1;
1238 if (worker->errorlinebuf) free(worker->errorlinebuf);
1239 free(worker);
1240 return retval;
1244 /* Marks a worker as idle and stores it in a queue, optionally setting 'idle_expiration' value */
1245 static void moonbr_add_idle_worker(struct moonbr_worker *worker) {
1246 worker->prev_idle_worker = worker->pool->last_idle_worker;
1247 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker;
1248 else worker->pool->first_idle_worker = worker;
1249 worker->pool->last_idle_worker = worker;
1250 worker->idle = 1;
1251 worker->pool->idle_worker_count++;
1252 if (worker->assigned) {
1253 worker->assigned = 0;
1254 worker->pool->unassigned_worker_count++;
1256 worker->pool->worker_count_stat = 1;
1257 if (timerisset(&worker->pool->idle_timeout)) {
1258 struct timeval now;
1259 moonbr_now(&now);
1260 timeradd(&now, &worker->pool->idle_timeout, &worker->idle_expiration);
1264 /* Pops a worker from the queue of idle workers (idle queue must not be empty) */
1265 static struct moonbr_worker *moonbr_pop_idle_worker(struct moonbr_pool *pool) {
1266 struct moonbr_worker *worker;
1267 worker = pool->first_idle_worker;
1268 pool->first_idle_worker = worker->next_idle_worker;
1269 if (pool->first_idle_worker) pool->first_idle_worker->prev_idle_worker = NULL;
1270 else pool->last_idle_worker = NULL;
1271 worker->next_idle_worker = NULL;
1272 worker->idle = 0;
1273 worker->pool->idle_worker_count--;
1274 worker->assigned = 1;
1275 worker->pool->unassigned_worker_count--;
1276 worker->pool->worker_count_stat = 1;
1277 return worker;
1281 /*** Functions to communicate with child processes ***/
1283 /* Tells child process to terminate */
1284 static void moonbr_terminate_idle_worker(struct moonbr_worker *worker) {
1285 moonbr_send_control_message(worker, MOONBR_COMMAND_TERMINATE, -1, NULL);
1288 /* Handles status messages from child process */
1289 static void moonbr_read_controlchannel(struct moonbr_worker *worker) {
1290 char controlmsg;
1292 ssize_t bytes_read;
1293 while ((bytes_read = read(worker->controlfd, &controlmsg, 1)) <= 0) {
1294 if (bytes_read == 0 || errno == ECONNRESET) {
1295 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i unexpectedly closed control socket", worker->pool->poolnum, (int)worker->pid);
1296 if (close(worker->controlfd) && errno != EINTR) {
1297 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));
1298 moonbr_terminate_error();
1300 worker->controlfd = -1;
1301 moonbr_poll_refresh_needed = 1;
1302 return;
1304 if (errno != EINTR) {
1305 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));
1306 moonbr_terminate_error();
1310 if (worker->idle) {
1311 moonbr_log(LOG_CRIT, "Unexpected data from supposedly idle child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1312 moonbr_terminate_error();
1314 if (moonbr_debug) {
1315 moonbr_log(LOG_DEBUG, "Received control message from child in pool #%i with PID %i: \"%c\"", worker->pool->poolnum, (int)worker->pid, (int)controlmsg);
1317 switch (controlmsg) {
1318 case MOONBR_STATUS_IDLE:
1319 if (moonbr_stat) {
1320 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i reports as idle", worker->pool->poolnum, (int)worker->pid);
1322 moonbr_add_idle_worker(worker);
1323 break;
1324 case MOONBR_STATUS_GOODBYE:
1325 if (moonbr_stat) {
1326 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i announced termination", worker->pool->poolnum, (int)worker->pid);
1328 if (close(worker->controlfd) && errno != EINTR) {
1329 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));
1330 moonbr_terminate_error();
1332 worker->controlfd = -1;
1333 moonbr_poll_refresh_needed = 1;
1334 break;
1335 default:
1336 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);
1337 moonbr_terminate_error();
1341 /* Handles stderr stream from child process */
1342 static void moonbr_read_errorchannel(struct moonbr_worker *worker) {
1343 char staticbuf[MOONBR_MAXERRORLINELEN+1];
1344 char *buf = worker->errorlinebuf;
1345 if (!buf) buf = staticbuf;
1347 ssize_t bytes_read;
1348 while (
1349 (bytes_read = read(
1350 worker->errorfd,
1351 buf + worker->errorlinelen,
1352 MOONBR_MAXERRORLINELEN+1 - worker->errorlinelen
1353 )) <= 0
1354 ) {
1355 if (bytes_read == 0 || errno == ECONNRESET) {
1356 if (moonbr_debug) {
1357 moonbr_log(LOG_DEBUG, "Child process in pool #%i with PID %i closed stderr socket", worker->pool->poolnum, (int)worker->pid);
1359 if (close(worker->errorfd) && errno != EINTR) {
1360 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));
1361 moonbr_terminate_error();
1363 worker->errorfd = -1;
1364 moonbr_poll_refresh_needed = 1;
1365 break;
1367 if (errno != EINTR) {
1368 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));
1369 moonbr_terminate_error();
1372 worker->errorlinelen += bytes_read;
1375 int i;
1376 for (i=0; i<worker->errorlinelen; i++) {
1377 if (buf[i] == '\n') buf[i] = 0;
1378 if (!buf[i]) {
1379 if (worker->errorlineovf) {
1380 worker->errorlineovf = 0;
1381 } else {
1382 moonbr_log(LOG_WARNING, "Error log from process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, buf);
1384 worker->errorlinelen -= i+1;
1385 memmove(buf, buf+i+1, worker->errorlinelen);
1386 i = -1;
1389 if (i > MOONBR_MAXERRORLINELEN) {
1390 buf[MOONBR_MAXERRORLINELEN] = 0;
1391 if (!worker->errorlineovf) {
1392 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);
1394 worker->errorlinelen = 0;
1395 worker->errorlineovf = 1;
1398 if (!worker->errorlinebuf && worker->errorlinelen) { /* allocate buffer on heap only if necessary */
1399 worker->errorlinebuf = malloc((MOONBR_MAXERRORLINELEN+1) * sizeof(char));
1400 if (!worker->errorlinebuf) {
1401 moonbr_log(LOG_CRIT, "Memory allocation error");
1402 moonbr_terminate_error();
1404 memcpy(worker->errorlinebuf, staticbuf, worker->errorlinelen);
1409 /*** Handler for incoming connections ***/
1411 /* Accepts one or more incoming connections on listener socket and passes it to worker(s) popped from idle queue */
1412 static void moonbr_connect(struct moonbr_pool *pool) {
1413 struct moonbr_listener *listener = moonbr_pop_connected_listener(pool);
1414 struct moonbr_worker *worker;
1415 if (listener->proto == MOONBR_PROTO_MAIN) {
1416 worker = moonbr_pop_idle_worker(pool);
1417 if (moonbr_stat) {
1418 moonbr_log(LOG_INFO, "Dispatching main thread of pool #%i to PID %i", listener->pool->poolnum, (int)worker->pid);
1420 worker->main = 1;
1421 moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, -1, listener);
1422 /* do not push listener to queue of idle listeners */
1423 } else if (listener->proto == MOONBR_PROTO_INTERVAL) {
1424 worker = moonbr_pop_idle_worker(pool);
1425 if (moonbr_stat) {
1426 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);
1428 worker->restart_interval_listener = listener;
1429 moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, -1, listener);
1430 /* do not push listener to queue of idle listeners yet */
1431 } else {
1432 int peerfd;
1433 do {
1434 peerfd = accept4(listener->listenfd, NULL, NULL, SOCK_CLOEXEC);
1435 if (peerfd == -1) {
1436 if (errno == EWOULDBLOCK) {
1437 break;
1438 } else if (errno == ECONNABORTED) {
1439 moonbr_log(LOG_WARNING, "Connection aborted before accepting it");
1440 break;
1441 } else if (errno != EINTR) {
1442 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1443 moonbr_terminate_error();
1445 } else {
1446 worker = moonbr_pop_idle_worker(pool);
1447 if (moonbr_stat) {
1448 moonbr_log(LOG_INFO, "Dispatching connection for pool #%i to PID %i", listener->pool->poolnum, (int)worker->pid);
1450 moonbr_send_control_message(worker, MOONBR_COMMAND_CONNECT, peerfd, listener);
1451 if (close(peerfd) && errno != EINTR) {
1452 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1453 moonbr_terminate_error();
1456 } while (pool->first_idle_worker);
1457 moonbr_add_idle_listener(listener);
1462 /*** Functions to initialize and restart interval timers ***/
1464 /* Initializes all interval timers */
1465 static void moonbr_interval_initialize() {
1466 struct timeval now;
1467 struct moonbr_pool *pool;
1468 moonbr_now(&now);
1469 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1470 int i;
1471 for (i=0; i<pool->listener_count; i++) {
1472 struct moonbr_listener *listener = &pool->listener[i];
1473 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1474 timeradd(
1475 &now,
1476 &listener->type_specific.interval.delay,
1477 &listener->type_specific.interval.wakeup
1478 );
1484 /* If necessary, restarts interval timers and queues interval listener as idle after a worker changed status */
1485 static void moonbr_interval_restart(
1486 struct moonbr_worker *worker,
1487 struct timeval *now /* passed to synchronize with moonbr_run() function */
1488 ) {
1489 struct moonbr_listener *listener = worker->restart_interval_listener;
1490 if (listener) {
1491 moonbr_add_idle_listener(listener);
1492 worker->restart_interval_listener = NULL;
1493 if (listener->type_specific.interval.strict) {
1494 timeradd(
1495 &listener->type_specific.interval.wakeup,
1496 &listener->type_specific.interval.delay,
1497 &listener->type_specific.interval.wakeup
1498 );
1499 if (timercmp(&listener->type_specific.interval.wakeup, now, <)) {
1500 listener->type_specific.interval.wakeup = *now;
1502 } else {
1503 timeradd(
1504 now,
1505 &listener->type_specific.interval.delay,
1506 &listener->type_specific.interval.wakeup
1507 );
1513 /*** Main loop and helper functions ***/
1515 /* Stores the earliest required wakeup time in 'wait' variable */
1516 static void moonbr_calc_wait(struct timeval *wait, struct timeval *wakeup) {
1517 if (!timerisset(wait) || timercmp(wakeup, wait, <)) *wait = *wakeup;
1520 /* Main loop of Moonbridge system (including initialization of signal handlers and polling structures) */
1521 static void moonbr_run(lua_State *L) {
1522 struct timeval now;
1523 struct moonbr_pool *pool;
1524 struct moonbr_worker *worker;
1525 struct moonbr_worker *next_worker; /* needed when worker is removed during iteration of workers */
1526 struct moonbr_listener *listener;
1527 struct moonbr_listener *next_listener; /* needed when listener is removed during iteration of listeners */
1528 int i;
1529 moonbr_poll_init(); /* must be executed before moonbr_signal_init() */
1530 moonbr_signal_init();
1531 moonbr_interval_initialize();
1532 moonbr_pstate = MOONBR_PSTATE_RUNNING;
1533 while (1) {
1534 struct timeval wait = {0, }; /* point in time when premature wakeup of poll() is required */
1535 if (moonbr_cond_interrupt) {
1536 moonbr_log(LOG_WARNING, "Fast shutdown requested");
1537 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
1539 if (moonbr_cond_terminate) {
1540 moonbr_initiate_shutdown();
1541 moonbr_cond_terminate = 0;
1543 moonbr_cond_child = 0; /* must not be reset between moonbr_try_destroy_worker() and poll() */
1544 moonbr_now(&now);
1545 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1546 int terminated_worker_count = 0; /* allows shortcut for new worker creation */
1547 /* terminate idle workers when expired */
1548 if (timerisset(&pool->idle_timeout)) {
1549 while ((worker = pool->first_idle_worker) != NULL) {
1550 if (timercmp(&worker->idle_expiration, &now, >)) break;
1551 moonbr_pop_idle_worker(pool);
1552 moonbr_terminate_idle_worker(worker);
1555 /* mark listeners as connected when incoming connection is pending */
1556 for (listener=pool->first_idle_listener; listener; listener=next_listener) {
1557 next_listener = listener->next_listener; /* extra variable necessary due to changing list */
1558 if (listener->pollidx != -1) {
1559 if (moonbr_poll_fds[listener->pollidx].revents) {
1560 moonbr_poll_fds[listener->pollidx].revents = 0;
1561 moonbr_remove_idle_listener(listener);
1562 moonbr_add_connected_listener(listener);
1564 } else if (
1565 listener->proto != MOONBR_PROTO_INTERVAL ||
1566 !timercmp(&listener->type_specific.interval.wakeup, &now, >)
1567 ) {
1568 moonbr_remove_idle_listener(listener);
1569 moonbr_add_connected_listener(listener);
1572 /* process input from child processes */
1573 for (i=0; i<moonbr_poll_worker_count; i++) {
1574 if (moonbr_poll_worker_fds[i].revents) {
1575 moonbr_poll_worker_fds[i].revents = 0;
1576 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[i];
1577 switch (poll_worker->channel) {
1578 case MOONBR_POLL_WORKER_CONTROLCHANNEL:
1579 moonbr_read_controlchannel(poll_worker->worker);
1580 moonbr_interval_restart(poll_worker->worker, &now);
1581 break;
1582 case MOONBR_POLL_WORKER_ERRORCHANNEL:
1583 moonbr_read_errorchannel(poll_worker->worker);
1584 break;
1588 /* collect dead child processes */
1589 for (worker=pool->first_worker; worker; worker=next_worker) {
1590 next_worker = worker->next_worker; /* extra variable necessary due to changing list */
1591 switch (moonbr_try_destroy_worker(worker)) {
1592 case MOONBR_DESTROY_PREPARE:
1593 pool->use_fork_error_wakeup = 1;
1594 break;
1595 case MOONBR_DESTROY_IDLE_OR_ASSIGNED:
1596 terminated_worker_count++;
1597 break;
1600 if (!moonbr_shutdown_in_progress) {
1601 /* connect listeners with idle workers */
1602 while (pool->first_connected_listener && pool->first_idle_worker) {
1603 moonbr_connect(pool);
1605 /* create new worker processes */
1606 while (
1607 pool->total_worker_count < pool->max_fork && (
1608 pool->unassigned_worker_count < pool->pre_fork ||
1609 pool->total_worker_count < pool->min_fork
1611 ) {
1612 if (pool->use_fork_error_wakeup) {
1613 if (timercmp(&pool->fork_error_wakeup, &now, >)) {
1614 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
1615 break;
1617 } else {
1618 if (terminated_worker_count) {
1619 terminated_worker_count--;
1620 } else if (timercmp(&pool->fork_wakeup, &now, >)) {
1621 moonbr_calc_wait(&wait, &pool->fork_wakeup);
1622 break;
1625 if (moonbr_create_worker(pool, L)) {
1626 /* on error, enforce error delay */
1627 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
1628 pool->use_fork_error_wakeup = 1;
1629 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
1630 break;
1631 } else {
1632 /* normal fork delay on success */
1633 timeradd(&now, &pool->fork_delay, &pool->fork_wakeup);
1634 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
1635 pool->use_fork_error_wakeup = 0; /* gets set later if error occures during preparation */
1638 /* terminate excessive worker processes */
1639 while (
1640 pool->total_worker_count > pool->min_fork &&
1641 pool->idle_worker_count > pool->pre_fork
1642 ) {
1643 if (timerisset(&pool->exit_wakeup)) {
1644 if (timercmp(&pool->exit_wakeup, &now, >)) {
1645 moonbr_calc_wait(&wait, &pool->exit_wakeup);
1646 break;
1648 moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
1649 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
1650 } else {
1651 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
1652 break;
1655 if (!(
1656 pool->total_worker_count > pool->min_fork &&
1657 pool->idle_worker_count > pool->pre_fork
1658 )) {
1659 timerclear(&pool->exit_wakeup); /* timer gets restarted later when there are excessive workers */
1662 /* optionally output worker count stats */
1663 if (moonbr_stat && pool->worker_count_stat) {
1664 pool->worker_count_stat = 0;
1665 moonbr_log(
1666 LOG_INFO,
1667 "Worker count for pool #%i: %i idle, %i assigned, %i total",
1668 pool->poolnum, pool->idle_worker_count,
1669 pool->total_worker_count - pool->unassigned_worker_count,
1670 pool->total_worker_count);
1672 /* calculate wakeup time for interval listeners */
1673 for (listener=pool->first_idle_listener; listener; listener=listener->next_listener) {
1674 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1675 moonbr_calc_wait(&wait, &listener->type_specific.interval.wakeup);
1678 /* calculate wakeup time for idle workers (only first idle worker is significant) */
1679 if (timerisset(&pool->idle_timeout) && pool->first_idle_worker) {
1680 moonbr_calc_wait(&wait, &pool->first_idle_worker->idle_expiration);
1683 /* terminate idle workers in case of shutdown and check if shutdown is complete */
1684 if (moonbr_shutdown_in_progress) {
1685 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1686 if (pool->first_worker) {
1687 while (pool->idle_worker_count) {
1688 moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
1690 break;
1693 if (!pool) {
1694 moonbr_log(LOG_INFO, "All worker threads have terminated");
1695 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
1698 if (moonbr_poll_refresh_needed) moonbr_poll_refresh();
1699 moonbr_cond_poll = 1;
1700 if (!moonbr_cond_child && !moonbr_cond_terminate && !moonbr_cond_interrupt) {
1701 int timeout;
1702 if (timerisset(&wait)) {
1703 if (timercmp(&wait, &now, <)) {
1704 moonbr_log(LOG_CRIT, "Internal error (should not happen): Future is in the past");
1705 moonbr_terminate_error();
1707 timersub(&wait, &now, &wait);
1708 timeout = wait.tv_sec * 1000 + wait.tv_usec / 1000;
1709 } else {
1710 timeout = INFTIM;
1712 if (moonbr_debug) {
1713 moonbr_log(LOG_DEBUG, "Waiting for I/O");
1715 poll(moonbr_poll_fds, moonbr_poll_fds_count, timeout);
1716 } else {
1717 if (moonbr_debug) {
1718 moonbr_log(LOG_DEBUG, "Do not wait for I/O");
1721 moonbr_cond_poll = 0;
1722 moonbr_poll_reset_signal();
1727 /*** Lua interface ***/
1729 static int moonbr_lua_panic(lua_State *L) {
1730 const char *errmsg;
1731 errmsg = lua_tostring(L, -1);
1732 if (!errmsg) {
1733 if (lua_isnoneornil(L, -1)) errmsg = "(error message is nil)";
1734 else errmsg = "(error message is not a string)";
1736 if (moonbr_pstate == MOONBR_PSTATE_FORKED) {
1737 fprintf(stderr, "Uncaught Lua error: %s\n", errmsg);
1738 exit(1);
1739 } else {
1740 moonbr_log(LOG_CRIT, "Uncaught Lua error: %s", errmsg);
1741 moonbr_terminate_error();
1743 return 0;
1746 static int moonbr_addtraceback(lua_State *L) {
1747 luaL_traceback(L, L, luaL_tolstring(L, 1, NULL), 1);
1748 return 1;
1751 /* Memory allocator that allows limiting memory consumption */
1752 static void *moonbr_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
1753 (void)ud; /* not used */
1754 if (nsize == 0) {
1755 if (ptr) {
1756 moonbr_memory_usage -= osize;
1757 free(ptr);
1759 return NULL;
1760 } else if (ptr) {
1761 if (
1762 moonbr_memory_limit &&
1763 nsize > osize &&
1764 moonbr_memory_usage + (nsize - osize) > moonbr_memory_limit
1765 ) {
1766 return NULL;
1767 } else {
1768 ptr = realloc(ptr, nsize);
1769 if (ptr) moonbr_memory_usage += nsize - osize;
1771 } else {
1772 if (
1773 moonbr_memory_limit &&
1774 moonbr_memory_usage + nsize > moonbr_memory_limit
1775 ) {
1776 return NULL;
1777 } else {
1778 ptr = realloc(ptr, nsize);
1779 if (ptr) moonbr_memory_usage += nsize;
1782 return ptr;
1785 static int moonbr_lua_tonatural(lua_State *L, int idx) {
1786 int isnum;
1787 lua_Number n;
1788 n = lua_tonumberx(L, idx, &isnum);
1789 if (isnum && n>=0 && n<INT_MAX && (lua_Number)(int)n == n) return n;
1790 else return -1;
1793 static int moonbr_lua_totimeval(lua_State *L, int idx, struct timeval *value) {
1794 int isnum;
1795 lua_Number n;
1796 n = lua_tonumberx(L, idx, &isnum);
1797 if (isnum && n>=0 && n<=100000000) {
1798 value->tv_sec = n;
1799 value->tv_usec = 1e6 * (n - value->tv_sec);
1800 return 1;
1801 } else {
1802 return 0;
1806 static int moonbr_timeout(lua_State *L) {
1807 struct itimerval oldval;
1808 if (lua_isnoneornil(L, 1) && lua_isnoneornil(L, 2)) {
1809 getitimer(ITIMER_REAL, &oldval);
1810 } else {
1811 struct itimerval newval = {};
1812 timerclear(&newval.it_interval);
1813 timerclear(&newval.it_value);
1814 if (lua_toboolean(L, 1)) {
1815 luaL_argcheck(
1816 L, moonbr_lua_totimeval(L, 1, &newval.it_value), 1,
1817 "interval in seconds expected"
1818 );
1820 if (lua_isnoneornil(L, 2)) {
1821 if (setitimer(ITIMER_REAL, &newval, &oldval)) {
1822 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1823 moonbr_terminate_error();
1825 } else {
1826 getitimer(ITIMER_REAL, &oldval);
1827 if (!timerisset(&oldval.it_value)) {
1828 if (setitimer(ITIMER_REAL, &newval, NULL)) {
1829 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1830 moonbr_terminate_error();
1832 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
1833 timerclear(&newval.it_value);
1834 if (setitimer(ITIMER_REAL, &newval, NULL)) {
1835 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1836 moonbr_terminate_error();
1838 } else if (timercmp(&newval.it_value, &oldval.it_value, <)) {
1839 struct itimerval remval;
1840 if (setitimer(ITIMER_REAL, &newval, NULL)) {
1841 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1842 moonbr_terminate_error();
1844 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
1845 getitimer(ITIMER_REAL, &remval);
1846 timersub(&oldval.it_value, &newval.it_value, &newval.it_value);
1847 timeradd(&newval.it_value, &remval.it_value, &newval.it_value);
1848 if (setitimer(ITIMER_REAL, &newval, NULL)) {
1849 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
1850 moonbr_terminate_error();
1852 } else {
1853 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
1855 return lua_gettop(L) - 1;
1858 lua_pushnumber(L, oldval.it_value.tv_sec + 1e-6 * oldval.it_value.tv_usec);
1859 return 1;
1862 #define moonbr_listen_init_pool_forkoption(luaname, cname, defval) { \
1863 lua_getfield(L, 2, luaname); \
1864 pool->cname = lua_isnil(L, -1) ? (defval) : moonbr_lua_tonatural(L, -1); \
1865 } while(0)
1867 #define moonbr_listen_init_pool_timeoption(luaname, cname, defval, defvalu) ( \
1868 lua_getfield(L, 2, luaname), \
1869 lua_isnil(L, -1) ? ( \
1870 pool->cname.tv_sec = (defval), pool->cname.tv_usec = (defvalu), \
1871 1 \
1872 ) : ( \
1873 (lua_isboolean(L, -1) && !lua_toboolean(L, -1)) ? ( \
1874 pool->cname.tv_sec = 0, pool->cname.tv_usec = 0, \
1875 1 \
1876 ) : ( \
1877 moonbr_lua_totimeval(L, -1, &pool->cname) \
1878 ) \
1879 ) \
1882 static int moonbr_listen_init_pool(lua_State *L) {
1883 struct moonbr_pool *pool;
1884 const char *proto;
1885 int i;
1886 int dynamic = 0; /* nonzero = listeners exist which require dynamic worker creation */
1887 pool = lua_touserdata(L, 1);
1888 for (i=0; i<pool->listener_count; i++) {
1889 struct moonbr_listener *listener = &pool->listener[i];
1890 lua_settop(L, 2);
1891 #if LUA_VERSION_NUM >= 503
1892 lua_geti(L, 2, i+1);
1893 #else
1894 lua_pushinteger(L, i+1);
1895 lua_gettable(L, 2);
1896 #endif
1897 lua_getfield(L, 3, "proto");
1898 proto = lua_tostring(L, -1);
1899 if (proto && !strcmp(proto, "main")) {
1900 listener->proto = MOONBR_PROTO_MAIN;
1901 } else if (proto && !strcmp(proto, "interval")) {
1902 dynamic = 1;
1903 listener->proto = MOONBR_PROTO_INTERVAL;
1904 lua_getfield(L, 3, "name");
1906 const char *name = lua_tostring(L, -1);
1907 if (name) {
1908 if (asprintf(&listener->type_specific.interval.name, "%s", name) < 0) {
1909 moonbr_log(LOG_CRIT, "Memory allocation_error");
1910 moonbr_terminate_error();
1914 lua_getfield(L, 3, "delay");
1915 if (
1916 !moonbr_lua_totimeval(L, -1, &listener->type_specific.interval.delay) ||
1917 !timerisset(&listener->type_specific.interval.delay)
1918 ) {
1919 luaL_error(L, "No valid interval delay specified; use listen{{proto=\"interval\", delay=...}, ...}");
1921 lua_getfield(L, 3, "strict");
1922 if (!lua_isnil(L, -1)) {
1923 if (lua_isboolean(L, -1)) {
1924 if (lua_toboolean(L, -1)) listener->type_specific.interval.strict = 1;
1925 } else {
1926 luaL_error(L, "Option \"strict\" must be a boolean if set; use listen{{proto=\"interval\", strict=true, ...}, ...}");
1929 } else if (proto && !strcmp(proto, "local")) {
1930 const char *path;
1931 const int path_maxlen = (
1932 sizeof(listener->type_specific.socket.addr.addr_un) -
1933 ((void *)listener->type_specific.socket.addr.addr_un.sun_path - (void *)&listener->type_specific.socket.addr.addr_un)
1934 ) - 1; /* one byte for termination */
1935 dynamic = 1;
1936 listener->proto = MOONBR_PROTO_LOCAL;
1937 lua_getfield(L, 3, "path");
1938 path = lua_tostring(L, -1);
1939 if (!path) {
1940 luaL_error(L, "No valid path specified for local socket; use listen{{proto=\"local\", path=...}, ...}");
1942 if (strlen(path) > path_maxlen) {
1943 luaL_error(L, "Path name for local socket exceeded maximum length of %i characters", path_maxlen);
1945 strcpy(listener->type_specific.socket.addr.addr_un.sun_path, path);
1946 } else if (proto && !strcmp(proto, "tcp")) {
1947 const char *host, *port;
1948 struct addrinfo hints = { 0, };
1949 struct addrinfo *res, *addrinfo;
1950 int errcode;
1951 const char *ip;
1952 dynamic = 1;
1953 lua_getfield(L, 3, "host");
1954 host = lua_isnil(L, -1) ? "::" : lua_tostring(L, -1);
1955 if (!host) {
1956 luaL_error(L, "No host specified; use listen{{proto=\"tcp\", host=...}, ...}");
1958 lua_getfield(L, 3, "port");
1959 port = lua_tostring(L, -1);
1960 if (!port) {
1961 luaL_error(L, "No port specified; use listen{{proto=\"tcp\", host=...}, ...}");
1963 hints.ai_family = AF_UNSPEC;
1964 hints.ai_socktype = SOCK_STREAM;
1965 hints.ai_protocol = IPPROTO_TCP;
1966 hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
1967 errcode = getaddrinfo(host, port, &hints, &res);
1968 if (errcode) {
1969 freeaddrinfo(res);
1970 if (errcode == EAI_SYSTEM) {
1971 char errmsg[MOONBR_MAXSTRERRORLEN];
1972 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
1973 luaL_error(L, "Could not resolve host: %s: %s", gai_strerror(errcode), errmsg);
1974 } else {
1975 luaL_error(L, "Could not resolve host: %s", gai_strerror(errcode));
1978 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1979 if (addrinfo->ai_family == AF_INET6) goto moonbr_listen_init_pool_found;
1981 for (addrinfo=res; addrinfo; addrinfo=addrinfo->ai_next) {
1982 if (addrinfo->ai_family == AF_INET) goto moonbr_listen_init_pool_found;
1984 addrinfo = res;
1985 moonbr_listen_init_pool_found:
1986 if (addrinfo->ai_addrlen > sizeof(listener->type_specific.socket.addr)) {
1987 moonbr_log(LOG_CRIT, "Size of ai_addrlen is unexpectedly big (should not happen)");
1988 moonbr_terminate_error();
1990 memcpy(&listener->type_specific.socket.addr, addrinfo->ai_addr, addrinfo->ai_addrlen);
1991 listener->type_specific.socket.addrlen = addrinfo->ai_addrlen;
1992 switch (addrinfo->ai_family) {
1993 case AF_INET6:
1994 ip = inet_ntop(
1995 addrinfo->ai_family,
1996 &((struct sockaddr_in6 *)addrinfo->ai_addr)->sin6_addr,
1997 listener->proto_specific.tcp.ip,
1998 INET6_ADDRSTRLEN
1999 );
2000 if (!ip) {
2001 moonbr_log(LOG_CRIT, "System error in inet_ntop call: %s", strerror(errno));
2002 moonbr_terminate_error();
2004 listener->proto_specific.tcp.port = ntohs(((struct sockaddr_in6 *)addrinfo->ai_addr)->sin6_port);
2005 break;
2006 case AF_INET:
2007 ip = inet_ntop(
2008 addrinfo->ai_family,
2009 &((struct sockaddr_in *)addrinfo->ai_addr)->sin_addr,
2010 listener->proto_specific.tcp.ip,
2011 INET6_ADDRSTRLEN
2012 );
2013 if (!ip) {
2014 moonbr_log(LOG_CRIT, "System error in inet_ntop call: %s", strerror(errno));
2015 moonbr_terminate_error();
2017 listener->proto_specific.tcp.port = ntohs(((struct sockaddr_in *)addrinfo->ai_addr)->sin_port);
2018 break;
2019 default:
2020 strcpy(listener->proto_specific.tcp.ip, "unknown");
2021 listener->proto_specific.tcp.port = 0;
2023 listener->proto = MOONBR_PROTO_TCP;
2024 } else if (proto) {
2025 luaL_error(L, "Unknown protocol \"%s\"", proto);
2026 } else {
2027 luaL_error(L, "No valid protocol specified; use listen{{proto=..., ...}, ...}");
2030 lua_settop(L, 2);
2031 if (dynamic) {
2032 moonbr_listen_init_pool_forkoption("pre_fork", pre_fork, 1);
2033 moonbr_listen_init_pool_forkoption("min_fork", min_fork, pool->pre_fork > 2 ? pool->pre_fork : 2);
2034 moonbr_listen_init_pool_forkoption("max_fork", max_fork, pool->min_fork > 16 ? pool->min_fork : 16);
2035 if (!moonbr_listen_init_pool_timeoption("fork_delay", fork_delay, 0, 250000)) {
2036 luaL_error(L, "Option \"fork_delay\" is expected to be a non-negative number");
2038 if (!moonbr_listen_init_pool_timeoption("fork_error_delay", fork_error_delay, 2, 0)) {
2039 luaL_error(L, "Option \"fork_error_delay\" is expected to be a non-negative number");
2041 if (!moonbr_listen_init_pool_timeoption("exit_delay", exit_delay, 60, 0)) {
2042 luaL_error(L, "Option \"exit_delay\" is expected to be a non-negative number");
2044 if (timercmp(&pool->fork_error_delay, &pool->fork_delay, <)) {
2045 pool->fork_error_delay = pool->fork_delay;
2047 if (!moonbr_listen_init_pool_timeoption("idle_timeout", idle_timeout, 0, 0)) {
2048 luaL_error(L, "Option \"idle_timeout\" is expected to be a non-negative number");
2050 } else {
2051 pool->pre_fork = 0;
2052 pool->min_fork = pool->listener_count;
2053 pool->max_fork = pool->listener_count;
2055 lua_getfield(L, 2, "memory_limit");
2056 if (!lua_isnil(L, -1)) {
2057 int isnum;
2058 lua_Number n;
2059 n = lua_tonumberx(L, -1, &isnum);
2060 if (n < 0 || !isnum) {
2061 luaL_error(L, "Option \"memory_limit\" is expected to be a non-negative number");
2063 pool->memory_limit = n;
2065 lua_settop(L, 2);
2066 lua_getfield(L, 2, "prepare");
2067 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2068 luaL_error(L, "Option \"prepare\" must be nil or a function");
2070 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2071 lua_getfield(L, 2, "connect");
2072 if (!lua_isfunction(L, -1)) {
2073 luaL_error(L, "Option \"connect\" must be a function; use listen{{...}, {...}, connect=function(socket) ... end, ...}");
2075 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2076 lua_getfield(L, 2, "finish");
2077 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2078 luaL_error(L, "Option \"finish\" must be nil or a function");
2080 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2081 return 0;
2084 static int moonbr_listen(lua_State *L) {
2085 struct moonbr_pool *pool;
2086 lua_Integer listener_count;
2087 if (moonbr_booted) luaL_error(L, "Moonbridge bootup is already complete");
2088 luaL_checktype(L, 1, LUA_TTABLE);
2089 listener_count = luaL_len(L, 1);
2090 if (!listener_count) luaL_error(L, "No listen ports specified; use listen{{proto=..., port=...},...}");
2091 if (listener_count > 100) luaL_error(L, "Too many listeners");
2092 pool = moonbr_create_pool(listener_count);
2093 lua_pushcfunction(L, moonbr_listen_init_pool);
2094 lua_pushlightuserdata(L, pool);
2095 lua_pushvalue(L, 1);
2096 if (lua_pcall(L, 2, 0, 0)) goto moonbr_listen_error;
2098 int i;
2099 i = moonbr_start_pool(pool);
2100 if (i >= 0) {
2101 lua_pushfstring(L, "Could not initialize listener #%d: %s", i+1, strerror(errno));
2102 moonbr_listen_error:
2103 moonbr_destroy_pool(pool);
2104 lua_pushnil(L);
2105 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2106 lua_pushnil(L);
2107 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2108 lua_pushnil(L);
2109 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2110 lua_error(L);
2113 return 0;
2117 /*** Function to modify Lua's library path and/or cpath ***/
2119 #if defined(MOONBR_LUA_PATH) || defined(MOONBR_LUA_CPATH)
2120 static void moonbr_modify_path(lua_State *L, char *key, char *value) {
2121 int stackbase;
2122 stackbase = lua_gettop(L);
2123 lua_getglobal(L, "package");
2124 lua_getfield(L, stackbase+1, key);
2126 const char *current_str;
2127 size_t current_strlen;
2128 luaL_Buffer buf;
2129 current_str = lua_tolstring(L, stackbase+2, &current_strlen);
2130 luaL_buffinit(L, &buf);
2131 if (current_str) {
2132 lua_pushvalue(L, stackbase+2);
2133 luaL_addvalue(&buf);
2134 if (current_strlen && current_str[current_strlen-1] != ';') {
2135 luaL_addchar(&buf, ';');
2138 luaL_addstring(&buf, value);
2139 luaL_pushresult(&buf);
2141 lua_setfield(L, stackbase+1, key);
2142 lua_settop(L, stackbase);
2144 #endif
2147 /*** Main function and command line invokation ***/
2149 static void moonbr_usage(int err, const char *cmd) {
2150 FILE *out;
2151 out = err ? stderr : stdout;
2152 if (!cmd) cmd = "moonbridge";
2153 fprintf(out, "Get this help message: %s {-h|--help}\n", cmd);
2154 fprintf(out, "Usage: %s \\\n", cmd);
2155 fprintf(out, " [-b|--background] \\\n");
2156 fprintf(out, " [-d|--debug] \\\n");
2157 fprintf(out, " [-f|--logfacility {DAEMON|USER|0|1|...|7}] \\\n");
2158 fprintf(out, " [-i|--logident <syslog ident> \\\n");
2159 fprintf(out, " [-l|--logfile <logfile>] \\\n");
2160 fprintf(out, " [-p|--pidfile <pidfile>] \\\n");
2161 fprintf(out, " [-s|--stats] \\\n");
2162 fprintf(out, " -- <Lua script> [<cmdline options for Lua script>]\n");
2163 exit(err);
2166 #define moonbr_usage_error() moonbr_usage(MOONBR_EXITCODE_CMDLINEERROR, argc ? argv[0] : NULL)
2168 int main(int argc, char **argv) {
2170 int daemonize = 0;
2171 int log_facility = LOG_USER;
2172 const char *log_ident = "moonbridge";
2173 const char *log_filename = NULL;
2174 const char *pid_filename = NULL;
2175 int option;
2176 struct option longopts[] = {
2177 { "background", no_argument, NULL, 'b' },
2178 { "debug", no_argument, NULL, 'd' },
2179 { "logfacility", required_argument, NULL, 'f' },
2180 { "help", no_argument, NULL, 'h' },
2181 { "logident", required_argument, NULL, 'i' },
2182 { "logfile", required_argument, NULL, 'l' },
2183 { "pidfile", required_argument, NULL, 'p' },
2184 { "stats", no_argument, NULL, 's' }
2185 };
2186 while ((option = getopt_long(argc, argv, "bdf:hi:l:p:s", longopts, NULL)) != -1) {
2187 switch (option) {
2188 case 'b':
2189 daemonize = 1;
2190 break;
2191 case 'd':
2192 moonbr_debug = 1;
2193 moonbr_stat = 1;
2194 break;
2195 case 'f':
2196 if (!strcmp(optarg, "DAEMON")) {
2197 log_facility = LOG_DAEMON;
2198 } else if (!strcmp(optarg, "USER")) {
2199 log_facility = LOG_USER;
2200 } else if (!strcmp(optarg, "0")) {
2201 log_facility = LOG_LOCAL0;
2202 } else if (!strcmp(optarg, "1")) {
2203 log_facility = LOG_LOCAL1;
2204 } else if (!strcmp(optarg, "2")) {
2205 log_facility = LOG_LOCAL2;
2206 } else if (!strcmp(optarg, "3")) {
2207 log_facility = LOG_LOCAL3;
2208 } else if (!strcmp(optarg, "4")) {
2209 log_facility = LOG_LOCAL4;
2210 } else if (!strcmp(optarg, "5")) {
2211 log_facility = LOG_LOCAL5;
2212 } else if (!strcmp(optarg, "6")) {
2213 log_facility = LOG_LOCAL6;
2214 } else if (!strcmp(optarg, "7")) {
2215 log_facility = LOG_LOCAL7;
2216 } else {
2217 moonbr_usage_error();
2219 moonbr_use_syslog = 1;
2220 break;
2221 case 'h':
2222 moonbr_usage(MOONBR_EXITCODE_GRACEFUL, argv[0]);
2223 break;
2224 case 'i':
2225 log_ident = optarg;
2226 moonbr_use_syslog = 1;
2227 break;
2228 case 'l':
2229 log_filename = optarg;
2230 break;
2231 case 'p':
2232 pid_filename = optarg;
2233 break;
2234 case 's':
2235 moonbr_stat = 1;
2236 break;
2237 default:
2238 moonbr_usage_error();
2241 if (argc - optind < 1) moonbr_usage_error();
2242 if (pid_filename) {
2243 pid_t otherpid;
2244 while ((moonbr_pidfh = pidfile_open(pid_filename, 0644, &otherpid)) == NULL) {
2245 if (errno == EEXIST) {
2246 if (otherpid == -1) {
2247 fprintf(stderr, "PID file \"%s\" is already locked\n", pid_filename);
2248 } else {
2249 fprintf(stderr, "PID file \"%s\" is already locked by process with PID: %i\n", pid_filename, (int)otherpid);
2251 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2252 } else if (errno != EINTR) {
2253 fprintf(stderr, "Could not write PID file \"%s\": %s\n", pid_filename, strerror(errno));
2254 exit(MOONBR_EXITCODE_STARTUPERROR);
2258 if (log_filename) {
2259 int logfd;
2260 while (
2261 ( logfd = flopen(
2262 log_filename,
2263 O_WRONLY|O_NONBLOCK|O_CREAT|O_APPEND|O_CLOEXEC,
2264 0640
2266 ) < 0
2267 ) {
2268 if (errno == EWOULDBLOCK) {
2269 fprintf(stderr, "Logfile \"%s\" is locked\n", log_filename);
2270 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2271 } else if (errno != EINTR) {
2272 fprintf(stderr, "Could not open logfile \"%s\": %s\n", log_filename, strerror(errno));
2273 exit(MOONBR_EXITCODE_STARTUPERROR);
2276 moonbr_logfile = fdopen(logfd, "a");
2277 if (!moonbr_logfile) {
2278 fprintf(stderr, "Could not open write stream to logfile \"%s\": %s\n", log_filename, strerror(errno));
2279 exit(MOONBR_EXITCODE_STARTUPERROR);
2282 if (daemonize == 0 && !moonbr_logfile) moonbr_logfile = stderr;
2283 if (moonbr_logfile) setlinebuf(moonbr_logfile);
2284 else moonbr_use_syslog = 1;
2285 if (moonbr_use_syslog) openlog(log_ident, LOG_NDELAY | LOG_PID, log_facility);
2286 if (daemonize) {
2287 if (daemon(1, 0)) {
2288 moonbr_log(LOG_ERR, "Could not daemonize moonbridge process");
2289 moonbr_terminate_error();
2293 moonbr_log(LOG_NOTICE, "Starting moonbridge server");
2294 if (moonbr_pidfh && pidfile_write(moonbr_pidfh)) {
2295 moonbr_log(LOG_ERR, "Could not write pidfile (after locking)");
2298 lua_State *L;
2299 L = lua_newstate(moonbr_alloc, NULL);
2300 if (!L) {
2301 moonbr_log(LOG_CRIT, "Could not initialize Lua state");
2302 moonbr_terminate_error();
2304 lua_atpanic(L, moonbr_lua_panic);
2305 lua_pushliteral(L, MOONBR_VERSION_STRING);
2306 lua_setglobal(L, "_MOONBRIDGE_VERSION");
2307 luaL_openlibs(L);
2308 luaL_requiref(L, "moonbridge_io", luaopen_moonbridge_io, 1);
2309 lua_pop(L, 1);
2310 #ifdef MOONBR_LUA_PATH
2311 moonbr_modify_path(L, "path", MOONBR_LUA_PATH);
2312 #endif
2313 #ifdef MOONBR_LUA_CPATH
2314 moonbr_modify_path(L, "cpath", MOONBR_LUA_CPATH);
2315 #endif
2316 lua_pushcfunction(L, moonbr_timeout);
2317 lua_setglobal(L, "timeout");
2318 lua_pushcfunction(L, moonbr_listen);
2319 lua_setglobal(L, "listen");
2320 lua_pushcfunction(L, moonbr_addtraceback); /* on stack position 1 */
2321 moonbr_log(LOG_INFO, "Loading \"%s\"", argv[optind]);
2322 if (luaL_loadfile(L, argv[optind])) {
2323 moonbr_log(LOG_ERR, "Error while loading \"%s\": %s", argv[optind], lua_tostring(L, -1));
2324 moonbr_terminate_error();
2326 { int i; for (i=optind+1; i<argc; i++) lua_pushstring(L, argv[i]); }
2327 if (lua_pcall(L, argc-(optind+1), 0, 1)) {
2328 moonbr_log(LOG_ERR, "Error while executing \"%s\": %s", argv[optind], lua_tostring(L, -1));
2329 moonbr_terminate_error();
2331 if (!moonbr_first_pool) {
2332 moonbr_log(LOG_WARNING, "No listener initialized.");
2333 moonbr_terminate_error();
2335 lua_getglobal(L, "listen");
2336 lua_pushcfunction(L, moonbr_listen);
2337 if (lua_compare(L, -2, -1, LUA_OPEQ)) {
2338 lua_pushnil(L);
2339 lua_setglobal(L, "listen");
2341 lua_settop(L, 1);
2342 lua_gc(L, LUA_GCCOLLECT, 0); // collect garbage before forking later
2343 moonbr_run(L);
2345 return 0;

Impressum / About Us