On 2013-12-28 Sat 21:16 PM |, Craig R. Skinner wrote:
> On 2013-12-19 Thu 13:43 PM |, Craig R. Skinner wrote:
> > Enhance rc.d/rc.subr with lowered/raised daemon running priority.
> > 
> 
> Take 2:
> 
> Replace /etc/rc.d/<daemon> rc_renice=X with
> /etc/rc.conf.local <daemon>_nice=X
> 

Take 3 - simplify:

Use nice directly between ${rcexec} & ${daemon} with rc_start(),
rather than renice post start.
Change rc_reprioritise() to rc_validate_rcnice()

Backgrounding still works as expected.

This now works with privilege separated binaries, such as ntpd:

$ fgrep ntp /etc/rc.conf.local
ntpd_flags="-s"
ntpd_nice=YES

$ ps -l -U _ntp
  UID   PID  PPID CPU PRI  NI   VSZ   RSS WCHAN   STAT  TT       TIME COMMAND
   83  4226     1   0   2  10   708  1136 poll    SNs   ??    0:00.09 ntpd: ntp 
engine (ntpd)
   83 22421  4226   3   2  10   644  1020 poll    INs   ??    0:00.01 ntpd: dns 
engine (ntpd)



Index: rc.subr
===================================================================
RCS file: /cvs/src/etc/rc.d/rc.subr,v
retrieving revision 1.70
diff -u -u -p -r1.70 rc.subr
--- rc.subr     11 Jul 2013 09:34:33 -0000      1.70
+++ rc.subr     28 Dec 2013 23:10:14 -0000
@@ -1,4 +1,4 @@
-#      $OpenBSD: rc.subr,v 1.70 2013/07/11 09:34:33 otto Exp $
+#      $OpenBSD: rc.subr,v 1.15 2013/12/28 22:57:21 skinner Exp $
 #
 # Copyright (c) 2010, 2011 Antoine Jacoutot <ajacou...@openbsd.org>
 # Copyright (c) 2010, 2011 Ingo Schwarze <schwa...@openbsd.org>
@@ -54,7 +54,8 @@ rc_rm_runfile() {
 }
 
 rc_start() {
-       ${rcexec} "${daemon} ${daemon_flags} ${_bg}"
+       [[ -n ${_rcnice} ]] && _nice="$(which nice) -n ${_rcnice}"
+       ${rcexec} "${_nice} ${daemon} ${daemon_flags} ${_bg}"
 }
 
 rc_check() {
@@ -104,6 +105,46 @@ rc_wait() {
        return 1
 }
 
+rc_validate_rcnice()
+{
+       [[ -x $(which nice) ]] ||
+       {
+               # /usr not mounted?
+               unset _rcnice
+               return
+       }
+
+       [[ ${_rcnice} == 'YES' ]] &&
+       {
+               # nice(1): an increment of 10 is assumed.
+               _rcnice=10
+               return
+       }
+
+       # if digits present
+       printf "%d" ${_rcnice} > /dev/null 2>&1 &&
+       {
+               # strip non-digits for comparison
+               _rcnice=$(printf "%d" ${_rcnice})
+               [[ ${_rcnice} -eq 0 ]] &&
+               {
+                       unset _rcnice
+                       return
+               }
+       }
+
+       # nice(1): The priority can be adjusted over a
+       # range of -20 (the highest) to 20 (the lowest).
+       for _nice_level in $(jot 40 20 -20)
+       do
+               [[ ${_rcnice} == ${_nice_level} ]] && return
+       done
+
+       # Shouldn't get this far:
+       print -u2 "$0: ignoring invalid ${_name}_nice level: ${_rcnice}"
+       unset _rcnice
+}
+
 rc_cmd() {
        local _bg _n
 
@@ -136,6 +177,9 @@ rc_cmd() {
                fi
                [ -z "${INRC}" ] && rc_do rc_check && exit 0
                echo $_n "${INRC:+ }${_name}"
+
+               [[ -n ${_rcnice} ]] && rc_validate_rcnice
+
                while true; do  # no real loop, only needed to break
                        if type rc_pre >/dev/null; then
                                rc_do rc_pre || break
@@ -203,6 +247,7 @@ _RC_RUNFILE=${_RC_RUNDIR}/${_name}
 
 eval _rcflags=\${${_name}_flags}
 eval _rcuser=\${${_name}_user}
+eval _rcnice=\${${_name}_nice}
 
 getcap -f /etc/login.conf ${_name} 1>/dev/null 2>&1 && \
        daemon_class=${_name}
@@ -213,6 +258,7 @@ getcap -f /etc/login.conf ${_name} 1>/de
 [ -n "${_RC_FORCE}" ] && [ X"${_rcflags}" = X"NO" ] && unset _rcflags
 [ -n "${_rcflags}" ] && daemon_flags=${_rcflags}
 [ -n "${_rcuser}"  ] && daemon_user=${_rcuser}
+[[ ${_rcnice} == 'NO' ]] && unset _rcnice
 
 # sanitize
 daemon_flags=$(printf ' %s' ${daemon_flags})


Cheers,
-- 
Craig Skinner | http://www.bbc.co.uk/programmes/b03mtrg9/clips

Reply via email to