blob: f444a25a70e40382aca8e47f6be20d1a606e7c7d [file] [log] [blame]
Tomoki Sekiyama64c00312013-08-07 11:40:25 -04001/*
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
Peter Maydell4459bf32016-01-29 17:49:58 +000013#include "qemu/osdep.h"
Tomoki Sekiyama64c00312013-08-07 11:40:25 -040014#include <windows.h>
Markus Armbrustere688df62018-02-01 12:18:31 +010015#include "qapi/error.h"
Alistair Francis2ab4b132017-09-11 12:52:50 -070016#include "qemu/error-report.h"
Michael S. Tsirkindc032722018-05-03 22:50:57 +030017#include "guest-agent-core.h"
18#include "vss-win32.h"
19#include "vss-win32/requester.h"
Tomoki Sekiyama64c00312013-08-07 11:40:25 -040020
21#define QGA_VSS_DLL "qga-vss.dll"
22
23static HMODULE provider_lib;
24
25/* Call a function in qga-vss.dll with the specified name */
26static HRESULT call_vss_provider_func(const char *func_name)
27{
28 FARPROC WINAPI func;
29
30 g_assert(provider_lib);
31
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;
43 }
44
45 return func();
46}
47
48/* Check whether this OS version supports VSS providers */
49static bool vss_check_os_version(void)
50{
51 OSVERSIONINFO OSver;
52
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;
64 }
65 if (wow64) {
Alistair Francis2ab4b132017-09-11 12:52:50 -070066 warn_report("Running under WOW64");
Tomoki Sekiyama64c00312013-08-07 11:40:25 -040067 }
68#endif
69 return !wow64;
70 }
71 return false;
72}
73
74/* Load qga-vss.dll */
75bool vss_init(bool init_requester)
76{
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;
82 }
83
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;
95 }
96
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;
103 }
104 }
105
106 return true;
107}
108
109/* Unload qga-provider.dll */
110void vss_deinit(bool deinit_requester)
111{
112 if (deinit_requester) {
113 call_vss_provider_func("requester_deinit");
114 }
115 FreeLibrary(provider_lib);
116 provider_lib = NULL;
117}
118
119bool vss_initialized(void)
120{
121 return !!provider_lib;
122}
123
Tomoki Sekiyamaf311f2c2013-08-07 11:40:32 -0400124int ga_install_vss_provider(void)
125{
126 HRESULT hr;
127
128 if (!vss_init(false)) {
129 fprintf(stderr, "Installation of VSS provider is skipped. "
130 "fsfreeze will be disabled.\n");
131 return 0;
132 }
133 hr = call_vss_provider_func("COMRegister");
134 vss_deinit(false);
135
136 return SUCCEEDED(hr) ? 0 : EXIT_FAILURE;
137}
138
139void ga_uninstall_vss_provider(void)
140{
141 if (!vss_init(false)) {
142 fprintf(stderr, "Removal of VSS provider is skipped.\n");
143 return;
144 }
145 call_vss_provider_func("COMUnregister");
146 vss_deinit(false);
147}
148
Tomoki Sekiyama64c00312013-08-07 11:40:25 -0400149/* Call VSS requester and freeze/thaw filesystems and applications */
Chen Hanxiao0692b032018-08-31 14:22:50 +0800150void qga_vss_fsfreeze(int *nr_volume, bool freeze,
151 strList *mountpoints, Error **errp)
Tomoki Sekiyama64c00312013-08-07 11:40:25 -0400152{
153 const char *func_name = freeze ? "requester_freeze" : "requester_thaw";
154 QGAVSSRequesterFunc func;
155 ErrorSet errset = {
Michael Rothe55eb802016-02-24 18:14:52 -0600156 .error_setg_win32_wrapper = error_setg_win32_internal,
Markus Armbrustere7cf59e2015-06-19 20:44:54 +0200157 .errp = errp,
Tomoki Sekiyama64c00312013-08-07 11:40:25 -0400158 };
159
Markus Armbruster08e646402015-06-20 09:33:56 +0200160 g_assert(errp); /* requester.cpp requires it */
Tomoki Sekiyama64c00312013-08-07 11:40:25 -0400161 func = (QGAVSSRequesterFunc)GetProcAddress(provider_lib, func_name);
162 if (!func) {
Markus Armbruster77dbc812014-05-02 13:26:30 +0200163 error_setg_win32(errp, GetLastError(), "failed to load %s from %s",
Tomoki Sekiyama64c00312013-08-07 11:40:25 -0400164 func_name, QGA_VSS_DLL);
165 return;
166 }
167
Chen Hanxiao0692b032018-08-31 14:22:50 +0800168 func(nr_volume, mountpoints, &errset);
Tomoki Sekiyama64c00312013-08-07 11:40:25 -0400169}