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>
|
|
|
|
|
|
|
|
#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-08-28 14:43:46 +00:00
|
|
|
od_id_mgr_init(&instance->id_mgr);
|
2018-02-02 13:44:47 +00:00
|
|
|
instance->is_shared = 0;
|
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
|
|
|
{
|
|
|
|
/* 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-08-28 14:43:46 +00:00
|
|
|
char *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-09-18 15:26:25 +00:00
|
|
|
rc = od_config_reader_import(&instance->config, &error, 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-03-06 15:23:52 +00:00
|
|
|
/* validate configuration config */
|
|
|
|
rc = od_config_validate(&instance->config, &instance->logger);
|
2017-07-27 12:44:44 +00:00
|
|
|
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
|
|
|
|
2018-08-28 14:43:46 +00:00
|
|
|
/* initialize machinarium */
|
2018-06-04 11:51:13 +00:00
|
|
|
machinarium_set_stack_size(instance->config.coroutine_stack_size);
|
2018-03-06 15:23:52 +00:00
|
|
|
machinarium_set_pool_size(instance->config.resolvers);
|
|
|
|
machinarium_set_coroutine_cache_size(instance->config.cache_coroutine);
|
2018-09-05 13:04:36 +00:00
|
|
|
machinarium_set_msg_cache_gc_size(instance->config.cache_msg_gc_size);
|
2018-06-04 11:51:13 +00:00
|
|
|
rc = machinarium_init();
|
|
|
|
if (rc == -1) {
|
|
|
|
od_error(&instance->logger, "init", NULL, NULL,
|
|
|
|
"failed to init machinarium");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-17 13:56: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-08-28 14:43:46 +00:00
|
|
|
config_file);
|
2017-09-21 13:44:19 +00:00
|
|
|
od_log(&instance->logger, "init", NULL, NULL, "");
|
|
|
|
|
2018-03-06 15:23:52 +00:00
|
|
|
if (instance->config.log_config)
|
|
|
|
od_config_print(&instance->config, &instance->logger, 0);
|
2017-07-13 10:25:32 +00:00
|
|
|
|
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
|
|
|
|
2017-06-20 13:33:20 +00:00
|
|
|
/* seed id manager */
|
2018-08-28 14:43:46 +00:00
|
|
|
od_id_mgr_seed(&instance->id_mgr);
|
2017-06-20 15:39:52 +00:00
|
|
|
|
2018-02-02 13:44:47 +00:00
|
|
|
/* is multi-worker deploy */
|
2018-03-06 15:23:52 +00:00
|
|
|
instance->is_shared = instance->config.workers > 1;
|
2018-02-02 13:44:47 +00:00
|
|
|
|
2018-03-13 13:17:27 +00:00
|
|
|
/* prepare global services */
|
2018-03-13 13:26:04 +00:00
|
|
|
od_system_t system;
|
2018-03-13 13:37:53 +00:00
|
|
|
od_system_init(&system);
|
2018-02-02 12:50:23 +00:00
|
|
|
|
2017-05-26 13:44:42 +00:00
|
|
|
od_router_t router;
|
2017-06-24 14:59:28 +00:00
|
|
|
od_console_t console;
|
2018-03-02 13:12:32 +00:00
|
|
|
od_cron_t cron;
|
2018-08-28 14:43:46 +00:00
|
|
|
od_worker_pool_t worker_pool;
|
2018-02-02 12:50:23 +00:00
|
|
|
|
2018-03-13 13:17:27 +00:00
|
|
|
od_global_t *global;
|
2018-03-13 13:26:04 +00:00
|
|
|
global = &system.global;
|
2018-03-13 13:17:27 +00:00
|
|
|
global->instance = instance;
|
2018-03-13 13:26:04 +00:00
|
|
|
global->system = &system;
|
2018-03-13 13:17:27 +00:00
|
|
|
global->router = &router;
|
|
|
|
global->console = &console;
|
|
|
|
global->cron = &cron;
|
|
|
|
global->worker_pool = &worker_pool;
|
|
|
|
|
|
|
|
od_router_init(&router, global);
|
|
|
|
od_console_init(&console, global);
|
|
|
|
od_cron_init(&cron, global);
|
2018-08-28 14:43:46 +00:00
|
|
|
od_worker_pool_init(&worker_pool);
|
2017-07-13 10:25:32 +00:00
|
|
|
|
2018-03-13 13:26:04 +00:00
|
|
|
/* start system machine thread */
|
|
|
|
rc = od_system_start(&system);
|
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;
|
|
|
|
}
|