summary refs log tree commit diff
path: root/daemon.c
diff options
context:
space:
mode:
Diffstat (limited to 'daemon.c')
-rw-r--r--daemon.c31
1 files changed, 22 insertions, 9 deletions
diff --git a/daemon.c b/daemon.c
index 6cbf8eb..a2ce4dc 100644
--- a/daemon.c
+++ b/daemon.c
@@ -134,8 +134,8 @@ int main(int argc, char *argv[]) {
 	const char *configPath = ETCDIR "/spawntab";
 	const char *fifoPath = RUNDIR "/spawnd.pipe";
 
-	const char *user = NULL;
-	const char *group = NULL;
+	const char *userName = NULL;
+	const char *groupName = NULL;
 
 	for (int opt; 0 < (opt = getopt(argc, argv, "C:c:df:g:p:s:t:u:"));) {
 		switch (opt) {
@@ -143,29 +143,42 @@ int main(int argc, char *argv[]) {
 			break; case 'c': fifoPath = optarg;
 			break; case 'd': daemonize = false;
 			break; case 'f': configPath = optarg;
-			break; case 'g': group = optarg;
+			break; case 'g': groupName = optarg;
 			break; case 'p': pidPath = optarg;
 			break; case 's': parseExits(optarg);
 			break; case 't': restartInterval = strtoul(optarg, NULL, 10);
-			break; case 'u': user = optarg;
+			break; case 'u': userName = optarg;
 			break; default:  return EX_USAGE;
 		}
 	}
-
 	parseConfig(true, configPath);
 	
 	int error = access(serviceDir, X_OK);
 	if (error) err(EX_NOINPUT, "%s", serviceDir);
 
 	errno = 0;
-	serviceUser = (user ? getpwnam(user) : getpwuid(getuid()));
+	struct passwd *user = (userName ? getpwnam(userName) : getpwuid(getuid()));
 	if (errno) err(EX_OSFILE, "getpwnam");
-	if (!serviceUser) errx(EX_USAGE, "no such user %s", user);
+	if (!user) errx(EX_USAGE, "no such user %s", userName);
 
 	errno = 0;
-	serviceGroup = (group ? getgrnam(group) : getgrgid(serviceUser->pw_gid));
+	struct group *group = (
+		groupName ? getgrnam(groupName) : getgrgid(user->pw_gid)
+	);
 	if (errno) err(EX_OSFILE, "getgrnam");
-	if (!serviceGroup) errx(EX_USAGE, "no such group %s", group);
+	if (!group) errx(EX_USAGE, "no such group %s", groupName);
+
+	serviceUID = user->pw_uid;
+	serviceGID = group->gr_gid;
+
+	int len = asprintf(&serviceEnviron[LOGNAME], "LOGNAME=%s", user->pw_name);
+	if (len < 0) err(EX_OSERR, "asprintf");
+
+	len = asprintf(&serviceEnviron[USER], "USER=%s", user->pw_name);
+	if (len < 0) err(EX_OSERR, "asprintf");
+
+	len = asprintf(&serviceEnviron[HOME], "HOME=%s", user->pw_dir);
+	if (len < 0) err(EX_OSERR, "asprintf");
 
 	int pidFile = -1;
 	if (pidPath) {