Back to index

glibc  2.9
getgroups.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993, 1994, 1995, 1997 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 <hurd.h>
00022 #include <hurd/id.h>
00023 #include <string.h>
00024 
00025 int
00026 __getgroups (n, gidset)
00027      int n;
00028      gid_t *gidset;
00029 {
00030   error_t err;
00031   int ngids;
00032   void *crit;
00033 
00034   crit = _hurd_critical_section_lock ();
00035   __mutex_lock (&_hurd_id.lock);
00036 
00037   if (err = _hurd_check_ids ())
00038     {
00039       __mutex_unlock (&_hurd_id.lock);
00040       _hurd_critical_section_unlock (crit);
00041       return __hurd_fail (err);
00042     }
00043 
00044   ngids = _hurd_id.gen.ngids;
00045 
00046   if (n != 0)
00047     {
00048       /* Copy the gids onto stack storage and then release the idlock.  */
00049       gid_t gids[ngids];
00050       memcpy (gids, _hurd_id.gen.gids, sizeof (gids));
00051       __mutex_unlock (&_hurd_id.lock);
00052       _hurd_critical_section_unlock (crit);
00053 
00054       /* Now that the lock is released, we can safely copy the
00055         group set into the user's array, which might fault.  */
00056       if (ngids > n)
00057        ngids = n;
00058       memcpy (gidset, gids, ngids * sizeof (gid_t));
00059     }
00060   else
00061     {
00062       __mutex_unlock (&_hurd_id.lock);
00063       _hurd_critical_section_unlock (crit);
00064     }
00065 
00066   return ngids;
00067 }
00068 
00069 weak_alias (__getgroups, getgroups)