1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25#include "qemu/sockets.h"
26#include "qapi/error.h"
27#include "qemu-common.h"
28#include "chardev/char.h"
29#include "io/channel-file.h"
30
31#include "chardev/char-fd.h"
32#include "chardev/char-io.h"
33
34
35static int fd_chr_write(Chardev *chr, const uint8_t *buf, int len)
36{
37 FDChardev *s = FD_CHARDEV(chr);
38
39 return io_channel_send(s->ioc_out, buf, len);
40}
41
42static gboolean fd_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
43{
44 Chardev *chr = CHARDEV(opaque);
45 FDChardev *s = FD_CHARDEV(opaque);
46 int len;
47 uint8_t buf[CHR_READ_BUF_LEN];
48 ssize_t ret;
49
50 len = sizeof(buf);
51 if (len > s->max_size) {
52 len = s->max_size;
53 }
54 if (len == 0) {
55 return TRUE;
56 }
57
58 ret = qio_channel_read(
59 chan, (gchar *)buf, len, NULL);
60 if (ret == 0) {
61 remove_fd_in_watch(chr);
62 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
63 return FALSE;
64 }
65 if (ret > 0) {
66 qemu_chr_be_write(chr, buf, ret);
67 }
68
69 return TRUE;
70}
71
72static int fd_chr_read_poll(void *opaque)
73{
74 Chardev *chr = CHARDEV(opaque);
75 FDChardev *s = FD_CHARDEV(opaque);
76
77 s->max_size = qemu_chr_be_can_write(chr);
78 return s->max_size;
79}
80
81static GSource *fd_chr_add_watch(Chardev *chr, GIOCondition cond)
82{
83 FDChardev *s = FD_CHARDEV(chr);
84 return qio_channel_create_watch(s->ioc_out, cond);
85}
86
87static void fd_chr_update_read_handler(Chardev *chr)
88{
89 FDChardev *s = FD_CHARDEV(chr);
90
91 remove_fd_in_watch(chr);
92 if (s->ioc_in) {
93 chr->gsource = io_add_watch_poll(chr, s->ioc_in,
94 fd_chr_read_poll,
95 fd_chr_read, chr,
96 chr->gcontext);
97 }
98}
99
100static void char_fd_finalize(Object *obj)
101{
102 Chardev *chr = CHARDEV(obj);
103 FDChardev *s = FD_CHARDEV(obj);
104
105 remove_fd_in_watch(chr);
106 if (s->ioc_in) {
107 object_unref(OBJECT(s->ioc_in));
108 }
109 if (s->ioc_out) {
110 object_unref(OBJECT(s->ioc_out));
111 }
112
113 qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
114}
115
116int qmp_chardev_open_file_source(char *src, int flags, Error **errp)
117{
118 int fd = -1;
119
120 TFR(fd = qemu_open(src, flags, 0666));
121 if (fd == -1) {
122 error_setg_file_open(errp, errno, src);
123 }
124 return fd;
125}
126
127
128void qemu_chr_open_fd(Chardev *chr,
129 int fd_in, int fd_out)
130{
131 FDChardev *s = FD_CHARDEV(chr);
132 char *name;
133
134 s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in));
135 name = g_strdup_printf("chardev-file-in-%s", chr->label);
136 qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name);
137 g_free(name);
138 s->ioc_out = QIO_CHANNEL(qio_channel_file_new_fd(fd_out));
139 name = g_strdup_printf("chardev-file-out-%s", chr->label);
140 qio_channel_set_name(QIO_CHANNEL(s->ioc_out), name);
141 g_free(name);
142 qemu_set_nonblock(fd_out);
143}
144
145static void char_fd_class_init(ObjectClass *oc, void *data)
146{
147 ChardevClass *cc = CHARDEV_CLASS(oc);
148
149 cc->chr_add_watch = fd_chr_add_watch;
150 cc->chr_write = fd_chr_write;
151 cc->chr_update_read_handler = fd_chr_update_read_handler;
152}
153
154static const TypeInfo char_fd_type_info = {
155 .name = TYPE_CHARDEV_FD,
156 .parent = TYPE_CHARDEV,
157 .instance_size = sizeof(FDChardev),
158 .instance_finalize = char_fd_finalize,
159 .class_init = char_fd_class_init,
160 .abstract = true,
161};
162
163static void register_types(void)
164{
165 type_register_static(&char_fd_type_info);
166}
167
168type_init(register_types);
169