Subject: Re: misc/9954: rc.d system is missing one of my wish-list items
To: Perry E. Metzger <perry@piermont.com>
From: Dave Sainty <dave@dtsp.co.nz>
List: netbsd-bugs
Date: 04/23/2000 14:46:52
"Perry E. Metzger" writes:

> dave@dtsp.co.nz writes:
> > 	The changes are fairly mechanical (depending on what should and
> > 	shouldn't be implemented), but I'll happily provide a patch on
> > 	request if changes are agreed on.
> 
> I don't know that your ideas would be accepted even with patches, but
> providing patches makes it ever so much easier to adopt an idea....

Yes, I partly avoided sending patches because I don't use rc.d yet, so
testing is difficult.  Laziness played a part :)

However, here's a patch that implements the idea.  It's _completely_
untested, but looks ok :)

I also added a *_nice, because it would be useful.

If *_nice or *_user is set, a fresh shell is spawned and $command
loses access to shell local variables.  This is probably a good side
effect :)

It occurred to me a bit later that 'sysctl proc.curproc.corename'
would be a better way to choose the core directory than chdir, but
that really does need to be tested, and *_chdir seems a useful knob
anyway.

A 'chroot' knob would be useful too, but I can see problems with that
if there is no shell in the chrooted area, because at least 'xfs'
assumes that "$command $_flags $command_args" is parsed by the shell.
Maybe that assumption should be deemed illegal?  ('xfs' needs a
_postcmd feature).

Cheers,

Dave

--- /etc/rc.subr	Sun Apr 23 13:38:21 2000
+++ rc.subr	Sun Apr 23 14:12:02 2000
@@ -180,9 +180,13 @@
 #				the action should be run.
 #				If this variable isn't set, ${name} is checked 
 #				instead.
+#	${name}_chdir	n	Directory to change to before running
+#				${command}.
 #	${name}_flags	n	Arguments to call ${command} with.
 #				NOTE:	if $flags is set (e.g, from the parent
 #					environment), it overrides this.
+#	${name}_nice	n	Nice level to run ${command} at.
+#	${name}_user	n	User to run ${command} as.
 #	${_arg}_cmd	n	If set, use this as the action when invoked;
 #				$_arg is available to the action to use.
 #				Otherwise, use default command (see below)
@@ -272,6 +276,9 @@
 	else
 		eval _flags=\$${name}_flags
 	fi
+	eval _user=\$${name}_user
+	eval _chdir=\$${name}_chdir
+	eval _nice=\$${name}_nice
 
 	eval $_pidcmd
 
@@ -332,7 +339,15 @@
 
 			eval $_precmd || return 1
 			echo "Starting ${name}."
-			eval $command $_flags $command_args
+			if [ -z "$_user" ]; then
+				eval ${_chdir+cd $_chdir;} \
+				    ${_nice+nice -n $_nice sh -c} \
+				    $command $_flags $command_args
+			else
+				echo ${_chdir+cd $_chdir;} \
+				    $command $_flags $command_args | \
+				    eval ${_nice+nice -n $_nice} su -m $_user
+			fi
 			;;
 
 		stop)
@@ -351,7 +366,12 @@
 
 			eval $_precmd || return 1
 			echo "Stopping ${name}."
-			kill -${sig_stop:-TERM} $_pid
+			if [ -z "$_user" ]; then
+				kill -${sig_stop:-TERM} $_pid
+			else
+				echo kill -${sig_stop:-TERM} $_pid | \
+				    su -m $_user
+			fi
 			;;
 
 		reload)
@@ -369,7 +389,12 @@
 			fi
 			echo "Reloading ${name} config files."
 			eval $_precmd || return 1
-			kill -${sig_reload:-HUP} $_pid
+			if [ -z "$_user" ]; then
+				kill -${sig_reload:-HUP} $_pid
+			else
+				echo kill -${sig_reload:-HUP} $_pid | \
+				    su -m $_user
+			fi
 			;;
 
 		restart)