]>
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); | |
febf1c49 | 32 | n = fprintf(stderr, "%s. (Error: %d) %s", text, (int)err, message); |
bc62fa03 MR |
33 | LocalFree(message); |
34 | ||
35 | return n; | |
36 | } | |
37 | ||
a839ee77 LE |
38 | int ga_install_service(const char *path, const char *logfile, |
39 | const char *state_dir) | |
bc62fa03 | 40 | { |
108365fd | 41 | int ret = EXIT_FAILURE; |
bc62fa03 MR |
42 | SC_HANDLE manager; |
43 | SC_HANDLE service; | |
a880845f LE |
44 | TCHAR module_fname[MAX_PATH]; |
45 | GString *cmdline; | |
108365fd | 46 | SERVICE_DESCRIPTION desc = { (char *)QGA_SERVICE_DESCRIPTION }; |
bc62fa03 | 47 | |
a880845f | 48 | if (GetModuleFileName(NULL, module_fname, MAX_PATH) == 0) { |
bc62fa03 MR |
49 | printf_win_error("No full path to service's executable"); |
50 | return EXIT_FAILURE; | |
51 | } | |
52 | ||
a880845f LE |
53 | cmdline = g_string_new(module_fname); |
54 | g_string_append(cmdline, " -d"); | |
bc62fa03 MR |
55 | |
56 | if (path) { | |
a880845f | 57 | g_string_append_printf(cmdline, " -p %s", path); |
bc62fa03 MR |
58 | } |
59 | if (logfile) { | |
a880845f | 60 | g_string_append_printf(cmdline, " -l %s -v", logfile); |
bc62fa03 | 61 | } |
a839ee77 LE |
62 | if (state_dir) { |
63 | g_string_append_printf(cmdline, " -t %s", state_dir); | |
64 | } | |
bc62fa03 | 65 | |
a880845f | 66 | g_debug("service's cmdline: %s", cmdline->str); |
bc62fa03 MR |
67 | |
68 | manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); | |
69 | if (manager == NULL) { | |
70 | printf_win_error("No handle to service control manager"); | |
108365fd | 71 | goto out_strings; |
bc62fa03 MR |
72 | } |
73 | ||
74 | service = CreateService(manager, QGA_SERVICE_NAME, QGA_SERVICE_DISPLAY_NAME, | |
75 | SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, | |
a880845f | 76 | SERVICE_ERROR_NORMAL, cmdline->str, NULL, NULL, NULL, NULL, NULL); |
108365fd | 77 | if (service == NULL) { |
bc62fa03 | 78 | printf_win_error("Failed to install service"); |
108365fd | 79 | goto out_manager; |
bc62fa03 MR |
80 | } |
81 | ||
108365fd LE |
82 | ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &desc); |
83 | fprintf(stderr, "Service was installed successfully.\n"); | |
84 | ret = EXIT_SUCCESS; | |
bc62fa03 | 85 | CloseServiceHandle(service); |
108365fd LE |
86 | |
87 | out_manager: | |
bc62fa03 MR |
88 | CloseServiceHandle(manager); |
89 | ||
108365fd | 90 | out_strings: |
a880845f | 91 | g_string_free(cmdline, TRUE); |
108365fd | 92 | return ret; |
bc62fa03 MR |
93 | } |
94 | ||
95 | int ga_uninstall_service(void) | |
96 | { | |
97 | SC_HANDLE manager; | |
98 | SC_HANDLE service; | |
99 | ||
100 | manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); | |
101 | if (manager == NULL) { | |
102 | printf_win_error("No handle to service control manager"); | |
103 | return EXIT_FAILURE; | |
104 | } | |
105 | ||
106 | service = OpenService(manager, QGA_SERVICE_NAME, DELETE); | |
107 | if (service == NULL) { | |
108 | printf_win_error("No handle to service"); | |
109 | CloseServiceHandle(manager); | |
110 | return EXIT_FAILURE; | |
111 | } | |
112 | ||
113 | if (DeleteService(service) == FALSE) { | |
114 | printf_win_error("Failed to delete service"); | |
115 | } else { | |
febf1c49 | 116 | fprintf(stderr, "Service was deleted successfully.\n"); |
bc62fa03 MR |
117 | } |
118 | ||
119 | CloseServiceHandle(service); | |
120 | CloseServiceHandle(manager); | |
121 | ||
122 | return EXIT_SUCCESS; | |
123 | } |