fs.c 12 KB

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