/* Licensed to the Apache Software Foundation (ASF) under one or more *contributorlicenseagreements.SeetheNOTICEfiledistributedwith *thisworkforadditionalinformationregardingcopyrightownership. *TheASFlicensesthisfiletoYouundertheApacheLicense,Version2.0 *(the"License");youmaynotusethisfileexceptincompliancewith *theLicense.YoumayobtainacopyoftheLicenseat * *http://www.apache.org/licenses/LICENSE-2.0 * *Unlessrequiredbyapplicablelaworagreedtoinwriting,software *distributedundertheLicenseisdistributedonan"ASIS"BASIS, *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. *SeetheLicenseforthespecificlanguagegoverningpermissionsand *limitationsundertheLicense.
*/
/* This module ALONE requires the window message API from user.h *andthedefaultAPRincludeofwindows.hwillomitit,so *preloadtheAPIsymbolsnow...
*/
/* exit() for Win32 is macro mapped (horrible, we agree) that allows us *tocatchthenon-zeroconditionsandinformtheconsoleprocessthat *theapplicationdied,andhangontotheconsoleabitlonger. * *Themacroonlymapsforhttp_main.candothersourcesthatinclude *theservice.hheader,sowebestassumeit'sanerrortoexitfrom *_any_othermodule. * *Ifap_real_exit_codeisresetto0,itwillnotbesetortriggerthis *behavioronexit.Allserviceandchildprocessesareexpectedto *resetthisflagtozerotoavoidundesirablesideeffects.
*/
AP_DECLARE_DATA int ap_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 (!ap_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, (DWORD)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...",
(int)remains); /* 30 or less, so can't overflow int */ if (!SetConsoleCursorPosition(hConErr, coninfo.dwCursorPosition)) return; if (!WriteConsole(hConErr, count, (DWORD)strlen(count), &result, NULL)
|| !result) return;
} while ((remains > 0) && WaitForSingleObject(hConIn, 1000) != WAIT_FAILED);
}
staticBOOL CALLBACK console_control_handler(DWORD ctrl_type)
{ switch (ctrl_type)
{ case CTRL_BREAK_EVENT:
fprintf(stderr, "Apache server restarting...\n");
ap_signal_parent(SIGNAL_PARENT_RESTART); returnTRUE; case CTRL_C_EVENT:
fprintf(stderr, "Apache server interrupted...\n"); /* for Interrupt signals, shut down the server. *Tellthesystemwehavedealtwiththesignal *withoutwaitingforApachetoterminate.
*/
ap_signal_parent(SIGNAL_PARENT_SHUTDOWN); returnTRUE;
case CTRL_CLOSE_EVENT: case CTRL_LOGOFF_EVENT: case CTRL_SHUTDOWN_EVENT: /* for Terminate signals, shut down the server. *WaitforApachetoterminate,butrespond *afterareasonabletimetotellthesystem *thatwedidattempttoshutourselfdown.
*/
fprintf(stderr, "Apache server shutdown initiated...\n");
ap_signal_parent(SIGNAL_PARENT_SHUTDOWN);
Sleep(30000); returnTRUE;
}
/* We should never get here, but this is (mostly) harmless */ returnFALSE;
}
/* Note this works on Win2000 and later due to ChangeServiceConfig2 *Continuetotestitsexistence,butatleastdropthefeature *ofrevisingservicedescriptiontagspriortoWin2000.
*/
/* borrowed from mpm_winnt.c */ extern apr_pool_t *pconf;
/* SHUTDOWN is offered before STOP, accept the first opportunity */ if ((dwCtrlCode == SERVICE_CONTROL_STOP)
|| (dwCtrlCode == SERVICE_CONTROL_SHUTDOWN))
{
ap_signal_parent(SIGNAL_PARENT_SHUTDOWN);
ReportStatusToSCMgr(SERVICE_STOP_PENDING, 30000, ctx); return (NO_ERROR);
} if (dwCtrlCode == SERVICE_APACHE_RESTART)
{
ap_signal_parent(SIGNAL_PARENT_RESTART);
ReportStatusToSCMgr(SERVICE_START_PENDING, 30000, ctx); return (NO_ERROR);
} if (dwCtrlCode == SERVICE_CONTROL_INTERROGATE) {
ReportStatusToSCMgr(globdat.ssStatus.dwCurrentState, 0, ctx); return (NO_ERROR);
}
return (ERROR_CALL_NOT_IMPLEMENTED);
}
/* service_nt_main_fn is outside of the call stack and outside of the *primaryserverthread...sonowwe_really_needaplaceholder! *Thewinnt_rewrite_argshascreatedandsharedmpm_new_argvwithus.
*/ extern apr_array_header_t *mpm_new_argv;
/* Report status, no errors, and buy 3 more seconds */
ReportStatusToSCMgr(SERVICE_START_PENDING, 30000, ctx);
/* We need to append all the command arguments passed via StartService() *toourrunningservice...whichjustgothereviatheSCM... *butwehavenointerestinargv[0]forthempm_new_argvlist.
*/ if (argc > 1)
{ char **cmb_data, **cmb;
DWORD i;
if (!(ctx->hServiceStatus =
RegisterServiceCtrlHandlerExA(argv[0], service_nt_ctrl, ctx)))
{
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP,
apr_get_os_error(), NULL,
APLOGNO(10008) "Failure registering service handler"); return;
}
/* Report status, no errors, and buy 3 more seconds */
ReportStatusToSCMgr(SERVICE_START_PENDING, 30000, ctx);
/* We need to append all the command arguments passed via StartService() *toourrunningservice...whichjustgothereviatheSCM... *butwehavenointerestinargv[0]forthempm_new_argvlist.
*/ if (argc > 1)
{ char **cmb_data;
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
rv = StartServiceCtrlDispatcherW(dispatchTable_w); #endif #if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
rv = StartServiceCtrlDispatcherA(dispatchTable); #endif if (rv) {
rv = APR_SUCCESS;
} else { /* This is a genuine failure of the SCM. */
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00366) "Error starting Windows service control " "dispatcher");
} return (rv);
}
/* The service configuration's is stored under the following trees: * *HKLM\System\CurrentControlSet\Services\[servicename] * *\DisplayName *\ImagePath *\Parameters\ConfigArgs
*/
apr_snprintf(key_name, sizeof(key_name), SERVICECONFIG, mpm_service_name);
rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, key_name,
APR_READ, pconf); if (rv == APR_SUCCESS) {
rv = ap_regkey_value_get(&mpm_display_name, key, "DisplayName", pconf);
ap_regkey_close(key);
} if (rv != APR_SUCCESS) { /* Take the given literal name if there is no service entry */
mpm_display_name = apr_pstrdup(p, set_name);
}
*display_name = mpm_display_name;
apr_snprintf(conf_key, sizeof(conf_key), SERVICEPARAMS, mpm_service_name);
rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, conf_key, APR_READ, p); if (rv == APR_SUCCESS) {
rv = ap_regkey_value_array_get(&svc_args, key, "ConfigArgs", p);
ap_regkey_close(key);
} if (rv != APR_SUCCESS) { if (rv == ERROR_FILE_NOT_FOUND) {
ap_log_error(APLOG_MARK, APLOG_INFO, 0, NULL, APLOGNO(00367) "No ConfigArgs registered for the '%s' service, " "perhaps this service is not installed?",
mpm_service_name); return APR_SUCCESS;
} else return (rv);
}
if (!svc_args || svc_args->nelts == 0) { return (APR_SUCCESS);
}
/* Now we have the mpm_service_name arg, and the mpm_runservice_nt() *callappendedtheargumentspassedbyStartService(),soit's *timeto_prepend_thedefaultargumentsfortheserverfrom *theservice'sdefaultarguments(allothersoverridethem)...
*/
args->nalloc = args->nelts + svc_args->nelts;
cmb_data = malloc(args->nalloc * sizeof(constchar *));
/* First three args (argv[0], -f, path) remain first */
memcpy(cmb_data, args->elts, args->elt_size * fixed_args);
/* Service args follow from service registry array */
memcpy(cmb_data + fixed_args, svc_args->elts,
svc_args->elt_size * svc_args->nelts);
staticvoid service_stopped(void)
{ /* Still have a thread & window to clean up, so signal now */ if (globdat.service_thread)
{ /* Stop logging to the event log */
mpm_nt_eventlog_stderr_flush();
/* Cause the service_nt_main_fn to complete */
ReleaseMutex(globdat.service_term);
schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
SC_MANAGER_CREATE_SERVICE); if (!schSCManager) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00369) "Failed to open the Windows service " "manager, perhaps you forgot to log in as Administrator?"); return (rv);
}
/* !schService aborts configuration below */
CloseServiceHandle(schService);
schService = NULL;
}
} else { /* RPCSS is the Remote Procedure Call (RPC) Locator required *forDCOMcommunicationpipes.Iamfarfromconvincedwe *shouldaddthistothedefaultservicedependencies,but *bewarnedthatfutureapachemodulesorISAPIdll'smay *dependonit.
*/ #if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
schService = CreateServiceW(schSCManager, // SCManager database
mpm_service_name_w, // name of service
display_name_w, // name to display
SERVICE_ALL_ACCESS, // access required
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
launch_cmd_w, // service's binary
NULL, // no load svc group
NULL, // no tag identifier
L"Tcpip\0Afd\0", // dependencies
NULL, // use SYSTEM account
NULL); // no password
} #endif/* APR_HAS_UNICODE_FS */ #if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
schService = CreateService(schSCManager, // SCManager database
mpm_service_name, // name of service
mpm_display_name, // name to display
SERVICE_ALL_ACCESS, // access required
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
launch_cmd, // service's binary
NULL, // no load svc group
NULL, // no tag identifier "Tcpip\0Afd\0", // dependencies
NULL, // use SYSTEM account
NULL); // no password
} #endif if (!schService)
{
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00370) "Failed to create the '%s' service",
mpm_display_name);
CloseServiceHandle(schSCManager); return (rv);
}
}
/* Store the service ConfigArgs in the registry...
*/
apr_snprintf(key_name, sizeof(key_name), SERVICEPARAMS, mpm_service_name);
rv = ap_regkey_open(&key, AP_REGKEY_LOCAL_MACHINE, key_name,
APR_READ | APR_WRITE | APR_CREATE, pconf); if (rv == APR_SUCCESS) {
rv = ap_regkey_value_array_set(key, "ConfigArgs", argc, argv, pconf);
ap_regkey_close(key);
} if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00371) "Failed to store ConfigArgs for the " "'%s' service in the registry.", mpm_display_name); return (rv);
}
fprintf(stderr, "The '%s' service is successfully installed.\n",
mpm_display_name); return APR_SUCCESS;
}
fprintf(stderr, "Removing the '%s' service\n", mpm_display_name);
schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
SC_MANAGER_CONNECT); if (!schSCManager) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(10009) "Failed to open the Windows service " "manager, perhaps you forgot to log in as Administrator?"); return (rv);
}
/* assure the service is stopped before continuing * *Thismaybeoutoforder...wemightnotbeabletobe *grantedallaccessiftheserviceisrunninganyway. * *Anddowewanttomakeit*thiseasy*forthem *touninstalltheirserviceunintentionally?
*/ /* ap_stop_service(schService);
*/
if (DeleteService(schService) == 0) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(00374) "Failed to delete the '%s' service",
mpm_display_name); return (rv);
}
fprintf(stderr, "The '%s' service has been removed successfully.\n",
mpm_display_name); return APR_SUCCESS;
}
/* signal_service_transition is a simple thunk to signal the service *andmonitoritssuccessfultransition.Ifthesignalpassedis0, *thenthecallerisassumedtoalreadyhaveperformedsomeservice *operationtobemonitored(suchasStartService),andnoactual *ControlServicesignalissent.
*/
fprintf(stderr, "Starting the '%s' service\n", mpm_display_name);
schSCManager = OpenSCManager(NULL, NULL, /* local, default database */
SC_MANAGER_CONNECT); if (!schSCManager) {
rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP, rv, NULL,
APLOGNO(10011) "Failed to open the Windows service " "manager, perhaps you forgot to log in as Administrator?"); return (rv);
}
if (rv == APR_SUCCESS)
fprintf(stderr, "The '%s' service is running.\n", mpm_display_name); else
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL, APLOGNO(00378) "Failed to start the '%s' service",
mpm_display_name);
return rv;
}
/* signal is zero to stop, non-zero for restart */
void mpm_signal_service(apr_pool_t *ptemp, int signal)
{ int success = FALSE;
SC_HANDLE schService;
SC_HANDLE schSCManager;
if (!schSCManager) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP,
apr_get_os_error(), NULL,
APLOGNO(10013) "Failed to open the Windows service " "manager, perhaps you forgot to log in as Administrator?"); return;
}
#if APR_HAS_UNICODE_FS
IF_WIN_OS_IS_UNICODE
{
schService = OpenServiceW(schSCManager, mpm_service_name_w,
SERVICE_INTERROGATE | SERVICE_QUERY_STATUS |
SERVICE_USER_DEFINED_CONTROL |
SERVICE_START | SERVICE_STOP);
} #endif/* APR_HAS_UNICODE_FS */ #if APR_HAS_ANSI_FS
ELSE_WIN_OS_IS_ANSI
{
schService = OpenService(schSCManager, mpm_service_name,
SERVICE_INTERROGATE | SERVICE_QUERY_STATUS |
SERVICE_USER_DEFINED_CONTROL |
SERVICE_START | SERVICE_STOP);
} #endif if (schService == NULL) { /* Could not open the service */
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP,
apr_get_os_error(), NULL,
APLOGNO(10014) "Failed to open the '%s' service",
mpm_display_name);
CloseServiceHandle(schSCManager); return;
}
if (!QueryServiceStatus(schService, &globdat.ssStatus)) {
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_STARTUP,
apr_get_os_error(), NULL,
APLOGNO(00381) "Query of the '%s' service failed",
mpm_display_name);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager); return;
}
if (!signal && (globdat.ssStatus.dwCurrentState == SERVICE_STOPPED)) {
fprintf(stderr, "The '%s' service is not started.\n", mpm_display_name);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager); return;
}
fprintf(stderr, signal ? "The '%s' service is restarting.\n"
: "The '%s' service is stopping.\n",
mpm_display_name);
if (success)
fprintf(stderr, signal ? "The '%s' service has restarted.\n"
: "The '%s' service has stopped.\n",
mpm_display_name); else
fprintf(stderr, signal ? "Failed to restart the '%s' service.\n"
: "Failed to stop the '%s' service.\n",
mpm_display_name);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.21 Sekunden
(vorverarbeitet am 2026-09-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.