summaryrefslogtreecommitdiff
path: root/src/etc/dhcp/dhclient-enter-hooks.d/02-vyos-stopdhclient
blob: f737148dcaf8235f4550d912ea69277df53e1894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# skip all of this if dhclient-script running by stop command defined below
if [ -z ${CONTROLLED_STOP} ] ; then
    # stop dhclient for this interface, if it is not current one
    # get PID for current dhclient
    current_dhclient=`ps --no-headers --format ppid --pid $$ | awk '{ print \$1 }'`

    # get PID for master process (current can be a fork)
    master_dhclient=`ps --no-headers --format ppid --pid $current_dhclient | awk '{ print \$1 }'`

    # get IP version for current dhclient
    ipversion_arg=`ps --no-headers --format args --pid $current_dhclient | awk 'match(\$0, /\s-(4|6)\s/, IPV) { printf("%s", IPV[1]) }'`

    # get list of all dhclient running for current interface
    if [[ $ipversion_arg == "6" ]]; then
        dhclients_pids=(`pgrep -f "dhclient.*\s-6\s.*\s$interface(\s|$)"`)
    else
        dhclients_pids=(`ps --no-headers --format pid,args -C dhclient | awk "{ if(match(\\$0, /\s${interface}(\s|$)/) && !match(\\$0, /\s-6\s/)) printf(\"%s\n\", \\$1) }"`)
    fi

    logmsg info "Current dhclient PID: $current_dhclient, Parent PID: $master_dhclient, IP version: $ipversion_arg, All dhclients for interface $interface: ${dhclients_pids[@]}"
    # stop all dhclients for current interface, except current one
    for dhclient in ${dhclients_pids[@]}; do
        if ([ $dhclient -ne $current_dhclient ] && [ $dhclient -ne $master_dhclient ]); then
            # get path to PID-file of dhclient process
            local dhclient_pidfile=`ps --no-headers --format args --pid $dhclient | awk 'match(\$0, ".*-pf (/.*pid) .*", PF) { print PF[1] }'`
            # stop dhclient with native command - this will run dhclient-script with correct reason unlike simple kill
            logmsg info "Stopping dhclient with PID: ${dhclient}, PID file: $dhclient_pidfile"
            if [[ -e $dhclient_pidfile ]]; then
                dhclient -e CONTROLLED_STOP=yes -x -pf $dhclient_pidfile
            else
                logmsg error "PID file $dhclient_pidfile does not exists, killing dhclient with SIGTERM signal"
                kill -s 15 ${dhclient}
            fi
        fi
    done
fi