getgroups.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* provide consistent interface to getgroups for systems that don't allow N==0
  2. Copyright (C) 1996, 1999, 2003, 2006-2015 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* written by Jim Meyering */
  14. #include <config.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #if !HAVE_GETGROUPS
  20. /* Provide a stub that fails with ENOSYS, since there is no group
  21. information available on mingw. */
  22. int
  23. getgroups (int n _GL_UNUSED, GETGROUPS_T *groups _GL_UNUSED)
  24. {
  25. errno = ENOSYS;
  26. return -1;
  27. }
  28. #else /* HAVE_GETGROUPS */
  29. # undef getgroups
  30. # ifndef GETGROUPS_ZERO_BUG
  31. # define GETGROUPS_ZERO_BUG 0
  32. # endif
  33. /* On OS X 10.6 and later, use the usual getgroups, not the one
  34. supplied when _DARWIN_C_SOURCE is defined. _DARWIN_C_SOURCE is
  35. normally defined, since it means "conform to POSIX, but add
  36. non-POSIX extensions even if that violates the POSIX namespace
  37. rules", which is what we normally want. But with getgroups there
  38. is an inconsistency, and _DARWIN_C_SOURCE means "change getgroups()
  39. so that it no longer works right". The BUGS section of compat(5)
  40. says that the behavior is dubious if you compile different sections
  41. of a program with different _DARWIN_C_SOURCE settings, so fix only
  42. the offending symbol. */
  43. # ifdef __APPLE__
  44. int posix_getgroups (int, gid_t []) __asm ("_getgroups");
  45. # define getgroups posix_getgroups
  46. # endif
  47. /* On at least Ultrix 4.3 and NextStep 3.2, getgroups (0, NULL) always
  48. fails. On other systems, it returns the number of supplemental
  49. groups for the process. This function handles that special case
  50. and lets the system-provided function handle all others. However,
  51. it can fail with ENOMEM if memory is tight. It is unspecified
  52. whether the effective group id is included in the list. */
  53. int
  54. rpl_getgroups (int n, gid_t *group)
  55. {
  56. int n_groups;
  57. GETGROUPS_T *gbuf;
  58. int saved_errno;
  59. if (n < 0)
  60. {
  61. errno = EINVAL;
  62. return -1;
  63. }
  64. if (n != 0 || !GETGROUPS_ZERO_BUG)
  65. {
  66. int result;
  67. if (sizeof *group == sizeof *gbuf)
  68. return getgroups (n, (GETGROUPS_T *) group);
  69. if (SIZE_MAX / sizeof *gbuf <= n)
  70. {
  71. errno = ENOMEM;
  72. return -1;
  73. }
  74. gbuf = malloc (n * sizeof *gbuf);
  75. if (!gbuf)
  76. return -1;
  77. result = getgroups (n, gbuf);
  78. if (0 <= result)
  79. {
  80. n = result;
  81. while (n--)
  82. group[n] = gbuf[n];
  83. }
  84. saved_errno = errno;
  85. free (gbuf);
  86. errno = saved_errno;
  87. return result;
  88. }
  89. n = 20;
  90. while (1)
  91. {
  92. /* No need to worry about address arithmetic overflow here,
  93. since the ancient systems that we're running on have low
  94. limits on the number of secondary groups. */
  95. gbuf = malloc (n * sizeof *gbuf);
  96. if (!gbuf)
  97. return -1;
  98. n_groups = getgroups (n, gbuf);
  99. if (n_groups == -1 ? errno != EINVAL : n_groups < n)
  100. break;
  101. free (gbuf);
  102. n *= 2;
  103. }
  104. saved_errno = errno;
  105. free (gbuf);
  106. errno = saved_errno;
  107. return n_groups;
  108. }
  109. #endif /* HAVE_GETGROUPS */