fcntl.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Debug.h>
  7. #include <Kernel/FileSystem/FileDescription.h>
  8. #include <Kernel/Process.h>
  9. namespace Kernel {
  10. KResultOr<FlatPtr> Process::sys$fcntl(int fd, int cmd, u32 arg)
  11. {
  12. REQUIRE_PROMISE(stdio);
  13. dbgln_if(IO_DEBUG, "sys$fcntl: fd={}, cmd={}, arg={}", fd, cmd, arg);
  14. auto description = file_description(fd);
  15. if (!description)
  16. return EBADF;
  17. // NOTE: The FD flags are not shared between FileDescription objects.
  18. // This means that dup() doesn't copy the FD_CLOEXEC flag!
  19. switch (cmd) {
  20. case F_DUPFD: {
  21. int arg_fd = (int)arg;
  22. if (arg_fd < 0)
  23. return EINVAL;
  24. int new_fd = alloc_fd(arg_fd);
  25. if (new_fd < 0)
  26. return new_fd;
  27. m_fds[new_fd].set(*description);
  28. return new_fd;
  29. }
  30. case F_GETFD:
  31. return m_fds[fd].flags();
  32. case F_SETFD:
  33. m_fds[fd].set_flags(arg);
  34. break;
  35. case F_GETFL:
  36. return description->file_flags();
  37. case F_SETFL:
  38. description->set_file_flags(arg);
  39. break;
  40. case F_ISTTY:
  41. return description->is_tty();
  42. default:
  43. return EINVAL;
  44. }
  45. return 0;
  46. }
  47. }