Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/monitor/fds.c
Line
Count
Source
1
/*
2
 * QEMU monitor file descriptor passing
3
 *
4
 * Copyright (c) 2003-2004 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 "monitor-internal.h"
27
#include "qapi/error.h"
28
#include "qapi/qapi-commands-misc.h"
29
#include "qapi/qmp/qerror.h"
30
#include "qemu/ctype.h"
31
#include "qemu/cutils.h"
32
#include "qemu/lockable.h"
33
#include "system/runstate.h"
34
35
/* file descriptors passed via SCM_RIGHTS */
36
typedef struct mon_fd_t mon_fd_t;
37
struct mon_fd_t {
38
    char *name;
39
    int fd;
40
    QLIST_ENTRY(mon_fd_t) next;
41
};
42
43
/* file descriptor associated with a file descriptor set */
44
typedef struct MonFdsetFd MonFdsetFd;
45
struct MonFdsetFd {
46
    int fd;
47
    char *opaque;
48
    QLIST_ENTRY(MonFdsetFd) next;
49
};
50
51
/* file descriptor set containing fds passed via SCM_RIGHTS */
52
typedef struct MonFdset MonFdset;
53
struct MonFdset {
54
    int64_t id;
55
    QLIST_HEAD(, MonFdsetFd) fds;
56
    QLIST_HEAD(, MonFdsetFd) dup_fds;
57
    QLIST_ENTRY(MonFdset) next;
58
};
59
60
/* Protects mon_fdsets */
61
static QemuMutex mon_fdsets_lock;
62
static QLIST_HEAD(, MonFdset) mon_fdsets;
63
64
static bool monitor_add_fd(Monitor *mon, int fd, const char *fdname, Error **errp)
65
0
{
66
0
    mon_fd_t *monfd;
67
68
0
    if (qemu_isdigit(fdname[0])) {
69
0
        close(fd);
70
0
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
71
0
                   "a name not starting with a digit");
72
0
        return false;
73
0
    }
74
75
    /* See close() call below. */
76
0
    qemu_mutex_lock(&mon->mon_lock);
77
0
    QLIST_FOREACH(monfd, &mon->fds, next) {
78
0
        int tmp_fd;
79
80
0
        if (strcmp(monfd->name, fdname) != 0) {
81
0
            continue;
82
0
        }
83
84
0
        tmp_fd = monfd->fd;
85
0
        monfd->fd = fd;
86
0
        qemu_mutex_unlock(&mon->mon_lock);
87
        /* Make sure close() is outside critical section */
88
0
        close(tmp_fd);
89
0
        return true;
90
0
    }
91
92
0
    monfd = g_new0(mon_fd_t, 1);
93
0
    monfd->name = g_strdup(fdname);
94
0
    monfd->fd = fd;
95
96
0
    QLIST_INSERT_HEAD(&mon->fds, monfd, next);
97
0
    qemu_mutex_unlock(&mon->mon_lock);
98
0
    return true;
99
0
}
100
101
#ifdef CONFIG_POSIX
102
void qmp_getfd(const char *fdname, Error **errp)
103
0
{
104
0
    Monitor *cur_mon = monitor_cur();
105
0
    int fd;
106
107
0
    fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
108
0
    if (fd == -1) {
109
0
        error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
110
0
        return;
111
0
    }
112
113
0
    monitor_add_fd(cur_mon, fd, fdname, errp);
114
0
}
115
#endif
116
117
void qmp_closefd(const char *fdname, Error **errp)
118
0
{
119
0
    Monitor *cur_mon = monitor_cur();
120
0
    mon_fd_t *monfd;
121
0
    int tmp_fd;
122
123
0
    qemu_mutex_lock(&cur_mon->mon_lock);
124
0
    QLIST_FOREACH(monfd, &cur_mon->fds, next) {
125
0
        if (strcmp(monfd->name, fdname) != 0) {
126
0
            continue;
127
0
        }
128
129
0
        QLIST_REMOVE(monfd, next);
130
0
        tmp_fd = monfd->fd;
131
0
        g_free(monfd->name);
132
0
        g_free(monfd);
133
0
        qemu_mutex_unlock(&cur_mon->mon_lock);
134
        /* Make sure close() is outside critical section */
135
0
        close(tmp_fd);
136
0
        return;
137
0
    }
138
139
0
    qemu_mutex_unlock(&cur_mon->mon_lock);
140
0
    error_setg(errp, "File descriptor named '%s' not found", fdname);
141
0
}
142
143
int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
144
0
{
145
0
    mon_fd_t *monfd;
146
147
0
    QEMU_LOCK_GUARD(&mon->mon_lock);
148
0
    QLIST_FOREACH(monfd, &mon->fds, next) {
149
0
        int fd;
150
151
0
        if (strcmp(monfd->name, fdname) != 0) {
152
0
            continue;
153
0
        }
154
155
0
        fd = monfd->fd;
156
0
        assert(fd >= 0);
157
158
        /* caller takes ownership of fd */
159
0
        QLIST_REMOVE(monfd, next);
160
0
        g_free(monfd->name);
161
0
        g_free(monfd);
162
163
0
        return fd;
164
0
    }
165
166
0
    error_setg(errp, "File descriptor named '%s' has not been found", fdname);
167
0
    return -1;
168
0
}
169
170
static void monitor_fdset_free(MonFdset *mon_fdset)
171
0
{
172
0
    QLIST_REMOVE(mon_fdset, next);
173
0
    g_free(mon_fdset);
174
0
}
175
176
static void monitor_fdset_free_if_empty(MonFdset *mon_fdset)
177
0
{
178
    /*
179
     * Only remove an empty fdset. The fds are owned by the user and
180
     * should have been removed with qmp_remove_fd(). The dup_fds are
181
     * owned by QEMU and should have been removed with qemu_close().
182
     */
183
0
    if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
184
0
        monitor_fdset_free(mon_fdset);
185
0
    }
186
0
}
187
188
static void monitor_fdset_fd_free(MonFdsetFd *mon_fdset_fd)
189
0
{
190
0
    close(mon_fdset_fd->fd);
191
0
    g_free(mon_fdset_fd->opaque);
192
0
    QLIST_REMOVE(mon_fdset_fd, next);
193
0
    g_free(mon_fdset_fd);
194
0
}
195
196
void monitor_fdsets_cleanup(void)
197
0
{
198
0
    MonFdset *mon_fdset;
199
0
    MonFdset *mon_fdset_next;
200
201
0
    QEMU_LOCK_GUARD(&mon_fdsets_lock);
202
0
    QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
203
0
        monitor_fdset_free_if_empty(mon_fdset);
204
0
    }
205
0
}
206
207
AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id,
208
                      const char *opaque, Error **errp)
209
0
{
210
0
    int fd;
211
0
    Monitor *mon = monitor_cur();
212
0
    AddfdInfo *fdinfo;
213
214
0
    fd = qemu_chr_fe_get_msgfd(&mon->chr);
215
0
    if (fd == -1) {
216
0
        error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
217
0
        goto error;
218
0
    }
219
220
0
    fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id, opaque, errp);
221
0
    if (fdinfo) {
222
0
        return fdinfo;
223
0
    }
224
225
0
error:
226
0
    if (fd != -1) {
227
0
        close(fd);
228
0
    }
229
0
    return NULL;
230
0
}
231
232
#ifdef WIN32
233
void qmp_get_win32_socket(const char *infos, const char *fdname, Error **errp)
234
{
235
    g_autofree WSAPROTOCOL_INFOW *info = NULL;
236
    gsize len;
237
    SOCKET sk;
238
    int fd;
239
240
    info = (void *)g_base64_decode(infos, &len);
241
    if (len != sizeof(*info)) {
242
        error_setg(errp, "Invalid WSAPROTOCOL_INFOW value");
243
        return;
244
    }
245
246
    sk = WSASocketW(FROM_PROTOCOL_INFO,
247
                    FROM_PROTOCOL_INFO,
248
                    FROM_PROTOCOL_INFO,
249
                    info, 0, 0);
250
    if (sk == INVALID_SOCKET) {
251
        error_setg_win32(errp, WSAGetLastError(), "Couldn't import socket");
252
        return;
253
    }
254
255
    fd = _open_osfhandle(sk, _O_BINARY);
256
    if (fd < 0) {
257
        error_setg_errno(errp, errno, "Failed to associate a FD with the SOCKET");
258
        closesocket(sk);
259
        return;
260
    }
261
262
    monitor_add_fd(monitor_cur(), fd, fdname, errp);
263
}
264
#endif
265
266
267
void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
268
0
{
269
0
    MonFdset *mon_fdset;
270
0
    MonFdsetFd *mon_fdset_fd, *mon_fdset_fd_next;
271
0
    char fd_str[60];
272
273
0
    QEMU_LOCK_GUARD(&mon_fdsets_lock);
274
0
    QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
275
0
        if (mon_fdset->id != fdset_id) {
276
0
            continue;
277
0
        }
278
0
        QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next,
279
0
                           mon_fdset_fd_next) {
280
0
            if (has_fd) {
281
0
                if (mon_fdset_fd->fd != fd) {
282
0
                    continue;
283
0
                }
284
0
                monitor_fdset_fd_free(mon_fdset_fd);
285
0
                break;
286
0
            } else {
287
0
                monitor_fdset_fd_free(mon_fdset_fd);
288
0
            }
289
0
        }
290
0
        if (has_fd && !mon_fdset_fd) {
291
0
            goto error;
292
0
        }
293
0
        monitor_fdset_free_if_empty(mon_fdset);
294
0
        return;
295
0
    }
296
297
0
error:
298
0
    if (has_fd) {
299
0
        snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
300
0
                 fdset_id, fd);
301
0
    } else {
302
0
        snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
303
0
    }
304
0
    error_setg(errp, "File descriptor named '%s' not found", fd_str);
305
0
}
306
307
FdsetInfoList *qmp_query_fdsets(Error **errp)
308
0
{
309
0
    MonFdset *mon_fdset;
310
0
    MonFdsetFd *mon_fdset_fd;
311
0
    FdsetInfoList *fdset_list = NULL;
312
313
0
    QEMU_LOCK_GUARD(&mon_fdsets_lock);
314
0
    QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
315
0
        FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
316
317
0
        fdset_info->fdset_id = mon_fdset->id;
318
319
0
        QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
320
0
            FdsetFdInfo *fdsetfd_info;
321
322
0
            fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
323
0
            fdsetfd_info->fd = mon_fdset_fd->fd;
324
0
            fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
325
326
0
            QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
327
0
        }
328
329
0
        QAPI_LIST_PREPEND(fdset_list, fdset_info);
330
0
    }
331
332
0
    return fdset_list;
333
0
}
334
335
AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
336
                                const char *opaque, Error **errp)
337
0
{
338
0
    MonFdset *mon_fdset = NULL;
339
0
    MonFdsetFd *mon_fdset_fd;
340
0
    AddfdInfo *fdinfo;
341
342
0
    QEMU_LOCK_GUARD(&mon_fdsets_lock);
343
0
    if (has_fdset_id) {
344
0
        QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
345
            /* Break if match found or match impossible due to ordering by ID */
346
0
            if (fdset_id <= mon_fdset->id) {
347
0
                if (fdset_id < mon_fdset->id) {
348
0
                    mon_fdset = NULL;
349
0
                }
350
0
                break;
351
0
            }
352
0
        }
353
0
    }
354
355
0
    if (mon_fdset == NULL) {
356
0
        int64_t fdset_id_prev = -1;
357
0
        MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
358
359
0
        if (has_fdset_id) {
360
0
            if (fdset_id < 0) {
361
0
                error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
362
0
                           "a non-negative value");
363
0
                return NULL;
364
0
            }
365
            /* Use specified fdset ID */
366
0
            QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
367
0
                mon_fdset_cur = mon_fdset;
368
0
                if (fdset_id < mon_fdset_cur->id) {
369
0
                    break;
370
0
                }
371
0
            }
372
0
        } else {
373
            /* Use first available fdset ID */
374
0
            QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
375
0
                mon_fdset_cur = mon_fdset;
376
0
                if (fdset_id_prev == mon_fdset_cur->id - 1) {
377
0
                    fdset_id_prev = mon_fdset_cur->id;
378
0
                    continue;
379
0
                }
380
0
                break;
381
0
            }
382
0
        }
383
384
0
        mon_fdset = g_malloc0(sizeof(*mon_fdset));
385
0
        if (has_fdset_id) {
386
0
            mon_fdset->id = fdset_id;
387
0
        } else {
388
0
            mon_fdset->id = fdset_id_prev + 1;
389
0
        }
390
391
        /* The fdset list is ordered by fdset ID */
392
0
        if (!mon_fdset_cur) {
393
0
            QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
394
0
        } else if (mon_fdset->id < mon_fdset_cur->id) {
395
0
            QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
396
0
        } else {
397
0
            QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
398
0
        }
399
0
    }
400
401
0
    mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
402
0
    mon_fdset_fd->fd = fd;
403
0
    mon_fdset_fd->opaque = g_strdup(opaque);
404
0
    QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
405
406
0
    fdinfo = g_malloc0(sizeof(*fdinfo));
407
0
    fdinfo->fdset_id = mon_fdset->id;
408
0
    fdinfo->fd = mon_fdset_fd->fd;
409
410
0
    return fdinfo;
411
0
}
412
413
int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags, Error **errp)
414
0
{
415
#ifdef _WIN32
416
    error_setg(errp, "Platform does not support fd passing (fdset)");
417
    return -ENOENT;
418
#else
419
0
    MonFdset *mon_fdset;
420
421
0
    QEMU_LOCK_GUARD(&mon_fdsets_lock);
422
0
    QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
423
0
        MonFdsetFd *mon_fdset_fd;
424
0
        MonFdsetFd *mon_fdset_fd_dup;
425
0
        int fd = -1;
426
0
        int dup_fd;
427
0
        int mon_fd_flags;
428
0
        int mask = O_ACCMODE;
429
430
0
#ifdef O_DIRECT
431
0
        mask |= O_DIRECT;
432
0
#endif
433
434
0
        if (mon_fdset->id != fdset_id) {
435
0
            continue;
436
0
        }
437
438
0
        QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
439
0
            mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
440
0
            if (mon_fd_flags == -1) {
441
0
                error_setg(errp, "Failed to read file status flags for fd=%d",
442
0
                           mon_fdset_fd->fd);
443
0
                return -1;
444
0
            }
445
446
0
            if ((flags & mask) == (mon_fd_flags & mask)) {
447
0
                fd = mon_fdset_fd->fd;
448
0
                break;
449
0
            }
450
0
        }
451
452
0
        if (fd == -1) {
453
0
            errno = EACCES;
454
0
            error_setg(errp,
455
0
                       "Failed to find file descriptor with matching flags=0x%x",
456
0
                       flags);
457
0
            return -1;
458
0
        }
459
460
0
        dup_fd = qemu_dup_flags(fd, flags);
461
0
        if (dup_fd == -1) {
462
0
            error_setg(errp, "Failed to dup() given file descriptor fd=%d", fd);
463
0
            return -1;
464
0
        }
465
466
0
        mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
467
0
        mon_fdset_fd_dup->fd = dup_fd;
468
0
        QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
469
0
        return dup_fd;
470
0
    }
471
472
0
    error_setg(errp, "Failed to find fdset /dev/fdset/%" PRId64, fdset_id);
473
0
    errno = ENOENT;
474
0
    return -1;
475
0
#endif
476
0
}
477
478
void monitor_fdset_dup_fd_remove(int dup_fd)
479
0
{
480
0
    MonFdset *mon_fdset;
481
0
    MonFdsetFd *mon_fdset_fd_dup;
482
483
0
    QEMU_LOCK_GUARD(&mon_fdsets_lock);
484
0
    QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
485
0
        QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
486
0
            if (mon_fdset_fd_dup->fd == dup_fd) {
487
0
                QLIST_REMOVE(mon_fdset_fd_dup, next);
488
0
                g_free(mon_fdset_fd_dup);
489
0
                monitor_fdset_free_if_empty(mon_fdset);
490
0
                return;
491
0
            }
492
0
        }
493
0
    }
494
0
}
495
496
int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
497
0
{
498
0
    int fd;
499
500
0
    if (!qemu_isdigit(fdname[0]) && mon) {
501
0
        fd = monitor_get_fd(mon, fdname, errp);
502
0
    } else {
503
0
        fd = qemu_parse_fd(fdname);
504
0
        if (fd < 0) {
505
0
            error_setg(errp, "Invalid file descriptor number '%s'",
506
0
                       fdname);
507
0
        }
508
0
    }
509
510
0
    return fd;
511
0
}
512
513
static void __attribute__((__constructor__)) monitor_fds_init(void)
514
61
{
515
61
    qemu_mutex_init(&mon_fdsets_lock);
516
61
}