~ubuntu-branches/ubuntu/trusty/nginx/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/nginx.c

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry
  • Date: 2013-04-25 12:51:45 UTC
  • mfrom: (1.3.28)
  • mto: (1.3.29) (15.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 64.
  • Revision ID: package-import@ubuntu.com-20130425125145-ugl0wor6bq0u5eae
Tags: upstream-1.4.0
ImportĀ upstreamĀ versionĀ 1.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/*
3
3
 * Copyright (C) Igor Sysoev
 
4
 * Copyright (C) Nginx, Inc.
4
5
 */
5
6
 
6
7
 
7
8
#include <ngx_config.h>
8
9
#include <ngx_core.h>
9
 
#include <ngx_event.h>
10
10
#include <nginx.h>
11
11
 
12
12
 
13
13
static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
14
 
static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv);
 
14
static ngx_int_t ngx_get_options(int argc, char *const *argv);
 
15
static ngx_int_t ngx_process_options(ngx_cycle_t *cycle);
15
16
static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv);
16
17
static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);
17
18
static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
20
21
static char *ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
21
22
static char *ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd,
22
23
    void *conf);
 
24
static char *ngx_set_worker_processes(ngx_conf_t *cf, ngx_command_t *cmd,
 
25
    void *conf);
23
26
 
24
27
 
25
28
static ngx_conf_enum_t  ngx_debug_points[] = {
68
71
 
69
72
    { ngx_string("worker_processes"),
70
73
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
71
 
      ngx_conf_set_num_slot,
72
 
      0,
73
 
      offsetof(ngx_core_conf_t, worker_processes),
 
74
      ngx_set_worker_processes,
 
75
      0,
 
76
      0,
74
77
      NULL },
75
78
 
76
79
    { ngx_string("debug_points"),
110
113
 
111
114
    { ngx_string("worker_rlimit_core"),
112
115
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
113
 
      ngx_conf_set_size_slot,
 
116
      ngx_conf_set_off_slot,
114
117
      0,
115
118
      offsetof(ngx_core_conf_t, rlimit_core),
116
119
      NULL },
181
184
};
182
185
 
183
186
 
184
 
ngx_uint_t  ngx_max_module;
185
 
 
186
 
static ngx_uint_t  ngx_show_version;
187
 
static ngx_uint_t  ngx_show_configure;
 
187
ngx_uint_t          ngx_max_module;
 
188
 
 
189
static ngx_uint_t   ngx_show_help;
 
190
static ngx_uint_t   ngx_show_version;
 
191
static ngx_uint_t   ngx_show_configure;
 
192
static u_char      *ngx_prefix;
 
193
static u_char      *ngx_conf_file;
 
194
static u_char      *ngx_conf_params;
 
195
static char        *ngx_signal;
 
196
 
188
197
 
189
198
static char **ngx_os_environ;
190
199
 
192
201
int ngx_cdecl
193
202
main(int argc, char *const *argv)
194
203
{
195
 
    char             *p;
196
 
    ssize_t           n;
197
204
    ngx_int_t         i;
198
205
    ngx_log_t        *log;
199
206
    ngx_cycle_t      *cycle, init_cycle;
200
207
    ngx_core_conf_t  *ccf;
201
208
 
202
 
#if (NGX_FREEBSD)
203
209
    ngx_debug_init();
204
 
#endif
 
210
 
 
211
    if (ngx_strerror_init() != NGX_OK) {
 
212
        return 1;
 
213
    }
 
214
 
 
215
    if (ngx_get_options(argc, argv) != NGX_OK) {
 
216
        return 1;
 
217
    }
 
218
 
 
219
    if (ngx_show_version) {
 
220
        ngx_write_stderr("nginx version: " NGINX_VER NGX_LINEFEED);
 
221
 
 
222
        if (ngx_show_help) {
 
223
            ngx_write_stderr(
 
224
                "Usage: nginx [-?hvVtq] [-s signal] [-c filename] "
 
225
                             "[-p prefix] [-g directives]" NGX_LINEFEED
 
226
                             NGX_LINEFEED
 
227
                "Options:" NGX_LINEFEED
 
228
                "  -?,-h         : this help" NGX_LINEFEED
 
229
                "  -v            : show version and exit" NGX_LINEFEED
 
230
                "  -V            : show version and configure options then exit"
 
231
                                   NGX_LINEFEED
 
232
                "  -t            : test configuration and exit" NGX_LINEFEED
 
233
                "  -q            : suppress non-error messages "
 
234
                                   "during configuration testing" NGX_LINEFEED
 
235
                "  -s signal     : send signal to a master process: "
 
236
                                   "stop, quit, reopen, reload" NGX_LINEFEED
 
237
#ifdef NGX_PREFIX
 
238
                "  -p prefix     : set prefix path (default: "
 
239
                                   NGX_PREFIX ")" NGX_LINEFEED
 
240
#else
 
241
                "  -p prefix     : set prefix path (default: NONE)" NGX_LINEFEED
 
242
#endif
 
243
                "  -c filename   : set configuration file (default: "
 
244
                                   NGX_CONF_PATH ")" NGX_LINEFEED
 
245
                "  -g directives : set global directives out of configuration "
 
246
                                   "file" NGX_LINEFEED NGX_LINEFEED
 
247
                );
 
248
        }
 
249
 
 
250
        if (ngx_show_configure) {
 
251
            ngx_write_stderr(
 
252
#ifdef NGX_COMPILER
 
253
                "built by " NGX_COMPILER NGX_LINEFEED
 
254
#endif
 
255
#if (NGX_SSL)
 
256
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
 
257
                "TLS SNI support enabled" NGX_LINEFEED
 
258
#else
 
259
                "TLS SNI support disabled" NGX_LINEFEED
 
260
#endif
 
261
#endif
 
262
                "configure arguments:" NGX_CONFIGURE NGX_LINEFEED);
 
263
        }
 
264
 
 
265
        if (!ngx_test_config) {
 
266
            return 0;
 
267
        }
 
268
    }
205
269
 
206
270
    /* TODO */ ngx_max_sockets = -1;
207
271
 
213
277
 
214
278
    ngx_pid = ngx_getpid();
215
279
 
216
 
    log = ngx_log_init();
 
280
    log = ngx_log_init(ngx_prefix);
217
281
    if (log == NULL) {
218
282
        return 1;
219
283
    }
223
287
    ngx_ssl_init(log);
224
288
#endif
225
289
 
226
 
    /* init_cycle->log is required for signal handlers and ngx_getopt() */
 
290
    /*
 
291
     * init_cycle->log is required for signal handlers and
 
292
     * ngx_process_options()
 
293
     */
227
294
 
228
295
    ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
229
296
    init_cycle.log = log;
238
305
        return 1;
239
306
    }
240
307
 
241
 
    if (ngx_getopt(&init_cycle, argc, ngx_argv) != NGX_OK) {
 
308
    if (ngx_process_options(&init_cycle) != NGX_OK) {
242
309
        return 1;
243
310
    }
244
311
 
245
 
    if (ngx_show_version) {
246
 
 
247
 
        p = "nginx version: " NGINX_VER CRLF;
248
 
        n = sizeof("nginx version: " NGINX_VER CRLF) - 1;
249
 
 
250
 
        if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) {
251
 
            return 1;
252
 
        }
253
 
 
254
 
        if (ngx_show_configure) {
255
 
#ifdef NGX_COMPILER
256
 
            p = "built by " NGX_COMPILER CRLF;
257
 
            n = sizeof("built by " NGX_COMPILER CRLF) - 1;
258
 
 
259
 
            if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) {
260
 
                return 1;
261
 
            }
262
 
#endif
263
 
 
264
 
            p = "configure arguments: " NGX_CONFIGURE CRLF;
265
 
            n = sizeof("configure arguments :" NGX_CONFIGURE CRLF) - 1;
266
 
 
267
 
            if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) {
268
 
                return 1;
269
 
            }
270
 
        }
271
 
 
272
 
        if (!ngx_test_config) {
273
 
            return 0;
274
 
        }
275
 
    }
276
 
 
277
 
    if (ngx_test_config) {
278
 
        log->log_level = NGX_LOG_INFO;
279
 
    }
280
 
 
281
312
    if (ngx_os_init(log) != NGX_OK) {
282
313
        return 1;
283
314
    }
302
333
    cycle = ngx_init_cycle(&init_cycle);
303
334
    if (cycle == NULL) {
304
335
        if (ngx_test_config) {
305
 
            ngx_log_error(NGX_LOG_EMERG, log, 0,
306
 
                          "the configuration file %s test failed",
307
 
                          init_cycle.conf_file.data);
 
336
            ngx_log_stderr(0, "configuration file %s test failed",
 
337
                           init_cycle.conf_file.data);
308
338
        }
309
339
 
310
340
        return 1;
311
341
    }
312
342
 
313
343
    if (ngx_test_config) {
314
 
        ngx_log_error(NGX_LOG_INFO, log, 0,
315
 
                      "the configuration file %s was tested successfully",
316
 
                      cycle->conf_file.data);
 
344
        if (!ngx_quiet_mode) {
 
345
            ngx_log_stderr(0, "configuration file %s test is successful",
 
346
                           cycle->conf_file.data);
 
347
        }
 
348
 
317
349
        return 0;
318
350
    }
319
351
 
 
352
    if (ngx_signal) {
 
353
        return ngx_signal_process(cycle, ngx_signal);
 
354
    }
 
355
 
320
356
    ngx_os_status(cycle->log);
321
357
 
322
358
    ngx_cycle = cycle;
323
359
 
324
360
    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
325
361
 
326
 
    ngx_process = ccf->master ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE;
327
 
 
328
 
#if (NGX_WIN32)
329
 
 
330
 
#if 0
331
 
 
332
 
    TODO:
333
 
 
334
 
    if (ccf->run_as_service) {
335
 
        if (ngx_service(cycle->log) != NGX_OK) {
336
 
            return 1;
337
 
        }
338
 
 
339
 
        return 0;
 
362
    if (ccf->master && ngx_process == NGX_PROCESS_SINGLE) {
 
363
        ngx_process = NGX_PROCESS_MASTER;
340
364
    }
341
 
#endif
342
365
 
343
 
#else
 
366
#if !(NGX_WIN32)
344
367
 
345
368
    if (ngx_init_signals(cycle->log) != NGX_OK) {
346
369
        return 1;
354
377
        ngx_daemonized = 1;
355
378
    }
356
379
 
 
380
    if (ngx_inherited) {
 
381
        ngx_daemonized = 1;
 
382
    }
 
383
 
 
384
#endif
 
385
 
357
386
    if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
358
387
        return 1;
359
388
    }
360
389
 
361
 
#endif
362
 
 
363
 
    if (ngx_process == NGX_PROCESS_MASTER) {
 
390
    if (cycle->log->file->fd != ngx_stderr) {
 
391
 
 
392
        if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {
 
393
            ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
 
394
                          ngx_set_stderr_n " failed");
 
395
            return 1;
 
396
        }
 
397
    }
 
398
 
 
399
    if (log->file->fd != ngx_stderr) {
 
400
        if (ngx_close_file(log->file->fd) == NGX_FILE_ERROR) {
 
401
            ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
 
402
                          ngx_close_file_n " built-in log failed");
 
403
        }
 
404
    }
 
405
 
 
406
    ngx_use_stderr = 0;
 
407
 
 
408
    if (ngx_process == NGX_PROCESS_SINGLE) {
 
409
        ngx_single_process_cycle(cycle);
 
410
 
 
411
    } else {
364
412
        ngx_master_process_cycle(cycle);
365
 
 
366
 
    } else {
367
 
        ngx_single_process_cycle(cycle);
368
413
    }
369
414
 
370
415
    return 0;
389
434
 
390
435
    if (ngx_array_init(&cycle->listening, cycle->pool, 10,
391
436
                       sizeof(ngx_listening_t))
392
 
        == NGX_ERROR)
 
437
        != NGX_OK)
393
438
    {
394
439
        return NGX_ERROR;
395
440
    }
534
579
    ngx_core_conf_t   *ccf;
535
580
    ngx_listening_t   *ls;
536
581
 
 
582
    ngx_memzero(&ctx, sizeof(ngx_exec_ctx_t));
 
583
 
537
584
    ctx.path = argv[0];
538
585
    ctx.name = "new binary process";
539
586
    ctx.argv = argv;
547
594
    var = ngx_alloc(sizeof(NGINX_VAR)
548
595
                    + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
549
596
                    cycle->log);
 
597
    if (var == NULL) {
 
598
        ngx_free(env);
 
599
        return NGX_INVALID_PID;
 
600
    }
550
601
 
551
602
    p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
552
603
 
586
637
 
587
638
    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
588
639
 
589
 
    if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data) != NGX_OK) {
 
640
    if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data) == NGX_FILE_ERROR) {
590
641
        ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
591
642
                      ngx_rename_file_n " %s to %s failed "
592
643
                      "before executing new binary process \"%s\"",
601
652
    pid = ngx_execute(cycle, &ctx);
602
653
 
603
654
    if (pid == NGX_INVALID_PID) {
604
 
        if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data) != NGX_OK) {
 
655
        if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data)
 
656
            == NGX_FILE_ERROR)
 
657
        {
605
658
            ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
606
659
                          ngx_rename_file_n " %s back to %s failed after "
607
 
                          "the try to execute the new binary process \"%s\"",
 
660
                          "an attempt to execute new binary process \"%s\"",
608
661
                          ccf->oldpid.data, ccf->pid.data, argv[0]);
609
662
        }
610
663
    }
617
670
 
618
671
 
619
672
static ngx_int_t
620
 
ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv)
 
673
ngx_get_options(int argc, char *const *argv)
621
674
{
622
 
    ngx_int_t  i;
 
675
    u_char     *p;
 
676
    ngx_int_t   i;
623
677
 
624
678
    for (i = 1; i < argc; i++) {
625
 
        if (argv[i][0] != '-') {
626
 
            ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
627
 
                          "invalid option: \"%s\"", argv[i]);
 
679
 
 
680
        p = (u_char *) argv[i];
 
681
 
 
682
        if (*p++ != '-') {
 
683
            ngx_log_stderr(0, "invalid option: \"%s\"", argv[i]);
628
684
            return NGX_ERROR;
629
685
        }
630
686
 
631
 
        switch (argv[i][1]) {
632
 
 
633
 
        case 'v':
634
 
            ngx_show_version = 1;
635
 
            break;
636
 
 
637
 
        case 'V':
638
 
            ngx_show_version = 1;
639
 
            ngx_show_configure = 1;
640
 
            break;
641
 
 
642
 
        case 't':
643
 
            ngx_test_config = 1;
644
 
            break;
645
 
 
646
 
        case 'c':
647
 
            if (argv[i + 1] == NULL) {
648
 
                ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
649
 
                              "the option: \"%s\" requires file name",
650
 
                              argv[i]);
 
687
        while (*p) {
 
688
 
 
689
            switch (*p++) {
 
690
 
 
691
            case '?':
 
692
            case 'h':
 
693
                ngx_show_version = 1;
 
694
                ngx_show_help = 1;
 
695
                break;
 
696
 
 
697
            case 'v':
 
698
                ngx_show_version = 1;
 
699
                break;
 
700
 
 
701
            case 'V':
 
702
                ngx_show_version = 1;
 
703
                ngx_show_configure = 1;
 
704
                break;
 
705
 
 
706
            case 't':
 
707
                ngx_test_config = 1;
 
708
                break;
 
709
 
 
710
            case 'q':
 
711
                ngx_quiet_mode = 1;
 
712
                break;
 
713
 
 
714
            case 'p':
 
715
                if (*p) {
 
716
                    ngx_prefix = p;
 
717
                    goto next;
 
718
                }
 
719
 
 
720
                if (argv[++i]) {
 
721
                    ngx_prefix = (u_char *) argv[i];
 
722
                    goto next;
 
723
                }
 
724
 
 
725
                ngx_log_stderr(0, "option \"-p\" requires directory name");
 
726
                return NGX_ERROR;
 
727
 
 
728
            case 'c':
 
729
                if (*p) {
 
730
                    ngx_conf_file = p;
 
731
                    goto next;
 
732
                }
 
733
 
 
734
                if (argv[++i]) {
 
735
                    ngx_conf_file = (u_char *) argv[i];
 
736
                    goto next;
 
737
                }
 
738
 
 
739
                ngx_log_stderr(0, "option \"-c\" requires file name");
 
740
                return NGX_ERROR;
 
741
 
 
742
            case 'g':
 
743
                if (*p) {
 
744
                    ngx_conf_params = p;
 
745
                    goto next;
 
746
                }
 
747
 
 
748
                if (argv[++i]) {
 
749
                    ngx_conf_params = (u_char *) argv[i];
 
750
                    goto next;
 
751
                }
 
752
 
 
753
                ngx_log_stderr(0, "option \"-g\" requires parameter");
 
754
                return NGX_ERROR;
 
755
 
 
756
            case 's':
 
757
                if (*p) {
 
758
                    ngx_signal = (char *) p;
 
759
 
 
760
                } else if (argv[++i]) {
 
761
                    ngx_signal = argv[i];
 
762
 
 
763
                } else {
 
764
                    ngx_log_stderr(0, "option \"-s\" requires parameter");
 
765
                    return NGX_ERROR;
 
766
                }
 
767
 
 
768
                if (ngx_strcmp(ngx_signal, "stop") == 0
 
769
                    || ngx_strcmp(ngx_signal, "quit") == 0
 
770
                    || ngx_strcmp(ngx_signal, "reopen") == 0
 
771
                    || ngx_strcmp(ngx_signal, "reload") == 0)
 
772
                {
 
773
                    ngx_process = NGX_PROCESS_SIGNALLER;
 
774
                    goto next;
 
775
                }
 
776
 
 
777
                ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal);
 
778
                return NGX_ERROR;
 
779
 
 
780
            default:
 
781
                ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1));
651
782
                return NGX_ERROR;
652
783
            }
653
 
 
654
 
            cycle->conf_file.data = (u_char *) argv[++i];
655
 
            cycle->conf_file.len = ngx_strlen(cycle->conf_file.data);
656
 
            break;
657
 
 
658
 
        default:
659
 
            ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
660
 
                          "invalid option: \"%s\"", argv[i]);
661
 
            return NGX_ERROR;
662
784
        }
663
 
    }
664
 
 
665
 
    if (cycle->conf_file.data == NULL) {
666
 
        cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;
667
 
        cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
 
785
 
 
786
    next:
 
787
 
 
788
        continue;
668
789
    }
669
790
 
670
791
    return NGX_OK;
713
834
}
714
835
 
715
836
 
 
837
static ngx_int_t
 
838
ngx_process_options(ngx_cycle_t *cycle)
 
839
{
 
840
    u_char  *p;
 
841
    size_t   len;
 
842
 
 
843
    if (ngx_prefix) {
 
844
        len = ngx_strlen(ngx_prefix);
 
845
        p = ngx_prefix;
 
846
 
 
847
        if (len && !ngx_path_separator(p[len - 1])) {
 
848
            p = ngx_pnalloc(cycle->pool, len + 1);
 
849
            if (p == NULL) {
 
850
                return NGX_ERROR;
 
851
            }
 
852
 
 
853
            ngx_memcpy(p, ngx_prefix, len);
 
854
            p[len++] = '/';
 
855
        }
 
856
 
 
857
        cycle->conf_prefix.len = len;
 
858
        cycle->conf_prefix.data = p;
 
859
        cycle->prefix.len = len;
 
860
        cycle->prefix.data = p;
 
861
 
 
862
    } else {
 
863
 
 
864
#ifndef NGX_PREFIX
 
865
 
 
866
        p = ngx_pnalloc(cycle->pool, NGX_MAX_PATH);
 
867
        if (p == NULL) {
 
868
            return NGX_ERROR;
 
869
        }
 
870
 
 
871
        if (ngx_getcwd(p, NGX_MAX_PATH) == 0) {
 
872
            ngx_log_stderr(ngx_errno, "[emerg]: " ngx_getcwd_n " failed");
 
873
            return NGX_ERROR;
 
874
        }
 
875
 
 
876
        len = ngx_strlen(p);
 
877
 
 
878
        p[len++] = '/';
 
879
 
 
880
        cycle->conf_prefix.len = len;
 
881
        cycle->conf_prefix.data = p;
 
882
        cycle->prefix.len = len;
 
883
        cycle->prefix.data = p;
 
884
 
 
885
#else
 
886
 
 
887
#ifdef NGX_CONF_PREFIX
 
888
        ngx_str_set(&cycle->conf_prefix, NGX_CONF_PREFIX);
 
889
#else
 
890
        ngx_str_set(&cycle->conf_prefix, NGX_PREFIX);
 
891
#endif
 
892
        ngx_str_set(&cycle->prefix, NGX_PREFIX);
 
893
 
 
894
#endif
 
895
    }
 
896
 
 
897
    if (ngx_conf_file) {
 
898
        cycle->conf_file.len = ngx_strlen(ngx_conf_file);
 
899
        cycle->conf_file.data = ngx_conf_file;
 
900
 
 
901
    } else {
 
902
        ngx_str_set(&cycle->conf_file, NGX_CONF_PATH);
 
903
    }
 
904
 
 
905
    if (ngx_conf_full_name(cycle, &cycle->conf_file, 0) != NGX_OK) {
 
906
        return NGX_ERROR;
 
907
    }
 
908
 
 
909
    for (p = cycle->conf_file.data + cycle->conf_file.len - 1;
 
910
         p > cycle->conf_file.data;
 
911
         p--)
 
912
    {
 
913
        if (ngx_path_separator(*p)) {
 
914
            cycle->conf_prefix.len = p - ngx_cycle->conf_file.data + 1;
 
915
            cycle->conf_prefix.data = ngx_cycle->conf_file.data;
 
916
            break;
 
917
        }
 
918
    }
 
919
 
 
920
    if (ngx_conf_params) {
 
921
        cycle->conf_param.len = ngx_strlen(ngx_conf_params);
 
922
        cycle->conf_param.data = ngx_conf_params;
 
923
    }
 
924
 
 
925
    if (ngx_test_config) {
 
926
        cycle->log->log_level = NGX_LOG_INFO;
 
927
    }
 
928
 
 
929
    return NGX_OK;
 
930
}
 
931
 
 
932
 
716
933
static void *
717
934
ngx_core_module_create_conf(ngx_cycle_t *cycle)
718
935
{
724
941
    }
725
942
 
726
943
    /*
727
 
     * set by pcalloc()
 
944
     * set by ngx_pcalloc()
728
945
     *
729
946
     *     ccf->pid = NULL;
730
947
     *     ccf->oldpid = NULL;
741
958
    ccf->debug_points = NGX_CONF_UNSET;
742
959
 
743
960
    ccf->rlimit_nofile = NGX_CONF_UNSET;
744
 
    ccf->rlimit_core = NGX_CONF_UNSET_SIZE;
 
961
    ccf->rlimit_core = NGX_CONF_UNSET;
745
962
    ccf->rlimit_sigpending = NGX_CONF_UNSET;
746
963
 
747
964
    ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT;
774
991
    ngx_conf_init_value(ccf->worker_processes, 1);
775
992
    ngx_conf_init_value(ccf->debug_points, 0);
776
993
 
777
 
#if (NGX_HAVE_SCHED_SETAFFINITY)
 
994
#if (NGX_HAVE_CPU_AFFINITY)
778
995
 
779
996
    if (ccf->cpu_affinity_n
780
997
        && ccf->cpu_affinity_n != 1
781
998
        && ccf->cpu_affinity_n != (ngx_uint_t) ccf->worker_processes)
782
999
    {
783
1000
        ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
784
 
                      "number of the \"worker_processes\" is not equal to "
785
 
                      "the number of the \"worker_cpu_affinity\" mask, "
 
1001
                      "the number of \"worker_processes\" is not equal to "
 
1002
                      "the number of \"worker_cpu_affinity\" masks, "
786
1003
                      "using last mask for remaining worker processes");
787
1004
    }
788
1005
 
796
1013
 
797
1014
#endif
798
1015
 
 
1016
 
 
1017
    if (ccf->pid.len == 0) {
 
1018
        ngx_str_set(&ccf->pid, NGX_PID_PATH);
 
1019
    }
 
1020
 
 
1021
    if (ngx_conf_full_name(cycle, &ccf->pid, 0) != NGX_OK) {
 
1022
        return NGX_CONF_ERROR;
 
1023
    }
 
1024
 
 
1025
    ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT);
 
1026
 
 
1027
    ccf->oldpid.data = ngx_pnalloc(cycle->pool, ccf->oldpid.len);
 
1028
    if (ccf->oldpid.data == NULL) {
 
1029
        return NGX_CONF_ERROR;
 
1030
    }
 
1031
 
 
1032
    ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len),
 
1033
               NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
 
1034
 
 
1035
 
799
1036
#if !(NGX_WIN32)
800
1037
 
801
1038
    if (ccf->user == (uid_t) NGX_CONF_UNSET_UINT && geteuid() == 0) {
824
1061
        ccf->group = grp->gr_gid;
825
1062
    }
826
1063
 
827
 
    if (ccf->pid.len == 0) {
828
 
        ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
829
 
        ccf->pid.data = (u_char *) NGX_PID_PATH;
830
 
    }
831
 
 
832
 
    if (ngx_conf_full_name(cycle, &ccf->pid, 0) == NGX_ERROR) {
833
 
        return NGX_CONF_ERROR;
834
 
    }
835
 
 
836
 
    ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT);
837
 
 
838
 
    ccf->oldpid.data = ngx_palloc(cycle->pool, ccf->oldpid.len);
839
 
    if (ccf->oldpid.data == NULL) {
840
 
        return NGX_CONF_ERROR;
841
 
    }
842
 
 
843
 
    ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len),
844
 
               NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
845
 
 
846
1064
 
847
1065
    if (ccf->lock_file.len == 0) {
848
 
        ccf->lock_file.len = sizeof(NGX_LOCK_PATH) - 1;
849
 
        ccf->lock_file.data = (u_char *) NGX_LOCK_PATH;
 
1066
        ngx_str_set(&ccf->lock_file, NGX_LOCK_PATH);
850
1067
    }
851
1068
 
852
 
    if (ngx_conf_full_name(cycle, &ccf->lock_file, 0) == NGX_ERROR) {
 
1069
    if (ngx_conf_full_name(cycle, &ccf->lock_file, 0) != NGX_OK) {
853
1070
        return NGX_CONF_ERROR;
854
1071
    }
855
1072
 
879
1096
 
880
1097
    } else {
881
1098
        cycle->lock_file.len = ccf->lock_file.len + 1;
882
 
        cycle->lock_file.data = ngx_palloc(cycle->pool,
 
1099
        cycle->lock_file.data = ngx_pnalloc(cycle->pool,
883
1100
                                      ccf->lock_file.len + sizeof(".accept"));
884
1101
        if (cycle->lock_file.data == NULL) {
885
1102
            return NGX_CONF_ERROR;
1033
1250
static char *
1034
1251
ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1035
1252
{
1036
 
#if (NGX_HAVE_SCHED_SETAFFINITY)
 
1253
#if (NGX_HAVE_CPU_AFFINITY)
1037
1254
    ngx_core_conf_t  *ccf = conf;
1038
1255
 
1039
1256
    u_char            ch;
1040
 
    u_long           *mask;
 
1257
    uint64_t         *mask;
1041
1258
    ngx_str_t        *value;
1042
1259
    ngx_uint_t        i, n;
1043
1260
 
1045
1262
        return "is duplicate";
1046
1263
    }
1047
1264
 
1048
 
    mask = ngx_palloc(cf->pool, (cf->args->nelts - 1) * sizeof(long));
 
1265
    mask = ngx_palloc(cf->pool, (cf->args->nelts - 1) * sizeof(uint64_t));
1049
1266
    if (mask == NULL) {
1050
1267
        return NGX_CONF_ERROR;
1051
1268
    }
1057
1274
 
1058
1275
    for (n = 1; n < cf->args->nelts; n++) {
1059
1276
 
1060
 
        if (value[n].len > 32) {
 
1277
        if (value[n].len > 64) {
1061
1278
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1062
 
                         "\"worker_cpu_affinity\" supports up to 32 CPU only");
 
1279
                         "\"worker_cpu_affinity\" supports up to 64 CPUs only");
1063
1280
            return NGX_CONF_ERROR;
1064
1281
        }
1065
1282
 
1102
1319
}
1103
1320
 
1104
1321
 
1105
 
u_long
 
1322
uint64_t
1106
1323
ngx_get_cpu_affinity(ngx_uint_t n)
1107
1324
{
1108
1325
    ngx_core_conf_t  *ccf;
1120
1337
 
1121
1338
    return ccf->cpu_affinity[ccf->cpu_affinity_n - 1];
1122
1339
}
 
1340
 
 
1341
 
 
1342
static char *
 
1343
ngx_set_worker_processes(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
 
1344
{
 
1345
    ngx_str_t        *value;
 
1346
    ngx_core_conf_t  *ccf;
 
1347
 
 
1348
    ccf = (ngx_core_conf_t *) conf;
 
1349
 
 
1350
    if (ccf->worker_processes != NGX_CONF_UNSET) {
 
1351
        return "is duplicate";
 
1352
    }
 
1353
 
 
1354
    value = (ngx_str_t *) cf->args->elts;
 
1355
 
 
1356
    if (ngx_strcmp(value[1].data, "auto") == 0) {
 
1357
        ccf->worker_processes = ngx_ncpu;
 
1358
        return NGX_CONF_OK;
 
1359
    }
 
1360
 
 
1361
    ccf->worker_processes = ngx_atoi(value[1].data, value[1].len);
 
1362
 
 
1363
    if (ccf->worker_processes == NGX_ERROR) {
 
1364
        return "invalid value";
 
1365
    }
 
1366
 
 
1367
    return NGX_CONF_OK;
 
1368
}