diff --git a/daemon.c b/daemon.c index 686b7fd..490e13f 100644 --- a/daemon.c +++ b/daemon.c @@ -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); - - errno = 0; - struct group *group = ( - groupName ? getgrnam(groupName) : getgrgid(user->pw_gid) - ); - if (errno) err(EX_OSFILE, "getgrnam"); - if (!group) errx(EX_USAGE, "no such group %s", groupName); - serviceUID = user->pw_uid; - serviceGID = group->gr_gid; + + if (groupName) { + errno = 0; + struct group *group = getgrnam(groupName); + if (errno) err(EX_OSFILE, "getgrnam"); + if (!group) errx(EX_USAGE, "no such group %s", groupName); + 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");