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
= printf("%s. (Error: %d) %s", text
, err
, message
);
38 int ga_install_service(const char *path
, const char *logfile
)
42 TCHAR cmdline
[MAX_PATH
];
44 if (GetModuleFileName(NULL
, cmdline
, MAX_PATH
) == 0) {
45 printf_win_error("No full path to service's executable");
49 _snprintf(cmdline
, MAX_PATH
- strlen(cmdline
), "%s -d", cmdline
);
52 _snprintf(cmdline
, MAX_PATH
- strlen(cmdline
), "%s -p %s", cmdline
, path
);
55 _snprintf(cmdline
, MAX_PATH
- strlen(cmdline
), "%s -l %s -v",
59 g_debug("service's cmdline: %s", cmdline
);
61 manager
= OpenSCManager(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
62 if (manager
== NULL
) {
63 printf_win_error("No handle to service control manager");
67 service
= CreateService(manager
, QGA_SERVICE_NAME
, QGA_SERVICE_DISPLAY_NAME
,
68 SERVICE_ALL_ACCESS
, SERVICE_WIN32_OWN_PROCESS
, SERVICE_AUTO_START
,
69 SERVICE_ERROR_NORMAL
, cmdline
, NULL
, NULL
, NULL
, NULL
, NULL
);
72 SERVICE_DESCRIPTION desc
= { (char *)QGA_SERVICE_DESCRIPTION
};
73 ChangeServiceConfig2(service
, SERVICE_CONFIG_DESCRIPTION
, &desc
);
75 printf("Service was installed successfully.\n");
77 printf_win_error("Failed to install service");
80 CloseServiceHandle(service
);
81 CloseServiceHandle(manager
);
83 return (service
== NULL
);
86 int ga_uninstall_service(void)
91 manager
= OpenSCManager(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
92 if (manager
== NULL
) {
93 printf_win_error("No handle to service control manager");
97 service
= OpenService(manager
, QGA_SERVICE_NAME
, DELETE
);
98 if (service
== NULL
) {
99 printf_win_error("No handle to service");
100 CloseServiceHandle(manager
);
104 if (DeleteService(service
) == FALSE
) {
105 printf_win_error("Failed to delete service");
107 printf("Service was deleted successfully.\n");
110 CloseServiceHandle(service
);
111 CloseServiceHandle(manager
);