4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/module.h"
28 #include "qemu/sockets.h"
29 #include "qapi/error.h"
30 #include "chardev/char.h"
31 #include "chardev/char-fe.h"
32 #include "io/channel-file.h"
34 #include "chardev/char-fd.h"
35 #include "chardev/char-io.h"
37 /* Called with chr_write_lock held. */
38 static int fd_chr_write(Chardev
*chr
, const uint8_t *buf
, int len
)
40 FDChardev
*s
= FD_CHARDEV(chr
);
46 return io_channel_send(s
->ioc_out
, buf
, len
);
49 static gboolean
fd_chr_read(QIOChannel
*chan
, GIOCondition cond
, void *opaque
)
51 Chardev
*chr
= CHARDEV(opaque
);
52 FDChardev
*s
= FD_CHARDEV(opaque
);
54 uint8_t buf
[CHR_READ_BUF_LEN
];
58 if (len
> s
->max_size
) {
65 ret
= qio_channel_read(
66 chan
, (gchar
*)buf
, len
, NULL
);
68 remove_fd_in_watch(chr
);
69 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
73 qemu_chr_be_write(chr
, buf
, ret
);
79 static int fd_chr_read_poll(void *opaque
)
81 Chardev
*chr
= CHARDEV(opaque
);
82 FDChardev
*s
= FD_CHARDEV(opaque
);
84 s
->max_size
= qemu_chr_be_can_write(chr
);
88 typedef struct FDSource
{
95 fd_source_prepare(GSource
*source
,
98 FDSource
*src
= (FDSource
*)source
;
100 return src
->cond
!= 0;
104 fd_source_check(GSource
*source
)
106 FDSource
*src
= (FDSource
*)source
;
108 return src
->cond
!= 0;
112 fd_source_dispatch(GSource
*source
, GSourceFunc callback
,
115 FDSource
*src
= (FDSource
*)source
;
116 FEWatchFunc func
= (FEWatchFunc
)callback
;
117 gboolean ret
= G_SOURCE_CONTINUE
;
120 ret
= func(NULL
, src
->cond
, user_data
);
127 static GSourceFuncs fd_source_funcs
= {
134 static GSource
*fd_source_new(FDChardev
*chr
)
136 return g_source_new(&fd_source_funcs
, sizeof(FDSource
));
139 static gboolean
child_func(GIOChannel
*source
,
140 GIOCondition condition
,
143 FDSource
*parent
= data
;
145 parent
->cond
|= condition
;
147 return G_SOURCE_CONTINUE
;
150 static GSource
*fd_chr_add_watch(Chardev
*chr
, GIOCondition cond
)
152 FDChardev
*s
= FD_CHARDEV(chr
);
153 g_autoptr(GSource
) source
= fd_source_new(s
);
156 g_autoptr(GSource
) child
= qio_channel_create_watch(s
->ioc_out
, cond
& ~G_IO_IN
);
157 g_source_set_callback(child
, (GSourceFunc
)child_func
, source
, NULL
);
158 g_source_add_child_source(source
, child
);
161 g_autoptr(GSource
) child
= qio_channel_create_watch(s
->ioc_in
, cond
& ~G_IO_OUT
);
162 g_source_set_callback(child
, (GSourceFunc
)child_func
, source
, NULL
);
163 g_source_add_child_source(source
, child
);
166 return g_steal_pointer(&source
);
169 static void fd_chr_update_read_handler(Chardev
*chr
)
171 FDChardev
*s
= FD_CHARDEV(chr
);
173 remove_fd_in_watch(chr
);
175 chr
->gsource
= io_add_watch_poll(chr
, s
->ioc_in
,
182 static void char_fd_finalize(Object
*obj
)
184 Chardev
*chr
= CHARDEV(obj
);
185 FDChardev
*s
= FD_CHARDEV(obj
);
187 remove_fd_in_watch(chr
);
189 object_unref(OBJECT(s
->ioc_in
));
192 object_unref(OBJECT(s
->ioc_out
));
195 qemu_chr_be_event(chr
, CHR_EVENT_CLOSED
);
198 int qmp_chardev_open_file_source(char *src
, int flags
, Error
**errp
)
202 TFR(fd
= qemu_open_old(src
, flags
, 0666));
204 error_setg_file_open(errp
, errno
, src
);
209 /* open a character device to a unix fd */
210 void qemu_chr_open_fd(Chardev
*chr
,
211 int fd_in
, int fd_out
)
213 FDChardev
*s
= FD_CHARDEV(chr
);
214 g_autofree
char *name
= NULL
;
217 qemu_set_nonblock(fd_out
);
220 if (fd_out
== fd_in
&& fd_in
>= 0) {
221 s
->ioc_in
= QIO_CHANNEL(qio_channel_file_new_fd(fd_in
));
222 name
= g_strdup_printf("chardev-file-%s", chr
->label
);
223 qio_channel_set_name(QIO_CHANNEL(s
->ioc_in
), name
);
224 s
->ioc_out
= QIO_CHANNEL(object_ref(s
->ioc_in
));
229 s
->ioc_in
= QIO_CHANNEL(qio_channel_file_new_fd(fd_in
));
230 name
= g_strdup_printf("chardev-file-in-%s", chr
->label
);
231 qio_channel_set_name(QIO_CHANNEL(s
->ioc_in
), name
);
235 s
->ioc_out
= QIO_CHANNEL(qio_channel_file_new_fd(fd_out
));
237 name
= g_strdup_printf("chardev-file-out-%s", chr
->label
);
238 qio_channel_set_name(QIO_CHANNEL(s
->ioc_out
), name
);
242 static void char_fd_class_init(ObjectClass
*oc
, void *data
)
244 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
246 cc
->chr_add_watch
= fd_chr_add_watch
;
247 cc
->chr_write
= fd_chr_write
;
248 cc
->chr_update_read_handler
= fd_chr_update_read_handler
;
251 static const TypeInfo char_fd_type_info
= {
252 .name
= TYPE_CHARDEV_FD
,
253 .parent
= TYPE_CHARDEV
,
254 .instance_size
= sizeof(FDChardev
),
255 .instance_finalize
= char_fd_finalize
,
256 .class_init
= char_fd_class_init
,
260 static void register_types(void)
262 type_register_static(&char_fd_type_info
);
265 type_init(register_types
);