[dev] [utmp][PATCH 3/3] Launch a login shell

From: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
Date: Tue, 16 Sep 2014 08:48:01 +0200

Since we are creating a utmp entry, it is logical to create a login
shell
---
 utmp.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/utmp.c b/utmp.c
index f551f9d..75c35f5 100644
--- a/utmp.c
+++ b/utmp.c
_AT_@ -38,8 +38,10 @@ int
 main(int argc, char *argv[])
 {
 	int status;
+	size_t len;
 	uid_t uid;
 	sigset_t set;
+	char *p, argv0[FILENAME_MAX], *sh;
 	extern void addutmp(void), delutmp(void);
 
 	egid = getegid();
_AT_@ -59,15 +61,23 @@ main(int argc, char *argv[])
 	setenv("SHELL", pw->pw_shell, 1);
 	setenv("HOME", pw->pw_dir, 1);
 
+	if ((p = strrchr(pw->pw_shell, '/')) == NULL)
+		die("incorrect shell field of passwd");
+	if ((len = strlen(++p)) > sizeof(argv0) - 2)
+		die("shell name too long");
+	argv0[0] = '-';
+	memcpy(&argv0[1], p, len);
+
 	sigfillset(&set);
 	sigprocmask(SIG_BLOCK, &set, NULL);
 
 	switch (fork()) {
 	case 0:
 		sigprocmask(SIG_UNBLOCK, &set, NULL);
-		argv[0] = getenv("SHELL");
-		execv(argv[0], argv);
-		die("error executing shell:%s", strerror(errno));
+		sh = pw->pw_shell;
+		argv[0] = argv0;
+		execv(sh, argv);
+		die("error executing shell(%s):%s", sh, strerror(errno));
 	case -1:
 		die("error spawning child:%s", strerror(errno));
 	default:
-- 
1.9.3
Received on Tue Sep 16 2014 - 08:48:01 CEST

This archive was generated by hypermail 2.3.0 : Tue Sep 16 2014 - 09:00:22 CEST