1.\" $NetBSD: rc.subr.8,v 1.9 2002/07/08 16:14:55 atatat Exp $ 2.\" $FreeBSD$ 3.\" 4.\" Copyright (c) 2002 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" This code is derived from software contributed to The NetBSD Foundation 8.\" by Luke Mewburn. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. All advertising materials mentioning features or use of this software 19.\" must display the following acknowledgement: 20.\" This product includes software developed by the NetBSD 21.\" Foundation, Inc. and its contributors. 22.\" 4. Neither the name of The NetBSD Foundation nor the names of its 23.\" contributors may be used to endorse or promote products derived 24.\" from this software without specific prior written permission. 25.\" 26.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36.\" POSSIBILITY OF SUCH DAMAGE. 37.\" 38.Dd April 18, 2002 39.Dt RC.SUBR 8 40.Os 41.Sh NAME 42.Nm rc.subr 43.Nd functions used by system shell scripts 44.Sh SYNOPSIS 45.Bl -item 46.It 47.Li . /etc/rc.subr 48.It 49.Ic backup_file Ar action Ar file Ar current Ar backup 50.It 51.Ic checkyesno Ar var 52.It 53.Ic check_pidfile Ar pidfile Ar procname Op Ar interpreter 54.It 55.Ic check_process Ar procname Op Ar interpreter 56.It 57.Ic debug Ar message 58.It 59.Ic err Ar exitval Ar message 60.It 61.Ic force_depend Ar name 62.It 63.Ic info Ar message 64.It 65.Ic load_rc_config Ar command 66.It 67.Ic mount_critical_filesystems Ar type 68.It 69.Ic rc_usage Ar command Op Ar ... 70.It 71.Ic reverse_list Ar item Op Ar ... 72.It 73.Ic run_rc_command Ar argument 74.It 75.Ic run_rc_script Ar file Ar argument 76.It 77.Ic set_rcvar Op Ar base 78.It 79.Ic wait_for_pids Op Ar pid Op Ar ... 80.It 81.Ic warn Ar message 82.El 83.Sh DESCRIPTION 84.Nm 85contains commonly used shell script functions and variable 86definitions which are used by various scripts such as 87.Xr rc 8 . 88Scripts required by ports in 89.Pa /usr/local/etc/rc.d 90will also eventually 91be rewritten to make use of it. 92.Pp 93The 94.Nm 95functions were mostly imported from 96.Nx 97and it is intended that they remain synced between the 98two projects. With that in mind there are several variable 99defenitions that can help in this regard. They are: 100.Bl -tag -width 4n 101.It Ic OSTYPE 102Its value will be either 103.Fx 104or 105.Nx , 106depending on which OS it is running on. 107.It Ic SYSCTL 108The path to the 109.Xr sysctl 8 110command. 111.It Ic SYSCTL_N 112The path and argument list to display only the 113.Xr sysctl 8 114values instead of a name=value pair. 115.It Ic SYSCTL_W 116The path and argument to write or modify 117.Xr sysctl 8 118values. 119.El 120.Pp 121The 122.Nm 123functions are accessed by sourcing 124.Pa /etc/rc.subr 125into the current shell. 126.Pp 127The following shell functions are available: 128.Bl -tag -width 4n 129.It Xo 130.Ic backup_file Ar action Ar file Ar current Ar backup 131.Xc 132Make a backup copy of 133.Ar file 134into 135.Ar current . 136If the 137.Xr rc.conf 5 138variable 139.Sy backup_uses_rcs 140is 141.Sq YES , 142use 143.Xr rcs 1 144to archive the previous version of 145.Ar current , 146otherwise save the previous version of 147.Ar current 148as 149.Ar backup . 150.Pp 151.Ar action 152may be one of the following: 153.Bl -tag -width remove 154.It Sy add 155.Ar file 156is now being backed up by or possibly re-entered into this backup mechanism. 157.Ar current 158is created, and if necessary, the 159.Xr rcs 1 160files are created as well. 161.It Sy update 162.Ar file 163has changed and needs to be backed up. 164If 165.Ar current 166exists, it is copied to 167.Ar backup 168or checked into 169.Xr rcs 1 170(if the repository file is old), 171and then 172.Ar file 173is copied to 174.Ar current . 175.It Sy remove 176.Ar file 177is no longer being tracked by this backup mechanism. 178If 179.Xr rcs 1 180is being used, an empty file is checked in and 181.Ar current 182is removed, 183otherwise 184.Ar current 185is moved to 186.Ar backup . 187.El 188.It Ic checkyesno Ar var 189Return 0 if 190.Ar var 191is defined to 192.Sq YES , 193.Sq TRUE , 194.Sq ON , 195or 196.Sq 1 . 197Return 1 if 198.Ar var 199is defined to 200.Sq NO , 201.Sq FALSE , 202.Sq OFF , 203or 204.Sq 0 . 205Otherwise, warn that 206.Ar var 207is not set correctly. 208The values are case insensitive. 209.It Xo 210.Ic check_pidfile 211.Ar pidfile 212.Ar procname 213.Op Ar interpreter 214.Xc 215Parses the first word of the first line of 216.Ar pidfile 217for a PID, and ensures that the process with that PID 218is running and its first argument matches 219.Ar procname . 220Prints the matching PID if successfull, otherwise nothing. 221If 222.Ar interpreter 223is provided, parse the first line of 224.Ar procname , 225ensure that the line is of the form 226.Dl #! interpreter [...] 227and use 228.Ar interpreter 229with its optional arguments and 230.Ar procname 231appended as the process string to search for. 232.It Ic check_process Ar procname Op Ar interpreter 233Prints the PIDs of any processes that are running with a first 234argument that matches 235.Ar procname . 236.Ar interpreter 237is handled as per 238.Ic check_pidfile . 239.It Ic debug Ar message 240Display a debugging message to 241.Em stderr , 242log it to the system log using 243.Xr logger 1 , 244and 245return to the caller. 246The error message consists of the sript name 247(from 248.Sy $0 ) , 249followed by 250.Dq ": DEBUG: " , 251and then 252.Ar message . 253This function is intended to be used by developers 254as an aid to debuging scripts. It can be turned on or off 255by the 256.Xr rc.conf 5 257variable 258.Em rc_debug . 259.It Ic err Ar exitval Ar message 260Display an error message to 261.Em stderr , 262log it to the system log 263using 264.Xr logger 1 , 265and 266.Cm exit 267with an exit value of 268.Ar exitval . 269The error message consists of the script name 270(from 271.Sy $0 ) , 272followed by 273.Dq ": ERROR: " , 274and then 275.Ar message . 276.It Ic force_depend name 277Output an advisory message and force the 278.Ar name 279service to start. The 280.Ar name 281argument is the 282.Xr basename 1 , 283component of the path to the script, usually 284.Em /etc/rc.d/name . 285If the script fails for any reason it will output a warning 286and return with a return value of 1. If it was successful 287it will return 0. 288.It Ic info Ar message 289Display an informational message to 290.Em stdout , 291and log it to the system log using 292.Xr logger 1 . 293The message consists of the script name 294(from 295.Sy $0 ) , 296followed by 297.Dq ": INFO: " , 298and then 299.Ar mesage . 300The display of this informational output can be 301turned on or off by the 302.Xr rc.conf 5 303variable 304.Em rc_info . 305.It Ic load_rc_config Ar command 306Source in the configuration files for 307.Ar command . 308First, 309.Pa /etc/rc.conf 310is sourced if it has not yet been read in. 311Then, 312.Pa /etc/rc.conf.d/ Ns Ar command 313is sourced if it is an existing file. 314The latter may also contain other variable assignments to override 315.Ic run_rc_command 316arguments defined by the calling script, to provide an easy 317mechanism for an administrator to override the behaviour of a given 318.Xr rc.d 8 319script without requiring the editing of that script. 320.It Ic mount_critical_filesystems Ar type 321Go through a list of critical file systems, 322as found in the 323.Xr rc.conf 5 324variable 325.Sy critical_filesystems_ Ns Ar type , 326mounting each one that 327is not currently mounted. 328.It Ic rc_usage Ar command Op Ar ... 329Print a usage message for 330.Sy $0 , 331with 332.Ar commands 333being the list of valid arguments 334prefixed by 335.Dq "[fast|force]" . 336.It Ic reverse_list Ar item Op Ar ... 337Print the list of 338.Ar items 339in reverse order. 340.It Ic run_rc_command Ar argument 341Run the 342.Ar argument 343method for the current 344.Xr rc.d 8 345script, based on the settings of various shell variables. 346.Ic run_rc_command 347is extremely flexible, and allows fully functional 348.Xr rc.d 8 349scripts to be implemented in a small amount of shell code. 350.Pp 351.Ar argument 352is searched for in the list of supported commands, which may be one 353of: 354.Dl start stop restart rcvar 355as well as any word listed in the optional variable 356.Sy extra_commands . 357If 358.Sy pidfile 359or 360.Sy procname 361is set, also allow: 362.Dl status poll 363.Pp 364.Ar argument 365may have one of the following prefixes which alters its operation: 366.Bl -tag -width "Prefix" -offset indent -compact 367.It Sy Prefix 368.Sy Operation 369.It Li fast 370Skip the check for an existing running process, 371and sets 372.Sy rc_fast=YES . 373.It Li force 374Skip the checks for 375.Sy rcvar 376being set to yes, 377and sets 378.Sy rc_force=YES . 379This ignores 380.Ar argument Ns Sy _precmd 381returning non-zero, and ignores any of the 382.Sy required_* 383tests failing . 384.El 385.Pp 386.Ic run_rc_command 387uses the following shell variables to control its behaviour. 388Unless otherwise stated, these are optional. 389.Bl -tag -width procname -offset indent 390.It Sy name 391The name of this script. 392This is not optional. 393.It Sy rcvar 394The value of 395.Sy rcvar 396is checked with 397.Ic checkyesno 398to determine if this method should be run. 399.It Sy command 400Full path to the command. 401Not required if 402.Ar argument Ns Sy _cmd 403is defined for each supported keyword. 404.It Sy command_args 405Optional arguments and/or shell directives for 406.Sy command . 407.It Sy command_interpreter 408.Sy command 409is started with 410.Dl #! command_interpreter [...] 411which results in its 412.Xr ps 1 413command being 414.Dl command_interpreter [...] command 415so use that string to find the PID(s) of the running command 416rather than 417.Ql command . 418.It Sy extra_commands 419Extra commands/keywords/arguments supported. 420.It Sy pidfile 421Path to pid file. 422Used to determine the PID(s) of the running command. 423If 424.Sy pidfile 425is set, use 426.Dl check_pidfile $pidfile $procname 427to find the PID. 428Otherwise, if 429.Sy command 430is set, use 431.Dl check_process $procname 432to find the PID. 433.It Sy procname 434Process name to check for. 435Defaults to the value of 436.Sy command . 437.It Sy required_dirs 438Check for the existence of the listed directories 439before running the default start method. 440.It Sy required_files 441Check for the readability of the listed files 442before running the default start method. 443.It Sy required_vars 444Perform 445.Ic checkyesno 446on each of the list variables 447before running the default start method. 448.It Sy ${name}_chdir 449Directory to 450.Ic cd 451to before running 452.Sy command , 453if 454.Sy ${name}_chroot 455is not provided. 456.It Sy ${name}_chroot 457Directory to 458.Xr chroot 8 459to before running 460.Sy command . 461Only supported after 462.Pa /usr 463is mounted. 464.It Sy ${name}_flags 465Arguments to call 466.Sy command 467with. 468This is usually set in 469.Xr rc.conf 5 , 470and not in the 471.Xr rc.d 8 472script. 473The environment variable 474.Sq Ev flags 475can be used to override this. 476.It Sy ${name}_nice 477.Xr nice 1 478level to run 479.Sy command 480as. 481Only supported after 482.Pa /usr 483is mounted. 484.It Sy ${name}_user 485User to run 486.Sy command 487as, using 488.Xr chroot 8 . 489if 490.Sy ${name}_chroot 491is set, otherwise 492uses 493.Xr su 1 . 494Only supported after 495.Pa /usr 496is mounted. 497.It Sy ${name}_group 498Group to run the chrooted 499.Sy command 500as. 501.It Sy ${name}_groups 502Comma separated list of supplementary groups to run the chrooted 503.Sy command 504with. 505.It Sy ${name}_systrace 506Flags passed to 507.Xr systrace 1 , 508if it is used. 509Setting this variable enables systracing 510of the given program. The use of 511.Sq -a 512is 513recommended so that the boot process is not 514stalled. In order to pass no flags to 515systrace, set this variable to 516.Sq -- . 517.It Ar argument Ns Sy _cmd 518Shell commands which override the default method for 519.Ar argument . 520.It Ar argument Ns Sy _precmd 521Shell commands to run just before running 522.Ar argument Ns Sy _cmd 523or the default method for 524.Ar argument . 525If this returns a non-zero exit code, the main method is not performed. 526If the default method is being executed, this check is performed after 527the 528.Sy required_* 529checks and process (non-)existence checks. 530.It Ar argument Ns Sy _postcmd 531Shell commands to run if running 532.Ar argument Ns Sy _cmd 533or the default method for 534.Ar argument 535returned a zero exit code. 536.It Sy sig_stop 537Signal to send the processes to stop in the default 538.Sy stop 539method. 540Defaults to 541.Dv SIGTERM . 542.It Sy sig_reload 543Signal to send the processes to reload in the default 544.Sy reload 545method. 546Defaults to 547.Dv SIGHUP . 548.El 549.Pp 550For a given method 551.Ar argument , 552if 553.Ar argument Ns Sy _cmd 554is not defined, then a default method is provided by 555.Sy run_rc_command : 556.Bl -tag -width "argument" -offset indent 557.It Sy Argument 558.Sy Default method 559.It Sy start 560If 561.Sy command 562is not running and 563.Ic checkyesno Sy rcvar 564succeeds, start 565.Sy command . 566.It Sy stop 567Determine the PIDs of 568.Sy command 569with 570.Ic check_pidfile 571or 572.Ic check_process 573(as appropriate), 574.Ic kill Sy sig_stop 575those PIDs, and run 576.Ic wait_for_pids 577on those PIDs. 578.It Sy reload 579Similar to 580.Sy stop , 581except that it uses 582.Sy sig_reload 583instead, and doesn't run 584.Ic wait_for_pids . 585.It Sy restart 586Runs the 587.Sy stop 588method, then the 589.Sy start 590method. 591.It Sy status 592Show the PID of 593.Sy command , 594or some other script specific status operation. 595.It Sy poll 596Wait for 597.Sy command 598to exit. 599.It Sy rcvar 600Display which 601.Xr rc.conf 5 602variable is used (if any). 603This method always works, even if the appropriate 604.Xr rc.conf 5 605variable is set to 606.Sq NO . 607.El 608.Pp 609The following variables are available to the methods 610(such as 611.Ar argument Ns Sy _cmd ) 612as well as after 613.Ic run_rc_command 614has completed: 615.Bl -tag -width "rc_flags" -offset indent 616.It Sy rc_arg 617Argument provided to 618.Sy run_rc_command , 619after fast and force processing has been performed. 620.It Sy rc_flags 621Flasg to start the default command with. 622Defaults to 623.Sy ${name}_flags , 624unless overridden by the environment variable 625.Sq Ev flags . 626This variable may be changed by the 627.Ar argument Ns Sy _precmd 628method. 629.It Sy rc_pid 630PID of 631.Sy command 632(if appropriate). 633.It Sy rc_fast 634Not empty if 635.Dq fast 636prefix was used. 637.It Sy rc_force 638Not empty if 639.Dq force 640prefix was used. 641.El 642.It Ic run_rc_script Ar file Ar argument 643Start the script 644.Ar file 645with an argument of 646.Ar argument , 647and handle the return value from the script. 648.Pp 649Various shell variables are unset before 650.Ar file 651is started: 652.Bd -ragged -offset indent 653.Sy name , 654.Sy command , 655.Sy command_args , 656.Sy command_interpreter , 657.Sy extra_commands , 658.Sy pidfile , 659.Sy rcvar , 660.Sy required_dirs , 661.Sy required_files , 662.Sy required_vars , 663.Ar argument Ns Sy _cmd , 664.Ar argument Ns Sy _precmd . 665.Ar argument Ns Sy _postcmd . 666.Ed 667.Pp 668The startup behaviour of 669.Ar file 670depends upon the following checks: 671.Bl -enum 672.It 673If 674.Ar file 675ends in 676.Pa .sh , 677it is sourced into the current shell. 678.It 679If 680.Ar file 681appears to be a backup or scratch file 682(e.g., with a suffix of 683.Sq ~ , 684.Sq # , 685.Sq .OLD , 686or 687.Sq .orig ) , 688ignore it. 689.It 690If 691.Ar file 692is not executable, ignore it. 693.It 694If the 695.Xr rc.conf 5 696variable 697.Sy rc_fast_and_loose 698is empty, 699source 700.Ar file 701in a sub shell, 702otherwise source 703.Ar file 704into the current shell. 705.El 706.It Ic set_rcvar Op Ar base 707Set the variable name required to start a service. In 708.Fx 709a daemon is usually controlled by an 710.Xr rc.conf 5 711variable consisting of a daemon's name postfixed by the string 712.Sy "_enable" . 713This is not the case in 714.Nx . 715When the following line is included in a script 716.Pp 717.Dl rcvar=`set_rcvar` 718.Pp 719This function will use the value of the 720.Sy $name 721variable, which should be defined by the calling script, to construct the appropriate 722.Xr rc.conf 5 723knob. If the 724.Ar base 725argument is set it will use 726.Ar base 727instead of 728.Sy $name . 729.It Ic wait_for_pids Op Ar pid Op Ar ... 730Wait until all of the provided 731.Ar pids 732don't exist any more, printing the list of outstanding 733.Ar pids 734every two seconds. 735.It Ic warn Ar message 736Display a warning message to 737.Em stderr 738and log it to the system log 739using 740.Xr logger 1 . 741The warning message consists of the script name 742(from 743.Sy $0 ) , 744followed by 745.Dq ": WARNING: " , 746and then 747.Ar message . 748.El 749.Sh FILES 750.Bl -tag -width /etc/rc.subr -compact 751.It Pa /etc/rc.subr 752The 753.Nm 754file resides in 755.Pa /etc . 756.El 757.Sh SEE ALSO 758.Xr rc.conf 5 , 759.Xr rc 8 760.Sh HISTORY 761.Nm 762appeared in 763.Nx 1.3 . 764The 765.Xr rc.d 8 766support functions appeared in 767.Nx 1.5 . 768.Nm 769first appeared in 770.Fx 5.0 . 771