nxt_controller.c (1926:6e85d6c0b8bb) nxt_controller.c (1969:be6409cdb028)
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Valentin V. Bartenev
5 * Copyright (C) NGINX, Inc.
6 */
7
8#include <nxt_main.h>

--- 27 unchanged lines hidden (view full) ---

36 ssize_t offset;
37 nxt_uint_t line;
38 nxt_uint_t column;
39} nxt_controller_response_t;
40
41
42static nxt_int_t nxt_controller_prefork(nxt_task_t *task,
43 nxt_process_t *process, nxt_mp_t *mp);
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Valentin V. Bartenev
5 * Copyright (C) NGINX, Inc.
6 */
7
8#include <nxt_main.h>

--- 27 unchanged lines hidden (view full) ---

36 ssize_t offset;
37 nxt_uint_t line;
38 nxt_uint_t column;
39} nxt_controller_response_t;
40
41
42static nxt_int_t nxt_controller_prefork(nxt_task_t *task,
43 nxt_process_t *process, nxt_mp_t *mp);
44static nxt_int_t nxt_controller_file_read(nxt_task_t *task, const char *name,
45 nxt_str_t *str, nxt_mp_t *mp);
44static nxt_int_t nxt_controller_start(nxt_task_t *task,
45 nxt_process_data_t *data);
46static void nxt_controller_process_new_port_handler(nxt_task_t *task,
47 nxt_port_recv_msg_t *msg);
48static void nxt_controller_send_current_conf(nxt_task_t *task);
49static void nxt_controller_router_ready_handler(nxt_task_t *task,
50 nxt_port_recv_msg_t *msg);
51static void nxt_controller_remove_pid_handler(nxt_task_t *task,

--- 97 unchanged lines hidden (view full) ---

149 .port_handlers = &nxt_controller_process_port_handlers,
150 .signals = nxt_process_signals,
151};
152
153
154static nxt_int_t
155nxt_controller_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp)
156{
46static nxt_int_t nxt_controller_start(nxt_task_t *task,
47 nxt_process_data_t *data);
48static void nxt_controller_process_new_port_handler(nxt_task_t *task,
49 nxt_port_recv_msg_t *msg);
50static void nxt_controller_send_current_conf(nxt_task_t *task);
51static void nxt_controller_router_ready_handler(nxt_task_t *task,
52 nxt_port_recv_msg_t *msg);
53static void nxt_controller_remove_pid_handler(nxt_task_t *task,

--- 97 unchanged lines hidden (view full) ---

151 .port_handlers = &nxt_controller_process_port_handlers,
152 .signals = nxt_process_signals,
153};
154
155
156static nxt_int_t
157nxt_controller_prefork(nxt_task_t *task, nxt_process_t *process, nxt_mp_t *mp)
158{
157 ssize_t n;
158 nxt_int_t ret;
159 nxt_str_t *conf;
160 nxt_file_t file;
159 nxt_str_t ver;
160 nxt_int_t ret, num;
161 nxt_runtime_t *rt;
161 nxt_runtime_t *rt;
162 nxt_file_info_t fi;
163 nxt_controller_init_t ctrl_init;
164
165 nxt_log(task, NXT_LOG_INFO, "controller started");
166
167 rt = task->thread->runtime;
168
169 nxt_memzero(&ctrl_init, sizeof(nxt_controller_init_t));
170
162 nxt_controller_init_t ctrl_init;
163
164 nxt_log(task, NXT_LOG_INFO, "controller started");
165
166 rt = task->thread->runtime;
167
168 nxt_memzero(&ctrl_init, sizeof(nxt_controller_init_t));
169
171 conf = &ctrl_init.conf;
170 /*
171 * Since configuration version has only been introduced in 1.26,
172 * set the default version to 1.25.
173 */
174 nxt_conf_ver = 12500;
172
175
176 ret = nxt_controller_file_read(task, rt->conf, &ctrl_init.conf, mp);
177 if (nxt_slow_path(ret == NXT_ERROR)) {
178 return NXT_ERROR;
179 }
180
181 if (ret == NXT_OK) {
182 ret = nxt_controller_file_read(task, rt->ver, &ver, mp);
183 if (nxt_slow_path(ret == NXT_ERROR)) {
184 return NXT_ERROR;
185 }
186
187 if (ret == NXT_OK) {
188 num = nxt_int_parse(ver.start, ver.length);
189
190 if (nxt_slow_path(num < 0)) {
191 nxt_alert(task, "failed to restore previous configuration: "
192 "invalid version string \"%V\"", &ver);
193
194 nxt_str_null(&ctrl_init.conf);
195
196 } else {
197 nxt_conf_ver = num;
198 }
199 }
200 }
201
202#if (NXT_TLS)
203 ctrl_init.certs = nxt_cert_store_load(task, mp);
204
205 nxt_mp_cleanup(mp, nxt_controller_cert_cleanup, task, ctrl_init.certs, rt);
206#endif
207
208 process->data.controller = ctrl_init;
209
210 return NXT_OK;
211}
212
213
214static nxt_int_t
215nxt_controller_file_read(nxt_task_t *task, const char *name, nxt_str_t *str,
216 nxt_mp_t *mp)
217{
218 ssize_t n;
219 nxt_int_t ret;
220 nxt_file_t file;
221 nxt_file_info_t fi;
222
173 nxt_memzero(&file, sizeof(nxt_file_t));
174
223 nxt_memzero(&file, sizeof(nxt_file_t));
224
175 file.name = (nxt_file_name_t *) rt->conf;
225 file.name = (nxt_file_name_t *) name;
176
177 ret = nxt_file_open(task, &file, NXT_FILE_RDONLY, NXT_FILE_OPEN, 0);
178
179 if (ret == NXT_OK) {
180 ret = nxt_file_info(&file, &fi);
226
227 ret = nxt_file_open(task, &file, NXT_FILE_RDONLY, NXT_FILE_OPEN, 0);
228
229 if (ret == NXT_OK) {
230 ret = nxt_file_info(&file, &fi);
231 if (nxt_slow_path(ret != NXT_OK)) {
232 goto fail;
233 }
181
234
182 if (nxt_fast_path(ret == NXT_OK && nxt_is_file(&fi))) {
183 conf->length = nxt_file_size(&fi);
184 conf->start = nxt_mp_alloc(mp, conf->length);
185 if (nxt_slow_path(conf->start == NULL)) {
186 nxt_file_close(task, &file);
187 return NXT_ERROR;
235 if (nxt_fast_path(nxt_is_file(&fi))) {
236 str->length = nxt_file_size(&fi);
237 str->start = nxt_mp_nget(mp, str->length);
238 if (nxt_slow_path(str->start == NULL)) {
239 goto fail;
188 }
189
240 }
241
190 n = nxt_file_read(&file, conf->start, conf->length, 0);
242 n = nxt_file_read(&file, str->start, str->length, 0);
243 if (nxt_slow_path(n != (ssize_t) str->length)) {
244 goto fail;
245 }
191
246
192 if (nxt_slow_path(n != (ssize_t) conf->length)) {
193 conf->start = NULL;
194 conf->length = 0;
247 nxt_file_close(task, &file);
195
248
196 nxt_alert(task, "failed to restore previous configuration: "
197 "cannot read the file");
198 }
249 return NXT_OK;
199 }
200
201 nxt_file_close(task, &file);
202 }
203
250 }
251
252 nxt_file_close(task, &file);
253 }
254
204#if (NXT_TLS)
205 ctrl_init.certs = nxt_cert_store_load(task, mp);
255 return NXT_DECLINED;
206
256
207 nxt_mp_cleanup(mp, nxt_controller_cert_cleanup, task, ctrl_init.certs, rt);
208#endif
257fail:
209
258
210 process->data.controller = ctrl_init;
259 nxt_file_close(task, &file);
211
260
212 return NXT_OK;
261 return NXT_ERROR;
213}
214
215
216#if (NXT_TLS)
217
218static void
219nxt_controller_cert_cleanup(nxt_task_t *task, void *obj, void *data)
220{

--- 67 unchanged lines hidden (view full) ---

288
289 vldt.pool = nxt_mp_create(1024, 128, 256, 32);
290 if (nxt_slow_path(vldt.pool == NULL)) {
291 nxt_mp_destroy(mp);
292 return NXT_ERROR;
293 }
294
295 vldt.conf = conf;
262}
263
264
265#if (NXT_TLS)
266
267static void
268nxt_controller_cert_cleanup(nxt_task_t *task, void *obj, void *data)
269{

--- 67 unchanged lines hidden (view full) ---

337
338 vldt.pool = nxt_mp_create(1024, 128, 256, 32);
339 if (nxt_slow_path(vldt.pool == NULL)) {
340 nxt_mp_destroy(mp);
341 return NXT_ERROR;
342 }
343
344 vldt.conf = conf;
345 vldt.conf_pool = mp;
346 vldt.ver = nxt_conf_ver;
296
297 ret = nxt_conf_validate(&vldt);
298
299 if (nxt_slow_path(ret != NXT_OK)) {
300
301 if (ret == NXT_DECLINED) {
302 nxt_alert(task, "the previous configuration is invalid: %V",
303 &vldt.error);

--- 915 unchanged lines hidden (view full) ---

1219 goto alloc_fail;
1220 }
1221 }
1222
1223 nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
1224
1225 vldt.conf = value;
1226 vldt.pool = c->mem_pool;
347
348 ret = nxt_conf_validate(&vldt);
349
350 if (nxt_slow_path(ret != NXT_OK)) {
351
352 if (ret == NXT_DECLINED) {
353 nxt_alert(task, "the previous configuration is invalid: %V",
354 &vldt.error);

--- 915 unchanged lines hidden (view full) ---

1270 goto alloc_fail;
1271 }
1272 }
1273
1274 nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
1275
1276 vldt.conf = value;
1277 vldt.pool = c->mem_pool;
1278 vldt.conf_pool = mp;
1279 vldt.ver = NXT_VERNUM;
1227
1228 rc = nxt_conf_validate(&vldt);
1229
1230 if (nxt_slow_path(rc != NXT_OK)) {
1231 nxt_mp_destroy(mp);
1232
1233 if (rc == NXT_DECLINED) {
1234 resp.detail = vldt.error;

--- 65 unchanged lines hidden (view full) ---

1300 nxt_mp_destroy(mp);
1301 goto alloc_fail;
1302 }
1303
1304 nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
1305
1306 vldt.conf = value;
1307 vldt.pool = c->mem_pool;
1280
1281 rc = nxt_conf_validate(&vldt);
1282
1283 if (nxt_slow_path(rc != NXT_OK)) {
1284 nxt_mp_destroy(mp);
1285
1286 if (rc == NXT_DECLINED) {
1287 resp.detail = vldt.error;

--- 65 unchanged lines hidden (view full) ---

1353 nxt_mp_destroy(mp);
1354 goto alloc_fail;
1355 }
1356
1357 nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
1358
1359 vldt.conf = value;
1360 vldt.pool = c->mem_pool;
1361 vldt.conf_pool = mp;
1362 vldt.ver = NXT_VERNUM;
1308
1309 rc = nxt_conf_validate(&vldt);
1310
1311 if (nxt_slow_path(rc != NXT_OK)) {
1312 nxt_mp_destroy(mp);
1313
1314 if (rc == NXT_DECLINED) {
1315 resp.detail = vldt.error;

--- 772 unchanged lines hidden ---
1363
1364 rc = nxt_conf_validate(&vldt);
1365
1366 if (nxt_slow_path(rc != NXT_OK)) {
1367 nxt_mp_destroy(mp);
1368
1369 if (rc == NXT_DECLINED) {
1370 resp.detail = vldt.error;

--- 772 unchanged lines hidden ---