From: Bill Stoddard Date: Mon, 20 Dec 1999 19:07:33 +0000 (+0000) Subject: Handle -k restart|shutdown command line option from http_main.c X-Git-Tag: 1.3.10~68 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ec1cc5b5ce2829c4040e5a23ad6e800cd079dcba;p=apache Handle -k restart|shutdown command line option from http_main.c git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@84341 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/include/ap_mpm.h b/include/ap_mpm.h index 7e021beeb2..35fc4b229b 100644 --- a/include/ap_mpm.h +++ b/include/ap_mpm.h @@ -128,6 +128,11 @@ API_EXPORT(int) ap_graceful_stop_signalled(void); void ap_start_shutdown(void); void ap_start_restart(int graceful); +/* + * ap_signal_parent() - used to send a signal to the parent process. + */ +void ap_signal_parent(ap_context_t *p, const char* signal, const char* server_root); + #ifdef HAS_OTHER_CHILD /* * register an other_child -- a child which the main loop keeps track of diff --git a/server/main.c b/server/main.c index 7e05e43c97..a685caa794 100644 --- a/server/main.c +++ b/server/main.c @@ -66,7 +66,8 @@ const char *ap_server_argv0; -API_VAR_EXPORT const char *ap_server_root; +//API_VAR_IMPORT const char *ap_server_root; +const char *ap_server_root; ap_array_header_t *ap_server_pre_read_config; ap_array_header_t *ap_server_post_read_config; @@ -236,6 +237,11 @@ static void usage(process_rec *process) ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Usage: %s [-D name] [-d directory] [-f file]", bin); #endif ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " %s [-C \"directive\"] [-c \"directive\"]", pad); +#ifdef WIN32 + ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " %s [-k restart|shutdown|start]", pad); + ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " %s [-n service_name]", pad); + ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " %s [-i] [-u]", pad); +#endif ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " %s [-v] [-V] [-h] [-l] [-L] [-S] [-t] [-T]", pad); ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Options:"); #ifdef SHARED_CORE @@ -246,6 +252,14 @@ static void usage(process_rec *process) ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -f file : specify an alternate ServerConfigFile"); ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -C \"directive\" : process directive before reading config files"); ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -c \"directive\" : process directive after reading config files"); +#ifdef WIN32 + ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -n name : set service name and use its ServerConfigFile"); + ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -k shutdown : tell running Apache to shutdown"); + ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -k restart : tell running Apache to do a graceful restart"); + ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -k start : tell Apache to start"); + ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -i : install an Apache service"); + ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -u : uninstall an Apache service"); +#endif ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -v : show version number"); ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -V : show compile settings"); ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, " -h : list available command line options (this page)"); @@ -283,6 +297,10 @@ API_EXPORT_NONSTD(int) main(int argc, char *argv[]) ap_context_t *ptemp; /* Pool for temporary config stuff, reset often */ ap_context_t *pcommands; /* Pool for -C and -c switches */ +#ifdef WIN32 + char *signal = NULL; +#endif + ap_server_argv0 = process->short_name; ap_util_uri_init(); @@ -296,7 +314,7 @@ API_EXPORT_NONSTD(int) main(int argc, char *argv[]) ap_server_post_read_config = ap_make_array(pcommands, 1, sizeof(char *)); ap_server_config_defines = ap_make_array(pcommands, 1, sizeof(char *)); - while (ap_getopt(pcommands, argc, argv, "C:c:d:f:vVlLth", &c) == APR_SUCCESS) { + while (ap_getopt(pcommands, argc, argv, "C:c:d:f:k:vVlLth", &c) == APR_SUCCESS) { char **new; switch (c) { case 'c': @@ -313,6 +331,11 @@ API_EXPORT_NONSTD(int) main(int argc, char *argv[]) case 'f': confname = ap_optarg; break; +#ifdef WIN32 + case 'k': + signal = ap_optarg; + break; +#endif case 'v': printf("Server version: %s\n", ap_get_server_version()); printf("Server built: %s\n", ap_get_server_built()); @@ -351,6 +374,12 @@ API_EXPORT_NONSTD(int) main(int argc, char *argv[]) ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Syntax OK\n"); destroy_and_exit_process(process, 0); } +#ifdef WIN32 + if (signal) { + ap_signal_parent(pconf, signal, ap_server_root); + destroy_and_exit_process(process, 0); + } +#endif ap_clear_pool(plog); ap_run_open_logs(pconf, plog, ptemp, server_conf); ap_post_config_hook(pconf, plog, ptemp, server_conf); diff --git a/server/mpm/winnt/mpm_winnt.c b/server/mpm/winnt/mpm_winnt.c index 483cbfc7e6..6cac05fc71 100644 --- a/server/mpm/winnt/mpm_winnt.c +++ b/server/mpm/winnt/mpm_winnt.c @@ -75,7 +75,7 @@ * Definitions of WINNT MPM specific config globals */ -char *ap_pid_fname=NULL; +static char *ap_pid_fname = NULL; static int ap_threads_per_child = 0; static int workers_may_exit = 0; static int max_requests_per_child = 0; @@ -349,6 +349,61 @@ void ap_start_shutdown(void) { signal_parent(0); } + +void ap_signal_parent(ap_context_t *p, const char* signal, + const char* server_root) +{ + HANDLE event; + char prefix[20]; + char *EventName; + FILE *fp; + int nread; + char *fname; + int end; + + printf("signal = %s\n", signal); + + fname = ap_server_root_relative (p, ap_pid_fname); + + fp = fopen(fname, "r"); + if (!fp) { + printf("Cannot read apache PID file %s. Error = %d\n", fname, errno); + return; + } + prefix[0] = 'a'; + prefix[1] = 'p'; + + nread = fread(prefix+2, 1, sizeof(prefix)-3, fp); + if (nread == 0) { + fclose(fp); + printf("PID file %s was empty\n", fname); + return; + } + fclose(fp); + + /* Terminate the prefix string */ + end = 2 + nread - 1; + while (end > 0 && (prefix[end] == '\r' || prefix[end] == '\n')) + end--; + prefix[end + 1] = '\0'; + + /* Build the event name. Should be one of the following... + * apPID_shutdown + * apPID_restart + */ + EventName = ap_pstrcat(p,prefix,"_",signal,NULL); + printf("event name = %s\n", EventName); + event = OpenEvent(EVENT_ALL_ACCESS, FALSE, EventName); + printf("event handle = %d\n", event); + if (event == NULL) { + printf("Unable to open event %s.\n", EventName); + return; + } + SetEvent(event); + ResetEvent(event); + CloseHandle(event); + return; +} /* * Initialise the signal names, in the global variables signal_name_prefix, * signal_restart_name and signal_shutdown_name. @@ -1321,7 +1376,7 @@ static int create_process(ap_context_t *p, HANDLE *handles, HANDLE *events, int int rv; char buf[1024]; char *pCommand; - + int i; STARTUPINFO si; /* Filled in prior to call to CreateProcess */ PROCESS_INFORMATION pi; /* filled in on call to CreateProces */ @@ -1363,8 +1418,11 @@ static int create_process(ap_context_t *p, HANDLE *handles, HANDLE *events, int return -1; } - //pCommand = ap_psprintf(p, "\"%s\" -f \"%s\"", buf, ap_server_confname); - pCommand = ap_psprintf(p, "\"%s\" -f \"%s\"", buf, SERVER_CONFIG_FILE); + /* Build the command line */ + pCommand = ap_psprintf(p, "\"%s\"", buf); + for (i = 1; i < server_conf->process->argc; i++) { + pCommand = ap_pstrcat(p, pCommand, " \"", server_conf->process->argv[i], "\"", NULL); + } /* Create a pipe to send socket info to the child */ if (!CreatePipe(&hPipeRead, &hPipeWrite, &sa, 0)) { @@ -1497,6 +1555,11 @@ static int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_even /* Create child process * Should only be one in this version of Apache for WIN32 */ +#if 1 +// ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000); +#else +// service_set_status(SERVICE_START_PENDING); +#endif while (remaining_children_to_start--) { if (create_process(pconf, process_handles, process_kill_events, ¤t_live_processes) < 0) { @@ -1506,8 +1569,11 @@ static int master_main(server_rec *s, HANDLE shutdown_event, HANDLE restart_even goto die_now; } } - - /* service_set_status(SERVICE_RUNNING);*/ +#if 1 +// ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 3000); +#else +// service_set_status(SERVICE_RUNNING); +#endif restart_pending = shutdown_pending = 0; /* Wait for shutdown or restart events or for child death */ @@ -1640,7 +1706,7 @@ static void winnt_pre_config(ap_context_t *pconf, ap_context_t *plog, ap_context else { /* This is the parent */ parent_pid = my_pid = getpid(); - ap_log_pid(pconf, ap_pid_fname); + } ap_listen_pre_config(); @@ -1700,9 +1766,14 @@ API_EXPORT(int) ap_mpm_run(ap_context_t *_pconf, ap_context_t *plog, server_rec ap_clear_pool(plog); ap_open_logs(server_conf, plog); - setup_signal_names(ap_psprintf(pconf,"ap%d", parent_pid)); + setup_signal_names(ap_psprintf(pconf,"ap%d", parent_pid)); if (!restart) { - /* service_set_status(SERVICE_START_PENDING);*/ + ap_log_pid(pconf, ap_pid_fname); +#if 1 +// ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000); +#else +// service_set_status(SERVICE_START_PENDING); +#endif AMCSocketInitialize(); // setup_signal_names(ap_psprintf(pconf,"ap%d", parent_pid)); @@ -1756,7 +1827,11 @@ API_EXPORT(int) ap_mpm_run(ap_context_t *_pconf, ap_context_t *plog, server_rec CloseHandle(restart_event); CloseHandle(shutdown_event); AMCSocketCleanup(); - /* service_set_status(SERVICE_STOPPED); */ +#if 1 +// ReportStatusToSCMgr(SERVICE_STOPPED, NO_ERROR, 3000); +#else +// service_set_status(SERVICE_STOPPED); +#endif } } return !restart;