]> git.proxmox.com Git - mirror_qemu.git/blame - chardev/char-file.c
hw/arm/aspeed: Set default CPU count using aspeed_soc_num_cpus()
[mirror_qemu.git] / chardev / char-file.c
CommitLineData
6fdafac1
MAL
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
922a01a0 24
6fdafac1
MAL
25#include "qemu/osdep.h"
26#include "qapi/error.h"
0b8fa32f 27#include "qemu/module.h"
922a01a0 28#include "qemu/option.h"
8228e353 29#include "chardev/char.h"
6fdafac1
MAL
30
31#ifdef _WIN32
8228e353 32#include "chardev/char-win.h"
6fdafac1 33#else
8228e353 34#include "chardev/char-fd.h"
6fdafac1
MAL
35#endif
36
37static void qmp_chardev_open_file(Chardev *chr,
38 ChardevBackend *backend,
39 bool *be_opened,
40 Error **errp)
41{
42 ChardevFile *file = backend->u.file.data;
43#ifdef _WIN32
44 HANDLE out;
45 DWORD accessmode;
46 DWORD flags;
47
8de69efa 48 if (file->in) {
6fdafac1
MAL
49 error_setg(errp, "input file not supported");
50 return;
51 }
52
53 if (file->has_append && file->append) {
54 /* Append to file if it already exists. */
55 accessmode = FILE_GENERIC_WRITE & ~FILE_WRITE_DATA;
56 flags = OPEN_ALWAYS;
57 } else {
58 /* Truncate file if it already exists. */
59 accessmode = GENERIC_WRITE;
60 flags = CREATE_ALWAYS;
61 }
62
63 out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags,
64 FILE_ATTRIBUTE_NORMAL, NULL);
65 if (out == INVALID_HANDLE_VALUE) {
66 error_setg(errp, "open %s failed", file->out);
67 return;
68 }
69
541815ff 70 win_chr_set_file(chr, out, false);
6fdafac1
MAL
71#else
72 int flags, in = -1, out;
73
74 flags = O_WRONLY | O_CREAT | O_BINARY;
75 if (file->has_append && file->append) {
76 flags |= O_APPEND;
77 } else {
78 flags |= O_TRUNC;
79 }
80
81 out = qmp_chardev_open_file_source(file->out, flags, errp);
82 if (out < 0) {
83 return;
84 }
85
8de69efa 86 if (file->in) {
6fdafac1
MAL
87 flags = O_RDONLY;
88 in = qmp_chardev_open_file_source(file->in, flags, errp);
89 if (in < 0) {
90 qemu_close(out);
91 return;
92 }
93 }
94
95 qemu_chr_open_fd(chr, in, out);
96#endif
97}
98
99static void qemu_chr_parse_file_out(QemuOpts *opts, ChardevBackend *backend,
100 Error **errp)
101{
102 const char *path = qemu_opt_get(opts, "path");
5b18a6bf 103 const char *inpath = qemu_opt_get(opts, "input-path");
6fdafac1
MAL
104 ChardevFile *file;
105
106 backend->type = CHARDEV_BACKEND_KIND_FILE;
107 if (path == NULL) {
108 error_setg(errp, "chardev: file: no filename given");
109 return;
110 }
5b18a6bf
PM
111#ifdef _WIN32
112 if (inpath) {
113 error_setg(errp, "chardev: file: input-path not supported on Windows");
114 return;
115 }
116#endif
6fdafac1
MAL
117 file = backend->u.file.data = g_new0(ChardevFile, 1);
118 qemu_chr_parse_common(opts, qapi_ChardevFile_base(file));
119 file->out = g_strdup(path);
5b18a6bf 120 file->in = g_strdup(inpath);
6fdafac1
MAL
121
122 file->has_append = true;
123 file->append = qemu_opt_get_bool(opts, "append", false);
124}
125
126static void char_file_class_init(ObjectClass *oc, void *data)
127{
128 ChardevClass *cc = CHARDEV_CLASS(oc);
129
130 cc->parse = qemu_chr_parse_file_out;
131 cc->open = qmp_chardev_open_file;
132}
133
134static const TypeInfo char_file_type_info = {
135 .name = TYPE_CHARDEV_FILE,
136#ifdef _WIN32
137 .parent = TYPE_CHARDEV_WIN,
138#else
139 .parent = TYPE_CHARDEV_FD,
140#endif
141 .class_init = char_file_class_init,
142};
143
144static void register_types(void)
145{
146 type_register_static(&char_file_type_info);
147}
148
149type_init(register_types);