📄 svcproc.cxx
字号:
/*
* svcproc.cxx
*
* Service process implementation for Win95 and WinNT
*
* Portable Windows Library
*
* Copyright (c) 1993-1998 Equivalence Pty. Ltd.
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Portable Windows Library.
*
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
*
* Portions are Copyright (C) 1993 Free Software Foundation, Inc.
* All Rights Reserved.
*
* Contributor(s): ______________________________________.
*
* $Log: svcproc.cxx,v $
* Revision 1.80 2005/11/30 12:47:42 csoutheren
* Removed tabs, reformatted some code, and changed tags for Doxygen
*
* Revision 1.79 2005/10/21 06:03:51 csoutheren
* Fixed warning on VS.NET 2005
*
* Revision 1.78 2004/06/01 05:22:44 csoutheren
* Restored memory check functionality
*
* Revision 1.77 2004/04/03 08:22:22 csoutheren
* Remove pseudo-RTTI and replaced with real RTTI
*
* Revision 1.76 2004/04/03 06:54:30 rjongbloed
* Many and various changes to support new Visual C++ 2003
*
* Revision 1.75 2004/03/20 09:08:15 rjongbloed
* Changed interaction between PTrace and PSystemLog so that the tracing code does
* not need to know about the system log, thus reducing the code footprint for most apps.
*
* Revision 1.74 2003/09/17 09:02:14 csoutheren
* Removed memory leak detection code
*
* Revision 1.73 2003/09/17 05:45:10 csoutheren
* Removed recursive includes
*
* Revision 1.72 2003/02/11 07:58:40 robertj
* Added ignore allocations so don't get meaningless memory leak at end of run
* cause by system log stream not being deleted, thanks Sebastian Meyer
*
* Revision 1.71 2002/11/01 05:16:41 robertj
* Fixed additional debug levels in PSystemLog to text file.
*
* Revision 1.70 2002/09/23 07:17:24 robertj
* Changes to allow winsock2 to be included.
*
* Revision 1.69 2002/03/10 15:37:35 robertj
* Added milliseconds to timestamp output
*
* Revision 1.68 2001/12/13 09:20:57 robertj
* Fixed system log so does not crash if current thread not created by PWLib.
*
* Revision 1.67 2001/09/13 00:23:33 robertj
* Fixed problem where system log output can occur with no current thread.
*
* Revision 1.66 2001/06/02 01:33:29 robertj
* Thread name is always presend now so always use it in system log
* output and make it wider in text output and tab area in window.
*
* Revision 1.65 2001/04/18 00:19:07 robertj
* Removed newline from error code string in NT event.
*
* Revision 1.64 2001/04/16 23:04:25 craigs
* Fixed problem with unknown command causing page fault due to missing comma in command list
*
* Revision 1.63 2001/04/12 01:34:02 robertj
* Added threads to NT event log.
*
* Revision 1.62 2001/03/24 05:53:12 robertj
* Added Windows 98 and ME to GetOSName()
*
* Revision 1.61 2001/03/24 05:37:01 robertj
* Changed default directory of log file to same as executable.
* Change tray icon to wait for service stop before displaying message.
* Changed tray icon message display to not have date and thread.
*
* Revision 1.60 2001/03/23 05:35:34 robertj
* Added ability for a service to output trace/system log to file while in debug mode.
* Added saving of debug window position.
*
* Revision 1.59 2001/02/15 01:12:15 robertj
* Moved some often repeated HTTP service code into PHTTPServiceProcess.
*
* Revision 1.58 2001/02/13 03:30:22 robertj
* Added function to do heap validation.
*
* Revision 1.57 2000/05/02 03:16:46 robertj
* Added display of thread name in SystemLog, thanks Ashley Unitt.
*
* Revision 1.56 1999/09/13 13:15:08 robertj
* Changed PTRACE so will output to system log in PServiceProcess applications.
*
* Revision 1.55 1999/08/07 01:43:41 robertj
* Added "NoWin" option to prevent display of window in command line commands.
*
* Revision 1.54 1999/07/16 03:22:16 robertj
* Fixed tray icon version command so does not ask question.
*
* Revision 1.53 1999/06/14 07:59:39 robertj
* Enhanced tracing again to add options to trace output (timestamps etc).
*
* Revision 1.52 1999/04/21 01:57:09 robertj
* Added confirmation dialog to menu commands.
*
* Revision 1.51 1999/03/09 10:30:19 robertj
* Fixed ability to have PMEMORY_CHECK on/off on both debug/release versions.
*
* Revision 1.50 1999/02/16 08:08:07 robertj
* MSVC 6.0 compatibility changes.
*
* Revision 1.49 1999/01/29 12:20:19 robertj
* Changed service process to output trace info to the Win32 debug output.
*
* Revision 1.48 1998/12/04 10:10:45 robertj
* Added virtual for determining if process is a service. Fixes linkage problem.
*
* Revision 1.47 1998/11/30 04:50:17 robertj
* New directory structure
*
* Revision 1.46 1998/10/18 14:28:34 robertj
* Renamed argv/argc to eliminate accidental usage.
* Fixed strange problem withs etting debug window tabstops in optimised version.
*
* Revision 1.45 1998/10/13 14:14:09 robertj
* Added thread ID to log.
* Added heap debug display to service menus.
*
* Revision 1.44 1998/09/24 03:30:57 robertj
* Added open software license.
*
* Revision 1.43 1998/08/20 06:06:03 robertj
* Fixed bug where web page can be asked for when service not running.
*
* Revision 1.42 1998/05/21 04:29:44 robertj
* Fixed "Proxies stopped" dialog appearing when shutting down windows.
*
* Revision 1.41 1998/05/07 05:21:38 robertj
* Improved formatting of debug window, adding tabs and tab stops.
*
* Revision 1.40 1998/04/07 13:32:14 robertj
* Changed startup code to support PApplication class.
*
* Revision 1.39 1998/04/01 01:52:53 robertj
* Fixed problem with NoAutoDelete threads.
*
* Revision 1.38 1998/03/29 06:16:53 robertj
* Rearranged initialisation sequence so PProcess descendent constructors can do "things".
*
* Revision 1.37 1998/03/20 03:20:45 robertj
* Lined up debug output.
*
* Revision 1.36 1998/03/05 12:49:55 robertj
* MemCheck fixes.
*
* Revision 1.35 1998/02/20 23:01:10 robertj
* Fixed bug where application exits on log out in win95.
*
* Revision 1.34 1998/02/16 01:43:57 robertj
* Really fixed spurious error display on install/start/stop etc
*
* Revision 1.33 1998/02/16 00:12:22 robertj
* Added tray icon support.
* Fixed problem with services and directory paths with spaces in them.
*
* Revision 1.32 1998/02/03 06:16:31 robertj
* Added extra log levels.
* Fixed bug where window disappears after debug service termination.
*
* Revision 1.31 1998/01/26 00:56:11 robertj
* Changed ServiceProcess to exclusively use named event to detect running process.
*
* Revision 1.30 1997/12/18 05:05:45 robertj
* Added Edit menu.
*
* Revision 1.29 1997/11/04 06:01:45 robertj
* Fix of "service hung at startup" message for NT service.
*
* Revision 1.28 1997/10/30 10:17:10 robertj
* Fixed bug in detection of running service.
*
* Revision 1.27 1997/10/03 15:14:17 robertj
* Fixed crash on exit.
*
* Revision 1.26 1997/08/28 12:50:32 robertj
* Fixed race condition in cleaning up threads on application termination.
*
* Revision 1.25 1997/07/17 12:43:29 robertj
* Fixed bug for auto-start of service under '95.
*
* Revision 1.24 1997/07/14 11:47:20 robertj
* Added "const" to numerous variables.
*
* Revision 1.23 1997/07/08 13:00:30 robertj
* DLL support.
* Fixed '95 support so service runs without logging in.
*
* Revision 1.22 1997/04/27 05:50:27 robertj
* DLL support.
*
* Revision 1.21 1997/03/18 21:23:27 robertj
* Fix service manager falsely accusing app of crashing if OnStart() is slow.
*
* Revision 1.20 1997/02/05 11:50:40 robertj
* Changed current process function to return reference and validate objects descendancy.
* Changed log file name calculation to occur only once.
* Added some MSVC memory debugging functions.
*
* Revision 1.19 1996/12/05 11:53:49 craigs
* Fixed failure to output PError to debug window if CRLF pairs used
*
* Revision 1.18 1996/11/30 12:07:19 robertj
* Changed service creation for NT so is auto-start,
*
* Revision 1.17 1996/11/18 11:32:04 robertj
* Fixed bug in doing a "stop" command closing ALL instances of service.
*
* Revision 1.16 1996/11/12 10:15:16 robertj
* Fixed bug in NT 3.51 locking up when needs to output to window.
*
* Revision 1.15 1996/11/10 21:04:32 robertj
* Added category names to event log.
* Fixed menu enables for debug and command modes.
*
* Revision 1.14 1996/11/04 03:39:13 robertj
* Improved detection of running service so debug mode cannot run.
*
* Revision 1.13 1996/10/31 12:54:01 robertj
* Fixed bug in window not being displayed when command line used.
*
* Revision 1.12 1996/10/18 11:22:14 robertj
* Fixed problems with window not being shown under NT.
*
* Revision 1.11 1996/10/14 03:09:58 robertj
* Fixed major bug in debug outpuit locking up (infinite loop)
* Changed menus so cannot start service if in debug mode
*
* Revision 1.10 1996/10/08 13:04:43 robertj
* Rewrite to use standard window isntead of console window.
*
* Revision 1.9 1996/09/16 12:56:27 robertj
* DLL support
*
* Revision 1.8 1996/09/14 12:34:23 robertj
* Fixed problem with spontaneous exit from app under Win95.
*
* Revision 1.7 1996/08/19 13:36:03 robertj
* Added "Debug" level to system log.
*
* Revision 1.6 1996/07/30 12:23:32 robertj
* Added better service running test.
* Changed SIGINTR handler to just set termination event.
*
* Revision 1.5 1996/07/27 04:07:57 robertj
* Changed thread creation to use C library function instead of direct WIN32.
* Changed SystemLog to be stream based rather than printf based.
* Fixed Win95 support for service start/stop and prevent multiple starts.
*
* Revision 1.4 1996/06/10 09:54:08 robertj
* Fixed Win95 service install bug (typo!)
*
* Revision 1.3 1996/05/30 11:49:10 robertj
* Fixed crash on exit bug.
*
* Revision 1.2 1996/05/23 10:03:21 robertj
* Windows 95 support.
*
* Revision 1.1 1996/05/15 21:11:51 robertj
* Initial revision
*
*/
#include <ptlib.h>
#include <winuser.h>
#include <winnls.h>
#include <shellapi.h>
#include <commdlg.h>
#include <process.h>
#include <signal.h>
#include <fcntl.h>
#include <io.h>
#ifdef __USE_STL__
#include <fstream>
#else
#include <fstream.h>
#endif
#include <ptlib/svcproc.h>
#include <ptlib/msos/ptlib/debstrm.h>
#define UWM_SYSTRAY (WM_USER + 1)
#define ICON_RESID 1
#define SYSTRAY_ICON_ID 1
static HINSTANCE hInstance;
#define DATE_WIDTH 72
#define THREAD_WIDTH 80
#define LEVEL_WIDTH 32
#define PROTO_WIDTH 40
#define ACTION_WIDTH 48
enum {
SvcCmdTray,
SvcCmdNoTray,
SvcCmdVersion,
SvcCmdInstall,
SvcCmdRemove,
SvcCmdStart,
SvcCmdStop,
SvcCmdPause,
SvcCmdResume,
SvcCmdDeinstall,
SvcCmdNoWindow,
NumSvcCmds
};
static const char * const ServiceCommandNames[NumSvcCmds] = {
"Tray",
"NoTray",
"Version",
"Install",
"Remove",
"Start",
"Stop",
"Pause",
"Resume",
"Deinstall",
"NoWin"
};
class PNotifyIconData : public NOTIFYICONDATA {
public:
PNotifyIconData(HWND hWnd, UINT flags, const char * tip = NULL);
void Add() { Shell_NotifyIcon(NIM_ADD, this); }
void Delete() { Shell_NotifyIcon(NIM_DELETE, this); }
void Modify() { Shell_NotifyIcon(NIM_MODIFY, this); }
};
PNotifyIconData::PNotifyIconData(HWND window, UINT flags, const char * tip)
{
cbSize = sizeof(NOTIFYICONDATA);
hWnd = window;
uID = SYSTRAY_ICON_ID;
uFlags = flags;
if (tip != NULL) {
strncpy(szTip, tip, sizeof(szTip)-1);
szTip[sizeof(szTip)-1] = '\0';
uFlags |= NIF_TIP;
}
}
enum TrayIconRegistryCommand {
AddTrayIcon,
DelTrayIcon,
CheckTrayIcon
};
static BOOL TrayIconRegistry(PServiceProcess * svc, TrayIconRegistryCommand cmd)
{
HKEY key;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
0, KEY_ALL_ACCESS, &key) != ERROR_SUCCESS)
return FALSE;
DWORD err = 1;
DWORD type;
DWORD len;
PString str;
switch (cmd) {
case CheckTrayIcon :
err = RegQueryValueEx(key, svc->GetName(), 0, &type, NULL, &len);
break;
case AddTrayIcon :
str = "\"" + svc->GetFile() + "\" Tray";
err = RegSetValueEx(key, svc->GetName(), 0, REG_SZ,
(LPBYTE)(const char *)str, str.GetLength() + 1);
break;
case DelTrayIcon :
err = RegDeleteValue(key, (char *)(const char *)svc->GetName());
}
RegCloseKey(key);
return err == ERROR_SUCCESS;
}
///////////////////////////////////////////////////////////////////////////////
// PSystemLog
void PSystemLog::Output(Level level, const char * msg)
{
PServiceProcess & process = PServiceProcess::Current();
if (level > process.GetLogLevel())
return;
DWORD err = ::GetLastError();
if (process.isWin95 || process.controlWindow != NULL) {
static HANDLE mutex = CreateMutex(NULL, FALSE, NULL);
WaitForSingleObject(mutex, INFINITE);
ostream * out;
if (!process.systemLogFileName)
out = new ofstream(process.systemLogFileName, ios::app);
else
out = new PStringStream;
PTime now;
*out << now.AsString("yyyy/MM/dd hh:mm:ss.uuu\t");
PThread * thread = PThread::Current();
if (thread == NULL)
*out << "ThreadID=0x"
<< setfill('0') << hex
<< setw(8) << GetCurrentThreadId()
<< setfill(' ') << dec;
else {
PString threadName = thread->GetThreadName();
if (threadName.GetLength() <= 23)
*out << setw(23) << threadName;
else
*out << threadName.Left(10) << "..." << threadName.Right(10);
}
*out << '\t';
if (level < 0)
*out << "Message";
else {
static const char * const levelName[4] = {
"Fatal error",
"Error",
"Warning",
"Info"
};
if (level < PARRAYSIZE(levelName))
*out << levelName[level];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -