fs.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. * fs.c
  15. *
  16. * This file contains codes for implementation of 'socket' filesystem.
  17. */
  18. #define __KERNEL__
  19. #include <asm/fcntl.h>
  20. #include <asm/mman.h>
  21. #include <asm/prctl.h>
  22. #include <asm/unistd.h>
  23. #include <errno.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/stat.h>
  26. #include <pal.h>
  27. #include <pal_error.h>
  28. #include <shim_fs.h>
  29. #include <shim_internal.h>
  30. #include <shim_profile.h>
  31. static int socket_close(struct shim_handle* hdl) {
  32. /* XXX: Shouldn't this do something? */
  33. __UNUSED(hdl);
  34. return 0;
  35. }
  36. static ssize_t socket_read(struct shim_handle* hdl, void* buf, size_t count) {
  37. struct shim_sock_handle* sock = &hdl->info.sock;
  38. lock(&hdl->lock);
  39. if (sock->sock_type == SOCK_STREAM && sock->sock_state != SOCK_ACCEPTED &&
  40. sock->sock_state != SOCK_CONNECTED && sock->sock_state != SOCK_BOUNDCONNECTED) {
  41. sock->error = ENOTCONN;
  42. unlock(&hdl->lock);
  43. return -ENOTCONN;
  44. }
  45. if (sock->sock_type == SOCK_DGRAM && sock->sock_state != SOCK_CONNECTED &&
  46. sock->sock_state != SOCK_BOUNDCONNECTED) {
  47. sock->error = EDESTADDRREQ;
  48. unlock(&hdl->lock);
  49. return -EDESTADDRREQ;
  50. }
  51. unlock(&hdl->lock);
  52. PAL_NUM bytes = DkStreamRead(hdl->pal_handle, 0, count, buf, NULL, 0);
  53. if (bytes == PAL_STREAM_ERROR)
  54. switch (PAL_NATIVE_ERRNO) {
  55. case PAL_ERROR_ENDOFSTREAM:
  56. return 0;
  57. default: {
  58. int err = PAL_ERRNO;
  59. lock(&hdl->lock);
  60. sock->error = err;
  61. unlock(&hdl->lock);
  62. return -err;
  63. }
  64. }
  65. return (ssize_t)bytes;
  66. }
  67. static ssize_t socket_write(struct shim_handle* hdl, const void* buf, size_t count) {
  68. struct shim_sock_handle* sock = &hdl->info.sock;
  69. lock(&hdl->lock);
  70. if (sock->sock_type == SOCK_STREAM && sock->sock_state != SOCK_ACCEPTED &&
  71. sock->sock_state != SOCK_CONNECTED && sock->sock_state != SOCK_BOUNDCONNECTED) {
  72. sock->error = ENOTCONN;
  73. unlock(&hdl->lock);
  74. return -ENOTCONN;
  75. }
  76. if (sock->sock_type == SOCK_DGRAM && sock->sock_state != SOCK_CONNECTED &&
  77. sock->sock_state != SOCK_BOUNDCONNECTED) {
  78. sock->error = EDESTADDRREQ;
  79. unlock(&hdl->lock);
  80. return -EDESTADDRREQ;
  81. }
  82. unlock(&hdl->lock);
  83. PAL_NUM bytes = DkStreamWrite(hdl->pal_handle, 0, count, (void*)buf, NULL);
  84. if (bytes == PAL_STREAM_ERROR) {
  85. int err;
  86. switch (PAL_NATIVE_ERRNO) {
  87. case PAL_ERROR_CONNFAILED:
  88. err = EPIPE;
  89. break;
  90. default:
  91. err = PAL_ERRNO;
  92. break;
  93. }
  94. lock(&hdl->lock);
  95. sock->error = err;
  96. unlock(&hdl->lock);
  97. return -err;
  98. }
  99. return (ssize_t)bytes;
  100. }
  101. static int socket_hstat(struct shim_handle* hdl, struct stat* stat) {
  102. if (!stat)
  103. return 0;
  104. PAL_STREAM_ATTR attr;
  105. if (!DkStreamAttributesQueryByHandle(hdl->pal_handle, &attr))
  106. return -PAL_ERRNO;
  107. memset(stat, 0, sizeof(struct stat));
  108. stat->st_ino = 0;
  109. stat->st_size = (off_t)attr.pending_size;
  110. stat->st_mode = S_IFSOCK;
  111. return 0;
  112. }
  113. static int socket_checkout(struct shim_handle* hdl) {
  114. hdl->fs = NULL;
  115. return 0;
  116. }
  117. static off_t socket_poll(struct shim_handle* hdl, int poll_type) {
  118. struct shim_sock_handle* sock = &hdl->info.sock;
  119. off_t ret = 0;
  120. lock(&hdl->lock);
  121. if (poll_type & FS_POLL_RD) {
  122. if (sock->sock_type == SOCK_STREAM) {
  123. if (sock->sock_state == SOCK_CREATED || sock->sock_state == SOCK_BOUND ||
  124. sock->sock_state == SOCK_SHUTDOWN) {
  125. ret = -ENOTCONN;
  126. goto out;
  127. }
  128. }
  129. if (sock->sock_type == SOCK_DGRAM && sock->sock_state == SOCK_SHUTDOWN) {
  130. ret = -ENOTCONN;
  131. goto out;
  132. }
  133. }
  134. if (poll_type & FS_POLL_WR) {
  135. if (sock->sock_type == SOCK_STREAM) {
  136. if (sock->sock_state == SOCK_CREATED || sock->sock_state == SOCK_BOUND ||
  137. sock->sock_state == SOCK_LISTENED || sock->sock_state == SOCK_SHUTDOWN) {
  138. ret = -ENOTCONN;
  139. goto out;
  140. }
  141. }
  142. if (sock->sock_type == SOCK_DGRAM && sock->sock_state == SOCK_SHUTDOWN) {
  143. ret = -ENOTCONN;
  144. goto out;
  145. }
  146. }
  147. if (!hdl->pal_handle) {
  148. ret = -EBADF;
  149. goto out;
  150. }
  151. PAL_STREAM_ATTR attr;
  152. if (!DkStreamAttributesQueryByHandle(hdl->pal_handle, &attr)) {
  153. ret = -PAL_ERRNO;
  154. goto out;
  155. }
  156. if (poll_type == FS_POLL_SZ) {
  157. ret = attr.pending_size;
  158. goto out;
  159. }
  160. ret = 0;
  161. if (attr.disconnected)
  162. ret |= FS_POLL_ER;
  163. if ((poll_type & FS_POLL_RD) && attr.readable)
  164. ret |= FS_POLL_RD;
  165. if ((poll_type & FS_POLL_WR) && attr.writable)
  166. ret |= FS_POLL_WR;
  167. out:
  168. if (ret < 0) {
  169. debug("socket_poll failed (%ld)\n", ret);
  170. sock->error = -ret;
  171. }
  172. unlock(&hdl->lock);
  173. return ret;
  174. }
  175. static int socket_setflags(struct shim_handle* hdl, int flags) {
  176. if (!hdl->pal_handle)
  177. return 0;
  178. PAL_STREAM_ATTR attr;
  179. if (!DkStreamAttributesQueryByHandle(hdl->pal_handle, &attr))
  180. return -PAL_ERRNO;
  181. if (attr.nonblocking) {
  182. if (flags & O_NONBLOCK)
  183. return 0;
  184. attr.nonblocking = PAL_FALSE;
  185. } else {
  186. if (!(flags & O_NONBLOCK))
  187. return 0;
  188. attr.nonblocking = PAL_TRUE;
  189. }
  190. if (!DkStreamAttributesSetByHandle(hdl->pal_handle, &attr))
  191. return -PAL_ERRNO;
  192. return 0;
  193. }
  194. struct shim_fs_ops socket_fs_ops = {
  195. .close = &socket_close,
  196. .read = &socket_read,
  197. .write = &socket_write,
  198. .hstat = &socket_hstat,
  199. .checkout = &socket_checkout,
  200. .poll = &socket_poll,
  201. .setflags = &socket_setflags,
  202. };
  203. struct shim_mount socket_builtin_fs = {
  204. .type = "socket",
  205. .fs_ops = &socket_fs_ops,
  206. };