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