db_eventfd.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Copyright (C) 2019 Intel Corporation
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_eventfd.c
  15. *
  16. * This file contains operations to handle streams with URIs that have "eventfd:".
  17. */
  18. #include <asm/fcntl.h>
  19. #include <asm/poll.h>
  20. #include <linux/time.h>
  21. #include <linux/types.h>
  22. #include <linux/un.h>
  23. #include <sys/eventfd.h>
  24. #include "api.h"
  25. #include "pal.h"
  26. #include "pal_debug.h"
  27. #include "pal_defs.h"
  28. #include "pal_error.h"
  29. #include "pal_internal.h"
  30. #include "pal_linux.h"
  31. #include "pal_linux_defs.h"
  32. #include "pal_linux_error.h"
  33. #include "pal_security.h"
  34. static inline int eventfd_type(int options) {
  35. int type = 0;
  36. if (options & PAL_OPTION_NONBLOCK)
  37. type |= EFD_NONBLOCK;
  38. if (options & PAL_OPTION_CLOEXEC)
  39. type |= EFD_CLOEXEC;
  40. if (options & PAL_OPTION_EFD_SEMAPHORE)
  41. type |= EFD_SEMAPHORE;
  42. return type;
  43. }
  44. /* `type` must be eventfd, `uri` & `access` & `share` are unused, `create` holds eventfd's initval,
  45. * `options` holds eventfd's flags */
  46. static int eventfd_pal_open(PAL_HANDLE* handle, const char* type, const char* uri, int access,
  47. int share, int create, int options) {
  48. int ret;
  49. __UNUSED(access);
  50. __UNUSED(share);
  51. if ((strcmp_static(type, URI_TYPE_EVENTFD) != 0) || (*uri != '\0')) {
  52. return -PAL_ERROR_INVAL;
  53. }
  54. /* Using create arg as a work-around (note: initval is uint32 but create is int32).*/
  55. ret = INLINE_SYSCALL(eventfd2, 2, create, eventfd_type(options));
  56. if (IS_ERR(ret))
  57. return unix_to_pal_error(ERRNO(ret));
  58. PAL_HANDLE hdl = malloc(HANDLE_SIZE(eventfd));
  59. SET_HANDLE_TYPE(hdl, eventfd);
  60. /* Note: using index 0, given that there is only 1 eventfd FD per pal-handle. */
  61. HANDLE_HDR(hdl)->flags = RFD(0) | WFD(0);
  62. hdl->eventfd.fd = ret;
  63. hdl->eventfd.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE;
  64. *handle = hdl;
  65. return 0;
  66. }
  67. static int64_t eventfd_pal_read(PAL_HANDLE handle, uint64_t offset, uint64_t len, void* buffer) {
  68. if (offset)
  69. return -PAL_ERROR_INVAL;
  70. if (!IS_HANDLE_TYPE(handle, eventfd))
  71. return -PAL_ERROR_NOTCONNECTION;
  72. if (len < sizeof(uint64_t))
  73. return -PAL_ERROR_INVAL;
  74. int bytes = INLINE_SYSCALL(read, 3, handle->eventfd.fd, buffer, len);
  75. if (IS_ERR(bytes))
  76. return unix_to_pal_error(ERRNO(bytes));
  77. if (!bytes)
  78. return -PAL_ERROR_ENDOFSTREAM;
  79. return bytes;
  80. }
  81. static int64_t eventfd_pal_write(PAL_HANDLE handle, uint64_t offset, uint64_t len,
  82. const void* buffer) {
  83. if (offset)
  84. return -PAL_ERROR_INVAL;
  85. if (!IS_HANDLE_TYPE(handle, eventfd))
  86. return -PAL_ERROR_NOTCONNECTION;
  87. if (len < sizeof(uint64_t))
  88. return -PAL_ERROR_INVAL;
  89. int bytes = INLINE_SYSCALL(write, 3, handle->eventfd.fd, buffer, len);
  90. if (IS_ERR(bytes))
  91. return unix_to_pal_error(ERRNO(bytes));
  92. return bytes;
  93. }
  94. static int eventfd_pal_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  95. int ret;
  96. int val;
  97. if (handle->eventfd.fd == PAL_IDX_POISON)
  98. return -PAL_ERROR_BADHANDLE;
  99. attr->handle_type = HANDLE_HDR(handle)->type;
  100. attr->nonblocking = handle->eventfd.nonblocking;
  101. attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0);
  102. /* get number of bytes available for reading */
  103. ret = INLINE_SYSCALL(ioctl, 3, handle->eventfd.fd, FIONREAD, &val);
  104. if (IS_ERR(ret))
  105. return unix_to_pal_error(ERRNO(ret));
  106. attr->pending_size = val;
  107. /* query if there is data available for reading */
  108. struct pollfd pfd = {.fd = handle->eventfd.fd, .events = POLLIN | POLLOUT, .revents = 0};
  109. struct timespec tp = {0, 0};
  110. ret = INLINE_SYSCALL(ppoll, 5, &pfd, 1, &tp, NULL, 0);
  111. if (IS_ERR(ret))
  112. return unix_to_pal_error(ERRNO(ret));
  113. attr->readable = ret == 1 && (pfd.revents & (POLLIN | POLLERR | POLLHUP)) == POLLIN;
  114. attr->writable = ret == 1 && (pfd.revents & (POLLOUT | POLLERR | POLLHUP)) == POLLOUT;
  115. /* For future use, so that Linux host kernel can send notifications to user-space apps. App
  116. * receives virtual FD from LibOS, but the Linux-host eventfd is memorized here, such that this
  117. * Linux-host eventfd can be retrieved (by LibOS) during app's ioctl(). */
  118. attr->no_of_fds = 1;
  119. attr->fds[0] = handle->eventfd.fd;
  120. return 0;
  121. }
  122. static int eventfd_pal_close(PAL_HANDLE handle) {
  123. if (IS_HANDLE_TYPE(handle, eventfd)) {
  124. if (handle->eventfd.fd != PAL_IDX_POISON) {
  125. INLINE_SYSCALL(close, 1, handle->eventfd.fd);
  126. handle->eventfd.fd = PAL_IDX_POISON;
  127. }
  128. }
  129. return 0;
  130. }
  131. struct handle_ops eventfd_ops = {
  132. .open = &eventfd_pal_open,
  133. .read = &eventfd_pal_read,
  134. .write = &eventfd_pal_write,
  135. .close = &eventfd_pal_close,
  136. .attrquerybyhdl = &eventfd_pal_attrquerybyhdl,
  137. };