📄 service.c
字号:
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. *//* This module ALONE requires the window message API from user.h * and the default APR include of windows.h will omit it, so * preload the API symbols now... */#define CORE_PRIVATE #define _WINUSER_#include "httpd.h"#include "http_log.h"#include "mpm_winnt.h"#include "apr_strings.h"#include "apr_lib.h"#ifdef NOUSER#undef NOUSER#endif#undef _WINUSER_#include <winuser.h>static const char * service_name = NULL;/* ### should be namespace-protected */const char * display_name = NULL;static struct{ HANDLE mpm_thread; /* primary thread handle of the apache server */ HANDLE service_thread; /* thread service/monitor handle */ DWORD service_thread_id;/* thread service/monitor ID */ HANDLE signal_monitor; /* service monitor thread signal event */ SERVICE_STATUS ssStatus; SERVICE_STATUS_HANDLE hServiceStatus;} globdat;static int ReportStatusToSCMgr(int currentState, int exitCode, int waitHint);/* The service configuration's is stored under the following trees: * * HKLM\System\CurrentControlSet\Services\[service name] * * \DisplayName * \ImagePath (NT Only) * \Parameters\ConfigArgs * * For Win9x, the launch service command is stored under: * * HKLM\Software\Microsoft\Windows\CurrentVersion\RunServices\[service name] *//* exit() for Win32 is macro mapped (horrible, we agree) that allows us * to catch the non-zero conditions and inform the console process that * the application died, and hang on to the console a bit longer. * * The macro only maps for http_main.c and other sources that include * the service.h header, so we best assume it's an error to exit from * _any_ other module. * * If real_exit_code is reset to 0, it will not be set or trigger this * behavior on exit. All service and child processes are expected to * reset this flag to zero to avoid undesireable side effects. */int real_exit_code = 1;void hold_console_open_on_error(void){ HANDLE hConIn; HANDLE hConErr; DWORD result; time_t start; time_t remains; char *msg = "Note the errors or messages above, " "and press the <ESC> key to exit. "; CONSOLE_SCREEN_BUFFER_INFO coninfo; INPUT_RECORD in; char count[16]; if (!real_exit_code) return; hConIn = GetStdHandle(STD_INPUT_HANDLE); hConErr = GetStdHandle(STD_ERROR_HANDLE); if ((hConIn == INVALID_HANDLE_VALUE) || (hConErr == INVALID_HANDLE_VALUE)) return; if (!WriteConsole(hConErr, msg, strlen(msg), &result, NULL) || !result) return; if (!GetConsoleScreenBufferInfo(hConErr, &coninfo)) return; if (!SetConsoleMode(hConIn, ENABLE_MOUSE_INPUT | 0x80)) return; start = time(NULL); do { while (PeekConsoleInput(hConIn, &in, 1, &result) && result) { if (!ReadConsoleInput(hConIn, &in, 1, &result) || !result) return; if ((in.EventType == KEY_EVENT) && in.Event.KeyEvent.bKeyDown && (in.Event.KeyEvent.uChar.AsciiChar == 27)) return; if (in.EventType == MOUSE_EVENT && (in.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)) return; } remains = ((start + 30) - time(NULL)); sprintf (count, "%d...", remains); if (!SetConsoleCursorPosition(hConErr, coninfo.dwCursorPosition)) return; if (!WriteConsole(hConErr, count, strlen(count), &result, NULL) || !result) return; } while ((remains > 0) && WaitForSingleObject(hConIn, 1000) != WAIT_FAILED);}static BOOL die_on_logoff = FALSE;static LRESULT CALLBACK monitor_service_9x_proc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){/* This is the WndProc procedure for our invisible window. * When the user shuts down the system, this window is sent * a signal WM_ENDSESSION. We clean up by signaling Apache * to shut down, and idle until Apache's primary thread quits. */ if ((msg == WM_ENDSESSION) && (die_on_logoff || (lParam != ENDSESSION_LOGOFF))) { signal_parent(0); if (wParam) /* Don't leave this message until we are dead! */ WaitForSingleObject(globdat.mpm_thread, 30000); return 0; } return (DefWindowProc(hWnd, msg, wParam, lParam));}static DWORD WINAPI monitor_service_9x_thread(void *service_name){ /* When running as a service under Windows 9x, there is no console * window present, and no ConsoleCtrlHandler to call when the system * is shutdown. If the WatchWindow thread is created with a NULL * service_name argument, then the ...SystemMonitor window class is * used to create the "Apache" window to watch for logoff and shutdown. * If the service_name is provided, the ...ServiceMonitor window class * is used to create the window named by the service_name argument, * and the logoff message is ignored. */ WNDCLASS wc; HWND hwndMain; MSG msg; wc.style = CS_GLOBALCLASS; wc.lpfnWndProc = monitor_service_9x_proc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = NULL; wc.hIcon = NULL; wc.hCursor = NULL; wc.hbrBackground = NULL; wc.lpszMenuName = NULL; if (service_name) wc.lpszClassName = "ApacheWin95ServiceMonitor"; else wc.lpszClassName = "ApacheWin95SystemMonitor"; die_on_logoff = service_name ? FALSE : TRUE; if (!RegisterClass(&wc)) { ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(), NULL, "Could not register window class for WatchWindow"); SetEvent(globdat.signal_monitor); globdat.service_thread_id = 0; return 0; } /* Create an invisible window */ hwndMain = CreateWindow(wc.lpszClassName, service_name ? (char *) service_name : "Apache", WS_OVERLAPPEDWINDOW & ~WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL); if (!hwndMain) { ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, apr_get_os_error(), NULL, "Could not create WatchWindow"); SetEvent(globdat.signal_monitor); globdat.service_thread_id = 0; return 0; } /* If we succeed, eliminate the console window. * Signal the parent we are all set up, and * watch the message queue while the window lives. */ FreeConsole(); SetEvent((HANDLE) globdat.signal_monitor); while (GetMessage(&msg, NULL, 0, 0)) { if (msg.message == WM_CLOSE) DestroyWindow(hwndMain); else { TranslateMessage(&msg); DispatchMessage(&msg); } } globdat.service_thread_id = 0; return 0;}static BOOL CALLBACK console_control_handler(DWORD ctrl_type){ switch (ctrl_type) { case CTRL_BREAK_EVENT: fprintf(stderr, "Apache server restarting...\n"); signal_parent(1); return TRUE; case CTRL_C_EVENT: fprintf(stderr, "Apache server interrupted...\n"); /* for Interrupt signals, shut down the server. * Tell the system we have dealt with the signal * without waiting for Apache to terminate. */ signal_parent(0); return TRUE; case CTRL_CLOSE_EVENT: case CTRL_LOGOFF_EVENT: case CTRL_SHUTDOWN_EVENT: /* for Terminate signals, shut down the server. * Wait for Apache to terminate, but respond * after a reasonable time to tell the system * that we did attempt to shut ourself down. * THESE EVENTS WILL NOT OCCUR UNDER WIN9x! */ fprintf(stderr, "Apache server shutdown initiated...\n"); signal_parent(0); Sleep(30000); return TRUE; } /* We should never get here, but this is (mostly) harmless */ return FALSE;}static void stop_console_handler(void){ SetConsoleCtrlHandler(console_control_handler, FALSE);}void mpm_start_console_handler(void){ SetConsoleCtrlHandler(console_control_handler, TRUE); atexit(stop_console_handler);}/* Special situation - children of services need to mind their * P's & Q's and wait quietly, ignoring the mean OS signaling * shutdown and other horrors, to kill them gracefully... */static BOOL CALLBACK child_control_handler(DWORD ctrl_type){ switch (ctrl_type) { case CTRL_C_EVENT: case CTRL_BREAK_EVENT: /* for Interrupt signals, ignore them. * The system will also signal the parent process, * which will terminate Apache. */ return TRUE; case CTRL_CLOSE_EVENT: case CTRL_LOGOFF_EVENT: case CTRL_SHUTDOWN_EVENT: /* for Shutdown signals, ignore them, but... . * The system will also signal the parent process, * which will terminate Apache, so we need to wait. */ Sleep(30000); return TRUE; } /* We should never get here, but this is (mostly) harmless */ return FALSE;}static void stop_child_console_handler(void){ SetConsoleCtrlHandler(child_control_handler, FALSE);}void mpm_start_child_console_handler(void){ if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT) { FreeConsole(); } else { SetConsoleCtrlHandler(child_control_handler, TRUE); atexit(stop_child_console_handler); }}/********************************** WinNT service control management **********************************/static int ReportStatusToSCMgr(int currentState, int exitCode, int waitHint){ static int checkPoint = 1; int rv = APR_SUCCESS; if (globdat.hServiceStatus) { if (currentState == SERVICE_RUNNING) globdat.ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; else globdat.ssStatus.dwControlsAccepted = 0; globdat.ssStatus.dwCurrentState = currentState; globdat.ssStatus.dwWin32ExitCode = exitCode; if ( ( currentState == SERVICE_RUNNING ) || ( currentState == SERVICE_STOPPED ) ) { globdat.ssStatus.dwWaitHint = 0; globdat.ssStatus.dwCheckPoint = 0; } else { if(waitHint) globdat.ssStatus.dwWaitHint = waitHint; globdat.ssStatus.dwCheckPoint = ++checkPoint; } rv = SetServiceStatus(globdat.hServiceStatus, &globdat.ssStatus); } return(rv);}/* handle the SCM's ControlService() callbacks to our service */static VOID WINAPI service_nt_ctrl(DWORD dwCtrlCode){ if (dwCtrlCode == SERVICE_CONTROL_STOP) { ap_start_shutdown(); globdat.ssStatus.dwCurrentState = SERVICE_STOP_PENDING; ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 3000); return; } if (dwCtrlCode == SERVICE_APACHE_RESTART) { ap_start_restart(1); globdat.ssStatus.dwCurrentState = SERVICE_START_PENDING; ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 3000); return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -