getgroups, setgroups - get/set list of supplementary group IDs
getgroups, setgroups - 获取/设置附加组id的列表
所需头文件
#include <sys/types.h>
#include <unistd.h>
int getgroups(int size, gid_t list[]);
#include <grp.h>
int setgroups(size_t size, const gid_t *list);
getgroups() returns the supplementary group IDs of the calling process in list. The argument size should be set to the maximum number of items that can be stored in the buffer pointed to by list. If the calling process is a member of more than size supplementary groups, then an error results. It is unspecified whether the effective group ID of the calling process is included in the returned list. (Thus, an application should also call getegid(2) and add or remove the resulting value.)
getgroups返回调用进程的附加组id列表,参数size应该设置成list数组所能容纳最多的item值,如果调用进程大于附加组的的size,会返回出错的结果,并没有规定调用进程的有效组id是否要包含在返回列表里面
If size is zero, list is not modified, but the total number of supplementary group IDs for the process is returned. This allows the caller to determine the size of a dynamically allocated list to be used in a further call to getgroups().
如果size的值是0,list将不可修改的,附加组id的数目将会被返回,这样子调用者可以动态的确定后续getgroup的调用list的大小
setgroups() sets the supplementary group IDs for the calling process. Appropriate privileges (Linux: the CAP_SETGID capability) are required. The size argument specifies the number of supplementary group IDs in the buffer pointed to by list.
setgroups设置调用进程的附加组id,需要适当的权限,size参数指出了list数组里面附加组id的数量
On success, getgroups() returns the number of supplementary group IDs. On error, -1 is returned, and errno is set appropriately.
On success, setgroups() returns 0. On error, -1 is returned, and errno is set appropriately.
成功的时候getgroups返回附加组id的数量,失败的时候返回-1,errno被设置成适当的值
成功的时候setgroups返回0,失败的时候返回-1,errno被设置成适当的值
getgroups() can additionally fail with the following errors:
EFAULT list has an invalid address.
list数组的地址非法
EINVAL size is less than the number of supplementary group IDs, but is not zero.
size的值比附加组的数量少,但是不是0(因为是0的话其实是返回size的大小,并不是把结果存储到list数组中)
setgroups() can additionally fail with the following errors:
EINVAL size is greater than NGROUPS_MAX (32 before Linux 2.6.4; 65536 since Linux 2.6.4).
size的值大于NGROUPS_MAX
ENOMEM Out of memory.
内存不足
EPERM The calling process has insufficient privilege.
权限不够
A process can have up to NGROUPS_MAX supplementary group IDs in addition to the effective group ID. The set of supplementary group IDs is inherited from the parent process, and preserved across an execve(2).
The maximum number of supplementary group IDs can be found using sysconf(3):
long ngroups_max;
ngroups_max = sysconf(_SC_NGROUPS_MAX);
The maximum return value of getgroups() cannot be larger than one more than this value.
The original Linux getgroups() system call supported only 16-bit group IDs. Subsequently, Linux 2.4 added getgroups32(), supporting 32-bit IDs. The glibc getgroups() wrapper function transparently deals with the variation across kernel versions.
一个进程除有效组id之外,最多可以有NGROUPS_MAX个附加组id,附加组id序列是继承与父进程的,通过execve保存
最多附加组id的值可以用sysconf获取
getgroups的返回值不可能比这个值更大
运行的结果如下:
getgroups, setgroups - 获取/设置附加组id的列表
所需头文件
#include <sys/types.h>
#include <unistd.h>
int getgroups(int size, gid_t list[]);
#include <grp.h>
int setgroups(size_t size, const gid_t *list);
getgroups() returns the supplementary group IDs of the calling process in list. The argument size should be set to the maximum number of items that can be stored in the buffer pointed to by list. If the calling process is a member of more than size supplementary groups, then an error results. It is unspecified whether the effective group ID of the calling process is included in the returned list. (Thus, an application should also call getegid(2) and add or remove the resulting value.)
getgroups返回调用进程的附加组id列表,参数size应该设置成list数组所能容纳最多的item值,如果调用进程大于附加组的的size,会返回出错的结果,并没有规定调用进程的有效组id是否要包含在返回列表里面
If size is zero, list is not modified, but the total number of supplementary group IDs for the process is returned. This allows the caller to determine the size of a dynamically allocated list to be used in a further call to getgroups().
如果size的值是0,list将不可修改的,附加组id的数目将会被返回,这样子调用者可以动态的确定后续getgroup的调用list的大小
setgroups() sets the supplementary group IDs for the calling process. Appropriate privileges (Linux: the CAP_SETGID capability) are required. The size argument specifies the number of supplementary group IDs in the buffer pointed to by list.
setgroups设置调用进程的附加组id,需要适当的权限,size参数指出了list数组里面附加组id的数量
On success, getgroups() returns the number of supplementary group IDs. On error, -1 is returned, and errno is set appropriately.
On success, setgroups() returns 0. On error, -1 is returned, and errno is set appropriately.
成功的时候getgroups返回附加组id的数量,失败的时候返回-1,errno被设置成适当的值
成功的时候setgroups返回0,失败的时候返回-1,errno被设置成适当的值
getgroups() can additionally fail with the following errors:
EFAULT list has an invalid address.
list数组的地址非法
EINVAL size is less than the number of supplementary group IDs, but is not zero.
size的值比附加组的数量少,但是不是0(因为是0的话其实是返回size的大小,并不是把结果存储到list数组中)
setgroups() can additionally fail with the following errors:
EINVAL size is greater than NGROUPS_MAX (32 before Linux 2.6.4; 65536 since Linux 2.6.4).
size的值大于NGROUPS_MAX
ENOMEM Out of memory.
内存不足
EPERM The calling process has insufficient privilege.
权限不够
A process can have up to NGROUPS_MAX supplementary group IDs in addition to the effective group ID. The set of supplementary group IDs is inherited from the parent process, and preserved across an execve(2).
The maximum number of supplementary group IDs can be found using sysconf(3):
long ngroups_max;
ngroups_max = sysconf(_SC_NGROUPS_MAX);
The maximum return value of getgroups() cannot be larger than one more than this value.
The original Linux getgroups() system call supported only 16-bit group IDs. Subsequently, Linux 2.4 added getgroups32(), supporting 32-bit IDs. The glibc getgroups() wrapper function transparently deals with the variation across kernel versions.
一个进程除有效组id之外,最多可以有NGROUPS_MAX个附加组id,附加组id序列是继承与父进程的,通过execve保存
最多附加组id的值可以用sysconf获取
getgroups的返回值不可能比这个值更大
原始linux gegroups系统调用仅仅支持16字节的group id,后续的linux2.4增加的getgroup32函数来支持32字节的id,glibc getgroups系统调用跟kernel保持一致
testcase如下:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(void)
{
gid_t list[100];
int size;
size = getgroups(0, list);
getgroups(size, list);
while (size != -1) {
printf("list[%d] = %d\n", size, list[size]);
size--;
}
return 0;
}
运行的结果如下:
list[3] = 32704
list[2] = 1001
list[1] = 125
list[0] = 27
这个函数具体干啥用的我也不是很清楚,感觉很少用到。。。