| |
| /* |
| * Copyright (C) Igor Sysoev |
| */ |
| |
| |
| #include <ngx_config.h> |
| #include <ngx_core.h> |
| #include <ngx_event.h> |
| #include <nginx.h> |
| |
| |
| static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle); |
| static ngx_int_t ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv); |
| static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv); |
| static void *ngx_core_module_create_conf(ngx_cycle_t *cycle); |
| static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf); |
| static char *ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
| static char *ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
| static char *ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, |
| void *conf); |
| |
| |
| static ngx_conf_enum_t ngx_debug_points[] = { |
| { ngx_string("stop"), NGX_DEBUG_POINTS_STOP }, |
| { ngx_string("abort"), NGX_DEBUG_POINTS_ABORT }, |
| { ngx_null_string, 0 } |
| }; |
| |
| |
| static ngx_command_t ngx_core_commands[] = { |
| |
| { ngx_string("daemon"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG, |
| ngx_conf_set_flag_slot, |
| 0, |
| offsetof(ngx_core_conf_t, daemon), |
| NULL }, |
| |
| { ngx_string("master_process"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG, |
| ngx_conf_set_flag_slot, |
| 0, |
| offsetof(ngx_core_conf_t, master), |
| NULL }, |
| |
| { ngx_string("timer_resolution"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| ngx_conf_set_msec_slot, |
| 0, |
| offsetof(ngx_core_conf_t, timer_resolution), |
| NULL }, |
| |
| { ngx_string("pid"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| ngx_conf_set_str_slot, |
| 0, |
| offsetof(ngx_core_conf_t, pid), |
| NULL }, |
| |
| { ngx_string("worker_processes"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| ngx_conf_set_num_slot, |
| 0, |
| offsetof(ngx_core_conf_t, worker_processes), |
| NULL }, |
| |
| { ngx_string("debug_points"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| ngx_conf_set_enum_slot, |
| 0, |
| offsetof(ngx_core_conf_t, debug_points), |
| &ngx_debug_points }, |
| |
| { ngx_string("user"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12, |
| ngx_set_user, |
| 0, |
| 0, |
| NULL }, |
| |
| { ngx_string("worker_priority"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| ngx_set_priority, |
| 0, |
| 0, |
| NULL }, |
| |
| { ngx_string("worker_cpu_affinity"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_1MORE, |
| ngx_set_cpu_affinity, |
| 0, |
| 0, |
| NULL }, |
| |
| { ngx_string("worker_rlimit_nofile"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| ngx_conf_set_num_slot, |
| 0, |
| offsetof(ngx_core_conf_t, rlimit_nofile), |
| NULL }, |
| |
| { ngx_string("worker_rlimit_sigpending"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| ngx_conf_set_num_slot, |
| 0, |
| offsetof(ngx_core_conf_t, rlimit_sigpending), |
| NULL }, |
| |
| { ngx_string("working_directory"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| ngx_conf_set_str_slot, |
| 0, |
| offsetof(ngx_core_conf_t, working_directory), |
| NULL }, |
| |
| #if (NGX_THREADS) |
| |
| { ngx_string("worker_threads"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| ngx_conf_set_num_slot, |
| 0, |
| offsetof(ngx_core_conf_t, worker_threads), |
| NULL }, |
| |
| { ngx_string("thread_stack_size"), |
| NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| ngx_conf_set_size_slot, |
| 0, |
| offsetof(ngx_core_conf_t, thread_stack_size), |
| NULL }, |
| |
| #endif |
| |
| ngx_null_command |
| }; |
| |
| |
| static ngx_core_module_t ngx_core_module_ctx = { |
| ngx_string("core"), |
| ngx_core_module_create_conf, |
| ngx_core_module_init_conf |
| }; |
| |
| |
| ngx_module_t ngx_core_module = { |
| NGX_MODULE_V1, |
| &ngx_core_module_ctx, /* module context */ |
| ngx_core_commands, /* module directives */ |
| NGX_CORE_MODULE, /* module type */ |
| NULL, /* init master */ |
| NULL, /* init module */ |
| NULL, /* init process */ |
| NULL, /* init thread */ |
| NULL, /* exit thread */ |
| NULL, /* exit process */ |
| NULL, /* exit master */ |
| NGX_MODULE_V1_PADDING |
| }; |
| |
| |
| ngx_uint_t ngx_max_module; |
| |
| |
| int ngx_cdecl |
| main(int argc, char *const *argv) |
| { |
| ngx_int_t i; |
| ngx_log_t *log; |
| ngx_cycle_t *cycle, init_cycle; |
| ngx_core_conf_t *ccf; |
| |
| #if (NGX_FREEBSD) |
| ngx_debug_init(); |
| #endif |
| |
| /* TODO */ ngx_max_sockets = -1; |
| |
| ngx_time_init(); |
| |
| #if (NGX_PCRE) |
| ngx_regex_init(); |
| #endif |
| |
| ngx_pid = ngx_getpid(); |
| |
| log = ngx_log_init(); |
| if (log == NULL) { |
| return 1; |
| } |
| |
| /* STUB */ |
| #if (NGX_OPENSSL) |
| ngx_ssl_init(log); |
| #endif |
| |
| /* init_cycle->log is required for signal handlers and ngx_getopt() */ |
| |
| ngx_memzero(&init_cycle, sizeof(ngx_cycle_t)); |
| init_cycle.log = log; |
| ngx_cycle = &init_cycle; |
| |
| init_cycle.pool = ngx_create_pool(1024, log); |
| if (init_cycle.pool == NULL) { |
| return 1; |
| } |
| |
| if (ngx_save_argv(&init_cycle, argc, argv) == NGX_ERROR) { |
| return 1; |
| } |
| |
| if (ngx_getopt(&init_cycle, argc, ngx_argv) == NGX_ERROR) { |
| return 1; |
| } |
| |
| if (ngx_test_config) { |
| log->log_level = NGX_LOG_INFO; |
| } |
| |
| if (ngx_os_init(log) == NGX_ERROR) { |
| return 1; |
| } |
| |
| if (ngx_add_inherited_sockets(&init_cycle) == NGX_ERROR) { |
| return 1; |
| } |
| |
| ngx_max_module = 0; |
| for (i = 0; ngx_modules[i]; i++) { |
| ngx_modules[i]->index = ngx_max_module++; |
| } |
| |
| cycle = ngx_init_cycle(&init_cycle); |
| if (cycle == NULL) { |
| if (ngx_test_config) { |
| ngx_log_error(NGX_LOG_EMERG, log, 0, |
| "the configuration file %s test failed", |
| init_cycle.conf_file.data); |
| } |
| |
| return 1; |
| } |
| |
| if (ngx_test_config) { |
| ngx_log_error(NGX_LOG_INFO, log, 0, |
| "the configuration file %s was tested successfully", |
| cycle->conf_file.data); |
| return 0; |
| } |
| |
| ngx_os_status(cycle->log); |
| |
| ngx_cycle = cycle; |
| |
| ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
| |
| ngx_process = ccf->master ? NGX_PROCESS_MASTER : NGX_PROCESS_SINGLE; |
| |
| #if (NGX_WIN32) |
| |
| #if 0 |
| |
| TODO: |
| |
| if (ccf->run_as_service) { |
| if (ngx_service(cycle->log) == NGX_ERROR) { |
| return 1; |
| } |
| |
| return 0; |
| } |
| #endif |
| |
| #else |
| |
| if (ngx_init_signals(cycle->log) == NGX_ERROR) { |
| return 1; |
| } |
| |
| if (!ngx_inherited && ccf->daemon) { |
| if (ngx_daemon(cycle->log) == NGX_ERROR) { |
| return 1; |
| } |
| |
| ngx_daemonized = 1; |
| } |
| |
| if (ngx_create_pidfile(cycle, NULL) == NGX_ERROR) { |
| return 1; |
| } |
| |
| #endif |
| |
| if (ngx_process == NGX_PROCESS_MASTER) { |
| ngx_master_process_cycle(cycle); |
| |
| } else { |
| ngx_single_process_cycle(cycle); |
| } |
| |
| return 0; |
| } |
| |
| |
| static ngx_int_t |
| ngx_add_inherited_sockets(ngx_cycle_t *cycle) |
| { |
| u_char *p, *v, *inherited; |
| ngx_int_t s; |
| ngx_listening_t *ls; |
| |
| inherited = (u_char *) getenv(NGINX_VAR); |
| |
| if (inherited == NULL) { |
| return NGX_OK; |
| } |
| |
| ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, |
| "using inherited sockets from \"%s\"", inherited); |
| |
| if (ngx_array_init(&cycle->listening, cycle->pool, 10, |
| sizeof(ngx_listening_t)) == NGX_ERROR) |
| { |
| return NGX_ERROR; |
| } |
| |
| for (p = inherited, v = p; *p; p++) { |
| if (*p == ':' || *p == ';') { |
| s = ngx_atoi(v, p - v); |
| if (s == NGX_ERROR) { |
| ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
| "invalid socket number \"%s\" in " NGINX_VAR |
| " environment variable, ignoring the rest" |
| " of the variable", v); |
| break; |
| } |
| |
| v = p + 1; |
| |
| ls = ngx_array_push(&cycle->listening); |
| if (ls == NULL) { |
| return NGX_ERROR; |
| } |
| |
| ngx_memzero(ls, sizeof(ngx_listening_t)); |
| |
| ls->fd = (ngx_socket_t) s; |
| } |
| } |
| |
| ngx_inherited = 1; |
| |
| return ngx_set_inherited_sockets(cycle); |
| } |
| |
| |
| ngx_pid_t |
| ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv) |
| { |
| char *env[3], *var; |
| u_char *p; |
| ngx_uint_t i; |
| ngx_pid_t pid; |
| ngx_exec_ctx_t ctx; |
| ngx_core_conf_t *ccf; |
| ngx_listening_t *ls; |
| |
| ctx.path = argv[0]; |
| ctx.name = "new binary process"; |
| ctx.argv = argv; |
| |
| var = ngx_alloc(sizeof(NGINX_VAR) |
| + cycle->listening.nelts * (NGX_INT32_LEN + 1) + 2, |
| cycle->log); |
| |
| p = ngx_cpymem(var, NGINX_VAR "=", sizeof(NGINX_VAR)); |
| |
| ls = cycle->listening.elts; |
| for (i = 0; i < cycle->listening.nelts; i++) { |
| p = ngx_sprintf(p, "%ud;", ls[i].fd); |
| } |
| |
| *p = '\0'; |
| |
| ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0, "inherited: %s", var); |
| |
| env[0] = var; |
| |
| #if (NGX_SETPROCTITLE_USES_ENV) |
| |
| /* allocate the spare 300 bytes for the new binary process title */ |
| |
| env[1] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; |
| |
| env[2] = NULL; |
| |
| #else |
| |
| env[1] = NULL; |
| |
| #endif |
| |
| ctx.envp = (char *const *) &env; |
| |
| ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); |
| |
| if (ngx_rename_file((char *) ccf->pid.data, (char *) ccf->oldpid.data) |
| != NGX_OK) |
| { |
| ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
| ngx_rename_file_n " %s to %s failed " |
| "before executing new binary process \"%s\"", |
| ccf->pid.data, ccf->oldpid.data, argv[0]); |
| |
| ngx_free(var); |
| |
| return NGX_INVALID_PID; |
| } |
| |
| pid = ngx_execute(cycle, &ctx); |
| |
| if (pid == NGX_INVALID_PID) { |
| if (ngx_rename_file((char *) ccf->oldpid.data, (char *) ccf->pid.data) |
| != NGX_OK) |
| { |
| ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, |
| ngx_rename_file_n " %s back to %s failed " |
| "after try to executing new binary process \"%s\"", |
| ccf->oldpid.data, ccf->pid.data, argv[0]); |
| } |
| } |
| |
| ngx_free(var); |
| |
| return pid; |
| } |
| |
| |
| static ngx_int_t |
| ngx_getopt(ngx_cycle_t *cycle, int argc, char *const *argv) |
| { |
| ngx_int_t i; |
| |
| for (i = 1; i < argc; i++) { |
| if (argv[i][0] != '-') { |
| ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
| "invalid option: \"%s\"", argv[i]); |
| return NGX_ERROR; |
| } |
| |
| switch (argv[i][1]) { |
| |
| case 't': |
| ngx_test_config = 1; |
| break; |
| |
| case 'c': |
| if (argv[i + 1] == NULL) { |
| ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
| "the option: \"%s\" requires file name", |
| argv[i]); |
| return NGX_ERROR; |
| } |
| |
| cycle->conf_file.data = (u_char *) argv[++i]; |
| cycle->conf_file.len = ngx_strlen(cycle->conf_file.data); |
| break; |
| |
| default: |
| ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, |
| "invalid option: \"%s\"", argv[i]); |
| return NGX_ERROR; |
| } |
| } |
| |
| if (cycle->conf_file.data == NULL) { |
| cycle->conf_file.len = sizeof(NGX_CONF_PATH) - 1; |
| cycle->conf_file.data = (u_char *) NGX_CONF_PATH; |
| } |
| |
| if (ngx_conf_full_name(cycle, &cycle->conf_file) == NGX_ERROR) { |
| return NGX_ERROR; |
| } |
| |
| return NGX_OK; |
| } |
| |
| |
| static ngx_int_t |
| ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv) |
| { |
| #if (NGX_FREEBSD) |
| |
| ngx_os_argv = (char **) argv; |
| ngx_argc = argc; |
| ngx_argv = (char **) argv; |
| |
| #else |
| size_t len; |
| ngx_int_t i; |
| |
| ngx_os_argv = (char **) argv; |
| ngx_argc = argc; |
| |
| ngx_argv = ngx_alloc((argc + 1) * sizeof(char *), cycle->log); |
| if (ngx_argv == NULL) { |
| return NGX_ERROR; |
| } |
| |
| for (i = 0; i < argc; i++) { |
| len = ngx_strlen(argv[i]) + 1; |
| |
| ngx_argv[i] = ngx_alloc(len, cycle->log); |
| if (ngx_argv[i] == NULL) { |
| return NGX_ERROR; |
| } |
| |
| (void) ngx_cpystrn((u_char *) ngx_argv[i], (u_char *) argv[i], len); |
| } |
| |
| ngx_argv[i] = NULL; |
| |
| #endif |
| |
| return NGX_OK; |
| } |
| |
| |
| static void * |
| ngx_core_module_create_conf(ngx_cycle_t *cycle) |
| { |
| ngx_core_conf_t *ccf; |
| |
| ccf = ngx_pcalloc(cycle->pool, sizeof(ngx_core_conf_t)); |
| if (ccf == NULL) { |
| return NULL; |
| } |
| |
| /* |
| * set by pcalloc() |
| * |
| * ccf->pid = NULL; |
| * ccf->oldpid = NULL; |
| * ccf->priority = 0; |
| * ccf->cpu_affinity_n = 0; |
| * ccf->cpu_affinity = NULL; |
| */ |
| |
| ccf->daemon = NGX_CONF_UNSET; |
| ccf->master = NGX_CONF_UNSET; |
| ccf->timer_resolution = NGX_CONF_UNSET_MSEC; |
| |
| ccf->worker_processes = NGX_CONF_UNSET; |
| ccf->debug_points = NGX_CONF_UNSET; |
| |
| ccf->rlimit_nofile = NGX_CONF_UNSET; |
| ccf->rlimit_sigpending = NGX_CONF_UNSET; |
| |
| ccf->user = (ngx_uid_t) NGX_CONF_UNSET_UINT; |
| ccf->group = (ngx_gid_t) NGX_CONF_UNSET_UINT; |
| |
| #if (NGX_THREADS) |
| ccf->worker_threads = NGX_CONF_UNSET; |
| ccf->thread_stack_size = NGX_CONF_UNSET_SIZE; |
| #endif |
| |
| return ccf; |
| } |
| |
| |
| static char * |
| ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf) |
| { |
| ngx_core_conf_t *ccf = conf; |
| |
| #if !(NGX_WIN32) |
| struct passwd *pwd; |
| struct group *grp; |
| #endif |
| |
| ngx_conf_init_value(ccf->daemon, 1); |
| ngx_conf_init_value(ccf->master, 1); |
| ngx_conf_init_msec_value(ccf->timer_resolution, 0); |
| |
| ngx_conf_init_value(ccf->worker_processes, 1); |
| ngx_conf_init_value(ccf->debug_points, 0); |
| |
| #if (NGX_HAVE_SCHED_SETAFFINITY) |
| |
| if (ccf->cpu_affinity_n |
| && ccf->cpu_affinity_n != 1 |
| && ccf->cpu_affinity_n != (ngx_uint_t) ccf->worker_processes) |
| { |
| ngx_log_error(NGX_LOG_WARN, cycle->log, 0, |
| "number of the \"worker_processes\" is not equal to " |
| "the number of the \"worker_cpu_affinity\" mask, " |
| "using last mask for remaining worker processes"); |
| } |
| |
| #endif |
| |
| #if (NGX_THREADS) |
| |
| ngx_conf_init_value(ccf->worker_threads, 0); |
| ngx_threads_n = ccf->worker_threads; |
| ngx_conf_init_size_value(ccf->thread_stack_size, 2 * 1024 * 1024); |
| |
| #endif |
| |
| #if !(NGX_WIN32) |
| |
| if (ccf->user == (uid_t) NGX_CONF_UNSET_UINT && geteuid() == 0) { |
| |
| pwd = getpwnam(NGX_USER); |
| if (pwd == NULL) { |
| ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| "getpwnam(\"" NGX_USER "\") failed"); |
| return NGX_CONF_ERROR; |
| } |
| |
| ccf->username = NGX_USER; |
| ccf->user = pwd->pw_uid; |
| |
| grp = getgrnam(NGX_GROUP); |
| if (grp == NULL) { |
| ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| "getgrnam(\"" NGX_GROUP "\") failed"); |
| return NGX_CONF_ERROR; |
| } |
| |
| ccf->group = grp->gr_gid; |
| } |
| |
| if (ccf->pid.len == 0) { |
| ccf->pid.len = sizeof(NGX_PID_PATH) - 1; |
| ccf->pid.data = (u_char *) NGX_PID_PATH; |
| } |
| |
| if (ngx_conf_full_name(cycle, &ccf->pid) == NGX_ERROR) { |
| return NGX_CONF_ERROR; |
| } |
| |
| ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT); |
| |
| ccf->oldpid.data = ngx_palloc(cycle->pool, ccf->oldpid.len); |
| if (ccf->oldpid.data == NULL) { |
| return NGX_CONF_ERROR; |
| } |
| |
| ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len), |
| NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT)); |
| |
| #endif |
| |
| return NGX_CONF_OK; |
| } |
| |
| |
| static char * |
| ngx_set_user(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
| { |
| #if (NGX_WIN32) |
| |
| ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
| "\"user\" is not supported, ignored"); |
| |
| return NGX_CONF_OK; |
| |
| #else |
| |
| ngx_core_conf_t *ccf = conf; |
| |
| char *group; |
| struct passwd *pwd; |
| struct group *grp; |
| ngx_str_t *value; |
| |
| if (ccf->user != (uid_t) NGX_CONF_UNSET_UINT) { |
| return "is duplicate"; |
| } |
| |
| if (geteuid() != 0) { |
| ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
| "the \"user\" directive makes sense only " |
| "if the master process runs " |
| "with super-user privileges, ignored"); |
| return NGX_CONF_OK; |
| } |
| |
| value = (ngx_str_t *) cf->args->elts; |
| |
| ccf->username = (char *) value[1].data; |
| |
| pwd = getpwnam((const char *) value[1].data); |
| if (pwd == NULL) { |
| ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno, |
| "getpwnam(\"%s\") failed", value[1].data); |
| return NGX_CONF_ERROR; |
| } |
| |
| ccf->user = pwd->pw_uid; |
| |
| group = (char *) ((cf->args->nelts == 2) ? value[1].data : value[2].data); |
| |
| grp = getgrnam(group); |
| if (grp == NULL) { |
| ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno, |
| "getgrnam(\"%s\") failed", group); |
| return NGX_CONF_ERROR; |
| } |
| |
| ccf->group = grp->gr_gid; |
| |
| return NGX_CONF_OK; |
| |
| #endif |
| } |
| |
| |
| static char * |
| ngx_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
| { |
| ngx_core_conf_t *ccf = conf; |
| |
| ngx_str_t *value; |
| ngx_uint_t n, minus; |
| |
| if (ccf->priority != 0) { |
| return "is duplicate"; |
| } |
| |
| value = cf->args->elts; |
| |
| if (value[1].data[0] == '-') { |
| n = 1; |
| minus = 1; |
| |
| } else if (value[1].data[0] == '+') { |
| n = 1; |
| minus = 0; |
| |
| } else { |
| n = 0; |
| minus = 0; |
| } |
| |
| ccf->priority = ngx_atoi(&value[1].data[n], value[1].len - n); |
| if (ccf->priority == NGX_ERROR) { |
| return "invalid number"; |
| } |
| |
| if (minus) { |
| ccf->priority = -ccf->priority; |
| } |
| |
| return NGX_CONF_OK; |
| } |
| |
| |
| static char * |
| ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
| { |
| #if (NGX_HAVE_SCHED_SETAFFINITY) |
| ngx_core_conf_t *ccf = conf; |
| |
| u_char ch; |
| u_long *mask; |
| ngx_str_t *value; |
| ngx_uint_t i, n; |
| |
| if (ccf->cpu_affinity) { |
| return "is duplicate"; |
| } |
| |
| mask = ngx_palloc(cf->pool, (cf->args->nelts - 1) * sizeof(long)); |
| if (mask == NULL) { |
| return NGX_CONF_ERROR; |
| } |
| |
| ccf->cpu_affinity_n = cf->args->nelts - 1; |
| ccf->cpu_affinity = mask; |
| |
| value = cf->args->elts; |
| |
| for (n = 1; n < cf->args->nelts; n++) { |
| |
| if (value[n].len > 32) { |
| ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| "\"worker_cpu_affinity\" supports up to 32 CPU only"); |
| return NGX_CONF_ERROR; |
| } |
| |
| mask[n - 1] = 0; |
| |
| for (i = 0; i < value[n].len; i++) { |
| |
| ch = value[n].data[i]; |
| |
| if (ch == ' ') { |
| continue; |
| } |
| |
| mask[n - 1] <<= 1; |
| |
| if (ch == '0') { |
| continue; |
| } |
| |
| if (ch == '1') { |
| mask[n - 1] |= 1; |
| continue; |
| } |
| |
| ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
| "invalid character \"%c\" in \"worker_cpu_affinity\"", |
| ch); |
| return NGX_CONF_ERROR ; |
| } |
| } |
| |
| #else |
| |
| ngx_conf_log_error(NGX_LOG_WARN, cf, 0, |
| "\"worker_cpu_affinity\" is not supported " |
| "on this platform, ignored"); |
| #endif |
| |
| return NGX_CONF_OK; |
| } |
| |
| |
| u_long |
| ngx_get_cpu_affinity(ngx_uint_t n) |
| { |
| ngx_core_conf_t *ccf; |
| |
| ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx, |
| ngx_core_module); |
| |
| if (ccf->cpu_affinity == NULL) { |
| return 0; |
| } |
| |
| if (ccf->cpu_affinity_n > n) { |
| return ccf->cpu_affinity[n]; |
| } |
| |
| return ccf->cpu_affinity[ccf->cpu_affinity_n - 1]; |
| } |