35 #include "log.h" |
35 #include "log.h" |
36 #include "webserver.h" |
36 #include "webserver.h" |
37 |
37 |
38 #include "../util/systhr.h" |
38 #include "../util/systhr.h" |
39 |
39 |
40 #include <ucx/utils.h> |
40 #include <cx/utils.h> |
41 #include <ucx/buffer.h> |
41 #include <cx/buffer.h> |
|
42 #include <cx/printf.h> |
42 |
43 |
43 #include "srvctrl.h" |
44 #include "srvctrl.h" |
44 |
45 |
45 #define SRVCTRL_THREAD_STACKSIZE 8192 |
46 #define SRVCTRL_THREAD_STACKSIZE 8192 |
46 |
47 |
47 static int srvctrl; |
48 static int srvctrl; |
48 static sstr_t srvctrl_path; |
49 static cxmutstr srvctrl_path; |
49 |
50 |
50 static WSBool srv_shutdown; |
51 static WSBool srv_shutdown; |
51 |
52 |
52 int srvctrl_init(ServerConfiguration *cfg) { |
53 int srvctrl_init(ServerConfiguration *cfg) { |
53 // create srvctrl unix domain socket |
54 // create srvctrl unix domain socket |
54 // this socket is used for stop, reconfigure and other operations |
55 // this socket is used for stop, reconfigure and other operations |
55 srvctrl_path = ucx_sprintf("%s/private/srvctrl.sock", cfg->tmp.ptr); |
56 srvctrl_path = cx_asprintf("%s/private/srvctrl.sock", cfg->tmp.ptr); |
56 struct sockaddr_un addr; |
57 struct sockaddr_un addr; |
57 if(srvctrl_path.length > sizeof(addr.sun_path)-1) { |
58 if(srvctrl_path.length > sizeof(addr.sun_path)-1) { |
58 log_ereport( |
59 log_ereport( |
59 LOG_CATASTROPHE, |
60 LOG_CATASTROPHE, |
60 "path '%s' too long for unix domain socket", |
61 "path '%s' too long for unix domain socket", |
146 log.write = (log_writefunc)srvctrl_log; |
147 log.write = (log_writefunc)srvctrl_log; |
147 log.cookie = client; |
148 log.cookie = client; |
148 log_add_logdup(&log); |
149 log_add_logdup(&log); |
149 |
150 |
150 char buf[64]; |
151 char buf[64]; |
151 UcxBuffer *line = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND); |
152 CxBuffer line; |
|
153 cxBufferInit(&line, NULL, 32, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
152 |
154 |
153 ssize_t r; |
155 ssize_t r; |
154 WSBool br = FALSE; |
156 WSBool br = FALSE; |
155 while((r = read(client->fd, buf, 64)) > 0) { |
157 while((r = read(client->fd, buf, 64)) > 0) { |
156 for(int i=0;i<r;i++) { |
158 for(int i=0;i<r;i++) { |
157 char c = buf[i]; |
159 char c = buf[i]; |
158 if(c == '\n') { |
160 if(c == '\n') { |
159 sstr_t ln = sstrn(line->space, line->pos); |
161 cxmutstr ln = cx_mutstrn(line.space, line.pos); |
160 if(srvctrl_handle_cmd(client, ln)) { |
162 if(srvctrl_handle_cmd(client, ln)) { |
161 br = TRUE; |
163 br = TRUE; |
162 break; |
164 break; |
163 } |
165 } |
164 } else { |
166 } else { |
165 ucx_buffer_putc(line, c); |
167 cxBufferPut(&line, c); |
166 } |
168 } |
167 } |
169 } |
168 if(br) { |
170 if(br) { |
169 break; |
171 break; |
170 } |
172 } |
171 } |
173 } |
172 log_remove_logdup(&log); |
174 log_remove_logdup(&log); |
173 |
175 |
174 ucx_buffer_free(line); |
176 cxBufferDestroy(&line); |
175 close(client->fd); |
177 close(client->fd); |
176 free(client); |
178 free(client); |
177 return NULL; |
179 return NULL; |
178 } |
180 } |
179 |
181 |
180 int srvctrl_handle_cmd(SrvCtrlClient *client, sstr_t cmd) { |
182 int srvctrl_handle_cmd(SrvCtrlClient *client, cxmutstr cmd) { |
181 if(!sstrcmp(cmd, S("reconfig"))) { |
183 if(!cx_strcmp(cx_strcast(cmd), cx_str("reconfig"))) { |
182 log_ereport(LOG_INFORM, "reconfigure server"); |
184 log_ereport(LOG_INFORM, "reconfigure server"); |
183 if(webserver_reconfig()) { |
185 if(webserver_reconfig()) { |
184 log_ereport(LOG_FAILURE, "cannot reload config"); |
186 log_ereport(LOG_FAILURE, "cannot reload config"); |
185 } else { |
187 } else { |
186 log_ereport(LOG_INFORM, "reconfig: success"); |
188 log_ereport(LOG_INFORM, "reconfig: success"); |
187 } |
189 } |
188 } else if(!sstrcmp(cmd, S("shutdown"))) { |
190 } else if(!cx_strcmp(cx_strcast(cmd), cx_str("shutdown"))) { |
189 webserver_shutdown(); |
191 webserver_shutdown(); |
190 } else if(!sstrcmp(cmd, S("stat"))) { |
192 } else if(!cx_strcmp(cx_strcast(cmd), cx_str("stat"))) { |
191 // TODO: implement |
193 // TODO: implement |
192 } else if(!sstrcmp(cmd, S("log"))) { |
194 } else if(!cx_strcmp(cx_strcast(cmd), cx_str("log"))) { |
193 return 0; |
195 return 0; |
194 } else { |
196 } else { |
195 log_ereport( |
197 log_ereport( |
196 LOG_FAILURE, |
198 LOG_FAILURE, |
197 "unknown srvctrl command: %.*s", |
199 "unknown srvctrl command: %.*s", |