/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* 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 _WINUSER_
#include "apr.h"
#include "apr_strings.h"
#include "apr_lib.h"
#include "arch/win32/apr_arch_utf8.h"
#include "arch/win32/apr_arch_misc.h"
#include <wchar.h>
#endif
#include "httpd.h"
#include "http_log.h"
#include "mpm_winnt.h"
#include "ap_regkey.h"
#ifdef NOUSER
#endif
#include <winuser.h>
#include <time.h>
/* Todo; clear up statics */
#endif
typedef struct nt_service_ctx_t
{
/* Rather than repeat this logic throughout, create an either-or wide or narrow
* implementation because we don't actually pass strings to OpenSCManager.
* This election is based on build time defines and runtime os version test.
*/
const void *lpDatabase,
const void *lpDatabase,
{
if (!pfn_OpenSCManager) {
#endif
#if APR_HAS_ANSI_FS
#endif
}
}
/* 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 ap_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.
*/
void hold_console_open_on_error(void)
{
"and press the <ESC> key to exit. ";
if (!ap_real_exit_code)
return;
return;
|| !result)
return;
return;
return;
do
{
{
return;
return;
return;
}
(int)remains); /* 30 or less, so can't overflow int */
return;
|| !result)
return;
}
}
{
switch (ctrl_type)
{
case CTRL_BREAK_EVENT:
return TRUE;
case CTRL_C_EVENT:
/* for Interrupt signals, shut down the server.
* Tell the system we have dealt with the signal
* without waiting for Apache to terminate.
*/
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.
*/
Sleep(30000);
return TRUE;
}
/* We should never get here, but this is (mostly) harmless */
return FALSE;
}
static void stop_console_handler(void)
{
}
void mpm_start_console_handler(void)
{
}
void mpm_start_child_console_handler(void)
{
FreeConsole();
}
/**********************************
WinNT service control management
**********************************/
{
if (ctx->hServiceStatus)
{
if (currentState == SERVICE_RUNNING) {
}
else if (currentState == SERVICE_STOPPED) {
/* An unexpected exit? Better to error! */
}
else {
if(waitHint)
}
}
return(rv);
}
/* Note this works on Win2000 and later due to ChangeServiceConfig2
* Continue to test its existence, but at least drop the feature
* of revising service description tags prior to Win2000.
*/
/* borrowed from mpm_winnt.c */
extern apr_pool_t *pconf;
static void set_service_description(void)
{
const char *full_description;
/* Nothing to do if we are a console
*/
if (!mpm_service_name)
return;
/* Time to fix up the description, upon each successful restart
*/
if ((ChangeServiceConfig2) &&
{
{
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
{
}
#endif
if (schService) {
/* Cast is necessary, ChangeServiceConfig2 handles multiple
* object types, some volatile, some not.
*/
{
wslen * sizeof(apr_wchar_t));
&wslen);
/*SERVICE_CONFIG_DESCRIPTION*/,
(LPVOID) &full_description_w))
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
{
1 /* SERVICE_CONFIG_DESCRIPTION */,
(LPVOID) &full_description))
}
#endif
}
}
}
/* handle the SCM's ControlService() callbacks to our service */
{
/* SHUTDOWN is offered before STOP, accept the first opportunity */
if ((dwCtrlCode == SERVICE_CONTROL_STOP)
|| (dwCtrlCode == SERVICE_CONTROL_SHUTDOWN))
{
return (NO_ERROR);
}
if (dwCtrlCode == SERVICE_APACHE_RESTART)
{
return (NO_ERROR);
}
if (dwCtrlCode == SERVICE_CONTROL_INTERROGATE) {
return (NO_ERROR);
}
return (ERROR_CALL_NOT_IMPLEMENTED);
}
/* service_nt_main_fn is outside of the call stack and outside of the
* primary server thread... so now we _really_ need a placeholder!
* The winnt_rewrite_args has created and shared mpm_new_argv with us.
*/
extern apr_array_header_t *mpm_new_argv;
{
const char *ignored;
char *service_name;
/* args and service names live in the same pool */
if (!(ctx->hServiceStatus =
{
apr_get_os_error(), NULL,
return;
}
/* Report status, no errors, and buy 3 more seconds */
/* We need to append all the command arguments passed via StartService()
* to our running service... which just got here via the SCM...
* but we have no interest in argv[0] for the mpm_new_argv list.
*/
if (argc > 1)
{
DWORD i;
/* mpm_new_argv remains first (of lower significance) */
/* Service args follow from StartService() invocation */
for (i = 1; i < argc; ++i)
{
}
/* The replacement arg list is complete */
}
/* Let the main thread continue now... but hang on to the
* signal_monitor event so we can take further action
*/
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
{
const char *ignored;
/* args and service names live in the same pool */
if (!(ctx->hServiceStatus =
{
apr_get_os_error(), NULL,
return;
}
/* Report status, no errors, and buy 3 more seconds */
/* We need to append all the command arguments passed via StartService()
* to our running service... which just got here via the SCM...
* but we have no interest in argv[0] for the mpm_new_argv list.
*/
if (argc > 1)
{
char **cmb_data;
/* mpm_new_argv remains first (of lower significance) */
/* Service args follow from StartService() invocation */
/* The replacement arg list is complete */
}
/* Let the main thread continue now... but hang on to the
* signal_monitor event so we can take further action
*/
}
#endif
{
{
{ L"", service_nt_main_fn_w },
};
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
{
{ "", service_nt_main_fn },
};
#endif
#endif
#if APR_HAS_ANSI_FS
#endif
if (rv) {
rv = APR_SUCCESS;
}
else {
/* This is a genuine failure of the SCM. */
rv = apr_get_os_error();
"dispatcher");
}
return (rv);
}
/* The service configuration's is stored under the following trees:
*
* HKLM\System\CurrentControlSet\Services\[service name]
*
* \DisplayName
* \ImagePath
* \Parameters\ConfigArgs
*/
const char *set_name)
{
/* ### Needs improvement, on Win2K the user can _easily_
* change the display name to a string that doesn't reflect
* the internal service name + whitespace!
*/
{
if (rv != APR_SUCCESS)
return rv;
else if (slen)
return APR_ENAMETOOLONG;
}
#endif /* APR_HAS_UNICODE_FS */
if (rv == APR_SUCCESS) {
}
if (rv != APR_SUCCESS) {
/* Take the given literal name if there is no service entry */
}
return rv;
}
int fixed_args)
{
char **cmb_data;
if (rv == APR_SUCCESS) {
}
if (rv != APR_SUCCESS) {
if (rv == ERROR_FILE_NOT_FOUND) {
"No ConfigArgs registered for the '%s' service, "
"perhaps this service is not installed?",
return APR_SUCCESS;
}
else
return (rv);
}
return (APR_SUCCESS);
}
/* Now we have the mpm_service_name arg, and the mpm_runservice_nt()
* call appended the arguments passed by StartService(), so it's
* time to _prepend_ the default arguments for the server from
* the service's default arguments (all others override them)...
*/
/* First three args (argv[0], -f, path) remain first */
/* Service args follow from service registry array */
/* Remaining new args follow */
return APR_SUCCESS;
}
static void service_stopped(void)
{
/* Still have a thread & window to clean up, so signal now */
if (globdat.service_thread)
{
/* Stop logging to the event log */
/* Cause the service_nt_main_fn to complete */
}
}
{
/* Prevent holding open the (hidden) console */
ap_real_exit_code = 0;
/* GetCurrentThread returns a psuedo-handle, we need
* a real handle for another thread to wait upon.
*/
0, FALSE, DUPLICATE_SAME_ACCESS)) {
return APR_ENOTHREAD;
}
return APR_EGENERAL;
}
if (!globdat.service_thread) {
return APR_ENOTHREAD;
}
/* Wait for controlling thread init or termination */
return APR_ENOTHREAD;
}
return APR_SUCCESS;
}
{
return APR_SUCCESS;
}
void mpm_service_stopping(void)
{
}
{
char *launch_cmd;
#endif
: "Installing the '%s' service\n",
{
display_name_w, &wslen);
if (rv != APR_SUCCESS)
return rv;
else if (slen)
return APR_ENAMETOOLONG;
launch_cmd_w[0] = L'"';
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
{
launch_cmd[0] = '"';
}
#endif
if (rc == 0) {
rv = apr_get_os_error();
return rv;
}
if (!schSCManager) {
rv = apr_get_os_error();
"manager, perhaps you forgot to log in as Adminstrator?");
return (rv);
}
if (reconfig) {
{
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
{
}
#endif
if (!schService) {
rv = apr_get_os_error();
return (rv);
}
{
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
{
}
#endif
if (!rc) {
apr_get_os_error(), NULL,
/* !schService aborts configuration below */
schService = NULL;
}
}
else {
/* RPCSS is the Remote Procedure Call (RPC) Locator required
* for DCOM communication pipes. I am far from convinced we
* should add this to the default service dependencies, but
* be warned that future apache modules or ISAPI dll's may
* depend on it.
*/
{
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
{
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();
return (rv);
}
}
/* Store the service ConfigArgs in the registry...
*/
if (rv == APR_SUCCESS) {
}
if (rv != APR_SUCCESS) {
"'%s' service in the registry.", mpm_display_name);
return (rv);
}
return APR_SUCCESS;
}
{
if (!schSCManager) {
rv = apr_get_os_error();
"manager, perhaps you forgot to log in as Adminstrator?");
return (rv);
}
{
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
{
}
#endif
if (!schService) {
rv = apr_get_os_error();
return (rv);
}
/* assure the service is stopped before continuing
*
* This may be out of order... we might not be able to be
* granted all access if the service is running anyway.
*
* And do we want to make it *this easy* for them
* to uninstall their service unintentionally?
*/
/* ap_stop_service(schService);
*/
if (DeleteService(schService) == 0) {
rv = apr_get_os_error();
return (rv);
}
return APR_SUCCESS;
}
/* signal_service_transition is a simple thunk to signal the service
* and monitor its successful transition. If the signal passed is 0,
* then the caller is assumed to already have performed some service
* operation to be monitored (such as StartService), and no actual
* ControlService signal is sent.
*/
{
return FALSE;
do {
Sleep(1000);
return FALSE;
}
const char * const * argv)
{
if (!schSCManager) {
rv = apr_get_os_error();
"manager, perhaps you forgot to log in as Adminstrator?");
return (rv);
}
{
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
{
}
#endif
if (!schService) {
rv = apr_get_os_error();
return (rv);
}
return 0;
}
{
int i;
for (i = 0; i < argc; ++i)
{
if (rv != APR_SUCCESS)
return rv;
else if (slen)
return APR_ENAMETOOLONG;
}
rv = APR_SUCCESS;
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
{
rv = APR_SUCCESS;
}
#endif
if (rv != APR_SUCCESS)
rv = apr_get_os_error();
if (rv == APR_SUCCESS)
else
"Failed to start the '%s' service",
return rv;
}
/* signal is zero to stop, non-zero for restart */
{
if (!schSCManager) {
apr_get_os_error(), NULL,
"manager, perhaps you forgot to log in as Adminstrator?");
return;
}
{
}
#endif /* APR_HAS_UNICODE_FS */
#if APR_HAS_ANSI_FS
{
}
#endif
if (schService == NULL) {
/* Could not open the service */
apr_get_os_error(), NULL,
return;
}
apr_get_os_error(), NULL,
return;
}
return;
}
: "The '%s' service is stopping.\n",
if (!signal)
return;
}
else
if (success)
: "The '%s' service has stopped.\n",
else
: "Failed to stop the '%s' service.\n",
}