Browse Source

Log --command option errors to the event log

This supplements the non-zero exit-code.

Signed-off-by: Selva Nair <selva.nair@gmail.com>
pull/255/head openvpn-install-2.4.6-I601
Selva Nair 7 years ago committed by Samuli Seppänen
parent
commit
343643657f
  1. 54
      main.c
  2. 1
      main.h
  3. 1
      options.h

54
main.c

@ -126,14 +126,29 @@ NotifyRunningInstance()
PrintDebug(L"Instance 2: called with action %d : %s", o.action, o.action_arg); PrintDebug(L"Instance 2: called with action %d : %s", o.action, o.action_arg);
if (!SendMessageTimeout (hwnd_master, WM_COPYDATA, 0, if (!SendMessageTimeout (hwnd_master, WM_COPYDATA, 0,
(LPARAM) &config_data, 0, timeout, NULL)) (LPARAM) &config_data, 0, timeout, NULL))
{
DWORD error = GetLastError();
if (error == ERROR_TIMEOUT)
{
exit_code = OVPN_EXITCODE_TIMEOUT; exit_code = OVPN_EXITCODE_TIMEOUT;
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Sending command to running instance timed out.");
} }
else else
{ {
PrintDebug(L"Instance 2: Previous instance not yet ready to accept comamnds"); exit_code = OVPN_EXITCODE_ERROR;
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Sending command to running instance failed (error = %lu).",
error);
}
}
}
else
{
/* An instance is already running but its main window not yet initialized */
exit_code = OVPN_EXITCODE_NOTREADY; exit_code = OVPN_EXITCODE_NOTREADY;
MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Previous instance not yet ready to accept commands. "
"Try again later.");
} }
PrintDebug(L"Instance 2: Returning exit code %d", exit_code);
return exit_code; return exit_code;
} }
@ -235,7 +250,7 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance,
} }
else if (o.action) else if (o.action)
{ {
PrintDebug(L"Instance 1: Called with --command when no previous instance available"); MsgToEventLog(EVENTLOG_ERROR_TYPE, L"Called with --command when no previous instance available");
exit(OVPN_EXITCODE_ERROR); exit(OVPN_EXITCODE_ERROR);
} }
@ -312,6 +327,8 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance,
CloseSemaphore(o.session_semaphore); CloseSemaphore(o.session_semaphore);
o.session_semaphore = NULL; /* though we're going to die.. */ o.session_semaphore = NULL; /* though we're going to die.. */
if (o.event_log)
DeregisterEventSource(o.event_log);
/* The program return-value is 0 - The value that PostQuitMessage() gave */ /* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam; return messages.wParam;
@ -466,8 +483,10 @@ HandleCopyDataMessage(const COPYDATASTRUCT *copy_data)
} }
else else
{ {
PrintDebug (L"WM_COPYDATA message ignored. (dwData: %lu, cbData: %lu)", MsgToEventLog(EVENTLOG_ERROR_TYPE,
copy_data->dwData, copy_data->cbData); L"Unknown WM_COPYDATA message ignored. (dwData: %lu, cbData: %lu, lpData: %s)",
copy_data->dwData, copy_data->cbData, copy_data->lpData);
return FALSE;
} }
return TRUE; /* indicate we handled the message */ return TRUE; /* indicate we handled the message */
} }
@ -835,3 +854,28 @@ DWORD GetDllVersion(LPCTSTR lpszDllName)
} }
return dwVersion; return dwVersion;
} }
void
MsgToEventLog(WORD type, wchar_t *format, ...)
{
const wchar_t *msg[2];
wchar_t buf[256];
int size = _countof(buf);
if (!o.event_log)
{
o.event_log = RegisterEventSource(NULL, TEXT(PACKAGE_NAME));
if (!o.event_log)
return;
}
va_list args;
va_start(args, format);
if (vswprintf(buf, size-1, format, args) == -1)
return;
buf[size - 1] = '\0';
msg[0] = TEXT(PACKAGE_NAME);
msg[1] = buf;
ReportEventW(o.event_log, type, 0, 0, NULL, 2, 0, msg, NULL);
}

1
main.h

@ -131,5 +131,6 @@ void PrintDebugMsg(TCHAR *msg);
DWORD GetDllVersion(LPCTSTR lpszDllName); DWORD GetDllVersion(LPCTSTR lpszDllName);
#define DPI_SCALE(x) MulDiv(x, o.dpi_scale, 100) #define DPI_SCALE(x) MulDiv(x, o.dpi_scale, 100)
void MsgToEventLog(WORD type, wchar_t *format, ...);
#endif #endif

1
options.h

@ -187,6 +187,7 @@ typedef struct {
int action; /* action to send to a running instance */ int action; /* action to send to a running instance */
TCHAR *action_arg; TCHAR *action_arg;
HANDLE session_semaphore; HANDLE session_semaphore;
HANDLE event_log;
} options_t; } options_t;
void InitOptions(options_t *); void InitOptions(options_t *);

Loading…
Cancel
Save