From owner-svn-src-head@freebsd.org Tue Aug 14 11:01:54 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 327CD10746AA; Tue, 14 Aug 2018 11:01:54 +0000 (UTC) (envelope-from trasz@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id DE09989F19; Tue, 14 Aug 2018 11:01:53 +0000 (UTC) (envelope-from trasz@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id C02561AB76; Tue, 14 Aug 2018 11:01:53 +0000 (UTC) (envelope-from trasz@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w7EB1rli014418; Tue, 14 Aug 2018 11:01:53 GMT (envelope-from trasz@FreeBSD.org) Received: (from trasz@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w7EB1rnj014416; Tue, 14 Aug 2018 11:01:53 GMT (envelope-from trasz@FreeBSD.org) Message-Id: <201808141101.w7EB1rnj014416@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: trasz set sender to trasz@FreeBSD.org using -f From: Edward Tomasz Napierala Date: Tue, 14 Aug 2018 11:01:53 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r337740 - in head: sbin/init stand/man X-SVN-Group: head X-SVN-Commit-Author: trasz X-SVN-Commit-Paths: in head: sbin/init stand/man X-SVN-Commit-Revision: 337740 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Aug 2018 11:01:54 -0000 Author: trasz Date: Tue Aug 14 11:01:52 2018 New Revision: 337740 URL: https://svnweb.freebsd.org/changeset/base/337740 Log: Add init_exec kenv(1) variable, to make init(8) execute a file after opening the console, replacing init as PID 1. From the user point of view, it makes it possible to run eg the shell as PID 1, using 'set init_exec=/bin/sh' at the loader(8) prompt. Reviewed by: kib MFC after: 2 weeks Relnotes: yes Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D16625 Modified: head/sbin/init/init.8 head/sbin/init/init.c head/stand/man/loader.8 Modified: head/sbin/init/init.8 ============================================================================== --- head/sbin/init/init.8 Tue Aug 14 11:00:54 2018 (r337739) +++ head/sbin/init/init.8 Tue Aug 14 11:01:52 2018 (r337740) @@ -31,7 +31,7 @@ .\" @(#)init.8 8.3 (Berkeley) 4/18/94 .\" $FreeBSD$ .\" -.Dd August 7, 2018 +.Dd August 14, 2018 .Dt INIT 8 .Os .Sh NAME @@ -316,6 +316,14 @@ See .Xr reboot 8 .Fl r for details. +.It Va init_exec +If set to a valid file name in the root file system, +instructs +.Nm +to directly execute that file as the very first action, +replacing +.Nm +as PID 1. .It Va init_script If set to a valid file name in the root file system, instructs @@ -341,6 +349,8 @@ Defines the shell binary to be used for executing the The default is .Dq Li /bin/sh . It is used for running the +.Va init_exec +or .Va init_script if set, as well as for the .Pa /etc/rc Modified: head/sbin/init/init.c ============================================================================== --- head/sbin/init/init.c Tue Aug 14 11:00:54 2018 (r337739) +++ head/sbin/init/init.c Tue Aug 14 11:01:52 2018 (r337740) @@ -149,6 +149,7 @@ static state_t current_state = death_single; static void execute_script(char *argv[]); static void open_console(void); static const char *get_shell(void); +static void replace_init(char *path); static void write_stderr(const char *message); typedef struct init_session { @@ -330,6 +331,11 @@ invalid: close(1); close(2); + if (kenv(KENV_GET, "init_exec", kenv_value, sizeof(kenv_value)) > 0) { + replace_init(kenv_value); + _exit(0); /* reboot */ + } + if (kenv(KENV_GET, "init_script", kenv_value, sizeof(kenv_value)) > 0) { state_func_t next_transition; @@ -1085,6 +1091,22 @@ execute_script(char *argv[]) shell = get_shell(); execv(shell, argv); stall("can't exec %s for %s: %m", shell, script); +} + +/* + * Execute binary, replacing init(8) as PID 1. + */ +static void +replace_init(char *path) +{ + char *argv[3]; + char sh[] = "sh"; + + argv[0] = sh; + argv[1] = path; + argv[2] = NULL; + + execute_script(argv); } /* Modified: head/stand/man/loader.8 ============================================================================== --- head/stand/man/loader.8 Tue Aug 14 11:00:54 2018 (r337739) +++ head/stand/man/loader.8 Tue Aug 14 11:01:52 2018 (r337740) @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd August 13, 2018 +.Dd August 14, 2018 .Dt LOADER 8 .Os .Sh NAME @@ -490,6 +490,9 @@ directive from has been processed, allowing kernel panics that happen during the early stages of boot to be captured. .It Va init_chroot +See +.Xr init 8 . +.It Va init_exec See .Xr init 8 . .It Va init_path