1

操作を続行する前に、ユーザーが正しいグループに属していることを確認しようとしています。何らかの理由で、奇妙なコンパイル時エラーが発生しています。

#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>

bool getGroup() {
    const std::string group = "Controller";

    group *grp = getgrnam(group.c_str());
    if (grp == NULL) {
        ERROR("No group (%s) exists", group.c_str());
        return false;
    }

    passwd *pw = getpwuid(getuid());
    std::string uid(pw->pw_name);

    for (char **member = grp->gr_mem; *member; member++) {
        if (uid.compare(*member) == 0) {
            DEBUG("Matched (%s) in group (%s)", uid.c_str(), group.c_str());
            return true;
        }
    }

    ERROR("Unable to match user (%s) in group (%s)", uid.c_str(), group.c_str());
    return false;
}

コンパイラが不平を言っていmain.cppます : 関数内

bool getGroup():
main.cpp:72: error: ‘grp’ was not declared in this scope

このエラーは次の行に対応します。

group *grp = getgrnam(group.c_str());

私はmanページでこれに関するドキュメントを読んでおり、正しくやっていると信じています。また、コードに間違っているものは何もありません。たぶん、新鮮な目が助けになるでしょう。

ありがとう!

4

2 に答える 2