root / qga / service-win32.c @ c48c6522
History | View | Annotate | Download (3 kB)
1 | bc62fa03 | Michael Roth | /*
|
---|---|---|---|
2 | bc62fa03 | Michael Roth | * QEMU Guest Agent helpers for win32 service management
|
3 | bc62fa03 | Michael Roth | *
|
4 | bc62fa03 | Michael Roth | * Copyright IBM Corp. 2012
|
5 | bc62fa03 | Michael Roth | *
|
6 | bc62fa03 | Michael Roth | * Authors:
|
7 | bc62fa03 | Michael Roth | * Gal Hammer <ghammer@redhat.com>
|
8 | bc62fa03 | Michael Roth | * Michael Roth <mdroth@linux.vnet.ibm.com>
|
9 | bc62fa03 | Michael Roth | *
|
10 | bc62fa03 | Michael Roth | * This work is licensed under the terms of the GNU GPL, version 2 or later.
|
11 | bc62fa03 | Michael Roth | * See the COPYING file in the top-level directory.
|
12 | bc62fa03 | Michael Roth | */
|
13 | bc62fa03 | Michael Roth | #include <stdlib.h> |
14 | bc62fa03 | Michael Roth | #include <stdio.h> |
15 | bc62fa03 | Michael Roth | #include <glib.h> |
16 | bc62fa03 | Michael Roth | #include <windows.h> |
17 | bc62fa03 | Michael Roth | #include "qga/service-win32.h" |
18 | bc62fa03 | Michael Roth | |
19 | bc62fa03 | Michael Roth | static int printf_win_error(const char *text) |
20 | bc62fa03 | Michael Roth | { |
21 | bc62fa03 | Michael Roth | DWORD err = GetLastError(); |
22 | bc62fa03 | Michael Roth | char *message;
|
23 | bc62fa03 | Michael Roth | int n;
|
24 | bc62fa03 | Michael Roth | |
25 | bc62fa03 | Michael Roth | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
26 | bc62fa03 | Michael Roth | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
27 | bc62fa03 | Michael Roth | NULL,
|
28 | bc62fa03 | Michael Roth | err, |
29 | bc62fa03 | Michael Roth | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
30 | bc62fa03 | Michael Roth | (char *)&message, 0, |
31 | bc62fa03 | Michael Roth | NULL);
|
32 | bc62fa03 | Michael Roth | n = printf("%s. (Error: %d) %s", text, err, message);
|
33 | bc62fa03 | Michael Roth | LocalFree(message); |
34 | bc62fa03 | Michael Roth | |
35 | bc62fa03 | Michael Roth | return n;
|
36 | bc62fa03 | Michael Roth | } |
37 | bc62fa03 | Michael Roth | |
38 | bc62fa03 | Michael Roth | int ga_install_service(const char *path, const char *logfile) |
39 | bc62fa03 | Michael Roth | { |
40 | bc62fa03 | Michael Roth | SC_HANDLE manager; |
41 | bc62fa03 | Michael Roth | SC_HANDLE service; |
42 | bc62fa03 | Michael Roth | TCHAR cmdline[MAX_PATH]; |
43 | bc62fa03 | Michael Roth | |
44 | bc62fa03 | Michael Roth | if (GetModuleFileName(NULL, cmdline, MAX_PATH) == 0) { |
45 | bc62fa03 | Michael Roth | printf_win_error("No full path to service's executable");
|
46 | bc62fa03 | Michael Roth | return EXIT_FAILURE;
|
47 | bc62fa03 | Michael Roth | } |
48 | bc62fa03 | Michael Roth | |
49 | bc62fa03 | Michael Roth | _snprintf(cmdline, MAX_PATH - strlen(cmdline), "%s -d", cmdline);
|
50 | bc62fa03 | Michael Roth | |
51 | bc62fa03 | Michael Roth | if (path) {
|
52 | bc62fa03 | Michael Roth | _snprintf(cmdline, MAX_PATH - strlen(cmdline), "%s -p %s", cmdline, path);
|
53 | bc62fa03 | Michael Roth | } |
54 | bc62fa03 | Michael Roth | if (logfile) {
|
55 | bc62fa03 | Michael Roth | _snprintf(cmdline, MAX_PATH - strlen(cmdline), "%s -l %s -v",
|
56 | bc62fa03 | Michael Roth | cmdline, logfile); |
57 | bc62fa03 | Michael Roth | } |
58 | bc62fa03 | Michael Roth | |
59 | bc62fa03 | Michael Roth | g_debug("service's cmdline: %s", cmdline);
|
60 | bc62fa03 | Michael Roth | |
61 | bc62fa03 | Michael Roth | manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); |
62 | bc62fa03 | Michael Roth | if (manager == NULL) { |
63 | bc62fa03 | Michael Roth | printf_win_error("No handle to service control manager");
|
64 | bc62fa03 | Michael Roth | return EXIT_FAILURE;
|
65 | bc62fa03 | Michael Roth | } |
66 | bc62fa03 | Michael Roth | |
67 | bc62fa03 | Michael Roth | service = CreateService(manager, QGA_SERVICE_NAME, QGA_SERVICE_DISPLAY_NAME, |
68 | bc62fa03 | Michael Roth | SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, |
69 | bc62fa03 | Michael Roth | SERVICE_ERROR_NORMAL, cmdline, NULL, NULL, NULL, NULL, NULL); |
70 | bc62fa03 | Michael Roth | |
71 | bc62fa03 | Michael Roth | if (service) {
|
72 | bc62fa03 | Michael Roth | SERVICE_DESCRIPTION desc = { (char *)QGA_SERVICE_DESCRIPTION };
|
73 | bc62fa03 | Michael Roth | ChangeServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, &desc); |
74 | bc62fa03 | Michael Roth | |
75 | bc62fa03 | Michael Roth | printf("Service was installed successfully.\n");
|
76 | bc62fa03 | Michael Roth | } else {
|
77 | bc62fa03 | Michael Roth | printf_win_error("Failed to install service");
|
78 | bc62fa03 | Michael Roth | } |
79 | bc62fa03 | Michael Roth | |
80 | bc62fa03 | Michael Roth | CloseServiceHandle(service); |
81 | bc62fa03 | Michael Roth | CloseServiceHandle(manager); |
82 | bc62fa03 | Michael Roth | |
83 | bc62fa03 | Michael Roth | return (service == NULL); |
84 | bc62fa03 | Michael Roth | } |
85 | bc62fa03 | Michael Roth | |
86 | bc62fa03 | Michael Roth | int ga_uninstall_service(void) |
87 | bc62fa03 | Michael Roth | { |
88 | bc62fa03 | Michael Roth | SC_HANDLE manager; |
89 | bc62fa03 | Michael Roth | SC_HANDLE service; |
90 | bc62fa03 | Michael Roth | |
91 | bc62fa03 | Michael Roth | manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); |
92 | bc62fa03 | Michael Roth | if (manager == NULL) { |
93 | bc62fa03 | Michael Roth | printf_win_error("No handle to service control manager");
|
94 | bc62fa03 | Michael Roth | return EXIT_FAILURE;
|
95 | bc62fa03 | Michael Roth | } |
96 | bc62fa03 | Michael Roth | |
97 | bc62fa03 | Michael Roth | service = OpenService(manager, QGA_SERVICE_NAME, DELETE); |
98 | bc62fa03 | Michael Roth | if (service == NULL) { |
99 | bc62fa03 | Michael Roth | printf_win_error("No handle to service");
|
100 | bc62fa03 | Michael Roth | CloseServiceHandle(manager); |
101 | bc62fa03 | Michael Roth | return EXIT_FAILURE;
|
102 | bc62fa03 | Michael Roth | } |
103 | bc62fa03 | Michael Roth | |
104 | bc62fa03 | Michael Roth | if (DeleteService(service) == FALSE) {
|
105 | bc62fa03 | Michael Roth | printf_win_error("Failed to delete service");
|
106 | bc62fa03 | Michael Roth | } else {
|
107 | bc62fa03 | Michael Roth | printf("Service was deleted successfully.\n");
|
108 | bc62fa03 | Michael Roth | } |
109 | bc62fa03 | Michael Roth | |
110 | bc62fa03 | Michael Roth | CloseServiceHandle(service); |
111 | bc62fa03 | Michael Roth | CloseServiceHandle(manager); |
112 | bc62fa03 | Michael Roth | |
113 | bc62fa03 | Michael Roth | return EXIT_SUCCESS;
|
114 | bc62fa03 | Michael Roth | } |