Commit ea3adb64 authored by Marek Vavrusa's avatar Marek Vavrusa

Fixed config path for daemon mode.

Server automatically translates relative config path to absolute.
parent cfe00d24
......@@ -28,27 +28,6 @@ static const char *DEFAULT_CONFIG[2] = {
* Utilities.
*/
/*!
* \brief Create new string from a concatenation of s1 and s2.
* \param s1 First string.
* \param s2 Second string.
* \retval Newly allocated string on success.
* \retval NULL on error.
*/
static char* strcdup(const char *s1, const char *s2)
{
size_t slen = strlen(s1);
size_t nlen = slen + strlen(s2) + 1;
char* dst = malloc(nlen);
if (!dst) {
return 0;
}
memcpy(dst, s1, slen);
strcpy(dst + slen, s2); // With trailing '\0'
return dst;
}
/*!
* \brief Recursively create directories.
*
......@@ -140,7 +119,7 @@ void cf_error(const char *msg)
log_server_error("Config '%s' - %s on line %d (current token '%s').\n",
new_config->filename, msg, yylineno, yytext);
_parser_res = -1;
_parser_res = KNOT_EPARSEFAIL;
}
/*
......@@ -327,10 +306,10 @@ void __attribute__ ((destructor)) conf_deinit()
static int conf_fparser(conf_t *conf)
{
if (!conf->filename) {
return -1;
return KNOT_EINVAL;
}
int ret = 0;
int ret = KNOT_EOK;
pthread_mutex_lock(&_parser_lock);
// {
// Hook new configuration
......@@ -339,11 +318,11 @@ static int conf_fparser(conf_t *conf)
_parser_remaining = -1;
if (_parser_src == 0) {
pthread_mutex_unlock(&_parser_lock);
return -2;
return KNOT_ENOENT;
}
// Parse config
_parser_res = 0;
_parser_res = KNOT_EOK;
cf_read_hook = cf_read_file;
cf_parse();
ret = _parser_res;
......@@ -360,10 +339,10 @@ static int conf_fparser(conf_t *conf)
static int conf_strparser(conf_t *conf, const char *src)
{
if (!src) {
return -1;
return KNOT_EINVAL;
}
int ret = 0;
int ret = KNOT_EOK;
pthread_mutex_lock(&_parser_lock);
// {
// Hook new configuration
......@@ -374,11 +353,11 @@ static int conf_strparser(conf_t *conf, const char *src)
_parser_src = 0;
_parser_remaining = -1;
pthread_mutex_unlock(&_parser_lock);
return -2;
return KNOT_ENOENT;
}
// Parse config
_parser_res = 0;
_parser_res = KNOT_EOK;
cf_read_hook = cf_read_mem;
char *oldfn = new_config->filename;
new_config->filename = "(stdin)";
......@@ -629,6 +608,20 @@ int conf_open(const char* path)
return KNOT_EOK;
}
char* strcdup(const char *s1, const char *s2)
{
size_t slen = strlen(s1);
size_t nlen = slen + strlen(s2) + 1;
char* dst = malloc(nlen);
if (!dst) {
return 0;
}
memcpy(dst, s1, slen);
strcpy(dst + slen, s2); // With trailing '\0'
return dst;
}
char* strcpath(char *path)
{
// Remote trailing slash
......
......@@ -292,6 +292,17 @@ static inline void conf_read_unlock() {
* Utilities.
*/
/*!
* \brief Create new string from a concatenation of s1 and s2.
*
* \param s1 First string.
* \param s2 Second string.
*
* \retval Newly allocated string on success.
* \retval NULL on error.
*/
char* strcdup(const char *s1, const char *s2);
/*!
* \brief Normalize file path and expand '~' placeholders.
*
......
......@@ -58,12 +58,12 @@ int main(int argc, char **argv)
int c = 0;
int verbose = 0;
int daemonize = 0;
const char* config_fn = 0;
char* config_fn = 0;
while ((c = getopt (argc, argv, "c:dvVh")) != -1) {
switch (c)
{
case 'c':
config_fn = optarg;
config_fn = strdup(optarg);
break;
case 'd':
daemonize = 1;
......@@ -85,20 +85,25 @@ int main(int argc, char **argv)
}
}
// Now check if we want to daemonize
if (daemonize) {
if (daemon(1, 0) != 0) {
fprintf(stderr, "Daemonization failed, "
"shutting down...\n");
return 1;
}
}
// Setup event queue
evqueue_set(evqueue_new());
// Initialize log
log_init();
// Now check if we want to daemonize
if (daemonize) {
if (daemon(1, 0) != 0) {
log_server_fatal("Daemonization failed, "
"shutting down...\n");
log_close();
return 1;
}
// Verbose mode
if (verbose) {
int mask = LOG_MASK(LOG_INFO)|LOG_MASK(LOG_DEBUG);
log_levels_add(LOGT_STDOUT, LOG_ANY, mask);
}
// Create server
......@@ -112,17 +117,39 @@ int main(int argc, char **argv)
conf_read_unlock();
// Find implicit configuration file
char *default_fn = 0;
if (!config_fn) {
default_fn = conf_find_default();
config_fn = default_fn;
config_fn = conf_find_default();
}
// Find absolute path for config file
if (config_fn[0] != '/')
{
// Get absolute path to cwd
size_t cwbuflen = 64;
char *cwbuf = malloc((cwbuflen + 1) * sizeof(char));
while (getcwd(cwbuf, cwbuflen) == 0) {
cwbuflen += 64;
cwbuf = realloc(cwbuf, (cwbuflen + 1) * sizeof(char));
}
cwbuflen = strlen(cwbuf);
// Append ending slash
if (cwbuf[cwbuflen - 1] != '/') {
cwbuf = strcat(cwbuf, "/");
}
// Assemble path to config file
char *abs_cfg = strcdup(cwbuf, config_fn);
free(config_fn);
free(cwbuf);
config_fn = abs_cfg;
}
// Open configuration
log_server_info("Parsing configuration...\n");
log_server_info("Parsing configuration '%s' ...\n", config_fn);
if (conf_open(config_fn) != KNOT_EOK) {
log_server_error("Failed to parse configuration '%s'.\n",
log_server_error("Failed to parse configuration file '%s'.\n",
config_fn);
log_server_warning("No zone served.\n");
......@@ -130,15 +157,8 @@ int main(int argc, char **argv)
log_server_info("Configured %d interfaces and %d zones.\n",
conf()->ifaces_count, conf()->zones_count);
}
log_server_info("\n");
// Verbose mode
if (verbose) {
int mask = LOG_MASK(LOG_INFO)|LOG_MASK(LOG_DEBUG);
log_levels_add(LOGT_STDOUT, LOG_ANY, mask);
}
// Create server instance
char* pidfile = pid_filename();
......@@ -164,7 +184,6 @@ int main(int argc, char **argv)
"PID = %ld\n", (long)getpid());
}
log_server_info("PID stored in %s\n", pidfile);
log_server_info("\n");
// Setup signal blocking
sigset_t emptyset;
......@@ -200,11 +219,23 @@ int main(int argc, char **argv)
if (sig_req_reload) {
log_server_info("Reloading configuration...\n");
sig_req_reload = 0;
if (conf_open(config_fn) == 0) {
int cf_ret = cf_ret = conf_open(config_fn);
switch (cf_ret) {
case KNOT_EOK:
log_server_info("Configuration "
"reloaded.\n");
break;
case KNOT_ENOENT:
log_server_error("Configuration "
"file '%s' "
"not found.\n",
conf()->filename);
break;
default:
log_server_error("Configuration "
"reload failed.\n");
break;
}
}
/* Events. */
......@@ -249,7 +280,7 @@ int main(int argc, char **argv)
evqueue_free(&q);
// Free default config filename if exists
free(default_fn);
free(config_fn);
return res;
}
......@@ -72,7 +72,7 @@ int log_init()
int emask = LOG_MASK(LOG_WARNING)|LOG_MASK(LOG_ERR)|LOG_MASK(LOG_FATAL);
int imask = LOG_MASK(LOG_INFO)|LOG_MASK(LOG_NOTICE);
ret = log_setup(0);
log_levels_set(LOGT_SYSLOG, LOG_ANY, emask);
log_levels_set(LOGT_SYSLOG, LOG_ANY, imask|emask);
log_levels_set(LOGT_STDERR, LOG_ANY, emask);
log_levels_set(LOGT_STDOUT, LOG_ANY, imask);
......
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