windows/APIService.cpp File Reference
#include <aclapi.h>
#include "APIServer.hpp"
#include "stdio.h"
Include dependency graph for windows/APIService.cpp:

Go to the source code of this file.

Macros

#define SERVICENAME   "RXAPI"
 
#define SERVICEDESCRIPTION   "%s Service for Open Object Rexx version %d.%d.%d"
 
#define SYNTAX_HELP
 
#define APP_LOG_KEYNAME   "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\"
 
#define WINDOWS_95_98_ME   VER_PLATFORM_WIN32_WINDOWS
 

Enumerations

enum  ServiceStateType { installed_state , running_state , disabled_state , uninstalled_state }
 
enum  WindowsVersionType { Windows_NT , Windows_2K }
 

Functions

void setServiceDACL (SC_HANDLE hService)
 
void showMessage (const char *msg, unsigned int icon)
 
bool isAtLeastVersion (WindowsVersionType type)
 
void Run (bool asService)
 
bool OnInit ()
 
void OnStop ()
 
void OnInterrogate ()
 
void OnPause ()
 
void OnContinue ()
 
void OnShutdown ()
 
BOOL OnUserControl (DWORD dwOpcode)
 
void SetStatus (DWORD dwState)
 
static void __cdecl Handler (DWORD dwOpcode)
 
bool Initialize ()
 
static void WINAPI ServiceMain (DWORD dwArgc, LPTSTR *lpszArgv)
 
bool startTheService (void)
 
bool stopTheService (SC_HANDLE hService, DWORD timeOut)
 
bool Install ()
 
bool Uninstall ()
 
bool serviceIsRunning (SC_HANDLE hService)
 
ServiceStateType getServiceState (void)
 
bool startAsWindowsService (void)
 
void processCmdLine (const char *cmdLine)
 
int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
 

Variables

static SERVICE_STATUS_HANDLE m_hServiceStatus
 
static SERVICE_STATUS m_Status
 
static SERVICE_DESCRIPTION Info
 
APIServer apiServer
 

Macro Definition Documentation

◆ APP_LOG_KEYNAME

#define APP_LOG_KEYNAME   "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\"

Definition at line 53 of file windows/APIService.cpp.

◆ SERVICEDESCRIPTION

#define SERVICEDESCRIPTION   "%s Service for Open Object Rexx version %d.%d.%d"

Definition at line 44 of file windows/APIService.cpp.

◆ SERVICENAME

#define SERVICENAME   "RXAPI"

Definition at line 43 of file windows/APIService.cpp.

◆ SYNTAX_HELP

#define SYNTAX_HELP
Value:
"Syntax: rxapi opt [/s]\n\n" \
"Where opt is exactly one of:\n" \
"/i :\tinstall as Service.\n" \
"/u :\tuninstall as Service.\n" \
"/v :\tshow Version number\n\n" \
"The /s (silent) is optional and prevents any messages from displaying."

Definition at line 46 of file windows/APIService.cpp.

◆ WINDOWS_95_98_ME

#define WINDOWS_95_98_ME   VER_PLATFORM_WIN32_WINDOWS

Definition at line 113 of file windows/APIService.cpp.

Enumeration Type Documentation

◆ ServiceStateType

Enumerator
installed_state 
running_state 
disabled_state 
uninstalled_state 

Definition at line 55 of file windows/APIService.cpp.

◆ WindowsVersionType

Enumerator
Windows_NT 
Windows_2K 

Definition at line 56 of file windows/APIService.cpp.

Function Documentation

◆ getServiceState()

ServiceStateType getServiceState ( void  )

Determines if: rxapi is installed as a service and is already running, is installed as a service but not running, installed but the service is currently disabled, or not installed as a service.

Returns
One of the 4 service state enums.

Definition at line 758 of file windows/APIService.cpp.

References disabled_state, installed_state, running_state, serviceIsRunning(), SERVICENAME, and uninstalled_state.

Referenced by processCmdLine(), and startAsWindowsService().

◆ Handler()

static void __cdecl Handler ( DWORD  dwOpcode)
static

The handler function registered with the Service Control dispatcher. The dispatcher uses this function to send service control messages to the service process.

Parameters
dwOpcodeThe control message.

Definition at line 223 of file windows/APIService.cpp.

References m_hServiceStatus, m_Status, OnContinue(), OnInterrogate(), OnPause(), OnShutdown(), OnStop(), and SetStatus().

Referenced by ServiceMain().

◆ Initialize()

bool Initialize ( )

Definition at line 255 of file windows/APIService.cpp.

References m_Status, OnInit(), and SetStatus().

Referenced by ServiceMain().

◆ Install()

bool Install ( )

Install rxapi as a Windows service and add the registry entries to allow logging through the Event Log service.

Returns
True if the service was installed, otherwise false.

Definition at line 522 of file windows/APIService.cpp.

References APP_LOG_KEYNAME, cleanup(), Info, isAtLeastVersion(), SERVICEDESCRIPTION, SERVICENAME, setServiceDACL(), and Windows_2K.

Referenced by processCmdLine().

◆ isAtLeastVersion()

bool isAtLeastVersion ( WindowsVersionType  type)

Definition at line 115 of file windows/APIService.cpp.

References type, version_info, Windows_2K, WINDOWS_95_98_ME, and Windows_NT.

Referenced by Install(), and WinMain().

◆ OnContinue()

void OnContinue ( )

Definition at line 190 of file windows/APIService.cpp.

Referenced by Handler().

◆ OnInit()

bool OnInit ( )

The following functions handle the Service control requests.

Definition at line 168 of file windows/APIService.cpp.

Referenced by Initialize().

◆ OnInterrogate()

void OnInterrogate ( )

Definition at line 180 of file windows/APIService.cpp.

Referenced by Handler().

◆ OnPause()

void OnPause ( )

Definition at line 185 of file windows/APIService.cpp.

Referenced by Handler().

◆ OnShutdown()

void OnShutdown ( )

Definition at line 195 of file windows/APIService.cpp.

Referenced by Handler().

◆ OnStop()

void OnStop ( )

Definition at line 174 of file windows/APIService.cpp.

References apiServer, and APIServer::terminateServer().

Referenced by Handler().

◆ OnUserControl()

BOOL OnUserControl ( DWORD  dwOpcode)

Definition at line 200 of file windows/APIService.cpp.

◆ processCmdLine()

void processCmdLine ( const char *  cmdLine)

We have command line args, process them. Most likely the args are to install or uninstall rxapi as a service.

Parameters
cmdLineThe command line, must not be null.

Definition at line 875 of file windows/APIService.cpp.

References getServiceState(), Install(), SERVICENAME, showMessage(), SYNTAX_HELP, Uninstall(), and uninstalled_state.

Referenced by WinMain().

◆ Run()

void Run ( bool  asService)

Starts up the API server and has it listen for messages.

Run() is either called from ServiceMain() in which case the API server will be running in a Service process. If rxapi is not installed as a service, or for some reason the service can not be started, then Run() is called from WinMain() and the API server runs in a standard process.

Parameters
asServiceIf true, Run() was called from ServiceMain(), otherwise Run() was called from WinMain().

Definition at line 150 of file windows/APIService.cpp.

References apiServer, APIServer::initServer(), APIServer::listenForConnections(), and APIServer::terminateServer().

Referenced by ServiceMain(), and WinMain().

◆ serviceIsRunning()

bool serviceIsRunning ( SC_HANDLE  hService)

Determines if the service is currently running.

If the current state is START PENDING will wait until the service is running or times out.

Parameters
hServiceHandle to the opened service. Note that the handle has to have been opened with SERVICE_QUERY_STATUS so that we can use QueryServiceStatusEx().
Returns
True if the service is running, false otherwise.

Definition at line 674 of file windows/APIService.cpp.

Referenced by getServiceState().

◆ ServiceMain()

static void WINAPI ServiceMain ( DWORD  dwArgc,
LPTSTR *  lpszArgv 
)
static

After the service control dislpatcher receives a start request, (see startTheService(),) it creates a new thread and invokes this function on that thread to do the actual work of the service.

Parameters
dwArgcCount of args (not used by rxapi)
lpszArgvArgs (not used by rxapi)

Definition at line 287 of file windows/APIService.cpp.

References Handler(), Initialize(), m_hServiceStatus, m_Status, Run(), SERVICENAME, and SetStatus().

Referenced by startTheService().

◆ setServiceDACL()

void setServiceDACL ( SC_HANDLE  hService)

Definition at line 439 of file windows/APIService.cpp.

References cleanup().

Referenced by Install().

◆ SetStatus()

void SetStatus ( DWORD  dwState)

Updates the Service Control Manager with our current state.

Parameters
dwStateThe current state.

Definition at line 210 of file windows/APIService.cpp.

References m_hServiceStatus, and m_Status.

Referenced by Handler(), Initialize(), and ServiceMain().

◆ showMessage()

void showMessage ( const char *  msg,
unsigned int  icon 
)
inline

Definition at line 108 of file windows/APIService.cpp.

References SERVICENAME.

Referenced by processCmdLine().

◆ startAsWindowsService()

bool startAsWindowsService ( void  )

Start rxapi as a Windows Service, if possible. This function determines if rxapi is installed as a Service and if it is already running as a service.

If it is installled as a service, but not running, an attempt is made to start it as a Service.

Returns
True if now running as a Windows Service, otherwis false.

Definition at line 813 of file windows/APIService.cpp.

References getServiceState(), installed_state, running_state, and startTheService().

Referenced by WinMain().

◆ startTheService()

bool startTheService ( void  )

Connects the main thread of this service process to the Service Control Manager. When this function succeeds, it will not return until the rxapi Service is stopped.

Returns
A return of true indicates that rxapi was successfully started as a service process and has run to completion. A return of false indicates the rxapi service process was not started.
Note
When rxapi is installed as a service, it can not run as a service process unless it is started through the Service Control Manager. If rxapi is started through the command line or by CreateProcess(), StartServiceCtrlDispatcher() will return ERROR_FAILED_SERVICE_CONTROLLER_CONNECT and the service process will not have run.

Definition at line 330 of file windows/APIService.cpp.

References m_Status, ServiceMain(), and SERVICENAME.

Referenced by startAsWindowsService().

◆ stopTheService()

bool stopTheService ( SC_HANDLE  hService,
DWORD  timeOut 
)

If rxapi is currently running as a Service process, then stop it.

Parameters
hServiceOpened handle to the service. The handle must have been opened with SERVICE_QUERY_STATUS and SERVICE_STOP access rights.
timeOutTime to wait, in miliseconds for a pending stop to clear.
Returns
True if the service is stopped, false if not sure that the service is stopped.

Definition at line 366 of file windows/APIService.cpp.

Referenced by Uninstall().

◆ Uninstall()

bool Uninstall ( )

Deletes rxapi as a service and cleans up the registry entries that were made when rxapi was installed as a service.

Returns
true on success, othewise false.

Definition at line 618 of file windows/APIService.cpp.

References APP_LOG_KEYNAME, SERVICENAME, and stopTheService().

Referenced by processCmdLine().

◆ WinMain()

int APIENTRY WinMain ( HINSTANCE  hInstance,
HINSTANCE  hPrevInstance,
LPSTR  lpCmdLine,
int  nCmdShow 
)

The main entry point for rxapi.exe.

When rxapi is installed as a service, the invoker might be the service control manager, or the interpreter could be starting rxapi as a service because rxapi was not running.

When rxapi is not installed as a service, then the invoker is most likely the interpreter starting up rxapi. Although, the user could also be invoking rxapi from the command line.

The third possibility is that rxapi is being invoked from the command line with arguments to either install, uninstall, or query rxapi as a service.

Parameters
hInstance
hPrevInstance
lpCmdLineThe only arg we are interested in, the command line arguments.
nCmdShow
Returns
0

Definition at line 1003 of file windows/APIService.cpp.

References isAtLeastVersion(), processCmdLine(), Run(), startAsWindowsService(), and Windows_NT.

Variable Documentation

◆ apiServer

APIServer apiServer

Definition at line 105 of file windows/APIService.cpp.

Referenced by OnStop(), and Run().

◆ Info

SERVICE_DESCRIPTION Info
static

Definition at line 64 of file windows/APIService.cpp.

Referenced by Install().

◆ m_hServiceStatus

SERVICE_STATUS_HANDLE m_hServiceStatus
static

Definition at line 62 of file windows/APIService.cpp.

Referenced by Handler(), ServiceMain(), and SetStatus().

◆ m_Status

SERVICE_STATUS m_Status
static

Definition at line 63 of file windows/APIService.cpp.

Referenced by Handler(), Initialize(), ServiceMain(), SetStatus(), and startTheService().