[core] collect more config logic into configfile.c

This commit is contained in:
Glenn Strauss 2019-11-16 13:49:14 -05:00
parent ed62e354ff
commit 65ef0a3d69
5 changed files with 127 additions and 95 deletions

View File

@ -302,6 +302,8 @@ typedef struct {
unsigned char preflight_check;
unsigned char enable_cores;
unsigned char compat_module_load;
unsigned char config_deprecated;
unsigned char config_unsupported;
unsigned char systemd_socket_activation;
unsigned char errorlog_use_syslog;
const buffer *errorlog_file;
@ -316,6 +318,8 @@ typedef struct {
buffer *pid_file;
buffer *modules_dir;
array *modules;
array *config_touched;
array empty_array;
} server_config;
typedef struct server_socket {
@ -400,17 +404,12 @@ struct server {
/* config-file */
void *config_data_base;
array *config_context;
array empty_array;
/* members used at start-up or rarely used */
server_socket_array srv_sockets;
server_socket_array srv_sockets_inherited;
buffer_plugin plugins;
array *config_touched;
unsigned char config_deprecated;
unsigned char config_unsupported;
int event_handler;
time_t startup_ts;

View File

@ -37,7 +37,7 @@ void config_get_config_cond_info(server *srv, uint32_t idx, config_cond_info *cf
int config_plugin_values_init(server * const srv, void *p_d, const config_plugin_keys_t * const cpk, const char * const mname) {
plugin_data_base * const p = (plugin_data_base *)p_d;
array * const touched = srv->config_touched;
array * const touched = srv->srvconf.config_touched;
unsigned char matches[4096]; /*directives matches (4k is way too many!)*/
unsigned short contexts[4096]; /*conditions matches (4k is way too many!)*/
uint32_t n = 0;
@ -232,12 +232,12 @@ int config_plugin_values_init(server * const srv, void *p_d, const config_plugin
case T_CONFIG_UNSUPPORTED:
log_error(srv->errh, __FILE__, __LINE__,
"ERROR: found unsupported key: %s (%s)", cpk[i].k, mname);
srv->config_unsupported = 1;
srv->srvconf.config_unsupported = 1;
continue;
case T_CONFIG_DEPRECATED:
log_error(srv->errh, __FILE__, __LINE__,
"ERROR: found deprecated key: %s (%s)", cpk[i].k, mname);
srv->config_deprecated = 1;
srv->srvconf.config_deprecated = 1;
continue;
}
@ -397,13 +397,13 @@ int config_insert_values_internal(server *srv, const array *ca, const config_val
case T_CONFIG_UNSUPPORTED:
log_error_write(srv, __FILE__, __LINE__, "ssss", "ERROR: found unsupported key:", cv[i].key, "-", (char *)(cv[i].destination));
srv->config_unsupported = 1;
srv->srvconf.config_unsupported = 1;
break;
case T_CONFIG_DEPRECATED:
log_error_write(srv, __FILE__, __LINE__, "ssss", "ERROR: found deprecated key:", cv[i].key, "-", (char *)(cv[i].destination));
srv->config_deprecated = 1;
srv->srvconf.config_deprecated = 1;
break;
}
@ -422,7 +422,7 @@ int config_insert_values_global(server *srv, const array *ca, const config_value
continue;
}
array_set_key_value(srv->config_touched, CONST_BUF_LEN(&du->key), CONST_STR_LEN(""));
array_set_key_value(srv->srvconf.config_touched, CONST_BUF_LEN(&du->key), CONST_STR_LEN(""));
}
return config_insert_values_internal(srv, ca, cv, scope);

View File

@ -34,7 +34,7 @@ typedef struct {
specific_config defaults;
} config_data_base;
void config_free_config(void * const p_d) {
static void config_free_config(void * const p_d) {
plugin_data_base * const p = p_d;
if (NULL == p) return;
if (NULL == p->cvlist) { free(p); return; }
@ -838,8 +838,6 @@ static int config_insert(server *srv) {
T_CONFIG_SCOPE_UNSET }
};
if (0 != config_insert_srvconf(srv)) return HANDLER_ERROR;
int rc = 0;
config_data_base * const p = calloc(1, sizeof(config_data_base));
force_assert(p);
@ -963,7 +961,7 @@ static int config_insert(server *srv) {
| (srv->srvconf.http_host_normalize ? HTTP_PARSEOPT_HOST_NORMALIZE :0)
| (srv->srvconf.http_method_get_body ? HTTP_PARSEOPT_METHOD_GET_BODY :0);
p->defaults.http_parseopts |= srv->srvconf.http_url_normalize;
p->defaults.mimetypes = &srv->empty_array; /*(mimetypes must not be NULL)*/
p->defaults.mimetypes = &srv->srvconf.empty_array; /*(must not be NULL)*/
/* initialize p->defaults from global config context */
if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
@ -979,6 +977,101 @@ static int config_insert(server *srv) {
return rc;
}
int config_finalize(server *srv, const buffer *default_server_tag) {
/* (call after plugins_call_set_defaults()) */
config_data_base * const p = srv->config_data_base;
/* settings might be enabled during plugins_call_set_defaults() */
p->defaults.high_precision_timestamps =
srv->srvconf.high_precision_timestamps;
/* configure default server_tag if not set
* (if configured to blank, unset server_tag)*/
if (buffer_is_empty(p->defaults.server_tag))
p->defaults.server_tag = default_server_tag;
else if (buffer_string_is_empty(p->defaults.server_tag))
p->defaults.server_tag = NULL;
/* dump unused config keys */
for (uint32_t i = 0; i < srv->config_context->used; ++i) {
array *config = ((data_config *)srv->config_context->data[i])->value;
for (uint32_t j = 0; config && j < config->used; ++j) {
const buffer * const k = &config->data[j]->key;
/* all var.* is known as user defined variable */
if (strncmp(k->ptr, "var.", sizeof("var.") - 1) == 0)
continue;
if (!array_get_element_klen(srv->srvconf.config_touched,
CONST_BUF_LEN(k)))
log_error(srv->errh, __FILE__, __LINE__,
"WARNING: unknown config-key: %s (ignored)", k->ptr);
}
}
array_free(srv->srvconf.config_touched);
srv->srvconf.config_touched = NULL;
if (srv->srvconf.config_unsupported || srv->srvconf.config_deprecated) {
if (srv->srvconf.config_unsupported)
log_error(srv->errh, __FILE__, __LINE__,
"Configuration contains unsupported keys. Going down.");
if (srv->srvconf.config_deprecated)
log_error(srv->errh, __FILE__, __LINE__,
"Configuration contains deprecated keys. Going down.");
return 0;
}
return 1;
}
void config_print(server *srv) {
data_unset *dc = srv->config_context->data[0];
dc->fn->print(dc, 0);
}
void config_free(server *srv) {
config_free_config(srv->config_data_base);
array_free(srv->config_context);
array_free(srv->srvconf.config_touched);
array_free(srv->srvconf.modules);
buffer_free(srv->srvconf.modules_dir);
array_free(srv->srvconf.upload_tempdirs);
}
void config_init(server *srv) {
srv->config_context = array_init();
srv->srvconf.config_touched = array_init();
srv->srvconf.port = 0;
srv->srvconf.dont_daemonize = 0;
srv->srvconf.preflight_check = 0;
srv->srvconf.compat_module_load = 1;
srv->srvconf.systemd_socket_activation = 0;
srv->srvconf.high_precision_timestamps = 0;
srv->srvconf.max_request_field_size = 8192;
srv->srvconf.xattr_name = "Content-Type";
srv->srvconf.http_header_strict = 1;
srv->srvconf.http_host_strict = 1; /*(implies http_host_normalize)*/
srv->srvconf.http_host_normalize = 0;
srv->srvconf.http_url_normalize =
HTTP_PARSEOPT_URL_NORMALIZE
| HTTP_PARSEOPT_URL_NORMALIZE_UNRESERVED
| HTTP_PARSEOPT_URL_NORMALIZE_CTRLS_REJECT
| HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_DECODE
| HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REMOVE;
srv->srvconf.modules = array_init();
srv->srvconf.modules_dir = buffer_init_string(LIBRARY_DIR);
srv->srvconf.upload_tempdirs = array_init();
}
typedef struct {
const char *source;
const char *input;
@ -1648,6 +1741,10 @@ int config_read(server *srv, const char *fn) {
return ret;
}
if (0 != config_insert_srvconf(srv)) {
return -1;
}
if (0 != config_insert(srv)) {
return -1;
}

View File

@ -129,7 +129,16 @@ __attribute_cold__
int config_parse_cmd(server *srv, config_t *context, const char *cmd);
__attribute_cold__
void config_free_config(void *p);
void config_init(server *srv);
__attribute_cold__
void config_print(server *srv);
__attribute_cold__
int config_finalize(server *srv, const buffer *default_server_tag);
__attribute_cold__
void config_free(server *srv);
void config_reset_config_bytes_sec(void *p);

View File

@ -24,6 +24,7 @@
#endif
#define PACKAGE_DESC PACKAGE_NAME "/" PACKAGE_VERSION REPO_VERSION
static const buffer default_server_tag = { CONST_STR_LEN(PACKAGE_DESC), 0 };
#include <sys/types.h>
#include <sys/time.h>
@ -245,8 +246,6 @@ static server *server_init(void) {
#define CLEAN(x) \
srv->x = array_init();
CLEAN(config_context);
CLEAN(config_touched);
CLEAN(status);
#undef CLEAN
@ -261,22 +260,7 @@ static server *server_init(void) {
srv->errh = log_error_st_init(&srv->cur_ts, &srv->last_generated_debug_ts);
srv->srvconf.modules = array_init();
srv->srvconf.modules_dir = buffer_init_string(LIBRARY_DIR);
srv->srvconf.upload_tempdirs = array_init();
srv->srvconf.xattr_name = "Content-Type";
srv->srvconf.http_header_strict = 1;
srv->srvconf.http_host_strict = 1; /*(implies http_host_normalize)*/
srv->srvconf.http_host_normalize = 0;
srv->srvconf.http_url_normalize = HTTP_PARSEOPT_URL_NORMALIZE
| HTTP_PARSEOPT_URL_NORMALIZE_UNRESERVED
| HTTP_PARSEOPT_URL_NORMALIZE_CTRLS_REJECT
| HTTP_PARSEOPT_URL_NORMALIZE_PATH_2F_DECODE
| HTTP_PARSEOPT_URL_NORMALIZE_PATH_DOTSEG_REMOVE;
srv->srvconf.high_precision_timestamps = 0;
srv->srvconf.max_request_field_size = 8192;
srv->srvconf.compat_module_load = 1;
srv->srvconf.systemd_socket_activation = 0;
config_init(srv);
srv->request_env = plugins_call_handle_request_env;
@ -303,22 +287,17 @@ static void server_free(server *srv) {
CLEAN(ts_date_str);
CLEAN(tmp_buf);
CLEAN(srvconf.modules_dir);
CLEAN(tmp_chunk_len);
#undef CLEAN
fdevent_free(srv->ev);
config_free_config(srv->config_data_base);
config_free(srv);
#define CLEAN(x) \
array_free(srv->x);
CLEAN(config_context);
CLEAN(config_touched);
CLEAN(status);
CLEAN(srvconf.upload_tempdirs);
#undef CLEAN
free(srv->joblist.ptr);
@ -328,8 +307,6 @@ static void server_free(server *srv) {
stat_cache_free(srv->stat_cache);
}
array_free(srv->srvconf.modules);
li_rand_cleanup();
chunkqueue_chunk_pool_free();
@ -975,10 +952,6 @@ static int server_main (server * const srv, int argc, char **argv) {
/*memset(inherited_sockets, 0, sizeof(inherited_sockets));*/
/*pid_fd = -1;*/
srv->srvconf.port = 0;
srv->srvconf.dont_daemonize = 0;
srv->srvconf.preflight_check = 0;
while(-1 != (o = getopt(argc, argv, "f:m:i:hvVD1pt"))) {
switch(o) {
case 'f':
@ -1039,14 +1012,8 @@ static int server_main (server * const srv, int argc, char **argv) {
}
if (print_config) {
data_unset *dc = srv->config_context->data[0];
if (dc) {
dc->fn->print(dc, 0);
fprintf(stdout, "\n");
} else {
/* shouldn't happend */
fprintf(stderr, "global config not found\n");
}
config_print(srv);
fprintf(stdout, "\n");
}
if (test_config) {
@ -1418,52 +1385,12 @@ static int server_main (server * const srv, int argc, char **argv) {
log_error_write(srv, __FILE__, __LINE__, "s", "server started (" PACKAGE_DESC ")");
}
specific_config *s = &((config_data_base *)srv->config_data_base)->defaults;
if (buffer_is_empty(s->server_tag)) {
static const buffer server_tag =
{ CONST_STR_LEN(PACKAGE_DESC), 0 };
s->server_tag = &server_tag;
}
if (HANDLER_GO_ON != plugins_call_set_defaults(srv)) {
log_error_write(srv, __FILE__, __LINE__, "s", "Configuration of plugins failed. Going down.");
return -1;
}
/* settings might be enabled during module config set defaults */
s->high_precision_timestamps = srv->srvconf.high_precision_timestamps;
/* dump unused config-keys */
for (i = 0; i < srv->config_context->used; i++) {
array *config = ((data_config *)srv->config_context->data[i])->value;
for (uint32_t j = 0; config && j < config->used; ++j) {
data_unset *du = config->data[j];
/* all var.* is known as user defined variable */
if (strncmp(du->key.ptr, "var.", sizeof("var.") - 1) == 0) {
continue;
}
if (NULL == array_get_element_klen(srv->config_touched, CONST_BUF_LEN(&du->key))) {
log_error_write(srv, __FILE__, __LINE__, "sbs",
"WARNING: unknown config-key:",
&du->key,
"(ignored)");
}
}
}
if (srv->config_unsupported) {
log_error_write(srv, __FILE__, __LINE__, "s",
"Configuration contains unsupported keys. Going down.");
}
if (srv->config_deprecated) {
log_error_write(srv, __FILE__, __LINE__, "s",
"Configuration contains deprecated keys. Going down.");
}
if (srv->config_unsupported || srv->config_deprecated) {
if (!config_finalize(srv, &default_server_tag)) {
return -1;
}