📄 leader.c
字号:
hold_off_on_exponential_spawning = 10; } ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf, "%s configured -- resuming normal operations", ap_get_server_version()); ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf, "Server built: %s", ap_get_server_built());#ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf, "AcceptMutex: %s (default: %s)", apr_proc_mutex_name(accept_mutex), apr_proc_mutex_defname());#endif restart_pending = shutdown_pending = 0; mpm_state = AP_MPMQ_RUNNING; server_main_loop(remaining_children_to_start); mpm_state = AP_MPMQ_STOPPING; if (shutdown_pending) { /* Time to gracefully shut down: * Kill child processes, tell them to call child_exit, etc... * (By "gracefully" we don't mean graceful in the same sense as * "apachectl graceful" where we allow old connections to finish.) */ if (unixd_killpg(getpgrp(), SIGTERM) < 0) { ap_log_error(APLOG_MARK, APLOG_WARNING, errno, ap_server_conf, "killpg SIGTERM"); } ap_reclaim_child_processes(1); /* Start with SIGTERM */ if (!child_fatal) { /* cleanup pid file on normal shutdown */ const char *pidfile = NULL; pidfile = ap_server_root_relative (pconf, ap_pid_fname); if ( pidfile != NULL && unlink(pidfile) == 0) ap_log_error(APLOG_MARK, APLOG_INFO, 0, ap_server_conf, "removed PID file %s (pid=%ld)", pidfile, (long)getpid()); ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf, "caught SIGTERM, shutting down"); } return 1; } /* we've been told to restart */ apr_signal(SIGHUP, SIG_IGN); if (one_process) { /* not worth thinking about */ return 1; } /* advance to the next generation */ /* XXX: we really need to make sure this new generation number isn't in * use by any of the children. */ ++ap_my_generation; ap_scoreboard_image->global->running_generation = ap_my_generation; if (is_graceful) { ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf, AP_SIG_GRACEFUL_STRING " received. Doing graceful restart"); /* wake up the children...time to die. But we'll have more soon */ ap_mpm_pod_killpg(pod, ap_daemons_limit); /* This is mostly for debugging... so that we know what is still * gracefully dealing with existing request. */ } else { /* Kill 'em all. Since the child acts the same on the parents SIGTERM * and a SIGHUP, we may as well use the same signal, because some user * pthreads are stealing signals from us left and right. */ ap_mpm_pod_killpg(pod, ap_daemons_limit); ap_reclaim_child_processes(1); /* Start with SIGTERM */ ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf, "SIGHUP received. Attempting to restart"); } return 0;}/* This really should be a post_config hook, but the error log is already * redirected by that point, so we need to do this in the open_logs phase. */static int leader_open_logs(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s){ apr_status_t rv; pconf = p; ap_server_conf = s; if ((num_listensocks = ap_setup_listeners(ap_server_conf)) < 1) { ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_STARTUP, 0, NULL, "no listening sockets available, shutting down"); return DONE; } if (!one_process) { if ((rv = ap_mpm_pod_open(pconf, &pod))) { ap_log_error(APLOG_MARK, APLOG_CRIT|APLOG_STARTUP, rv, NULL, "Could not open pipe-of-death."); return DONE; } } return OK;}static int leader_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp){ static int restart_num = 0; int no_detach, debug, foreground; ap_directive_t *pdir; ap_directive_t *max_clients = NULL; apr_status_t rv; mpm_state = AP_MPMQ_STARTING; /* make sure that "ThreadsPerChild" gets set before "MaxClients" */ for (pdir = ap_conftree; pdir != NULL; pdir = pdir->next) { if (strncasecmp(pdir->directive, "ThreadsPerChild", 15) == 0) { if (!max_clients) { break; /* we're in the clear, got ThreadsPerChild first */ } else { /* now to swap the data */ ap_directive_t temp; temp.directive = pdir->directive; temp.args = pdir->args; /* Make sure you don't change 'next', or you may get loops! */ /* XXX: first_child, parent, and data can never be set * for these directives, right? -aaron */ temp.filename = pdir->filename; temp.line_num = pdir->line_num; pdir->directive = max_clients->directive; pdir->args = max_clients->args; pdir->filename = max_clients->filename; pdir->line_num = max_clients->line_num; max_clients->directive = temp.directive; max_clients->args = temp.args; max_clients->filename = temp.filename; max_clients->line_num = temp.line_num; break; } } else if (!max_clients && strncasecmp(pdir->directive, "MaxClients", 10) == 0) { max_clients = pdir; } } debug = ap_exists_config_define("DEBUG"); if (debug) { foreground = one_process = 1; no_detach = 0; } else { one_process = ap_exists_config_define("ONE_PROCESS"); no_detach = ap_exists_config_define("NO_DETACH"); foreground = ap_exists_config_define("FOREGROUND"); } /* sigh, want this only the second time around */ if (restart_num++ == 1) { is_graceful = 0; if (!one_process && !foreground) { rv = apr_proc_detach(no_detach ? APR_PROC_DETACH_FOREGROUND : APR_PROC_DETACH_DAEMONIZE); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL, "apr_proc_detach failed"); return HTTP_INTERNAL_SERVER_ERROR; } } parent_pid = ap_my_pid = getpid(); } unixd_pre_config(ptemp); ap_listen_pre_config(); ap_daemons_to_start = DEFAULT_START_DAEMON; min_spare_threads = DEFAULT_MIN_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD; max_spare_threads = DEFAULT_MAX_FREE_DAEMON * DEFAULT_THREADS_PER_CHILD; ap_daemons_limit = server_limit; ap_threads_per_child = DEFAULT_THREADS_PER_CHILD; ap_pid_fname = DEFAULT_PIDLOG; ap_lock_fname = DEFAULT_LOCKFILE; ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD; ap_extended_status = 0;#ifdef AP_MPM_WANT_SET_MAX_MEM_FREE ap_max_mem_free = APR_ALLOCATOR_MAX_FREE_UNLIMITED;#endif apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir)); return OK;}static void leader_hooks(apr_pool_t *p){ /* The leader open_logs phase must run before the core's, or stderr * will be redirected to a file, and the messages won't print to the * console. */ static const char *const aszSucc[] = {"core.c", NULL}; one_process = 0; ap_hook_open_logs(leader_open_logs, NULL, aszSucc, APR_HOOK_MIDDLE); /* we need to set the MPM state before other pre-config hooks use MPM query * to retrieve it, so register as REALLY_FIRST */ ap_hook_pre_config(leader_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);}static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy, const char *arg) { const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } ap_daemons_to_start = atoi(arg); return NULL;}static const char *set_min_spare_threads(cmd_parms *cmd, void *dummy, const char *arg){ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } min_spare_threads = atoi(arg); if (min_spare_threads <= 0) { ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "WARNING: detected MinSpareThreads set to non-positive."); ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "Resetting to 1 to avoid almost certain Apache failure."); ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "Please read the documentation."); min_spare_threads = 1; } return NULL;}static const char *set_max_spare_threads(cmd_parms *cmd, void *dummy, const char *arg){ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } max_spare_threads = atoi(arg); return NULL;}static const char *set_max_clients (cmd_parms *cmd, void *dummy, const char *arg) { int max_clients; const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } /* It is ok to use ap_threads_per_child here because we are * sure that it gets set before MaxClients in the pre_config stage. */ max_clients = atoi(arg); if (max_clients < ap_threads_per_child) { ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "WARNING: MaxClients (%d) must be at least as large", max_clients); ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, " large as ThreadsPerChild (%d). Automatically", ap_threads_per_child); ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, " increasing MaxClients to %d.", ap_threads_per_child); max_clients = ap_threads_per_child; } ap_daemons_limit = max_clients / ap_threads_per_child; if ((max_clients > 0) && (max_clients % ap_threads_per_child)) { ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "WARNING: MaxClients (%d) is not an integer multiple", max_clients); ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, " of ThreadsPerChild (%d), lowering MaxClients to %d", ap_threads_per_child, ap_daemons_limit * ap_threads_per_child); ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, " for a maximum of %d child processes,", ap_daemons_limit); max_clients = ap_daemons_limit * ap_threads_per_child; } if (ap_daemons_limit > server_limit) { ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "WARNING: MaxClients of %d would require %d servers,", max_clients, ap_daemons_limit); ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, " and would exceed the ServerLimit value of %d.", server_limit); ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, " Automatically lowering MaxClients to %d. To increase,", server_limit * ap_threads_per_child); ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, " please see the ServerLimit directive."); ap_daemons_limit = server_limit; } else if (ap_daemons_limit < 1) { ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "WARNING: Require MaxClients > 0, setting to 1"); ap_daemons_limit = 1; } return NULL;}static const char *set_threads_per_child (cmd_parms *cmd, void *dummy, const char *arg) { const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } ap_threads_per_child = atoi(arg); if (ap_threads_per_child > thread_limit) { ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "WARNING: ThreadsPerChild of %d exceeds ThreadLimit " "value of %d", ap_threads_per_child, thread_limit); ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "threads, lowering ThreadsPerChild to %d. To increase, please" " see the", thread_limit); ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, " ThreadLimit directive."); ap_threads_per_child = thread_limit; } else if (ap_threads_per_child < 1) { ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, NULL, "WARNING: Require ThreadsPerChild > 0, setting to 1"); ap_threads_per_child = 1; } return NULL;}static const char *set_server_limit (cmd_parms *cmd, void *dummy, const char *arg) { int tmp_server_limit; const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } tmp_server_limit = atoi(arg); /* you cannot change ServerLimit across a restart; ignore * any such attempts */ if (first_server_limi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -