shim_dup.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * shim_clone.c
  15. *
  16. * Implementation of system call "dup", "dup2" and "dup3".
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_table.h>
  20. #include <shim_thread.h>
  21. #include <shim_handle.h>
  22. #include <shim_fs.h>
  23. #include <shim_utils.h>
  24. #include <pal.h>
  25. #include <pal_error.h>
  26. #include <errno.h>
  27. int shim_do_dup (int fd)
  28. {
  29. struct shim_handle_map * handle_map = get_cur_handle_map(NULL);
  30. int flags = 0;
  31. struct shim_handle * hdl = get_fd_handle(fd, &flags, handle_map);
  32. if (!hdl)
  33. return -EBADF;
  34. int vfd = set_new_fd_handle(hdl, flags, handle_map);
  35. put_handle(hdl);
  36. return vfd < 0 ? -EMFILE : vfd;
  37. }
  38. int shim_do_dup2 (int oldfd, int newfd)
  39. {
  40. struct shim_handle_map * handle_map = get_cur_handle_map(NULL);
  41. struct shim_handle * hdl = get_fd_handle(oldfd, NULL, handle_map);
  42. if (!hdl)
  43. return -EBADF;
  44. struct shim_handle * new_hdl = detach_fd_handle(newfd, NULL, handle_map);
  45. if (new_hdl)
  46. put_handle(new_hdl);
  47. int vfd = set_new_fd_handle_by_fd(newfd, hdl, 0, handle_map);
  48. put_handle(hdl);
  49. return vfd < 0 ? -EMFILE : vfd;
  50. }
  51. int shim_do_dup3 (int oldfd, int newfd, int flags)
  52. {
  53. struct shim_handle_map * handle_map = get_cur_handle_map(NULL);
  54. struct shim_handle * hdl = get_fd_handle(oldfd, NULL, handle_map);
  55. if (!hdl)
  56. return -EBADF;
  57. struct shim_handle * new_hdl = detach_fd_handle(newfd, NULL, handle_map);
  58. if (new_hdl)
  59. put_handle(new_hdl);
  60. int vfd = set_new_fd_handle_by_fd(newfd, hdl, flags, handle_map);
  61. put_handle(hdl);
  62. return vfd < 0 ? -EMFILE : vfd;
  63. }