1 // Copyright (c) 2014-2018 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <qt/winshutdownmonitor.h>
6 
7 #if defined(Q_OS_WIN)
8 #include <shutdown.h>
9 #include <util/system.h>
10 
11 #include <windows.h>
12 
13 #include <QDebug>
14 
15 #include <openssl/rand.h>
16 
17 // If we don't want a message to be processed by Qt, return true and set result to
18 // the value that the window procedure should return. Otherwise return false.
nativeEventFilter(const QByteArray & eventType,void * pMessage,long * pnResult)19 bool WinShutdownMonitor::nativeEventFilter(const QByteArray &eventType, void *pMessage, long *pnResult)
20 {
21        Q_UNUSED(eventType);
22 
23        MSG *pMsg = static_cast<MSG *>(pMessage);
24 
25        // Seed OpenSSL PRNG with Windows event data (e.g.  mouse movements and other user interactions)
26        if (RAND_event(pMsg->message, pMsg->wParam, pMsg->lParam) == 0) {
27             // Warn only once as this is performance-critical
28             static bool warned = false;
29             if (!warned) {
30                 LogPrintf("%s: OpenSSL RAND_event() failed to seed OpenSSL PRNG with enough data.\n", __func__);
31                 warned = true;
32             }
33        }
34 
35        switch(pMsg->message)
36        {
37            case WM_QUERYENDSESSION:
38            {
39                // Initiate a client shutdown after receiving a WM_QUERYENDSESSION and block
40                // Windows session end until we have finished client shutdown.
41                StartShutdown();
42                *pnResult = FALSE;
43                return true;
44            }
45 
46            case WM_ENDSESSION:
47            {
48                *pnResult = FALSE;
49                return true;
50            }
51        }
52 
53        return false;
54 }
55 
registerShutdownBlockReason(const QString & strReason,const HWND & mainWinId)56 void WinShutdownMonitor::registerShutdownBlockReason(const QString& strReason, const HWND& mainWinId)
57 {
58     typedef BOOL (WINAPI *PSHUTDOWNBRCREATE)(HWND, LPCWSTR);
59     PSHUTDOWNBRCREATE shutdownBRCreate = (PSHUTDOWNBRCREATE)GetProcAddress(GetModuleHandleA("User32.dll"), "ShutdownBlockReasonCreate");
60     if (shutdownBRCreate == nullptr) {
61         qWarning() << "registerShutdownBlockReason: GetProcAddress for ShutdownBlockReasonCreate failed";
62         return;
63     }
64 
65     if (shutdownBRCreate(mainWinId, strReason.toStdWString().c_str()))
66         qWarning() << "registerShutdownBlockReason: Successfully registered: " + strReason;
67     else
68         qWarning() << "registerShutdownBlockReason: Failed to register: " + strReason;
69 }
70 #endif
71