shim_open.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. * 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. if (!buf || test_user_memory(buf, count, true))
  50. return -EFAULT;
  51. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  52. if (!hdl)
  53. return -EBADF;
  54. int ret = do_handle_read(hdl, buf, count);
  55. put_handle(hdl);
  56. return ret;
  57. }
  58. int do_handle_write (struct shim_handle * hdl, const void * buf, int count)
  59. {
  60. if (!(hdl->acc_mode & MAY_WRITE))
  61. return -EACCES;
  62. struct shim_mount * fs = hdl->fs;
  63. assert (fs && fs->fs_ops);
  64. if (!fs->fs_ops->write)
  65. return -EBADF;
  66. if (hdl->type == TYPE_DIR)
  67. return -EISDIR;
  68. return fs->fs_ops->write(hdl, buf, count);
  69. }
  70. size_t shim_do_write (int fd, const void * buf, size_t count)
  71. {
  72. if (!buf || test_user_memory((void *) buf, count, false))
  73. return -EFAULT;
  74. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  75. if (!hdl)
  76. return -EBADF;
  77. int ret = do_handle_write(hdl, buf, count);
  78. put_handle(hdl);
  79. return ret;
  80. }
  81. int shim_do_open (const char * file, int flags, mode_t mode)
  82. {
  83. if (!file || test_user_string(file))
  84. return -EFAULT;
  85. if (file[0] == '\0')
  86. return -EINVAL;
  87. struct shim_handle * hdl = get_new_handle();
  88. if (!hdl)
  89. return -ENOMEM;
  90. int ret = 0;
  91. ret = open_namei(hdl, NULL, file, flags, mode, NULL);
  92. if (ret < 0)
  93. goto out;
  94. ret = set_new_fd_handle(hdl, flags & O_CLOEXEC ? FD_CLOEXEC : 0, NULL);
  95. out:
  96. put_handle(hdl);
  97. return ret;
  98. }
  99. int shim_do_creat (const char * path, mode_t mode)
  100. {
  101. return shim_do_open(path, O_CREAT|O_TRUNC|O_WRONLY, mode);
  102. }
  103. int shim_do_openat (int dfd, const char * filename, int flags, int mode)
  104. {
  105. if (!filename || test_user_string(filename))
  106. return -EFAULT;
  107. if (*filename == '/')
  108. return shim_do_open(filename, flags, mode);
  109. struct shim_dentry * dir = NULL;
  110. int ret = 0;
  111. if ((ret = path_startat(dfd, &dir)) < 0)
  112. return ret;
  113. struct shim_handle * hdl = get_new_handle();
  114. if (!hdl) {
  115. ret = -ENOMEM;
  116. goto out;
  117. }
  118. ret = open_namei(hdl, dir, filename, flags, mode, NULL);
  119. if (ret < 0)
  120. goto out_hdl;
  121. ret = set_new_fd_handle(hdl, flags & O_CLOEXEC ? FD_CLOEXEC : 0, NULL);
  122. out_hdl:
  123. put_handle(hdl);
  124. out:
  125. put_dentry(dir);
  126. return ret;
  127. }
  128. int shim_do_close (int fd)
  129. {
  130. struct shim_handle * handle = detach_fd_handle(fd, NULL, NULL);
  131. if (!handle)
  132. return -EBADF;
  133. close_handle(handle);
  134. return 0;
  135. }
  136. /* lseek is simply doing arithmetic on the offset, no PAL call here */
  137. off_t shim_do_lseek (int fd, off_t offset, int origin)
  138. {
  139. if (origin != SEEK_SET && origin != SEEK_CUR && origin != SEEK_END)
  140. return -EINVAL;
  141. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  142. if (!hdl)
  143. return -EBADF;
  144. int ret = 0;
  145. struct shim_mount * fs = hdl->fs;
  146. assert(fs && fs->fs_ops);
  147. if (!fs->fs_ops->seek) {
  148. ret = -ESPIPE;
  149. goto out;
  150. }
  151. if (hdl->type == TYPE_DIR) {
  152. /* TODO: handle lseek'ing of directories */
  153. ret = -ENOSYS;
  154. goto out;
  155. }
  156. ret = fs->fs_ops->seek(hdl, offset, origin);
  157. out:
  158. put_handle(hdl);
  159. return ret;
  160. }
  161. size_t shim_do_pread64 (int fd, char * buf, size_t count, loff_t pos)
  162. {
  163. if (!buf || test_user_memory(buf, count, true))
  164. return -EFAULT;
  165. if (pos < 0)
  166. return -EINVAL;
  167. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  168. if (!hdl)
  169. return -EBADF;
  170. struct shim_mount * fs = hdl->fs;
  171. size_t ret = -EACCES;
  172. if (!fs || !fs->fs_ops)
  173. goto out;
  174. if (!fs->fs_ops->seek) {
  175. ret = -ESPIPE;
  176. goto out;
  177. }
  178. if (!fs->fs_ops->read)
  179. goto out;
  180. if (hdl->type == TYPE_DIR)
  181. goto out;
  182. int offset = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
  183. if (offset < 0) {
  184. ret = offset;
  185. goto out;
  186. }
  187. ret = fs->fs_ops->seek(hdl, pos, SEEK_SET);
  188. if (ret < 0)
  189. goto out;
  190. int bytes = fs->fs_ops->read(hdl, buf, count);
  191. ret = fs->fs_ops->seek(hdl, offset, SEEK_SET);
  192. if (ret < 0)
  193. goto out;
  194. ret = bytes;
  195. out:
  196. put_handle(hdl);
  197. return ret;
  198. }
  199. size_t shim_do_pwrite64 (int fd, char * buf, size_t count, loff_t pos)
  200. {
  201. if (!buf || test_user_memory(buf, count, false))
  202. return -EFAULT;
  203. if (pos < 0)
  204. return -EINVAL;
  205. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  206. if (!hdl)
  207. return -EBADF;
  208. struct shim_mount * fs = hdl->fs;
  209. size_t ret = -EACCES;
  210. if (!fs || !fs->fs_ops)
  211. goto out;
  212. if (!fs->fs_ops->seek) {
  213. ret = -ESPIPE;
  214. goto out;
  215. }
  216. if (!fs->fs_ops->write)
  217. goto out;
  218. if (hdl->type == TYPE_DIR)
  219. goto out;
  220. int offset = fs->fs_ops->seek(hdl, 0, SEEK_CUR);
  221. if (offset < 0) {
  222. ret = offset;
  223. goto out;
  224. }
  225. ret = fs->fs_ops->seek(hdl, pos, SEEK_SET);
  226. if (ret < 0)
  227. goto out;
  228. int bytes = fs->fs_ops->write(hdl, buf, count);
  229. ret = fs->fs_ops->seek(hdl, offset, SEEK_SET);
  230. if (ret < 0)
  231. goto out;
  232. ret = bytes;
  233. out:
  234. put_handle(hdl);
  235. return ret;
  236. }
  237. static inline int get_dirent_type (mode_t type)
  238. {
  239. switch (type) {
  240. case S_IFLNK:
  241. return LINUX_DT_LNK;
  242. case S_IFREG:
  243. return LINUX_DT_REG;
  244. case S_IFDIR:
  245. return LINUX_DT_DIR;
  246. case S_IFCHR:
  247. return LINUX_DT_CHR;
  248. case S_IFBLK:
  249. return LINUX_DT_BLK;
  250. case S_IFIFO:
  251. return LINUX_DT_FIFO;
  252. case S_IFSOCK:
  253. return LINUX_DT_SOCK;
  254. default:
  255. return LINUX_DT_UNKNOWN;
  256. }
  257. }
  258. size_t shim_do_getdents (int fd, struct linux_dirent * buf, size_t count)
  259. {
  260. if (!buf || test_user_memory(buf, count, true))
  261. return -EFAULT;
  262. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  263. if (!hdl)
  264. return -EBADF;
  265. int ret = -EACCES;
  266. if (hdl->type != TYPE_DIR) {
  267. ret = -ENOTDIR;
  268. goto out_no_unlock;
  269. }
  270. /* DEP 3/3/17: Properly handle an unlinked directory */
  271. if (hdl->dentry->state & DENTRY_NEGATIVE) {
  272. ret = -ENOENT;
  273. goto out_no_unlock;
  274. }
  275. /* we are grabbing the lock because the handle content is actually
  276. updated */
  277. lock(hdl->lock);
  278. struct shim_dir_handle * dirhdl = &hdl->info.dir;
  279. struct shim_dentry * dent = hdl->dentry;
  280. struct linux_dirent * b = buf;
  281. int bytes = 0;
  282. /* If we haven't listed the directory, do this first */
  283. if (!(dent->state & DENTRY_LISTED)) {
  284. ret = list_directory_dentry(dent);
  285. if (ret < 0)
  286. goto out;
  287. }
  288. #define DIRENT_SIZE(len) (sizeof(struct linux_dirent) + \
  289. sizeof(struct linux_dirent_tail) + (len) + 1)
  290. #define ASSIGN_DIRENT(dent, name, type) \
  291. do { \
  292. int len = strlen(name); \
  293. if (bytes + DIRENT_SIZE(len) > count) \
  294. goto done; \
  295. \
  296. struct linux_dirent_tail * bt \
  297. = (void *) b + sizeof(struct linux_dirent) + len + 1; \
  298. \
  299. b->d_ino = dent->ino; \
  300. b->d_off = ++dirhdl->offset; \
  301. b->d_reclen = DIRENT_SIZE(len); \
  302. \
  303. memcpy(b->d_name, name, len + 1); \
  304. \
  305. bt->pad = 0; \
  306. bt->d_type = type ? : get_dirent_type(dent->mode); \
  307. \
  308. b = (void *) bt + sizeof(struct linux_dirent_tail); \
  309. bytes += DIRENT_SIZE(len); \
  310. } while(0)
  311. if (dirhdl->dot) {
  312. ASSIGN_DIRENT(dirhdl->dot, ".", LINUX_DT_DIR);
  313. put_dentry(dirhdl->dot);
  314. dirhdl->dot = NULL;
  315. }
  316. if (dirhdl->dotdot) {
  317. ASSIGN_DIRENT(dirhdl->dotdot, "..", LINUX_DT_DIR);
  318. put_dentry(dirhdl->dotdot);
  319. dirhdl->dotdot = NULL;
  320. }
  321. if (dirhdl->ptr == (void *) -1) {
  322. ret = list_directory_handle(dent, hdl);
  323. if (ret < 0)
  324. goto out;
  325. }
  326. while (dirhdl->ptr && *dirhdl->ptr) {
  327. dent = *dirhdl->ptr;
  328. /* DEP 3/3/17: We need to filter negative dentries */
  329. if (!(dent->state & DENTRY_NEGATIVE))
  330. ASSIGN_DIRENT(dent, dentry_get_name(dent), 0);
  331. put_dentry(dent);
  332. *(dirhdl->ptr++) = NULL;
  333. }
  334. #undef DIRENT_SIZE
  335. #undef ASSIGN_DIRENT
  336. done:
  337. ret = bytes;
  338. /* DEP 3/3/17: Properly detect EINVAL case, where buffer is too small to
  339. * hold anything */
  340. if (bytes == 0 && (dirhdl->dot || dirhdl->dotdot ||
  341. (dirhdl->ptr && *dirhdl->ptr)))
  342. ret = -EINVAL;
  343. out:
  344. unlock(hdl->lock);
  345. out_no_unlock:
  346. put_handle(hdl);
  347. return ret;
  348. }
  349. size_t shim_do_getdents64 (int fd, struct linux_dirent64 * buf, size_t count)
  350. {
  351. if (!buf || test_user_memory(buf, count, true))
  352. return -EFAULT;
  353. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  354. if (!hdl)
  355. return -EBADF;
  356. int ret = -EACCES;
  357. if (hdl->type != TYPE_DIR) {
  358. ret = -ENOTDIR;
  359. goto out;
  360. }
  361. /* DEP 3/3/17: Properly handle an unlinked directory */
  362. if (hdl->dentry->state & DENTRY_NEGATIVE) {
  363. ret = -ENOENT;
  364. goto out;
  365. }
  366. lock(hdl->lock);
  367. struct shim_dir_handle * dirhdl = &hdl->info.dir;
  368. struct shim_dentry * dent = hdl->dentry;
  369. struct linux_dirent64 * b = buf;
  370. int bytes = 0;
  371. /* If we haven't listed the directory, do this first */
  372. if (!(dent->state & DENTRY_LISTED)) {
  373. ret = list_directory_dentry(dent);
  374. if (ret) goto out;
  375. }
  376. #define DIRENT_SIZE(len) (sizeof(struct linux_dirent64) + (len) + 1)
  377. #define ASSIGN_DIRENT(dent, name, type) \
  378. do { \
  379. int len = strlen(name); \
  380. if (bytes + DIRENT_SIZE(len) > count) \
  381. goto done; \
  382. \
  383. b->d_ino = dent->ino; \
  384. b->d_off = ++dirhdl->offset; \
  385. b->d_reclen = DIRENT_SIZE(len); \
  386. b->d_type = type ? : get_dirent_type(dent->mode); \
  387. \
  388. memcpy(b->d_name, name, len + 1); \
  389. \
  390. b = (void *) b + DIRENT_SIZE(len); \
  391. bytes += DIRENT_SIZE(len); \
  392. } while(0)
  393. if (dirhdl->dot) {
  394. ASSIGN_DIRENT(dirhdl->dot, ".", LINUX_DT_DIR);
  395. put_dentry(dirhdl->dot);
  396. dirhdl->dot = NULL;
  397. }
  398. if (dirhdl->dotdot) {
  399. ASSIGN_DIRENT(dirhdl->dotdot, "..", LINUX_DT_DIR);
  400. put_dentry(dirhdl->dotdot);
  401. dirhdl->dotdot = NULL;
  402. }
  403. if (dirhdl->ptr == (void *) -1) {
  404. ret = list_directory_handle(dent, hdl);
  405. if (ret) goto out;
  406. }
  407. while (dirhdl->ptr && *dirhdl->ptr) {
  408. dent = *dirhdl->ptr;
  409. /* DEP 3/3/17: We need to filter negative dentries */
  410. if (!(dent->state & DENTRY_NEGATIVE))
  411. ASSIGN_DIRENT(dent, dentry_get_name(dent), 0);
  412. put_dentry(dent);
  413. *(dirhdl->ptr++) = NULL;
  414. }
  415. #undef DIRENT_SIZE
  416. #undef ASSIGN_DIRENT
  417. done:
  418. ret = bytes;
  419. /* DEP 3/3/17: Properly detect EINVAL case, where buffer is too small to
  420. * hold anything */
  421. if (bytes == 0 && (dirhdl->dot || dirhdl->dotdot ||
  422. (dirhdl->ptr && *dirhdl->ptr)))
  423. ret = -EINVAL;
  424. unlock(hdl->lock);
  425. out:
  426. put_handle(hdl);
  427. return ret;
  428. }
  429. int shim_do_fsync (int fd)
  430. {
  431. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  432. if (!hdl)
  433. return -EBADF;
  434. int ret = -EACCES;
  435. struct shim_mount * fs = hdl->fs;
  436. if (!fs || !fs->fs_ops)
  437. goto out;
  438. if (hdl->type == TYPE_DIR)
  439. goto out;
  440. if (!fs->fs_ops->flush) {
  441. ret = -EROFS;
  442. goto out;
  443. }
  444. ret = fs->fs_ops->flush(hdl);
  445. out:
  446. put_handle(hdl);
  447. return ret;
  448. }
  449. // DEP 10/20/16: Assuming fsync >> fdatasync for now
  450. // and no app depends on only syncing data for correctness.
  451. int shim_do_fdatasync (int fd)
  452. {
  453. return shim_do_fsync(fd);
  454. }
  455. int shim_do_truncate (const char * path, loff_t length)
  456. {
  457. struct shim_dentry * dent = NULL;
  458. int ret = 0;
  459. if (!path || test_user_string(path))
  460. return -EFAULT;
  461. if ((ret = path_lookupat(NULL, path, 0, &dent, NULL)) < 0)
  462. return ret;
  463. struct shim_mount * fs = dent->fs;
  464. if (!fs || !fs->d_ops || !fs->d_ops->open) {
  465. ret = -EBADF;
  466. goto out;
  467. }
  468. if (!fs->fs_ops->truncate) {
  469. ret = -EROFS;
  470. goto out;
  471. }
  472. struct shim_handle * hdl = get_new_handle();
  473. if (!hdl) {
  474. ret = -ENOMEM;
  475. goto out;
  476. }
  477. hdl->fs = fs;
  478. if ((ret = fs->d_ops->open(hdl, dent, O_WRONLY)) < 0)
  479. goto out_handle;
  480. ret = fs->fs_ops->truncate(hdl, length);
  481. flush_handle(hdl);
  482. out_handle:
  483. put_handle(hdl);
  484. out:
  485. return ret;
  486. }
  487. int shim_do_ftruncate (int fd, loff_t length)
  488. {
  489. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  490. if (!hdl)
  491. return -EBADF;
  492. struct shim_mount * fs = hdl->fs;
  493. int ret = -EINVAL;
  494. if (!fs || !fs->fs_ops)
  495. goto out;
  496. if (hdl->type == TYPE_DIR ||
  497. !fs->fs_ops->truncate)
  498. goto out;
  499. ret = fs->fs_ops->truncate(hdl, length);
  500. out:
  501. put_handle(hdl);
  502. return ret;
  503. }