shim_dup.c 2.5 KB

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