shim_wrappers.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Copyright (C) 2014 Stony Brook University
  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. * shim_wrapper.c
  15. *
  16. * Implementation of system call "readv" and "writev".
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_utils.h>
  20. #include <shim_table.h>
  21. #include <shim_handle.h>
  22. #include <shim_fs.h>
  23. #include <shim_table.h>
  24. #include <pal.h>
  25. #include <pal_error.h>
  26. #include <errno.h>
  27. ssize_t shim_do_readv (int fd, const struct iovec * vec, int vlen)
  28. {
  29. if (!vec || test_user_memory((void *) vec, sizeof(*vec) * vlen, false))
  30. return -EINVAL;
  31. for (int i = 0 ; i < vlen ; i++) {
  32. if (vec[i].iov_base) {
  33. if (vec[i].iov_base + vec[i].iov_len <= vec[i].iov_base)
  34. return -EINVAL;
  35. if (test_user_memory(vec[i].iov_base, vec[i].iov_len, true))
  36. return -EFAULT;
  37. }
  38. }
  39. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  40. if (!hdl)
  41. return -EBADF;
  42. int ret = 0;
  43. if (!(hdl->acc_mode & MAY_READ) ||
  44. !hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->read) {
  45. ret = -EACCES;
  46. goto out;
  47. }
  48. ssize_t bytes = 0;
  49. for (int i = 0 ; i < vlen ; i++) {
  50. int b_vec;
  51. if (!vec[i].iov_base)
  52. continue;
  53. b_vec = hdl->fs->fs_ops->read(hdl, vec[i].iov_base, vec[i].iov_len);
  54. if (b_vec < 0) {
  55. ret = bytes ? : b_vec;
  56. goto out;
  57. }
  58. bytes += b_vec;
  59. }
  60. ret = bytes;
  61. out:
  62. put_handle(hdl);
  63. return ret;
  64. }
  65. /*
  66. * Writev can not be implemented as write because :
  67. * writev() has the same requirements as write() with respect to write requests
  68. * of <= PIPE_BUF bytes to a pipe or FIFO: no interleaving and no partial
  69. * writes. Neither of these can be guaranteed in the general case if writev()
  70. * simply calls write() for each struct iovec.
  71. */
  72. /*
  73. * The problem here is that we have to gaurantee Atomic writev
  74. *
  75. * Upon successful completion, writev() shall return the number of bytes
  76. * actually written. Otherwise, it shall return a value of -1, the file-pointer
  77. * shall remain unchanged, and errno shall be set to indicate an error
  78. */
  79. ssize_t shim_do_writev (int fd, const struct iovec * vec, int vlen)
  80. {
  81. if (!vec || test_user_memory((void *) vec, sizeof(*vec) * vlen, false))
  82. return -EINVAL;
  83. for (int i = 0 ; i < vlen ; i++) {
  84. if (vec[i].iov_base) {
  85. if (vec[i].iov_base + vec[i].iov_len < vec[i].iov_base)
  86. return -EINVAL;
  87. if (test_user_memory(vec[i].iov_base, vec[i].iov_len, false))
  88. return -EFAULT;
  89. }
  90. }
  91. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  92. if (!hdl)
  93. return -EBADF;
  94. int ret = 0;
  95. if (!(hdl->acc_mode & MAY_WRITE) ||
  96. !hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->write) {
  97. ret = -EACCES;
  98. goto out;
  99. }
  100. ssize_t bytes = 0;
  101. for (int i = 0 ; i < vlen ; i++)
  102. {
  103. int b_vec;
  104. if (!vec[i].iov_base)
  105. continue;
  106. b_vec = hdl->fs->fs_ops->write(hdl, vec[i].iov_base, vec[i].iov_len);
  107. if (b_vec < 0) {
  108. ret = bytes ? : b_vec;
  109. goto out;
  110. }
  111. bytes += b_vec;
  112. }
  113. ret = bytes;
  114. out:
  115. put_handle(hdl);
  116. return ret;
  117. }