1c767ae62SDaniel P. Berrange /* 2c767ae62SDaniel P. Berrange * QEMU I/O channels utility APIs 3c767ae62SDaniel P. Berrange * 4c767ae62SDaniel P. Berrange * Copyright (c) 2016 Red Hat, Inc. 5c767ae62SDaniel P. Berrange * 6c767ae62SDaniel P. Berrange * This library is free software; you can redistribute it and/or 7c767ae62SDaniel P. Berrange * modify it under the terms of the GNU Lesser General Public 8c767ae62SDaniel P. Berrange * License as published by the Free Software Foundation; either 9c8198bd5SChetan Pant * version 2.1 of the License, or (at your option) any later version. 10c767ae62SDaniel P. Berrange * 11c767ae62SDaniel P. Berrange * This library is distributed in the hope that it will be useful, 12c767ae62SDaniel P. Berrange * but WITHOUT ANY WARRANTY; without even the implied warranty of 13c767ae62SDaniel P. Berrange * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14c767ae62SDaniel P. Berrange * Lesser General Public License for more details. 15c767ae62SDaniel P. Berrange * 16c767ae62SDaniel P. Berrange * You should have received a copy of the GNU Lesser General Public 17c767ae62SDaniel P. Berrange * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18c767ae62SDaniel P. Berrange * 19c767ae62SDaniel P. Berrange */ 20c767ae62SDaniel P. Berrange 2130456d5bSPeter Maydell #include "qemu/osdep.h" 22c767ae62SDaniel P. Berrange #include "io/channel-util.h" 23c767ae62SDaniel P. Berrange #include "io/channel-file.h" 24c767ae62SDaniel P. Berrange #include "io/channel-socket.h" 25c767ae62SDaniel P. Berrange 26c767ae62SDaniel P. Berrange 27c767ae62SDaniel P. Berrange QIOChannel *qio_channel_new_fd(int fd, 28c767ae62SDaniel P. Berrange Error **errp) 29c767ae62SDaniel P. Berrange { 30c767ae62SDaniel P. Berrange QIOChannel *ioc; 31c767ae62SDaniel P. Berrange 32c767ae62SDaniel P. Berrange if (fd_is_socket(fd)) { 33c767ae62SDaniel P. Berrange ioc = QIO_CHANNEL(qio_channel_socket_new_fd(fd, errp)); 34c767ae62SDaniel P. Berrange } else { 35c767ae62SDaniel P. Berrange ioc = QIO_CHANNEL(qio_channel_file_new_fd(fd)); 36c767ae62SDaniel P. Berrange } 37c767ae62SDaniel P. Berrange return ioc; 38c767ae62SDaniel P. Berrange } 39*06e0f098SStefan Hajnoczi 40*06e0f098SStefan Hajnoczi 41*06e0f098SStefan Hajnoczi void qio_channel_util_set_aio_fd_handler(int read_fd, 42*06e0f098SStefan Hajnoczi AioContext *read_ctx, 43*06e0f098SStefan Hajnoczi IOHandler *io_read, 44*06e0f098SStefan Hajnoczi int write_fd, 45*06e0f098SStefan Hajnoczi AioContext *write_ctx, 46*06e0f098SStefan Hajnoczi IOHandler *io_write, 47*06e0f098SStefan Hajnoczi void *opaque) 48*06e0f098SStefan Hajnoczi { 49*06e0f098SStefan Hajnoczi if (read_fd == write_fd && read_ctx == write_ctx) { 50*06e0f098SStefan Hajnoczi aio_set_fd_handler(read_ctx, read_fd, io_read, io_write, 51*06e0f098SStefan Hajnoczi NULL, NULL, opaque); 52*06e0f098SStefan Hajnoczi } else { 53*06e0f098SStefan Hajnoczi if (read_ctx) { 54*06e0f098SStefan Hajnoczi aio_set_fd_handler(read_ctx, read_fd, io_read, NULL, 55*06e0f098SStefan Hajnoczi NULL, NULL, opaque); 56*06e0f098SStefan Hajnoczi } 57*06e0f098SStefan Hajnoczi if (write_ctx) { 58*06e0f098SStefan Hajnoczi aio_set_fd_handler(write_ctx, write_fd, NULL, io_write, 59*06e0f098SStefan Hajnoczi NULL, NULL, opaque); 60*06e0f098SStefan Hajnoczi } 61*06e0f098SStefan Hajnoczi } 62*06e0f098SStefan Hajnoczi } 63