Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Guest Agent |
| 3 | * |
| 4 | * Copyright IBM Corp. 2011 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Adam Litke <aglitke@linux.vnet.ibm.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 <stdbool.h> |
| 16 | #include <glib.h> |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 17 | #include <getopt.h> |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 18 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 19 | #include <syslog.h> |
Luiz Capitulino | 11d0f12 | 2012-02-28 11:03:03 -0300 | [diff] [blame] | 20 | #include <sys/wait.h> |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 21 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 22 | #include "json-streamer.h" |
| 23 | #include "json-parser.h" |
| 24 | #include "qint.h" |
| 25 | #include "qjson.h" |
| 26 | #include "qga/guest-agent-core.h" |
| 27 | #include "module.h" |
| 28 | #include "signal.h" |
| 29 | #include "qerror.h" |
| 30 | #include "error_int.h" |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 31 | #include "qapi/qmp-core.h" |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 32 | #include "qga/channel.h" |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 33 | #ifdef _WIN32 |
| 34 | #include "qga/service-win32.h" |
| 35 | #include <windows.h> |
| 36 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 37 | |
Michael Roth | 7868e26 | 2012-01-20 19:01:30 -0600 | [diff] [blame] | 38 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 39 | #define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_agent.0" |
Michael Roth | 7868e26 | 2012-01-20 19:01:30 -0600 | [diff] [blame] | 40 | #else |
| 41 | #define QGA_VIRTIO_PATH_DEFAULT "\\\\.\\Global\\org.qemu.guest_agent.0" |
| 42 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 43 | #define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid" |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 44 | #define QGA_SENTINEL_BYTE 0xFF |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 45 | |
| 46 | struct GAState { |
| 47 | JSONMessageParser parser; |
| 48 | GMainLoop *main_loop; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 49 | GAChannel *channel; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 50 | bool virtio; /* fastpath to check for virtio to deal with poll() quirks */ |
| 51 | GACommandState *command_state; |
| 52 | GLogLevelFlags log_level; |
| 53 | FILE *log_file; |
| 54 | bool logging_enabled; |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 55 | #ifdef _WIN32 |
| 56 | GAService service; |
| 57 | #endif |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 58 | bool delimit_response; |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame^] | 59 | bool frozen; |
| 60 | GList *blacklist; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 63 | struct GAState *ga_state; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 64 | |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame^] | 65 | /* commands that are safe to issue while filesystems are frozen */ |
| 66 | static const char *ga_freeze_whitelist[] = { |
| 67 | "guest-ping", |
| 68 | "guest-info", |
| 69 | "guest-sync", |
| 70 | "guest-fsfreeze-status", |
| 71 | "guest-fsfreeze-thaw", |
| 72 | NULL |
| 73 | }; |
| 74 | |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 75 | #ifdef _WIN32 |
| 76 | DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data, |
| 77 | LPVOID ctx); |
| 78 | VOID WINAPI service_main(DWORD argc, TCHAR *argv[]); |
| 79 | #endif |
| 80 | |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 81 | static void quit_handler(int sig) |
| 82 | { |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame^] | 83 | /* if we're frozen, don't exit unless we're absolutely forced to, |
| 84 | * because it's basically impossible for graceful exit to complete |
| 85 | * unless all log/pid files are on unfreezable filesystems. there's |
| 86 | * also a very likely chance killing the agent before unfreezing |
| 87 | * the filesystems is a mistake (or will be viewed as one later). |
| 88 | */ |
| 89 | if (ga_is_frozen(ga_state)) { |
| 90 | return; |
| 91 | } |
Stefan Weil | 2542bfd | 2011-08-28 21:45:40 +0200 | [diff] [blame] | 92 | g_debug("received signal num %d, quitting", sig); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 93 | |
| 94 | if (g_main_loop_is_running(ga_state->main_loop)) { |
| 95 | g_main_loop_quit(ga_state->main_loop); |
| 96 | } |
| 97 | } |
| 98 | |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 99 | #ifndef _WIN32 |
Luiz Capitulino | 11d0f12 | 2012-02-28 11:03:03 -0300 | [diff] [blame] | 100 | /* reap _all_ terminated children */ |
| 101 | static void child_handler(int sig) |
| 102 | { |
| 103 | int status; |
| 104 | while (waitpid(-1, &status, WNOHANG) > 0) /* NOTHING */; |
| 105 | } |
| 106 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 107 | static gboolean register_signal_handlers(void) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 108 | { |
Luiz Capitulino | 11d0f12 | 2012-02-28 11:03:03 -0300 | [diff] [blame] | 109 | struct sigaction sigact, sigact_chld; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 110 | int ret; |
| 111 | |
| 112 | memset(&sigact, 0, sizeof(struct sigaction)); |
| 113 | sigact.sa_handler = quit_handler; |
| 114 | |
| 115 | ret = sigaction(SIGINT, &sigact, NULL); |
| 116 | if (ret == -1) { |
| 117 | g_error("error configuring signal handler: %s", strerror(errno)); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 118 | return false; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 119 | } |
| 120 | ret = sigaction(SIGTERM, &sigact, NULL); |
| 121 | if (ret == -1) { |
| 122 | g_error("error configuring signal handler: %s", strerror(errno)); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 123 | return false; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 124 | } |
Luiz Capitulino | 11d0f12 | 2012-02-28 11:03:03 -0300 | [diff] [blame] | 125 | |
| 126 | memset(&sigact_chld, 0, sizeof(struct sigaction)); |
| 127 | sigact_chld.sa_handler = child_handler; |
| 128 | sigact_chld.sa_flags = SA_NOCLDSTOP; |
| 129 | ret = sigaction(SIGCHLD, &sigact_chld, NULL); |
| 130 | if (ret == -1) { |
| 131 | g_error("error configuring signal handler: %s", strerror(errno)); |
| 132 | } |
| 133 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 134 | return true; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 135 | } |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 136 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 137 | |
| 138 | static void usage(const char *cmd) |
| 139 | { |
| 140 | printf( |
Michael Roth | 4bdd041 | 2012-04-17 11:28:27 -0500 | [diff] [blame] | 141 | "Usage: %s [-m <method> -p <path>] [<options>]\n" |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 142 | "QEMU Guest Agent %s\n" |
| 143 | "\n" |
| 144 | " -m, --method transport method: one of unix-listen, virtio-serial, or\n" |
| 145 | " isa-serial (virtio-serial is the default)\n" |
Michael Roth | 4bdd041 | 2012-04-17 11:28:27 -0500 | [diff] [blame] | 146 | " -p, --path device/socket path (the default for virtio-serial is:\n" |
| 147 | " %s)\n" |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 148 | " -l, --logfile set logfile path, logs to stderr by default\n" |
| 149 | " -f, --pidfile specify pidfile (default is %s)\n" |
| 150 | " -v, --verbose log extra debugging information\n" |
| 151 | " -V, --version print version information and exit\n" |
| 152 | " -d, --daemonize become a daemon\n" |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 153 | #ifdef _WIN32 |
| 154 | " -s, --service service commands: install, uninstall\n" |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 155 | #endif |
Michael Roth | 4bdd041 | 2012-04-17 11:28:27 -0500 | [diff] [blame] | 156 | " -b, --blacklist comma-separated list of RPCs to disable (no spaces, \"?\"\n" |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 157 | " to list available RPCs)\n" |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 158 | " -h, --help display this help and exit\n" |
| 159 | "\n" |
| 160 | "Report bugs to <mdroth@linux.vnet.ibm.com>\n" |
| 161 | , cmd, QGA_VERSION, QGA_VIRTIO_PATH_DEFAULT, QGA_PIDFILE_DEFAULT); |
| 162 | } |
| 163 | |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 164 | static const char *ga_log_level_str(GLogLevelFlags level) |
| 165 | { |
| 166 | switch (level & G_LOG_LEVEL_MASK) { |
| 167 | case G_LOG_LEVEL_ERROR: |
| 168 | return "error"; |
| 169 | case G_LOG_LEVEL_CRITICAL: |
| 170 | return "critical"; |
| 171 | case G_LOG_LEVEL_WARNING: |
| 172 | return "warning"; |
| 173 | case G_LOG_LEVEL_MESSAGE: |
| 174 | return "message"; |
| 175 | case G_LOG_LEVEL_INFO: |
| 176 | return "info"; |
| 177 | case G_LOG_LEVEL_DEBUG: |
| 178 | return "debug"; |
| 179 | default: |
| 180 | return "user"; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | bool ga_logging_enabled(GAState *s) |
| 185 | { |
| 186 | return s->logging_enabled; |
| 187 | } |
| 188 | |
| 189 | void ga_disable_logging(GAState *s) |
| 190 | { |
| 191 | s->logging_enabled = false; |
| 192 | } |
| 193 | |
| 194 | void ga_enable_logging(GAState *s) |
| 195 | { |
| 196 | s->logging_enabled = true; |
| 197 | } |
| 198 | |
| 199 | static void ga_log(const gchar *domain, GLogLevelFlags level, |
| 200 | const gchar *msg, gpointer opaque) |
| 201 | { |
| 202 | GAState *s = opaque; |
| 203 | GTimeVal time; |
| 204 | const char *level_str = ga_log_level_str(level); |
| 205 | |
| 206 | if (!ga_logging_enabled(s)) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | level &= G_LOG_LEVEL_MASK; |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 211 | #ifndef _WIN32 |
Michael Roth | 8f47747 | 2011-08-11 15:38:11 -0500 | [diff] [blame] | 212 | if (domain && strcmp(domain, "syslog") == 0) { |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 213 | syslog(LOG_INFO, "%s: %s", level_str, msg); |
| 214 | } else if (level & s->log_level) { |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 215 | #else |
| 216 | if (level & s->log_level) { |
| 217 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 218 | g_get_current_time(&time); |
| 219 | fprintf(s->log_file, |
| 220 | "%lu.%lu: %s: %s\n", time.tv_sec, time.tv_usec, level_str, msg); |
| 221 | fflush(s->log_file); |
| 222 | } |
| 223 | } |
| 224 | |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 225 | void ga_set_response_delimited(GAState *s) |
| 226 | { |
| 227 | s->delimit_response = true; |
| 228 | } |
| 229 | |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame^] | 230 | static gint ga_strcmp(gconstpointer str1, gconstpointer str2) |
| 231 | { |
| 232 | return strcmp(str1, str2); |
| 233 | } |
| 234 | |
| 235 | /* disable commands that aren't safe for fsfreeze */ |
| 236 | static void ga_disable_non_whitelisted(void) |
| 237 | { |
| 238 | char **list_head, **list; |
| 239 | bool whitelisted; |
| 240 | int i; |
| 241 | |
| 242 | list_head = list = qmp_get_command_list(); |
| 243 | while (*list != NULL) { |
| 244 | whitelisted = false; |
| 245 | i = 0; |
| 246 | while (ga_freeze_whitelist[i] != NULL) { |
| 247 | if (strcmp(*list, ga_freeze_whitelist[i]) == 0) { |
| 248 | whitelisted = true; |
| 249 | } |
| 250 | i++; |
| 251 | } |
| 252 | if (!whitelisted) { |
| 253 | g_debug("disabling command: %s", *list); |
| 254 | qmp_disable_command(*list); |
| 255 | } |
| 256 | g_free(*list); |
| 257 | list++; |
| 258 | } |
| 259 | g_free(list_head); |
| 260 | } |
| 261 | |
| 262 | /* [re-]enable all commands, except those explictly blacklisted by user */ |
| 263 | static void ga_enable_non_blacklisted(GList *blacklist) |
| 264 | { |
| 265 | char **list_head, **list; |
| 266 | |
| 267 | list_head = list = qmp_get_command_list(); |
| 268 | while (*list != NULL) { |
| 269 | if (g_list_find_custom(blacklist, *list, ga_strcmp) == NULL && |
| 270 | !qmp_command_is_enabled(*list)) { |
| 271 | g_debug("enabling command: %s", *list); |
| 272 | qmp_enable_command(*list); |
| 273 | } |
| 274 | g_free(*list); |
| 275 | list++; |
| 276 | } |
| 277 | g_free(list_head); |
| 278 | } |
| 279 | |
| 280 | bool ga_is_frozen(GAState *s) |
| 281 | { |
| 282 | return s->frozen; |
| 283 | } |
| 284 | |
| 285 | void ga_set_frozen(GAState *s) |
| 286 | { |
| 287 | if (ga_is_frozen(s)) { |
| 288 | return; |
| 289 | } |
| 290 | /* disable all non-whitelisted (for frozen state) commands */ |
| 291 | ga_disable_non_whitelisted(); |
| 292 | g_warning("disabling logging due to filesystem freeze"); |
| 293 | ga_disable_logging(s); |
| 294 | s->frozen = true; |
| 295 | } |
| 296 | |
| 297 | void ga_unset_frozen(GAState *s) |
| 298 | { |
| 299 | if (!ga_is_frozen(s)) { |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | ga_enable_logging(s); |
| 304 | g_warning("logging re-enabled"); |
| 305 | |
| 306 | /* enable all disabled, non-blacklisted commands */ |
| 307 | ga_enable_non_blacklisted(s->blacklist); |
| 308 | s->frozen = false; |
| 309 | } |
| 310 | |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 311 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 312 | static void become_daemon(const char *pidfile) |
| 313 | { |
| 314 | pid_t pid, sid; |
| 315 | int pidfd; |
| 316 | char *pidstr = NULL; |
| 317 | |
| 318 | pid = fork(); |
| 319 | if (pid < 0) { |
| 320 | exit(EXIT_FAILURE); |
| 321 | } |
| 322 | if (pid > 0) { |
| 323 | exit(EXIT_SUCCESS); |
| 324 | } |
| 325 | |
| 326 | pidfd = open(pidfile, O_CREAT|O_WRONLY|O_EXCL, S_IRUSR|S_IWUSR); |
| 327 | if (pidfd == -1) { |
| 328 | g_critical("Cannot create pid file, %s", strerror(errno)); |
| 329 | exit(EXIT_FAILURE); |
| 330 | } |
| 331 | |
| 332 | if (asprintf(&pidstr, "%d", getpid()) == -1) { |
| 333 | g_critical("Cannot allocate memory"); |
| 334 | goto fail; |
| 335 | } |
| 336 | if (write(pidfd, pidstr, strlen(pidstr)) != strlen(pidstr)) { |
| 337 | free(pidstr); |
| 338 | g_critical("Failed to write pid file"); |
| 339 | goto fail; |
| 340 | } |
| 341 | |
| 342 | umask(0); |
| 343 | sid = setsid(); |
| 344 | if (sid < 0) { |
| 345 | goto fail; |
| 346 | } |
| 347 | if ((chdir("/")) < 0) { |
| 348 | goto fail; |
| 349 | } |
| 350 | |
| 351 | close(STDIN_FILENO); |
| 352 | close(STDOUT_FILENO); |
| 353 | close(STDERR_FILENO); |
| 354 | free(pidstr); |
| 355 | return; |
| 356 | |
| 357 | fail: |
| 358 | unlink(pidfile); |
| 359 | g_critical("failed to daemonize"); |
| 360 | exit(EXIT_FAILURE); |
| 361 | } |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 362 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 363 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 364 | static int send_response(GAState *s, QObject *payload) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 365 | { |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 366 | const char *buf; |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 367 | QString *payload_qstr, *response_qstr; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 368 | GIOStatus status; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 369 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 370 | g_assert(payload && s->channel); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 371 | |
| 372 | payload_qstr = qobject_to_json(payload); |
| 373 | if (!payload_qstr) { |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 377 | if (s->delimit_response) { |
| 378 | s->delimit_response = false; |
| 379 | response_qstr = qstring_new(); |
| 380 | qstring_append_chr(response_qstr, QGA_SENTINEL_BYTE); |
| 381 | qstring_append(response_qstr, qstring_get_str(payload_qstr)); |
| 382 | QDECREF(payload_qstr); |
| 383 | } else { |
| 384 | response_qstr = payload_qstr; |
| 385 | } |
| 386 | |
| 387 | qstring_append_chr(response_qstr, '\n'); |
| 388 | buf = qstring_get_str(response_qstr); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 389 | status = ga_channel_write_all(s->channel, buf, strlen(buf)); |
Michael Roth | 3cf0bed | 2012-02-07 13:56:48 -0600 | [diff] [blame] | 390 | QDECREF(response_qstr); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 391 | if (status != G_IO_STATUS_NORMAL) { |
| 392 | return -EIO; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 393 | } |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 394 | |
| 395 | return 0; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static void process_command(GAState *s, QDict *req) |
| 399 | { |
| 400 | QObject *rsp = NULL; |
| 401 | int ret; |
| 402 | |
| 403 | g_assert(req); |
| 404 | g_debug("processing command"); |
| 405 | rsp = qmp_dispatch(QOBJECT(req)); |
| 406 | if (rsp) { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 407 | ret = send_response(s, rsp); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 408 | if (ret) { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 409 | g_warning("error sending response: %s", strerror(ret)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 410 | } |
| 411 | qobject_decref(rsp); |
| 412 | } else { |
| 413 | g_warning("error getting response"); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /* handle requests/control events coming in over the channel */ |
| 418 | static void process_event(JSONMessageParser *parser, QList *tokens) |
| 419 | { |
| 420 | GAState *s = container_of(parser, GAState, parser); |
| 421 | QObject *obj; |
| 422 | QDict *qdict; |
| 423 | Error *err = NULL; |
| 424 | int ret; |
| 425 | |
| 426 | g_assert(s && parser); |
| 427 | |
| 428 | g_debug("process_event: called"); |
| 429 | obj = json_parser_parse_err(tokens, NULL, &err); |
| 430 | if (err || !obj || qobject_type(obj) != QTYPE_QDICT) { |
| 431 | qobject_decref(obj); |
| 432 | qdict = qdict_new(); |
| 433 | if (!err) { |
| 434 | g_warning("failed to parse event: unknown error"); |
| 435 | error_set(&err, QERR_JSON_PARSING); |
| 436 | } else { |
| 437 | g_warning("failed to parse event: %s", error_get_pretty(err)); |
| 438 | } |
| 439 | qdict_put_obj(qdict, "error", error_get_qobject(err)); |
| 440 | error_free(err); |
| 441 | } else { |
| 442 | qdict = qobject_to_qdict(obj); |
| 443 | } |
| 444 | |
| 445 | g_assert(qdict); |
| 446 | |
| 447 | /* handle host->guest commands */ |
| 448 | if (qdict_haskey(qdict, "execute")) { |
| 449 | process_command(s, qdict); |
| 450 | } else { |
| 451 | if (!qdict_haskey(qdict, "error")) { |
| 452 | QDECREF(qdict); |
| 453 | qdict = qdict_new(); |
| 454 | g_warning("unrecognized payload format"); |
| 455 | error_set(&err, QERR_UNSUPPORTED); |
| 456 | qdict_put_obj(qdict, "error", error_get_qobject(err)); |
| 457 | error_free(err); |
| 458 | } |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 459 | ret = send_response(s, QOBJECT(qdict)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 460 | if (ret) { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 461 | g_warning("error sending error response: %s", strerror(ret)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | QDECREF(qdict); |
| 466 | } |
| 467 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 468 | /* false return signals GAChannel to close the current client connection */ |
| 469 | static gboolean channel_event_cb(GIOCondition condition, gpointer data) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 470 | { |
| 471 | GAState *s = data; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 472 | gchar buf[QGA_READ_COUNT_DEFAULT+1]; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 473 | gsize count; |
| 474 | GError *err = NULL; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 475 | GIOStatus status = ga_channel_read(s->channel, buf, QGA_READ_COUNT_DEFAULT, &count); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 476 | if (err != NULL) { |
| 477 | g_warning("error reading channel: %s", err->message); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 478 | g_error_free(err); |
| 479 | return false; |
| 480 | } |
| 481 | switch (status) { |
| 482 | case G_IO_STATUS_ERROR: |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 483 | g_warning("error reading channel"); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 484 | return false; |
| 485 | case G_IO_STATUS_NORMAL: |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 486 | buf[count] = 0; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 487 | g_debug("read data, count: %d, data: %s", (int)count, buf); |
| 488 | json_message_parser_feed(&s->parser, (char *)buf, (int)count); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 489 | break; |
| 490 | case G_IO_STATUS_EOF: |
| 491 | g_debug("received EOF"); |
| 492 | if (!s->virtio) { |
| 493 | return false; |
| 494 | } |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 495 | case G_IO_STATUS_AGAIN: |
| 496 | /* virtio causes us to spin here when no process is attached to |
| 497 | * host-side chardev. sleep a bit to mitigate this |
| 498 | */ |
| 499 | if (s->virtio) { |
| 500 | usleep(100*1000); |
| 501 | } |
| 502 | return true; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 503 | default: |
| 504 | g_warning("unknown channel read status, closing"); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 505 | return false; |
| 506 | } |
| 507 | return true; |
| 508 | } |
| 509 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 510 | static gboolean channel_init(GAState *s, const gchar *method, const gchar *path) |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 511 | { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 512 | GAChannelMethod channel_method; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 513 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 514 | if (method == NULL) { |
| 515 | method = "virtio-serial"; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 516 | } |
| 517 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 518 | if (path == NULL) { |
| 519 | if (strcmp(method, "virtio-serial") != 0) { |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 520 | g_critical("must specify a path for this channel"); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 521 | return false; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 522 | } |
| 523 | /* try the default path for the virtio-serial port */ |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 524 | path = QGA_VIRTIO_PATH_DEFAULT; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 525 | } |
| 526 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 527 | if (strcmp(method, "virtio-serial") == 0) { |
| 528 | s->virtio = true; /* virtio requires special handling in some cases */ |
| 529 | channel_method = GA_CHANNEL_VIRTIO_SERIAL; |
| 530 | } else if (strcmp(method, "isa-serial") == 0) { |
| 531 | channel_method = GA_CHANNEL_ISA_SERIAL; |
| 532 | } else if (strcmp(method, "unix-listen") == 0) { |
| 533 | channel_method = GA_CHANNEL_UNIX_LISTEN; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 534 | } else { |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 535 | g_critical("unsupported channel method/type: %s", method); |
| 536 | return false; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 537 | } |
| 538 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 539 | s->channel = ga_channel_new(channel_method, path, channel_event_cb, s); |
| 540 | if (!s->channel) { |
| 541 | g_critical("failed to create guest agent channel"); |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | return true; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 546 | } |
| 547 | |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 548 | #ifdef _WIN32 |
| 549 | DWORD WINAPI service_ctrl_handler(DWORD ctrl, DWORD type, LPVOID data, |
| 550 | LPVOID ctx) |
| 551 | { |
| 552 | DWORD ret = NO_ERROR; |
| 553 | GAService *service = &ga_state->service; |
| 554 | |
| 555 | switch (ctrl) |
| 556 | { |
| 557 | case SERVICE_CONTROL_STOP: |
| 558 | case SERVICE_CONTROL_SHUTDOWN: |
| 559 | quit_handler(SIGTERM); |
| 560 | service->status.dwCurrentState = SERVICE_STOP_PENDING; |
| 561 | SetServiceStatus(service->status_handle, &service->status); |
| 562 | break; |
| 563 | |
| 564 | default: |
| 565 | ret = ERROR_CALL_NOT_IMPLEMENTED; |
| 566 | } |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | VOID WINAPI service_main(DWORD argc, TCHAR *argv[]) |
| 571 | { |
| 572 | GAService *service = &ga_state->service; |
| 573 | |
| 574 | service->status_handle = RegisterServiceCtrlHandlerEx(QGA_SERVICE_NAME, |
| 575 | service_ctrl_handler, NULL); |
| 576 | |
| 577 | if (service->status_handle == 0) { |
| 578 | g_critical("Failed to register extended requests function!\n"); |
| 579 | return; |
| 580 | } |
| 581 | |
| 582 | service->status.dwServiceType = SERVICE_WIN32; |
| 583 | service->status.dwCurrentState = SERVICE_RUNNING; |
| 584 | service->status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; |
| 585 | service->status.dwWin32ExitCode = NO_ERROR; |
| 586 | service->status.dwServiceSpecificExitCode = NO_ERROR; |
| 587 | service->status.dwCheckPoint = 0; |
| 588 | service->status.dwWaitHint = 0; |
| 589 | SetServiceStatus(service->status_handle, &service->status); |
| 590 | |
| 591 | g_main_loop_run(ga_state->main_loop); |
| 592 | |
| 593 | service->status.dwCurrentState = SERVICE_STOPPED; |
| 594 | SetServiceStatus(service->status_handle, &service->status); |
| 595 | } |
| 596 | #endif |
| 597 | |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 598 | int main(int argc, char **argv) |
| 599 | { |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 600 | const char *sopt = "hVvdm:p:l:f:b:s:"; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 601 | const char *method = NULL, *path = NULL, *pidfile = QGA_PIDFILE_DEFAULT; |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 602 | const char *log_file_name = NULL; |
| 603 | #ifdef _WIN32 |
| 604 | const char *service = NULL; |
| 605 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 606 | const struct option lopt[] = { |
| 607 | { "help", 0, NULL, 'h' }, |
| 608 | { "version", 0, NULL, 'V' }, |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 609 | { "logfile", 1, NULL, 'l' }, |
| 610 | { "pidfile", 1, NULL, 'f' }, |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 611 | { "verbose", 0, NULL, 'v' }, |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 612 | { "method", 1, NULL, 'm' }, |
| 613 | { "path", 1, NULL, 'p' }, |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 614 | { "daemonize", 0, NULL, 'd' }, |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 615 | { "blacklist", 1, NULL, 'b' }, |
| 616 | #ifdef _WIN32 |
| 617 | { "service", 1, NULL, 's' }, |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame^] | 618 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 619 | { NULL, 0, NULL, 0 } |
| 620 | }; |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 621 | int opt_ind = 0, ch, daemonize = 0, i, j, len; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 622 | GLogLevelFlags log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL; |
| 623 | FILE *log_file = stderr; |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame^] | 624 | GList *blacklist = NULL; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 625 | GAState *s; |
| 626 | |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 627 | module_call_init(MODULE_INIT_QAPI); |
| 628 | |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 629 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
| 630 | switch (ch) { |
| 631 | case 'm': |
| 632 | method = optarg; |
| 633 | break; |
| 634 | case 'p': |
| 635 | path = optarg; |
| 636 | break; |
| 637 | case 'l': |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 638 | log_file_name = optarg; |
| 639 | log_file = fopen(log_file_name, "a"); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 640 | if (!log_file) { |
| 641 | g_critical("unable to open specified log file: %s", |
| 642 | strerror(errno)); |
| 643 | return EXIT_FAILURE; |
| 644 | } |
| 645 | break; |
| 646 | case 'f': |
| 647 | pidfile = optarg; |
| 648 | break; |
| 649 | case 'v': |
| 650 | /* enable all log levels */ |
| 651 | log_level = G_LOG_LEVEL_MASK; |
| 652 | break; |
| 653 | case 'V': |
| 654 | printf("QEMU Guest Agent %s\n", QGA_VERSION); |
| 655 | return 0; |
| 656 | case 'd': |
| 657 | daemonize = 1; |
| 658 | break; |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 659 | case 'b': { |
| 660 | char **list_head, **list; |
| 661 | if (*optarg == '?') { |
| 662 | list_head = list = qmp_get_command_list(); |
| 663 | while (*list != NULL) { |
| 664 | printf("%s\n", *list); |
| 665 | g_free(*list); |
| 666 | list++; |
| 667 | } |
| 668 | g_free(list_head); |
| 669 | return 0; |
| 670 | } |
| 671 | for (j = 0, i = 0, len = strlen(optarg); i < len; i++) { |
| 672 | if (optarg[i] == ',') { |
| 673 | optarg[i] = 0; |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame^] | 674 | blacklist = g_list_append(blacklist, &optarg[j]); |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 675 | j = i + 1; |
| 676 | } |
| 677 | } |
| 678 | if (j < i) { |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame^] | 679 | blacklist = g_list_append(blacklist, &optarg[j]); |
Michael Roth | abd6cf6 | 2011-12-06 22:03:42 -0600 | [diff] [blame] | 680 | } |
| 681 | break; |
| 682 | } |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 683 | #ifdef _WIN32 |
| 684 | case 's': |
| 685 | service = optarg; |
| 686 | if (strcmp(service, "install") == 0) { |
| 687 | return ga_install_service(path, log_file_name); |
| 688 | } else if (strcmp(service, "uninstall") == 0) { |
| 689 | return ga_uninstall_service(); |
| 690 | } else { |
| 691 | printf("Unknown service command.\n"); |
| 692 | return EXIT_FAILURE; |
| 693 | } |
| 694 | break; |
| 695 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 696 | case 'h': |
| 697 | usage(argv[0]); |
| 698 | return 0; |
| 699 | case '?': |
| 700 | g_print("Unknown option, try '%s --help' for more information.\n", |
| 701 | argv[0]); |
| 702 | return EXIT_FAILURE; |
| 703 | } |
| 704 | } |
| 705 | |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 706 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 707 | if (daemonize) { |
| 708 | g_debug("starting daemon"); |
| 709 | become_daemon(pidfile); |
| 710 | } |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 711 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 712 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 713 | s = g_malloc0(sizeof(GAState)); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 714 | s->log_file = log_file; |
| 715 | s->log_level = log_level; |
| 716 | g_log_set_default_handler(ga_log, s); |
| 717 | g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR); |
| 718 | s->logging_enabled = true; |
Michael Roth | f22d85e | 2012-04-17 19:01:45 -0500 | [diff] [blame^] | 719 | s->frozen = false; |
| 720 | if (blacklist) { |
| 721 | s->blacklist = blacklist; |
| 722 | do { |
| 723 | g_debug("disabling command: %s", (char *)blacklist->data); |
| 724 | qmp_disable_command(blacklist->data); |
| 725 | blacklist = g_list_next(blacklist); |
| 726 | } while (blacklist); |
| 727 | } |
Michael Roth | e3d4d25 | 2011-07-19 15:41:55 -0500 | [diff] [blame] | 728 | s->command_state = ga_command_state_new(); |
| 729 | ga_command_state_init(s, s->command_state); |
| 730 | ga_command_state_init_all(s->command_state); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 731 | json_message_parser_init(&s->parser, process_event); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 732 | ga_state = s; |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 733 | #ifndef _WIN32 |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 734 | if (!register_signal_handlers()) { |
| 735 | g_critical("failed to register signal handlers"); |
| 736 | goto out_bad; |
| 737 | } |
Michael Roth | d8ca685 | 2012-01-19 22:04:34 -0600 | [diff] [blame] | 738 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 739 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 740 | s->main_loop = g_main_loop_new(NULL, false); |
| 741 | if (!channel_init(ga_state, method, path)) { |
| 742 | g_critical("failed to initialize guest agent channel"); |
| 743 | goto out_bad; |
| 744 | } |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 745 | #ifndef _WIN32 |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 746 | g_main_loop_run(ga_state->main_loop); |
Michael Roth | bc62fa0 | 2012-01-21 16:42:27 -0600 | [diff] [blame] | 747 | #else |
| 748 | if (daemonize) { |
| 749 | SERVICE_TABLE_ENTRY service_table[] = { |
| 750 | { (char *)QGA_SERVICE_NAME, service_main }, { NULL, NULL } }; |
| 751 | StartServiceCtrlDispatcher(service_table); |
| 752 | } else { |
| 753 | g_main_loop_run(ga_state->main_loop); |
| 754 | } |
| 755 | #endif |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 756 | |
Michael Roth | e3d4d25 | 2011-07-19 15:41:55 -0500 | [diff] [blame] | 757 | ga_command_state_cleanup_all(ga_state->command_state); |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 758 | ga_channel_free(ga_state->channel); |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 759 | |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 760 | if (daemonize) { |
| 761 | unlink(pidfile); |
| 762 | } |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 763 | return 0; |
Michael Roth | 125b310 | 2012-01-19 00:18:20 -0600 | [diff] [blame] | 764 | |
| 765 | out_bad: |
| 766 | if (daemonize) { |
| 767 | unlink(pidfile); |
| 768 | } |
| 769 | return EXIT_FAILURE; |
Michael Roth | 48ff7a6 | 2011-07-20 15:19:37 -0500 | [diff] [blame] | 770 | } |