fs.c 6.9 KB

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