Commit 92326a23 authored by Marek Vavrusa's avatar Marek Vavrusa

main: cleanup in server startup

- reorganized
- some code blocks are split
- SIGALRM handler for main thread
parent a4b5df22
......@@ -105,6 +105,10 @@ int log_init()
void log_close()
{
log_truncate();
fflush(stdout);
fflush(stderr);
closelog();
}
......
......@@ -35,12 +35,7 @@
* Defaults.
*/
/*! \brief Default config paths. */
static const char *DEFAULT_CONFIG[] = {
CONFIG_DIR "/" "knot.conf",
};
#define DEFAULT_CONF_COUNT 1 /*!< \brief Number of default config paths. */
#define DEFAULT_CONFIG CONFIG_DIR "/" "knot.conf" /*!< \brief Default config path. */
#define ERROR_BUFFER_SIZE 512 /*!< \brief Error buffer size. */
/*
......@@ -453,7 +448,7 @@ conf_t *s_config = NULL; /*! \brief Singleton config instance. */
void __attribute__ ((constructor)) conf_init()
{
// Create new config
s_config = conf_new(0);
s_config = conf_new(NULL);
if (!s_config) {
return;
}
......@@ -587,15 +582,13 @@ static int conf_strparser(conf_t *conf, const char *src)
* API functions.
*/
conf_t *conf_new(const char* path)
conf_t *conf_new(char* path)
{
conf_t *c = malloc(sizeof(conf_t));
memset(c, 0, sizeof(conf_t));
/* Add path. */
if (path) {
c->filename = strdup(path);
}
c->filename = path;
/* Initialize lists. */
init_list(&c->logs);
......@@ -815,32 +808,9 @@ void conf_free(conf_t *conf)
free(conf);
}
char* conf_find_default()
const char* conf_find_default()
{
/* Try sequentially each default path. */
char *path = NULL;
for (int i = 0; i < DEFAULT_CONF_COUNT; ++i) {
path = strcpath(strdup(DEFAULT_CONFIG[i]));
/* Break, if the path exists. */
struct stat st;
if (stat(path, &st) == 0) {
break;
}
log_server_notice("Config '%s' does not exist.\n",
path);
/* Keep the last item. */
if (i < DEFAULT_CONF_COUNT - 1) {
free(path);
path = NULL;
}
}
log_server_info("Using '%s' as default configuration.\n",
path);
return path;
return DEFAULT_CONFIG;
}
int conf_open(const char* path)
......@@ -850,15 +820,22 @@ int conf_open(const char* path)
return KNOT_EINVAL;
}
/* Find real path of the config file */
char *config_realpath = realpath(path, NULL);
if (config_realpath == NULL) {
return KNOT_ENOMEM;
}
/* Check if exists. */
struct stat st;
if (stat(path, &st) != 0) {
if (stat(config_realpath, &st) != 0) {
return KNOT_ENOENT;
}
/* Create new config. */
conf_t *nconf = conf_new(path);
if (!nconf) {
conf_t *nconf = conf_new(config_realpath);
if (nconf == NULL) {
free(config_realpath);
return KNOT_ENOMEM;
}
......
......@@ -304,7 +304,7 @@ typedef struct conf_hook_t {
* \retval new structure if successful.
* \retval NULL on error.
*/
conf_t *conf_new(const char* path);
conf_t *conf_new(char *path);
/*!
* \brief Register on-update callback.
......@@ -367,13 +367,9 @@ void conf_free(conf_t *conf);
/*!
* \brief Find implicit configuration file.
*
* Ordering:
* 1. ~/.knot/knot.conf (if exists)
* 2. /etc/knot/knot.conf (fallback)
*
* \return Path to implicit configuration file.
*/
char* conf_find_default();
const char* conf_find_default();
/*!
* \brief Open singleton configuration from file.
......
......@@ -417,8 +417,7 @@ int main(int argc, char **argv)
/* Parse command line arguments */
int c = 0, li = 0, rc = 0;
unsigned flags = F_NULL;
char *config_fn = NULL;
char *default_config = conf_find_default();
const char *config_fn = conf_find_default();
/* Remote server descriptor. */
const char *r_addr = NULL;
......@@ -479,7 +478,7 @@ int main(int argc, char **argv)
flags |= F_INTERACTIVE;
break;
case 'c':
config_fn = strdup(optarg);
config_fn = optarg;
break;
case 'V':
rc = 0;
......@@ -522,9 +521,7 @@ int main(int argc, char **argv)
/* Open config, allow if not exists. */
if (conf_open(config_fn) != KNOT_EOK) {
if(conf_open(default_config) != KNOT_EOK) {
flags |= F_NOCONF;
}
flags |= F_NOCONF;
}
/* Create remote iface if not present in config. */
......@@ -583,8 +580,6 @@ exit:
/* Finish */
knot_tsig_key_free(&r_key);
log_close();
free(config_fn);
free(default_config);
return rc;
}
......
This diff is collapsed.
......@@ -183,17 +183,11 @@ static void remove_ifacelist(struct ref_t *p)
*/
static int reconfigure_sockets(const struct conf_t *conf, server_t *s)
{
/*! \todo This requires locking to disable parallel updates (issue #278).
* However, this is only used when RCU is read-locked, so count with that.
*/
/* Lock configuration. */
rcu_read_lock();
/* Prepare helper lists. */
char addr_str[SOCKADDR_STRLEN] = {0};
int bound = 0;
iface_t *m = 0;
ifacelist_t *oldlist = s->ifaces;
ifacelist_t *newlist = malloc(sizeof(ifacelist_t));
ref_init(&newlist->ref, &remove_ifacelist);
ref_retain(&newlist->ref);
......@@ -246,13 +240,7 @@ static int reconfigure_sockets(const struct conf_t *conf, server_t *s)
}
/* Publish new list. */
ifacelist_t *oldlist = rcu_xchg_pointer(&s->ifaces, newlist);
/* Unlock configuration. */
rcu_read_unlock();
/* Ensure no one is reading old interfaces. */
synchronize_rcu();
s->ifaces = newlist;
/* Update TCP+UDP ifacelist (reload all threads). */
for (unsigned proto = IO_UDP; proto <= IO_TCP; ++proto) {
......@@ -358,10 +346,10 @@ static int server_init_handler(server_t *server, int index, int thread_count,
return KNOT_EOK;
}
static int server_free_handler(iohandler_t *h)
static void server_free_handler(iohandler_t *h)
{
if (!h || !h->server) {
return KNOT_EINVAL;
if (h == NULL || h->server == NULL) {
return;
}
/* Wait for threads to finish */
......@@ -374,7 +362,6 @@ static int server_free_handler(iohandler_t *h)
dt_delete(&h->unit);
free(h->thread_state);
memset(h, 0, sizeof(iohandler_t));
return KNOT_EOK;
}
int server_start(server_t *s)
......@@ -406,24 +393,22 @@ int server_start(server_t *s)
return ret;
}
int server_wait(server_t *s)
void server_wait(server_t *s)
{
if (!s) return KNOT_EINVAL;
if (s == NULL) {
return;
}
xfr_join(s->xfr);
dt_join(s->iosched);
xfr_join(s->xfr);
if (s->tu_size == 0) {
return KNOT_EOK;
return;
}
int ret = KNOT_EOK;
for (unsigned i = 0; i < IO_COUNT; ++i) {
if ((ret = server_free_handler(s->handler + i)) != KNOT_EOK) {
break;
}
server_free_handler(s->handler + i);
}
return ret;
}
int server_reload(server_t *server, const char *cf)
......@@ -513,7 +498,7 @@ static int reconfigure_threads(const struct conf_t *conf, server_t *server)
/* Free old handlers */
if (server->tu_size > 0) {
for (unsigned i = 0; i < IO_COUNT; ++i) {
ret = server_free_handler(server->handler + i);
server_free_handler(server->handler + i);
}
}
......
......@@ -154,10 +154,8 @@ int server_start(server_t *server);
*
* \param server Server structure to be used for operation.
*
* \retval 0 On success (EOK).
* \retval <0 If an error occured (EINVAL).
*/
int server_wait(server_t *server);
void server_wait(server_t *server);
/*!
* \brief Reload server configuration.
......
......@@ -31,7 +31,7 @@ int main(int argc, char *argv[])
// Test 1: Allocate new config
const char *config_fn = "rc:/sample_conf";
conf_t *conf = conf_new(config_fn);
conf_t *conf = conf_new(strdup(config_fn));
ok(conf != 0, "config_new()");
// Test 2: Parse config
......
......@@ -26,7 +26,7 @@ static void interrupt_handle(int s)
/*! API: run tests. */
int main(int argc, char *argv[])
{
plan(3);
plan(2);
server_t server;
int ret = 0;
......@@ -55,8 +55,7 @@ int main(int argc, char *argv[])
server_stop(&server);
/* Wait for server to finish. */
ret = server_wait(&server);
ok(ret == KNOT_EOK, "server: waiting for finish");
server_wait(&server);
/* Wait for server to finish. */
server_deinit(&server);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment