[package] base-files, ppp: fix interface shutdown
[openwrt.git] / package / ppp / files / ppp.sh
1 stop_interface_ppp() {
2 local cfg="$1"
3
4 local proto
5 config_get proto "$cfg" proto
6
7 local ifname
8 config_get ifname "$cfg" ifname
9
10 [ -f "/var/run/ppp-${ifname}.pid" ] && {
11 local pid="$(head -n1 /var/run/ppp-${ifname}.pid 2>/dev/null)"
12 grep -qs pppd "/proc/$pid/cmdline" && kill -TERM $pid && \
13 while grep -qs pppd "/proc/$pid/cmdline"; do sleep 1; done
14 rm -f "/var/run/ppp-${ifname}.pid"
15 }
16
17 local lock="/var/lock/ppp-$ifname"
18 [ -f "$lock" ] && lock -u "$lock"
19 }
20
21 start_pppd() {
22 local cfg="$1"; shift
23
24 local proto
25 config_get proto "$cfg" proto
26
27 # unique link identifier
28 local link="${proto:-ppp}-$cfg"
29
30 # make sure only one pppd process is started
31 lock "/var/lock/ppp-${link}"
32 local pid="$(head -n1 /var/run/ppp-${link}.pid 2>/dev/null)"
33 [ -d "/proc/$pid" ] && grep pppd "/proc/$pid/cmdline" 2>/dev/null >/dev/null && {
34 lock -u "/var/lock/ppp-${link}"
35 return 0
36 }
37
38 # Workaround: sometimes hotplug2 doesn't deliver the hotplug event for creating
39 # /dev/ppp fast enough to be used here
40 [ -e /dev/ppp ] || mknod /dev/ppp c 108 0
41
42 local device
43 config_get device "$cfg" device
44
45 local username
46 config_get username "$cfg" username
47
48 local password
49 config_get password "$cfg" password
50
51 local keepalive
52 config_get keepalive "$cfg" keepalive
53
54 local connect
55 config_get connect "$cfg" connect
56
57 local disconnect
58 config_get disconnect "$cfg" disconnect
59
60 local pppd_options
61 config_get pppd_options "$cfg" pppd_options
62
63 local defaultroute
64 config_get_bool defaultroute "$cfg" defaultroute 1
65 [ "$defaultroute" -eq 1 ] && \
66 defaultroute="defaultroute replacedefaultroute" || defaultroute="nodefaultroute"
67
68 local interval="${keepalive##*[, ]}"
69 [ "$interval" != "$keepalive" ] || interval=5
70
71 local dns
72 config_get dns "$config" dns
73
74 local has_dns=0
75 local peer_default=1
76 [ -n "$dns" ] && {
77 has_dns=1
78 peer_default=0
79 }
80
81 local peerdns
82 config_get_bool peerdns "$cfg" peerdns $peer_default
83
84 if [ "$peerdns" -eq 1 ] || [ ! -e /tmp/resolv.conf.auto ]; then
85 echo -n "" > /tmp/resolv.conf.auto
86 fi
87
88 [ "$peerdns" -eq 1 ] && {
89 peerdns="usepeerdns"
90 } || {
91 peerdns=""
92 for dns in $dns; do
93 echo "nameserver $dns" >> /tmp/resolv.conf.auto
94 done
95 }
96
97 local demand
98 config_get_bool demand "$cfg" demand 0
99
100 local demandargs
101 [ "$demand" -eq 1 ] && {
102 demandargs="precompiled-active-filter /etc/ppp/filter demand idle"
103 [ "$has_dns" -eq 0 ] && \
104 echo "nameserver 1.1.1.1" > /tmp/resolv.conf.auto
105 } || {
106 demandargs="persist"
107 }
108
109 local ipv6
110 config_get_bool ipv6 "$cfg" ipv6 0
111 [ "$ipv6" -eq 1 ] && ipv6="+ipv6" || ipv6=""
112
113 /usr/sbin/pppd "$@" \
114 ${keepalive:+lcp-echo-interval $interval lcp-echo-failure ${keepalive%%[, ]*}} \
115 $demandargs \
116 $peerdns \
117 $defaultroute \
118 ${username:+user "$username" password "$password"} \
119 linkname "$link" \
120 ipparam "$cfg" \
121 ifname "$link" \
122 ${connect:+connect "$connect"} \
123 ${disconnect:+disconnect "$disconnect"} \
124 ${ipv6} \
125 ${pppd_options}
126
127 lock -u "/var/lock/ppp-${link}"
128 }
129
130 setup_interface_ppp() {
131 local iface="$1"
132 local config="$2"
133
134 local device
135 config_get device "$config" device
136
137 local mtu
138 config_get mtu "$config" mtu
139
140 mtu=${mtu:-1492}
141 start_pppd "$config" \
142 mtu $mtu mru $mtu \
143 "$device"
144 }
145
This page took 0.050928 seconds and 5 git commands to generate.