2017-05-25 10:53:48 +00:00
|
|
|
|
|
|
|
/*
|
2017-06-07 11:50:58 +00:00
|
|
|
* ODISSEY.
|
2017-05-25 10:53:48 +00:00
|
|
|
*
|
|
|
|
* PostgreSQL connection pooler and request router.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
#include "od_macro.h"
|
|
|
|
#include "od_version.h"
|
|
|
|
#include "od_list.h"
|
|
|
|
#include "od_pid.h"
|
2017-06-20 15:39:52 +00:00
|
|
|
#include "od_id.h"
|
2017-05-25 10:53:48 +00:00
|
|
|
#include "od_syslog.h"
|
|
|
|
#include "od_log.h"
|
|
|
|
#include "od_daemon.h"
|
|
|
|
#include "od_scheme.h"
|
|
|
|
#include "od_lex.h"
|
|
|
|
#include "od_config.h"
|
2017-05-26 13:44:42 +00:00
|
|
|
#include "od_msg.h"
|
2017-05-26 12:17:45 +00:00
|
|
|
#include "od_system.h"
|
2017-05-26 13:44:42 +00:00
|
|
|
#include "od_instance.h"
|
|
|
|
|
|
|
|
#include "od_server.h"
|
|
|
|
#include "od_server_pool.h"
|
|
|
|
#include "od_client.h"
|
|
|
|
#include "od_client_pool.h"
|
|
|
|
#include "od_route_id.h"
|
|
|
|
#include "od_route.h"
|
|
|
|
#include "od_route_pool.h"
|
|
|
|
#include "od_io.h"
|
|
|
|
|
|
|
|
#include "od_router.h"
|
2017-06-05 13:45:28 +00:00
|
|
|
#include "od_pooler.h"
|
2017-06-05 14:05:39 +00:00
|
|
|
#include "od_periodic.h"
|
2017-05-26 11:49:17 +00:00
|
|
|
#include "od_relay.h"
|
2017-06-01 12:45:49 +00:00
|
|
|
#include "od_relay_pool.h"
|
|
|
|
#include "od_frontend.h"
|
2017-05-25 10:53:48 +00:00
|
|
|
|
|
|
|
void od_instance_init(od_instance_t *instance)
|
|
|
|
{
|
|
|
|
od_pid_init(&instance->pid);
|
|
|
|
od_syslog_init(&instance->syslog);
|
|
|
|
od_log_init(&instance->log, &instance->pid, &instance->syslog);
|
|
|
|
od_scheme_init(&instance->scheme);
|
|
|
|
od_config_init(&instance->config, &instance->log, &instance->scheme);
|
2017-06-20 13:33:20 +00:00
|
|
|
od_idmgr_init(&instance->id_mgr);
|
2017-05-25 10:53:48 +00:00
|
|
|
|
2017-06-14 12:14:53 +00:00
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGINT);
|
|
|
|
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);
|
|
|
|
od_config_close(&instance->config);
|
|
|
|
od_log_close(&instance->log);
|
|
|
|
od_syslog_close(&instance->syslog);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
od_usage(od_instance_t *instance, char *path)
|
|
|
|
{
|
2017-06-21 12:18:48 +00:00
|
|
|
od_log(&instance->log, "odissey (git: %s %s)",
|
2017-05-25 10:53:48 +00:00
|
|
|
OD_VERSION_GIT,
|
|
|
|
OD_VERSION_BUILD);
|
2017-05-31 15:47:15 +00:00
|
|
|
od_log(&instance->log, "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]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
char *config_file;
|
|
|
|
if (argc == 2) {
|
|
|
|
if (strcmp(argv[1], "-h") == 0 ||
|
|
|
|
strcmp(argv[1], "--help") == 0) {
|
|
|
|
od_usage(instance, argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
config_file = argv[1];
|
|
|
|
}
|
|
|
|
/* read config file */
|
|
|
|
int rc;
|
|
|
|
rc = od_config_open(&instance->config, config_file);
|
|
|
|
if (rc == -1)
|
|
|
|
return 1;
|
|
|
|
rc = od_config_parse(&instance->config);
|
|
|
|
if (rc == -1)
|
|
|
|
return 1;
|
2017-06-01 09:38:46 +00:00
|
|
|
/* set log debug on/off */
|
|
|
|
od_logset_debug(&instance->log, instance->scheme.log_debug);
|
2017-05-25 10:53:48 +00:00
|
|
|
/* run as daemon */
|
|
|
|
if (instance->scheme.daemonize) {
|
|
|
|
rc = od_daemonize();
|
|
|
|
if (rc == -1)
|
|
|
|
return 1;
|
|
|
|
/* update pid */
|
|
|
|
od_pid_init(&instance->pid);
|
|
|
|
}
|
|
|
|
/* reopen log file after config parsing */
|
|
|
|
if (instance->scheme.log_file) {
|
|
|
|
rc = od_log_open(&instance->log, instance->scheme.log_file);
|
|
|
|
if (rc == -1) {
|
2017-06-20 15:39:52 +00:00
|
|
|
od_error(&instance->log, NULL, "failed to open log file '%s'",
|
2017-05-25 10:53:48 +00:00
|
|
|
instance->scheme.log_file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* syslog */
|
|
|
|
if (instance->scheme.syslog) {
|
|
|
|
od_syslog_open(&instance->syslog,
|
|
|
|
instance->scheme.syslog_ident,
|
|
|
|
instance->scheme.syslog_facility);
|
|
|
|
}
|
2017-06-21 12:18:48 +00:00
|
|
|
od_log(&instance->log, "odissey (git: %s %s)",
|
2017-05-25 10:53:48 +00:00
|
|
|
OD_VERSION_GIT,
|
|
|
|
OD_VERSION_BUILD);
|
2017-05-31 15:47:15 +00:00
|
|
|
od_log(&instance->log, "");
|
2017-05-25 10:53:48 +00:00
|
|
|
/* validate configuration scheme */
|
|
|
|
rc = od_scheme_validate(&instance->scheme, &instance->log);
|
|
|
|
if (rc == -1)
|
|
|
|
return 1;
|
|
|
|
/* print configuration scheme */
|
2017-06-21 12:58:59 +00:00
|
|
|
if (instance->scheme.log_config)
|
2017-06-01 09:45:49 +00:00
|
|
|
od_scheme_print(&instance->scheme, &instance->log);
|
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-06-20 13:33:20 +00:00
|
|
|
/* seed id manager */
|
2017-06-20 15:39:52 +00:00
|
|
|
rc = od_idmgr_seed(&instance->id_mgr);
|
|
|
|
if (rc == -1)
|
|
|
|
od_error(&instance->log, NULL, "failed to open random source device");
|
|
|
|
|
2017-05-26 13:44:42 +00:00
|
|
|
/* run system services */
|
|
|
|
od_router_t router;
|
2017-06-05 14:05:39 +00:00
|
|
|
od_periodic_t periodic;
|
|
|
|
od_pooler_t pooler;
|
2017-06-01 12:45:49 +00:00
|
|
|
od_relaypool_t relay_pool;
|
2017-05-26 12:17:45 +00:00
|
|
|
od_system_t system = {
|
2017-06-01 12:45:49 +00:00
|
|
|
.pooler = &pooler,
|
|
|
|
.router = &router,
|
2017-06-05 14:05:39 +00:00
|
|
|
.periodic = &periodic,
|
2017-06-01 12:45:49 +00:00
|
|
|
.relay_pool = &relay_pool,
|
|
|
|
.instance = instance
|
2017-05-26 12:17:45 +00:00
|
|
|
};
|
2017-06-05 13:45:28 +00:00
|
|
|
od_router_init(&router, &system);
|
2017-06-05 14:05:39 +00:00
|
|
|
od_periodic_init(&periodic, &system);
|
2017-05-26 12:17:45 +00:00
|
|
|
od_pooler_init(&pooler, &system);
|
2017-06-05 14:05:39 +00:00
|
|
|
rc = od_relaypool_init(&relay_pool, &system, instance->scheme.workers);
|
2017-05-26 13:44:42 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return 1;
|
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)
|
|
|
|
return 1;
|
2017-06-07 14:59:13 +00:00
|
|
|
/* start worker threads */
|
2017-06-01 12:45:49 +00:00
|
|
|
rc = od_relaypool_start(&relay_pool);
|
2017-05-26 11:49:17 +00:00
|
|
|
if (rc == -1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
machine_wait(pooler.machine);
|
2017-05-25 10:53:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|