2017-05-25 10:53:48 +00:00
|
|
|
|
|
|
|
/*
|
2018-03-12 14:03:15 +00:00
|
|
|
* Odyssey.
|
2017-05-25 10:53:48 +00:00
|
|
|
*
|
2018-04-04 13:19:58 +00:00
|
|
|
* Scalable PostgreSQL connection pooler.
|
2017-05-25 10:53:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2018-08-28 14:43:46 +00:00
|
|
|
#include <ctype.h>
|
2017-05-31 15:47:15 +00:00
|
|
|
#include <inttypes.h>
|
2018-08-28 14:43:46 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2017-05-25 10:53:48 +00:00
|
|
|
#include <signal.h>
|
2018-11-14 13:14:13 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2017-05-25 10:53:48 +00:00
|
|
|
|
|
|
|
#include <machinarium.h>
|
2018-08-28 14:43:46 +00:00
|
|
|
#include <kiwi.h>
|
|
|
|
#include <odyssey.h>
|
|
|
|
|
|
|
|
void
|
|
|
|
od_instance_init(od_instance_t *instance)
|
2017-05-25 10:53:48 +00:00
|
|
|
{
|
|
|
|
od_pid_init(&instance->pid);
|
2017-07-26 14:05:29 +00:00
|
|
|
od_logger_init(&instance->logger, &instance->pid);
|
2018-03-06 15:23:52 +00:00
|
|
|
od_config_init(&instance->config);
|
2018-09-18 16:22:22 +00:00
|
|
|
instance->config_file = NULL;
|
2017-05-25 10:53:48 +00:00
|
|
|
|
2017-06-14 12:14:53 +00:00
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGINT);
|
2018-02-28 14:33:38 +00:00
|
|
|
sigaddset(&mask, SIGTERM);
|
2017-07-13 11:49:44 +00:00
|
|
|
sigaddset(&mask, SIGHUP);
|
2017-06-14 12:14:53 +00:00
|
|
|
sigaddset(&mask, SIGPIPE);
|
|
|
|
sigprocmask(SIG_BLOCK, &mask, NULL);
|
2017-05-25 10:53:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
void
|
|
|
|
od_instance_free(od_instance_t *instance)
|
2017-05-25 10:53:48 +00:00
|
|
|
{
|
2018-03-06 15:23:52 +00:00
|
|
|
if (instance->config.pid_file)
|
|
|
|
od_pid_unlink(&instance->pid, instance->config.pid_file);
|
|
|
|
od_config_free(&instance->config);
|
2017-07-26 14:05:29 +00:00
|
|
|
od_logger_close(&instance->logger);
|
2017-08-17 13:56:32 +00:00
|
|
|
machinarium_free();
|
2017-05-25 10:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
od_usage(od_instance_t *instance, char *path)
|
|
|
|
{
|
2017-09-21 13:44:19 +00:00
|
|
|
od_log(&instance->logger, "init", NULL, NULL,
|
2018-03-12 14:03:15 +00:00
|
|
|
"odyssey (git: %s %s)",
|
2017-05-25 10:53:48 +00:00
|
|
|
OD_VERSION_GIT,
|
|
|
|
OD_VERSION_BUILD);
|
2017-09-21 13:44:19 +00:00
|
|
|
od_log(&instance->logger, "init", NULL, NULL,
|
|
|
|
"usage: %s <config_file>", path);
|
2017-05-25 10:53:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
int
|
|
|
|
od_instance_main(od_instance_t *instance, int argc, char **argv)
|
2017-05-25 10:53:48 +00:00
|
|
|
{
|
2018-12-06 14:23:15 +00:00
|
|
|
/* prepare system services */
|
|
|
|
od_system_t system;
|
|
|
|
od_router_t router;
|
|
|
|
od_cron_t cron;
|
|
|
|
od_worker_pool_t worker_pool;
|
|
|
|
od_global_t global;
|
|
|
|
|
|
|
|
od_system_init(&system);
|
|
|
|
od_router_init(&router);
|
|
|
|
od_cron_init(&cron);
|
|
|
|
od_worker_pool_init(&worker_pool);
|
|
|
|
od_global_init(&global, instance, &system, &router, &cron, &worker_pool);
|
|
|
|
|
2017-05-25 10:53:48 +00:00
|
|
|
/* validate command line options */
|
|
|
|
if (argc != 2) {
|
|
|
|
od_usage(instance, argv[0]);
|
2017-07-13 10:25:32 +00:00
|
|
|
return -1;
|
2017-05-25 10:53:48 +00:00
|
|
|
}
|
2017-07-13 12:58:32 +00:00
|
|
|
if (strcmp(argv[1], "-h") == 0 ||
|
|
|
|
strcmp(argv[1], "--help") == 0) {
|
|
|
|
od_usage(instance, argv[0]);
|
|
|
|
return 0;
|
2017-05-25 10:53:48 +00:00
|
|
|
}
|
2018-09-18 16:22:22 +00:00
|
|
|
instance->config_file = argv[1];
|
2017-07-13 10:25:32 +00:00
|
|
|
|
2017-05-25 10:53:48 +00:00
|
|
|
/* read config file */
|
2017-09-15 13:52:11 +00:00
|
|
|
od_error_t error;
|
|
|
|
od_error_init(&error);
|
2017-05-25 10:53:48 +00:00
|
|
|
int rc;
|
2018-12-06 14:23:15 +00:00
|
|
|
rc = od_config_reader_import(&instance->config, &router.rules, &error, instance->config_file);
|
2017-09-15 13:52:11 +00:00
|
|
|
if (rc == -1) {
|
2018-06-04 11:51:13 +00:00
|
|
|
od_error(&instance->logger, "config", NULL, NULL,
|
|
|
|
"%s", error.error);
|
2017-07-13 10:25:32 +00:00
|
|
|
return -1;
|
2017-09-15 13:52:11 +00:00
|
|
|
}
|
2017-07-13 10:25:32 +00:00
|
|
|
|
2018-12-06 14:23:15 +00:00
|
|
|
/* validate configuration */
|
2018-03-06 15:23:52 +00:00
|
|
|
rc = od_config_validate(&instance->config, &instance->logger);
|
2017-07-27 12:44:44 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
|
|
|
|
2018-12-06 14:23:15 +00:00
|
|
|
/* validate rules */
|
|
|
|
rc = od_rules_validate(&router.rules, &instance->config, &instance->logger);
|
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
/* configure logger */
|
2018-03-06 15:23:52 +00:00
|
|
|
od_logger_set_format(&instance->logger, instance->config.log_format);
|
|
|
|
od_logger_set_debug(&instance->logger, instance->config.log_debug);
|
|
|
|
od_logger_set_stdout(&instance->logger, instance->config.log_to_stdout);
|
2017-07-27 11:35:19 +00:00
|
|
|
|
2017-05-25 10:53:48 +00:00
|
|
|
/* run as daemon */
|
2018-03-06 15:23:52 +00:00
|
|
|
if (instance->config.daemonize) {
|
2017-05-25 10:53:48 +00:00
|
|
|
rc = od_daemonize();
|
|
|
|
if (rc == -1)
|
2017-07-13 10:25:32 +00:00
|
|
|
return -1;
|
2017-05-25 10:53:48 +00:00
|
|
|
/* update pid */
|
|
|
|
od_pid_init(&instance->pid);
|
|
|
|
}
|
2017-07-13 10:25:32 +00:00
|
|
|
|
2017-05-25 10:53:48 +00:00
|
|
|
/* reopen log file after config parsing */
|
2018-03-06 15:23:52 +00:00
|
|
|
if (instance->config.log_file) {
|
|
|
|
rc = od_logger_open(&instance->logger, instance->config.log_file);
|
2017-05-25 10:53:48 +00:00
|
|
|
if (rc == -1) {
|
2017-09-21 13:44:19 +00:00
|
|
|
od_error(&instance->logger, "init", NULL, NULL,
|
|
|
|
"failed to open log file '%s'",
|
2018-03-06 15:23:52 +00:00
|
|
|
instance->config.log_file);
|
2017-07-13 10:25:32 +00:00
|
|
|
return -1;
|
2017-05-25 10:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-13 10:25:32 +00:00
|
|
|
|
2017-05-25 10:53:48 +00:00
|
|
|
/* syslog */
|
2018-03-06 15:23:52 +00:00
|
|
|
if (instance->config.log_syslog) {
|
2017-07-26 14:05:29 +00:00
|
|
|
od_logger_open_syslog(&instance->logger,
|
2018-03-06 15:23:52 +00:00
|
|
|
instance->config.log_syslog_ident,
|
|
|
|
instance->config.log_syslog_facility);
|
2017-05-25 10:53:48 +00:00
|
|
|
}
|
2018-03-12 14:03:15 +00:00
|
|
|
od_log(&instance->logger, "init", NULL, NULL, "odyssey (git: %s %s)",
|
2017-05-25 10:53:48 +00:00
|
|
|
OD_VERSION_GIT,
|
|
|
|
OD_VERSION_BUILD);
|
2017-09-21 13:44:19 +00:00
|
|
|
od_log(&instance->logger, "init", NULL, NULL, "");
|
2017-07-13 10:25:32 +00:00
|
|
|
|
2017-06-21 13:04:00 +00:00
|
|
|
/* print configuration */
|
2017-09-21 13:44:19 +00:00
|
|
|
od_log(&instance->logger, "init", NULL, NULL, "using configuration file '%s'",
|
2018-09-18 16:22:22 +00:00
|
|
|
instance->config_file);
|
2017-09-21 13:44:19 +00:00
|
|
|
od_log(&instance->logger, "init", NULL, NULL, "");
|
|
|
|
|
2018-12-06 14:23:15 +00:00
|
|
|
if (instance->config.log_config) {
|
|
|
|
od_config_print(&instance->config, &instance->logger);
|
|
|
|
od_rules_print(&router.rules, &instance->logger);
|
|
|
|
}
|
2017-07-13 10:25:32 +00:00
|
|
|
|
2018-11-14 13:14:13 +00:00
|
|
|
/* set process priority */
|
|
|
|
if (instance->config.priority != 0) {
|
|
|
|
int rc;
|
|
|
|
rc = setpriority(PRIO_PROCESS, 0, instance->config.priority);
|
|
|
|
if (rc == -1) {
|
|
|
|
od_error(&instance->logger, "init", NULL, NULL,
|
|
|
|
"failed to set process priority: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize machinarium */
|
|
|
|
machinarium_set_stack_size(instance->config.coroutine_stack_size);
|
|
|
|
machinarium_set_pool_size(instance->config.resolvers);
|
|
|
|
machinarium_set_coroutine_cache_size(instance->config.cache_coroutine);
|
|
|
|
machinarium_set_msg_cache_gc_size(instance->config.cache_msg_gc_size);
|
|
|
|
rc = machinarium_init();
|
|
|
|
if (rc == -1) {
|
|
|
|
od_error(&instance->logger, "init", NULL, NULL,
|
|
|
|
"failed to init machinarium");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-25 10:53:48 +00:00
|
|
|
/* create pid file */
|
2018-03-06 15:23:52 +00:00
|
|
|
if (instance->config.pid_file)
|
|
|
|
od_pid_create(&instance->pid, instance->config.pid_file);
|
2017-07-13 10:25:32 +00:00
|
|
|
|
2018-03-13 13:26:04 +00:00
|
|
|
/* start system machine thread */
|
2018-12-06 14:23:15 +00:00
|
|
|
rc = od_system_start(&system, &global);
|
2017-06-01 12:45:49 +00:00
|
|
|
if (rc == -1)
|
2017-07-13 10:25:32 +00:00
|
|
|
return -1;
|
2018-03-13 12:13:42 +00:00
|
|
|
|
2018-03-13 13:26:04 +00:00
|
|
|
machine_wait(system.machine);
|
2017-05-25 10:53:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|