docs/devel/loads-stores.rst: Document our various load and store APIs
[qemu.git] / qga / vss-win32.c
blobdcb27567bb255161cd3509a0388a3b7eca8efa4a
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 "qemu/error-report.h"
16 #include "qga/guest-agent-core.h"
17 #include "qga/vss-win32.h"
18 #include "qga/vss-win32/requester.h"
20 #define QGA_VSS_DLL "qga-vss.dll"
22 static HMODULE provider_lib;
24 /* Call a function in qga-vss.dll with the specified name */
25 static HRESULT call_vss_provider_func(const char *func_name)
27 FARPROC WINAPI func;
29 g_assert(provider_lib);
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;
44 return func();
47 /* Check whether this OS version supports VSS providers */
48 static bool vss_check_os_version(void)
50 OSVERSIONINFO OSver;
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;
64 if (wow64) {
65 warn_report("Running under WOW64");
67 #endif
68 return !wow64;
70 return false;
73 /* Load qga-vss.dll */
74 bool vss_init(bool init_requester)
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;
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;
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;
105 return true;
108 /* Unload qga-provider.dll */
109 void vss_deinit(bool deinit_requester)
111 if (deinit_requester) {
112 call_vss_provider_func("requester_deinit");
114 FreeLibrary(provider_lib);
115 provider_lib = NULL;
118 bool vss_initialized(void)
120 return !!provider_lib;
123 int ga_install_vss_provider(void)
125 HRESULT hr;
127 if (!vss_init(false)) {
128 fprintf(stderr, "Installation of VSS provider is skipped. "
129 "fsfreeze will be disabled.\n");
130 return 0;
132 hr = call_vss_provider_func("COMRegister");
133 vss_deinit(false);
135 return SUCCEEDED(hr) ? 0 : EXIT_FAILURE;
138 void ga_uninstall_vss_provider(void)
140 if (!vss_init(false)) {
141 fprintf(stderr, "Removal of VSS provider is skipped.\n");
142 return;
144 call_vss_provider_func("COMUnregister");
145 vss_deinit(false);
148 /* Call VSS requester and freeze/thaw filesystems and applications */
149 void qga_vss_fsfreeze(int *nr_volume, bool freeze, Error **errp)
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,
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;
166 func(nr_volume, &errset);