ApacheMonitor.c revision f1dca7251fadc2750abfa00e19c154aa61cb6a53
/* ====================================================================
* 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.
*/
/* ====================================================================
* ApacheMonitor.c Simple program to manage and monitor Apache services.
*
* Contributed by Mladen Turk <mturk@mappingsoft.com>
*
* 05 Aug 2001
* ====================================================================
*/
#define _WIN32_WINNT 0x0400
#ifndef STRICT
#define STRICT
#endif
#ifndef OEMRESOURCE
#define OEMRESOURCE
#endif
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <objbase.h>
#include <shlobj.h>
#include <stdlib.h>
#include <stdio.h>
#include "ApacheMonitor.h"
#define OS_VERSION_WIN9X 1
#define OS_VERSION_WINNT 2
#define OS_VERSION_WIN2K 3
/* Should be enough */
#define MAX_APACHE_SERVICES 128
#define MAX_APACHE_COMPUTERS 32
#define WM_TIMER_REFRESH 10
#define WM_TIMER_RESCAN 11
#define SERVICE_APACHE_RESTART 128
#define XBITMAP 16
#define YBITMAP 16
#define MAX_LOADSTRING 100
typedef struct _st_APACHE_SERVICE
{
typedef struct _st_MONITORED_COMPUTERS
{
/* Global variables */
/* locale language support */
void am_ClearServicesSt()
{
int i;
for (i = 0; i < MAX_APACHE_SERVICES; i++)
{
if (g_stServices[i].szServiceName)
if (g_stServices[i].szDisplayName)
if (g_stServices[i].szDescription)
if (g_stServices[i].szImagePath)
if (g_stServices[i].szComputerName)
}
}
void am_ClearComputersSt()
{
int i;
for (i = 0; i < MAX_APACHE_COMPUTERS; i++)
{
if (g_stComputers[i].szComputerName) {
}
}
}
{
int i = 0;
return TRUE;
++i;
}
return FALSE;
}
{
int i = 0, j;
break;
++i;
}
for( j = i; j < MAX_APACHE_COMPUTERS - 1; j++) {
}
for( i = j; i < MAX_APACHE_COMPUTERS; i++) {
}
}
}
{
if (szError)
else
{
NULL,
GetLastError(),
}
if ( bFatal)
PostQuitMessage(0);
}
{
int i = 0;
return FALSE;
++i;
}
if (i > MAX_APACHE_COMPUTERS - 1)
return FALSE;
{
return FALSE;
}
else
{
return TRUE;
}
}
{
buffer[0] = 0;
return buffer;
}
{
/*
Try calling GetVersionEx using the OSVERSIONINFOEX structure.
If that fails, try using the OSVERSIONINFO structure.
*/
if (!GetVersionEx(&osvi))
return FALSE;
switch (osvi.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
else
return FALSE;
break;
break;
case VER_PLATFORM_WIN32s:
*dwVersion = 0;
return FALSE;
default:
return FALSE;
break;
}
return TRUE;
}
{
int i = 0, n = 0;
{
if (g_stServices[i].dwPid != 0)
++n;
++i;
}
if (dwMessage != NIM_DELETE)
{
if (n)
else
}
else
if (n == i && n > 0)
else if (n)
else if (i)
else
}
{
{
if (fDefault)
if (!fEnabled)
}
else
}
{
smh = CreatePopupMenu();
appendMenuItem(smh, IDM_SM_START + uMenuId, g_lpMsg[IDS_MSG_SSTART-IDS_MSG_FIRST], FALSE, !fRunning);
appendMenuItem(smh, IDM_SM_RESTART + uMenuId, g_lpMsg[IDS_MSG_SRESTART-IDS_MSG_FIRST], FALSE, fRunning);
}
{
/* create popup menu */
if (hMenu)
{
if (g_dwOSVersion >= OS_VERSION_WINNT)
if (!SetForegroundWindow(hWnd))
GetCursorPos(&pt);
}
}
{
/* create services list popup menu and submenus */
int i = 0;
if (hMenu)
{
{
g_stServices[i].dwPid != 0);
++i;
}
if (i)
{
if (!SetForegroundWindow(hWnd))
GetCursorPos(&pt);
}
}
}
{
/* Get the Height and Width of the child window */
/* Get the limits of the 'workarea' */
sizeof(RECT),
0);
if (!bResult) {
}
/* Calculate new X and Y position*/
}
{
int nItem;
}
{
static int nItems = 0;
if (!g_bDlgServiceOn)
return;
++nItems;
if ( nItems > MAX_LOADSTRING)
{
nItems = 1;
}
}
{
int nPtr = 0;
{
if (dwReaded > 0)
{
{
nPtr = 0;
}
{
int i;
for (i = 0; i < 4; ++i)
}
else if (ch != '\r')
}
}
return 0;
}
{
MessageBeep(100);
return 0;
}
{
hProc = GetCurrentProcess();
NULL,
NULL,
TRUE,
NULL,
NULL ,
&stInfo,
if (!bResult)
{
return FALSE;
}
return TRUE;
}
{
if (bRedirectConsole)
return RunRedirectedConsole(szCmdLine);
NULL,
NULL,
TRUE,
NULL,
NULL ,
&stInfo,
&prInfo);
if (!bResult)
{
return FALSE;
}
Sleep(2000);
return TRUE;
}
BOOL ApacheManageService(LPCSTR szServiceName, LPCSTR szImagePath, LPSTR szComputerName, DWORD dwCommand)
{
int ticks;
if (g_dwOSVersion == OS_VERSION_WIN9X)
{
if (sPos)
{
switch (dwCommand)
{
case SERVICE_CONTROL_STOP:
break;
case SERVICE_CONTROL_CONTINUE:
serviceFlag = FALSE;
break;
case SERVICE_APACHE_RESTART:
break;
default:
return FALSE;
}
}
else
return FALSE;
{
return FALSE;
}
else if (!serviceFlag)
{
return TRUE;
}
}
else
{
/* Apache 2.0 uses '-k runservice' as cmdline parameter */
if (!sPos)
{
serviceFlag = FALSE;
}
if (sPos)
else
return FALSE;
NULL,
);
if (!schSCManager)
return FALSE;
if (schService != NULL)
{
switch (dwCommand)
{
case SERVICE_CONTROL_STOP:
{
Sleep(1000);
{
Sleep(1000);
else
break;
}
}
{
{
}
}
break;
case SERVICE_CONTROL_CONTINUE:
if (serviceFlag)
else
{
}
{
Sleep(1000);
{
Sleep(1000);
else
break;
}
}
{
{
}
}
/* is this OK to do? */
break;
case SERVICE_APACHE_RESTART:
{
ticks = 60;
{
Sleep(1000);
{
return FALSE;
}
if (!--ticks)
break;
}
}
{
}
break;
}
if (!retValue)
return retValue;
}
else
return FALSE;
}
return FALSE;
}
{
if (g_dwOSVersion == OS_VERSION_WIN9X)
{
{
*lpdwPid = 1;
return TRUE;
}
else
return FALSE;
}
else
{
dwPid = 0;
NULL,
);
if (!schSCManager)
return FALSE;
if (schService != NULL)
{
{
if (lpdwPid)
*lpdwPid = 1;
}
}
else
return FALSE;
}
return FALSE;
}
BOOL FindRunningServices(void)
{
int i = 0;
{
dwPid = 0;
++i;
}
return rv;
}
{
int i, stPos = 0;
int computers = 0;
"System\\CurrentControlSet\\Services\\",
if (retCode != ERROR_SUCCESS)
{
return FALSE;
}
{
if (retCode == ERROR_SUCCESS)
{
{
{
/* the service name could be Apache*.exe */
{
}
else
}
++stPos;
if (stPos >= MAX_APACHE_SERVICES)
retCode = !ERROR_SUCCESS;
}
}
}
}
}
++computers;
}
return TRUE;
}
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
{
case IDOK:
return TRUE;
}
case IDCANCEL:
return TRUE;
case IDC_LBROWSE:
{
ITEMIDLIST *il;
}
if (SHGetMalloc(&pMalloc)) {
}
}
return TRUE;
}
break;
case WM_QUIT:
case WM_CLOSE:
return TRUE;
default:
return FALSE;
}
return FALSE;
}
{
static HWND hStatusBar;
int i, y;
switch (message)
{
case WM_INITDIALOG:
if (g_dwOSVersion < OS_VERSION_WINNT)
{
}
| WS_CHILD | WS_VISIBLE,
if (GetApacheServicesStatus())
{
i = 0;
{
++i;
}
}
return TRUE;
break;
case WM_MANAGEMESSAGE:
return TRUE;
break;
case WM_UPDATEMESSAGE:
i = 0;
{
++i;
}
/* Dirty hack to bring the window to the foreground */
return TRUE;
break;
case WM_MEASUREITEM:
return TRUE;
case WM_SETCURSOR:
if (g_bConsoleRun)
else
return TRUE;
case WM_DRAWITEM:
{
break;
}
switch (lpdis->itemAction)
{
case ODA_SELECT:
case ODA_DRAWENTIRE:
{
if (g_hBmpPicture == g_hBmpStop)
{
}
else if (g_hBmpPicture == g_hBmpStart)
{
}
else {
}
else
else
}
else
{
}
XBITMAP + 6,
y,
break;
case ODA_FOCUS:
break;
}
return TRUE;
case WM_COMMAND:
{
case IDL_SERVICES:
{
case LBN_DBLCLK:
/* if started then stop, if stopped then start the service */
{
if (g_hBmpPicture == g_hBmpStop)
{
}
else
}
return TRUE;
break;
}
break;
case IDOK:
return TRUE;
case IDC_SSTART:
{
}
return TRUE;
case IDC_SSTOP:
{
}
return TRUE;
case IDC_SRESTART:
{
}
return TRUE;
case IDC_SMANAGER:
if (g_dwOSVersion >= OS_VERSION_WIN2K)
else
return TRUE;
case IDC_SEXIT:
return TRUE;
case IDC_SCONNECT:
return TRUE;
case IDC_SDISCONN:
}
return TRUE;
}
break;
case WM_SIZE:
{
case SIZE_MINIMIZED:
return TRUE;
break;
}
break;
case WM_QUIT:
case WM_CLOSE:
return TRUE;
default:
return FALSE;
}
return FALSE;
}
{
if (message == g_bUiTaskbarCreated)
{
/* restore the tray icon on shell restart */
}
switch (message)
{
case WM_CREATE:
break;
case WM_TIMER:
switch (wParam)
{
case WM_TIMER_RESCAN:
{
if (FindRunningServices() || g_bRescanServices)
{
if (g_hwndServiceDlg)
}
/* check if services list changed */
++nPrev;
++nNew;
{
if (g_hwndServiceDlg)
}
break;
}
case WM_TIMER_REFRESH:
{
if (g_bRescanServices)
{
if (g_hwndServiceDlg)
}
else if (FindRunningServices())
{
if (g_hwndServiceDlg)
}
break;
}
}
break;
case WM_QUIT:
break;
case WM_TRAYMESSAGE:
switch(lParam)
{
case WM_LBUTTONDBLCLK:
if (!g_bDlgServiceOn)
{
}
else if (IsWindow(g_hwndServiceDlg))
{
/* Dirty hack to bring the window to the foreground */
}
break;
case WM_LBUTTONUP:
break;
case WM_RBUTTONUP:
break;
}
break;
case WM_COMMAND:
{
return TRUE;
}
{
return TRUE;
}
{
return TRUE;
}
{
case IDM_RESTORE:
if (!g_bDlgServiceOn)
{
}
else if (IsWindow(g_hwndServiceDlg))
break;
case IDC_SMANAGER:
if (g_dwOSVersion >= OS_VERSION_WIN2K)
else
return TRUE;
case IDM_EXIT:
PostQuitMessage(0);
return TRUE;
}
default:
}
return FALSE;
}
/* Create main invisible window */
{
if (!GetSystemOSVersion(&g_dwOSVersion))
{
return hWnd;
}
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.lpszMenuName = 0;
if (RegisterClassEx(&wcex))
0, 0, 0, 0, 0,
return hWnd;
}
int nCmdShow)
{
/* single instance mutex */
int i;
DWORD d;
for (i = IDS_MSG_FIRST; i <= IDS_MSG_LAST; ++i) {
}
d = MAX_COMPUTERNAME_LENGTH+1;
{
if (hMutex)
return 0;
}
if (g_hwndMain != NULL)
{
{
}
}
return 0;
}