Bug 146916 - TERM not set correctly in single-user mode
Summary: TERM not set correctly in single-user mode
Status: Open
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 8.0-RELEASE
Hardware: Any Any
: Normal Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-24 12:50 UTC by guyyur
Modified: 2017-12-31 22:32 UTC (History)
0 users

See Also:


Attachments
file.diff (1.90 KB, patch)
2010-05-24 12:50 UTC, guyyur
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description guyyur 2010-05-24 12:50:01 UTC
1. sh doesn't call setenv() for "TERM" so getenv("TERM") will return NULL
   in single-user mode when used as the first shell.
   libedit el_init() passes "dumb" to ncurses if getenv("TERM") returns NULL.
   "dumb" isn't in /usr/src/etc/termcap.small

2. sh doesn't call el_set(el, EL_TERMINAL, term) when TERM changes.
   so libedit will keep thinking it has a "dumb" terminal.

3. rev 206182 changed 'bin/sh/options.c' to set -o emacs if interactive shell.
   this is done before /.profile can set TERM= in single-user mode
   so el_init() is called without TERM env var

Fix: looking in NetBSD, /bin/sh calls setenv/unsetenv for "TERM" before el_init()
and calls el_set(el, EL_TERMINAL, term) when TERM changes.

I have made patches using the changes against bin/sh in NetBSD.
Only the TERM support changes are included in the patches.
The patches are against head, svn revision 208452

cd /usr/src/bin/sh
patch < /patch/to/sh.patch

possible fixes to revision 206182 'bin/sh/options.c'
1. add the "dumb" entry to etc/termcap.small to silence el_init()

2. instead of enabling -o emacs in 'bin/sh/options.c',
   create /usr/src/etc/root/dot.shrc with
     set -o emacs
   add to /usr/src/etc/root/dot.profile
     ENV=$HOME/.shrc
     export ENV
   this way -o emacs will be called after /.profile has set a valid TERM

Patch attached with submission follows:
How-To-Repeat: create /etc/termcap.small.db taken from /usr/src/etc/termcap.small
it should have cons25 (or xterm on CURRENT)
it should not have a "dumb" entry

in /.profile have
  export TERM=${TERM:-cons25}    # xterm on CURRENT
or any terminal found in /etc/termcap.small.db

boot into single-user mode and use the default /bin/sh
/usr shouldn't be not mounted
or have /usr/share/misc/termcap.db not accessible.

run `set -o emacs`

see
  Cannot read termcap database;
  using dumb terminal settings.
even though /etc/termcap.small.db is readable and cons25/xterm entry exists
Comment 1 guyyur 2010-05-24 13:30:54 UTC
the spacing for the Fix field was lost, reformatted below

Fix:
looking in NetBSD, /bin/sh calls setenv/unsetenv for "TERM" before el_init()
and calls el_set(el, EL_TERMINAL, term) when TERM changes.

I have made patches using the changes against bin/sh in NetBSD.
Only the TERM support changes are included in the patches.
The patches are against head, svn revision 208452

cd /usr/src/bin/sh
patch < /path/to/patch.txt

possible fixes to revision 206182 'bin/sh/options.c'
1.
add the "dumb" entry to etc/termcap.small to silence el_init()

2.
instead of enabling -o emacs in 'bin/sh/options.c', create
/usr/src/etc/root/dot.shrc with
  set -o emacs

add to /usr/src/etc/root/dot.profile
  ENV=$HOME/.shrc
  export ENV
this way -o emacs will be called after /.profile has set a valid TERM
already done this way in /usr/src/share/skel
Comment 2 Bruce Cran freebsd_committer 2010-05-28 11:31:53 UTC
Responsible Changed
From-To: freebsd-bugs->ed

Ed, it looks like this is related to the terminal changes -  
could you take a look?
Comment 3 dfilter service freebsd_committer 2010-06-02 20:17:12 UTC
Author: jilles
Date: Wed Jun  2 19:16:58 2010
New Revision: 208755
URL: http://svn.freebsd.org/changeset/base/208755

Log:
  sh: Pass TERM changes to libedit.
  
  I have changed the patch slightly to ignore TERM changes in subshells.
  
  PR:		bin/146916
  Submitted by:	Guy Yur
  Obtained from:	NetBSD

Modified:
  head/bin/sh/histedit.c
  head/bin/sh/myhistedit.h
  head/bin/sh/var.c
  head/bin/sh/var.h

Modified: head/bin/sh/histedit.c
==============================================================================
--- head/bin/sh/histedit.c	Wed Jun  2 18:46:12 2010	(r208754)
+++ head/bin/sh/histedit.c	Wed Jun  2 19:16:58 2010	(r208755)
@@ -98,6 +98,8 @@ histedit(void)
 			/*
 			 * turn editing on
 			 */
+			char *term;
+
 			INTOFF;
 			if (el_in == NULL)
 				el_in = fdopen(0, "r");
@@ -107,6 +109,11 @@ histedit(void)
 				el_out = fdopen(2, "w");
 			if (el_in == NULL || el_err == NULL || el_out == NULL)
 				goto bad;
+			term = lookupvar("TERM");
+			if (term)
+				setenv("TERM", term, 1);
+			else
+				unsetenv("TERM");
 			el = el_init(arg0, el_in, el_out, el_err);
 			if (el != NULL) {
 				if (hist)
@@ -160,6 +167,13 @@ sethistsize(hs)
 	}
 }
 
+void
+setterm(const char *term)
+{
+	if (rootshell && el != NULL && term != NULL)
+		el_set(el, EL_TERMINAL, term);
+}
+
 int
 histcmd(int argc, char **argv)
 {

Modified: head/bin/sh/myhistedit.h
==============================================================================
--- head/bin/sh/myhistedit.h	Wed Jun  2 18:46:12 2010	(r208754)
+++ head/bin/sh/myhistedit.h	Wed Jun  2 19:16:58 2010	(r208755)
@@ -38,6 +38,7 @@ extern int displayhist;
 
 void histedit(void);
 void sethistsize(const char *);
+void setterm(const char *);
 int histcmd(int, char **);
 int not_fcnumber(const char *);
 int str_to_event(const char *, int);

Modified: head/bin/sh/var.c
==============================================================================
--- head/bin/sh/var.c	Wed Jun  2 18:46:12 2010	(r208754)
+++ head/bin/sh/var.c	Wed Jun  2 19:16:58 2010	(r208755)
@@ -80,6 +80,7 @@ struct varinit {
 
 #ifndef NO_HISTORY
 struct var vhistsize;
+struct var vterm;
 #endif
 struct var vifs;
 struct var vmail;
@@ -114,6 +115,10 @@ STATIC const struct varinit varinit[] = 
 	  NULL },
 	{ &vps4,	0,				"PS4=+ ",
 	  NULL },
+#ifndef NO_HISTORY
+	{ &vterm,	VUNSET,				"TERM=",
+	  setterm },
+#endif
 	{ &voptind,	0,				"OPTIND=1",
 	  getoptsreset },
 	{ NULL,	0,				NULL,

Modified: head/bin/sh/var.h
==============================================================================
--- head/bin/sh/var.h	Wed Jun  2 18:46:12 2010	(r208754)
+++ head/bin/sh/var.h	Wed Jun  2 19:16:58 2010	(r208755)
@@ -77,6 +77,7 @@ extern struct var vps2;
 extern struct var vps4;
 #ifndef NO_HISTORY
 extern struct var vhistsize;
+extern struct var vterm;
 #endif
 
 /*
@@ -96,6 +97,7 @@ extern struct var vhistsize;
 #define optindval()	(voptind.text + 7)
 #ifndef NO_HISTORY
 #define histsizeval()	(vhistsize.text + 9)
+#define termval()	(vterm.text + 5)
 #endif
 
 #define mpathset()	((vmpath.flags & VUNSET) == 0)
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 4 Ed Schouten freebsd_committer 2011-02-19 11:26:12 UTC
Responsible Changed
From-To: ed->jilles

This may already be fixed. Over to jilles@.
Comment 5 Jilles Tjoelker freebsd_committer 2011-06-17 22:32:13 UTC
Responsible Changed
From-To: jilles->freebsd-bugs

I have fixed the part that needs to be fixed in sh. 
The rest needs to be fixed elsewhere.
Comment 6 Eitan Adler freebsd_committer freebsd_triage 2017-12-31 08:01:06 UTC
For bugs matching the following criteria:

Status: In Progress Changed: (is less than) 2014-06-01

Reset to default assignee and clear in-progress tags.

Mail being skipped