shim_open.c 14 KB

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