(root)/
glibc-2.38/
nptl/
tst-setgroups.c
       1  /* Test setgroups as root and in the presence of threads (Bug 26248)
       2     Copyright (C) 2020-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <stdlib.h>
      20  #include <limits.h>
      21  #include <grp.h>
      22  #include <errno.h>
      23  #include <error.h>
      24  #include <support/xthread.h>
      25  #include <support/support.h>
      26  #include <support/test-driver.h>
      27  #include <support/xunistd.h>
      28  
      29  /* The purpose of this test is to test the setgroups API as root and in
      30     the presence of threads.  Once we create a thread the setgroups
      31     implementation must ensure that all threads are set to the same
      32     group and this operation should not fail. Lastly we test setgroups
      33     with a zero sized group and a bad address and verify we get EPERM.  */
      34  
      35  static void *
      36  start_routine (void *args)
      37  {
      38    return NULL;
      39  }
      40  
      41  static int
      42  do_test (void)
      43  {
      44    int size;
      45    /* NB: Stack address can be at 0xfffXXXXX on 32-bit OSes.  */
      46    gid_t list[NGROUPS_MAX];
      47    int status = EXIT_SUCCESS;
      48  
      49    pthread_t thread = xpthread_create (NULL, start_routine, NULL);
      50  
      51    size = getgroups (sizeof (list) / sizeof (list[0]), list);
      52    if (size < 0)
      53      {
      54        status = EXIT_FAILURE;
      55        error (0, errno, "getgroups failed");
      56      }
      57    if (setgroups (size, list) < 0)
      58      {
      59        if (errno == EPERM)
      60  	status = EXIT_UNSUPPORTED;
      61        else
      62  	{
      63  	  status = EXIT_FAILURE;
      64  	  error (0, errno, "setgroups failed");
      65  	}
      66      }
      67  
      68    if (status == EXIT_SUCCESS && setgroups (0, list) < 0)
      69      {
      70        status = EXIT_FAILURE;
      71        error (0, errno, "setgroups failed");
      72      }
      73  
      74    xpthread_join (thread);
      75  
      76    return status;
      77  }
      78  
      79  #include <support/test-driver.c>