fs.c 7.0 KB

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