master
c 155 lines 4.25 KB
Raw
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 */
24
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/module.h"
28 #include "qemu/option.h"
29 #include "chardev/char.h"
30
31 #ifdef _WIN32
32 #include "chardev/char-win.h"
33 #else
34 #include "chardev/char-fd.h"
35 #endif
36
37 static bool file_chr_open(Chardev *chr, ChardevBackend *backend, Error **errp)
38 {
39 ChardevFile *file = backend->u.file.data;
40 #ifdef _WIN32
41 HANDLE out;
42 DWORD accessmode;
43 DWORD flags;
44
45 if (file->in) {
46 error_setg(errp, "input file not supported");
47 return false;
48 }
49
50 if (file->has_append && file->append) {
51 /* Append to file if it already exists. */
52 accessmode = FILE_GENERIC_WRITE & ~FILE_WRITE_DATA;
53 flags = OPEN_ALWAYS;
54 } else {
55 /* Truncate file if it already exists. */
56 accessmode = GENERIC_WRITE;
57 flags = CREATE_ALWAYS;
58 }
59
60 out = CreateFile(file->out, accessmode, FILE_SHARE_READ, NULL, flags,
61 FILE_ATTRIBUTE_NORMAL, NULL);
62 if (out == INVALID_HANDLE_VALUE) {
63 error_setg(errp, "open %s failed", file->out);
64 return false;
65 }
66
67 win_chr_set_file(chr, out, false);
68 #else
69 int flags, in = -1, out;
70
71 flags = O_WRONLY | O_CREAT | O_BINARY;
72 if (file->has_append && file->append) {
73 flags |= O_APPEND;
74 } else {
75 flags |= O_TRUNC;
76 }
77
78 out = qmp_chardev_open_file_source(file->out, flags, errp);
79 if (out < 0) {
80 return false;
81 }
82
83 if (file->in) {
84 flags = O_RDONLY;
85 in = qmp_chardev_open_file_source(file->in, flags, errp);
86 if (in < 0) {
87 qemu_close(out);
88 return false;
89 }
90 }
91
92 if (!qemu_chr_open_fd(chr, in, out, errp)) {
93 qemu_close(out);
94 if (in >= 0) {
95 qemu_close(in);
96 }
97 return false;
98 }
99 #endif
100
101 qemu_chr_be_event(chr, CHR_EVENT_OPENED);
102 return true;
103 }
104
105 static void file_chr_parse(QemuOpts *opts, ChardevBackend *backend,
106 Error **errp)
107 {
108 const char *path = qemu_opt_get(opts, "path");
109 const char *inpath = qemu_opt_get(opts, "input-path");
110 ChardevFile *file;
111
112 backend->type = CHARDEV_BACKEND_KIND_FILE;
113 if (path == NULL) {
114 error_setg(errp, "chardev: file: no filename given");
115 return;
116 }
117 #ifdef _WIN32
118 if (inpath) {
119 error_setg(errp, "chardev: file: input-path not supported on Windows");
120 return;
121 }
122 #endif
123 file = backend->u.file.data = g_new0(ChardevFile, 1);
124 qemu_chr_parse_common(opts, qapi_ChardevFile_base(file));
125 file->out = g_strdup(path);
126 file->in = g_strdup(inpath);
127
128 file->has_append = true;
129 file->append = qemu_opt_get_bool(opts, "append", false);
130 }
131
132 static void char_file_class_init(ObjectClass *oc, const void *data)
133 {
134 ChardevClass *cc = CHARDEV_CLASS(oc);
135
136 cc->chr_parse = file_chr_parse;
137 cc->chr_open = file_chr_open;
138 }
139
140 static const TypeInfo char_file_type_info = {
141 .name = TYPE_CHARDEV_FILE,
142 #ifdef _WIN32
143 .parent = TYPE_CHARDEV_WIN,
144 #else
145 .parent = TYPE_CHARDEV_FD,
146 #endif
147 .class_init = char_file_class_init,
148 };
149
150 static void register_types(void)
151 {
152 type_register_static(&char_file_type_info);
153 }
154
155 type_init(register_types);