From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MOYan-0003Ay-Nz for qemu-devel@nongnu.org; Wed, 08 Jul 2009 10:57:53 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MOYai-00033B-Q2 for qemu-devel@nongnu.org; Wed, 08 Jul 2009 10:57:53 -0400 Received: from [199.232.76.173] (port=56158 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MOYai-00032L-5d for qemu-devel@nongnu.org; Wed, 08 Jul 2009 10:57:48 -0400 Received: from mail02.svc.cra.dublin.eircom.net ([159.134.118.18]:47749) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1MOYah-0000YH-L2 for qemu-devel@nongnu.org; Wed, 08 Jul 2009 10:57:47 -0400 From: Mark McLoughlin Date: Wed, 8 Jul 2009 15:57:26 +0100 Message-Id: <1247065048-15706-3-git-send-email-markmc@redhat.com> In-Reply-To: <1247065048-15706-2-git-send-email-markmc@redhat.com> References: <1247064963.3270.63.camel@blaa> <1247065048-15706-1-git-send-email-markmc@redhat.com> <1247065048-15706-2-git-send-email-markmc@redhat.com> Subject: [Qemu-devel] [PATCH 3/5] Add getfd and closefd monitor commands List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Mark McLoughlin Add monitor commands to support passing file descriptors via SCM_RIGHTS. getfd assigns the passed file descriptor a name for use with other monitor commands. closefd allows passed file descriptors to be closed. If a monitor command actually uses a named file descriptor, closefd will not be required. Signed-off-by: Mark McLoughlin --- monitor.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-monitor.hx | 18 ++++++++++++++ 2 files changed, 87 insertions(+), 0 deletions(-) diff --git a/monitor.c b/monitor.c index bad79fe..85fa137 100644 --- a/monitor.c +++ b/monitor.c @@ -70,6 +70,14 @@ typedef struct mon_cmd_t { const char *help; } mon_cmd_t; +/* file descriptors passed via SCM_RIGHTS */ +typedef struct mon_fd_t mon_fd_t; +struct mon_fd_t { + char *name; + int fd; + LIST_ENTRY(mon_fd_t) next; +}; + struct Monitor { CharDriverState *chr; int flags; @@ -80,6 +88,7 @@ struct Monitor { CPUState *mon_cpu; BlockDriverCompletionFunc *password_completion_cb; void *password_opaque; + LIST_HEAD(,mon_fd_t) fds; LIST_ENTRY(Monitor) entry; }; @@ -1677,6 +1686,66 @@ static void do_acl_remove(Monitor *mon, const char *aclname, const char *match) } } +static void do_getfd(Monitor *mon, const char *fdname) +{ + mon_fd_t *monfd; + int fd; + + fd = qemu_chr_get_msgfd(mon->chr); + if (fd == -1) { + monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n"); + return; + } + + if (qemu_isdigit(fdname[0])) { + monitor_printf(mon, "getfd: monitor names may not begin with a number\n"); + return; + } + + fd = dup(fd); + if (fd == -1) { + monitor_printf(mon, "Failed to dup() file descriptor: %s\n", + strerror(errno)); + return; + } + + LIST_FOREACH(monfd, &mon->fds, next) { + if (strcmp(monfd->name, fdname) != 0) { + continue; + } + + close(monfd->fd); + monfd->fd = fd; + return; + } + + monfd = qemu_mallocz(sizeof(mon_fd_t)); + monfd->name = qemu_strdup(fdname); + monfd->fd = fd; + + LIST_INSERT_HEAD(&mon->fds, monfd, next); +} + +static void do_closefd(Monitor *mon, const char *fdname) +{ + mon_fd_t *monfd; + + LIST_FOREACH(monfd, &mon->fds, next) { + if (strcmp(monfd->name, fdname) != 0) { + continue; + } + + LIST_REMOVE(monfd, next); + close(monfd->fd); + qemu_free(monfd->name); + qemu_free(monfd); + return; + } + + monitor_printf(mon, "Failed to find file descriptor named %s\n", + fdname); +} + static const mon_cmd_t mon_cmds[] = { #include "qemu-monitor.h" { NULL, NULL, }, diff --git a/qemu-monitor.hx b/qemu-monitor.hx index dc10b75..2279012 100644 --- a/qemu-monitor.hx +++ b/qemu-monitor.hx @@ -615,6 +615,24 @@ Remove all matches from the access control list, and set the default policy back to @code{deny}. ETEXI + { "getfd", "s", do_getfd, "getfd name", + "receive a file descriptor via SCM rights and assign it a name" }, +STEXI +@item getfd @var{fdname} +If a file descriptor is passed alongside this command using the SCM_RIGHTS +mechanism on unix sockets, it is stored using the name @var{fdname} for +later use by other monitor commands. +ETEXI + + { "closefd", "s", do_closefd, "closefd name", + "close a file descriptor previously passed via SCM rights" }, +STEXI +@item closefd @var{fdname} +Close the file descriptor previously assigned to @var{fdname} using the +@code{getfd} command. This is only needed if the file descriptor was never +used by another monitor command. +ETEXI + STEXI @end table ETEXI -- 1.6.2.5