2016-12-12 16:51:52 +01:00
|
|
|
/*
|
|
|
|
* QEMU System Emulator
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2018-02-01 12:18:46 +01:00
|
|
|
|
2016-12-12 16:51:52 +01:00
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qapi/error.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2018-02-01 12:18:46 +01:00
|
|
|
#include "qemu/option.h"
|
2017-01-26 14:19:46 +01:00
|
|
|
#include "chardev/char.h"
|
2016-12-12 16:51:52 +01:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2017-01-26 14:19:46 +01:00
|
|
|
#include "chardev/char-win.h"
|
2016-12-12 16:51:52 +01:00
|
|
|
#else
|
2017-01-26 14:19:46 +01:00
|
|
|
#include "chardev/char-fd.h"
|
2016-12-12 16:51:52 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void qmp_chardev_open_file(Chardev *chr,
|
|
|
|
ChardevBackend *backend,
|
|
|
|
bool *be_opened,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
ChardevFile *file = backend->u.file.data;
|
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE out;
|
|
|
|
DWORD accessmode;
|
|
|
|
DWORD flags;
|
|
|
|
|
2022-11-04 17:06:53 +01:00
|
|
|
if (file->in) {
|
2016-12-12 16:51:52 +01:00
|
|
|
error_setg(errp, "input file not supported");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file->has_append && file->append) {
|
|
|
|
/* Append to file if it already exists. */
|
|
|
|
accessmode = FILE_GENERIC_WRITE & ~FILE_WRITE_DATA;
|
|
|
|
flags = OPEN_ALWAYS;
|
|
|
|
} else {
|
|
|
|
/* Truncate file if it already exists. */
|
|
|
|
accessmode = GENERIC_WRITE;
|
|
|
|
flags = CREATE_ALWAYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (out == INVALID_HANDLE_VALUE) {
|
|
|
|
error_setg(errp, "open %s failed", file->out);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:34:42 +01:00
|
|
|
win_chr_set_file(chr, out, false);
|
2016-12-12 16:51:52 +01:00
|
|
|
#else
|
|
|
|
int flags, in = -1, out;
|
|
|
|
|
|
|
|
flags = O_WRONLY | O_CREAT | O_BINARY;
|
|
|
|
if (file->has_append && file->append) {
|
|
|
|
flags |= O_APPEND;
|
|
|
|
} else {
|
|
|
|
flags |= O_TRUNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
out = qmp_chardev_open_file_source(file->out, flags, errp);
|
|
|
|
if (out < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-04 17:06:53 +01:00
|
|
|
if (file->in) {
|
2016-12-12 16:51:52 +01:00
|
|
|
flags = O_RDONLY;
|
|
|
|
in = qmp_chardev_open_file_source(file->in, flags, errp);
|
|
|
|
if (in < 0) {
|
|
|
|
qemu_close(out);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_chr_open_fd(chr, in, out);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
const char *path = qemu_opt_get(opts, "path");
|
2023-04-13 17:07:24 +02:00
|
|
|
const char *inpath = qemu_opt_get(opts, "input-path");
|
2016-12-12 16:51:52 +01:00
|
|
|
ChardevFile *file;
|
|
|
|
|
|
|
|
backend->type = CHARDEV_BACKEND_KIND_FILE;
|
|
|
|
if (path == NULL) {
|
|
|
|
error_setg(errp, "chardev: file: no filename given");
|
|
|
|
return;
|
|
|
|
}
|
2023-04-13 17:07:24 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (inpath) {
|
|
|
|
error_setg(errp, "chardev: file: input-path not supported on Windows");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2016-12-12 16:51:52 +01:00
|
|
|
file = backend->u.file.data = g_new0(ChardevFile, 1);
|
|
|
|
qemu_chr_parse_common(opts, qapi_ChardevFile_base(file));
|
|
|
|
file->out = g_strdup(path);
|
2023-04-13 17:07:24 +02:00
|
|
|
file->in = g_strdup(inpath);
|
2016-12-12 16:51:52 +01:00
|
|
|
|
|
|
|
file->has_append = true;
|
|
|
|
file->append = qemu_opt_get_bool(opts, "append", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void char_file_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
ChardevClass *cc = CHARDEV_CLASS(oc);
|
|
|
|
|
|
|
|
cc->parse = qemu_chr_parse_file_out;
|
|
|
|
cc->open = qmp_chardev_open_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo char_file_type_info = {
|
|
|
|
.name = TYPE_CHARDEV_FILE,
|
|
|
|
#ifdef _WIN32
|
|
|
|
.parent = TYPE_CHARDEV_WIN,
|
|
|
|
#else
|
|
|
|
.parent = TYPE_CHARDEV_FD,
|
|
|
|
#endif
|
|
|
|
.class_init = char_file_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&char_file_type_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(register_types);
|