fs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 'dev' filesystem.
  17. */
  18. #include <asm/fcntl.h>
  19. #include <asm/mman.h>
  20. #include <asm/prctl.h>
  21. #include <asm/unistd.h>
  22. #include <errno.h>
  23. #include <linux/fcntl.h>
  24. #include <pal.h>
  25. #include <pal_error.h>
  26. #include <shim_fs.h>
  27. #include <shim_handle.h>
  28. #include <shim_internal.h>
  29. #include <shim_profile.h>
  30. #include <shim_utils.h>
  31. // TODO: For some reason S_IF* macros are missing if this file is included before our headers. We
  32. // should investigate and fix this behavior.
  33. #include <linux/stat.h>
  34. #define EMPTY_DEV_OPS \
  35. { \
  36. .open = NULL, \
  37. .close = NULL, \
  38. .read = NULL, \
  39. .write = NULL, \
  40. .flush = NULL, \
  41. .seek = NULL, \
  42. .truncate = NULL, \
  43. .mode = NULL, \
  44. .stat = NULL, \
  45. .hstat = NULL, \
  46. }
  47. #define DEV_INO_BASE 1025
  48. static ssize_t dev_null_read(struct shim_handle* hdl, void* buf, size_t count) {
  49. // Arguments for compatibility
  50. __UNUSED(hdl);
  51. __UNUSED(buf);
  52. __UNUSED(count);
  53. return 0;
  54. }
  55. static ssize_t dev_zero_read(struct shim_handle* hdl, void* buf, size_t count) {
  56. // Argument for compatibility
  57. __UNUSED(hdl);
  58. memset(buf, 0, count);
  59. return count;
  60. }
  61. static ssize_t dev_null_write(struct shim_handle* hdl, const void* buf, size_t count) {
  62. // Arguments for compatibility
  63. __UNUSED(hdl);
  64. __UNUSED(buf);
  65. __UNUSED(count);
  66. return count;
  67. }
  68. static int dev_null_mode(const char* name, mode_t* mode) {
  69. __UNUSED(name); // We know it is /dev/null
  70. *mode = 0666 | S_IFCHR;
  71. return 0;
  72. }
  73. static int dev_null_stat(const char* name, struct stat* stat) {
  74. __UNUSED(name); // We know it is /dev/null
  75. stat->st_mode = 0666 | S_IFCHR;
  76. stat->st_uid = 0;
  77. stat->st_gid = 0;
  78. stat->st_size = 0;
  79. stat->st_blksize = 0;
  80. return 0;
  81. }
  82. static int dev_null_hstat(struct shim_handle* hdl, struct stat* stat) {
  83. __UNUSED(hdl); // We know it is /dev/null
  84. stat->st_mode = 0666 | S_IFCHR;
  85. stat->st_uid = 0;
  86. stat->st_gid = 0;
  87. stat->st_size = 0;
  88. stat->st_blksize = 0;
  89. return 0;
  90. }
  91. static int dev_null_truncate(struct shim_handle* hdl, uint64_t size) {
  92. // Arguments for compatibility
  93. __UNUSED(hdl);
  94. __UNUSED(size);
  95. return 0;
  96. }
  97. static int dev_random_mode(const char* name, mode_t* mode) {
  98. __UNUSED(name); // We know it is /dev/random
  99. *mode = 0666 | S_IFCHR;
  100. return 0;
  101. }
  102. static ssize_t dev_urandom_read(struct shim_handle* hdl, void* buf, size_t count) {
  103. __UNUSED(hdl);
  104. ssize_t ret = DkRandomBitsRead(buf, count);
  105. if (ret < 0)
  106. return -convert_pal_errno(-ret);
  107. return count;
  108. }
  109. static ssize_t dev_random_read(struct shim_handle* hdl, void* buf, size_t count) {
  110. return dev_urandom_read(hdl, buf, count);
  111. }
  112. static int dev_random_stat(const char* name, struct stat* stat) {
  113. __UNUSED(name); // we know it is /dev/random
  114. stat->st_mode = 0666 | S_IFCHR;
  115. stat->st_uid = 0;
  116. stat->st_gid = 0;
  117. stat->st_size = 0;
  118. stat->st_blksize = 0;
  119. return 0;
  120. }
  121. static int dev_random_hstat(struct shim_handle* hdl, struct stat* stat) {
  122. __UNUSED(hdl); // pseudo-device
  123. stat->st_mode = 0444 | S_IFCHR;
  124. stat->st_uid = 0;
  125. stat->st_gid = 0;
  126. stat->st_size = 0;
  127. stat->st_blksize = 0;
  128. return 0;
  129. }
  130. static int search_dev_driver(const char* name, struct shim_dev_ops* ops) {
  131. if (strcmp_static(name, "null") || strcmp_static(name, "tty")) {
  132. if (ops)
  133. ops->read = &dev_null_read;
  134. null_dev:
  135. if (ops) {
  136. ops->write = &dev_null_write;
  137. ops->truncate = &dev_null_truncate;
  138. ops->mode = &dev_null_mode;
  139. ops->stat = &dev_null_stat;
  140. ops->hstat = &dev_null_hstat;
  141. }
  142. return 0;
  143. }
  144. if (strcmp_static(name, "zero")) {
  145. if (ops)
  146. ops->read = &dev_zero_read;
  147. goto null_dev;
  148. }
  149. if (strcmp_static(name, "random")) {
  150. if (ops)
  151. ops->read = &dev_random_read;
  152. random_dev:
  153. if (ops) {
  154. ops->mode = &dev_random_mode;
  155. ops->stat = &dev_random_stat;
  156. ops->hstat = &dev_random_hstat;
  157. }
  158. return 0;
  159. }
  160. if (strcmp_static(name, "urandom")) {
  161. if (ops)
  162. ops->read = &dev_urandom_read;
  163. goto random_dev;
  164. }
  165. if (strcmp_static(name, "stdin") || strcmp_static(name, "stdout") ||
  166. strcmp_static(name, "stderr"))
  167. return -EISLINK;
  168. return -ENOENT;
  169. }
  170. static int dev_mount(const char* uri, void** mount_data) {
  171. // Arguments for compatibility
  172. __UNUSED(uri);
  173. __UNUSED(mount_data);
  174. /* do nothing */
  175. return 0;
  176. }
  177. static int dev_unmount(void* mount_data) {
  178. // Arguments for compatibility
  179. __UNUSED(mount_data);
  180. /* do nothing */
  181. return 0;
  182. }
  183. static int dev_open(struct shim_handle* hdl, struct shim_dentry* dent, int flags) {
  184. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  185. int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
  186. if (ret < 0)
  187. return ret;
  188. hdl->type = TYPE_DEV;
  189. hdl->flags = flags & ~O_ACCMODE;
  190. hdl->acc_mode = ACC_MODE(flags & O_ACCMODE);
  191. memcpy(&hdl->info.dev.dev_ops, &ops_buf, sizeof(struct shim_dev_ops));
  192. if (!ops_buf.read && (hdl->acc_mode & MAY_READ))
  193. return -EACCES;
  194. if (!ops_buf.write && (hdl->acc_mode & MAY_WRITE))
  195. return -EACCES;
  196. if (ops_buf.open)
  197. return ops_buf.open(hdl, qstrgetstr(&dent->rel_path), flags);
  198. return 0;
  199. }
  200. static int dev_lookup(struct shim_dentry* dent) {
  201. if (qstrempty(&dent->rel_path)) {
  202. dent->state |= DENTRY_ISDIRECTORY;
  203. dent->ino = DEV_INO_BASE;
  204. return 0;
  205. }
  206. /* we don't care about forced or not */
  207. return search_dev_driver(qstrgetstr(&dent->rel_path), NULL);
  208. }
  209. static int dev_mode(struct shim_dentry* dent, mode_t* mode) {
  210. if (qstrempty(&dent->rel_path)) {
  211. dent->ino = DEV_INO_BASE;
  212. *mode = 0555 | S_IFDIR;
  213. return 0;
  214. }
  215. /* we don't care about forced or not */
  216. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  217. int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
  218. if (ret < 0)
  219. return ret;
  220. return ops_buf.mode(qstrgetstr(&dent->rel_path), mode);
  221. }
  222. static int dev_flush(struct shim_handle* hdl) {
  223. if (!hdl->info.dev.dev_ops.flush)
  224. return 0;
  225. return hdl->info.dev.dev_ops.flush(hdl);
  226. }
  227. static int dev_close(struct shim_handle* hdl) {
  228. if (!hdl->info.dev.dev_ops.close)
  229. return 0;
  230. return hdl->info.dev.dev_ops.close(hdl);
  231. }
  232. static ssize_t dev_read(struct shim_handle* hdl, void* buf, size_t count) {
  233. if (!hdl->info.dev.dev_ops.read)
  234. return -EACCES;
  235. return hdl->info.dev.dev_ops.read(hdl, buf, count);
  236. }
  237. static ssize_t dev_write(struct shim_handle* hdl, const void* buf, size_t count) {
  238. if (!hdl->info.dev.dev_ops.write)
  239. return -EACCES;
  240. return hdl->info.dev.dev_ops.write(hdl, buf, count);
  241. }
  242. static off_t dev_seek(struct shim_handle* hdl, off_t offset, int wence) {
  243. if (!hdl->info.dev.dev_ops.seek)
  244. return -EACCES;
  245. return hdl->info.dev.dev_ops.seek(hdl, offset, wence);
  246. }
  247. static int dev_truncate(struct shim_handle* hdl, off_t len) {
  248. if (!hdl->info.dev.dev_ops.truncate)
  249. return -EACCES;
  250. return hdl->info.dev.dev_ops.truncate(hdl, len);
  251. }
  252. static int dev_readdir(struct shim_dentry* dent, struct shim_dirent** dirent) {
  253. if (!qstrempty(&dent->rel_path)) {
  254. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  255. int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
  256. if (ret < 0 && ret != -EISLINK)
  257. return ret;
  258. return -ENOTDIR;
  259. }
  260. struct shim_dirent *buf, *ptr;
  261. int buf_size = MAX_PATH;
  262. retry:
  263. buf = malloc(buf_size);
  264. *dirent = ptr = buf;
  265. struct shim_dirent** last = dirent;
  266. #define COPY_ENTRY(devname, devtype) \
  267. do { \
  268. int name_len = strlen(devname); \
  269. \
  270. if ((void*)(ptr + 1) + name_len + 1 > (void*)buf + buf_size) \
  271. goto nomem; \
  272. \
  273. ptr->next = (void*)(ptr + 1) + name_len + 1; \
  274. ptr->ino = 1; \
  275. ptr->type = (devtype); \
  276. memcpy(ptr->name, (devname), name_len + 1); \
  277. last = &ptr->next; \
  278. ptr = ptr->next; \
  279. } while (0)
  280. COPY_ENTRY("null", LINUX_DT_CHR);
  281. COPY_ENTRY("zero", LINUX_DT_CHR);
  282. COPY_ENTRY("stdin", LINUX_DT_LNK);
  283. COPY_ENTRY("stdout", LINUX_DT_LNK);
  284. COPY_ENTRY("stderr", LINUX_DT_LNK);
  285. #undef COPY_ENTRY
  286. *last = NULL;
  287. return 0;
  288. nomem:
  289. buf_size *= 2;
  290. free(buf);
  291. goto retry;
  292. }
  293. static int dev_stat(struct shim_dentry* dent, struct stat* buf) {
  294. if (qstrempty(&dent->rel_path)) {
  295. buf->st_dev = DEV_INO_BASE;
  296. buf->st_ino = DEV_INO_BASE;
  297. buf->st_mode = 0777 | S_IFDIR;
  298. buf->st_size = 4096;
  299. buf->st_blksize = 4096;
  300. return 0;
  301. }
  302. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  303. int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
  304. if (ret < 0 && ret != -EISLINK)
  305. return ret;
  306. if (ret == -EISLINK) {
  307. buf->st_dev = DEV_INO_BASE;
  308. buf->st_ino = DEV_INO_BASE;
  309. buf->st_mode = 0777 | S_IFLNK;
  310. buf->st_size = 0;
  311. buf->st_blksize = 0;
  312. return 0;
  313. }
  314. buf->st_dev = DEV_INO_BASE;
  315. buf->st_ino = DEV_INO_BASE;
  316. return ops_buf.stat ? ops_buf.stat(qstrgetstr(&dent->rel_path), buf) : -EACCES;
  317. }
  318. static int dev_hstat(struct shim_handle* hdl, struct stat* buf) {
  319. if (!hdl->info.dev.dev_ops.hstat)
  320. return -EACCES;
  321. return hdl->info.dev.dev_ops.hstat(hdl, buf);
  322. }
  323. static off_t dev_poll(struct shim_handle* hdl, int poll_type) {
  324. if (poll_type == FS_POLL_SZ)
  325. return 0;
  326. off_t ret = 0;
  327. if ((poll_type & FS_POLL_RD) && hdl->info.dev.dev_ops.read)
  328. ret |= FS_POLL_RD;
  329. if ((poll_type & FS_POLL_WR) && hdl->info.dev.dev_ops.write)
  330. ret |= FS_POLL_WR;
  331. return ret;
  332. }
  333. static int dev_follow_link(struct shim_dentry* dent, struct shim_qstr* link) {
  334. const char* name = qstrgetstr(&dent->rel_path);
  335. if (strcmp_static(name, "stdin")) {
  336. qstrsetstr(link, "/proc/self/0", static_strlen("/proc/self/0"));
  337. return 0;
  338. } else if (strcmp_static(name, "stdout")) {
  339. qstrsetstr(link, "/proc/self/1", static_strlen("/proc/self/1"));
  340. return 0;
  341. } else if (strcmp_static(name, "stderr")) {
  342. qstrsetstr(link, "/proc/self/2", static_strlen("/proc/self/2"));
  343. return 0;
  344. }
  345. if (strcmp_static(name, "null") || strcmp_static(name, "zero"))
  346. return -ENOTLINK;
  347. return -ENOENT;
  348. }
  349. struct shim_fs_ops dev_fs_ops = {
  350. .mount = &dev_mount,
  351. .unmount = &dev_unmount,
  352. .flush = &dev_flush,
  353. .close = &dev_close,
  354. .read = &dev_read,
  355. .write = &dev_write,
  356. .seek = &dev_seek,
  357. .hstat = &dev_hstat,
  358. .poll = &dev_poll,
  359. .truncate = &dev_truncate,
  360. };
  361. struct shim_d_ops dev_d_ops = {
  362. .open = &dev_open,
  363. .lookup = &dev_lookup,
  364. .mode = &dev_mode,
  365. .readdir = &dev_readdir,
  366. .stat = &dev_stat,
  367. .follow_link = &dev_follow_link,
  368. };