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/types.h>
  21. #include <linux/un.h>
  22. #include <sys/eventfd.h>
  23. #include "api.h"
  24. #include "pal.h"
  25. #include "pal_debug.h"
  26. #include "pal_defs.h"
  27. #include "pal_error.h"
  28. #include "pal_internal.h"
  29. #include "pal_linux.h"
  30. #include "pal_linux_defs.h"
  31. #include "pal_linux_error.h"
  32. #include "pal_security.h"
  33. static inline int eventfd_type(int options) {
  34. int type = 0;
  35. if (options & PAL_OPTION_NONBLOCK)
  36. type |= EFD_NONBLOCK;
  37. if (options & PAL_OPTION_CLOEXEC)
  38. type |= EFD_CLOEXEC;
  39. if (options & PAL_OPTION_EFD_SEMAPHORE)
  40. type |= EFD_SEMAPHORE;
  41. return type;
  42. }
  43. /* `type` must be eventfd, `uri` & `access` & `share` are unused, `create` holds eventfd's initval,
  44. * `options` holds eventfd's flags */
  45. static int eventfd_pal_open(PAL_HANDLE* handle, const char* type, const char* uri, int access,
  46. int share, int create, int options) {
  47. int ret;
  48. __UNUSED(access);
  49. __UNUSED(share);
  50. if ((strcmp_static(type, URI_TYPE_EVENTFD) != 0) || (*uri != '\0')) {
  51. return -PAL_ERROR_INVAL;
  52. }
  53. /* Using create arg as a work-around (note: initval is uint32 but create is int32).*/
  54. ret = ocall_eventfd(create, eventfd_type(options));
  55. if (IS_ERR(ret))
  56. return unix_to_pal_error(ERRNO(ret));
  57. PAL_HANDLE hdl = malloc(HANDLE_SIZE(eventfd));
  58. SET_HANDLE_TYPE(hdl, eventfd);
  59. /* Note: using index 0, given that there is only 1 eventfd FD per pal-handle. */
  60. HANDLE_HDR(hdl)->flags = RFD(0) | WFD(0);
  61. hdl->eventfd.fd = ret;
  62. hdl->eventfd.nonblocking = (options & PAL_OPTION_NONBLOCK) ? PAL_TRUE : PAL_FALSE;
  63. *handle = hdl;
  64. return 0;
  65. }
  66. static int64_t eventfd_pal_read(PAL_HANDLE handle, uint64_t offset, uint64_t len, void* buffer) {
  67. if (offset)
  68. return -PAL_ERROR_INVAL;
  69. if (!IS_HANDLE_TYPE(handle, eventfd))
  70. return -PAL_ERROR_NOTCONNECTION;
  71. if (len < sizeof(uint64_t))
  72. return -PAL_ERROR_INVAL;
  73. /* TODO: verify that the value returned in buffer is somehow meaningful (to prevent Iago
  74. * attacks) */
  75. ssize_t bytes = ocall_read(handle->eventfd.fd, buffer, len);
  76. if (IS_ERR(bytes))
  77. return unix_to_pal_error(ERRNO(bytes));
  78. if (!bytes)
  79. return -PAL_ERROR_ENDOFSTREAM;
  80. return bytes;
  81. }
  82. static int64_t eventfd_pal_write(PAL_HANDLE handle, uint64_t offset, uint64_t len,
  83. const void* buffer) {
  84. if (offset)
  85. return -PAL_ERROR_INVAL;
  86. if (!IS_HANDLE_TYPE(handle, eventfd))
  87. return -PAL_ERROR_NOTCONNECTION;
  88. if (len < sizeof(uint64_t))
  89. return -PAL_ERROR_INVAL;
  90. ssize_t bytes = ocall_write(handle->eventfd.fd, buffer, len);
  91. if (IS_ERR(bytes))
  92. return unix_to_pal_error(ERRNO(bytes));
  93. return bytes;
  94. }
  95. /* invoked during poll operation on eventfd from LibOS. */
  96. static int eventfd_pal_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  97. int ret;
  98. if (handle->eventfd.fd == PAL_IDX_POISON)
  99. return -PAL_ERROR_BADHANDLE;
  100. attr->handle_type = HANDLE_HDR(handle)->type;
  101. attr->nonblocking = handle->eventfd.nonblocking;
  102. attr->disconnected = HANDLE_HDR(handle)->flags & ERROR(0);
  103. /* get number of bytes available for reading */
  104. ret = ocall_fionread(handle->eventfd.fd);
  105. if (IS_ERR(ret))
  106. return unix_to_pal_error(ERRNO(ret));
  107. attr->pending_size = ret;
  108. /* query if there is data available for reading */
  109. struct pollfd pfd = {.fd = handle->eventfd.fd, .events = POLLIN | POLLOUT, .revents = 0};
  110. ret = ocall_poll(&pfd, 1, 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. ocall_close(handle->eventfd.fd);
  126. handle->eventfd.fd = PAL_IDX_POISON;
  127. }
  128. return 0;
  129. }
  130. return 0;
  131. }
  132. struct handle_ops eventfd_ops = {
  133. .open = &eventfd_pal_open,
  134. .read = &eventfd_pal_read,
  135. .write = &eventfd_pal_write,
  136. .close = &eventfd_pal_close,
  137. .attrquerybyhdl = &eventfd_pal_attrquerybyhdl,
  138. };