1*26da9de7Sriastradh.\" $NetBSD: rc.subr.8,v 1.32 2021/01/10 19:26:31 riastradh Exp $ 2868c9ba1Slukem.\" 375350585Sapb.\" Copyright (c) 2002-2011 The NetBSD Foundation, Inc. 4868c9ba1Slukem.\" All rights reserved. 5868c9ba1Slukem.\" 6868c9ba1Slukem.\" This code is derived from software contributed to The NetBSD Foundation 7868c9ba1Slukem.\" by Luke Mewburn. 8868c9ba1Slukem.\" 9868c9ba1Slukem.\" Redistribution and use in source and binary forms, with or without 10868c9ba1Slukem.\" modification, are permitted provided that the following conditions 11868c9ba1Slukem.\" are met: 12868c9ba1Slukem.\" 1. Redistributions of source code must retain the above copyright 13868c9ba1Slukem.\" notice, this list of conditions and the following disclaimer. 14868c9ba1Slukem.\" 2. Redistributions in binary form must reproduce the above copyright 15868c9ba1Slukem.\" notice, this list of conditions and the following disclaimer in the 16868c9ba1Slukem.\" documentation and/or other materials provided with the distribution. 17868c9ba1Slukem.\" 18868c9ba1Slukem.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19868c9ba1Slukem.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20868c9ba1Slukem.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21868c9ba1Slukem.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22868c9ba1Slukem.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23868c9ba1Slukem.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24868c9ba1Slukem.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25868c9ba1Slukem.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26868c9ba1Slukem.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27868c9ba1Slukem.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28868c9ba1Slukem.\" POSSIBILITY OF SUCH DAMAGE. 29868c9ba1Slukem.\" 30d8b2f12eSwiz.Dd December 17, 2012 31868c9ba1Slukem.Dt RC.SUBR 8 32868c9ba1Slukem.Os 33868c9ba1Slukem.Sh NAME 34868c9ba1Slukem.Nm rc.subr 35868c9ba1Slukem.Nd functions used by system shell scripts 36868c9ba1Slukem.Sh SYNOPSIS 37868c9ba1Slukem.Bl -item 38868c9ba1Slukem.It 39868c9ba1Slukem.Li . /etc/rc.subr 40868c9ba1Slukem.It 41868c9ba1Slukem.Ic backup_file Ar action Ar file Ar current Ar backup 42868c9ba1Slukem.It 43532fa0d9Sapb.Ic basename Ar file Op Ar suffix 44532fa0d9Sapb.It 45868c9ba1Slukem.Ic checkyesno Ar var 46868c9ba1Slukem.It 47ee821504Slukem.Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter 48868c9ba1Slukem.It 49ee821504Slukem.Ic check_process Ar procname Op Ar interpreter 50868c9ba1Slukem.It 51f265a7c0Sapb.Ic collapse_backslash_newline 52f265a7c0Sapb.It 53532fa0d9Sapb.Ic dirname Ar file 54532fa0d9Sapb.It 55868c9ba1Slukem.Ic err Ar exitval Ar message 56868c9ba1Slukem.It 57868c9ba1Slukem.Ic load_rc_config Ar command 58868c9ba1Slukem.It 593c81b28aSlukem.Ic load_rc_config_var Ar command Ar var 603c81b28aSlukem.It 61868c9ba1Slukem.Ic mount_critical_filesystems Ar type 62868c9ba1Slukem.It 6389fd5357Sapb.Ic no_rc_postprocess Ar command Op Ar arguments 6489fd5357Sapb.It 6589fd5357Sapb.Ic print_rc_metadata Ar string 6689fd5357Sapb.It 6775350585Sapb.Ic print_rc_normal Oo Fl n Oc Ar string 6889fd5357Sapb.It 69868c9ba1Slukem.Ic rc_usage Ar command Op Ar ... 70868c9ba1Slukem.It 71868c9ba1Slukem.Ic reverse_list Ar item Op Ar ... 72868c9ba1Slukem.It 73f5490001Sjmmv.Ic run_rc_command Ar argument [parameters] 74868c9ba1Slukem.It 75ee821504Slukem.Ic run_rc_script Ar file Ar argument 76868c9ba1Slukem.It 7731edfc6fSapb.Ic stop_boot 7831edfc6fSapb.It 7989fd5357Sapb.Ic twiddle 8089fd5357Sapb.It 81868c9ba1Slukem.Ic wait_for_pids Op Ar pid Op Ar ... 82868c9ba1Slukem.It 83868c9ba1Slukem.Ic warn Ar message 8489fd5357Sapb.It 8589fd5357Sapb.Ic yesno_to_truefalse Ar var 86868c9ba1Slukem.El 87868c9ba1Slukem.Sh DESCRIPTION 88868c9ba1Slukem.Nm 89868c9ba1Slukemcontains commonly used shell script functions which are used by 90868c9ba1Slukemvarious scripts such as 91868c9ba1Slukem.Xr rc 8 , 92868c9ba1Slukemand the periodic system services which are controlled by 93868c9ba1Slukem.Xr daily.conf 5 , 94868c9ba1Slukem.Xr monthly.conf 5 , 95868c9ba1Slukem.Xr security.conf 5 , 96868c9ba1Slukemand 97868c9ba1Slukem.Xr weekly.conf 5 . 98868c9ba1Slukem.Pp 99868c9ba1SlukemThe 100868c9ba1Slukem.Nm 101868c9ba1Slukemfunctions are accessed by sourcing 102868c9ba1Slukem.Pa /etc/rc.subr 103868c9ba1Slukeminto the current shell. 104868c9ba1Slukem.Pp 105868c9ba1SlukemThe following shell functions are available: 106868c9ba1Slukem.Bl -tag -width 4n 107e597a0f4Sjoerg.It Ic backup_file Ar action Ar file Ar current Ar backup 108868c9ba1SlukemMake a backup copy of 109868c9ba1Slukem.Ar file 110868c9ba1Slukeminto 111868c9ba1Slukem.Ar current . 112868c9ba1SlukemIf the 113868c9ba1Slukem.Xr rc.conf 5 114868c9ba1Slukemvariable 115868c9ba1Slukem.Sy backup_uses_rcs 116868c9ba1Slukemis 117868c9ba1Slukem.Sq YES , 118868c9ba1Slukemuse 119868c9ba1Slukem.Xr rcs 1 120868c9ba1Slukemto archive the previous version of 121868c9ba1Slukem.Ar current , 122868c9ba1Slukemotherwise save the previous version of 123868c9ba1Slukem.Ar current 124868c9ba1Slukemas 125868c9ba1Slukem.Ar backup . 126868c9ba1Slukem.Pp 127868c9ba1Slukem.Ar action 128868c9ba1Slukemmay be one of the following: 129868c9ba1Slukem.Bl -tag -width remove 130868c9ba1Slukem.It Sy add 131868c9ba1Slukem.Ar file 132868c9ba1Slukemis now being backed up by or possibly re-entered into this backup mechanism. 133868c9ba1Slukem.Ar current 134868c9ba1Slukemis created, and if necessary, the 135868c9ba1Slukem.Xr rcs 1 136868c9ba1Slukemfiles are created as well. 137868c9ba1Slukem.It Sy update 138868c9ba1Slukem.Ar file 139868c9ba1Slukemhas changed and needs to be backed up. 140868c9ba1SlukemIf 141868c9ba1Slukem.Ar current 142868c9ba1Slukemexists, it is copied to 143868c9ba1Slukem.Ar backup 144868c9ba1Slukemor checked into 145868c9ba1Slukem.Xr rcs 1 146868c9ba1Slukem(if the repository file is old), 147868c9ba1Slukemand then 148868c9ba1Slukem.Ar file 149868c9ba1Slukemis copied to 150868c9ba1Slukem.Ar current . 151868c9ba1Slukem.It Sy remove 152868c9ba1Slukem.Ar file 153868c9ba1Slukemis no longer being tracked by this backup mechanism. 154868c9ba1SlukemIf 155868c9ba1Slukem.Xr rcs 1 156868c9ba1Slukemis being used, an empty file is checked in and 157868c9ba1Slukem.Ar current 158868c9ba1Slukemis removed, 159868c9ba1Slukemotherwise 160868c9ba1Slukem.Ar current 161868c9ba1Slukemis moved to 162868c9ba1Slukem.Ar backup . 163868c9ba1Slukem.El 164*26da9de7Sriastradh.It Ic basename Ar file Op Ar suffix 165532fa0d9SapbJust like 166532fa0d9Sapb.Xr basename 1 , 167532fa0d9Sapbexcept implemented using shell built-in commands, and usable before the 168532fa0d9Sapb.Pa /usr/bin 169532fa0d9Sapbdirecory is available. 170868c9ba1Slukem.It Ic checkyesno Ar var 171868c9ba1SlukemReturn 0 if 172868c9ba1Slukem.Ar var 173868c9ba1Slukemis defined to 174868c9ba1Slukem.Sq YES , 175868c9ba1Slukem.Sq TRUE , 176868c9ba1Slukem.Sq ON , 177868c9ba1Slukemor 178868c9ba1Slukem.Sq 1 . 179868c9ba1SlukemReturn 1 if 180868c9ba1Slukem.Ar var 181868c9ba1Slukemis defined to 182868c9ba1Slukem.Sq NO , 183868c9ba1Slukem.Sq FALSE , 184868c9ba1Slukem.Sq OFF , 185868c9ba1Slukemor 186868c9ba1Slukem.Sq 0 . 187868c9ba1SlukemOtherwise, warn that 188868c9ba1Slukem.Ar var 189868c9ba1Slukemis not set correctly. 190868c9ba1SlukemThe values are case insensitive. 1913c8a1444Sjmmv.Pp 1923c8a1444SjmmvNote that the warning message shown by this function when 1933c8a1444Sjmmv.Ar var 1943c8a1444Sjmmvis not set references a manual page where the user can find more information. 1953c8a1444SjmmvIts name is picked up from the 1963c8a1444Sjmmv.Sy rcvar_manpage 1973c8a1444Sjmmvvariable. 198e597a0f4Sjoerg.It Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter 199868c9ba1SlukemParses the first word of the first line of 200868c9ba1Slukem.Ar pidfile 201868c9ba1Slukemfor a PID, and ensures that the process with that PID 202868c9ba1Slukemis running and its first argument matches 203868c9ba1Slukem.Ar procname . 20445ad5c95SwizPrints the matching PID if successful, otherwise nothing. 205ee821504SlukemIf 206ee821504Slukem.Ar interpreter 207ee821504Slukemis provided, parse the first line of 208ee821504Slukem.Ar procname , 209ee821504Slukemensure that the line is of the form 210ee821504Slukem.Dl #! interpreter [...] 211ee821504Slukemand use 212ee821504Slukem.Ar interpreter 213ee821504Slukemwith its optional arguments and 214ee821504Slukem.Ar procname 215ee821504Slukemappended as the process string to search for. 216ee821504Slukem.It Ic check_process Ar procname Op Ar interpreter 217868c9ba1SlukemPrints the PIDs of any processes that are running with a first 218868c9ba1Slukemargument that matches 219868c9ba1Slukem.Ar procname . 220ee821504Slukem.Ar interpreter 221ee821504Slukemis handled as per 222ee821504Slukem.Ic check_pidfile . 223f265a7c0Sapb.It Ic collapse_backslash_newline 224f265a7c0SapbCopy input to output, collapsing 225f265a7c0Sapb.Ao backslash Ac Ns Ao newline Ac 226f265a7c0Sapbto nothing, but leaving other backslashes alone. 227532fa0d9Sapb.Ic dirname Ar file 228532fa0d9SapbJust like 229532fa0d9Sapb.Xr dirname 1 , 230532fa0d9Sapbexcept implemented using shell built-in commands, and usable before the 231532fa0d9Sapb.Pa /usr/bin 232532fa0d9Sapbdirecory is available. 233868c9ba1Slukem.It Ic err Ar exitval Ar message 234868c9ba1SlukemDisplay an error message to 235868c9ba1Slukem.Em stderr , 236868c9ba1Slukemlog it to the system log 237868c9ba1Slukemusing 238868c9ba1Slukem.Xr logger 1 , 239868c9ba1Slukemand 240868c9ba1Slukem.Cm exit 241868c9ba1Slukemwith an exit value of 242868c9ba1Slukem.Ar exitval . 243868c9ba1SlukemThe error message consists of the script name 244868c9ba1Slukem(from 2450e798bbeSlukem.Sy $0 ) , 246868c9ba1Slukemfollowed by 247868c9ba1Slukem.Dq ": ERROR: " , 248868c9ba1Slukemand then 249868c9ba1Slukem.Ar message . 250868c9ba1Slukem.It Ic load_rc_config Ar command 2513c81b28aSlukemSource in the 2523c81b28aSlukem.Xr rc.conf 5 2533c81b28aSlukemconfiguration files for 254868c9ba1Slukem.Ar command . 255868c9ba1SlukemFirst, 256868c9ba1Slukem.Pa /etc/rc.conf 257868c9ba1Slukemis sourced if it has not yet been read in. 258868c9ba1SlukemThen, 259868c9ba1Slukem.Pa /etc/rc.conf.d/ Ns Ar command 260868c9ba1Slukemis sourced if it is an existing file. 261868c9ba1SlukemThe latter may also contain other variable assignments to override 262868c9ba1Slukem.Ic run_rc_command 263868c9ba1Slukemarguments defined by the calling script, to provide an easy 264868c9ba1Slukemmechanism for an administrator to override the behaviour of a given 265868c9ba1Slukem.Xr rc.d 8 266868c9ba1Slukemscript without requiring the editing of that script. 2673c81b28aSlukem.It Ic load_rc_config_var Ar command Ar var 2683c81b28aSlukemRead the 2693c81b28aSlukem.Xr rc.conf 5 2703c81b28aSlukemvariable 2713c81b28aSlukem.Ar var 2723c81b28aSlukemfor 2733c81b28aSlukem.Ar command 2743c81b28aSlukemand set in the current shell, using 2753c81b28aSlukem.Ic load_rc_config 2763c81b28aSlukemin a sub-shell to prevent unwanted side effects from other variable 2773c81b28aSlukemassignments. 278868c9ba1Slukem.It Ic mount_critical_filesystems Ar type 2795ee7ac88SlukemGo through a list of critical file systems, 2805ee7ac88Slukemas found in the 281868c9ba1Slukem.Xr rc.conf 5 282868c9ba1Slukemvariable 2835ee7ac88Slukem.Sy critical_filesystems_ Ns Ar type , 2845ee7ac88Slukemmounting each one that 2855ee7ac88Slukemis not currently mounted. 28689fd5357Sapb.It Ic no_rc_postprocess Ar command Op Ar arguments 28789fd5357SapbExecute the specified command with the specified arguments, 28889fd5357Sapbin such a way that its output bypasses the post-processor that 28989fd5357Sapb.Xr rc 8 29089fd5357Sapbuses for most commands. 29189fd5357SapbThis implies that the output will not appear in the 29289fd5357Sapb.Pa /var/run/rc.log 29389fd5357Sapbfile, and will appear on the console regardless of the 29489fd5357Sapbvalue of 29589fd5357Sapb.Va rc_silent . 29689fd5357SapbThis is expected to be useful for interactive commands, 29789fd5357Sapband this mechanism is automatically used by 29889fd5357Sapb.Ic run_rc_command 29989fd5357Sapbwhen a script contains the 30089fd5357Sapb.Xr rcorder 8 30189fd5357Sapbkeyword 30289fd5357Sapb.Dq interactive . 30389fd5357Sapb.Pp 30489fd5357SapbIf invoked from a context that does not appear to be under the control of 30589fd5357Sapb.Xr rc 8 , 30689fd5357Sapbthen the command is executed without special treatment. 30789fd5357Sapb.It Ic print_rc_metadata Ar string 30889fd5357SapbPrint the specified 30989fd5357Sapb.Ar string 31089fd5357Sapbin such a way that it should be handled as meta-data by the 31189fd5357Sapb.Xr rc 8 31289fd5357Sapbpost-processor. 31389fd5357SapbIf invoked from a context that does not appear to be under the control of 31489fd5357Sapb.Xr rc 8 , 31589fd5357Sapbthen the 31689fd5357Sapb.Ar string 31789fd5357Sapbis discarded. 31889fd5357Sapb.Pp 31989fd5357SapbAny 32089fd5357Sapb.Xr rc.d 8 32189fd5357Sapbscript may invoke this function with an argument that begins with 32289fd5357Sapb.Dq note: , 32389fd5357Sapbfollowed by one line of arbitrary text; 32489fd5357Sapbthe text will be logged by 32589fd5357Sapb.Xr rc 8 32689fd5357Sapbbut will not be displayed on the console. 32789fd5357Sapb.Pp 32889fd5357SapbThe use of arguments that do not begin with 32989fd5357Sapb.Dq note: 33089fd5357Sapbis reserved for internal use by 33189fd5357Sapb.Xr rc 8 33289fd5357Sapband 3330b645b22Swiz.Nm . 33475350585Sapb.It Ic print_rc_normal Oo Fl n Oc Ar string 33589fd5357SapbPrint the specified 33689fd5357Sapb.Ar string 33789fd5357Sapbin such a way that it should be handled as normal output by the 33889fd5357Sapb.Xr rc 8 33989fd5357Sapbpost-processor. 34089fd5357SapbIf invoked from a context that does not appear to be under the control of 34189fd5357Sapb.Xr rc 8 , 34289fd5357Sapbthen the 34389fd5357Sapb.Ar string 34489fd5357Sapbis printed to standard output. 34589fd5357Sapb.Pp 34675350585SapbIf the 34775350585Sapb.Fl n 34875350585Sapbflag is specified, then the string is printed without a newline. 34975350585Sapb.Pp 35075350585SapbIntended use cases include: 35175350585Sapb.Bl -bullet 35275350585Sapb.It 35375350585SapbAn rc.d script can use 35475350585Sapb.Dq Sy print_rc_normal Fl n 35575350585Sapbto print a partial line in such a way that it appears 35675350585Sapbimmediately instead of being buffered by 35775350585Sapb.Xr rc 8 Ap s 35875350585Sapbpost-processor. 35975350585Sapb.It 36075350585SapbAn rc.d script that is run via the 36175350585Sapb.Sy no_rc_postprocess 36275350585Sapbfunction (so most of its output is invisible to 36375350585Sapb.Xr rc 8 Ap s 36475350585Sapbpost-processor) can use 36575350585Sapb.Sy print_rc_normal 36675350585Sapbto force some of its output to be seen by the post-processor. 36775350585Sapb.El 368868c9ba1Slukem.It Ic rc_usage Ar command Op Ar ... 369868c9ba1SlukemPrint a usage message for 3700e798bbeSlukem.Sy $0 , 371868c9ba1Slukemwith 372868c9ba1Slukem.Ar commands 373868c9ba1Slukembeing the list of valid arguments 374868c9ba1Slukemprefixed by 375f3bab2a8Slukem.Dq "[fast|force|one]" . 376868c9ba1Slukem.It Ic reverse_list Ar item Op Ar ... 377868c9ba1SlukemPrint the list of 378868c9ba1Slukem.Ar items 379868c9ba1Slukemin reverse order. 380f5490001Sjmmv.It Ic run_rc_command Ar argument Op Ar parameter ... 381868c9ba1SlukemRun the 382ee821504Slukem.Ar argument 383868c9ba1Slukemmethod for the current 384868c9ba1Slukem.Xr rc.d 8 385868c9ba1Slukemscript, based on the settings of various shell variables. 386868c9ba1Slukem.Ic run_rc_command 387868c9ba1Slukemis extremely flexible, and allows fully functional 388868c9ba1Slukem.Xr rc.d 8 389868c9ba1Slukemscripts to be implemented in a small amount of shell code. 390f5490001SjmmvThe optional set of parameters is passed verbatim to the command, but not to its 391f5490001Sjmmvpre/post hooks. 392868c9ba1Slukem.Pp 393ee821504Slukem.Ar argument 394868c9ba1Slukemis searched for in the list of supported commands, which may be one 395868c9ba1Slukemof: 396f3bab2a8Slukem.Bl -tag -width restart -offset indent 397f3bab2a8Slukem.It Sy start 398f3bab2a8SlukemStart the service. 399f3bab2a8SlukemThis should check that the service is to be started as specified by 400f3bab2a8Slukem.Xr rc.conf 5 . 401f3bab2a8SlukemAlso checks if the service is already running and refuses to start if 402f3bab2a8Slukemit is. 403f3bab2a8SlukemThis latter check is not performed by standard 404f3bab2a8Slukem.Nx 405f3bab2a8Slukemscripts if the system is starting directly to multi-user mode, to 406f3bab2a8Slukemspeed up the boot process. 407f3bab2a8Slukem.It Sy stop 408f3bab2a8SlukemIf the service is to be started as specified by 409f3bab2a8Slukem.Xr rc.conf 5 , 410f3bab2a8Slukemstop the service. 411f3bab2a8SlukemThis should check that the service is running and complain if it's not. 412f3bab2a8Slukem.It Sy restart 413f3bab2a8SlukemPerform a 414f3bab2a8Slukem.Sy stop 415f3bab2a8Slukemthen a 416f3bab2a8Slukem.Sy start . 417f3bab2a8SlukemDefaults to displaying the process ID of the program (if running). 418f3bab2a8Slukem.It Sy rcvar 419f3bab2a8SlukemDisplay which 420f3bab2a8Slukem.Xr rc.conf 5 421f3bab2a8Slukemvariables are used to control the startup of the service (if any). 422f3bab2a8Slukem.El 423f3bab2a8Slukem.Pp 424868c9ba1SlukemIf 425868c9ba1Slukem.Sy pidfile 426868c9ba1Slukemor 42780ce7c6cSlukem.Sy procname 428f3bab2a8Slukemis set, also support: 429f3bab2a8Slukem.Bl -tag -width restart -offset indent 430f3bab2a8Slukem.It Sy poll 431f3bab2a8SlukemWait for the command to exit. 432f3bab2a8Slukem.It Sy status 433f3bab2a8SlukemShow the status of the process. 434f3bab2a8Slukem.El 435f3bab2a8Slukem.Pp 436f3bab2a8SlukemOther supported commands are listed in the optional variable 437f3bab2a8Slukem.Sy extra_commands . 438868c9ba1Slukem.Pp 439ee821504Slukem.Ar argument 440868c9ba1Slukemmay have one of the following prefixes which alters its operation: 441f3bab2a8Slukem.Bl -tag -width "Prefix" -offset indent 442f3bab2a8Slukem.It Sy fast 443ee821504SlukemSkip the check for an existing running process, 444ee821504Slukemand sets 445ee821504Slukem.Sy rc_fast=YES . 446f3bab2a8Slukem.It Sy force 447868c9ba1SlukemSkip the checks for 448868c9ba1Slukem.Sy rcvar 449ee821504Slukembeing set to yes, 450ee821504Slukemand sets 451ee821504Slukem.Sy rc_force=YES . 452ee821504SlukemThis ignores 453ee821504Slukem.Ar argument Ns Sy _precmd 454ee821504Slukemreturning non-zero, and ignores any of the 455868c9ba1Slukem.Sy required_* 456f3bab2a8Slukemtests failing, and always returns a zero exit status. 457f3bab2a8Slukem.It Sy one 458f3bab2a8SlukemSkip the checks for 459f3bab2a8Slukem.Sy rcvar 460f3bab2a8Slukembeing set to yes, but performs all the other prerequisite tests. 461868c9ba1Slukem.El 462868c9ba1Slukem.Pp 463868c9ba1Slukem.Ic run_rc_command 464868c9ba1Slukemuses the following shell variables to control its behaviour. 465868c9ba1SlukemUnless otherwise stated, these are optional. 46680ce7c6cSlukem.Bl -tag -width procname -offset indent 467868c9ba1Slukem.It Sy name 468868c9ba1SlukemThe name of this script. 469868c9ba1SlukemThis is not optional. 470868c9ba1Slukem.It Sy rcvar 471868c9ba1SlukemThe value of 472868c9ba1Slukem.Sy rcvar 473868c9ba1Slukemis checked with 474868c9ba1Slukem.Ic checkyesno 475868c9ba1Slukemto determine if this method should be run. 4763c8a1444Sjmmv.It Sy rcvar_manpage 4771913c0ffSwizThe manual page containing information about 4783c8a1444Sjmmv.Sy rcvar . 4793c8a1444SjmmvIt will be part of the warning message shown when 4803c8a1444Sjmmv.Sy rcvar 4813c8a1444Sjmmvis undefined. 4823c8a1444SjmmvDefaults to 4833c8a1444Sjmmv.Xr rc.conf 5 . 484868c9ba1Slukem.It Sy command 485868c9ba1SlukemFull path to the command. 486868c9ba1SlukemNot required if 487ee821504Slukem.Ar argument Ns Sy _cmd 488868c9ba1Slukemis defined for each supported keyword. 489868c9ba1Slukem.It Sy command_args 490868c9ba1SlukemOptional arguments and/or shell directives for 491868c9ba1Slukem.Sy command . 492b8bcb612Slukem.It Sy command_interpreter 493b8bcb612Slukem.Sy command 494b8bcb612Slukemis started with 495b8bcb612Slukem.Dl #! command_interpreter [...] 496b8bcb612Slukemwhich results in its 497b8bcb612Slukem.Xr ps 1 498b8bcb612Slukemcommand being 499b8bcb612Slukem.Dl command_interpreter [...] command 500b8bcb612Slukemso use that string to find the PID(s) of the running command 501b8bcb612Slukemrather than 502b8bcb612Slukem.Ql command . 503868c9ba1Slukem.It Sy extra_commands 504868c9ba1SlukemExtra commands/keywords/arguments supported. 505868c9ba1Slukem.It Sy pidfile 506868c9ba1SlukemPath to pid file. 507868c9ba1SlukemUsed to determine the PID(s) of the running command. 508868c9ba1SlukemIf 509868c9ba1Slukem.Sy pidfile 510868c9ba1Slukemis set, use 51180ce7c6cSlukem.Dl check_pidfile $pidfile $procname 512868c9ba1Slukemto find the PID. 513868c9ba1SlukemOtherwise, if 514868c9ba1Slukem.Sy command 515868c9ba1Slukemis set, use 51680ce7c6cSlukem.Dl check_process $procname 517868c9ba1Slukemto find the PID. 51880ce7c6cSlukem.It Sy procname 51980ce7c6cSlukemProcess name to check for. 52080ce7c6cSlukemDefaults to the value of 52180ce7c6cSlukem.Sy command . 522868c9ba1Slukem.It Sy required_dirs 523868c9ba1SlukemCheck for the existence of the listed directories 524868c9ba1Slukembefore running the default start method. 525868c9ba1Slukem.It Sy required_files 526868c9ba1SlukemCheck for the readability of the listed files 527868c9ba1Slukembefore running the default start method. 528868c9ba1Slukem.It Sy required_vars 529868c9ba1SlukemPerform 530868c9ba1Slukem.Ic checkyesno 531868c9ba1Slukemon each of the list variables 532868c9ba1Slukembefore running the default start method. 533868c9ba1Slukem.It Sy ${name}_chdir 534868c9ba1SlukemDirectory to 535868c9ba1Slukem.Ic cd 536868c9ba1Slukemto before running 537868c9ba1Slukem.Sy command , 538868c9ba1Slukemif 539868c9ba1Slukem.Sy ${name}_chroot 540868c9ba1Slukemis not provided. 541868c9ba1Slukem.It Sy ${name}_chroot 542868c9ba1SlukemDirectory to 543868c9ba1Slukem.Xr chroot 8 544868c9ba1Slukemto before running 545868c9ba1Slukem.Sy command . 546868c9ba1SlukemOnly supported after 547868c9ba1Slukem.Pa /usr 548868c9ba1Slukemis mounted. 5490bf72560She.It Sy ${name}_env 5500bf72560SheList of additional or modified environment variables to set 5510bf72560Shewhen starting 5520bf72560She.Sy command . 553868c9ba1Slukem.It Sy ${name}_flags 554868c9ba1SlukemArguments to call 555868c9ba1Slukem.Sy command 556868c9ba1Slukemwith. 557868c9ba1SlukemThis is usually set in 558868c9ba1Slukem.Xr rc.conf 5 , 559868c9ba1Slukemand not in the 560868c9ba1Slukem.Xr rc.d 8 561868c9ba1Slukemscript. 562868c9ba1SlukemThe environment variable 563868c9ba1Slukem.Sq Ev flags 564868c9ba1Slukemcan be used to override this. 565868c9ba1Slukem.It Sy ${name}_nice 566868c9ba1Slukem.Xr nice 1 567868c9ba1Slukemlevel to run 568868c9ba1Slukem.Sy command 569868c9ba1Slukemas. 570868c9ba1SlukemOnly supported after 571868c9ba1Slukem.Pa /usr 572868c9ba1Slukemis mounted. 573868c9ba1Slukem.It Sy ${name}_user 574868c9ba1SlukemUser to run 575868c9ba1Slukem.Sy command 576868c9ba1Slukemas, using 577868c9ba1Slukem.Xr chroot 8 . 578868c9ba1Slukemif 579868c9ba1Slukem.Sy ${name}_chroot 580868c9ba1Slukemis set, otherwise 581868c9ba1Slukemuses 582868c9ba1Slukem.Xr su 1 . 583868c9ba1SlukemOnly supported after 584868c9ba1Slukem.Pa /usr 585868c9ba1Slukemis mounted. 586868c9ba1Slukem.It Sy ${name}_group 587868c9ba1SlukemGroup to run the chrooted 588868c9ba1Slukem.Sy command 589868c9ba1Slukemas. 590868c9ba1Slukem.It Sy ${name}_groups 591868c9ba1SlukemComma separated list of supplementary groups to run the chrooted 592868c9ba1Slukem.Sy command 593868c9ba1Slukemwith. 594ee821504Slukem.It Ar argument Ns Sy _cmd 595868c9ba1SlukemShell commands which override the default method for 596ee821504Slukem.Ar argument . 597ee821504Slukem.It Ar argument Ns Sy _precmd 598868c9ba1SlukemShell commands to run just before running 599ee821504Slukem.Ar argument Ns Sy _cmd 600868c9ba1Slukemor the default method for 601ee821504Slukem.Ar argument . 602ce3cfa06SlukemIf this returns a non-zero exit code, the main method is not performed. 603868c9ba1SlukemIf the default method is being executed, this check is performed after 604868c9ba1Slukemthe 605868c9ba1Slukem.Sy required_* 606868c9ba1Slukemchecks and process (non-)existence checks. 607ee821504Slukem.It Ar argument Ns Sy _postcmd 608ce3cfa06SlukemShell commands to run if running 609ee821504Slukem.Ar argument Ns Sy _cmd 610ce3cfa06Slukemor the default method for 611ee821504Slukem.Ar argument 612ce3cfa06Slukemreturned a zero exit code. 613868c9ba1Slukem.It Sy sig_stop 614868c9ba1SlukemSignal to send the processes to stop in the default 615868c9ba1Slukem.Sy stop 616868c9ba1Slukemmethod. 617868c9ba1SlukemDefaults to 618868c9ba1Slukem.Dv SIGTERM . 619868c9ba1Slukem.It Sy sig_reload 620868c9ba1SlukemSignal to send the processes to reload in the default 621868c9ba1Slukem.Sy reload 622868c9ba1Slukemmethod. 623868c9ba1SlukemDefaults to 624868c9ba1Slukem.Dv SIGHUP . 625868c9ba1Slukem.El 626868c9ba1Slukem.Pp 627868c9ba1SlukemFor a given method 628ee821504Slukem.Ar argument , 629868c9ba1Slukemif 630ee821504Slukem.Ar argument Ns Sy _cmd 631868c9ba1Slukemis not defined, then a default method is provided by 632868c9ba1Slukem.Sy run_rc_command : 633868c9ba1Slukem.Bl -tag -width "argument" -offset indent 634868c9ba1Slukem.It Sy Argument 635868c9ba1Slukem.Sy Default method 636868c9ba1Slukem.It Sy start 637868c9ba1SlukemIf 638868c9ba1Slukem.Sy command 639868c9ba1Slukemis not running and 640868c9ba1Slukem.Ic checkyesno Sy rcvar 641868c9ba1Slukemsucceeds, start 642868c9ba1Slukem.Sy command . 643868c9ba1Slukem.It Sy stop 644868c9ba1SlukemDetermine the PIDs of 645868c9ba1Slukem.Sy command 646868c9ba1Slukemwith 647868c9ba1Slukem.Ic check_pidfile 648868c9ba1Slukemor 649868c9ba1Slukem.Ic check_process 650868c9ba1Slukem(as appropriate), 651868c9ba1Slukem.Ic kill Sy sig_stop 652868c9ba1Slukemthose PIDs, and run 653868c9ba1Slukem.Ic wait_for_pids 654868c9ba1Slukemon those PIDs. 655868c9ba1Slukem.It Sy reload 656868c9ba1SlukemSimilar to 657868c9ba1Slukem.Sy stop , 658868c9ba1Slukemexcept that it uses 659868c9ba1Slukem.Sy sig_reload 660868c9ba1Slukeminstead, and doesn't run 661868c9ba1Slukem.Ic wait_for_pids . 662868c9ba1Slukem.It Sy restart 663868c9ba1SlukemRuns the 664868c9ba1Slukem.Sy stop 665868c9ba1Slukemmethod, then the 666868c9ba1Slukem.Sy start 667868c9ba1Slukemmethod. 668868c9ba1Slukem.It Sy status 669868c9ba1SlukemShow the PID of 670868c9ba1Slukem.Sy command , 671868c9ba1Slukemor some other script specific status operation. 672868c9ba1Slukem.It Sy poll 673868c9ba1SlukemWait for 674868c9ba1Slukem.Sy command 675868c9ba1Slukemto exit. 676868c9ba1Slukem.It Sy rcvar 677868c9ba1SlukemDisplay which 678868c9ba1Slukem.Xr rc.conf 5 679868c9ba1Slukemvariable is used (if any). 680868c9ba1SlukemThis method always works, even if the appropriate 681868c9ba1Slukem.Xr rc.conf 5 682868c9ba1Slukemvariable is set to 683868c9ba1Slukem.Sq NO . 684868c9ba1Slukem.El 685ee821504Slukem.Pp 686ee821504SlukemThe following variables are available to the methods 687ee821504Slukem(such as 688ee821504Slukem.Ar argument Ns Sy _cmd ) 689ee821504Slukemas well as after 690ee821504Slukem.Ic run_rc_command 691ee821504Slukemhas completed: 692ee821504Slukem.Bl -tag -width "rc_flags" -offset indent 693ee821504Slukem.It Sy rc_arg 694ee821504SlukemArgument provided to 695ee821504Slukem.Sy run_rc_command , 696ee821504Slukemafter fast and force processing has been performed. 697ee821504Slukem.It Sy rc_flags 698ed8de01aSgendaliaFlags to start the default command with. 699ee821504SlukemDefaults to 700ee821504Slukem.Sy ${name}_flags , 701ee821504Slukemunless overridden by the environment variable 702ee821504Slukem.Sq Ev flags . 703ee821504SlukemThis variable may be changed by the 704ee821504Slukem.Ar argument Ns Sy _precmd 705ee821504Slukemmethod. 706ee821504Slukem.It Sy rc_pid 707ee821504SlukemPID of 708ee821504Slukem.Sy command 709ee821504Slukem(if appropriate). 710ee821504Slukem.It Sy rc_fast 711ee821504SlukemNot empty if 712ee821504Slukem.Dq fast 713ee821504Slukemprefix was used. 714ee821504Slukem.It Sy rc_force 715ee821504SlukemNot empty if 716ee821504Slukem.Dq force 717ee821504Slukemprefix was used. 718ee821504Slukem.El 719ee821504Slukem.It Ic run_rc_script Ar file Ar argument 720868c9ba1SlukemStart the script 721868c9ba1Slukem.Ar file 722868c9ba1Slukemwith an argument of 723ee821504Slukem.Ar argument , 724868c9ba1Slukemand handle the return value from the script. 725868c9ba1Slukem.Pp 726868c9ba1SlukemVarious shell variables are unset before 727868c9ba1Slukem.Ar file 728868c9ba1Slukemis started: 729868c9ba1Slukem.Bd -ragged -offset indent 7300e798bbeSlukem.Sy name , 7310e798bbeSlukem.Sy command , 7320e798bbeSlukem.Sy command_args , 7330e798bbeSlukem.Sy command_interpreter , 7340e798bbeSlukem.Sy extra_commands , 7350e798bbeSlukem.Sy pidfile , 7360e798bbeSlukem.Sy rcvar , 7370e798bbeSlukem.Sy required_dirs , 7380e798bbeSlukem.Sy required_files , 7390e798bbeSlukem.Sy required_vars , 7400e798bbeSlukem.Ar argument Ns Sy _cmd , 7410e798bbeSlukem.Ar argument Ns Sy _precmd . 7420e798bbeSlukem.Ar argument Ns Sy _postcmd . 743868c9ba1Slukem.Ed 744868c9ba1Slukem.Pp 745868c9ba1SlukemThe startup behaviour of 746868c9ba1Slukem.Ar file 747868c9ba1Slukemdepends upon the following checks: 748868c9ba1Slukem.Bl -enum 749868c9ba1Slukem.It 750868c9ba1SlukemIf 751868c9ba1Slukem.Ar file 752868c9ba1Slukemends in 753868c9ba1Slukem.Pa .sh , 754868c9ba1Slukemit is sourced into the current shell. 755868c9ba1Slukem.It 756868c9ba1SlukemIf 757868c9ba1Slukem.Ar file 758868c9ba1Slukemappears to be a backup or scratch file 759868c9ba1Slukem(e.g., with a suffix of 760868c9ba1Slukem.Sq ~ , 761868c9ba1Slukem.Sq # , 762868c9ba1Slukem.Sq .OLD , 763868c9ba1Slukemor 764868c9ba1Slukem.Sq .orig ) , 765868c9ba1Slukemignore it. 766868c9ba1Slukem.It 767868c9ba1SlukemIf 768868c9ba1Slukem.Ar file 769868c9ba1Slukemis not executable, ignore it. 770868c9ba1Slukem.It 771868c9ba1SlukemIf the 772868c9ba1Slukem.Xr rc.conf 5 773868c9ba1Slukemvariable 774868c9ba1Slukem.Sy rc_fast_and_loose 775868c9ba1Slukemis empty, 776868c9ba1Slukemsource 777868c9ba1Slukem.Ar file 778868c9ba1Slukemin a sub shell, 779868c9ba1Slukemotherwise source 780868c9ba1Slukem.Ar file 781868c9ba1Slukeminto the current shell. 78289fd5357Sapb.It 78389fd5357SapbIf 78489fd5357Sapb.Ar file 78589fd5357Sapbcontains the 78689fd5357Sapb.Xr rcorder 8 78789fd5357Sapbkeyword 78889fd5357Sapb.Dq interactive , 78989fd5357Sapbthen the command is executed using 79089fd5357Sapb.Ic no_rc_postprocess . 791868c9ba1Slukem.El 79231edfc6fSapb.It Ic stop_boot 79331edfc6fSapbPrevent booting to multiuser mode. 79431edfc6fSapbIf the 79531edfc6fSapb.Sy autoboot 79631edfc6fSapbvariable is 79731edfc6fSapb.Sq yes , 79831edfc6fSapbthen a 79931edfc6fSapb.Sy SIGTERM 80031edfc6fSapbsignal is sent to the parent 80131edfc6fSapbprocess (which is assumed to be 80231edfc6fSapb.Xr rc 8 ) . 80331edfc6fSapbOtherwise, the shell exits with status 80431edfc6fSapb.Li 1 . 80589fd5357Sapb.It Ic twiddle 80689fd5357SapbDisplay one of the characters 80789fd5357Sapb.Sq \&/ , \&- , \&\e , \&| , 80889fd5357Sapbfollowed by a backspace. 80989fd5357SapbRepeated calls to this function will create the appearance of a spinning 81089fd5357Sapbsymbol, as a different character is displayed on each call. 81189fd5357SapbOutput is to 81289fd5357Sapb.Pa /dev/tty , 81389fd5357Sapbso this function may be useful even inside a script whose output 81489fd5357Sapbhas been redirected. 815868c9ba1Slukem.It Ic wait_for_pids Op Ar pid Op Ar ... 816868c9ba1SlukemWait until all of the provided 817868c9ba1Slukem.Ar pids 818868c9ba1Slukemdon't exist any more, printing the list of outstanding 819868c9ba1Slukem.Ar pids 820868c9ba1Slukemevery two seconds. 821868c9ba1Slukem.It Ic warn Ar message 822868c9ba1SlukemDisplay a warning message to 823868c9ba1Slukem.Em stderr 824868c9ba1Slukemand log it to the system log 825868c9ba1Slukemusing 826868c9ba1Slukem.Xr logger 1 . 827868c9ba1SlukemThe warning message consists of the script name 828868c9ba1Slukem(from 8290e798bbeSlukem.Sy $0 ) , 830868c9ba1Slukemfollowed by 831868c9ba1Slukem.Dq ": WARNING: " , 832868c9ba1Slukemand then 833868c9ba1Slukem.Ar message . 83489fd5357Sapb.It Ic yesno_to_truefalse Ar var 83589fd5357SapbChange the value of the specified variable from any of the 83689fd5357Sapbforms acceptable to the 8377222c07eSapb.Ic checkyesno 83889fd5357Sapbfunction, to 83989fd5357Sapb.Dq true 84089fd5357Sapbor 84189fd5357Sapb.Dq false . 842868c9ba1Slukem.El 843868c9ba1Slukem.Sh FILES 844868c9ba1Slukem.Bl -tag -width /etc/rc.subr -compact 845868c9ba1Slukem.It Pa /etc/rc.subr 846868c9ba1SlukemThe 847868c9ba1Slukem.Nm 848868c9ba1Slukemfile resides in 849868c9ba1Slukem.Pa /etc . 850868c9ba1Slukem.El 851868c9ba1Slukem.Sh SEE ALSO 852868c9ba1Slukem.Xr rc.conf 5 , 853868c9ba1Slukem.Xr rc 8 854868c9ba1Slukem.Sh HISTORY 855868c9ba1Slukem.Nm 856868c9ba1Slukemappeared in 857868c9ba1Slukem.Nx 1.3 . 858868c9ba1SlukemThe 859868c9ba1Slukem.Xr rc.d 8 860868c9ba1Slukemsupport functions appeared in 861868c9ba1Slukem.Nx 1.5 . 86289fd5357SapbSupport for the 86389fd5357Sapb.Xr rc 8 86489fd5357Sapbpost-processor appeared in 86589fd5357Sapb.Nx 6.0 . 866