Chapter Contents

Previous

Next
setregid

setregid



Set Real and/or Effective Group ID

Portability: UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
USAGE NOTES
EXAMPLE
RELATED FUNCTIONS


SYNOPSIS

#include <sys/types.h>
#include <unistd.h>
int setregid(gid_t realgid, 
             gid_t effgid);


DESCRIPTION

The setregid function is used to set the real and/or the effective group ids for the calling process. A superuser or daemon process has the ability to set any valid group id. Other processes are limited in their use of setregid .

Note:   See the IBM UNIX System Services Assembler Callable Services publication, SC28-2899, for more information on the use of setregid by unprivileged processes.  [cautionend]
The realgid argument to setregid specifies the new real group id. If realgid is specified as -1 , the real group id is unchanged. The effgid argument to setregid specifies the new effective group id. If effgid is specified as -1 , the effective group id is unchanged.


RETURN VALUE

setregid returns 0 if successful, or -1 if unsuccessful.


USAGE NOTES

The setregid function can only be used with MVS 5.2.2 or a later release.


EXAMPLE

This example is compiled using sascc370 -Krent -o . This program demonstrates the use of the UNIX System Services functions setregid() and setreuid() .

/*---------------------------------------+
| POSIX/UNIX header files                |
+---------------------------------------*/
#include <sys/types.h>

#include <unistd.h>

/*---------------------------------------+
| ISO/ANSI header files                  |
+---------------------------------------*/
#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

/*---------------------------------------+
| Name:      main                        |
| Returns:   exit(EXIT_SUCCESS) or       |
|            exit(EXIT_FAILURE)          |
+---------------------------------------*/
int main()
{
/* generic return code                          */
   int rc;            

/* real group ID of calling process             */
   gid_t rgid;        

/* effective group ID of calling process        */
   gid_t egid;        

/* real user ID of calling process              */
   uid_t ruid;        

/* effective user ID of calling process         */
   uid_t euid;        

/*----------------------------------------------*/
/* Get the real and effective group and user    */
/* ids for this process.                        */
/*----------------------------------------------*/
   printf("\nGet the Real and Effective 
      Group and User IDs\n");
/* get real group ID                            */
   rgid = getgid();   

/* get effective group ID                       */
   egid = getegid();  

/* get real user ID                             */
   ruid = getuid();   

/* get effective user ID                        */
   euid = geteuid();  

   printf("     The real group id is: %d\n", 
    (int)rgid);
   printf("The effective group id is: %d\n", 
    (int)egid);
   printf("      The real user id is: %d\n", 
    (int)ruid);
   printf(" The effective user id is: %d\n", 
    (int)euid);

/*-----------------------------------------------*/
/* Set the real and effective group id for this  */
/* process.                                      */
/*-----------------------------------------------*/
printf("\nSetting the Real and Effective Group ID\n");

   errno = 0;
   egid = rgid;
/* -1 implies use current real group id (nochg) */
   rgid = (gid_t)-1;  

   rc = setregid(rgid, egid);

   /* Test for Error */
   if (rc == -1)
   {
      perror("Call to setregid failed");
      exit(EXIT_FAILURE);
   }

/*--------------------------------------------*/
/* Set the real and effective user id for     */
/* this process.                              */
/*--------------------------------------------*/
 printf("\nSetting the Real and 
   Effective User ID\n");

   errno = 0;
   euid = ruid;
/* -1 implies use current real user id (nochg) */
   ruid = (gid_t)-1;  

   rc = setreuid(ruid, euid);

   /* Test for Error */
   if (rc == -1)
   {
      perror("Call to setreuid failed");
      exit(EXIT_FAILURE);
   }

/*-----------------------------------------*/
/* Get the real and effective group and    */
/* user ids                                */
/*-----------------------------------------*/
   printf("\nThe Real and Effective 
     Group and User IDs are now!\n");

/* get real group ID                       */
   rgid = getgid();   

/* get effective group ID                  */
   egid = getegid();  

/* get real user ID                        */
   ruid = getuid();   

/* get effective user ID                   */
   euid = geteuid();  

   printf("     The real group id is: %d\n", 
    (int)rgid);
   printf("The effective group id is: %d\n", 
    (int)egid);
   printf("      The real user id is: %d\n", 
    (int)ruid);
   printf(" The effective user id is: %d\n", 
    (int)euid);

   exit(EXIT_SUCCESS);

}  /* end of main() */


RELATED FUNCTIONS

getegid , getgid , setegid , setgid


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.