fs.c 12 KB

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