fs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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->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, bool force)
  210. {
  211. if (qstrempty(&dent->rel_path)) {
  212. dent->ino = DEV_INO_BASE;
  213. *mode = 0555|S_IFDIR;
  214. return 0;
  215. }
  216. /* we don't care about forced or not */
  217. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  218. int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
  219. if (ret < 0)
  220. return ret;
  221. return ops_buf.mode(qstrgetstr(&dent->rel_path), mode);
  222. }
  223. static int dev_flush (struct shim_handle * hdl)
  224. {
  225. if (!hdl->info.dev.dev_ops.flush)
  226. return 0;
  227. return hdl->info.dev.dev_ops.flush(hdl);
  228. }
  229. static int dev_close (struct shim_handle * hdl)
  230. {
  231. if (!hdl->info.dev.dev_ops.close)
  232. return 0;
  233. return hdl->info.dev.dev_ops.close(hdl);
  234. }
  235. static int dev_read (struct shim_handle * hdl, void * buf,
  236. size_t count)
  237. {
  238. if (!hdl->info.dev.dev_ops.read)
  239. return -EACCES;
  240. return hdl->info.dev.dev_ops.read(hdl, buf, count);
  241. }
  242. static int dev_write (struct shim_handle * hdl, const void * buf,
  243. size_t count)
  244. {
  245. if (!hdl->info.dev.dev_ops.write)
  246. return -EACCES;
  247. return hdl->info.dev.dev_ops.write(hdl, buf, count);
  248. }
  249. static int dev_seek (struct shim_handle * hdl, off_t offset, int wence)
  250. {
  251. if (!hdl->info.dev.dev_ops.seek)
  252. return -EACCES;
  253. return hdl->info.dev.dev_ops.seek(hdl, offset, wence);
  254. }
  255. static int dev_truncate (struct shim_handle * hdl, uint64_t len)
  256. {
  257. if (!hdl->info.dev.dev_ops.truncate)
  258. return -EACCES;
  259. return hdl->info.dev.dev_ops.truncate(hdl, len);
  260. }
  261. static int dev_readdir (struct shim_dentry * dent, struct shim_dirent ** dirent)
  262. {
  263. if (!qstrempty(&dent->rel_path)) {
  264. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  265. int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
  266. if (ret < 0 && ret != -EISLINK)
  267. return ret;
  268. return -ENOTDIR;
  269. }
  270. struct shim_dirent * buf, * ptr;
  271. int buf_size = MAX_PATH;
  272. retry:
  273. buf = malloc(buf_size);
  274. *dirent = ptr = buf;
  275. struct shim_dirent ** last = dirent;
  276. #define copy_entry(devname, devtype) \
  277. do { \
  278. int name_len = strlen(devname); \
  279. \
  280. if ((void *) (ptr + 1) + name_len + 1 > \
  281. (void *) buf + buf_size) \
  282. goto nomem; \
  283. \
  284. ptr->next = (void *) (ptr + 1) + name_len + 1; \
  285. ptr->ino = 1; \
  286. ptr->type = (devtype); \
  287. memcpy(ptr->name, (devname), name_len + 1); \
  288. last = &ptr->next; \
  289. ptr = ptr->next; \
  290. } while (0)
  291. copy_entry("null", LINUX_DT_CHR);
  292. copy_entry("zero", LINUX_DT_CHR);
  293. copy_entry("stdin", LINUX_DT_LNK);
  294. copy_entry("stdout", LINUX_DT_LNK);
  295. copy_entry("stderr", LINUX_DT_LNK);
  296. *last = NULL;
  297. return 0;
  298. nomem:
  299. buf_size *= 2;
  300. free(buf);
  301. goto retry;
  302. }
  303. static int dev_stat (struct shim_dentry * dent, struct stat * buf)
  304. {
  305. if (qstrempty(&dent->rel_path)) {
  306. buf->st_dev = DEV_INO_BASE;
  307. buf->st_ino = DEV_INO_BASE;
  308. buf->st_mode = 0777|S_IFDIR;
  309. buf->st_size = 4096;
  310. buf->st_blksize = 4096;
  311. return 0;
  312. }
  313. struct shim_dev_ops ops_buf = EMPTY_DEV_OPS;
  314. int ret = search_dev_driver(qstrgetstr(&dent->rel_path), &ops_buf);
  315. if (ret < 0 && ret != -EISLINK)
  316. return ret;
  317. if (ret == -EISLINK) {
  318. buf->st_dev = DEV_INO_BASE;
  319. buf->st_ino = DEV_INO_BASE;
  320. buf->st_mode = 0777|S_IFLNK;
  321. buf->st_size = 0;
  322. buf->st_blksize = 0;
  323. return 0;
  324. }
  325. buf->st_dev = DEV_INO_BASE;
  326. buf->st_ino = DEV_INO_BASE;
  327. return ops_buf.stat ? ops_buf.stat(qstrgetstr(&dent->rel_path), buf) :
  328. -EACCES;
  329. }
  330. static int dev_hstat (struct shim_handle * hdl, struct stat * buf)
  331. {
  332. if (!hdl->info.dev.dev_ops.hstat)
  333. return -EACCES;
  334. return hdl->info.dev.dev_ops.hstat(hdl, buf);
  335. }
  336. static int dev_poll (struct shim_handle * hdl, int poll_type)
  337. {
  338. if (poll_type == FS_POLL_SZ)
  339. return 0;
  340. int ret = 0;
  341. if ((poll_type & FS_POLL_RD) && hdl->info.dev.dev_ops.read)
  342. ret |= FS_POLL_RD;
  343. if ((poll_type & FS_POLL_WR) && hdl->info.dev.dev_ops.write)
  344. ret |= FS_POLL_WR;
  345. return ret;
  346. }
  347. static int dev_follow_link (struct shim_dentry * dent, struct shim_qstr * link)
  348. {
  349. const char * name = qstrgetstr(&dent->rel_path);
  350. if (strcmp_static(name, "stdin")) {
  351. qstrsetstr(link, "/proc/self/0", static_strlen("/proc/self/0"));
  352. return 0;
  353. } else if (strcmp_static(name, "stdout")) {
  354. qstrsetstr(link, "/proc/self/1", static_strlen("/proc/self/1"));
  355. return 0;
  356. } else if (strcmp_static(name, "stderr")) {
  357. qstrsetstr(link, "/proc/self/2", static_strlen("/proc/self/2"));
  358. return 0;
  359. }
  360. if (strcmp_static(name, "null") || strcmp_static(name, "zero"))
  361. return -ENOTLINK;
  362. return -ENOENT;
  363. }
  364. struct shim_fs_ops dev_fs_ops = {
  365. .mount = &dev_mount,
  366. .unmount = &dev_unmount,
  367. .flush = &dev_flush,
  368. .close = &dev_close,
  369. .read = &dev_read,
  370. .write = &dev_write,
  371. .seek = &dev_seek,
  372. .hstat = &dev_hstat,
  373. .poll = &dev_poll,
  374. .truncate = &dev_truncate,
  375. };
  376. struct shim_d_ops dev_d_ops = {
  377. .open = &dev_open,
  378. .lookup = &dev_lookup,
  379. .mode = &dev_mode,
  380. .readdir = &dev_readdir,
  381. .stat = &dev_stat,
  382. .follow_link = &dev_follow_link,
  383. };