1 # $NetBSD: rc.subr,v 1.60 2003/07/26 05:13:47 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
46 if [ -z "${_rc_subr_loaded}" ]; then
51 SYSCTL_N="${SYSCTL} -n"
52 CMD_OSTYPE="${SYSCTL_N} kern.ostype"
53 OSTYPE=`${CMD_OSTYPE}`
55 IDCMD="if [ -x $ID ]; then $ID -un; fi"
62 SYSCTL_W="${SYSCTL} -w"
72 # Set the variable name enabling a specific service.
73 # FreeBSD uses ${service}_enable, while NetBSD uses
74 # just the name of the service. For example:
75 # FreeBSD: sendmail_enable="YES"
76 # NetBSD : sendmail="YES"
77 # $1 - if $name is not the base to work of off, specify
90 echo ${base_var}_enable
102 # force_depend script
103 # Force a service to start. Intended for use by services
104 # to resolve dependency issues. It is assumed the caller
105 # has check to make sure this call is necessary
106 # $1 - filename of script, in /etc/rc.d, to run
112 info "${name} depends on ${_depend}, which will be forced to start."
113 if ! /etc/rc.d/${_depend} forcestart ; then
114 warn "Unable to force ${_depend}. It may already be running."
122 # Test $1 variable, and warn if not set to YES or NO.
123 # Return 0 if it's "yes" (et al), nonzero otherwise.
128 debug "checkyesno: $1 is set to $_value."
131 # "yes", "true", "on", or "1"
132 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
136 # "no", "false", "off", or "0"
137 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
141 warn "\$${1} is not set properly - see rc.conf(5)."
148 # print the list in reverse order
154 _revlist="$_revfile $_revlist"
160 # mount_critical_filesystems type
161 # Go through the list of critical filesystems as provided in
162 # the rc.conf(5) variable $critical_filesystems_${type}, checking
163 # each one to see if it is mounted, and if it is not, mounting it.
165 mount_critical_filesystems()
167 eval _fslist=\$critical_filesystems_${1}
168 for _fs in $_fslist; do
171 while read what _on on _type type; do
172 if [ $on = $_fs ]; then
179 mount $_fs >/dev/null 2>&1
186 # check_pidfile pidfile procname [interpreter]
187 # Parses the first line of pidfile for a PID, and ensures
188 # that the process is running and matches procname.
189 # Prints the matching PID upon success, nothing otherwise.
190 # interpreter is optional; see _find_processes() for details.
197 if [ -z "$_pidfile" -o -z "$_procname" ]; then
198 err 3 'USAGE: check_pidfile pidfile procname [interpreter]'
200 if [ ! -f $_pidfile ]; then
201 debug "pid file ($_pidfile): not readable."
204 read _pid _junk < $_pidfile
205 if [ -z "$_pid" ]; then
206 debug "pid file {$_pidfile): no pid in file."
209 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
213 # check_process procname [interpreter]
214 # Ensures that a process (or processes) named procname is running.
215 # Prints a list of matching PIDs.
216 # interpreter is optional; see _find_processes() for details.
222 if [ -z "$_procname" ]; then
223 err 3 'USAGE: check_process procname [interpreter]'
225 _find_processes $_procname ${_interpreter:-.} '-ax'
229 # _find_processes procname interpreter psargs
230 # Search for procname in the output of ps generated by psargs.
231 # Prints the PIDs of any matching processes, space separated.
233 # If interpreter == ".", check the following variations of procname
234 # against the first word of each command:
236 # `basename procname`
237 # `basename procname` + ":"
238 # "(" + `basename procname` + ")"
240 # If interpreter != ".", read the first line of procname, remove the
241 # leading #!, normalise whitespace, append procname, and attempt to
242 # match that against each command, either as is, or with extra words
247 if [ $# -ne 3 ]; then
248 err 3 'USAGE: _find_processes procname interpreter psargs'
255 if [ $_interpreter != "." ]; then # an interpreted script
256 read _interp < $_procname # read interpreter name
257 _interp=${_interp#\#!} # strip #!
259 if [ $_interpreter != $1 ]; then
260 warn "\$command_interpreter $_interpreter != $1"
262 _interp="$* $_procname" # cleanup spaces, add _procname
264 _fp_match='case "$_argv" in
265 ${_interp}|"${_interp} "*)'
266 else # a normal daemon
267 _procnamebn=${_procname##*/}
268 _fp_args='_arg0 _argv'
269 _fp_match='case "$_arg0" in
270 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")'
274 ps 2>/dev/null -o "pid,command" '"$_psargs"' |
275 while read _npid '"$_fp_args"'; do
279 esac ; '"$_fp_match"'
280 echo -n "$_pref$_npid" ;
286 # debug "in _find_processes: proccheck is ($_proccheck)."
291 # wait_for_pids pid [pid ...]
292 # spins until none of the pids exist
297 if [ -z "$_list" ]; then
304 if kill -0 $_j 2>/dev/null; then
305 _nlist="${_nlist}${_nlist:+ }$_j"
308 if [ -z "$_nlist" ]; then
312 echo -n ${_prefix:-"Waiting for PIDS: "}$_list
316 if [ -n "$_prefix" ]; then
322 # run_rc_command argument
323 # Search for argument in the list of supported commands, which is:
324 # "start stop restart rcvar status poll ${extra_commands}"
325 # If there's a match, run ${argument}_cmd or the default method
328 # If argument has a given prefix, then change the operation as follows:
331 # fast Skip the pid check, and set rc_fast=yes
332 # force Set ${rcvar} to YES, and set rc_force=yes
333 # one Set ${rcvar} to YES
335 # The following globals are used:
337 # Name Needed Purpose
338 # ---- ------ -------
339 # name y Name of script.
341 # command n Full path to command.
342 # Not needed if ${rc_arg}_cmd is set for
345 # command_args n Optional args/shell directives for command.
347 # command_interpreter n If not empty, command is interpreted, so
348 # call check_{pidfile,process}() appropriately.
350 # extra_commands n List of extra commands supported.
352 # pidfile n If set, use check_pidfile $pidfile $command,
353 # otherwise use check_process $command.
354 # In either case, only check if $command is set.
356 # procname n Process name to check for instead of $command.
358 # rcvar n This is checked with checkyesno to determine
359 # if the action should be run.
361 # ${name}_chroot n Directory to chroot to before running ${command}
362 # Requires /usr to be mounted.
364 # ${name}_chdir n Directory to cd to before running ${command}
365 # (if not using ${name}_chroot).
367 # ${name}_flags n Arguments to call ${command} with.
368 # NOTE: $flags from the parent environment
369 # can be used to override this.
371 # ${name}_nice n Nice level to run ${command} at.
373 # ${name}_user n User to run ${command} as, using su(1) if not
374 # using ${name}_chroot.
375 # Requires /usr to be mounted.
377 # ${name}_group n Group to run chrooted ${command} as.
378 # Requires /usr to be mounted.
380 # ${name}_groups n Comma separated list of supplementary groups
381 # to run the chrooted ${command} with.
382 # Requires /usr to be mounted.
384 # ${rc_arg}_cmd n If set, use this as the method when invoked;
385 # Otherwise, use default command (see below)
387 # ${rc_arg}_precmd n If set, run just before performing the
388 # ${rc_arg}_cmd method in the default
389 # operation (i.e, after checking for required
390 # bits and process (non)existence).
391 # If this completes with a non-zero exit code,
392 # don't run ${rc_arg}_cmd.
394 # ${rc_arg}_postcmd n If set, run just after performing the
395 # ${rc_arg}_cmd method, if that method
396 # returned a zero exit code.
398 # required_dirs n If set, check for the existence of the given
399 # directories before running the default
402 # required_files n If set, check for the readability of the given
403 # files before running the default (re)start
406 # required_vars n If set, perform checkyesno on each of the
407 # listed variables before running the default
410 # Default behaviour for a given argument, if no override method is
413 # Argument Default behaviour
414 # -------- -----------------
415 # start if !running && checkyesno ${rcvar}
419 # rc_pid=$(check_pidfile $pidfile $command)
421 # rc_pid=$(check_process $command)
422 # kill $sig_stop $rc_pid
423 # wait_for_pids $rc_pid
424 # ($sig_stop defaults to TERM.)
426 # reload Similar to stop, except use $sig_reload instead,
427 # and doesn't wait_for_pids.
428 # $sig_reload defaults to HUP.
430 # restart Run `stop' then `start'.
432 # status Show if ${command} is running, etc.
434 # poll Wait for ${command} to exit.
436 # rcvar Display what rc.conf variable is used (if any).
438 # Variables available to methods, and after run_rc_command() has
443 # rc_arg Argument to command, after fast/force/one processing
446 # rc_flags Flags to start the default command with.
447 # Defaults to ${name}_flags, unless overridden
448 # by $flags from the environment.
449 # This variable may be changed by the precmd method.
451 # rc_pid PID of command (if appropriate)
453 # rc_fast Not empty if "fast" was provided (q.v.)
455 # rc_force Not empty if "force" was provided (q.v.)
462 if [ -z "$name" ]; then
463 err 3 'run_rc_command: $name is not set.'
468 fast*) # "fast" prefix; don't check pid
469 rc_arg=${rc_arg#fast}
472 force*) # "force prefix; always run
475 rc_arg=${rc_arg#${_rc_prefix}}
476 if [ -n "${rcvar}" ]; then
480 one*) # "one" prefix; set ${rcvar}=yes
482 rc_arg=${rc_arg#${_rc_prefix}}
483 if [ -n "${rcvar}" ]; then
489 eval _overide_command=\$${name}_program
490 if [ -n "$_overide_command" ]; then
491 command=$_overide_command
494 _keywords="start stop restart rcvar $extra_commands"
497 _procname=${procname:-${command}}
499 # setup pid check command
500 if [ -n "$_procname" ]; then
501 if [ -n "$pidfile" ]; then
502 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
504 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
506 if [ -n "$_pidcmd" ]; then
507 _keywords="${_keywords} status poll"
511 if [ -z "$rc_arg" ]; then
512 rc_usage "$_keywords"
515 if [ -n "$flags" ]; then # allow override from environment
518 eval rc_flags=\$${name}_flags
520 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \
521 _nice=\$${name}_nice _user=\$${name}_user \
522 _group=\$${name}_group _groups=\$${name}_groups
524 if [ -n "$_user" ]; then # unset $_user if running as that user
525 if [ "$_user" = "$(eval $IDCMD)" ]; then
530 # if ${rcvar} is set, and $1 is not
532 # checkyesno ${rcvar}
533 # and return if that failed
535 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then
536 if ! checkyesno ${rcvar}; then
541 eval $_pidcmd # determine the pid if necessary
543 for _elem in $_keywords; do
544 if [ "$_elem" != "$rc_arg" ]; then
548 # if there's a custom ${XXX_cmd},
549 # run that instead of the default
551 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \
552 _postcmd=\$${rc_arg}_postcmd
553 if [ -n "$_cmd" ]; then
554 # if the precmd failed and force
557 if [ -n "$_precmd" ]; then
558 debug "run_rc_command: evaluating ${_precmd}()."
561 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
565 if [ -n "$_cmd" ]; then
566 debug "run_rc_command: evaluating ${_cmd}()."
569 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
573 if [ -n "$_postcmd" ]; then
574 debug "run_rc_command: evaluating ${_postcmd}()."
581 case "$rc_arg" in # default operations...
584 if [ -n "$rc_pid" ]; then
585 echo "${name} is running as pid $rc_pid."
587 echo "${name} is not running."
593 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
594 echo "${name} already running? (pid=$rc_pid)."
598 if [ ! -x ${_chroot}${command} ]; then
599 info "run_rc_command: cannot run ($command)."
603 # check for required variables,
604 # directories, and files
606 for _f in $required_vars; do
607 if ! checkyesno $_f; then
608 warn "\$${_f} is not set."
609 if [ -z "$rc_force" ]; then
614 for _f in $required_dirs; do
615 if [ ! -d "${_f}/." ]; then
616 warn "${_f} is not a directory."
617 if [ -z "$rc_force" ]; then
622 for _f in $required_files; do
623 if [ ! -r "${_f}" ]; then
624 warn "${_f} is not readable."
625 if [ -z "$rc_force" ]; then
631 # if the precmd failed and force
634 if [ -n "${_precmd}" ]; then
635 debug "run_rc_command: evaluating ${_precmd}()."
638 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
642 # setup the command to run, and run it
644 echo "Starting ${name}."
645 if [ -n "$_chroot" ]; then
647 ${_nice:+nice -n $_nice }\
648 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
649 $_chroot $command $rc_flags $command_args"
652 ${_chdir:+cd $_chdir; }\
653 ${_nice:+nice -n $_nice }\
654 $command $rc_flags $command_args"
655 if [ -n "$_user" ]; then
656 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
660 # if the cmd failed and force
663 debug "run_rc_command: _doit: $_doit"
666 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
668 # finally, run postcmd
670 if [ -n "${_postcmd}" ]; then
671 debug "run_rc_command: evaluating ${_postcmd}()."
677 if [ -z "$rc_pid" ]; then
678 [ -n "$rc_fast" ] && exit 0
679 if [ -n "$pidfile" ]; then
681 "${name} not running? (check $pidfile)."
683 echo "${name} not running?"
688 # if the precmd failed and force
691 if [ -n "$_precmd" ]; then
694 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
698 # send the signal to stop
700 echo "Stopping ${name}."
701 _doit="kill -${sig_stop:-TERM} $rc_pid"
702 if [ -n "$_user" ]; then
703 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
706 # if the stop cmd failed and force
711 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
713 # wait for the command to exit,
715 wait_for_pids $rc_pid
716 if [ -n "$_postcmd" ]; then
723 if [ -z "$rc_pid" ]; then
724 if [ -n "$pidfile" ]; then
726 "${name} not running? (check $pidfile)."
728 echo "${name} not running?"
732 echo "Reloading ${name} config files."
733 if [ -n "$_precmd" ]; then
736 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
739 _doit="kill -${sig_reload:-HUP} $rc_pid"
740 if [ -n "$_user" ]; then
741 _doit="su -m $_user -c 'sh -c \"$_doit\"'"
745 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
746 if [ -n "$_postcmd" ]; then
753 if [ -n "$_precmd" ]; then
756 [ $_return -ne 0 ] && [ -z "$rc_force" ] &&
759 # prevent restart being called more
760 # than once by any given script
762 if ${_rc_restart_done:-false}; then
765 _rc_restart_done=true
767 ( $0 ${_rc_prefix}stop )
768 $0 ${_rc_prefix}start
770 if [ -n "$_postcmd" ]; then
777 if [ -n "$rc_pid" ]; then
778 wait_for_pids $rc_pid
784 if [ -n "$rcvar" ]; then
785 if checkyesno ${rcvar}; then
786 echo "\$${rcvar}=YES"
794 rc_usage "$_keywords"
801 echo 1>&2 "$0: unknown directive '$rc_arg'."
802 rc_usage "$_keywords"
807 # run_rc_script file arg
808 # Start the script `file' with `arg', and correctly handle the
809 # return value from the script. If `file' ends with `.sh', it's
810 # sourced into the current environment. If `file' appears to be
811 # a backup or scratch file, ignore it. Otherwise if it's
812 # executable run as a child process.
818 if [ -z "$_file" -o -z "$_arg" ]; then
819 err 3 'USAGE: run_rc_script file arg'
822 unset name command command_args command_interpreter \
823 extra_commands pidfile procname \
824 rcvar required_dirs required_files required_vars
825 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd
828 *.sh) # run in current shell
831 *[~#]|*.OLD|*.orig|*,v) # scratch file; skip
832 warn "Ignoring scratch file $_file"
835 if [ -x $_file ]; then
836 if [ -n "$rc_fast_and_loose" ]; then
839 ( trap "echo Script $_file interrupted; kill -QUIT $$" 3
840 trap "echo Script $_file interrupted; exit 1" 2
841 set $_arg ; . $_file )
850 # Source in the configuration file for a given command.
855 if [ -z "$_command" ]; then
856 err 3 'USAGE: load_rc_config command'
859 if ${_rc_conf_loaded:-false}; then
862 if [ -r /etc/defaults/rc.conf ]; then
863 debug "Sourcing /etc/defaults/rc.conf"
864 . /etc/defaults/rc.conf
866 elif [ -r /etc/rc.conf ]; then
867 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)."
872 if [ -f /etc/rc.conf.d/"$_command" ]; then
873 debug "Sourcing /etc/rc.conf.d/${_command}"
874 . /etc/rc.conf.d/"$_command"
877 # XXX - Deprecated variable name support
881 [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable"
882 [ -n "$portmap_program" ] && rpcbind_program="$portmap_program"
883 [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags"
884 [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable"
885 [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable"
886 [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program"
887 [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags"
888 [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program"
889 [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags"
897 # Print a usage string for $0, with `commands' being a list of
902 echo -n 1>&2 "Usage: $0 [fast|force|one]("
906 echo -n 1>&2 "$_sep$_elem"
914 # err exitval message
915 # Display message to stderr and log to the syslog, and exit with exitval.
922 if [ -x /usr/bin/logger ]; then
923 logger "$0: ERROR: $*"
925 echo 1>&2 "$0: ERROR: $*"
931 # Display message to stderr and log to the syslog.
935 if [ -x /usr/bin/logger ]; then
936 logger "$0: WARNING: $*"
938 echo 1>&2 "$0: WARNING: $*"
943 # Display informational message to stdout and log to syslog.
948 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
949 if [ -x /usr/bin/logger ]; then
950 logger "$0: INFO: $*"
959 # If debugging is enabled in rc.conf output message to stderr.
960 # BEWARE that you don't call any subroutine that itself calls this
966 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
967 if [ -x /usr/bin/logger ]; then
968 logger "$0: INFO: $*"
970 echo 1>&2 "$0: DEBUG: $*"
976 # backup_file action file cur backup
977 # Make a backup copy of `file' into `cur', and save the previous
978 # version of `cur' as `backup' or use rcs for archiving.
980 # This routine checks the value of the backup_uses_rcs variable,
981 # which can be either YES or NO.
983 # The `action' keyword can be one of the following:
985 # add `file' is now being backed up (and is possibly
986 # being reentered into the backups system). `cur'
987 # is created and RCS files, if necessary, are
990 # update `file' has changed and needs to be backed up.
991 # If `cur' exists, it is copied to to `back' or
992 # checked into RCS (if the repository file is old),
993 # and then `file' is copied to `cur'. Another RCS
994 # check in done here if RCS is being used.
996 # remove `file' is no longer being tracked by the backups
997 # system. If RCS is not being used, `cur' is moved
998 # to `back', otherwise an empty file is checked in,
999 # and then `cur' is removed.
1009 if checkyesno backup_uses_rcs; then
1010 _msg0="backup archive"
1013 # ensure that history file is not locked
1014 if [ -f $_cur,v ]; then
1015 rcs -q -u -U -M $_cur
1018 # ensure after switching to rcs that the
1019 # current backup is not lost
1020 if [ -f $_cur ]; then
1021 # no archive, or current newer than archive
1022 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then
1023 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1032 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1035 chown root:wheel $_cur $_cur,v
1039 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur
1041 chown root:wheel $_cur $_cur,v
1048 if [ -f $_cur ]; then
1052 chown root:wheel $_cur
1061 # make_symlink src link
1062 # Make a symbolic link 'link' to src from basedir. If the
1063 # directory in which link is to be created does not exist
1064 # a warning will be displayed and an error will be returned.
1065 # Returns 0 on sucess, 1 otherwise.
1069 local src link linkdir _me
1072 linkdir="`dirname $link`"
1073 _me="make_symlink()"
1075 if [ -z "$src" -o -z "$link" ]; then
1076 warn "$_me: requires two arguments."
1079 if [ ! -d "$linkdir" ]; then
1080 warn "$_me: the directory $linkdir does not exist"
1083 if ! ln -sf $src $link ; then
1084 warn "$_me: unable to make a symbolic link from $link to $src"
1090 # devfs_rulesets_from_file file
1091 # Reads a set of devfs commands from file, and creates
1092 # the specified rulesets with their rules. Returns non-zero
1093 # if there was an error.
1095 devfs_rulesets_from_file()
1099 _me="devfs_rulesets_from_file"
1102 if [ -z "$file" ]; then
1103 warn "$_me: you must specify a file"
1106 if [ ! -e "$file" ]; then
1107 debug "$_me: no such file ($file)"
1110 debug "reading rulesets from file ($file)"
1118 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"`
1119 if [ -z "$rulenum" ]; then
1120 warn "$_me: cannot extract rule number ($line)"
1124 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"`
1125 if [ -z "$rulename" ]; then
1126 warn "$_me: cannot extract rule name ($line)"
1130 eval $rulename=\$rulenum
1131 debug "found ruleset: $rulename=$rulenum"
1132 if ! /sbin/devfs rule -s $rulenum delset ; then
1138 rulecmd="${line%%"\#*"}"
1139 # evaluate the command incase it includes
1141 if [ -n "$rulecmd" ]; then
1142 debug "adding rule ($rulecmd)"
1143 if ! eval /sbin/devfs rule -s $rulenum $rulecmd
1151 if [ $_err -ne 0 ]; then
1152 debug "error in $_me"
1159 # devfs_init_rulesets
1160 # Initializes rulesets from configuration files. Returns
1161 # non-zero if there was an error.
1163 devfs_init_rulesets()
1166 _me="devfs_init_rulesets"
1168 # Go through this only once
1169 if [ -n "$devfs_rulesets_init" ]; then
1170 debug "$_me: devfs rulesets already initialized"
1173 for file in $devfs_rulesets ; do
1174 devfs_rulesets_from_file $file || return 1
1176 devfs_rulesets_init=1
1177 debug "$_me: devfs rulesets initialized"
1181 # devfs_set_ruleset ruleset [dir]
1182 # Sets the default ruleset of dir to ruleset. The ruleset arguement
1183 # must be a ruleset name as specified in devfs.rules(5) file.
1184 # Returns non-zero if it could not set it successfully.
1189 [ -n "$1" ] && eval rs=\$$1 || rs=
1190 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1191 _me="devfs_set_ruleset"
1193 if [ -z "$rs" ]; then
1194 warn "$_me: you must specify a ruleset number"
1197 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })"
1198 if ! /sbin/devfs $devdir ruleset $rs ; then
1199 warn "$_me: unable to set ruleset $rs to ${devdir#-m }"
1205 # devfs_apply_ruleset ruleset [dir]
1206 # Apply ruleset number $ruleset to the devfs mountpoint $dir.
1207 # The ruleset argument must be a ruleset name as specified
1208 # in a devfs.rules(5) file. Returns 0 on success or non-zero
1209 # if it could not apply the ruleset.
1211 devfs_apply_ruleset()
1214 [ -n "$1" ] && eval rs=\$$1 || rs=
1215 [ -n "$2" ] && devdir="-m "$2"" || devdir=
1216 _me="devfs_apply_ruleset"
1218 if [ -z "$rs" ]; then
1219 warn "$_me: you must specify a ruleset"
1222 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })"
1223 if ! /sbin/devfs $devdir rule -s $rs applyset ; then
1224 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }"
1230 # devfs_domount dir [ruleset]
1231 # Mount devfs on dir. If ruleset is specified it is set
1232 # on the mount-point. It must also be a ruleset name as specified
1233 # in a devfs.rules(5) file. Returns 0 on success.
1239 [ -n "$2" ] && rs=$2 || rs=
1240 _me="devfs_domount()"
1242 if [ -z "$devdir" ]; then
1243 warn "$_me: you must specify a mount-point"
1246 debug "$_me: mount-point is ($devdir), ruleset is ($rs)"
1247 if ! mount -t devfs dev "$devdir" ; then
1248 warn "$_me: Unable to mount devfs on $devdir"
1251 if [ -n "$rs" ]; then
1253 devfs_set_ruleset $rs $devdir
1254 devfs -m $devdir rule applyset
1259 # devfs_mount_jail dir [ruleset]
1260 # Mounts a devfs file system appropriate for jails
1261 # on the directory dir. If ruleset is specified, the ruleset
1262 # it names will be used instead. If present, ruleset must
1263 # be the name of a ruleset as defined in a devfs.rules(5) file.
1264 # This function returns non-zero if an error occurs.
1270 [ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
1271 _me="devfs_mount_jail"
1274 if ! devfs_domount "$jdev" $rs ; then
1275 warn "$_me: devfs was not mounted on $jdev"
1281 # Provide a function for normalizing the mounting of memory
1282 # filesystems. This should allow the rest of the code here to remain
1283 # as close as possible between 5-current and 4-stable.
1286 # $3 = (optional) bytes-per-inode
1288 if [ -n "$3" ]; then
1291 /sbin/mdmfs $bpi -s $1 -M md $2