1 # $NetBSD: rc.subr,v 1.49 2002/05/21 12:31:01 lukem Exp $
4 # Copyright (c) 1997-2002 The NetBSD Foundation, Inc.
7 # This code is derived from software contributed to The NetBSD Foundation
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
18 # 3. All advertising materials mentioning features or use of this software
19 # must display the following acknowledgement:
20 # This product includes software developed by the NetBSD
21 # Foundation, Inc. and its contributors.
22 # 4. Neither the name of The NetBSD Foundation nor the names of its
23 # contributors may be used to endorse or promote products derived
24 # from this software without specific prior written permission.
26 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 # POSSIBILITY OF SUCH DAMAGE.
39 # functions used by various rc scripts
43 # Operating System dependent/independent variables
47 SYSCTL_N="${SYSCTL} -n"
48 CMD_OSTYPE="${SYSCTL_N} kern.ostype"
49 OSTYPE=`${CMD_OSTYPE}`
51 IDCMD="if [ -x $ID ]; then $ID -un; fi"
58 SYSCTL_W="${SYSCTL} -w"
68 # Set the variable name enabling a specific service.
69 # FreeBSD uses ${service}_enable, while NetBSD uses
70 # just the name of the service. For example:
71 # FreeBSD: sendmail_enable="YES"
72 # NetBSD : sendmail="YES"
73 # $1 - if $name is not the base to work of off, specify
86 echo ${base_var}_enable
99 # Force a service to start. Intended for use by services
100 # to resolve dependency issues. It is assumed the caller
101 # has check to make sure this call is necessary
102 # $1 - filename of script, in /etc/rc.d, to run
108 info "${name} depends on ${_depend}, which will be forced to start."
109 if ! /etc/rc.d/${_depend} forcestart ; then
110 warn "Unable to force ${_depend}. It may already be running."
118 # Test $1 variable, and warn if not set to YES or NO.
119 # Return 0 if it's "yes" (et al), nonzero otherwise.
124 debug "checkyesno: $1 is set to $_value."
127 # "yes", "true", "on", or "1"
128 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
132 # "no", "false", "off", or "0"
133 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
137 warn "\$${1} is not set properly - see rc.conf(5)."
144 # print the list in reverse order
149 for _revfile in $*; do
150 _revlist="$_revfile $_revlist"
156 # mount_critical_filesystems type
157 # Go through the list of critical filesystems as provided in
158 # the rc.conf(5) variable $critical_filesystems_${type}, checking
159 # each one to see if it is mounted, and if it is not, mounting it.
161 mount_critical_filesystems()
163 eval _fslist=\$critical_filesystems_${1}
164 for _fs in $_fslist; do
167 while read what _on on _type type; do
168 if [ $on = $_fs ]; then
172 if [ $_ismounted = no ]; then
173 mount $_fs >/dev/null 2>&1
180 # check_pidfile pidfile procname [interpreter]
181 # Parses the first line of pidfile for a PID, and ensures
182 # that the process is running and matches procname.
183 # Prints the matching PID upon success, nothing otherwise.
184 # interpreter is optional; see _find_processes() for details.
191 if [ -z "$_pidfile" -o -z "$_procname" ]; then
192 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
194 if [ ! -f $_pidfile ]; then
195 debug "pid file {$_pidfile): not readable."
198 read _pid _junk < $_pidfile
199 if [ -z "$_pid" ]; then
200 debug "pid file {$_pidfile): no pid in file."
203 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
207 # check_process procname [interpreter]
208 # Ensures that a process (or processes) named procname is running.
209 # Prints a list of matching PIDs.
210 # interpreter is optional; see _find_processes() for details.
216 if [ -z "$_procname" ]; then
217 err 3 'USAGE: check_process procname [interpreter]'
219 _find_processes $_procname ${_interpreter:-.} '-ax'
223 # _find_processes procname interpreter psargs
224 # Search for procname in the output of ps generated by psargs.
225 # Prints the PIDs of any matching processes, space separated.
227 # If interpreter == ".", check the following variations of procname
228 # against the first word of each command:
230 # `basename procname`
231 # `basename procname` + ":"
232 # "(" + `basename procname` + ")"
234 # If interpreter != ".", read the first line of procname, remove the
235 # leading #!, normalise whitespace, append procname, and attempt to
236 # match that against each command, either as is, or with extra words
241 if [ $# -ne 3 ]; then
242 err 3 'USAGE: _find_processes procname interpreter psargs'
249 if [ $_interpreter != "." ]; then # an interpreted script
250 read _interp < $_procname # read interpreter name
251 _interp=${_interp#\#!} # strip #!
253 if [ $_interpreter != $1 ]; then
254 warn "\$command_interpreter $_interpreter != $1"
256 _interp="$* $_procname" # cleanup spaces, add _procname
258 _fp_match='case "$_argv" in
259 ${_interp}|"${_interp} "*)'
260 else # a normal daemon
261 _procnamebn=${_procname##*/}
262 _fp_args='_arg0 _argv'
263 _fp_match='case "$_arg0" in
264 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
268 ps -o "pid,command" '"$_psargs"' |
269 while read _npid '"$_fp_args"'; do
273 esac ; '"$_fp_match"'
274 echo -n "$_pref$_npid" ;
280 # debug "in _find_processes: proccheck is ($_proccheck)."
285 # wait_for_pids pid [pid ...]
286 # spins until none of the pids exist
291 if [ -z "$_list" ]; then
298 if kill -0 $_j 2>/dev/null; then
299 _nlist="${_nlist}${_nlist:+ }$_j"
302 if [ -z "$_nlist" ]; then
306 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
310 if [ -n "$_prefix" ]; then
316 # run_rc_command argument
317 # Search for argument in the list of supported commands, which is:
318 # "start stop restart rcvar status poll ${extra_commands}"
319 # If there's a match, run ${argument}_cmd or the default method
322 # If argument has a given prefix, then change the operation as follows:
325 # fast Skip the pid check, and set rc_fast=yes
326 # force Set ${rcvar} to YES, and set rc_force=yes
328 # The following globals are used:
330 # Name Needed Purpose
331 # ---- ------ -------
332 # name y Name of script.
334 # command n Full path to command.
335 # Not needed if ${rc_arg}_cmd is set for
338 # command_args n Optional args/shell directives for command.
340 # command_interpreter n If not empty, command is interpreted, so
341 # call check_{pidfile,process}() appropriately.
343 # extra_commands n List of extra commands supported.
345 # pidfile n If set, use check_pidfile $pidfile $command,
346 # otherwise use check_process $command.
347 # In either case, only check if $command is set.
349 # procname n Process name to check for instead of $command.
351 # rcvar n This is checked with checkyesno to determine
352 # if the action should be run.
354 # ${name}_chroot n Directory to chroot to before running ${command}
355 # Requires /usr to be mounted.
357 # ${name}_chdir n Directory to cd to before running ${command}
358 # (if not using ${name}_chroot).
360 # ${name}_flags n Arguments to call ${command} with.
361 # NOTE: $flags from the parent environment
362 # can be used to override this.
364 # ${name}_nice n Nice level to run ${command} at.
366 # ${name}_user n User to run ${command} as, using su(1) if not
367 # using ${name}_chroot.
368 # Requires /usr to be mounted.
370 # ${name}_group n Group to run chrooted ${command} as.
371 # Requires /usr to be mounted.
373 # ${name}_groups n Comma separated list of supplementary groups
374 # to run the chrooted ${command} with.
375 # Requires /usr to be mounted.
377 # ${rc_arg}_cmd n If set, use this as the method when invoked;
378 # Otherwise, use default command (see below)
380 # ${rc_arg}_precmd n If set, run just before performing the
381 # ${rc_arg}_cmd method in the default
382 # operation (i.e, after checking for required
383 # bits and process (non)existence).
384 # If this completes with a non-zero exit code,
385 # don't run ${rc_arg}_cmd.
387 # ${rc_arg}_postcmd n If set, run just after performing the
388 # ${rc_arg}_cmd method, if that method
389 # returned a zero exit code.
391 # required_dirs n If set, check for the existence of the given
392 # directories before running the default
395 # required_files n If set, check for the readability of the given
396 # files before running the default (re)start
399 # required_vars n If set, perform checkyesno on each of the
400 # listed variables before running the default
403 # Default behaviour for a given argument, if no override method is
406 # Argument Default behaviour
407 # -------- -----------------
408 # start if !running && checkyesno ${rcvar}
412 # rc_pid=$(check_pidfile $pidfile $command)
414 # rc_pid=$(check_process $command)
415 # kill $sig_stop $rc_pid
416 # wait_for_pids $rc_pid
417 # ($sig_stop defaults to TERM.)
419 # reload Similar to stop, except use $sig_reload instead,
420 # and doesn't wait_for_pids.
421 # $sig_reload defaults to HUP.
423 # restart Run `stop' then `start'.
425 # status Show if ${command} is running, etc.
427 # poll Wait for ${command} to exit.
429 # rcvar Display what rc.conf variable is used (if any).
431 # Variables available to methods, and after run_rc_command() has
436 # rc_arg Argument to command, after fast/force processing
439 # rc_flags Flags to start the default command with.
440 # Defaults to ${name}_flags, unless overridden
441 # by $flags from the environment.
442 # This variable may be changed by the precmd method.
444 # rc_pid PID of command (if appropriate)
446 # rc_fast Not empty if "fast" was provided (q.v.)
448 # rc_force Not empty if "force" was provided (q.v.)
455 if [ -z "$name" ]; then
456 err 3 'run_rc_command: $name is not set.'
460 fast*) # "fast" prefix; don't check pid
461 rc_arg=${rc_arg#fast}
464 force*) # "force prefix; always start
465 rc_arg=${rc_arg#force}
467 if [ -n "${rcvar}" ]; then
473 eval _overide_command=\$${name}_program
474 if [ -n "$_overide_command" ]; then
475 command=$_overide_command
478 _keywords="start stop restart rcvar $extra_commands"
481 _procname=${procname:-${command}}
483 # setup pid check command if not fast
484 if [ -z "$rc_fast" -a -n "$_procname" ]; then
485 if [ -n "$pidfile" ]; then
486 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
488 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
490 if [ -n "$_pidcmd" ]; then
491 _keywords="${_keywords} status poll"
495 if [ -z "$rc_arg" ]; then
496 rc_usage "$_keywords"
499 if [ -n "$flags" ]; then # allow override from environment
502 eval rc_flags=\$${name}_flags
504 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
505 _nice=\$${name}_nice _user=\$${name}_user \
506 _group=\$${name}_group _groups=\$${name}_groups
508 if [ -n "$_user" ]; then # unset $_user if running as that user
509 if [ "$_user" = "$(eval $IDCMD)" ]; then
514 # if ${rcvar} is set, and $1 is not
516 # checkyesno ${rcvar}
517 # and return if that failed
519 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
520 if ! checkyesno ${rcvar}; then
525 eval $_pidcmd # determine the pid if necessary
527 for _elem in $_keywords; do
528 if [ "$_elem" != "$rc_arg" ]; then
532 # if there's a custom ${XXX_cmd},
533 # run that instead of the default
535 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
536 _postcmd=\$${rc_arg}_postcmd
537 if [ -n "$_cmd" ]; then
538 # if the precmd failed and force
541 if [ -n "$_precmd" ]; then
542 debug "run_rc_command: evaluating ${_precmd}()."
545 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
549 if [ -n "$_cmd" ]; then
550 debug "run_rc_command: evaluating ${_cmd}()."
553 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
557 if [ -n "$_postcmd" ]; then
558 debug "run_rc_command: evaluating ${_postcmd}()."
565 case "$rc_arg" in # default operations...
568 if [ -n "$rc_pid" ]; then
569 echo "${name} is running as pid $rc_pid."
571 echo "${name} is not running."
577 if [ -n "$rc_pid" ]; then
578 echo "${name} already running? (pid=$rc_pid)."
582 if [ ! -x $command ]; then
583 info "run_rc_command: cannot run ($command)."
587 # check for required variables,
588 # directories, and files
590 for _f in $required_vars; do
591 if ! checkyesno $_f; then
592 warn "\$${_f} is not set."
593 if [ -z "$rc_force" ]; then
598 for _f in $required_dirs; do
599 if [ ! -d "${_f}/." ]; then
600 warn "${_f} is not a directory."
601 if [ -z "$rc_force" ]; then
606 for _f in $required_files; do
607 if [ ! -r "${_f}" ]; then
608 warn "${_f} is not readable."
609 if [ -z "$rc_force" ]; then
615 # if the precmd failed and force
618 if [ -n "${_precmd}" ]; then
619 debug "run_rc_command: evaluating ${_precmd}()."
622 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
626 # setup the command to run, and run it
628 echo "Starting ${name}."
629 if [ -n "$_chroot" ]; then
631 ${_nice:+nice -n $_nice }\
632 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
633 $_chroot $command $rc_flags $command_args"
636 ${_chdir:+cd $_chdir; }\
637 ${_nice:+nice -n $_nice }\
638 $command $rc_flags $command_args"
639 if [ -n "$_user" ]; then
640 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
644 # if the cmd failed and force
647 debug "run_rc_command: _doit: $_doit"
650 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
652 # finally, run postcmd
654 if [ -n "${_postcmd}" ]; then
655 debug "run_rc_command: evaluating ${_postcmd}()."
661 if [ -z "$rc_pid" ]; then
662 if [ -n "$pidfile" ]; then
664 "${name} not running? (check $pidfile)."
666 echo "${name} not running?"
671 # if the precmd failed and force
674 if [ -n "$_precmd" ]; then
677 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
681 # send the signal to stop
683 echo "Stopping ${name}."
684 _doit="kill -${sig_stop:-TERM} $rc_pid"
685 if [ -n "$_user" ]; then
686 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
689 # if the stop cmd failed and force
694 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
696 # wait for the command to exit,
698 wait_for_pids $rc_pid
699 if [ -n "$_postcmd" ]; then
706 if [ -z "$rc_pid" ]; then
707 if [ -n "$pidfile" ]; then
709 "${name} not running? (check $pidfile)."
711 echo "${name} not running?"
715 echo "Reloading ${name} config files."
716 if [ -n "$_precmd" ]; then
719 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
722 _doit="kill -${sig_reload:-HUP} $rc_pid"
723 if [ -n "$_user" ]; then
724 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
728 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
729 if [ -n "$_postcmd" ]; then
736 if [ -n "$_precmd" ]; then
739 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
742 # prevent restart being called more
743 # than once by any given script
745 if [ -n "$_rc_restart_done" ]; then
750 ( $0 ${rc_force:+force}stop )
751 $0 ${rc_force:+force}start
753 if [ -n "$_postcmd" ]; then
760 if [ -n "$rc_pid" ]; then
761 wait_for_pids $rc_pid
767 if [ -n "$rcvar" ]; then
768 if checkyesno ${rcvar}; then
769 echo "\$${rcvar}=YES"
777 rc_usage "$_keywords"
784 echo 1>&2 "$0: unknown directive '$rc_arg'."
785 rc_usage "$_keywords"
790 # run_rc_script file arg
791 # Start the script `file' with `arg', and correctly handle the
792 # return value from the script. If `file' ends with `.sh', it's
793 # sourced into the current environment. If `file' appears to be
794 # a backup or scratch file, ignore it. Otherwise if it's
795 # executable run as a child process.
801 if [ -z "$_file" -o -z "$_arg" ]; then
802 err 3 'USAGE: run_rc_script file arg'
805 trap "echo 'Reboot interrupted'; exit 1" 3
807 unset name command command_args command_interpreter \
808 extra_commands pidfile procname \
809 rcvar required_dirs required_files required_vars
810 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
813 *.sh) # run in current shell
816 *[~#]|*.OLD|*.orig) # scratch file; skip
817 warn "Ignoring scratch file $_file"
820 if [ -x $_file ]; then
821 if [ -n "$rc_fast_and_loose" ]; then
824 ( trap "echo 'Reboot interrupted'; exit 1" 3
825 set $_arg ; . $_file )
834 # Source in the configuration file for a given command.
839 if [ -z "$_command" ]; then
840 err 3 'USAGE: load_rc_config command'
843 if [ -z "$_rc_conf_loaded" ]; then
844 if [ -r /etc/defaults/rc.conf ]; then
845 debug "Sourcing /etc/defaults/rc.conf"
846 . /etc/defaults/rc.conf
848 elif [ -r /etc/rc.conf ]; then
849 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
854 if [ -f /etc/rc.conf.d/"$_command" ]; then
855 debug "Sourcing /etc/rc.conf.d/${_command}"
856 . /etc/rc.conf.d/"$_command"
859 # XXX - Deprecated variable name support
863 [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
864 [ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
865 [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
866 [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
867 [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
868 [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
869 [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
870 [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
871 [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
879 # Print a usage string for $0, with `commands' being a list of
884 echo -n 1>&2 "Usage: $0 [fast|force]("
888 echo -n 1>&2 "$_sep$_elem"
896 # err exitval message
897 # Display message to stderr and log to the syslog, and exit with exitval.
904 if [ -x /usr/bin/logger ]; then
905 logger "$0: ERROR: $*"
907 echo 1>&2 "$0: ERROR: $*"
913 # Display message to stderr and log to the syslog.
917 if [ -x /usr/bin/logger ]; then
918 logger "$0: WARNING: $*"
920 echo 1>&2 "$0: WARNING: $*"
925 # Display informational message to stdout and log to syslog.
930 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
931 if [ -x /usr/bin/logger ]; then
932 logger "$0: INFO: $*"
941 # If debugging is enabled in rc.conf output message to stderr.
942 # BEWARE that you don't call any subroutine that itself calls this
948 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
949 if [ -x /usr/bin/logger ]; then
950 logger "$0: INFO: $*"
952 echo 1>&2 "$0: DEBUG: $*"
958 # backup_file action file cur backup
959 # Make a backup copy of `file' into `cur', and save the previous
960 # version of `cur' as `backup' or use rcs for archiving.
962 # This routine checks the value of the backup_uses_rcs variable,
963 # which can be either YES or NO.
965 # The `action' keyword can be one of the following:
967 # add `file' is now being backed up (and is possibly
968 # being reentered into the backups system). `cur'
969 # is created and RCS files, if necessary, are
972 # update `file' has changed and needs to be backed up.
973 # If `cur' exists, it is copied to to `back' or
974 # checked into RCS (if the repository file is old),
975 # and then `file' is copied to `cur'. Another RCS
976 # check in done here if RCS is being used.
978 # remove `file' is no longer being tracked by the backups
979 # system. If RCS is not being used, `cur' is moved
980 # to `back', otherwise an empty file is checked in,
981 # and then `cur' is removed.
991 if checkyesno backup_uses_rcs; then
992 _msg0="backup archive"
995 # ensure that history file is not locked
996 if [ -f $_cur,v ]; then
997 rcs -q -u -U -M $_cur
1000 # ensure after switching to rcs that the
1001 # current backup is not lost
1002 if [ -f $_cur ]; then
1003 # no archive, or current newer than archive
1004 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1005 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1014 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1017 chown root:wheel $_cur $_cur,v
1021 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1023 chown root:wheel $_cur $_cur,v
1030 if [ -f $_cur ]; then
1034 chown root:wheel $_cur
1043 # make_symlink src link
1044 # Make a symbolic link 'link' to src from basedir. If the
1045 # directory in which link is to be created does not exist
1046 # a warning will be displayed and an error will be returned.
1047 # Returns 0 on sucess, 1 otherwise.
1051 local src link linkdir _me
1054 linkdir="`dirname $link`"
1055 _me="make_symlink()"
1057 if [ -z "$src" -o -z "$link" ]; then
1058 warn "$_me: requires two arguments."
1061 if [ ! -d "$linkdir" ]; then
1062 warn "$_me: the directory $linkdir does not exist"
1065 if ! ln -sf $src $link ; then
1066 warn "$_me: unable to make a symbolic link from $link to $src"
1072 # devfs_rulesets_from_file file
1073 # Reads a set of devfs commands from file, and creates
1074 # the specified rulesets with their rules. Returns non-zero
1075 # if there was an error.
1077 devfs_rulesets_from_file()
1081 _me="devfs_rulesets_from_file"
1084 if [ -z "$file" ]; then
1085 warn "$_me: you must specify a file"
1088 if [ ! -e "$file" ]; then
1089 debug "$_me: no such file ($file)"
1092 debug "reading rulesets from file ($file)"
1100 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1101 if [ -z "$rulenum" ]; then
1102 warn "$_me: cannot extract rule number ($line)"
1106 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1107 if [ -z "$rulename" ]; then
1108 warn "$_me: cannot extract rule name ($line)"
1112 eval $rulename=\$rulenum
1113 debug "found ruleset: $rulename=$rulenum"
1114 if ! /sbin/devfs rule -s $rulenum delset ; then
1120 rulecmd="${line%%"\#*"}"
1121 # evaluate the command incase it includes
1123 if [ -n "$rulecmd" ]; then
1124 debug "adding rule ($rulecmd)"
1125 if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1133 if [ $_err -ne 0 ]; then
1134 debug "error in $_me"
1141 # devfs_init_rulesets
1142 # Initializes rulesets from configuration files. Returns
1143 # non-zero if there was an error.
1145 devfs_init_rulesets()
1148 _me="devfs_init_rulesets"
1150 # Go through this only once
1151 if [ -n "$devfs_rulesets_init" ]; then
1152 debug "$_me: devfs rulesets already initialized"
1155 for file in $devfs_rulesets ; do
1156 devfs_rulesets_from_file $file || return 1
1158 devfs_rulesets_init=1
1159 debug "$_me: devfs rulesets initialized"
1163 # devfs_set_ruleset ruleset [dir]
1164 # Sets the default ruleset of dir to ruleset. The ruleset arguement
1165 # must be a ruleset name as specified in devfs.rules(5) file.
1166 # Returns non-zero if it could not set it successfully.
1171 [ -n "$1" ] && eval rs=\$$1 || rs=
1172 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1173 _me="devfs_set_ruleset"
1175 if [ -z "$rs" ]; then
1176 warn "$_me: you must specify a ruleset number"
1179 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1180 if ! /sbin/devfs $devdir ruleset $rs ; then
1181 warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1187 # devfs_apply_ruleset ruleset [dir]
1188 # Apply ruleset number $ruleset to the devfs mountpoint $dir.
1189 # The ruleset argument must be a ruleset name as specified
1190 # in a devfs.rules(5) file. Returns 0 on success or non-zero
1191 # if it could not apply the ruleset.
1193 devfs_apply_ruleset()
1196 [ -n "$1" ] && eval rs=\$$1 || rs=
1197 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1198 _me="devfs_apply_ruleset"
1200 if [ -z "$rs" ]; then
1201 warn "$_me: you must specify a ruleset"
1204 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1205 if ! /sbin/devfs $devdir rule -s $rs applyset ; then
1206 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1212 # devfs_domount dir [ruleset]
1213 # Mount devfs on dir. If ruleset is specified it is set
1214 # on the mount-point. It must also be a ruleset name as specified
1215 # in a devfs.rules(5) file. Returns 0 on success.
1221 [ -n "$2" ] && rs=$2 || rs=
1222 _me="devfs_domount()"
1224 if [ -z "$devdir" ]; then
1225 warn "$_me: you must specify a mount-point"
1228 debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1229 if ! mount -t devfs dev "$devdir" ; then
1230 warn "$_me: Unable to mount devfs on $devdir"
1233 if [ -n "$rs" ]; then
1235 devfs_set_ruleset $rs $devdir
1236 devfs -m $devdir rule applyset
1241 # devfs_mount_jail dir [ruleset]
1242 # Mounts a devfs file system appropriate for jails
1243 # on the directory dir. If ruleset is specified, the ruleset
1244 # it names will be used instead. If present, ruleset must
1245 # be the name of a ruleset as defined in a devfs.rules(5) file.
1246 # This function returns non-zero if an error occurs.
1252 [ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1253 _me="devfs_mount_jail"
1256 if ! devfs_domount "$jdev" $rs ; then
1257 warn "$_me: devfs was not mounted on $jdev"