Back to index

glibc  2.9
getgroups.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <unistd.h>
00021 #include <sys/param.h>
00022 #include <sys/types.h>
00023 
00024 #include <sysdep.h>
00025 #include <sys/syscall.h>
00026 #include <bp-checks.h>
00027 
00028 #include <linux/posix_types.h>
00029 #include <kernel-features.h>
00030 
00031 
00032 #ifdef __NR_getgroups32
00033 # if __ASSUME_32BITUIDS == 0
00034 /* This variable is shared with all files that need to check for 32bit
00035    uids.  */
00036 extern int __libc_missing_32bit_uids attribute_hidden;
00037 # endif
00038 #endif /* __NR_getgroups32 */
00039 
00040 /* For Linux we must convert the array of groups from the format that the
00041    kernel returns.  */
00042 int
00043 __getgroups (int n, gid_t *groups)
00044 {
00045 #if __ASSUME_32BITUIDS > 0
00046   return INLINE_SYSCALL (getgroups32, 2, n, CHECK_N (groups, n));
00047 #else
00048   if (__builtin_expect (n, 1) < 0)
00049     {
00050       __set_errno (EINVAL);
00051       return -1;
00052     }
00053   else
00054     {
00055 # ifdef __NR_getgroups32
00056       if (__libc_missing_32bit_uids <= 0)
00057        {
00058          int result;
00059          int saved_errno = errno;
00060 
00061          result = INLINE_SYSCALL (getgroups32, 2, n, CHECK_N (groups, n));
00062          if (result != -1 || errno != ENOSYS)
00063            return result;
00064 
00065          __set_errno (saved_errno);
00066          __libc_missing_32bit_uids = 1;
00067        }
00068 # endif /* __NR_getgroups32 */
00069 
00070       int i, ngids;
00071       __kernel_gid_t kernel_groups[n = MIN (n, __sysconf (_SC_NGROUPS_MAX))];
00072 
00073       ngids = INLINE_SYSCALL (getgroups, 2, n, CHECK_N (kernel_groups, n));
00074       if (n != 0 && ngids > 0)
00075        for (i = 0; i < ngids; i++)
00076          (__ptrvalue (groups))[i] = kernel_groups[i];
00077       return ngids;
00078     }
00079 #endif
00080 }
00081 
00082 weak_alias (__getgroups, getgroups)