Initialize environment variables to correct values

From: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
Date: Mon, 8 Oct 2012 21:03:54 +0200

Calling st without SHELL variable caused a segmentation fault, because no
test was done about this variable. It is responsability of the terminal
emulator set correct values for LOGNAME and USER, and the value for the
variable WINDOWID (It contains a numeric identifier of the window that
contains the terminal. (This allows X-aware applications that are run from
console to do some operations over the window, e.g. changing its icon.)

This patch doesn't overwrite values in SHELL and HOME because maybe the user
put some values here which can be useful for him.
---
 st.c |   37 ++++++++++++++++++++++++++++++++-----
 1 file changed, 32 insertions(+), 5 deletions(-)
diff --git a/st.c b/st.c
index 197dd79..46db942 100644
--- a/st.c
+++ b/st.c
_AT_@ -5,6 +5,7 @@
 #include <fcntl.h>
 #include <limits.h>
 #include <locale.h>
+#include <pwd.h>
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
_AT_@ -261,6 +262,7 @@ static void die(const char *, ...);
 static void draw(void);
 static void redraw(void);
 static void drawregion(int, int, int, int);
+static void initenv(void);
 static void execsh(void);
 static void sigchld(int);
 static void run(void);
_AT_@ -881,10 +883,6 @@ execsh(void) {
 	char **args;
 	char *envshell = getenv("SHELL");
 
-	unsetenv("COLUMNS");
-	unsetenv("LINES");
-	unsetenv("TERMCAP");
-
 	signal(SIGCHLD, SIG_DFL);
 	signal(SIGHUP, SIG_DFL);
 	signal(SIGINT, SIG_DFL);
_AT_@ -893,7 +891,6 @@ execsh(void) {
 	signal(SIGALRM, SIG_DFL);
 
 	DEFAULT(envshell, SHELL);
-	putenv("TERM="TNAME);
 	args = opt_cmd ? opt_cmd : (char*[]){envshell, "-i", NULL};
 	execvp(args[0], args);
 	exit(EXIT_FAILURE);
_AT_@ -914,6 +911,35 @@ sigchld(int a) {
 }
 
 void
+initenv(void) {
+	const struct passwd *pass = getpwuid(getuid());
+	char buff[sizeof(long) * 8];
+
+	if(!pass)
+		die("Process is running with an incorrect uid\n");
+
+	unsetenv("COLUMNS");
+	unsetenv("LINES");
+	unsetenv("TERMCAP");
+	/*
+	 * We assume the first input in passwd with this uid
+	 * is the correct. If you have more of one user with the same
+	 * uid you suck a lot.
+	 */
+	setenv("LOGNAME", pass->pw_name, 1);
+	setenv("USER", pass->pw_name, 1);
+	setenv("SHELL", pass->pw_shell, 0);
+	setenv("HOME", pass->pw_dir, 0);
+	putenv("TERM=" TNAME);
+	/*
+	 * we can be sure that DISPLAY is correct because we don't have
+	 * a -diplay option
+	 */
+	sprintf(buff, "%lu", xw.win);
+	setenv("WINDOWID", buff, 1);
+}
+
+void
 ttynew(void) {
 	int m, s;
 	struct winsize w = {term.row, term.col, 0, 0};
_AT_@ -935,6 +961,7 @@ ttynew(void) {
 			die("ioctl TIOCSCTTY failed: %s\n", SERRNO);
 		close(s);
 		close(m);
+		initenv();
 		execsh();
 		break;
 	default:
-- 
1.7.10.4
--Fba/0zbH8Xs+Fj9o
Content-Type: text/x-diff; charset=utf-8
Content-Disposition: attachment; filename="0002-Add-utmp-stuff.patch"
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Tue Oct 09 2012 - 19:00:07 CEST