| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | LxssUserCallback.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains kernel->user callback function declarations. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | #include <mutex> |
| 17 | #include <wil/resource.h> |
| 18 | |
| 19 | using LXSS_USER_CALLBACK = std::function<NTSTATUS(PVOID CallbackBuffer, ULONG_PTR CallbackBufferSize)>; |
| 20 | |
| 21 | class LxssUserCallback |
| 22 | { |
| 23 | public: |
| 24 | /// <summary> |
| 25 | /// Destructor. |
| 26 | /// </summary> |
| 27 | ~LxssUserCallback(); |
| 28 | |
| 29 | /// <summary> |
| 30 | /// Register a new user callback handler. |
| 31 | /// </summary> |
| 32 | static std::unique_ptr<LxssUserCallback> Register( |
| 33 | _In_ HANDLE Handle, _In_ LXBUS_USER_CALLBACK_TYPE CallbackType, _In_ const LXSS_USER_CALLBACK& Callback, _In_ ULONG OutputBufferSize); |
| 34 | |
| 35 | private: |
| 36 | /// <summary> |
| 37 | /// No default constructor. |
| 38 | /// </summary> |
| 39 | LxssUserCallback() = delete; |
| 40 | /// <summary> |
| 41 | /// No copy constructor. |
| 42 | /// </summary> |
| 43 | LxssUserCallback(const LxssUserCallback&) = delete; |
| 44 | |
| 45 | /// <summary> |
| 46 | /// Private constructor. |
| 47 | /// </summary> |
| 48 | LxssUserCallback(_In_ HANDLE Handle, _In_ LXBUS_USER_CALLBACK_TYPE CallbackType, _In_ const LXSS_USER_CALLBACK& Callback, _In_ ULONG OutputBufferSize); |
| 49 | |
| 50 | /// <summary> |
| 51 | /// Queue the request with the kernel driver. |
| 52 | /// </summary> |
| 53 | VOID QueueRequest(); |
| 54 | |
| 55 | /// <summary> |
| 56 | /// Threadpool callback handler. |
| 57 | /// </summary> |
| 58 | VOID ThreadpoolCallback(_Inout_ PTP_CALLBACK_INSTANCE Instance, _Inout_ PTP_WAIT Wait, _In_ TP_WAIT_RESULT WaitResult); |
| 59 | |
| 60 | /// <summary> |
| 61 | /// Threadpool callback entry. |
| 62 | /// </summary> |
| 63 | static VOID CALLBACK ThreadpoolCallbackProxy( |
| 64 | _Inout_ PTP_CALLBACK_INSTANCE Instance, _Inout_opt_ PVOID Context, _Inout_ PTP_WAIT Wait, _In_ TP_WAIT_RESULT WaitResult); |
| 65 | |
| 66 | /// <summary> |
| 67 | /// Output buffer. |
| 68 | /// </summary> |
| 69 | std::vector<BYTE> m_buffer; |
| 70 | |
| 71 | /// <summary> |
| 72 | /// Reference to callback function. |
| 73 | /// </summary> |
| 74 | LXSS_USER_CALLBACK m_callback; |
| 75 | |
| 76 | /// <summary> |
| 77 | /// Set when instance is destructing. Should be accessed holding m_lock. |
| 78 | /// </summary> |
| 79 | bool m_exiting; |
| 80 | |
| 81 | /// <summary> |
| 82 | /// The type of callback to register. |
| 83 | /// </summary> |
| 84 | LXBUS_USER_CALLBACK_TYPE m_callbackType; |
| 85 | |
| 86 | /// <summary> |
| 87 | /// Event triggered when asynchronous callback IOCTL is completed. |
| 88 | /// </summary> |
| 89 | wil::unique_event m_event; |
| 90 | |
| 91 | /// <summary> |
| 92 | /// Stores handle to use to send IOCTL to kernel driver. |
| 93 | /// </summary> |
| 94 | wil::unique_handle m_handle; |
| 95 | |
| 96 | /// <summary> |
| 97 | /// IO status. |
| 98 | /// </summary> |
| 99 | IO_STATUS_BLOCK m_ioStatus; |
| 100 | |
| 101 | /// <summary> |
| 102 | /// Synchronize access to certain fields. |
| 103 | /// </summary> |
| 104 | std::mutex m_lock; |
| 105 | |
| 106 | /// <summary> |
| 107 | /// Threadpool IO handle. |
| 108 | /// </summary> |
| 109 | // N.B. Keep this as the last member so that it is the first to be |
| 110 | // destructed. This will ensure that if the threadpool callback |
| 111 | // races with instance destruction, all of the fields will be valid. |
| 112 | // At completion of this member's destruction, any outstanding |
| 113 | // threadpool thread should have completed and the callback |
| 114 | // will have been unregistered. |
| 115 | wil::unique_threadpool_wait m_threadpoolWait; |
| 116 | }; |