From mboxrd@z Thu Jan 1 00:00:00 1970 From: Chris Webb Subject: Re: qemu-send.c (was Re: Since we're sharing, here's my kvmctl script) Date: Thu, 12 Jun 2008 09:35:23 +0100 Message-ID: <20080612083523.GD8555@arachsys.com> References: <20080612004201.GA7622@arachsys.com> <200806112110.51553.javier@guerrag.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Freddie Cash , kvm@vger.kernel.org To: Javier Guerra Giraldez Return-path: Received: from alpha.arachsys.com ([91.203.57.7]:59881 "EHLO alpha.arachsys.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757981AbYFLIhJ (ORCPT ); Thu, 12 Jun 2008 04:37:09 -0400 Content-Disposition: inline In-Reply-To: <200806112110.51553.javier@guerrag.com> Sender: kvm-owner@vger.kernel.org List-ID: Javier Guerra Giraldez writes: > On Wednesday 11 June 2008, Chris Webb wrote: > > Hi. I have a small 'qemu-send' utility for talking to a running qemu/kvm > > process whose monitor console listens on a filesystem socket, which I think > > might be a useful building block when extending these kinds of script to do > > things like migratation, pausing, and so on. The source is attached. > > there's a utility called socat that let's you send text to/from TCP sockets > and unix-domain sockets. it can even (temporarily) attach the terminal, or > use GNU's readline to regain interactive control of KVM/Qemu Hi. Yes, I'm aware of socat, netcat, tcpclient et al. and even have a similar pair of little unix/tcp/udp/syslogging utilities myself called sk/skd which I initially used for scripting our local kvm management system. However, it's a little bit clumsy to use these tools correctly from a shell script if you want to get back the command output intact. You need to open your connection to the unix server socket, wait for the prompt (skipping the welcome banner), send the command, copy the response out until you get a line '(qemu) ', then disconnect. For the same reason you can't do echo -e "GET / HTTP/1.1\n\n" >/dev/tcp/www.google.com/80 cat /dev/tcp/www.google.com/80 echo -e "GET / HTTP/1.1\n\n" >&3 cat <&3 instead, you need to avoid disconnecting from the socket in the middle of the command/response exchange. (In fact, with qemu, it nearly works anyway: the new connection gets all the output and the next prompt from the old one before the new banner, so you just have a couple of extra prompts, a command echo and a banner at the top and bottom to filter away. However, I'd be very reluctant to rely on this behaviour, and in particular on it not losing output between connections. The method I implemented in qemu-send.c should be robust again changes in the way qemu handles its monitor sockets.) To get the convenient syntax and behaviour I wanted, it felt easier and cleaner to write the few lines of C needed for a standalone utility rather than introduce a parsing shell script/function plus a dependency on one of sk/socat/netcat/tcpclient. I suspect also that I'm just more comfortable in C than sh; YMMV! Cheers, Chris.