]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd_wnbd/wnbd_wmi.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / tools / rbd_wnbd / wnbd_wmi.h
1 /*
2 * Ceph - scalable distributed file system
3 *
4 * Copyright (c) 2019 SUSE LLC
5 * Copyright (C) 2022 Cloudbase Solutions
6 *
7 * This is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License version 2.1, as published by the Free Software
10 * Foundation. See file COPYING.
11 *
12 */
13
14 #pragma once
15 #include <comutil.h>
16
17 #define _WIN32_DCOM
18 #include <wbemcli.h>
19
20 #include <string>
21 #include <vector>
22
23 #include "common/ceph_mutex.h"
24
25 // Convenience helper for initializing and cleaning up the
26 // Windows COM library using "COINIT_MULTITHREADED" concurrency mode.
27 // Any WMI objects (including connections, event subscriptions, etc)
28 // must be released before the COM library gets closed.
29 class COMBootstrapper
30 {
31 private:
32 bool initialized = false;
33
34 ceph::mutex init_lock = ceph::make_mutex("COMBootstrapper::InitLocker");
35
36 public:
37 HRESULT initialize();
38 void cleanup();
39
40 ~COMBootstrapper()
41 {
42 cleanup();
43 }
44 };
45
46 class WmiConnection
47 {
48 private:
49 std::wstring ns;
50 public:
51 IWbemLocator* wbem_loc;
52 IWbemServices* wbem_svc;
53
54 WmiConnection(std::wstring ns)
55 : ns(ns)
56 , wbem_loc(nullptr)
57 , wbem_svc(nullptr)
58 {
59 }
60 ~WmiConnection()
61 {
62 close();
63 }
64
65 HRESULT initialize();
66 void close();
67 };
68
69 HRESULT get_property_str(
70 IWbemClassObject* cls_obj,
71 const std::wstring& property,
72 std::wstring& value);
73 HRESULT get_property_int(
74 IWbemClassObject* cls_obj,
75 const std::wstring& property,
76 uint32_t& value);
77
78 class WmiSubscription
79 {
80 private:
81 std::wstring query;
82
83 WmiConnection conn;
84 IEnumWbemClassObject *event_enum;
85
86 public:
87 WmiSubscription(std::wstring ns, std::wstring query)
88 : query(query)
89 , conn(WmiConnection(ns))
90 , event_enum(nullptr)
91 {
92 }
93 ~WmiSubscription()
94 {
95 close();
96 }
97
98 HRESULT initialize();
99 void close();
100
101 // IEnumWbemClassObject::Next wrapper
102 HRESULT next(
103 long timeout,
104 ULONG count,
105 IWbemClassObject **objects,
106 ULONG *returned);
107 };
108
109 WmiSubscription subscribe_wnbd_adapter_events(uint32_t interval);