46 #include "../public/auth.h" |
46 #include "../public/auth.h" |
47 #include "../util/systhr.h" |
47 #include "../util/systhr.h" |
48 #include "../util/io.h" |
48 #include "../util/io.h" |
49 #include "../util/util.h" |
49 #include "../util/util.h" |
50 |
50 |
|
51 #include "../../ucx/utils.h" |
|
52 |
51 #include "../safs/common.h" |
53 #include "../safs/common.h" |
52 |
54 |
53 #include "func.h" |
55 #include "func.h" |
54 #include "config.h" |
56 #include "config.h" |
55 #include "configmanager.h" |
57 #include "configmanager.h" |
56 #include "httplistener.h" |
58 #include "httplistener.h" |
57 #include "webserver.h" |
59 #include "webserver.h" |
58 #include "log.h" |
60 #include "log.h" |
59 #include "auth.h" |
61 #include "auth.h" |
|
62 #include "srvctrl.h" |
60 |
63 |
61 extern struct FuncStruct webserver_funcs[]; |
64 extern struct FuncStruct webserver_funcs[]; |
62 |
65 |
63 static RestartCallback *atrestart; |
66 static RestartCallback *atrestart; |
64 |
67 |
88 return -1; |
91 return -1; |
89 } |
92 } |
90 |
93 |
91 // init caches |
94 // init caches |
92 auth_cache_init(); |
95 auth_cache_init(); |
93 |
|
94 // create tmp dir and pid file |
|
95 char *mkdir_cmd = NULL; |
|
96 asprintf(&mkdir_cmd, "mkdir -p %s", cfg->tmp.ptr); |
|
97 system(mkdir_cmd); |
|
98 free(mkdir_cmd); |
|
99 |
|
100 char *pid_file_path = NULL; |
|
101 asprintf(&pid_file_path, "%s/pid", cfg->tmp.ptr); |
|
102 FILE *pidfile = fopen(pid_file_path, "w"); // TODO: check error |
|
103 pid_t pid = getpid(); |
|
104 fprintf(pidfile, "%d", pid); |
|
105 fclose(pidfile); |
|
106 free(pid_file_path); |
|
107 |
96 |
108 // init SAFs |
97 // init SAFs |
109 common_saf_init(); |
98 common_saf_init(); |
110 |
99 |
111 // set global vars |
100 // set global vars |
166 log_ereport( |
155 log_ereport( |
167 LOG_WARN, |
156 LOG_WARN, |
168 "server must be started as root to change uid"); |
157 "server must be started as root to change uid"); |
169 } |
158 } |
170 |
159 |
|
160 // create tmp dir and pid file |
|
161 char *mkdir_cmd = NULL; |
|
162 asprintf(&mkdir_cmd, "mkdir -p %s", cfg->tmp.ptr); |
|
163 system(mkdir_cmd); |
|
164 free(mkdir_cmd); |
|
165 |
|
166 char *pid_file_path = NULL; |
|
167 asprintf(&pid_file_path, "%s/pid", cfg->tmp.ptr); |
|
168 FILE *pidfile = fopen(pid_file_path, "w"); // TODO: check error |
|
169 pid_t pid = getpid(); |
|
170 fprintf(pidfile, "%d", pid); |
|
171 fclose(pidfile); |
|
172 free(pid_file_path); |
|
173 |
|
174 // create unix domain socket for server control |
|
175 sstr_t tmp_priv = ucx_sprintf("%s/private", cfg->tmp.ptr); |
|
176 // TODO: remove existing private dir |
|
177 if(mkdir(tmp_priv.ptr, S_IRWXU)) { |
|
178 if(errno == EEXIST) { |
|
179 if(chmod(tmp_priv.ptr, S_IRWXU)) { |
|
180 log_ereport( |
|
181 LOG_CATASTROPHE, |
|
182 "cannot change permissions of tmp dir %s:", |
|
183 tmp_priv.ptr, |
|
184 strerror(errno)); |
|
185 return 0; |
|
186 } |
|
187 } else { |
|
188 log_ereport( |
|
189 LOG_CATASTROPHE, |
|
190 "cannot create tmp dir %s:", |
|
191 tmp_priv.ptr, |
|
192 strerror(errno)); |
|
193 return -1; |
|
194 } |
|
195 } |
|
196 |
|
197 |
|
198 // create srvctrl unix domain socket |
|
199 // this socket is used for stop, reconfigure and other operations |
|
200 if(srvctrl_init(cfg)) { |
|
201 return -1; |
|
202 } |
|
203 |
171 //endpwent(); // TODO: close or not? |
204 //endpwent(); // TODO: close or not? |
172 //free(pwbuf); // TODO: ? |
205 //free(pwbuf); // TODO: ? |
173 |
206 |
174 return 0; |
207 return 0; |
175 } |
208 } |
187 return 0; |
220 return 0; |
188 } |
221 } |
189 |
222 |
190 void webserver_shutdown() { |
223 void webserver_shutdown() { |
191 log_ereport(LOG_INFORM, "webserver shutdown"); |
224 log_ereport(LOG_INFORM, "webserver shutdown"); |
|
225 |
|
226 srvctrl_shutdown(); |
192 |
227 |
193 // execute restart callbacks |
228 // execute restart callbacks |
194 RestartCallback *re = atrestart; |
229 RestartCallback *re = atrestart; |
195 while(re) { |
230 while(re) { |
196 re->func(re->data); |
231 re->func(re->data); |
197 re = re->next; |
232 re = re->next; |
198 } |
233 } |
|
234 } |
|
235 |
|
236 int webserver_reconfig() { |
|
237 if(cfgmgr_load_config(NULL) != 0) { |
|
238 return -1; |
|
239 } |
|
240 // start newly created listeners |
|
241 start_all_listener(); |
|
242 |
|
243 return 0; |
199 } |
244 } |
200 |
245 |
201 void webserver_atrestart(void (*fn)(void *), void *data) { |
246 void webserver_atrestart(void (*fn)(void *), void *data) { |
202 RestartCallback *cb = malloc(sizeof(RestartCallback)); |
247 RestartCallback *cb = malloc(sizeof(RestartCallback)); |
203 cb->func = fn; |
248 cb->func = fn; |