36 #include "httplistener.h" |
36 #include "httplistener.h" |
37 #include "log.h" |
37 #include "log.h" |
38 #include "configmanager.h" |
38 #include "configmanager.h" |
39 |
39 |
40 static ServerConfiguration *current_config = NULL; |
40 static ServerConfiguration *current_config = NULL; |
41 static time_t sc_last_modified = 0; |
|
42 |
41 |
43 static UcxMap *config_files; |
|
44 |
42 |
45 static conf_global_vars_s global_vars; |
43 static conf_global_vars_s global_vars; |
46 |
44 |
47 void init_configuration_manager() { |
45 void init_configuration_manager() { |
48 /* init parser */ |
46 /* init parser */ |
49 init_server_config_parser(); |
47 init_server_config_parser(); |
50 |
|
51 config_files = ucx_map_new(16); |
|
52 } |
48 } |
53 |
49 |
54 NSAPI_PUBLIC conf_global_vars_s* conf_getglobals() { |
50 NSAPI_PUBLIC conf_global_vars_s* conf_getglobals() { |
55 return &global_vars; |
51 return &global_vars; |
56 } |
52 } |
57 |
53 |
58 void cfgmgr_attach_file(ConfigFile *cf) { |
54 int cfgmgr_load_config(ServerConfiguration **set_cfg) { |
59 ucx_map_sstr_put(config_files, cf->file, cf); |
55 ServerConfiguration *config = load_server_conf("config/server.conf"); |
60 } |
56 |
61 |
57 if(!config) { |
62 ConfigFile* cfgmgr_get_file(sstr_t name) { |
|
63 return ucx_map_sstr_get(config_files, name); |
|
64 } |
|
65 |
|
66 int cfgmgr_reload_file(ConfigFile *f, ServerConfiguration *conf, int *reload) { |
|
67 struct stat s; |
|
68 if(stat(f->file.ptr, &s) != 0) { |
|
69 fprintf( |
|
70 stderr, |
|
71 "Error: Cannot get stat of file %s\n", f->file.ptr); |
|
72 perror("cfgmgr_load_config: stat"); |
|
73 return -1; |
58 return -1; |
74 } |
|
75 |
|
76 //printf("1 time: %d - %d\n", f->last_modified, s.st_mtim.tv_sec); |
|
77 if(f->last_modified != s.st_mtime) { |
|
78 /* reload the file */ |
|
79 if(f->last_modified != 0) { |
|
80 log_ereport( |
|
81 LOG_INFORM, |
|
82 "reload configuration file: %s", |
|
83 f->file.ptr); |
|
84 } |
|
85 if(f->reload(f, conf)) { |
|
86 return -1; |
|
87 } |
|
88 f->last_modified = s.st_mtime; |
|
89 if(reload) { |
|
90 *reload = 1; |
|
91 } |
|
92 } |
|
93 return 0; |
|
94 } |
|
95 |
|
96 int cfgmgr_load_config(ServerConfiguration **set_cfg) { |
|
97 int cfgreload = 0; |
|
98 |
|
99 /* check config files */ |
|
100 UcxMapIterator iter = ucx_map_iterator(config_files); |
|
101 ConfigFile *f; |
|
102 UCX_MAP_FOREACH(key, f, iter) { |
|
103 if(cfgmgr_reload_file(f, current_config, &cfgreload) == -1) { |
|
104 return -1; |
|
105 } |
|
106 } |
|
107 |
|
108 struct stat s; |
|
109 if(stat("config/server.conf", &s) != 0) { |
|
110 perror("stat(\"config/server.conf\")"); |
|
111 return -1; |
|
112 } |
|
113 |
|
114 ServerConfiguration *config; |
|
115 if(cfgreload || !current_config || sc_last_modified != s.st_mtime) { |
|
116 config = load_server_conf( |
|
117 current_config, |
|
118 "config/server.conf"); |
|
119 |
|
120 if(config == NULL) { |
|
121 return -1; |
|
122 } |
|
123 |
|
124 sc_last_modified = s.st_mtime; |
|
125 } else { |
|
126 log_ereport(LOG_VERBOSE, "no reconfig required"); |
|
127 config = current_config; |
|
128 } |
59 } |
129 |
60 |
130 if(set_cfg) { |
61 if(set_cfg) { |
131 *set_cfg = config; |
62 *set_cfg = config; |
132 } |
63 } |
133 ServerConfiguration *old_conf = NULL; |
64 |
134 if(current_config != config) { |
65 if(current_config) { |
135 old_conf = current_config; |
66 cfg_unref(current_config); |
136 } |
67 } |
137 current_config = config; |
68 current_config = config; |
138 if(old_conf) { |
69 |
139 cfg_unref(old_conf); |
|
140 } |
|
141 return 0; |
70 return 0; |
142 } |
71 } |
143 |
72 |
144 ServerConfiguration *cfgmgr_get_server_config() { |
73 ServerConfiguration *cfgmgr_get_server_config() { |
145 //cfg_ref(current_config); |
|
146 return current_config; |
74 return current_config; |
147 } |
75 } |
148 |
76 |
149 |
77 |