2017-05-25 10:53:48 +00:00
|
|
|
|
|
|
|
/*
|
2017-07-05 12:42:49 +00:00
|
|
|
* Odissey.
|
2017-05-25 10:53:48 +00:00
|
|
|
*
|
2017-07-05 12:42:49 +00:00
|
|
|
* Advanced 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>
|
2017-05-31 15:47:15 +00:00
|
|
|
#include <inttypes.h>
|
2017-05-25 10:53:48 +00:00
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <machinarium.h>
|
2017-06-07 11:50:58 +00:00
|
|
|
#include <shapito.h>
|
2017-05-25 10:53:48 +00:00
|
|
|
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/macro.h"
|
|
|
|
#include "sources/version.h"
|
2017-08-08 13:50:50 +00:00
|
|
|
#include "sources/atomic.h"
|
2017-11-27 12:54:16 +00:00
|
|
|
#include "sources/util.h"
|
|
|
|
#include "sources/error.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/list.h"
|
|
|
|
#include "sources/pid.h"
|
|
|
|
#include "sources/id.h"
|
2017-07-26 14:05:29 +00:00
|
|
|
#include "sources/logger.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/daemon.h"
|
|
|
|
#include "sources/scheme.h"
|
2017-07-14 13:40:31 +00:00
|
|
|
#include "sources/scheme_mgr.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/config.h"
|
|
|
|
#include "sources/msg.h"
|
|
|
|
#include "sources/system.h"
|
|
|
|
#include "sources/server.h"
|
|
|
|
#include "sources/server_pool.h"
|
|
|
|
#include "sources/client.h"
|
|
|
|
#include "sources/client_pool.h"
|
|
|
|
#include "sources/route_id.h"
|
|
|
|
#include "sources/route.h"
|
|
|
|
#include "sources/route_pool.h"
|
|
|
|
#include "sources/io.h"
|
2017-09-15 12:58:29 +00:00
|
|
|
#include "sources/instance.h"
|
2017-07-05 12:15:17 +00:00
|
|
|
#include "sources/router.h"
|
|
|
|
#include "sources/console.h"
|
|
|
|
#include "sources/pooler.h"
|
|
|
|
#include "sources/periodic.h"
|
|
|
|
#include "sources/relay.h"
|
|
|
|
#include "sources/relay_pool.h"
|
|
|
|
#include "sources/frontend.h"
|
2017-05-25 10:53:48 +00:00
|
|
|
|
|
|
|
void od_instance_init(od_instance_t *instance)
|
|
|
|
{
|
|
|
|
od_pid_init(&instance->pid);
|
2017-07-26 14:05:29 +00:00
|
|
|
od_logger_init(&instance->logger, &instance->pid);
|
2017-05-25 10:53:48 +00:00
|
|
|
od_scheme_init(&instance->scheme);
|
2017-07-14 13:40:31 +00:00
|
|
|
od_schememgr_init(&instance->scheme_mgr);
|
2017-06-20 13:33:20 +00:00
|
|
|
od_idmgr_init(&instance->id_mgr);
|
2018-02-12 13:27:06 +00:00
|
|
|
shapito_cache_init(&instance->stream_cache);
|
2017-07-19 11:29:23 +00:00
|
|
|
instance->config_file = NULL;
|
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);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void od_instance_free(od_instance_t *instance)
|
|
|
|
{
|
|
|
|
if (instance->scheme.pid_file)
|
|
|
|
od_pid_unlink(&instance->pid, instance->scheme.pid_file);
|
|
|
|
od_scheme_free(&instance->scheme);
|
2017-07-26 14:05:29 +00:00
|
|
|
od_logger_close(&instance->logger);
|
2018-02-12 13:27:06 +00:00
|
|
|
shapito_cache_free(&instance->stream_cache);
|
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,
|
|
|
|
"odissey (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
|
|
|
}
|
|
|
|
|
|
|
|
int od_instance_main(od_instance_t *instance, int argc, char **argv)
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
}
|
2017-07-19 11:29:23 +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-08-03 14:03:37 +00:00
|
|
|
uint64_t scheme_version;
|
2017-08-01 15:16:29 +00:00
|
|
|
scheme_version = od_schememgr_version(&instance->scheme_mgr);
|
|
|
|
|
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;
|
2017-09-15 13:52:11 +00:00
|
|
|
rc = od_config_load(&instance->scheme, &error,
|
2017-08-01 15:16:29 +00:00
|
|
|
instance->config_file,
|
|
|
|
scheme_version);
|
2017-09-15 13:52:11 +00:00
|
|
|
if (rc == -1) {
|
2017-09-21 13:44:19 +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
|
|
|
|
2017-07-27 12:44:44 +00:00
|
|
|
/* validate configuration scheme */
|
|
|
|
rc = od_scheme_validate(&instance->scheme, &instance->logger);
|
|
|
|
if (rc == -1)
|
|
|
|
return -1;
|
|
|
|
|
2017-09-21 13:44:19 +00:00
|
|
|
/* set log in format */
|
|
|
|
od_logger_set_format(&instance->logger, instance->scheme.log_format);
|
2017-07-27 12:24:38 +00:00
|
|
|
|
2017-07-27 11:35:19 +00:00
|
|
|
/* set log debug messages */
|
2017-07-26 14:05:29 +00:00
|
|
|
od_logger_set_debug(&instance->logger, instance->scheme.log_debug);
|
2017-07-27 11:35:19 +00:00
|
|
|
|
|
|
|
/* set log to stdout */
|
2017-07-28 13:23:05 +00:00
|
|
|
od_logger_set_stdout(&instance->logger, instance->scheme.log_to_stdout);
|
2017-07-27 11:35:19 +00:00
|
|
|
|
2018-02-13 14:18:13 +00:00
|
|
|
/* set cache limits */
|
2018-02-14 11:55:38 +00:00
|
|
|
shapito_cache_set_limit(&instance->stream_cache, instance->scheme.cache);
|
|
|
|
shapito_cache_set_limit_size(&instance->stream_cache, instance->scheme.cache_chunk);
|
2018-02-13 14:18:13 +00:00
|
|
|
|
2017-05-25 10:53:48 +00:00
|
|
|
/* run as daemon */
|
|
|
|
if (instance->scheme.daemonize) {
|
|
|
|
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-08-17 13:56:32 +00:00
|
|
|
/* init machinarium machinery */
|
|
|
|
machinarium_init();
|
|
|
|
|
2017-05-25 10:53:48 +00:00
|
|
|
/* reopen log file after config parsing */
|
|
|
|
if (instance->scheme.log_file) {
|
2017-07-26 14:05:29 +00:00
|
|
|
rc = od_logger_open(&instance->logger, instance->scheme.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'",
|
2017-05-25 10:53:48 +00:00
|
|
|
instance->scheme.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 */
|
2017-09-18 13:14:52 +00:00
|
|
|
if (instance->scheme.log_syslog) {
|
2017-07-26 14:05:29 +00:00
|
|
|
od_logger_open_syslog(&instance->logger,
|
2017-09-18 13:14:52 +00:00
|
|
|
instance->scheme.log_syslog_ident,
|
|
|
|
instance->scheme.log_syslog_facility);
|
2017-05-25 10:53:48 +00:00
|
|
|
}
|
2017-09-21 13:44:19 +00:00
|
|
|
od_log(&instance->logger, "init", NULL, NULL, "odissey (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'",
|
2017-07-19 11:29:23 +00:00
|
|
|
instance->config_file);
|
2017-09-21 13:44:19 +00:00
|
|
|
od_log(&instance->logger, "init", NULL, NULL, "");
|
|
|
|
|
2017-06-21 12:58:59 +00:00
|
|
|
if (instance->scheme.log_config)
|
2017-07-26 14:05:29 +00:00
|
|
|
od_scheme_print(&instance->scheme, &instance->logger, 0);
|
2017-07-13 10:25:32 +00:00
|
|
|
|
2017-05-25 10:53:48 +00:00
|
|
|
/* create pid file */
|
|
|
|
if (instance->scheme.pid_file)
|
|
|
|
od_pid_create(&instance->pid, instance->scheme.pid_file);
|
2017-07-13 10:25:32 +00:00
|
|
|
|
2017-06-20 13:33:20 +00:00
|
|
|
/* seed id manager */
|
2017-11-24 12:19:14 +00:00
|
|
|
od_idmgr_seed(&instance->id_mgr);
|
2017-06-20 15:39:52 +00:00
|
|
|
|
2018-02-02 13:44:47 +00:00
|
|
|
/* is multi-worker deploy */
|
|
|
|
instance->is_shared = instance->scheme.workers > 1;
|
|
|
|
|
2018-02-02 12:50:23 +00:00
|
|
|
/* prepare system services */
|
|
|
|
od_pooler_t pooler;
|
|
|
|
od_pooler_init(&pooler, instance);
|
|
|
|
|
2017-05-26 13:44:42 +00:00
|
|
|
od_router_t router;
|
2017-06-24 14:59:28 +00:00
|
|
|
od_console_t console;
|
2017-06-05 14:05:39 +00:00
|
|
|
od_periodic_t periodic;
|
2017-06-01 12:45:49 +00:00
|
|
|
od_relaypool_t relay_pool;
|
2018-02-02 12:50:23 +00:00
|
|
|
|
|
|
|
od_system_t *system;
|
|
|
|
system = &pooler.system;
|
|
|
|
system->instance = instance;
|
|
|
|
system->pooler = &pooler;
|
|
|
|
system->router = &router;
|
|
|
|
system->console = &console;
|
|
|
|
system->periodic = &periodic;
|
|
|
|
system->relay_pool = &relay_pool;
|
|
|
|
|
|
|
|
od_router_init(&router, system);
|
|
|
|
od_console_init(&console, system);
|
|
|
|
od_periodic_init(&periodic, system);
|
|
|
|
od_relaypool_init(&relay_pool);
|
2017-07-13 10:25:32 +00:00
|
|
|
|
2017-06-05 14:05:39 +00:00
|
|
|
/* start pooler machine thread */
|
|
|
|
rc = od_pooler_start(&pooler);
|
2017-06-01 12:45:49 +00:00
|
|
|
if (rc == -1)
|
2017-07-13 10:25:32 +00:00
|
|
|
return -1;
|
2017-05-26 11:49:17 +00:00
|
|
|
machine_wait(pooler.machine);
|
2017-05-25 10:53:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|