Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/chardev/char-io.c
Line
Count
Source
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
#include "qemu/osdep.h"
25
#include "chardev/char-io.h"
26
27
typedef struct IOWatchPoll {
28
    GSource parent;
29
30
    QIOChannel *ioc;
31
    GSource *src;
32
33
    IOCanReadHandler *fd_can_read;
34
    GSourceFunc fd_read;
35
    void *opaque;
36
    GMainContext *context;
37
} IOWatchPoll;
38
39
static IOWatchPoll *io_watch_poll_from_source(GSource *source)
40
96
{
41
96
    return container_of(source, IOWatchPoll, parent);
42
96
}
43
44
static gboolean io_watch_poll_prepare(GSource *source,
45
                                      gint *timeout)
46
48
{
47
48
    IOWatchPoll *iwp = io_watch_poll_from_source(source);
48
48
    bool now_active = iwp->fd_can_read(iwp->opaque) > 0;
49
48
    bool was_active = iwp->src != NULL;
50
48
    if (was_active == now_active) {
51
24
        return FALSE;
52
24
    }
53
54
    /*
55
     * We do not register the QIOChannel watch as a child GSource.
56
     * The 'prepare' function on the parent GSource will be
57
     * skipped if a child GSource's 'prepare' function indicates
58
     * readiness. We need this prepare function be guaranteed
59
     * to run on *every* iteration of the main loop, because
60
     * it is critical to ensure we remove the QIOChannel watch
61
     * if 'fd_can_read' indicates the frontend cannot receive
62
     * more data.
63
     */
64
24
    if (now_active) {
65
24
        iwp->src = qio_channel_create_watch(
66
24
            iwp->ioc, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
67
24
        g_source_set_callback(iwp->src, iwp->fd_read, iwp->opaque, NULL);
68
24
        g_source_attach(iwp->src, iwp->context);
69
24
    } else {
70
0
        g_source_destroy(iwp->src);
71
0
        g_source_unref(iwp->src);
72
0
        iwp->src = NULL;
73
0
    }
74
24
    return FALSE;
75
48
}
76
77
static gboolean io_watch_poll_check(GSource *source)
78
48
{
79
48
    return FALSE;
80
48
}
81
82
static gboolean io_watch_poll_dispatch(GSource *source, GSourceFunc callback,
83
                                       gpointer user_data)
84
0
{
85
0
    abort();
86
0
}
87
88
static void io_watch_poll_finalize(GSource *source)
89
24
{
90
24
    IOWatchPoll *iwp = io_watch_poll_from_source(source);
91
92
24
    object_unref(OBJECT(iwp->ioc));
93
94
24
    if (iwp->src) {
95
24
        g_source_destroy(iwp->src);
96
24
        g_source_unref(iwp->src);
97
24
        iwp->src = NULL;
98
24
    }
99
24
}
100
101
static GSourceFuncs io_watch_poll_funcs = {
102
    .prepare = io_watch_poll_prepare,
103
    .check = io_watch_poll_check,
104
    .dispatch = io_watch_poll_dispatch,
105
    .finalize = io_watch_poll_finalize,
106
};
107
108
GSource *io_add_watch_poll(Chardev *chr,
109
                        QIOChannel *ioc,
110
                        IOCanReadHandler *fd_can_read,
111
                        QIOChannelFunc fd_read,
112
                        gpointer user_data,
113
                        GMainContext *context)
114
63
{
115
63
    IOWatchPoll *iwp;
116
63
    char *name;
117
118
63
    iwp = (IOWatchPoll *) g_source_new(&io_watch_poll_funcs,
119
63
                                       sizeof(IOWatchPoll));
120
63
    iwp->fd_can_read = fd_can_read;
121
63
    iwp->opaque = user_data;
122
63
    iwp->ioc = ioc;
123
63
    object_ref(OBJECT(iwp->ioc));
124
125
63
    iwp->fd_read = (GSourceFunc) fd_read;
126
63
    iwp->src = NULL;
127
63
    iwp->context = context;
128
129
63
    name = g_strdup_printf("chardev-iowatch-%s", chr->label);
130
63
    g_source_set_name((GSource *)iwp, name);
131
63
    g_free(name);
132
133
63
    g_source_attach(&iwp->parent, context);
134
63
    g_source_unref(&iwp->parent);
135
63
    return (GSource *)iwp;
136
63
}
137
138
static void io_remove_watch_poll(GSource *source)
139
24
{
140
24
    IOWatchPoll *iwp;
141
142
24
    iwp = io_watch_poll_from_source(source);
143
24
    g_source_destroy(&iwp->parent);
144
24
}
145
146
void remove_fd_in_watch(Chardev *chr)
147
89
{
148
89
    if (chr->gsource) {
149
24
        io_remove_watch_poll(chr->gsource);
150
24
        chr->gsource = NULL;
151
24
    }
152
89
}
153
154
int io_channel_send_full(QIOChannel *ioc,
155
                         const void *buf, size_t len,
156
                         int *fds, size_t nfds)
157
72.4k
{
158
72.4k
    size_t offset = 0;
159
160
144k
    while (offset < len) {
161
72.4k
        ssize_t ret = 0;
162
72.4k
        struct iovec iov = { .iov_base = (char *)buf + offset,
163
72.4k
                             .iov_len = len - offset };
164
165
72.4k
        ret = qio_channel_writev_full(
166
72.4k
            ioc, &iov, 1,
167
72.4k
            fds, nfds, 0, NULL);
168
72.4k
        if (ret == QIO_CHANNEL_ERR_BLOCK) {
169
0
            if (offset) {
170
0
                return offset;
171
0
            }
172
173
0
            errno = EAGAIN;
174
0
            return -1;
175
72.4k
        } else if (ret < 0) {
176
0
            errno = EINVAL;
177
0
            return -1;
178
0
        }
179
180
72.4k
        offset += ret;
181
72.4k
    }
182
183
72.4k
    return offset;
184
72.4k
}
185
186
int io_channel_send(QIOChannel *ioc, const void *buf, size_t len)
187
72.4k
{
188
72.4k
    return io_channel_send_full(ioc, buf, len, NULL, 0);
189
72.4k
}
190
191
void remove_listener_fd_in_watch(Chardev *chr)
192
0
{
193
0
    ChardevClass *cc = CHARDEV_GET_CLASS(chr);
194
0
    if (cc->chr_listener_cleanup) {
195
0
        cc->chr_listener_cleanup(chr);
196
0
    }
197
0
}