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 "qemu/sockets.h" 26 #include "qapi/error.h" 27 #include "qemu-common.h" 28 #include "sysemu/char.h" 29 #include "io/channel-file.h" 30 31 #include "char-fd.h" 32 #include "char-io.h" 33 34 /* Called with chr_write_lock held. */ 35 static 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 42 static 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 72 static 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 81 static 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 87 static void fd_chr_update_read_handler(Chardev *chr, 88 GMainContext *context) 89 { 90 FDChardev *s = FD_CHARDEV(chr); 91 92 remove_fd_in_watch(chr); 93 if (s->ioc_in) { 94 chr->fd_in_tag = io_add_watch_poll(chr, s->ioc_in, 95 fd_chr_read_poll, 96 fd_chr_read, chr, 97 context); 98 } 99 } 100 101 static void char_fd_finalize(Object *obj) 102 { 103 Chardev *chr = CHARDEV(obj); 104 FDChardev *s = FD_CHARDEV(obj); 105 106 remove_fd_in_watch(chr); 107 if (s->ioc_in) { 108 object_unref(OBJECT(s->ioc_in)); 109 } 110 if (s->ioc_out) { 111 object_unref(OBJECT(s->ioc_out)); 112 } 113 114 qemu_chr_be_event(chr, CHR_EVENT_CLOSED); 115 } 116 117 int qmp_chardev_open_file_source(char *src, int flags, Error **errp) 118 { 119 int fd = -1; 120 121 TFR(fd = qemu_open(src, flags, 0666)); 122 if (fd == -1) { 123 error_setg_file_open(errp, errno, src); 124 } 125 return fd; 126 } 127 128 /* open a character device to a unix fd */ 129 void qemu_chr_open_fd(Chardev *chr, 130 int fd_in, int fd_out) 131 { 132 FDChardev *s = FD_CHARDEV(chr); 133 char *name; 134 135 s->ioc_in = QIO_CHANNEL(qio_channel_file_new_fd(fd_in)); 136 name = g_strdup_printf("chardev-file-in-%s", chr->label); 137 qio_channel_set_name(QIO_CHANNEL(s->ioc_in), name); 138 g_free(name); 139 s->ioc_out = QIO_CHANNEL(qio_channel_file_new_fd(fd_out)); 140 name = g_strdup_printf("chardev-file-out-%s", chr->label); 141 qio_channel_set_name(QIO_CHANNEL(s->ioc_out), name); 142 g_free(name); 143 qemu_set_nonblock(fd_out); 144 s->chr = chr; 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); 171