getgrouplist

Section: C Library Functions (3)
Updated: 2025-09-21
Index JM Home Page roff page
 

名前

getgrouplist - ユーザーが所属するグループのリストを取得する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <grp.h>

int getgrouplist(const char *user, gid_t group,
                 gid_t *groups, int *ngroups);

glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):

getgrouplist():

    glibc 2.19 以降:
        _DEFAULT_SOURCE
    glibc 2.19 以前:
        _BSD_SOURCE
 

説明

getgrouplist() 関数は、グループデータベース (group(5) 参照) を調べて、 user が所属するグループのリストを取得する。 見つかったグループのうち最大 *ngroups 個のグループが、配列 groups に格納されて返される。

引数 group がグループデータベースに user が所属するグループがなかった場合、 getgrouplist() が返すグループのリストに引数 group も追加される。 通常は、この引数にはユーザー user のパスワードレコードに書かれているグループ ID を指定する。

引数 ngroups は、値渡しと結果の両方に使用される引数 (value-result argument) であり、 リターン時には、常に group も含めた user が所属するグループ数が格納される。 この値は groups に格納されたグループ数より大きくなる可能性がある。  

返り値

user が所属しているグループ数が *ngroups 以下の場合、 *ngroups の値が返される。

If the user is a member of more than *ngroups groups, then getgrouplist() returns -1. In this case, the value returned in *ngroups can be used to resize the buffer passed to a further call to getgrouplist().  

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
getgrouplist() Thread safetyMT-Safe locale
 

標準規格

なし。  

歴史

glibc 2.2.4.  

バグ

Before glibc 2.3.3, the implementation of this function contains a buffer-overrun bug: it returns the complete list of groups for user in the array groups, even when the number of groups exceeds *ngroups.  

例

以下のプログラムは、一つ目のコマンドライン引数で指定された名前のユーザー が所属するグループのリストを表示する。 二番目のコマンドライン引数には、 getgrouplist() に渡す ngroups の値を指定する。 以下のシェルのセッションはこのプログラムの使用例を示したものである。

$ ./a.out cecilia 0
getgrouplist() returned -1
ngroups = 3
$ ./a.out cecilia 3
ngroups = 3
16 (dialout)
33 (video)
100 (users)
 

プログラムのソース

#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    int            ngroups;
    gid_t          *groups;
    struct group   *gr;
    struct passwd  *pw;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s <user> <ngroups>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    ngroups = atoi(argv[2]);

    groups = malloc(sizeof(*groups) * ngroups);
    if (groups == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    /* Fetch passwd structure (contains first group ID for user).  */

    errno = 0;
    pw = getpwnam(argv[1]);
    if (pw == NULL) {
        if (errno)
            perror("getpwnam");
        else
            fprintf(stderr, "no such user\n");
        exit(EXIT_FAILURE);
    }

    /* Retrieve group list.  */

    if (getgrouplist(argv[1], pw->pw_gid, groups, &ngroups) == -1) {
        fprintf(stderr, "getgrouplist() returned -1\n");
        fprintf(stderr, "ngroups = %d\n", ngroups);
        exit(EXIT_FAILURE);
    }

    /* Display list of retrieved groups, along with group names.  */

    fprintf(stderr, "ngroups = %d\n", ngroups);
    for (int j = 0; j < ngroups; j++) {
        printf("%d", groups[j]);
        gr = getgrgid(groups[j]);
        if (gr != NULL)
            printf(" (%s)", gr->gr_name);
        printf("\n");
    }

    exit(EXIT_SUCCESS);
}
 

関連項目

getgroups(2), setgroups(2), getgrent(3), group_member(3), group(5), passwd(5)


 

Index

名前
ライブラリ
書式
説明
返り値
属性
標準規格
歴史
バグ
例
プログラムのソース
関連項目

This document was created by man2html, using the manual pages.