shim_open.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. * shim_open.c
  17. *
  18. * Implementation of system call "read", "write", "open", "creat", "openat",
  19. * "close", "lseek", "pread64", "pwrite64", "getdents", "getdents64",
  20. * "fsync", "truncate" and "ftruncate".
  21. */
  22. #include <shim_internal.h>
  23. #include <shim_utils.h>
  24. #include <shim_table.h>
  25. #include <shim_thread.h>
  26. #include <shim_handle.h>
  27. #include <shim_fs.h>
  28. #include <shim_profile.h>
  29. #include <pal.h>
  30. #include <pal_error.h>
  31. #include <fcntl.h>
  32. #include <errno.h>
  33. #include <dirent.h>
  34. int do_handle_read (struct shim_handle * hdl, void * buf, size_t count)
  35. {
  36. if (!(hdl->acc_mode & MAY_READ))
  37. return -EACCES;
  38. struct shim_mount * fs = hdl->fs;
  39. assert (fs && fs->fs_ops);
  40. if (!fs->fs_ops->read)
  41. return -EBADF;
  42. if (hdl->type == TYPE_DIR)
  43. return -EISDIR;
  44. return fs->fs_ops->read(hdl, buf, count);
  45. }
  46. size_t shim_do_read (int fd, void * buf, size_t count)
  47. {
  48. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  49. if (!hdl)
  50. return -EBADF;
  51. int ret = do_handle_read(hdl, buf, count);
  52. put_handle(hdl);
  53. return ret;
  54. }
  55. int do_handle_write (struct shim_handle * hdl, const void * buf, size_t count)
  56. {
  57. if (!(hdl->acc_mode & MAY_WRITE))
  58. return -EACCES;
  59. struct shim_mount * fs = hdl->fs;
  60. assert (fs && fs->fs_ops);
  61. if (!fs->fs_ops->write)
  62. return -EBADF;
  63. if (hdl->type == TYPE_DIR)
  64. return -EISDIR;
  65. return fs->fs_ops->write(hdl, buf, count);
  66. }
  67. size_t shim_do_write (int fd, const void * buf, size_t count)
  68. {
  69. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  70. if (!hdl)
  71. return -EBADF;
  72. int ret = do_handle_write(hdl, buf, count);
  73. put_handle(hdl);
  74. return ret;
  75. }
  76. int shim_do_open (const char * file, int flags, mode_t mode)
  77. {
  78. if (!file || !(*file))
  79. return -EINVAL;
  80. struct shim_handle * hdl = get_new_handle();
  81. if (!hdl)
  82. return -ENOMEM;
  83. int ret = 0;
  84. ret = open_namei(hdl, NULL, file, flags, mode, NULL);
  85. if (ret < 0)
  86. goto out;
  87. ret = set_new_fd_handle(hdl, flags & O_CLOEXEC ? FD_CLOEXEC : 0, NULL);
  88. out:
  89. put_handle(hdl);
  90. return ret;
  91. }
  92. int shim_do_creat (const char * path, mode_t mode)
  93. {
  94. return shim_do_open(path, O_CREAT|O_TRUNC|O_WRONLY, mode);
  95. }
  96. int shim_do_openat (int dfd, const char * filename, int flags, int mode)
  97. {
  98. if (!filename)
  99. return -EINVAL;
  100. if (*filename == '/')
  101. return shim_do_open(filename, flags, mode);
  102. struct shim_dentry * dir = NULL;
  103. int ret = 0;
  104. if ((ret = path_startat(dfd, &dir)) < 0)
  105. return ret;
  106. struct shim_handle * hdl = get_new_handle();
  107. if (!hdl) {
  108. ret = -ENOMEM;
  109. goto out;
  110. }
  111. ret = open_namei(hdl, dir, filename, flags, mode, NULL);
  112. if (ret < 0)
  113. goto out_hdl;
  114. ret = set_new_fd_handle(hdl, flags & O_CLOEXEC ? FD_CLOEXEC : 0, NULL);
  115. out_hdl:
  116. put_handle(hdl);
  117. out:
  118. put_dentry(dir);
  119. return ret;
  120. }
  121. int shim_do_close (int fd)
  122. {
  123. struct shim_handle * handle = detach_fd_handle(fd, NULL, NULL);
  124. if (!handle)
  125. return -EBADF;
  126. close_handle(handle);
  127. return 0;
  128. }
  129. /* lseek is simply doing arithmetic on the offset, no PAL call here */
  130. off_t shim_do_lseek (int fd, off_t offset, int origin)
  131. {
  132. if (origin != SEEK_SET && origin != SEEK_CUR && origin != SEEK_END)
  133. return -EINVAL;
  134. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  135. if (!hdl)
  136. return -EBADF;
  137. int ret = 0;
  138. struct shim_mount * fs = hdl->fs;
  139. assert(fs && fs->fs_ops);
  140. if (!fs->fs_ops->seek) {
  141. ret = -ESPIPE;
  142. goto out;
  143. }
  144. if (hdl->type == TYPE_DIR) {
  145. /* TODO: handle lseek'ing of directories */
  146. ret = -ENOSYS;
  147. goto out;
  148. }
  149. ret = fs->fs_ops->seek(hdl, offset, origin);
  150. out:
  151. put_handle(hdl);
  152. return ret;
  153. }
  154. size_t shim_do_pread64 (int fd, char * buf, size_t count, loff_t pos)
  155. {
  156. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  157. if (!hdl)
  158. return -EBADF;
  159. struct shim_mount * fs = hdl->fs;
  160. size_t ret = -EACCES;
  161. if (!fs || !fs->fs_ops)
  162. goto out;
  163. if (!fs->fs_ops->seek) {
  164. ret = -ESPIPE;
  165. goto out;
  166. }
  167. if (!fs->fs_ops->read)
  168. goto out;
  169. if (hdl->type == TYPE_DIR)
  170. goto out;
  171. int offset = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
  172. if (offset < 0) {
  173. ret = offset;
  174. goto out;
  175. }
  176. ret = fs->fs_ops->seek(hdl, pos, SEEK_SET);
  177. if (ret < 0)
  178. goto out;
  179. int bytes = fs->fs_ops->read(hdl, buf, count);
  180. ret = fs->fs_ops->seek(hdl, offset, SEEK_SET);
  181. if (ret < 0)
  182. goto out;
  183. ret = bytes;
  184. out:
  185. put_handle(hdl);
  186. return ret;
  187. }
  188. size_t shim_do_pwrite64 (int fd, char * buf, size_t count, loff_t pos)
  189. {
  190. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  191. if (!hdl)
  192. return -EBADF;
  193. struct shim_mount * fs = hdl->fs;
  194. size_t ret = -EACCES;
  195. if (!fs || !fs->fs_ops)
  196. goto out;
  197. if (!fs->fs_ops->seek) {
  198. ret = -ESPIPE;
  199. goto out;
  200. }
  201. if (!fs->fs_ops->write)
  202. goto out;
  203. if (hdl->type == TYPE_DIR)
  204. goto out;
  205. int offset = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
  206. if (offset < 0) {
  207. ret = offset;
  208. goto out;
  209. }
  210. ret = fs->fs_ops->seek(hdl, pos, SEEK_SET);
  211. if (ret < 0)
  212. goto out;
  213. int bytes = fs->fs_ops->write(hdl, buf, count);
  214. ret = fs->fs_ops->seek(hdl, offset, SEEK_SET);
  215. if (ret < 0)
  216. goto out;
  217. ret = bytes;
  218. out:
  219. put_handle(hdl);
  220. return ret;
  221. }
  222. static inline int get_dirent_type (mode_t type)
  223. {
  224. switch (type) {
  225. case S_IFLNK:
  226. return LINUX_DT_LNK;
  227. case S_IFREG:
  228. return LINUX_DT_REG;
  229. case S_IFDIR:
  230. return LINUX_DT_DIR;
  231. case S_IFCHR:
  232. return LINUX_DT_CHR;
  233. case S_IFBLK:
  234. return LINUX_DT_BLK;
  235. case S_IFIFO:
  236. return LINUX_DT_FIFO;
  237. case S_IFSOCK:
  238. return LINUX_DT_SOCK;
  239. default:
  240. return LINUX_DT_UNKNOWN;
  241. }
  242. }
  243. size_t shim_do_getdents (int fd, struct linux_dirent * buf, size_t count)
  244. {
  245. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  246. if (!hdl)
  247. return -EBADF;
  248. int ret = -EACCES;
  249. if (hdl->type != TYPE_DIR) {
  250. ret = -ENOTDIR;
  251. goto out;
  252. }
  253. /* we are grabbing the lock because the handle content is actually
  254. updated */
  255. lock(hdl->lock);
  256. struct shim_dir_handle * dirhdl = &hdl->info.dir;
  257. struct shim_dentry * dent = hdl->dentry;
  258. struct linux_dirent * b = buf;
  259. int bytes = 0;
  260. #define DIRENT_SIZE(len) (sizeof(struct linux_dirent) + \
  261. sizeof(struct linux_dirent_tail) + (len) + 1)
  262. #define ASSIGN_DIRENT(dent, name, type) \
  263. do { \
  264. int len = strlen(name); \
  265. if (bytes + DIRENT_SIZE(len) > count) \
  266. goto done; \
  267. \
  268. struct linux_dirent_tail * bt \
  269. = (void *) b + sizeof(struct linux_dirent) + len + 1; \
  270. \
  271. b->d_ino = dent->ino; \
  272. b->d_off = ++dirhdl->offset; \
  273. b->d_reclen = DIRENT_SIZE(len); \
  274. \
  275. memcpy(b->d_name, name, len + 1); \
  276. \
  277. bt->pad = 0; \
  278. bt->d_type = type ? : get_dirent_type(dent->mode); \
  279. \
  280. b = (void *) bt + sizeof(struct linux_dirent_tail); \
  281. bytes += DIRENT_SIZE(len); \
  282. } while(0)
  283. if (dirhdl->dot) {
  284. ASSIGN_DIRENT(dirhdl->dot, ".", LINUX_DT_DIR);
  285. put_dentry(dirhdl->dot);
  286. dirhdl->dot = NULL;
  287. }
  288. if (dirhdl->dotdot) {
  289. ASSIGN_DIRENT(dirhdl->dotdot, "..", LINUX_DT_DIR);
  290. put_dentry(dirhdl->dotdot);
  291. dirhdl->dotdot = NULL;
  292. }
  293. while (*dirhdl->ptr) {
  294. dent = *dirhdl->ptr;
  295. ASSIGN_DIRENT(dent, dentry_get_name(dent), 0);
  296. put_dentry(dent);
  297. *(dirhdl->ptr++) = NULL;
  298. }
  299. #undef DIRENT_SIZE
  300. #undef ASSIGN_DIRENT
  301. done:
  302. ret = bytes;
  303. unlock(hdl->lock);
  304. out:
  305. put_handle(hdl);
  306. return ret;
  307. }
  308. size_t shim_do_getdents64 (int fd, struct linux_dirent64 * buf, size_t count)
  309. {
  310. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  311. if (!hdl)
  312. return -EBADF;
  313. int ret = -EACCES;
  314. if (hdl->type != TYPE_DIR) {
  315. ret = -ENOTDIR;
  316. goto out;
  317. }
  318. lock(hdl->lock);
  319. struct shim_dir_handle * dirhdl = &hdl->info.dir;
  320. struct shim_dentry * dent = hdl->dentry;
  321. struct linux_dirent64 * b = buf;
  322. int bytes = 0;
  323. #define DIRENT_SIZE(len) (sizeof(struct linux_dirent64) + (len) + 1)
  324. #define ASSIGN_DIRENT(dent, name, type) \
  325. do { \
  326. int len = strlen(name); \
  327. if (bytes + DIRENT_SIZE(len) > count) \
  328. goto done; \
  329. \
  330. b->d_ino = dent->ino; \
  331. b->d_off = ++dirhdl->offset; \
  332. b->d_reclen = DIRENT_SIZE(len); \
  333. b->d_type = type ? : get_dirent_type(dent->mode); \
  334. \
  335. memcpy(b->d_name, name, len + 1); \
  336. \
  337. b = (void *) b + DIRENT_SIZE(len); \
  338. bytes += DIRENT_SIZE(len); \
  339. } while(0)
  340. if (dirhdl->dot) {
  341. ASSIGN_DIRENT(dirhdl->dot, ".", LINUX_DT_DIR);
  342. put_dentry(dirhdl->dot);
  343. dirhdl->dot = NULL;
  344. }
  345. if (dirhdl->dotdot) {
  346. ASSIGN_DIRENT(dirhdl->dotdot, "..", LINUX_DT_DIR);
  347. put_dentry(dirhdl->dotdot);
  348. dirhdl->dotdot = NULL;
  349. }
  350. while (*dirhdl->ptr) {
  351. dent = *dirhdl->ptr;
  352. ASSIGN_DIRENT(dent, dentry_get_name(dent), 0);
  353. put_dentry(dent);
  354. *(dirhdl->ptr++) = NULL;
  355. }
  356. #undef DIRENT_SIZE
  357. #undef ASSIGN_DIRENT
  358. done:
  359. ret = bytes;
  360. unlock(hdl->lock);
  361. out:
  362. put_handle(hdl);
  363. return ret;
  364. }
  365. int shim_do_fsync (int fd)
  366. {
  367. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  368. if (!hdl)
  369. return -EBADF;
  370. int ret = -EACCES;
  371. struct shim_mount * fs = hdl->fs;
  372. if (!fs || !fs->fs_ops)
  373. goto out;
  374. if (hdl->type == TYPE_DIR)
  375. goto out;
  376. if (!fs->fs_ops->flush) {
  377. ret = -EROFS;
  378. goto out;
  379. }
  380. ret = fs->fs_ops->flush(hdl);
  381. out:
  382. put_handle(hdl);
  383. return ret;
  384. }
  385. int shim_do_truncate (const char * path, loff_t length)
  386. {
  387. struct shim_dentry * dent = NULL;
  388. int ret = 0;
  389. if ((ret = path_lookupat(NULL, path, 0, &dent)) < 0)
  390. return ret;
  391. struct shim_mount * fs = dent->fs;
  392. if (!fs || !fs->d_ops || !fs->d_ops->open) {
  393. ret = -EBADF;
  394. goto out;
  395. }
  396. if (!fs->fs_ops->truncate) {
  397. ret = -EROFS;
  398. goto out;
  399. }
  400. struct shim_handle * hdl = get_new_handle();
  401. if (!hdl) {
  402. ret = -ENOMEM;
  403. goto out;
  404. }
  405. hdl->fs = fs;
  406. if ((ret = fs->d_ops->open(hdl, dent, O_WRONLY)) < 0)
  407. goto out_handle;
  408. ret = fs->fs_ops->truncate(hdl, length);
  409. flush_handle(hdl);
  410. out_handle:
  411. put_handle(hdl);
  412. out:
  413. return ret;
  414. }
  415. int shim_do_ftruncate (int fd, loff_t length)
  416. {
  417. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  418. if (!hdl)
  419. return -EBADF;
  420. struct shim_mount * fs = hdl->fs;
  421. int ret = -EACCES;
  422. if (!fs || !fs->fs_ops)
  423. goto out;
  424. if (hdl->type == TYPE_DIR)
  425. goto out;
  426. if (!fs->fs_ops->truncate) {
  427. ret = -EROFS;
  428. goto out;
  429. }
  430. ret = fs->fs_ops->truncate(hdl, length);
  431. out:
  432. put_handle(hdl);
  433. return ret;
  434. }