pseries: Add H_SET_MODE hcall to change guest exception endianness
[qemu/kevin.git] / qga / service-win32.c
blobaef41f04f163450434f62b795d37494d25f2ece1
1 /*
2 * QEMU Guest Agent helpers for win32 service management
4 * Copyright IBM Corp. 2012
6 * Authors:
7 * Gal Hammer <ghammer@redhat.com>
8 * Michael Roth <mdroth@linux.vnet.ibm.com>
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.
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <glib.h>
16 #include <windows.h>
17 #include "qga/service-win32.h"
19 static int printf_win_error(const char *text)
21 DWORD err = GetLastError();
22 char *message;
23 int n;
25 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
26 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
27 NULL,
28 err,
29 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
30 (char *)&message, 0,
31 NULL);
32 n = fprintf(stderr, "%s. (Error: %d) %s", text, (int)err, message);
33 LocalFree(message);
35 return n;
38 /* Windows command line escaping. Based on
39 * <http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx> and
40 * <http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft%28v=vs.85%29.aspx>.
42 * The caller is responsible for initializing @buffer; prior contents are lost.
44 static const char *win_escape_arg(const char *to_escape, GString *buffer)
46 size_t backslash_count;
47 const char *c;
49 /* open with a double quote */
50 g_string_assign(buffer, "\"");
52 backslash_count = 0;
53 for (c = to_escape; *c != '\0'; ++c) {
54 switch (*c) {
55 case '\\':
56 /* The meaning depends on the first non-backslash character coming
57 * up.
59 ++backslash_count;
60 break;
62 case '"':
63 /* We must escape each pending backslash, then escape the double
64 * quote. This creates a case of "odd number of backslashes [...]
65 * followed by a double quotation mark".
67 while (backslash_count) {
68 --backslash_count;
69 g_string_append(buffer, "\\\\");
71 g_string_append(buffer, "\\\"");
72 break;
74 default:
75 /* Any pending backslashes are without special meaning, flush them.
76 * "Backslashes are interpreted literally, unless they immediately
77 * precede a double quotation mark."
79 while (backslash_count) {
80 --backslash_count;
81 g_string_append_c(buffer, '\\');
83 g_string_append_c(buffer, *c);
87 /* We're about to close with a double quote in string delimiter role.
88 * Double all pending backslashes, creating a case of "even number of
89 * backslashes [...] followed by a double quotation mark".
91 while (backslash_count) {
92 --backslash_count;
93 g_string_append(buffer, "\\\\");
95 g_string_append_c(buffer, '"');
97 return buffer->str;
100 int ga_install_service(const char *path, const char *logfile,
101 const char *state_dir)
103 int ret = EXIT_FAILURE;
104 SC_HANDLE manager;
105 SC_HANDLE service;
106 TCHAR module_fname[MAX_PATH];
107 GString *esc;
108 GString *cmdline;
109 SERVICE_DESCRIPTION desc = { (char *)QGA_SERVICE_DESCRIPTION };
111 if (GetModuleFileName(NULL, module_fname, MAX_PATH) == 0) {
112 printf_win_error("No full path to service's executable");
113 return EXIT_FAILURE;
116 esc = g_string_new("");
117 cmdline = g_string_new("");
119 g_string_append_printf(cmdline, "%s -d",
120 win_escape_arg(module_fname, esc));
122 if (path) {
123 g_string_append_printf(cmdline, " -p %s", win_escape_arg(path, esc));
125 if (logfile) {
126 g_string_append_printf(cmdline, " -l %s -v",
127 win_escape_arg(logfile, esc));
129 if (state_dir) {
130 g_string_append_printf(cmdline, " -t %s",
131 win_escape_arg(state_dir, esc));
134 g_debug("service's cmdline: %s", cmdline->str);
136 manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
137 if (manager == NULL) {
138 printf_win_error("No handle to service control manager");
139 goto out_strings;
142 service = CreateService(manager, QGA_SERVICE_NAME, QGA_SERVICE_DISPLAY_NAME,
143 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
144 SERVICE_ERROR_NORMAL, cmdline->str, NULL, NULL, NULL, NULL, NULL);
145 if (service == NULL) {
146 printf_win_error("Failed to install service");
147 goto out_manager;
150 ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &desc);
151 fprintf(stderr, "Service was installed successfully.\n");
152 ret = EXIT_SUCCESS;
153 CloseServiceHandle(service);
155 out_manager:
156 CloseServiceHandle(manager);
158 out_strings:
159 g_string_free(cmdline, TRUE);
160 g_string_free(esc, TRUE);
161 return ret;
164 int ga_uninstall_service(void)
166 SC_HANDLE manager;
167 SC_HANDLE service;
169 manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
170 if (manager == NULL) {
171 printf_win_error("No handle to service control manager");
172 return EXIT_FAILURE;
175 service = OpenService(manager, QGA_SERVICE_NAME, DELETE);
176 if (service == NULL) {
177 printf_win_error("No handle to service");
178 CloseServiceHandle(manager);
179 return EXIT_FAILURE;
182 if (DeleteService(service) == FALSE) {
183 printf_win_error("Failed to delete service");
184 } else {
185 fprintf(stderr, "Service was deleted successfully.\n");
188 CloseServiceHandle(service);
189 CloseServiceHandle(manager);
191 return EXIT_SUCCESS;