1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2013 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 #include <string.h> |
|
32 |
|
33 #include "serverconf.h" |
|
34 |
|
35 ServerConfig *load_server_config(char *file) { |
|
36 FILE *in = fopen(file, "r"); |
|
37 if(in == NULL) { |
|
38 return NULL; |
|
39 } |
|
40 |
|
41 ServerConfig *conf = malloc(sizeof(ServerConfig)); |
|
42 conf->parser.parse = serverconf_parse; |
|
43 conf->file = file; |
|
44 conf->obj = NULL; |
|
45 |
|
46 conf->objects = ucx_map_new(16); |
|
47 int r = cfg_parse_basic_file((ConfigParser*)conf, in); |
|
48 cfg_map_destr(conf->parser.mp->pool, conf->objects); |
|
49 if(r != 0) { |
|
50 // TODO: free |
|
51 return NULL; |
|
52 } |
|
53 |
|
54 fclose(in); |
|
55 |
|
56 return conf; |
|
57 } |
|
58 |
|
59 void free_server_config(ServerConfig *conf) { |
|
60 ucx_mempool_destroy(conf->parser.mp->pool); |
|
61 free(conf); |
|
62 } |
|
63 |
|
64 int serverconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line){ |
|
65 ServerConfig *conf = p; |
|
66 UcxAllocator *mp = conf->parser.mp; |
|
67 |
|
68 begin->type = cfg_get_line_type(line); |
|
69 switch(begin->type) { |
|
70 case LINE_BEGIN_TAG: { |
|
71 ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.mp); |
|
72 |
|
73 // create server config object |
|
74 ServerConfigObject *obj = OBJ_NEW( |
|
75 conf->parser.mp, |
|
76 ServerConfigObject); |
|
77 obj->type = tag->name; |
|
78 obj->begin = begin; |
|
79 obj->end = end; |
|
80 obj->directives = NULL; |
|
81 |
|
82 // add object to server config |
|
83 UcxList *list = ucx_map_sstr_get(conf->objects, obj->type); |
|
84 list = ucx_list_append_a(mp, list, obj); |
|
85 ucx_map_sstr_put(conf->objects, obj->type, list); |
|
86 conf->obj = obj; |
|
87 |
|
88 break; |
|
89 } |
|
90 case LINE_END_TAG: { |
|
91 sstr_t tag = cfg_get_end_tag_name(line); |
|
92 if(sstrcmp(tag, conf->obj->type) != 0) { |
|
93 log_ereport(LOG_FAILURE, "server.conf: syntax error: wrong close tag"); |
|
94 log_ereport(LOG_FAILURE, "open tag: %s close tag: %s", sstrdup(tag).ptr, sstrdup(conf->obj->type).ptr); |
|
95 exit(-1); |
|
96 } |
|
97 conf->obj = NULL; |
|
98 |
|
99 break; |
|
100 } |
|
101 case LINE_DIRECTIVE: { |
|
102 if(conf->obj == NULL) { |
|
103 log_ereport(LOG_FAILURE, "server.conf: directive outside of object"); |
|
104 exit(-1); |
|
105 } |
|
106 |
|
107 ConfigDirective *d = cfg_parse_directive(line, conf->parser.mp); |
|
108 d->begin = begin; |
|
109 d->end = end; |
|
110 |
|
111 //printf("%s.%s\n", conf->obj->type.ptr, d->directive_type.ptr); |
|
112 conf->obj->directives = ucx_list_append_a(mp, conf->obj->directives, d); |
|
113 } |
|
114 } |
|
115 return 0; |
|
116 } |
|
117 |
|
118 |
|
119 UcxList* srvcfg_get_listeners(ServerConfig *cfg, UcxAllocator *mp, int *error) { |
|
120 mp = mp ? mp : cfg->parser.mp; |
|
121 |
|
122 UcxList *list = ucx_map_sstr_get(cfg->objects, sstrn("Listener", 8)); |
|
123 UcxList *lslist = NULL; |
|
124 UCX_FOREACH(elm, list) { |
|
125 ServerConfigObject *ls = elm->data; |
|
126 sstr_t name = cfg_directivelist_get_str(ls->directives, sstr("Name")); |
|
127 sstr_t port = cfg_directivelist_get_str(ls->directives, sstr("Port")); |
|
128 sstr_t vs = cfg_directivelist_get_str( |
|
129 ls->directives, |
|
130 sstr("DefaultVS")); |
|
131 sstr_t threadpool = cfg_directivelist_get_str( |
|
132 ls->directives, |
|
133 sstr("Threadpool")); |
|
134 |
|
135 CfgListener *listener = OBJ_NEW_N(mp, CfgListener); |
|
136 // threadpool is optional, all other configs must be set |
|
137 if(!name.ptr || !port.ptr || !vs.ptr) { |
|
138 // TODO: log error |
|
139 *error = 1; |
|
140 listener->cfg_correct = 0; |
|
141 } else { |
|
142 listener->cfg_correct = 1; |
|
143 } |
|
144 |
|
145 if(name.ptr) { |
|
146 listener->name = sstrdup_a(mp, name); |
|
147 } |
|
148 if(port.ptr) { |
|
149 // don't expect that port is null terminated, sstrdup it to be sure |
|
150 sstr_t portdp = sstrdup(port); |
|
151 listener->port = atoi(portdp.ptr); |
|
152 free(portdp.ptr); |
|
153 } |
|
154 if(vs.ptr) { |
|
155 listener->vs = sstrdup_a(mp, vs); |
|
156 } |
|
157 if(threadpool.ptr) { |
|
158 listener->threadpool = sstrdup_a(mp, threadpool); |
|
159 } |
|
160 |
|
161 lslist = ucx_list_append_a(mp, lslist, listener); |
|
162 } |
|
163 |
|
164 return lslist; |
|
165 } |
|