fs.c 4.6 KB

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