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 May 28, 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 DebugOn Ar tag ... 51.It 52.Ic DebugOff Ar tag ... 53.It 54.Ic debug Ar message 55.It 56.Ic dot Ar file ... 57.It 58.Ic err Ar exitval Ar message 59.It 60.Ic force_depend Ar name 61.It 62.Ic info Ar message 63.It 64.Ic is_verified Ar file 65.It 66.Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file 67.It 68.Ic load_rc_config Oo Ar flag Oc Op Ar service 69.It 70.Ic load_rc_config_var Ar name Ar var 71.It 72.Ic mount_critical_filesystems Ar type 73.It 74.Ic rc_log Ar message 75.It 76.It rc_trace Ar level Ar message 77.It 78.Ic rc_usage Ar command ... 79.It 80.Ic reverse_list Ar item ... 81.It 82.Ic run_rc_command Ar argument 83.It 84.Ic run_rc_script Ar file Ar argument 85.It 86.Ic run_rc_scripts Oo options Oc Ar file ... 87.It 88.Ic safe_dot Ar file ... 89.It 90.Ic sdot Ar file ... 91.It 92.Ic startmsg Oo Fl n Oc Ar message 93.It 94.Ic vdot Ar file ... 95.It 96.Ic wait_for_pids Op Ar pid ... 97.It 98.Ic warn Ar message 99.El 100.Sh DESCRIPTION 101The 102.Nm 103script 104contains commonly used shell script functions and variable 105definitions which are used by various scripts such as 106.Xr rc 8 . 107Scripts required by ports in 108.Pa /usr/local/etc/rc.d 109will also eventually 110be rewritten to make use of it. 111.Pp 112The 113.Nm 114functions were mostly imported from 115.Nx . 116.Pp 117They are accessed by sourcing 118.Pa /etc/rc.subr 119into the current shell. 120.Pp 121The following shell functions are available: 122.Bl -tag -width 4n 123.It Ic backup_file Ar action file current backup 124Make a backup copy of 125.Ar file 126into 127.Ar current . 128Save the previous version of 129.Ar current 130as 131.Ar backup . 132.Pp 133The 134.Ar action 135argument 136may be one of the following: 137.Bl -tag -width ".Cm remove" 138.It Cm add 139.Ar file 140is now being backed up by or possibly re-entered into this backup mechanism. 141.Ar current 142is created. 143.It Cm update 144.Ar file 145has changed and needs to be backed up. 146If 147.Ar current 148exists, it is copied to 149.Ar backup 150and then 151.Ar file 152is copied to 153.Ar current . 154.It Cm remove 155.Ar file 156is no longer being tracked by this backup mechanism. 157.Ar current 158is moved to 159.Ar backup . 160.El 161.It Ic checkyesno Ar var 162Return 0 if 163.Ar var 164is defined to 165.Dq Li YES , 166.Dq Li TRUE , 167.Dq Li ON , 168or 169.Ql 1 . 170Return 1 if 171.Ar var 172is defined to 173.Dq Li NO , 174.Dq Li FALSE , 175.Dq Li OFF , 176or 177.Ql 0 . 178Otherwise, warn that 179.Ar var 180is not set correctly. 181The values are case insensitive. 182.Em Note : 183.Ar var 184should be a variable name, not its value; 185.Ic checkyesno 186will expand the variable by itself. 187.It Ic check_pidfile Ar pidfile procname Op Ar interpreter 188Parses the first word of the first line of 189.Ar pidfile 190for a PID, and ensures that the process with that PID 191is running and its first argument matches 192.Ar procname . 193Prints the matching PID if successful, otherwise nothing. 194If 195.Ar interpreter 196is provided, parse the first line of 197.Ar procname , 198ensure that the line is of the form: 199.Pp 200.Dl "#! interpreter [...]" 201.Pp 202and use 203.Ar interpreter 204with its optional arguments and 205.Ar procname 206appended as the process string to search for. 207.It Ic check_process Ar procname Op Ar interpreter 208Prints the PIDs of any processes that are running with a first 209argument that matches 210.Ar procname . 211.Ar interpreter 212is handled as per 213.Ic check_pidfile . 214.It Ic DebugOn Ar tag ... 215Enable tracing if not already enabled, 216and any 217.Ar tag 218is found in 219.Va DEBUG_SH 220(a comma separated list of tags). 221.Pp 222Record the 223.Ar tag 224that caused it to be enabled in 225.Va DEBUG_ON , 226set 227.Va DEBUG_DO 228empty and 229.Va DEBUG_SKIP 230to 231.Ql \&: . 232.Pp 233See 234.Xr debug.sh 8 235for more details. 236.It Ic DebugOff Ar tag ... 237Disable tracing if enabled and any 238.Ar tag 239matches 240.Va DEBUG_ON , 241which means it was the reason tracing was enabled. 242.Pp 243Set 244.Va DEBUG_DO 245to 246.Ql \&: , 247and 248.Va DEBUG_ON , 249.Va DEBUG_SKIP 250empty. 251.It Ic debug Ar message 252Display a debugging message to 253.Va stderr , 254log it to the system log using 255.Xr logger 1 , 256and 257return to the caller. 258The error message consists of the script name 259(from 260.Va $0 ) , 261followed by 262.Dq Li ": DEBUG: " , 263and then 264.Ar message . 265This function is intended to be used by developers 266as an aid to debugging scripts. 267It can be turned on or off 268by the 269.Xr rc.conf 5 270variable 271.Va rc_debug . 272.It Ic dot Ar file ... 273For reading in unverified files. 274.Pp 275Ensure shell 276.Li verify 277option is off. 278This option is only meaningful when 279.Xr mac_veriexec 4 280is active. 281.Pp 282Read each 283.Ar file 284if it exists. 285.Pp 286Restore previous state of the 287.Li verify 288option. 289.It Ic err Ar exitval message 290Display an error message to 291.Va stderr , 292log it to the system log 293using 294.Xr logger 1 , 295and 296.Ic exit 297with an exit value of 298.Ar exitval . 299The error message consists of the script name 300(from 301.Va $0 ) , 302followed by 303.Dq Li ": ERROR: " , 304and then 305.Ar message . 306.It Ic force_depend Ar name 307Output an advisory message and force the 308.Ar name 309service to start. 310The 311.Ar name 312argument is the 313.Xr basename 1 314component of the path to the script located at 315.Pa /etc/rc.d 316(scripts stored in other locations such as 317.Pa /usr/local/etc/rc.d 318cannot be controlled with 319.Ic force_depend 320currently). 321If the script fails for any reason it will output a warning 322and return with a return value of 1. 323If it was successful 324it will return 0. 325.It Ic is_verified Ar file 326If 327.Xr veriexec 8 328does not exist, or 329.Xr mac_veriexec 4 330is not active, just return success. 331Otherwise use 332.Xr veriexec 8 333to check if 334.Ar file 335is verified. 336If not verified the return code will be 80 (EAUTH). 337.It Ic info Ar message 338Display an informational message to 339.Va stdout , 340and log it to the system log using 341.Xr logger 1 . 342The message consists of the script name 343(from 344.Va $0 ) , 345followed by 346.Dq Li ": INFO: " , 347and then 348.Ar message . 349The display of this informational output can be 350turned on or off by the 351.Xr rc.conf 5 352variable 353.Va rc_info . 354.It Ic load_kld Oo Fl e Ar regex Oc Oo Fl m Ar module Oc Ar file 355Load 356.Ar file 357as a kernel module unless it is already loaded. 358For the purpose of checking the module status, 359either the exact module name can be specified using 360.Fl m , 361or an 362.Xr egrep 1 363regular expression matching the module name can be supplied via 364.Fl e . 365By default, the module is assumed to have the same name as 366.Ar file , 367which is not always the case. 368.It Ic load_rc_config Oo Ar flag Oc Op Ar service 369Source in the configuration file(s) for 370.Ar service . 371If no 372.Ar service 373is specified, 374only the global configuration file(s) will be loaded. 375First, 376.Pa /etc/rc.conf 377is sourced if it has not yet been read in. 378Then, 379.Pa /etc/rc.conf.d/ Ns Ar service 380is sourced if it is an existing file. 381The latter may also contain other variable assignments to override 382.Ic run_rc_command 383arguments defined by the calling script, to provide an easy 384mechanism for an administrator to override the behaviour of a given 385.Xr rc.d 8 386script without requiring the editing of that script. 387.Pp 388The function named by 389.Va load_rc_config_reader 390(default is 391.Ic dot ) 392is used to read configuration unless 393.Ar flag 394is: 395.Bl -tag -width Ds 396.It Fl s 397use 398.Ic sdot 399to read configuration, 400because we want verified configuration or 401to use 402.Ic safe_dot 403to read an unverified configuration. 404.It Fl v 405use 406.Ic vdot 407to read in configuration only if it is verified. 408.El 409.Pp 410.Ic DebugOn 411will be called with tags derived from 412.Ar name 413to enable tracing if any appear in 414.Va DEBUG_SH . 415.It Ic load_rc_config_var Ar name Ar var 416Read the 417.Xr rc.conf 5 418variable 419.Ar var 420for 421.Ar name 422and set in the current shell, using 423.Ic load_rc_config 424in a sub-shell to prevent unwanted side effects from other variable 425assignments. 426.It Ic mount_critical_filesystems Ar type 427Go through a list of critical file systems, 428as found in the 429.Xr rc.conf 5 430variable 431.Va critical_filesystems_ Ns Ar type , 432mounting each one that 433is not currently mounted. 434.It Ic rc_log Ar message 435Output 436.Ar message 437with a timestamp, which is both human readable and 438easily parsed for post processing, using: 439.Bd -literal -offset indent 440date "+@ %s [%Y-%m-%d %H:%M:%S %Z] $*" 441.Ed 442.It Ic rc_trace Ar level Ar message 443If the file 444.Pa /etc/rc.conf.d/rc_trace 445exists and is not empty attempt to set 446.Va RC_LEVEL 447based on its content. 448If the file is empty or does not contain 449a value for 450.Va RC_LEVEL , 451set it to 452.Li 0 . 453.Pp 454If 455.Ar level 456is greater than or equal to 457.Va RC_LEVEL 458pass 459.Ar message 460to 461.Ic rc_log . 462.It Ic rc_usage Ar command ... 463Print a usage message for 464.Va $0 , 465with 466.Ar commands 467being the list of valid arguments 468prefixed by 469.Sm off 470.Dq Bq Li fast | force | one | quiet . 471.Sm on 472.It Ic reverse_list Ar item ... 473Print the list of 474.Ar items 475in reverse order. 476.It Ic run_rc_command Ar argument 477Run the 478.Ar argument 479method for the current 480.Xr rc.d 8 481script, based on the settings of various shell variables. 482.Ic run_rc_command 483is extremely flexible, and allows fully functional 484.Xr rc.d 8 485scripts to be implemented in a small amount of shell code. 486.Pp 487.Ar argument 488is searched for in the list of supported commands, which may be one 489of: 490.Bl -tag -width ".Cm restart" -offset indent 491.It Cm start 492Start the service. 493This should check that the service is to be started as specified by 494.Xr rc.conf 5 . 495Also checks if the service is already running and refuses to start if 496it is. 497This latter check is not performed by standard 498.Fx 499scripts if the system is starting directly to multi-user mode, to 500speed up the boot process. 501.It Cm stop 502If the service is to be started as specified by 503.Xr rc.conf 5 , 504stop the service. 505This should check that the service is running and complain if it is not. 506.It Cm restart 507Perform a 508.Cm stop 509then a 510.Cm start . 511Defaults to displaying the process ID of the program (if running). 512.It Cm enabled 513Return 0 if the service is enabled and 1 if it is not. 514This command does not print anything. 515.It Cm rcvar 516Display which 517.Xr rc.conf 5 518variables are used to control the startup of the service (if any). 519.El 520.Pp 521If 522.Va pidfile 523or 524.Va procname 525is set, also support: 526.Bl -tag -width ".Cm restart" -offset indent 527.It Cm poll 528Wait for the command to exit. 529.It Cm status 530Show the status of the process. 531.El 532.Pp 533Other supported commands are listed in the optional variable 534.Va extra_commands . 535.Pp 536.Ar argument 537may have one of the following prefixes which alters its operation: 538.Bl -tag -width ".Li force" -offset indent 539.It Li fast 540Skip the check for an existing running process, 541and sets 542.Va rc_fast Ns = Ns Li YES . 543.It Li force 544Skip the checks for 545.Va rcvar 546being set to 547.Dq Li YES , 548and sets 549.Va rc_force Ns = Ns Li YES . 550This ignores 551.Ar argument Ns Va _precmd 552returning non-zero, and ignores any of the 553.Va required_* 554tests failing, and always returns a zero exit status. 555.It Li one 556Skip the checks for 557.Va rcvar 558being set to 559.Dq Li YES , 560but performs all the other prerequisite tests. 561.It Li quiet 562Inhibits some verbose diagnostics. 563Currently, this includes messages 564.Qq Starting ${name} 565(as checked by 566.Ic check_startmsgs 567inside 568.Nm ) 569and errors about usage of services that are not enabled in 570.Xr rc.conf 5 . 571This prefix also sets 572.Va rc_quiet Ns = Ns Li YES . 573.Em Note : 574.Va rc_quiet 575is not intended to completely mask all debug and warning messages, 576but only certain small classes of them. 577.El 578.Pp 579.Ic run_rc_command 580uses the following shell variables to control its behaviour. 581Unless otherwise stated, these are optional. 582.Bl -tag -width ".Va procname" -offset indent 583.It Va name 584The name of this script. 585This is not optional. 586.It Va rcvar 587The value of 588.Va rcvar 589is checked with 590.Ic checkyesno 591to determine if this method should be run. 592.It Va command 593Full path to the command. 594Not required if 595.Ar argument Ns Va _cmd 596is defined for each supported keyword. 597Can be overridden by 598.Va ${name}_program . 599.It Va command_args 600Optional arguments and/or shell directives for 601.Va command . 602.It Va command_interpreter 603.Va command 604is started with: 605.Pp 606.Dl "#! command_interpreter [...]" 607.Pp 608which results in its 609.Xr ps 1 610command being: 611.Pp 612.Dl "command_interpreter [...] command" 613.Pp 614so use that string to find the PID(s) of the running command 615rather than 616.Va command . 617.It Va extra_commands 618Extra commands/keywords/arguments supported. 619.It Va pidfile 620Path to PID file. 621Used to determine the PID(s) of the running command. 622If 623.Va pidfile 624is set, use: 625.Pp 626.Dl "check_pidfile $pidfile $procname" 627.Pp 628to find the PID. 629Otherwise, if 630.Va command 631is set, use: 632.Pp 633.Dl "check_process $procname" 634.Pp 635to find the PID. 636.It Va procname 637Process name to check for. 638Defaults to the value of 639.Va command . 640.It Va required_dirs 641Check for the existence of the listed directories 642before running the 643.Cm start 644method. 645The list is checked before running 646.Va start_precmd . 647.It Va required_files 648Check for the readability of the listed files 649before running the 650.Cm start 651method. 652The list is checked before running 653.Va start_precmd . 654.It Va required_modules 655Ensure that the listed kernel modules are loaded 656before running the 657.Cm start 658method. 659The list is checked after running 660.Va start_precmd . 661This is done after invoking the commands from 662.Va start_precmd 663so that the missing modules are not loaded in vain 664if the preliminary commands indicate a error condition. 665A word in the list can have an optional 666.Dq Li \&: Ns Ar modname 667or 668.Dq Li ~ Ns Ar pattern 669suffix. 670The 671.Ar modname 672or 673.Ar pattern 674parameter is passed to 675.Ic load_kld 676through a 677.Fl m 678or 679.Fl e 680option, respectively. 681See the description of 682.Ic load_kld 683in this document for details. 684.It Va required_vars 685Perform 686.Ic checkyesno 687on each of the list variables 688before running the 689.Cm start 690method. 691The list is checked after running 692.Va start_precmd . 693.It Va ${name}_chdir 694Directory to 695.Ic cd 696to before running 697.Va command , 698if 699.Va ${name}_chroot 700is not provided. 701.It Va ${name}_chroot 702Directory to 703.Xr chroot 8 704to before running 705.Va command . 706Only supported after 707.Pa /usr 708is mounted. 709.It Va ${name}_env 710A list of environment variables to run 711.Va command 712with. 713Those variables will be passed as arguments to the 714.Xr env 1 715utility unless 716.Ar argument Ns Va _cmd 717is defined. 718In that case the contents of 719.Va ${name}_env 720will be exported via the 721.Xr export 1 722builtin of 723.Xr sh 1 , 724which puts some limitations on the names of variables 725(e.g., a variable name may not start with a digit). 726.It Va ${name}_env_file 727A file to source for environmental variables to run 728.Va command 729with. 730.Em Note : 731all the variables which are being assigned in this file are going 732to be exported into the environment of 733.Va command . 734.It Va ${name}_fib 735FIB 736.Pa Routing Table 737number to run 738.Va command 739with. 740See 741.Xr setfib 1 742for more details. 743.It Va ${name}_flags 744Arguments to call 745.Va command 746with. 747This is usually set in 748.Xr rc.conf 5 , 749and not in the 750.Xr rc.d 8 751script. 752The environment variable 753.Sq Ev flags 754can be used to override this. 755.It Va ${name}_nice 756.Xr nice 1 757level to run 758.Va command 759as. 760Only supported after 761.Pa /usr 762is mounted. 763.It Va ${name}_limits 764Resource limits to apply to 765.Va command . 766This will be passed as arguments to the 767.Xr limits 1 768utility. 769By default, the resource limits are based on the login class defined in 770.Va ${name}_login_class . 771.It Va ${name}_login_class 772Login class to use with 773.Va ${name}_limits . 774Defaults to 775.Dq Li daemon . 776.It Va ${name}_oomprotect 777.Xr protect 1 778.Va command 779from being killed when swap space is exhausted. 780If 781.Dq Li YES 782is used, no child processes are protected. 783If 784.Dq Li ALL , 785protect all child processes. 786.It Va ${name}_program 787Full path to the command. 788Overrides 789.Va command 790if both are set, but has no effect if 791.Va command 792is unset. 793As a rule, 794.Va command 795should be set in the script while 796.Va ${name}_program 797should be set in 798.Xr rc.conf 5 . 799.It Va ${name}_user 800User to run 801.Va command 802as, using 803.Xr chroot 8 804if 805.Va ${name}_chroot 806is set, otherwise 807uses 808.Xr su 1 . 809Only supported after 810.Pa /usr 811is mounted. 812.It Va ${name}_group 813Group to run the chrooted 814.Va command 815as. 816.It Va ${name}_groups 817Comma separated list of supplementary groups to run the chrooted 818.Va command 819with. 820.It Va ${name}_prepend 821Commands to be prepended to 822.Va command . 823This is a generic version of 824.Va ${name}_env , 825.Va ${name}_fib , 826or 827.Va ${name}_nice . 828.It Va ${name}_setup 829Optional command to be run during 830.Cm start , 831.Cm restart , 832and 833.Cm reload 834prior to the respective 835.Ar argument Ns Va _precmd . 836If the command fails for any reason it will output a warning, 837but execution will continue. 838.It Ar argument Ns Va _cmd 839Shell commands which override the default method for 840.Ar argument . 841.It Ar argument Ns Va _precmd 842Shell commands to run just before running 843.Ar argument Ns Va _cmd 844or the default method for 845.Ar argument . 846If this returns a non-zero exit code, the main method is not performed. 847If the default method is being executed, this check is performed after 848the 849.Va required_* 850checks and process (non-)existence checks. 851.It Ar argument Ns Va _postcmd 852Shell commands to run if running 853.Ar argument Ns Va _cmd 854or the default method for 855.Ar argument 856returned a zero exit code. 857.It Va sig_stop 858Signal to send the processes to stop in the default 859.Cm stop 860method. 861Defaults to 862.Dv SIGTERM . 863.It Va sig_reload 864Signal to send the processes to reload in the default 865.Cm reload 866method. 867Defaults to 868.Dv SIGHUP . 869.El 870.Pp 871For a given method 872.Ar argument , 873if 874.Ar argument Ns Va _cmd 875is not defined, then a default method is provided by 876.Ic run_rc_command : 877.Bl -tag -width ".Sy Argument" -offset indent 878.It Sy Argument 879.Sy Default method 880.It Cm start 881If 882.Va command 883is not running and 884.Ic checkyesno Va rcvar 885succeeds, start 886.Va command . 887.It Cm stop 888Determine the PIDs of 889.Va command 890with 891.Ic check_pidfile 892or 893.Ic check_process 894(as appropriate), 895.Ic kill Va sig_stop 896those PIDs, and run 897.Ic wait_for_pids 898on those PIDs. 899.It Cm reload 900Similar to 901.Cm stop , 902except that it uses 903.Va sig_reload 904instead, and does not run 905.Ic wait_for_pids . 906Another difference from 907.Cm stop 908is that 909.Cm reload 910is not provided by default. 911It can be enabled via 912.Va extra_commands 913if appropriate: 914.Pp 915.Dl "extra_commands=reload" 916.It Cm restart 917Runs the 918.Cm stop 919method, then the 920.Cm start 921method. 922.It Cm status 923Show the PID of 924.Va command , 925or some other script specific status operation. 926.It Cm poll 927Wait for 928.Va command 929to exit. 930.It Cm rcvar 931Display which 932.Xr rc.conf 5 933variable is used (if any). 934This method always works, even if the appropriate 935.Xr rc.conf 5 936variable is set to 937.Dq Li NO . 938.El 939.Pp 940The following variables are available to the methods 941(such as 942.Ar argument Ns Va _cmd ) 943as well as after 944.Ic run_rc_command 945has completed: 946.Bl -tag -width ".Va rc_service" -offset indent 947.It Va rc_arg 948Argument provided to 949.Ic run_rc_command , 950after fast and force processing has been performed. 951.It Va rc_flags 952Flags to start the default command with. 953Defaults to 954.Va ${name}_flags , 955unless overridden by the environment variable 956.Sq Ev flags . 957This variable may be changed by the 958.Ar argument Ns Va _precmd 959method. 960.It Va rc_service 961Path to the service script being executed, in case it needs to re-invoke itself. 962.It Va rc_pid 963PID of 964.Va command 965(if appropriate). 966.It Va rc_fast 967Not empty if 968.Dq Li fast 969prefix was used. 970.It Va rc_force 971Not empty if 972.Dq Li force 973prefix was used. 974.El 975.It Ic run_rc_script Ar file argument 976Start the script 977.Ar file 978with an argument of 979.Ar argument , 980and handle the return value from the script. 981.Pp 982Various shell variables are unset before 983.Ar file 984is started: 985.Bd -ragged -offset indent 986.Va name , 987.Va command , 988.Va command_args , 989.Va command_interpreter , 990.Va extra_commands , 991.Va pidfile , 992.Va rcvar , 993.Va required_dirs , 994.Va required_files , 995.Va required_vars , 996.Ar argument Ns Va _cmd , 997.Ar argument Ns Va _precmd . 998.Ar argument Ns Va _postcmd . 999.Ed 1000.Pp 1001Call 1002.Ic rc_trace 1003to indicate that 1004.Ar file 1005is to be run. 1006.Pp 1007However, if 1008.Ic is_verified Ar file 1009fails, just return. 1010.Pp 1011.Ic DebugOn 1012will be called with tags derrived from 1013.Va name 1014and 1015.Va rc_arg 1016to enable tracing if any of those tags appear in 1017.Va DEBUG_SH . 1018.Pp 1019The startup behaviour of 1020.Ar file 1021depends upon the following checks: 1022.Bl -enum 1023.It 1024If 1025.Ar file 1026ends in 1027.Pa .sh , 1028it is sourced into the current shell. 1029.It 1030If 1031.Ar file 1032appears to be a backup or scratch file 1033(e.g., with a suffix of 1034.Pa ~ , # , .OLD , 1035or 1036.Pa .orig ) , 1037ignore it. 1038.It 1039If 1040.Ar file 1041is not executable, ignore it. 1042.It 1043If the 1044.Xr rc.conf 5 1045variable 1046.Va rc_fast_and_loose 1047is empty, 1048source 1049.Ar file 1050in a sub shell, 1051otherwise source 1052.Ar file 1053into the current shell. 1054.El 1055.It Ic run_rc_scripts Oo options Oc file ... 1056Call 1057.Ic run_rc_script 1058for each 1059.Ar file , 1060unless it is already recorded as having been run. 1061.Pp 1062The 1063.Ar options 1064are: 1065.Bl -tag -width "--break break" 1066.It Ic --arg Ar arg 1067Pass 1068.Ar arg 1069to 1070.Ic run_rc_script , 1071default is 1072.Ar _boot 1073set by 1074.Xr rc 8 . 1075.It Ic --break Ar break 1076Stop processing if any 1077.Ar file 1078matches any 1079.Ar break 1080.El 1081.It Ic safe_dot Ar file ... 1082Used by 1083.Ic sdot 1084when 1085.Xr mac_veriexec 4 1086is active and 1087.Ar file 1088is not verified. 1089.Pp 1090This function limits the input from 1091.Ar file 1092to simple variable assignments with any 1093non-alphanumeric characters replaced with 1094.Ql _ . 1095.It Ic sdot Ar file ... 1096For reading in configuration files. 1097Skip files that do not exist or are empty. 1098Try using 1099.Ic vdot 1100and if that fails (the file is unverified) 1101fall back to using 1102.Ic safe_dot . 1103.It Ic startmsg Oo Fl n Oc Ar message 1104Display a start message to 1105.Va stdout . 1106It should be used instead of 1107.Xr echo 1 . 1108The display of this output can be turned off if the 1109.Xr rc.conf 5 1110variable 1111.Va rc_startmsgs 1112is set to 1113.Dq Li NO . 1114.It Ic stop_boot Op Ar always 1115Prevent booting to multiuser mode. 1116If the 1117.Va autoboot 1118variable is set to 1119.Ql yes 1120(see 1121.Xr rc 8 1122to learn more about 1123.Va autoboot ) , 1124or 1125.Ic checkyesno Ar always 1126indicates a truth value, then a 1127.Dv SIGTERM 1128signal is sent to the parent 1129process, which is assumed to be 1130.Xr rc 8 . 1131Otherwise, the shell exits with a non-zero status. 1132.It Ic vdot Ar file ... 1133For reading in only verified files. 1134.Pp 1135Ensure shell 1136.Li verify 1137option is on. 1138This option is only meaningful when 1139.Xr mac_veriexec 4 1140is active, 1141otherwise this function is effectively the same as 1142.Ic dot . 1143.Pp 1144Read in each 1145.Ar file 1146if it exists and 1147.Ic is_verfied Ar file 1148is successful, otherwise set return code to 80 (EAUTH). 1149.Pp 1150Restore previous state of the 1151.Li verify 1152option. 1153.It Ic wait_for_pids Op Ar pid ... 1154Wait until all of the provided 1155.Ar pids 1156do not exist any more, printing the list of outstanding 1157.Ar pids 1158every two seconds. 1159.It Ic warn Ar message 1160Display a warning message to 1161.Va stderr 1162and log it to the system log 1163using 1164.Xr logger 1 . 1165The warning message consists of the script name 1166(from 1167.Va $0 ) , 1168followed by 1169.Dq Li ": WARNING: " , 1170and then 1171.Ar message . 1172.El 1173.Sh FILES 1174.Bl -tag -width ".Pa /etc/rc.subr" -compact 1175.It Pa /etc/rc.subr 1176The 1177.Nm 1178file resides in 1179.Pa /etc . 1180.El 1181.Sh SEE ALSO 1182.Xr debug.sh 8 , 1183.Xr rc.conf 5 , 1184.Xr rc 8 1185.Sh HISTORY 1186The 1187.Nm 1188script 1189appeared in 1190.Nx 1.3 . 1191The 1192.Xr rc.d 8 1193support functions appeared in 1194.Nx 1.5 . 1195The 1196.Nm 1197script 1198first appeared in 1199.Fx 5.0 . 1200