Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20180323' into...
[qemu/ar7.git] / qga / vss-win32.c
blob0199c2a792255a167e7b6f987b088739e8ada092
1 /*
2 * QEMU Guest Agent VSS utility functions
4 * Copyright Hitachi Data Systems Corp. 2013
6 * Authors:
7 * Tomoki Sekiyama <tomoki.sekiyama@hds.com>
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.
13 #include "qemu/osdep.h"
14 #include <windows.h>
15 #include "qapi/error.h"
16 #include "qemu/error-report.h"
17 #include "qga/guest-agent-core.h"
18 #include "qga/vss-win32.h"
19 #include "qga/vss-win32/requester.h"
21 #define QGA_VSS_DLL "qga-vss.dll"
23 static HMODULE provider_lib;
25 /* Call a function in qga-vss.dll with the specified name */
26 static HRESULT call_vss_provider_func(const char *func_name)
28 FARPROC WINAPI func;
30 g_assert(provider_lib);
32 func = GetProcAddress(provider_lib, func_name);
33 if (!func) {
34 char *msg;
35 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
36 FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
37 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
38 (char *)&msg, 0, NULL);
39 fprintf(stderr, "failed to load %s from %s: %s",
40 func_name, QGA_VSS_DLL, msg);
41 LocalFree(msg);
42 return E_FAIL;
45 return func();
48 /* Check whether this OS version supports VSS providers */
49 static bool vss_check_os_version(void)
51 OSVERSIONINFO OSver;
53 OSver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
54 GetVersionEx(&OSver);
55 if ((OSver.dwMajorVersion == 5 && OSver.dwMinorVersion >= 2) ||
56 OSver.dwMajorVersion > 5) {
57 BOOL wow64 = false;
58 #ifndef _WIN64
59 /* Provider doesn't work under WOW64 (32bit agent on 64bit OS) */
60 if (!IsWow64Process(GetCurrentProcess(), &wow64)) {
61 fprintf(stderr, "failed to IsWow64Process (Error: %lx\n)\n",
62 GetLastError());
63 return false;
65 if (wow64) {
66 warn_report("Running under WOW64");
68 #endif
69 return !wow64;
71 return false;
74 /* Load qga-vss.dll */
75 bool vss_init(bool init_requester)
77 if (!vss_check_os_version()) {
78 /* Do nothing if OS doesn't support providers. */
79 fprintf(stderr, "VSS provider is not supported in this OS version: "
80 "fsfreeze is disabled.\n");
81 return false;
84 provider_lib = LoadLibraryA(QGA_VSS_DLL);
85 if (!provider_lib) {
86 char *msg;
87 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
88 FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
89 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
90 (char *)&msg, 0, NULL);
91 fprintf(stderr, "failed to load %s: %sfsfreeze is disabled\n",
92 QGA_VSS_DLL, msg);
93 LocalFree(msg);
94 return false;
97 if (init_requester) {
98 HRESULT hr = call_vss_provider_func("requester_init");
99 if (FAILED(hr)) {
100 fprintf(stderr, "fsfreeze is disabled.\n");
101 vss_deinit(false);
102 return false;
106 return true;
109 /* Unload qga-provider.dll */
110 void vss_deinit(bool deinit_requester)
112 if (deinit_requester) {
113 call_vss_provider_func("requester_deinit");
115 FreeLibrary(provider_lib);
116 provider_lib = NULL;
119 bool vss_initialized(void)
121 return !!provider_lib;
124 int ga_install_vss_provider(void)
126 HRESULT hr;
128 if (!vss_init(false)) {
129 fprintf(stderr, "Installation of VSS provider is skipped. "
130 "fsfreeze will be disabled.\n");
131 return 0;
133 hr = call_vss_provider_func("COMRegister");
134 vss_deinit(false);
136 return SUCCEEDED(hr) ? 0 : EXIT_FAILURE;
139 void ga_uninstall_vss_provider(void)
141 if (!vss_init(false)) {
142 fprintf(stderr, "Removal of VSS provider is skipped.\n");
143 return;
145 call_vss_provider_func("COMUnregister");
146 vss_deinit(false);
149 /* Call VSS requester and freeze/thaw filesystems and applications */
150 void qga_vss_fsfreeze(int *nr_volume, bool freeze, Error **errp)
152 const char *func_name = freeze ? "requester_freeze" : "requester_thaw";
153 QGAVSSRequesterFunc func;
154 ErrorSet errset = {
155 .error_setg_win32_wrapper = error_setg_win32_internal,
156 .errp = errp,
159 g_assert(errp); /* requester.cpp requires it */
160 func = (QGAVSSRequesterFunc)GetProcAddress(provider_lib, func_name);
161 if (!func) {
162 error_setg_win32(errp, GetLastError(), "failed to load %s from %s",
163 func_name, QGA_VSS_DLL);
164 return;
167 func(nr_volume, &errset);