service.c revision 059f354a6b7b116e4469ff7fe99c1142affa0ad6
/* ====================================================================
* 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
#include "os.h"
#include <stdlib.h>
#include <direct.h>
#define CORE_PRIVATE
#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;
char *service_dir;
} 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;
}
{
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
service_cd();
/* 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);
}
{
{
"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 */