From 0e8089772d5b548f46753580df2a3cf37c4e1076 Mon Sep 17 00:00:00 2001 Message-Id: <0e8089772d5b548f46753580df2a3cf37c4e1076.1374754302.git.minovotn@redhat.com> In-Reply-To: <5d75a8513d08b33975bdf5971871c0c977167cd1.1374754301.git.minovotn@redhat.com> References: <5d75a8513d08b33975bdf5971871c0c977167cd1.1374754301.git.minovotn@redhat.com> From: Gerd Hoffmann Date: Mon, 24 Jun 2013 07:05:45 +0200 Subject: [PATCH 34/65] chardev: add file chardev support to chardev-add (qmp) RH-Author: Gerd Hoffmann Message-id: <1372057576-26450-35-git-send-email-kraxel@redhat.com> Patchwork-id: 52121 O-Subject: [RHEL-6.5 qemu-kvm PATCH v2 34/65] chardev: add file chardev support to chardev-add (qmp) Bugzilla: 676568 RH-Acked-by: Laszlo Ersek RH-Acked-by: Hans de Goede RH-Acked-by: Luiz Capitulino Add support for file chardevs. Output file is mandatory, input file is optional. Signed-off-by: Gerd Hoffmann (cherry picked from commit ffbdbe59acc5f175d6c05a5d90f0b7c865fafd5b) Conflicts: qemu-char.c qmp-commands.hx [ rhel6: use close instead of qemu_close ] --- qapi-schema.json | 16 +++++++++++++- qemu-char.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) Signed-off-by: Michal Novotny --- qapi-schema.json | 16 ++++++++++++++- qemu-char.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/qapi-schema.json b/qapi-schema.json index a2fda87..36aefa0 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -409,6 +409,19 @@ 'fd': 'String' } } ## +## @ChardevFile: +# +# Configuration info for file chardevs. +# +# @in: #optional The name of the input file +# @out: The name of the output file +# +# Since: 1.4 +## +{ 'type': 'ChardevFile', 'data': { '*in' : 'str', + 'out' : 'str' } } + +## # @ChardevBackend: # # Configuration info for the new chardev backend. @@ -417,7 +430,8 @@ ## { 'type': 'ChardevDummy', 'data': { } } -{ 'union': 'ChardevBackend', 'data': { 'null' : 'ChardevDummy' } } +{ 'union': 'ChardevBackend', 'data': { 'file' : 'ChardevFile', + 'null' : 'ChardevDummy' } } ## # @ChardevReturn: diff --git a/qemu-char.c b/qemu-char.c index 9ef1dad..1089b0b 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -2979,6 +2979,64 @@ CharDriverState *qemu_chr_find(const char *name) return NULL; } +#ifdef _WIN32 + +static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) +{ + HANDLE out; + + if (file->in) { + error_setg(errp, "input file not supported"); + return NULL; + } + + out = CreateFile(file->out, GENERIC_WRITE, FILE_SHARE_READ, NULL, + OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (out == INVALID_HANDLE_VALUE) { + error_setg(errp, "open %s failed", file->out); + return NULL; + } + return qemu_chr_open_win_file(out); +} + +#else /* WIN32 */ + +static int qmp_chardev_open_file_source(char *src, int flags, + Error **errp) +{ + int fd = -1; + + TFR(fd = qemu_open(src, flags, 0666)); + if (fd == -1) { + error_setg(errp, "open %s: %s", src, strerror(errno)); + } + return fd; +} + +static CharDriverState *qmp_chardev_open_file(ChardevFile *file, Error **errp) +{ + int flags, in = -1, out = -1; + + flags = O_WRONLY | O_TRUNC | O_CREAT | O_BINARY; + out = qmp_chardev_open_file_source(file->out, flags, errp); + if (error_is_set(errp)) { + return NULL; + } + + if (file->in) { + flags = O_RDONLY; + in = qmp_chardev_open_file_source(file->in, flags, errp); + if (error_is_set(errp)) { + close(out); + return NULL; + } + } + + return qemu_chr_open_fd(in, out); +} + +#endif /* WIN32 */ + ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, Error **errp) { @@ -2993,6 +3051,9 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, } switch (backend->kind) { + case CHARDEV_BACKEND_KIND_FILE: + chr = qmp_chardev_open_file(backend->file, errp); + break; case CHARDEV_BACKEND_KIND_NULL: chr = qemu_chr_open_null(NULL); break; -- 1.7.11.7