1.\" $NetBSD: rc.subr.8,v 1.19 2008/04/30 13:10:57 martin 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 April 6, 2007 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 38.It 39.Li . /etc/rc.subr 40.It 41.Ic backup_file Ar action Ar file Ar current Ar backup 42.It 43.Ic checkyesno Ar var 44.It 45.Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter 46.It 47.Ic check_process Ar procname Op Ar interpreter 48.It 49.Ic err Ar exitval Ar message 50.It 51.Ic load_rc_config Ar command 52.It 53.Ic load_rc_config_var Ar command Ar var 54.It 55.Ic mount_critical_filesystems Ar type 56.It 57.Ic rc_usage Ar command Op Ar ... 58.It 59.Ic reverse_list Ar item Op Ar ... 60.It 61.Ic run_rc_command Ar argument 62.It 63.Ic run_rc_script Ar file Ar argument 64.It 65.Ic stop_boot 66.It 67.Ic wait_for_pids Op Ar pid Op Ar ... 68.It 69.Ic warn Ar message 70.El 71.Sh DESCRIPTION 72.Nm 73contains commonly used shell script functions which are used by 74various scripts such as 75.Xr rc 8 , 76and the periodic system services which are controlled by 77.Xr daily.conf 5 , 78.Xr monthly.conf 5 , 79.Xr security.conf 5 , 80and 81.Xr weekly.conf 5 . 82.Pp 83The 84.Nm 85functions are accessed by sourcing 86.Pa /etc/rc.subr 87into the current shell. 88.Pp 89The following shell functions are available: 90.Bl -tag -width 4n 91.It Xo 92.Ic backup_file Ar action Ar file Ar current Ar backup 93.Xc 94Make a backup copy of 95.Ar file 96into 97.Ar current . 98If the 99.Xr rc.conf 5 100variable 101.Sy backup_uses_rcs 102is 103.Sq YES , 104use 105.Xr rcs 1 106to archive the previous version of 107.Ar current , 108otherwise save the previous version of 109.Ar current 110as 111.Ar backup . 112.Pp 113.Ar action 114may be one of the following: 115.Bl -tag -width remove 116.It Sy add 117.Ar file 118is now being backed up by or possibly re-entered into this backup mechanism. 119.Ar current 120is created, and if necessary, the 121.Xr rcs 1 122files are created as well. 123.It Sy update 124.Ar file 125has changed and needs to be backed up. 126If 127.Ar current 128exists, it is copied to 129.Ar backup 130or checked into 131.Xr rcs 1 132(if the repository file is old), 133and then 134.Ar file 135is copied to 136.Ar current . 137.It Sy remove 138.Ar file 139is no longer being tracked by this backup mechanism. 140If 141.Xr rcs 1 142is being used, an empty file is checked in and 143.Ar current 144is removed, 145otherwise 146.Ar current 147is moved to 148.Ar backup . 149.El 150.It Ic checkyesno Ar var 151Return 0 if 152.Ar var 153is defined to 154.Sq YES , 155.Sq TRUE , 156.Sq ON , 157or 158.Sq 1 . 159Return 1 if 160.Ar var 161is defined to 162.Sq NO , 163.Sq FALSE , 164.Sq OFF , 165or 166.Sq 0 . 167Otherwise, warn that 168.Ar var 169is not set correctly. 170The values are case insensitive. 171.Pp 172Note that the warning message shown by this function when 173.Ar var 174is not set references a manual page where the user can find more information. 175Its name is picked up from the 176.Sy rcvar_manpage 177variable. 178.It Xo 179.Ic check_pidfile 180.Ar pidfile 181.Ar procname 182.Op Ar interpreter 183.Xc 184Parses the first word of the first line of 185.Ar pidfile 186for a PID, and ensures that the process with that PID 187is running and its first argument matches 188.Ar procname . 189Prints the matching PID if successful, otherwise nothing. 190If 191.Ar interpreter 192is provided, parse the first line of 193.Ar procname , 194ensure that the line is of the form 195.Dl #! interpreter [...] 196and use 197.Ar interpreter 198with its optional arguments and 199.Ar procname 200appended as the process string to search for. 201.It Ic check_process Ar procname Op Ar interpreter 202Prints the PIDs of any processes that are running with a first 203argument that matches 204.Ar procname . 205.Ar interpreter 206is handled as per 207.Ic check_pidfile . 208.It Ic err Ar exitval Ar message 209Display an error message to 210.Em stderr , 211log it to the system log 212using 213.Xr logger 1 , 214and 215.Cm exit 216with an exit value of 217.Ar exitval . 218The error message consists of the script name 219(from 220.Sy $0 ) , 221followed by 222.Dq ": ERROR: " , 223and then 224.Ar message . 225.It Ic load_rc_config Ar command 226Source in the 227.Xr rc.conf 5 228configuration files for 229.Ar command . 230First, 231.Pa /etc/rc.conf 232is sourced if it has not yet been read in. 233Then, 234.Pa /etc/rc.conf.d/ Ns Ar command 235is sourced if it is an existing file. 236The latter may also contain other variable assignments to override 237.Ic run_rc_command 238arguments defined by the calling script, to provide an easy 239mechanism for an administrator to override the behaviour of a given 240.Xr rc.d 8 241script without requiring the editing of that script. 242.It Ic load_rc_config_var Ar command Ar var 243Read the 244.Xr rc.conf 5 245variable 246.Ar var 247for 248.Ar command 249and set in the current shell, using 250.Ic load_rc_config 251in a sub-shell to prevent unwanted side effects from other variable 252assignments. 253.It Ic mount_critical_filesystems Ar type 254Go through a list of critical file systems, 255as found in the 256.Xr rc.conf 5 257variable 258.Sy critical_filesystems_ Ns Ar type , 259mounting each one that 260is not currently mounted. 261.It Ic rc_usage Ar command Op Ar ... 262Print a usage message for 263.Sy $0 , 264with 265.Ar commands 266being the list of valid arguments 267prefixed by 268.Dq "[fast|force|one]" . 269.It Ic reverse_list Ar item Op Ar ... 270Print the list of 271.Ar items 272in reverse order. 273.It Ic run_rc_command Ar argument 274Run the 275.Ar argument 276method for the current 277.Xr rc.d 8 278script, based on the settings of various shell variables. 279.Ic run_rc_command 280is extremely flexible, and allows fully functional 281.Xr rc.d 8 282scripts to be implemented in a small amount of shell code. 283.Pp 284.Ar argument 285is searched for in the list of supported commands, which may be one 286of: 287.Bl -tag -width restart -offset indent 288.It Sy start 289Start the service. 290This should check that the service is to be started as specified by 291.Xr rc.conf 5 . 292Also checks if the service is already running and refuses to start if 293it is. 294This latter check is not performed by standard 295.Nx 296scripts if the system is starting directly to multi-user mode, to 297speed up the boot process. 298.It Sy stop 299If the service is to be started as specified by 300.Xr rc.conf 5 , 301stop the service. 302This should check that the service is running and complain if it's not. 303.It Sy restart 304Perform a 305.Sy stop 306then a 307.Sy start . 308Defaults to displaying the process ID of the program (if running). 309.It Sy rcvar 310Display which 311.Xr rc.conf 5 312variables are used to control the startup of the service (if any). 313.El 314.Pp 315If 316.Sy pidfile 317or 318.Sy procname 319is set, also support: 320.Bl -tag -width restart -offset indent 321.It Sy poll 322Wait for the command to exit. 323.It Sy status 324Show the status of the process. 325.El 326.Pp 327Other supported commands are listed in the optional variable 328.Sy extra_commands . 329.Pp 330.Ar argument 331may have one of the following prefixes which alters its operation: 332.Bl -tag -width "Prefix" -offset indent 333.It Sy fast 334Skip the check for an existing running process, 335and sets 336.Sy rc_fast=YES . 337.It Sy force 338Skip the checks for 339.Sy rcvar 340being set to yes, 341and sets 342.Sy rc_force=YES . 343This ignores 344.Ar argument Ns Sy _precmd 345returning non-zero, and ignores any of the 346.Sy required_* 347tests failing, and always returns a zero exit status. 348.It Sy one 349Skip the checks for 350.Sy rcvar 351being set to yes, but performs all the other prerequisite tests. 352.El 353.Pp 354.Ic run_rc_command 355uses the following shell variables to control its behaviour. 356Unless otherwise stated, these are optional. 357.Bl -tag -width procname -offset indent 358.It Sy name 359The name of this script. 360This is not optional. 361.It Sy rcvar 362The value of 363.Sy rcvar 364is checked with 365.Ic checkyesno 366to determine if this method should be run. 367.It Sy rcvar_manpage 368The manual page containing information about 369.Sy rcvar . 370It will be part of the warning message shown when 371.Sy rcvar 372is undefined. 373Defaults to 374.Xr rc.conf 5 . 375.It Sy command 376Full path to the command. 377Not required if 378.Ar argument Ns Sy _cmd 379is defined for each supported keyword. 380.It Sy command_args 381Optional arguments and/or shell directives for 382.Sy command . 383.It Sy command_interpreter 384.Sy command 385is started with 386.Dl #! command_interpreter [...] 387which results in its 388.Xr ps 1 389command being 390.Dl command_interpreter [...] command 391so use that string to find the PID(s) of the running command 392rather than 393.Ql command . 394.It Sy extra_commands 395Extra commands/keywords/arguments supported. 396.It Sy pidfile 397Path to pid file. 398Used to determine the PID(s) of the running command. 399If 400.Sy pidfile 401is set, use 402.Dl check_pidfile $pidfile $procname 403to find the PID. 404Otherwise, if 405.Sy command 406is set, use 407.Dl check_process $procname 408to find the PID. 409.It Sy procname 410Process name to check for. 411Defaults to the value of 412.Sy command . 413.It Sy required_dirs 414Check for the existence of the listed directories 415before running the default start method. 416.It Sy required_files 417Check for the readability of the listed files 418before running the default start method. 419.It Sy required_vars 420Perform 421.Ic checkyesno 422on each of the list variables 423before running the default start method. 424.It Sy ${name}_chdir 425Directory to 426.Ic cd 427to before running 428.Sy command , 429if 430.Sy ${name}_chroot 431is not provided. 432.It Sy ${name}_chroot 433Directory to 434.Xr chroot 8 435to before running 436.Sy command . 437Only supported after 438.Pa /usr 439is mounted. 440.It Sy ${name}_flags 441Arguments to call 442.Sy command 443with. 444This is usually set in 445.Xr rc.conf 5 , 446and not in the 447.Xr rc.d 8 448script. 449The environment variable 450.Sq Ev flags 451can be used to override this. 452.It Sy ${name}_nice 453.Xr nice 1 454level to run 455.Sy command 456as. 457Only supported after 458.Pa /usr 459is mounted. 460.It Sy ${name}_user 461User to run 462.Sy command 463as, using 464.Xr chroot 8 . 465if 466.Sy ${name}_chroot 467is set, otherwise 468uses 469.Xr su 1 . 470Only supported after 471.Pa /usr 472is mounted. 473.It Sy ${name}_group 474Group to run the chrooted 475.Sy command 476as. 477.It Sy ${name}_groups 478Comma separated list of supplementary groups to run the chrooted 479.Sy command 480with. 481.It Ar argument Ns Sy _cmd 482Shell commands which override the default method for 483.Ar argument . 484.It Ar argument Ns Sy _precmd 485Shell commands to run just before running 486.Ar argument Ns Sy _cmd 487or the default method for 488.Ar argument . 489If this returns a non-zero exit code, the main method is not performed. 490If the default method is being executed, this check is performed after 491the 492.Sy required_* 493checks and process (non-)existence checks. 494.It Ar argument Ns Sy _postcmd 495Shell commands to run if running 496.Ar argument Ns Sy _cmd 497or the default method for 498.Ar argument 499returned a zero exit code. 500.It Sy sig_stop 501Signal to send the processes to stop in the default 502.Sy stop 503method. 504Defaults to 505.Dv SIGTERM . 506.It Sy sig_reload 507Signal to send the processes to reload in the default 508.Sy reload 509method. 510Defaults to 511.Dv SIGHUP . 512.El 513.Pp 514For a given method 515.Ar argument , 516if 517.Ar argument Ns Sy _cmd 518is not defined, then a default method is provided by 519.Sy run_rc_command : 520.Bl -tag -width "argument" -offset indent 521.It Sy Argument 522.Sy Default method 523.It Sy start 524If 525.Sy command 526is not running and 527.Ic checkyesno Sy rcvar 528succeeds, start 529.Sy command . 530.It Sy stop 531Determine the PIDs of 532.Sy command 533with 534.Ic check_pidfile 535or 536.Ic check_process 537(as appropriate), 538.Ic kill Sy sig_stop 539those PIDs, and run 540.Ic wait_for_pids 541on those PIDs. 542.It Sy reload 543Similar to 544.Sy stop , 545except that it uses 546.Sy sig_reload 547instead, and doesn't run 548.Ic wait_for_pids . 549.It Sy restart 550Runs the 551.Sy stop 552method, then the 553.Sy start 554method. 555.It Sy status 556Show the PID of 557.Sy command , 558or some other script specific status operation. 559.It Sy poll 560Wait for 561.Sy command 562to exit. 563.It Sy rcvar 564Display which 565.Xr rc.conf 5 566variable is used (if any). 567This method always works, even if the appropriate 568.Xr rc.conf 5 569variable is set to 570.Sq NO . 571.El 572.Pp 573The following variables are available to the methods 574(such as 575.Ar argument Ns Sy _cmd ) 576as well as after 577.Ic run_rc_command 578has completed: 579.Bl -tag -width "rc_flags" -offset indent 580.It Sy rc_arg 581Argument provided to 582.Sy run_rc_command , 583after fast and force processing has been performed. 584.It Sy rc_flags 585Flags to start the default command with. 586Defaults to 587.Sy ${name}_flags , 588unless overridden by the environment variable 589.Sq Ev flags . 590This variable may be changed by the 591.Ar argument Ns Sy _precmd 592method. 593.It Sy rc_pid 594PID of 595.Sy command 596(if appropriate). 597.It Sy rc_fast 598Not empty if 599.Dq fast 600prefix was used. 601.It Sy rc_force 602Not empty if 603.Dq force 604prefix was used. 605.El 606.It Ic run_rc_script Ar file Ar argument 607Start the script 608.Ar file 609with an argument of 610.Ar argument , 611and handle the return value from the script. 612.Pp 613Various shell variables are unset before 614.Ar file 615is started: 616.Bd -ragged -offset indent 617.Sy name , 618.Sy command , 619.Sy command_args , 620.Sy command_interpreter , 621.Sy extra_commands , 622.Sy pidfile , 623.Sy rcvar , 624.Sy required_dirs , 625.Sy required_files , 626.Sy required_vars , 627.Ar argument Ns Sy _cmd , 628.Ar argument Ns Sy _precmd . 629.Ar argument Ns Sy _postcmd . 630.Ed 631.Pp 632The startup behaviour of 633.Ar file 634depends upon the following checks: 635.Bl -enum 636.It 637If 638.Ar file 639ends in 640.Pa .sh , 641it is sourced into the current shell. 642.It 643If 644.Ar file 645appears to be a backup or scratch file 646(e.g., with a suffix of 647.Sq ~ , 648.Sq # , 649.Sq .OLD , 650or 651.Sq .orig ) , 652ignore it. 653.It 654If 655.Ar file 656is not executable, ignore it. 657.It 658If the 659.Xr rc.conf 5 660variable 661.Sy rc_fast_and_loose 662is empty, 663source 664.Ar file 665in a sub shell, 666otherwise source 667.Ar file 668into the current shell. 669.El 670.It Ic stop_boot 671Prevent booting to multiuser mode. 672If the 673.Sy autoboot 674variable is 675.Sq yes , 676then a 677.Sy SIGTERM 678signal is sent to the parent 679process (which is assumed to be 680.Xr rc 8 ) . 681Otherwise, the shell exits with status 682.Li 1 . 683.It Ic wait_for_pids Op Ar pid Op Ar ... 684Wait until all of the provided 685.Ar pids 686don't exist any more, printing the list of outstanding 687.Ar pids 688every two seconds. 689.It Ic warn Ar message 690Display a warning message to 691.Em stderr 692and log it to the system log 693using 694.Xr logger 1 . 695The warning message consists of the script name 696(from 697.Sy $0 ) , 698followed by 699.Dq ": WARNING: " , 700and then 701.Ar message . 702.El 703.Sh FILES 704.Bl -tag -width /etc/rc.subr -compact 705.It Pa /etc/rc.subr 706The 707.Nm 708file resides in 709.Pa /etc . 710.El 711.Sh SEE ALSO 712.Xr rc.conf 5 , 713.Xr rc 8 714.Sh HISTORY 715.Nm 716appeared in 717.Nx 1.3 . 718The 719.Xr rc.d 8 720support functions appeared in 721.Nx 1.5 . 722