Subject: Re: rc.d boot log [was: Re: Cosmetic changes to rc.d scripts]
To: mouss <usebsd@free.fr>
From: Mike M. Volokhov <mishka@apk.od.ua>
List: tech-userlevel
Date: 10/15/2004 12:34:47
On Thu, 14 Oct 2004 15:20:42 +0200
mouss <usebsd@free.fr> wrote:

> Mike M. Volokhov wrote:
> 
> > 
> > I've thinking about this way. But it takes more effort to handle this
> > (checking if /var/log was mounted or not, switching between two logging
> > scenaries, etc.) when there are no big advantages. Of course, using tee
> > it would be possible handle logs with arbitrary size, but what's more?
> 
> - tee way
> There is an additionnal problem with tee. It is in /usr/bin, so
> one would need to check for /usr being mounted and for tee to exist.
[snip]
> Of course, output produced before /usr and /var is mounted won't be 
> logged. we can replace /var with / by first writing to /etc/rclog.tmp 
> and moving this at the end of rc. This way, as soon as / is writable and 
> /usr is mouned (tee is found), rc output will be logged.

This is a main reason why I use variables to store log. They can easy
hold all data when /var/log is unmounted.

> 
> - var way
> one issue I see is that rclogvar may be (accidentally) used in rc 
> scripts. so you should call it _rclogbuf instead.

OK.

> Also, as rclog() is not supposed to used by other scripts, it is better 
> to put the definition in /etc/rc directly.

Strictly said, initially I put all stuff in /etc/rc, but later rclog()
has been moved to /etc/rc.subr so as I have planned to log
/etc/rc.shutdown as well (now this is done, please see patch below). In
addition, rc.subr holds just all functions.

> Note that people who modified rc.local to run pkg scripts do not need to 
> call rclog() as it rc.local is called by rc.

Yes, it is (/etc/rc.d/local).

> - just too boot logs?
> I think we only need two boot logs: the current and previous, so
> at end, do
> 	mv /var/log/rc.log /var/log/rc.prev
> 	echo $var > /var/log/rc.log

I've done this, thanks for discussing this aspect of a problem.

However, I meant a single log, i.e. when at a boot time the output will
grew too much (for example up to few megabytes). In this case variables
will store huge sets of data. Is it ok?

> - if /var/log can't be found, write to /etc so that one can find out 
> what the problem was.
> if [ -d /var/log ]; then
> 	echo "${rclogvar}" >> /var/log/boot.log
> else
> 	echo "${rclogvar}" > /etc/rclog.tmp
> 	warn "boot log has been saved in /etc/rclog.tmp"
> fi

OK.

Please take a look to new patchset below (diffed against /etc/rc,
/etc/rc.shutdown, and /etc/rc.subr). Please note that I did
rc_log_output (if rc_log_output=YES, it will create /var/log/rc.log, and
will not otherwise) checks in rclog() rather to in /etc/rc and
/etc/rc.shutdown so as it used several times and just more covenient
without any impacts.

--
Mishka.


--- rc.orig	2004-10-15 12:15:10.000000000 +0300
+++ rc	2004-10-15 12:14:38.000000000 +0300
@@ -35,13 +35,26 @@
 trap : INT
 trap "echo 'Boot interrupted.'; exit 1" QUIT
 
-date
+rclog "echo Starting up at `date`"
 
 files=$(rcorder -s nostart ${rc_rcorder_flags} /etc/rc.d/*)
 
 for _rc_elem in $files; do
-	run_rc_script $_rc_elem start
+	rclog "run_rc_script $_rc_elem start"
 done
 
-date
+rclog "echo System was started at `date`"
+
+if checkyesno rc_log_output && [ -n "$_rclogbuf" ]; then
+	if [ -d /var/log ]; then
+		if [ -f /var/log/rc.log ]; then
+			mv /var/log/rc.log /var/log/rc.prev
+		fi
+		echo "$_rclogbuf" > /var/log/rc.log
+	else
+		echo "$_rclogbuf" > /etc/rclog.tmp
+		warn "boot log has been saved to /etc/rclog.tmp"
+	fi
+fi
+
 exit 0
--- rc.shutdown.orig	2004-10-15 12:15:28.000000000 +0300
+++ rc.shutdown	2004-10-15 12:23:19.000000000 +0300
@@ -43,6 +43,7 @@
 	_rcshutdown_watchdog=$!
 fi
 
+rclog "echo System going down at `date`"
 
 #	Determine the shutdown order of the /etc/rc.d scripts,
 #	and perform the operation
@@ -50,7 +51,7 @@
 files=$(rcorder -k shutdown ${rcshutdown_rcorder_flags} /etc/rc.d/*)
 
 for _rc_elem in $(reverse_list $files); do
-	run_rc_script $_rc_elem stop
+	rclog "run_rc_script $_rc_elem stop"
 done
 
 
@@ -60,5 +61,15 @@
 	kill -TERM $_rcshutdown_watchdog >/dev/null 2>&1
 fi
 
-date
+rclog "echo Running shutdown scripts finished at `date`"
+
+if checkyesno rc_log_output && [ -n "$_rclogbuf" ]; then
+	if [ -d /var/log ]; then
+		echo "$_rclogbuf" >> /var/log/rc.log
+	else
+		echo "$_rclogbuf" >> /etc/rclog.tmp
+		warn "shutdown log has been saved to /etc/rclog.tmp"
+	fi
+fi
+
 exit 0
--- rc.subr.orig	2004-10-15 12:15:21.000000000 +0300
+++ rc.subr	2004-10-15 12:14:38.000000000 +0300
@@ -819,6 +819,25 @@
 }
 
 #
+# rclog command
+#	Run "command" and append all output to $_rclogbuf variable.
+#
+rclog()
+{
+	_rclogcmd=$1
+	if checkyesno rc_log_output; then
+		_rclogcmdbuf=$(${_rclogcmd} 2>&1)
+		if [ -n "$_rclogcmdbuf" ]; then
+			echo "$_rclogcmdbuf"
+			_rclogbuf="$_rclogbuf
+$_rclogcmdbuf"
+		fi
+	else
+		$_rclogcmd
+	fi
+}
+
+#
 # backup_file action file cur backup
 #	Make a backup copy of `file' into `cur', and save the previous
 #	version of `cur' as `backup' or use rcs for archiving.