stoddard 00/02/14 14:01:11
Modified: src/main http_main.c src/modules/mpm/winnt winnt.c src/os/win32 main_win32.c Log: Rework the code to handle apache -k shutdown|restart. The fundamental problem being solved here is determining the best way to discover the Apache parent process PID (stored in the location specified by the PidFile directive). This patch attempts to read the config file directly to determine the pidfile and avoids going through the motions of calling all the module initialization routines. This patch will not work if the pidfile directive is in a file pointed to by an include directive. Not sure is this is a common case or not on Windows. If it is, it is easy enough to add a bit more code to follow include directives. An interesting modification would be to detect the presence of a \ -C directive containing a pointer to the pidfile: E.g., apache -k restart -C "pidfile d:/mypidfile". Let's keep it simple for now and see what happens. Revision Changes Path 1.29 +0 -15 apache-2.0/src/main/http_main.c Index: http_main.c =================================================================== RCS file: /home/cvs/apache-2.0/src/main/http_main.c,v retrieving revision 1.28 retrieving revision 1.29 diff -u -r1.28 -r1.29 --- http_main.c 2000/02/03 19:38:08 1.28 +++ http_main.c 2000/02/14 22:01:04 1.29 @@ -298,10 +298,6 @@ 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(); @@ -334,11 +330,6 @@ 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()); @@ -377,12 +368,6 @@ 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); 1.39 +0 -55 apache-2.0/src/modules/mpm/winnt/winnt.c Index: winnt.c =================================================================== RCS file: /home/cvs/apache-2.0/src/modules/mpm/winnt/winnt.c,v retrieving revision 1.38 retrieving revision 1.39 diff -u -r1.38 -r1.39 --- winnt.c 2000/02/14 14:28:16 1.38 +++ winnt.c 2000/02/14 22:01:08 1.39 @@ -359,61 +359,6 @@ { 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. 1.7 +89 -6 apache-2.0/src/os/win32/main_win32.c Index: main_win32.c =================================================================== RCS file: /home/cvs/apache-2.0/src/os/win32/main_win32.c,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- main_win32.c 2000/02/09 22:16:24 1.6 +++ main_win32.c 2000/02/14 22:01:10 1.7 @@ -77,6 +77,84 @@ __declspec(dllexport) int apache_main(int argc, char *argv[]); #define DEFAULTSERVICENAME "Apache" +static const char* read_config_cmd(char *filename, char *cmd, ap_context_t *p) +{ + ap_status_t rc; + char l[MAX_STRING_LEN]; + const char *args, *cmd_name; + const char *s = NULL; + configfile_t *config_file = NULL; + + rc = ap_pcfg_openfile(&config_file, p, filename); + if (rc != APR_SUCCESS) { + printf("Apache: Unable to open %s.\n", filename); + return NULL; + } + while (!(ap_cfg_getline(l, MAX_STRING_LEN, config_file))) { + if ((l[0] == '#') || (!l[0])) + continue; + args = l; + cmd_name = ap_getword_conf(p, &args); + if (!stricmp(cmd_name,cmd)) { + s = args; + break; + } + } + ap_cfg_closefile(config_file); + if (s == NULL) { + printf("Apache: Did not find directive %s in %s.\n", cmd, filename); + } + return s; +} +static void signal_parent(const char* file, const char* signal, + ap_context_t *p) +{ + HANDLE event; + char prefix[20]; + char *EventName; + FILE *fp; + int nread; + int end; + fp = fopen(file, "r"); + if (!fp) { + printf("Cannot read apache PID file %s. Error = %d\n", file, 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", file); + 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; + +} int main(int argc, char *argv[]) { ap_context_t *pwincmd; @@ -220,13 +298,18 @@ exit(0); } - /* Let http_main handle -k restart|shutdown */ + /* Handle -k restart|shutdown */ if (signal && strcasecmp(signal, "start")) { - new = (char **) ap_push_array(cmdtbl); - *new = "-k"; - new = (char **) ap_push_array(cmdtbl); - *new = signal; - new_argc += 2; + char *s; + s = read_config_cmd(server_confname, "pidfile", pwincmd); + if (s == NULL) { + exit(0); + } + if (!ap_os_is_path_absolute(s)) { + s = ap_make_full_path(pwincmd, server_root, s); + } + signal_parent(s, signal, pwincmd); + exit(0); } /* Complete building the new argv list. Need to add: