3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
12 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
13 static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv);
14 static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv);
15 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);
16 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
17 static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
18 static char *ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
19 static char *ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
20 static char *ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd,
24 static ngx_conf_enum_t ngx_debug_points[] = {
25 { ngx_string("stop"), NGX_DEBUG_POINTS_STOP },
26 { ngx_string("abort"), NGX_DEBUG_POINTS_ABORT },
27 { ngx_null_string, 0 }
31 static ngx_command_t ngx_core_commands[] = {
33 { ngx_string("daemon"),
34 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
35 ngx_conf_set_flag_slot,
37 offsetof(ngx_core_conf_t, daemon),
40 { ngx_string("master_process"),
41 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
42 ngx_conf_set_flag_slot,
44 offsetof(ngx_core_conf_t, master),
47 { ngx_string("timer_resolution"),
48 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
49 ngx_conf_set_msec_slot,
51 offsetof(ngx_core_conf_t, timer_resolution),
55 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
56 ngx_conf_set_str_slot,
58 offsetof(ngx_core_conf_t, pid),
61 { ngx_string("lock_file"),
62 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
63 ngx_conf_set_str_slot,
65 offsetof(ngx_core_conf_t, lock_file),
68 { ngx_string("worker_processes"),
69 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
70 ngx_conf_set_num_slot,
72 offsetof(ngx_core_conf_t, worker_processes),
75 { ngx_string("debug_points"),
76 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
77 ngx_conf_set_enum_slot,
79 offsetof(ngx_core_conf_t, debug_points),
83 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12,
89 { ngx_string("worker_priority"),
90 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
96 { ngx_string("worker_cpu_affinity"),
97 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_1MORE,
103 { ngx_string("worker_rlimit_nofile"),
104 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
105 ngx_conf_set_num_slot,
107 offsetof(ngx_core_conf_t, rlimit_nofile),
110 { ngx_string("worker_rlimit_core"),
111 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
112 ngx_conf_set_size_slot,
114 offsetof(ngx_core_conf_t, rlimit_core),
117 { ngx_string("worker_rlimit_sigpending"),
118 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
119 ngx_conf_set_num_slot,
121 offsetof(ngx_core_conf_t, rlimit_sigpending),
124 { ngx_string("working_directory"),
125 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
126 ngx_conf_set_str_slot,
128 offsetof(ngx_core_conf_t, working_directory),
132 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
140 { ngx_string("worker_threads"),
141 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
142 ngx_conf_set_num_slot,
144 offsetof(ngx_core_conf_t, worker_threads),
147 { ngx_string("thread_stack_size"),
148 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
149 ngx_conf_set_size_slot,
151 offsetof(ngx_core_conf_t, thread_stack_size),
160 static ngx_core_module_t ngx_core_module_ctx = {
162 ngx_core_module_create_conf,
163 ngx_core_module_init_conf
167 ngx_module_t ngx_core_module = {
169 &ngx_core_module_ctx, /* module context */
170 ngx_core_commands, /* module directives */
171 NGX_CORE_MODULE, /* module type */
172 NULL, /* init master */
173 NULL, /* init module */
174 NULL, /* init process */
175 NULL, /* init thread */
176 NULL, /* exit thread */
177 NULL, /* exit process */
178 NULL, /* exit master */
179 NGX_MODULE_V1_PADDING
183 ngx_uint_t ngx_max_module;
185 static ngx_uint_t ngx_show_version;
186 static ngx_uint_t ngx_show_configure;
188 static char **ngx_os_environ;
192 main(int argc, char *const *argv)
198 ngx_cycle_t *cycle, init_cycle;
199 ngx_core_conf_t *ccf;
205 /* TODO */ ngx_max_sockets = -1;
213 ngx_pid = ngx_getpid();
215 log = ngx_log_init();
225 /* init_cycle->log is required for signal handlers and ngx_getopt() */
227 ngx_memzero(&init_cycle, sizeof(ngx_cycle_t));
228 init_cycle.log = log;
229 ngx_cycle = &init_cycle;
231 init_cycle.pool = ngx_create_pool(1024, log);
232 if (init_cycle.pool == NULL) {
236 if (ngx_save_argv(&init_cycle, argc, argv) != NGX_OK) {
240 if (ngx_getopt(&init_cycle, argc, ngx_argv) != NGX_OK) {
244 if (ngx_show_version) {
246 p = "nginx version: " NGINX_VER CRLF;
247 n = sizeof("nginx version: " NGINX_VER CRLF) - 1;
249 if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) {
253 if (ngx_show_configure) {
255 p = "built by " NGX_COMPILER CRLF;
256 n = sizeof("built by " NGX_COMPILER CRLF) - 1;
258 if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) {
263 p = "configure arguments: " NGX_CONFIGURE CRLF;
264 n = sizeof("configure arguments :" NGX_CONFIGURE CRLF) - 1;
266 if (ngx_write_fd(ngx_stderr_fileno, p, n) != n) {
271 if (!ngx_test_config) {
276 if (ngx_test_config) {
277 log->log_level = NGX_LOG_INFO;
280 if (ngx_os_init(log) != NGX_OK) {
285 * ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init()
288 if (ngx_crc32_table_init() != NGX_OK) {
292 if (ngx_add_inherited_sockets(&init_cycle) != NGX_OK) {
297 for (i = 0; ngx_modules[i]; i++) {
298 ngx_modules[i]->index = ngx_max_module++;
301 cycle = ngx_init_cycle(&init_cycle);
303 if (ngx_test_config) {
304 ngx_log_error(NGX_LOG_EMERG, log, 0,
305 "the configuration file %s test failed",
306 init_cycle.conf_file.data);
312 if (ngx_test_config) {
313 ngx_log_error(NGX_LOG_INFO, log, 0,
314 "the configuration file %s was tested successfully",
315 cycle->conf_file.data);
319 ngx_os_status(cycle->log);
323 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
325 ngx_process = ccf->master ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE;
333 if (ccf->run_as_service) {
334 if (ngx_service(cycle->log) != NGX_OK) {
344 if (ngx_init_signals(cycle->log) != NGX_OK) {
348 if (!ngx_inherited && ccf->daemon) {
349 if (ngx_daemon(cycle->log) != NGX_OK) {
356 if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
362 if (ngx_process == NGX_PROCESS_MASTER) {
363 ngx_master_process_cycle(cycle);
366 ngx_single_process_cycle(cycle);
374 ngx_add_inherited_sockets(ngx_cycle_t *cycle)
376 u_char *p, *v, *inherited;
380 inherited = (u_char *) getenv(NGINX_VAR);
382 if (inherited == NULL) {
386 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
387 "using inherited sockets from \"%s\"", inherited);
389 if (ngx_array_init(&cycle->listening, cycle->pool, 10,
390 sizeof(ngx_listening_t))
396 for (p = inherited, v = p; *p; p++) {
397 if (*p == ':' || *p == ';') {
398 s = ngx_atoi(v, p - v);
399 if (s == NGX_ERROR) {
400 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
401 "invalid socket number \"%s\" in " NGINX_VAR
402 " environment variable, ignoring the rest"
403 " of the variable", v);
409 ls = ngx_array_push(&cycle->listening);
414 ngx_memzero(ls, sizeof(ngx_listening_t));
416 ls->fd = (ngx_socket_t) s;
422 return ngx_set_inherited_sockets(cycle);
427 ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last)
432 ngx_core_conf_t *ccf;
434 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
436 if (last == NULL && ccf->environment) {
437 return ccf->environment;
442 for (i = 0; i < ccf->env.nelts; i++) {
443 if (ngx_strcmp(var[i].data, "TZ") == 0
444 || ngx_strncmp(var[i].data, "TZ=", 3) == 0)
450 var = ngx_array_push(&ccf->env);
456 var->data = (u_char *) "TZ";
464 for (i = 0; i < ccf->env.nelts; i++) {
466 if (var[i].data[var[i].len] == '=') {
471 for (p = ngx_os_environ; *p; p++) {
473 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
474 && (*p)[var[i].len] == '=')
483 env = ngx_alloc((*last + n + 1) * sizeof(char *), cycle->log);
487 env = ngx_palloc(cycle->pool, (n + 1) * sizeof(char *));
496 for (i = 0; i < ccf->env.nelts; i++) {
498 if (var[i].data[var[i].len] == '=') {
499 env[n++] = (char *) var[i].data;
503 for (p = ngx_os_environ; *p; p++) {
505 if (ngx_strncmp(*p, var[i].data, var[i].len) == 0
506 && (*p)[var[i].len] == '=')
517 ccf->environment = env;
526 ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv)
533 ngx_core_conf_t *ccf;
537 ctx.name = "new binary process";
541 env = ngx_set_environment(cycle, &n);
543 return NGX_INVALID_PID;
546 var = ngx_alloc(sizeof(NGINX_VAR)
547 + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2,
550 p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR));
552 ls = cycle->listening.elts;
553 for (i = 0; i < cycle->listening.nelts; i++) {
554 p = ngx_sprintf(p, "%ud;", ls[i].fd);
561 #if (NGX_SETPROCTITLE_USES_ENV)
563 /* allocate the spare 300 bytes for the new binary process title */
565 env[n++] = "SPARE=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
566 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
567 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
568 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
569 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
578 for (e = env; *e; e++) {
579 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "env: %s", *e);
584 ctx.envp = (char *const *) env;
586 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
588 if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data) != NGX_OK) {
589 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
590 ngx_rename_file_n " %s to %s failed "
591 "before executing new binary process \"%s\"",
592 ccf->pid.data, ccf->oldpid.data, argv[0]);
597 return NGX_INVALID_PID;
600 pid = ngx_execute(cycle, &ctx);
602 if (pid == NGX_INVALID_PID) {
603 if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data) != NGX_OK) {
604 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
605 ngx_rename_file_n " %s back to %s failed after "
606 "the try to execute the new binary process \"%s\"",
607 ccf->oldpid.data, ccf->pid.data, argv[0]);
619 ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv)
623 for (i = 1; i < argc; i++) {
624 if (argv[i][0] != '-') {
625 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
626 "invalid option: \"%s\"", argv[i]);
630 switch (argv[i][1]) {
633 ngx_show_version = 1;
637 ngx_show_version = 1;
638 ngx_show_configure = 1;
646 if (argv[i + 1] == NULL) {
647 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
648 "the option \"-c\" requires file name");
652 cycle->conf_file.data = (u_char *) argv[++i];
653 cycle->conf_file.len = ngx_strlen(cycle->conf_file.data);
657 if (argv[i + 1] == NULL) {
658 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
659 "the option \"-g\" requires parameter");
663 cycle->conf_param.data = (u_char *) argv[++i];
664 cycle->conf_param.len = ngx_strlen(cycle->conf_param.data);
668 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
669 "invalid option: \"%s\"", argv[i]);
674 if (cycle->conf_file.data == NULL) {
675 cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1;
676 cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
684 ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv)
688 ngx_os_argv = (char **) argv;
690 ngx_argv = (char **) argv;
696 ngx_os_argv = (char **) argv;
699 ngx_argv = ngx_alloc((argc + 1) * sizeof(char *), cycle->log);
700 if (ngx_argv == NULL) {
704 for (i = 0; i < argc; i++) {
705 len = ngx_strlen(argv[i]) + 1;
707 ngx_argv[i] = ngx_alloc(len, cycle->log);
708 if (ngx_argv[i] == NULL) {
712 (void) ngx_cpystrn((u_char *) ngx_argv[i], (u_char *) argv[i], len);
719 ngx_os_environ = environ;
726 ngx_core_module_create_conf(ngx_cycle_t *cycle)
728 ngx_core_conf_t *ccf;
730 ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t));
739 * ccf->oldpid = NULL;
741 * ccf->cpu_affinity_n = 0;
742 * ccf->cpu_affinity = NULL;
745 ccf->daemon = NGX_CONF_UNSET;
746 ccf->master = NGX_CONF_UNSET;
747 ccf->timer_resolution = NGX_CONF_UNSET_MSEC;
749 ccf->worker_processes = NGX_CONF_UNSET;
750 ccf->debug_points = NGX_CONF_UNSET;
752 ccf->rlimit_nofile = NGX_CONF_UNSET;
753 ccf->rlimit_core = NGX_CONF_UNSET_SIZE;
754 ccf->rlimit_sigpending = NGX_CONF_UNSET;
756 ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT;
757 ccf->group = (ngx_gid_t) NGX_CONF_UNSET_UINT;
760 ccf->worker_threads = NGX_CONF_UNSET;
761 ccf->thread_stack_size = NGX_CONF_UNSET_SIZE;
764 if (ngx_array_init(&ccf->env, cycle->pool, 1, sizeof(ngx_str_t))
775 ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf)
777 ngx_core_conf_t *ccf = conf;
779 ngx_conf_init_value(ccf->daemon, 1);
780 ngx_conf_init_value(ccf->master, 1);
781 ngx_conf_init_msec_value(ccf->timer_resolution, 0);
783 ngx_conf_init_value(ccf->worker_processes, 1);
784 ngx_conf_init_value(ccf->debug_points, 0);
786 #if (NGX_HAVE_SCHED_SETAFFINITY)
788 if (ccf->cpu_affinity_n
789 && ccf->cpu_affinity_n != 1
790 && ccf->cpu_affinity_n != (ngx_uint_t) ccf->worker_processes)
792 ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
793 "number of the \"worker_processes\" is not equal to "
794 "the number of the \"worker_cpu_affinity\" mask, "
795 "using last mask for remaining worker processes");
802 ngx_conf_init_value(ccf->worker_threads, 0);
803 ngx_threads_n = ccf->worker_threads;
804 ngx_conf_init_size_value(ccf->thread_stack_size, 2 * 1024 * 1024);
810 if (ccf->user == (uid_t) NGX_CONF_UNSET_UINT && geteuid() == 0) {
815 pwd = getpwnam(NGX_USER);
817 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
818 "getpwnam(\"" NGX_USER "\") failed");
819 return NGX_CONF_ERROR;
822 ccf->username = NGX_USER;
823 ccf->user = pwd->pw_uid;
826 grp = getgrnam(NGX_GROUP);
828 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
829 "getgrnam(\"" NGX_GROUP "\") failed");
830 return NGX_CONF_ERROR;
833 ccf->group = grp->gr_gid;
836 if (ccf->pid.len == 0) {
837 ccf->pid.len = sizeof(NGX_PID_PATH) - 1;
838 ccf->pid.data = (u_char *) NGX_PID_PATH;
841 if (ngx_conf_full_name(cycle, &ccf->pid, 0) == NGX_ERROR) {
842 return NGX_CONF_ERROR;
845 ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT);
847 ccf->oldpid.data = ngx_pnalloc(cycle->pool, ccf->oldpid.len);
848 if (ccf->oldpid.data == NULL) {
849 return NGX_CONF_ERROR;
852 ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len),
853 NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
856 if (ccf->lock_file.len == 0) {
857 ccf->lock_file.len = sizeof(NGX_LOCK_PATH) - 1;
858 ccf->lock_file.data = (u_char *) NGX_LOCK_PATH;
861 if (ngx_conf_full_name(cycle, &ccf->lock_file, 0) == NGX_ERROR) {
862 return NGX_CONF_ERROR;
868 lock_file = cycle->old_cycle->lock_file;
873 if (ccf->lock_file.len != lock_file.len
874 || ngx_strncmp(ccf->lock_file.data, lock_file.data, lock_file.len)
877 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
878 "\"lock_file\" could not be changed, ignored");
881 cycle->lock_file.len = lock_file.len + 1;
882 lock_file.len += sizeof(".accept");
884 cycle->lock_file.data = ngx_pstrdup(cycle->pool, &lock_file);
885 if (cycle->lock_file.data == NULL) {
886 return NGX_CONF_ERROR;
890 cycle->lock_file.len = ccf->lock_file.len + 1;
891 cycle->lock_file.data = ngx_pnalloc(cycle->pool,
892 ccf->lock_file.len + sizeof(".accept"));
893 if (cycle->lock_file.data == NULL) {
894 return NGX_CONF_ERROR;
897 ngx_memcpy(ngx_cpymem(cycle->lock_file.data, ccf->lock_file.data,
899 ".accept", sizeof(".accept"));
910 ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
914 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
915 "\"user\" is not supported, ignored");
921 ngx_core_conf_t *ccf = conf;
928 if (ccf->user != (uid_t) NGX_CONF_UNSET_UINT) {
929 return "is duplicate";
932 if (geteuid() != 0) {
933 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
934 "the \"user\" directive makes sense only "
935 "if the master process runs "
936 "with super-user privileges, ignored");
940 value = (ngx_str_t *) cf->args->elts;
942 ccf->username = (char *) value[1].data;
945 pwd = getpwnam((const char *) value[1].data);
947 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
948 "getpwnam(\"%s\") failed", value[1].data);
949 return NGX_CONF_ERROR;
952 ccf->user = pwd->pw_uid;
954 group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data);
957 grp = getgrnam(group);
959 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
960 "getgrnam(\"%s\") failed", group);
961 return NGX_CONF_ERROR;
964 ccf->group = grp->gr_gid;
973 ngx_set_env(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
975 ngx_core_conf_t *ccf = conf;
977 ngx_str_t *value, *var;
980 var = ngx_array_push(&ccf->env);
982 return NGX_CONF_ERROR;
985 value = cf->args->elts;
988 for (i = 0; i < value[1].len; i++) {
990 if (value[1].data[i] == '=') {
1003 ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1005 ngx_core_conf_t *ccf = conf;
1008 ngx_uint_t n, minus;
1010 if (ccf->priority != 0) {
1011 return "is duplicate";
1014 value = cf->args->elts;
1016 if (value[1].data[0] == '-') {
1020 } else if (value[1].data[0] == '+') {
1029 ccf->priority = ngx_atoi(&value[1].data[n], value[1].len - n);
1030 if (ccf->priority == NGX_ERROR) {
1031 return "invalid number";
1035 ccf->priority = -ccf->priority;
1043 ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
1045 #if (NGX_HAVE_SCHED_SETAFFINITY)
1046 ngx_core_conf_t *ccf = conf;
1053 if (ccf->cpu_affinity) {
1054 return "is duplicate";
1057 mask = ngx_palloc(cf->pool, (cf->args->nelts - 1) * sizeof(long));
1059 return NGX_CONF_ERROR;
1062 ccf->cpu_affinity_n = cf->args->nelts - 1;
1063 ccf->cpu_affinity = mask;
1065 value = cf->args->elts;
1067 for (n = 1; n < cf->args->nelts; n++) {
1069 if (value[n].len > 32) {
1070 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1071 "\"worker_cpu_affinity\" supports up to 32 CPU only");
1072 return NGX_CONF_ERROR;
1077 for (i = 0; i < value[n].len; i++) {
1079 ch = value[n].data[i];
1096 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1097 "invalid character \"%c\" in \"worker_cpu_affinity\"",
1099 return NGX_CONF_ERROR;
1105 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1106 "\"worker_cpu_affinity\" is not supported "
1107 "on this platform, ignored");
1115 ngx_get_cpu_affinity(ngx_uint_t n)
1117 ngx_core_conf_t *ccf;
1119 ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
1122 if (ccf->cpu_affinity == NULL) {
1126 if (ccf->cpu_affinity_n > n) {
1127 return ccf->cpu_affinity[n];
1130 return ccf->cpu_affinity[ccf->cpu_affinity_n - 1];