]>
Commit | Line | Data |
---|---|---|
bc62fa03 MR |
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 | */ | |
13 | #include <stdlib.h> | |
14 | #include <stdio.h> | |
15 | #include <glib.h> | |
16 | #include <windows.h> | |
17 | #include "qga/service-win32.h" | |
18 | ||
19 | static int printf_win_error(const char *text) | |
20 | { | |
21 | DWORD err = GetLastError(); | |
22 | char *message; | |
23 | int n; | |
24 | ||
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); | |
ed2cbf74 | 32 | n = printf("%s. (Error: %d) %s", text, (int)err, message); |
bc62fa03 MR |
33 | LocalFree(message); |
34 | ||
35 | return n; | |
36 | } | |
37 | ||
38 | int ga_install_service(const char *path, const char *logfile) | |
39 | { | |
40 | SC_HANDLE manager; | |
41 | SC_HANDLE service; | |
a880845f LE |
42 | TCHAR module_fname[MAX_PATH]; |
43 | GString *cmdline; | |
bc62fa03 | 44 | |
a880845f | 45 | if (GetModuleFileName(NULL, module_fname, MAX_PATH) == 0) { |
bc62fa03 MR |
46 | printf_win_error("No full path to service's executable"); |
47 | return EXIT_FAILURE; | |
48 | } | |
49 | ||
a880845f LE |
50 | cmdline = g_string_new(module_fname); |
51 | g_string_append(cmdline, " -d"); | |
bc62fa03 MR |
52 | |
53 | if (path) { | |
a880845f | 54 | g_string_append_printf(cmdline, " -p %s", path); |
bc62fa03 MR |
55 | } |
56 | if (logfile) { | |
a880845f | 57 | g_string_append_printf(cmdline, " -l %s -v", logfile); |
bc62fa03 MR |
58 | } |
59 | ||
a880845f | 60 | g_debug("service's cmdline: %s", cmdline->str); |
bc62fa03 MR |
61 | |
62 | manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); | |
63 | if (manager == NULL) { | |
64 | printf_win_error("No handle to service control manager"); | |
a880845f | 65 | g_string_free(cmdline, TRUE); |
bc62fa03 MR |
66 | return EXIT_FAILURE; |
67 | } | |
68 | ||
69 | service = CreateService(manager, QGA_SERVICE_NAME, QGA_SERVICE_DISPLAY_NAME, | |
70 | SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, | |
a880845f | 71 | SERVICE_ERROR_NORMAL, cmdline->str, NULL, NULL, NULL, NULL, NULL); |
bc62fa03 MR |
72 | |
73 | if (service) { | |
74 | SERVICE_DESCRIPTION desc = { (char *)QGA_SERVICE_DESCRIPTION }; | |
75 | ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &desc); | |
76 | ||
77 | printf("Service was installed successfully.\n"); | |
78 | } else { | |
79 | printf_win_error("Failed to install service"); | |
80 | } | |
81 | ||
82 | CloseServiceHandle(service); | |
83 | CloseServiceHandle(manager); | |
84 | ||
a880845f | 85 | g_string_free(cmdline, TRUE); |
bc62fa03 MR |
86 | return (service == NULL); |
87 | } | |
88 | ||
89 | int ga_uninstall_service(void) | |
90 | { | |
91 | SC_HANDLE manager; | |
92 | SC_HANDLE service; | |
93 | ||
94 | manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); | |
95 | if (manager == NULL) { | |
96 | printf_win_error("No handle to service control manager"); | |
97 | return EXIT_FAILURE; | |
98 | } | |
99 | ||
100 | service = OpenService(manager, QGA_SERVICE_NAME, DELETE); | |
101 | if (service == NULL) { | |
102 | printf_win_error("No handle to service"); | |
103 | CloseServiceHandle(manager); | |
104 | return EXIT_FAILURE; | |
105 | } | |
106 | ||
107 | if (DeleteService(service) == FALSE) { | |
108 | printf_win_error("Failed to delete service"); | |
109 | } else { | |
110 | printf("Service was deleted successfully.\n"); | |
111 | } | |
112 | ||
113 | CloseServiceHandle(service); | |
114 | CloseServiceHandle(manager); | |
115 | ||
116 | return EXIT_SUCCESS; | |
117 | } |