Ticket #4605: dhcpcd-6.0.5-to-6.2.1_lib_dhcpcd_dhcpcd-run-hooks.diff

File dhcpcd-6.0.5-to-6.2.1_lib_dhcpcd_dhcpcd-run-hooks.diff, 3.4 KB (added by Fernando de Oliveira, 10 years ago)

Very different, many init systems to consider, now.

  • lib/dhcpcd/dhcpcd-run-hooks

    old new  
    1818signature_base_end="# End of dhcpcd"
    1919signature_end="$signature_base_end $from $ifname"
    2020state_dir=/var/run/dhcpcd
     21_detected_init=false
    2122
    2223: ${if_up:=false}
    2324: ${if_down:=false}
     
    193194        return 0
    194195}
    195196
     197# With the advent of alternative init systems, it's possible to have
     198# more than one installed. So we need to try and guess what one we're
     199# using unless overriden by configure.
     200detect_init()
     201{
     202        _service_exists=""
     203        _service_cmd=""
     204        _service_status=""
     205
     206        [ -n "$_service_cmd" ] && return 0
     207
     208        if ${_detected_init}; then
     209                [ -n "$_service_cmd" ]
     210                return $?
     211        fi
     212
     213        # Detect the running init system.
     214        # As systemd and OpenRC can be installed on top of legacy init
     215        # systems we try to detect them first.
     216        _service_status=
     217        if [ -x /bin/systemctl -a -S /run/systemd/private ]; then
     218                _service_exists="/bin/systemctl --quiet is-enabled \$1.service"
     219                _service_status="/bin/systemctl --quiet is-active \$1.service"
     220                _service_cmd="/bin/systemctl \$2 \$1.service"
     221        elif [ -x /usr/bin/systemctl -a -S /run/systemd/private ]; then
     222                _service_exists="/usr/bin/systemctl --quiet is-enabled \$1.service"
     223                _service_status="/usr/bin/systemctl --quiet is-active \$1.service"
     224                _service_cmd="/usr/bin/systemctl \$2 \$1.service"
     225        elif [ -x /sbin/rc-service -a \
     226            -s /libexec/rc/init.d/softlevel -o -s /run/openrc/softlevel ]
     227        then
     228                _service_exists="/sbin/rc-service -e \$1"
     229                _service_cmd="/sbin/rc-service \$1 -- -D \$2"
     230        elif [ -x /usr/sbin/invoke-rc.d ]; then
     231                _service_exists="/usr/sbin/invoke-rc.d --query --quiet \$1 start >/dev/null 2>&1 || [ \$? = 104 ]"
     232                _service_cmd="/usr/sbin/invoke-rc.d \$1 \$2"
     233        elif [ -x /sbin/service ]; then
     234                _service_exists="/sbin/service \$1 >/dev/null 2>&1"
     235                _service_cmd="/sbin/service \$1 \$2"
     236        elif [ -e /etc/slackware-version -a -d /etc/rc.d ]; then
     237                _service_exists="[ -x /etc/rc.d/rc.\$1 ]"
     238                _service_cmd="/etc/rc.d/rc.\$1 \$2"
     239                _service_status="/etc/rc.d/rc.\$1 status 1>/dev/null 2>&1"
     240        else
     241                for x in /etc/init.d/rc.d /etc/rc.d /etc/init.d; do
     242                        if [ -d $x ]; then
     243                                _service_exists="[ -x $x/\$1 ]"
     244                                _service_cmd="$x/\$1 \$2"
     245                                break
     246                        fi
     247                done
     248                if [ -e /etc/arch-release ]; then
     249                        _service_status="[ -e /var/run/daemons/\$1 ]"
     250                fi
     251        fi
     252
     253        _detected_init=true
     254        if [ -z "$_service_cmd" ]; then
     255                syslog err "could not detect a useable init system"
     256                return 1
     257        fi
     258        return 0
     259}
     260
    196261# Check a system service exists
    197262service_exists()
    198263{
    199         [ -x /etc/rc.d/$1 ]
     264
     265        if [ -z "$_service_exists" ]; then
     266                detect_init || return 1
     267        fi
     268        eval $_service_exists
    200269}
    201270
    202271# Send a command to a system service
    203272service_cmd()
    204273{
    205         /etc/rc.d/$1 $2
     274
     275        if [ -z "$_service_cmd" ]; then
     276                detect_init || return 1
     277        fi
     278        eval $_service_cmd
    206279}
    207280
    208281# Send a command to a system service if it is running
    209282service_status()
    210283{
    211         service_command $1 status >/dev/null 2>&1
     284
     285        if [ -z "$_service_cmd" ]; then
     286                detect_init || return 1
     287        fi
     288        if [ -n "$_service_status" ]; then
     289                eval $_service_status
     290        else
     291                service_command $1 status >/dev/null 2>&1
     292        fi
    212293}
    213294
    214295# Handy macros for our hooks
    215296service_command()
    216297{
     298
    217299        service_exists $1 && service_cmd $1 $2
    218300}
    219301service_condcommand()
    220302{
     303
    221304        service_exists $1 && service_status $1 && service_cmd $1 $2
    222305}
    223306