shim_wrappers.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_wrapper.c
  17. *
  18. * Implementation of system call "readv" and "writev".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_utils.h>
  22. #include <shim_table.h>
  23. #include <shim_handle.h>
  24. #include <shim_fs.h>
  25. #include <shim_table.h>
  26. #include <pal.h>
  27. #include <pal_error.h>
  28. #include <errno.h>
  29. ssize_t shim_do_readv (int fd, const struct iovec * vec, int vlen)
  30. {
  31. if (!vec)
  32. return -EINVAL;
  33. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  34. if (!hdl)
  35. return -EBADF;
  36. int ret = 0;
  37. if (!(hdl->acc_mode & MAY_READ) ||
  38. !hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->read) {
  39. ret = -EACCES;
  40. goto out;
  41. }
  42. ssize_t bytes = 0;
  43. for (int i = 0 ; i < vlen ; i++) {
  44. int b_vec;
  45. b_vec = hdl->fs->fs_ops->read(hdl, vec[i].iov_base, vec[i].iov_len);
  46. if (b_vec < 0) {
  47. ret = bytes ? : b_vec;
  48. goto out;
  49. }
  50. bytes += b_vec;
  51. }
  52. ret = bytes;
  53. out:
  54. put_handle(hdl);
  55. return ret;
  56. }
  57. /*
  58. * Writev can not be implemented as write because :
  59. * writev() has the same requirements as write() with respect to write requests
  60. * of <= PIPE_BUF bytes to a pipe or FIFO: no interleaving and no partial
  61. * writes. Neither of these can be guaranteed in the general case if writev()
  62. * simply calls write() for each struct iovec.
  63. */
  64. /*
  65. * The problem here is that we have to gaurantee Atomic writev
  66. *
  67. * Upon successful completion, writev() shall return the number of bytes
  68. * actually written. Otherwise, it shall return a value of -1, the file-pointer
  69. * shall remain unchanged, and errno shall be set to indicate an error
  70. */
  71. ssize_t shim_do_writev (int fd, const struct iovec * vec, int vlen)
  72. {
  73. if(!vec)
  74. return -EINVAL;
  75. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  76. if (!hdl)
  77. return -EBADF;
  78. int ret = 0;
  79. if (!(hdl->acc_mode & MAY_WRITE) ||
  80. !hdl->fs || !hdl->fs->fs_ops || !hdl->fs->fs_ops->write) {
  81. ret = -EACCES;
  82. goto out;
  83. }
  84. ssize_t bytes = 0;
  85. for (int i = 0 ; i < vlen ; i++)
  86. {
  87. int b_vec;
  88. b_vec = hdl->fs->fs_ops->write(hdl, vec[i].iov_base, vec[i].iov_len);
  89. if (b_vec < 0) {
  90. ret = bytes ? : b_vec;
  91. goto out;
  92. }
  93. bytes += b_vec;
  94. }
  95. ret = bytes;
  96. out:
  97. put_handle(hdl);
  98. return ret;
  99. }