aboutsummaryrefslogtreecommitdiff
path: root/monitor.c
diff options
context:
space:
mode:
Diffstat (limited to 'monitor.c')
-rw-r--r--monitor.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/monitor.c b/monitor.c
index fec9a19a8..86ff296da 100644
--- a/monitor.c
+++ b/monitor.c
@@ -73,6 +73,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;
@@ -83,6 +91,7 @@ struct Monitor {
CPUState *mon_cpu;
BlockDriverCompletionFunc *password_completion_cb;
void *password_opaque;
+ LIST_HEAD(,mon_fd_t) fds;
LIST_ENTRY(Monitor) entry;
};
@@ -1729,6 +1738,90 @@ static void do_inject_mce(Monitor *mon,
}
#endif
+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);
+}
+
+int monitor_get_fd(Monitor *mon, const char *fdname)
+{
+ mon_fd_t *monfd;
+
+ LIST_FOREACH(monfd, &mon->fds, next) {
+ int fd;
+
+ if (strcmp(monfd->name, fdname) != 0) {
+ continue;
+ }
+
+ fd = monfd->fd;
+
+ /* caller takes ownership of fd */
+ LIST_REMOVE(monfd, next);
+ qemu_free(monfd->name);
+ qemu_free(monfd);
+
+ return fd;
+ }
+
+ return -1;
+}
+
static const mon_cmd_t mon_cmds[] = {
#include "qemu-monitor.h"
{ NULL, NULL, },