Browse Source

klishd: klishd has access to raw config file but not known options only

Serj Kalichev 2 years ago
parent
commit
6a50bed1d3
3 changed files with 22 additions and 8 deletions
  1. 10 2
      bin/klishd/klishd.c
  2. 9 5
      bin/klishd/opts.c
  3. 3 1
      bin/klishd/private.h

+ 10 - 2
bin/klishd/klishd.c

@@ -74,6 +74,7 @@ int main(int argc, char **argv)
 	ktpd_clients_t *clients = NULL;
 	kscheme_t *scheme = NULL;
 	faux_error_t *error = faux_error_new();
+	faux_ini_t *config = NULL;
 
 	struct timespec delayed = { .tv_sec = 10, .tv_nsec = 0 };
 	struct timespec period = { .tv_sec = 3, .tv_nsec = 0 };
@@ -94,7 +95,7 @@ int main(int argc, char **argv)
 	// Parse config file
 	syslog(LOG_DEBUG, "Parse config file: %s\n", opts->cfgfile);
 	if (!access(opts->cfgfile, R_OK)) {
-		if (config_parse(opts->cfgfile, opts))
+		if (!(config = config_parse(opts->cfgfile, opts)))
 			goto err;
 	} else if (opts->cfgfile_userdefined) {
 		// User defined config must be found
@@ -226,6 +227,8 @@ err:
 
 	// Free command line options
 	opts_free(opts);
+	faux_ini_free(config);
+
 	syslog(LOG_INFO, "Stop daemon.\n");
 
 	if (faux_error_len(error) > 0)
@@ -306,19 +309,24 @@ static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
 
 
 /** @brief Re-read config file.
+ *
+ * This function can refresh klishd options but plugins (dbs for example) are
+ * already inited and there is no way to re-init them on-the-fly.
  */
 static bool_t refresh_config_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
 	void *associated_data, void *user_data)
 {
 	struct options *opts = (struct options *)user_data;
+	faux_ini_t *ini = NULL;
 
 	if (access(opts->cfgfile, R_OK) == 0) {
 		syslog(LOG_DEBUG, "Re-reading config file \"%s\"\n", opts->cfgfile);
-		if (config_parse(opts->cfgfile, opts) < 0)
+		if (!(ini = config_parse(opts->cfgfile, opts)))
 			syslog(LOG_ERR, "Error while config file parsing.\n");
 	} else if (opts->cfgfile_userdefined) {
 		syslog(LOG_ERR, "Can't find config file \"%s\"\n", opts->cfgfile);
 	}
+	faux_ini_free(ini); // No way to use it later
 
 	// Happy compiler
 	eloop = eloop;

+ 9 - 5
bin/klishd/opts.c

@@ -152,8 +152,13 @@ void help(int status, const char *argv0)
 
 
 /** @brief Parse config file
+ *
+ * It parses known options and put them to opts arg. But also it returns
+ * parsed config file. The parts of config file can be unknown to klish
+ * itself but can be known to third party plugins. For example DBs
+ * subsystem get settings from config file.
  */
-int config_parse(const char *cfgfile, struct options *opts)
+faux_ini_t *config_parse(const char *cfgfile, struct options *opts)
 {
 	faux_ini_t *ini = NULL;
 	const char *tmp = NULL;
@@ -161,11 +166,11 @@ int config_parse(const char *cfgfile, struct options *opts)
 	ini = faux_ini_new();
 	assert(ini);
 	if (!ini)
-		return -1;
+		NULL;
 	if (!faux_ini_parse_file(ini, cfgfile)) {
 		syslog(LOG_ERR, "Can't parse config file: %s\n", cfgfile);
 		faux_ini_free(ini);
-		return -1;
+		return NULL;
 	}
 
 	if ((tmp = faux_ini_find(ini, "UnixSocketPath"))) {
@@ -173,8 +178,7 @@ int config_parse(const char *cfgfile, struct options *opts)
 		opts->unix_socket_path = faux_str_dup(tmp);
 	}
 
-	faux_ini_free(ini);
-	return 0;
+	return ini;
 }
 
 

+ 3 - 1
bin/klishd/private.h

@@ -2,6 +2,8 @@
 #define VERSION "1.0.0"
 #endif
 
+#include <faux/ini.h>
+
 #define LOG_NAME "klishd"
 #define DEFAULT_PIDFILE "/var/run/klishd.pid"
 #define DEFAULT_CFGFILE "/etc/klish/klishd.conf"
@@ -25,4 +27,4 @@ struct options *opts_init(void);
 void opts_free(struct options *opts);
 int opts_parse(int argc, char *argv[], struct options *opts);
 int opts_show(struct options *opts);
-int config_parse(const char *cfgfile, struct options *opts);
+faux_ini_t *config_parse(const char *cfgfile, struct options *opts);