fs.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 'pipe' filesystem.
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_thread.h>
  20. #include <shim_handle.h>
  21. #include <shim_fs.h>
  22. #include <shim_profile.h>
  23. #include <pal.h>
  24. #include <pal_error.h>
  25. #include <pal_debug.h>
  26. #include <errno.h>
  27. #include <linux/stat.h>
  28. #include <linux/fcntl.h>
  29. #include <asm/fcntl.h>
  30. #include <asm/mman.h>
  31. #include <asm/unistd.h>
  32. #include <asm/prctl.h>
  33. #include <asm/fcntl.h>
  34. #include <shim_profile.h>
  35. static ssize_t pipe_read (struct shim_handle * hdl, void * buf,
  36. size_t count)
  37. {
  38. if (!count)
  39. return 0;
  40. PAL_NUM bytes = DkStreamRead(hdl->pal_handle, 0, count, buf, NULL, 0);
  41. if (!bytes)
  42. return -PAL_ERRNO;
  43. assert((ssize_t) bytes > 0);
  44. return (ssize_t) bytes;
  45. }
  46. static ssize_t pipe_write (struct shim_handle * hdl, const void * buf,
  47. size_t count)
  48. {
  49. if (!count)
  50. return 0;
  51. PAL_NUM bytes = DkStreamWrite(hdl->pal_handle, 0, count, (void *) buf, NULL);
  52. if (!bytes)
  53. return -PAL_ERRNO;
  54. assert((ssize_t) bytes > 0);
  55. return (ssize_t) bytes;
  56. }
  57. static int pipe_hstat (struct shim_handle * hdl, struct stat * stat)
  58. {
  59. /* XXX: Is any of this right?
  60. * Shouldn't we be using hdl to figure something out?
  61. * if stat is NULL, should we not return -EFAULT?
  62. */
  63. __UNUSED(hdl);
  64. if (!stat)
  65. return 0;
  66. struct shim_thread * thread = get_cur_thread();
  67. stat->st_dev = (dev_t) 0; /* ID of device containing file */
  68. stat->st_ino = (ino_t) 0; /* inode number */
  69. stat->st_nlink = (nlink_t) 0; /* number of hard links */
  70. stat->st_uid = (uid_t) thread->uid; /* user ID of owner */
  71. stat->st_gid = (gid_t) thread->gid; /* group ID of owner */
  72. stat->st_rdev = (dev_t) 0; /* device ID (if special file) */
  73. stat->st_size = (off_t) 0; /* total size, in bytes */
  74. stat->st_blksize = 0; /* blocksize for file system I/O */
  75. stat->st_blocks = 0; /* number of 512B blocks allocated */
  76. stat->st_atime = (time_t) 0; /* access time */
  77. stat->st_mtime = (time_t) 0; /* last modification */
  78. stat->st_ctime = (time_t) 0; /* last status change */
  79. stat->st_mode = S_IRUSR|S_IWUSR|S_IFIFO;
  80. return 0;
  81. }
  82. static int pipe_checkout (struct shim_handle * hdl)
  83. {
  84. hdl->fs = NULL;
  85. return 0;
  86. }
  87. static off_t pipe_poll (struct shim_handle * hdl, int poll_type)
  88. {
  89. off_t ret = 0;
  90. lock(&hdl->lock);
  91. if (!hdl->pal_handle) {
  92. ret = -EBADF;
  93. goto out;
  94. }
  95. PAL_STREAM_ATTR attr;
  96. if (!DkStreamAttributesQueryByHandle(hdl->pal_handle, &attr)) {
  97. ret = -PAL_ERRNO;
  98. goto out;
  99. }
  100. if (poll_type == FS_POLL_SZ) {
  101. ret = attr.pending_size;
  102. goto out;
  103. }
  104. ret = 0;
  105. if (attr.disconnected)
  106. ret |= FS_POLL_ER;
  107. if ((poll_type & FS_POLL_RD) && attr.readable)
  108. ret |= FS_POLL_RD;
  109. if ((poll_type & FS_POLL_WR) && attr.writable)
  110. ret |= FS_POLL_WR;
  111. out:
  112. unlock(&hdl->lock);
  113. return ret;
  114. }
  115. static int pipe_setflags (struct shim_handle * hdl, int flags)
  116. {
  117. if (!hdl->pal_handle)
  118. return 0;
  119. PAL_STREAM_ATTR attr;
  120. if (!DkStreamAttributesQueryByHandle(hdl->pal_handle, &attr))
  121. return -PAL_ERRNO;
  122. if (attr.nonblocking) {
  123. if (flags & O_NONBLOCK)
  124. return 0;
  125. attr.nonblocking = PAL_FALSE;
  126. } else {
  127. if (!(flags & O_NONBLOCK))
  128. return 0;
  129. attr.nonblocking = PAL_TRUE;
  130. }
  131. if (!DkStreamAttributesSetByHandle(hdl->pal_handle, &attr))
  132. return -PAL_ERRNO;
  133. return 0;
  134. }
  135. struct shim_fs_ops pipe_fs_ops = {
  136. .read = &pipe_read,
  137. .write = &pipe_write,
  138. .hstat = &pipe_hstat,
  139. .checkout = &pipe_checkout,
  140. .poll = &pipe_poll,
  141. .setflags = &pipe_setflags,
  142. };
  143. struct shim_mount pipe_builtin_fs = { .type = "pipe",
  144. .fs_ops = &pipe_fs_ops, };