fs.c 7.0 KB

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