On Mon, Feb 18, 2019 at 11:21:38AM +0900, YASUOKA Masahiko wrote:
> Hi,
> 
> On Sun, 17 Feb 2019 10:55:11 +0100
> Antoine Jacoutot <ajacou...@bsdfrog.org> wrote:
> > On Fri, Feb 15, 2019 at 02:50:22PM +0900, YASUOKA Masahiko wrote:
> >> On Fri, 15 Feb 2019 14:45:14 +0900 (JST)
> >> YASUOKA Masahiko <yasu...@openbsd.org> wrote:
> >> > The diff adds "getrun" command for rcctl(8) which shows the daemon
> >> > variables from the running daemon.
> >> > 
> >> > ok? comment?
> >> 
> >> Sorry, previous diff is broken.  It could not get any value other than
> >> daemon_pexp.  Let me update the diff.
> > 
> > That's a lot of chunk of code for an enhanced 'cat /var/run/rc.d/foo'.
> > What is your usage for this?
> 
> My actual usage is to know whether current "rtable" or "user" is
> changed from the values of the running daemon.  If either is changed,
> we need to restart the daemon apply that change.
> 
> For example,
> 
>   curr=$(rcctl get ntpd rtable)
>   running=$(rcctl getrun ntpd rtable)
>   if [[ $curr != $running ]]; then
>      /etc/rc.d/ntpd restart
>   fi

You better of using this I think:
running=$(sed -n 's/^daemon_rtable=*//p' /var/run/rc.d/ntpd)

What's under /var/run/rc.d/ is only a "hint".
If ntpd is manually killed (outside of the rc.d system), the information is not
removed from there.
That's why I'd like to avoid relying on it too much in rcctl.


> >> Index: usr.sbin/rcctl/rcctl.8
> >> ===================================================================
> >> RCS file: /cvs/src/usr.sbin/rcctl/rcctl.8,v
> >> retrieving revision 1.35
> >> diff -u -p -r1.35 rcctl.8
> >> --- usr.sbin/rcctl/rcctl.8 20 Sep 2018 12:24:14 -0000      1.35
> >> +++ usr.sbin/rcctl/rcctl.8 15 Feb 2019 05:48:16 -0000
> >> @@ -22,7 +22,7 @@
> >>  .Nd configure and control daemons and services
> >>  .Sh SYNOPSIS
> >>  .Nm rcctl
> >> -.Cm get Ns | Ns Cm getdef Ns | Ns Cm set
> >> +.Cm get Ns | Ns Cm getdef Ns | Ns Cm getrun Ns | Ns Cm set
> >>  .Ar service | daemon Op Ar variable Op Ar arguments
> >>  .Nm rcctl
> >>  .Op Fl df
> >> @@ -103,6 +103,10 @@ will display all services and daemons va
> >>  Like
> >>  .Cm get
> >>  but returns the default values.
> >> +.It Cm getrun Ar service | daemon Op Ar variable
> >> +Like
> >> +.Cm get
> >> +but returns the values of the running daemon.
> >>  .It Cm ls Ar lsarg
> >>  Display a list of services and daemons matching
> >>  .Ar lsarg ,
> >> @@ -180,6 +184,9 @@ exits with 0 if the daemon or service is
> >>  .Nm Cm getdef Ar daemon | service Op Cm status
> >>  exits with 0 if the daemon or service is enabled by default
> >>  and 1 if it is not.
> >> +.Nm Cm getrun Ar daemon | service
> >> +exits with 0 if the values of the running daemon exists
> >> +and 1 if it doesn't.
> >>  .Nm Cm ls failed
> >>  exits with 1 if an enabled daemon is not running.
> >>  Otherwise, the
> >> Index: usr.sbin/rcctl/rcctl.sh
> >> ===================================================================
> >> RCS file: /cvs/src/usr.sbin/rcctl/rcctl.sh,v
> >> retrieving revision 1.107
> >> diff -u -p -r1.107 rcctl.sh
> >> --- usr.sbin/rcctl/rcctl.sh        21 Oct 2018 21:20:40 -0000      1.107
> >> +++ usr.sbin/rcctl/rcctl.sh        15 Feb 2019 05:48:16 -0000
> >> @@ -21,6 +21,7 @@ _special_svcs="accounting check_quotas i
> >>                 spamd_black"
> >>  readonly _special_svcs
> >>  
> >> +_RC_RUNDIR=/var/run/rc.d
> >>  # get local functions from rc.subr(8)
> >>  FUNCS_ONLY=1
> >>  . /etc/rc.d/rc.subr
> >> @@ -32,7 +33,7 @@ usage()
> >>    for _i in ${_rc_actions}; do _a="$(echo -n ${_i}${_a:+|${_a}})"; done
> >>  
> >>    _rc_err \
> >> -  "usage: rcctl get|getdef|set service | daemon [variable [arguments]]
> >> +  "usage: rcctl get|getdef|getrun|set service | daemon [variable 
> >> [arguments]]
> >>    rcctl [-df] ${_a} daemon ...
> >>    rcctl disable|enable|order [daemon ...]
> >>    rcctl ls all|failed|off|on|started|stopped"
> >> @@ -374,6 +375,35 @@ svc_getdef()
> >>    fi
> >>  }
> >>  
> >> +svc_getrun()
> >> +{
> >> +  local _svc=$1
> >> +
> >> +  ( svc_is_special ${_svc} || svc_is_meta ${_svc} ) && return 1
> >> +
> >> +  local _val _var=$2
> >> +  local daemon_class daemon_flags daemon_rtable daemon_timeout daemon_user
> >> +  local daemon_pexp
> >> +
> >> +  [ ! -f $_RC_RUNDIR/$_svc ] && return 1
> >> +  _rc_parse_conf -readonly $_RC_RUNDIR/$_svc
> >> +
> >> +  [ -z "${daemon_pexp}" ] && eval daemon_pexp=\${pexp}
> >> +
> >> +  if [ -n "${_var}" ]; then
> >> +          eval _val=\${daemon_${_var}}
> >> +          [ -z "${_val}" ] || print -r -- "${_val}"
> >> +  else
> >> +          echo "${_svc}_class=${daemon_class}"
> >> +          echo "${_svc}_flags=${daemon_flags}"
> >> +          echo "${_svc}_rtable=${daemon_rtable}"
> >> +          echo "${_svc}_timeout=${daemon_timeout}"
> >> +          echo "${_svc}_user=${daemon_user}"
> >> +          echo "${_svc}_pexp=${daemon_pexp}"
> >> +  fi
> >> +  return 0
> >> +}
> >> +
> >>  svc_rm()
> >>  {
> >>    local _svc=$1
> >> @@ -509,7 +539,7 @@ case ${action} in
> >>                    done
> >>            fi
> >>            ;;
> >> -  get|getdef)
> >> +  get|getdef|getrun)
> >>            svc=$2
> >>            var=$3
> >>            [ -z "${svc}" ] && usage
> >> @@ -571,7 +601,7 @@ case ${action} in
> >>            done
> >>            exit ${ret}
> >>            ;;
> >> -  get|getdef)
> >> +  get|getdef|getrun)
> >>            if [ "${svc}" = "all" ]; then
> >>                    for svc in $(svc_ls all); do
> >>                            ( svc_${action} ${svc} "${var}" )
> >> Index: etc/rc.d/rc.subr
> >> ===================================================================
> >> RCS file: /cvs/src/etc/rc.d/rc.subr,v
> >> retrieving revision 1.130
> >> diff -u -p -r1.130 rc.subr
> >> --- etc/rc.d/rc.subr       20 Jan 2019 04:52:07 -0000      1.130
> >> +++ etc/rc.d/rc.subr       15 Feb 2019 05:48:16 -0000
> >> @@ -138,18 +138,24 @@ _rc_quirks() {
> >>  
> >>  _rc_parse_conf() {
> >>    typeset -l _key
> >> -  local _l _rcfile _val
> >> +  local _l _rcfile _val _readonly=
> >>    set -A _allowed_keys -- \
> >>            accounting amd_master check_quotas ipsec library_aslr \
> >>            multicast nfs_server pexp pf pkg_scripts shlib_dirs spamd_black
> >>  
> >> +  if [ "$1" = "-readonly" ]; then
> >> +          _readonly=y
> >> +          shift
> >> +  fi
> >> +
> >>    [ $# -gt 0 ] || set -- /etc/rc.conf /etc/rc.conf.local
> >>    for _rcfile; do
> >>            [[ -f $_rcfile ]] || continue
> >>            while IFS='     ' read -r _l; do
> >>                    [[ $_l == [!#=]*=* ]] || continue
> >>                    _key=${_l%%*([[:blank:]])=*}
> >> -                  [[ $_key == *_@(flags|rtable|user|timeout) ]] ||
> >> +                  [[ $_readonly != "" && $_key == *_class ]] ||
> >> +                          [[ $_key == *_@(flags|rtable|user|timeout) ]] ||
> >>                            [[ " ${_allowed_keys[*]} " == *" $_key "* ]] ||
> >>                            continue
> >>                    [[ $_key == "" ]] && continue
> >> 
> >> 
> > 
> > -- 
> > Antoine

-- 
Antoine

Reply via email to