ApacheMonitor.c revision 385313f8fa11244ec086f59ec2b3bf09efd9592b
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* ====================================================================
* ApacheMonitor.c Simple program to manage and monitor Apache services.
*
* Contributed by Mladen Turk <mturk mappingsoft.com>
*
* 05 Aug 2001
* ====================================================================
*/
#define _WIN32_WINNT 0x0500
#ifndef STRICT
#define STRICT
#endif
#ifndef OEMRESOURCE
#define OEMRESOURCE
#endif
#define _CRT_SECURE_NO_DEPRECATE
#endif
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <objbase.h>
#include <shlobj.h>
#include <stdlib.h>
#include <stdio.h>
#include <WtsApi32.h>
#include <tchar.h>
#include "ApacheMonitor.h"
#ifndef AM_STRINGIFY
/** Properly quote a value as a string in the C preprocessor */
#define AM_STRINGIFY(n) AM_STRINGIFY_HELPER(n)
/** Helper macro for AM_STRINGIFY */
#define AM_STRINGIFY_HELPER(n) #n
#endif
#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++) {
}
}
}
{
if (szError) {
}
else {
}
if (bFatal) {
PostQuitMessage(0);
}
}
{
if (g_dwOSVersion < OS_VERSION_WIN2K) {
return 0;
}
FALSE);
return 0;
}
return 1;
}
{
int i = 0;
return FALSE;
}
++i;
}
if (i > MAX_APACHE_COMPUTERS - 1) {
return FALSE;
}
!= ERROR_SUCCESS) {
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
break;
case VER_PLATFORM_WIN32s:
default:
*dwVersion = 0;
return FALSE;
}
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();
}
{
/* create popup menu */
if (hMenu)
{
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' */
&rWorkArea, 0);
if (!bResult) {
}
/* Calculate new X and Y position*/
}
{
}
{
static int nItems = 0;
if (!g_bDlgServiceOn) {
return;
}
++nItems;
if (nItems > MAX_LOADSTRING)
{
nItems = 1;
}
}
{
int ticks;
if (!schSCManager) {
FALSE);
return FALSE;
}
if (schService == NULL)
{
/* Avoid recursion of ImagePath NULL (from this Respawn) */
if (szImagePath) {
}
else {
FALSE);
}
return FALSE;
}
else
{
switch (dwCommand)
{
case SERVICE_CONTROL_STOP:
&schSStatus)) {
Sleep(1000);
{
{
Sleep(1000);
}
else {
break;
}
}
}
{
{
}
}
break;
case SERVICE_CONTROL_CONTINUE:
{
Sleep(1000);
{
{
Sleep(1000);
}
else {
break;
}
}
}
{
{
}
}
break;
case SERVICE_APACHE_RESTART:
&schSStatus))
{
ticks = 60;
{
Sleep(1000);
{
return FALSE;
}
if (!--ticks) {
break;
}
}
}
{
}
break;
}
if (!retValue) {
FALSE);
}
return retValue;
}
return FALSE;
}
{
dwPid = 0;
if (!schSCManager) {
return FALSE;
}
if (schService != NULL)
{
{
if (lpdwPid) {
*lpdwPid = 1;
}
}
}
else {
}
return FALSE;
}
BOOL FindRunningServices(void)
{
int i = 0;
{
dwPid = 0;
}
}
++i;
}
return rv;
}
{
int i, stPos = 0;
int computers = 0;
_T("System\\CurrentControlSet\\Services\\"),
if (retCode != ERROR_SUCCESS)
{
return FALSE;
}
{
if (retCode == ERROR_SUCCESS)
{
{
if (rv == ERROR_SUCCESS
|| dwKeyType == REG_EXPAND_SZ)
&& dwBufLen)
{
/* the service name could be httpd*.exe or Apache*.exe */
{
== ERROR_SUCCESS) {
}
== ERROR_SUCCESS)
{
.szComputerName, g_szLocalHost) != 0)
{
.szComputerName + 2);
}
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:
| WS_CHILD | WS_VISIBLE,
if (GetApacheServicesStatus())
{
i = 0;
{
: g_hBmpStart);
++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_FOCUS:
case ODA_SELECT:
case ODA_DRAWENTIRE:
{
if (g_hBmpPicture == g_hBmpStop)
{
}
else if (g_hBmpPicture == g_hBmpStart)
{
}
else {
}
g_szLocalHost) == 0) {
}
else {
}
}
else {
}
}
else {
}
}
else
{
}
break;
}
return TRUE;
case WM_COMMAND:
{
case IDL_SERVICES:
{
case LBN_DBLCLK:
/* if started then stop, if stopped then start */
{
if (g_hBmpPicture == g_hBmpStop) {
}
else {
}
}
return TRUE;
}
break;
case IDCANCEL:
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_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;
}
{
SetLastError(0);
if (GetLastError())
return(2);
== WAIT_OBJECT_0) {
return (0);
}
}
if (appproc)
if (TerminateProcess(appproc, 0)) {
return (0);
}
}
/* Perhaps we were short of permissions? */
return (2);
}
static int KillAllMonitors(void)
{
int exitcode = 0;
DWORD tsProcCount, i;
/* This is graceful, close our own Window, clearing the icon */
if (g_dwOSVersion < OS_VERSION_WIN2K)
return exitcode;
&tsProcs, &tsProcCount))
return exitcode;
/* This is ungraceful; close other Windows, with a lingering icon.
* Since on terminal server it's not possible to post the message
* to exit across sessions, we have to suffer this side effect
* of a taskbar 'icon' which will evaporate the next time that
* the user hovers over it or when the taskbar area is updated.
*/
for (i = 0; i < tsProcCount; ++i) {
}
return exitcode;
}
/* Create main invisible window */
{
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.lpszMenuName = 0;
if (RegisterClassEx(&wcex)) {
0, 0, 0, 0, 0,
}
return hWnd;
}
#ifndef UNICODE
/* Borrowed from CRT internal.h for _MBCS argc/argv parsing in this GUI app */
#endif
{
/* existing window */
int i;
DWORD d;
if (!GetSystemOSVersion(&g_dwOSVersion))
{
return 1;
}
}
for (i = IDS_MSG_FIRST; i <= IDS_MSG_LAST; ++i) {
}
d = MAX_COMPUTERNAME_LENGTH+1;
#ifdef UNICODE
#else
_setargv();
#endif
{
/* Off to chase and close up every ApacheMonitor taskbar window */
return KillAllMonitors();
}
{
if ((dwControl != SERVICE_CONTROL_CONTINUE) &&
(dwControl != SERVICE_APACHE_RESTART) &&
{
return 1;
}
/* Chase down and close up our session's previous window */
}
else if (__argc != 1) {
return 1;
}
else if (appwindow)
{
return 0;
}
if (g_hwndMain != NULL)
{
/* To avoid recursion, pass ImagePath NULL (a noop on NT and later) */
{
}
}
return 0;
}