shim_wrappers.c 3.8 KB

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