xref: /openbmc/qemu/qga/vss-win32.c (revision c39f95dc)
1 /*
2  * QEMU Guest Agent VSS utility functions
3  *
4  * Copyright Hitachi Data Systems Corp. 2013
5  *
6  * Authors:
7  *  Tomoki Sekiyama   <tomoki.sekiyama@hds.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include <windows.h>
15 #include "qemu/error-report.h"
16 #include "qga/guest-agent-core.h"
17 #include "qga/vss-win32.h"
18 #include "qga/vss-win32/requester.h"
19 
20 #define QGA_VSS_DLL "qga-vss.dll"
21 
22 static HMODULE provider_lib;
23 
24 /* Call a function in qga-vss.dll with the specified name */
25 static HRESULT call_vss_provider_func(const char *func_name)
26 {
27     FARPROC WINAPI func;
28 
29     g_assert(provider_lib);
30 
31     func = GetProcAddress(provider_lib, func_name);
32     if (!func) {
33         char *msg;
34         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
35                       FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
36                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
37                       (char *)&msg, 0, NULL);
38         fprintf(stderr, "failed to load %s from %s: %s",
39                 func_name, QGA_VSS_DLL, msg);
40         LocalFree(msg);
41         return E_FAIL;
42     }
43 
44     return func();
45 }
46 
47 /* Check whether this OS version supports VSS providers */
48 static bool vss_check_os_version(void)
49 {
50     OSVERSIONINFO OSver;
51 
52     OSver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
53     GetVersionEx(&OSver);
54     if ((OSver.dwMajorVersion == 5 && OSver.dwMinorVersion >= 2) ||
55        OSver.dwMajorVersion > 5) {
56         BOOL wow64 = false;
57 #ifndef _WIN64
58         /* Provider doesn't work under WOW64 (32bit agent on 64bit OS) */
59         if (!IsWow64Process(GetCurrentProcess(), &wow64)) {
60             fprintf(stderr, "failed to IsWow64Process (Error: %lx\n)\n",
61                     GetLastError());
62             return false;
63         }
64         if (wow64) {
65             warn_report("Running under WOW64");
66         }
67 #endif
68         return !wow64;
69     }
70     return false;
71 }
72 
73 /* Load qga-vss.dll */
74 bool vss_init(bool init_requester)
75 {
76     if (!vss_check_os_version()) {
77         /* Do nothing if OS doesn't support providers. */
78         fprintf(stderr, "VSS provider is not supported in this OS version: "
79                 "fsfreeze is disabled.\n");
80         return false;
81     }
82 
83     provider_lib = LoadLibraryA(QGA_VSS_DLL);
84     if (!provider_lib) {
85         char *msg;
86         FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
87                       FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
88                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
89                       (char *)&msg, 0, NULL);
90         fprintf(stderr, "failed to load %s: %sfsfreeze is disabled\n",
91                 QGA_VSS_DLL, msg);
92         LocalFree(msg);
93         return false;
94     }
95 
96     if (init_requester) {
97         HRESULT hr = call_vss_provider_func("requester_init");
98         if (FAILED(hr)) {
99             fprintf(stderr, "fsfreeze is disabled.\n");
100             vss_deinit(false);
101             return false;
102         }
103     }
104 
105     return true;
106 }
107 
108 /* Unload qga-provider.dll */
109 void vss_deinit(bool deinit_requester)
110 {
111     if (deinit_requester) {
112         call_vss_provider_func("requester_deinit");
113     }
114     FreeLibrary(provider_lib);
115     provider_lib = NULL;
116 }
117 
118 bool vss_initialized(void)
119 {
120     return !!provider_lib;
121 }
122 
123 int ga_install_vss_provider(void)
124 {
125     HRESULT hr;
126 
127     if (!vss_init(false)) {
128         fprintf(stderr, "Installation of VSS provider is skipped. "
129                 "fsfreeze will be disabled.\n");
130         return 0;
131     }
132     hr = call_vss_provider_func("COMRegister");
133     vss_deinit(false);
134 
135     return SUCCEEDED(hr) ? 0 : EXIT_FAILURE;
136 }
137 
138 void ga_uninstall_vss_provider(void)
139 {
140     if (!vss_init(false)) {
141         fprintf(stderr, "Removal of VSS provider is skipped.\n");
142         return;
143     }
144     call_vss_provider_func("COMUnregister");
145     vss_deinit(false);
146 }
147 
148 /* Call VSS requester and freeze/thaw filesystems and applications */
149 void qga_vss_fsfreeze(int *nr_volume, bool freeze, Error **errp)
150 {
151     const char *func_name = freeze ? "requester_freeze" : "requester_thaw";
152     QGAVSSRequesterFunc func;
153     ErrorSet errset = {
154         .error_setg_win32_wrapper = error_setg_win32_internal,
155         .errp = errp,
156     };
157 
158     g_assert(errp);             /* requester.cpp requires it */
159     func = (QGAVSSRequesterFunc)GetProcAddress(provider_lib, func_name);
160     if (!func) {
161         error_setg_win32(errp, GetLastError(), "failed to load %s from %s",
162                          func_name, QGA_VSS_DLL);
163         return;
164     }
165 
166     func(nr_volume, &errset);
167 }
168