channel-file.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * QEMU I/O channels files driver
  3. *
  4. * Copyright (c) 2015 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "io/channel-file.h"
  22. #include "io/channel-watch.h"
  23. #include "qapi/error.h"
  24. #include "qemu/module.h"
  25. #include "qemu/sockets.h"
  26. #include "trace.h"
  27. QIOChannelFile *
  28. qio_channel_file_new_fd(int fd)
  29. {
  30. QIOChannelFile *ioc;
  31. ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
  32. ioc->fd = fd;
  33. trace_qio_channel_file_new_fd(ioc, fd);
  34. return ioc;
  35. }
  36. QIOChannelFile *
  37. qio_channel_file_new_path(const char *path,
  38. int flags,
  39. mode_t mode,
  40. Error **errp)
  41. {
  42. QIOChannelFile *ioc;
  43. ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
  44. ioc->fd = qemu_open(path, flags, mode);
  45. if (ioc->fd < 0) {
  46. object_unref(OBJECT(ioc));
  47. error_setg_errno(errp, errno,
  48. "Unable to open %s", path);
  49. return NULL;
  50. }
  51. trace_qio_channel_file_new_path(ioc, path, flags, mode, ioc->fd);
  52. return ioc;
  53. }
  54. static void qio_channel_file_init(Object *obj)
  55. {
  56. QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
  57. ioc->fd = -1;
  58. }
  59. static void qio_channel_file_finalize(Object *obj)
  60. {
  61. QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
  62. if (ioc->fd != -1) {
  63. qemu_close(ioc->fd);
  64. ioc->fd = -1;
  65. }
  66. }
  67. static ssize_t qio_channel_file_readv(QIOChannel *ioc,
  68. const struct iovec *iov,
  69. size_t niov,
  70. int **fds,
  71. size_t *nfds,
  72. Error **errp)
  73. {
  74. QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
  75. ssize_t ret;
  76. retry:
  77. ret = readv(fioc->fd, iov, niov);
  78. if (ret < 0) {
  79. if (errno == EAGAIN) {
  80. return QIO_CHANNEL_ERR_BLOCK;
  81. }
  82. if (errno == EINTR) {
  83. goto retry;
  84. }
  85. error_setg_errno(errp, errno,
  86. "Unable to read from file");
  87. return -1;
  88. }
  89. return ret;
  90. }
  91. static ssize_t qio_channel_file_writev(QIOChannel *ioc,
  92. const struct iovec *iov,
  93. size_t niov,
  94. int *fds,
  95. size_t nfds,
  96. Error **errp)
  97. {
  98. QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
  99. ssize_t ret;
  100. retry:
  101. ret = writev(fioc->fd, iov, niov);
  102. if (ret <= 0) {
  103. if (errno == EAGAIN) {
  104. return QIO_CHANNEL_ERR_BLOCK;
  105. }
  106. if (errno == EINTR) {
  107. goto retry;
  108. }
  109. error_setg_errno(errp, errno,
  110. "Unable to write to file");
  111. return -1;
  112. }
  113. return ret;
  114. }
  115. static int qio_channel_file_set_blocking(QIOChannel *ioc,
  116. bool enabled,
  117. Error **errp)
  118. {
  119. QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
  120. if (enabled) {
  121. qemu_set_block(fioc->fd);
  122. } else {
  123. qemu_set_nonblock(fioc->fd);
  124. }
  125. return 0;
  126. }
  127. static off_t qio_channel_file_seek(QIOChannel *ioc,
  128. off_t offset,
  129. int whence,
  130. Error **errp)
  131. {
  132. QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
  133. off_t ret;
  134. ret = lseek(fioc->fd, offset, whence);
  135. if (ret == (off_t)-1) {
  136. error_setg_errno(errp, errno,
  137. "Unable to seek to offset %lld whence %d in file",
  138. (long long int)offset, whence);
  139. return -1;
  140. }
  141. return ret;
  142. }
  143. static int qio_channel_file_close(QIOChannel *ioc,
  144. Error **errp)
  145. {
  146. QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
  147. if (qemu_close(fioc->fd) < 0) {
  148. error_setg_errno(errp, errno,
  149. "Unable to close file");
  150. return -1;
  151. }
  152. fioc->fd = -1;
  153. return 0;
  154. }
  155. static void qio_channel_file_set_aio_fd_handler(QIOChannel *ioc,
  156. AioContext *ctx,
  157. IOHandler *io_read,
  158. IOHandler *io_write,
  159. void *opaque)
  160. {
  161. QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
  162. aio_set_fd_handler(ctx, fioc->fd, false, io_read, io_write, NULL, opaque);
  163. }
  164. static GSource *qio_channel_file_create_watch(QIOChannel *ioc,
  165. GIOCondition condition)
  166. {
  167. QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
  168. return qio_channel_create_fd_watch(ioc,
  169. fioc->fd,
  170. condition);
  171. }
  172. static void qio_channel_file_class_init(ObjectClass *klass,
  173. void *class_data G_GNUC_UNUSED)
  174. {
  175. QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
  176. ioc_klass->io_writev = qio_channel_file_writev;
  177. ioc_klass->io_readv = qio_channel_file_readv;
  178. ioc_klass->io_set_blocking = qio_channel_file_set_blocking;
  179. ioc_klass->io_seek = qio_channel_file_seek;
  180. ioc_klass->io_close = qio_channel_file_close;
  181. ioc_klass->io_create_watch = qio_channel_file_create_watch;
  182. ioc_klass->io_set_aio_fd_handler = qio_channel_file_set_aio_fd_handler;
  183. }
  184. static const TypeInfo qio_channel_file_info = {
  185. .parent = TYPE_QIO_CHANNEL,
  186. .name = TYPE_QIO_CHANNEL_FILE,
  187. .instance_size = sizeof(QIOChannelFile),
  188. .instance_init = qio_channel_file_init,
  189. .instance_finalize = qio_channel_file_finalize,
  190. .class_init = qio_channel_file_class_init,
  191. };
  192. static void qio_channel_file_register_types(void)
  193. {
  194. type_register_static(&qio_channel_file_info);
  195. }
  196. type_init(qio_channel_file_register_types);