diff options
Diffstat (limited to 'src/bin/pgevent')
-rw-r--r-- | src/bin/pgevent/MSG00001.bin | bin | 0 -> 28 bytes | |||
-rw-r--r-- | src/bin/pgevent/Makefile | 38 | ||||
-rw-r--r-- | src/bin/pgevent/README | 20 | ||||
-rw-r--r-- | src/bin/pgevent/exports.txt | 3 | ||||
-rw-r--r-- | src/bin/pgevent/pgevent.c | 160 | ||||
-rw-r--r-- | src/bin/pgevent/pgevent.def | 5 | ||||
-rw-r--r-- | src/bin/pgevent/pgmsgevent.h | 46 | ||||
-rw-r--r-- | src/bin/pgevent/pgmsgevent.mc | 5 | ||||
-rw-r--r-- | src/bin/pgevent/pgmsgevent.rc | 4 |
9 files changed, 281 insertions, 0 deletions
diff --git a/src/bin/pgevent/MSG00001.bin b/src/bin/pgevent/MSG00001.bin Binary files differnew file mode 100644 index 0000000..6ac08e5 --- /dev/null +++ b/src/bin/pgevent/MSG00001.bin diff --git a/src/bin/pgevent/Makefile b/src/bin/pgevent/Makefile new file mode 100644 index 0000000..da69e91 --- /dev/null +++ b/src/bin/pgevent/Makefile @@ -0,0 +1,38 @@ +#------------------------------------------------------------------------- +# +# Makefile for src/bin/pgevent +# +# Copyright (c) 1996-2021, PostgreSQL Global Development Group +# +#------------------------------------------------------------------------- + +PGFILEDESC = "Eventlog message formatter" +PGAPPICON=win32 + +subdir = src/bin/pgevent +top_builddir = ../../.. +include $(top_builddir)/src/Makefile.global + +ifeq ($(PORTNAME), win32) + +OBJS = \ + pgevent.o \ + pgmsgevent.o +NAME=pgevent + +SHLIB_LINK = +SHLIB_EXPORTS = exports.txt + +all: all-lib + +install: all install-lib + +include $(top_srcdir)/src/Makefile.shlib + +pgmsgevent.o: pgmsgevent.rc win32ver.rc + $(WINDRES) $< -o $@ --include-dir=$(top_builddir)/src/include --include-dir=$(top_srcdir)/src/include --include-dir=$(srcdir) --include-dir=. + +clean distclean: clean-lib + rm -f $(OBJS) win32ver.rc + +endif diff --git a/src/bin/pgevent/README b/src/bin/pgevent/README new file mode 100644 index 0000000..10ec8d2 --- /dev/null +++ b/src/bin/pgevent/README @@ -0,0 +1,20 @@ +src/bin/pgevent/README + +pgevent +======= + +MSG00001.bin is a binary file, result of Microsoft MC compiler. MC compiler +can be downloaded for free with MS Core SDK but it is not included with MSYS +tools and I didn't find an alternative way to compile MC file. + +To summarize: the command "MC pgmsgevent.mc" generates pgmsgevent.h, +pgmsgevent.rc, and MSG00001.bin files. In MC file, we declare a string +with %s format, so we can write anything we want in the future without +needing to change the definition of this string. + +To finish, because DllUnregisterServer and DllRegisterServer are system +defined entry points, we need to export these two functions with their names +without "decoration", so we cannot use auto generated .def files without +handy modifications. + +Laurent Ballester diff --git a/src/bin/pgevent/exports.txt b/src/bin/pgevent/exports.txt new file mode 100644 index 0000000..8facaed --- /dev/null +++ b/src/bin/pgevent/exports.txt @@ -0,0 +1,3 @@ + DllUnregisterServer@0 ; + DllRegisterServer@0 ; + DllInstall ; diff --git a/src/bin/pgevent/pgevent.c b/src/bin/pgevent/pgevent.c new file mode 100644 index 0000000..807a2c9 --- /dev/null +++ b/src/bin/pgevent/pgevent.c @@ -0,0 +1,160 @@ +/*------------------------------------------------------------------------- + * + * pgevent.c + * Defines the entry point for pgevent dll. + * The DLL defines event source for backend + * + * + * IDENTIFICATION + * src/bin/pgevent/pgevent.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include <olectl.h> + +/* Global variables */ +HANDLE g_module = NULL; /* hModule of DLL */ + +/* + * The event source is stored as a registry key. + * The maximum length of a registry key is 255 characters. + * http://msdn.microsoft.com/en-us/library/ms724872(v=vs.85).aspx + */ +char event_source[256] = DEFAULT_EVENT_SOURCE; + +/* Prototypes */ +HRESULT DllInstall(BOOL bInstall, LPCWSTR pszCmdLine); +STDAPI DllRegisterServer(void); +STDAPI DllUnregisterServer(void); +BOOL WINAPI DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved); + +/* + * DllInstall --- Passes the command line argument to DLL + */ + +HRESULT +DllInstall(BOOL bInstall, + LPCWSTR pszCmdLine) +{ + if (pszCmdLine && *pszCmdLine != '\0') + wcstombs(event_source, pszCmdLine, sizeof(event_source)); + + /* + * This is an ugly hack due to the strange behavior of "regsvr32 /i". + * + * When installing, regsvr32 calls DllRegisterServer before DllInstall. + * When uninstalling (i.e. "regsvr32 /u /i"), on the other hand, regsvr32 + * calls DllInstall and then DllUnregisterServer as expected. + * + * This strange behavior forces us to specify -n (i.e. "regsvr32 /n /i"). + * Without -n, DllRegisterServer called before DllInstall would mistakenly + * overwrite the default "PostgreSQL" event source registration. + */ + if (bInstall) + DllRegisterServer(); + return S_OK; +} + +/* + * DllRegisterServer --- Instructs DLL to create its registry entries + */ + +STDAPI +DllRegisterServer(void) +{ + HKEY key; + DWORD data; + char buffer[_MAX_PATH]; + char key_name[400]; + + /* Set the name of DLL full path name. */ + if (!GetModuleFileName((HMODULE) g_module, buffer, sizeof(buffer))) + { + MessageBox(NULL, "Could not retrieve DLL filename", "PostgreSQL error", MB_OK | MB_ICONSTOP); + return SELFREG_E_TYPELIB; + } + + /* + * Add PostgreSQL source name as a subkey under the Application key in the + * EventLog registry key. + */ + _snprintf(key_name, sizeof(key_name), + "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s", + event_source); + if (RegCreateKey(HKEY_LOCAL_MACHINE, key_name, &key)) + { + MessageBox(NULL, "Could not create the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP); + return SELFREG_E_TYPELIB; + } + + /* Add the name to the EventMessageFile subkey. */ + if (RegSetValueEx(key, + "EventMessageFile", + 0, + REG_EXPAND_SZ, + (LPBYTE) buffer, + strlen(buffer) + 1)) + { + MessageBox(NULL, "Could not set the event message file.", "PostgreSQL error", MB_OK | MB_ICONSTOP); + return SELFREG_E_TYPELIB; + } + + /* Set the supported event types in the TypesSupported subkey. */ + data = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE; + + if (RegSetValueEx(key, + "TypesSupported", + 0, + REG_DWORD, + (LPBYTE) &data, + sizeof(DWORD))) + { + MessageBox(NULL, "Could not set the supported types.", "PostgreSQL error", MB_OK | MB_ICONSTOP); + return SELFREG_E_TYPELIB; + } + + RegCloseKey(key); + return S_OK; +} + +/* + * DllUnregisterServer --- Instructs DLL to remove only those entries created through DllRegisterServer + */ + +STDAPI +DllUnregisterServer(void) +{ + char key_name[400]; + + /* + * Remove PostgreSQL source name as a subkey under the Application key in + * the EventLog registry key. + */ + + _snprintf(key_name, sizeof(key_name), + "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s", + event_source); + if (RegDeleteKey(HKEY_LOCAL_MACHINE, key_name)) + { + MessageBox(NULL, "Could not delete the registry key.", "PostgreSQL error", MB_OK | MB_ICONSTOP); + return SELFREG_E_TYPELIB; + } + return S_OK; +} + +/* + * DllMain --- is an optional entry point into a DLL. + */ + +BOOL WINAPI +DllMain(HANDLE hModule, + DWORD ul_reason_for_call, + LPVOID lpReserved +) +{ + if (ul_reason_for_call == DLL_PROCESS_ATTACH) + g_module = hModule; + return TRUE; +} diff --git a/src/bin/pgevent/pgevent.def b/src/bin/pgevent/pgevent.def new file mode 100644 index 0000000..6b4d44a --- /dev/null +++ b/src/bin/pgevent/pgevent.def @@ -0,0 +1,5 @@ +; dlltool --output-def pgevent.def pgevent.o pgmsgevent.o +EXPORTS + DllUnregisterServer ; + DllRegisterServer ; + DllInstall ; diff --git a/src/bin/pgevent/pgmsgevent.h b/src/bin/pgevent/pgmsgevent.h new file mode 100644 index 0000000..aba71e4 --- /dev/null +++ b/src/bin/pgevent/pgmsgevent.h @@ -0,0 +1,46 @@ +/* src/bin/pgevent/pgmsgevent.h */ + +/* */ +/* Values are 32 bit values laid out as follows: */ +/* */ +/* 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 */ +/* 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 */ +/* +---+-+-+-----------------------+-------------------------------+ */ +/* |Sev|C|R| Facility | Code | */ +/* +---+-+-+-----------------------+-------------------------------+ */ +/* */ +/* where */ +/* */ +/* Sev - is the severity code */ +/* */ +/* 00 - Success */ +/* 01 - Informational */ +/* 10 - Warning */ +/* 11 - Error */ +/* */ +/* C - is the Customer code flag */ +/* */ +/* R - is a reserved bit */ +/* */ +/* Facility - is the facility code */ +/* */ +/* Code - is the facility's status code */ +/* */ +/* */ +/* Define the facility codes */ +/* */ + + +/* */ +/* Define the severity codes */ +/* */ + + +/* */ +/* MessageId: PGWIN32_EVENTLOG_MSG */ +/* */ +/* MessageText: */ +/* */ +/* %1 */ +/* */ +#define PGWIN32_EVENTLOG_MSG 0x00000000L diff --git a/src/bin/pgevent/pgmsgevent.mc b/src/bin/pgevent/pgmsgevent.mc new file mode 100644 index 0000000..5d16e3e --- /dev/null +++ b/src/bin/pgevent/pgmsgevent.mc @@ -0,0 +1,5 @@ +MessageId=0 +SymbolicName=PGWIN32_EVENTLOG_MSG +Language=English +%1 +. diff --git a/src/bin/pgevent/pgmsgevent.rc b/src/bin/pgevent/pgmsgevent.rc new file mode 100644 index 0000000..e69862e --- /dev/null +++ b/src/bin/pgevent/pgmsgevent.rc @@ -0,0 +1,4 @@ +LANGUAGE 0x9,0x1 +1 11 MSG00001.bin + +#include "win32ver.rc" |