#include <getopt.h>
#ifndef _WIN32
#include <syslog.h>
+#include <sys/wait.h>
#endif
#include "json-streamer.h"
#include "json-parser.h"
#include "error_int.h"
#include "qapi/qmp-core.h"
#include "qga/channel.h"
+#ifdef _WIN32
+#include "qga/service-win32.h"
+#include <windows.h>
+#endif
#ifndef _WIN32
#define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0"
#define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0"
#endif
#define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid"
+#define QGA_SENTINEL_BYTE 0xFF
struct GAState {
JSONMessageParser parser;
GLogLevelFlags log_level;
FILE *log_file;
bool logging_enabled;
+#ifdef _WIN32
+ GAService service;
+#endif
+ bool delimit_response;
};
-static struct GAState *ga_state;
+struct GAState *ga_state;
+
+#ifdef _WIN32
+DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
+ LPVOID ctx);
+VOID WINAPI service_main(DWORD argc, TCHAR *argv[]);
+#endif
-#ifndef _WIN32
static void quit_handler(int sig)
{
g_debug("received signal num %d, quitting", sig);
}
}
+#ifndef _WIN32
+/* reap _all_ terminated children */
+static void child_handler(int sig)
+{
+ int status;
+ while (waitpid(-1, &status, WNOHANG) > 0) /* NOTHING */;
+}
+
static gboolean register_signal_handlers(void)
{
- struct sigaction sigact;
+ struct sigaction sigact, sigact_chld;
int ret;
memset(&sigact, 0, sizeof(struct sigaction));
g_error("error configuring signal handler: %s", strerror(errno));
return false;
}
+
+ memset(&sigact_chld, 0, sizeof(struct sigaction));
+ sigact_chld.sa_handler = child_handler;
+ sigact_chld.sa_flags = SA_NOCLDSTOP;
+ ret = sigaction(SIGCHLD, &sigact_chld, NULL);
+ if (ret == -1) {
+ g_error("error configuring signal handler: %s", strerror(errno));
+ }
+
return true;
}
#endif
static void usage(const char *cmd)
{
printf(
-"Usage: %s -c <channel_opts>\n"
+"Usage: %s [-m <method> -p <path>] [<options>]\n"
"QEMU Guest Agent %s\n"
"\n"
" -m, --method transport method: one of unix-listen, virtio-serial, or\n"
" isa-serial (virtio-serial is the default)\n"
-" -p, --path device/socket path (%s is the default for virtio-serial)\n"
+" -p, --path device/socket path (the default for virtio-serial is:\n"
+" %s)\n"
" -l, --logfile set logfile path, logs to stderr by default\n"
" -f, --pidfile specify pidfile (default is %s)\n"
" -v, --verbose log extra debugging information\n"
" -V, --version print version information and exit\n"
-#ifndef _WIN32
" -d, --daemonize become a daemon\n"
+#ifdef _WIN32
+" -s, --service service commands: install, uninstall\n"
#endif
-" -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\""
+" -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\"\n"
" to list available RPCs)\n"
" -h, --help display this help and exit\n"
"\n"
}
}
+void ga_set_response_delimited(GAState *s)
+{
+ s->delimit_response = true;
+}
+
#ifndef _WIN32
static void become_daemon(const char *pidfile)
{
static int send_response(GAState *s, QObject *payload)
{
const char *buf;
- QString *payload_qstr;
+ QString *payload_qstr, *response_qstr;
GIOStatus status;
g_assert(payload && s->channel);
return -EINVAL;
}
- qstring_append_chr(payload_qstr, '\n');
- buf = qstring_get_str(payload_qstr);
+ if (s->delimit_response) {
+ s->delimit_response = false;
+ response_qstr = qstring_new();
+ qstring_append_chr(response_qstr, QGA_SENTINEL_BYTE);
+ qstring_append(response_qstr, qstring_get_str(payload_qstr));
+ QDECREF(payload_qstr);
+ } else {
+ response_qstr = payload_qstr;
+ }
+
+ qstring_append_chr(response_qstr, '\n');
+ buf = qstring_get_str(response_qstr);
status = ga_channel_write_all(s->channel, buf, strlen(buf));
- QDECREF(payload_qstr);
+ QDECREF(response_qstr);
if (status != G_IO_STATUS_NORMAL) {
return -EIO;
}
return true;
}
+#ifdef _WIN32
+DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data,
+ LPVOID ctx)
+{
+ DWORD ret = NO_ERROR;
+ GAService *service = &ga_state->service;
+
+ switch (ctrl)
+ {
+ case SERVICE_CONTROL_STOP:
+ case SERVICE_CONTROL_SHUTDOWN:
+ quit_handler(SIGTERM);
+ service->status.dwCurrentState = SERVICE_STOP_PENDING;
+ SetServiceStatus(service->status_handle, &service->status);
+ break;
+
+ default:
+ ret = ERROR_CALL_NOT_IMPLEMENTED;
+ }
+ return ret;
+}
+
+VOID WINAPI service_main(DWORD argc, TCHAR *argv[])
+{
+ GAService *service = &ga_state->service;
+
+ service->status_handle = RegisterServiceCtrlHandlerEx(QGA_SERVICE_NAME,
+ service_ctrl_handler, NULL);
+
+ if (service->status_handle == 0) {
+ g_critical("Failed to register extended requests function!\n");
+ return;
+ }
+
+ service->status.dwServiceType = SERVICE_WIN32;
+ service->status.dwCurrentState = SERVICE_RUNNING;
+ service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
+ service->status.dwWin32ExitCode = NO_ERROR;
+ service->status.dwServiceSpecificExitCode = NO_ERROR;
+ service->status.dwCheckPoint = 0;
+ service->status.dwWaitHint = 0;
+ SetServiceStatus(service->status_handle, &service->status);
+
+ g_main_loop_run(ga_state->main_loop);
+
+ service->status.dwCurrentState = SERVICE_STOPPED;
+ SetServiceStatus(service->status_handle, &service->status);
+}
+#endif
+
int main(int argc, char **argv)
{
- const char *sopt = "hVvdm:p:l:f:b:";
+ const char *sopt = "hVvdm:p:l:f:b:s:";
const char *method = NULL, *path = NULL, *pidfile = QGA_PIDFILE_DEFAULT;
+ const char *log_file_name = NULL;
+#ifdef _WIN32
+ const char *service = NULL;
+#endif
const struct option lopt[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
- { "logfile", 0, NULL, 'l' },
- { "pidfile", 0, NULL, 'f' },
+ { "logfile", 1, NULL, 'l' },
+ { "pidfile", 1, NULL, 'f' },
{ "verbose", 0, NULL, 'v' },
- { "method", 0, NULL, 'm' },
- { "path", 0, NULL, 'p' },
+ { "method", 1, NULL, 'm' },
+ { "path", 1, NULL, 'p' },
{ "daemonize", 0, NULL, 'd' },
- { "blacklist", 0, NULL, 'b' },
+ { "blacklist", 1, NULL, 'b' },
+#ifdef _WIN32
+ { "service", 1, NULL, 's' },
+#endif
{ NULL, 0, NULL, 0 }
};
int opt_ind = 0, ch, daemonize = 0, i, j, len;
path = optarg;
break;
case 'l':
- log_file = fopen(optarg, "a");
+ log_file_name = optarg;
+ log_file = fopen(log_file_name, "a");
if (!log_file) {
g_critical("unable to open specified log file: %s",
strerror(errno));
}
break;
}
+#ifdef _WIN32
+ case 's':
+ service = optarg;
+ if (strcmp(service, "install") == 0) {
+ return ga_install_service(path, log_file_name);
+ } else if (strcmp(service, "uninstall") == 0) {
+ return ga_uninstall_service();
+ } else {
+ printf("Unknown service command.\n");
+ return EXIT_FAILURE;
+ }
+ break;
+#endif
case 'h':
usage(argv[0]);
return 0;
g_critical("failed to initialize guest agent channel");
goto out_bad;
}
+#ifndef _WIN32
g_main_loop_run(ga_state->main_loop);
+#else
+ if (daemonize) {
+ SERVICE_TABLE_ENTRY service_table[] = {
+ { (char *)QGA_SERVICE_NAME, service_main }, { NULL, NULL } };
+ StartServiceCtrlDispatcher(service_table);
+ } else {
+ g_main_loop_run(ga_state->main_loop);
+ }
+#endif
ga_command_state_cleanup_all(ga_state->command_state);
ga_channel_free(ga_state->channel);