src/server/daemon/config.c

changeset 36
450d2d5f4735
parent 33
96dbfe4f91e5
child 37
360b9aabe17e
equal deleted inserted replaced
35:4417619a9bbd 36:450d2d5f4735
36 #include <sys/file.h> 36 #include <sys/file.h>
37 #include <sys/stat.h> 37 #include <sys/stat.h>
38 #include <sys/mman.h> 38 #include <sys/mman.h>
39 39
40 #include "../ucx/string.h" 40 #include "../ucx/string.h"
41 #include "../ucx/atomic.h"
41 42
42 #include "httplistener.h" 43 #include "httplistener.h"
43 #include "config.h" 44 #include "config.h"
44 #include "func.h" 45 #include "func.h"
45 #include "log.h" 46 #include "log.h"
109 ServerConfig *serverconf = load_server_config(file); 110 ServerConfig *serverconf = load_server_config(file);
110 if(serverconf == NULL) { 111 if(serverconf == NULL) {
111 fprintf(stderr, "Cannot load server.conf\n"); 112 fprintf(stderr, "Cannot load server.conf\n");
112 } 113 }
113 ServerConfiguration *serverconfig = malloc(sizeof(ServerConfiguration)); 114 ServerConfiguration *serverconfig = malloc(sizeof(ServerConfiguration));
115 serverconfig->ref = 1;
114 serverconfig->pool = pool_create(); 116 serverconfig->pool = pool_create();
115 serverconfig->listeners = NULL; 117 serverconfig->listeners = NULL;
116 serverconfig->host_vs = ucx_map_new(16); 118 serverconfig->host_vs = ucx_map_new(16);
117 // TODO: init serverconfig stuff 119 // TODO: init serverconfig stuff
118 120
182 UCX_FOREACH(UcxList*, list, elm) { 184 UCX_FOREACH(UcxList*, list, elm) {
183 ServerConfigObject *scfgobj = elm->data; 185 ServerConfigObject *scfgobj = elm->data;
184 cfg_handle_vs(serverconfig, scfgobj); 186 cfg_handle_vs(serverconfig, scfgobj);
185 } 187 }
186 188
187 /* check event handler config */ 189 // check event handler config
188 if(check_event_handler_cfg() != 0) { 190 if(check_event_handler_cfg() != 0) {
189 /* critical error */ 191 /* critical error */
190 return NULL; 192 return NULL;
191 } 193 }
192 194
193 /* check thread pool config */ 195 // check thread pool config
194 if(check_thread_pool_cfg() != 0) { 196 if(check_thread_pool_cfg() != 0) {
195 /* critical error */ 197 /* critical error */
196 return NULL; 198 return NULL;
197 } 199 }
198 200
199 /* set VirtualServer for all listeners */ 201 // set VirtualServer for all listeners
200 UcxList *ls = serverconfig->listeners; 202 UcxList *ls = serverconfig->listeners;
201 while(ls) { 203 while(ls) {
202 HttpListener *listener = ls->data; 204 HttpListener *listener = ls->data;
203 205
204 sstr_t vsname = sstr(listener->default_vs.vs_name); 206 sstr_t vsname = sstr(listener->default_vs.vs_name);
205 207
206 /* search for VirtualServer */ 208 // search for VirtualServer
207 int b = 0; 209 //int b = 0;
208 UcxMapIterator iter = ucx_map_iterator(serverconfig->host_vs); 210 UcxMapIterator iter = ucx_map_iterator(serverconfig->host_vs);
209 UCX_MAP_FOREACH(VirtualServer*, vs, serverconfig->host_vs, iter) { 211 VirtualServer *vs;
212 UCX_MAP_FOREACH(vs, iter) {
210 if(!sstrcmp(vsname, vs->name)) { 213 if(!sstrcmp(vsname, vs->name)) {
211 listener->default_vs.vs = vs; 214 listener->default_vs.vs = vs;
212 break; 215 break;
213 } 216 }
214 } 217 }
217 } 220 }
218 221
219 return serverconfig; 222 return serverconfig;
220 } 223 }
221 224
225 void cfg_ref(ServerConfiguration *cfg) {
226 ucx_atomic_inc_32(&cfg->ref);
227 }
228
229 void cfg_unref(ServerConfiguration *cfg) {
230 uint32_t ref = ucx_atomic_dec_32_nv(&cfg->ref);
231 if(ref == 0) {
232 // TODO: free configuration
233 }
234 }
235
222 236
223 void init_server_config_parser() { 237 void init_server_config_parser() {
224 238
225 } 239 }
226 240
227 int cfg_handle_runtime(ServerConfiguration *cfg, ServerConfigObject *obj) { 241 int cfg_handle_runtime(ServerConfiguration *cfg, ServerConfigObject *obj) {
228 cfg->user = sstrdub(cfg_directivelist_get_str( 242 cfg->user = sstrdup(cfg_directivelist_get_str(
229 obj->directives, 243 obj->directives,
230 sstr("User"))); 244 sstr("User")));
231 cfg->tmp = sstrdub(cfg_directivelist_get_str( 245 cfg->tmp = sstrdup(cfg_directivelist_get_str(
232 obj->directives, 246 obj->directives,
233 sstr("Temp"))); 247 sstr("Temp")));
234 248
235 return 0; 249 return 0;
236 } 250 }
244 return -1; 258 return -1;
245 } 259 }
246 260
247 LogConfig logcfg; 261 LogConfig logcfg;
248 logcfg.file = sstrdup(file).ptr; 262 logcfg.file = sstrdup(file).ptr;
249 logcfg.level = sstrdub(lvl).ptr; 263 logcfg.level = sstrdup(lvl).ptr;
250 /* TODO: stdout, stderr config */ 264 /* TODO: stdout, stderr config */
251 265
252 int ret = init_log_file(&logcfg); 266 int ret = init_log_file(&logcfg);
253 267
254 free(logcfg.file); 268 free(logcfg.file);
287 return 0; 301 return 0;
288 } 302 }
289 303
290 int cfg_handle_listener(ServerConfiguration *cfg, ServerConfigObject *obj) { 304 int cfg_handle_listener(ServerConfiguration *cfg, ServerConfigObject *obj) {
291 ListenerConfig lc; 305 ListenerConfig lc;
306 lc.cfg = cfg;
292 lc.port = 8080; 307 lc.port = 8080;
293 lc.nacceptors = 1; 308 lc.nacceptors = 1;
294 309
295 lc.name = sstrdub(cfg_directivelist_get_str( 310 // TODO: use sstrdup_pool?
311 lc.name = sstrdup(cfg_directivelist_get_str(
296 obj->directives, 312 obj->directives,
297 sstr("Name"))); 313 sstr("Name")));
298 lc.port = atoi(cfg_directivelist_get_str( 314 lc.port = atoi(cfg_directivelist_get_str(
299 obj->directives, 315 obj->directives,
300 sstr("Port")).ptr); 316 sstr("Port")).ptr);
301 lc.vs = sstrdub(cfg_directivelist_get_str( 317 lc.vs = sstrdup(cfg_directivelist_get_str(
302 obj->directives, 318 obj->directives,
303 sstr("DefaultVS"))); 319 sstr("DefaultVS")));
304 320
305 321
306 HttpListener *listener = http_listener_new(&lc); 322 HttpListener *listener = http_listener_create(&lc);
307 listener->default_vs.vs_name = lc.vs.ptr; 323 listener->default_vs.vs_name = lc.vs.ptr;
308 cfg->listeners = ucx_list_append(cfg->listeners, listener); 324 cfg->listeners = ucx_list_append(cfg->listeners, listener);
309 325
310 return 0; 326 return 0;
311 } 327 }
312 328
313 int cfg_handle_vs(ServerConfiguration *cfg, ServerConfigObject *obj) { 329 int cfg_handle_vs(ServerConfiguration *cfg, ServerConfigObject *obj) {
314 VirtualServer *vs = vs_new(); 330 VirtualServer *vs = vs_new();
315 331
316 vs->name = sstrdub(cfg_directivelist_get_str( 332 vs->name = sstrdup(cfg_directivelist_get_str(
317 obj->directives, 333 obj->directives,
318 sstr("Name"))); 334 sstr("Name")));
319 vs->host = sstrdub(cfg_directivelist_get_str( 335 vs->host = sstrdup(cfg_directivelist_get_str(
320 obj->directives, 336 obj->directives,
321 sstr("Host"))); 337 sstr("Host")));
322 vs->document_root = sstrdub(cfg_directivelist_get_str( 338 vs->document_root = sstrdup(cfg_directivelist_get_str(
323 obj->directives, 339 obj->directives,
324 sstr("DocRoot"))); 340 sstr("DocRoot")));
325 sstr_t objfile = cfg_directivelist_get_str( 341 sstr_t objfile = cfg_directivelist_get_str(
326 obj->directives, 342 obj->directives,
327 sstr("ObjectFile")); 343 sstr("ObjectFile"));
333 file = sstrncat(2, file, base, objfile); 349 file = sstrncat(2, file, base, objfile);
334 350
335 ConfigFile *f = cfgmgr_get_file(file); 351 ConfigFile *f = cfgmgr_get_file(file);
336 if(f == NULL) { 352 if(f == NULL) {
337 f = malloc(sizeof(ConfigFile)); 353 f = malloc(sizeof(ConfigFile));
338 f->file = sstrdub(file); 354 f->file = sstrdup(file);
339 f->reload = object_conf_reload; 355 f->reload = object_conf_reload;
340 f->reload(f, cfg); 356 f->reload(f, cfg);
341 cfgmgr_attach_file(f); 357 cfgmgr_attach_file(f);
342 } 358 }
343 vs->objects = (HTTPObjectConfig*)f->data; 359 vs->objectfile = sstrdup(file); // TODO: pool
360 vs->objects = (HTTPObjectConfig*)f->data; // TODO: ref
344 361
345 ucx_map_sstr_put(cfg->host_vs, vs->host, vs); 362 ucx_map_sstr_put(cfg->host_vs, vs->host, vs);
346 363
347 return 0; 364 return 0;
348 } 365 }
385 402
386 /* get name and ppath */ 403 /* get name and ppath */
387 char *name = NULL; 404 char *name = NULL;
388 char *ppath = NULL; 405 char *ppath = NULL;
389 if(cob->name.length > 0) { 406 if(cob->name.length > 0) {
390 name = sstrdub(cob->name).ptr; 407 name = sstrdup(cob->name).ptr;
391 } 408 }
392 if(cob->ppath.length > 0) { 409 if(cob->ppath.length > 0) {
393 ppath = sstrdub(cob->ppath).ptr; 410 ppath = sstrdup(cob->ppath).ptr;
394 } 411 }
395 412
396 /* create and add object */ 413 /* create and add object */
397 httpd_object *obj = object_new(name); 414 httpd_object *obj = object_new(name);
398 obj->path = NULL; 415 obj->path = NULL;

mercurial