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