Tomoki Sekiyama | 64c0031 | 2013-08-07 11:40:25 -0400 | [diff] [blame] | 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 | |
Peter Maydell | 4459bf3 | 2016-01-29 17:49:58 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Tomoki Sekiyama | 64c0031 | 2013-08-07 11:40:25 -0400 | [diff] [blame] | 14 | #include <windows.h> |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 15 | #include "qapi/error.h" |
Alistair Francis | 2ab4b13 | 2017-09-11 12:52:50 -0700 | [diff] [blame] | 16 | #include "qemu/error-report.h" |
Michael S. Tsirkin | dc03272 | 2018-05-03 22:50:57 +0300 | [diff] [blame] | 17 | #include "guest-agent-core.h" |
| 18 | #include "vss-win32.h" |
| 19 | #include "vss-win32/requester.h" |
Tomoki Sekiyama | 64c0031 | 2013-08-07 11:40:25 -0400 | [diff] [blame] | 20 | |
| 21 | #define QGA_VSS_DLL "qga-vss.dll" |
| 22 | |
| 23 | static HMODULE provider_lib; |
| 24 | |
| 25 | /* Call a function in qga-vss.dll with the specified name */ |
| 26 | static 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 */ |
| 49 | static 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 Francis | 2ab4b13 | 2017-09-11 12:52:50 -0700 | [diff] [blame] | 66 | warn_report("Running under WOW64"); |
Tomoki Sekiyama | 64c0031 | 2013-08-07 11:40:25 -0400 | [diff] [blame] | 67 | } |
| 68 | #endif |
| 69 | return !wow64; |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | /* Load qga-vss.dll */ |
| 75 | bool 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 */ |
| 110 | void 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 | |
| 119 | bool vss_initialized(void) |
| 120 | { |
| 121 | return !!provider_lib; |
| 122 | } |
| 123 | |
Tomoki Sekiyama | f311f2c | 2013-08-07 11:40:32 -0400 | [diff] [blame] | 124 | int 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 | |
| 139 | void 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 Sekiyama | 64c0031 | 2013-08-07 11:40:25 -0400 | [diff] [blame] | 149 | /* Call VSS requester and freeze/thaw filesystems and applications */ |
Chen Hanxiao | 0692b03 | 2018-08-31 14:22:50 +0800 | [diff] [blame] | 150 | void qga_vss_fsfreeze(int *nr_volume, bool freeze, |
| 151 | strList *mountpoints, Error **errp) |
Tomoki Sekiyama | 64c0031 | 2013-08-07 11:40:25 -0400 | [diff] [blame] | 152 | { |
| 153 | const char *func_name = freeze ? "requester_freeze" : "requester_thaw"; |
| 154 | QGAVSSRequesterFunc func; |
| 155 | ErrorSet errset = { |
Michael Roth | e55eb80 | 2016-02-24 18:14:52 -0600 | [diff] [blame] | 156 | .error_setg_win32_wrapper = error_setg_win32_internal, |
Markus Armbruster | e7cf59e | 2015-06-19 20:44:54 +0200 | [diff] [blame] | 157 | .errp = errp, |
Tomoki Sekiyama | 64c0031 | 2013-08-07 11:40:25 -0400 | [diff] [blame] | 158 | }; |
| 159 | |
Markus Armbruster | 08e64640 | 2015-06-20 09:33:56 +0200 | [diff] [blame] | 160 | g_assert(errp); /* requester.cpp requires it */ |
Tomoki Sekiyama | 64c0031 | 2013-08-07 11:40:25 -0400 | [diff] [blame] | 161 | func = (QGAVSSRequesterFunc)GetProcAddress(provider_lib, func_name); |
| 162 | if (!func) { |
Markus Armbruster | 77dbc81 | 2014-05-02 13:26:30 +0200 | [diff] [blame] | 163 | error_setg_win32(errp, GetLastError(), "failed to load %s from %s", |
Tomoki Sekiyama | 64c0031 | 2013-08-07 11:40:25 -0400 | [diff] [blame] | 164 | func_name, QGA_VSS_DLL); |
| 165 | return; |
| 166 | } |
| 167 | |
Chen Hanxiao | 0692b03 | 2018-08-31 14:22:50 +0800 | [diff] [blame] | 168 | func(nr_volume, mountpoints, &errset); |
Tomoki Sekiyama | 64c0031 | 2013-08-07 11:40:25 -0400 | [diff] [blame] | 169 | } |