Only look up group if one is named

This commit is contained in:
C. McEnroe 2020-10-23 11:37:10 -04:00
parent 87083ad381
commit 363ab9f834

View File

@ -242,16 +242,17 @@ int main(int argc, char *argv[]) {
struct passwd *user = (userName ? getpwnam(userName) : getpwuid(getuid()));
if (errno) err(EX_OSFILE, "getpwnam");
if (!user) errx(EX_USAGE, "no such user %s", userName);
serviceUID = user->pw_uid;
if (groupName) {
errno = 0;
struct group *group = (
groupName ? getgrnam(groupName) : getgrgid(user->pw_gid)
);
struct group *group = getgrnam(groupName);
if (errno) err(EX_OSFILE, "getgrnam");
if (!group) errx(EX_USAGE, "no such group %s", groupName);
serviceUID = user->pw_uid;
serviceGID = group->gr_gid;
} else {
serviceGID = user->pw_gid;
}
int len = asprintf(&serviceEnviron[USER], "USER=%s", user->pw_name);
if (len < 0) err(EX_OSERR, "asprintf");