hurdfchdir.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Change a port cell to a directory in an open file descriptor.
  2. Copyright (C) 1999-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <hurd.h>
  18. #include <hurd/port.h>
  19. #include <hurd/fd.h>
  20. #include <fcntl.h>
  21. int
  22. _hurd_change_directory_port_from_fd (struct hurd_port *portcell, int fd)
  23. {
  24. int ret;
  25. struct hurd_fd *d = _hurd_fd_get (fd);
  26. if (!d)
  27. return __hurd_fail (EBADF);
  28. retry:
  29. HURD_CRITICAL_BEGIN;
  30. ret = HURD_PORT_USE (&d->port,
  31. ({
  32. int ret;
  33. /* We look up "." to force ENOTDIR if it's not a
  34. directory and EACCES if we don't have search
  35. permission. */
  36. file_t dir = __file_name_lookup_under (port, ".",
  37. O_NOTRANS, 0);
  38. if (dir == MACH_PORT_NULL)
  39. ret = -1;
  40. else
  41. {
  42. _hurd_port_set (portcell, dir);
  43. ret = 0;
  44. }
  45. ret;
  46. }));
  47. HURD_CRITICAL_END;
  48. if (ret == -1 && errno == EINTR)
  49. /* Got a signal while inside an RPC of the critical section, retry again */
  50. goto retry;
  51. return ret;
  52. }