geteuids.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright (C) 1993-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library 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 GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <hurd.h>
  15. #include <hurd/id.h>
  16. #include <string.h>
  17. int
  18. geteuids (int n, uid_t *uidset)
  19. {
  20. error_t err;
  21. int nuids;
  22. void *crit;
  23. retry:
  24. crit = _hurd_critical_section_lock ();
  25. __mutex_lock (&_hurd_id.lock);
  26. if (err = _hurd_check_ids ())
  27. {
  28. __mutex_unlock (&_hurd_id.lock);
  29. _hurd_critical_section_unlock (crit);
  30. if (err == EINTR)
  31. /* Got a signal while inside an RPC of the critical section, retry again */
  32. goto retry;
  33. return __hurd_fail (err);
  34. }
  35. nuids = _hurd_id.gen.nuids;
  36. if (n != 0)
  37. {
  38. /* Copy the uids onto stack storage and then release the idlock. */
  39. uid_t uids[nuids];
  40. memcpy (uids, _hurd_id.gen.uids, sizeof (uids));
  41. __mutex_unlock (&_hurd_id.lock);
  42. _hurd_critical_section_unlock (crit);
  43. /* Now that the lock is released, we can safely copy the
  44. uid set into the user's array, which might fault. */
  45. if (nuids > n)
  46. nuids = n;
  47. memcpy (uidset, uids, nuids * sizeof (uid_t));
  48. }
  49. else
  50. {
  51. __mutex_unlock (&_hurd_id.lock);
  52. _hurd_critical_section_unlock (crit);
  53. }
  54. return nuids;
  55. }
  56. /* XXX Remove this alias when we bump the libc soname. */
  57. #ifdef SHARED
  58. weak_alias (geteuids, __getuids)
  59. #endif