From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:48606) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QjcOT-000531-C1 for qemu-devel@nongnu.org; Wed, 20 Jul 2011 15:25:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QjcOO-00059E-Ao for qemu-devel@nongnu.org; Wed, 20 Jul 2011 15:25:17 -0400 Received: from e37.co.us.ibm.com ([32.97.110.158]:58107) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QjcON-0004s5-AK for qemu-devel@nongnu.org; Wed, 20 Jul 2011 15:25:11 -0400 Received: from d03relay01.boulder.ibm.com (d03relay01.boulder.ibm.com [9.17.195.226]) by e37.co.us.ibm.com (8.14.4/8.13.1) with ESMTP id p6KJM47C019527 for ; Wed, 20 Jul 2011 13:22:04 -0600 Received: from d03av01.boulder.ibm.com (d03av01.boulder.ibm.com [9.17.195.167]) by d03relay01.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p6KJP8Px180576 for ; Wed, 20 Jul 2011 13:25:08 -0600 Received: from d03av01.boulder.ibm.com (loopback [127.0.0.1]) by d03av01.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p6KJP7iA010990 for ; Wed, 20 Jul 2011 13:25:08 -0600 Message-ID: <4E272B92.5090708@linux.vnet.ibm.com> Date: Wed, 20 Jul 2011 14:25:06 -0500 From: Michael Roth MIME-Version: 1.0 References: <1311108115-27128-1-git-send-email-mdroth@linux.vnet.ibm.com> <1311108115-27128-4-git-send-email-mdroth@linux.vnet.ibm.com> <20110720145603.4c19e899@doriath> In-Reply-To: <20110720145603.4c19e899@doriath> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v8 3/4] guest agent: qemu-ga daemon List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Luiz Capitulino Cc: aliguori@linux.vnet.ibm.com, agl@linux.vnet.ibm.com, qemu-devel@nongnu.org, Jes.Sorensen@redhat.com On 07/20/2011 12:56 PM, Luiz Capitulino wrote: > On Tue, 19 Jul 2011 15:41:54 -0500 > Michael Roth wrote: > >> This is the actual guest daemon, it listens for requests over a >> virtio-serial/isa-serial/unix socket channel and routes them through >> to dispatch routines, and writes the results back to the channel in >> a manner similar to QMP. >> >> A shorthand invocation: >> >> qemu-ga -d >> >> Is equivalent to: >> >> qemu-ga -m virtio-serial -p /dev/virtio-ports/org.qemu.guest_agent.= 0 \ >> -f /var/run/qemu-ga.pid -d >> >> Signed-off-by: Michael Roth >> --- >> Makefile | 8 +- >> configure | 1 + >> qemu-ga.c | 667 ++++++++++++++++++++++++++++++++++++++= ++++++++++ >> qga/guest-agent-core.h | 4 + >> 4 files changed, 677 insertions(+), 3 deletions(-) >> create mode 100644 qemu-ga.c >> >> diff --git a/Makefile b/Makefile >> index b8cdf0e..0d2e33d 100644 >> --- a/Makefile >> +++ b/Makefile >> @@ -140,7 +140,7 @@ endif >> ####################################################################= ## >> >> qemu-img.o: qemu-img-cmds.h >> -qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o cmd.o: $(GENERATED_HEADER= S) >> +qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o cmd.o qemu-ga.o: $(GENERA= TED_HEADERS) >> >> qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(oslib-obj-y= ) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-ti= mer-common.o >> >> @@ -184,13 +184,15 @@ test-qmp-commands: test-qmp-commands.o qfloat.o = qint.o qdict.o qstring.o qlist.o >> >> QGALIB=3Dqga/guest-agent-command-state.o >> >> +qemu-ga$(EXESUF): qemu-ga.o $(QGALIB) qemu-tool.o qemu-error.o error.= o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version= -obj-y) $(qapi-obj-y) qemu-timer-common.o qemu-sockets.o module.o qapi/qm= p-dispatch.o qapi/qmp-registry.o >> + >> QEMULIBS=3Dlibhw32 libhw64 libuser libdis libdis-user >> >> clean: >> # avoid old build problems by removing potentially incorrect old fil= es >> rm -f config.mak op-i386.h opc-i386.h gen-op-i386.h op-arm.h opc-ar= m.h gen-op-arm.h >> rm -f qemu-options.def >> - rm -f *.o *.d *.a *.lo $(TOOLS) TAGS cscope.* *.pod *~ */*~ >> + rm -f *.o *.d *.a *.lo $(TOOLS) qemu-ga TAGS cscope.* *.pod *~ */*~ >> rm -Rf .libs >> rm -f slirp/*.o slirp/*.d audio/*.o audio/*.d block/*.o block/*.d n= et/*.o net/*.d fsdev/*.o fsdev/*.d ui/*.o ui/*.d qapi/*.o qapi/*.d qga/*.= o qga/*.d >> rm -f qemu-img-cmds.h >> @@ -386,4 +388,4 @@ tarbin: >> $(mandir)/man8/qemu-nbd.8 >> >> # Include automatically generated dependency files >> --include $(wildcard *.d audio/*.d slirp/*.d block/*.d net/*.d ui/*.d = qapi/*.d) >> +-include $(wildcard *.d audio/*.d slirp/*.d block/*.d net/*.d ui/*.d = qapi/*.d qga/*.d) >> diff --git a/configure b/configure >> index 6a03002..445f674 100755 >> --- a/configure >> +++ b/configure >> @@ -2532,6 +2532,7 @@ if test "$softmmu" =3D yes ; then >> tools=3D"qemu-img\$(EXESUF) qemu-io\$(EXESUF) $tools" >> if [ "$linux" =3D "yes" -o "$bsd" =3D "yes" -o "$solaris" =3D "yes= " ] ; then >> tools=3D"qemu-nbd\$(EXESUF) $tools" >> + tools=3D"qemu-ga\$(EXESUF) $tools" >> if [ "$check_utests" =3D "yes" ]; then >> tools=3D"check-qint check-qstring check-qdict check-qlist $too= ls" >> tools=3D"check-qfloat check-qjson $tools" >> diff --git a/qemu-ga.c b/qemu-ga.c >> new file mode 100644 >> index 0000000..eb09100 >> --- /dev/null >> +++ b/qemu-ga.c >> @@ -0,0 +1,667 @@ >> +/* >> + * QEMU Guest Agent >> + * >> + * Copyright IBM Corp. 2011 >> + * >> + * Authors: >> + * Adam Litke >> + * Michael Roth >> + * >> + * This work is licensed under the terms of the GNU GPL, version 2 or= later. >> + * See the COPYING file in the top-level directory. >> + */ >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include "qemu_socket.h" >> +#include "json-streamer.h" >> +#include "json-parser.h" >> +#include "qint.h" >> +#include "qjson.h" >> +#include "qga/guest-agent-core.h" >> +#include "module.h" >> +#include "signal.h" >> +#include "qerror.h" >> +#include "error_int.h" >> + >> +#define QGA_VIRTIO_PATH_DEFAULT "/dev/virtio-ports/org.qemu.guest_age= nt.0" >> +#define QGA_PIDFILE_DEFAULT "/var/run/qemu-ga.pid" >> +#define QGA_BAUDRATE_DEFAULT B38400 /* for isa-serial channels */ >> +#define QGA_TIMEOUT_DEFAULT 30*1000 /* ms */ >> + >> +struct GAState { >> + JSONMessageParser parser; >> + GMainLoop *main_loop; >> + guint conn_id; >> + GSocket *conn_sock; >> + GIOChannel *conn_channel; >> + guint listen_id; >> + GSocket *listen_sock; >> + GIOChannel *listen_channel; >> + const char *path; >> + const char *method; >> + bool virtio; /* fastpath to check for virtio to deal with poll() = quirks */ >> + GACommandState *command_state; >> + GLogLevelFlags log_level; >> + FILE *log_file; >> + bool logging_enabled; >> +}; >> + >> +static struct GAState *ga_state; >> + >> +static void quit_handler(int sig) >> +{ >> + g_debug("recieved signal num %d, quitting", sig); >> + >> + if (g_main_loop_is_running(ga_state->main_loop)) { >> + g_main_loop_quit(ga_state->main_loop); >> + } >> +} >> + >> +static void register_signal_handlers(void) >> +{ >> + struct sigaction sigact; >> + int ret; >> + >> + memset(&sigact, 0, sizeof(struct sigaction)); >> + sigact.sa_handler =3D quit_handler; >> + >> + ret =3D sigaction(SIGINT,&sigact, NULL); >> + if (ret =3D=3D -1) { >> + g_error("error configuring signal handler: %s", strerror(errn= o)); >> + exit(EXIT_FAILURE); >> + } >> + ret =3D sigaction(SIGTERM,&sigact, NULL); >> + if (ret =3D=3D -1) { >> + g_error("error configuring signal handler: %s", strerror(errn= o)); >> + } >> +} >> + >> +static void usage(const char *cmd) >> +{ >> + printf( >> +"Usage: %s -c\n" >> +"QEMU Guest Agent %s\n" >> +"\n" >> +" -m, --method transport method: one of unix-listen, virtio-ser= ial, or\n" >> +" isa-serial (virtio-serial is the default)\n" >> +" -p, --path device/socket path (%s is the default for virtio= -serial)\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" >> +" -d, --daemonize become a daemon\n" >> +" -h, --help display this help and exit\n" >> +"\n" >> +"Report bugs to\n" >> + , cmd, QGA_VERSION, QGA_VIRTIO_PATH_DEFAULT, QGA_PIDFILE_DEFAULT)= ; >> +} >> + >> +static void conn_channel_close(GAState *s); >> + >> +static const char *ga_log_level_str(GLogLevelFlags level) >> +{ >> + switch (level& G_LOG_LEVEL_MASK) { >> + case G_LOG_LEVEL_ERROR: >> + return "error"; >> + case G_LOG_LEVEL_CRITICAL: >> + return "critical"; >> + case G_LOG_LEVEL_WARNING: >> + return "warning"; >> + case G_LOG_LEVEL_MESSAGE: >> + return "message"; >> + case G_LOG_LEVEL_INFO: >> + return "info"; >> + case G_LOG_LEVEL_DEBUG: >> + return "debug"; >> + default: >> + return "user"; >> + } >> +} >> + >> +bool ga_logging_enabled(GAState *s) >> +{ >> + return s->logging_enabled; >> +} >> + >> +void ga_disable_logging(GAState *s) >> +{ >> + s->logging_enabled =3D false; >> +} >> + >> +void ga_enable_logging(GAState *s) >> +{ >> + s->logging_enabled =3D true; >> +} >> + >> +static void ga_log(const gchar *domain, GLogLevelFlags level, >> + const gchar *msg, gpointer opaque) >> +{ >> + GAState *s =3D opaque; >> + GTimeVal time; >> + const char *level_str =3D ga_log_level_str(level); >> + >> + if (!ga_logging_enabled(s)) { >> + return; >> + } >> + >> + level&=3D G_LOG_LEVEL_MASK; >> + if (g_strcmp0(domain, "syslog") =3D=3D 0) { >> + syslog(LOG_INFO, "%s: %s", level_str, msg); >> + } else if (level& s->log_level) { >> + g_get_current_time(&time); >> + fprintf(s->log_file, >> + "%lu.%lu: %s: %s\n", time.tv_sec, time.tv_usec, level= _str, msg); >> + fflush(s->log_file); >> + } >> +} >> + >> +static void become_daemon(const char *pidfile) >> +{ >> + pid_t pid, sid; >> + int pidfd; >> + char *pidstr =3D NULL; >> + >> + pid =3D fork(); >> + if (pid< 0) { >> + exit(EXIT_FAILURE); >> + } >> + if (pid> 0) { >> + exit(EXIT_SUCCESS); >> + } >> + >> + pidfd =3D open(pidfile, O_CREAT|O_WRONLY|O_EXCL, S_IRUSR|S_IWUSR)= ; >> + if (pidfd =3D=3D -1) { >> + g_critical("Cannot create pid file, %s", strerror(errno)); >> + exit(EXIT_FAILURE); >> + } >> + >> + if (asprintf(&pidstr, "%d", getpid()) =3D=3D -1) { >> + g_critical("Cannot allocate memory"); >> + goto fail; >> + } >> + if (write(pidfd, pidstr, strlen(pidstr)) !=3D strlen(pidstr)) { >> + free(pidstr); >> + g_critical("Failed to write pid file"); >> + goto fail; >> + } >> + >> + umask(0); >> + sid =3D setsid(); >> + if (sid< 0) { >> + goto fail; >> + } >> + if ((chdir("/"))< 0) { >> + goto fail; >> + } >> + >> + close(STDIN_FILENO); >> + close(STDOUT_FILENO); >> + close(STDERR_FILENO); >> + free(pidstr); >> + return; >> + >> +fail: >> + unlink(pidfile); >> + g_critical("failed to daemonize"); >> + exit(EXIT_FAILURE); >> +} >> + >> +static int conn_channel_send_buf(GIOChannel *channel, const char *buf= , >> + gsize count) >> +{ >> + GError *err =3D NULL; >> + gsize written =3D 0; >> + GIOStatus status; >> + >> + while (count) { >> + status =3D g_io_channel_write_chars(channel, buf, count,&writ= ten,&err); >> + g_debug("sending data, count: %d", (int)count); >> + if (err !=3D NULL) { >> + g_warning("error sending newline: %s", err->message); >> + return err->code; >> + } >> + if (status =3D=3D G_IO_STATUS_ERROR || status =3D=3D G_IO_STA= TUS_EOF) { >> + return -EPIPE; >> + } >> + >> + if (status =3D=3D G_IO_STATUS_NORMAL) { >> + count -=3D written; >> + } >> + } >> + >> + return 0; >> +} >> + >> +static int conn_channel_send_payload(GIOChannel *channel, QObject *pa= yload) >> +{ >> + int ret =3D 0; >> + const char *buf; >> + QString *payload_qstr; >> + GError *err =3D NULL; >> + >> + g_assert(payload&& channel); >> + >> + payload_qstr =3D qobject_to_json(payload); >> + if (!payload_qstr) { >> + return -EINVAL; >> + } >> + >> + qstring_append_chr(payload_qstr, '\n'); >> + buf =3D qstring_get_str(payload_qstr); >> + ret =3D conn_channel_send_buf(channel, buf, strlen(buf)); >> + if (ret) { >> + goto out_free; >> + } >> + >> + g_io_channel_flush(channel,&err); >> + if (err !=3D NULL) { >> + g_warning("error flushing payload: %s", err->message); >> + ret =3D err->code; >> + goto out_free; >> + } >> + >> +out_free: >> + QDECREF(payload_qstr); >> + if (err) { >> + g_error_free(err); >> + } >> + return ret; >> +} >> + >> +static void process_command(GAState *s, QDict *req) >> +{ >> + QObject *rsp =3D NULL; >> + int ret; >> + >> + g_assert(req); >> + g_debug("processing command"); >> + rsp =3D qmp_dispatch(QOBJECT(req)); >> + if (rsp) { >> + ret =3D conn_channel_send_payload(s->conn_channel, rsp); >> + if (ret) { >> + g_warning("error sending payload: %s", strerror(ret)); >> + } >> + qobject_decref(rsp); >> + } else { >> + g_warning("error getting response"); >> + } >> +} >> + >> +/* handle requests/control events coming in over the channel */ >> +static void process_event(JSONMessageParser *parser, QList *tokens) >> +{ >> + GAState *s =3D container_of(parser, GAState, parser); >> + QObject *obj; >> + QDict *qdict; >> + Error *err =3D NULL; >> + int ret; >> + >> + g_assert(s&& parser); >> + >> + g_debug("process_event: called"); >> + obj =3D json_parser_parse_err(tokens, NULL,&err); >> + if (err || !obj || qobject_type(obj) !=3D QTYPE_QDICT) { >> + qobject_decref(obj); >> + qdict =3D qdict_new(); >> + if (!err) { >> + g_warning("failed to parse event: unknown error"); >> + error_set(&err, QERR_JSON_PARSING); >> + } else { >> + g_warning("failed to parse event: %s", error_get_pretty(e= rr)); >> + } >> + qdict_put_obj(qdict, "error", error_get_qobject(err)); >> + error_free(err); >> + } else { >> + qdict =3D qobject_to_qdict(obj); >> + } >> + >> + g_assert(qdict); >> + >> + /* handle host->guest commands */ >> + if (qdict_haskey(qdict, "execute")) { >> + process_command(s, qdict); >> + } else { >> + if (!qdict_haskey(qdict, "error")) { >> + QDECREF(qdict); >> + qdict =3D qdict_new(); >> + g_warning("unrecognized payload format"); >> + error_set(&err, QERR_UNSUPPORTED); >> + qdict_put_obj(qdict, "error", error_get_qobject(err)); >> + error_free(err); >> + } >> + ret =3D conn_channel_send_payload(s->conn_channel, QOBJECT(qd= ict)); >> + if (ret) { >> + g_warning("error sending payload: %s", strerror(ret)); >> + } >> + } >> + >> + QDECREF(qdict); >> +} >> + >> +static gboolean conn_channel_read(GIOChannel *channel, GIOCondition c= ondition, >> + gpointer data) >> +{ >> + GAState *s =3D data; >> + gchar buf[1024]; >> + gsize count; >> + GError *err =3D NULL; >> + memset(buf, 0, 1024); >> + GIOStatus status =3D g_io_channel_read_chars(channel, buf, 1024, >> +&count,&err); >> + if (err !=3D NULL) { >> + g_warning("error reading channel: %s", err->message); >> + conn_channel_close(s); >> + g_error_free(err); >> + return false; >> + } >> + switch (status) { >> + case G_IO_STATUS_ERROR: >> + g_warning("problem"); >> + return false; >> + case G_IO_STATUS_NORMAL: >> + g_debug("read data, count: %d, data: %s", (int)count, buf); >> + json_message_parser_feed(&s->parser, (char *)buf, (int)count)= ; >> + case G_IO_STATUS_AGAIN: >> + /* virtio causes us to spin here when no process is attached = to >> + * host-side chardev. sleep a bit to mitigate this >> + */ >> + if (s->virtio) { >> + usleep(100*1000); >> + } >> + return true; >> + case G_IO_STATUS_EOF: >> + g_debug("received EOF"); >> + conn_channel_close(s); >> + if (s->virtio) { >> + return true; >> + } >> + return false; >> + default: >> + g_warning("unknown channel read status, closing"); >> + conn_channel_close(s); >> + return false; >> + } >> + return true; >> +} >> + >> +static int conn_channel_add(GAState *s, int fd) >> +{ >> + GIOChannel *conn_channel; >> + guint conn_id; >> + GError *err =3D NULL; >> + >> + g_assert(s&& !s->conn_channel); >> + conn_channel =3D g_io_channel_unix_new(fd); >> + g_assert(conn_channel); >> + g_io_channel_set_encoding(conn_channel, NULL,&err); >> + if (err !=3D NULL) { >> + g_warning("error setting channel encoding to binary"); >> + g_error_free(err); >> + return -1; >> + } >> + conn_id =3D g_io_add_watch(conn_channel, G_IO_IN | G_IO_HUP, >> + conn_channel_read, s); >> + if (err !=3D NULL) { >> + g_warning("error adding io watch: %s", err->message); >> + g_error_free(err); >> + return -1; >> + } >> + s->conn_channel =3D conn_channel; >> + s->conn_id =3D conn_id; >> + return 0; >> +} >> + >> +static gboolean listen_channel_accept(GIOChannel *channel, >> + GIOCondition condition, gpointe= r data) >> +{ >> + GAState *s =3D data; >> + GError *err =3D NULL; >> + g_assert(channel !=3D NULL); >> + int ret; >> + bool accepted =3D false; >> + >> + s->conn_sock =3D g_socket_accept(s->listen_sock, NULL,&err); >> + if (err !=3D NULL) { >> + g_warning("error converting fd to gsocket: %s", err->message)= ; >> + g_error_free(err); >> + goto out; >> + } >> + ret =3D conn_channel_add(s, g_socket_get_fd(s->conn_sock)); >> + if (ret) { >> + g_warning("error setting up connection"); >> + goto out; >> + } >> + accepted =3D true; >> + >> +out: >> + /* only accept 1 connection at a time */ >> + return !accepted; >> +} >> + >> +/* start polling for readable events on listen fd, new=3D=3Dtrue >> + * indicates we should use the existing s->listen_channel >> + */ >> +static int listen_channel_add(GAState *s, int listen_fd, bool new) >> +{ >> + GError *err =3D NULL; >> + guint listen_id; >> + >> + if (new) { >> + s->listen_channel =3D g_io_channel_unix_new(listen_fd); >> + if (s->listen_sock) { >> + g_object_unref(s->listen_sock); >> + } >> + s->listen_sock =3D g_socket_new_from_fd(listen_fd,&err); >> + if (err !=3D NULL) { >> + g_warning("error converting fd to gsocket: %s", err->mess= age); >> + g_error_free(err); >> + return -1; >> + } >> + } >> + listen_id =3D g_io_add_watch(s->listen_channel, G_IO_IN, >> + listen_channel_accept, s); >> + if (err !=3D NULL) { >> + g_warning("error adding io watch: %s", err->message); >> + g_error_free(err); >> + return -1; >> + } >> + return 0; > > I'm getting the following error when building this on a F15 guest: > > qemu-ga.c: In function =E2=80=98listen_channel_add=E2=80=99: > qemu-ga.c:453:11: error: variable =E2=80=98listen_id=E2=80=99 set but n= ot used [-Werror=3Dunused-but-set-variable] > cc1: all warnings being treated as errors > > g_io_add_watch() doesn't seem to return an error. So if the fix is just= to > drop 'listen_id' and the last if statement altogether I can do it mysel= f, > otherwise you can send me only this patch. > > The series looks good otherwise. > Doh, sorry for not catching this. I did the same thing with conn_id as=20 well. I made a patch available here: http://repo.or.cz/w/qemu/mdroth.git/blobdiff/a8d38e5bb2d558918ac0bad1ee61= 8472ae34fe7a..581922e148f37e8f5844eeb11257eef8e9302709:/qemu-ga.c I tested the patch on a 32-bit F15 guest and it seems to do the trick. If you'd like you can just pull from git://repo.or.cz/qemu/mdroth.git qga-dev Which is this series (set3-v8) plus the patch right now. >> +} >> + >> +/* cleanup state for closed connection/session, start accepting new >> + * connections if we're in listening mode >> + */ >> +static void conn_channel_close(GAState *s) >> +{ >> + if (strcmp(s->method, "unix-listen") =3D=3D 0) { >> + g_io_channel_shutdown(s->conn_channel, true, NULL); >> + g_object_unref(s->conn_sock); >> + s->conn_sock =3D NULL; >> + listen_channel_add(s, 0, false); >> + } else if (strcmp(s->method, "virtio-serial") =3D=3D 0) { >> + /* we spin on EOF for virtio-serial, so back off a bit. also, >> + * dont close the connection in this case, it'll resume norma= l >> + * operation when another process connects to host chardev >> + */ >> + usleep(100*1000); >> + goto out_noclose; >> + } >> + g_io_channel_unref(s->conn_channel); >> + s->conn_channel =3D NULL; >> + s->conn_id =3D 0; >> +out_noclose: >> + return; >> +} >> + >> +static void init_guest_agent(GAState *s) >> +{ >> + struct termios tio; >> + int ret, fd; >> + >> + if (s->method =3D=3D NULL) { >> + /* try virtio-serial as our default */ >> + s->method =3D "virtio-serial"; >> + } >> + >> + if (s->path =3D=3D NULL) { >> + if (strcmp(s->method, "virtio-serial") !=3D 0) { >> + g_critical("must specify a path for this channel"); >> + exit(EXIT_FAILURE); >> + } >> + /* try the default path for the virtio-serial port */ >> + s->path =3D QGA_VIRTIO_PATH_DEFAULT; >> + } >> + >> + if (strcmp(s->method, "virtio-serial") =3D=3D 0) { >> + s->virtio =3D true; >> + fd =3D qemu_open(s->path, O_RDWR | O_NONBLOCK | O_ASYNC); >> + if (fd =3D=3D -1) { >> + g_critical("error opening channel: %s", strerror(errno)); >> + exit(EXIT_FAILURE); >> + } >> + ret =3D conn_channel_add(s, fd); >> + if (ret) { >> + g_critical("error adding channel to main loop"); >> + exit(EXIT_FAILURE); >> + } >> + } else if (strcmp(s->method, "isa-serial") =3D=3D 0) { >> + fd =3D qemu_open(s->path, O_RDWR | O_NOCTTY); >> + if (fd =3D=3D -1) { >> + g_critical("error opening channel: %s", strerror(errno)); >> + exit(EXIT_FAILURE); >> + } >> + tcgetattr(fd,&tio); >> + /* set up serial port for non-canonical, dumb byte streaming = */ >> + tio.c_iflag&=3D ~(IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK |= ISTRIP | >> + INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY= | >> + IMAXBEL); >> + tio.c_oflag =3D 0; >> + tio.c_lflag =3D 0; >> + tio.c_cflag |=3D QGA_BAUDRATE_DEFAULT; >> + /* 1 available byte min or reads will block (we'll set non-bl= ocking >> + * elsewhere, else we have to deal with read()=3D0 instead) >> + */ >> + tio.c_cc[VMIN] =3D 1; >> + tio.c_cc[VTIME] =3D 0; >> + /* flush everything waiting for read/xmit, it's garbage at th= is point */ >> + tcflush(fd, TCIFLUSH); >> + tcsetattr(fd, TCSANOW,&tio); >> + ret =3D conn_channel_add(s, fd); >> + if (ret) { >> + g_error("error adding channel to main loop"); >> + } >> + } else if (strcmp(s->method, "unix-listen") =3D=3D 0) { >> + fd =3D unix_listen(s->path, NULL, strlen(s->path)); >> + if (fd =3D=3D -1) { >> + g_critical("error opening path: %s", strerror(errno)); >> + exit(EXIT_FAILURE); >> + } >> + ret =3D listen_channel_add(s, fd, true); >> + if (ret) { >> + g_critical("error binding/listening to specified socket")= ; >> + exit(EXIT_FAILURE); >> + } >> + } else { >> + g_critical("unsupported channel method/type: %s", s->method); >> + exit(EXIT_FAILURE); >> + } >> + >> + json_message_parser_init(&s->parser, process_event); >> + s->main_loop =3D g_main_loop_new(NULL, false); >> +} >> + >> +int main(int argc, char **argv) >> +{ >> + const char *sopt =3D "hVvdm:p:l:f:"; >> + const char *method =3D NULL, *path =3D NULL, *pidfile =3D QGA_PID= FILE_DEFAULT; >> + const struct option lopt[] =3D { >> + { "help", 0, NULL, 'h' }, >> + { "version", 0, NULL, 'V' }, >> + { "logfile", 0, NULL, 'l' }, >> + { "pidfile", 0, NULL, 'f' }, >> + { "verbose", 0, NULL, 'v' }, >> + { "method", 0, NULL, 'm' }, >> + { "path", 0, NULL, 'p' }, >> + { "daemonize", 0, NULL, 'd' }, >> + { NULL, 0, NULL, 0 } >> + }; >> + int opt_ind =3D 0, ch, daemonize =3D 0; >> + GLogLevelFlags log_level =3D G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRIT= ICAL; >> + FILE *log_file =3D stderr; >> + GAState *s; >> + >> + while ((ch =3D getopt_long(argc, argv, sopt, lopt,&opt_ind)) !=3D= -1) { >> + switch (ch) { >> + case 'm': >> + method =3D optarg; >> + break; >> + case 'p': >> + path =3D optarg; >> + break; >> + case 'l': >> + log_file =3D fopen(optarg, "a"); >> + if (!log_file) { >> + g_critical("unable to open specified log file: %s", >> + strerror(errno)); >> + return EXIT_FAILURE; >> + } >> + break; >> + case 'f': >> + pidfile =3D optarg; >> + break; >> + case 'v': >> + /* enable all log levels */ >> + log_level =3D G_LOG_LEVEL_MASK; >> + break; >> + case 'V': >> + printf("QEMU Guest Agent %s\n", QGA_VERSION); >> + return 0; >> + case 'd': >> + daemonize =3D 1; >> + break; >> + case 'h': >> + usage(argv[0]); >> + return 0; >> + case '?': >> + g_print("Unknown option, try '%s --help' for more informa= tion.\n", >> + argv[0]); >> + return EXIT_FAILURE; >> + } >> + } >> + >> + if (daemonize) { >> + g_debug("starting daemon"); >> + become_daemon(pidfile); >> + } >> + >> + g_type_init(); >> + g_thread_init(NULL); >> + >> + s =3D qemu_mallocz(sizeof(GAState)); >> + s->conn_id =3D 0; >> + s->conn_channel =3D NULL; >> + s->path =3D path; >> + s->method =3D method; >> + s->log_file =3D log_file; >> + s->log_level =3D log_level; >> + g_log_set_default_handler(ga_log, s); >> + g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR); >> + s->logging_enabled =3D true; >> + ga_state =3D s; >> + >> + module_call_init(MODULE_INIT_QAPI); >> + init_guest_agent(ga_state); >> + register_signal_handlers(); >> + >> + g_main_loop_run(ga_state->main_loop); >> + >> + unlink(pidfile); >> + >> + return 0; >> +} >> diff --git a/qga/guest-agent-core.h b/qga/guest-agent-core.h >> index 688f120..66d1729 100644 >> --- a/qga/guest-agent-core.h >> +++ b/qga/guest-agent-core.h >> @@ -15,6 +15,7 @@ >> >> #define QGA_VERSION "1.0" >> >> +typedef struct GAState GAState; >> typedef struct GACommandState GACommandState; >> >> void ga_command_state_add(GACommandState *cs, >> @@ -23,3 +24,6 @@ void ga_command_state_add(GACommandState *cs, >> void ga_command_state_init_all(GACommandState *cs); >> void ga_command_state_cleanup_all(GACommandState *cs); >> GACommandState *ga_command_state_new(void); >> +bool ga_logging_enabled(GAState *s); >> +void ga_disable_logging(GAState *s); >> +void ga_enable_logging(GAState *s); >