1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "../public/nsapi.h"
33
34 #include <ucx/string.h>
35
36 #include "httplistener.h"
37 #include "log.h"
38 #include "configmanager.h"
39
40 static ServerConfiguration *current_config =
NULL;
41 static time_t sc_last_modified =
0;
42
43 static UcxMap *config_files;
44
45 static conf_global_vars_s global_vars;
46
47 void init_configuration_manager() {
48
49 init_server_config_parser();
50
51 config_files = ucx_map_new(
16);
52 }
53
54 NSAPI_PUBLIC conf_global_vars_s* conf_getglobals() {
55 return &global_vars;
56 }
57
58 void cfgmgr_attach_file(ConfigFile *cf) {
59 ucx_map_sstr_put(config_files, cf->file, cf);
60 }
61
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;
74 }
75
76
77 if(f->last_modified != s.st_mtime) {
78
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
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 }
129
130 if(set_cfg) {
131 *set_cfg = config;
132 }
133 ServerConfiguration *old_conf =
NULL;
134 if(current_config != config) {
135 old_conf = current_config;
136 }
137 current_config = config;
138 if(old_conf) {
139 cfg_unref(old_conf);
140 }
141 return 0;
142 }
143
144 ServerConfiguration *cfgmgr_get_server_config() {
145
146 return current_config;
147 }
148
149
150