fs.c 12 KB

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