rc.subr (164640) | rc.subr (165565) |
---|---|
1# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ | 1# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ |
2# $FreeBSD: head/etc/rc.subr 164640 2006-11-26 22:02:09Z flz $ | 2# $FreeBSD: head/etc/rc.subr 165565 2006-12-27 13:15:33Z yar $ |
3# 4# Copyright (c) 1997-2004 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 --- 392 unchanged lines hidden (view full) --- 403# If this completes with a non-zero exit code, 404# don't run ${rc_arg}_cmd. 405# 406# ${rc_arg}_postcmd n If set, run just after performing the 407# ${rc_arg}_cmd method, if that method 408# returned a zero exit code. 409# 410# required_dirs n If set, check for the existence of the given | 3# 4# Copyright (c) 1997-2004 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 --- 392 unchanged lines hidden (view full) --- 403# If this completes with a non-zero exit code, 404# don't run ${rc_arg}_cmd. 405# 406# ${rc_arg}_postcmd n If set, run just after performing the 407# ${rc_arg}_cmd method, if that method 408# returned a zero exit code. 409# 410# required_dirs n If set, check for the existence of the given |
411# directories before running the default 412# (re)start command. | 411# directories before running a (re)start command. |
413# 414# required_files n If set, check for the readability of the given | 412# 413# required_files n If set, check for the readability of the given |
415# files before running the default (re)start 416# command. | 414# files before running a (re)start command. |
417# | 415# |
416# required_modules n If set, ensure the given kernel modules are 417# loaded before running a (re)start command. 418# The check and possible loads are actually 419# done after start_precmd so that the modules 420# aren't loaded in vain, should the precmd 421# return a non-zero status to indicate a error. 422# If a word in the list looks like "foo:bar", 423# "foo" is the KLD file name and "bar" is the 424# module name. If a word looks like "foo~bar", 425# "foo" is the KLD file name and "bar" is a 426# egrep(1) pattern matching the module name. 427# Otherwise the module name is assumed to be 428# the same as the KLD file name, which is most 429# common. See load_kld(). 430# |
|
418# required_vars n If set, perform checkyesno on each of the 419# listed variables before running the default 420# (re)start command. 421# 422# Default behaviour for a given argument, if no override method is 423# provided: 424# 425# Argument Default behaviour --- 131 unchanged lines hidden (view full) --- 557 fi 558 559 eval $_pidcmd # determine the pid if necessary 560 561 for _elem in $_keywords; do 562 if [ "$_elem" != "$rc_arg" ]; then 563 continue 564 fi | 431# required_vars n If set, perform checkyesno on each of the 432# listed variables before running the default 433# (re)start command. 434# 435# Default behaviour for a given argument, if no override method is 436# provided: 437# 438# Argument Default behaviour --- 131 unchanged lines hidden (view full) --- 570 fi 571 572 eval $_pidcmd # determine the pid if necessary 573 574 for _elem in $_keywords; do 575 if [ "$_elem" != "$rc_arg" ]; then 576 continue 577 fi |
565 | |
566 # if there's a custom ${XXX_cmd}, 567 # run that instead of the default 568 # | 578 # if there's a custom ${XXX_cmd}, 579 # run that instead of the default 580 # |
569 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 570 _postcmd=\$${rc_arg}_postcmd 571 if [ -n "$_cmd" ]; then 572 # if the precmd failed and force 573 # isn't set, exit 574 # 575 if [ -n "$_precmd" ]; then 576 debug "run_rc_command: evaluating ${_precmd}()." 577 eval $_precmd $rc_extra_args 578 _return=$? 579 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 580 return 1 581 fi | 581 eval _cmd=\$${rc_arg}_cmd \ 582 _precmd=\$${rc_arg}_precmd \ 583 _postcmd=\$${rc_arg}_postcmd |
582 | 584 |
583 if [ -n "$_cmd" ]; then 584 debug "run_rc_command: evaluating ${_cmd}()." 585 eval $_cmd $rc_extra_args 586 _return=$? 587 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 588 return 1 589 fi 590 591 if [ -n "$_postcmd" ]; then 592 debug "run_rc_command: evaluating ${_postcmd}()." 593 eval $_postcmd $rc_extra_args 594 _return=$? 595 fi | 585 if [ -n "$_cmd" ]; then 586 _run_rc_precmd || return 1 587 _run_rc_doit "$_cmd $rc_extra_args" || return 1 588 _run_rc_postcmd |
596 return $_return 597 fi 598 599 case "$rc_arg" in # default operations... 600 601 status) | 589 return $_return 590 fi 591 592 case "$rc_arg" in # default operations... 593 594 status) |
595 _run_rc_precmd || return 1 |
|
602 if [ -n "$rc_pid" ]; then 603 echo "${name} is running as pid $rc_pid." 604 else 605 echo "${name} is not running." 606 return 1 607 fi | 596 if [ -n "$rc_pid" ]; then 597 echo "${name} is running as pid $rc_pid." 598 else 599 echo "${name} is not running." 600 return 1 601 fi |
602 _run_rc_postcmd |
|
608 ;; 609 610 start) 611 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 612 echo 1>&2 "${name} already running? (pid=$rc_pid)." 613 return 1 614 fi 615 616 if [ ! -x ${_chroot}${command} ]; then 617 warn "run_rc_command: cannot run $command" 618 return 1 619 fi 620 | 603 ;; 604 605 start) 606 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 607 echo 1>&2 "${name} already running? (pid=$rc_pid)." 608 return 1 609 fi 610 611 if [ ! -x ${_chroot}${command} ]; then 612 warn "run_rc_command: cannot run $command" 613 return 1 614 fi 615 |
621 # check for required variables, 622 # directories, and files 623 # 624 for _f in $required_vars; do 625 if ! checkyesno $_f; then 626 warn "\$${_f} is not enabled." 627 if [ -z "$rc_force" ]; then 628 return 1 629 fi 630 fi 631 done 632 for _f in $required_dirs; do 633 if [ ! -d "${_f}/." ]; then 634 warn "${_f} is not a directory." 635 if [ -z "$rc_force" ]; then 636 return 1 637 fi 638 fi 639 done 640 for _f in $required_files; do 641 if [ ! -r "${_f}" ]; then 642 warn "${_f} is not readable." 643 if [ -z "$rc_force" ]; then 644 return 1 645 fi 646 fi 647 done | 616 _run_rc_precmd || return 1 |
648 | 617 |
649 # if the precmd failed and force 650 # isn't set, exit 651 # 652 if [ -n "${_precmd}" ]; then 653 debug "run_rc_command: evaluating ${_precmd}()." 654 eval $_precmd 655 _return=$? 656 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 657 return 1 658 fi 659 | |
660 # setup the full command to run 661 # 662 echo "Starting ${name}." 663 if [ -n "$_chroot" ]; then 664 _doit="\ 665${_nice:+nice -n $_nice }\ 666chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 667$_chroot $command $rc_flags $command_args" --- 7 unchanged lines hidden (view full) --- 675 if [ -n "$_nice" ]; then 676 if [ -z "$_user" ]; then 677 _doit="sh -c \"$_doit\"" 678 fi 679 _doit="nice -n $_nice $_doit" 680 fi 681 fi 682 | 618 # setup the full command to run 619 # 620 echo "Starting ${name}." 621 if [ -n "$_chroot" ]; then 622 _doit="\ 623${_nice:+nice -n $_nice }\ 624chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 625$_chroot $command $rc_flags $command_args" --- 7 unchanged lines hidden (view full) --- 633 if [ -n "$_nice" ]; then 634 if [ -z "$_user" ]; then 635 _doit="sh -c \"$_doit\"" 636 fi 637 _doit="nice -n $_nice $_doit" 638 fi 639 fi 640 |
683 # run the full command; 684 # if the cmd failed and force 685 # isn't set, exit | 641 # run the full command |
686 # | 642 # |
687 debug "run_rc_command: _doit: $_doit" 688 eval $_doit 689 _return=$? 690 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 | 643 _run_rc_doit "$_doit" || return 1 |
691 692 # finally, run postcmd 693 # | 644 645 # finally, run postcmd 646 # |
694 if [ -n "${_postcmd}" ]; then 695 debug "run_rc_command: evaluating ${_postcmd}()." 696 eval $_postcmd 697 fi | 647 _run_rc_postcmd |
698 ;; 699 700 stop) 701 if [ -z "$rc_pid" ]; then 702 [ -n "$rc_fast" ] && return 0 | 648 ;; 649 650 stop) 651 if [ -z "$rc_pid" ]; then 652 [ -n "$rc_fast" ] && return 0 |
703 if [ -n "$pidfile" ]; then 704 echo 1>&2 \ 705 "${name} not running? (check $pidfile)." 706 else 707 echo 1>&2 "${name} not running?" 708 fi | 653 _run_rc_notrunning |
709 return 1 710 fi 711 | 654 return 1 655 fi 656 |
712 # if the precmd failed and force 713 # isn't set, exit 714 # 715 if [ -n "$_precmd" ]; then 716 eval $_precmd 717 _return=$? 718 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 719 return 1 720 fi | 657 _run_rc_precmd || return 1 |
721 722 # send the signal to stop 723 # 724 echo "Stopping ${name}." | 658 659 # send the signal to stop 660 # 661 echo "Stopping ${name}." |
725 _doit="kill -${sig_stop:-TERM} $rc_pid" 726 if [ -n "$_user" ]; then 727 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 728 fi | 662 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 663 _run_rc_doit "$_doit" || return 1 |
729 | 664 |
730 # if the stop cmd failed and force 731 # isn't set, exit 732 # 733 eval $_doit 734 _return=$? 735 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 736 | |
737 # wait for the command to exit, 738 # and run postcmd. 739 wait_for_pids $rc_pid | 665 # wait for the command to exit, 666 # and run postcmd. 667 wait_for_pids $rc_pid |
740 if [ -n "$_postcmd" ]; then 741 eval $_postcmd 742 _return=$? 743 fi | 668 669 _run_rc_postcmd |
744 ;; 745 746 reload) 747 if [ -z "$rc_pid" ]; then | 670 ;; 671 672 reload) 673 if [ -z "$rc_pid" ]; then |
748 if [ -n "$pidfile" ]; then 749 echo 1>&2 \ 750 "${name} not running? (check $pidfile)." 751 else 752 echo 1>&2 "${name} not running?" 753 fi | 674 _run_rc_notrunning |
754 return 1 755 fi | 675 return 1 676 fi |
756 echo "Reloading ${name} config files." 757 if [ -n "$_precmd" ]; then 758 eval $_precmd 759 _return=$? 760 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 761 return 1 762 fi 763 _doit="kill -${sig_reload:-HUP} $rc_pid" 764 if [ -n "$_user" ]; then 765 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 766 fi 767 eval $_doit 768 _return=$? 769 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 770 if [ -n "$_postcmd" ]; then 771 eval $_postcmd 772 _return=$? 773 fi | 677 678 _run_rc_precmd || return 1 679 680 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 681 _run_rc_doit "$_doit" || return 1 682 683 _run_rc_postcmd |
774 ;; 775 776 restart) | 684 ;; 685 686 restart) |
777 if [ -n "$_precmd" ]; then 778 eval $_precmd $rc_extra_args 779 _return=$? 780 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 781 return 1 782 fi | |
783 # prevent restart being called more 784 # than once by any given script 785 # 786 if ${_rc_restart_done:-false}; then 787 return 0 788 fi 789 _rc_restart_done=true 790 | 687 # prevent restart being called more 688 # than once by any given script 689 # 690 if ${_rc_restart_done:-false}; then 691 return 0 692 fi 693 _rc_restart_done=true 694 |
791 # run stop in a subshell to keep variables for start | 695 _run_rc_precmd || return 1 696 697 # run those in a subshell to keep global variables |
792 ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) | 698 ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) |
793 run_rc_command ${_rc_prefix}start $rc_extra_args | 699 ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 700 _return=$? 701 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 |
794 | 702 |
795 if [ -n "$_postcmd" ]; then 796 eval $_postcmd $rc_extra_args 797 _return=$? 798 fi | 703 _run_rc_postcmd |
799 ;; 800 801 poll) | 704 ;; 705 706 poll) |
707 _run_rc_precmd || return 1 |
|
802 if [ -n "$rc_pid" ]; then 803 wait_for_pids $rc_pid 804 fi | 708 if [ -n "$rc_pid" ]; then 709 wait_for_pids $rc_pid 710 fi |
711 _run_rc_postcmd |
|
805 ;; 806 807 rcvar) 808 echo "# $name" 809 if [ -n "$rcvar" ]; then 810 if checkyesno ${rcvar}; then 811 echo "${rcvar}=YES" 812 else --- 11 unchanged lines hidden (view full) --- 824 done 825 826 echo 1>&2 "$0: unknown directive '$rc_arg'." 827 rc_usage $_keywords 828 # not reached 829} 830 831# | 712 ;; 713 714 rcvar) 715 echo "# $name" 716 if [ -n "$rcvar" ]; then 717 if checkyesno ${rcvar}; then 718 echo "${rcvar}=YES" 719 else --- 11 unchanged lines hidden (view full) --- 731 done 732 733 echo 1>&2 "$0: unknown directive '$rc_arg'." 734 rc_usage $_keywords 735 # not reached 736} 737 738# |
739# Helper functions for run_rc_command: common code. 740# They use such global variables besides the exported rc_* ones: 741# 742# name R/W 743# ------------------ 744# _precmd R 745# _postcmd R 746# _return W 747# 748_run_rc_precmd() 749{ 750 check_required_before "$rc_arg" || return 1 751 752 if [ -n "$_precmd" ]; then 753 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 754 eval "$_precmd $rc_extra_args" 755 _return=$? 756 757 # If precmd failed and force isn't set, request exit. 758 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 759 return 1 760 fi 761 fi 762 763 check_required_after "$rc_arg" || return 1 764 765 return 0 766} 767 768_run_rc_postcmd() 769{ 770 if [ -n "$_postcmd" ]; then 771 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 772 eval "$_postcmd $rc_extra_args" 773 _return=$? 774 fi 775 return 0 776} 777 778_run_rc_doit() 779{ 780 debug "run_rc_command: doit: $*" 781 eval "$@" 782 _return=$? 783 784 # If command failed and force isn't set, request exit. 785 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 786 return 1 787 fi 788 789 return 0 790} 791 792_run_rc_notrunning() 793{ 794 local _pidmsg 795 796 if [ -n "$pidfile" ]; then 797 _pidmsg=" (check $pidfile)." 798 else 799 _pidmsg= 800 fi 801 echo 1>&2 "${name} not running?${_pidmsg}" 802} 803 804_run_rc_killcmd() 805{ 806 local _cmd 807 808 _cmd="kill -$1 $rc_pid" 809 if [ -n "$_user" ]; then 810 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 811 fi 812 echo "$_cmd" 813} 814 815# |
|
832# run_rc_script file arg 833# Start the script `file' with `arg', and correctly handle the 834# return value from the script. If `file' ends with `.sh', it's 835# sourced into the current environment. If `file' appears to be 836# a backup or scratch file, ignore it. Otherwise if it's 837# executable run as a child process. 838# 839run_rc_script() --- 641 unchanged lines hidden (view full) --- 1481 fi 1482 ;; 1483 esac 1484 done 1485 fi 1486 done 1487} 1488 | 816# run_rc_script file arg 817# Start the script `file' with `arg', and correctly handle the 818# return value from the script. If `file' ends with `.sh', it's 819# sourced into the current environment. If `file' appears to be 820# a backup or scratch file, ignore it. Otherwise if it's 821# executable run as a child process. 822# 823run_rc_script() --- 641 unchanged lines hidden (view full) --- 1465 fi 1466 ;; 1467 esac 1468 done 1469 fi 1470 done 1471} 1472 |
1473# check_required_{before|after} command 1474# Check for things required by the command before and after its precmd, 1475# respectively. The two separate functions are needed because some 1476# conditions should prevent precmd from being run while other things 1477# depend on precmd having already been run. 1478# 1479check_required_before() 1480{ 1481 local _f 1482 1483 case "$1" in 1484 start) 1485 for _f in $required_vars; do 1486 if ! checkyesno $_f; then 1487 warn "\$${_f} is not enabled." 1488 if [ -z "$rc_force" ]; then 1489 return 1 1490 fi 1491 fi 1492 done 1493 1494 for _f in $required_dirs; do 1495 if [ ! -d "${_f}/." ]; then 1496 warn "${_f} is not a directory." 1497 if [ -z "$rc_force" ]; then 1498 return 1 1499 fi 1500 fi 1501 done 1502 1503 for _f in $required_files; do 1504 if [ ! -r "${_f}" ]; then 1505 warn "${_f} is not readable." 1506 if [ -z "$rc_force" ]; then 1507 return 1 1508 fi 1509 fi 1510 done 1511 ;; 1512 esac 1513 1514 return 0 1515} 1516 1517check_required_after() 1518{ 1519 local _f _args 1520 1521 case "$1" in 1522 start) 1523 for _f in $required_modules; do 1524 case "${_f}" in 1525 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 1526 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 1527 *) _args="${_f}" ;; 1528 esac 1529 if ! load_kld ${_args}; then 1530 if [ -z "$rc_force" ]; then 1531 return 1 1532 fi 1533 fi 1534 done 1535 ;; 1536 esac 1537 1538 return 0 1539} 1540 |
|
1489fi 1490 1491_rc_subr_loaded=: | 1541fi 1542 1543_rc_subr_loaded=: |