[Midnightbsd-cvs] src [6471] trunk/sbin/init: allow logging to init.log when terminal is not availabe.

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sat Dec 7 19:20:32 EST 2013


Revision: 6471
          http://svnweb.midnightbsd.org/src/?rev=6471
Author:   laffer1
Date:     2013-12-07 19:20:32 -0500 (Sat, 07 Dec 2013)
Log Message:
-----------
allow logging to init.log when terminal is not availabe.

Modified Paths:
--------------
    trunk/sbin/init/init.8
    trunk/sbin/init/init.c
    trunk/sbin/init/pathnames.h

Modified: trunk/sbin/init/init.8
===================================================================
--- trunk/sbin/init/init.8	2013-12-07 23:57:11 UTC (rev 6470)
+++ trunk/sbin/init/init.8	2013-12-08 00:20:32 UTC (rev 6471)
@@ -31,7 +31,7 @@
 .\"     @(#)init.8	8.3 (Berkeley) 4/18/94
 .\" $MidnightBSD$
 .\"
-.Dd January 23, 2011
+.Dd March 14, 2012
 .Dt INIT 8
 .Os
 .Sh NAME
@@ -300,16 +300,12 @@
 file
 .El
 .Sh FILES
-.Bl -tag -width /etc/rc.shutdown -compact
+.Bl -tag -width /var/log/init.log -compact
 .It Pa /dev/console
 system console device
 .It Pa /dev/tty*
 terminal ports found in
 .Xr ttys 5
-.It Pa /var/run/utx.active
-record of current users on the system
-.It Pa /var/log/utx.log
-record of all logins and logouts
 .It Pa /etc/ttys
 the terminal initialization information file
 .It Pa /etc/rc
@@ -316,6 +312,10 @@
 system startup commands
 .It Pa /etc/rc.shutdown
 system shutdown commands
+.It Pa /var/log/init.log
+log of
+.Xr rc 8
+output if the system console device is not available
 .El
 .Sh DIAGNOSTICS
 .Bl -diag

Modified: trunk/sbin/init/init.c
===================================================================
--- trunk/sbin/init/init.c	2013-12-07 23:57:11 UTC (rev 6470)
+++ trunk/sbin/init/init.c	2013-12-08 00:20:32 UTC (rev 6471)
@@ -32,7 +32,7 @@
  * @(#) Copyright (c) 1991, 1993 The Regents of the University of California.  All rights reserved.
  * @(#)init.c      8.1 (Berkeley) 7/15/93
  * $FreeBSD: src/sbin/init/init.c,v 1.60.2.2 2006/07/08 15:34:27 kib Exp $
- * $MidnightBSD: src/sbin/init/init.c,v 1.5 2007/04/07 02:17:03 laffer1 Exp $
+ * $MidnightBSD$
  */
 
 #include <sys/param.h>
@@ -130,7 +130,7 @@
 static state_t requested_transition;
 static state_t current_state = death_single;
 
-static void setctty(const char *);
+static void open_console(void);
 static const char *get_shell(void);
 static void write_stderr(const char *message);
 
@@ -576,19 +576,39 @@
  * Only called by children of init after forking.
  */
 static void
-setctty(const char *name)
+open_console(void)
 {
 	int fd;
 
-	revoke(name);
-	if ((fd = open(name, O_RDWR)) == -1) {
-		stall("can't open %s: %m", name);
-		_exit(1);
+	/*
+	 * Try to open /dev/console.  Open the device with O_NONBLOCK to
+	 * prevent potential blocking on a carrier.
+	 */
+	revoke(_PATH_CONSOLE);
+	if ((fd = open(_PATH_CONSOLE, O_RDWR | O_NONBLOCK)) != -1) {
+		(void)fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
+		if (login_tty(fd) == 0)
+			return;
+		close(fd);
 	}
-	if (login_tty(fd) == -1) {
-		stall("can't get %s for controlling terminal: %m", name);
+
+	/* No luck.  Log output to file if possible. */
+	if ((fd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
+		stall("cannot open null device.");
 		_exit(1);
 	}
+	if (fd != STDIN_FILENO) {
+		dup2(fd, STDIN_FILENO);
+		close(fd);
+	}
+	fd = open(_PATH_INITLOG, O_WRONLY | O_APPEND | O_CREAT, 0644);
+	if (fd == -1)
+		dup2(STDIN_FILENO, STDOUT_FILENO);
+	else if (fd != STDOUT_FILENO) {
+		dup2(fd, STDOUT_FILENO);
+		close(fd);
+	}
+	dup2(STDOUT_FILENO, STDERR_FILENO);
 }
 
 static const char *
@@ -646,7 +666,7 @@
 		/*
 		 * Start the single user session.
 		 */
-		setctty(_PATH_CONSOLE);
+		open_console();
 
 #ifdef SECURE
 		/*
@@ -810,7 +830,7 @@
 		sigaction(SIGTSTP, &sa, (struct sigaction *)0);
 		sigaction(SIGHUP, &sa, (struct sigaction *)0);
 
-		setctty(_PATH_CONSOLE);
+		open_console();
 
 		char _sh[]		= "sh";
 		char _autoboot[]	= "autoboot";
@@ -1593,7 +1613,7 @@
 		sigaction(SIGTSTP, &sa, (struct sigaction *)0);
 		sigaction(SIGHUP, &sa, (struct sigaction *)0);
 
-		setctty(_PATH_CONSOLE);
+		open_console();
 
 		char _sh[]	= "sh";
 		char _reboot[]	= "reboot";

Modified: trunk/sbin/init/pathnames.h
===================================================================
--- trunk/sbin/init/pathnames.h	2013-12-07 23:57:11 UTC (rev 6470)
+++ trunk/sbin/init/pathnames.h	2013-12-08 00:20:32 UTC (rev 6471)
@@ -31,10 +31,11 @@
  *
  *	@(#)pathnames.h	8.1 (Berkeley) 6/5/93
  * $FreeBSD: src/sbin/init/pathnames.h,v 1.3 2004/04/09 19:58:30 markm Exp $
- * $MidnightBSD: src/sbin/init/pathnames.h,v 1.2 2006/12/29 21:00:15 laffer1 Exp $
+ * $MidnightBSD$
  */
 
 #include <paths.h>
 
+#define	_PATH_INITLOG	"/var/log/init.log"
 #define	_PATH_RUNCOM	"/etc/rc"
 #define _PATH_RUNDOWN   "/etc/rc.shutdown"



More information about the Midnightbsd-cvs mailing list