comparison src/core/ngx_conf_file.c @ 2222:d200fbe9aa81

test conf file size, this fixes OpenBSD's "nginx -c /tmp/" bug
author Igor Sysoev <igor@sysoev.ru>
date Mon, 01 Sep 2008 13:59:11 +0000
parents 31fda45f696f
children 7f3e1c63606b
comparison
equal deleted inserted replaced
2221:78415b0469bc 2222:d200fbe9aa81
5 5
6 6
7 #include <ngx_config.h> 7 #include <ngx_config.h>
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 9
10 #define NGX_CONF_BUFFER 4096
10 11
11 static ngx_int_t ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last); 12 static ngx_int_t ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last);
12 static ngx_int_t ngx_conf_read_token(ngx_conf_t *cf); 13 static ngx_int_t ngx_conf_read_token(ngx_conf_t *cf);
13 static char *ngx_conf_include(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 14 static char *ngx_conf_include(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
14 static void ngx_conf_flush_files(ngx_cycle_t *cycle); 15 static void ngx_conf_flush_files(ngx_cycle_t *cycle);
41 NULL, /* exit master */ 42 NULL, /* exit master */
42 NGX_MODULE_V1_PADDING 43 NGX_MODULE_V1_PADDING
43 }; 44 };
44 45
45 46
46 /* The ten fixed arguments */ 47 /* The eight fixed arguments */
47 48
48 static ngx_uint_t argument_number[] = { 49 static ngx_uint_t argument_number[] = {
49 NGX_CONF_NOARGS, 50 NGX_CONF_NOARGS,
50 NGX_CONF_TAKE1, 51 NGX_CONF_TAKE1,
51 NGX_CONF_TAKE2, 52 NGX_CONF_TAKE2,
139 return NGX_CONF_ERROR; 140 return NGX_CONF_ERROR;
140 } 141 }
141 142
142 cf->conf_file->buffer = b; 143 cf->conf_file->buffer = b;
143 144
144 b->start = ngx_alloc(ngx_pagesize, cf->log); 145 b->start = ngx_alloc(NGX_CONF_BUFFER, cf->log);
145 if (b->start == NULL) { 146 if (b->start == NULL) {
146 return NGX_CONF_ERROR; 147 return NGX_CONF_ERROR;
147 } 148 }
148 149
149 b->pos = b->start; 150 b->pos = b->start;
150 b->last = b->start; 151 b->last = b->start;
151 b->end = b->last + ngx_pagesize; 152 b->end = b->last + NGX_CONF_BUFFER;
152 b->temporary = 1; 153 b->temporary = 1;
153 154
154 cf->conf_file->file.fd = fd; 155 cf->conf_file->file.fd = fd;
155 cf->conf_file->file.name.len = filename->len; 156 cf->conf_file->file.name.len = filename->len;
156 cf->conf_file->file.name.data = filename->data; 157 cf->conf_file->file.name.data = filename->data;
431 432
432 static ngx_int_t 433 static ngx_int_t
433 ngx_conf_read_token(ngx_conf_t *cf) 434 ngx_conf_read_token(ngx_conf_t *cf)
434 { 435 {
435 u_char *start, ch, *src, *dst; 436 u_char *start, ch, *src, *dst;
437 off_t file_size;
436 size_t len; 438 size_t len;
437 ssize_t n; 439 ssize_t n, size;
438 ngx_uint_t found, need_space, last_space, sharp_comment, variable; 440 ngx_uint_t found, need_space, last_space, sharp_comment, variable;
439 ngx_uint_t quoted, s_quoted, d_quoted, start_line; 441 ngx_uint_t quoted, s_quoted, d_quoted, start_line;
440 ngx_str_t *word; 442 ngx_str_t *word;
441 ngx_buf_t *b; 443 ngx_buf_t *b;
442 444
450 cf->args->nelts = 0; 452 cf->args->nelts = 0;
451 b = cf->conf_file->buffer; 453 b = cf->conf_file->buffer;
452 start = b->pos; 454 start = b->pos;
453 start_line = cf->conf_file->line; 455 start_line = cf->conf_file->line;
454 456
457 file_size = ngx_file_size(&cf->conf_file->file.info);
458
455 for ( ;; ) { 459 for ( ;; ) {
456 460
457 if (b->pos >= b->last) { 461 if (b->pos >= b->last) {
458 462
459 if (cf->conf_file->file.offset 463 if (cf->conf_file->file.offset >= file_size) {
460 >= ngx_file_size(&cf->conf_file->file.info)) 464
461 {
462 if (cf->args->nelts > 0) { 465 if (cf->args->nelts > 0) {
463 466
464 if (cf->conf_file->file.fd == NGX_INVALID_FILE) { 467 if (cf->conf_file->file.fd == NGX_INVALID_FILE) {
465 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, 468 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
466 "unexpected end of parameter, " 469 "unexpected end of parameter, "
477 return NGX_CONF_FILE_DONE; 480 return NGX_CONF_FILE_DONE;
478 } 481 }
479 482
480 len = b->pos - start; 483 len = b->pos - start;
481 484
482 if (len == ngx_pagesize) { 485 if (len == NGX_CONF_BUFFER) {
483 cf->conf_file->line = start_line; 486 cf->conf_file->line = start_line;
484 487
485 if (d_quoted) { 488 if (d_quoted) {
486 ch = '"'; 489 ch = '"';
487 490
503 506
504 if (len) { 507 if (len) {
505 ngx_memcpy(b->start, start, len); 508 ngx_memcpy(b->start, start, len);
506 } 509 }
507 510
508 n = ngx_read_file(&cf->conf_file->file, b->start + len, 511 size = file_size - cf->conf_file->file.offset;
509 b->end - (b->start + len), 512
513 if (size > b->end - (b->start + len)) {
514 size = b->end - (b->start + len);
515 }
516
517 n = ngx_read_file(&cf->conf_file->file, b->start + len, size,
510 cf->conf_file->file.offset); 518 cf->conf_file->file.offset);
511 519
512 if (n == NGX_ERROR) { 520 if (n == NGX_ERROR) {
521 return NGX_ERROR;
522 }
523
524 if (n != size) {
525 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
526 ngx_read_file_n " returned "
527 "only %z bytes instead of %z",
528 n, size);
513 return NGX_ERROR; 529 return NGX_ERROR;
514 } 530 }
515 531
516 b->pos = b->start + len; 532 b->pos = b->start + len;
517 b->last = b->pos + n; 533 b->last = b->pos + n;