service.c revision 5e062d92cab5b4c1d7f998f4912b807bf9302698
/* ====================================================================
* Copyright (c) 1995-1999 The Apache Group. 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. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* 4. The names "Apache Server" and "Apache Group" 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 names without prior written
* permission of the Apache Group.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``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 GROUP 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 Group and was originally based
* on public domain software written at the National Center for
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
* For more information on the Apache Group and the Apache HTTP server
* project, please see <http://www.apache.org/>.
*
*/
#ifdef WIN32
#include "os.h"
#include <stdlib.h>
#include <direct.h>
#include "httpd.h"
#include "http_conf_globals.h"
#include "http_log.h"
#include "http_main.h"
#include "service.h"
#include "registry.h"
static struct
{
int (*main_fn)(int, char **);
int connected;
char *name;
int exit_status;
} globdat;
static int ap_start_service(SC_HANDLE);
static int ap_stop_service(SC_HANDLE);
{
{
{ "", 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);
}
}
void service_cd()
{
/* change to the drive with the executable */
char buf[300];
buf[2] = 0;
}
{
{
"Failure registering service handler");
return;
}
SERVICE_START_PENDING, // service state
NO_ERROR, // exit code
3000); // wait hint
service_cd();
// if( service_init() )
/* Arguments are ok except for \! */
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);
}
{
{
"GetModuleFileName failed");
return;
}
NULL, // machine (NULL == local)
NULL, // database (NULL == default)
SC_MANAGER_ALL_ACCESS // access required
);
if (!schSCManager) {
"OpenSCManager failed");
}
else {
schSCManager, // SCManager database
service_name, // name of service
service_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
NULL, // dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService) {
/* Now store the server_root in the registry */
}
else {
"CreateService failed");
}
}
}
void RemoveService(char *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
*/
int Err;
"OpenSCManager failed");
return FALSE;
}
return TRUE;
}
Err = GetLastError();
"OpenService failed");
return FALSE;
}
break;
printf("signal must be start, restart, or shutdown\n");
return FALSE;
}
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 */