moonbridge

view moonbridge.c @ 73:460e457730a2

Another initialization to avoid (wrong) compiler warning
author jbe
date Sat Apr 04 21:51:33 2015 +0200 (2015-04-04)
parents 7764a1fd85e7
children 9fff0cb4fc65
line source
2 /*** Version ***/
3 #define MOONBR_VERSION_STRING "0.4.0"
6 /*** Compile-time configuration ***/
8 #define MOONBR_LUA_PANIC_BUG_WORKAROUND 1
11 /*** C preprocessor macros for portability support ***/
13 #ifndef __has_include
14 #define __has_include(x) 0
15 #endif
18 /*** Include directives for used system libraries ***/
20 #if defined(__linux__)
21 #define _GNU_SOURCE
22 #endif
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdint.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <syslog.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <time.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <netinet/in.h>
36 #include <poll.h>
37 #include <signal.h>
38 #include <sys/wait.h>
39 #include <sys/resource.h>
40 #include <sys/file.h>
41 #if defined(__FreeBSD__) || __has_include(<libutil.h>)
42 #include <libutil.h>
43 #endif
44 #if defined(__linux__) || __has_include(<bsd/stdio.h>)
45 #include <bsd/stdio.h>
46 #endif
47 #if defined(__linux__) || __has_include(<bsd/libutil.h>)
48 #include <bsd/libutil.h>
49 #endif
50 #if defined(__linux__) || __has_include(<bsd/unistd.h>)
51 #include <bsd/unistd.h>
52 #endif
55 /*** Fallback definitions for missing constants on some platforms ***/
57 /* INFTIM is used as timeout parameter for poll() */
58 #ifndef INFTIM
59 #define INFTIM -1
60 #endif
63 /*** Include directives for Lua ***/
65 #include <lua.h>
66 #include <lauxlib.h>
67 #include <lualib.h>
70 /*** Constants ***/
72 /* Backlog option for listen() call */
73 #define MOONBR_LISTEN_BACKLOG 1024
75 /* Maximum length of a timestamp used for strftime() */
76 #define MOONBR_LOG_MAXTIMELEN 40
78 /* Maximum length of a log message */
79 #define MOONBR_LOG_MAXMSGLEN 4095
81 /* Exitcodes passed to exit() call */
82 #define MOONBR_EXITCODE_GRACEFUL 0
83 #define MOONBR_EXITCODE_CMDLINEERROR 1
84 #define MOONBR_EXITCODE_ALREADYRUNNING 2
85 #define MOONBR_EXITCODE_STARTUPERROR 3
86 #define MOONBR_EXITCODE_RUNTIMEERROR 4
88 /* Maximum length of a line sent to stderr by child processes */
89 #define MOONBR_MAXERRORLINELEN 1024
91 /* Maximum length of an error string returned by strerror() */
92 #define MOONBR_MAXSTRERRORLEN 80
94 /* Status bytes exchanged between master and child processes */
95 #define MOONBR_SOCKETTYPE_INTERVAL 'I'
96 #define MOONBR_SOCKETTYPE_LOCAL 'L'
97 #define MOONBR_SOCKETTYPE_NETWORK 'N'
98 #define MOONBR_STATUS_IDLE '1'
99 #define MOONBR_COMMAND_TERMINATE '2'
100 #define MOONBR_STATUS_GOODBYE '3'
102 /* Constant file descriptors */
103 #define MOONBR_FD_STDERR 2
104 #define MOONBR_FD_CONTROL 3
105 #define MOONBR_FD_END 4
107 /* Return values of moonbr_try_destroy_worker() */
108 #define MOONBR_DESTROY_NONE 0
109 #define MOONBR_DESTROY_PREPARE 1
110 #define MOONBR_DESTROY_IDLE_OR_ASSIGNED 2
113 /*** Types ***/
115 /* Enum for 'moonbr_pstate' */
116 #define MOONBR_PSTATE_STARTUP 0
117 #define MOONBR_PSTATE_RUNNING 1
118 #define MOONBR_PSTATE_FORKED 2
120 /* Enum for 'proto' field of struct moonbr_listener */
121 #define MOONBR_PROTO_INTERVAL 1
122 #define MOONBR_PROTO_LOCAL 2
123 #define MOONBR_PROTO_TCP6 3
124 #define MOONBR_PROTO_TCP4 4
126 /* Data structure for a pool's listener that can accept incoming connections */
127 struct moonbr_listener {
128 struct moonbr_pool *pool;
129 struct moonbr_listener *prev_listener; /* previous idle or(!) connected listener */
130 struct moonbr_listener *next_listener; /* next idle or(!) connected listener */
131 int proto;
132 union {
133 struct {
134 char *name; /* name of interval passed to 'connect' function as 'interval' field in table */
135 int strict; /* nonzero = runtime of 'connect' function does not delay interval */
136 struct timeval delay; /* interval between invocations of 'connect' function */
137 struct timeval wakeup; /* point in time of next invocation */
138 } interval;
139 struct {
140 char *path; /* full path name (i.e. filename with path) of UNIX domain socket */
141 } local;
142 struct {
143 int port; /* port number to listen on (in host endianess) */
144 int localhost_only; /* nonzero = listen on localhost only */
145 } tcp;
146 } proto_specific;
147 int listenfd; /* -1 = none */
148 int pollidx; /* -1 = none */
149 };
151 /* Data structure for a child process that is handling incoming connections */
152 struct moonbr_worker {
153 struct moonbr_pool *pool;
154 struct moonbr_worker *prev_worker;
155 struct moonbr_worker *next_worker;
156 struct moonbr_worker *prev_idle_worker;
157 struct moonbr_worker *next_idle_worker;
158 int idle; /* nonzero = waiting for command from parent process */
159 int assigned; /* nonzero = currently handling a connection */
160 pid_t pid;
161 int controlfd; /* socket to send/receive control message to/from child process */
162 int errorfd; /* socket to receive error output from child process' stderr */
163 char *errorlinebuf; /* optional buffer for collecting stderr data from child process */
164 int errorlinelen; /* number of bytes stored in 'errorlinebuf' */
165 int errorlineovf; /* nonzero = line length overflow */
166 struct timeval idle_expiration; /* point in time until child process may stay in idle state */
167 struct moonbr_listener *restart_interval_listener; /* set while interval listener is assigned */
168 };
170 /* Data structure for a pool of workers and listeners */
171 struct moonbr_pool {
172 int poolnum; /* number of pool for log output */
173 struct moonbr_pool *next_pool; /* next entry in linked list starting with 'moonbr_first_pool' */
174 struct moonbr_worker *first_worker; /* first worker of pool */
175 struct moonbr_worker *last_worker; /* last worker of pool */
176 struct moonbr_worker *first_idle_worker; /* first idle worker of pool */
177 struct moonbr_worker *last_idle_worker; /* last idle worker of pool */
178 int idle_worker_count;
179 int unassigned_worker_count;
180 int total_worker_count;
181 int worker_count_stat; /* only needed for statistics */
182 int pre_fork; /* desired minimum number of unassigned workers */
183 int min_fork; /* desired minimum number of workers in total */
184 int max_fork; /* maximum number of workers */
185 struct timeval fork_delay; /* delay after each fork() until a fork may happen again */
186 struct timeval fork_wakeup; /* point in time when a fork may happen again (unless a worker terminates before) */
187 struct timeval fork_error_delay; /* delay between fork()s when an error during fork or preparation occurred */
188 struct timeval fork_error_wakeup; /* point in time when fork may happen again if an error in preparation occurred */
189 int use_fork_error_wakeup; /* nonzero = error in preparation occured; gets reset on next fork */
190 struct timeval exit_delay; /* delay for terminating excessive workers (unassigned_worker_count > pre_fork) */
191 struct timeval exit_wakeup; /* point in time when terminating an excessive worker */
192 struct timeval idle_timeout; /* delay before an idle worker is terminated */
193 size_t memory_limit; /* maximum bytes of memory that the Lua machine may allocate */
194 int listener_count; /* total number of listeners of pool (and size of 'listener' array at end of this struct) */
195 struct moonbr_listener *first_idle_listener; /* first listener that is idle (i.e. has no waiting connection) */
196 struct moonbr_listener *last_idle_listener; /* last listener that is idle (i.e. has no waiting connection) */
197 struct moonbr_listener *first_connected_listener; /* first listener that has a pending connection */
198 struct moonbr_listener *last_connected_listener; /* last listener that has a pending connection */
199 struct moonbr_listener listener[1]; /* static array of variable(!) size to contain 'listener' structures */
200 };
202 /* Enum for 'channel' field of struct moonbr_poll_worker */
203 #define MOONBR_POLL_WORKER_CONTROLCHANNEL 1
204 #define MOONBR_POLL_WORKER_ERRORCHANNEL 2
206 /* Structure to refer from 'moonbr_poll_worker_fds' entry to worker structure */
207 struct moonbr_poll_worker {
208 struct moonbr_worker *worker;
209 int channel; /* field indicating whether file descriptor is 'controlfd' or 'errorfd' */
210 };
212 /* Variable indicating that clean shutdown was requested */
213 static int moonbr_shutdown_in_progress = 0;
216 /*** Macros for Lua registry ***/
218 /* Lightuserdata keys for Lua registry to store 'prepare', 'connect', and 'finish' functions */
219 #define moonbr_luakey_prepare_func(pool) ((void *)(intptr_t)(pool) + 0)
220 #define moonbr_luakey_connect_func(pool) ((void *)(intptr_t)(pool) + 1)
221 #define moonbr_luakey_finish_func(pool) ((void *)(intptr_t)(pool) + 2)
224 /*** Global variables ***/
226 /* State of process execution */
227 static int moonbr_pstate = MOONBR_PSTATE_STARTUP;
229 /* Process ID of the main process */
230 static pid_t moonbr_masterpid;
232 /* Condition variables set by the signal handler */
233 static volatile sig_atomic_t moonbr_cond_poll = 0;
234 static volatile sig_atomic_t moonbr_cond_terminate = 0;
235 static volatile sig_atomic_t moonbr_cond_interrupt = 0;
236 static volatile sig_atomic_t moonbr_cond_child = 0;
238 /* Socket pair to denote signal delivery when signal handler was called just before poll() */
239 static int moonbr_poll_signalfds[2];
240 #define moonbr_poll_signalfd_read moonbr_poll_signalfds[0]
241 #define moonbr_poll_signalfd_write moonbr_poll_signalfds[1]
243 /* Global variables for pidfile and logging */
244 static struct pidfh *moonbr_pidfh = NULL;
245 static FILE *moonbr_logfile = NULL;
246 static int moonbr_use_syslog = 0;
248 /* First and last entry of linked list of all created pools during initialization */
249 static struct moonbr_pool *moonbr_first_pool = NULL;
250 static struct moonbr_pool *moonbr_last_pool = NULL;
252 /* Total count of pools */
253 static int moonbr_pool_count = 0;
255 /* Set to a nonzero value if dynamic part of 'moonbr_poll_fds' ('moonbr_poll_worker_fds') needs an update */
256 static int moonbr_poll_refresh_needed = 0;
258 /* Array passed to poll(), consisting of static part and dynamic part ('moonbr_poll_worker_fds') */
259 static struct pollfd *moonbr_poll_fds = NULL; /* the array */
260 static int moonbr_poll_fds_bufsize = 0; /* memory allocated for this number of elements */
261 static int moonbr_poll_fds_count = 0; /* total number of elements */
262 static int moonbr_poll_fds_static_count; /* number of elements in static part */
264 /* Dynamic part of 'moonbr_poll_fds' array */
265 #define moonbr_poll_worker_fds (moonbr_poll_fds+moonbr_poll_fds_static_count)
267 /* Additional information for dynamic part of 'moonbr_poll_fds' array */
268 struct moonbr_poll_worker *moonbr_poll_workers; /* the array */
269 static int moonbr_poll_workers_bufsize = 0; /* memory allocated for this number of elements */
270 static int moonbr_poll_worker_count = 0; /* number of elements in array */
272 /* Variable set to nonzero value to disallow further calls of 'listen' function */
273 static int moonbr_booted = 0;
275 /* Global variables to store information on connection socket in child process */
276 static int moonbr_child_peersocket_type; /* type of socket by MOONBR_SOCKETTYPE constant */
277 static int moonbr_child_peersocket_fd; /* Original file descriptor of peer socket */
278 static luaL_Stream *moonbr_child_peersocket_inputstream; /* Lua input stream of socket */
279 static luaL_Stream *moonbr_child_peersocket_outputstream; /* Lua output stream of socket */
281 /* Verbosity settings */
282 static int moonbr_debug = 0;
283 static int moonbr_stat = 0;
285 /* Memory consumption by Lua machine */
286 static size_t moonbr_memory_usage = 0;
287 static size_t moonbr_memory_limit = 0;
290 /*** Functions for signal handling ***/
292 /* Signal handler for master and child processes */
293 static void moonbr_signal(int sig) {
294 if (getpid() == moonbr_masterpid) {
295 /* master process */
296 switch (sig) {
297 case SIGHUP:
298 case SIGINT:
299 /* fast shutdown requested */
300 moonbr_cond_interrupt = 1;
301 break;
302 case SIGTERM:
303 /* clean shutdown requested */
304 moonbr_cond_terminate = 1;
305 break;
306 case SIGCHLD:
307 /* child process terminated */
308 moonbr_cond_child = 1;
309 break;
310 }
311 if (moonbr_cond_poll) {
312 /* avoid race condition if signal handler is invoked right before poll() */
313 char buf[1] = {0};
314 write(moonbr_poll_signalfd_write, buf, 1);
315 }
316 } else {
317 /* child process forwards certain signals to parent process */
318 switch (sig) {
319 case SIGHUP:
320 case SIGINT:
321 case SIGTERM:
322 kill(moonbr_masterpid, sig);
323 }
324 }
325 }
327 /* Initialize signal handling */
328 static void moonbr_signal_init(){
329 moonbr_masterpid = getpid();
330 signal(SIGHUP, moonbr_signal);
331 signal(SIGINT, moonbr_signal);
332 signal(SIGTERM, moonbr_signal);
333 signal(SIGCHLD, moonbr_signal);
334 signal(SIGPIPE, SIG_IGN); /* generate I/O errors instead of signal 13 */
335 }
338 /*** Functions for logging in master process ***/
340 /* Logs a pre-formatted message with given syslog() priority */
341 static void moonbr_log_msg(int priority, const char *msg) {
342 if (moonbr_logfile) {
343 /* logging to logfile desired (timestamp is prepended in that case) */
344 time_t now_time = 0;
345 struct tm now_tmstruct;
346 char timestr[MOONBR_LOG_MAXTIMELEN+1];
347 time(&now_time);
348 localtime_r(&now_time, &now_tmstruct);
349 if (!strftime(
350 timestr, MOONBR_LOG_MAXTIMELEN+1, "%Y-%m-%d %H:%M:%S %Z: ", &now_tmstruct
351 )) timestr[0] = 0;
352 fprintf(moonbr_logfile, "%s%s\n", timestr, msg);
353 }
354 if (moonbr_use_syslog) {
355 /* logging through syslog desired */
356 syslog(priority, "%s", msg);
357 }
358 }
360 /* Formats a message via vsnprintf() and logs it with given syslog() priority */
361 static void moonbr_log(int priority, const char *message, ...) {
362 char msgbuf[MOONBR_LOG_MAXMSGLEN+1]; /* buffer of static size to store formatted message */
363 int msglen; /* length of full message (may exceed MOONBR_LOG_MAXMSGLEN) */
364 {
365 /* pass variable arguments to vsnprintf() to format message */
366 va_list ap;
367 va_start(ap, message);
368 msglen = vsnprintf(msgbuf, MOONBR_LOG_MAXMSGLEN+1, message, ap);
369 va_end(ap);
370 }
371 {
372 /* split and log message line by line */
373 char *line = msgbuf;
374 while (1) {
375 char *endptr = strchr(line, '\n');
376 if (endptr) {
377 /* terminate string where newline character is found */
378 *endptr = 0;
379 } else if (line != msgbuf && msglen > MOONBR_LOG_MAXMSGLEN) {
380 /* break if line is incomplete and not the first line */
381 break;
382 }
383 moonbr_log_msg(priority, line);
384 if (!endptr) break; /* break if end of formatted message is reached */
385 line = endptr+1; /* otherwise continue with remaining message */
386 }
387 }
388 if (msglen > MOONBR_LOG_MAXMSGLEN) {
389 /* print warning if message was truncated */
390 moonbr_log_msg(priority, "Previous log message has been truncated due to excessive length");
391 }
392 }
395 /*** Termination function ***/
397 /* Kill all child processes, remove PID file (if existent), and exit master process with given exitcode */
398 static void moonbr_terminate(int exitcode) {
399 {
400 struct moonbr_pool *pool;
401 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
402 {
403 struct moonbr_worker *worker;
404 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
405 moonbr_log(LOG_INFO, "Sending SIGKILL to child with PID %i", (int)worker->pid);
406 if (kill(worker->pid, SIGKILL)) {
407 moonbr_log(LOG_ERR, "Error while killing child process: %s", strerror(errno));
408 }
409 }
410 }
411 {
412 int i;
413 for (i=0; i<pool->listener_count; i++) {
414 struct moonbr_listener *listener = &pool->listener[i];
415 if (listener->proto == MOONBR_PROTO_LOCAL) {
416 moonbr_log(LOG_INFO, "Unlinking local socket \"%s\"", listener->proto_specific.local.path);
417 if (unlink(listener->proto_specific.local.path)) {
418 moonbr_log(LOG_ERR, "Error while unlinking local socket: %s", strerror(errno));
419 }
420 }
421 }
422 }
423 }
424 }
425 moonbr_log(exitcode ? LOG_ERR : LOG_NOTICE, "Terminating with exit code %i", exitcode);
426 if (moonbr_pidfh && pidfile_remove(moonbr_pidfh)) {
427 moonbr_log(LOG_ERR, "Error while removing PID file: %s", strerror(errno));
428 }
429 exit(exitcode);
430 }
432 /* Terminate with either MOONBR_EXITCODE_STARTUPERROR or MOONBR_EXITCODE_RUNTIMEERROR */
433 #define moonbr_terminate_error() \
434 moonbr_terminate( \
435 moonbr_pstate == MOONBR_PSTATE_STARTUP ? \
436 MOONBR_EXITCODE_STARTUPERROR : \
437 MOONBR_EXITCODE_RUNTIMEERROR \
438 )
441 /*** Helper functions ***/
443 /* Fills a 'struct timeval' structure with the current time (using CLOCK_MONOTONIC) */
444 static void moonbr_now(struct timeval *now) {
445 struct timespec ts = {0, };
446 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
447 moonbr_log(LOG_CRIT, "Error in clock_gettime() call: %s", strerror(errno));
448 moonbr_terminate_error();
449 }
450 *now = (struct timeval){ .tv_sec = ts.tv_sec, .tv_usec = ts.tv_nsec / 1000 };
451 }
453 /* Formats a 'struct timeval' value (not thread-safe) */
454 static char *moonbr_format_timeval(struct timeval *t) {
455 static char buf[32];
456 snprintf(buf, 32, "%ji.%06ji seconds", (intmax_t)t->tv_sec, (intmax_t)t->tv_usec);
457 return buf;
458 }
461 /*** Functions for pool creation and startup ***/
463 /* Creates a 'struct moonbr_pool' structure with a given number of listeners */
464 static struct moonbr_pool *moonbr_create_pool(int listener_count) {
465 struct moonbr_pool *pool;
466 pool = calloc(1,
467 sizeof(struct moonbr_pool) + /* size of 'struct moonbr_pool' with one listener */
468 (listener_count-1) * sizeof(struct moonbr_listener) /* size of extra listeners */
469 );
470 if (!pool) {
471 moonbr_log(LOG_CRIT, "Memory allocation error");
472 moonbr_terminate_error();
473 }
474 pool->listener_count = listener_count;
475 {
476 /* initialization of listeners */
477 int i;
478 for (i=0; i<listener_count; i++) {
479 struct moonbr_listener *listener = &pool->listener[i];
480 listener->pool = pool;
481 listener->listenfd = -1;
482 listener->pollidx = -1;
483 }
484 }
485 return pool;
486 }
488 /* Destroys a 'struct moonbr_pool' structure before it has been started */
489 static void moonbr_destroy_pool(struct moonbr_pool *pool) {
490 int i;
491 for (i=0; i<pool->listener_count; i++) {
492 struct moonbr_listener *listener = &pool->listener[i];
493 if (
494 listener->proto == MOONBR_PROTO_INTERVAL &&
495 listener->proto_specific.interval.name
496 ) {
497 free(listener->proto_specific.interval.name);
498 }
499 if (
500 listener->proto == MOONBR_PROTO_LOCAL &&
501 listener->proto_specific.local.path
502 ) {
503 free(listener->proto_specific.local.path);
504 }
505 }
506 free(pool);
507 }
509 /* Starts a all listeners in a pool */
510 static int moonbr_start_pool(struct moonbr_pool *pool) {
511 moonbr_log(LOG_INFO, "Creating pool", pool->poolnum);
512 {
513 int i;
514 for (i=0; i<pool->listener_count; i++) {
515 struct moonbr_listener *listener = &pool->listener[i];
516 switch (listener->proto) {
517 case MOONBR_PROTO_INTERVAL:
518 /* nothing to do here: starting intervals is performed in moonbr_run() function */
519 if (!listener->proto_specific.interval.name) {
520 moonbr_log(LOG_INFO, "Adding unnamed interval listener");
521 } else {
522 moonbr_log(LOG_INFO, "Adding interval listener \"%s\"", listener->proto_specific.interval.name);
523 }
524 break;
525 case MOONBR_PROTO_LOCAL:
526 moonbr_log(LOG_INFO, "Adding local socket listener for path \"%s\"", listener->proto_specific.local.path);
527 {
528 struct sockaddr_un servaddr = { .sun_family = AF_UNIX };
529 const int path_maxlen = sizeof(struct sockaddr_un) - (
530 (void *)&servaddr.sun_path - (void *)&servaddr
531 );
532 if (
533 snprintf(
534 servaddr.sun_path,
535 path_maxlen,
536 "%s",
537 listener->proto_specific.local.path
538 ) >= path_maxlen
539 ) {
540 errno = ENAMETOOLONG;
541 };
542 listener->listenfd = socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
543 if (listener->listenfd == -1) goto moonbr_start_pool_error;
544 if (!unlink(listener->proto_specific.local.path)) {
545 moonbr_log(LOG_WARNING, "Unlinked named socket \"%s\" prior to listening", listener->proto_specific.local.path);
546 } else {
547 if (errno != ENOENT) {
548 moonbr_log(LOG_ERR, "Could not unlink named socket \"%s\" prior to listening: %s", listener->proto_specific.local.path, strerror(errno));
549 }
550 }
551 if (
552 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
553 ) goto moonbr_start_pool_error;
554 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
555 }
556 break;
557 case MOONBR_PROTO_TCP6:
558 if (listener->proto_specific.tcp.localhost_only) {
559 moonbr_log(LOG_INFO, "Adding localhost TCP/IPv6 listener on port %i", listener->proto_specific.tcp.port);
560 } else {
561 moonbr_log(LOG_INFO, "Adding public TCP/IPv6 listener on port %i", listener->proto_specific.tcp.port);
562 }
563 {
564 struct sockaddr_in6 servaddr = {
565 .sin6_family = AF_INET6,
566 .sin6_port = htons(listener->proto_specific.tcp.port)
567 };
568 listener->listenfd = socket(PF_INET6, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
569 if (listener->listenfd == -1) goto moonbr_start_pool_error;
570 {
571 /* avoid "Address already in use" error when restarting service */
572 static const int reuseval = 1;
573 if (setsockopt(
574 listener->listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval)
575 )) goto moonbr_start_pool_error;
576 }
577 {
578 /* default to send TCP RST when process terminates unexpectedly */
579 static const struct linger lingerval = {
580 .l_onoff = 1,
581 .l_linger = 0
582 };
583 if (setsockopt(
584 listener->listenfd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval)
585 )) goto moonbr_start_pool_error;
586 }
587 if (listener->proto_specific.tcp.localhost_only) {
588 servaddr.sin6_addr.s6_addr[15] = 1;
589 }
590 if (
591 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
592 ) goto moonbr_start_pool_error;
593 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
594 }
595 break;
596 case MOONBR_PROTO_TCP4:
597 if (listener->proto_specific.tcp.localhost_only) {
598 moonbr_log(LOG_INFO, "Adding localhost TCP/IPv4 listener on port %i", listener->proto_specific.tcp.port);
599 } else {
600 moonbr_log(LOG_INFO, "Adding public TCP/IPv4 listener on port %i", listener->proto_specific.tcp.port);
601 }
602 {
603 struct sockaddr_in servaddr = {
604 .sin_family = AF_INET,
605 .sin_port = htons(listener->proto_specific.tcp.port)
606 };
607 listener->listenfd = socket(PF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
608 if (listener->listenfd == -1) goto moonbr_start_pool_error;
609 {
610 /* avoid "Address already in use" error when restarting service */
611 static const int reuseval = 1;
612 if (setsockopt(
613 listener->listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseval, sizeof(reuseval)
614 )) goto moonbr_start_pool_error;
615 }
616 {
617 /* default to send TCP RST when process terminates unexpectedly */
618 static const struct linger lingerval = {
619 .l_onoff = 1,
620 .l_linger = 0
621 };
622 if (setsockopt(
623 listener->listenfd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval)
624 )) goto moonbr_start_pool_error;
625 }
626 if (listener->proto_specific.tcp.localhost_only) {
627 ((uint8_t *)&servaddr.sin_addr.s_addr)[0] = 127;
628 ((uint8_t *)&servaddr.sin_addr.s_addr)[3] = 1;
629 }
630 if (
631 bind(listener->listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr))
632 ) goto moonbr_start_pool_error;
633 if (listen(listener->listenfd, MOONBR_LISTEN_BACKLOG)) goto moonbr_start_pool_error;
634 }
635 break;
636 default:
637 moonbr_log(LOG_CRIT, "Internal error (should not happen): Unexpected value in listener.proto field");
638 moonbr_terminate_error();
639 }
640 }
641 goto moonbr_start_pool_ok;
642 moonbr_start_pool_error:
643 {
644 int j = i;
645 int errno2 = errno;
646 for (; i>=0; i--) {
647 struct moonbr_listener *listener = &pool->listener[i];
648 if (listener->listenfd != -1) close(listener->listenfd);
649 }
650 errno = errno2;
651 return j;
652 }
653 }
654 moonbr_start_pool_ok:
655 pool->poolnum = ++moonbr_pool_count;
656 moonbr_log(LOG_INFO, "Pool #%i created", pool->poolnum);
657 if (moonbr_last_pool) moonbr_last_pool->next_pool = pool;
658 else moonbr_first_pool = pool;
659 moonbr_last_pool = pool;
660 return -1;
661 }
664 /*** Function to send data and a file descriptor to child process */
666 /* Sends control message of one bye plus optional file descriptor plus optional pointer to child process */
667 static void moonbr_send_control_message(struct moonbr_worker *worker, char status, int fd, void *ptr) {
668 {
669 struct iovec iovector = { .iov_base = &status, .iov_len = 1 }; /* carrying status byte */
670 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to transfer file descriptor */
671 struct msghdr message = { .msg_iov = &iovector, .msg_iovlen = 1 }; /* data structure passed to sendmsg() call */
672 if (moonbr_debug) {
673 if (fd == -1) {
674 moonbr_log(LOG_DEBUG, "Sending control message \"%c\" to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
675 } else {
676 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);
677 }
678 }
679 if (fd != -1) {
680 /* attach control message with file descriptor */
681 message.msg_control = control_message_buffer;
682 message.msg_controllen = CMSG_SPACE(sizeof(int));
683 {
684 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
685 control_message->cmsg_level = SOL_SOCKET;
686 control_message->cmsg_type = SCM_RIGHTS;
687 control_message->cmsg_len = CMSG_LEN(sizeof(int));
688 memcpy(CMSG_DATA(control_message), &fd, sizeof(int));
689 }
690 }
691 while (sendmsg(worker->controlfd, &message, MSG_NOSIGNAL) < 0) {
692 if (errno == EPIPE) {
693 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));
694 return; /* do not close socket; socket is closed when reading from it */
695 }
696 if (errno != EINTR) {
697 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));
698 moonbr_terminate_error();
699 }
700 }
701 }
702 if (ptr) {
703 char buf[sizeof(void *)];
704 char *pos = buf;
705 int len = sizeof(void *);
706 ssize_t written;
707 if (moonbr_debug) {
708 moonbr_log(LOG_DEBUG, "Sending memory pointer to child process in pool #%i (PID %i)", (int)status, worker->pool->poolnum, (int)worker->pid);
709 }
710 memcpy(buf, &ptr, sizeof(void *));
711 while (len) {
712 written = send(worker->controlfd, pos, len, MSG_NOSIGNAL);
713 if (written > 0) {
714 pos += written;
715 len -= written;
716 } else if (errno == EPIPE) {
717 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));
718 return; /* do not close socket; socket is closed when reading from it */
719 } else if (errno != EINTR) {
720 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));
721 moonbr_terminate_error();
722 }
723 }
724 }
725 }
728 /*** Functions running in child process ***/
730 /* Logs an error in child process */
731 static void moonbr_child_log(const char *message) {
732 fprintf(stderr, "%s\n", message);
733 }
735 /* Logs a fatal error in child process and terminates process with error status */
736 static void moonbr_child_log_fatal(const char *message) {
737 moonbr_child_log(message);
738 exit(1);
739 }
741 /* Logs an error in child process while appending error string for global errno variable */
742 static void moonbr_child_log_errno(const char *message) {
743 char errmsg[MOONBR_MAXSTRERRORLEN];
744 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
745 fprintf(stderr, "%s: %s\n", message, errmsg);
746 }
748 /* Logs a fatal error in child process while appending error string for errno and terminating process */
749 static void moonbr_child_log_errno_fatal(const char *message) {
750 moonbr_child_log_errno(message);
751 exit(1);
752 }
754 /* Receives a control message consisting of one character plus an optional file descriptor from parent process */
755 static void moonbr_child_receive_control_message(int socketfd, char *status, int *fd) {
756 struct iovec iovector = { .iov_base = status, .iov_len = 1 }; /* reference to status byte variable */
757 char control_message_buffer[CMSG_SPACE(sizeof(int))] = {0, }; /* used to receive file descriptor */
758 struct msghdr message = { /* data structure passed to recvmsg() call */
759 .msg_iov = &iovector,
760 .msg_iovlen = 1,
761 .msg_control = control_message_buffer,
762 .msg_controllen = CMSG_SPACE(sizeof(int))
763 };
764 {
765 int received;
766 while ((received = recvmsg(socketfd, &message, MSG_CMSG_CLOEXEC)) < 0) {
767 if (errno != EINTR) {
768 moonbr_child_log_errno_fatal("Error while trying to receive connection socket from parent process");
769 }
770 }
771 if (!received) {
772 moonbr_child_log_fatal("Unexpected EOF while trying to receive connection socket from parent process");
773 }
774 }
775 {
776 struct cmsghdr *control_message = CMSG_FIRSTHDR(&message);
777 if (control_message) {
778 if (control_message->cmsg_level != SOL_SOCKET) {
779 moonbr_child_log_fatal("Received control message with cmsg_level not equal to SOL_SOCKET");
780 }
781 if (control_message->cmsg_type != SCM_RIGHTS) {
782 moonbr_child_log_fatal("Received control message with cmsg_type not equal to SCM_RIGHTS");
783 }
784 memcpy(fd, CMSG_DATA(control_message), sizeof(int));
785 } else {
786 *fd = -1;
787 }
788 }
789 }
791 /* Receives a pointer from parent process */
792 static void *moonbr_child_receive_pointer(int socketfd) {
793 char buf[sizeof(void *)];
794 char *pos = buf;
795 int len = sizeof(void *);
796 ssize_t bytes_read;
797 while (len) {
798 bytes_read = recv(socketfd, pos, len, 0);
799 if (bytes_read > 0) {
800 pos += bytes_read;
801 len -= bytes_read;
802 } else if (!bytes_read) {
803 moonbr_child_log_fatal("Unexpected EOF while trying to receive memory pointer from parent process");
804 } else if (errno != EINTR) {
805 moonbr_child_log_errno_fatal("Error while trying to receive memory pointer from parent process");
806 }
807 }
808 {
809 void *ptr; /* avoid breaking strict-aliasing rules */
810 memcpy(&ptr, buf, sizeof(void *));
811 return ptr;
812 }
813 }
815 /* Closes the input stream from peer unless it has already been closed */
816 static int moonbr_child_close_peersocket_inputstream(
817 int cleanshut, /* nonzero = use shutdown() if applicable */
818 int mark /* nonzero = mark the stream as closed for Lua */
819 ) {
820 int err = 0; /* nonzero = error occurred */
821 int errno2; /* stores previous errno values that take precedence */
822 if (moonbr_child_peersocket_inputstream->f) {
823 /* NOTE: shutdown() with SHUT_RD shows different behavior on different
824 * operating systems and particularly causes problems with Linux. Hence it
825 * is disabled here. */
826 /*
827 if (cleanshut && moonbr_child_peersocket_type == MOONBR_SOCKETTYPE_NETWORK) {
828 if (shutdown(moonbr_child_peersocket_fd, SHUT_RD)) {
829 errno2 = errno;
830 err = -1;
831 }
832 }
833 */
834 if (fclose(moonbr_child_peersocket_inputstream->f)) {
835 if (!err) errno2 = errno;
836 err = -1;
837 }
838 moonbr_child_peersocket_inputstream->f = NULL;
839 }
840 if (mark) moonbr_child_peersocket_inputstream->closef = NULL;
841 if (err) errno = errno2;
842 return err;
843 }
845 /* Closes the output stream to peer unless it has already been closed */
846 static int moonbr_child_close_peersocket_outputstream(
847 int cleanshut, /* nonzero = use fflush() and shutdown() if applicable */
848 int mark /* nonzero = mark the stream as closed for Lua */
849 ) {
850 int err = 0; /* nonzero = error occurred */
851 int errno2; /* stores previous errno values that take precedence */
852 if (moonbr_child_peersocket_outputstream->f) {
853 if (moonbr_child_peersocket_type == MOONBR_SOCKETTYPE_NETWORK) {
854 if (cleanshut) {
855 if (fflush(moonbr_child_peersocket_outputstream->f)) {
856 errno2 = errno;
857 err = -1;
858 } else {
859 if (shutdown(moonbr_child_peersocket_fd, SHUT_WR)) {
860 errno2 = errno;
861 err = -1;
862 }
863 }
864 } else {
865 fpurge(moonbr_child_peersocket_outputstream->f);
866 }
867 }
868 if (fclose(moonbr_child_peersocket_outputstream->f)) {
869 if (!err) errno2 = errno;
870 err = -1;
871 }
872 moonbr_child_peersocket_outputstream->f = NULL;
873 }
874 if (mark) moonbr_child_peersocket_outputstream->closef = NULL;
875 if (err) errno = errno2;
876 return err;
877 }
879 /* Perform a clean shutdown of input and output stream (may be called multiple times) */
880 static int moonbr_child_close_peersocket(int timeout) {
881 int errprio = 0;
882 int errno2;
883 if (moonbr_child_peersocket_fd == -1) return 0;
884 if (moonbr_child_close_peersocket_inputstream(1, 1)) {
885 errprio = 1;
886 errno2 = errno;
887 }
888 if (moonbr_child_close_peersocket_outputstream(1, 1)) {
889 errprio = 4;
890 errno2 = errno;
891 }
892 if (moonbr_child_peersocket_type == MOONBR_SOCKETTYPE_NETWORK) {
893 struct linger lingerval = { 0, };
894 if (timeout && !errprio) {
895 lingerval.l_onoff = 1;
896 lingerval.l_linger = timeout;
897 }
898 if (setsockopt(moonbr_child_peersocket_fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) {
899 if (errprio < 2) {
900 errprio = 2;
901 errno2 = errno;
902 }
903 }
904 }
905 if (close(moonbr_child_peersocket_fd)) {
906 if (errprio < 3) {
907 errprio = 3;
908 errno2 = errno;
909 }
910 }
911 moonbr_child_peersocket_fd = -1;
912 if (errprio) {
913 errno = errno2;
914 return -1;
915 }
916 return 0;
917 }
919 /* Close socket and cause reset of TCP connection (TCP RST aka "Connection reset by peer") if possible */
920 static int moonbr_child_cancel_peersocket() {
921 int err = 0;
922 if (moonbr_child_close_peersocket_inputstream(0, 1)) err = -1;
923 if (moonbr_child_close_peersocket_outputstream(0, 1)) err = -1;
924 if (close(moonbr_child_peersocket_fd)) err = -1;
925 moonbr_child_peersocket_fd = -1;
926 return err;
927 }
929 /* Lua method for socket object to read from input stream */
930 static int moonbr_child_lua_read_stream(lua_State *L) {
931 lua_getfield(L, 1, "input");
932 lua_getfield(L, -1, "read");
933 lua_insert(L, 1);
934 lua_replace(L, 2);
935 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
936 return lua_gettop(L);
937 }
939 /* Lua method for socket object to read from input stream using xread */
940 static int moonbr_child_lua_xread_stream(lua_State *L) {
941 lua_getfield(L, 1, "input");
942 lua_getfield(L, -1, "xread");
943 lua_insert(L, 1);
944 lua_replace(L, 2);
945 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
946 return lua_gettop(L);
947 }
949 /* Lua method for socket object to read from input stream using xread_nb */
950 static int moonbr_child_lua_xread_nb_stream(lua_State *L) {
951 lua_getfield(L, 1, "input");
952 lua_getfield(L, -1, "xread_nb");
953 lua_insert(L, 1);
954 lua_replace(L, 2);
955 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
956 return lua_gettop(L);
957 }
959 /* Lua method for socket object to iterate over input stream */
960 static int moonbr_child_lua_lines_stream(lua_State *L) {
961 lua_getfield(L, 1, "input");
962 lua_getfield(L, -1, "lines");
963 lua_insert(L, 1);
964 lua_replace(L, 2);
965 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
966 return lua_gettop(L);
967 }
969 /* Lua method for socket object to write to output stream */
970 static int moonbr_child_lua_write_stream(lua_State *L) {
971 lua_getfield(L, 1, "output");
972 lua_getfield(L, -1, "write");
973 lua_insert(L, 1);
974 lua_replace(L, 2);
975 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
976 return lua_gettop(L);
977 }
979 /* Lua method for socket object to write to output stream using write_nb */
980 static int moonbr_child_lua_write_nb_stream(lua_State *L) {
981 lua_getfield(L, 1, "input");
982 lua_getfield(L, -1, "write_nb");
983 lua_insert(L, 1);
984 lua_replace(L, 2);
985 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
986 return lua_gettop(L);
987 }
989 /* Lua method for socket object to flush the output stream */
990 static int moonbr_child_lua_flush_stream(lua_State *L) {
991 lua_getfield(L, 1, "output");
992 lua_getfield(L, -1, "flush");
993 lua_insert(L, 1);
994 lua_replace(L, 2);
995 lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
996 return lua_gettop(L);
997 }
999 /* Lua function to close a single stream (input or output) from/to peer */
1000 static int moonbr_child_lua_close_stream(lua_State *L) {
1001 luaL_Stream *stream = lua_touserdata(L, 1);
1002 if (stream == moonbr_child_peersocket_inputstream) {
1003 if (moonbr_child_close_peersocket_inputstream(1, 0)) { /* don't mark as closed as it's done by Lua */
1004 char errmsg[MOONBR_MAXSTRERRORLEN];
1005 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
1006 lua_pushnil(L);
1007 lua_pushfstring(L, "Could not close input stream from peer: %s", errmsg);
1008 return 2;
1010 } else if (stream == moonbr_child_peersocket_outputstream) {
1011 if (moonbr_child_close_peersocket_outputstream(1, 0)) { /* don't mark as closed as it's done by Lua */
1012 char errmsg[MOONBR_MAXSTRERRORLEN];
1013 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
1014 lua_pushnil(L);
1015 lua_pushfstring(L, "Could not close output stream to peer: %s", errmsg);
1016 return 2;
1018 } else {
1019 luaL_argerror(L, 1, "Not a connection socket");
1021 lua_pushboolean(L, 1);
1022 return 1;
1025 /* Lua function to close both input and output stream from/to peer */
1026 static int moonbr_child_lua_close_both_streams(lua_State *L) {
1027 int timeout = 0;
1028 if (!lua_isnoneornil(L, 2)) {
1029 lua_Integer n = luaL_checkinteger(L, 2);
1030 luaL_argcheck(L, n >= 0 && n <= INT_MAX, 2, "out of range");
1031 timeout = n;
1033 if (moonbr_child_peersocket_fd == -1) {
1034 luaL_error(L, "Connection with peer has already been explicitly closed");
1036 if (moonbr_child_close_peersocket(timeout)) {
1037 char errmsg[MOONBR_MAXSTRERRORLEN];
1038 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
1039 lua_pushnil(L);
1040 lua_pushfstring(L, "Could not close socket connection with peer: %s", errmsg);
1041 return 2;
1043 lua_pushboolean(L, 1);
1044 return 1;
1047 /* Lua function to close both input and output stream from/to peer */
1048 static int moonbr_child_lua_cancel_both_streams(lua_State *L) {
1049 if (moonbr_child_peersocket_fd == -1) {
1050 luaL_error(L, "Connection with peer has already been explicitly closed");
1052 if (moonbr_child_cancel_peersocket()) {
1053 char errmsg[MOONBR_MAXSTRERRORLEN];
1054 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
1055 lua_pushnil(L);
1056 lua_pushfstring(L, "Could not cancel socket connection with peer: %s", errmsg);
1057 return 2;
1059 lua_pushboolean(L, 1);
1060 return 1;
1063 /* Methods of (bidirectional) socket object passed to handler */
1064 static luaL_Reg moonbr_child_lua_socket_functions[] = {
1065 {"read", moonbr_child_lua_read_stream},
1066 {"xread", moonbr_child_lua_xread_stream},
1067 {"xread_nb", moonbr_child_lua_xread_nb_stream},
1068 {"lines", moonbr_child_lua_lines_stream},
1069 {"write", moonbr_child_lua_write_stream},
1070 {"write_nb", moonbr_child_lua_write_nb_stream},
1071 {"flush", moonbr_child_lua_flush_stream},
1072 {"close", moonbr_child_lua_close_both_streams},
1073 {"cancel", moonbr_child_lua_cancel_both_streams},
1074 {NULL, NULL}
1075 };
1077 /* Main function of child process to be called after fork() and file descriptor rearrangement */
1078 void moonbr_child_run(struct moonbr_pool *pool, lua_State *L) {
1079 char controlmsg;
1080 struct itimerval notimer = { { 0, }, { 0, } };
1081 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
1082 if (lua_isnil(L, -1)) lua_pop(L, 1);
1083 else if (lua_pcall(L, 0, 0, 1)) {
1084 fprintf(stderr, "Error in \"prepare\" function: %s\n", lua_tostring(L, -1));
1085 exit(1);
1087 while (1) {
1088 struct moonbr_listener *listener;
1089 if (setitimer(ITIMER_REAL, &notimer, NULL)) {
1090 moonbr_child_log_errno_fatal("Could not reset ITIMER_REAL via setitimer()");
1092 controlmsg = MOONBR_STATUS_IDLE;
1093 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
1094 moonbr_child_log_errno_fatal("Error while sending ready message to parent process");
1096 moonbr_child_receive_control_message(
1097 MOONBR_FD_CONTROL,
1098 &controlmsg,
1099 &moonbr_child_peersocket_fd
1100 );
1101 if (!(
1102 (controlmsg == MOONBR_COMMAND_TERMINATE && moonbr_child_peersocket_fd == -1) ||
1103 (controlmsg == MOONBR_SOCKETTYPE_INTERVAL && moonbr_child_peersocket_fd == -1) ||
1104 (controlmsg == MOONBR_SOCKETTYPE_LOCAL && moonbr_child_peersocket_fd != -1) ||
1105 (controlmsg == MOONBR_SOCKETTYPE_NETWORK && moonbr_child_peersocket_fd != -1)
1106 )) {
1107 moonbr_child_log_fatal("Received illegal control message from parent process");
1109 if (controlmsg == MOONBR_COMMAND_TERMINATE) break;
1110 listener = moonbr_child_receive_pointer(MOONBR_FD_CONTROL);
1111 moonbr_child_peersocket_type = controlmsg;
1112 if (moonbr_child_peersocket_fd != -1) {
1114 int clonedfd;
1115 clonedfd = dup(moonbr_child_peersocket_fd);
1116 if (!clonedfd) {
1117 moonbr_child_log_errno_fatal("Could not duplicate file descriptor for input stream");
1119 moonbr_child_peersocket_inputstream = lua_newuserdata(L, sizeof(luaL_Stream));
1120 if (!moonbr_child_peersocket_inputstream) {
1121 moonbr_child_log_fatal("Memory allocation error");
1123 moonbr_child_peersocket_inputstream->f = fdopen(clonedfd, "rb");
1124 if (!moonbr_child_peersocket_inputstream->f) {
1125 moonbr_child_log_errno_fatal("Could not open input stream for remote connection");
1127 moonbr_child_peersocket_inputstream->closef = moonbr_child_lua_close_stream;
1128 if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
1129 moonbr_child_log_fatal("Lua metatable LUA_FILEHANDLE does not exist");
1131 lua_setmetatable(L, -2);
1134 int clonedfd;
1135 clonedfd = dup(moonbr_child_peersocket_fd);
1136 if (!clonedfd) {
1137 moonbr_child_log_errno_fatal("Could not duplicate file descriptor for output stream");
1139 moonbr_child_peersocket_outputstream = lua_newuserdata(L, sizeof(luaL_Stream));
1140 if (!moonbr_child_peersocket_outputstream) {
1141 moonbr_child_log_fatal("Memory allocation error");
1143 moonbr_child_peersocket_outputstream->f = fdopen(clonedfd, "wb");
1144 if (!moonbr_child_peersocket_outputstream->f) {
1145 moonbr_child_log_errno_fatal("Could not open output stream for remote connection");
1147 moonbr_child_peersocket_outputstream->closef = moonbr_child_lua_close_stream;
1148 if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
1149 moonbr_child_log_fatal("Lua metatable LUA_FILEHANDLE does not exist");
1151 lua_setmetatable(L, -2);
1154 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
1155 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1156 lua_newtable(L);
1157 lua_pushstring(L,
1158 listener->proto_specific.interval.name ?
1159 listener->proto_specific.interval.name : ""
1160 );
1161 lua_setfield(L, -2, "interval");
1162 } else {
1163 lua_newtable(L);
1164 lua_pushvalue(L, -4);
1165 lua_setfield(L, -2, "input");
1166 lua_pushvalue(L, -3);
1167 lua_setfield(L, -2, "output");
1168 luaL_setfuncs(L, moonbr_child_lua_socket_functions, 0);
1169 if (listener->proto == MOONBR_PROTO_TCP6) {
1170 struct sockaddr_in6 addr;
1171 socklen_t addr_len = sizeof(struct sockaddr_in6);
1172 if (getsockname(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1173 moonbr_child_log_errno("Could not get local IP address/port");
1174 } else {
1175 lua_pushlstring(L, (char *)addr.sin6_addr.s6_addr, 16);
1176 lua_setfield(L, -2, "local_ip6");
1177 lua_pushinteger(L, ntohs(addr.sin6_port));
1178 lua_setfield(L, -2, "local_tcpport");
1180 if (getpeername(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1181 moonbr_child_log_errno("Could not get remote IP address/port");
1182 } else {
1183 lua_pushlstring(L, (char *)addr.sin6_addr.s6_addr, 16);
1184 lua_setfield(L, -2, "remote_ip6");
1185 lua_pushinteger(L, ntohs(addr.sin6_port));
1186 lua_setfield(L, -2, "remote_tcpport");
1188 } else if (listener->proto == MOONBR_PROTO_TCP4) {
1189 struct sockaddr_in addr;
1190 socklen_t addr_len = sizeof(struct sockaddr_in);
1191 if (getsockname(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1192 moonbr_child_log_errno("Could not get local IP address/port");
1193 } else {
1194 lua_pushlstring(L, (char *)&addr.sin_addr.s_addr, 4);
1195 lua_setfield(L, -2, "local_ip4");
1196 lua_pushinteger(L, ntohs(addr.sin_port));
1197 lua_setfield(L, -2, "local_tcpport");
1199 if (getpeername(moonbr_child_peersocket_fd, (struct sockaddr *)&addr, &addr_len)) {
1200 moonbr_child_log_errno("Could not get remote IP address/port");
1201 } else {
1202 lua_pushlstring(L, (char *)&addr.sin_addr.s_addr, 4);
1203 lua_setfield(L, -2, "remote_ip4");
1204 lua_pushinteger(L, ntohs(addr.sin_port));
1205 lua_setfield(L, -2, "remote_tcpport");
1209 if (lua_pcall(L, 1, 1, 1)) {
1210 fprintf(stderr, "Error in \"connect\" function: %s\n", lua_tostring(L, -1));
1211 exit(1);
1213 if (moonbr_child_close_peersocket(0)) {
1214 moonbr_child_log_errno("Could not close socket connection with peer");
1216 if (lua_type(L, -1) != LUA_TBOOLEAN || !lua_toboolean(L, -1)) break;
1217 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
1218 lua_settop(L, 2);
1219 #else
1220 lua_settop(L, 1);
1221 #endif
1223 controlmsg = MOONBR_STATUS_GOODBYE;
1224 if (write(MOONBR_FD_CONTROL, &controlmsg, 1) <= 0) {
1225 moonbr_child_log_errno_fatal("Error while sending goodbye message to parent process");
1227 if (close(MOONBR_FD_CONTROL) && errno != EINTR) {
1228 moonbr_child_log_errno("Error while closing control socket");
1230 lua_rawgetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
1231 if (lua_isnil(L, -1)) lua_pop(L, 1);
1232 else if (lua_pcall(L, 0, 0, 1)) {
1233 fprintf(stderr, "Error in \"finish\" function: %s\n", lua_tostring(L, -1));
1234 exit(1);
1236 lua_close(L);
1237 exit(0);
1241 /*** Functions to spawn child process ***/
1243 /* Helper function to send an error message to a file descriptor (not needing a file stream) */
1244 static void moonbr_child_emergency_print(int fd, char *message) {
1245 size_t len = strlen(message);
1246 ssize_t written;
1247 while (len) {
1248 written = write(fd, message, len);
1249 if (written > 0) {
1250 message += written;
1251 len -= written;
1252 } else {
1253 if (written != -1 || errno != EINTR) break;
1258 /* Helper function to send an error message plus a text for errno to a file descriptor and terminate the process */
1259 static void moonbr_child_emergency_error(int fd, char *message) {
1260 int errno2 = errno;
1261 moonbr_child_emergency_print(fd, message);
1262 moonbr_child_emergency_print(fd, ": ");
1263 moonbr_child_emergency_print(fd, strerror(errno2));
1264 moonbr_child_emergency_print(fd, "\n");
1265 exit(1);
1268 /* Creates a child process and (in case of success) registers it in the 'struct moonbr_pool' structure */
1269 static int moonbr_create_worker(struct moonbr_pool *pool, lua_State *L) {
1270 struct moonbr_worker *worker;
1271 worker = calloc(1, sizeof(struct moonbr_worker));
1272 if (!worker) {
1273 moonbr_log(LOG_CRIT, "Memory allocation error");
1274 return -1;
1276 worker->pool = pool;
1278 int controlfds[2];
1279 int errorfds[2];
1280 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, controlfds)) {
1281 moonbr_log(LOG_ERR, "Could not create control socket pair for communcation with child process: %s", strerror(errno));
1282 free(worker);
1283 return -1;
1285 if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, errorfds)) {
1286 moonbr_log(LOG_ERR, "Could not create socket pair to redirect stderr of child process: %s", strerror(errno));
1287 close(controlfds[0]);
1288 close(controlfds[1]);
1289 free(worker);
1290 return -1;
1292 if (moonbr_logfile && fflush(moonbr_logfile)) {
1293 moonbr_log(LOG_CRIT, "Could not flush log file prior to forking: %s", strerror(errno));
1294 moonbr_terminate_error();
1296 worker->pid = fork();
1297 if (worker->pid == -1) {
1298 moonbr_log(LOG_ERR, "Could not fork: %s", strerror(errno));
1299 close(controlfds[0]);
1300 close(controlfds[1]);
1301 close(errorfds[0]);
1302 close(errorfds[1]);
1303 free(worker);
1304 return -1;
1305 } else if (!worker->pid) {
1306 moonbr_pstate = MOONBR_PSTATE_FORKED;
1307 #ifdef MOONBR_LUA_PANIC_BUG_WORKAROUND
1308 lua_pushliteral(L, "Failed to pass error message due to bug in Lua panic handler (hint: not enough memory?)");
1309 #endif
1310 moonbr_memory_limit = pool->memory_limit;
1311 if (moonbr_pidfh && pidfile_close(moonbr_pidfh)) {
1312 moonbr_child_emergency_error(errorfds[1], "Could not close PID file in forked child process");
1314 if (moonbr_logfile && moonbr_logfile != stderr && fclose(moonbr_logfile)) {
1315 moonbr_child_emergency_error(errorfds[1], "Could not close log file in forked child process");
1317 if (dup2(errorfds[1], MOONBR_FD_STDERR) == -1) {
1318 moonbr_child_emergency_error(errorfds[1], "Could not duplicate socket to stderr file descriptor");
1320 if (dup2(controlfds[1], MOONBR_FD_CONTROL) == -1) {
1321 moonbr_child_emergency_error(errorfds[1], "Could not duplicate control socket");
1323 closefrom(MOONBR_FD_END);
1324 moonbr_child_run(pool, L);
1326 if (moonbr_stat) {
1327 moonbr_log(LOG_INFO, "Created new worker in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1329 worker->controlfd = controlfds[0];
1330 worker->errorfd = errorfds[0];
1331 if (close(controlfds[1]) && errno != EINTR) {
1332 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
1333 moonbr_terminate_error();
1335 if (close(errorfds[1]) && errno != EINTR) {
1336 moonbr_log(LOG_CRIT, "Could not close opposite end of control file descriptor after forking");
1337 moonbr_terminate_error();
1340 worker->prev_worker = pool->last_worker;
1341 if (worker->prev_worker) worker->prev_worker->next_worker = worker;
1342 else pool->first_worker = worker;
1343 pool->last_worker = worker;
1344 pool->unassigned_worker_count++;
1345 pool->total_worker_count++;
1346 pool->worker_count_stat = 1;
1347 moonbr_poll_refresh_needed = 1;
1348 return 0; /* return zero only in case of success */
1352 /*** Functions to handle previously created 'struct moonbr_worker' structures ***/
1354 #define moonbr_try_destroy_worker_stat(str, field) \
1355 moonbr_log(LOG_INFO, "Resource usage in pool #%i for PID %i: " str " %li", worker->pool->poolnum, (int)worker->pid, (long)childusage.field);
1357 /* Destroys a worker structure if socket connections have been closed and child process has terminated */
1358 static int moonbr_try_destroy_worker(struct moonbr_worker *worker) {
1359 if (worker->controlfd != -1 || worker->errorfd != -1) return MOONBR_DESTROY_NONE;
1361 int childstatus;
1362 struct rusage childusage;
1364 pid_t waitedpid;
1365 while (
1366 (waitedpid = wait4(worker->pid, &childstatus, WNOHANG, &childusage)) == -1
1367 ) {
1368 if (errno != EINTR) {
1369 moonbr_log(LOG_CRIT, "Error in wait4() call: %s", strerror(errno));
1370 moonbr_terminate_error();
1373 if (!waitedpid) return 0; /* return 0 if worker couldn't be destroyed */
1374 if (waitedpid != worker->pid) {
1375 moonbr_log(LOG_CRIT, "Wrong PID returned by wait4() call");
1376 moonbr_terminate_error();
1379 if (WIFEXITED(childstatus)) {
1380 if (WEXITSTATUS(childstatus) || moonbr_stat) {
1381 moonbr_log(
1382 WEXITSTATUS(childstatus) ? LOG_WARNING : LOG_INFO,
1383 "Child process in pool #%i with PID %i returned with exit code %i", worker->pool->poolnum, (int)worker->pid, WEXITSTATUS(childstatus)
1384 );
1386 } else if (WIFSIGNALED(childstatus)) {
1387 if (WCOREDUMP(childstatus)) {
1388 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));
1389 } else if (WTERMSIG(childstatus) == SIGALRM) {
1390 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i exited prematurely due to timeout", worker->pool->poolnum, (int)worker->pid);
1391 } else {
1392 moonbr_log(LOG_ERR, "Child process in pool #%i with PID %i died from signal %i", worker->pool->poolnum, (int)worker->pid, WTERMSIG(childstatus));
1394 } else {
1395 moonbr_log(LOG_CRIT, "Illegal exit status from child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1396 moonbr_terminate_error();
1398 if (moonbr_stat) {
1399 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));
1400 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));
1401 moonbr_try_destroy_worker_stat("max resident set size", ru_maxrss);
1402 moonbr_try_destroy_worker_stat("integral shared memory size", ru_ixrss);
1403 moonbr_try_destroy_worker_stat("integral unshared data", ru_idrss);
1404 moonbr_try_destroy_worker_stat("integral unshared stack", ru_isrss);
1405 moonbr_try_destroy_worker_stat("page replaims", ru_minflt);
1406 moonbr_try_destroy_worker_stat("page faults", ru_majflt);
1407 moonbr_try_destroy_worker_stat("swaps", ru_nswap);
1408 moonbr_try_destroy_worker_stat("block input operations", ru_inblock);
1409 moonbr_try_destroy_worker_stat("block output operations", ru_oublock);
1410 moonbr_try_destroy_worker_stat("messages sent", ru_msgsnd);
1411 moonbr_try_destroy_worker_stat("messages received", ru_msgrcv);
1412 moonbr_try_destroy_worker_stat("signals received", ru_nsignals);
1413 moonbr_try_destroy_worker_stat("voluntary context switches", ru_nvcsw);
1414 moonbr_try_destroy_worker_stat("involuntary context switches", ru_nivcsw);
1418 int retval = (
1419 (worker->idle || worker->assigned) ?
1420 MOONBR_DESTROY_IDLE_OR_ASSIGNED :
1421 MOONBR_DESTROY_PREPARE
1422 );
1423 if (worker->prev_worker) worker->prev_worker->next_worker = worker->next_worker;
1424 else worker->pool->first_worker = worker->next_worker;
1425 if (worker->next_worker) worker->next_worker->prev_worker = worker->prev_worker;
1426 else worker->pool->last_worker = worker->prev_worker;
1427 if (worker->idle) {
1428 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker->next_idle_worker;
1429 else worker->pool->first_idle_worker = worker->next_idle_worker;
1430 if (worker->next_idle_worker) worker->next_idle_worker->prev_idle_worker = worker->prev_idle_worker;
1431 else worker->pool->last_idle_worker = worker->prev_idle_worker;
1432 worker->pool->idle_worker_count--;
1434 if (!worker->assigned) worker->pool->unassigned_worker_count--;
1435 worker->pool->total_worker_count--;
1436 worker->pool->worker_count_stat = 1;
1437 if (worker->errorlinebuf) free(worker->errorlinebuf);
1438 free(worker);
1439 return retval;
1443 /* Marks a worker as idle and stores it in a queue, optionally setting 'idle_expiration' value */
1444 static void moonbr_add_idle_worker(struct moonbr_worker *worker) {
1445 worker->prev_idle_worker = worker->pool->last_idle_worker;
1446 if (worker->prev_idle_worker) worker->prev_idle_worker->next_idle_worker = worker;
1447 else worker->pool->first_idle_worker = worker;
1448 worker->pool->last_idle_worker = worker;
1449 worker->idle = 1;
1450 worker->pool->idle_worker_count++;
1451 if (worker->assigned) {
1452 worker->assigned = 0;
1453 worker->pool->unassigned_worker_count++;
1455 worker->pool->worker_count_stat = 1;
1456 if (timerisset(&worker->pool->idle_timeout)) {
1457 struct timeval now;
1458 moonbr_now(&now);
1459 timeradd(&now, &worker->pool->idle_timeout, &worker->idle_expiration);
1463 /* Pops a worker from the queue of idle workers (idle queue must not be empty) */
1464 static struct moonbr_worker *moonbr_pop_idle_worker(struct moonbr_pool *pool) {
1465 struct moonbr_worker *worker;
1466 worker = pool->first_idle_worker;
1467 pool->first_idle_worker = worker->next_idle_worker;
1468 if (pool->first_idle_worker) pool->first_idle_worker->prev_idle_worker = NULL;
1469 else pool->last_idle_worker = NULL;
1470 worker->next_idle_worker = NULL;
1471 worker->idle = 0;
1472 worker->pool->idle_worker_count--;
1473 worker->assigned = 1;
1474 worker->pool->unassigned_worker_count--;
1475 worker->pool->worker_count_stat = 1;
1476 return worker;
1480 /*** Functions for queues of 'struct moonbr_listener' ***/
1482 /* Appends a 'struct moonbr_listener' to the queue of idle listeners and registers it for poll() */
1483 static void moonbr_add_idle_listener(struct moonbr_listener *listener) {
1484 listener->prev_listener = listener->pool->last_idle_listener;
1485 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
1486 else listener->pool->first_idle_listener = listener;
1487 listener->pool->last_idle_listener = listener;
1488 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events |= POLLIN;
1491 /* Removes a 'struct moonbr_listener' from the queue of idle listeners and unregisters it from poll() */
1492 static void moonbr_remove_idle_listener(struct moonbr_listener *listener) {
1493 if (listener->prev_listener) listener->prev_listener->next_listener = listener->next_listener;
1494 else listener->pool->first_idle_listener = listener->next_listener;
1495 if (listener->next_listener) listener->next_listener->prev_listener = listener->prev_listener;
1496 else listener->pool->last_idle_listener = listener->prev_listener;
1497 listener->prev_listener = NULL;
1498 listener->next_listener = NULL;
1499 if (listener->pollidx != -1) moonbr_poll_fds[listener->pollidx].events &= ~POLLIN;
1502 /* Adds a listener to the queue of connected listeners (i.e. waiting to have their incoming connection accepted) */
1503 static void moonbr_add_connected_listener(struct moonbr_listener *listener) {
1504 listener->prev_listener = listener->pool->last_connected_listener;
1505 if (listener->prev_listener) listener->prev_listener->next_listener = listener;
1506 else listener->pool->first_connected_listener = listener;
1507 listener->pool->last_connected_listener = listener;
1510 /* Removes and returns the first connected listener in the queue */
1511 static struct moonbr_listener *moonbr_pop_connected_listener(struct moonbr_pool *pool) {
1512 struct moonbr_listener *listener = pool->first_connected_listener;
1513 listener->pool->first_connected_listener = listener->next_listener;
1514 if (listener->pool->first_connected_listener) listener->pool->first_connected_listener->prev_listener = NULL;
1515 else listener->pool->last_connected_listener = NULL;
1516 listener->next_listener = NULL;
1517 return listener;
1521 /*** Functions to handle polling ***/
1523 /* Returns an index to a new initialized entry in moonbr_poll_fds[] */
1524 int moonbr_poll_fds_nextindex() {
1525 if (moonbr_poll_fds_count >= moonbr_poll_fds_bufsize) {
1526 if (moonbr_poll_fds_bufsize) moonbr_poll_fds_bufsize *= 2;
1527 else moonbr_poll_fds_bufsize = 1;
1528 moonbr_poll_fds = realloc(
1529 moonbr_poll_fds, moonbr_poll_fds_bufsize * sizeof(struct pollfd)
1530 );
1531 if (!moonbr_poll_fds) {
1532 moonbr_log(LOG_CRIT, "Memory allocation error");
1533 moonbr_terminate_error();
1536 moonbr_poll_fds[moonbr_poll_fds_count] = (struct pollfd){0, };
1537 return moonbr_poll_fds_count++;
1540 /* Returns an index to a new initialized entry in moonbr_poll_workers[] */
1541 int moonbr_poll_workers_nextindex() {
1542 if (moonbr_poll_worker_count >= moonbr_poll_workers_bufsize) {
1543 if (moonbr_poll_workers_bufsize) moonbr_poll_workers_bufsize *= 2;
1544 else moonbr_poll_workers_bufsize = 1;
1545 moonbr_poll_workers = realloc(
1546 moonbr_poll_workers, moonbr_poll_workers_bufsize * sizeof(struct moonbr_poll_worker)
1547 );
1548 if (!moonbr_poll_workers) {
1549 moonbr_log(LOG_CRIT, "Memory allocation error");
1550 moonbr_terminate_error();
1553 moonbr_poll_workers[moonbr_poll_worker_count] = (struct moonbr_poll_worker){0, };
1554 return moonbr_poll_worker_count++;
1557 /* Queues all listeners as idle, and initializes static part of moonbr_poll_fds[], which is passed to poll() */
1558 static void moonbr_poll_init() {
1559 if (socketpair(
1560 PF_LOCAL,
1561 SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
1562 0,
1563 moonbr_poll_signalfds
1564 )) {
1565 moonbr_log(LOG_CRIT, "Could not create socket pair for signal delivery during polling: %s", strerror(errno));
1566 moonbr_terminate_error();
1569 int j = moonbr_poll_fds_nextindex();
1570 struct pollfd *pollfd = &moonbr_poll_fds[j];
1571 pollfd->fd = moonbr_poll_signalfd_read;
1572 pollfd->events = POLLIN;
1575 struct moonbr_pool *pool;
1576 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1577 int i;
1578 for (i=0; i<pool->listener_count; i++) {
1579 struct moonbr_listener *listener = &pool->listener[i];
1580 if (listener->listenfd != -1) {
1581 int j = moonbr_poll_fds_nextindex();
1582 listener->pollidx = j;
1583 moonbr_poll_fds[j].fd = listener->listenfd;
1585 moonbr_add_idle_listener(listener);
1589 moonbr_poll_fds_static_count = moonbr_poll_fds_count; /* remember size of static part of array */
1592 /* Disables polling of all listeners (required for clean shutdown) */
1593 static void moonbr_poll_shutdown() {
1594 int i;
1595 for (i=1; i<moonbr_poll_fds_static_count; i++) {
1596 moonbr_poll_fds[i].fd = -1;
1600 /* (Re)builds dynamic part of moonbr_poll_fds[] array, and (re)builds moonbr_poll_workers[] array */
1601 static void moonbr_poll_refresh() {
1602 moonbr_poll_refresh_needed = 0;
1603 moonbr_poll_fds_count = moonbr_poll_fds_static_count;
1604 moonbr_poll_worker_count = 0;
1606 struct moonbr_pool *pool;
1607 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1608 struct moonbr_worker *worker;
1609 for (worker=pool->first_worker; worker; worker=worker->next_worker) {
1610 if (worker->controlfd != -1) {
1611 int j = moonbr_poll_fds_nextindex();
1612 int k = moonbr_poll_workers_nextindex();
1613 struct pollfd *pollfd = &moonbr_poll_fds[j];
1614 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1615 pollfd->fd = worker->controlfd;
1616 pollfd->events = POLLIN;
1617 poll_worker->channel = MOONBR_POLL_WORKER_CONTROLCHANNEL;
1618 poll_worker->worker = worker;
1620 if (worker->errorfd != -1) {
1621 int j = moonbr_poll_fds_nextindex();
1622 int k = moonbr_poll_workers_nextindex();
1623 struct pollfd *pollfd = &moonbr_poll_fds[j];
1624 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[k];
1625 pollfd->fd = worker->errorfd;
1626 pollfd->events = POLLIN;
1627 poll_worker->channel = MOONBR_POLL_WORKER_ERRORCHANNEL;
1628 poll_worker->worker = worker;
1635 /* resets socket and 'revents' field of moonbr_poll_fds[] for signal delivery just before poll() is called */
1636 static void moonbr_poll_reset_signal() {
1637 ssize_t readcount;
1638 char buf[1];
1639 moonbr_poll_fds[0].revents = 0;
1640 while ((readcount = read(moonbr_poll_signalfd_read, buf, 1)) < 0) {
1641 if (errno == EAGAIN) break;
1642 if (errno != EINTR) {
1643 moonbr_log(LOG_CRIT, "Error while reading from signal delivery socket: %s", strerror(errno));
1644 moonbr_terminate_error();
1647 if (!readcount) {
1648 moonbr_log(LOG_CRIT, "Unexpected EOF when reading from signal delivery socket: %s", strerror(errno));
1649 moonbr_terminate_error();
1654 /*** Shutdown initiation ***/
1656 /* Sets global variable 'moonbr_shutdown_in_progress', closes listeners, and demands worker termination */
1657 static void moonbr_initiate_shutdown() {
1658 struct moonbr_pool *pool;
1659 int i;
1660 if (moonbr_shutdown_in_progress) {
1661 moonbr_log(LOG_NOTICE, "Shutdown already in progress");
1662 return;
1664 moonbr_shutdown_in_progress = 1;
1665 moonbr_log(LOG_NOTICE, "Initiate shutdown");
1666 for (pool = moonbr_first_pool; pool; pool = pool->next_pool) {
1667 for (i=0; i<pool->listener_count; i++) {
1668 struct moonbr_listener *listener = &pool->listener[i];
1669 if (listener->listenfd != -1) {
1670 if (close(listener->listenfd) && errno != EINTR) {
1671 moonbr_log(LOG_CRIT, "Could not close listening socket: %s", strerror(errno));
1672 moonbr_terminate_error();
1676 pool->pre_fork = 0;
1677 pool->min_fork = 0;
1678 pool->max_fork = 0;
1679 timerclear(&pool->exit_delay);
1681 moonbr_poll_shutdown(); /* avoids loops due to error condition when polling closed listeners */
1685 /*** Functions to communicate with child processes ***/
1687 /* Tells child process to terminate */
1688 static void moonbr_terminate_idle_worker(struct moonbr_worker *worker) {
1689 moonbr_send_control_message(worker, MOONBR_COMMAND_TERMINATE, -1, NULL);
1692 /* Handles status messages from child process */
1693 static void moonbr_read_controlchannel(struct moonbr_worker *worker) {
1694 char controlmsg;
1696 ssize_t bytes_read;
1697 while ((bytes_read = read(worker->controlfd, &controlmsg, 1)) <= 0) {
1698 if (bytes_read == 0 || errno == ECONNRESET) {
1699 moonbr_log(LOG_WARNING, "Child process in pool #%i with PID %i unexpectedly closed control socket", worker->pool->poolnum, (int)worker->pid);
1700 if (close(worker->controlfd) && errno != EINTR) {
1701 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));
1702 moonbr_terminate_error();
1704 worker->controlfd = -1;
1705 moonbr_poll_refresh_needed = 1;
1706 return;
1708 if (errno != EINTR) {
1709 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));
1710 moonbr_terminate_error();
1714 if (worker->idle) {
1715 moonbr_log(LOG_CRIT, "Unexpected data from supposedly idle child process in pool #%i with PID %i", worker->pool->poolnum, (int)worker->pid);
1716 moonbr_terminate_error();
1718 if (moonbr_debug) {
1719 moonbr_log(LOG_DEBUG, "Received control message from child in pool #%i with PID %i: \"%c\"", worker->pool->poolnum, (int)worker->pid, (int)controlmsg);
1721 switch (controlmsg) {
1722 case MOONBR_STATUS_IDLE:
1723 if (moonbr_stat) {
1724 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i reports as idle", worker->pool->poolnum, (int)worker->pid);
1726 moonbr_add_idle_worker(worker);
1727 break;
1728 case MOONBR_STATUS_GOODBYE:
1729 if (moonbr_stat) {
1730 moonbr_log(LOG_INFO, "Child process in pool #%i with PID %i announced termination", worker->pool->poolnum, (int)worker->pid);
1732 if (close(worker->controlfd) && errno != EINTR) {
1733 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));
1734 moonbr_terminate_error();
1736 worker->controlfd = -1;
1737 moonbr_poll_refresh_needed = 1;
1738 break;
1739 default:
1740 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);
1741 moonbr_terminate_error();
1745 /* Handles stderr stream from child process */
1746 static void moonbr_read_errorchannel(struct moonbr_worker *worker) {
1747 char staticbuf[MOONBR_MAXERRORLINELEN+1];
1748 char *buf = worker->errorlinebuf;
1749 if (!buf) buf = staticbuf;
1751 ssize_t bytes_read;
1752 while (
1753 (bytes_read = read(
1754 worker->errorfd,
1755 buf + worker->errorlinelen,
1756 MOONBR_MAXERRORLINELEN+1 - worker->errorlinelen
1757 )) <= 0
1758 ) {
1759 if (bytes_read == 0 || errno == ECONNRESET) {
1760 if (moonbr_debug) {
1761 moonbr_log(LOG_DEBUG, "Child process in pool #%i with PID %i closed stderr socket", worker->pool->poolnum, (int)worker->pid);
1763 if (close(worker->errorfd) && errno != EINTR) {
1764 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));
1765 moonbr_terminate_error();
1767 worker->errorfd = -1;
1768 moonbr_poll_refresh_needed = 1;
1769 break;
1771 if (errno != EINTR) {
1772 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));
1773 moonbr_terminate_error();
1776 worker->errorlinelen += bytes_read;
1779 int i;
1780 for (i=0; i<worker->errorlinelen; i++) {
1781 if (buf[i] == '\n') buf[i] = 0;
1782 if (!buf[i]) {
1783 if (worker->errorlineovf) {
1784 worker->errorlineovf = 0;
1785 } else {
1786 moonbr_log(LOG_WARNING, "Error log from process in pool #%i with PID %i: %s", worker->pool->poolnum, (int)worker->pid, buf);
1788 worker->errorlinelen -= i+1;
1789 memmove(buf, buf+i+1, worker->errorlinelen);
1790 i = -1;
1793 if (i > MOONBR_MAXERRORLINELEN) {
1794 buf[MOONBR_MAXERRORLINELEN] = 0;
1795 if (!worker->errorlineovf) {
1796 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);
1798 worker->errorlinelen = 0;
1799 worker->errorlineovf = 1;
1802 if (!worker->errorlinebuf && worker->errorlinelen) { /* allocate buffer on heap only if necessary */
1803 worker->errorlinebuf = malloc((MOONBR_MAXERRORLINELEN+1) * sizeof(char));
1804 if (!worker->errorlinebuf) {
1805 moonbr_log(LOG_CRIT, "Memory allocation error");
1806 moonbr_terminate_error();
1808 memcpy(worker->errorlinebuf, staticbuf, worker->errorlinelen);
1813 /*** Handler for incoming connections ***/
1815 /* Accepts one or more incoming connections on listener socket and passes it to worker(s) popped from idle queue */
1816 static void moonbr_connect(struct moonbr_pool *pool) {
1817 struct moonbr_listener *listener = moonbr_pop_connected_listener(pool);
1818 struct moonbr_worker *worker;
1819 switch (listener->proto) {
1820 case MOONBR_PROTO_INTERVAL:
1821 worker = moonbr_pop_idle_worker(pool);
1822 if (moonbr_stat) {
1823 moonbr_log(LOG_INFO, "Dispatching interval timer \"%s\" of pool #%i to PID %i", listener->proto_specific.interval.name, listener->pool->poolnum, (int)worker->pid);
1825 worker->restart_interval_listener = listener;
1826 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_INTERVAL, -1, listener);
1827 /* do not push listener to queue of idle listeners yet */
1828 break;
1829 case MOONBR_PROTO_LOCAL:
1830 do {
1831 int peerfd;
1832 struct sockaddr_un peeraddr;
1833 socklen_t peeraddr_len = sizeof(struct sockaddr_un);
1834 peerfd = accept4(
1835 listener->listenfd,
1836 (struct sockaddr *)&peeraddr,
1837 &peeraddr_len,
1838 SOCK_CLOEXEC
1839 );
1840 if (peerfd == -1) {
1841 if (errno == EWOULDBLOCK) {
1842 break;
1843 } else if (errno == ECONNABORTED) {
1844 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"local\", path=\"%s\")", listener->proto_specific.local.path);
1845 break;
1846 } else if (errno != EINTR) {
1847 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1848 moonbr_terminate_error();
1850 } else {
1851 worker = moonbr_pop_idle_worker(pool);
1852 if (moonbr_stat) {
1853 moonbr_log(LOG_INFO, "Dispatching local socket connection on path \"%s\" for pool #%i to PID %i", listener->proto_specific.local.path, listener->pool->poolnum, (int)worker->pid);
1855 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_LOCAL, peerfd, listener);
1856 if (close(peerfd) && errno != EINTR) {
1857 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1858 moonbr_terminate_error();
1861 } while (pool->first_idle_worker);
1862 moonbr_add_idle_listener(listener);
1863 break;
1864 case MOONBR_PROTO_TCP6:
1865 do {
1866 int peerfd;
1867 struct sockaddr_in6 peeraddr;
1868 socklen_t peeraddr_len = sizeof(struct sockaddr_in6);
1869 peerfd = accept4(
1870 listener->listenfd,
1871 (struct sockaddr *)&peeraddr,
1872 &peeraddr_len,
1873 SOCK_CLOEXEC
1874 );
1875 if (peerfd == -1) {
1876 if (errno == EWOULDBLOCK) {
1877 break;
1878 } else if (errno == ECONNABORTED) {
1879 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"tcp6\", port=%i)", listener->proto_specific.tcp.port);
1880 break;
1881 } else if (errno != EINTR) {
1882 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1883 moonbr_terminate_error();
1885 } else {
1886 worker = moonbr_pop_idle_worker(pool);
1887 if (moonbr_stat) {
1888 moonbr_log(LOG_INFO, "Dispatching TCP/IPv6 connection for pool #%i on port %i to PID %i", listener->pool->poolnum, listener->proto_specific.tcp.port, (int)worker->pid);
1890 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_NETWORK, peerfd, listener);
1891 if (close(peerfd) && errno != EINTR) {
1892 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1893 moonbr_terminate_error();
1896 } while (pool->first_idle_worker);
1897 moonbr_add_idle_listener(listener);
1898 break;
1899 case MOONBR_PROTO_TCP4:
1900 do {
1901 int peerfd;
1902 struct sockaddr_in peeraddr;
1903 socklen_t peeraddr_len = sizeof(struct sockaddr_in);
1904 peerfd = accept4(
1905 listener->listenfd,
1906 (struct sockaddr *)&peeraddr,
1907 &peeraddr_len,
1908 SOCK_CLOEXEC
1909 );
1910 if (peerfd == -1) {
1911 if (errno == EWOULDBLOCK) {
1912 break;
1913 } else if (errno == ECONNABORTED) {
1914 moonbr_log(LOG_WARNING, "Connection aborted before accepting it (proto=\"tcp4\", port=%i)", listener->proto_specific.tcp.port);
1915 break;
1916 } else if (errno != EINTR) {
1917 moonbr_log(LOG_ERR, "Could not accept socket connection: %s", strerror(errno));
1918 moonbr_terminate_error();
1920 } else {
1921 worker = moonbr_pop_idle_worker(pool);
1922 if (moonbr_stat) {
1923 moonbr_log(LOG_INFO, "Dispatching TCP/IPv4 connection for pool #%i on port %i to PID %i", listener->pool->poolnum, listener->proto_specific.tcp.port, (int)worker->pid);
1925 moonbr_send_control_message(worker, MOONBR_SOCKETTYPE_NETWORK, peerfd, listener);
1926 if (close(peerfd) && errno != EINTR) {
1927 moonbr_log(LOG_ERR, "Could not close incoming socket connection in parent process: %s", strerror(errno));
1928 moonbr_terminate_error();
1931 } while (pool->first_idle_worker);
1932 moonbr_add_idle_listener(listener);
1933 break;
1934 default:
1935 moonbr_log(LOG_ERR, "Internal error (should not happen): Unexpected value in listener.proto field");
1936 moonbr_terminate_error();
1941 /*** Functions to initialize and restart interval timers ***/
1943 /* Initializes all interval timers */
1944 static void moonbr_interval_initialize() {
1945 struct timeval now;
1946 struct moonbr_pool *pool;
1947 moonbr_now(&now);
1948 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
1949 int i;
1950 for (i=0; i<pool->listener_count; i++) {
1951 struct moonbr_listener *listener = &pool->listener[i];
1952 if (listener->proto == MOONBR_PROTO_INTERVAL) {
1953 timeradd(
1954 &now,
1955 &listener->proto_specific.interval.delay,
1956 &listener->proto_specific.interval.wakeup
1957 );
1963 /* If necessary, restarts interval timers and queues interval listener as idle after a worker changed status */
1964 static void moonbr_interval_restart(
1965 struct moonbr_worker *worker,
1966 struct timeval *now /* passed to synchronize with moonbr_run() function */
1967 ) {
1968 struct moonbr_listener *listener = worker->restart_interval_listener;
1969 if (listener) {
1970 moonbr_add_idle_listener(listener);
1971 worker->restart_interval_listener = NULL;
1972 if (listener->proto_specific.interval.strict) {
1973 timeradd(
1974 &listener->proto_specific.interval.wakeup,
1975 &listener->proto_specific.interval.delay,
1976 &listener->proto_specific.interval.wakeup
1977 );
1978 if (timercmp(&listener->proto_specific.interval.wakeup, now, <)) {
1979 listener->proto_specific.interval.wakeup = *now;
1981 } else {
1982 timeradd(
1983 now,
1984 &listener->proto_specific.interval.delay,
1985 &listener->proto_specific.interval.wakeup
1986 );
1992 /*** Main loop and helper functions ***/
1994 /* Stores the earliest required wakeup time in 'wait' variable */
1995 static void moonbr_calc_wait(struct timeval *wait, struct timeval *wakeup) {
1996 if (!timerisset(wait) || timercmp(wakeup, wait, <)) *wait = *wakeup;
1999 /* Main loop of Moonbridge system (including initialization of signal handlers and polling structures) */
2000 static void moonbr_run(lua_State *L) {
2001 struct timeval now;
2002 struct moonbr_pool *pool;
2003 struct moonbr_worker *worker;
2004 struct moonbr_worker *next_worker; /* needed when worker is removed during iteration of workers */
2005 struct moonbr_listener *listener;
2006 struct moonbr_listener *next_listener; /* needed when listener is removed during iteration of listeners */
2007 int i;
2008 moonbr_poll_init(); /* must be executed before moonbr_signal_init() */
2009 moonbr_signal_init();
2010 moonbr_interval_initialize();
2011 moonbr_pstate = MOONBR_PSTATE_RUNNING;
2012 while (1) {
2013 struct timeval wait = {0, }; /* point in time when premature wakeup of poll() is required */
2014 if (moonbr_cond_interrupt) {
2015 moonbr_log(LOG_WARNING, "Fast shutdown requested");
2016 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
2018 if (moonbr_cond_terminate) {
2019 moonbr_initiate_shutdown();
2020 moonbr_cond_terminate = 0;
2022 moonbr_cond_child = 0; /* must not be reset between moonbr_try_destroy_worker() and poll() */
2023 moonbr_now(&now);
2024 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
2025 int terminated_worker_count = 0; /* allows shortcut for new worker creation */
2026 /* terminate idle workers when expired */
2027 if (timerisset(&pool->idle_timeout)) {
2028 while ((worker = pool->first_idle_worker) != NULL) {
2029 if (timercmp(&worker->idle_expiration, &now, >)) break;
2030 moonbr_pop_idle_worker(pool);
2031 moonbr_terminate_idle_worker(worker);
2034 /* mark listeners as connected when incoming connection is pending */
2035 for (listener=pool->first_idle_listener; listener; listener=next_listener) {
2036 next_listener = listener->next_listener; /* extra variable necessary due to changing list */
2037 if (listener->pollidx != -1) {
2038 if (moonbr_poll_fds[listener->pollidx].revents) {
2039 moonbr_poll_fds[listener->pollidx].revents = 0;
2040 moonbr_remove_idle_listener(listener);
2041 moonbr_add_connected_listener(listener);
2043 } else if (listener->proto == MOONBR_PROTO_INTERVAL) {
2044 if (!timercmp(&listener->proto_specific.interval.wakeup, &now, >)) {
2045 moonbr_remove_idle_listener(listener);
2046 moonbr_add_connected_listener(listener);
2048 } else {
2049 moonbr_log(LOG_CRIT, "Internal error (should not happen): Listener is neither an interval timer nor has the 'pollidx' value set");
2050 moonbr_terminate_error();
2053 /* process input from child processes */
2054 for (i=0; i<moonbr_poll_worker_count; i++) {
2055 if (moonbr_poll_worker_fds[i].revents) {
2056 moonbr_poll_worker_fds[i].revents = 0;
2057 struct moonbr_poll_worker *poll_worker = &moonbr_poll_workers[i];
2058 switch (poll_worker->channel) {
2059 case MOONBR_POLL_WORKER_CONTROLCHANNEL:
2060 moonbr_read_controlchannel(poll_worker->worker);
2061 moonbr_interval_restart(poll_worker->worker, &now);
2062 break;
2063 case MOONBR_POLL_WORKER_ERRORCHANNEL:
2064 moonbr_read_errorchannel(poll_worker->worker);
2065 break;
2069 /* collect dead child processes */
2070 for (worker=pool->first_worker; worker; worker=next_worker) {
2071 next_worker = worker->next_worker; /* extra variable necessary due to changing list */
2072 switch (moonbr_try_destroy_worker(worker)) {
2073 case MOONBR_DESTROY_PREPARE:
2074 pool->use_fork_error_wakeup = 1;
2075 break;
2076 case MOONBR_DESTROY_IDLE_OR_ASSIGNED:
2077 terminated_worker_count++;
2078 break;
2081 /* connect listeners with idle workers */
2082 if (!moonbr_shutdown_in_progress) {
2083 while (pool->first_connected_listener && pool->first_idle_worker) {
2084 moonbr_connect(pool);
2087 /* create new worker processes */
2088 while (
2089 pool->total_worker_count < pool->max_fork && (
2090 pool->unassigned_worker_count < pool->pre_fork ||
2091 pool->total_worker_count < pool->min_fork
2093 ) {
2094 if (pool->use_fork_error_wakeup) {
2095 if (timercmp(&pool->fork_error_wakeup, &now, >)) {
2096 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
2097 break;
2099 } else {
2100 if (terminated_worker_count) {
2101 terminated_worker_count--;
2102 } else if (timercmp(&pool->fork_wakeup, &now, >)) {
2103 moonbr_calc_wait(&wait, &pool->fork_wakeup);
2104 break;
2107 if (moonbr_create_worker(pool, L)) {
2108 /* on error, enforce error delay */
2109 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
2110 pool->use_fork_error_wakeup = 1;
2111 moonbr_calc_wait(&wait, &pool->fork_error_wakeup);
2112 break;
2113 } else {
2114 /* normal fork delay on success */
2115 timeradd(&now, &pool->fork_delay, &pool->fork_wakeup);
2116 timeradd(&now, &pool->fork_error_delay, &pool->fork_error_wakeup);
2117 pool->use_fork_error_wakeup = 0; /* gets set later if error occures during preparation */
2120 /* terminate excessive worker processes */
2121 while (
2122 pool->total_worker_count > pool->min_fork &&
2123 pool->idle_worker_count > pool->pre_fork
2124 ) {
2125 if (timerisset(&pool->exit_wakeup)) {
2126 if (timercmp(&pool->exit_wakeup, &now, >)) {
2127 moonbr_calc_wait(&wait, &pool->exit_wakeup);
2128 break;
2130 moonbr_terminate_idle_worker(moonbr_pop_idle_worker(pool));
2131 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
2132 } else {
2133 timeradd(&now, &pool->exit_delay, &pool->exit_wakeup);
2134 break;
2137 if (!(
2138 pool->total_worker_count > pool->min_fork &&
2139 pool->idle_worker_count > pool->pre_fork
2140 )) {
2141 timerclear(&pool->exit_wakeup); /* timer gets restarted later when there are excessive workers */
2143 /* optionally output worker count stats */
2144 if (moonbr_stat && pool->worker_count_stat) {
2145 pool->worker_count_stat = 0;
2146 moonbr_log(
2147 LOG_INFO,
2148 "Worker count for pool #%i: %i idle, %i assigned, %i total",
2149 pool->poolnum, pool->idle_worker_count,
2150 pool->total_worker_count - pool->unassigned_worker_count,
2151 pool->total_worker_count);
2153 /* calculate wakeup time for interval listeners */
2154 for (listener=pool->first_idle_listener; listener; listener=listener->next_listener) {
2155 if (listener->proto == MOONBR_PROTO_INTERVAL) {
2156 moonbr_calc_wait(&wait, &listener->proto_specific.interval.wakeup);
2159 /* calculate wakeup time for idle workers (only first idle worker is significant) */
2160 if (timerisset(&pool->idle_timeout) && pool->first_idle_worker) {
2161 moonbr_calc_wait(&wait, &pool->first_idle_worker->idle_expiration);
2164 /* check if shutdown is complete */
2165 if (moonbr_shutdown_in_progress) {
2166 for (pool=moonbr_first_pool; pool; pool=pool->next_pool) {
2167 if (pool->first_worker) break;
2169 if (!pool) {
2170 moonbr_log(LOG_INFO, "All worker threads have terminated");
2171 moonbr_terminate(MOONBR_EXITCODE_GRACEFUL);
2174 if (moonbr_poll_refresh_needed) moonbr_poll_refresh();
2175 moonbr_cond_poll = 1;
2176 if (!moonbr_cond_child && !moonbr_cond_terminate && !moonbr_cond_interrupt) {
2177 int timeout;
2178 if (timerisset(&wait)) {
2179 if (timercmp(&wait, &now, <)) {
2180 moonbr_log(LOG_CRIT, "Internal error (should not happen): Future is in the past");
2181 moonbr_terminate_error();
2183 timersub(&wait, &now, &wait);
2184 timeout = wait.tv_sec * 1000 + wait.tv_usec / 1000;
2185 } else {
2186 timeout = INFTIM;
2188 if (moonbr_debug) {
2189 moonbr_log(LOG_DEBUG, "Waiting for I/O");
2191 poll(moonbr_poll_fds, moonbr_poll_fds_count, timeout);
2192 } else {
2193 if (moonbr_debug) {
2194 moonbr_log(LOG_DEBUG, "Do not wait for I/O");
2197 moonbr_cond_poll = 0;
2198 moonbr_poll_reset_signal();
2203 /*** Lua interface ***/
2205 static int moonbr_lua_panic(lua_State *L) {
2206 const char *errmsg;
2207 errmsg = lua_tostring(L, -1);
2208 if (!errmsg) {
2209 if (lua_isnoneornil(L, -1)) errmsg = "(error message is nil)";
2210 else errmsg = "(error message is not a string)";
2212 if (moonbr_pstate == MOONBR_PSTATE_FORKED) {
2213 fprintf(stderr, "Uncaught Lua error: %s\n", errmsg);
2214 exit(1);
2215 } else {
2216 moonbr_log(LOG_CRIT, "Uncaught Lua error: %s", errmsg);
2217 moonbr_terminate_error();
2219 return 0;
2222 static int moonbr_addtraceback(lua_State *L) {
2223 luaL_traceback(L, L, luaL_tolstring(L, 1, NULL), 1);
2224 return 1;
2227 /* Memory allocator that allows limiting memory consumption */
2228 static void *moonbr_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
2229 (void)ud; /* not used */
2230 if (nsize == 0) {
2231 if (ptr) {
2232 moonbr_memory_usage -= osize;
2233 free(ptr);
2235 return NULL;
2236 } else if (ptr) {
2237 if (
2238 moonbr_memory_limit &&
2239 nsize > osize &&
2240 moonbr_memory_usage + (nsize - osize) > moonbr_memory_limit
2241 ) {
2242 return NULL;
2243 } else {
2244 ptr = realloc(ptr, nsize);
2245 if (ptr) moonbr_memory_usage += nsize - osize;
2247 } else {
2248 if (
2249 moonbr_memory_limit &&
2250 moonbr_memory_usage + nsize > moonbr_memory_limit
2251 ) {
2252 return NULL;
2253 } else {
2254 ptr = realloc(ptr, nsize);
2255 if (ptr) moonbr_memory_usage += nsize;
2258 return ptr;
2261 /* Helper function to convert a value at the given stack index to a natural number */
2262 static int moonbr_lua_tonatural(lua_State *L, int idx) {
2263 int isnum;
2264 lua_Number n;
2265 n = lua_tonumberx(L, idx, &isnum);
2266 if (isnum && n>=0 && n<INT_MAX && (lua_Number)(int)n == n) return n;
2267 else return -1;
2270 /* Helper function to convert a value at the given stack index to a timeval struct */
2271 static int moonbr_lua_totimeval(lua_State *L, int idx, struct timeval *value) {
2272 int isnum;
2273 lua_Number n;
2274 n = lua_tonumberx(L, idx, &isnum);
2275 if (isnum && n>=0 && n<=100000000) {
2276 value->tv_sec = n;
2277 value->tv_usec = 1e6 * (n - value->tv_sec);
2278 return 1;
2279 } else {
2280 return 0;
2284 /* New function io.poll(...) */
2285 static int moonbr_io_poll(lua_State *L) {
2286 int i;
2287 luaL_Stream *stream;
2288 int fd, isnum;
2289 int nfds = 0;
2290 fd_set readfds, writefds, exceptfds;
2291 struct timeval timeout = {0, };
2292 int status;
2293 FD_ZERO(&readfds);
2294 FD_ZERO(&writefds);
2295 FD_ZERO(&exceptfds);
2296 if (!lua_isnoneornil(L, 1)) {
2297 luaL_checktype(L, 1, LUA_TTABLE);
2298 for (i=1; ; i++) {
2299 #if LUA_VERSION_NUM >= 503
2300 if (lua_geti(L, 1, i) == LUA_TNIL) break;
2301 #else
2302 lua_pushinteger(L, i);
2303 lua_gettable(L, 1);
2304 if (lua_isnil(L, -1)) break;
2305 #endif
2306 stream = luaL_testudata(L, -1, LUA_FILEHANDLE);
2307 if (stream) {
2308 FILE *file = stream->f;
2309 int flags, chr;
2310 flockfile(file);
2311 fd = fileno_unlocked(file);
2312 if (ferror(file) || feof(file)) goto moonbr_io_poll_pending_shortcut1;
2313 flags = fcntl(fd, F_GETFL, 0);
2314 if (flags == -1) goto moonbr_io_poll_pending_error;
2315 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) goto moonbr_io_poll_pending_error;
2316 chr = getc_unlocked(file);
2317 if (chr == EOF) {
2318 if (ferror(file) && errno == EAGAIN) clearerr_unlocked(file);
2319 else goto moonbr_io_poll_pending_shortcut2;
2320 } else {
2321 if (ungetc(chr, file) == EOF) {
2322 fcntl(fd, F_SETFL, flags);
2323 goto moonbr_io_poll_pending_error;
2325 goto moonbr_io_poll_pending_shortcut2;
2327 if (fcntl(fd, F_SETFL, flags) == -1) goto moonbr_io_poll_pending_error;
2328 funlockfile(file);
2329 goto moonbr_io_poll_pending_cont;
2330 moonbr_io_poll_pending_shortcut2:
2331 if (fcntl(fd, F_SETFL, flags) == -1) goto moonbr_io_poll_pending_error;
2332 moonbr_io_poll_pending_shortcut1:
2333 funlockfile(file);
2334 lua_pushboolean(L, 1);
2335 return 1;
2336 moonbr_io_poll_pending_error:
2337 funlockfile(file);
2339 char errmsg[MOONBR_MAXSTRERRORLEN];
2340 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
2341 return luaL_error(L, "Unexpected error while checking buffer of FILE handle: %s", errmsg);
2344 fd = lua_tointegerx(L, -1, &isnum);
2345 if (!isnum) luaL_error(L, "File descriptor is not an integer");
2346 moonbr_io_poll_pending_cont:
2347 FD_SET(fd, &readfds);
2348 if (fd+1 > nfds) nfds = fd+1;
2349 lua_pop(L, 1);
2352 if (!lua_isnoneornil(L, 2)) {
2353 luaL_checktype(L, 2, LUA_TTABLE);
2354 for (i=1; ; i++) {
2355 #if LUA_VERSION_NUM >= 503
2356 if (lua_geti(L, 2, i) == LUA_TNIL) break;
2357 #else
2358 lua_pushinteger(L, i);
2359 lua_gettable(L, 2);
2360 if (lua_isnil(L, -1)) break;
2361 #endif
2362 stream = luaL_testudata(L, -1, LUA_FILEHANDLE);
2363 if (stream) {
2364 fd = fileno(stream->f);
2365 } else {
2366 fd = lua_tointegerx(L, -1, &isnum);
2367 if (!isnum) luaL_error(L, "File descriptor is not an integer");
2369 FD_SET(fd, &writefds);
2370 if (fd+1 > nfds) nfds = fd+1;
2371 lua_pop(L, 1);
2373 lua_pop(L, 2);
2375 if (!lua_isnoneornil(L, 3)) {
2376 luaL_argcheck(L, moonbr_lua_totimeval(L, 3, &timeout), 3, "not a valid timeout");
2378 status = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
2379 if (status == -1) {
2380 if (errno == EINTR) {
2381 lua_pushboolean(L, 0);
2382 lua_pushliteral(L, "Signal received while polling file descriptors");
2383 return 2;
2384 } else {
2385 char errmsg[MOONBR_MAXSTRERRORLEN];
2386 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
2387 return luaL_error(L, "Unexpected error during \"select\" system call: %s", errmsg);
2389 } else if (status == 0) {
2390 lua_pushboolean(L, 0);
2391 lua_pushliteral(L, "Timeout while polling file descriptors");
2392 return 2;
2393 } else {
2394 lua_pushboolean(L, 1);
2395 return 1;
2399 /* New methods for Lua file objects: read until terminator or length exceeded (optional non-blocking) */
2400 static int moonbr_io_xread_impl(lua_State *L, int nonblock) {
2401 luaL_Stream *stream;
2402 lua_Integer maxlen;
2403 const char *terminatorstr;
2404 size_t terminatorlen;
2405 FILE *file;
2406 int fd, flags, chr, terminator;
2407 luaL_Buffer buf;
2408 int wouldblock = 0;
2409 stream = luaL_checkudata(L, 1, LUA_FILEHANDLE);
2410 maxlen = luaL_optinteger(L, 2, 0);
2411 terminatorstr = luaL_optlstring(L, 3, "", &terminatorlen);
2412 luaL_argcheck(L, terminatorlen <= 1, 3, "single byte expected");
2413 if (!stream->closef) luaL_error(L, "attempt to use a closed file");
2414 file = stream->f;
2415 if (ferror(file)) {
2416 lua_pushnil(L);
2417 lua_pushliteral(L, "Previous error condition on file handle");
2418 return 2;
2420 if (nonblock) {
2421 fd = fileno(file);
2422 flags = fcntl(fd, F_GETFL, 0);
2423 if (flags == -1) goto moonbr_io_xread_impl_error;
2424 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) goto moonbr_io_xread_impl_error;
2425 } else {
2426 fd = -1; /* avoids compiler warning */
2427 flags = 0; /* avoids compiler warning */
2429 if (!maxlen) maxlen = -1;
2430 terminator = terminatorlen ? terminatorstr[0] : -1;
2431 luaL_buffinit(L, &buf);
2432 while (maxlen > 0 ? maxlen-- : maxlen) {
2433 chr = getc(file);
2434 if (chr == EOF) {
2435 if (ferror(file)) {
2436 if (nonblock && errno == EAGAIN) {
2437 clearerr(file);
2438 wouldblock = 1;
2439 } else {
2440 char errmsg[MOONBR_MAXSTRERRORLEN];
2441 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
2442 if (fcntl(fd, F_SETFL, flags) == -1) goto moonbr_io_xread_impl_error;
2443 lua_pushnil(L);
2444 lua_pushstring(L, errmsg);
2445 return 2;
2448 break;
2450 luaL_addchar(&buf, chr);
2451 if (chr == terminator) break;
2453 if (nonblock) {
2454 if (fcntl(fd, F_SETFL, flags) == -1) goto moonbr_io_xread_impl_error;
2456 luaL_pushresult(&buf);
2457 if (wouldblock || lua_rawlen(L, -1)) {
2458 return 1;
2459 } else {
2460 lua_pushboolean(L, 0);
2461 lua_pushliteral(L, "End of file");
2462 return 2;
2464 moonbr_io_xread_impl_error:
2466 char errmsg[MOONBR_MAXSTRERRORLEN];
2467 strerror_r(errno, errmsg, MOONBR_MAXSTRERRORLEN); /* use thread-safe call in case child created threads */
2468 return luaL_error(L, "Unexpected error in xread method: %s", errmsg);
2471 static int moonbr_io_xread(lua_State *L) {
2472 return moonbr_io_xread_impl(L, 0);
2474 static int moonbr_io_xread_nb(lua_State *L) {
2475 return moonbr_io_xread_impl(L, 1);
2478 /* New methods for Lua file objects: non-blocking writing */
2479 static int moonbr_io_write_nb(lua_State *L) {
2480 return luaL_error(L, "Not implemented"); // TODO
2483 /* New global function timeout(...) */
2484 static int moonbr_timeout(lua_State *L) {
2485 struct itimerval oldval;
2486 if (lua_isnoneornil(L, 1) && lua_isnoneornil(L, 2)) {
2487 getitimer(ITIMER_REAL, &oldval);
2488 } else {
2489 struct itimerval newval = {};
2490 timerclear(&newval.it_interval);
2491 timerclear(&newval.it_value);
2492 if (lua_toboolean(L, 1)) {
2493 luaL_argcheck(
2494 L, moonbr_lua_totimeval(L, 1, &newval.it_value), 1,
2495 "interval in seconds expected"
2496 );
2498 if (lua_isnoneornil(L, 2)) {
2499 if (setitimer(ITIMER_REAL, &newval, &oldval)) {
2500 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2501 moonbr_terminate_error();
2503 } else {
2504 getitimer(ITIMER_REAL, &oldval);
2505 if (!timerisset(&oldval.it_value)) {
2506 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2507 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2508 moonbr_terminate_error();
2510 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
2511 timerclear(&newval.it_value);
2512 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2513 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2514 moonbr_terminate_error();
2516 } else if (timercmp(&newval.it_value, &oldval.it_value, <)) {
2517 struct itimerval remval;
2518 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2519 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2520 moonbr_terminate_error();
2522 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
2523 getitimer(ITIMER_REAL, &remval);
2524 timersub(&oldval.it_value, &newval.it_value, &newval.it_value);
2525 timeradd(&newval.it_value, &remval.it_value, &newval.it_value);
2526 if (setitimer(ITIMER_REAL, &newval, NULL)) {
2527 moonbr_log(LOG_CRIT, "Could not set ITIMER_REAL via setitimer()");
2528 moonbr_terminate_error();
2530 } else {
2531 lua_call(L, lua_gettop(L) - 2, LUA_MULTRET);
2533 return lua_gettop(L) - 1;
2536 lua_pushnumber(L, oldval.it_value.tv_sec + 1e-6 * oldval.it_value.tv_usec);
2537 return 1;
2540 #define moonbr_listen_init_pool_forkoption(luaname, cname, defval) { \
2541 lua_getfield(L, 2, luaname); \
2542 pool->cname = lua_isnil(L, -1) ? (defval) : moonbr_lua_tonatural(L, -1); \
2543 } while(0)
2545 #define moonbr_listen_init_pool_timeoption(luaname, cname, defval, defvalu) ( \
2546 lua_getfield(L, 2, luaname), \
2547 lua_isnil(L, -1) ? ( \
2548 pool->cname.tv_sec = (defval), pool->cname.tv_usec = (defvalu), \
2549 1 \
2550 ) : ( \
2551 (lua_isboolean(L, -1) && !lua_toboolean(L, -1)) ? ( \
2552 pool->cname.tv_sec = 0, pool->cname.tv_usec = 0, \
2553 1 \
2554 ) : ( \
2555 moonbr_lua_totimeval(L, -1, &pool->cname) \
2556 ) \
2557 ) \
2560 static int moonbr_listen_init_pool(lua_State *L) {
2561 struct moonbr_pool *pool;
2562 const char *proto;
2563 int i;
2564 pool = lua_touserdata(L, 1);
2565 for (i=0; i<pool->listener_count; i++) {
2566 struct moonbr_listener *listener = &pool->listener[i];
2567 lua_settop(L, 2);
2568 #if LUA_VERSION_NUM >= 503
2569 lua_geti(L, 2, i+1);
2570 #else
2571 lua_pushinteger(L, i+1);
2572 lua_gettable(L, 2);
2573 #endif
2574 lua_getfield(L, 3, "proto");
2575 proto = lua_tostring(L, -1);
2576 if (proto && !strcmp(proto, "interval")) {
2577 listener->proto = MOONBR_PROTO_INTERVAL;
2578 lua_getfield(L, 3, "name");
2580 const char *name = lua_tostring(L, -1);
2581 if (name) {
2582 if (asprintf(&listener->proto_specific.interval.name, "%s", name) < 0) {
2583 moonbr_log(LOG_CRIT, "Memory allocation_error");
2584 moonbr_terminate_error();
2588 lua_getfield(L, 3, "delay");
2589 if (
2590 !moonbr_lua_totimeval(L, -1, &listener->proto_specific.interval.delay) ||
2591 !timerisset(&listener->proto_specific.interval.delay)
2592 ) {
2593 luaL_error(L, "No valid interval delay specified; use listen{{proto=\"interval\", delay=...}, ...}");
2595 lua_getfield(L, 3, "strict");
2596 if (!lua_isnil(L, -1)) {
2597 if (lua_isboolean(L, -1)) {
2598 if (lua_toboolean(L, -1)) listener->proto_specific.interval.strict = 1;
2599 } else {
2600 luaL_error(L, "Option \"strict\" must be a boolean if set; use listen{{proto=\"interval\", strict=true, ...}, ...}");
2603 } else if (proto && !strcmp(proto, "local")) {
2604 listener->proto = MOONBR_PROTO_LOCAL;
2605 lua_getfield(L, 3, "path");
2607 const char *path = lua_tostring(L, -1);
2608 if (!path) {
2609 luaL_error(L, "No valid path specified for local socket; use listen{{proto=\"local\", path=...}, ...}");
2611 if (asprintf(&listener->proto_specific.local.path, "%s", path) < 0) {
2612 moonbr_log(LOG_CRIT, "Memory allocation_error");
2613 moonbr_terminate_error();
2616 } else if (proto && !strcmp(proto, "tcp6")) {
2617 listener->proto = MOONBR_PROTO_TCP6;
2618 lua_getfield(L, 3, "port");
2619 listener->proto_specific.tcp.port = lua_tointeger(L, -1);
2620 if (
2621 listener->proto_specific.tcp.port < 1 ||
2622 listener->proto_specific.tcp.port > 65535
2623 ) {
2624 luaL_error(L, "No valid port number specified; use listen{{proto=\"tcp6\", port=...}, ...}");
2626 lua_getfield(L, 3, "localhost");
2627 if (!lua_isnil(L, -1)) {
2628 if (lua_isboolean(L, -1)) {
2629 if (lua_toboolean(L, -1)) listener->proto_specific.tcp.localhost_only = 1;
2630 } else {
2631 luaL_error(L, "Option \"localhost\" must be a boolean if set; use listen{{proto=\"tcp6\", localhost=true, ...}, ...}");
2634 } else if (proto && !strcmp(proto, "tcp4")) {
2635 listener->proto = MOONBR_PROTO_TCP4;
2636 lua_getfield(L, 3, "port");
2637 listener->proto_specific.tcp.port = lua_tointeger(L, -1);
2638 if (
2639 listener->proto_specific.tcp.port < 1 ||
2640 listener->proto_specific.tcp.port > 65535
2641 ) {
2642 luaL_error(L, "No valid port number specified; use listen{{proto=\"tcp4\", port=...}, ...}");
2644 lua_getfield(L, 3, "localhost");
2645 if (!lua_isnil(L, -1)) {
2646 if (lua_isboolean(L, -1)) {
2647 if (lua_toboolean(L, -1)) listener->proto_specific.tcp.localhost_only = 1;
2648 } else {
2649 luaL_error(L, "Option \"localhost\" must be a boolean if set; use listen{{proto=\"tcp4\", localhost=true, ...}, ...}");
2654 lua_settop(L, 2);
2655 moonbr_listen_init_pool_forkoption("pre_fork", pre_fork, 1);
2656 moonbr_listen_init_pool_forkoption("min_fork", min_fork, pool->pre_fork > 2 ? pool->pre_fork : 2);
2657 moonbr_listen_init_pool_forkoption("max_fork", max_fork, pool->min_fork > 16 ? pool->min_fork : 16);
2658 if (!moonbr_listen_init_pool_timeoption("fork_delay", fork_delay, 0, 250000)) {
2659 luaL_error(L, "Option \"fork_delay\" is expected to be a non-negative number");
2661 if (!moonbr_listen_init_pool_timeoption("fork_error_delay", fork_error_delay, 2, 0)) {
2662 luaL_error(L, "Option \"fork_error_delay\" is expected to be a non-negative number");
2664 if (!moonbr_listen_init_pool_timeoption("exit_delay", exit_delay, 60, 0)) {
2665 luaL_error(L, "Option \"exit_delay\" is expected to be a non-negative number");
2667 if (timercmp(&pool->fork_error_delay, &pool->fork_delay, <)) {
2668 pool->fork_error_delay = pool->fork_delay;
2670 if (!moonbr_listen_init_pool_timeoption("idle_timeout", idle_timeout, 0, 0)) {
2671 luaL_error(L, "Option \"idle_timeout\" is expected to be a non-negative number");
2673 lua_getfield(L, 2, "memory_limit");
2674 if (!lua_isnil(L, -1)) {
2675 int isnum;
2676 lua_Number n;
2677 n = lua_tonumberx(L, -1, &isnum);
2678 if (n < 0 || !isnum) {
2679 luaL_error(L, "Option \"memory_limit\" is expected to be a non-negative number");
2681 pool->memory_limit = n;
2683 lua_settop(L, 2);
2684 lua_getfield(L, 2, "prepare");
2685 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2686 luaL_error(L, "Option \"prepare\" must be nil or a function");
2688 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2689 lua_getfield(L, 2, "connect");
2690 if (!lua_isfunction(L, -1)) {
2691 luaL_error(L, "Option \"connect\" must be a function; use listen{{...}, {...}, connect=function(socket) ... end, ...}");
2693 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2694 lua_getfield(L, 2, "finish");
2695 if (!lua_isnil(L, -1) && !lua_isfunction(L, -1)) {
2696 luaL_error(L, "Option \"finish\" must be nil or a function");
2698 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2699 return 0;
2702 static int moonbr_listen(lua_State *L) {
2703 struct moonbr_pool *pool;
2704 lua_Integer listener_count;
2705 if (moonbr_booted) luaL_error(L, "Moonbridge bootup is already complete");
2706 luaL_checktype(L, 1, LUA_TTABLE);
2707 listener_count = luaL_len(L, 1);
2708 if (!listener_count) luaL_error(L, "No listen ports specified; use listen{{proto=..., port=...},...}");
2709 if (listener_count > 100) luaL_error(L, "Too many listeners");
2710 pool = moonbr_create_pool(listener_count);
2711 lua_pushcfunction(L, moonbr_listen_init_pool);
2712 lua_pushlightuserdata(L, pool);
2713 lua_pushvalue(L, 1);
2714 if (lua_pcall(L, 2, 0, 0)) goto moonbr_listen_error;
2716 int i;
2717 i = moonbr_start_pool(pool);
2718 if (i >= 0) {
2719 struct moonbr_listener *listener = &pool->listener[i];
2720 switch (listener->proto) {
2721 case MOONBR_PROTO_INTERVAL:
2722 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"interval\"): %s", i+1, strerror(errno));
2723 break;
2724 case MOONBR_PROTO_LOCAL:
2725 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"local\", path=\"%s\"): %s", i+1, listener->proto_specific.local.path, strerror(errno));
2726 break;
2727 case MOONBR_PROTO_TCP6:
2728 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"tcp6\", port=%d): %s", i+1, listener->proto_specific.tcp.port, strerror(errno));
2729 break;
2730 case MOONBR_PROTO_TCP4:
2731 lua_pushfstring(L, "Could not initialize listener #%d (proto=\"tcp4\", port=%d): %s", i+1, listener->proto_specific.tcp.port, strerror(errno));
2732 break;
2733 default:
2734 moonbr_log(LOG_ERR, "Internal error (should not happen): Unexpected value in listener.proto field");
2735 moonbr_terminate_error();
2737 goto moonbr_listen_error;
2740 return 0;
2741 moonbr_listen_error:
2742 moonbr_destroy_pool(pool);
2743 lua_pushnil(L);
2744 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_prepare_func(pool));
2745 lua_pushnil(L);
2746 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_connect_func(pool));
2747 lua_pushnil(L);
2748 lua_rawsetp(L, LUA_REGISTRYINDEX, moonbr_luakey_finish_func(pool));
2749 lua_error(L);
2750 return 0; /* avoid compiler warning */
2754 /*** Function to modify Lua's library path and/or cpath ***/
2756 #if defined(MOONBR_LUA_PATH) || defined(MOONBR_LUA_CPATH)
2757 static void moonbr_modify_path(lua_State *L, char *key, char *value) {
2758 int stackbase;
2759 stackbase = lua_gettop(L);
2760 lua_getglobal(L, "package");
2761 lua_getfield(L, stackbase+1, key);
2763 const char *current_str;
2764 size_t current_strlen;
2765 luaL_Buffer buf;
2766 current_str = lua_tolstring(L, stackbase+2, &current_strlen);
2767 luaL_buffinit(L, &buf);
2768 if (current_str) {
2769 lua_pushvalue(L, stackbase+2);
2770 luaL_addvalue(&buf);
2771 if (current_strlen && current_str[current_strlen-1] != ';') {
2772 luaL_addchar(&buf, ';');
2775 luaL_addstring(&buf, value);
2776 luaL_pushresult(&buf);
2778 lua_setfield(L, stackbase+1, key);
2779 lua_settop(L, stackbase);
2781 #endif
2784 /*** Main function and command line invokation ***/
2786 static void moonbr_usage(int err, const char *cmd) {
2787 FILE *out;
2788 out = err ? stderr : stdout;
2789 if (!cmd) cmd = "moonbridge";
2790 fprintf(out, "Get this help message: %s {-h|--help}\n", cmd);
2791 fprintf(out, "Usage: %s \\\n", cmd);
2792 fprintf(out, " [-b|--background] \\\n");
2793 fprintf(out, " [-d|--debug] \\\n");
2794 fprintf(out, " [-f|--logfacility {DAEMON|USER|0|1|...|7}] \\\n");
2795 fprintf(out, " [-i|--logident <syslog ident> \\\n");
2796 fprintf(out, " [-l|--logfile <logfile>] \\\n");
2797 fprintf(out, " [-p|--pidfile <pidfile>] \\\n");
2798 fprintf(out, " [-s|--stats] \\\n");
2799 fprintf(out, " -- <Lua script> [<cmdline options for Lua script>]\n");
2800 exit(err);
2803 #define moonbr_usage_error() moonbr_usage(MOONBR_EXITCODE_CMDLINEERROR, argc ? argv[0] : NULL)
2805 int main(int argc, char **argv) {
2807 int daemonize = 0;
2808 int log_facility = LOG_USER;
2809 const char *log_ident = "moonbridge";
2810 const char *log_filename = NULL;
2811 const char *pid_filename = NULL;
2812 int option;
2813 struct option longopts[] = {
2814 { "background", no_argument, NULL, 'b' },
2815 { "debug", no_argument, NULL, 'd' },
2816 { "logfacility", required_argument, NULL, 'f' },
2817 { "help", no_argument, NULL, 'h' },
2818 { "logident", required_argument, NULL, 'i' },
2819 { "logfile", required_argument, NULL, 'l' },
2820 { "pidfile", required_argument, NULL, 'p' },
2821 { "stats", no_argument, NULL, 's' }
2822 };
2823 while ((option = getopt_long(argc, argv, "bdf:hi:l:p:s", longopts, NULL)) != -1) {
2824 switch (option) {
2825 case 'b':
2826 daemonize = 1;
2827 break;
2828 case 'd':
2829 moonbr_debug = 1;
2830 moonbr_stat = 1;
2831 break;
2832 case 'f':
2833 if (!strcmp(optarg, "DAEMON")) {
2834 log_facility = LOG_DAEMON;
2835 } else if (!strcmp(optarg, "USER")) {
2836 log_facility = LOG_USER;
2837 } else if (!strcmp(optarg, "0")) {
2838 log_facility = LOG_LOCAL0;
2839 } else if (!strcmp(optarg, "1")) {
2840 log_facility = LOG_LOCAL1;
2841 } else if (!strcmp(optarg, "2")) {
2842 log_facility = LOG_LOCAL2;
2843 } else if (!strcmp(optarg, "3")) {
2844 log_facility = LOG_LOCAL3;
2845 } else if (!strcmp(optarg, "4")) {
2846 log_facility = LOG_LOCAL4;
2847 } else if (!strcmp(optarg, "5")) {
2848 log_facility = LOG_LOCAL5;
2849 } else if (!strcmp(optarg, "6")) {
2850 log_facility = LOG_LOCAL6;
2851 } else if (!strcmp(optarg, "7")) {
2852 log_facility = LOG_LOCAL7;
2853 } else {
2854 moonbr_usage_error();
2856 moonbr_use_syslog = 1;
2857 break;
2858 case 'h':
2859 moonbr_usage(MOONBR_EXITCODE_GRACEFUL, argv[0]);
2860 break;
2861 case 'i':
2862 log_ident = optarg;
2863 moonbr_use_syslog = 1;
2864 break;
2865 case 'l':
2866 log_filename = optarg;
2867 break;
2868 case 'p':
2869 pid_filename = optarg;
2870 break;
2871 case 's':
2872 moonbr_stat = 1;
2873 break;
2874 default:
2875 moonbr_usage_error();
2878 if (argc - optind < 1) moonbr_usage_error();
2879 if (pid_filename) {
2880 pid_t otherpid;
2881 while ((moonbr_pidfh = pidfile_open(pid_filename, 0644, &otherpid)) == NULL) {
2882 if (errno == EEXIST) {
2883 if (otherpid == -1) {
2884 fprintf(stderr, "PID file \"%s\" is already locked\n", pid_filename);
2885 } else {
2886 fprintf(stderr, "PID file \"%s\" is already locked by process with PID: %i\n", pid_filename, (int)otherpid);
2888 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2889 } else if (errno != EINTR) {
2890 fprintf(stderr, "Could not write PID file \"%s\": %s\n", pid_filename, strerror(errno));
2891 exit(MOONBR_EXITCODE_STARTUPERROR);
2895 if (log_filename) {
2896 int logfd;
2897 while (
2898 ( logfd = flopen(
2899 log_filename,
2900 O_WRONLY|O_NONBLOCK|O_CREAT|O_APPEND|O_CLOEXEC,
2901 0640
2903 ) < 0
2904 ) {
2905 if (errno == EWOULDBLOCK) {
2906 fprintf(stderr, "Logfile \"%s\" is locked\n", log_filename);
2907 exit(MOONBR_EXITCODE_ALREADYRUNNING);
2908 } else if (errno != EINTR) {
2909 fprintf(stderr, "Could not open logfile \"%s\": %s\n", log_filename, strerror(errno));
2910 exit(MOONBR_EXITCODE_STARTUPERROR);
2913 moonbr_logfile = fdopen(logfd, "a");
2914 if (!moonbr_logfile) {
2915 fprintf(stderr, "Could not open write stream to logfile \"%s\": %s\n", log_filename, strerror(errno));
2916 exit(MOONBR_EXITCODE_STARTUPERROR);
2919 if (daemonize == 0 && !moonbr_logfile) moonbr_logfile = stderr;
2920 if (moonbr_logfile) setlinebuf(moonbr_logfile);
2921 else moonbr_use_syslog = 1;
2922 if (moonbr_use_syslog) openlog(log_ident, LOG_NDELAY | LOG_PID, log_facility);
2923 if (daemonize) {
2924 if (daemon(1, 0)) {
2925 moonbr_log(LOG_ERR, "Could not daemonize moonbridge process");
2926 moonbr_terminate_error();
2930 moonbr_log(LOG_NOTICE, "Starting moonbridge server");
2931 if (moonbr_pidfh && pidfile_write(moonbr_pidfh)) {
2932 moonbr_log(LOG_ERR, "Could not write pidfile (after locking)");
2935 lua_State *L;
2936 L = lua_newstate(moonbr_alloc, NULL);
2937 if (!L) {
2938 moonbr_log(LOG_CRIT, "Could not initialize Lua state");
2939 moonbr_terminate_error();
2941 lua_atpanic(L, moonbr_lua_panic);
2942 lua_pushliteral(L, MOONBR_VERSION_STRING);
2943 lua_setglobal(L, "_MOONBRIDGE_VERSION");
2944 luaL_openlibs(L);
2945 #ifdef MOONBR_LUA_PATH
2946 moonbr_modify_path(L, "path", MOONBR_LUA_PATH);
2947 #endif
2948 #ifdef MOONBR_LUA_CPATH
2949 moonbr_modify_path(L, "cpath", MOONBR_LUA_CPATH);
2950 #endif
2951 lua_getglobal(L, "io");
2952 lua_pushcfunction(L, moonbr_io_poll);
2953 lua_setfield(L, -2, "poll");
2954 lua_pop(L, 1);
2955 if (luaL_newmetatable(L, LUA_FILEHANDLE)) {
2956 moonbr_log(LOG_CRIT, "Lua metatable LUA_FILEHANDLE does not exist");
2957 moonbr_terminate_error();
2959 lua_getfield(L, -1, "__index");
2960 lua_pushcfunction(L, moonbr_io_xread);
2961 lua_setfield(L, -2, "xread");
2962 lua_pushcfunction(L, moonbr_io_xread_nb);
2963 lua_setfield(L, -2, "xread_nb");
2964 lua_pushcfunction(L, moonbr_io_write_nb);
2965 lua_setfield(L, -2, "write_nb");
2966 lua_pop(L, 2);
2967 lua_pushcfunction(L, moonbr_timeout);
2968 lua_setglobal(L, "timeout");
2969 lua_pushcfunction(L, moonbr_listen);
2970 lua_setglobal(L, "listen");
2971 lua_pushcfunction(L, moonbr_addtraceback); /* on stack position 1 */
2972 moonbr_log(LOG_INFO, "Loading \"%s\"", argv[optind]);
2973 if (luaL_loadfile(L, argv[optind])) {
2974 moonbr_log(LOG_ERR, "Error while loading \"%s\": %s", argv[optind], lua_tostring(L, -1));
2975 moonbr_terminate_error();
2977 { int i; for (i=optind+1; i<argc; i++) lua_pushstring(L, argv[i]); }
2978 if (lua_pcall(L, argc-(optind+1), 0, 1)) {
2979 moonbr_log(LOG_ERR, "Error while executing \"%s\": %s", argv[optind], lua_tostring(L, -1));
2980 moonbr_terminate_error();
2982 if (!moonbr_first_pool) {
2983 moonbr_log(LOG_WARNING, "No listener initialized.");
2984 moonbr_terminate_error();
2986 lua_getglobal(L, "listen");
2987 lua_pushcfunction(L, moonbr_listen);
2988 if (lua_compare(L, -2, -1, LUA_OPEQ)) {
2989 lua_pushnil(L);
2990 lua_setglobal(L, "listen");
2992 lua_settop(L, 1);
2993 lua_gc(L, LUA_GCCOLLECT, 0); // collect garbage before forking later
2994 moonbr_run(L);
2996 return 0;

Impressum / About Us