shim_dup.c 2.3 KB

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