1.\" $NetBSD: rc.subr.8,v 1.12 2004/01/06 00:52:24 lukem Exp $ 2.\" 3.\" Copyright (c) 2002-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.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.\" $FreeBSD$ 31.\" 32.Dd March 18, 2022 33.Dt RC.SUBR 8 34.Os 35.Sh NAME 36.Nm rc.subr 37.Nd functions used by system shell scripts 38.Sh SYNOPSIS 39.Bl -item -compact 40.It 41.Ic .\& Pa /etc/rc.subr 42.Pp 43.It 44.Ic backup_file Ar action Ar file Ar current Ar backup 45.It 46.Ic checkyesno Ar var 47.It 48.Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter 49.It 50.Ic check_process Ar procname Op Ar interpreter 51.It 52.Ic debug Ar message 53.It 54.Ic err Ar exitval Ar message 55.It 56.Ic force_depend Ar name 57.It 58.Ic info Ar message 59.It 60.Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file 61.It 62.Ic load_rc_config Op Ar service 63.It 64.Ic load_rc_config_var Ar name Ar var 65.It 66.Ic mount_critical_filesystems Ar type 67.It 68.Ic rc_usage Ar command ... 69.It 70.Ic reverse_list Ar item ... 71.It 72.Ic run_rc_command Ar argument 73.It 74.Ic run_rc_script Ar file Ar argument 75.It 76.Ic startmsg Oo Fl n Oc Ar message 77.It 78.Ic wait_for_pids Op Ar pid ... 79.It 80.Ic warn Ar message 81.El 82.Sh DESCRIPTION 83The 84.Nm 85script 86contains commonly used shell script functions and variable 87definitions which are used by various scripts such as 88.Xr rc 8 . 89Scripts required by ports in 90.Pa /usr/local/etc/rc.d 91will also eventually 92be rewritten to make use of it. 93.Pp 94The 95.Nm 96functions were mostly imported from 97.Nx . 98.Pp 99They are accessed by sourcing 100.Pa /etc/rc.subr 101into the current shell. 102.Pp 103The following shell functions are available: 104.Bl -tag -width 4n 105.It Ic backup_file Ar action file current backup 106Make a backup copy of 107.Ar file 108into 109.Ar current . 110Save the previous version of 111.Ar current 112as 113.Ar backup . 114.Pp 115The 116.Ar action 117argument 118may be one of the following: 119.Bl -tag -width ".Cm remove" 120.It Cm add 121.Ar file 122is now being backed up by or possibly re-entered into this backup mechanism. 123.Ar current 124is created. 125.It Cm update 126.Ar file 127has changed and needs to be backed up. 128If 129.Ar current 130exists, it is copied to 131.Ar backup 132and then 133.Ar file 134is copied to 135.Ar current . 136.It Cm remove 137.Ar file 138is no longer being tracked by this backup mechanism. 139.Ar current 140is moved to 141.Ar backup . 142.El 143.It Ic checkyesno Ar var 144Return 0 if 145.Ar var 146is defined to 147.Dq Li YES , 148.Dq Li TRUE , 149.Dq Li ON , 150or 151.Ql 1 . 152Return 1 if 153.Ar var 154is defined to 155.Dq Li NO , 156.Dq Li FALSE , 157.Dq Li OFF , 158or 159.Ql 0 . 160Otherwise, warn that 161.Ar var 162is not set correctly. 163The values are case insensitive. 164.Em Note : 165.Ar var 166should be a variable name, not its value; 167.Ic checkyesno 168will expand the variable by itself. 169.It Ic check_pidfile Ar pidfile procname Op Ar interpreter 170Parses the first word of the first line of 171.Ar pidfile 172for a PID, and ensures that the process with that PID 173is running and its first argument matches 174.Ar procname . 175Prints the matching PID if successful, otherwise nothing. 176If 177.Ar interpreter 178is provided, parse the first line of 179.Ar procname , 180ensure that the line is of the form: 181.Pp 182.Dl "#! interpreter [...]" 183.Pp 184and use 185.Ar interpreter 186with its optional arguments and 187.Ar procname 188appended as the process string to search for. 189.It Ic check_process Ar procname Op Ar interpreter 190Prints the PIDs of any processes that are running with a first 191argument that matches 192.Ar procname . 193.Ar interpreter 194is handled as per 195.Ic check_pidfile . 196.It Ic debug Ar message 197Display a debugging message to 198.Va stderr , 199log it to the system log using 200.Xr logger 1 , 201and 202return to the caller. 203The error message consists of the script name 204(from 205.Va $0 ) , 206followed by 207.Dq Li ": DEBUG: " , 208and then 209.Ar message . 210This function is intended to be used by developers 211as an aid to debugging scripts. 212It can be turned on or off 213by the 214.Xr rc.conf 5 215variable 216.Va rc_debug . 217.It Ic err Ar exitval message 218Display an error message to 219.Va stderr , 220log it to the system log 221using 222.Xr logger 1 , 223and 224.Ic exit 225with an exit value of 226.Ar exitval . 227The error message consists of the script name 228(from 229.Va $0 ) , 230followed by 231.Dq Li ": ERROR: " , 232and then 233.Ar message . 234.It Ic force_depend Ar name 235Output an advisory message and force the 236.Ar name 237service to start. 238The 239.Ar name 240argument is the 241.Xr basename 1 242component of the path to the script located at 243.Pa /etc/rc.d 244(scripts stored in other locations such as 245.Pa /usr/local/etc/rc.d 246cannot be controlled with 247.Ic force_depend 248currently). 249If the script fails for any reason it will output a warning 250and return with a return value of 1. 251If it was successful 252it will return 0. 253.It Ic info Ar message 254Display an informational message to 255.Va stdout , 256and log it to the system log using 257.Xr logger 1 . 258The message consists of the script name 259(from 260.Va $0 ) , 261followed by 262.Dq Li ": INFO: " , 263and then 264.Ar message . 265The display of this informational output can be 266turned on or off by the 267.Xr rc.conf 5 268variable 269.Va rc_info . 270.It Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file 271Load 272.Ar file 273as a kernel module unless it is already loaded. 274For the purpose of checking the module status, 275either the exact module name can be specified using 276.Fl m , 277or an 278.Xr egrep 1 279regular expression matching the module name can be supplied via 280.Fl e . 281By default, the module is assumed to have the same name as 282.Ar file , 283which is not always the case. 284.It Ic load_rc_config Op Ar service 285Source in the configuration file(s) for 286.Ar service . 287If no 288.Ar service 289is specified, 290only the global configuration file(s) will be loaded. 291First, 292.Pa /etc/rc.conf 293is sourced if it has not yet been read in. 294Then, 295.Pa /etc/rc.conf.d/ Ns Ar service 296is sourced if it is an existing file. 297The latter may also contain other variable assignments to override 298.Ic run_rc_command 299arguments defined by the calling script, to provide an easy 300mechanism for an administrator to override the behaviour of a given 301.Xr rc.d 8 302script without requiring the editing of that script. 303.It Ic load_rc_config_var Ar name Ar var 304Read the 305.Xr rc.conf 5 306variable 307.Ar var 308for 309.Ar name 310and set in the current shell, using 311.Ic load_rc_config 312in a sub-shell to prevent unwanted side effects from other variable 313assignments. 314.It Ic mount_critical_filesystems Ar type 315Go through a list of critical file systems, 316as found in the 317.Xr rc.conf 5 318variable 319.Va critical_filesystems_ Ns Ar type , 320mounting each one that 321is not currently mounted. 322.It Ic rc_usage Ar command ... 323Print a usage message for 324.Va $0 , 325with 326.Ar commands 327being the list of valid arguments 328prefixed by 329.Sm off 330.Dq Bq Li fast | force | one | quiet . 331.Sm on 332.It Ic reverse_list Ar item ... 333Print the list of 334.Ar items 335in reverse order. 336.It Ic run_rc_command Ar argument 337Run the 338.Ar argument 339method for the current 340.Xr rc.d 8 341script, based on the settings of various shell variables. 342.Ic run_rc_command 343is extremely flexible, and allows fully functional 344.Xr rc.d 8 345scripts to be implemented in a small amount of shell code. 346.Pp 347.Ar argument 348is searched for in the list of supported commands, which may be one 349of: 350.Bl -tag -width ".Cm restart" -offset indent 351.It Cm start 352Start the service. 353This should check that the service is to be started as specified by 354.Xr rc.conf 5 . 355Also checks if the service is already running and refuses to start if 356it is. 357This latter check is not performed by standard 358.Fx 359scripts if the system is starting directly to multi-user mode, to 360speed up the boot process. 361.It Cm stop 362If the service is to be started as specified by 363.Xr rc.conf 5 , 364stop the service. 365This should check that the service is running and complain if it is not. 366.It Cm restart 367Perform a 368.Cm stop 369then a 370.Cm start . 371Defaults to displaying the process ID of the program (if running). 372.It Cm enabled 373Return 0 if the service is enabled and 1 if it is not. 374This command does not print anything. 375.It Cm rcvar 376Display which 377.Xr rc.conf 5 378variables are used to control the startup of the service (if any). 379.El 380.Pp 381If 382.Va pidfile 383or 384.Va procname 385is set, also support: 386.Bl -tag -width ".Cm restart" -offset indent 387.It Cm poll 388Wait for the command to exit. 389.It Cm status 390Show the status of the process. 391.El 392.Pp 393Other supported commands are listed in the optional variable 394.Va extra_commands . 395.Pp 396.Ar argument 397may have one of the following prefixes which alters its operation: 398.Bl -tag -width ".Li force" -offset indent 399.It Li fast 400Skip the check for an existing running process, 401and sets 402.Va rc_fast Ns = Ns Li YES . 403.It Li force 404Skip the checks for 405.Va rcvar 406being set to 407.Dq Li YES , 408and sets 409.Va rc_force Ns = Ns Li YES . 410This ignores 411.Ar argument Ns Va _precmd 412returning non-zero, and ignores any of the 413.Va required_* 414tests failing, and always returns a zero exit status. 415.It Li one 416Skip the checks for 417.Va rcvar 418being set to 419.Dq Li YES , 420but performs all the other prerequisite tests. 421.It Li quiet 422Inhibits some verbose diagnostics. 423Currently, this includes messages 424.Qq Starting ${name} 425(as checked by 426.Ic check_startmsgs 427inside 428.Nm ) 429and errors about usage of services that are not enabled in 430.Xr rc.conf 5 . 431This prefix also sets 432.Va rc_quiet Ns = Ns Li YES . 433.Em Note : 434.Va rc_quiet 435is not intended to completely mask all debug and warning messages, 436but only certain small classes of them. 437.El 438.Pp 439.Ic run_rc_command 440uses the following shell variables to control its behaviour. 441Unless otherwise stated, these are optional. 442.Bl -tag -width ".Va procname" -offset indent 443.It Va name 444The name of this script. 445This is not optional. 446.It Va rcvar 447The value of 448.Va rcvar 449is checked with 450.Ic checkyesno 451to determine if this method should be run. 452.It Va command 453Full path to the command. 454Not required if 455.Ar argument Ns Va _cmd 456is defined for each supported keyword. 457Can be overridden by 458.Va ${name}_program . 459.It Va command_args 460Optional arguments and/or shell directives for 461.Va command . 462.It Va command_interpreter 463.Va command 464is started with: 465.Pp 466.Dl "#! command_interpreter [...]" 467.Pp 468which results in its 469.Xr ps 1 470command being: 471.Pp 472.Dl "command_interpreter [...] command" 473.Pp 474so use that string to find the PID(s) of the running command 475rather than 476.Va command . 477.It Va extra_commands 478Extra commands/keywords/arguments supported. 479.It Va pidfile 480Path to PID file. 481Used to determine the PID(s) of the running command. 482If 483.Va pidfile 484is set, use: 485.Pp 486.Dl "check_pidfile $pidfile $procname" 487.Pp 488to find the PID. 489Otherwise, if 490.Va command 491is set, use: 492.Pp 493.Dl "check_process $procname" 494.Pp 495to find the PID. 496.It Va procname 497Process name to check for. 498Defaults to the value of 499.Va command . 500.It Va required_dirs 501Check for the existence of the listed directories 502before running the 503.Cm start 504method. 505The list is checked before running 506.Va start_precmd . 507.It Va required_files 508Check for the readability of the listed files 509before running the 510.Cm start 511method. 512The list is checked before running 513.Va start_precmd . 514.It Va required_modules 515Ensure that the listed kernel modules are loaded 516before running the 517.Cm start 518method. 519The list is checked after running 520.Va start_precmd . 521This is done after invoking the commands from 522.Va start_precmd 523so that the missing modules are not loaded in vain 524if the preliminary commands indicate a error condition. 525A word in the list can have an optional 526.Dq Li \&: Ns Ar modname 527or 528.Dq Li ~ Ns Ar pattern 529suffix. 530The 531.Ar modname 532or 533.Ar pattern 534parameter is passed to 535.Ic load_kld 536through a 537.Fl m 538or 539.Fl e 540option, respectively. 541See the description of 542.Ic load_kld 543in this document for details. 544.It Va required_vars 545Perform 546.Ic checkyesno 547on each of the list variables 548before running the 549.Cm start 550method. 551The list is checked after running 552.Va start_precmd . 553.It Va ${name}_chdir 554Directory to 555.Ic cd 556to before running 557.Va command , 558if 559.Va ${name}_chroot 560is not provided. 561.It Va ${name}_chroot 562Directory to 563.Xr chroot 8 564to before running 565.Va command . 566Only supported after 567.Pa /usr 568is mounted. 569.It Va ${name}_env 570A list of environment variables to run 571.Va command 572with. 573Those variables will be passed as arguments to the 574.Xr env 1 575utility unless 576.Ar argument Ns Va _cmd 577is defined. 578In that case the contents of 579.Va ${name}_env 580will be exported via the 581.Xr export 1 582builtin of 583.Xr sh 1 , 584which puts some limitations on the names of variables 585(e.g., a variable name may not start with a digit). 586.It Va ${name}_env_file 587A file to source for environmental variables to run 588.Va command 589with. 590.Em Note : 591all the variables which are being assigned in this file are going 592to be exported into the environment of 593.Va command . 594.It Va ${name}_fib 595FIB 596.Pa Routing Table 597number to run 598.Va command 599with. 600See 601.Xr setfib 1 602for more details. 603.It Va ${name}_flags 604Arguments to call 605.Va command 606with. 607This is usually set in 608.Xr rc.conf 5 , 609and not in the 610.Xr rc.d 8 611script. 612The environment variable 613.Sq Ev flags 614can be used to override this. 615.It Va ${name}_nice 616.Xr nice 1 617level to run 618.Va command 619as. 620Only supported after 621.Pa /usr 622is mounted. 623.It Va ${name}_limits 624Resource limits to apply to 625.Va command . 626This will be passed as arguments to the 627.Xr limits 1 628utility. 629By default, the resource limits are based on the login class defined in 630.Va ${name}_login_class . 631.It Va ${name}_login_class 632Login class to use with 633.Va ${name}_limits . 634Defaults to 635.Dq Li daemon . 636.It Va ${name}_oomprotect 637.Xr protect 1 638.Va command 639from being killed when swap space is exhausted. 640If 641.Dq Li YES 642is used, no child processes are protected. 643If 644.Dq Li ALL , 645protect all child processes. 646.It Va ${name}_program 647Full path to the command. 648Overrides 649.Va command 650if both are set, but has no effect if 651.Va command 652is unset. 653As a rule, 654.Va command 655should be set in the script while 656.Va ${name}_program 657should be set in 658.Xr rc.conf 5 . 659.It Va ${name}_user 660User to run 661.Va command 662as, using 663.Xr chroot 8 664if 665.Va ${name}_chroot 666is set, otherwise 667uses 668.Xr su 1 . 669Only supported after 670.Pa /usr 671is mounted. 672.It Va ${name}_group 673Group to run the chrooted 674.Va command 675as. 676.It Va ${name}_groups 677Comma separated list of supplementary groups to run the chrooted 678.Va command 679with. 680.It Va ${name}_prepend 681Commands to be prepended to 682.Va command . 683This is a generic version of 684.Va ${name}_env , 685.Va ${name}_fib , 686or 687.Va ${name}_nice . 688.It Ar argument Ns Va _cmd 689Shell commands which override the default method for 690.Ar argument . 691.It Ar argument Ns Va _precmd 692Shell commands to run just before running 693.Ar argument Ns Va _cmd 694or the default method for 695.Ar argument . 696If this returns a non-zero exit code, the main method is not performed. 697If the default method is being executed, this check is performed after 698the 699.Va required_* 700checks and process (non-)existence checks. 701.It Ar argument Ns Va _postcmd 702Shell commands to run if running 703.Ar argument Ns Va _cmd 704or the default method for 705.Ar argument 706returned a zero exit code. 707.It Va sig_stop 708Signal to send the processes to stop in the default 709.Cm stop 710method. 711Defaults to 712.Dv SIGTERM . 713.It Va sig_reload 714Signal to send the processes to reload in the default 715.Cm reload 716method. 717Defaults to 718.Dv SIGHUP . 719.El 720.Pp 721For a given method 722.Ar argument , 723if 724.Ar argument Ns Va _cmd 725is not defined, then a default method is provided by 726.Ic run_rc_command : 727.Bl -tag -width ".Sy Argument" -offset indent 728.It Sy Argument 729.Sy Default method 730.It Cm start 731If 732.Va command 733is not running and 734.Ic checkyesno Va rcvar 735succeeds, start 736.Va command . 737.It Cm stop 738Determine the PIDs of 739.Va command 740with 741.Ic check_pidfile 742or 743.Ic check_process 744(as appropriate), 745.Ic kill Va sig_stop 746those PIDs, and run 747.Ic wait_for_pids 748on those PIDs. 749.It Cm reload 750Similar to 751.Cm stop , 752except that it uses 753.Va sig_reload 754instead, and does not run 755.Ic wait_for_pids . 756Another difference from 757.Cm stop 758is that 759.Cm reload 760is not provided by default. 761It can be enabled via 762.Va extra_commands 763if appropriate: 764.Pp 765.Dl "extra_commands=reload" 766.It Cm restart 767Runs the 768.Cm stop 769method, then the 770.Cm start 771method. 772.It Cm status 773Show the PID of 774.Va command , 775or some other script specific status operation. 776.It Cm poll 777Wait for 778.Va command 779to exit. 780.It Cm rcvar 781Display which 782.Xr rc.conf 5 783variable is used (if any). 784This method always works, even if the appropriate 785.Xr rc.conf 5 786variable is set to 787.Dq Li NO . 788.El 789.Pp 790The following variables are available to the methods 791(such as 792.Ar argument Ns Va _cmd ) 793as well as after 794.Ic run_rc_command 795has completed: 796.Bl -tag -width ".Va rc_service" -offset indent 797.It Va rc_arg 798Argument provided to 799.Ic run_rc_command , 800after fast and force processing has been performed. 801.It Va rc_flags 802Flags to start the default command with. 803Defaults to 804.Va ${name}_flags , 805unless overridden by the environment variable 806.Sq Ev flags . 807This variable may be changed by the 808.Ar argument Ns Va _precmd 809method. 810.It Va rc_service 811Path to the service script being executed, in case it needs to re-invoke itself. 812.It Va rc_pid 813PID of 814.Va command 815(if appropriate). 816.It Va rc_fast 817Not empty if 818.Dq Li fast 819prefix was used. 820.It Va rc_force 821Not empty if 822.Dq Li force 823prefix was used. 824.El 825.It Ic run_rc_script Ar file argument 826Start the script 827.Ar file 828with an argument of 829.Ar argument , 830and handle the return value from the script. 831.Pp 832Various shell variables are unset before 833.Ar file 834is started: 835.Bd -ragged -offset indent 836.Va name , 837.Va command , 838.Va command_args , 839.Va command_interpreter , 840.Va extra_commands , 841.Va pidfile , 842.Va rcvar , 843.Va required_dirs , 844.Va required_files , 845.Va required_vars , 846.Ar argument Ns Va _cmd , 847.Ar argument Ns Va _precmd . 848.Ar argument Ns Va _postcmd . 849.Ed 850.Pp 851The startup behaviour of 852.Ar file 853depends upon the following checks: 854.Bl -enum 855.It 856If 857.Ar file 858ends in 859.Pa .sh , 860it is sourced into the current shell. 861.It 862If 863.Ar file 864appears to be a backup or scratch file 865(e.g., with a suffix of 866.Pa ~ , # , .OLD , 867or 868.Pa .orig ) , 869ignore it. 870.It 871If 872.Ar file 873is not executable, ignore it. 874.It 875If the 876.Xr rc.conf 5 877variable 878.Va rc_fast_and_loose 879is empty, 880source 881.Ar file 882in a sub shell, 883otherwise source 884.Ar file 885into the current shell. 886.El 887.It Ic startmsg Oo Fl n Oc Ar message 888Display a start message to 889.Va stdout . 890It should be used instead of 891.Xr echo 1 . 892The display of this output can be turned off if the 893.Xr rc.conf 5 894variable 895.Va rc_startmsgs 896is set to 897.Dq Li NO . 898.It Ic stop_boot Op Ar always 899Prevent booting to multiuser mode. 900If the 901.Va autoboot 902variable is set to 903.Ql yes 904(see 905.Xr rc 8 906to learn more about 907.Va autoboot ) , 908or 909.Ic checkyesno Ar always 910indicates a truth value, then a 911.Dv SIGTERM 912signal is sent to the parent 913process, which is assumed to be 914.Xr rc 8 . 915Otherwise, the shell exits with a non-zero status. 916.It Ic wait_for_pids Op Ar pid ... 917Wait until all of the provided 918.Ar pids 919do not exist any more, printing the list of outstanding 920.Ar pids 921every two seconds. 922.It Ic warn Ar message 923Display a warning message to 924.Va stderr 925and log it to the system log 926using 927.Xr logger 1 . 928The warning message consists of the script name 929(from 930.Va $0 ) , 931followed by 932.Dq Li ": WARNING: " , 933and then 934.Ar message . 935.El 936.Sh FILES 937.Bl -tag -width ".Pa /etc/rc.subr" -compact 938.It Pa /etc/rc.subr 939The 940.Nm 941file resides in 942.Pa /etc . 943.El 944.Sh SEE ALSO 945.Xr rc.conf 5 , 946.Xr rc 8 947.Sh HISTORY 948The 949.Nm 950script 951appeared in 952.Nx 1.3 . 953The 954.Xr rc.d 8 955support functions appeared in 956.Nx 1.5 . 957The 958.Nm 959script 960first appeared in 961.Fx 5.0 . 962