Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Guest Agent helpers for win32 service management |
| 3 | * |
| 4 | * Copyright IBM Corp. 2012 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Gal Hammer <ghammer@redhat.com> |
| 8 | * Michael Roth <mdroth@linux.vnet.ibm.com> |
| 9 | * |
| 10 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 11 | * See the COPYING file in the top-level directory. |
| 12 | */ |
Peter Maydell | 4459bf3 | 2016-01-29 17:49:58 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 14 | #include <windows.h> |
| 15 | #include "qga/service-win32.h" |
| 16 | |
| 17 | static int printf_win_error(const char *text) |
| 18 | { |
| 19 | DWORD err = GetLastError(); |
| 20 | char *message; |
| 21 | int n; |
| 22 | |
| 23 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 24 | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
| 25 | NULL, |
| 26 | err, |
| 27 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 28 | (char *)&message, 0, |
| 29 | NULL); |
Laszlo Ersek | febf1c4 | 2013-07-03 15:14:08 +0200 | [diff] [blame] | 30 | n = fprintf(stderr, "%s. (Error: %d) %s", text, (int)err, message); |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 31 | LocalFree(message); |
| 32 | |
| 33 | return n; |
| 34 | } |
| 35 | |
Laszlo Ersek | 340d51d | 2013-07-03 15:14:10 +0200 | [diff] [blame] | 36 | /* Windows command line escaping. Based on |
| 37 | * <http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx> and |
| 38 | * <http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft%28v=vs.85%29.aspx>. |
| 39 | * |
| 40 | * The caller is responsible for initializing @buffer; prior contents are lost. |
| 41 | */ |
| 42 | static const char *win_escape_arg(const char *to_escape, GString *buffer) |
| 43 | { |
| 44 | size_t backslash_count; |
| 45 | const char *c; |
| 46 | |
| 47 | /* open with a double quote */ |
| 48 | g_string_assign(buffer, "\""); |
| 49 | |
| 50 | backslash_count = 0; |
| 51 | for (c = to_escape; *c != '\0'; ++c) { |
| 52 | switch (*c) { |
| 53 | case '\\': |
| 54 | /* The meaning depends on the first non-backslash character coming |
| 55 | * up. |
| 56 | */ |
| 57 | ++backslash_count; |
| 58 | break; |
| 59 | |
| 60 | case '"': |
| 61 | /* We must escape each pending backslash, then escape the double |
| 62 | * quote. This creates a case of "odd number of backslashes [...] |
| 63 | * followed by a double quotation mark". |
| 64 | */ |
| 65 | while (backslash_count) { |
| 66 | --backslash_count; |
| 67 | g_string_append(buffer, "\\\\"); |
| 68 | } |
| 69 | g_string_append(buffer, "\\\""); |
| 70 | break; |
| 71 | |
| 72 | default: |
| 73 | /* Any pending backslashes are without special meaning, flush them. |
| 74 | * "Backslashes are interpreted literally, unless they immediately |
| 75 | * precede a double quotation mark." |
| 76 | */ |
| 77 | while (backslash_count) { |
| 78 | --backslash_count; |
| 79 | g_string_append_c(buffer, '\\'); |
| 80 | } |
| 81 | g_string_append_c(buffer, *c); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* We're about to close with a double quote in string delimiter role. |
| 86 | * Double all pending backslashes, creating a case of "even number of |
| 87 | * backslashes [...] followed by a double quotation mark". |
| 88 | */ |
| 89 | while (backslash_count) { |
| 90 | --backslash_count; |
| 91 | g_string_append(buffer, "\\\\"); |
| 92 | } |
| 93 | g_string_append_c(buffer, '"'); |
| 94 | |
| 95 | return buffer->str; |
| 96 | } |
| 97 | |
Laszlo Ersek | a839ee7 | 2013-05-18 06:31:53 +0200 | [diff] [blame] | 98 | int ga_install_service(const char *path, const char *logfile, |
| 99 | const char *state_dir) |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 100 | { |
Laszlo Ersek | 108365f | 2013-07-03 15:14:09 +0200 | [diff] [blame] | 101 | int ret = EXIT_FAILURE; |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 102 | SC_HANDLE manager; |
| 103 | SC_HANDLE service; |
Laszlo Ersek | a880845 | 2013-05-18 06:31:52 +0200 | [diff] [blame] | 104 | TCHAR module_fname[MAX_PATH]; |
Laszlo Ersek | 340d51d | 2013-07-03 15:14:10 +0200 | [diff] [blame] | 105 | GString *esc; |
Laszlo Ersek | a880845 | 2013-05-18 06:31:52 +0200 | [diff] [blame] | 106 | GString *cmdline; |
Laszlo Ersek | 108365f | 2013-07-03 15:14:09 +0200 | [diff] [blame] | 107 | SERVICE_DESCRIPTION desc = { (char *)QGA_SERVICE_DESCRIPTION }; |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 108 | |
Laszlo Ersek | a880845 | 2013-05-18 06:31:52 +0200 | [diff] [blame] | 109 | if (GetModuleFileName(NULL, module_fname, MAX_PATH) == 0) { |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 110 | printf_win_error("No full path to service's executable"); |
| 111 | return EXIT_FAILURE; |
| 112 | } |
| 113 | |
Laszlo Ersek | 340d51d | 2013-07-03 15:14:10 +0200 | [diff] [blame] | 114 | esc = g_string_new(""); |
| 115 | cmdline = g_string_new(""); |
| 116 | |
| 117 | g_string_append_printf(cmdline, "%s -d", |
| 118 | win_escape_arg(module_fname, esc)); |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 119 | |
| 120 | if (path) { |
Laszlo Ersek | 340d51d | 2013-07-03 15:14:10 +0200 | [diff] [blame] | 121 | g_string_append_printf(cmdline, " -p %s", win_escape_arg(path, esc)); |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 122 | } |
| 123 | if (logfile) { |
Laszlo Ersek | 340d51d | 2013-07-03 15:14:10 +0200 | [diff] [blame] | 124 | g_string_append_printf(cmdline, " -l %s -v", |
| 125 | win_escape_arg(logfile, esc)); |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 126 | } |
Laszlo Ersek | a839ee7 | 2013-05-18 06:31:53 +0200 | [diff] [blame] | 127 | if (state_dir) { |
Laszlo Ersek | 340d51d | 2013-07-03 15:14:10 +0200 | [diff] [blame] | 128 | g_string_append_printf(cmdline, " -t %s", |
| 129 | win_escape_arg(state_dir, esc)); |
Laszlo Ersek | a839ee7 | 2013-05-18 06:31:53 +0200 | [diff] [blame] | 130 | } |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 131 | |
Laszlo Ersek | a880845 | 2013-05-18 06:31:52 +0200 | [diff] [blame] | 132 | g_debug("service's cmdline: %s", cmdline->str); |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 133 | |
| 134 | manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); |
| 135 | if (manager == NULL) { |
| 136 | printf_win_error("No handle to service control manager"); |
Laszlo Ersek | 108365f | 2013-07-03 15:14:09 +0200 | [diff] [blame] | 137 | goto out_strings; |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | service = CreateService(manager, QGA_SERVICE_NAME, QGA_SERVICE_DISPLAY_NAME, |
| 141 | SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, |
Laszlo Ersek | a880845 | 2013-05-18 06:31:52 +0200 | [diff] [blame] | 142 | SERVICE_ERROR_NORMAL, cmdline->str, NULL, NULL, NULL, NULL, NULL); |
Laszlo Ersek | 108365f | 2013-07-03 15:14:09 +0200 | [diff] [blame] | 143 | if (service == NULL) { |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 144 | printf_win_error("Failed to install service"); |
Laszlo Ersek | 108365f | 2013-07-03 15:14:09 +0200 | [diff] [blame] | 145 | goto out_manager; |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 146 | } |
| 147 | |
Laszlo Ersek | 108365f | 2013-07-03 15:14:09 +0200 | [diff] [blame] | 148 | ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &desc); |
| 149 | fprintf(stderr, "Service was installed successfully.\n"); |
| 150 | ret = EXIT_SUCCESS; |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 151 | CloseServiceHandle(service); |
Laszlo Ersek | 108365f | 2013-07-03 15:14:09 +0200 | [diff] [blame] | 152 | |
| 153 | out_manager: |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 154 | CloseServiceHandle(manager); |
| 155 | |
Laszlo Ersek | 108365f | 2013-07-03 15:14:09 +0200 | [diff] [blame] | 156 | out_strings: |
Laszlo Ersek | a880845 | 2013-05-18 06:31:52 +0200 | [diff] [blame] | 157 | g_string_free(cmdline, TRUE); |
Laszlo Ersek | 340d51d | 2013-07-03 15:14:10 +0200 | [diff] [blame] | 158 | g_string_free(esc, TRUE); |
Laszlo Ersek | 108365f | 2013-07-03 15:14:09 +0200 | [diff] [blame] | 159 | return ret; |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | int ga_uninstall_service(void) |
| 163 | { |
| 164 | SC_HANDLE manager; |
| 165 | SC_HANDLE service; |
| 166 | |
| 167 | manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); |
| 168 | if (manager == NULL) { |
| 169 | printf_win_error("No handle to service control manager"); |
| 170 | return EXIT_FAILURE; |
| 171 | } |
| 172 | |
| 173 | service = OpenService(manager, QGA_SERVICE_NAME, DELETE); |
| 174 | if (service == NULL) { |
| 175 | printf_win_error("No handle to service"); |
| 176 | CloseServiceHandle(manager); |
| 177 | return EXIT_FAILURE; |
| 178 | } |
| 179 | |
| 180 | if (DeleteService(service) == FALSE) { |
| 181 | printf_win_error("Failed to delete service"); |
| 182 | } else { |
Laszlo Ersek | febf1c4 | 2013-07-03 15:14:08 +0200 | [diff] [blame] | 183 | fprintf(stderr, "Service was deleted successfully.\n"); |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | CloseServiceHandle(service); |
| 187 | CloseServiceHandle(manager); |
| 188 | |
| 189 | return EXIT_SUCCESS; |
| 190 | } |