rc.subr (f06fda7ff14701905acb0089143ebf356c2a9b04) rc.subr (aa3b7a2fbc4687c0a09b6166aa2c2d117989d8fa)
1# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
2#
3# Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Luke Mewburn.
8#

--- 52 unchanged lines hidden (view full) ---

61# during system startup, all rc scripts will be invoked via /etc/rc, so
62# run_rc_script will overwrite rc_service with the file being sourced.
63rc_service="$0"
64
65#
66# functions
67# ---------
68
1# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
2#
3# Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Luke Mewburn.
8#

--- 52 unchanged lines hidden (view full) ---

61# during system startup, all rc scripts will be invoked via /etc/rc, so
62# run_rc_script will overwrite rc_service with the file being sourced.
63rc_service="$0"
64
65#
66# functions
67# ---------
68
69# is_verified file
70# if VERIEXEC is active check that $file is verified
71#
72VERIEXEC="/sbin/veriexec"
73if test -x $VERIEXEC && $VERIEXEC -i active > /dev/null 2>&1; then
74 is_verified() { $VERIEXEC -x $1; }
75else
76 is_verified() { return 0; }
77fi
78
79# indicate that we have vdot
80_VDOT_SH=:
81
82# current state of O_VERIFY
83o_verify()
84{
85 set -o | sed -n '/^verify/s,.*[[:space:]],,p'
86}
87
88##
89# o_verify_set want [save]
90#
91# record current state of verify in $save
92# and set it to $want if different
93#
94o_verify_set() {
95 local x=$(o_verify)
96
97 [ -z "$x" ] && return 0
98 [ -z "$2" ] || eval $2=$x
99 [ "$x" = "$1" ] && return 0
100 case "$1" in
101 on)
102 set -o verify
103 ;;
104 off)
105 set +o verify
106 ;;
107 esac
108}
109
110# for unverified files
111dotted=
112dot()
113{
114 local f verify
115
116 o_verify_set off verify
117 for f in "$@"; do
118 if [ -f $f -a -s $f ]; then
119 dotted="$dotted $f"
120 . $f
121 fi
122 done
123 o_verify_set $verify
124}
125
126# try for verified, fallback to safe
127sdot()
128{
129 local f
130
131 for f in "$@"; do
132 [ -f $f -a -s $f ] || continue
133 vdot $f || safe_dot $f
134 done
135}
136
137# convenience function - skip if not verified
138vdot()
139{
140 local f rc=0 verify
141
142 o_verify_set on verify
143 for f in "$@"; do
144 [ -f $f -a -s $f ] || continue
145 if is_verified $f 2> /dev/null; then
146 dotted="$dotted $f"
147 . $f
148 else
149 rc=80 # EAUTH
150 fi
151 done
152 o_verify_set $verify
153 return $rc
154}
155
156# do we have $1 (could be a function)
157have()
158{
159 type "$1" > /dev/null 2>&1
160}
161
162# provide consistent means of logging progress
163rc_log()
164{
165 date "+@ %s [%Y-%m-%d %H:%M:%S %Z] $*"
166}
167
168# only rc_log if tracing enabled
169# and $level >= $RC_LEVEL
170rc_trace()
171{
172 local level=$1; shift
173 local cf=/etc/rc.conf.d/rc_trace
174
175 if [ -z "$RC_LEVEL" ]; then
176 [ -f $cf ] || return
177 [ -s $cf ] && \
178 RC_LEVEL=$(sed -n '/^RC_LEVEL=/ { s/.*=//p;q; }' $cf)
179 RC_LEVEL=${RC_LEVEL:-0}
180 fi
181 [ ${RC_LEVEL:-0} -ge ${level:-0} ] || return
182 rc_log "$@"
183}
184
69# list_vars pattern
70# List variables matching glob pattern.
71#
72list_vars()
73{
74 # Localize 'set' option below.
75 local -
76 local IFS=$'\n' line varname

--- 842 unchanged lines hidden (view full) ---

919run_rc_command()
920{
921 _return=0
922 rc_arg=$1
923 if [ -z "$name" ]; then
924 err 3 'run_rc_command: $name is not set.'
925 fi
926
185# list_vars pattern
186# List variables matching glob pattern.
187#
188list_vars()
189{
190 # Localize 'set' option below.
191 local -
192 local IFS=$'\n' line varname

--- 842 unchanged lines hidden (view full) ---

1035run_rc_command()
1036{
1037 _return=0
1038 rc_arg=$1
1039 if [ -z "$name" ]; then
1040 err 3 'run_rc_command: $name is not set.'
1041 fi
1042
1043 DebugOn rc:$name rc:$name:$rc_arg $name:$rc_arg
1044
927 # Don't repeat the first argument when passing additional command-
928 # line arguments to the command subroutines.
929 #
930 shift 1
931 rc_extra_args="$*"
932
933 _rc_prefix=
934 case "$rc_arg" in

--- 137 unchanged lines hidden (view full) ---

1072 # if there's a custom ${XXX_cmd},
1073 # run that instead of the default
1074 #
1075 eval _cmd=\$${rc_arg}_cmd \
1076 _precmd=\$${rc_arg}_precmd \
1077 _postcmd=\$${rc_arg}_postcmd
1078
1079 if [ -n "$_cmd" ]; then
1045 # Don't repeat the first argument when passing additional command-
1046 # line arguments to the command subroutines.
1047 #
1048 shift 1
1049 rc_extra_args="$*"
1050
1051 _rc_prefix=
1052 case "$rc_arg" in

--- 137 unchanged lines hidden (view full) ---

1190 # if there's a custom ${XXX_cmd},
1191 # run that instead of the default
1192 #
1193 eval _cmd=\$${rc_arg}_cmd \
1194 _precmd=\$${rc_arg}_precmd \
1195 _postcmd=\$${rc_arg}_postcmd
1196
1197 if [ -n "$_cmd" ]; then
1198 rc_trace 1 "$_cmd"
1080 if [ -n "$_env" ]; then
1081 eval "export -- $_env"
1082 fi
1083 _run_rc_precmd || return 1
1084 _run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || return 1
1085 _run_rc_postcmd
1086 return $_return
1087 fi

--- 356 unchanged lines hidden (view full) ---

1444 fi
1445
1446 unset name command command_args command_interpreter \
1447 extra_commands pidfile procname \
1448 rcvar rcvars rcvars_obsolete required_dirs required_files \
1449 required_vars
1450 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
1451
1199 if [ -n "$_env" ]; then
1200 eval "export -- $_env"
1201 fi
1202 _run_rc_precmd || return 1
1203 _run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || return 1
1204 _run_rc_postcmd
1205 return $_return
1206 fi

--- 356 unchanged lines hidden (view full) ---

1563 fi
1564
1565 unset name command command_args command_interpreter \
1566 extra_commands pidfile procname \
1567 rcvar rcvars rcvars_obsolete required_dirs required_files \
1568 required_vars
1569 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
1570
1571 rc_trace 0 "$_file $_arg"
1572 # don't use it if we don't trust it
1573 is_verified $_file || return
1574
1452 rc_service="$_file"
1453 case "$_file" in
1454 /etc/rc.d/*.sh) # no longer allowed in the base
1455 warn "Ignoring old-style startup script $_file"
1456 ;;
1457 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip
1458 warn "Ignoring scratch file $_file"
1459 ;;
1460 *) # run in subshell
1461 if [ -x $_file ]; then
1575 rc_service="$_file"
1576 case "$_file" in
1577 /etc/rc.d/*.sh) # no longer allowed in the base
1578 warn "Ignoring old-style startup script $_file"
1579 ;;
1580 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip
1581 warn "Ignoring scratch file $_file"
1582 ;;
1583 *) # run in subshell
1584 if [ -x $_file ]; then
1585 DebugOn $_file $_file:$_arg rc:${_file##*/} rc:${_file##*/}:$_arg ${_file##*/} ${_file##*/}:$_arg
1586
1462 if [ -n "$rc_boottrace" ]; then
1463 boottrace_fn "$_file" "$_arg"
1464 elif [ -n "$rc_fast_and_loose" ]; then
1465 set $_arg; . $_file
1466 else
1467 ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3
1468 trap "echo Script $_file interrupted >&2 ; exit 1" 2
1469 trap "echo Script $_file running >&2" 29
1470 set $_arg; . $_file )
1471 fi
1587 if [ -n "$rc_boottrace" ]; then
1588 boottrace_fn "$_file" "$_arg"
1589 elif [ -n "$rc_fast_and_loose" ]; then
1590 set $_arg; . $_file
1591 else
1592 ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3
1593 trap "echo Script $_file interrupted >&2 ; exit 1" 2
1594 trap "echo Script $_file running >&2" 29
1595 set $_arg; . $_file )
1596 fi
1597 DebugOff $_file $_file:$_arg rc:${_file##*/} rc:${_file##*/}:$_arg ${_file##*/} ${_file##*/}:$_arg
1472 fi
1473 ;;
1474 esac
1475}
1476
1598 fi
1599 ;;
1600 esac
1601}
1602
1603#
1604# run_rc_scripts [options] file [...]
1605#
1606# Call `run_rc_script' for each "file" unless already listed in
1607# $_rc_elem_done.
1608#
1609# Options:
1610#
1611# --arg "arg"
1612# Pass "arg" to `run_rc_script' default is $_boot.
1613#
1614# --break "marker"
1615# If any "file" matches "marker" stop processing.
1616#
1617_rc_elem_done=
1618run_rc_scripts()
1619{
1620 local _arg=${_boot}
1621 local _rc_elem
1622 local _rc_breaks=
1623
1624 while :; do
1625 case "$1" in
1626 --arg)
1627 _arg="$2"
1628 shift 2
1629 ;;
1630 --break)
1631 _rc_breaks="$_rc_breaks $2"
1632 shift 2
1633 ;;
1634 *)
1635 break
1636 ;;
1637 esac
1638 done
1639 for _rc_elem in "$@"; do
1640 : _rc_elem=$_rc_elem
1641 case " $_rc_elem_done " in
1642 *" $_rc_elem "*)
1643 continue
1644 ;;
1645 esac
1646 run_rc_script ${_rc_elem} ${_arg}
1647 _rc_elem_done="$_rc_elem_done $_rc_elem"
1648 case " $_rc_breaks " in
1649 *" ${_rc_elem##*/} "*)
1650 break
1651 ;;
1652 esac
1653 done
1654}
1655
1477boottrace_fn()
1478{
1479 local _file _arg
1480 _file=$1
1481 _arg=$2
1482
1483 if [ -n "$rc_fast_and_loose" ]; then
1484 boottrace_sysctl "$_file start"

--- 12 unchanged lines hidden (view full) ---

1497#
1498# load_rc_config [service]
1499# Source in the configuration file(s) for a given service.
1500# If no service is specified, only the global configuration
1501# file(s) will be loaded.
1502#
1503load_rc_config()
1504{
1656boottrace_fn()
1657{
1658 local _file _arg
1659 _file=$1
1660 _arg=$2
1661
1662 if [ -n "$rc_fast_and_loose" ]; then
1663 boottrace_sysctl "$_file start"

--- 12 unchanged lines hidden (view full) ---

1676#
1677# load_rc_config [service]
1678# Source in the configuration file(s) for a given service.
1679# If no service is specified, only the global configuration
1680# file(s) will be loaded.
1681#
1682load_rc_config()
1683{
1505 local _name _rcvar_val _var _defval _v _msg _new _d
1684 local _name _rcvar_val _var _defval _v _msg _new _d _dot
1506 _name=$1
1685 _name=$1
1686 _dot=${load_rc_config_reader:-dot}
1507
1687
1688 case "$_dot" in
1689 dot|[sv]dot)
1690 ;;
1691 *) warn "Ignoring invalid load_rc_config_reader"
1692 _dot=dot
1693 ;;
1694 esac
1695 case "$1" in
1696 -s|--safe)
1697 _dot=sdot
1698 _name=$2
1699 shift
1700 ;;
1701 -v|--verify)
1702 _dot=vdot
1703 _name=$2
1704 shift
1705 ;;
1706 esac
1707
1708 DebugOn rc:$_name $_name
1709
1508 if ${_rc_conf_loaded:-false}; then
1509 :
1510 else
1511 if [ -r /etc/defaults/rc.conf ]; then
1512 debug "Sourcing /etc/defaults/rc.conf"
1710 if ${_rc_conf_loaded:-false}; then
1711 :
1712 else
1713 if [ -r /etc/defaults/rc.conf ]; then
1714 debug "Sourcing /etc/defaults/rc.conf"
1513 . /etc/defaults/rc.conf
1715 $_dot /etc/defaults/rc.conf
1514 source_rc_confs
1515 elif [ -r /etc/rc.conf ]; then
1516 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1716 source_rc_confs
1717 elif [ -r /etc/rc.conf ]; then
1718 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
1517 . /etc/rc.conf
1719 $_dot /etc/rc.conf
1518 fi
1519 _rc_conf_loaded=true
1520 fi
1521
1522 # If a service name was specified, attempt to load
1523 # service-specific configuration
1524 if [ -n "$_name" ] ; then
1525 for _d in /etc ${local_startup}; do
1526 _d=${_d%/rc.d}
1527 if [ -f ${_d}/rc.conf.d/"$_name" ]; then
1528 debug "Sourcing ${_d}/rc.conf.d/$_name"
1720 fi
1721 _rc_conf_loaded=true
1722 fi
1723
1724 # If a service name was specified, attempt to load
1725 # service-specific configuration
1726 if [ -n "$_name" ] ; then
1727 for _d in /etc ${local_startup}; do
1728 _d=${_d%/rc.d}
1729 if [ -f ${_d}/rc.conf.d/"$_name" ]; then
1730 debug "Sourcing ${_d}/rc.conf.d/$_name"
1529 . ${_d}/rc.conf.d/"$_name"
1731 $_dot ${_d}/rc.conf.d/"$_name"
1530 elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then
1531 local _rc
1532 for _rc in ${_d}/rc.conf.d/"$_name"/* ; do
1533 if [ -f "$_rc" ] ; then
1534 debug "Sourcing $_rc"
1732 elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then
1733 local _rc
1734 for _rc in ${_d}/rc.conf.d/"$_name"/* ; do
1735 if [ -f "$_rc" ] ; then
1736 debug "Sourcing $_rc"
1535 . "$_rc"
1737 $_dot "$_rc"
1536 fi
1537 done
1538 fi
1539 done
1540 fi
1541
1542 # Set defaults if defined.
1543 for _var in $rcvar $rcvars; do

--- 737 unchanged lines hidden (view full) ---

2281if kenv -q rc.debug > /dev/null ; then
2282 rc_debug=YES
2283fi
2284
2285boottrace_cmd=`command -v boottrace`
2286if [ -n "$boottrace_cmd" ] && [ "`${SYSCTL_N} -q kern.boottrace.enabled`" = "1" ]; then
2287 rc_boottrace=YES
2288fi
1738 fi
1739 done
1740 fi
1741 done
1742 fi
1743
1744 # Set defaults if defined.
1745 for _var in $rcvar $rcvars; do

--- 737 unchanged lines hidden (view full) ---

2483if kenv -q rc.debug > /dev/null ; then
2484 rc_debug=YES
2485fi
2486
2487boottrace_cmd=`command -v boottrace`
2488if [ -n "$boottrace_cmd" ] && [ "`${SYSCTL_N} -q kern.boottrace.enabled`" = "1" ]; then
2489 rc_boottrace=YES
2490fi
2491
2492# Allow for local additions and overrides.
2493# Use vdot to ensure the file has not been tampered with.
2494vdot /etc/local.rc.subr
2495
2496# safe_eval.sh provides safe_dot - for untrusted files
2497$_SAFE_EVAL_SH vdot /libexec/safe_eval.sh
2498$_DEBUG_SH vdot /libexec/debug.sh
2499
2500# Ensure we can still operate if debug.sh and
2501# safe_eval.sh are not found.
2502if have DebugOn; then
2503 # allow DEBUG_SH to be set from loader prompt
2504 DEBUG_SH=${DEBUG_SH:-$(kenv -q DEBUG_SH)}
2505else
2506 DebugOn() { return 0; }
2507 DebugOff() { return 0; }
2508fi
2509if ! have save_dot; then
2510 safe_dot() { dot "$@"; }
2511fi