📄 spmt_os2.c
字号:
idle_spawn_rate = 1; }}AP_DECLARE(apr_status_t) ap_mpm_query(int query_code, int *result){ switch(query_code){ case AP_MPMQ_MAX_DAEMONS: *result = max_daemons_limit; return APR_SUCCESS; case AP_MPMQ_IS_THREADED: *result = 1; return APR_SUCCESS; case AP_MPMQ_IS_FORKED: *result = 0; return APR_SUCCESS; } return APR_ENOTIMPL;} /***************************************************************** * Executive routines. */int ap_mpm_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s){ int remaining_children_to_start; int i; apr_status_t status; pconf = _pconf; ap_server_conf = s; ap_log_pid(pconf, ap_pid_fname); if ((status = ap_listen_open(s->process, s->port)) != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ALERT, status, s, "no listening sockets available, shutting down"); return -1; } SAFE_ACCEPT(accept_mutex_init(pconf)); if (!is_graceful) { ap_create_scoreboard(pconf, SB_NOT_SHARED); memset(thread_control, 0, sizeof(thread_control)); } set_signals(); if (ppthread_globals == NULL) { if (DosAllocThreadLocalMemory(1, (PULONG *)&ppthread_globals)) { ap_log_error(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, 0, ap_server_conf, "Error allocating thread local storage" "Apache is exiting!"); } else { *ppthread_globals = (struct thread_globals *)apr_palloc(pconf, sizeof(struct thread_globals)); } } if (ap_daemons_max_free < ap_daemons_min_free + 1) /* Don't thrash... */ ap_daemons_max_free = ap_daemons_min_free + 1; /* If we're doing a graceful_restart then we're going to see a lot * of children exiting immediately when we get into the main loop * below (because we just sent them SIGUSR1). This happens pretty * rapidly... and for each one that exits we'll start a new one until * we reach at least daemons_min_free. But we may be permitted to * start more than that, so we'll just keep track of how many we're * supposed to start up without the 1 second penalty between each fork. */ remaining_children_to_start = ap_daemons_to_start; if (remaining_children_to_start > ap_daemons_limit) { remaining_children_to_start = ap_daemons_limit; } if (!is_graceful) { startup_children(remaining_children_to_start); remaining_children_to_start = 0; } else { /* give the system some time to recover before kicking into * exponential mode */ hold_off_on_exponential_spawning = 10; } ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf, "%s configured -- resuming normal operations", ap_get_server_version()); ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, ap_server_conf, "Server built: %s", ap_get_server_built()); restart_pending = shutdown_pending = 0; printf("%s \n", ap_get_server_version()); while (!restart_pending && !shutdown_pending) { int thread_slot; apr_wait_t status; int tid = wait_or_timeout(&status); /* XXX: if it takes longer than 1 second for all our children * to start up and get into IDLE state then we may spawn an * extra child */ if (tid >= 0) { apr_proc_t dummyproc; dummyproc.pid = tid; ap_process_child_status(&dummyproc, status); /* non-fatal death... note that it's gone in the scoreboard. */ thread_slot = find_thread_by_tid(tid); if (thread_slot >= 0) { (void) ap_update_child_status(0, thread_slot, SERVER_DEAD, (request_rec *) NULL); if (remaining_children_to_start && thread_slot < ap_daemons_limit) { /* we're still doing a 1-for-1 replacement of dead * children with new children */ make_child(ap_server_conf, thread_slot); --remaining_children_to_start; }#if APR_HAS_OTHER_CHILD/* TODO: this won't work, we waited on a thread not a process } else if (reap_other_child(pid, status) == 0) {*/#endif } else if (is_graceful) { /* Great, we've probably just lost a slot in the * scoreboard. Somehow we don't know about this * child. */ ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, ap_server_conf, "long lost child came home! (tid %d)", tid); } /* Don't perform idle maintenance when a child dies, * only do it when there's a timeout. Remember only a * finite number of children can die, and it's pretty * pathological for a lot to die suddenly. */ continue; } else if (remaining_children_to_start) { /* we hit a 1 second timeout in which none of the previous * generation of children needed to be reaped... so assume * they're all done, and pick up the slack if any is left. */ startup_children(remaining_children_to_start); remaining_children_to_start = 0; /* In any event we really shouldn't do the code below because * few of the servers we just started are in the IDLE state * yet, so we'd mistakenly create an extra server. */ continue; } perform_idle_server_maintenance(); } if (shutdown_pending) { /* Time to gracefully shut down */ const char *pidfile = NULL; int slot; TID tid; ULONG rc; ap_listen_rec *lr; for (lr = ap_listeners; lr; lr = lr->next) { apr_socket_close(lr->sd); DosSleep(0); } /* Kill off running threads */ for (slot=0; slot<max_daemons_limit; slot++) { if (ap_scoreboard_image->servers[0][slot].status != SERVER_DEAD) { tid = ap_scoreboard_image->servers[0][slot].tid; rc = DosKillThread(tid); if (rc != ERROR_INVALID_THREADID) { // Already dead, ignore if (rc == 0) { rc = DosWaitThread(&tid, DCWW_WAIT); if (rc) { ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, ap_server_conf, "error %lu waiting for thread to terminate", rc); } } else { ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, ap_server_conf, "error %lu killing thread", rc); } } } } /* cleanup pid file on normal shutdown */ pidfile = ap_server_root_relative (pconf, ap_pid_fname); if ( pidfile != NULL && unlink(pidfile) == 0) ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_INFO, 0, ap_server_conf, "removed PID file %s (pid=%ld)", pidfile, (long)getpid()); ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf, "caught SIGTERM, shutting down"); return 1; } /* we've been told to restart */ signal(SIGHUP, SIG_IGN); signal(SIGUSR1, 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_scoreboard_image->global.running_generation; if (is_graceful) { ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf, "SIGUSR1 received. Doing graceful restart"); /* kill off the idle ones */ for (i = 0; i < ap_daemons_limit; ++i) { thread_control[i].deferred_die = 1; } /* This is mostly for debugging... so that we know what is still * gracefully dealing with existing request. But we can't really * do it if we're in a SCOREBOARD_FILE because it'll cause * corruption too easily. */ for (i = 0; i < ap_daemons_limit; ++i) { if (ap_scoreboard_image->servers[0][i].status != SERVER_DEAD) { ap_scoreboard_image->servers[0][i].status = SERVER_GRACEFUL; } } } else { /* Kill 'em off */ for (i = 0; i < ap_daemons_limit; ++i) { DosKillThread(ap_scoreboard_image->servers[0][i].tid); } ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_NOTICE, 0, ap_server_conf, "SIGHUP received. Attempting to restart"); } return 0;}static void spmt_os2_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp){ one_process = !!ap_exists_config_define("ONE_PROCESS"); is_graceful = 0; ap_listen_pre_config(); ap_daemons_to_start = DEFAULT_START_DAEMON; ap_daemons_min_free = DEFAULT_MIN_FREE_DAEMON; ap_daemons_max_free = DEFAULT_MAX_FREE_DAEMON; ap_daemons_limit = HARD_THREAD_LIMIT; ap_pid_fname = DEFAULT_PIDLOG; ap_max_requests_per_child = DEFAULT_MAX_REQUESTS_PER_CHILD; ap_extended_status = 0; ap_scoreboard_fname = NULL; apr_cpystrn(ap_coredump_dir, ap_server_root, sizeof(ap_coredump_dir));}static void spmt_os2_hooks(apr_pool_t *p){ /* TODO: set one_process properly */ one_process = 0; ap_hook_pre_config(spmt_os2_pre_config, NULL, NULL, APR_HOOK_MIDDLE);}static const char *set_pidfile(cmd_parms *cmd, void *dummy, char *arg) { const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } if (cmd->server->is_virtual) { return "PidFile directive not allowed in <VirtualHost>"; } ap_pid_fname = arg; return NULL;}static const char *set_daemons_to_start(cmd_parms *cmd, void *dummy, 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_free_servers(cmd_parms *cmd, void *dummy, char *arg){ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } ap_daemons_min_free = atoi(arg); if (ap_daemons_min_free <= 0) { ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "WARNING: detected MinSpareServers set to non-positive."); ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Resetting to 1 to avoid almost certain Apache failure."); ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Please read the documentation."); ap_daemons_min_free = 1; } return NULL;}static const char *set_max_free_servers(cmd_parms *cmd, void *dummy, char *arg){ const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } ap_daemons_max_free = atoi(arg); return NULL;}static const char *set_server_limit (cmd_parms *cmd, void *dummy, char *arg) { const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } ap_daemons_limit = atoi(arg); if (ap_daemons_limit > HARD_THREAD_LIMIT) { ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "WARNING: MaxClients of %d exceeds compile time limit " "of %d servers,", ap_daemons_limit, HARD_THREAD_LIMIT); ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " lowering MaxClients to %d. To increase, please " "see the", HARD_THREAD_LIMIT); ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " HARD_THREAD_LIMIT define in %s.", AP_MPM_HARD_LIMITS_FILE); ap_daemons_limit = HARD_THREAD_LIMIT; } else if (ap_daemons_limit < 1) { ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "WARNING: Require MaxClients > 0, setting to 1"); ap_daemons_limit = 1; } return NULL;}static const char *set_max_requests(cmd_parms *cmd, void *dummy, char *arg) { const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } ap_max_requests_per_child = atoi(arg); return NULL;}static const char *set_coredumpdir (cmd_parms *cmd, void *dummy, char *arg) { apr_finfo_t finfo; const char *fname; const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY); if (err != NULL) { return err; } fname = ap_server_root_relative(cmd->pool, arg); if ((apr_stat(&finfo, fname, APR_FINFO_TYPE, cmd->pool) != APR_SUCCESS) || (finfo.filetype != APR_DIR)) { return apr_pstrcat(cmd->pool, "CoreDumpDirectory ", fname, " does not exist or is not a directory", NULL); } apr_cpystrn(ap_coredump_dir, fname, sizeof(ap_coredump_dir)); return NULL;}/* Stub functions until this MPM supports the connection status API */AP_DECLARE(void) ap_update_connection_status(long conn_id, const char *key, \ const char *value){ /* NOP */}AP_DECLARE(void) ap_reset_connection_status(long conn_id){ /* NOP */}static const command_rec spmt_os2_cmds[] = {LISTEN_COMMANDS{ "PidFile", set_pidfile, NULL, RSRC_CONF, TAKE1, "A file for logging the server process ID"},{ "StartServers", set_daemons_to_start, NULL, RSRC_CONF, TAKE1, "Number of child processes launched at server startup" },{ "MinSpareServers", set_min_free_servers, NULL, RSRC_CONF, TAKE1, "Minimum number of idle children, to handle request spikes" },{ "MaxSpareServers", set_max_free_servers, NULL, RSRC_CONF, TAKE1, "Maximum number of idle children" },{ "MaxClients", set_server_limit, NULL, RSRC_CONF, TAKE1, "Maximum number of children alive at the same time" },{ "MaxRequestsPerChild", set_max_requests, NULL, RSRC_CONF, TAKE1, "Maximum number of requests a particular child serves before dying." },{ "CoreDumpDirectory", set_coredumpdir, NULL, RSRC_CONF, TAKE1, "The location of the directory Apache changes to before dumping core" },{ NULL }};module AP_MODULE_DECLARE_DATA mpm_spmt_os2_module = { MPM20_MODULE_STUFF, NULL, /* hook to run before apache parses args */ NULL, /* create per-directory config structure */ NULL, /* merge per-directory config structures */ NULL, /* create per-server config structure */ NULL, /* merge per-server config structures */ spmt_os2_cmds, /* command apr_table_t */ spmt_os2_hooks, /* register_hooks */};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -