2 * QEMU Guest Agent helpers for win32 service management
4 * Copyright IBM Corp. 2012
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.
17 #include "qga/service-win32.h"
19 static int printf_win_error(const char *text
)
21 DWORD err
= GetLastError();
25 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
26 FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
,
29 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
32 n
= fprintf(stderr
, "%s. (Error: %d) %s", text
, (int)err
, message
);
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
;
49 /* open with a double quote */
50 g_string_assign(buffer
, "\"");
53 for (c
= to_escape
; *c
!= '\0'; ++c
) {
56 /* The meaning depends on the first non-backslash character coming
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
) {
69 g_string_append(buffer
, "\\\\");
71 g_string_append(buffer
, "\\\"");
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
) {
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
) {
93 g_string_append(buffer
, "\\\\");
95 g_string_append_c(buffer
, '"');
100 int ga_install_service(const char *path
, const char *logfile
,
101 const char *state_dir
)
103 int ret
= EXIT_FAILURE
;
106 TCHAR module_fname
[MAX_PATH
];
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");
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
));
123 g_string_append_printf(cmdline
, " -p %s", win_escape_arg(path
, esc
));
126 g_string_append_printf(cmdline
, " -l %s -v",
127 win_escape_arg(logfile
, esc
));
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");
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");
150 ChangeServiceConfig2(service
, SERVICE_CONFIG_DESCRIPTION
, &desc
);
151 fprintf(stderr
, "Service was installed successfully.\n");
153 CloseServiceHandle(service
);
156 CloseServiceHandle(manager
);
159 g_string_free(cmdline
, TRUE
);
160 g_string_free(esc
, TRUE
);
164 int ga_uninstall_service(void)
169 manager
= OpenSCManager(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
170 if (manager
== NULL
) {
171 printf_win_error("No handle to service control manager");
175 service
= OpenService(manager
, QGA_SERVICE_NAME
, DELETE
);
176 if (service
== NULL
) {
177 printf_win_error("No handle to service");
178 CloseServiceHandle(manager
);
182 if (DeleteService(service
) == FALSE
) {
183 printf_win_error("Failed to delete service");
185 fprintf(stderr
, "Service was deleted successfully.\n");
188 CloseServiceHandle(service
);
189 CloseServiceHandle(manager
);