service.c revision e68becff3c3ddc18723c9799b8cc2e6e9c3dbd66
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 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
* 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
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
#ifdef WIN32
#define CORE_PRIVATE
#include "main_win32.h"
#include "ap_config.h"
#include "httpd.h"
#include "http_conf_globals.h"
#include "http_log.h"
#include "service.h"
#include "registry.h"
#include "ap_mpm.h"
#include "..\..\modules\mpm\winnt\winnt.h"
typedef void (CALLBACK *ap_completion_t)();
API_VAR_EXPORT char *ap_server_argv0;
static struct
{
int (*main_fn)(int, char **);
int connected;
char *name;
int exit_status;
char *service_dir;
} globdat;
static int ap_start_service(SC_HANDLE);
static int ap_stop_service(SC_HANDLE);
{
/* 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 (wParam)
return 0;
}
}
{
/* When running on Windows 9x, the ConsoleCtrlHandler is _NOT_
* called when the system is shutdown. So create an invisible
* window to watch for the WM_ENDSESSION message, and watch for
* the WM_CLOSE message to shut the window down.
*/
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
if (RegisterClass(&wc))
{
/* Create an invisible window */
if (hwndMain)
{
/* 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();
{
else {
}
}
globdat.threadMonitor = 0;
return(0);
}
}
/* We failed or are soon to die...
* we won't need this much longer
*/
globdat.threadMonitor = 0;
return(0);
}
static void CALLBACK report_service9x_running()
{
}
{
/* Obtain a handle to the kernel library */
if (hkernel) {
/* Find the RegisterServiceProcess function */
if (RegisterServiceProcess) {
0, &threadId);
}
}
}
/* Run the service */
/* Still have a thread & window to clean up, so signal now */
if (globdat.threadMonitor)
{
}
/* When the service quits, remove it from the
system service table */
/* Free the kernel library */
if (hkernel)
return (globdat.exit_status);
}
{
{
{ "", service_main_fn },
};
{
/* This is a genuine failure of the SCM. */
"Error starting service control dispatcher");
return(globdat.exit_status);
}
else
{
return(globdat.exit_status);
}
}
static void CALLBACK report_servicent_started()
{
SERVICE_RUNNING, // service state
NO_ERROR, // exit code
0); // wait hint
}
{
int i, new_argc;
char *server_confname = SERVER_CONFIG_FILE;
exit(0);
}
*tmp = '\0';
{
"Failure registering service handler");
return;
}
SERVICE_START_PENDING, // service state
NO_ERROR, // exit code
3000); // wait hint
/* Fetch server_conf from the registry
* Rebuild argv and argc adding the -d server_root and -f server_conf then
* call apache_main
*/
for (i = 0; i < argc ; i++) {
}
/* Add server_confname to the argument list */
*new = "-f";
*new = server_confname;
*new = "-d";
*new = server_root;
return;
}
void service_set_status(int status)
{
}
//
// FUNCTION: service_ctrl
//
// PURPOSE: This function is called by the SCM whenever
// ControlService() is called on this service.
//
// PARAMETERS:
// dwCtrlCode - type of control requested
//
// RETURN VALUE:
// none
//
// COMMENTS:
//
{
int state;
switch(dwCtrlCode)
{
// Stop the service.
//
case SERVICE_CONTROL_STOP:
break;
// Update the service status.
//
break;
// invalid control code
//
default:
break;
}
}
{
static int firstTime = 1;
static int checkPoint = 1;
int rv;
if(firstTime)
{
firstTime = 0;
}
{
if (currentState == SERVICE_START_PENDING)
else
if(waitHint)
if ( ( currentState == SERVICE_RUNNING ) ||
( currentState == SERVICE_STOPPED ) )
{
}
else
}
return(1);
}
{
char service_name[256];
{
"GetModuleFileName failed");
return;
}
/* Remove spaces from display name to create service name */
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (!schSCManager) {
"OpenSCManager failed");
}
else {
/* Added dependencies for the following: TCPIP, AFD
* AFD is the winsock handler, TCPIP is self evident
*
* RPCSS is the Remote Procedure Call (RPC) Locator
* required for DCOM communication. I am far from
* convinced we should toggle this, but be warned that
* future apache modules or ISAPI dll's may depend on it.
*/
schSCManager, // SCManager database
service_name, // name of service
display_name, // name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szQuotedPath, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
"Tcpip\0Afd\0", // dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService) {
/* Now store the server_root in the registry */
}
else {
"CreateService failed");
}
}
}
void RemoveServiceNT(char *display_name)
{
char service_name[256];
/* Remove spaces from display name to create service name */
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (!schSCManager) {
"OpenSCManager failed");
}
else {
if (schService == NULL) {
/* Could not open the service */
"OpenService failed");
}
else {
/* try to stop the service */
// now remove the service
if (DeleteService(schService) == 0)
"DeleteService failed");
else
}
/* SCM removes registry parameters */
}
}
/* A hack to determine if we're running as a service without waiting for
* the SCM to fail; if AllocConsole succeeds, we're a service.
*/
BOOL isProcessService() {
if( !AllocConsole() )
return FALSE;
FreeConsole();
return TRUE;
}
/* Determine is service_name is a valid service
* Simplify by testing the registry rather than the SCM
* as this will work on both WinNT and Win9x.
*/
char service_name[256];
char *service_conf;
/* Remove spaces from display name to create service name */
return TRUE;
}
return FALSE;
#if 0
int Err;
"OpenSCManager failed");
return FALSE;
}
return TRUE;
}
Err = GetLastError();
"OpenService failed");
return FALSE;
#endif
}
/* Although the Win9x service enhancement added -k startservice,
* it is never processed here, so we still ignore that param.
*/
char service_name[256];
break;
printf("signal must be start, restart, or shutdown\n");
return FALSE;
}
/* Remove spaces from display name to create service name */
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (!schSCManager) {
"OpenSCManager failed");
}
else {
if (schService == NULL) {
/* Could not open the service */
"OpenService failed");
}
else {
"QueryService failed");
else {
else {
if( success )
else
}
}
}
/* SCM removes registry parameters */
}
return success;
}
{
Sleep(1000);
Sleep(1000);
else
break;
}
}
return TRUE;
return FALSE;
}
Sleep(1000);
Sleep(1000);
else
break;
}
}
return TRUE;
return FALSE;
}
#endif /* WIN32 */