fs.c 13 KB

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