fs.c 4.6 KB

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