Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 16 Sep 2025 20:41:39 GMT
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 779812d66989 - main - init: Use root's home directory in single-user mode
Message-ID:  <202509162041.58GKfdd1032627@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by jilles:

URL: https://cgit.FreeBSD.org/src/commit/?id=779812d66989a9c3aaed09e4573400fc137f92b0

commit 779812d66989a9c3aaed09e4573400fc137f92b0
Author:     Jilles Tjoelker <jilles@FreeBSD.org>
AuthorDate: 2025-09-14 21:53:34 +0000
Commit:     Jilles Tjoelker <jilles@FreeBSD.org>
CommitDate: 2025-09-16 20:36:41 +0000

    init: Use root's home directory in single-user mode
    
    When starting single-user mode, use the home directory from user root as
    current directory and for the HOME environment variable. If the
    directory does not exist, set HOME=/.
    
    Also adjust /root/.profile to stop setting HOME, since it should always
    have been set now.
    
    This is intended to keep shell startup files working in single-user mode
    after /.profile has been removed.
    
    Reviewed by:    emaste, ivy, kevans
    MFC after:      1 week
    Relnotes:       yes
    Differential Revision:  https://reviews.freebsd.org/D52527
---
 bin/sh/dot.profile |  2 --
 sbin/init/init.c   | 14 +++++++++++---
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/bin/sh/dot.profile b/bin/sh/dot.profile
index d27a2ae2fdbe..cba9bcf18ad9 100644
--- a/bin/sh/dot.profile
+++ b/bin/sh/dot.profile
@@ -1,6 +1,4 @@
 #
-HOME=/root
-export HOME
 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:~/bin
 export PATH
 TERM=${TERM:-xterm}
diff --git a/sbin/init/init.c b/sbin/init/init.c
index b345c8fa219a..d28501053c7f 100644
--- a/sbin/init/init.c
+++ b/sbin/init/init.c
@@ -851,9 +851,9 @@ single_user(void)
 	const char *shell;
 	char *argv[2];
 	struct timeval tv, tn;
+	struct passwd *pp;
 #ifdef SECURE
 	struct ttyent *typ;
-	struct passwd *pp;
 	static const char banner[] =
 		"Enter root password, or ^D to go multi-user\n";
 	char *clear, *password;
@@ -885,6 +885,7 @@ single_user(void)
 		 */
 		open_console();
 
+		pp = getpwnam("root");
 #ifdef SECURE
 		/*
 		 * Check the root password.
@@ -892,7 +893,6 @@ single_user(void)
 		 * it's the only tty that can be 'off' and 'secure'.
 		 */
 		typ = getttynam("console");
-		pp = getpwnam("root");
 		if (typ && (typ->ty_status & TTY_SECURE) == 0 &&
 		    pp && *pp->pw_passwd) {
 			write_stderr(banner);
@@ -909,7 +909,6 @@ single_user(void)
 			}
 		}
 		endttyent();
-		endpwent();
 #endif /* SECURE */
 
 #ifdef DEBUGSHELL
@@ -930,6 +929,15 @@ single_user(void)
 		}
 #endif /* DEBUGSHELL */
 
+		if (pp != NULL && pp->pw_dir != NULL && *pp->pw_dir != '\0' &&
+		    chdir(pp->pw_dir) == 0) {
+			setenv("HOME", pp->pw_dir, 1);
+		} else {
+			chdir("/");
+			setenv("HOME", "/", 1);
+		}
+		endpwent();
+
 		/*
 		 * Unblock signals.
 		 * We catch all the interesting ones,



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202509162041.58GKfdd1032627>