src/server/daemon/webserver.c

branch
srvctrl
changeset 156
724e107983e9
parent 154
6394ce09889a
child 158
77f4f0079428
equal deleted inserted replaced
155:36cd2e280386 156:724e107983e9
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"
60 62
61 extern struct FuncStruct webserver_funcs[]; 63 extern struct FuncStruct webserver_funcs[];
62 64
63 static RestartCallback *atrestart; 65 static RestartCallback *atrestart;
64 66
67 static int srvctrl;
68
65 int webserver_init() { 69 int webserver_init() {
66 // init NSPR 70 // init NSPR
67 systhread_init("webserver"); 71 systhread_init("webserver");
68 72
69 // init ssl 73 // init ssl
88 return -1; 92 return -1;
89 } 93 }
90 94
91 // init caches 95 // init caches
92 auth_cache_init(); 96 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 97
108 // init SAFs 98 // init SAFs
109 common_saf_init(); 99 common_saf_init();
110 100
111 // set global vars 101 // set global vars
166 log_ereport( 156 log_ereport(
167 LOG_WARN, 157 LOG_WARN,
168 "server must be started as root to change uid"); 158 "server must be started as root to change uid");
169 } 159 }
170 160
161 // create tmp dir and pid file
162 char *mkdir_cmd = NULL;
163 asprintf(&mkdir_cmd, "mkdir -p %s", cfg->tmp.ptr);
164 system(mkdir_cmd);
165 free(mkdir_cmd);
166
167 char *pid_file_path = NULL;
168 asprintf(&pid_file_path, "%s/pid", cfg->tmp.ptr);
169 FILE *pidfile = fopen(pid_file_path, "w"); // TODO: check error
170 pid_t pid = getpid();
171 fprintf(pidfile, "%d", pid);
172 fclose(pidfile);
173 free(pid_file_path);
174
175 // create unix domain socket for server control
176 sstr_t tmp_priv = ucx_sprintf("%s/private", cfg->tmp.ptr);
177 // TODO: remove existing private dir
178 if(mkdir(tmp_priv.ptr, S_IRWXU)) {
179 if(errno == EEXIST) {
180 if(chmod(tmp_priv.ptr, S_IRWXU)) {
181 log_ereport(
182 LOG_CATASTROPHE,
183 "cannot change permissions of tmp dir %s:",
184 tmp_priv.ptr,
185 strerror(errno));
186 return 0;
187 }
188 } else {
189 log_ereport(
190 LOG_CATASTROPHE,
191 "cannot create tmp dir %s:",
192 tmp_priv.ptr,
193 strerror(errno));
194 return -1;
195 }
196 }
197
198
199 // create srvctrl unix domain socket
200 // this socket is used for stop, reconfigure and other operations
201 sstr_t uds_path = ucx_sprintf("%s/private/srvctrl.sock", cfg->tmp.ptr);
202 struct sockaddr_un addr;
203 if(uds_path.length > sizeof(addr.sun_path)-1) {
204 log_ereport(
205 LOG_CATASTROPHE,
206 "path '%s' too long for unix domain socket",
207 uds_path.ptr);
208 return -1;
209 }
210
211 // make sure there is no old srvctrl socket file
212 // otherweise bind would fail
213 if(unlink(uds_path.ptr)) {
214 if(errno != ENOENT) {
215 log_ereport(
216 LOG_CATASTROPHE,
217 "cannot unlink old srvctrl socket '%s': %s",
218 uds_path.ptr,
219 strerror(errno));
220 return -1;
221 }
222 }
223
224 ZERO(&addr, sizeof(addr));
225 addr.sun_family = AF_UNIX;
226 memcpy(addr.sun_path, uds_path.ptr, uds_path.length);
227
228 srvctrl = socket(AF_UNIX, SOCK_STREAM, 0);
229 if(srvctrl == -1) {
230 log_ereport(
231 LOG_CATASTROPHE,
232 "Cannot create server control socket: %s",
233 strerror(errno));
234 return -1;
235 }
236 if(bind(srvctrl, (struct sockaddr*)&addr, sizeof(addr))) {
237 log_ereport(
238 LOG_CATASTROPHE,
239 "srvctrl socket bind failed: %s",
240 strerror(errno));
241 return -1;
242 }
243
244 free(uds_path.ptr);
245
246
171 //endpwent(); // TODO: close or not? 247 //endpwent(); // TODO: close or not?
172 //free(pwbuf); // TODO: ? 248 //free(pwbuf); // TODO: ?
173 249
174 return 0; 250 return 0;
175 } 251 }
194 RestartCallback *re = atrestart; 270 RestartCallback *re = atrestart;
195 while(re) { 271 while(re) {
196 re->func(re->data); 272 re->func(re->data);
197 re = re->next; 273 re = re->next;
198 } 274 }
275 }
276
277 int webserver_srvctrl_fd() {
278 return srvctrl;
199 } 279 }
200 280
201 void webserver_atrestart(void (*fn)(void *), void *data) { 281 void webserver_atrestart(void (*fn)(void *), void *data) {
202 RestartCallback *cb = malloc(sizeof(RestartCallback)); 282 RestartCallback *cb = malloc(sizeof(RestartCallback));
203 cb->func = fn; 283 cb->func = fn;

mercurial