xref: /openbmc/qemu/qga/service-win32.c (revision 98c710f2d5cdf37f29a267352eb1f3c28cbf369d)
1bc62fa03SMichael Roth /*
2bc62fa03SMichael Roth  * QEMU Guest Agent helpers for win32 service management
3bc62fa03SMichael Roth  *
4bc62fa03SMichael Roth  * Copyright IBM Corp. 2012
5bc62fa03SMichael Roth  *
6bc62fa03SMichael Roth  * Authors:
7bc62fa03SMichael Roth  *  Gal Hammer        <ghammer@redhat.com>
8bc62fa03SMichael Roth  *  Michael Roth      <mdroth@linux.vnet.ibm.com>
9bc62fa03SMichael Roth  *
10bc62fa03SMichael Roth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
11bc62fa03SMichael Roth  * See the COPYING file in the top-level directory.
12bc62fa03SMichael Roth  */
13*4459bf38SPeter Maydell #include "qemu/osdep.h"
14bc62fa03SMichael Roth #include <windows.h>
15bc62fa03SMichael Roth #include "qga/service-win32.h"
16bc62fa03SMichael Roth 
printf_win_error(const char * text)17bc62fa03SMichael Roth static int printf_win_error(const char *text)
18bc62fa03SMichael Roth {
19bc62fa03SMichael Roth     DWORD err = GetLastError();
20bc62fa03SMichael Roth     char *message;
21bc62fa03SMichael Roth     int n;
22bc62fa03SMichael Roth 
23bc62fa03SMichael Roth     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
24bc62fa03SMichael Roth         FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
25bc62fa03SMichael Roth         NULL,
26bc62fa03SMichael Roth         err,
27bc62fa03SMichael Roth         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
28bc62fa03SMichael Roth         (char *)&message, 0,
29bc62fa03SMichael Roth         NULL);
30febf1c49SLaszlo Ersek     n = fprintf(stderr, "%s. (Error: %d) %s", text, (int)err, message);
31bc62fa03SMichael Roth     LocalFree(message);
32bc62fa03SMichael Roth 
33bc62fa03SMichael Roth     return n;
34bc62fa03SMichael Roth }
35bc62fa03SMichael Roth 
36340d51dfSLaszlo Ersek /* Windows command line escaping. Based on
37340d51dfSLaszlo Ersek  * <http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx> and
38340d51dfSLaszlo Ersek  * <http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft%28v=vs.85%29.aspx>.
39340d51dfSLaszlo Ersek  *
40340d51dfSLaszlo Ersek  * The caller is responsible for initializing @buffer; prior contents are lost.
41340d51dfSLaszlo Ersek  */
win_escape_arg(const char * to_escape,GString * buffer)42340d51dfSLaszlo Ersek static const char *win_escape_arg(const char *to_escape, GString *buffer)
43340d51dfSLaszlo Ersek {
44340d51dfSLaszlo Ersek     size_t backslash_count;
45340d51dfSLaszlo Ersek     const char *c;
46340d51dfSLaszlo Ersek 
47340d51dfSLaszlo Ersek     /* open with a double quote */
48340d51dfSLaszlo Ersek     g_string_assign(buffer, "\"");
49340d51dfSLaszlo Ersek 
50340d51dfSLaszlo Ersek     backslash_count = 0;
51340d51dfSLaszlo Ersek     for (c = to_escape; *c != '\0'; ++c) {
52340d51dfSLaszlo Ersek         switch (*c) {
53340d51dfSLaszlo Ersek         case '\\':
54340d51dfSLaszlo Ersek             /* The meaning depends on the first non-backslash character coming
55340d51dfSLaszlo Ersek              * up.
56340d51dfSLaszlo Ersek              */
57340d51dfSLaszlo Ersek             ++backslash_count;
58340d51dfSLaszlo Ersek             break;
59340d51dfSLaszlo Ersek 
60340d51dfSLaszlo Ersek         case '"':
61340d51dfSLaszlo Ersek             /* We must escape each pending backslash, then escape the double
62340d51dfSLaszlo Ersek              * quote. This creates a case of "odd number of backslashes [...]
63340d51dfSLaszlo Ersek              * followed by a double quotation mark".
64340d51dfSLaszlo Ersek              */
65340d51dfSLaszlo Ersek             while (backslash_count) {
66340d51dfSLaszlo Ersek                 --backslash_count;
67340d51dfSLaszlo Ersek                 g_string_append(buffer, "\\\\");
68340d51dfSLaszlo Ersek             }
69340d51dfSLaszlo Ersek             g_string_append(buffer, "\\\"");
70340d51dfSLaszlo Ersek             break;
71340d51dfSLaszlo Ersek 
72340d51dfSLaszlo Ersek         default:
73340d51dfSLaszlo Ersek             /* Any pending backslashes are without special meaning, flush them.
74340d51dfSLaszlo Ersek              * "Backslashes are interpreted literally, unless they immediately
75340d51dfSLaszlo Ersek              * precede a double quotation mark."
76340d51dfSLaszlo Ersek              */
77340d51dfSLaszlo Ersek             while (backslash_count) {
78340d51dfSLaszlo Ersek                 --backslash_count;
79340d51dfSLaszlo Ersek                 g_string_append_c(buffer, '\\');
80340d51dfSLaszlo Ersek             }
81340d51dfSLaszlo Ersek             g_string_append_c(buffer, *c);
82340d51dfSLaszlo Ersek         }
83340d51dfSLaszlo Ersek     }
84340d51dfSLaszlo Ersek 
85340d51dfSLaszlo Ersek     /* We're about to close with a double quote in string delimiter role.
86340d51dfSLaszlo Ersek      * Double all pending backslashes, creating a case of "even number of
87340d51dfSLaszlo Ersek      * backslashes [...] followed by a double quotation mark".
88340d51dfSLaszlo Ersek      */
89340d51dfSLaszlo Ersek     while (backslash_count) {
90340d51dfSLaszlo Ersek         --backslash_count;
91340d51dfSLaszlo Ersek         g_string_append(buffer, "\\\\");
92340d51dfSLaszlo Ersek     }
93340d51dfSLaszlo Ersek     g_string_append_c(buffer, '"');
94340d51dfSLaszlo Ersek 
95340d51dfSLaszlo Ersek     return buffer->str;
96340d51dfSLaszlo Ersek }
97340d51dfSLaszlo Ersek 
ga_install_service(const char * path,const char * logfile,const char * state_dir)98a839ee77SLaszlo Ersek int ga_install_service(const char *path, const char *logfile,
99a839ee77SLaszlo Ersek                        const char *state_dir)
100bc62fa03SMichael Roth {
101108365fdSLaszlo Ersek     int ret = EXIT_FAILURE;
102bc62fa03SMichael Roth     SC_HANDLE manager;
103bc62fa03SMichael Roth     SC_HANDLE service;
104a880845fSLaszlo Ersek     TCHAR module_fname[MAX_PATH];
105340d51dfSLaszlo Ersek     GString *esc;
106a880845fSLaszlo Ersek     GString *cmdline;
107108365fdSLaszlo Ersek     SERVICE_DESCRIPTION desc = { (char *)QGA_SERVICE_DESCRIPTION };
108bc62fa03SMichael Roth 
109a880845fSLaszlo Ersek     if (GetModuleFileName(NULL, module_fname, MAX_PATH) == 0) {
110bc62fa03SMichael Roth         printf_win_error("No full path to service's executable");
111bc62fa03SMichael Roth         return EXIT_FAILURE;
112bc62fa03SMichael Roth     }
113bc62fa03SMichael Roth 
114340d51dfSLaszlo Ersek     esc     = g_string_new("");
115340d51dfSLaszlo Ersek     cmdline = g_string_new("");
116340d51dfSLaszlo Ersek 
117340d51dfSLaszlo Ersek     g_string_append_printf(cmdline, "%s -d",
118340d51dfSLaszlo Ersek                            win_escape_arg(module_fname, esc));
119bc62fa03SMichael Roth 
120bc62fa03SMichael Roth     if (path) {
121340d51dfSLaszlo Ersek         g_string_append_printf(cmdline, " -p %s", win_escape_arg(path, esc));
122bc62fa03SMichael Roth     }
123bc62fa03SMichael Roth     if (logfile) {
124340d51dfSLaszlo Ersek         g_string_append_printf(cmdline, " -l %s -v",
125340d51dfSLaszlo Ersek                                win_escape_arg(logfile, esc));
126bc62fa03SMichael Roth     }
127a839ee77SLaszlo Ersek     if (state_dir) {
128340d51dfSLaszlo Ersek         g_string_append_printf(cmdline, " -t %s",
129340d51dfSLaszlo Ersek                                win_escape_arg(state_dir, esc));
130a839ee77SLaszlo Ersek     }
131bc62fa03SMichael Roth 
132a880845fSLaszlo Ersek     g_debug("service's cmdline: %s", cmdline->str);
133bc62fa03SMichael Roth 
134bc62fa03SMichael Roth     manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
135bc62fa03SMichael Roth     if (manager == NULL) {
136bc62fa03SMichael Roth         printf_win_error("No handle to service control manager");
137108365fdSLaszlo Ersek         goto out_strings;
138bc62fa03SMichael Roth     }
139bc62fa03SMichael Roth 
140bc62fa03SMichael Roth     service = CreateService(manager, QGA_SERVICE_NAME, QGA_SERVICE_DISPLAY_NAME,
141bc62fa03SMichael Roth         SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
142a880845fSLaszlo Ersek         SERVICE_ERROR_NORMAL, cmdline->str, NULL, NULL, NULL, NULL, NULL);
143108365fdSLaszlo Ersek     if (service == NULL) {
144bc62fa03SMichael Roth         printf_win_error("Failed to install service");
145108365fdSLaszlo Ersek         goto out_manager;
146bc62fa03SMichael Roth     }
147bc62fa03SMichael Roth 
148108365fdSLaszlo Ersek     ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &desc);
149108365fdSLaszlo Ersek     fprintf(stderr, "Service was installed successfully.\n");
150108365fdSLaszlo Ersek     ret = EXIT_SUCCESS;
151bc62fa03SMichael Roth     CloseServiceHandle(service);
152108365fdSLaszlo Ersek 
153108365fdSLaszlo Ersek out_manager:
154bc62fa03SMichael Roth     CloseServiceHandle(manager);
155bc62fa03SMichael Roth 
156108365fdSLaszlo Ersek out_strings:
157a880845fSLaszlo Ersek     g_string_free(cmdline, TRUE);
158340d51dfSLaszlo Ersek     g_string_free(esc, TRUE);
159108365fdSLaszlo Ersek     return ret;
160bc62fa03SMichael Roth }
161bc62fa03SMichael Roth 
ga_uninstall_service(void)162bc62fa03SMichael Roth int ga_uninstall_service(void)
163bc62fa03SMichael Roth {
164bc62fa03SMichael Roth     SC_HANDLE manager;
165bc62fa03SMichael Roth     SC_HANDLE service;
166bc62fa03SMichael Roth 
167bc62fa03SMichael Roth     manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
168bc62fa03SMichael Roth     if (manager == NULL) {
169bc62fa03SMichael Roth         printf_win_error("No handle to service control manager");
170bc62fa03SMichael Roth         return EXIT_FAILURE;
171bc62fa03SMichael Roth     }
172bc62fa03SMichael Roth 
173bc62fa03SMichael Roth     service = OpenService(manager, QGA_SERVICE_NAME, DELETE);
174bc62fa03SMichael Roth     if (service == NULL) {
175bc62fa03SMichael Roth         printf_win_error("No handle to service");
176bc62fa03SMichael Roth         CloseServiceHandle(manager);
177bc62fa03SMichael Roth         return EXIT_FAILURE;
178bc62fa03SMichael Roth     }
179bc62fa03SMichael Roth 
180bc62fa03SMichael Roth     if (DeleteService(service) == FALSE) {
181bc62fa03SMichael Roth         printf_win_error("Failed to delete service");
182bc62fa03SMichael Roth     } else {
183febf1c49SLaszlo Ersek         fprintf(stderr, "Service was deleted successfully.\n");
184bc62fa03SMichael Roth     }
185bc62fa03SMichael Roth 
186bc62fa03SMichael Roth     CloseServiceHandle(service);
187bc62fa03SMichael Roth     CloseServiceHandle(manager);
188bc62fa03SMichael Roth 
189bc62fa03SMichael Roth     return EXIT_SUCCESS;
190bc62fa03SMichael Roth }
191