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