moonbridge

annotate moonbridge.c @ 123:20e0d4f51381

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

Impressum / About Us